Java Code Examples for org.apache.kylin.rest.request.SQLRequest#setProject()

The following examples show how to use org.apache.kylin.rest.request.SQLRequest#setProject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryServiceTest.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateTableToWith() {
    String create_table1 = " create table tableId as select * from some_table1;";
    String create_table2 = "CREATE TABLE tableId2 AS select * FROM some_table2;";
    String select_table = "select * from tableId join tableId2 on tableId.a = tableId2.b;";

    KylinConfig config = KylinConfig.getInstanceFromEnv();
    config.setProperty("kylin.query.convert-create-table-to-with", "true");
    try (SetAndUnsetThreadLocalConfig autoUnset = KylinConfig.setAndUnsetThreadLocalConfig(config)) {

        SQLRequest request = new SQLRequest();
        request.setProject("default");
        request.setSql(create_table1);
        queryService.doQueryWithCache(request);

        request.setSql(create_table2);
        queryService.doQueryWithCache(request);

        request.setSql(select_table);
        SQLResponse response = queryService.doQueryWithCache(request, true);

        Assert.assertEquals(
                "WITH tableId as (select * from some_table1) , tableId2 AS (select * FROM some_table2) select * from tableId join tableId2 on tableId.a = tableId2.b;",
                response.getExceptionMessage());
    }
}
 
Example 2
Source File: KylinQueryTimeoutTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryTimeout() throws SQLException {
    thrown.expectCause(CoreMatchers.isA(KylinTimeoutException.class));
    thrown.expectMessage(CoreMatchers.containsString("Kylin query timeout"));
    StorageFactory.clearCache();
    BadQueryDetector detector = new BadQueryDetector(100, BadQueryDetector.getSystemAvailMB() * 2, 100, 1);
    detector.start();
    SQLRequest request = new SQLRequest();
    request.setProject("default");
    request.setSql("select count(*) from STREAMING_TABLE");
    detector.queryStart(Thread.currentThread(), request, "ADMIN", RandomUtil.randomUUID().toString());
    try {
        QueryACLTestUtil.mockQuery("default", "select * from STREAMING_TABLE");
    } finally{
        detector.queryEnd(Thread.currentThread(), "timeout");
        detector.interrupt();
    }
    // every place that thrown KylinTimeoutException should reset the interrupt.
    Assert.assertEquals(false, Thread.currentThread().isInterrupted());
}
 
Example 3
Source File: QueryServiceTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateTableToWith() {
    String create_table1 = " create table tableId as select * from some_table1;";
    String create_table2 = "CREATE TABLE tableId2 AS select * FROM some_table2;";
    String select_table = "select * from tableId join tableId2 on tableId.a = tableId2.b;";

    KylinConfig config = KylinConfig.getInstanceFromEnv();
    config.setProperty("kylin.query.convert-create-table-to-with", "true");
    try (SetAndUnsetThreadLocalConfig autoUnset = KylinConfig.setAndUnsetThreadLocalConfig(config)) {

        SQLRequest request = new SQLRequest();
        request.setProject("default");
        request.setSql(create_table1);
        queryService.doQueryWithCache(request);

        request.setSql(create_table2);
        queryService.doQueryWithCache(request);

        request.setSql(select_table);
        SQLResponse response = queryService.doQueryWithCache(request, true);

        Assert.assertEquals(
                "WITH tableId as (select * from some_table1) , tableId2 AS (select * FROM some_table2) select * from tableId join tableId2 on tableId.a = tableId2.b;",
                response.getExceptionMessage());
    }
}
 
Example 4
Source File: QueryServiceTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void testSyntaxError() {
    KylinConfig config = KylinConfig.getInstanceFromEnv();
    config.setProperty("kylin.query.cache-enabled", "true");
    config.setProperty("kylin.query.lazy-query-enabled", "true");

    String badSql = "select with syntax error";

    SQLRequest request = new SQLRequest();
    request.setProject("default");
    request.setSql(badSql);

    try (SetAndUnsetThreadLocalConfig autoUnset = KylinConfig.setAndUnsetThreadLocalConfig(config)) {
        queryService.doQueryWithCache(request, false);
    } catch (Exception e) {
        // expected error
        Cache.ValueWrapper wrapper = cacheManager.getCache(QueryService.QUERY_CACHE).get(request.getCacheKey());
        Assert.assertTrue(wrapper == null || wrapper.get() == null);
    }
}
 
Example 5
Source File: BadQueryDetectorTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlowQuery() throws InterruptedException, IOException {
    int alertMB = BadQueryDetector.getSystemAvailMB() * 2;
    int alertRunningSec = 2;
    String mockSql = "select * from just_a_test";

    BadQueryDetector badQueryDetector = new BadQueryDetector(alertRunningSec * 1000, alertMB, alertRunningSec, 1000);
    badQueryDetector.start();

    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setProject("test_project");
    sqlRequest.setSql(mockSql);
    badQueryDetector.queryStart(Thread.currentThread(), sqlRequest, "user", RandomUtil.randomUUID().toString());

    try {
        Thread.sleep(1000);

        QueryInfoCollector.current().setCubeNames(Lists.newArrayList("[CUBE[name=TEST_CUBE]]"));

        Thread.sleep((alertRunningSec * 2 + 1) * 1000);

        BadQueryHistory badQueryHistory = BadQueryHistoryManager.getInstance(KylinConfig.getInstanceFromEnv()).getBadQueriesForProject("test_project");
        Set entries = badQueryHistory.getEntries();

        assertEquals(1, entries.size());

        BadQueryEntry entry = (BadQueryEntry) ((NavigableSet) entries).pollFirst();

        assertNotNull(entry);
        assertEquals(BadQueryEntry.ADJ_SLOW, entry.getAdj());
        assertEquals("[CUBE[name=TEST_CUBE]]", entry.getCube());
    } finally {
        badQueryDetector.queryEnd(Thread.currentThread(), BadQueryEntry.ADJ_SLOW);
        badQueryDetector.interrupt();
        QueryInfoCollector.reset();
    }
}
 
Example 6
Source File: BadQueryDetectorTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlowQuery() throws InterruptedException, IOException {
    int alertMB = BadQueryDetector.getSystemAvailMB() * 2;
    int alertRunningSec = 2;
    String mockSql = "select * from just_a_test";

    BadQueryDetector badQueryDetector = new BadQueryDetector(alertRunningSec * 1000, alertMB, alertRunningSec, 1000);
    badQueryDetector.start();

    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setProject("test_project");
    sqlRequest.setSql(mockSql);
    badQueryDetector.queryStart(Thread.currentThread(), sqlRequest, "user", RandomUtil.randomUUID().toString());

    try {
        Thread.sleep(1000);

        QueryInfoCollector.current().setCubeNames(Lists.newArrayList("[CUBE[name=TEST_CUBE]]"));

        Thread.sleep((alertRunningSec * 2 + 1) * 1000);

        BadQueryHistory badQueryHistory = BadQueryHistoryManager.getInstance(KylinConfig.getInstanceFromEnv()).getBadQueriesForProject("test_project");
        Set entries = badQueryHistory.getEntries();

        assertEquals(1, entries.size());

        BadQueryEntry entry = (BadQueryEntry) ((NavigableSet) entries).pollFirst();

        assertNotNull(entry);
        assertEquals(BadQueryEntry.ADJ_SLOW, entry.getAdj());
        assertEquals("[CUBE[name=TEST_CUBE]]", entry.getCube());
    } finally {
        badQueryDetector.queryEnd(Thread.currentThread(), BadQueryEntry.ADJ_SLOW);
        badQueryDetector.interrupt();
        QueryInfoCollector.reset();
    }
}
 
Example 7
Source File: QueryControllerTest.java    From Kylin with Apache License 2.0 5 votes vote down vote up
@Test(expected = Exception.class)
public void testQueryException() throws Exception {
    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setSql("select * from not_exist_table");
    sqlRequest.setProject("default");
    queryController.query(sqlRequest);
}
 
Example 8
Source File: QueryMetricsTest.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryMetrics() throws Exception {
    System.setProperty("kylin.server.query-metrics-enabled", "true");
    QueryMetricsFacade.init();

    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setSql("select * from TEST_KYLIN_FACT");
    sqlRequest.setProject("default");

    SQLResponse sqlResponse = new SQLResponse();
    sqlResponse.setDuration(10);
    sqlResponse.setCube("test_cube");
    sqlResponse.setIsException(false);
    sqlResponse.setTotalScanCount(100);
    List<String> list1 = new ArrayList<>();
    list1.add("111");
    list1.add("112");
    List<String> list2 = new ArrayList<>();
    list2.add("111");
    list2.add("112");
    List<List<String>> results = new ArrayList<>();
    results.add(list1);
    results.add(list2);
    sqlResponse.setResults(results);
    sqlResponse.setStorageCacheUsed(true);

    QueryMetricsFacade.updateMetrics(sqlRequest, sqlResponse);

    Thread.sleep(2000);

    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QueryCount"));
    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QuerySuccessCount"));
    Assert.assertEquals(0L, mBeanServer.getAttribute(objectName, "QueryFailCount"));
    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "CacheHitCount"));

    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "ScanRowCountNumOps"));
    Assert.assertEquals(100.0, mBeanServer.getAttribute(objectName, "ScanRowCountAvgTime"));
    Assert.assertEquals(100.0, mBeanServer.getAttribute(objectName, "ScanRowCountMaxTime"));
    Assert.assertEquals(100.0, mBeanServer.getAttribute(objectName, "ScanRowCountMinTime"));

    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "ResultRowCountNumOps"));
    Assert.assertEquals(2.0, mBeanServer.getAttribute(objectName, "ResultRowCountMaxTime"));
    Assert.assertEquals(2.0, mBeanServer.getAttribute(objectName, "ResultRowCountAvgTime"));
    Assert.assertEquals(2.0, mBeanServer.getAttribute(objectName, "ResultRowCountMinTime"));

    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QueryLatencyNumOps"));
    Assert.assertEquals(10.0, mBeanServer.getAttribute(objectName, "QueryLatencyMaxTime"));
    Assert.assertEquals(10.0, mBeanServer.getAttribute(objectName, "QueryLatencyAvgTime"));
    Assert.assertEquals(10.0, mBeanServer.getAttribute(objectName, "QueryLatencyMinTime"));

    SQLResponse sqlResponse2 = new SQLResponse();
    sqlResponse2.setDuration(10);
    sqlResponse2.setCube("test_cube");
    sqlResponse2.setIsException(true);

    QueryMetricsFacade.updateMetrics(sqlRequest, sqlResponse2);

    Thread.sleep(2000);

    Assert.assertEquals(2L, mBeanServer.getAttribute(objectName, "QueryCount"));
    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QuerySuccessCount"));
    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QueryFailCount"));

    System.clearProperty("kylin.server.query-metrics-enabled");
}
 
Example 9
Source File: QueryMetricsTest.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryStatisticsResult() throws Exception {
    System.setProperty("kylin.metrics.reporter-query-enabled", "true");
    QueryMetricsFacade.init();

    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setSql("select * from TEST_KYLIN_FACT");
    sqlRequest.setProject("default");

    QueryContext context = QueryContextFacade.current();
    
    SQLResponse sqlResponse = new SQLResponse();
    sqlResponse.setDuration(10);
    sqlResponse.setCube("test_cube");
    sqlResponse.setIsException(false);
    sqlResponse.setTotalScanCount(100);
    List<String> list1 = new ArrayList<>();
    list1.add("111");
    list1.add("112");
    List<String> list2 = new ArrayList<>();
    list2.add("111");
    list2.add("112");
    List<List<String>> results = new ArrayList<>();
    results.add(list1);
    results.add(list2);
    sqlResponse.setResults(results);
    sqlResponse.setStorageCacheUsed(true);

    int ctxId = 0;
    context.addContext(ctxId, "OLAP", true);
    context.addRPCStatistics(ctxId, "sandbox", "test_cube", "20100101000000_20150101000000", 3L, 3L, 3L, null, 80L,
            0L, 2L, 2L, 0L, 30L);

    sqlResponse.setCubeSegmentStatisticsList(context.getCubeSegmentStatisticsResultList());

    QueryMetricsFacade.updateMetrics(sqlRequest, sqlResponse);

    Thread.sleep(2000);

    System.clearProperty("kylin.server.query-metrics-enabled");
    System.out.println("------------testQueryStatisticsResult done------------");
}
 
Example 10
Source File: QueryMetrics2Test.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryMetrics() throws Exception {
    System.setProperty("kylin.server.query-metrics2-enabled", "true");
    QueryMetrics2Facade.init();
    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setSql("select * from TEST_KYLIN_FACT");
    sqlRequest.setProject("default");
    SQLResponse sqlResponse = new SQLResponse();
    sqlResponse.setDuration(10);
    sqlResponse.setCube("test_cube");
    sqlResponse.setIsException(false);
    sqlResponse.setTotalScanCount(100);
    List<String> list1 = new ArrayList<>();
    list1.add("111");
    list1.add("112");
    List<String> list2 = new ArrayList<>();
    list2.add("111");
    list2.add("112");
    List<List<String>> results = new ArrayList<>();
    results.add(list1);
    results.add(list2);
    sqlResponse.setResults(results);
    sqlResponse.setStorageCacheUsed(true);
    Metrics metrics = MetricsFactory.getInstance();
    QueryMetrics2Facade.updateMetrics(sqlRequest, sqlResponse);
    String prefix = buildCubeMetricPrefix(sqlRequest.getProject(), sqlResponse.getCube().replace("=", "->"));
    Thread.sleep(2000);

    Assert.assertEquals(1L,
            metrics.getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_COUNT)).getCount());
    Assert.assertEquals(1L,
            metrics.getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_SUCCESS_COUNT))
                    .getCount());
    Assert.assertEquals(0L, metrics
            .getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_FAIL_COUNT)).getCount());
    Assert.assertEquals(1L, metrics
            .getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_CACHE_COUNT)).getCount());
    //
    Snapshot queryScanSnapshot = metrics
            .getHistogram(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_SCAN_ROWCOUNT))
            .getSnapshot();
    Assert.assertEquals(100.0, queryScanSnapshot.getMean(), 0);
    Assert.assertEquals(100.0, queryScanSnapshot.getMax(), 0);
    Assert.assertEquals(100.0, queryScanSnapshot.getMin(), 0);

    Snapshot queryResultSnapshot = metrics
            .getHistogram(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_RESULT_ROWCOUNT))
            .getSnapshot();

    Assert.assertEquals(2.0, queryResultSnapshot.getMean(), 0);
    Assert.assertEquals(2.0, queryResultSnapshot.getMax(), 0);
    Assert.assertEquals(2.0, queryResultSnapshot.getMin(), 0);

    Snapshot queryLatencySnapshot = metrics
            .getTimer(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_DURATION)).getSnapshot();
    Assert.assertEquals(TimeUnit.MILLISECONDS.toNanos(10), queryLatencySnapshot.getMean(), 0);
    Assert.assertEquals(TimeUnit.MILLISECONDS.toNanos(10), queryLatencySnapshot.getMax(), 0);
    Assert.assertEquals(TimeUnit.MILLISECONDS.toNanos(10), queryLatencySnapshot.getMin(), 0);

    SQLResponse sqlResponse2 = new SQLResponse();
    sqlResponse2.setDuration(10);
    sqlResponse2.setCube("test_cube");
    sqlResponse2.setIsException(true);

    QueryMetrics2Facade.updateMetrics(sqlRequest, sqlResponse2);
    prefix = buildCubeMetricPrefix(sqlRequest.getProject(), sqlResponse.getCube().replace("=", "->"));
    Assert.assertEquals(2L,
            metrics.getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_COUNT)).getCount());
    Assert.assertEquals(1L,
            metrics.getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_SUCCESS_COUNT))
                    .getCount());
    Assert.assertEquals(1L, metrics
            .getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_FAIL_COUNT)).getCount());
}
 
Example 11
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
public SQLResponse querySystemCube(String sql) {
    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setProject(MetricsManager.SYSTEM_PROJECT);
    sqlRequest.setSql(sql);
    return doQueryWithCache(sqlRequest, false);
}
 
Example 12
Source File: QueryMetricsTest.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryMetrics() throws Exception {
    System.setProperty("kylin.server.query-metrics-enabled", "true");
    QueryMetricsFacade.init();

    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setSql("select * from TEST_KYLIN_FACT");
    sqlRequest.setProject("default");

    SQLResponse sqlResponse = new SQLResponse();
    sqlResponse.setDuration(10);
    sqlResponse.setCube("test_cube");
    sqlResponse.setIsException(false);
    sqlResponse.setTotalScanCount(100);
    List<String> list1 = new ArrayList<>();
    list1.add("111");
    list1.add("112");
    List<String> list2 = new ArrayList<>();
    list2.add("111");
    list2.add("112");
    List<List<String>> results = new ArrayList<>();
    results.add(list1);
    results.add(list2);
    sqlResponse.setResults(results);
    sqlResponse.setStorageCacheUsed(true);

    QueryMetricsFacade.updateMetrics(sqlRequest, sqlResponse);

    Thread.sleep(2000);

    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QueryCount"));
    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QuerySuccessCount"));
    Assert.assertEquals(0L, mBeanServer.getAttribute(objectName, "QueryFailCount"));
    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "CacheHitCount"));

    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "ScanRowCountNumOps"));
    Assert.assertEquals(100.0, mBeanServer.getAttribute(objectName, "ScanRowCountAvgTime"));
    Assert.assertEquals(100.0, mBeanServer.getAttribute(objectName, "ScanRowCountMaxTime"));
    Assert.assertEquals(100.0, mBeanServer.getAttribute(objectName, "ScanRowCountMinTime"));

    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "ResultRowCountNumOps"));
    Assert.assertEquals(2.0, mBeanServer.getAttribute(objectName, "ResultRowCountMaxTime"));
    Assert.assertEquals(2.0, mBeanServer.getAttribute(objectName, "ResultRowCountAvgTime"));
    Assert.assertEquals(2.0, mBeanServer.getAttribute(objectName, "ResultRowCountMinTime"));

    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QueryLatencyNumOps"));
    Assert.assertEquals(10.0, mBeanServer.getAttribute(objectName, "QueryLatencyMaxTime"));
    Assert.assertEquals(10.0, mBeanServer.getAttribute(objectName, "QueryLatencyAvgTime"));
    Assert.assertEquals(10.0, mBeanServer.getAttribute(objectName, "QueryLatencyMinTime"));

    SQLResponse sqlResponse2 = new SQLResponse();
    sqlResponse2.setDuration(10);
    sqlResponse2.setCube("test_cube");
    sqlResponse2.setIsException(true);

    QueryMetricsFacade.updateMetrics(sqlRequest, sqlResponse2);

    Thread.sleep(2000);

    Assert.assertEquals(2L, mBeanServer.getAttribute(objectName, "QueryCount"));
    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QuerySuccessCount"));
    Assert.assertEquals(1L, mBeanServer.getAttribute(objectName, "QueryFailCount"));

    System.clearProperty("kylin.server.query-metrics-enabled");
}
 
Example 13
Source File: QueryMetricsTest.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryStatisticsResult() throws Exception {
    System.setProperty("kylin.metrics.reporter-query-enabled", "true");
    QueryMetricsFacade.init();

    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setSql("select * from TEST_KYLIN_FACT");
    sqlRequest.setProject("default");

    QueryContext context = QueryContextFacade.current();
    
    SQLResponse sqlResponse = new SQLResponse();
    sqlResponse.setDuration(10);
    sqlResponse.setCube("test_cube");
    sqlResponse.setIsException(false);
    sqlResponse.setTotalScanCount(100);
    List<String> list1 = new ArrayList<>();
    list1.add("111");
    list1.add("112");
    List<String> list2 = new ArrayList<>();
    list2.add("111");
    list2.add("112");
    List<List<String>> results = new ArrayList<>();
    results.add(list1);
    results.add(list2);
    sqlResponse.setResults(results);
    sqlResponse.setStorageCacheUsed(true);

    int ctxId = 0;
    context.addContext(ctxId, "OLAP", true);
    context.addRPCStatistics(ctxId, "sandbox", "test_cube", "20100101000000_20150101000000", 3L, 3L, 3L, null, 80L,
            0L, 2L, 2L, 0L, 30L);

    sqlResponse.setCubeSegmentStatisticsList(context.getCubeSegmentStatisticsResultList());

    QueryMetricsFacade.updateMetrics(sqlRequest, sqlResponse);

    Thread.sleep(2000);

    System.clearProperty("kylin.server.query-metrics-enabled");
    System.out.println("------------testQueryStatisticsResult done------------");
}
 
Example 14
Source File: QueryMetrics2Test.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryMetrics() throws Exception {
    System.setProperty("kylin.server.query-metrics2-enabled", "true");
    QueryMetrics2Facade.init();
    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setSql("select * from TEST_KYLIN_FACT");
    sqlRequest.setProject("default");
    SQLResponse sqlResponse = new SQLResponse();
    sqlResponse.setDuration(10);
    sqlResponse.setCube("test_cube");
    sqlResponse.setIsException(false);
    sqlResponse.setTotalScanCount(100);
    List<String> list1 = new ArrayList<>();
    list1.add("111");
    list1.add("112");
    List<String> list2 = new ArrayList<>();
    list2.add("111");
    list2.add("112");
    List<List<String>> results = new ArrayList<>();
    results.add(list1);
    results.add(list2);
    sqlResponse.setResults(results);
    sqlResponse.setStorageCacheUsed(true);
    Metrics metrics = MetricsFactory.getInstance();
    QueryMetrics2Facade.updateMetrics(sqlRequest, sqlResponse);
    String prefix = buildCubeMetricPrefix(sqlRequest.getProject(), sqlResponse.getCube().replace("=", "->"));
    Thread.sleep(2000);

    Assert.assertEquals(1L,
            metrics.getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_COUNT)).getCount());
    Assert.assertEquals(1L,
            metrics.getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_SUCCESS_COUNT))
                    .getCount());
    Assert.assertEquals(0L, metrics
            .getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_FAIL_COUNT)).getCount());
    Assert.assertEquals(1L, metrics
            .getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_CACHE_COUNT)).getCount());
    //
    Snapshot queryScanSnapshot = metrics
            .getHistogram(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_SCAN_ROWCOUNT))
            .getSnapshot();
    Assert.assertEquals(100.0, queryScanSnapshot.getMean(), 0);
    Assert.assertEquals(100.0, queryScanSnapshot.getMax(), 0);
    Assert.assertEquals(100.0, queryScanSnapshot.getMin(), 0);

    Snapshot queryResultSnapshot = metrics
            .getHistogram(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_RESULT_ROWCOUNT))
            .getSnapshot();

    Assert.assertEquals(2.0, queryResultSnapshot.getMean(), 0);
    Assert.assertEquals(2.0, queryResultSnapshot.getMax(), 0);
    Assert.assertEquals(2.0, queryResultSnapshot.getMin(), 0);

    Snapshot queryLatencySnapshot = metrics
            .getTimer(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_DURATION)).getSnapshot();
    Assert.assertEquals(TimeUnit.MILLISECONDS.toNanos(10), queryLatencySnapshot.getMean(), 0);
    Assert.assertEquals(TimeUnit.MILLISECONDS.toNanos(10), queryLatencySnapshot.getMax(), 0);
    Assert.assertEquals(TimeUnit.MILLISECONDS.toNanos(10), queryLatencySnapshot.getMin(), 0);

    SQLResponse sqlResponse2 = new SQLResponse();
    sqlResponse2.setDuration(10);
    sqlResponse2.setCube("test_cube");
    sqlResponse2.setIsException(true);

    QueryMetrics2Facade.updateMetrics(sqlRequest, sqlResponse2);
    prefix = buildCubeMetricPrefix(sqlRequest.getProject(), sqlResponse.getCube().replace("=", "->"));
    Assert.assertEquals(2L,
            metrics.getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_COUNT)).getCount());
    Assert.assertEquals(1L,
            metrics.getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_SUCCESS_COUNT))
                    .getCount());
    Assert.assertEquals(1L, metrics
            .getCounter(MetricsNameBuilder.buildMetricName(prefix, MetricsConstant.QUERY_FAIL_COUNT)).getCount());
}
 
Example 15
Source File: QueryService.java    From kylin with Apache License 2.0 4 votes vote down vote up
public SQLResponse querySystemCube(String sql) {
    SQLRequest sqlRequest = new SQLRequest();
    sqlRequest.setProject(MetricsManager.SYSTEM_PROJECT);
    sqlRequest.setSql(sql);
    return doQueryWithCache(sqlRequest, false);
}