Search in sources :

Example 56 with Duration

use of io.airlift.units.Duration in project presto by prestodb.

the class TestDbResourceGroupConfigurationManager method testConfiguration.

@Test
public void testConfiguration() {
    H2DaoProvider daoProvider = setup("test_configuration");
    H2ResourceGroupsDao dao = daoProvider.get();
    dao.createResourceGroupsGlobalPropertiesTable();
    dao.createResourceGroupsTable();
    dao.createSelectorsTable();
    dao.insertResourceGroupsGlobalProperties("cpu_quota_period", "1h");
    dao.insertResourceGroup(1, "global", "1MB", 1000, 100, "weighted", null, true, "1h", "1d", null);
    dao.insertResourceGroup(2, "sub", "2MB", 4, 3, null, 5, null, null, null, 1L);
    dao.insertSelector(2, null, null);
    DbResourceGroupConfigurationManager manager = new DbResourceGroupConfigurationManager((poolId, listener) -> {
    }, daoProvider.get());
    AtomicBoolean exported = new AtomicBoolean();
    InternalResourceGroup global = new InternalResourceGroup.RootInternalResourceGroup("global", (group, export) -> exported.set(export), directExecutor());
    manager.configure(global, new SelectionContext(true, "user", Optional.empty(), 1));
    assertEqualsResourceGroup(global, "1MB", 1000, 100, WEIGHTED, DEFAULT_WEIGHT, true, new Duration(1, HOURS), new Duration(1, DAYS));
    exported.set(false);
    InternalResourceGroup sub = global.getOrCreateSubGroup("sub");
    manager.configure(sub, new SelectionContext(true, "user", Optional.empty(), 1));
    assertEqualsResourceGroup(sub, "2MB", 4, 3, FAIR, 5, false, new Duration(Long.MAX_VALUE, MILLISECONDS), new Duration(Long.MAX_VALUE, MILLISECONDS));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalResourceGroup(com.facebook.presto.execution.resourceGroups.InternalResourceGroup) Duration(io.airlift.units.Duration) SelectionContext(com.facebook.presto.spi.resourceGroups.SelectionContext) Test(org.testng.annotations.Test)

Example 57 with Duration

use of io.airlift.units.Duration in project presto by prestodb.

the class TestDbResourceGroupConfigurationManager method testReconfig.

@Test(timeOut = 60_000)
public void testReconfig() throws Exception {
    H2DaoProvider daoProvider = setup("test_reconfig");
    H2ResourceGroupsDao dao = daoProvider.get();
    dao.createResourceGroupsGlobalPropertiesTable();
    dao.createResourceGroupsTable();
    dao.createSelectorsTable();
    dao.insertResourceGroup(1, "global", "1MB", 1000, 100, "weighted", null, true, "1h", "1d", null);
    dao.insertResourceGroup(2, "sub", "2MB", 4, 3, null, 5, null, null, null, 1L);
    dao.insertSelector(2, null, null);
    dao.insertResourceGroupsGlobalProperties("cpu_quota_period", "1h");
    DbResourceGroupConfigurationManager manager = new DbResourceGroupConfigurationManager((poolId, listener) -> {
    }, daoProvider.get());
    manager.start();
    AtomicBoolean exported = new AtomicBoolean();
    InternalResourceGroup global = new InternalResourceGroup.RootInternalResourceGroup("global", (group, export) -> exported.set(export), directExecutor());
    manager.configure(global, new SelectionContext(true, "user", Optional.empty(), 1));
    InternalResourceGroup globalSub = global.getOrCreateSubGroup("sub");
    manager.configure(globalSub, new SelectionContext(true, "user", Optional.empty(), 1));
    // Verify record exists
    assertEqualsResourceGroup(globalSub, "2MB", 4, 3, FAIR, 5, false, new Duration(Long.MAX_VALUE, MILLISECONDS), new Duration(Long.MAX_VALUE, MILLISECONDS));
    dao.updateResourceGroup(2, "sub", "3MB", 2, 1, "weighted", 6, true, "1h", "1d", 1L);
    do {
        MILLISECONDS.sleep(500);
    } while (globalSub.getJmxExport() == false);
    // Verify update
    assertEqualsResourceGroup(globalSub, "3MB", 2, 1, WEIGHTED, 6, true, new Duration(1, HOURS), new Duration(1, DAYS));
    // Verify delete
    dao.deleteSelectors(2);
    dao.deleteResourceGroup(2);
    do {
        MILLISECONDS.sleep(500);
    } while (globalSub.getMaxQueuedQueries() != 0 || globalSub.getMaxRunningQueries() != 0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalResourceGroup(com.facebook.presto.execution.resourceGroups.InternalResourceGroup) Duration(io.airlift.units.Duration) SelectionContext(com.facebook.presto.spi.resourceGroups.SelectionContext) Test(org.testng.annotations.Test)

Example 58 with Duration

use of io.airlift.units.Duration in project presto by prestodb.

the class QueryAssertions method assertQuery.

public static void assertQuery(QueryRunner actualQueryRunner, Session session, @Language("SQL") String actual, H2QueryRunner h2QueryRunner, @Language("SQL") String expected, boolean ensureOrdering, boolean compareUpdate) {
    long start = System.nanoTime();
    MaterializedResult actualResults = null;
    try {
        actualResults = actualQueryRunner.execute(session, actual).toJdbcTypes();
    } catch (RuntimeException ex) {
        fail("Execution of 'actual' query failed: " + actual, ex);
    }
    Duration actualTime = nanosSince(start);
    long expectedStart = System.nanoTime();
    MaterializedResult expectedResults = null;
    try {
        expectedResults = h2QueryRunner.execute(session, expected, actualResults.getTypes());
    } catch (RuntimeException ex) {
        fail("Execution of 'expected' query failed: " + actual, ex);
    }
    log.info("FINISHED in presto: %s, h2: %s, total: %s", actualTime, nanosSince(expectedStart), nanosSince(start));
    if (actualResults.getUpdateType().isPresent() || actualResults.getUpdateCount().isPresent()) {
        if (!actualResults.getUpdateType().isPresent()) {
            fail("update count present without update type for query: \n" + actual);
        }
        if (!compareUpdate) {
            fail("update type should not be present (use assertUpdate) for query: \n" + actual);
        }
    }
    List<MaterializedRow> actualRows = actualResults.getMaterializedRows();
    List<MaterializedRow> expectedRows = expectedResults.getMaterializedRows();
    if (compareUpdate) {
        if (!actualResults.getUpdateType().isPresent()) {
            fail("update type not present for query: \n" + actual);
        }
        if (!actualResults.getUpdateCount().isPresent()) {
            fail("update count not present for query: \n" + actual);
        }
        assertEquals(actualRows.size(), 1, "For query: \n " + actual + "\n:");
        assertEquals(expectedRows.size(), 1, "For query: \n " + actual + "\n:");
        MaterializedRow row = expectedRows.get(0);
        assertEquals(row.getFieldCount(), 1, "For query: \n " + actual + "\n:");
        assertEquals(row.getField(0), actualResults.getUpdateCount().getAsLong(), "For query: \n " + actual + "\n:");
    }
    if (ensureOrdering) {
        if (!actualRows.equals(expectedRows)) {
            assertEquals(actualRows, expectedRows, "For query: \n " + actual + "\n:");
        }
    } else {
        assertEqualsIgnoreOrder(actualRows, expectedRows, "For query: \n " + actual);
    }
}
Also used : Duration(io.airlift.units.Duration) MaterializedResult(com.facebook.presto.testing.MaterializedResult) MaterializedRow(com.facebook.presto.testing.MaterializedRow)

Example 59 with Duration

use of io.airlift.units.Duration in project presto by prestodb.

the class Validator method executeQuery.

private QueryResult executeQuery(String url, String username, String password, Query query, String sql, Duration timeout, Map<String, String> sessionProperties) {
    String queryId = null;
    try (Connection connection = DriverManager.getConnection(url, username, password)) {
        trySetConnectionProperties(query, connection);
        for (Map.Entry<String, String> entry : sessionProperties.entrySet()) {
            connection.unwrap(PrestoConnection.class).setSessionProperty(entry.getKey(), entry.getValue());
        }
        try (Statement statement = connection.createStatement()) {
            TimeLimiter limiter = new SimpleTimeLimiter();
            Stopwatch stopwatch = Stopwatch.createStarted();
            Statement limitedStatement = limiter.newProxy(statement, Statement.class, timeout.toMillis(), TimeUnit.MILLISECONDS);
            if (explainOnly) {
                sql = "EXPLAIN " + sql;
            }
            long start = System.nanoTime();
            PrestoStatement prestoStatement = limitedStatement.unwrap(PrestoStatement.class);
            ProgressMonitor progressMonitor = new ProgressMonitor();
            prestoStatement.setProgressMonitor(progressMonitor);
            try {
                boolean isSelectQuery = limitedStatement.execute(sql);
                List<List<Object>> results = null;
                if (isSelectQuery) {
                    results = limiter.callWithTimeout(getResultSetConverter(limitedStatement.getResultSet()), timeout.toMillis() - stopwatch.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS, true);
                } else {
                    results = ImmutableList.of(ImmutableList.of(limitedStatement.getLargeUpdateCount()));
                }
                prestoStatement.clearProgressMonitor();
                QueryStats queryStats = progressMonitor.getFinalQueryStats();
                if (queryStats == null) {
                    throw new VerifierException("Cannot fetch query stats");
                }
                Duration queryCpuTime = new Duration(queryStats.getCpuTimeMillis(), TimeUnit.MILLISECONDS);
                queryId = queryStats.getQueryId();
                return new QueryResult(State.SUCCESS, null, nanosSince(start), queryCpuTime, queryId, results);
            } catch (AssertionError e) {
                if (e.getMessage().startsWith("unimplemented type:")) {
                    return new QueryResult(State.INVALID, null, null, null, queryId, ImmutableList.of());
                }
                throw e;
            } catch (SQLException | VerifierException e) {
                throw e;
            } catch (UncheckedTimeoutException e) {
                return new QueryResult(State.TIMEOUT, null, null, null, queryId, ImmutableList.of());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw Throwables.propagate(e);
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    } catch (SQLException e) {
        Exception exception = e;
        if (("Error executing query".equals(e.getMessage()) || "Error fetching results".equals(e.getMessage())) && (e.getCause() instanceof Exception)) {
            exception = (Exception) e.getCause();
        }
        State state = isPrestoQueryInvalid(e) ? State.INVALID : State.FAILED;
        return new QueryResult(state, exception, null, null, null, null);
    } catch (VerifierException e) {
        return new QueryResult(State.TOO_MANY_ROWS, e, null, null, null, null);
    }
}
Also used : SQLException(java.sql.SQLException) Stopwatch(com.google.common.base.Stopwatch) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) Duration(io.airlift.units.Duration) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) QueryStats(com.facebook.presto.jdbc.QueryStats) State(com.facebook.presto.verifier.QueryResult.State) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Map(java.util.Map) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 60 with Duration

use of io.airlift.units.Duration in project presto by prestodb.

the class TestShadowing method testCreateTableAsSelect.

@Test
public void testCreateTableAsSelect() throws Exception {
    handle.execute("CREATE TABLE \"my_test_table\" (column1 BIGINT, column2 DOUBLE)");
    SqlParser parser = new SqlParser();
    Query query = new Query(CATALOG, SCHEMA, ImmutableList.of(), "CREATE TABLE my_test_table AS SELECT 1 column1, CAST(2.0 AS DOUBLE) column2 LIMIT 1", ImmutableList.of(), null, null, ImmutableMap.of());
    QueryRewriter rewriter = new QueryRewriter(parser, URL, QualifiedName.of("tmp_"), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), 1, new Duration(10, SECONDS));
    Query rewrittenQuery = rewriter.shadowQuery(query);
    assertEquals(rewrittenQuery.getPreQueries().size(), 1);
    assertEquals(rewrittenQuery.getPostQueries().size(), 1);
    CreateTableAsSelect createTableAs = (CreateTableAsSelect) parser.createStatement(rewrittenQuery.getPreQueries().get(0));
    assertEquals(createTableAs.getName().getParts().size(), 1);
    assertTrue(createTableAs.getName().getSuffix().startsWith("tmp_"));
    assertFalse(createTableAs.getName().getSuffix().contains("my_test_table"));
    assertEquals(PrestoVerifier.statementToQueryType(parser, rewrittenQuery.getQuery()), READ);
    Table table = new Table(createTableAs.getName());
    SingleColumn column1 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new Identifier("column1"))));
    SingleColumn column2 = new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new FunctionCall(QualifiedName.of("round"), ImmutableList.of(new Identifier("column2"), new LongLiteral("1"))))));
    Select select = new Select(false, ImmutableList.of(column1, column2));
    QuerySpecification querySpecification = new QuerySpecification(select, Optional.of(table), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    assertEquals(parser.createStatement(rewrittenQuery.getQuery()), new com.facebook.presto.sql.tree.Query(Optional.empty(), querySpecification, Optional.empty(), Optional.empty()));
    assertEquals(parser.createStatement(rewrittenQuery.getPostQueries().get(0)), new DropTable(createTableAs.getName(), true));
}
Also used : Table(com.facebook.presto.sql.tree.Table) DropTable(com.facebook.presto.sql.tree.DropTable) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SqlParser(com.facebook.presto.sql.parser.SqlParser) Duration(io.airlift.units.Duration) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) DropTable(com.facebook.presto.sql.tree.DropTable) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Identifier(com.facebook.presto.sql.tree.Identifier) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Select(com.facebook.presto.sql.tree.Select) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Aggregations

Duration (io.airlift.units.Duration)124 Test (org.testng.annotations.Test)66 DataSize (io.airlift.units.DataSize)35 URI (java.net.URI)12 TestingHttpClient (io.airlift.http.client.testing.TestingHttpClient)11 Session (com.facebook.presto.Session)9 DateTime (org.joda.time.DateTime)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 TestingTicker (io.airlift.testing.TestingTicker)8 List (java.util.List)8 PrestoException (com.facebook.presto.spi.PrestoException)7 ImmutableList (com.google.common.collect.ImmutableList)7 File (java.io.File)6 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 Produces (javax.ws.rs.Produces)5 Type (com.facebook.presto.spi.type.Type)4 IOException (java.io.IOException)4