Search in sources :

Example 1 with ManagedChannelFactory

use of org.apache.beam.sdk.fn.channel.ManagedChannelFactory in project beam by apache.

the class BeamFnStatusClientTest method testActiveBundleState.

@Test
public void testActiveBundleState() {
    ProcessBundleHandler handler = mock(ProcessBundleHandler.class);
    BundleProcessorCache processorCache = mock(BundleProcessorCache.class);
    Map<String, BundleProcessor> bundleProcessorMap = new HashMap<>();
    for (int i = 0; i < 11; i++) {
        BundleProcessor processor = mock(BundleProcessor.class);
        ExecutionStateTracker executionStateTracker = mock(ExecutionStateTracker.class);
        when(processor.getStateTracker()).thenReturn(executionStateTracker);
        when(executionStateTracker.getMillisSinceLastTransition()).thenReturn(Integer.toUnsignedLong((10 - i) * 1000));
        when(executionStateTracker.getTrackedThread()).thenReturn(Thread.currentThread());
        String instruction = Integer.toString(i);
        when(processorCache.find(instruction)).thenReturn(processor);
        bundleProcessorMap.put(instruction, processor);
    }
    when(handler.getBundleProcessorCache()).thenReturn(processorCache);
    when(processorCache.getActiveBundleProcessors()).thenReturn(bundleProcessorMap);
    ManagedChannelFactory channelFactory = ManagedChannelFactory.createInProcess();
    BeamFnStatusClient client = new BeamFnStatusClient(apiServiceDescriptor, channelFactory::forDescriptor, handler.getBundleProcessorCache(), PipelineOptionsFactory.create(), Caches.noop());
    StringJoiner joiner = new StringJoiner("\n");
    joiner.add(client.getActiveProcessBundleState());
    String actualState = joiner.toString();
    List<String> expectedInstructions = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        expectedInstructions.add(String.format("Instruction %d", i));
    }
    assertThat(actualState, stringContainsInOrder(expectedInstructions));
    assertThat(actualState, not(containsString("Instruction 10")));
}
Also used : HashMap(java.util.HashMap) BundleProcessorCache(org.apache.beam.fn.harness.control.ProcessBundleHandler.BundleProcessorCache) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ProcessBundleHandler(org.apache.beam.fn.harness.control.ProcessBundleHandler) ManagedChannelFactory(org.apache.beam.sdk.fn.channel.ManagedChannelFactory) ExecutionStateTracker(org.apache.beam.runners.core.metrics.ExecutionStateTracker) BundleProcessor(org.apache.beam.fn.harness.control.ProcessBundleHandler.BundleProcessor) StringJoiner(java.util.StringJoiner) Test(org.junit.Test)

Example 2 with ManagedChannelFactory

use of org.apache.beam.sdk.fn.channel.ManagedChannelFactory in project beam by apache.

the class BeamFnStatusClientTest method testCacheStatsExist.

@Test
public void testCacheStatsExist() {
    ManagedChannelFactory channelFactory = ManagedChannelFactory.createInProcess();
    BeamFnStatusClient client = new BeamFnStatusClient(apiServiceDescriptor, channelFactory::forDescriptor, mock(BundleProcessorCache.class), PipelineOptionsFactory.create(), Caches.fromOptions(PipelineOptionsFactory.fromArgs("--maxCacheMemoryUsageMb=234").create()));
    assertThat(client.getCacheStats(), containsString("used/max 0/234 MB"));
}
Also used : ManagedChannelFactory(org.apache.beam.sdk.fn.channel.ManagedChannelFactory) BundleProcessorCache(org.apache.beam.fn.harness.control.ProcessBundleHandler.BundleProcessorCache) Test(org.junit.Test)

Example 3 with ManagedChannelFactory

use of org.apache.beam.sdk.fn.channel.ManagedChannelFactory in project beam by apache.

the class BeamFnStatusClientTest method testWorkerStatusResponse.

@Test
public void testWorkerStatusResponse() throws Exception {
    BlockingQueue<WorkerStatusResponse> values = new LinkedBlockingQueue<>();
    BlockingQueue<StreamObserver<WorkerStatusRequest>> requestObservers = new LinkedBlockingQueue<>();
    StreamObserver<WorkerStatusResponse> inboundServerObserver = TestStreams.withOnNext(values::add).build();
    Server server = InProcessServerBuilder.forName(apiServiceDescriptor.getUrl()).addService(new BeamFnWorkerStatusImplBase() {

        @Override
        public StreamObserver<WorkerStatusResponse> workerStatus(StreamObserver<WorkerStatusRequest> responseObserver) {
            Uninterruptibles.putUninterruptibly(requestObservers, responseObserver);
            return inboundServerObserver;
        }
    }).build();
    server.start();
    try {
        BundleProcessorCache processorCache = mock(BundleProcessorCache.class);
        when(processorCache.getActiveBundleProcessors()).thenReturn(Collections.emptyMap());
        ManagedChannelFactory channelFactory = ManagedChannelFactory.createInProcess();
        new BeamFnStatusClient(apiServiceDescriptor, channelFactory::forDescriptor, processorCache, PipelineOptionsFactory.create(), Caches.noop());
        StreamObserver<WorkerStatusRequest> requestObserver = requestObservers.take();
        requestObserver.onNext(WorkerStatusRequest.newBuilder().setId("id").build());
        WorkerStatusResponse response = values.take();
        assertThat(response.getStatusInfo(), containsString("No active processing bundles."));
        assertThat(response.getId(), is("id"));
    } finally {
        server.shutdownNow();
    }
}
Also used : StreamObserver(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver) WorkerStatusResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.WorkerStatusResponse) Server(org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Server) BundleProcessorCache(org.apache.beam.fn.harness.control.ProcessBundleHandler.BundleProcessorCache) WorkerStatusRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.WorkerStatusRequest) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ManagedChannelFactory(org.apache.beam.sdk.fn.channel.ManagedChannelFactory) BeamFnWorkerStatusImplBase(org.apache.beam.model.fnexecution.v1.BeamFnWorkerStatusGrpc.BeamFnWorkerStatusImplBase) Test(org.junit.Test)

Example 4 with ManagedChannelFactory

use of org.apache.beam.sdk.fn.channel.ManagedChannelFactory in project beam by apache.

the class FnHarness method main.

/**
 * Run a FnHarness with the given id and options that attaches to the specified logging and
 * control API service descriptors.
 *
 * @param id Harness ID
 * @param options The options for this pipeline
 * @param runnerCapabilites
 * @param loggingApiServiceDescriptor
 * @param controlApiServiceDescriptor
 * @param statusApiServiceDescriptor
 * @throws Exception
 */
public static void main(String id, PipelineOptions options, Set<String> runnerCapabilites, Endpoints.ApiServiceDescriptor loggingApiServiceDescriptor, Endpoints.ApiServiceDescriptor controlApiServiceDescriptor, @Nullable Endpoints.ApiServiceDescriptor statusApiServiceDescriptor) throws Exception {
    ManagedChannelFactory channelFactory;
    if (ExperimentalOptions.hasExperiment(options, "beam_fn_api_epoll")) {
        channelFactory = ManagedChannelFactory.createEpoll();
    } else {
        channelFactory = ManagedChannelFactory.createDefault();
    }
    OutboundObserverFactory outboundObserverFactory = HarnessStreamObserverFactories.fromOptions(options);
    main(id, options, runnerCapabilites, loggingApiServiceDescriptor, controlApiServiceDescriptor, statusApiServiceDescriptor, channelFactory, outboundObserverFactory, Caches.fromOptions(options));
}
Also used : ManagedChannelFactory(org.apache.beam.sdk.fn.channel.ManagedChannelFactory) OutboundObserverFactory(org.apache.beam.sdk.fn.stream.OutboundObserverFactory)

Aggregations

ManagedChannelFactory (org.apache.beam.sdk.fn.channel.ManagedChannelFactory)4 BundleProcessorCache (org.apache.beam.fn.harness.control.ProcessBundleHandler.BundleProcessorCache)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StringJoiner (java.util.StringJoiner)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ProcessBundleHandler (org.apache.beam.fn.harness.control.ProcessBundleHandler)1 BundleProcessor (org.apache.beam.fn.harness.control.ProcessBundleHandler.BundleProcessor)1 WorkerStatusRequest (org.apache.beam.model.fnexecution.v1.BeamFnApi.WorkerStatusRequest)1 WorkerStatusResponse (org.apache.beam.model.fnexecution.v1.BeamFnApi.WorkerStatusResponse)1 BeamFnWorkerStatusImplBase (org.apache.beam.model.fnexecution.v1.BeamFnWorkerStatusGrpc.BeamFnWorkerStatusImplBase)1 ExecutionStateTracker (org.apache.beam.runners.core.metrics.ExecutionStateTracker)1 OutboundObserverFactory (org.apache.beam.sdk.fn.stream.OutboundObserverFactory)1 Server (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.Server)1 StreamObserver (org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1