Skip to content

Commit 48633e6

Browse files
authored
feat: Automated backups are supported in the admin client (#2472)
* add automated backup policy model * add automated backup policy for create table * add updated automated backup requests * add backup policy object to table class and retrieval methods * changes to table.java * revert doc change * add unit test for policy setting on a table * add new test for creating automated backup * remove misc vscode settings file * undo formatting changes in authorized views * revert formatting * continue revert * redo formatting * remove authorized views formatting * add unit tests * fix syntax on unit test * fix unit test * fix formatting
1 parent 45617ee commit 48633e6

File tree

6 files changed

+270
-9
lines changed

6 files changed

+270
-9
lines changed

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ public CreateTableRequest addFamily(
100100
return this;
101101
}
102102

103-
/** Adds split at the specified key to the configuration */
103+
/** Adds split at the specified key to the configuration. */
104104
public CreateTableRequest addSplit(ByteString key) {
105105
Preconditions.checkNotNull(key);
106106
requestBuilder.addInitialSplitsBuilder().setKey(key);
107107
return this;
108108
}
109109

110-
/** Add change stream retention period between 1 day and 7 days */
110+
/** Add change stream retention period between 1 day and 7 days. */
111111
public CreateTableRequest addChangeStreamRetention(Duration retention) {
112112
Preconditions.checkNotNull(retention);
113113
requestBuilder
@@ -129,6 +129,26 @@ public CreateTableRequest setDeletionProtection(boolean deletionProtection) {
129129
return this;
130130
}
131131

132+
/** Set an automated backup policy for the table. */
133+
public CreateTableRequest setAutomatedBackup(Duration retentionPeriod, Duration frequency) {
134+
com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy policy =
135+
com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy.newBuilder()
136+
.setRetentionPeriod(
137+
com.google.protobuf.Duration.newBuilder()
138+
.setSeconds(retentionPeriod.getSeconds())
139+
.setNanos(retentionPeriod.getNano())
140+
.build())
141+
.setFrequency(
142+
com.google.protobuf.Duration.newBuilder()
143+
.setSeconds(frequency.getSeconds())
144+
.setNanos(frequency.getNano())
145+
.build())
146+
.build();
147+
148+
requestBuilder.getTableBuilder().setAutomatedBackupPolicy(policy);
149+
return this;
150+
}
151+
132152
@Override
133153
public boolean equals(Object o) {
134154
if (this == o) {

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package com.google.cloud.bigtable.admin.v2.models;
1718

1819
import com.google.api.core.InternalApi;
@@ -58,7 +59,7 @@ public enum ReplicationState {
5859

5960
/**
6061
* The table is fully created and ready for use after a restore, and is being optimized for
61-
* performance. When optimizations are complete, the table will transition to `READY` state.
62+
* performance. When optimizations are complete, the table will transition to`READY` state.
6263
*/
6364
READY_OPTIMIZING(
6465
com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.READY_OPTIMIZING),
@@ -99,13 +100,47 @@ public com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState toProto(
99100
}
100101
}
101102

103+
public static class AutomatedBackupPolicy {
104+
private final com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy proto;
105+
106+
/**
107+
* Wraps the protobuf. This method is considered an internal implementation detail and not meant
108+
* to be used by applications.
109+
*/
110+
@InternalApi
111+
public static AutomatedBackupPolicy fromProto(
112+
com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy proto) {
113+
return new AutomatedBackupPolicy(proto);
114+
}
115+
116+
AutomatedBackupPolicy(@Nonnull com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy proto) {
117+
this.proto = proto;
118+
}
119+
120+
/**
121+
* Creates the request protobuf. This method is considered an internal implementation detail and
122+
* not meant to be used by applications.
123+
*/
124+
@InternalApi
125+
public com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy toProto() {
126+
return proto;
127+
}
128+
129+
/** Returns policy config contents as a string. */
130+
public String viewConfig() {
131+
AutomatedBackupPolicy config = fromProto(proto);
132+
return config.proto.getAllFields().toString();
133+
}
134+
}
135+
102136
private final String id;
103137
private final String instanceId;
104138
private final Map<String, ReplicationState> replicationStatesByClusterId;
105139
private final List<ColumnFamily> columnFamilies;
106140

107141
private final Duration changeStreamRetention;
108142
private final boolean deletionProtection;
143+
private static AutomatedBackupPolicy automatedBackupPolicy;
109144

110145
@InternalApi
111146
public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto) {
@@ -132,26 +167,35 @@ public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto)
132167
proto.getChangeStreamConfig().getRetentionPeriod().getNanos());
133168
}
134169

170+
if (proto.hasAutomatedBackupPolicy()) {
171+
automatedBackupPolicy = AutomatedBackupPolicy.fromProto(proto.getAutomatedBackupPolicy());
172+
} else {
173+
automatedBackupPolicy = null;
174+
}
175+
135176
return new Table(
136177
TableName.parse(proto.getName()),
137178
replicationStates.build(),
138179
columnFamilies.build(),
139180
changeStreamConfig,
140-
proto.getDeletionProtection());
181+
proto.getDeletionProtection(),
182+
automatedBackupPolicy);
141183
}
142184

143185
private Table(
144186
TableName tableName,
145187
Map<String, ReplicationState> replicationStatesByClusterId,
146188
List<ColumnFamily> columnFamilies,
147189
Duration changeStreamRetention,
148-
boolean deletionProtection) {
190+
boolean deletionProtection,
191+
AutomatedBackupPolicy automatedBackupPolicy) {
149192
this.instanceId = tableName.getInstance();
150193
this.id = tableName.getTable();
151194
this.replicationStatesByClusterId = replicationStatesByClusterId;
152195
this.columnFamilies = columnFamilies;
153196
this.changeStreamRetention = changeStreamRetention;
154197
this.deletionProtection = deletionProtection;
198+
Table.automatedBackupPolicy = automatedBackupPolicy;
155199
}
156200

157201
/** Gets the table's id. */
@@ -181,6 +225,16 @@ public boolean isDeletionProtected() {
181225
return deletionProtection;
182226
}
183227

228+
/** Returns whether this table has automated backups enabled. */
229+
public boolean isAutomatedBackupEnabled() {
230+
return automatedBackupPolicy == null ? false : true;
231+
}
232+
233+
/** Returns the automated backup policy config. */
234+
public AutomatedBackupPolicy getAutomatedBackupPolicy() {
235+
return automatedBackupPolicy;
236+
}
237+
184238
@Override
185239
public boolean equals(Object o) {
186240
if (this == o) {
@@ -195,7 +249,8 @@ public boolean equals(Object o) {
195249
&& Objects.equal(replicationStatesByClusterId, table.replicationStatesByClusterId)
196250
&& Objects.equal(columnFamilies, table.columnFamilies)
197251
&& Objects.equal(changeStreamRetention, table.changeStreamRetention)
198-
&& Objects.equal(deletionProtection, table.deletionProtection);
252+
&& Objects.equal(deletionProtection, table.deletionProtection)
253+
&& Objects.equal(automatedBackupPolicy, Table.automatedBackupPolicy);
199254
}
200255

201256
@Override
@@ -206,6 +261,7 @@ public int hashCode() {
206261
replicationStatesByClusterId,
207262
columnFamilies,
208263
changeStreamRetention,
209-
deletionProtection);
264+
deletionProtection,
265+
automatedBackupPolicy);
210266
}
211267
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateTableRequest.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class UpdateTableRequest {
3939
private final com.google.bigtable.admin.v2.UpdateTableRequest.Builder requestBuilder =
4040
com.google.bigtable.admin.v2.UpdateTableRequest.newBuilder();
4141

42+
private final com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy.Builder
43+
automatedPolicyBuilder =
44+
com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy.newBuilder();
45+
4246
public static UpdateTableRequest of(String tableId) {
4347
return new UpdateTableRequest(tableId);
4448
}
@@ -69,7 +73,7 @@ public UpdateTableRequest addChangeStreamRetention(Duration retention) {
6973
return this;
7074
}
7175

72-
/** Disable change stream for table */
76+
/** Disable change stream for table. */
7377
public UpdateTableRequest disableChangeStreamRetention() {
7478
return addChangeStreamRetention(Duration.ZERO);
7579
}
@@ -81,6 +85,66 @@ public UpdateTableRequest setDeletionProtection(boolean deletionProtection) {
8185
return this;
8286
}
8387

88+
/** Disables table automated backup policy. */
89+
public UpdateTableRequest disableAutomatedBackup() {
90+
requestBuilder.getTableBuilder().setAutomatedBackupPolicy(automatedPolicyBuilder.build());
91+
requestBuilder.getUpdateMaskBuilder().addPaths("automated_backup_policy");
92+
return this;
93+
}
94+
95+
/** Set an automated backup policy for the table. */
96+
public UpdateTableRequest setAutomatedBackup(Duration retentionPeriod, Duration frequency) {
97+
com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy policy =
98+
com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy.newBuilder()
99+
.setRetentionPeriod(
100+
com.google.protobuf.Duration.newBuilder()
101+
.setSeconds(retentionPeriod.getSeconds())
102+
.setNanos(retentionPeriod.getNano())
103+
.build())
104+
.setFrequency(
105+
com.google.protobuf.Duration.newBuilder()
106+
.setSeconds(frequency.getSeconds())
107+
.setNanos(frequency.getNano())
108+
.build())
109+
.build();
110+
111+
requestBuilder.getTableBuilder().setAutomatedBackupPolicy(policy);
112+
requestBuilder.getUpdateMaskBuilder().addPaths("automated_backup_policy");
113+
return this;
114+
}
115+
116+
/** Updates table automated backup policy retention period. */
117+
public UpdateTableRequest setAutomatedBackupRetentionPeriod(Duration retention) {
118+
requestBuilder
119+
.getTableBuilder()
120+
.setAutomatedBackupPolicy(
121+
automatedPolicyBuilder
122+
.setRetentionPeriod(
123+
com.google.protobuf.Duration.newBuilder()
124+
.setSeconds(retention.getSeconds())
125+
.setNanos(retention.getNano())
126+
.build())
127+
.build());
128+
requestBuilder.getUpdateMaskBuilder().addPaths("automated_backup_policy.retention_period");
129+
return this;
130+
}
131+
132+
/** Updates table automated backup policy frequency. */
133+
public UpdateTableRequest setAutomatedBackupFrequency(Duration frequency) {
134+
requestBuilder
135+
.getTableBuilder()
136+
.setAutomatedBackupPolicy(
137+
automatedPolicyBuilder
138+
.setFrequency(
139+
com.google.protobuf.Duration.newBuilder()
140+
.setSeconds(frequency.getSeconds())
141+
.setNanos(frequency.getNano())
142+
.build())
143+
.build());
144+
requestBuilder.getUpdateMaskBuilder().addPaths("automated_backup_policy.frequency");
145+
return this;
146+
}
147+
84148
@InternalApi
85149
public com.google.bigtable.admin.v2.UpdateTableRequest toProto(
86150
String projectId, String instanceId) {

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequestTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public void testToProto() {
4949
.addSplit(splitKey)
5050
.addSplit(secondSplitKey)
5151
.addChangeStreamRetention(Duration.ofHours(24))
52-
.setDeletionProtection(true);
52+
.setDeletionProtection(true)
53+
.setAutomatedBackup(Duration.ofHours(24), Duration.ofHours(24));
5354

5455
com.google.bigtable.admin.v2.CreateTableRequest requestProto =
5556
com.google.bigtable.admin.v2.CreateTableRequest.newBuilder()
@@ -72,6 +73,17 @@ public void testToProto() {
7273
.setRetentionPeriod(
7374
com.google.protobuf.Duration.newBuilder().setSeconds(86400))
7475
.build())
76+
.setAutomatedBackupPolicy(
77+
com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy.newBuilder()
78+
.setRetentionPeriod(
79+
com.google.protobuf.Duration.newBuilder()
80+
.setSeconds(86400)
81+
.setNanos(0))
82+
.setFrequency(
83+
com.google.protobuf.Duration.newBuilder()
84+
.setSeconds(86400)
85+
.setNanos(0))
86+
.build())
7587
.setDeletionProtection(true))
7688
.setParent(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID))
7789
.addInitialSplits(
@@ -136,20 +148,23 @@ public void testEquality() {
136148
CreateTableRequest.of(TABLE_ID)
137149
.addFamily("family-id")
138150
.addFamily("another-family", GCRULES.maxAge(100, TimeUnit.HOURS))
151+
.setAutomatedBackup(Duration.ofHours(100), Duration.ofHours(100))
139152
.addSplit(splitKey);
140153

141154
assertThat(request)
142155
.isEqualTo(
143156
CreateTableRequest.of(TABLE_ID)
144157
.addFamily("family-id")
145158
.addFamily("another-family", GCRULES.maxAge(Duration.ofHours(100)))
159+
.setAutomatedBackup(Duration.ofHours(100), Duration.ofHours(100))
146160
.addSplit(splitKey));
147161

148162
assertThat(request)
149163
.isNotEqualTo(
150164
CreateTableRequest.of(TABLE_ID)
151165
.addFamily("family-id")
152166
.addFamily("another-family")
167+
.setAutomatedBackup(Duration.ofHours(100), Duration.ofHours(10))
153168
.addSplit(splitKey));
154169
}
155170

google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.cloud.bigtable.admin.v2.models;
1717

1818
import static com.google.common.truth.Truth.assertThat;
19+
import static org.junit.Assert.assertEquals;
1920

2021
import com.google.bigtable.admin.v2.ColumnFamily;
2122
import com.google.bigtable.admin.v2.GcRule;
@@ -67,6 +68,13 @@ public void testFromProto() {
6768
.setSeconds(1)
6869
.setNanos(99)))
6970
.build())
71+
.setAutomatedBackupPolicy(
72+
com.google.bigtable.admin.v2.Table.AutomatedBackupPolicy.newBuilder()
73+
.setRetentionPeriod(
74+
com.google.protobuf.Duration.newBuilder().setSeconds(1).setNanos(99))
75+
.setFrequency(
76+
com.google.protobuf.Duration.newBuilder().setSeconds(1).setNanos(99))
77+
.build())
7078
.setDeletionProtection(true)
7179
.build();
7280

@@ -79,6 +87,18 @@ public void testFromProto() {
7987
"cluster1", Table.ReplicationState.READY,
8088
"cluster2", Table.ReplicationState.INITIALIZING);
8189
assertThat(result.getColumnFamilies()).hasSize(3);
90+
assertThat(result.isAutomatedBackupEnabled()).isTrue();
91+
assertEquals(
92+
result.getAutomatedBackupPolicy().viewConfig(),
93+
"{google.bigtable.admin.v2.Table.AutomatedBackupPolicy.retention_period=seconds: 1\n"
94+
+ //
95+
"nanos: 99\n"
96+
+ //
97+
", google.bigtable.admin.v2.Table.AutomatedBackupPolicy.frequency=seconds: 1\n"
98+
+ //
99+
"nanos: 99\n"
100+
+ //
101+
"}");
82102
assertThat(result.isDeletionProtected()).isTrue();
83103

84104
for (Entry<String, ColumnFamily> entry : proto.getColumnFamiliesMap().entrySet()) {

0 commit comments

Comments
 (0)