Bigtable: clean up consistency token by igorbernstein2 · Pull Request #3570 · googleapis/google-cloud-java · GitHub
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,39 @@
package com.google.cloud.bigtable.admin.v2.models;

import com.google.api.core.InternalApi;
import com.google.api.core.InternalExtensionOnly;
import com.google.auto.value.AutoValue;
import com.google.bigtable.admin.v2.CheckConsistencyRequest;
import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.bigtable.admin.v2.InstanceName;
import com.google.bigtable.admin.v2.TableName;
import com.google.common.base.Preconditions;

/**
* Wrapper for {@link GenerateConsistencyTokenResponse#getConsistencyToken()}
*
* <p>Cannot be created. They are obtained by invoking {@link
* com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient#generateConsistencyToken(String)}
*/
public final class ConsistencyToken {
private final String token;

@InternalApi
public static ConsistencyToken fromProto(GenerateConsistencyTokenResponse proto) {
return new ConsistencyToken(proto.getConsistencyToken());
@InternalExtensionOnly
@AutoValue
public abstract class ConsistencyToken {
public static ConsistencyToken of(TableName tableName, String token) {
return new AutoValue_ConsistencyToken(tableName, token);
}

private ConsistencyToken(String token) {
this.token = token;
}
abstract TableName getTableName();
abstract String getToken();

// TODO(igorbernstein): tableName should be part of the token and be parameterized
@InternalApi
public CheckConsistencyRequest toProto(String tableName) {
public CheckConsistencyRequest toProto(InstanceName instanceName) {
Preconditions.checkArgument(
instanceName.equals(InstanceName.of(getTableName().getProject(), getTableName().getInstance())),
"Consistency tokens are only valid within a single instance.");

return CheckConsistencyRequest.newBuilder()
.setName(tableName)
.setConsistencyToken(token)
.setName(getTableName().toString())
.setConsistencyToken(getToken())
.build();
}

@VisibleForTesting
String getToken() {
return token;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ConsistencyToken that = (ConsistencyToken) o;
return Objects.equal(token, that.token);
}

@Override
public int hashCode() {
return Objects.hashCode(token);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("token", token).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ public void testGenerateConsistencyToken() {
ConsistencyToken actualResult = adminClient.generateConsistencyToken(TABLE_NAME.getTable());

// Verify
assertThat(actualResult).isEqualTo(ConsistencyToken.fromProto(expectedResponse));
assertThat(actualResult).isEqualTo(ConsistencyToken.of(TABLE_NAME, "fakeToken"));
}

@Test
Expand All @@ -519,7 +519,7 @@ public void testGenerateConsistencyTokenAsync() throws Exception {
.generateConsistencyTokenAsync(TABLE_NAME.getTable());

// Verify
assertThat(actualResult.get()).isEqualTo(ConsistencyToken.fromProto(expectedResponse));
assertThat(actualResult.get()).isEqualTo(ConsistencyToken.of(TABLE_NAME, "fakeToken"));
}

@Test
Expand All @@ -538,13 +538,9 @@ public void testCheckConsistencyToken() {
.thenReturn(ApiFutures.immediateFuture(expectedResponse));

// Execute
ConsistencyToken actualToken = ConsistencyToken.fromProto(
GenerateConsistencyTokenResponse.newBuilder()
.setConsistencyToken("fakeToken")
.build()
);
ConsistencyToken actualToken = ConsistencyToken.of(TABLE_NAME, "fakeToken");

boolean actualResult = adminClient.isConsistent(TABLE_NAME.getTable(), actualToken);
boolean actualResult = adminClient.isConsistent(actualToken);

// Verify
assertThat(actualResult).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public void checkConsistency() {
tableAdmin.createTable(CreateTableRequest.of(tableId));
ConsistencyToken consistencyToken = tableAdmin.generateConsistencyToken(tableId);
assertNotNull(consistencyToken);
boolean consistent = tableAdmin.isConsistent(tableId, consistencyToken);
boolean consistent = tableAdmin.isConsistent(consistencyToken);
assertTrue(consistent);
} finally {
tableAdmin.deleteTable(tableId);
Expand Down