Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions runners/google-cloud-dataflow-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def legacyPipelineOptions = [
"--project=${gcpProject}",
"--region=${gcpRegion}",
"--tempRoot=${dataflowValidatesTempRoot}",
"--stagingLocation=${dataflowValidatesTempRoot}/staging",
"--dataflowWorkerJar=${dataflowLegacyWorkerJar}",
"--usePublicIps=false",
"--experiments=enable_lineage"
Expand All @@ -190,6 +191,7 @@ def runnerV2CommonPipelineOptions = [
"--project=${gcpProject}",
"--region=${gcpRegion}",
"--tempRoot=${dataflowValidatesTempRoot}",
"--stagingLocation=${dataflowValidatesTempRoot}/staging",
"--experiments=use_unified_worker,use_runner_v2",
"--firestoreDb=${firestoreDb}",
"--usePublicIps=false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public static TestDataflowRunner fromOptions(PipelineOptions options) {
tempLocation = tempLocation.substring(0, tempLocation.length() - File.separator.length());
}
dataflowOptions.setTempLocation(tempLocation);
String defaultPerJobStagingLocation =
FileSystems.matchNewDirectory(tempLocation, "staging").toString();
if (defaultPerJobStagingLocation.equals(dataflowOptions.getStagingLocation())) {
dataflowOptions.setStagingLocation(
FileSystems.matchNewDirectory(dataflowOptions.getTempRoot(), "staging").toString());
}

return new TestDataflowRunner(
dataflowOptions, DataflowClient.create(options.as(DataflowPipelineOptions.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,42 @@
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;

import com.google.api.services.dataflow.model.DataflowPackage;
import com.google.auto.value.AutoValue;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.apache.beam.runners.dataflow.options.DataflowPipelineOptions;
import org.apache.beam.runners.dataflow.util.PackageUtil.StagedFile;
import org.apache.beam.sdk.extensions.gcp.storage.GcsCreateOptions;
import org.apache.beam.sdk.options.PipelineOptions;
import org.apache.beam.sdk.util.MimeTypes;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Throwables;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.Cache;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.CacheBuilder;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.util.concurrent.UncheckedExecutionException;

/** Utility class for staging files to GCS. */
public class GcsStager implements Stager {
@AutoValue
abstract static class StagedFilesCacheKey {
abstract String getStagingLocation();

abstract List<StagedFile> getFilesToStage();

static StagedFilesCacheKey of(String stagingLocation, List<StagedFile> filesToStage) {
return new AutoValue_GcsStager_StagedFilesCacheKey(stagingLocation, filesToStage);
}
}

private static final int MAX_STAGED_FILES_CACHE_SIZE = 5000;

private static final Cache<StagedFilesCacheKey, List<DataflowPackage>> STAGED_FILES_CACHE =
CacheBuilder.newBuilder()
.maximumSize(MAX_STAGED_FILES_CACHE_SIZE)
.expireAfterWrite(Duration.ofMinutes(30))
.build();

private DataflowPipelineOptions options;

private GcsStager(DataflowPipelineOptions options) {
Expand All @@ -49,6 +76,24 @@ public static GcsStager fromOptions(PipelineOptions options) {
*/
@Override
public List<DataflowPackage> stageFiles(List<StagedFile> filesToStage) {
String stagingLocation = options.getStagingLocation();
if (stagingLocation != null) {
StagedFilesCacheKey cacheKey = StagedFilesCacheKey.of(stagingLocation, filesToStage);
try {
return STAGED_FILES_CACHE.get(
cacheKey, () -> Collections.unmodifiableList(stageFilesUncached(filesToStage)));
} catch (ExecutionException | UncheckedExecutionException e) {
if (e.getCause() != null) {
Throwables.throwIfUnchecked(e.getCause());
throw new RuntimeException(e.getCause());
}
throw new RuntimeException(e);
}
}
return stageFilesUncached(filesToStage);
}

private List<DataflowPackage> stageFilesUncached(List<StagedFile> filesToStage) {
try (PackageUtil packageUtil = PackageUtil.withDefaultThreadPool()) {
return packageUtil.stageClasspathElements(
filesToStage, options.getStagingLocation(), buildCreateOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.beam.sdk.extensions.gcp.auth.TestCredential;
import org.apache.beam.sdk.extensions.gcp.storage.NoopPathValidator;
import org.apache.beam.sdk.extensions.gcp.util.Transport;
import org.apache.beam.sdk.io.FileSystems;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.testing.PAssert;
import org.apache.beam.sdk.testing.SerializableMatcher;
Expand Down Expand Up @@ -94,6 +95,7 @@ public void setUp() throws Exception {
options.setGcpCredential(new TestCredential());
options.setRunner(TestDataflowRunner.class);
options.setPathValidatorClass(NoopPathValidator.class);
FileSystems.setDefaultPipelineOptions(options);
}

@Test
Expand All @@ -102,6 +104,24 @@ public void testToString() {
"TestDataflowRunner#TestAppName", TestDataflowRunner.fromOptions(options).toString());
}

@Test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not directly due to this PR) Running the test alone (./gradlew :runners:google-cloud-dataflow-java:test --tests *testFromOptionsUsesSharedStagingLocationUnderTempRoot) it fails with

java.lang.IllegalArgumentException: No filesystem found for scheme gs
	at org.apache.beam.sdk.io.FileSystems.getFileSystemInternal(FileSystems.java:558)
	at org.apache.beam.sdk.io.FileSystems.matchNewResource(FileSystems.java:665)
	at org.apache.beam.sdk.io.FileSystems.matchNewDirectory(FileSystems.java:678)
	at org.apache.beam.runners.dataflow.TestDataflowRunner.fromOptions(TestDataflowRunner.java:78)
	at org.apache.beam.runners.dataflow.TestDataflowRunnerTest.testFromOptionsUsesSharedStagingLocationUnderTempRoot(TestDataflowRunnerTest.java:108)

but running the test class passed. Likely a pre-existing issue though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

taking a look....

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's because when doing this only TestDataflowRunner.fromOptions(options) is called and never Pipeline.create(options). Pipeline.create(options) registers the filesystems as a side effect.

Fixed

public void testFromOptionsUsesSharedStagingLocationUnderTempRoot() {
options.setJobName("test-job-1");
TestDataflowRunner.fromOptions(options);
assertEquals("gs://test/test-job-1/output/results", options.getTempLocation());
assertEquals("gs://test/test-job-1/output/results", options.getGcpTempLocation());
assertEquals("gs://test/staging/", options.getStagingLocation());
}

@Test
public void testFromOptionsPreservesExplicitStagingLocation() {
options.setJobName("test-job-2");
options.setStagingLocation("gs://custom-bucket/custom-staging/");
TestDataflowRunner.fromOptions(options);
assertEquals("gs://test/test-job-2/output/results", options.getTempLocation());
assertEquals("gs://custom-bucket/custom-staging/", options.getStagingLocation());
}

@Test
public void testRunBatchJobThatSucceeds() throws Exception {
Pipeline p = Pipeline.create(options);
Expand Down
Loading