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 static com.facebook.presto.SessionTestUtils.TEST_SESSION;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static java.util.concurrent.Executors.newCachedThreadPool;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertSame;
 import static org.testng.Assert.assertTrue;
 
 public class TestStageStateMachine
 {
     private static final StageId STAGE_ID = new StageId("query""stage");
     private static final URI LOCATION = URI.create("fake://fake-stage");
     private static final PlanFragment PLAN_FRAGMENT = createValuesPlan();
     private static final SQLException FAILED_CAUSE = new SQLException("FAILED");
 
     private final ExecutorService executor = newCachedThreadPool();
 
     @AfterClass
     public void tearDown()
     {
         .shutdownNow();
     }
 
     @Test
     public void testBasicStateChanges()
     {
         StageStateMachine stateMachine = createStageStateMachine();
         assertState(stateMachine.);
 
         assertTrue(stateMachine.transitionToScheduling());
         assertState(stateMachine.);
 
         assertTrue(stateMachine.transitionToScheduled());
         assertState(stateMachine.);
 
         assertTrue(stateMachine.transitionToRunning());
         assertState(stateMachine.);
 
         assertTrue(stateMachine.transitionToFinished());
         assertState(stateMachine.);
     }
 
     @Test
     public void testPlanned()
     {
         StageStateMachine stateMachine = createStageStateMachine();
         assertState(stateMachine.);
 
         stateMachine = createStageStateMachine();
         assertTrue(stateMachine.transitionToScheduling());
         assertState(stateMachine.);
 
         stateMachine = createStageStateMachine();
         assertTrue(stateMachine.transitionToRunning());
         assertState(stateMachine.);
 
         stateMachine = createStageStateMachine();
         assertTrue(stateMachine.transitionToFinished());
         assertState(stateMachine.);
 
         stateMachine = createStageStateMachine();
         assertTrue(stateMachine.transitionToFailed());
         assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToAborted());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToCanceled());
        assertState(stateMachine.);
    }
    @Test
    public void testScheduling()
    {
        StageStateMachine stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToScheduling());
        assertState(stateMachine.);
        assertFalse(stateMachine.transitionToScheduling());
        assertState(stateMachine.);
        assertTrue(stateMachine.transitionToScheduled());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduling();
        assertTrue(stateMachine.transitionToRunning());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduling();
        assertTrue(stateMachine.transitionToFinished());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduling();
        assertTrue(stateMachine.transitionToFailed());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduling();
        assertTrue(stateMachine.transitionToAborted());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduling();
        assertTrue(stateMachine.transitionToCanceled());
        assertState(stateMachine.);
    }
    @Test
    public void testScheduled()
    {
        StageStateMachine stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToScheduled());
        assertState(stateMachine.);
        assertFalse(stateMachine.transitionToScheduling());
        assertState(stateMachine.);
        assertFalse(stateMachine.transitionToScheduled());
        assertState(stateMachine.);
        assertTrue(stateMachine.transitionToRunning());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduled();
        assertTrue(stateMachine.transitionToFinished());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduled();
        assertTrue(stateMachine.transitionToFailed());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduled();
        assertTrue(stateMachine.transitionToAborted());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToScheduled();
        assertTrue(stateMachine.transitionToCanceled());
        assertState(stateMachine.);
    }
    @Test
    public void testRunning()
    {
        StageStateMachine stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToRunning());
        assertState(stateMachine.);
        assertFalse(stateMachine.transitionToScheduling());
        assertState(stateMachine.);
        assertFalse(stateMachine.transitionToScheduled());
        assertState(stateMachine.);
        assertFalse(stateMachine.transitionToRunning());
        assertState(stateMachine.);
        assertTrue(stateMachine.transitionToFinished());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToRunning();
        assertTrue(stateMachine.transitionToFailed());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToRunning();
        assertTrue(stateMachine.transitionToAborted());
        assertState(stateMachine.);
        stateMachine = createStageStateMachine();
        stateMachine.transitionToRunning();
        assertTrue(stateMachine.transitionToCanceled());
        assertState(stateMachine.);
    }
    @Test
    public void testFinished()
    {
        StageStateMachine stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToFinished());
        assertFinalState(stateMachine.);
    }
    @Test
    public void testFailed()
    {
        StageStateMachine stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToFailed());
        assertFinalState(stateMachine.);
    }
    @Test
    public void testAborted()
    {
        StageStateMachine stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToAborted());
        assertFinalState(stateMachine.);
    }
    @Test
    public void testCanceled()
    {
        StageStateMachine stateMachine = createStageStateMachine();
        assertTrue(stateMachine.transitionToCanceled());
        assertFinalState(stateMachine.);
    }
    private static void assertFinalState(StageStateMachine stateMachineStageState expectedState)
    {
        assertTrue(expectedState.isDone());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToScheduling());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToScheduled());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToRunning());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToFinished());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToFailed());
        assertState(stateMachineexpectedState);
        assertFalse(stateMachine.transitionToAborted());
        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(StageStateMachine stateMachineStageState expectedState)
    {
        assertEquals(stateMachine.getStageId(), );
        assertEquals(stateMachine.getLocation(), );
        assertSame(stateMachine.getSession(), );
        StageInfo stageInfo = stateMachine.getStageInfo(ImmutableList::of, ImmutableList::of);
        assertEquals(stageInfo.getStageId(), );
        assertEquals(stageInfo.getSelf(), );
        assertEquals(stageInfo.getSubStages(), ImmutableList.of());
        assertEquals(stageInfo.getTasks(), ImmutableList.of());
        assertEquals(stageInfo.getTypes(), ImmutableList.of());
        assertSame(stageInfo.getPlan(), );
        assertEquals(stateMachine.getState(), expectedState);
        assertEquals(stageInfo.getState(), expectedState);
        if (expectedState == .) {
            ExecutionFailureInfo failure = stageInfo.getFailureCause();
            assertEquals(failure.getMessage(), .getMessage());
            assertEquals(failure.getType(), .getClass().getName());
        }
        else {
            assertNull(stageInfo.getFailureCause());
        }
    }
    {
    }
    private static PlanFragment createValuesPlan()
    {
        Symbol symbol = new Symbol("column");
        PlanNodeId valuesNodeId = new PlanNodeId("plan");
        PlanFragment planFragment = new PlanFragment(
                new PlanFragmentId("plan"),
                new ValuesNode(valuesNodeId,
                        ImmutableList.of(symbol),
                        ImmutableList.of(ImmutableList.of(new StringLiteral("foo")))),
                ImmutableMap.<SymbolType>of(symbol),
                ImmutableList.of(symbol),
                .,
                valuesNodeId,
                .,
                ImmutableList.<Symbol>of(),
                Optional.empty());
        return planFragment;
    }
New to GrepCode? Check out our FAQ X