OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * Copyright (c) 2013 Stanford University. All rights reserved. |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); you |
| 5 * may not use this file except in compliance with the License. You |
| 6 * may obtain a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 * implied. See the License for the specific language governing |
| 14 * permissions and limitations under the License. See accompanying |
| 15 * LICENSE file. |
| 16 */ |
| 17 |
| 18 /* |
| 19 * This file implements a RAMCloud client DB layer for YCSB (Yahoo!'s Cloud |
| 20 * Storage Benchmark suite). |
| 21 * |
| 22 * See https://github.com/brianfrankcooper/YCSB/wiki/Adding-a-Database and the |
| 23 * com.yahoo.ycsb.DB abstract class if you're starting to hack on this. |
| 24 */ |
| 25 |
| 26 package com.yahoo.ycsb.db; |
| 27 import com.yahoo.ycsb.DB; |
| 28 import com.yahoo.ycsb.DBException; |
| 29 import com.yahoo.ycsb.ByteIterator; |
| 30 import com.yahoo.ycsb.StringByteIterator; |
| 31 |
| 32 import java.util.Map; |
| 33 import java.util.HashMap; |
| 34 import java.util.Iterator; |
| 35 import java.util.List; |
| 36 import java.util.Properties; |
| 37 import java.util.Set; |
| 38 import java.util.Vector; |
| 39 import java.util.ArrayList; |
| 40 |
| 41 import java.io.ByteArrayInputStream; |
| 42 import java.io.ByteArrayOutputStream; |
| 43 import java.io.ObjectInputStream; |
| 44 import java.io.ObjectOutputStream; |
| 45 import java.nio.ByteBuffer; |
| 46 import java.nio.ByteOrder; |
| 47 import java.nio.BufferUnderflowException; |
| 48 |
| 49 import edu.stanford.ramcloud.JRamCloud; |
| 50 |
| 51 public class RamCloudClient extends DB { |
| 52 private JRamCloud ramcloud; |
| 53 private HashMap<String, Long> tableIds; |
| 54 |
| 55 public static final String LOCATOR_PROPERTY = "ramcloud.coordinatorLocator"; |
| 56 |
| 57 /// Success is always 0. |
| 58 public static final int OK = 0; |
| 59 |
| 60 /// YCSB interprets anything non-0 as an error, but doesn't interpret the |
| 61 /// specific value. |
| 62 public static final int ERROR = 1; |
| 63 |
| 64 /** |
| 65 * This method returns the 64-bit table identifier for the given table, |
| 66 * creating it first if necessary. |
| 67 */ |
| 68 private long |
| 69 getTableId(String tableName) |
| 70 { |
| 71 if (!tableIds.containsKey(tableName)) |
| 72 tableIds.put(tableName, ramcloud.createTable(tableName)); |
| 73 return tableIds.get(tableName); |
| 74 } |
| 75 |
| 76 /** |
| 77 * Serialize the fields and values for a particular key into a byte[] |
| 78 * array to be written to RAMCloud. This method uses Java's built-in |
| 79 * Object serialization functionality. |
| 80 */ |
| 81 private static byte[] |
| 82 serializeUNUSED(HashMap<String, ByteIterator> values) |
| 83 { |
| 84 Object object = StringByteIterator.getStringMap(values); |
| 85 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); |
| 86 try { |
| 87 ObjectOutputStream objectStream = new ObjectOutputStream(byteStream)
; |
| 88 objectStream.writeObject(object); |
| 89 } catch (Exception e) { |
| 90 System.err.println("RamCloudClient serialization failed: " + e); |
| 91 } |
| 92 return byteStream.toByteArray(); |
| 93 } |
| 94 |
| 95 /** |
| 96 * Deserialize the fields and values stored in a RAMCloud object blob |
| 97 * into the given HashMap. This method uses Java's built-in Object |
| 98 * deserialization functionality. |
| 99 */ |
| 100 private static void |
| 101 deserializeUNUSED(byte[] bytes, HashMap<String, ByteIterator> into) throws D
BException |
| 102 { |
| 103 Object object = null; |
| 104 ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes); |
| 105 try { |
| 106 ObjectInputStream objectStream = new ObjectInputStream(byteStream); |
| 107 object = objectStream.readObject(); |
| 108 } catch (Exception e) { |
| 109 System.err.println("RamCloudClient deserialization failed: " + e); |
| 110 } |
| 111 HashMap<String, String> stringMap = (HashMap<String, String>)object; |
| 112 StringByteIterator.putAllAsByteIterators(into, stringMap); |
| 113 } |
| 114 |
| 115 /** |
| 116 * Serialize the fields and values for a particular key into a byte[] |
| 117 * array to be written to RAMCloud. This method uses a hand-coded |
| 118 * serializer. It's about 1.2-3x as fast as when using Java's object |
| 119 * serializer. |
| 120 */ |
| 121 private static byte[] |
| 122 serialize(HashMap<String, ByteIterator> values) |
| 123 { |
| 124 byte[][] kvArray = new byte[values.size() * 2][]; |
| 125 |
| 126 int kvIndex = 0; |
| 127 int totalLength = 0; |
| 128 for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { |
| 129 totalLength += 8; // fields denoting key length and value length |
| 130 |
| 131 byte[] keyBytes = entry.getKey().getBytes(); |
| 132 kvArray[kvIndex++] = keyBytes; |
| 133 totalLength += keyBytes.length; |
| 134 |
| 135 byte[] valueBytes = entry.getValue().toString().getBytes(); |
| 136 kvArray[kvIndex++] = valueBytes; |
| 137 totalLength += valueBytes.length; |
| 138 } |
| 139 ByteBuffer buf = ByteBuffer.allocate(totalLength); |
| 140 buf.order(ByteOrder.LITTLE_ENDIAN); |
| 141 |
| 142 kvIndex = 0; |
| 143 for (int i = 0; i < kvArray.length / 2; i++) { |
| 144 byte[] keyBytes = kvArray[kvIndex++]; |
| 145 buf.putInt(keyBytes.length); |
| 146 buf.put(keyBytes); |
| 147 byte[] valueBytes = kvArray[kvIndex++]; |
| 148 buf.putInt(valueBytes.length); |
| 149 buf.put(valueBytes); |
| 150 } |
| 151 |
| 152 return buf.array(); |
| 153 } |
| 154 |
| 155 /** |
| 156 * Deserialize the fields and values stored in a RAMCloud object blob |
| 157 * into the given HashMap. This method uses a hand-coded deserializer. |
| 158 * It's about 3-5x as fast as when using Java's object deserializer. |
| 159 */ |
| 160 private static void |
| 161 deserialize(byte[] bytes, HashMap<String, ByteIterator> into) throws DBExcep
tion |
| 162 { |
| 163 ByteBuffer buf = ByteBuffer.wrap(bytes); |
| 164 buf.order(ByteOrder.LITTLE_ENDIAN); |
| 165 |
| 166 boolean workingOnKey = false; |
| 167 try { |
| 168 while (true) { |
| 169 int keyByteLength = buf.getInt(); |
| 170 workingOnKey = true; |
| 171 String key = new String(bytes, buf.position(), keyByteLength); |
| 172 buf.position(buf.position() + keyByteLength); |
| 173 |
| 174 int valueByteLength = buf.getInt(); |
| 175 String value = new String(bytes, buf.position(), valueByteLength
); |
| 176 buf.position(buf.position() + valueByteLength); |
| 177 |
| 178 into.put(key, new StringByteIterator(value)); |
| 179 workingOnKey = false; |
| 180 } |
| 181 } catch (BufferUnderflowException e) { |
| 182 // Done, hopefully. |
| 183 if (buf.remaining() != 0 || workingOnKey) { |
| 184 throw new DBException("deserialize: ByteBuffer not parsed " + |
| 185 "properly! Had " + buf.remaining() + " bytes left over!"); |
| 186 } |
| 187 } |
| 188 } |
| 189 |
| 190 /** |
| 191 * Initialize any state for this DB. |
| 192 * Called once per DB instance; there is one DB instance per client thread. |
| 193 */ |
| 194 public void |
| 195 init() throws DBException |
| 196 { |
| 197 assert ramcloud == null; |
| 198 assert tableIds == null; |
| 199 |
| 200 Properties props = getProperties(); |
| 201 String locator = props.getProperty(LOCATOR_PROPERTY); |
| 202 if (locator == null) |
| 203 throw new DBException("Missing property " + LOCATOR_PROPERTY); |
| 204 |
| 205 System.err.println("RamCloudClient connecting to " + locator + " ..."); |
| 206 ramcloud = new JRamCloud(locator); |
| 207 tableIds = new HashMap<String, Long>(); |
| 208 } |
| 209 |
| 210 /** |
| 211 * Cleanup any state for this DB. |
| 212 * Called once per DB instance; there is one DB instance per client thread. |
| 213 */ |
| 214 public void |
| 215 cleanup() throws DBException |
| 216 { |
| 217 System.err.println("RamCloudClient disconnecting ..."); |
| 218 ramcloud.disconnect(); |
| 219 ramcloud = null; |
| 220 tableIds = null; |
| 221 } |
| 222 |
| 223 /** |
| 224 * Delete a record from the database. |
| 225 * |
| 226 * @param table The name of the table |
| 227 * @param key The record key of the record to delete. |
| 228 * @return Zero on success, a non-zero error code on error. |
| 229 */ |
| 230 @Override |
| 231 public int |
| 232 delete(String table, String key) { |
| 233 try { |
| 234 ramcloud.remove(getTableId(table), key); |
| 235 } catch (Exception e) { |
| 236 System.err.println("RamCloudClient delete threw: " + e); |
| 237 return ERROR; |
| 238 } |
| 239 return OK; |
| 240 } |
| 241 |
| 242 /** |
| 243 * Insert a record in the database. Any field/value pairs in the specified |
| 244 * values HashMap will be written into the record with the specified record |
| 245 * key. |
| 246 * |
| 247 * @param table The name of the table |
| 248 * @param key The record key of the record to insert. |
| 249 * @param values A HashMap of field/value pairs to insert in the record |
| 250 * @return Zero on success, a non-zero error code on error. |
| 251 */ |
| 252 @Override |
| 253 public int |
| 254 insert(String table, String key, HashMap<String, ByteIterator> values) |
| 255 { |
| 256 byte[] value = serialize(values); |
| 257 try { |
| 258 ramcloud.write(getTableId(table), key, value); |
| 259 } catch (Exception e) { |
| 260 System.err.println("RamCloudClient insert threw: " + e); |
| 261 return ERROR; |
| 262 } |
| 263 return OK; |
| 264 } |
| 265 |
| 266 /** |
| 267 * Read a record from the database. Each field/value pair from the result |
| 268 * will be stored in a HashMap. |
| 269 * |
| 270 * @param table The name of the table |
| 271 * @param key The record key of the record to read. |
| 272 * @param fields The list of fields to read, or null for all of them |
| 273 * @param result A HashMap of field/value pairs for the result |
| 274 * @return Zero on success, a non-zero error code on error or "not found". |
| 275 */ |
| 276 @Override |
| 277 public int |
| 278 read(String table, |
| 279 String key, |
| 280 Set<String> fields, |
| 281 HashMap<String, ByteIterator> result) |
| 282 { |
| 283 JRamCloud.Object object = null; |
| 284 HashMap<String, ByteIterator> map = new HashMap<String, ByteIterator>(); |
| 285 try { |
| 286 object = ramcloud.read(getTableId(table), key); |
| 287 } catch (Exception e) { |
| 288 System.err.println("RamCloudClient read threw: " + e); |
| 289 return ERROR; |
| 290 } |
| 291 |
| 292 try { |
| 293 deserialize(object.value, map); |
| 294 } catch (DBException e) { |
| 295 System.err.println("RamCloudClient deserializer threw: " + e); |
| 296 return ERROR; |
| 297 } |
| 298 |
| 299 if (fields == null) { |
| 300 result.putAll(map); |
| 301 } else { |
| 302 for (String field : fields) { |
| 303 if (!map.containsKey(field)) |
| 304 return ERROR; |
| 305 result.put(field, map.get(field)); |
| 306 } |
| 307 } |
| 308 |
| 309 return OK; |
| 310 } |
| 311 |
| 312 /** |
| 313 * Perform a range scan for a set of records in the database. Each field/val
ue |
| 314 * pair from the result will be stored in a HashMap. |
| 315 * |
| 316 * @param table The name of the table |
| 317 * @param startkey The record key of the first record to read. |
| 318 * @param recordcount The number of records to read |
| 319 * @param fields The list of fields to read, or null for all of them |
| 320 * @param result A Vector of HashMaps, where each HashMap is a set field/val
ue |
| 321 * pairs for one record |
| 322 * @return Zero on success, a non-zero error code on error. |
| 323 */ |
| 324 @Override |
| 325 public int |
| 326 scan(String table, |
| 327 String startkey, |
| 328 int recordcount, |
| 329 Set<String> fields, |
| 330 Vector<HashMap<String, ByteIterator>> result) |
| 331 { |
| 332 System.err.println("Warning: RAMCloud doesn't support range scans yet.")
; |
| 333 return ERROR; |
| 334 } |
| 335 |
| 336 /** |
| 337 * Update a record in the database. Any field/value pairs in the specified |
| 338 * values HashMap will be written into the record with the specified record |
| 339 * key, overwriting any existing values with the same field name. |
| 340 * |
| 341 * @param table The name of the table |
| 342 * @param key The record key of the record to write. |
| 343 * @param values A HashMap of field/value pairs to update in the record |
| 344 * @return Zero on success, a non-zero error code on error. |
| 345 */ |
| 346 @Override |
| 347 public int |
| 348 update(String table, String key, HashMap<String, ByteIterator> values) |
| 349 { |
| 350 // XXX- not sure if the semantics are right here. are we supposed to pre
serve |
| 351 // any old fields not in values? presently we dump them. |
| 352 return insert(table, key, values); |
| 353 } |
| 354 |
| 355 /*************************************************************************** |
| 356 * The following methods are for debugging / benchmarking this class as a |
| 357 * standalone application. |
| 358 **************************************************************************/ |
| 359 |
| 360 private static HashMap<String, ByteIterator> |
| 361 generateValues(int numFields, int fieldWidth) |
| 362 { |
| 363 HashMap<String, ByteIterator> values = new HashMap<String, ByteIterator>
(); |
| 364 String fieldValuePrototype = ""; |
| 365 for (int i = 0; i < fieldWidth; i++) |
| 366 fieldValuePrototype += "!"; |
| 367 for (int i = 0; i < numFields; i++) { |
| 368 String fieldValue = fieldValuePrototype.substring(0, fieldWidth - ("
" + i).length()) + i; |
| 369 values.put("field" + i, new StringByteIterator(fieldValue)); |
| 370 } |
| 371 return values; |
| 372 } |
| 373 |
| 374 private static double |
| 375 measureReadLatency(RamCloudClient client, int numFields, int fieldWidth) |
| 376 { |
| 377 HashMap<String, ByteIterator> values = generateValues(numFields, fieldWi
dth); |
| 378 client.insert("theTable", "theKey", values); |
| 379 HashMap<String, ByteIterator> result = new HashMap<String, ByteIterator>
(); |
| 380 long before = System.nanoTime(); |
| 381 for (int i = 0; i < 100000; i++) { |
| 382 client.read("theTable", "theKey", null, result); |
| 383 result.clear(); |
| 384 } |
| 385 long after = System.nanoTime(); |
| 386 client.delete("theTable", "theKey"); |
| 387 return ((double)(after - before) / 100000 / 1000); |
| 388 } |
| 389 |
| 390 private static double |
| 391 measureWriteLatency(RamCloudClient client, int numFields, int fieldWidth) |
| 392 { |
| 393 HashMap<String, ByteIterator> values = generateValues(numFields, fieldWi
dth); |
| 394 long before = System.nanoTime(); |
| 395 for (int i = 0; i < 100000; i++) |
| 396 client.insert("theTable", "theKey", values); |
| 397 long after = System.nanoTime(); |
| 398 client.delete("theTable", "theKey"); |
| 399 return ((double)(after - before) / 100000 / 1000); |
| 400 } |
| 401 |
| 402 private static double |
| 403 measureSerializerLatency(int numFields, int fieldWidth) |
| 404 { |
| 405 HashMap<String, ByteIterator> values = generateValues(numFields, fieldWi
dth); |
| 406 long before = System.nanoTime(); |
| 407 for (int i = 0; i < 100000; i++) { |
| 408 byte[] serialized = serialize(values); |
| 409 } |
| 410 long after = System.nanoTime(); |
| 411 return ((double)(after - before) / 100000 / 1000); |
| 412 } |
| 413 |
| 414 private static double |
| 415 measureDeserializerLatency(int numFields, int fieldWidth) |
| 416 { |
| 417 HashMap<String, ByteIterator> values = generateValues(numFields, fieldWi
dth); |
| 418 byte[] serialized = serialize(values); |
| 419 HashMap<String, ByteIterator> results = new HashMap<String, ByteIterator
>(); |
| 420 long before = System.nanoTime(); |
| 421 for (int i = 0; i < 100000; i++) { |
| 422 try { |
| 423 deserialize(serialized, results); |
| 424 } catch (DBException e) { |
| 425 } |
| 426 results.clear(); |
| 427 } |
| 428 long after = System.nanoTime(); |
| 429 return ((double)(after - before) / 100000 / 1000); |
| 430 } |
| 431 |
| 432 public static void |
| 433 main(String argv[]) |
| 434 { |
| 435 if (argv.length != 1) { |
| 436 System.err.println("Error: first argument must be the " + |
| 437 "coordinator ServiceLocator string"); |
| 438 return; |
| 439 } |
| 440 |
| 441 // argv[0] is the coordinatorLocator |
| 442 RamCloudClient client = new RamCloudClient(); |
| 443 Properties props = client.getProperties(); |
| 444 props.setProperty(LOCATOR_PROPERTY, argv[0]); |
| 445 try { |
| 446 client.init(); |
| 447 } catch (DBException e) { |
| 448 System.err.println("Failed to initialize RamCloudClient: " + e); |
| 449 return; |
| 450 } |
| 451 |
| 452 // Warm up first. |
| 453 measureReadLatency(client, 5, 10); |
| 454 |
| 455 int[] fieldCounts = { 1, 2, 3, 5, 10, 20, 50, 100 }; |
| 456 |
| 457 // Measure read performance. The JRamCloud bindings are good for about |
| 458 // 7.5us reads on Infiniband, so we need to be careful that our |
| 459 // field serialisation is fast. |
| 460 for (int fields : fieldCounts) { |
| 461 System.out.println("Avg read latency (" + fields + |
| 462 " field(s)): " + measureReadLatency(client, fields, 100) + " us
"); |
| 463 } |
| 464 |
| 465 // And now write performance... |
| 466 for (int fields : fieldCounts) { |
| 467 System.out.println("Avg write latency (" + fields + |
| 468 " field(s)): " + measureWriteLatency(client, fields, 100) + " u
s"); |
| 469 } |
| 470 |
| 471 // And let's see how fast our serializer and deserializer are... |
| 472 for (int fields : fieldCounts) { |
| 473 System.out.println("Avg serialization / deserialization latency (" +
fields + |
| 474 " field(s)): " + measureSerializerLatency(fields, 100) + " / "
+ |
| 475 measureDeserializerLatency(fields, 100) + " us"); |
| 476 } |
| 477 } |
| 478 } |
OLD | NEW |