Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
## Improvements

[comment]: # "generic improvements if not covered in following sections."
* (Java) Dataflow runner now warns when the staging location has an age-based lifecycle deletion rule that applies to staged files ([#18209](https://github.com/apache/beam/issues/18209)).

### New Feature

Expand Down
1 change: 1 addition & 0 deletions runners/google-cloud-dataflow-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ dependencies {
permitUnusedDeclared library.java.google_http_client_gson // BEAM-11761
implementation library.java.google_cloud_logging
permitUnusedDeclared library.java.google_cloud_logging // BEAM-11761
implementation library.java.google_cloud_storage
implementation library.java.opentelemetry_context
implementation library.java.opentelemetry_exporter_otlp
permitUnusedDeclared library.java.opentelemetry_exporter_otlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.google.api.services.dataflow.model.WorkerPool;
import com.google.auto.service.AutoService;
import com.google.auto.value.AutoValue;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo.LifecycleRule;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -81,6 +83,7 @@
import org.apache.beam.sdk.coders.VoidCoder;
import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
import org.apache.beam.sdk.extensions.gcp.storage.PathValidator;
import org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath;
import org.apache.beam.sdk.io.BoundedSource;
import org.apache.beam.sdk.io.FileSystems;
import org.apache.beam.sdk.io.Read;
Expand Down Expand Up @@ -511,6 +514,64 @@ && isServiceEndpoint(dataflowOptions.getDataflowEndpoint())) {
return new DataflowRunner(dataflowOptions);
}

@VisibleForTesting
static void warnIfStagingBucketHasTtl(DataflowPipelineOptions options) {
String stagingLocation = options.getStagingLocation();
if (isNullOrEmpty(stagingLocation) || !stagingLocation.startsWith("gs://")) {
return;
}
String bucketName = stagingLocation.substring("gs://".length()).split("/", 2)[0];
String stagingPrefix = stagingPrefix(stagingLocation, bucketName);
try {
Bucket bucket =
options.getGcsUtil().getBucketWithOptions(GcsPath.fromComponents(bucketName, ""));
for (LifecycleRule rule : bucket.getLifecycleRules()) {
LifecycleRule.LifecycleAction action = rule.getAction();
LifecycleRule.LifecycleCondition condition = rule.getCondition();
if (action == null
|| condition == null
|| !"Delete".equals(action.getActionType())
|| condition.getAge() == null
|| !matchesStagingPrefix(condition.getMatchesPrefix(), stagingPrefix)) {
continue;
}
LOG.warn(
"The staging location {} is in bucket {}, which has a lifecycle rule that deletes "
+ "objects after {} days. Long-running and streaming pipelines may fail because "
+ "staged files can be deleted while they are still needed.",
stagingLocation,
bucketName,
condition.getAge());
return;
}
} catch (IOException | RuntimeException e) {
LOG.debug("Unable to check lifecycle rules of staging bucket {}", bucketName, e);
}
}

private static String stagingPrefix(String stagingLocation, String bucketName) {
String prefix = stagingLocation.substring(("gs://" + bucketName).length());
if (prefix.startsWith("/")) {
prefix = prefix.substring(1);
}
return prefix.isEmpty() || prefix.endsWith("/") ? prefix : prefix + "/";
}

private static boolean matchesStagingPrefix(List<String> rulePrefixes, String stagingPrefix) {
if (rulePrefixes == null || rulePrefixes.isEmpty()) {
return true;
}
for (String rulePrefix : rulePrefixes) {
if (rulePrefix == null
|| rulePrefix.isEmpty()
|| stagingPrefix.startsWith(rulePrefix)
|| rulePrefix.startsWith(stagingPrefix)) {
return true;
}
}
return false;
}

static boolean isServiceEndpoint(String endpoint) {
return Strings.isNullOrEmpty(endpoint) || Pattern.matches(ENDPOINT_REGEXP, endpoint);
}
Expand Down Expand Up @@ -1372,6 +1433,7 @@ public DataflowPipelineJob run(Pipeline pipeline) {
// Note that `stageArtifacts` has to be called before `resolveArtifact` because
// `resolveArtifact` updates local paths to staged paths in pipeline proto.
portablePipelineProto = resolveAnyOfEnvironments(portablePipelineProto);
warnIfStagingBucketHasTtl(dataflowOptions);
List<DataflowPackage> packages = stageArtifacts(portablePipelineProto);
portablePipelineProto = resolveArtifacts(portablePipelineProto);
portablePipelineProto = applySdkEnvironmentOverrides(portablePipelineProto, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.JsonGenerator;
Expand All @@ -72,6 +76,9 @@
import com.google.api.services.dataflow.model.SdkHarnessContainerImage;
import com.google.api.services.storage.model.StorageObject;
import com.google.auto.service.AutoService;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.BucketInfo.LifecycleRule;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -367,6 +374,23 @@ static GcsUtil buildMockGcsUtil() throws IOException {
return mockGcsUtil;
}

private static Bucket bucketWithLifecycleRule(
String actionType, Integer age, String... prefixes) {
BucketInfo.LifecycleRule.LifecycleAction action =
mock(BucketInfo.LifecycleRule.LifecycleAction.class);
when(action.getActionType()).thenReturn(actionType);
BucketInfo.LifecycleRule.LifecycleCondition condition =
mock(BucketInfo.LifecycleRule.LifecycleCondition.class);
when(condition.getAge()).thenReturn(age);
when(condition.getMatchesPrefix()).thenReturn(Arrays.asList(prefixes));
LifecycleRule rule = mock(LifecycleRule.class);
when(rule.getAction()).thenReturn(action);
when(rule.getCondition()).thenReturn(condition);
Bucket bucket = mock(Bucket.class);
doReturn(ImmutableList.of(rule)).when(bucket).getLifecycleRules();
return bucket;
}

private DataflowPipelineOptions buildPipelineOptions() throws IOException {
return buildPipelineOptions("JOB_STATE_RUNNING");
}
Expand Down Expand Up @@ -1042,6 +1066,126 @@ public void testGcsStagingLocationInitialization() {
assertNotNull(options.getStagingLocation());
}

@Test
public void testWarnIfStagingBucketHasTtl() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
Bucket bucket = bucketWithLifecycleRule("Delete", 7);
when(mockGcsUtil.getBucketWithOptions(GcsPath.fromComponents(VALID_BUCKET, "")))
.thenReturn(bucket);

DataflowRunner.warnIfStagingBucketHasTtl(options);

expectedLogs.verifyWarn("deletes objects after 7 days");
}

@Test
public void testWarnIfStagingBucketTtlAppliesToStagingPrefix() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
options.setStagingLocation("gs://valid-bucket/staging/");
Bucket bucket = bucketWithLifecycleRule("Delete", 7, "staging/");
when(mockGcsUtil.getBucketWithOptions(GcsPath.fromComponents(VALID_BUCKET, "")))
.thenReturn(bucket);

DataflowRunner.warnIfStagingBucketHasTtl(options);

expectedLogs.verifyWarn("deletes objects after 7 days");
}

@Test
public void testNoWarningIfStagingBucketTtlDoesNotApplyToStagingPrefix() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
options.setStagingLocation("gs://valid-bucket/staging/");
Bucket bucket = bucketWithLifecycleRule("Delete", 7, "other/");
when(mockGcsUtil.getBucketWithOptions(GcsPath.fromComponents(VALID_BUCKET, "")))
.thenReturn(bucket);

DataflowRunner.warnIfStagingBucketHasTtl(options);

expectedLogs.verifyNotLogged("deletes objects after");
}

@Test
public void testNoWarningForSimilarStagingPrefix() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
options.setStagingLocation("gs://valid-bucket/staging");
Bucket bucket = bucketWithLifecycleRule("Delete", 7, "staging-other/");
when(mockGcsUtil.getBucketWithOptions(GcsPath.fromComponents(VALID_BUCKET, "")))
.thenReturn(bucket);

DataflowRunner.warnIfStagingBucketHasTtl(options);

expectedLogs.verifyNotLogged("deletes objects after");
}

@Test
public void testNoWarningIfStagingBucketHasNoLifecycleRules() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
Bucket bucket = mock(Bucket.class);
doReturn(ImmutableList.of()).when(bucket).getLifecycleRules();
when(mockGcsUtil.getBucketWithOptions(GcsPath.fromComponents(VALID_BUCKET, "")))
.thenReturn(bucket);

DataflowRunner.warnIfStagingBucketHasTtl(options);

expectedLogs.verifyNotLogged("deletes objects after");
}

@Test
public void testRunnerConstructionDoesNotCheckStagingBucketLifecycle() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();

DataflowRunner.fromOptions(options);

verify(mockGcsUtil, never()).getBucketWithOptions(any(GcsPath.class));
}

@Test
public void testNoWarningIfStagingBucketLifecycleRuleHasNoAge() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
Bucket bucket = bucketWithLifecycleRule("Delete", null);
when(mockGcsUtil.getBucketWithOptions(GcsPath.fromComponents(VALID_BUCKET, "")))
.thenReturn(bucket);

DataflowRunner.warnIfStagingBucketHasTtl(options);

expectedLogs.verifyNotLogged("deletes objects after");
}

@Test
public void testNoWarningIfStagingBucketLifecycleRuleIsNotDelete() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
Bucket bucket = bucketWithLifecycleRule("SetStorageClass", 7);
when(mockGcsUtil.getBucketWithOptions(GcsPath.fromComponents(VALID_BUCKET, "")))
.thenReturn(bucket);

DataflowRunner.warnIfStagingBucketHasTtl(options);

expectedLogs.verifyNotLogged("deletes objects after");
}

@Test
public void testStagingBucketLifecycleCheckFailureDoesNotFailSubmission() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
when(mockGcsUtil.getBucketWithOptions(GcsPath.fromComponents(VALID_BUCKET, "")))
.thenThrow(new IOException("access denied"));

DataflowRunner.warnIfStagingBucketHasTtl(options);

expectedLogs.verifyDebug("Unable to check lifecycle rules of staging bucket");
}

@Test
public void testLocalStagingLocationSkipsLifecycleCheck() throws Exception {
DataflowPipelineOptions options = buildPipelineOptions();
GcsUtil gcsUtil = mock(GcsUtil.class);
options.setGcsUtil(gcsUtil);
options.setStagingLocation("/tmp/staging");

DataflowRunner.warnIfStagingBucketHasTtl(options);

verifyNoInteractions(gcsUtil);
}

@Test
public void testInvalidGcpTempLocation() throws IOException {
DataflowPipelineOptions options = buildPipelineOptions();
Expand Down
Loading