Search in sources :

Example 56 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class Tree method fetchTree.

/**
   * Attempts to fetch the given tree from storage, loading the rule set at
   * the same time.
   * @param tsdb The TSDB to use for access
   * @param tree_id The Tree to fetch
   * @return A tree object if found, null if the tree did not exist
   * @throws IllegalArgumentException if the tree ID was invalid
   * @throws HBaseException if a storage exception occurred
   * @throws JSONException if the object could not be deserialized
   */
public static Deferred<Tree> fetchTree(final TSDB tsdb, final int tree_id) {
    if (tree_id < 1 || tree_id > 65535) {
        throw new IllegalArgumentException("Invalid Tree ID");
    }
    // fetch the whole row
    final GetRequest get = new GetRequest(tsdb.treeTable(), idToBytes(tree_id));
    get.family(TREE_FAMILY);
    /**
     * Called from the GetRequest with results from storage. Loops through the
     * columns and loads the tree definition and rules
     */
    final class FetchTreeCB implements Callback<Deferred<Tree>, ArrayList<KeyValue>> {

        @Override
        public Deferred<Tree> call(ArrayList<KeyValue> row) throws Exception {
            if (row == null || row.isEmpty()) {
                return Deferred.fromResult(null);
            }
            final Tree tree = new Tree();
            // WARNING: Since the JSON in storage doesn't store the tree ID, we need
            // to loadi t from the row key.
            tree.setTreeId(bytesToId(row.get(0).key()));
            for (KeyValue column : row) {
                if (Bytes.memcmp(TREE_QUALIFIER, column.qualifier()) == 0) {
                    // it's *this* tree. We deserialize to a new object and copy
                    // since the columns could be in any order and we may get a rule 
                    // before the tree object
                    final Tree local_tree = JSON.parseToObject(column.value(), Tree.class);
                    tree.created = local_tree.created;
                    tree.description = local_tree.description;
                    tree.name = local_tree.name;
                    tree.notes = local_tree.notes;
                    tree.strict_match = local_tree.strict_match;
                    tree.enabled = local_tree.enabled;
                    tree.store_failures = local_tree.store_failures;
                // Tree rule
                } else if (Bytes.memcmp(TreeRule.RULE_PREFIX(), column.qualifier(), 0, TreeRule.RULE_PREFIX().length) == 0) {
                    final TreeRule rule = TreeRule.parseFromStorage(column);
                    tree.addRule(rule);
                }
            }
            return Deferred.fromResult(tree);
        }
    }
    // issue the get request
    return tsdb.getClient().get(get).addCallbackDeferring(new FetchTreeCB());
}
Also used : KeyValue(org.hbase.async.KeyValue) Callback(com.stumbleupon.async.Callback) GetRequest(org.hbase.async.GetRequest) ArrayList(java.util.ArrayList)

Example 57 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestAppendDataPoints method parseKeyValueNotAppends.

@Test
public void parseKeyValueNotAppends() throws Exception {
    // regular data points
    try {
        KeyValue kv = new KeyValue(ROW_KEY, CF, DPQ_S, DPV);
        new AppendDataPoints().parseKeyValue(tsdb, kv);
        fail("Expected an IllegalArgumentException");
    } catch (IllegalArgumentException iae) {
    }
    try {
        KeyValue kv = new KeyValue(ROW_KEY, CF, DPQ_MS, DPV);
        new AppendDataPoints().parseKeyValue(tsdb, kv);
        fail("Expected an IllegalArgumentException");
    } catch (IllegalArgumentException iae) {
    }
    // different object
    try {
        KeyValue kv = new KeyValue(ROW_KEY, CF, new byte[] { 1, 0, 0 }, DPV);
        new AppendDataPoints().parseKeyValue(tsdb, kv);
        fail("Expected an IllegalArgumentException");
    } catch (IllegalArgumentException iae) {
    }
    // bad coder!
    try {
        new AppendDataPoints().parseKeyValue(tsdb, null);
        fail("Expected an NullPointerException");
    } catch (NullPointerException iae) {
    }
}
Also used : KeyValue(org.hbase.async.KeyValue) Test(org.junit.Test)

Example 58 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestAppendDataPoints method repairDuplicates.

@Test
public void repairDuplicates() throws Exception {
    setDataPointStorage();
    Whitebox.setInternalState(config, "repair_appends", true);
    final KeyValue kv = new KeyValue(ROW_KEY, CF, AppendDataPoints.APPEND_COLUMN_QUALIFIER, MockBase.concatByteArrays(DPQ_MS, DPV, DPQ_S, DPV2, DPQ_S, DPV2));
    final AppendDataPoints adp = new AppendDataPoints();
    final Collection<Cell> cells = adp.parseKeyValue(tsdb, kv);
    assertEquals(2, cells.size());
    final Iterator<Cell> iterator = cells.iterator();
    Cell cell = iterator.next();
    assertArrayEquals(DPV, cell.value);
    assertEquals(1356998400128L, cell.timestamp(1356998400));
    cell = iterator.next();
    assertArrayEquals(DPV2, cell.value);
    assertEquals(1356998402000L, cell.timestamp(1356998400));
    assertArrayEquals(MockBase.concatByteArrays(DPQ_MS, DPQ_S), adp.qualifier());
    assertArrayEquals(MockBase.concatByteArrays(DPV, DPV2), adp.value());
    verify(client, times(1)).put(any(PutRequest.class));
    adp.repairedDeferred().join();
    assertArrayEquals(MockBase.concatByteArrays(DPQ_MS, DPV, DPQ_S, DPV2), storage.getColumn(ROW_KEY, AppendDataPoints.APPEND_COLUMN_QUALIFIER));
}
Also used : KeyValue(org.hbase.async.KeyValue) PutRequest(org.hbase.async.PutRequest) Cell(net.opentsdb.core.Internal.Cell) Test(org.junit.Test)

Example 59 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestAppendDataPoints method repairOutOfOrder.

@Test
public void repairOutOfOrder() throws Exception {
    setDataPointStorage();
    Whitebox.setInternalState(config, "repair_appends", true);
    final KeyValue kv = new KeyValue(ROW_KEY, CF, AppendDataPoints.APPEND_COLUMN_QUALIFIER, MockBase.concatByteArrays(DPQ_S, DPV2, DPQ_MS, DPV));
    final AppendDataPoints adp = new AppendDataPoints();
    final Collection<Cell> cells = adp.parseKeyValue(tsdb, kv);
    assertEquals(2, cells.size());
    final Iterator<Cell> iterator = cells.iterator();
    Cell cell = iterator.next();
    assertArrayEquals(DPV, cell.value);
    assertEquals(1356998400128L, cell.timestamp(1356998400));
    cell = iterator.next();
    assertArrayEquals(DPV2, cell.value);
    assertEquals(1356998402000L, cell.timestamp(1356998400));
    assertArrayEquals(MockBase.concatByteArrays(DPQ_MS, DPQ_S), adp.qualifier());
    assertArrayEquals(MockBase.concatByteArrays(DPV, DPV2), adp.value());
    verify(client, times(1)).put(any(PutRequest.class));
    adp.repairedDeferred().join();
    assertArrayEquals(MockBase.concatByteArrays(DPQ_MS, DPV, DPQ_S, DPV2), storage.getColumn(ROW_KEY, AppendDataPoints.APPEND_COLUMN_QUALIFIER));
}
Also used : KeyValue(org.hbase.async.KeyValue) PutRequest(org.hbase.async.PutRequest) Cell(net.opentsdb.core.Internal.Cell) Test(org.junit.Test)

Example 60 with KeyValue

use of org.hbase.async.KeyValue in project opentsdb by OpenTSDB.

the class TestCompactionQueue method oneCellRow.

@Test
public void oneCellRow() throws Exception {
    ArrayList<KeyValue> kvs = new ArrayList<KeyValue>(1);
    ArrayList<Annotation> annotations = new ArrayList<Annotation>(0);
    final byte[] qual = { 0x00, 0x07 };
    final byte[] val = Bytes.fromLong(42L);
    kvs.add(makekv(qual, val));
    final KeyValue kv = compactionq.compact(kvs, annotations);
    assertArrayEquals(qual, kv.qualifier());
    assertArrayEquals(val, kv.value());
    // We had nothing to do so...
    // ... verify there were no put.
    verify(tsdb, never()).put(anyBytes(), anyBytes(), anyBytes());
    // ... verify there were no delete.
    verify(tsdb, never()).delete(anyBytes(), any(byte[][].class));
}
Also used : KeyValue(org.hbase.async.KeyValue) ArrayList(java.util.ArrayList) Annotation(net.opentsdb.meta.Annotation) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

KeyValue (org.hbase.async.KeyValue)171 Test (org.junit.Test)127 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)121 ArrayList (java.util.ArrayList)101 Annotation (net.opentsdb.meta.Annotation)50 Callback (com.stumbleupon.async.Callback)30 GetRequest (org.hbase.async.GetRequest)21 Scanner (org.hbase.async.Scanner)19 Deferred (com.stumbleupon.async.Deferred)14 HBaseException (org.hbase.async.HBaseException)13 TSDB (net.opentsdb.core.TSDB)12 Matchers.anyString (org.mockito.Matchers.anyString)11 Config (net.opentsdb.utils.Config)10 UniqueIdType (net.opentsdb.uid.UniqueId.UniqueIdType)9 DeleteRequest (org.hbase.async.DeleteRequest)8 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)7 Map (java.util.Map)7 HashMap (java.util.HashMap)6 PutRequest (org.hbase.async.PutRequest)6 List (java.util.List)5