Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 1 | package net.onrc.onos.datastore.ramcloud; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Arrays; |
| 6 | import java.util.Collection; |
| 7 | import java.util.Iterator; |
| 8 | import java.util.List; |
| 9 | import java.util.concurrent.ConcurrentHashMap; |
| 10 | |
| 11 | import net.onrc.onos.datastore.IKVClient; |
| 12 | import net.onrc.onos.datastore.IKVTable; |
| 13 | import net.onrc.onos.datastore.IKVTable.IKVEntry; |
| 14 | import net.onrc.onos.datastore.IKVTableID; |
| 15 | import net.onrc.onos.datastore.IMultiEntryOperation; |
| 16 | import net.onrc.onos.datastore.IMultiEntryOperation.STATUS; |
| 17 | import net.onrc.onos.datastore.ObjectDoesntExistException; |
| 18 | import net.onrc.onos.datastore.ObjectExistsException; |
| 19 | import net.onrc.onos.datastore.WrongVersionException; |
| 20 | import net.onrc.onos.datastore.internal.IModifiableMultiEntryOperation; |
| 21 | import net.onrc.onos.datastore.ramcloud.RCTable.Entry; |
| 22 | import net.onrc.onos.datastore.utils.ByteArrayUtil; |
| 23 | |
| 24 | import org.apache.commons.configuration.Configuration; |
| 25 | import org.apache.commons.configuration.ConfigurationException; |
| 26 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import edu.stanford.ramcloud.JRamCloud; |
| 31 | import edu.stanford.ramcloud.JRamCloud.MultiReadObject; |
| 32 | import edu.stanford.ramcloud.JRamCloud.MultiWriteObject; |
| 33 | import edu.stanford.ramcloud.JRamCloud.MultiWriteRspObject; |
| 34 | import edu.stanford.ramcloud.JRamCloud.RejectRules; |
| 35 | import edu.stanford.ramcloud.JRamCloud.RejectRulesException; |
| 36 | import edu.stanford.ramcloud.JRamCloud.TableEnumerator2; |
| 37 | |
| 38 | public class RCClient implements IKVClient { |
| 39 | private static final Logger log = LoggerFactory.getLogger(RCClient.class); |
| 40 | |
| 41 | private static final String DB_CONFIG_FILE = "conf/ramcloud.conf"; |
| 42 | public static final Configuration config = getConfiguration(); |
| 43 | |
| 44 | // Value taken from RAMCloud's Status.h |
| 45 | // FIXME These constants should be defined by JRamCloud |
| 46 | public static final int STATUS_OK = 0; |
| 47 | |
| 48 | // FIXME come up with a proper way to retrieve configuration |
| 49 | public static final int MAX_MULTI_READS = Math.max(1, Integer |
| 50 | .valueOf(System.getProperty("ramcloud.max_multi_reads", "400"))); |
| 51 | |
| 52 | public static final int MAX_MULTI_WRITES = Math.max(1, Integer |
| 53 | .valueOf(System.getProperty("ramcloud.max_multi_writes", "800"))); |
| 54 | |
| 55 | private static final ThreadLocal<JRamCloud> tlsRCClient = new ThreadLocal<JRamCloud>() { |
| 56 | @Override |
| 57 | protected JRamCloud initialValue() { |
| 58 | return new JRamCloud(getCoordinatorUrl(config)); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * @return JRamCloud instance intended to be used only within the |
| 64 | * SameThread. |
| 65 | * @note Do not store the returned instance in a member variable, etc. which |
| 66 | * may be accessed later by another thread. |
| 67 | */ |
| 68 | static JRamCloud getJRamCloudClient() { |
| 69 | return tlsRCClient.get(); |
| 70 | } |
| 71 | |
| 72 | // Currently RCClient is state-less |
| 73 | private static final RCClient theInstance= new RCClient(); |
| 74 | |
| 75 | public static RCClient getClient() { |
| 76 | return theInstance; |
| 77 | } |
| 78 | |
| 79 | public static final Configuration getConfiguration() { |
| 80 | final File configFile = new File(System.getProperty("ramcloud.config.path", DB_CONFIG_FILE)); |
| 81 | return getConfiguration(configFile); |
| 82 | } |
| 83 | |
| 84 | public static final Configuration getConfiguration(final File configFile) { |
| 85 | if (configFile == null) { |
| 86 | throw new IllegalArgumentException("Need to specify a configuration file or storage directory"); |
| 87 | } |
| 88 | |
| 89 | if (!configFile.isFile()) { |
| 90 | throw new IllegalArgumentException("Location of configuration must be a file"); |
| 91 | } |
| 92 | |
| 93 | try { |
| 94 | return new PropertiesConfiguration(configFile); |
| 95 | } catch (ConfigurationException e) { |
| 96 | throw new IllegalArgumentException("Could not load configuration at: " + configFile, e); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public static String getCoordinatorUrl(final Configuration configuration) { |
| 101 | final String coordinatorIp = configuration.getString("ramcloud.coordinatorIp", "fast+udp:host=127.0.0.1"); |
| 102 | final String coordinatorPort = configuration.getString("ramcloud.coordinatorPort", "port=12246"); |
| 103 | final String coordinatorURL = coordinatorIp + "," + coordinatorPort; |
| 104 | return coordinatorURL; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public IMultiEntryOperation createOp(IKVTableID tableId, byte[] key, byte[] value) { |
| 109 | return RCMultiEntryOperation.create(tableId, key, value); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param tableId RCTableID instance |
| 114 | */ |
| 115 | @Override |
| 116 | public long create(IKVTableID tableId, byte[] key, byte[] value) |
| 117 | throws ObjectExistsException { |
| 118 | |
| 119 | RCTableID rcTableId = (RCTableID) tableId; |
| 120 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 121 | |
| 122 | RejectRules rules = new RejectRules(); |
| 123 | rules.rejectIfExists(); |
| 124 | |
| 125 | try { |
| 126 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 127 | } catch (JRamCloud.ObjectExistsException e) { |
| 128 | throw new ObjectExistsException(rcTableId, key, e); |
| 129 | } catch (JRamCloud.RejectRulesException e) { |
| 130 | log.error("Unexpected RejectRulesException", e); |
| 131 | return JRamCloud.VERSION_NONEXISTENT; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public IMultiEntryOperation forceCreateOp(IKVTableID tableId, byte[] key, byte[] value) { |
| 137 | return RCMultiEntryOperation.forceCreate(tableId, key, value); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public long forceCreate(IKVTableID tableId, byte[] key, byte[] value) { |
| 142 | RCTableID rcTableId = (RCTableID) tableId; |
| 143 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 144 | |
| 145 | long updated_version = rcClient.write(rcTableId.getTableID(), key, value); |
| 146 | return updated_version; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public IMultiEntryOperation readOp(IKVTableID tableId, byte[] key) { |
| 151 | return RCMultiEntryOperation.read(tableId, key); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public IKVEntry read(IKVTableID tableId, byte[] key) |
| 156 | throws ObjectDoesntExistException { |
| 157 | |
| 158 | RCTableID rcTableId = (RCTableID) tableId; |
| 159 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 160 | |
| 161 | RejectRules rules = new RejectRules(); |
| 162 | rules.rejectIfDoesntExists(); |
| 163 | try { |
| 164 | JRamCloud.Object rcObj = rcClient.read(rcTableId.getTableID(), key, rules); |
| 165 | return new Entry(rcObj.key, rcObj.value, rcObj.version); |
| 166 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 167 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 168 | } catch (JRamCloud.RejectRulesException e) { |
| 169 | log.error("Unexpected RejectRulesException", e); |
| 170 | return null; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public IMultiEntryOperation updateOp(IKVTableID tableId, byte[] key, byte[] value,long version) { |
| 176 | return RCMultiEntryOperation.update(tableId, key, value, version); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public long update(IKVTableID tableId, byte[] key, byte[] value, |
| 181 | long version) throws ObjectDoesntExistException, |
| 182 | WrongVersionException { |
| 183 | |
| 184 | RCTableID rcTableId = (RCTableID) tableId; |
| 185 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 186 | |
| 187 | RejectRules rules = new RejectRules(); |
| 188 | rules.rejectIfDoesntExists(); |
| 189 | rules.rejectIfNeVersion(version); |
| 190 | |
| 191 | try { |
| 192 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 193 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 194 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 195 | } catch (JRamCloud.WrongVersionException e) { |
| 196 | throw new WrongVersionException(rcTableId, key, version, e); |
| 197 | } catch (JRamCloud.RejectRulesException e) { |
| 198 | log.error("Unexpected RejectRulesException", e); |
| 199 | return JRamCloud.VERSION_NONEXISTENT; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |
| 204 | @Override |
| 205 | public long update(IKVTableID tableId, byte[] key, byte[] value) |
| 206 | throws ObjectDoesntExistException { |
| 207 | |
| 208 | RCTableID rcTableId = (RCTableID) tableId; |
| 209 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 210 | |
| 211 | RejectRules rules = new RejectRules(); |
| 212 | rules.rejectIfDoesntExists(); |
| 213 | |
| 214 | try { |
| 215 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 216 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 217 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 218 | } catch (JRamCloud.RejectRulesException e) { |
| 219 | log.error("Unexpected RejectRulesException", e); |
| 220 | return JRamCloud.VERSION_NONEXISTENT; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public IMultiEntryOperation deleteOp(IKVTableID tableId, byte[] key, byte[] value,long version) { |
| 226 | return RCMultiEntryOperation.delete(tableId, key, value, version); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public long delete(IKVTableID tableId, byte[] key, long version) |
| 231 | throws ObjectDoesntExistException, WrongVersionException { |
| 232 | |
| 233 | RCTableID rcTableId = (RCTableID) tableId; |
| 234 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 235 | |
| 236 | RejectRules rules = new RejectRules(); |
| 237 | rules.rejectIfDoesntExists(); |
| 238 | rules.rejectIfNeVersion(version); |
| 239 | |
| 240 | try { |
| 241 | return rcClient.remove(rcTableId.getTableID(), key, rules); |
| 242 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 243 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 244 | } catch (JRamCloud.WrongVersionException e) { |
| 245 | throw new WrongVersionException(rcTableId, key, version, e); |
| 246 | } catch (JRamCloud.RejectRulesException e) { |
| 247 | log.error("Unexpected RejectRulesException", e); |
| 248 | return JRamCloud.VERSION_NONEXISTENT; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public IMultiEntryOperation forceDeleteOp(IKVTableID tableId, byte[] key) { |
| 254 | return RCMultiEntryOperation.forceDelete(tableId, key); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public long forceDelete(IKVTableID tableId, byte[] key) { |
| 259 | RCTableID rcTableId = (RCTableID) tableId; |
| 260 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 261 | long removed_version = rcClient.remove(rcTableId.getTableID(), key); |
| 262 | return removed_version; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public Iterable<IKVEntry> getAllEntries(IKVTableID tableId) { |
| 267 | return new RCTableEntryIterable((RCTableID) tableId); |
| 268 | } |
| 269 | |
| 270 | static class RCTableEntryIterable implements Iterable<IKVEntry> { |
| 271 | private final RCTableID tableId; |
| 272 | |
| 273 | public RCTableEntryIterable(final RCTableID tableId) { |
| 274 | this.tableId = tableId; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public Iterator<IKVEntry> iterator() { |
| 279 | return new RCClient.RCTableIterator(tableId); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | public static class RCTableIterator implements Iterator<IKVEntry> { |
| 284 | private final RCTableID tableId; |
| 285 | protected final TableEnumerator2 enumerator; |
| 286 | private JRamCloud.Object last; |
| 287 | |
| 288 | public RCTableIterator(final RCTableID tableId) { |
| 289 | this.tableId = tableId; |
| 290 | this.enumerator = getJRamCloudClient().new TableEnumerator2(tableId.getTableID()); |
| 291 | this.last = null; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public boolean hasNext() { |
| 296 | return this.enumerator.hasNext(); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public RCTable.Entry next() { |
| 301 | last = enumerator.next(); |
| 302 | return new RCTable.Entry(last.key, last.value, last.version); |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public void remove() { |
| 307 | if (last != null) { |
| 308 | getJRamCloudClient(); |
| 309 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 310 | |
| 311 | RejectRules rules = new RejectRules(); |
| 312 | rules.rejectIfNeVersion(last.version); |
| 313 | try { |
| 314 | rcClient.remove(tableId.getTableID(), last.key, rules); |
| 315 | } catch (RejectRulesException e) { |
| 316 | log.trace("remove failed", e); |
| 317 | } |
| 318 | last = null; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public boolean multiRead(final Collection<IMultiEntryOperation> ops) { |
| 325 | |
| 326 | if ( ops.size() <= MAX_MULTI_READS && ops instanceof ArrayList) { |
| 327 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 328 | final ArrayList<RCMultiEntryOperation> arrays = (ArrayList)ops; |
| 329 | return multiReadInternal(arrays); |
| 330 | } |
| 331 | |
| 332 | boolean fail_exists = false; |
| 333 | |
| 334 | ArrayList<RCMultiEntryOperation> req = new ArrayList<>(); |
| 335 | Iterator<IMultiEntryOperation> it = ops.iterator(); |
| 336 | while (it.hasNext()) { |
| 337 | |
| 338 | req.add((RCMultiEntryOperation) it.next()); |
| 339 | |
| 340 | if (req.size() >= MAX_MULTI_READS) { |
| 341 | // dispatch multiRead |
| 342 | fail_exists |= multiReadInternal(req); |
| 343 | req.clear(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (!req.isEmpty()) { |
| 348 | // dispatch multiRead |
| 349 | fail_exists |= multiReadInternal(req); |
| 350 | req.clear(); |
| 351 | } |
| 352 | |
| 353 | return fail_exists; |
| 354 | } |
| 355 | |
| 356 | @Override |
| 357 | public boolean multiWrite(final List<IMultiEntryOperation> ops) { |
| 358 | |
| 359 | if ( ops.size() <= MAX_MULTI_WRITES && ops instanceof ArrayList) { |
| 360 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 361 | final ArrayList<RCMultiEntryOperation> arrays = (ArrayList)ops; |
| 362 | return multiWriteInternal(arrays); |
| 363 | } |
| 364 | |
| 365 | boolean fail_exists = false; |
| 366 | |
| 367 | ArrayList<RCMultiEntryOperation> req = new ArrayList<>(); |
| 368 | Iterator<IMultiEntryOperation> it = ops.iterator(); |
| 369 | while (it.hasNext()) { |
| 370 | |
| 371 | req.add((RCMultiEntryOperation) it.next()); |
| 372 | |
| 373 | if (req.size() >= MAX_MULTI_WRITES) { |
| 374 | // dispatch multiWrite |
| 375 | fail_exists |= multiWriteInternal(req); |
| 376 | req.clear(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if (!req.isEmpty()) { |
| 381 | // dispatch multiWrite |
| 382 | fail_exists |= multiWriteInternal(req); |
| 383 | req.clear(); |
| 384 | } |
| 385 | |
| 386 | return fail_exists; |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public boolean multiDelete(final Collection<IMultiEntryOperation> ops) { |
| 391 | |
| 392 | // TODO implement multiRemove JNI, etc. if we need performance |
| 393 | |
| 394 | boolean fail_exists = false; |
| 395 | JRamCloud rcClient = getJRamCloudClient(); |
| 396 | |
| 397 | for (IMultiEntryOperation iop : ops) { |
| 398 | RCMultiEntryOperation op = (RCMultiEntryOperation)iop; |
| 399 | switch (op.getOperation()) { |
| 400 | case DELETE: |
| 401 | RejectRules rules = new RejectRules(); |
| 402 | rules.rejectIfDoesntExists(); |
| 403 | rules.rejectIfNeVersion(op.getVersion()); |
| 404 | |
| 405 | try { |
| 406 | long removed_version = rcClient.remove(op.tableId.getTableID(), op.entry.getKey(), rules); |
| 407 | op.entry.setVersion(removed_version); |
| 408 | op.status = STATUS.SUCCESS; |
| 409 | } catch (JRamCloud.ObjectDoesntExistException|JRamCloud.WrongVersionException e) { |
| 410 | log.error("Failed to remove key:" + ByteArrayUtil.toHexStringBuffer(op.entry.getKey(), "") + " from tableID:" + op.tableId, e ); |
| 411 | fail_exists = true; |
| 412 | op.status = STATUS.FAILED; |
| 413 | } catch (JRamCloud.RejectRulesException e) { |
| 414 | log.error("Failed to remove key:" + ByteArrayUtil.toHexStringBuffer(op.entry.getKey(), "") + " from tableID:" + op.tableId, e ); |
| 415 | fail_exists = true; |
| 416 | op.status = STATUS.FAILED; |
| 417 | } |
| 418 | break; |
| 419 | |
| 420 | case FORCE_DELETE: |
| 421 | long removed_version = rcClient.remove(op.tableId.getTableID(), op.entry.getKey()); |
| 422 | if (removed_version != JRamCloud.VERSION_NONEXISTENT) { |
| 423 | op.entry.setVersion(removed_version); |
| 424 | op.status = STATUS.SUCCESS; |
| 425 | } else { |
| 426 | log.error("Failed to remove key:{} from tableID:{}", ByteArrayUtil.toHexStringBuffer(op.entry.getKey(), ""), op.tableId ); |
| 427 | fail_exists = true; |
| 428 | op.status = STATUS.FAILED; |
| 429 | } |
| 430 | break; |
| 431 | |
| 432 | default: |
| 433 | log.error("Invalid operation {} specified on multiDelete", op.getOperation() ); |
| 434 | fail_exists = true; |
| 435 | op.status = STATUS.FAILED; |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | return fail_exists; |
| 440 | } |
| 441 | |
| 442 | private boolean multiReadInternal(final ArrayList<RCMultiEntryOperation> ops) { |
| 443 | boolean fail_exists = false; |
| 444 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 445 | |
| 446 | final int reqs = ops.size(); |
| 447 | |
| 448 | MultiReadObject multiReadObjects = new MultiReadObject(reqs); |
| 449 | |
| 450 | // setup multi-read operation objects |
| 451 | for (int i = 0; i < reqs; ++i) { |
| 452 | IMultiEntryOperation op = ops.get(i); |
| 453 | multiReadObjects.setObject(i, ((RCTableID)op.getTableId()).getTableID(), op.getKey()); |
| 454 | } |
| 455 | |
| 456 | // execute |
| 457 | JRamCloud.Object[] results = rcClient.multiRead(multiReadObjects.tableId, multiReadObjects.key, multiReadObjects.keyLength, reqs); |
| 458 | if (results.length != reqs) { |
| 459 | log.error("multiRead returned unexpected number of results. (requested:{}, returned:{})", reqs, results.length); |
| 460 | fail_exists = true; |
| 461 | } |
| 462 | |
| 463 | for (int i = 0; i < results.length; ++i) { |
| 464 | IModifiableMultiEntryOperation op = ops.get(i); |
| 465 | if (results[i] == null) { |
| 466 | log.error("MultiRead error, skipping {}, {}", op.getTableId(), op); |
| 467 | fail_exists = true; |
| 468 | op.setStatus(STATUS.FAILED); |
| 469 | continue; |
| 470 | } |
| 471 | assert (Arrays.equals(results[i].key, op.getKey())); |
| 472 | |
| 473 | op.setValue(results[i].value, results[i].version); |
| 474 | if (results[i].version == JRamCloud.VERSION_NONEXISTENT) { |
| 475 | fail_exists = true; |
| 476 | op.setStatus(STATUS.FAILED); |
| 477 | } else { |
| 478 | op.setStatus(STATUS.SUCCESS); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return fail_exists; |
| 483 | } |
| 484 | |
| 485 | private boolean multiWriteInternal(final ArrayList<RCMultiEntryOperation> ops) { |
| 486 | boolean fail_exists = false; |
| 487 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 488 | |
| 489 | final int reqs = ops.size(); |
| 490 | |
| 491 | MultiWriteObject multiWriteObjects = new MultiWriteObject(reqs); |
| 492 | |
| 493 | for (int i = 0; i < reqs; ++i) { |
| 494 | |
| 495 | IModifiableMultiEntryOperation op = ops.get(i); |
| 496 | RejectRules rules = new RejectRules(); |
| 497 | |
| 498 | switch (op.getOperation()) { |
| 499 | case CREATE: |
| 500 | rules.rejectIfExists(); |
| 501 | break; |
| 502 | case FORCE_CREATE: |
| 503 | // no reject rule |
| 504 | break; |
| 505 | case UPDATE: |
| 506 | rules.rejectIfDoesntExists(); |
| 507 | rules.rejectIfNeVersion(op.getVersion()); |
| 508 | break; |
| 509 | |
| 510 | default: |
| 511 | log.error("Invalid operation {} specified on multiWriteInternal", op.getOperation() ); |
| 512 | fail_exists = true; |
| 513 | op.setStatus(STATUS.FAILED); |
| 514 | return fail_exists; |
| 515 | } |
| 516 | multiWriteObjects.setObject(i, ((RCTableID)op.getTableId()).getTableID(), op.getKey(), op.getValue(), rules); |
| 517 | } |
| 518 | |
| 519 | MultiWriteRspObject[] results = rcClient.multiWrite(multiWriteObjects.tableId, multiWriteObjects.key, multiWriteObjects.keyLength, multiWriteObjects.value, multiWriteObjects.valueLength, ops.size(), multiWriteObjects.rules); |
| 520 | if (results.length != reqs) { |
| 521 | log.error("multiWrite returned unexpected number of results. (requested:{}, returned:{})", reqs, results.length); |
| 522 | fail_exists = true; |
| 523 | } |
| 524 | |
| 525 | for (int i = 0; i < results.length; ++i) { |
| 526 | IModifiableMultiEntryOperation op = ops.get(i); |
| 527 | |
| 528 | if (results[i] != null |
| 529 | && results[i].getStatus() == RCClient.STATUS_OK) { |
| 530 | op.setStatus(STATUS.SUCCESS); |
| 531 | op.setVersion(results[i].getVersion()); |
| 532 | } else { |
| 533 | op.setStatus(STATUS.FAILED); |
| 534 | fail_exists = true; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | return fail_exists; |
| 539 | } |
| 540 | |
| 541 | private static final ConcurrentHashMap<String, RCTable> tables = new ConcurrentHashMap<>(); |
| 542 | |
| 543 | @Override |
| 544 | public IKVTable getTable(final String tableName) { |
| 545 | RCTable table = tables.get(tableName); |
| 546 | if (table == null) { |
| 547 | RCTable new_table = new RCTable(tableName); |
| 548 | RCTable existing_table = tables |
| 549 | .putIfAbsent(tableName, new_table); |
| 550 | if (existing_table != null) { |
| 551 | return existing_table; |
| 552 | } else { |
| 553 | return new_table; |
| 554 | } |
| 555 | } |
| 556 | return table; |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public void dropTable(IKVTable table) { |
| 561 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 562 | rcClient.dropTable(table.getTableId().getTableName()); |
| 563 | tables.remove(table.getTableId().getTableName()); |
| 564 | } |
| 565 | |
| 566 | static final long VERSION_NONEXISTENT = JRamCloud.VERSION_NONEXISTENT; |
| 567 | |
| 568 | @Override |
| 569 | public long VERSION_NONEXISTENT() { |
| 570 | return VERSION_NONEXISTENT; |
| 571 | } |
| 572 | } |