@Test public void testSubmittedQueryCount() throws Exception { String json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.TIMER, MetricsConstant.HS2_SUBMITTED_QURIES, "0"); operation.onNewState(OperationState.FINISHED, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.TIMER, MetricsConstant.HS2_SUBMITTED_QURIES, "1"); }
@Test public void testOpenSessionTimeMetrics() throws Exception { String json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, "NaN"); long firstSessionOpen = System.currentTimeMillis(); SessionHandle handle = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>()); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, (double)(System.currentTimeMillis() - firstSessionOpen), 100d); long secondSessionOpen = System.currentTimeMillis(); sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>()); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, (double)(System.currentTimeMillis() - firstSessionOpen + System.currentTimeMillis() - secondSessionOpen) / 2d, 100d); sm.closeSession(handle); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_OPEN_SESSION_TIME, (double)(System.currentTimeMillis() - secondSessionOpen), 100d); }
@Test public void testOpenSessionMetrics() throws Exception { String json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_OPEN_SESSIONS, 0); SessionHandle handle = sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>()); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_OPEN_SESSIONS, 1); sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>()); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_OPEN_SESSIONS, 2); sm.closeSession(handle); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_OPEN_SESSIONS, 1); }
@Test public void testActiveUserQueriesCount() throws Exception { String name = MetricsConstant.SQL_OPERATION_PREFIX + "active_user"; String json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, name, ""); operation.onNewState(OperationState.RUNNING, OperationState.INITIALIZED); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, name, "1"); operation.onNewState(OperationState.RUNNING, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, name, "1"); operation.onNewState(OperationState.FINISHED, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, name, "0"); }
@Test public void testSucceededQueriesCount() throws Exception { String json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_SUCCEEDED_QUERIES, ""); operation.onNewState(OperationState.FINISHED, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_SUCCEEDED_QUERIES, "1"); operation.onNewState(OperationState.ERROR, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_SUCCEEDED_QUERIES, "1"); operation.onNewState(OperationState.CANCELED, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_SUCCEEDED_QUERIES, "1"); operation.onNewState(OperationState.FINISHED, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_SUCCEEDED_QUERIES, "2"); }
@Test public void testAbandonedSessionMetrics() throws Exception { sm.start(); String json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, MetricsConstant.HS2_ABANDONED_SESSIONS, ""); sm.openSession(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V9, "user", "passw", "127.0.0.1", new HashMap<String, String>()); // We're going to wait for the session to be abandoned. String currentValue; int count = 5; // how many times we'll sleep before giving up String expectedValue = "1"; do { // HIVE_SERVER2_SESSION_CHECK_INTERVAL is set to 3 seconds, so we have to wait for at least // that long to see an abandoned session Thread.sleep(3200); json = metrics.dumpJson(); currentValue = MetricsTestUtils .getJsonNode(json, MetricsTestUtils.COUNTER, MetricsConstant.HS2_ABANDONED_SESSIONS) .asText(); // loop until the value is correct or we run out of tries } while (!expectedValue.equals(currentValue) && --count > 0); Assert.assertEquals(expectedValue, currentValue); } }
@Test public void testFailedQueriesCount() throws Exception { String json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_FAILED_QUERIES, ""); operation.onNewState(OperationState.ERROR, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_FAILED_QUERIES, "1"); operation.onNewState(OperationState.FINISHED, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_FAILED_QUERIES, "1"); operation.onNewState(OperationState.CANCELED, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_FAILED_QUERIES, "1"); operation.onNewState(OperationState.ERROR, OperationState.RUNNING); json = ((CodahaleMetrics) metrics).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, MetricsConstant.HS2_FAILED_QUERIES, "2"); }
String json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_ACTIVE_SESSIONS, 0); ready.reset(); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_ACTIVE_SESSIONS, 1); ready.await(2, TimeUnit.SECONDS); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_ACTIVE_SESSIONS, 0);
CyclicBarrier completed = new CyclicBarrier(2); String json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, "NaN"); ready.reset(); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, (double)System.currentTimeMillis() - sessionActivateTime, 100d); ready.await(2, TimeUnit.SECONDS); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.HS2_AVG_ACTIVE_SESSION_TIME, "NaN");
@Test public void testMeter() throws Exception { String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, "meter", ""); MetricsFactory.getInstance().markMeter("meter"); json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, "meter", "1"); MetricsFactory.getInstance().markMeter("meter"); json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.METER, "meter", "2"); }
ready.reset(); String json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.EXEC_ASYNC_POOL_SIZE, 2); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.EXEC_ASYNC_QUEUE_SIZE, 2); ready.await(BARRIER_AWAIT_TIMEOUT, TimeUnit.SECONDS); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.EXEC_ASYNC_POOL_SIZE, 2); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, MetricsConstant.EXEC_ASYNC_QUEUE_SIZE, 0);
@Test public void testGauge() throws Exception { TestMetricsVariable testVar = new TestMetricsVariable(); testVar.setValue(20); MetricsFactory.getInstance().addGauge("gauge1", testVar); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "gauge1", testVar.getValue()); testVar.setValue(40); json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "gauge1", testVar.getValue()); }
@Test public void testRatioIsCalculatedNonExact() throws Exception { NumericVariable num = new NumericVariable(20); NumericVariable ord = new NumericVariable(3); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", 6.6666d, 1e-4); }
@Test public void testEmptyRatio() throws Exception { NumericVariable num = new NumericVariable(null); NumericVariable ord = new NumericVariable(null); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", "NaN"); }
@Test public void testZeroRatio() throws Exception { NumericVariable num = new NumericVariable(10); NumericVariable ord = new NumericVariable(0); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", "NaN"); }
@Test public void testRatioIsCalculated() throws Exception { NumericVariable num = new NumericVariable(10); NumericVariable ord = new NumericVariable(5); MetricsFactory.getInstance().addRatio("rat", num, ord); String json = ((CodahaleMetrics) MetricsFactory.getInstance()).dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.GAUGE, "rat", 2d); }
@Test public void testMetrics() throws Exception{ conf.setVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_QUORUM, "localhost"); conf.setVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_CLIENT_PORT, String.valueOf(server.getPort())); conf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_METRICS_ENABLED, true); conf.setBoolVar(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY, false); conf.setVar(HiveConf.ConfVars.HIVE_METRICS_REPORTER, MetricsReporting.JSON_FILE.name() + "," + MetricsReporting.JMX.name()); MetricsFactory.init(conf); CodahaleMetrics metrics = (CodahaleMetrics) MetricsFactory.getInstance(); HiveLockManagerCtx ctx = new HiveLockManagerCtx(conf); ZooKeeperHiveLockManager zMgr= new ZooKeeperHiveLockManager(); zMgr.setContext(ctx); ZooKeeperHiveLock curLock = zMgr.lock(hiveLock, HiveLockMode.SHARED, false); String json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, MetricsConstant.ZOOKEEPER_HIVE_SHAREDLOCKS, 1); zMgr.unlock(curLock); json = metrics.dumpJson(); MetricsTestUtils.verifyMetricsJson(json, MetricsTestUtils.COUNTER, MetricsConstant.ZOOKEEPER_HIVE_SHAREDLOCKS, 0); zMgr.close(); }