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