Search in sources :

Example 16 with FlinkCompletableFuture

use of org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture in project flink by apache.

the class AkkaRpcService method startServer.

@Override
public <C extends RpcGateway, S extends RpcEndpoint<C>> C startServer(S rpcEndpoint) {
    checkNotNull(rpcEndpoint, "rpc endpoint");
    CompletableFuture<Void> terminationFuture = new FlinkCompletableFuture<>();
    Props akkaRpcActorProps = Props.create(AkkaRpcActor.class, rpcEndpoint, terminationFuture);
    ActorRef actorRef;
    synchronized (lock) {
        checkState(!stopped, "RpcService is stopped");
        actorRef = actorSystem.actorOf(akkaRpcActorProps);
        actors.add(actorRef);
    }
    LOG.info("Starting RPC endpoint for {} at {} .", rpcEndpoint.getClass().getName(), actorRef.path());
    final String address = AkkaUtils.getAkkaURL(actorSystem, actorRef);
    final String hostname;
    Option<String> host = actorRef.path().address().host();
    if (host.isEmpty()) {
        hostname = "localhost";
    } else {
        hostname = host.get();
    }
    InvocationHandler akkaInvocationHandler = new AkkaInvocationHandler(address, hostname, actorRef, timeout, maximumFramesize, terminationFuture);
    // Rather than using the System ClassLoader directly, we derive the ClassLoader
    // from this class . That works better in cases where Flink runs embedded and all Flink
    // code is loaded dynamically (for example from an OSGI bundle) through a custom ClassLoader
    ClassLoader classLoader = getClass().getClassLoader();
    @SuppressWarnings("unchecked") C self = (C) Proxy.newProxyInstance(classLoader, new Class<?>[] { rpcEndpoint.getSelfGatewayType(), SelfGateway.class, MainThreadExecutable.class, StartStoppable.class, AkkaGateway.class }, akkaInvocationHandler);
    return self;
}
Also used : ActorRef(akka.actor.ActorRef) Props(akka.actor.Props) FlinkCompletableFuture(org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture) InvocationHandler(java.lang.reflect.InvocationHandler) MainThreadExecutable(org.apache.flink.runtime.rpc.MainThreadExecutable) SelfGateway(org.apache.flink.runtime.rpc.SelfGateway) StartStoppable(org.apache.flink.runtime.rpc.StartStoppable)

Example 17 with FlinkCompletableFuture

use of org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture in project flink by apache.

the class ExecutionGraphSchedulingTest method testTimeoutForSlotAllocation.

/**
	 * This test verifies that the slot allocations times out after a certain time, and that
	 * all slots are released in that case.
	 */
@Test
public void testTimeoutForSlotAllocation() throws Exception {
    //  we construct a simple graph:    (task)
    final int parallelism = 3;
    final JobVertex vertex = new JobVertex("task");
    vertex.setParallelism(parallelism);
    vertex.setInvokableClass(NoOpInvokable.class);
    final JobID jobId = new JobID();
    final JobGraph jobGraph = new JobGraph(jobId, "test", vertex);
    final SlotOwner slotOwner = mock(SlotOwner.class);
    final TaskManagerGateway taskManager = mock(TaskManagerGateway.class);
    final SimpleSlot[] slots = new SimpleSlot[parallelism];
    @SuppressWarnings({ "unchecked", "rawtypes" }) final FlinkCompletableFuture<SimpleSlot>[] slotFutures = new FlinkCompletableFuture[parallelism];
    for (int i = 0; i < parallelism; i++) {
        slots[i] = createSlot(taskManager, jobId, slotOwner);
        slotFutures[i] = new FlinkCompletableFuture<>();
    }
    ProgrammedSlotProvider slotProvider = new ProgrammedSlotProvider(parallelism);
    slotProvider.addSlots(vertex.getID(), slotFutures);
    final ExecutionGraph eg = createExecutionGraph(jobGraph, slotProvider, Time.milliseconds(20));
    final TerminalJobStatusListener statusListener = new TerminalJobStatusListener();
    eg.registerJobStatusListener(statusListener);
    //  we complete one future
    slotFutures[1].complete(slots[1]);
    //  kick off the scheduling
    eg.setScheduleMode(ScheduleMode.EAGER);
    eg.setQueuedSchedulingAllowed(true);
    eg.scheduleForExecution();
    //  we complete another future
    slotFutures[2].complete(slots[2]);
    // since future[0] is still missing the while operation must time out
    // we have no restarts allowed, so the job will go terminal
    statusListener.waitForTerminalState(2000);
    // wait until all slots are back
    verify(slotOwner, new Timeout(2000, times(2))).returnAllocatedSlot(any(Slot.class));
    //  verify that no deployments have happened
    verify(taskManager, times(0)).submitTask(any(TaskDeploymentDescriptor.class), any(Time.class));
    for (Future<SimpleSlot> future : slotFutures) {
        if (future.isDone()) {
            assertTrue(future.get().isCanceled());
        }
    }
}
Also used : Timeout(org.mockito.verification.Timeout) TaskManagerGateway(org.apache.flink.runtime.jobmanager.slots.TaskManagerGateway) Time(org.apache.flink.api.common.time.Time) SimpleSlot(org.apache.flink.runtime.instance.SimpleSlot) FlinkCompletableFuture(org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture) SlotOwner(org.apache.flink.runtime.jobmanager.slots.SlotOwner) JobGraph(org.apache.flink.runtime.jobgraph.JobGraph) JobVertex(org.apache.flink.runtime.jobgraph.JobVertex) Slot(org.apache.flink.runtime.instance.Slot) SimpleSlot(org.apache.flink.runtime.instance.SimpleSlot) AllocatedSlot(org.apache.flink.runtime.jobmanager.slots.AllocatedSlot) TaskDeploymentDescriptor(org.apache.flink.runtime.deployment.TaskDeploymentDescriptor) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 18 with FlinkCompletableFuture

use of org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture in project flink by apache.

the class FlinkFutureTest method testGetNow.

@Test
public void testGetNow() throws ExecutionException {
    CompletableFuture<Integer> initialFuture = new FlinkCompletableFuture<>();
    final int absentValue = 41;
    assertEquals(new Integer(absentValue), initialFuture.getNow(absentValue));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FlinkCompletableFuture(org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture) Test(org.junit.Test)

Example 19 with FlinkCompletableFuture

use of org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture in project flink by apache.

the class FutureUtilsTest method testConjunctFutureCompletion.

@Test
public void testConjunctFutureCompletion() throws Exception {
    // some futures that we combine
    CompletableFuture<Object> future1 = new FlinkCompletableFuture<>();
    CompletableFuture<Object> future2 = new FlinkCompletableFuture<>();
    CompletableFuture<Object> future3 = new FlinkCompletableFuture<>();
    CompletableFuture<Object> future4 = new FlinkCompletableFuture<>();
    // some future is initially completed
    future2.complete(new Object());
    // build the conjunct future
    ConjunctFuture result = FutureUtils.combineAll(Arrays.asList(future1, future2, future3, future4));
    Future<Void> resultMapped = result.thenAccept(new AcceptFunction<Void>() {

        @Override
        public void accept(Void value) {
        }
    });
    assertEquals(4, result.getNumFuturesTotal());
    assertEquals(1, result.getNumFuturesCompleted());
    assertFalse(result.isDone());
    assertFalse(resultMapped.isDone());
    // complete two more futures
    future4.complete(new Object());
    assertEquals(2, result.getNumFuturesCompleted());
    assertFalse(result.isDone());
    assertFalse(resultMapped.isDone());
    future1.complete(new Object());
    assertEquals(3, result.getNumFuturesCompleted());
    assertFalse(result.isDone());
    assertFalse(resultMapped.isDone());
    // complete one future again
    future1.complete(new Object());
    assertEquals(3, result.getNumFuturesCompleted());
    assertFalse(result.isDone());
    assertFalse(resultMapped.isDone());
    // complete the final future
    future3.complete(new Object());
    assertEquals(4, result.getNumFuturesCompleted());
    assertTrue(result.isDone());
    assertTrue(resultMapped.isDone());
}
Also used : ConjunctFuture(org.apache.flink.runtime.concurrent.FutureUtils.ConjunctFuture) FlinkCompletableFuture(org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture) Test(org.junit.Test)

Example 20 with FlinkCompletableFuture

use of org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture in project flink by apache.

the class FutureUtilsTest method testConjunctFutureFailureOnSuccessive.

@Test
public void testConjunctFutureFailureOnSuccessive() throws Exception {
    CompletableFuture<Object> future1 = new FlinkCompletableFuture<>();
    CompletableFuture<Object> future2 = new FlinkCompletableFuture<>();
    CompletableFuture<Object> future3 = new FlinkCompletableFuture<>();
    CompletableFuture<Object> future4 = new FlinkCompletableFuture<>();
    // build the conjunct future
    ConjunctFuture result = FutureUtils.combineAll(Arrays.asList(future1, future2, future3, future4));
    assertEquals(4, result.getNumFuturesTotal());
    Future<Void> resultMapped = result.thenAccept(new AcceptFunction<Void>() {

        @Override
        public void accept(Void value) {
        }
    });
    future1.complete(new Object());
    future3.complete(new Object());
    future4.complete(new Object());
    future2.completeExceptionally(new IOException());
    assertEquals(3, result.getNumFuturesCompleted());
    assertTrue(result.isDone());
    assertTrue(resultMapped.isDone());
    try {
        result.get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof IOException);
    }
    try {
        resultMapped.get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof IOException);
    }
}
Also used : ConjunctFuture(org.apache.flink.runtime.concurrent.FutureUtils.ConjunctFuture) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) FlinkCompletableFuture(org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture) Test(org.junit.Test)

Aggregations

FlinkCompletableFuture (org.apache.flink.runtime.concurrent.impl.FlinkCompletableFuture)21 Test (org.junit.Test)17 SimpleSlot (org.apache.flink.runtime.instance.SimpleSlot)10 Time (org.apache.flink.api.common.time.Time)8 JobID (org.apache.flink.api.common.JobID)6 TaskManagerGateway (org.apache.flink.runtime.jobmanager.slots.TaskManagerGateway)6 TaskDeploymentDescriptor (org.apache.flink.runtime.deployment.TaskDeploymentDescriptor)5 Instance (org.apache.flink.runtime.instance.Instance)5 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)5 JobVertex (org.apache.flink.runtime.jobgraph.JobVertex)5 JobVertexID (org.apache.flink.runtime.jobgraph.JobVertexID)5 IOException (java.io.IOException)4 ScheduledUnit (org.apache.flink.runtime.jobmanager.scheduler.ScheduledUnit)4 Scheduler (org.apache.flink.runtime.jobmanager.scheduler.Scheduler)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ConjunctFuture (org.apache.flink.runtime.concurrent.FutureUtils.ConjunctFuture)3 ExecutionGraphTestUtils.getExecutionVertex (org.apache.flink.runtime.executiongraph.ExecutionGraphTestUtils.getExecutionVertex)3 ExecutionGraphTestUtils.getInstance (org.apache.flink.runtime.executiongraph.ExecutionGraphTestUtils.getInstance)3 ExecutionVertex (org.apache.flink.runtime.executiongraph.ExecutionVertex)3 Slot (org.apache.flink.runtime.instance.Slot)3