Search in sources :

Example 1 with WorkItemCommitRequest

use of org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest in project beam by apache.

the class GrpcWindmillServerTest method testStreamingCommit.

@Test
public void testStreamingCommit() throws Exception {
    List<WorkItemCommitRequest> commitRequestList = new ArrayList<>();
    List<CountDownLatch> latches = new ArrayList<>();
    Map<Long, WorkItemCommitRequest> commitRequests = new HashMap<>();
    for (int i = 0; i < 500; ++i) {
        // Build some requests of varying size with a few big ones.
        WorkItemCommitRequest request = makeCommitRequest(i, i * (i < 480 ? 8 : 128));
        commitRequestList.add(request);
        commitRequests.put((long) i, request);
        latches.add(new CountDownLatch(1));
    }
    Collections.shuffle(commitRequestList);
    // This server receives WorkItemCommitRequests, and verifies they are equal to the above
    // commitRequest.
    serviceRegistry.addService(new CloudWindmillServiceV1Alpha1ImplBase() {

        @Override
        public StreamObserver<StreamingCommitWorkRequest> commitWorkStream(StreamObserver<StreamingCommitResponse> responseObserver) {
            return new StreamObserver<StreamingCommitWorkRequest>() {

                boolean sawHeader = false;

                InputStream buffer = null;

                long remainingBytes = 0;

                ResponseErrorInjector injector = new ResponseErrorInjector(responseObserver);

                @Override
                public void onNext(StreamingCommitWorkRequest request) {
                    maybeInjectError(responseObserver);
                    if (!sawHeader) {
                        errorCollector.checkThat(request.getHeader(), Matchers.equalTo(JobHeader.newBuilder().setJobId("job").setProjectId("project").setWorkerId("worker").build()));
                        sawHeader = true;
                        LOG.info("Received header");
                    } else {
                        boolean first = true;
                        LOG.info("Received request with {} chunks", request.getCommitChunkCount());
                        for (StreamingCommitRequestChunk chunk : request.getCommitChunkList()) {
                            assertTrue(chunk.getSerializedWorkItemCommit().size() <= STREAM_CHUNK_SIZE);
                            if (first || chunk.hasComputationId()) {
                                errorCollector.checkThat(chunk.getComputationId(), Matchers.equalTo("computation"));
                            }
                            if (remainingBytes != 0) {
                                errorCollector.checkThat(buffer, Matchers.notNullValue());
                                errorCollector.checkThat(remainingBytes, Matchers.is(chunk.getSerializedWorkItemCommit().size() + chunk.getRemainingBytesForWorkItem()));
                                buffer = new SequenceInputStream(buffer, chunk.getSerializedWorkItemCommit().newInput());
                            } else {
                                errorCollector.checkThat(buffer, Matchers.nullValue());
                                buffer = chunk.getSerializedWorkItemCommit().newInput();
                            }
                            remainingBytes = chunk.getRemainingBytesForWorkItem();
                            if (remainingBytes == 0) {
                                try {
                                    WorkItemCommitRequest received = WorkItemCommitRequest.parseFrom(buffer);
                                    errorCollector.checkThat(received, Matchers.equalTo(commitRequests.get(received.getWorkToken())));
                                    try {
                                        responseObserver.onNext(StreamingCommitResponse.newBuilder().addRequestId(chunk.getRequestId()).build());
                                    } catch (IllegalStateException e) {
                                    // Stream is closed.
                                    }
                                } catch (Exception e) {
                                    errorCollector.addError(e);
                                }
                                buffer = null;
                            } else {
                                errorCollector.checkThat(first, Matchers.is(true));
                            }
                            first = false;
                        }
                    }
                }

                @Override
                public void onError(Throwable throwable) {
                }

                @Override
                public void onCompleted() {
                    injector.cancel();
                    responseObserver.onCompleted();
                }
            };
        }
    });
    // Make the commit requests, waiting for each of them to be verified and acknowledged.
    CommitWorkStream stream = client.commitWorkStream();
    for (int i = 0; i < commitRequestList.size(); ) {
        final CountDownLatch latch = latches.get(i);
        if (stream.commitWorkItem("computation", commitRequestList.get(i), (CommitStatus status) -> {
            assertEquals(status, CommitStatus.OK);
            latch.countDown();
        })) {
            i++;
        } else {
            stream.flush();
        }
    }
    stream.flush();
    for (CountDownLatch latch : latches) {
        assertTrue(latch.await(1, TimeUnit.MINUTES));
    }
    stream.close();
    assertTrue(stream.awaitTermination(30, TimeUnit.SECONDS));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StreamingCommitWorkRequest(org.apache.beam.runners.dataflow.worker.windmill.Windmill.StreamingCommitWorkRequest) CommitWorkStream(org.apache.beam.runners.dataflow.worker.windmill.WindmillServerStub.CommitWorkStream) StreamingCommitRequestChunk(org.apache.beam.runners.dataflow.worker.windmill.Windmill.StreamingCommitRequestChunk) CommitStatus(org.apache.beam.runners.dataflow.worker.windmill.Windmill.CommitStatus) StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) CloudWindmillServiceV1Alpha1ImplBase(org.apache.beam.runners.dataflow.worker.windmill.CloudWindmillServiceV1Alpha1Grpc.CloudWindmillServiceV1Alpha1ImplBase) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) CountDownLatch(java.util.concurrent.CountDownLatch) StatusRuntimeException(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.StatusRuntimeException) SequenceInputStream(java.io.SequenceInputStream) WorkItemCommitRequest(org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest) StreamingCommitResponse(org.apache.beam.runners.dataflow.worker.windmill.Windmill.StreamingCommitResponse) Test(org.junit.Test)

Example 2 with WorkItemCommitRequest

use of org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest in project beam by apache.

the class FakeWindmillServer method commitWorkStream.

@Override
public CommitWorkStream commitWorkStream() {
    Instant startTime = Instant.now();
    return new CommitWorkStream() {

        @Override
        public boolean commitWorkItem(String computation, WorkItemCommitRequest request, Consumer<Windmill.CommitStatus> onDone) {
            LOG.debug("commitWorkStream::commitWorkItem: {}", request);
            errorCollector.checkThat(request.hasWorkToken(), equalTo(true));
            errorCollector.checkThat(request.getShardingKey(), allOf(greaterThan(0L), lessThan(Long.MAX_VALUE)));
            errorCollector.checkThat(request.getCacheToken(), not(equalTo(0L)));
            if (dropStreamingCommits) {
                droppedStreamingCommits.put(request.getWorkToken(), onDone);
            } else {
                commitsReceived.put(request.getWorkToken(), request);
                onDone.accept(Windmill.CommitStatus.OK);
            }
            // to simulate a dropped commit.
            return true;
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() {
        }

        @Override
        public boolean awaitTermination(int time, TimeUnit unit) {
            return true;
        }

        @Override
        public Instant startTime() {
            return startTime;
        }
    };
}
Also used : Consumer(java.util.function.Consumer) WorkItemCommitRequest(org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest) Instant(org.joda.time.Instant) Windmill(org.apache.beam.runners.dataflow.worker.windmill.Windmill) TimeUnit(java.util.concurrent.TimeUnit)

Example 3 with WorkItemCommitRequest

use of org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest in project beam by apache.

the class FakeWindmillServer method commitWork.

@Override
public CommitWorkResponse commitWork(Windmill.CommitWorkRequest request) {
    LOG.debug("commitWorkRequest: {}", request);
    validateCommitWorkRequest(request);
    for (ComputationCommitWorkRequest computationRequest : request.getRequestsList()) {
        for (WorkItemCommitRequest commit : computationRequest.getRequestsList()) {
            commitsReceived.put(commit.getWorkToken(), commit);
        }
    }
    CommitWorkResponse response = CommitWorkResponse.newBuilder().build();
    LOG.debug("commitWorkResponse: {}", response);
    return response;
}
Also used : WorkItemCommitRequest(org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest) CommitWorkResponse(org.apache.beam.runners.dataflow.worker.windmill.Windmill.CommitWorkResponse) ComputationCommitWorkRequest(org.apache.beam.runners.dataflow.worker.windmill.Windmill.ComputationCommitWorkRequest)

Example 4 with WorkItemCommitRequest

use of org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest in project beam by apache.

the class FakeWindmillServer method validateCommitWorkRequest.

private void validateCommitWorkRequest(Windmill.CommitWorkRequest request) {
    for (ComputationCommitWorkRequest computationRequest : request.getRequestsList()) {
        for (WorkItemCommitRequest commit : computationRequest.getRequestsList()) {
            errorCollector.checkThat(commit.hasWorkToken(), equalTo(true));
            errorCollector.checkThat(commit.getShardingKey(), allOf(greaterThan(0L), lessThan(Long.MAX_VALUE)));
            errorCollector.checkThat(commit.getCacheToken(), not(equalTo(0L)));
        }
    }
}
Also used : WorkItemCommitRequest(org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest) ComputationCommitWorkRequest(org.apache.beam.runners.dataflow.worker.windmill.Windmill.ComputationCommitWorkRequest)

Example 5 with WorkItemCommitRequest

use of org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest in project beam by apache.

the class WindmillStateInternals method persist.

public void persist(final Windmill.WorkItemCommitRequest.Builder commitBuilder) {
    List<Future<WorkItemCommitRequest>> commitsToMerge = new ArrayList<>();
    // Call persist on each first, which may schedule some futures for reading.
    persist(commitsToMerge, workItemState);
    persist(commitsToMerge, workItemDerivedState);
    try (Closeable scope = scopedReadStateSupplier.get()) {
        for (Future<WorkItemCommitRequest> commitFuture : commitsToMerge) {
            commitBuilder.mergeFrom(commitFuture.get());
        }
    } catch (ExecutionException | InterruptedException | IOException exc) {
        if (exc instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        throw new RuntimeException("Failed to retrieve Windmill state during persist()", exc);
    }
    cache.persist();
}
Also used : WorkItemCommitRequest(org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

WorkItemCommitRequest (org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest)10 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)5 ParallelInstruction (com.google.api.services.dataflow.model.ParallelInstruction)4 List (java.util.List)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 CloudObject (org.apache.beam.runners.dataflow.util.CloudObject)4 Structs.addString (org.apache.beam.runners.dataflow.util.Structs.addString)4 DataflowCounterUpdateExtractor.splitIntToLong (org.apache.beam.runners.dataflow.worker.counters.DataflowCounterUpdateExtractor.splitIntToLong)4 UnsignedLong (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.UnsignedLong)4 Test (org.junit.Test)4 CounterUpdate (com.google.api.services.dataflow.model.CounterUpdate)3 InstructionInput (com.google.api.services.dataflow.model.InstructionInput)3 InstructionOutput (com.google.api.services.dataflow.model.InstructionOutput)3 ParDoInstruction (com.google.api.services.dataflow.model.ParDoInstruction)3 InputStream (java.io.InputStream)3 SdkComponents (org.apache.beam.runners.core.construction.SdkComponents)3 WindowedValue (org.apache.beam.sdk.util.WindowedValue)3