Search in sources :

Example 1 with DispatchQuery

use of io.prestosql.dispatcher.DispatchQuery in project hetu-core by openlookeng.

the class TestStateUpdater method mockDispatchQueryData.

private DispatchQuery mockDispatchQueryData(boolean userError) {
    DispatchQuery dispatchQuery = Mockito.mock(LocalDispatchQuery.class);
    BasicQueryInfo basicQueryInfo = createBasicQueryInfo();
    when(dispatchQuery.getBasicQueryInfo()).then(new Returns(basicQueryInfo));
    when(dispatchQuery.getSession()).then(new Returns(TEST_SESSION));
    ErrorCode errorCode;
    if (!userError) {
        errorCode = CLUSTER_OUT_OF_MEMORY.toErrorCode();
    } else {
        errorCode = new ErrorCode(ERROR_CODE_VALUE_INDEX_TIME_NO_INVOCATION, ERROR_CODE_TEST, USER_ERROR);
    }
    Optional<ErrorCode> optionalErrorCode = Optional.of(errorCode);
    when(dispatchQuery.getErrorCode()).then(new Returns(optionalErrorCode));
    DataSize userDataSize = new DataSize(USER_DATA_SIZE, DataSize.Unit.BYTE);
    DataSize totalDataSize = new DataSize(TOTAL_DATA_SIZE, DataSize.Unit.BYTE);
    when(dispatchQuery.getUserMemoryReservation()).then(new Returns(userDataSize));
    when(dispatchQuery.getTotalMemoryReservation()).then(new Returns(totalDataSize));
    when(dispatchQuery.getTotalCpuTime()).then(new Returns(new Duration(ERROR_CODE_VALUE_INDEX_TIME_NO_INVOCATION, MILLISECONDS)));
    when(dispatchQuery.getExecutionStartTime()).then(new Returns(Optional.of(new DateTime(DateTimeZone.UTC))));
    return dispatchQuery;
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) DispatchQuery(io.prestosql.dispatcher.DispatchQuery) LocalDispatchQuery(io.prestosql.dispatcher.LocalDispatchQuery) BasicQueryInfo(io.prestosql.server.BasicQueryInfo) DataSize(io.airlift.units.DataSize) Duration(io.airlift.units.Duration) ErrorCode(io.prestosql.spi.ErrorCode) DateTime(org.joda.time.DateTime)

Example 2 with DispatchQuery

use of io.prestosql.dispatcher.DispatchQuery in project hetu-core by openlookeng.

the class TestStateUpdater method testRegisterQuery.

@Test
public void testRegisterQuery() {
    StateStoreProvider stateStoreProvider = Mockito.mock(LocalStateStoreProvider.class);
    StateUpdater stateUpdater = new StateUpdater(stateStoreProvider, updateInterval);
    DispatchQuery dispatchQuery = Mockito.mock(DispatchQuery.class);
    stateUpdater.registerQuery(STATE_COLLECTION_QUERY, dispatchQuery);
}
Also used : DispatchQuery(io.prestosql.dispatcher.DispatchQuery) LocalDispatchQuery(io.prestosql.dispatcher.LocalDispatchQuery) Test(org.testng.annotations.Test)

Example 3 with DispatchQuery

use of io.prestosql.dispatcher.DispatchQuery in project hetu-core by openlookeng.

the class StateUpdater method updateStates.

/**
 * Update local queued query states to state store
 *
 * @throws JsonProcessingException exception when fail to serialize states to json
 */
public void updateStates() throws JsonProcessingException {
    // State store hasn't been loaded yet
    final StateStore stateStore = stateStoreProvider.getStateStore();
    if (stateStore == null) {
        return;
    }
    long start = System.currentTimeMillis();
    LOG.debug("UpdateStates starts at current time milliseconds: %s, at format HH:mm:ss:SSS:%s", start, new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(start)));
    StateCollection finishedQueries = stateStore.getStateCollection(FINISHED_QUERY_STATE_COLLECTION_NAME);
    StateCollection queries = stateStore.getStateCollection(QUERY_STATE_COLLECTION_NAME);
    List<DispatchQuery> queriesToUnregister = new LinkedList<>();
    synchronized (registeredQueries) {
        for (DispatchQuery query : registeredQueries.get(QUERY_STATE_COLLECTION_NAME)) {
            SharedQueryState state = SharedQueryState.create(query);
            String stateJson = MAPPER.writeValueAsString(state);
            if (state.getBasicQueryInfo().getState() == QueryState.FINISHED || state.getBasicQueryInfo().getState() == QueryState.FAILED) {
                // No need to update states for finished queries
                // also move finished queries to finished-query state collection
                queriesToUnregister.add(query);
                ((StateMap) finishedQueries).put(state.getBasicQueryInfo().getQueryId().getId(), stateJson);
                continue;
            }
            ((StateMap) queries).put(state.getBasicQueryInfo().getQueryId().getId(), stateJson);
        }
    }
    for (DispatchQuery query : queriesToUnregister) {
        removeFromStateCollection(stateStore, QUERY_STATE_COLLECTION_NAME, query);
    }
    long end = System.currentTimeMillis();
    LOG.debug("updateStates ends at current time milliseconds: %s, at format HH:mm:ss:SSS:%s, total time use: %s", end, new SimpleDateFormat("HH:mm:ss:SSS").format(new Date(end)), end - start);
}
Also used : DispatchQuery(io.prestosql.dispatcher.DispatchQuery) StateCollection(io.prestosql.spi.statestore.StateCollection) StateMap(io.prestosql.spi.statestore.StateMap) StateStore(io.prestosql.spi.statestore.StateStore) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) LinkedList(java.util.LinkedList)

Example 4 with DispatchQuery

use of io.prestosql.dispatcher.DispatchQuery in project hetu-core by openlookeng.

the class AutoVacuumScanner method startVacuum.

private void startVacuum(Catalog catalog, String vacuumTable, boolean isFull) {
    String catalogNameVacuumTable = catalog.getCatalogName() + "." + vacuumTable;
    if (vacuumInProgressMap.containsKey(catalogNameVacuumTable)) {
        log.debug("return Present in vacuumInProgressMap %s ", catalogNameVacuumTable);
        return;
    }
    long attempts = 0;
    QueryId queryId = dispatchManager.createQueryId();
    String slug = "x" + randomUUID().toString().toLowerCase(ENGLISH).replace("-", "");
    String vacuumQuery;
    if (isFull) {
        vacuumQuery = "vacuum table " + catalogNameVacuumTable + "  full";
    } else {
        vacuumQuery = "vacuum table " + catalogNameVacuumTable;
    }
    Session.SessionBuilder sessionBuilder = Session.builder(sessionPropertyManager).setQueryId(queryId).setIdentity(new Identity("openLooKeng", Optional.empty())).setSource("auto-vacuum");
    Session session = sessionBuilder.build();
    AutoVacuumSessionContext sessionContext = new AutoVacuumSessionContext(session);
    vacuumInProgressMap.put(catalogNameVacuumTable, System.currentTimeMillis());
    log.debug("Query.create queryId %s  catalogNameVacuumTable: %s ", queryId.toString(), catalogNameVacuumTable);
    ListenableFuture<?> lf = waitForDispatched(queryId, slug, sessionContext, vacuumQuery);
    Futures.addCallback(lf, new FutureCallback<Object>() {

        @Override
        public void onSuccess(@Nullable Object result) {
            try {
                DispatchQuery dispatchQuery = dispatchManager.getQuery(queryId);
                dispatchQuery.addStateChangeListener((state) -> {
                    Query query = getQuery(queryId, slug);
                    if ((null != query) && (!dispatchManager.getQueryInfo(queryId).getState().isDone())) {
                        query.waitForResults(attempts, Duration.valueOf("1s"), DataSize.valueOf("1MB"));
                    }
                    if (state.isDone()) {
                        log.debug("STATUS  %s QueryID %s Query %s", state.name(), queryId.toString(), vacuumQuery);
                        vacuumInProgressMap.remove(catalogNameVacuumTable);
                    }
                });
            } catch (Throwable e) {
                vacuumInProgressMap.remove(catalogNameVacuumTable);
                log.error("Filed to execute vacuum for table %s QueryID %s", catalogNameVacuumTable, queryId.toString(), e.getMessage());
            }
        }

        @Override
        public void onFailure(Throwable t) {
            vacuumInProgressMap.remove(catalogNameVacuumTable);
            log.error("Query %s request to start vacuum scan failed at queryId[%s]: %s ", vacuumQuery, queryId, t.getMessage());
        }
    }, directExecutor());
}
Also used : ConnectorMetadata(io.prestosql.spi.connector.ConnectorMetadata) DispatchQuery(io.prestosql.dispatcher.DispatchQuery) Inject(com.google.inject.Inject) DataCenterStatementResource(io.prestosql.datacenter.DataCenterStatementResource) Duration(io.airlift.units.Duration) QueryManager(io.prestosql.execution.QueryManager) PreDestroy(javax.annotation.PreDestroy) Future(java.util.concurrent.Future) BoundedExecutor(io.airlift.concurrent.BoundedExecutor) Map(java.util.Map) SimpleLocalMemoryContext(io.prestosql.memory.context.SimpleLocalMemoryContext) ENGLISH(java.util.Locale.ENGLISH) PrestoException(io.prestosql.spi.PrestoException) Query(io.prestosql.server.protocol.Query) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Identity(io.prestosql.spi.security.Identity) ThreadSafe(javax.annotation.concurrent.ThreadSafe) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) DataSize(io.airlift.units.DataSize) List(java.util.List) SessionPropertyManager(io.prestosql.metadata.SessionPropertyManager) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) StandardErrorCode(io.prestosql.spi.StandardErrorCode) ConnectorVacuumTableInfo(io.prestosql.spi.connector.ConnectorVacuumTableInfo) AggregatedMemoryContext.newSimpleAggregatedMemoryContext(io.prestosql.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext) Connector(io.prestosql.spi.connector.Connector) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Logger(io.airlift.log.Logger) BlockEncodingSerde(io.prestosql.spi.block.BlockEncodingSerde) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExchangeClient(io.prestosql.operator.ExchangeClient) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) QueryId(io.prestosql.spi.QueryId) NoSuchElementException(java.util.NoSuchElementException) DispatchManager(io.prestosql.dispatcher.DispatchManager) Nullable(javax.annotation.Nullable) ForStatementResource(io.prestosql.server.ForStatementResource) FutureCallback(com.google.common.util.concurrent.FutureCallback) Futures(com.google.common.util.concurrent.Futures) UUID.randomUUID(java.util.UUID.randomUUID) Catalog(io.prestosql.metadata.Catalog) ExchangeClientSupplier(io.prestosql.operator.ExchangeClientSupplier) CatalogManager(io.prestosql.metadata.CatalogManager) DispatchQuery(io.prestosql.dispatcher.DispatchQuery) Query(io.prestosql.server.protocol.Query) DispatchQuery(io.prestosql.dispatcher.DispatchQuery) QueryId(io.prestosql.spi.QueryId) Identity(io.prestosql.spi.security.Identity) Session(io.prestosql.Session)

Example 5 with DispatchQuery

use of io.prestosql.dispatcher.DispatchQuery in project hetu-core by openlookeng.

the class TestStateUpdater method testStateChangeListenerOfRegisterQuery.

@Test
public void testStateChangeListenerOfRegisterQuery() throws JsonProcessingException {
    StateStoreProvider stateStoreProvider = Mockito.mock(LocalStateStoreProvider.class);
    StateUpdater stateUpdater = new StateUpdater(stateStoreProvider, updateInterval);
    DispatchQuery dispatchQuery = mockDispatchQueryData(true);
    updateStateChange(dispatchQuery);
    stateUpdater.registerQuery(STATE_COLLECTION_QUERY, dispatchQuery);
    when(stateStoreProvider.getStateStore()).then(new Returns(stateStore));
    when(stateStoreProvider.getStateStore().getStateCollection(any())).then(new Returns(Mockito.mock(StateMap.class)));
    when(stateStoreProvider.getStateStore().getStateCollection(any()).getType()).then(new Returns(StateCollection.Type.MAP));
    stateUpdater.updateStates();
    int numberOfCalls = mockingDetails(stateStoreProvider.getStateStore().getStateCollection(any())).getInvocations().size();
    assertNotEquals(numberOfCalls, ERROR_CODE_VALUE_INDEX_TIME_NO_INVOCATION);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) DispatchQuery(io.prestosql.dispatcher.DispatchQuery) LocalDispatchQuery(io.prestosql.dispatcher.LocalDispatchQuery) Test(org.testng.annotations.Test)

Aggregations

DispatchQuery (io.prestosql.dispatcher.DispatchQuery)6 LocalDispatchQuery (io.prestosql.dispatcher.LocalDispatchQuery)4 DataSize (io.airlift.units.DataSize)2 Duration (io.airlift.units.Duration)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 Test (org.testng.annotations.Test)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors.directExecutor (com.google.common.util.concurrent.MoreExecutors.directExecutor)1 Inject (com.google.inject.Inject)1 BoundedExecutor (io.airlift.concurrent.BoundedExecutor)1 Logger (io.airlift.log.Logger)1 Session (io.prestosql.Session)1 DataCenterStatementResource (io.prestosql.datacenter.DataCenterStatementResource)1 DispatchManager (io.prestosql.dispatcher.DispatchManager)1 QueryManager (io.prestosql.execution.QueryManager)1 AggregatedMemoryContext.newSimpleAggregatedMemoryContext (io.prestosql.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext)1 SimpleLocalMemoryContext (io.prestosql.memory.context.SimpleLocalMemoryContext)1