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 | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 19 | |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
| 23 | import com.hazelcast.client.HazelcastClient; |
| 24 | import com.hazelcast.client.config.ClientConfig; |
| 25 | import com.hazelcast.config.Config; |
| 26 | import com.hazelcast.config.FileSystemXmlConfig; |
| 27 | import com.hazelcast.config.MapConfig; |
| 28 | import com.hazelcast.config.SerializationConfig; |
| 29 | import com.hazelcast.core.Hazelcast; |
| 30 | import com.hazelcast.core.HazelcastInstance; |
| 31 | import com.hazelcast.core.IMap; |
| 32 | |
| 33 | public class HZClient implements IKVClient { |
| 34 | private static final Logger log = LoggerFactory.getLogger(HZClient.class); |
| 35 | |
| 36 | static final long VERSION_NONEXISTENT = 0L; |
| 37 | |
| 38 | private static final String MAP_PREFIX = "datastore://"; |
| 39 | |
| 40 | // make this path configurable |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 41 | private static final String BASE_CONFIG_FILENAME = System.getProperty("net.onrc.onos.core.datastore.hazelcast.baseConfig", "conf/hazelcast.xml"); |
| 42 | 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] | 43 | |
| 44 | // Note: xml configuration will overwrite this value if present |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 45 | private static int backupCount = Integer.valueOf(System.getProperty("net.onrc.onos.core.datastore.hazelcast.backupCount", "3")); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 46 | |
| 47 | private final HazelcastInstance hazelcastInstance; |
| 48 | |
| 49 | private static final HZClient THE_INSTANCE = new HZClient(); |
| 50 | |
| 51 | public static HZClient getClient() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 52 | return THE_INSTANCE; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private HZClient() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 56 | hazelcastInstance = getHZinstance(BASE_CONFIG_FILENAME); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | private static HazelcastInstance getHZinstance(final String hazelcastConfigFileName) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 60 | Config baseHzConfig = null; |
| 61 | try { |
| 62 | baseHzConfig = new FileSystemXmlConfig(hazelcastConfigFileName); |
| 63 | } catch (FileNotFoundException e) { |
| 64 | log.error("Error opening Hazelcast XML configuration. File not found: " + hazelcastConfigFileName, e); |
| 65 | throw new Error("Cannot find Hazelcast configuration: " + hazelcastConfigFileName , e); |
| 66 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 67 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 68 | // use xml config if present, if not use System.property |
| 69 | MapConfig mapConfig = baseHzConfig.getMapConfigs().get(MAP_PREFIX + "*"); |
| 70 | if (mapConfig != null) { |
| 71 | backupCount = mapConfig.getBackupCount(); |
| 72 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 73 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 74 | HazelcastInstance instance = null; |
| 75 | if (useClientMode) { |
| 76 | log.info("Configuring Hazelcast datastore as Client mode"); |
| 77 | ClientConfig clientConfig = new ClientConfig(); |
| 78 | final int port = baseHzConfig.getNetworkConfig().getPort(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 79 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 80 | 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] | 81 | clientConfig.addAddress(server + ":" + port); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 82 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 83 | // copy group config from base Hazelcast configuration |
| 84 | clientConfig.getGroupConfig().setName(baseHzConfig.getGroupConfig().getName()); |
| 85 | clientConfig.getGroupConfig().setPassword(baseHzConfig.getGroupConfig().getPassword()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 86 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 87 | // TODO We probably need to figure out what else need to be |
| 88 | // derived from baseConfig |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 89 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 90 | registerSerializer(clientConfig.getSerializationConfig()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 91 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 92 | log.info("Starting Hazelcast datastore client for [{}]", clientConfig.getAddressList()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 93 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 94 | try { |
| 95 | instance = HazelcastClient.newHazelcastClient(clientConfig); |
| 96 | if (!instance.getCluster().getMembers().isEmpty()) { |
| 97 | log.debug("Members in cluster: " + instance.getCluster().getMembers()); |
| 98 | return instance; |
| 99 | } |
| 100 | log.info("Failed to find cluster member, falling back to Instance mode"); |
| 101 | } catch (IllegalStateException e) { |
| 102 | log.info("Failed to initialize HazelcastClient, falling back to Instance mode"); |
| 103 | } |
| 104 | useClientMode = false; |
| 105 | instance = null; |
| 106 | } |
| 107 | log.info("Configuring Hazelcast datastore as Instance mode"); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 108 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 109 | // To run 2 Hazelcast instance in 1 JVM, |
| 110 | // we probably need to something like below |
| 111 | //int port = hazelcastConfig.getNetworkConfig().getPort(); |
| 112 | //hazelcastConfig.getNetworkConfig().setPort(port+1); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 113 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 114 | registerSerializer(baseHzConfig.getSerializationConfig()); |
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 | return Hazelcast.newHazelcastInstance(baseHzConfig); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Register serializer for VersionedValue class used to imitate value version. |
| 121 | * @param config |
| 122 | */ |
| 123 | private static void registerSerializer(final SerializationConfig config) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 124 | config.addDataSerializableFactoryClass( |
| 125 | VersionedValueSerializableFactory.FACTORY_ID, |
| 126 | VersionedValueSerializableFactory.class); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public IKVTable getTable(final String tableName) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 131 | IMap<byte[], VersionedValue> map = hazelcastInstance.getMap(MAP_PREFIX + tableName); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 132 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 133 | if (!useClientMode) { |
| 134 | // config only available in Instance Mode |
| 135 | // Client Mode must rely on hazelcast.xml to be properly configured. |
| 136 | MapConfig config = hazelcastInstance.getConfig().getMapConfig(MAP_PREFIX + tableName); |
| 137 | // config for this map to be strong consistent |
| 138 | if (config.isReadBackupData()) { |
| 139 | config.setReadBackupData(false); |
| 140 | } |
| 141 | if (config.isNearCacheEnabled()) { |
| 142 | config.getNearCacheConfig().setMaxSize(0); |
| 143 | } |
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 | if (config.getBackupCount() != backupCount) { |
| 146 | config.setAsyncBackupCount(0); |
| 147 | config.setBackupCount(backupCount); |
| 148 | } |
| 149 | } |
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 | return new HZTable(tableName, map); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void dropTable(final IKVTable table) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 156 | ((HZTable) table).getBackendMap().clear(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public long create(final IKVTableID tableId, final byte[] key, final byte[] value) |
| 161 | throws ObjectExistsException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 162 | IKVTable table = (IKVTable) tableId; |
| 163 | return table.create(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public long forceCreate(final IKVTableID tableId, final byte[] key, final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 168 | IKVTable table = (IKVTable) tableId; |
| 169 | return table.forceCreate(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public IKVEntry read(final IKVTableID tableId, final byte[] key) |
| 174 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 175 | IKVTable table = (IKVTable) tableId; |
| 176 | return table.read(key); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public long update(final IKVTableID tableId, final byte[] key, final byte[] value, |
| 181 | final long version) throws ObjectDoesntExistException, |
| 182 | WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 183 | IKVTable table = (IKVTable) tableId; |
| 184 | return table.update(key, value, version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public long update(final IKVTableID tableId, final byte[] key, final byte[] value) |
| 189 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 190 | IKVTable table = (IKVTable) tableId; |
| 191 | return table.update(key, value); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public long delete(final IKVTableID tableId, final byte[] key, final long version) |
| 196 | throws ObjectDoesntExistException, WrongVersionException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 197 | IKVTable table = (IKVTable) tableId; |
| 198 | return table.delete(key, version); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public long forceDelete(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 203 | IKVTable table = (IKVTable) tableId; |
| 204 | return table.forceDelete(key); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public Iterable<IKVEntry> getAllEntries(final IKVTableID tableId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 209 | IKVTable table = (IKVTable) tableId; |
| 210 | return table.getAllEntries(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public IMultiEntryOperation createOp(final IKVTableID tableId, final byte[] key, |
| 215 | final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 216 | return new HZMultiEntryOperation((HZTable) tableId, key, value, HZClient.VERSION_NONEXISTENT, OPERATION.CREATE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public IMultiEntryOperation forceCreateOp(final IKVTableID tableId, final byte[] key, |
| 221 | final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 222 | 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] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public IMultiEntryOperation readOp(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 227 | return new HZMultiEntryOperation((HZTable) tableId, key, OPERATION.READ); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public IMultiEntryOperation updateOp(final IKVTableID tableId, final byte[] key, |
| 232 | final byte[] value, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 233 | return new HZMultiEntryOperation((HZTable) tableId, key, value, version, OPERATION.UPDATE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public IMultiEntryOperation deleteOp(final IKVTableID tableId, final byte[] key, |
| 238 | final byte[] value, final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 239 | return new HZMultiEntryOperation((HZTable) tableId, key, value, version, OPERATION.DELETE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public IMultiEntryOperation forceDeleteOp(final IKVTableID tableId, final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 244 | return new HZMultiEntryOperation((HZTable) tableId, key, OPERATION.FORCE_DELETE); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public boolean multiDelete(final Collection<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 249 | boolean failExists = false; |
| 250 | for (IMultiEntryOperation op : ops) { |
| 251 | HZMultiEntryOperation mop = (HZMultiEntryOperation) op; |
| 252 | switch (mop.getOperation()) { |
| 253 | case DELETE: |
| 254 | try { |
| 255 | final long version = delete(mop.getTableId(), mop.getKey(), mop.getVersion()); |
| 256 | mop.setVersion(version); |
| 257 | mop.setStatus(STATUS.SUCCESS); |
| 258 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 259 | log.error(mop + " failed.", e); |
| 260 | mop.setStatus(STATUS.FAILED); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 261 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 262 | } |
| 263 | break; |
| 264 | case FORCE_DELETE: |
| 265 | final long version = forceDelete(mop.getTableId(), mop.getKey()); |
| 266 | mop.setVersion(version); |
| 267 | mop.setStatus(STATUS.SUCCESS); |
| 268 | break; |
| 269 | default: |
| 270 | throw new UnsupportedOperationException(mop.toString()); |
| 271 | } |
| 272 | } |
| 273 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public boolean multiWrite(final List<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 278 | // there may be room to batch to improve performance |
| 279 | boolean failExists = false; |
| 280 | for (IMultiEntryOperation op : ops) { |
| 281 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
| 282 | switch (mop.getOperation()) { |
| 283 | case CREATE: |
| 284 | try { |
| 285 | long version = create(mop.getTableId(), mop.getKey(), mop.getValue()); |
| 286 | mop.setVersion(version); |
| 287 | mop.setStatus(STATUS.SUCCESS); |
| 288 | } catch (ObjectExistsException e) { |
| 289 | log.error(mop + " failed.", e); |
| 290 | mop.setStatus(STATUS.FAILED); |
| 291 | failExists = true; |
| 292 | } |
| 293 | break; |
| 294 | case FORCE_CREATE: |
| 295 | { |
| 296 | final long version = forceCreate(mop.getTableId(), mop.getKey(), mop.getValue()); |
| 297 | mop.setVersion(version); |
| 298 | mop.setStatus(STATUS.SUCCESS); |
| 299 | break; |
| 300 | } |
| 301 | case UPDATE: |
| 302 | try { |
| 303 | long version = update(mop.getTableId(), mop.getKey(), mop.getValue(), mop.getVersion()); |
| 304 | mop.setVersion(version); |
| 305 | mop.setStatus(STATUS.SUCCESS); |
| 306 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 307 | log.error(mop + " failed.", e); |
| 308 | mop.setStatus(STATUS.FAILED); |
| 309 | failExists = true; |
| 310 | } |
| 311 | break; |
| 312 | default: |
| 313 | throw new UnsupportedOperationException(mop.toString()); |
| 314 | } |
| 315 | } |
| 316 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public boolean multiRead(final Collection<IMultiEntryOperation> ops) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 321 | boolean failExists = false; |
| 322 | for (IMultiEntryOperation op : ops) { |
| 323 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
| 324 | HZTable table = (HZTable) op.getTableId(); |
| 325 | ((HZMultiEntryOperation) mop.getActualOperation()).setFuture(table.getBackendMap().getAsync(op.getKey())); |
| 326 | } |
| 327 | for (IMultiEntryOperation op : ops) { |
| 328 | IModifiableMultiEntryOperation mop = (IModifiableMultiEntryOperation) op; |
| 329 | if (mop.hasSucceeded()) { |
| 330 | // status update is already done, nothing to do. |
| 331 | } else { |
| 332 | failExists = true; |
| 333 | } |
| 334 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 335 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 336 | return failExists; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public long VERSION_NONEXISTENT() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 341 | return VERSION_NONEXISTENT; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
| 345 | } |