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