Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit c1c7b87

Browse files
feat: Support Idempotency in MutateRow API
1 parent b5acca6 commit c1c7b87

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/models/RowMutation.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.api.core.BetaApi;
1919
import com.google.api.core.InternalApi;
20+
import com.google.bigtable.v2.Idempotency;
2021
import com.google.bigtable.v2.MutateRowRequest;
2122
import com.google.bigtable.v2.MutateRowsRequest;
2223
import com.google.bigtable.v2.MutateRowsRequest.Entry;
@@ -38,6 +39,7 @@ public final class RowMutation implements MutationApi<RowMutation>, Serializable
3839
private final TargetId targetId;
3940
private final ByteString key;
4041
private final Mutation mutation;
42+
private ByteString idempotency_token;
4143

4244
private RowMutation(TargetId targetId, ByteString key, Mutation mutation) {
4345
Preconditions.checkNotNull(targetId, "target id can't be null.");
@@ -255,6 +257,11 @@ public RowMutation mergeToCell(
255257
return this;
256258
}
257259

260+
public RowMutation setIdempotency(@Nonnull ByteString token) {
261+
this.idempotency_token = token;
262+
return this;
263+
}
264+
258265
@InternalApi
259266
public MutateRowRequest toProto(RequestContext requestContext) {
260267
MutateRowRequest.Builder builder = MutateRowRequest.newBuilder();
@@ -266,6 +273,12 @@ public MutateRowRequest toProto(RequestContext requestContext) {
266273
builder.setTableName(resourceName);
267274
}
268275

276+
if (idempotency_token != null) {
277+
Idempotency.Builder idempotencyBuilder = Idempotency.newBuilder();
278+
idempotencyBuilder.setToken(idempotency_token);
279+
builder.setIdempotency(idempotencyBuilder);
280+
}
281+
269282
return builder
270283
.setAppProfileId(requestContext.getAppProfileId())
271284
.setRowKey(key)

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/MutateRowIT.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,44 @@ public void testOnAuthorizedView() throws Exception {
152152
.getTableAdminClient()
153153
.deleteAuthorizedView(testEnvRule.env().getTableId(), testAuthorizedView.getId());
154154
}
155+
156+
@Test
157+
public void testIdempotentAggregates() throws Exception {
158+
String rowKey = UUID.randomUUID().toString();
159+
String familyId = testEnvRule.env().getFamilyId();
160+
161+
// First mutation w/ idempotency token.
162+
testEnvRule
163+
.env()
164+
.getDataClient()
165+
.mutateRowAsync(
166+
RowMutation.create(testEnvRule.env().getTableId(), rowKey)
167+
.addToCell(familyId, "cq", 0, 10)
168+
.setIdempotency(ByteString.copyFromUtf8("abcdefgh"))
169+
)
170+
.get(1, TimeUnit.MINUTES);
171+
172+
// Now replay the mutation.
173+
testEnvRule
174+
.env()
175+
.getDataClient()
176+
.mutateRowAsync(
177+
RowMutation.create(testEnvRule.env().getTableId(), rowKey)
178+
.addToCell(familyId, "cq", 0, 10)
179+
.setIdempotency(ByteString.copyFromUtf8("abcdefgh"))
180+
)
181+
.get(1, TimeUnit.MINUTES);
182+
183+
Row row =
184+
testEnvRule
185+
.env()
186+
.getDataClient()
187+
.readRowsCallable()
188+
.first()
189+
.call(Query.create(testEnvRule.env().getTableId()).rowKey(rowKey));
190+
191+
assertThat(row.getCells()).hasSize(1);
192+
// Ensure that the increment is only applied once.
193+
assertThat(row.getCells().get(0).getValue()).isEqualTo(10);Add commentMore actions
194+
}
155195
}

0 commit comments

Comments
 (0)