forked from googleapis/java-bigtable
/
TestEnvRule.java
352 lines (311 loc) · 11.3 KB
/
TestEnvRule.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
/*
* Copyright 2018 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
*
* https://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.bigtable.test_helpers.env;
import static com.google.common.truth.TruthJUnit.assume;
import com.google.api.core.ApiFuture;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
import com.google.cloud.bigtable.admin.v2.models.AppProfile;
import com.google.cloud.bigtable.admin.v2.models.Cluster;
import com.google.cloud.bigtable.admin.v2.models.Instance;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.threeten.bp.Instant;
import org.threeten.bp.temporal.ChronoUnit;
/**
* JUnit rule to start and stop the target test environment.
*
* <p>The environment can be specified via the system property {@code bigtable.env}. The choices
* are:
*
* <ul>
* <li>{@code emulator}: uses the cbtemulator component that can be installed by gcloud
* <li>{@code prod}: uses a pre-existing production table. The target table is defined using
* system properties listed in {@link CloudEnv} and application default credentials
* <li>{@code direct_path}: uses a pre-existing table in the direct path test environment. The
* target table is defined using system properties listed in {@link CloudEnv} and application
* default credentials
* </ul>
*
* <p>By default, {@code emulator} will be used
*
* <p>Also, when the system property {@code bigtable.grpc-log-dir} is set, it will enable fine
* grained gRPC logging to the configured path.
*/
public class TestEnvRule implements TestRule {
private static final Logger LOGGER = Logger.getLogger(TestEnvRule.class.getName());
private static final Boolean BIGTABLE_ENABLE_VERBOSE_GRPC_LOGS =
Boolean.getBoolean("enable-verbose-grpc-logs");
private static final String BIGTABLE_GRPC_LOG_DIR = System.getProperty("bigtable.grpc-log-dir");
private static final String BIGTABLE_EMULATOR_HOST_ENV_VAR = "BIGTABLE_EMULATOR_HOST";
private static final String ENV_PROPERTY = "bigtable.env";
private static final String env = System.getProperty(ENV_PROPERTY, "emulator");
private AbstractTestEnv testEnv;
private Handler grpcLogHandler;
private static final Set<String> GRPC_LOGGER_NAMES =
ImmutableSet.of("io.grpc", "io.grpc.netty.shaded");
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
public void evaluate() throws Throwable {
TestEnvRule.this.before(description);
try {
base.evaluate();
} finally {
TestEnvRule.this.after();
}
}
};
}
protected void before(Description description) throws Throwable {
assume()
.withMessage(
"Integration tests can't run with the BIGTABLE_EMULATOR_HOST environment variable set"
+ ". Please use the emulator-it maven profile instead")
.that(System.getenv())
.doesNotContainKey(BIGTABLE_EMULATOR_HOST_ENV_VAR);
configureLogging(description);
switch (env) {
case "emulator":
testEnv = EmulatorEnv.createBundled();
break;
case "cloud":
testEnv = CloudEnv.fromSystemProperties();
break;
default:
throw new IllegalArgumentException(
String.format(
"Unknown env: %s. Please set the system property %s to either 'emulator' or 'cloud'.",
env, ENV_PROPERTY));
}
testEnv.start();
}
private void configureLogging(Description description) throws IOException {
if (!BIGTABLE_ENABLE_VERBOSE_GRPC_LOGS) {
return;
}
Preconditions.checkState(
!Strings.isNullOrEmpty(BIGTABLE_GRPC_LOG_DIR),
"The property "
+ BIGTABLE_GRPC_LOG_DIR
+ " must be set when verbose grpc logs are enabled");
Files.createDirectories(Paths.get(BIGTABLE_GRPC_LOG_DIR));
String basename =
Joiner.on("-").useForNull("").join(description.getClassName(), description.getMethodName());
Path logPath = Paths.get(BIGTABLE_GRPC_LOG_DIR, basename + ".log");
grpcLogHandler = new FileHandler(logPath.toString());
grpcLogHandler.setFormatter(new SimpleFormatter());
grpcLogHandler.setLevel(Level.ALL);
for (String grpcLoggerName : GRPC_LOGGER_NAMES) {
Logger logger = Logger.getLogger(grpcLoggerName);
logger.setLevel(Level.ALL);
logger.addHandler(grpcLogHandler);
}
}
private void after() {
try {
cleanUpStale();
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to cleanup environment", e);
}
try {
testEnv.stop();
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to stop the environment", e);
}
testEnv = null;
teardownLogging();
}
private void teardownLogging() {
if (grpcLogHandler == null) {
return;
}
for (String grpcLoggerName : GRPC_LOGGER_NAMES) {
Logger.getLogger(grpcLoggerName).removeHandler(grpcLogHandler);
}
grpcLogHandler.flush();
grpcLogHandler = null;
}
void cleanUpStale() throws ExecutionException, InterruptedException, IOException {
// Sortable resource prefix - time, process identifier, serial counterck
String stalePrefix = PrefixGenerator.newTimePrefix(Instant.now().minus(1, ChronoUnit.DAYS));
cleanupStaleTables(stalePrefix);
if (env().isInstanceAdminSupported()) {
cleanUpStaleAppProfile(stalePrefix);
cleanUpStaleClusters(stalePrefix);
cleanUpStaleInstances(stalePrefix);
}
}
/**
* Clean up AppProfile that were dynamically created in the default instance that have been
* orphaned.
*
* @param stalePrefix
*/
private void cleanupStaleTables(String stalePrefix) {
for (String tableId : env().getTableAdminClient().listTables()) {
if (!tableId.startsWith(PrefixGenerator.PREFIX)) {
continue;
}
if (stalePrefix.compareTo(tableId) > 0) {
try {
env().getTableAdminClient().deleteTable(tableId);
} catch (NotFoundException ignored) {
}
}
}
}
/**
* Clean up AppProfile that were dynamically created in the default instance that have been
* orphaned.
*
* @param stalePrefix
*/
private void cleanUpStaleAppProfile(String stalePrefix) {
for (AppProfile appProfile :
env().getInstanceAdminClient().listAppProfiles(env().getInstanceId())) {
if (!appProfile.getId().startsWith(PrefixGenerator.PREFIX)) {
continue;
}
boolean isNewerThanStale = appProfile.getId().compareTo(stalePrefix) > 0;
if (isNewerThanStale) {
continue;
}
try {
env()
.getInstanceAdminClient()
.deleteAppProfile(env().getInstanceId(), appProfile.getId(), true);
} catch (NotFoundException ignored) {
}
}
}
/**
* Clean up clusters that were dynamically created in the default instance that have been
* orphaned.
*
* @param stalePrefix
*/
private void cleanUpStaleClusters(String stalePrefix)
throws ExecutionException, InterruptedException {
for (Cluster cluster : env().getInstanceAdminClient().listClusters(env().getInstanceId())) {
if (!cluster.getId().startsWith(PrefixGenerator.PREFIX)) {
continue;
}
boolean isNewerThanStale = cluster.getId().compareTo(stalePrefix) > 0;
if (isNewerThanStale) {
continue;
}
try {
deleteBackups(env().getTableAdminClient(), cluster.getId());
} catch (NotFoundException ignored) {
}
try {
env().getInstanceAdminClient().deleteCluster(env().getInstanceId(), cluster.getId());
} catch (NotFoundException ignored) {
}
}
}
/**
* Clean up dynamically created (non-default) instances that have been orphaned.
*
* @param stalePrefix
*/
private void cleanUpStaleInstances(String stalePrefix)
throws IOException, ExecutionException, InterruptedException {
for (Instance instance : env().getInstanceAdminClient().listInstances()) {
if (!instance.getId().startsWith(PrefixGenerator.PREFIX)) {
continue;
}
boolean isNewerThanStale = instance.getId().compareTo(stalePrefix) > 0;
if (isNewerThanStale) {
continue;
}
try {
deleteInstance(instance.getId());
} catch (NotFoundException ignored) {
}
}
}
/** Delete an instance with all of its resources. */
private void deleteInstance(String instanceId)
throws IOException, ExecutionException, InterruptedException {
BigtableTableAdminSettings settings =
env().getTableAdminSettings().toBuilder().setInstanceId(instanceId).build();
// Delete all child resources (backups & clusters) that wont be automatically deleted
try (BigtableTableAdminClient tableAdmin = BigtableTableAdminClient.create(settings)) {
List<Cluster> clusters = env().getInstanceAdminClient().listClusters(instanceId);
boolean isFirstCluster = true;
for (Cluster cluster : clusters) {
deleteBackups(tableAdmin, cluster.getId());
// Skip the first cluster so that it can be delete by deleteInstance (instances can't exist
// without clusters)
if (!isFirstCluster) {
try {
env().getInstanceAdminClient().deleteCluster(instanceId, cluster.getId());
} catch (NotFoundException ignored) {
}
}
isFirstCluster = false;
}
}
// Delete everything else
try {
env().getInstanceAdminClient().deleteInstance(instanceId);
} catch (NotFoundException ignored) {
}
}
private void deleteBackups(BigtableTableAdminClient tableAdmin, String clusterId)
throws ExecutionException, InterruptedException {
List<ApiFuture<?>> futures = new ArrayList<>();
for (String backupId : tableAdmin.listBackups(clusterId)) {
ApiFuture<Void> f = tableAdmin.deleteBackupAsync(clusterId, backupId);
futures.add(f);
}
for (ApiFuture<?> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
// Ignore not found
if (e.getCause() instanceof NotFoundException) {
continue;
}
throw e;
}
}
}
public AbstractTestEnv env() {
return testEnv;
}
}