Search in sources :

Example 6 with TreeRule

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

the class TestTreeRule method storeRuleMege.

@Test
public void storeRuleMege() throws Exception {
    setupStorage();
    final TreeRule rule = new TreeRule(1);
    rule.setLevel(2);
    rule.setOrder(1);
    rule.setNotes("Just some notes");
    assertTrue(rule.syncToStorage(storage.getTSDB(), false).joinUninterruptibly());
    assertEquals(2, storage.numColumns(TREE_TABLE, new byte[] { 0, 1 }));
    final TreeRule stored = JSON.parseToObject(storage.getColumn(TREE_TABLE, new byte[] { 0, 1 }, Tree.TREE_FAMILY(), "tree_rule:2:1".getBytes(MockBase.ASCII())), TreeRule.class);
    assertEquals("Host owner", stored.getDescription());
    assertEquals("Just some notes", stored.getNotes());
}
Also used : TreeRule(net.opentsdb.tree.TreeRule) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with TreeRule

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

the class TestTreeRule method storeRuleInvalidMissingCustomFieldTagkCustom.

@Test(expected = IllegalArgumentException.class)
public void storeRuleInvalidMissingCustomFieldTagkCustom() throws Exception {
    setupStorage();
    final TreeRule rule = new TreeRule(1);
    rule.setLevel(1);
    rule.setOrder(0);
    rule.setType(TreeRuleType.TAGK_CUSTOM);
    rule.setNotes("Just some notes");
    rule.setField("foo");
    rule.syncToStorage(storage.getTSDB(), false).joinUninterruptibly();
}
Also used : TreeRule(net.opentsdb.tree.TreeRule) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with TreeRule

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

the class TestTreeRule method storeRuleInvalidMissingFieldTagk.

@Test(expected = IllegalArgumentException.class)
public void storeRuleInvalidMissingFieldTagk() throws Exception {
    setupStorage();
    final TreeRule rule = new TreeRule(1);
    rule.setLevel(1);
    rule.setOrder(0);
    rule.setType(TreeRuleType.TAGK);
    rule.setNotes("Just some notes");
    rule.syncToStorage(storage.getTSDB(), false).joinUninterruptibly();
}
Also used : TreeRule(net.opentsdb.tree.TreeRule) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with TreeRule

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

the class TreeRpc method handleRules.

/**
   * Handles requests to replace or delete all of the rules in the given tree.
   * It's an efficiency helper for cases where folks don't want to make a single
   * call per rule when updating many rules at once.
   * @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 handleRules(TSDB tsdb, HttpQuery query) {
    int tree_id = 0;
    List<TreeRule> rules = null;
    if (query.hasContent()) {
        rules = query.serializer().parseTreeRulesV1();
        if (rules == null || rules.isEmpty()) {
            throw new BadRequestException("Missing tree rules");
        }
        // validate that they all belong to the same tree
        tree_id = rules.get(0).getTreeId();
        for (TreeRule rule : rules) {
            if (rule.getTreeId() != tree_id) {
                throw new BadRequestException("All rules must belong to the same tree");
            }
        }
    } else {
        tree_id = parseTreeId(query, false);
    }
    // make sure the tree exists
    try {
        if (Tree.fetchTree(tsdb, tree_id).joinUninterruptibly() == null) {
            throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to locate tree: " + tree_id);
        }
        if (query.getAPIMethod() == HttpMethod.POST || query.getAPIMethod() == HttpMethod.PUT) {
            if (rules == null || rules.isEmpty()) {
                if (rules == null || rules.isEmpty()) {
                    throw new BadRequestException("Missing tree rules");
                }
            }
            // purge the existing tree rules if we're told to PUT
            if (query.getAPIMethod() == HttpMethod.PUT) {
                TreeRule.deleteAllRules(tsdb, tree_id).joinUninterruptibly();
            }
            for (TreeRule rule : rules) {
                rule.syncToStorage(tsdb, query.getAPIMethod() == HttpMethod.PUT).joinUninterruptibly();
            }
            query.sendStatusOnly(HttpResponseStatus.NO_CONTENT);
        } else if (query.getAPIMethod() == HttpMethod.DELETE) {
            TreeRule.deleteAllRules(tsdb, tree_id).joinUninterruptibly();
            query.sendStatusOnly(HttpResponseStatus.NO_CONTENT);
        } else {
            throw new BadRequestException(HttpResponseStatus.BAD_REQUEST, "Unsupported HTTP request method");
        }
    } catch (BadRequestException e) {
        throw e;
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TreeRule(net.opentsdb.tree.TreeRule) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) DeferredGroupException(com.stumbleupon.async.DeferredGroupException)

Example 10 with TreeRule

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

the class TreeRpc method handleRule.

/**
   * Handles the CRUD calls for a single rule, enabling adding, editing or 
   * deleting the rule
   * @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 handleRule(TSDB tsdb, HttpQuery query) {
    final TreeRule rule;
    if (query.hasContent()) {
        rule = query.serializer().parseTreeRuleV1();
    } else {
        rule = parseRule(query);
    }
    try {
        // no matter what, we'll need a tree to work with, so make sure it exists
        Tree tree = null;
        tree = Tree.fetchTree(tsdb, rule.getTreeId()).joinUninterruptibly();
        if (tree == null) {
            throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to locate tree: " + rule.getTreeId());
        }
        // if get, then we're just returning a rule from a tree
        if (query.getAPIMethod() == HttpMethod.GET) {
            final TreeRule tree_rule = tree.getRule(rule.getLevel(), rule.getOrder());
            if (tree_rule == null) {
                throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to locate rule: " + rule);
            }
            query.sendReply(query.serializer().formatTreeRuleV1(tree_rule));
        } else if (query.getAPIMethod() == HttpMethod.POST || query.getAPIMethod() == HttpMethod.PUT) {
            if (rule.syncToStorage(tsdb, (query.getAPIMethod() == HttpMethod.PUT)).joinUninterruptibly()) {
                final TreeRule stored_rule = TreeRule.fetchRule(tsdb, rule.getTreeId(), rule.getLevel(), rule.getOrder()).joinUninterruptibly();
                query.sendReply(query.serializer().formatTreeRuleV1(stored_rule));
            } else {
                throw new RuntimeException("Unable to save rule " + rule + " to storage");
            }
        } else if (query.getAPIMethod() == HttpMethod.DELETE) {
            if (tree.getRule(rule.getLevel(), rule.getOrder()) == null) {
                throw new BadRequestException(HttpResponseStatus.NOT_FOUND, "Unable to locate rule: " + rule);
            }
            TreeRule.deleteRule(tsdb, tree.getTreeId(), rule.getLevel(), rule.getOrder()).joinUninterruptibly();
            query.sendStatusOnly(HttpResponseStatus.NO_CONTENT);
        } else {
            throw new BadRequestException(HttpResponseStatus.BAD_REQUEST, "Unsupported HTTP request method");
        }
    } catch (BadRequestException e) {
        throw e;
    } catch (IllegalStateException e) {
        query.sendStatusOnly(HttpResponseStatus.NOT_MODIFIED);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TreeRule(net.opentsdb.tree.TreeRule) Tree(net.opentsdb.tree.Tree) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) DeferredGroupException(com.stumbleupon.async.DeferredGroupException)

Aggregations

TreeRule (net.opentsdb.tree.TreeRule)22 Test (org.junit.Test)17 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)2 IOException (java.io.IOException)2 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 Tree (net.opentsdb.tree.Tree)2 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 TSDB (net.opentsdb.core.TSDB)1 MockBase (net.opentsdb.storage.MockBase)1 Branch (net.opentsdb.tree.Branch)1 Leaf (net.opentsdb.tree.Leaf)1 TestTree (net.opentsdb.tree.TestTree)1 Config (net.opentsdb.utils.Config)1 Before (org.junit.Before)1