use of org.apache.beam.model.pipeline.v1.RunnerApi.Environment in project beam by apache.
the class CreateExecutableStageNodeFunction method getEnvironmentFromPTransform.
private Environment getEnvironmentFromPTransform(RunnerApi.Components components, Set<PTransformNode> sdkTransforms) {
RehydratedComponents sdkComponents = RehydratedComponents.forComponents(components);
Environment env = null;
for (PTransformNode pTransformNode : sdkTransforms) {
env = Environments.getEnvironment(pTransformNode.getTransform(), sdkComponents).orElse(null);
if (env != null) {
break;
}
}
return env;
}
use of org.apache.beam.model.pipeline.v1.RunnerApi.Environment in project beam by apache.
the class RemoteEnvironmentTest method forHandlerReturnsProvided.
@Test
public void forHandlerReturnsProvided() {
InstructionRequestHandler handler = mock(InstructionRequestHandler.class);
Environment environment = Environments.createDockerEnvironment("my_url");
RemoteEnvironment remoteEnvironment = RemoteEnvironment.forHandler(environment, handler);
assertThat(remoteEnvironment.getEnvironment(), theInstance(environment));
assertThat(remoteEnvironment.getInstructionRequestHandler(), theInstance(handler));
}
use of org.apache.beam.model.pipeline.v1.RunnerApi.Environment in project beam by apache.
the class SingleEnvironmentInstanceJobBundleFactoryTest method closeShutsDownEnvironmentsWhenSomeFail.
@Test
public void closeShutsDownEnvironmentsWhenSomeFail() throws Exception {
Pipeline p = Pipeline.create();
ExperimentalOptions.addExperiment(p.getOptions().as(ExperimentalOptions.class), "beam_fn_api");
p.apply("Create", Create.of(1, 2, 3));
ExecutableStage firstEnvStage = GreedyPipelineFuser.fuse(PipelineTranslation.toProto(p)).getFusedStages().stream().findFirst().get();
ExecutableStagePayload basePayload = ExecutableStagePayload.parseFrom(firstEnvStage.toPTransform("foo").getSpec().getPayload());
Environment secondEnv = Environments.createDockerEnvironment("second_env");
ExecutableStage secondEnvStage = ExecutableStage.fromPayload(basePayload.toBuilder().setEnvironment(secondEnv).build());
Environment thirdEnv = Environments.createDockerEnvironment("third_env");
ExecutableStage thirdEnvStage = ExecutableStage.fromPayload(basePayload.toBuilder().setEnvironment(thirdEnv).build());
RemoteEnvironment firstRemoteEnv = mock(RemoteEnvironment.class, "First Remote Env");
RemoteEnvironment secondRemoteEnv = mock(RemoteEnvironment.class, "Second Remote Env");
RemoteEnvironment thirdRemoteEnv = mock(RemoteEnvironment.class, "Third Remote Env");
when(environmentFactory.createEnvironment(firstEnvStage.getEnvironment(), GENERATED_ID)).thenReturn(firstRemoteEnv);
when(environmentFactory.createEnvironment(secondEnvStage.getEnvironment(), GENERATED_ID)).thenReturn(secondRemoteEnv);
when(environmentFactory.createEnvironment(thirdEnvStage.getEnvironment(), GENERATED_ID)).thenReturn(thirdRemoteEnv);
when(firstRemoteEnv.getInstructionRequestHandler()).thenReturn(instructionRequestHandler);
when(secondRemoteEnv.getInstructionRequestHandler()).thenReturn(instructionRequestHandler);
when(thirdRemoteEnv.getInstructionRequestHandler()).thenReturn(instructionRequestHandler);
factory.forStage(firstEnvStage);
factory.forStage(secondEnvStage);
factory.forStage(thirdEnvStage);
IllegalStateException firstException = new IllegalStateException("first stage");
doThrow(firstException).when(firstRemoteEnv).close();
IllegalStateException thirdException = new IllegalStateException("third stage");
doThrow(thirdException).when(thirdRemoteEnv).close();
try {
factory.close();
fail("Factory close should have thrown");
} catch (IllegalStateException expected) {
if (expected.equals(firstException)) {
assertThat(ImmutableList.copyOf(expected.getSuppressed()), contains(thirdException));
} else if (expected.equals(thirdException)) {
assertThat(ImmutableList.copyOf(expected.getSuppressed()), contains(firstException));
} else {
throw expected;
}
verify(firstRemoteEnv).close();
verify(secondRemoteEnv).close();
verify(thirdRemoteEnv).close();
}
}
use of org.apache.beam.model.pipeline.v1.RunnerApi.Environment in project beam by apache.
the class DataflowPipelineTranslatorTest method testSetWorkerHarnessContainerImageInPipelineProto.
/**
* Tests that when (deprecated) {@link
* DataflowPipelineOptions#setWorkerHarnessContainerImage(String)} pipeline option is set, {@link
* DataflowRunner} sets that value as the {@link DockerPayload#getContainerImage()} of the default
* {@link Environment} used when generating the model pipeline proto.
*/
@Test
public void testSetWorkerHarnessContainerImageInPipelineProto() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
String containerImage = "gcr.io/image:foo";
options.as(DataflowPipelineOptions.class).setWorkerHarnessContainerImage(containerImage);
Pipeline p = Pipeline.create(options);
SdkComponents sdkComponents = createSdkComponents(options);
RunnerApi.Pipeline proto = PipelineTranslation.toProto(p, sdkComponents, true);
JobSpecification specification = DataflowPipelineTranslator.fromOptions(options).translate(p, proto, sdkComponents, DataflowRunner.fromOptions(options), Collections.emptyList());
RunnerApi.Pipeline pipelineProto = specification.getPipelineProto();
assertEquals(1, pipelineProto.getComponents().getEnvironmentsCount());
Environment defaultEnvironment = Iterables.getOnlyElement(pipelineProto.getComponents().getEnvironmentsMap().values());
DockerPayload payload = DockerPayload.parseFrom(defaultEnvironment.getPayload());
assertEquals(DataflowRunner.getContainerImageForJob(options), payload.getContainerImage());
}
use of org.apache.beam.model.pipeline.v1.RunnerApi.Environment in project flink by apache.
the class BeamPythonFunctionRunner method open.
// ------------------------------------------------------------------------
@Override
public void open(PythonConfig config) throws Exception {
this.bundleStarted = false;
this.resultBuffer = new LinkedBlockingQueue<>();
this.reusableResultTuple = new Tuple2<>();
// The creation of stageBundleFactory depends on the initialized environment manager.
environmentManager.open();
PortablePipelineOptions portableOptions = PipelineOptionsFactory.as(PortablePipelineOptions.class);
if (jobOptions.containsKey(PythonOptions.STATE_CACHE_SIZE.key())) {
portableOptions.as(ExperimentalOptions.class).setExperiments(Collections.singletonList(ExperimentalOptions.STATE_CACHE_SIZE + "=" + jobOptions.get(PythonOptions.STATE_CACHE_SIZE.key())));
}
Struct pipelineOptions = PipelineOptionsTranslation.toProto(portableOptions);
if (memoryManager != null && config.isUsingManagedMemory()) {
Preconditions.checkArgument(managedMemoryFraction > 0 && managedMemoryFraction <= 1.0, "The configured managed memory fraction for Python worker process must be within (0, 1], was: %s. " + "It may be because the consumer type \"Python\" was missing or set to 0 for the config option \"taskmanager.memory.managed.consumer-weights\"." + managedMemoryFraction);
final LongFunctionWithException<PythonSharedResources, Exception> initializer = (size) -> new PythonSharedResources(createJobBundleFactory(pipelineOptions), createPythonExecutionEnvironment(size));
sharedResources = memoryManager.getSharedMemoryResourceForManagedMemory(MANAGED_MEMORY_RESOURCE_ID, initializer, managedMemoryFraction);
LOG.info("Obtained shared Python process of size {} bytes", sharedResources.getSize());
sharedResources.getResourceHandle().addPythonEnvironmentManager(environmentManager);
JobBundleFactory jobBundleFactory = sharedResources.getResourceHandle().getJobBundleFactory();
RunnerApi.Environment environment = sharedResources.getResourceHandle().getEnvironment();
stageBundleFactory = createStageBundleFactory(jobBundleFactory, environment);
} else {
// there is no way to access the MemoryManager for the batch job of old planner,
// fallback to the way that spawning a Python process for each Python operator
jobBundleFactory = createJobBundleFactory(pipelineOptions);
stageBundleFactory = createStageBundleFactory(jobBundleFactory, createPythonExecutionEnvironment(-1));
}
progressHandler = getProgressHandler(flinkMetricContainer);
}
Aggregations