Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.execution;
 
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 
 import static com.facebook.presto.SessionTestUtils.TEST_SESSION;
 import static com.facebook.presto.execution.QueryState.FAILED;
 import static com.facebook.presto.execution.QueryState.FINISHED;
 import static com.facebook.presto.execution.QueryState.PLANNING;
 import static com.facebook.presto.execution.QueryState.QUEUED;
 import static com.facebook.presto.execution.QueryState.RUNNING;
 import static com.facebook.presto.execution.QueryState.STARTING;
 import static java.util.concurrent.Executors.newCachedThreadPool;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
 
 public class TestQueryStateMachine
 {
     private static final QueryId QUERY_ID = new QueryId("query_id");
     private static final String QUERY = "sql";
     private static final URI LOCATION = URI.create("fake://fake-query");
     private static final SQLException FAILED_CAUSE = new SQLException("FAILED");
     private static final List<InputINPUTS = ImmutableList.of(new Input("connector""schema""table", ImmutableList.of(new Column("a""varchar", Optional.empty()))));
     private static final List<StringOUTPUT_FIELD_NAMES = ImmutableList.of("a""b""c");
     private static final String UPDATE_TYPE = "update type";
     private static final VersionedMemoryPoolId MEMORY_POOL = new VersionedMemoryPoolId(new MemoryPoolId("pool"), 42);
     private static final Map<StringStringSET_SESSION_PROPERTIES = ImmutableMap.<StringString>builder()
             .put("fruit""apple")
             .put("drink""coffee")
             .build();
     private static final List<StringRESET_SESSION_PROPERTIES = ImmutableList.of("candy");
 
     private final ExecutorService executor = newCachedThreadPool();
 
     @AfterClass
     public void tearDown()
     {
         .shutdownNow();
     }
 
     @Test
     public void testBasicStateChanges()
     {
         QueryStateMachine stateMachine = createQueryStateMachine();
         assertState(stateMachine);
 
         assertTrue(stateMachine.transitionToPlanning());
         assertState(stateMachine);
 
         assertTrue(stateMachine.transitionToStarting());
         assertState(stateMachine);
 
         assertTrue(stateMachine.transitionToRunning());
         assertState(stateMachine);
 
         assertTrue(stateMachine.transitionToFinished());
         assertState(stateMachine);
     }
 
     @Test
     public void testQueued()
     {
         QueryStateMachine stateMachine = createQueryStateMachine();
         assertState(stateMachine);
 
         assertTrue(stateMachine.transitionToPlanning());
         assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToStarting());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToRunning());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToFinished());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToFailed());
        assertState(stateMachine);
    }
    @Test
    public void testPlanning()
    {
        QueryStateMachine stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToPlanning());
        assertState(stateMachine);
        assertFalse(stateMachine.transitionToPlanning());
        assertState(stateMachine);
        assertTrue(stateMachine.transitionToStarting());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        stateMachine.transitionToPlanning();
        assertTrue(stateMachine.transitionToRunning());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        stateMachine.transitionToPlanning();
        assertTrue(stateMachine.transitionToFinished());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        stateMachine.transitionToPlanning();
        assertTrue(stateMachine.transitionToFailed());
        assertState(stateMachine);
    }
    @Test
    public void testStarting()
    {
        QueryStateMachine stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToStarting());
        assertState(stateMachine);
        assertFalse(stateMachine.transitionToPlanning());
        assertState(stateMachine);
        assertFalse(stateMachine.transitionToStarting());
        assertState(stateMachine);
        assertTrue(stateMachine.transitionToRunning());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        stateMachine.transitionToStarting();
        assertTrue(stateMachine.transitionToFinished());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        stateMachine.transitionToStarting();
        assertTrue(stateMachine.transitionToFailed());
        assertState(stateMachine);
    }
    @Test
    public void testRunning()
    {
        QueryStateMachine stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToRunning());
        assertState(stateMachine);
        assertFalse(stateMachine.transitionToPlanning());
        assertState(stateMachine);
        assertFalse(stateMachine.transitionToStarting());
        assertState(stateMachine);
        assertFalse(stateMachine.transitionToRunning());
        assertState(stateMachine);
        assertTrue(stateMachine.transitionToFinished());
        assertState(stateMachine);
        stateMachine = createQueryStateMachine();
        stateMachine.transitionToRunning();
        assertTrue(stateMachine.transitionToFailed());
        assertState(stateMachine);
    }
    @Test
    public void testFinished()
    {
        QueryStateMachine stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToFinished());
        assertFinalState(stateMachine);
    }
    @Test
    public void testFailed()
    {
        QueryStateMachine stateMachine = createQueryStateMachine();
        assertTrue(stateMachine.transitionToFailed());
        assertFinalState(stateMachine);
    }
    private static void assertFinalState(QueryStateMachine stateMachineQueryState expectedState)
    {
        assertTrue(expectedState.isDone());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToPlanning());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToStarting());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToRunning());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToFinished());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToFailed());
        assertState(stateMachineexpectedState);
        // attempt to fail with another exception, which will fail
        assertFalse(stateMachine.transitionToFailed(new IOException("failure after finish")));
        assertState(stateMachineexpectedState);
    }
    private static void assertState(QueryStateMachine stateMachineQueryState expectedState)
    {
        assertEquals(stateMachine.getQueryId(), );
        assertSame(stateMachine.getSession(), );
        assertSame(stateMachine.getMemoryPool(), );
        QueryInfo queryInfo = stateMachine.getQueryInfo(null);
        assertEquals(queryInfo.getQueryId(), );
        assertEquals(queryInfo.getSelf(), );
        assertNull(queryInfo.getOutputStage());
        assertEquals(queryInfo.getQuery(), );
        assertEquals(queryInfo.getInputs(), );
        assertEquals(queryInfo.getFieldNames(), );
        assertEquals(queryInfo.getUpdateType(), );
        assertEquals(queryInfo.getMemoryPool(), .getId());
        QueryStats queryStats = queryInfo.getQueryStats();
        if (queryInfo.getState() == ) {
            assertNull(queryStats.getQueuedTime());
            assertNull(queryStats.getTotalPlanningTime());
            assertNull(queryStats.getExecutionStartTime());
            assertNull(queryStats.getEndTime());
        }
        else if (queryInfo.getState() == ) {
            assertNotNull(queryStats.getQueuedTime());
            assertNull(queryStats.getTotalPlanningTime());
            assertNull(queryStats.getExecutionStartTime());
            assertNull(queryStats.getEndTime());
        }
        else if (queryInfo.getState() == ) {
            assertNotNull(queryStats.getQueuedTime());
            assertNotNull(queryStats.getTotalPlanningTime());
            assertNull(queryStats.getExecutionStartTime());
            assertNull(queryStats.getEndTime());
        }
        else if (queryInfo.getState() == ) {
            assertNotNull(queryStats.getQueuedTime());
            assertNotNull(queryStats.getTotalPlanningTime());
            assertNotNull(queryStats.getExecutionStartTime());
            assertNull(queryStats.getEndTime());
        }
        else {
            assertNotNull(queryStats.getQueuedTime());
            assertNotNull(queryStats.getTotalPlanningTime());
            assertNotNull(queryStats.getExecutionStartTime());
            assertNotNull(queryStats.getEndTime());
        }
        assertEquals(stateMachine.getQueryState(), expectedState);
        assertEquals(queryInfo.getState(), expectedState);
        assertEquals(stateMachine.isDone(), expectedState.isDone());
        if (expectedState == ) {
            FailureInfo failure = queryInfo.getFailureInfo();
            assertNotNull(failure);
            assertEquals(failure.getMessage(), .getMessage());
            assertEquals(failure.getType(), .getClass().getName());
        }
        else {
            assertNull(queryInfo.getFailureInfo());
        }
    }
    @NotNull
    {
        QueryStateMachine stateMachine = new QueryStateMachine();
        stateMachine.setInputs();
        stateMachine.setOutputFieldNames();
        stateMachine.setUpdateType();
        stateMachine.setMemoryPool();
        for (Entry<StringStringentry : .entrySet()) {
            stateMachine.addSetSessionProperties(entry.getKey(), entry.getValue());
        }
        return stateMachine;
    }
New to GrepCode? Check out our FAQ X