/
ITCommitTimestampTest.java
357 lines (328 loc) · 13 KB
/
ITCommitTimestampTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner.it;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Database;
import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.IntegrationTestEnv;
import com.google.cloud.spanner.Key;
import com.google.cloud.spanner.KeySet;
import com.google.cloud.spanner.Mutation;
import com.google.cloud.spanner.ParallelIntegrationTest;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.TimestampBound;
import com.google.cloud.spanner.Value;
import com.google.cloud.spanner.connection.ConnectionOptions;
import com.google.cloud.spanner.testing.RemoteSpannerHelper;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;
import org.threeten.bp.Instant;
/** Integration test for commit timestamp of Cloud Spanner. */
@Category(ParallelIntegrationTest.class)
@RunWith(JUnit4.class)
public class ITCommitTimestampTest {
@ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv();
private static Database db;
private static DatabaseClient client;
private static DatabaseAdminClient dbAdminClient;
private static RemoteSpannerHelper testHelper;
private static String instanceId;
private static String databaseId;
@BeforeClass
public static void setUp() {
testHelper = env.getTestHelper();
db =
testHelper.createTestDatabase(
"CREATE TABLE T ("
+ "K STRING(MAX) NOT NULL,"
+ "T1 TIMESTAMP OPTIONS (allow_commit_timestamp = true),"
+ "T2 TIMESTAMP OPTIONS (allow_commit_timestamp = true),"
+ "T3 TIMESTAMP,"
+ ") PRIMARY KEY (K)");
client = testHelper.getDatabaseClient(db);
dbAdminClient = testHelper.getClient().getDatabaseAdminClient();
instanceId = testHelper.getInstanceId().getInstance();
databaseId = db.getId().getDatabase();
}
@AfterClass
public static void teardown() {
ConnectionOptions.closeSpanner();
}
@After
public void deleteAllTestRecords() {
client.write(ImmutableList.of(Mutation.delete("T", KeySet.all())));
}
private Timestamp write(Mutation m) {
return client.write(Collections.singletonList(m));
}
private Struct readRow(DatabaseClient client, String table, Key key, String... columns) {
return client.singleUse(TimestampBound.strong()).readRow(table, key, Arrays.asList(columns));
}
@Test
public void writeCommitTimestamp() {
// 1. timestamps auto populated and returned should be the same
Timestamp commitTimestamp =
write(
Mutation.newInsertOrUpdateBuilder("T")
.set("K")
.to("a")
.set("T1")
.to(Value.COMMIT_TIMESTAMP)
.set("T2")
.to(Value.COMMIT_TIMESTAMP)
.build());
Struct row = readRow(client, "T", Key.of("a"), "T1", "T2");
assertThat(row.getTimestamp(0)).isEqualTo(commitTimestamp);
assertThat(row.getTimestamp(1)).isEqualTo(commitTimestamp);
// 2. attempt to write CommitTimestamp to not enabled column should fail
// error_catalog error CommitTimestampOptionNotEnabled
try {
write(
Mutation.newInsertOrUpdateBuilder("T")
.set("K")
.to("a")
.set("T3")
.to(Value.COMMIT_TIMESTAMP)
.build());
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
}
}
@Test
public void consistency() {
// 1. timestamps populated are consistent in order
write(
Mutation.newInsertOrUpdateBuilder("T")
.set("K")
.to("a")
.set("T1")
.to(Value.COMMIT_TIMESTAMP)
.build());
write(
Mutation.newInsertOrUpdateBuilder("T")
.set("K")
.to("b")
.set("T1")
.to(Value.COMMIT_TIMESTAMP)
.build());
Struct row1 = readRow(client, "T", Key.of("a"), "T1");
Struct row2 = readRow(client, "T", Key.of("b"), "T1");
assertThat(row2.getTimestamp(0)).isGreaterThan(row1.getTimestamp(0));
}
@Test
public void schemaChangeTimestampInFuture() throws Exception {
write(
Mutation.newInsertOrUpdateBuilder("T")
.set("K")
.to("a")
.set("T3")
.to(Timestamp.MAX_VALUE)
.build());
// error_catalog error CommitTimestampNotInFuture
String statement = "ALTER TABLE T ALTER COLUMN T3 SET OPTIONS (allow_commit_timestamp=true)";
try {
dbAdminClient
.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null)
.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
}
}
@Test
public void insertTimestampInFuture() {
// error_catalog error TimestampInFuture
try {
write(
Mutation.newInsertOrUpdateBuilder("T")
.set("K")
.to("a")
.set("T1")
.to(Timestamp.MAX_VALUE)
.build());
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
}
}
@Test
public void invalidColumnOption() throws Exception {
// error_catalog error DDLStatementWithError
String statement = "ALTER TABLE T ALTER COLUMN T3 SET OPTIONS (bogus=null)";
try {
dbAdminClient
.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null)
.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
}
}
@Test
public void invalidColumnOptionValue() throws Exception {
// error_catalog error DDLStatementWithErrors
String statement = "ALTER TABLE T ALTER COLUMN T3 SET OPTIONS (allow_commit_timestamp=bogus)";
try {
dbAdminClient
.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null)
.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
}
}
@Test
public void invalidColumnType() throws Exception {
// error_catalog error OptionErrorList
String statement = "ALTER TABLE T ADD COLUMN T4 INT64 OPTIONS (allow_commit_timestamp=true)";
try {
dbAdminClient
.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null)
.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
}
}
private void alterColumnOption(String databaseId, String table, String opt) throws Exception {
String statement =
"ALTER TABLE "
+ table
+ " ALTER COLUMN ts"
+ " SET OPTIONS (allow_commit_timestamp="
+ opt
+ ")";
dbAdminClient
.updateDatabaseDdl(instanceId, databaseId, ImmutableList.of(statement), null)
.get();
}
private void writeAndVerify(DatabaseClient client, Timestamp ts) {
Timestamp commitTimestamp =
client.write(
Arrays.asList(
Mutation.newInsertOrUpdateBuilder("T1").set("ts").to(ts).build(),
Mutation.newInsertOrUpdateBuilder("T2").set("ts").to(ts).build(),
Mutation.newInsertOrUpdateBuilder("T3").set("ts").to(ts).build()));
if (ts == Value.COMMIT_TIMESTAMP) {
ts = commitTimestamp;
}
assertThat(readRow(client, "T1", Key.of(ts), "ts").getTimestamp(0)).isEqualTo(ts);
assertThat(readRow(client, "T2", Key.of(ts), "ts").getTimestamp(0)).isEqualTo(ts);
assertThat(readRow(client, "T3", Key.of(ts), "ts").getTimestamp(0)).isEqualTo(ts);
}
// 1) Write timestamps in the past
// 2) Set all interleaved tables allow_commit_timestamp=true
// 3) Use commit timestamp in all tables
// 4) Set all interleaved tables allow_commit_timestamp=null
// 5) Write timestamps in the future
@Test
public void interleavedTable() throws Exception {
Database db =
testHelper.createTestDatabase(
"CREATE TABLE T1 (ts TIMESTAMP) PRIMARY KEY (ts)",
"CREATE TABLE T2 (ts TIMESTAMP) PRIMARY KEY (ts), INTERLEAVE IN PARENT T1",
"CREATE TABLE T3 (ts TIMESTAMP) PRIMARY KEY (ts), INTERLEAVE IN PARENT T2");
DatabaseClient client = testHelper.getDatabaseClient(db);
String databaseId = db.getId().getDatabase();
Timestamp timeNow = Timestamp.ofTimeMicroseconds(Instant.now().toEpochMilli() * 1000);
Timestamp timeFuture =
Timestamp.ofTimeMicroseconds(
Instant.now().plus(Duration.ofDays(300)).toEpochMilli() * 1000);
writeAndVerify(client, timeNow);
alterColumnOption(databaseId, "T1", "true");
alterColumnOption(databaseId, "T2", "true");
alterColumnOption(databaseId, "T3", "true");
writeAndVerify(client, Value.COMMIT_TIMESTAMP);
alterColumnOption(databaseId, "T1", "null");
alterColumnOption(databaseId, "T2", "null");
alterColumnOption(databaseId, "T3", "null");
writeAndVerify(client, timeFuture);
}
// In interleaved table, use of commit timestamp in child table is not allowed
// if parent tables are not allow_commit_timestamp=true
@Test
public void interleavedTableHierarchy1() {
Database db =
testHelper.createTestDatabase(
"CREATE TABLE T1 (ts TIMESTAMP) PRIMARY KEY (ts)",
"CREATE TABLE T2 (ts TIMESTAMP) PRIMARY KEY (ts), INTERLEAVE IN PARENT T1",
"CREATE TABLE T3 (ts TIMESTAMP OPTIONS (allow_commit_timestamp = true)) "
+ "PRIMARY KEY (ts), INTERLEAVE IN PARENT T2");
DatabaseClient client = testHelper.getDatabaseClient(db);
// error_catalog error CommitTimestampOptionNotEnabled
try {
client.write(
Collections.singletonList(
Mutation.newInsertOrUpdateBuilder("T3")
.set("ts")
.to(Value.COMMIT_TIMESTAMP)
.build()));
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
}
}
// In interleaved table, use of commit timestamp in parent table is not
// allowed if child tables are not allow_commit_timestamp=true
@Test
public void interleavedTableHierarchy2() {
Database db =
testHelper.createTestDatabase(
"CREATE TABLE T1 (ts TIMESTAMP OPTIONS (allow_commit_timestamp = true)) "
+ "PRIMARY KEY (ts)",
"CREATE TABLE T2 (ts TIMESTAMP) PRIMARY KEY (ts), INTERLEAVE IN PARENT T1",
"CREATE TABLE T3 (ts TIMESTAMP OPTIONS (allow_commit_timestamp = true)) "
+ "PRIMARY KEY (ts), INTERLEAVE IN PARENT T2");
DatabaseClient client = testHelper.getDatabaseClient(db);
// error_catalog error CommitTimestampOptionNotEnabled
try {
client.write(
Collections.singletonList(
Mutation.newInsertOrUpdateBuilder("T1")
.set("ts")
.to(Value.COMMIT_TIMESTAMP)
.build()));
fail("missing expected exception");
} catch (SpannerException e) {
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.FAILED_PRECONDITION);
}
}
}