Skip to content

Commit

Permalink
Adding failure_mode parameter to Spanner Python transforms (#29529)
Browse files Browse the repository at this point in the history
* Adding failure_mode parameter to Spanner Python transforms

* fix formatting

* Improve pydoc

* fixup docs
  • Loading branch information
pabloem authored Dec 2, 2023
1 parent 85d4fcf commit 6593a0f
Show file tree
Hide file tree
Showing 3 changed files with 32 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
21 changes: 21 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 @@ -346,6 +352,11 @@ class {row_type}(NamedTuple):
(15min). If the mutations still have not been written after this time,
they are treated as a failure, and handled according to the setting of
failure_mode. Pass seconds as value.
:param failure_mode: Specifies the behavior for mutations that fail to be
written to Spanner. Default is FAIL_FAST. When FAIL_FAST is set,
an exception will be thrown for any failed mutation. When REPORT_FAILURES
is set, processing will continue instead of throwing an exception. Note
that REPORT_FAILURES can cause data loss if used incorrectly.
:param expansion_service: The address (host:port) of the ExpansionService.
"""

Expand Down Expand Up @@ -392,6 +403,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 +425,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 +458,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 +479,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 +512,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 +533,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 +565,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 +587,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 +619,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 +641,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 6593a0f

Please sign in to comment.