Search in sources :

Example 1 with PortablePipelineOptions

use of org.apache.beam.sdk.options.PortablePipelineOptions in project beam by apache.

the class DefaultJobBundleFactoryTest method loadBalancesBundles.

@Test
public void loadBalancesBundles() throws Exception {
    PortablePipelineOptions portableOptions = PipelineOptionsFactory.as(PortablePipelineOptions.class);
    portableOptions.setSdkWorkerParallelism(2);
    portableOptions.setLoadBalanceBundles(true);
    Struct pipelineOptions = PipelineOptionsTranslation.toProto(portableOptions);
    try (DefaultJobBundleFactory bundleFactory = new DefaultJobBundleFactory(JobInfo.create("testJob", "testJob", "token", pipelineOptions), envFactoryProviderMap, stageIdGenerator, serverInfo)) {
        OutputReceiverFactory orf = mock(OutputReceiverFactory.class);
        StateRequestHandler srh = mock(StateRequestHandler.class);
        when(srh.getCacheTokens()).thenReturn(Collections.emptyList());
        StageBundleFactory sbf = bundleFactory.forStage(getExecutableStage(environment));
        RemoteBundle b1 = sbf.getBundle(orf, srh, BundleProgressHandler.ignored());
        verify(envFactory, Mockito.times(1)).createEnvironment(eq(environment), any());
        final RemoteBundle b2 = sbf.getBundle(orf, srh, BundleProgressHandler.ignored());
        verify(envFactory, Mockito.times(2)).createEnvironment(eq(environment), any());
        AtomicBoolean b2Closing = new AtomicBoolean(false);
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        ScheduledFuture<Optional<Exception>> closingFuture = executor.schedule(() -> {
            try {
                b2Closing.compareAndSet(false, true);
                b2.close();
                return Optional.empty();
            } catch (Exception e) {
                return Optional.of(e);
            }
        }, 100, TimeUnit.MILLISECONDS);
        assertThat(b2Closing.get(), equalTo(false));
        // This call should block until closingFuture has finished closing b2 (100ms)
        RemoteBundle b3 = sbf.getBundle(orf, srh, BundleProgressHandler.ignored());
        // ensure the previous call waited for close
        assertThat(b2Closing.get(), equalTo(true));
        // Join closingFuture and check if an exception occurred
        Optional<Exception> closingException = closingFuture.get();
        if (closingException.isPresent()) {
            throw new AssertionError("Exception occurred while closing b2", closingException.get());
        }
        verify(envFactory, Mockito.times(2)).createEnvironment(eq(environment), any());
        b3.close();
        b1.close();
        executor.shutdown();
    }
}
Also used : StateRequestHandler(org.apache.beam.runners.fnexecution.state.StateRequestHandler) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Optional(java.util.Optional) ExpectedException(org.junit.rules.ExpectedException) Struct(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.Struct) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PortablePipelineOptions(org.apache.beam.sdk.options.PortablePipelineOptions) Test(org.junit.Test)

Example 2 with PortablePipelineOptions

use of org.apache.beam.sdk.options.PortablePipelineOptions in project beam by apache.

the class DefaultJobBundleFactory method getMaxEnvironmentClients.

private static int getMaxEnvironmentClients(JobInfo jobInfo) {
    PortablePipelineOptions pipelineOptions = PipelineOptionsTranslation.fromProto(jobInfo.pipelineOptions()).as(PortablePipelineOptions.class);
    int maxEnvironments = MoreObjects.firstNonNull(pipelineOptions.getSdkWorkerParallelism(), 1);
    Preconditions.checkArgument(maxEnvironments >= 0, "sdk_worker_parallelism must be >= 0");
    if (maxEnvironments == 0) {
        // if this is 0, use the auto behavior of num_cores - 1 so that we leave some resources
        // available for the java process
        maxEnvironments = Math.max(Runtime.getRuntime().availableProcessors() - 1, 1);
    }
    return maxEnvironments;
}
Also used : PortablePipelineOptions(org.apache.beam.sdk.options.PortablePipelineOptions)

Example 3 with PortablePipelineOptions

use of org.apache.beam.sdk.options.PortablePipelineOptions in project beam by apache.

the class DefaultJobBundleFactory method createServerInfo.

private ServerInfo createServerInfo(JobInfo jobInfo, ServerFactory serverFactory) throws IOException {
    Preconditions.checkNotNull(serverFactory, "serverFactory can not be null");
    PortablePipelineOptions portableOptions = PipelineOptionsTranslation.fromProto(jobInfo.pipelineOptions()).as(PortablePipelineOptions.class);
    GrpcFnServer<FnApiControlClientPoolService> controlServer = GrpcFnServer.allocatePortAndCreateFor(FnApiControlClientPoolService.offeringClientsToPool(clientPool.getSink(), GrpcContextHeaderAccessorProvider.getHeaderAccessor()), serverFactory);
    GrpcFnServer<GrpcLoggingService> loggingServer = GrpcFnServer.allocatePortAndCreateFor(GrpcLoggingService.forWriter(Slf4jLogWriter.getDefault()), serverFactory);
    GrpcFnServer<ArtifactRetrievalService> retrievalServer = GrpcFnServer.allocatePortAndCreateFor(new ArtifactRetrievalService(), serverFactory);
    ProvisionApi.ProvisionInfo.Builder provisionInfo = jobInfo.toProvisionInfo().toBuilder();
    provisionInfo.setLoggingEndpoint(loggingServer.getApiServiceDescriptor());
    provisionInfo.setArtifactEndpoint(retrievalServer.getApiServiceDescriptor());
    provisionInfo.setControlEndpoint(controlServer.getApiServiceDescriptor());
    GrpcFnServer<StaticGrpcProvisionService> provisioningServer = GrpcFnServer.allocatePortAndCreateFor(StaticGrpcProvisionService.create(provisionInfo.build(), GrpcContextHeaderAccessorProvider.getHeaderAccessor()), serverFactory);
    GrpcFnServer<GrpcDataService> dataServer = GrpcFnServer.allocatePortAndCreateFor(GrpcDataService.create(portableOptions, executor, OutboundObserverFactory.serverDirect()), serverFactory);
    GrpcFnServer<GrpcStateService> stateServer = GrpcFnServer.allocatePortAndCreateFor(GrpcStateService.create(), serverFactory);
    ServerInfo serverInfo = new AutoValue_DefaultJobBundleFactory_ServerInfo.Builder().setControlServer(controlServer).setLoggingServer(loggingServer).setRetrievalServer(retrievalServer).setProvisioningServer(provisioningServer).setDataServer(dataServer).setStateServer(stateServer).build();
    return serverInfo;
}
Also used : StaticGrpcProvisionService(org.apache.beam.runners.fnexecution.provisioning.StaticGrpcProvisionService) GrpcStateService(org.apache.beam.runners.fnexecution.state.GrpcStateService) GrpcDataService(org.apache.beam.runners.fnexecution.data.GrpcDataService) ArtifactRetrievalService(org.apache.beam.runners.fnexecution.artifact.ArtifactRetrievalService) GrpcLoggingService(org.apache.beam.runners.fnexecution.logging.GrpcLoggingService) PortablePipelineOptions(org.apache.beam.sdk.options.PortablePipelineOptions)

Example 4 with PortablePipelineOptions

use of org.apache.beam.sdk.options.PortablePipelineOptions in project beam by apache.

the class FlinkJobInvoker method invokeWithExecutor.

@Override
protected JobInvocation invokeWithExecutor(RunnerApi.Pipeline pipeline, Struct options, @Nullable String retrievalToken, ListeningExecutorService executorService) {
    // TODO: How to make Java/Python agree on names of keys and their values?
    LOG.trace("Parsing pipeline options");
    FlinkPipelineOptions flinkOptions = PipelineOptionsTranslation.fromProto(options).as(FlinkPipelineOptions.class);
    String invocationId = String.format("%s_%s", flinkOptions.getJobName(), UUID.randomUUID().toString());
    if (FlinkPipelineOptions.AUTO.equals(flinkOptions.getFlinkMaster())) {
        flinkOptions.setFlinkMaster(serverConfig.getFlinkMaster());
    }
    PortablePipelineOptions portableOptions = flinkOptions.as(PortablePipelineOptions.class);
    PortablePipelineRunner pipelineRunner;
    if (Strings.isNullOrEmpty(portableOptions.getOutputExecutablePath())) {
        pipelineRunner = new FlinkPipelineRunner(flinkOptions, serverConfig.getFlinkConfDir(), detectClassPathResourcesToStage(FlinkJobInvoker.class.getClassLoader(), flinkOptions));
    } else {
        pipelineRunner = new PortablePipelineJarCreator(FlinkPipelineRunner.class);
    }
    flinkOptions.setRunner(null);
    LOG.info("Invoking job {} with pipeline runner {}", invocationId, pipelineRunner);
    return createJobInvocation(invocationId, retrievalToken, executorService, pipeline, flinkOptions, pipelineRunner);
}
Also used : PortablePipelineRunner(org.apache.beam.runners.jobsubmission.PortablePipelineRunner) PortablePipelineOptions(org.apache.beam.sdk.options.PortablePipelineOptions) PortablePipelineJarCreator(org.apache.beam.runners.jobsubmission.PortablePipelineJarCreator)

Example 5 with PortablePipelineOptions

use of org.apache.beam.sdk.options.PortablePipelineOptions in project beam by apache.

the class EnvironmentsTest method createEnvironmentDockerFromEnvironmentOptions.

@Test
public void createEnvironmentDockerFromEnvironmentOptions() {
    PortablePipelineOptions options = PipelineOptionsFactory.as(PortablePipelineOptions.class);
    options.setDefaultEnvironmentType(Environments.ENVIRONMENT_DOCKER);
    options.setEnvironmentOptions(ImmutableList.of("docker_container_image=java"));
    assertThat(Environments.createOrGetDefaultEnvironment(options), is(Environment.newBuilder().setUrn(BeamUrns.getUrn(StandardEnvironments.Environments.DOCKER)).setPayload(DockerPayload.newBuilder().setContainerImage("java").build().toByteString()).addAllCapabilities(Environments.getJavaCapabilities()).build()));
}
Also used : PortablePipelineOptions(org.apache.beam.sdk.options.PortablePipelineOptions) Test(org.junit.Test)

Aggregations

PortablePipelineOptions (org.apache.beam.sdk.options.PortablePipelineOptions)21 Test (org.junit.Test)10 Struct (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.Struct)4 IOException (java.io.IOException)3 StateRequestHandler (org.apache.beam.runners.fnexecution.state.StateRequestHandler)3 ExperimentalOptions (org.apache.beam.sdk.options.ExperimentalOptions)3 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)3 Optional (java.util.Optional)2 Environment (org.apache.beam.model.pipeline.v1.RunnerApi.Environment)2 EnvironmentFactory (org.apache.beam.runners.fnexecution.environment.EnvironmentFactory)2 Provider (org.apache.beam.runners.fnexecution.environment.EnvironmentFactory.Provider)2 RemoteEnvironment (org.apache.beam.runners.fnexecution.environment.RemoteEnvironment)2 ServerFactory (org.apache.beam.sdk.fn.server.ServerFactory)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1