Repository: ambari Updated Branches: refs/heads/branch-2.4 43200145d -> eb93cdf6c
AMBARI-16926 Add ability to query all hosts using hostname=% through AMS API. (dsen) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/eb93cdf6 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/eb93cdf6 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/eb93cdf6 Branch: refs/heads/branch-2.4 Commit: eb93cdf6c916d7f260e2326a985fab3a4996ca7b Parents: 4320014 Author: Dmytro Sen <[email protected]> Authored: Fri May 27 19:46:12 2016 +0300 Committer: Dmytro Sen <[email protected]> Committed: Fri May 27 19:47:16 2016 +0300 ---------------------------------------------------------------------- .../timeline/query/DefaultCondition.java | 23 +++++++- .../timeline/TestPhoenixTransactSQL.java | 55 +++++++++++++++++++- 2 files changed, 74 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/eb93cdf6/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/query/DefaultCondition.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/query/DefaultCondition.java b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/query/DefaultCondition.java index b1159c3..81528df 100644 --- a/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/query/DefaultCondition.java +++ b/ambari-metrics/ambari-metrics-timelineservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/query/DefaultCondition.java @@ -258,10 +258,29 @@ public class DefaultCondition implements Condition { } protected boolean appendHostnameClause(StringBuilder sb, boolean appendConjunction) { + boolean hostnameContainsRegex = false; + if (hostnames != null) { + for (String hostname : hostnames) { + if (hostname.contains("%")) { + hostnameContainsRegex = true; + break; + } + } + } - if (hostnames != null && getHostnames().size() > 1) { - StringBuilder hostnamesCondition = new StringBuilder(); + StringBuilder hostnamesCondition = new StringBuilder(); + if (hostnameContainsRegex) { + hostnamesCondition.append(" ("); + for (String hostname : getHostnames()) { + if (hostnamesCondition.length() > 2) { + hostnamesCondition.append(" OR "); + } + hostnamesCondition.append("HOSTNAME LIKE ?"); + } + hostnamesCondition.append(")"); + appendConjunction = append(sb, appendConjunction, getHostnames(), hostnamesCondition.toString()); + } else if (hostnames != null && getHostnames().size() > 1) { for (String hostname : getHostnames()) { if (hostnamesCondition.length() > 0) { hostnamesCondition.append(" ,"); http://git-wip-us.apache.org/repos/asf/ambari/blob/eb93cdf6/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java ---------------------------------------------------------------------- diff --git a/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java b/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java index 888234f..b4cee67 100644 --- a/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java +++ b/ambari-metrics/ambari-metrics-timelineservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/metrics/timeline/TestPhoenixTransactSQL.java @@ -575,7 +575,7 @@ public class TestPhoenixTransactSQL { "GROUP BY METRIC_NAME, HOSTNAME, APP_ID ORDER BY MAX(METRIC_MAX) DESC LIMIT 2) " + "AND APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?"; - Assert.assertEquals(conditionClause, expectedClause); + Assert.assertEquals(expectedClause, conditionClause); } @Test @@ -597,7 +597,7 @@ public class TestPhoenixTransactSQL { "GROUP BY METRIC_NAME, APP_ID ORDER BY MAX(METRIC_MAX) DESC LIMIT 2) " + "AND HOSTNAME = ? AND APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?"; - Assert.assertEquals(conditionClause, expectedClause); + Assert.assertEquals(expectedClause, conditionClause); } @Test @@ -612,4 +612,55 @@ public class TestPhoenixTransactSQL { Assert.assertEquals(condition.getConditionClause(), null); } + + @Test + public void testHostsRegexpConditionClause() { + Condition condition = new TopNCondition( + Arrays.asList("m1"), Arrays.asList("%.ambari", "host1.apache"), + "a1", "i1", 1407959718L, 1407959918L, null, null, false, 2, null, false); + + String conditionClause = condition.getConditionClause().toString(); + String expectedClause = "(METRIC_NAME IN (?)) AND HOSTNAME IN (" + + "SELECT " + PhoenixTransactSQL.getNaiveTimeRangeHint(condition.getStartTime(),120000l) + + " HOSTNAME FROM METRIC_RECORD WHERE " + + "(METRIC_NAME IN (?)) AND " + + "(HOSTNAME LIKE ? OR HOSTNAME LIKE ?) AND " + + "APP_ID = ? AND INSTANCE_ID = ? AND " + + "SERVER_TIME >= ? AND SERVER_TIME < ? " + + "GROUP BY METRIC_NAME, HOSTNAME, APP_ID ORDER BY MAX(METRIC_MAX) DESC LIMIT 2) " + + "AND APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?"; + Assert.assertEquals(expectedClause, conditionClause); + + condition = new TopNCondition( + Arrays.asList("m1", "m2", "m3"), Arrays.asList("%.ambari"), + "a1", "i1", 1407959718L, 1407959918L, null, null, false, 2, null, false); + + conditionClause = condition.getConditionClause().toString(); + expectedClause = " METRIC_NAME IN (" + + "SELECT " + PhoenixTransactSQL.getNaiveTimeRangeHint(condition.getStartTime(),120000l) + + " METRIC_NAME FROM METRIC_RECORD WHERE " + + "(METRIC_NAME IN (?, ?, ?)) AND " + + "(HOSTNAME LIKE ?) AND " + + "APP_ID = ? AND INSTANCE_ID = ? AND " + + "SERVER_TIME >= ? AND SERVER_TIME < ? " + + "GROUP BY METRIC_NAME, APP_ID ORDER BY MAX(METRIC_MAX) DESC LIMIT 2) " + + "AND (HOSTNAME LIKE ?) AND APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?"; + Assert.assertEquals(expectedClause, conditionClause); + + condition = new TopNCondition( + Arrays.asList("m1", "m2", "m3"), Arrays.asList("h1.ambari"), + "a1", "i1", 1407959718L, 1407959918L, null, null, false, 2, null, false); + + conditionClause = condition.getConditionClause().toString(); + expectedClause = " METRIC_NAME IN (" + + "SELECT " + PhoenixTransactSQL.getNaiveTimeRangeHint(condition.getStartTime(),120000l) + + " METRIC_NAME FROM METRIC_RECORD WHERE " + + "(METRIC_NAME IN (?, ?, ?)) AND " + + "HOSTNAME = ? AND " + + "APP_ID = ? AND INSTANCE_ID = ? AND " + + "SERVER_TIME >= ? AND SERVER_TIME < ? " + + "GROUP BY METRIC_NAME, APP_ID ORDER BY MAX(METRIC_MAX) DESC LIMIT 2) " + + "AND HOSTNAME = ? AND APP_ID = ? AND INSTANCE_ID = ? AND SERVER_TIME >= ? AND SERVER_TIME < ?"; + Assert.assertEquals(expectedClause, conditionClause); + } }
