Search in sources :

Example 1 with Branch

use of net.opentsdb.tree.Branch in project opentsdb by OpenTSDB.

the class TreeRpc method handleBranch.

/**
   * Attempts to retrieve a single branch and return it to the user. If the 
   * requested branch doesn't exist, it returns a 404.
   * @param tsdb The TSDB to which we belong
   * @param query The HTTP query to work with
   * @throws BadRequestException if the request was invalid.
   */
private void handleBranch(TSDB tsdb, HttpQuery query) {
    if (query.getAPIMethod() != HttpMethod.GET) {
        throw new BadRequestException(HttpResponseStatus.BAD_REQUEST, "Unsupported HTTP request method");
    }
    try {
        final int tree_id = parseTreeId(query, false);
        final String branch_hex = query.getQueryStringParam("branch");
        // compile the branch ID. If the user did NOT supply a branch address, 
        // that would include the tree ID, then we fall back to the tree ID and
        // the root for that tree
        final byte[] branch_id;
        if (branch_hex == null || branch_hex.isEmpty()) {
            if (tree_id < 1) {
                throw new BadRequestException("Missing or invalid branch and tree IDs");
            }
            branch_id = Tree.idToBytes(tree_id);
        } else {
            branch_id = Branch.stringToId(branch_hex);
        }
        // fetch it
        final Branch branch = Branch.fetchBranch(tsdb, branch_id, true).joinUninterruptibly();
        if (branch == null) {
            throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to locate branch '" + Branch.idToString(branch_id) + "' for tree '" + Tree.bytesToId(branch_id) + "'");
        }
        query.sendReply(query.serializer().formatBranchV1(branch));
    } catch (BadRequestException e) {
        throw e;
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Branch(net.opentsdb.tree.Branch) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) DeferredGroupException(com.stumbleupon.async.DeferredGroupException)

Example 2 with Branch

use of net.opentsdb.tree.Branch in project opentsdb by OpenTSDB.

the class TestTreeRpc method setupStorage.

/**
   * Setups objects in MockBase including two trees, rule sets, root branch,
   * child branch, leaves and some collisions and no matches. These are used for
   * most of the tests so they're all here.
   */
private void setupStorage() throws Exception {
    Tree tree = TestTree.buildTestTree();
    // store root
    TreeMap<Integer, String> root_path = new TreeMap<Integer, String>();
    Branch root = new Branch(tree.getTreeId());
    root.setDisplayName("ROOT");
    root_path.put(0, "ROOT");
    root.prependParentPath(root_path);
    storage.addColumn(TREE_TABLE, root.compileBranchId(), Tree.TREE_FAMILY(), "branch".getBytes(MockBase.ASCII()), (byte[]) branchToStorageJson.invoke(root));
    // store the first tree
    byte[] key = new byte[] { 0, 1 };
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), "tree".getBytes(MockBase.ASCII()), (byte[]) TreetoStorageJson.invoke(TestTree.buildTestTree()));
    TreeRule rule = new TreeRule(1);
    rule.setField("host");
    rule.setDescription("Hostname rule");
    rule.setType(TreeRuleType.TAGK);
    rule.setDescription("Host Name");
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), "tree_rule:0:0".getBytes(MockBase.ASCII()), JSON.serializeToBytes(rule));
    rule = new TreeRule(1);
    rule.setField("");
    rule.setLevel(1);
    rule.setNotes("Metric rule");
    rule.setType(TreeRuleType.METRIC);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), "tree_rule:1:0".getBytes(MockBase.ASCII()), JSON.serializeToBytes(rule));
    root = new Branch(1);
    root.setDisplayName("ROOT");
    root_path = new TreeMap<Integer, String>();
    root_path.put(0, "ROOT");
    root.prependParentPath(root_path);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), "branch".getBytes(MockBase.ASCII()), (byte[]) branchToStorageJson.invoke(root));
    // tree 2
    key = new byte[] { 0, 2 };
    Tree tree2 = new Tree();
    tree2.setTreeId(2);
    tree2.setName("2nd Tree");
    tree2.setDescription("Other Tree");
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), "tree".getBytes(MockBase.ASCII()), (byte[]) TreetoStorageJson.invoke(tree2));
    rule = new TreeRule(2);
    rule.setField("host");
    rule.setType(TreeRuleType.TAGK);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), "tree_rule:0:0".getBytes(MockBase.ASCII()), JSON.serializeToBytes(rule));
    rule = new TreeRule(2);
    rule.setField("");
    rule.setLevel(1);
    rule.setType(TreeRuleType.METRIC);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), "tree_rule:1:0".getBytes(MockBase.ASCII()), JSON.serializeToBytes(rule));
    root = new Branch(2);
    root.setDisplayName("ROOT");
    root_path = new TreeMap<Integer, String>();
    root_path.put(0, "ROOT");
    root.prependParentPath(root_path);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), "branch".getBytes(MockBase.ASCII()), (byte[]) branchToStorageJson.invoke(root));
    // sprinkle in some collisions and no matches for fun
    // collisions
    key = new byte[] { 0, 1, 1 };
    String tsuid = "010101";
    byte[] qualifier = new byte[Tree.COLLISION_PREFIX().length + (tsuid.length() / 2)];
    System.arraycopy(Tree.COLLISION_PREFIX(), 0, qualifier, 0, Tree.COLLISION_PREFIX().length);
    byte[] tsuid_bytes = UniqueId.stringToUid(tsuid);
    System.arraycopy(tsuid_bytes, 0, qualifier, Tree.COLLISION_PREFIX().length, tsuid_bytes.length);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), qualifier, "AAAAAA".getBytes(MockBase.ASCII()));
    tsuid = "020202";
    qualifier = new byte[Tree.COLLISION_PREFIX().length + (tsuid.length() / 2)];
    System.arraycopy(Tree.COLLISION_PREFIX(), 0, qualifier, 0, Tree.COLLISION_PREFIX().length);
    tsuid_bytes = UniqueId.stringToUid(tsuid);
    System.arraycopy(tsuid_bytes, 0, qualifier, Tree.COLLISION_PREFIX().length, tsuid_bytes.length);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), qualifier, "BBBBBB".getBytes(MockBase.ASCII()));
    // not matched
    key = new byte[] { 0, 1, 2 };
    tsuid = "010101";
    qualifier = new byte[Tree.NOT_MATCHED_PREFIX().length + (tsuid.length() / 2)];
    System.arraycopy(Tree.NOT_MATCHED_PREFIX(), 0, qualifier, 0, Tree.NOT_MATCHED_PREFIX().length);
    tsuid_bytes = UniqueId.stringToUid(tsuid);
    System.arraycopy(tsuid_bytes, 0, qualifier, Tree.NOT_MATCHED_PREFIX().length, tsuid_bytes.length);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), qualifier, "Failed rule 0:0".getBytes(MockBase.ASCII()));
    tsuid = "020202";
    qualifier = new byte[Tree.NOT_MATCHED_PREFIX().length + (tsuid.length() / 2)];
    System.arraycopy(Tree.NOT_MATCHED_PREFIX(), 0, qualifier, 0, Tree.NOT_MATCHED_PREFIX().length);
    tsuid_bytes = UniqueId.stringToUid(tsuid);
    System.arraycopy(tsuid_bytes, 0, qualifier, Tree.NOT_MATCHED_PREFIX().length, tsuid_bytes.length);
    storage.addColumn(TREE_TABLE, key, Tree.TREE_FAMILY(), qualifier, "Failed rule 1:1".getBytes(MockBase.ASCII()));
    // drop some branches in for tree 1
    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[]) branchToStorageJson.invoke(branch));
    Leaf leaf = new Leaf("user", "000001000001000001");
    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[]) branchToStorageJson.invoke(branch));
    leaf = new Leaf("Asus", "000003000003000003");
    qualifier = leaf.columnQualifier();
    storage.addColumn(TREE_TABLE, branch.compileBranchId(), Tree.TREE_FAMILY(), qualifier, (byte[]) LeaftoStorageJson.invoke(leaf));
}
Also used : TreeRule(net.opentsdb.tree.TreeRule) Branch(net.opentsdb.tree.Branch) Tree(net.opentsdb.tree.Tree) TestTree(net.opentsdb.tree.TestTree) Leaf(net.opentsdb.tree.Leaf) TreeMap(java.util.TreeMap)

Aggregations

Branch (net.opentsdb.tree.Branch)2 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)1 IOException (java.io.IOException)1 TreeMap (java.util.TreeMap)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 Leaf (net.opentsdb.tree.Leaf)1 TestTree (net.opentsdb.tree.TestTree)1 Tree (net.opentsdb.tree.Tree)1 TreeRule (net.opentsdb.tree.TreeRule)1