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