use of org.hbase.async.HBaseClient in project YCSB by brianfrankcooper.
the class AsyncHBaseClient method init.
@Override
public void init() throws DBException {
if (getProperties().getProperty(CLIENT_SIDE_BUFFERING_PROPERTY, "false").toLowerCase().equals("true")) {
clientSideBuffering = true;
}
if (getProperties().getProperty(DURABILITY_PROPERTY, "true").toLowerCase().equals("false")) {
durability = false;
}
final String columnFamily = getProperties().getProperty(COLUMN_FAMILY_PROPERTY);
if (columnFamily == null || columnFamily.isEmpty()) {
System.err.println("Error, must specify a columnfamily for HBase table");
throw new DBException("No columnfamily specified");
}
columnFamilyBytes = columnFamily.getBytes();
if ((getProperties().getProperty("debug") != null) && (getProperties().getProperty("debug").compareTo("true") == 0)) {
debug = true;
}
joinTimeout = Integer.parseInt(getProperties().getProperty(JOIN_TIMEOUT_PROPERTY, JOIN_TIMEOUT_PROPERTY_DEFAULT));
final boolean prefetchMeta = getProperties().getProperty(PREFETCH_META_PROPERTY, "false").toLowerCase().equals("true") ? true : false;
try {
synchronized (MUTEX) {
++threadCount;
if (client == null) {
final String configPath = getProperties().getProperty(CONFIG_PROPERTY);
final Config config;
if (configPath == null || configPath.isEmpty()) {
config = new Config();
final Iterator<Entry<Object, Object>> iterator = getProperties().entrySet().iterator();
while (iterator.hasNext()) {
final Entry<Object, Object> property = iterator.next();
config.overrideConfig((String) property.getKey(), (String) property.getValue());
}
} else {
config = new Config(configPath);
}
client = new HBaseClient(config);
// Terminate right now if table does not exist, since the client
// will not propagate this error upstream once the workload
// starts.
String table = getProperties().getProperty(TABLENAME_PROPERTY, TABLENAME_PROPERTY_DEFAULT);
try {
client.ensureTableExists(table).join(joinTimeout);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
} catch (Exception e) {
throw new DBException(e);
}
if (prefetchMeta) {
try {
if (debug) {
System.out.println("Starting meta prefetch for table " + table);
}
client.prefetchMeta(table).join(joinTimeout);
if (debug) {
System.out.println("Completed meta prefetch for table " + table);
}
} catch (InterruptedException e) {
System.err.println("Interrupted during prefetch");
Thread.currentThread().interrupt();
} catch (Exception e) {
throw new DBException("Failed prefetch", e);
}
}
}
}
} catch (IOException e) {
throw new DBException("Failed instantiation of client", e);
}
}
use of org.hbase.async.HBaseClient in project opentsdb by OpenTSDB.
the class TestBranch method setupStorage.
/**
* Mocks classes for testing the storage calls
*/
private void setupStorage() throws Exception {
final HBaseClient client = mock(HBaseClient.class);
final Config config = new Config(false);
storage = new MockBase(new TSDB(client, config), client, true, true, true, true);
final List<byte[]> families = new ArrayList<byte[]>();
families.add(Tree.TREE_FAMILY());
storage.addTable(TREE_TABLE, families);
Branch branch = new Branch(1);
TreeMap<Integer, String> path = new TreeMap<Integer, String>();
path.put(0, "ROOT");
path.put(1, "sys");
path.put(2, "cpu");
branch.prependParentPath(path);
branch.setDisplayName("cpu");
storage.addColumn(TREE_TABLE, branch.compileBranchId(), Tree.TREE_FAMILY(), "branch".getBytes(MockBase.ASCII()), (byte[]) toStorageJson.invoke(branch));
Leaf leaf = new Leaf("user", "000001000001000001");
byte[] qualifier = leaf.columnQualifier();
storage.addColumn(TREE_TABLE, branch.compileBranchId(), Tree.TREE_FAMILY(), qualifier, (byte[]) LeaftoStorageJson.invoke(leaf));
leaf = new Leaf("nice", "000002000002000002");
qualifier = leaf.columnQualifier();
storage.addColumn(TREE_TABLE, branch.compileBranchId(), Tree.TREE_FAMILY(), qualifier, (byte[]) LeaftoStorageJson.invoke(leaf));
// child branch
branch = new Branch(1);
path.put(3, "mboard");
branch.prependParentPath(path);
branch.setDisplayName("mboard");
storage.addColumn(TREE_TABLE, branch.compileBranchId(), Tree.TREE_FAMILY(), "branch".getBytes(MockBase.ASCII()), (byte[]) toStorageJson.invoke(branch));
leaf = new Leaf("Asus", "000003000003000003");
qualifier = leaf.columnQualifier();
storage.addColumn(TREE_TABLE, branch.compileBranchId(), Tree.TREE_FAMILY(), qualifier, (byte[]) LeaftoStorageJson.invoke(leaf));
}
use of org.hbase.async.HBaseClient in project opentsdb by OpenTSDB.
the class TestUniqueId method getOrCreateIdAssignIdWithRaceCondition.
// Test the creation of an ID with a race condition.
@Test
public void getOrCreateIdAssignIdWithRaceCondition() {
// Simulate a race between client A and client B.
// A does a Get and sees that there's no ID for this name.
// B does a Get and sees that there's no ID too, and B actually goes
// through the entire process to create the ID.
// Then A attempts to go through the process and should discover that the
// ID has already been assigned.
// Used by client A.
uid = new UniqueId(client, table, METRIC, 3);
// For client B.
HBaseClient client_b = mock(HBaseClient.class);
final UniqueId uid_b = new UniqueId(client_b, table, METRIC, 3);
final byte[] id = { 0, 0, 5 };
final byte[] byte_name = { 'f', 'o', 'o' };
final ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
kvs.add(new KeyValue(byte_name, ID, METRIC_ARRAY, id));
final Deferred<ArrayList<KeyValue>> d = PowerMockito.spy(new Deferred<ArrayList<KeyValue>>());
when(client.get(anyGet())).thenReturn(d).thenReturn(Deferred.fromResult(kvs));
final Answer<byte[]> the_race = new Answer<byte[]>() {
public byte[] answer(final InvocationOnMock unused_invocation) throws Exception {
// While answering A's first Get, B doest a full getOrCreateId.
assertArrayEquals(id, uid_b.getOrCreateId("foo"));
d.callback(null);
return (byte[]) ((Deferred) d).join();
}
};
// Start the race when answering A's first Get.
try {
PowerMockito.doAnswer(the_race).when(d).joinUninterruptibly();
} catch (Exception e) {
fail("Should never happen: " + e);
}
// null => ID doesn't exist.
when(client_b.get(anyGet())).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
// Watch this! ______,^ I'm writing C++ in Java!
when(client_b.atomicIncrement(incrementForRow(MAXID))).thenReturn(Deferred.fromResult(5L));
when(client_b.compareAndSet(anyPut(), emptyArray())).thenReturn(Deferred.fromResult(true)).thenReturn(Deferred.fromResult(true));
// Now that B is finished, A proceeds and allocates a UID that will be
// wasted, and creates the reverse mapping, but fails at creating the
// forward mapping.
when(client.atomicIncrement(incrementForRow(MAXID))).thenReturn(Deferred.fromResult(6L));
when(client.compareAndSet(anyPut(), emptyArray())).thenReturn(// Orphan reverse mapping.
Deferred.fromResult(true)).thenReturn(// Already CAS'ed by A.
Deferred.fromResult(false));
// Start the execution.
assertArrayEquals(id, uid.getOrCreateId("foo"));
// Verify the order of execution too.
final InOrder order = inOrder(client, client_b);
// 1st Get for A.
order.verify(client).get(anyGet());
// 1st Get for B.
order.verify(client_b).get(anyGet());
order.verify(client_b).atomicIncrement(incrementForRow(MAXID));
// both mappings.
order.verify(client_b, times(2)).compareAndSet(// both mappings.
anyPut(), emptyArray());
order.verify(client).atomicIncrement(incrementForRow(MAXID));
// both mappings.
order.verify(client, times(2)).compareAndSet(// both mappings.
anyPut(), emptyArray());
// A retries and gets it.
order.verify(client).get(anyGet());
}
use of org.hbase.async.HBaseClient in project opentsdb by OpenTSDB.
the class TestUniqueId method renameRaceCondition.
@Test(expected = IllegalStateException.class)
public void renameRaceCondition() throws Exception {
// Simulate a race between client A(default) and client B.
// A and B rename same UID to different name.
// B waits till A start to invoke PutRequest to start.
uid = new UniqueId(client, table, METRIC, 3);
HBaseClient client_b = mock(HBaseClient.class);
final UniqueId uid_b = new UniqueId(client_b, table, METRIC, 3);
final byte[] foo_id = { 0, 'a', 0x42 };
final byte[] foo_name = { 'f', 'o', 'o' };
ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
kvs.add(new KeyValue(foo_name, ID, METRIC_ARRAY, foo_id));
when(client_b.get(anyGet())).thenReturn(Deferred.fromResult(kvs)).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
when(client_b.put(anyPut())).thenAnswer(answerTrue());
when(client_b.delete(anyDelete())).thenAnswer(answerTrue());
final Answer<Deferred<Boolean>> the_race = new Answer<Deferred<Boolean>>() {
public Deferred<Boolean> answer(final InvocationOnMock inv) throws Exception {
uid_b.rename("foo", "xyz");
return Deferred.fromResult(true);
}
};
when(client.get(anyGet())).thenReturn(Deferred.fromResult(kvs)).thenReturn(Deferred.<ArrayList<KeyValue>>fromResult(null));
when(client.put(anyPut())).thenAnswer(the_race);
when(client.delete(anyDelete())).thenAnswer(answerTrue());
uid.rename("foo", "bar");
}
use of org.hbase.async.HBaseClient in project YCSB by brianfrankcooper.
the class AsyncHBaseClient method init.
@Override
public void init() throws DBException {
if (getProperties().getProperty(CLIENT_SIDE_BUFFERING_PROPERTY, "false").toLowerCase().equals("true")) {
clientSideBuffering = true;
}
if (getProperties().getProperty(DURABILITY_PROPERTY, "true").toLowerCase().equals("false")) {
durability = false;
}
final String columnFamily = getProperties().getProperty(COLUMN_FAMILY_PROPERTY);
if (columnFamily == null || columnFamily.isEmpty()) {
System.err.println("Error, must specify a columnfamily for HBase table");
throw new DBException("No columnfamily specified");
}
columnFamilyBytes = columnFamily.getBytes();
if ((getProperties().getProperty("debug") != null) && (getProperties().getProperty("debug").compareTo("true") == 0)) {
debug = true;
}
joinTimeout = Integer.parseInt(getProperties().getProperty(JOIN_TIMEOUT_PROPERTY, JOIN_TIMEOUT_PROPERTY_DEFAULT));
final boolean prefetchMeta = getProperties().getProperty(PREFETCH_META_PROPERTY, "false").toLowerCase().equals("true") ? true : false;
try {
synchronized (MUTEX) {
++threadCount;
if (client == null) {
final String configPath = getProperties().getProperty(CONFIG_PROPERTY);
final Config config;
if (configPath == null || configPath.isEmpty()) {
config = new Config();
final Iterator<Entry<Object, Object>> iterator = getProperties().entrySet().iterator();
while (iterator.hasNext()) {
final Entry<Object, Object> property = iterator.next();
config.overrideConfig((String) property.getKey(), (String) property.getValue());
}
} else {
config = new Config(configPath);
}
client = new HBaseClient(config);
// Terminate right now if table does not exist, since the client
// will not propagate this error upstream once the workload
// starts.
String table = getProperties().getProperty(TABLENAME_PROPERTY, TABLENAME_PROPERTY_DEFAULT);
try {
client.ensureTableExists(table).join(joinTimeout);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
} catch (Exception e) {
throw new DBException(e);
}
if (prefetchMeta) {
try {
if (debug) {
System.out.println("Starting meta prefetch for table " + table);
}
client.prefetchMeta(table).join(joinTimeout);
if (debug) {
System.out.println("Completed meta prefetch for table " + table);
}
} catch (InterruptedException e) {
System.err.println("Interrupted during prefetch");
Thread.currentThread().interrupt();
} catch (Exception e) {
throw new DBException("Failed prefetch", e);
}
}
}
}
} catch (IOException e) {
throw new DBException("Failed instantiation of client", e);
}
}
Aggregations