Skip to content

Commit

Permalink
Adding failure_mode parameter to Spanner Python transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloem committed Nov 24, 2023
1 parent b6e19eb commit d210570
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public static class Configuration extends CrossLanguageConfiguration {
private @Nullable Integer groupingFactor;
private @Nullable Duration commitDeadline;
private @Nullable Duration maxCumulativeBackoff;
private @Nullable String failureMode;

public void setTable(String table) {
this.table = table;
Expand Down Expand Up @@ -322,6 +323,10 @@ public void setMaxCumulativeBackoff(@Nullable Long maxCumulativeBackoff) {
this.maxCumulativeBackoff = Duration.standardSeconds(maxCumulativeBackoff);
}
}

public void setFailureMode(@Nullable String failureMode) {
this.failureMode = failureMode;
}
}

@Override
Expand Down Expand Up @@ -361,6 +366,11 @@ public PTransform<PCollection<Row>, PDone> buildExternal(
writeTransform =
writeTransform.withMaxCumulativeBackoff(configuration.maxCumulativeBackoff);
}
if (configuration.failureMode != null) {
writeTransform =
writeTransform.withFailureMode(
SpannerIO.FailureMode.valueOf(configuration.failureMode));
}
return SpannerIO.WriteRows.of(writeTransform, operation, configuration.table);
}
}
Expand Down
16 changes: 16 additions & 0 deletions sdks/python/apache_beam/io/gcp/spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ class TimestampBoundMode(Enum):
STRONG = auto()


class FailureMode(Enum):
FAIL_FAST = auto()
REPORT_FAILURES = auto()


class ReadFromSpannerSchema(NamedTuple):
instance_id: str
database_id: str
Expand Down Expand Up @@ -282,6 +287,7 @@ class WriteToSpannerSchema(NamedTuple):
emulator_host: Optional[str]
commit_deadline: Optional[int]
max_cumulative_backoff: Optional[int]
failure_mode: Optional[str]


_CLASS_DOC = \
Expand Down Expand Up @@ -392,6 +398,7 @@ def __init__(
emulator_host=None,
commit_deadline=None,
max_cumulative_backoff=None,
failure_mode=None,
expansion_service=None,
):
max_cumulative_backoff = int(
Expand All @@ -413,6 +420,7 @@ def __init__(
emulator_host=emulator_host,
commit_deadline=commit_deadline,
max_cumulative_backoff=max_cumulative_backoff,
failure_mode=_get_enum_name(failure_mode),
),
),
expansion_service=expansion_service or default_io_expansion_service(),
Expand Down Expand Up @@ -445,6 +453,7 @@ def __init__(
commit_deadline=None,
max_cumulative_backoff=None,
expansion_service=None,
failure_mode=None,
):
max_cumulative_backoff = int(
max_cumulative_backoff) if max_cumulative_backoff else None
Expand All @@ -465,6 +474,7 @@ def __init__(
emulator_host=emulator_host,
commit_deadline=commit_deadline,
max_cumulative_backoff=max_cumulative_backoff,
failure_mode=_get_enum_name(failure_mode),
),
),
expansion_service=expansion_service or default_io_expansion_service(),
Expand Down Expand Up @@ -497,6 +507,7 @@ def __init__(
commit_deadline=None,
max_cumulative_backoff=None,
expansion_service=None,
failure_mode=None,
):
max_cumulative_backoff = int(
max_cumulative_backoff) if max_cumulative_backoff else None
Expand All @@ -517,6 +528,7 @@ def __init__(
emulator_host=emulator_host,
commit_deadline=commit_deadline,
max_cumulative_backoff=max_cumulative_backoff,
failure_mode=_get_enum_name(failure_mode),
),
),
expansion_service=expansion_service or default_io_expansion_service(),
Expand Down Expand Up @@ -548,6 +560,7 @@ def __init__(
emulator_host=None,
commit_deadline=None,
max_cumulative_backoff=None,
failure_mode=None,
expansion_service=None,
):
max_cumulative_backoff = int(
Expand All @@ -569,6 +582,7 @@ def __init__(
emulator_host=emulator_host,
commit_deadline=commit_deadline,
max_cumulative_backoff=max_cumulative_backoff,
failure_mode=_get_enum_name(failure_mode),
),
),
expansion_service=expansion_service or default_io_expansion_service(),
Expand Down Expand Up @@ -600,6 +614,7 @@ def __init__(
emulator_host=None,
commit_deadline=None,
max_cumulative_backoff=None,
failure_mode=None,
expansion_service=None,
):
max_cumulative_backoff = int(
Expand All @@ -621,6 +636,7 @@ def __init__(
emulator_host=emulator_host,
commit_deadline=commit_deadline,
max_cumulative_backoff=max_cumulative_backoff,
failure_mode=_get_enum_name(failure_mode),
),
),
expansion_service=expansion_service or default_io_expansion_service(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def run_write_pipeline(
database_id=self.database_id,
project_id=self.project_id,
table=self.table,
failure_mode=beam.io.gcp.spanner.FailureMode.REPORT_FAILURES,
emulator_host=self.spanner_helper.get_emulator_host(),
))

Expand Down

0 comments on commit d210570

Please sign in to comment.