Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.hazelcast; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.FileNotFoundException; |
| 4 | import java.util.Collection; |
| 5 | import java.util.List; |
| 6 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 7 | import net.onrc.onos.core.datastore.IKVClient; |
| 8 | import net.onrc.onos.core.datastore.IKVTable; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.datastore.IKVTableID; |
| 11 | import net.onrc.onos.core.datastore.IMultiEntryOperation; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.datastore.IMultiEntryOperation.OPERATION; |
| 13 | import net.onrc.onos.core.datastore.IMultiEntryOperation.STATUS; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.datastore.ObjectDoesntExistException; |
| 15 | import net.onrc.onos.core.datastore.ObjectExistsException; |
| 16 | import net.onrc.onos.core.datastore.WrongVersionException; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.datastore.hazelcast.HZTable.VersionedValue; |
| 18 | import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.datastore.utils.ByteArrayUtil; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 20 | |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import com.hazelcast.client.HazelcastClient; |
| 25 | import com.hazelcast.client.config.ClientConfig; |
| 26 | import com.hazelcast.config.Config; |
| 27 | import com.hazelcast.config.FileSystemXmlConfig; |
| 28 | import com.hazelcast.config.MapConfig; |
| 29 | import com.hazelcast.config.SerializationConfig; |
| 30 | import com.hazelcast.core.Hazelcast; |
| 31 | import com.hazelcast.core.HazelcastInstance; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 32 | import com.hazelcast.core.IAtomicLong; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 33 | import com.hazelcast.core.IMap; |
| 34 | |
Ray Milkey | 1584ec8 | 2014-04-10 11:58:30 -0700 | [diff] [blame] | 35 | public final class HZClient implements IKVClient { |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 36 | private static final Logger log = LoggerFactory.getLogger(HZClient.class); |
| 37 | |
| 38 | static final long VERSION_NONEXISTENT = 0L; |
| 39 | |
| 40 | private static final String MAP_PREFIX = "datastore://"; |
| 41 | |
| 42 | // make this path configurable |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 43 | private static final String BASE_CONFIG_FILENAME = System.getProperty("net.onrc.onos.core.datastore.hazelcast.baseConfig", "conf/hazelcast.xml"); |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 44 | private static final String HAZELCAST_DEFAULT_XML = "conf/hazelcast.default.xml"; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 45 | private static boolean useClientMode = Boolean.parseBoolean(System.getProperty("net.onrc.onos.core.datastore.hazelcast.clientMode", "true")); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 46 | |
| 47 | // Note: xml configuration will overwrite this value if present |
Yuta HIGUCHI | 0fe749a | 2014-05-27 09:35:16 -0700 | [diff] [blame] | 48 | private static int backupCount = Integer.parseInt(System.getProperty("net.onrc.onos.core.datastore.hazelcast.backupCount", "3")); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 49 | |
| 50 | private final HazelcastInstance hazelcastInstance; |
| 51 | |
| 52 | private static final HZClient THE_INSTANCE = new HZClient(); |
| 53 | |
| 54 | public static HZClient getClient() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 55 | return THE_INSTANCE; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | private HZClient() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 59 | hazelcastInstance = getHZinstance(BASE_CONFIG_FILENAME); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private static HazelcastInstance getHZinstance(final String hazelcastConfigFileName) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 63 | Config baseHzConfig = null; |
| 64 | try { |
| 65 | baseHzConfig = new FileSystemXmlConfig(hazelcastConfigFileName); |
| 66 | } catch (FileNotFoundException e) { |
| 67 | log.error("Error opening Hazelcast XML configuration. File not found: " + hazelcastConfigFileName, e); |
Yuta HIGUCHI | 3ebc948 | 2014-05-08 16:28:28 -0700 | [diff] [blame] | 68 | // Fallback mechanism to support running unit test without setup. |
| 69 | log.error("Falling back to default Hazelcast XML {}", HAZELCAST_DEFAULT_XML); |
| 70 | try { |
| 71 | baseHzConfig = new FileSystemXmlConfig(HAZELCAST_DEFAULT_XML); |
| 72 | } catch (FileNotFoundException e2) { |
| 73 | log.error("Error opening fall back Hazelcast XML configuration. " |
| 74 | + "File not found: " + HAZELCAST_DEFAULT_XML, e2); |
| 75 | |
| 76 | // intentionally throwing Exception "e" thrown from non-fallback |
| 77 | // Hazelcast configuration loading. |
| 78 | throw new Error("Cannot find Hazelcast configuration: " + hazelcastConfigFileName, e); |
| 79 | } |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 80 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 81 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 82 | // use xml config if present, if not use System.property |
| 83 | MapConfig mapConfig = baseHzConfig.getMapConfigs().get(MAP_PREFIX + "*"); |
| 84 | if (mapConfig != null) { |
| 85 | backupCount = mapConfig.getBackupCount(); |
| 86 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 87 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 88 | HazelcastInstance instance = null; |
| 89 | if (useClientMode) { |
| 90 | log.info("Configuring Hazelcast datastore as Client mode"); |
| 91 | ClientConfig clientConfig = new ClientConfig(); |
| 92 | final int port = baseHzConfig.getNetworkConfig().getPort(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 93 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 94 | String server = System.getProperty("net.onrc.onos.core.datastore.hazelcast.client.server", "localhost"); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 95 | clientConfig.addAddress(server + ":" + port); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 96 | |
Yuta HIGUCHI | ceb21b6 | 2014-04-17 15:46:05 -0700 | [diff] [blame] | 97 | // client mode connection limit. |
| 98 | // set to 0 for fast fall back to Instance mode. |
| 99 | String sAttempts = System.getProperty("net.onrc.onos.core.datastore.hazelcast.client.attemptLimit"); |
| 100 | if (sAttempts != null) { |
Yuta HIGUCHI | 0fe749a | 2014-05-27 09:35:16 -0700 | [diff] [blame] | 101 | clientConfig.setConnectionAttemptLimit(Integer.parseInt(sAttempts)); |
Yuta HIGUCHI | ceb21b6 | 2014-04-17 15:46:05 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 104 | // copy group config from base Hazelcast configuration |
| 105 | clientConfig.getGroupConfig().setName(baseHzConfig.getGroupConfig().getName()); |
| 106 | clientConfig.getGroupConfig().setPassword(baseHzConfig.getGroupConfig().getPassword()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 107 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 108 | // TODO We probably need to figure out what else need to be |
| 109 | // derived from baseConfig |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 110 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 111 | registerSerializer(clientConfig.getSerializationConfig()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 112 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 113 | log.info("Starting Hazelcast datastore client for [{}]", clientConfig.getAddressList()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 114 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 115 | try { |
| 116 | instance = HazelcastClient.newHazelcastClient(clientConfig); |
| 117 | if (!instance.getCluster().getMembers().isEmpty()) { |
| 118 | log.debug("Members in cluster: " + instance.getCluster().getMembers()); |
| 119 | return instance; |
| 120 | } |
| 121 | log.info("Failed to find cluster member, falling back to Instance mode"); |
| 122 | } catch (IllegalStateException e) { |
| 123 | log.info("Failed to initialize HazelcastClient, falling back to Instance mode"); |
| 124 | } |
| 125 | useClientMode = false; |
| 126 | instance = null; |
| 127 | } |
| 128 | log.info("Configuring Hazelcast datastore as Instance mode"); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 129 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 130 | // To run 2 Hazelcast instance in 1 JVM, |
| 131 | // we probably need to something like below |
| 132 | //int port = hazelcastConfig.getNetworkConfig().getPort(); |
| 133 | //hazelcastConfig.getNetworkConfig().setPort(port+1); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 134 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 135 | registerSerializer(baseHzConfig.getSerializationConfig()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 136 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 137 | return Hazelcast.newHazelcastInstance(baseHzConfig); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Register serializer for VersionedValue class used to imitate value version. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 143 | * @param config |
| 144 | */ |
| 145 | private static void registerSerializer(final SerializationConfig config) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 146 | config.addDataSerializableFactoryClass( |
| 147 | VersionedValueSerializableFactory.FACTORY_ID, |
| 148 | VersionedValueSerializableFactory.class); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public IKVTable getTable(final String tableName) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 153 | IMap<byte[], VersionedValue> map = hazelcastInstance.getMap(MAP_PREFIX + tableName); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 154 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 155 | if (!useClientMode) { |
| 156 | // config only available in Instance Mode |
| 157 | // Client Mode must rely on hazelcast.xml to be properly configured. |
| 158 | MapConfig config = hazelcastInstance.getConfig().getMapConfig(MAP_PREFIX + tableName); |
| 159 | // config for this map to be strong consistent |
| 160 | if (config.isReadBackupData()) { |
| 161 | config.setReadBackupData(false); |
| 162 | } |
| 163 | if (config.isNearCacheEnabled()) { |
| 164 | config.getNearCacheConfig().setMaxSize(0); |
| 165 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 166 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 167 | if (config.getBackupCount() != backupCount) { |
| 168 | config.setAsyncBackupCount(0); |
| 169 | config.setBackupCount(backupCount); |
| 170 | } |
| 171 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 172 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 173 | return new HZTable(tableName, map); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void dropTable(final IKVTable table) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 178 | ((HZTable) table).getBackendMap().clear(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public long create(final IKVTableID tableId, final byte[] key, final byte[] value) |
| 183 | throws ObjectExistsException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 184 | IKVTable table = (IKVTable) tableId; |
| 185 | return table.create(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public long forceCreate(final IKVTableID tableId, final byte[] key, final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 190 | IKVTable table = (IKVTable) tableId; |
| 191 | return table.forceCreate(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public IKVEntry read(final IKVTableID tableId, final byte[] key) |
| 196 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 197 | IKVTable table = (IKVTable) tableId; |
| 198 | return table.read(key); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public long update(final IKVTableID tableId, final byte[] key, final byte[] value, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 203 | final long version) throws ObjectDoesntExistException, |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 204 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 205 | IKVTable table = (IKVTable) tableId; |
| 206 | return table.update(key, value, version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public long update(final IKVTableID tableId, final byte[] key, final byte[] value) |
| 211 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 212 | IKVTable table = (IKVTable) tableId; |
| 213 | return table.update(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public long delete(final IKVTableID tableId, final byte[] key, final long version) |
| 218 | throws ObjectDoesntExistException, WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 219 | IKVTable table = (IKVTable) tableId; |
| 220 | return table.delete(key, version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public long forceDelete(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 225 | IKVTable table = (IKVTable) tableId; |
| 226 | return table.forceDelete(key); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Iterable<IKVEntry> getAllEntries(final IKVTableID tableId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 231 | IKVTable table = (IKVTable) tableId; |
| 232 | return table.getAllEntries(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public IMultiEntryOperation createOp(final IKVTableID tableId, final byte[] key, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 238 | return new HZMultiEntryOperation((HZTable) tableId, key, value, HZClient.VERSION_NONEXISTENT, OPERATION.CREATE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public IMultiEntryOperation forceCreateOp(final IKVTableID tableId, final byte[] key, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 243 | final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 244 | return new HZMultiEntryOperation((HZTable) tableId, key, value, HZClient.VERSION_NONEXISTENT, OPERATION.FORCE_CREATE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public IMultiEntryOperation readOp(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 249 | return new HZMultiEntryOperation((HZTable) tableId, key, OPERATION.READ); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public IMultiEntryOperation updateOp(final IKVTableID tableId, final byte[] key, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 254 | final byte[] value, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 255 | return new HZMultiEntryOperation((HZTable) tableId, key, value, version, OPERATION.UPDATE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public IMultiEntryOperation deleteOp(final IKVTableID tableId, final byte[] key, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 260 | final byte[] value, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 261 | return new HZMultiEntryOperation((HZTable) tableId, key, value, version, OPERATION.DELETE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public IMultiEntryOperation forceDeleteOp(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 266 | return new HZMultiEntryOperation((HZTable) tableId, key, OPERATION.FORCE_DELETE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public boolean multiDelete(final Collection<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 271 | boolean failExists = false; |
| 272 | for (IMultiEntryOperation op : ops) { |
| 273 | HZMultiEntryOperation mop = (HZMultiEntryOperation) op; |
| 274 | switch (mop.getOperation()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 275 | case DELETE: |
| 276 | try { |
| 277 | final long version = delete(mop.getTableId(), mop.getKey(), mop.getVersion()); |
| 278 | mop.setVersion(version); |
| 279 | mop.setStatus(STATUS.SUCCESS); |
| 280 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 281 | log.error(mop + " failed.", e); |
| 282 | mop.setStatus(STATUS.FAILED); |
| 283 | failExists = true; |
| 284 | } |
| 285 | break; |
| 286 | case FORCE_DELETE: |
| 287 | final long version = forceDelete(mop.getTableId(), mop.getKey()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 288 | mop.setVersion(version); |
| 289 | mop.setStatus(STATUS.SUCCESS); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 290 | break; |
| 291 | default: |
| 292 | throw new UnsupportedOperationException(mop.toString()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public boolean multiWrite(final List<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 300 | // there may be room to batch to improve performance |
| 301 | boolean failExists = false; |
| 302 | for (IMultiEntryOperation op : ops) { |
| 303 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
| 304 | switch (mop.getOperation()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 305 | case CREATE: |
| 306 | try { |
| 307 | long version = create(mop.getTableId(), mop.getKey(), mop.getValue()); |
| 308 | mop.setVersion(version); |
| 309 | mop.setStatus(STATUS.SUCCESS); |
| 310 | } catch (ObjectExistsException e) { |
| 311 | log.error(mop + " failed.", e); |
| 312 | mop.setStatus(STATUS.FAILED); |
| 313 | failExists = true; |
| 314 | } |
| 315 | break; |
| 316 | case FORCE_CREATE: { |
| 317 | final long version = forceCreate(mop.getTableId(), mop.getKey(), mop.getValue()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 318 | mop.setVersion(version); |
| 319 | mop.setStatus(STATUS.SUCCESS); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 320 | break; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 321 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 322 | case UPDATE: |
| 323 | try { |
| 324 | long version = update(mop.getTableId(), mop.getKey(), mop.getValue(), mop.getVersion()); |
| 325 | mop.setVersion(version); |
| 326 | mop.setStatus(STATUS.SUCCESS); |
| 327 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 328 | log.error(mop + " failed.", e); |
| 329 | mop.setStatus(STATUS.FAILED); |
| 330 | failExists = true; |
| 331 | } |
| 332 | break; |
| 333 | default: |
| 334 | throw new UnsupportedOperationException(mop.toString()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public boolean multiRead(final Collection<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 342 | boolean failExists = false; |
| 343 | for (IMultiEntryOperation op : ops) { |
| 344 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
| 345 | HZTable table = (HZTable) op.getTableId(); |
| 346 | ((HZMultiEntryOperation) mop.getActualOperation()).setFuture(table.getBackendMap().getAsync(op.getKey())); |
| 347 | } |
| 348 | for (IMultiEntryOperation op : ops) { |
| 349 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 350 | if (!mop.hasSucceeded()) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 351 | failExists = true; |
| 352 | } |
| 353 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 354 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 355 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 359 | public long getVersionNonexistant() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 360 | return VERSION_NONEXISTENT; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 363 | private String getCounterName(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 364 | StringBuilder buf = new StringBuilder(tableId.getTableName()); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 365 | buf.append('@'); |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 366 | ByteArrayUtil.toHexStringBuilder(key, ":", buf); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 367 | return buf.toString(); |
| 368 | } |
| 369 | |
| 370 | private IAtomicLong getAtomicLong(final IKVTableID tableId, final byte[] key) { |
| 371 | // TODO we probably want to implement some sort of caching |
| 372 | return hazelcastInstance.getAtomicLong(getCounterName(tableId, key)); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * {@inheritDoc} |
| 377 | * <p /> |
| 378 | * Warning: The counter is a different object from {@code key} entry on |
| 379 | * IKVTable with {@code tableId}. You cannot use table API to read/write |
| 380 | * counters. |
| 381 | * |
| 382 | * @param tableId Only getTableName() will be used. |
| 383 | * @param key tableId + key will be used as Counter name |
| 384 | */ |
| 385 | @Override |
| 386 | public void createCounter(final IKVTableID tableId, |
| 387 | final byte[] key, final long initialValue) |
| 388 | throws ObjectExistsException { |
| 389 | |
| 390 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 391 | // Assumption here is that AtomicLong is initialized to 0L |
| 392 | final boolean success = counter.compareAndSet(0L, initialValue); |
| 393 | if (!success) { |
| 394 | throw new ObjectExistsException("Atomic counter " |
| 395 | + getCounterName(tableId, key) |
| 396 | + " already exist with value:" + counter.get()); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public void setCounter(final IKVTableID tableId, |
| 402 | final byte[] key, final long value) { |
| 403 | |
| 404 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 405 | counter.set(value); |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public long incrementCounter(final IKVTableID tableId, |
| 410 | final byte[] key, final long incrementValue) { |
| 411 | |
| 412 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 413 | return counter.addAndGet(incrementValue); |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public void destroyCounter(final IKVTableID tableId, final byte[] key) { |
| 418 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 419 | counter.destroy(); |
| 420 | |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public long getCounter(final IKVTableID tableId, final byte[] key) |
| 425 | throws ObjectDoesntExistException { |
| 426 | |
| 427 | IAtomicLong counter = getAtomicLong(tableId, key); |
| 428 | return counter.get(); |
| 429 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 430 | |
| 431 | } |