-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathMockDatabaseAdminServiceImpl.java
1001 lines (929 loc) · 36.9 KB
/
MockDatabaseAdminServiceImpl.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019 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.spanner;
import com.google.api.gax.grpc.testing.MockGrpcService;
import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime;
import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.Policy;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
import com.google.longrunning.Operation;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.Any;
import com.google.protobuf.Empty;
import com.google.protobuf.Timestamp;
import com.google.rpc.Code;
import com.google.spanner.admin.database.v1.Backup;
import com.google.spanner.admin.database.v1.BackupInfo;
import com.google.spanner.admin.database.v1.CreateBackupMetadata;
import com.google.spanner.admin.database.v1.CreateBackupRequest;
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
import com.google.spanner.admin.database.v1.CreateDatabaseRequest;
import com.google.spanner.admin.database.v1.Database;
import com.google.spanner.admin.database.v1.Database.State;
import com.google.spanner.admin.database.v1.DatabaseAdminGrpc.DatabaseAdminImplBase;
import com.google.spanner.admin.database.v1.DeleteBackupRequest;
import com.google.spanner.admin.database.v1.DropDatabaseRequest;
import com.google.spanner.admin.database.v1.GetBackupRequest;
import com.google.spanner.admin.database.v1.GetDatabaseDdlRequest;
import com.google.spanner.admin.database.v1.GetDatabaseDdlResponse;
import com.google.spanner.admin.database.v1.GetDatabaseRequest;
import com.google.spanner.admin.database.v1.ListBackupOperationsRequest;
import com.google.spanner.admin.database.v1.ListBackupOperationsResponse;
import com.google.spanner.admin.database.v1.ListBackupsRequest;
import com.google.spanner.admin.database.v1.ListBackupsResponse;
import com.google.spanner.admin.database.v1.ListDatabaseOperationsRequest;
import com.google.spanner.admin.database.v1.ListDatabaseOperationsResponse;
import com.google.spanner.admin.database.v1.ListDatabasesRequest;
import com.google.spanner.admin.database.v1.ListDatabasesResponse;
import com.google.spanner.admin.database.v1.OperationProgress;
import com.google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata;
import com.google.spanner.admin.database.v1.RestoreDatabaseMetadata;
import com.google.spanner.admin.database.v1.RestoreDatabaseRequest;
import com.google.spanner.admin.database.v1.RestoreInfo;
import com.google.spanner.admin.database.v1.RestoreSourceType;
import com.google.spanner.admin.database.v1.UpdateBackupRequest;
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest;
import io.grpc.ServerServiceDefinition;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MockDatabaseAdminServiceImpl extends DatabaseAdminImplBase implements MockGrpcService {
private static final class MockDatabase {
private final String name;
private State state;
private final Timestamp createTime;
private final List<String> ddl = new ArrayList<>();
private final RestoreInfo restoreInfo;
private MockDatabase(String name, List<String> ddl, RestoreInfo restoreInfo) {
this.name = name;
this.state = State.CREATING;
this.createTime =
Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000L).build();
this.ddl.addAll(ddl);
this.restoreInfo = restoreInfo;
}
private Database toProto() {
return Database.newBuilder()
.setCreateTime(createTime)
.setName(name)
.setRestoreInfo(restoreInfo == null ? RestoreInfo.getDefaultInstance() : restoreInfo)
.setState(state)
.build();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof MockDatabase)) {
return false;
}
return ((MockDatabase) o).name.equals(this.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}
static final class MockBackup {
private final String name;
private Backup.State state;
private final Timestamp createTime;
private final String database;
private final List<String> ddl = new ArrayList<>();
private final List<String> referencingDatabases = new ArrayList<>();
private final long size;
private Timestamp expireTime;
private MockBackup(String name, Backup backup, MockDatabase database) {
this.name = name;
this.state = Backup.State.CREATING;
this.createTime =
Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000L).build();
this.database = database.name;
this.ddl.addAll(database.ddl);
this.size = RND.nextInt(Integer.MAX_VALUE);
this.expireTime = backup.getExpireTime();
}
private Backup toProto() {
return Backup.newBuilder()
.setCreateTime(createTime)
.setDatabase(database)
.setExpireTime(expireTime)
.setName(name)
.setSizeBytes(size)
.setState(state)
.addAllReferencingDatabases(referencingDatabases)
.build();
}
private BackupInfo toBackupInfo() {
return BackupInfo.newBuilder()
.setBackup(name)
.setCreateTime(createTime)
.setSourceDatabase(database)
.build();
}
public String getName() {
return name;
}
public String getDatabase() {
return database;
}
public Timestamp getExpireTime() {
return expireTime;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof MockDatabase)) {
return false;
}
return ((MockDatabase) o).name.equals(this.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
}
private final class CreateDatabaseCallable implements Callable<Database> {
private final String operationName;
private final String name;
private CreateDatabaseCallable(String operationName, String name) {
this.operationName = operationName;
this.name = name;
}
@Override
public Database call() {
MockDatabase db = databases.get(name);
db.state = State.READY;
Database proto = db.toProto();
Operation operation = operations.get(operationName);
if (operation != null) {
operations.update(operation.toBuilder().setDone(true).setResponse(Any.pack(proto)).build());
}
return proto;
}
}
private final class UpdateDatabaseDdlCallable implements Callable<Empty> {
private final String operationName;
private UpdateDatabaseDdlCallable(String operationName) {
this.operationName = operationName;
}
@Override
public Empty call() throws Exception {
Operation operation = operations.get(operationName);
if (operation != null) {
UpdateDatabaseDdlMetadata metadata =
operation.getMetadata().unpack(UpdateDatabaseDdlMetadata.class);
List<Timestamp> commitTimestamps = new ArrayList<>(metadata.getStatementsCount());
for (int i = 0; i < metadata.getStatementsCount(); i++) {
commitTimestamps.add(currentTime());
}
metadata = metadata.toBuilder().addAllCommitTimestamps(commitTimestamps).build();
operations.update(
operation
.toBuilder()
.setMetadata(Any.pack(metadata))
.setDone(true)
.setResponse(Any.pack(Empty.getDefaultInstance()))
.build());
}
return Empty.getDefaultInstance();
}
}
private final class CreateBackupCallable implements Callable<Backup> {
private final String operationName;
private final String name;
private CreateBackupCallable(String operationName, String name) {
this.operationName = operationName;
this.name = name;
}
@Override
public Backup call() throws Exception {
MockBackup backup = backups.get(name);
Backup proto = backup.toProto();
Operation operation = operations.get(operationName);
for (int progress = 1; progress <= 100; progress++) {
operation = operations.get(operationName);
long sleep = createBackupOperationExecutionTime / 100;
if (progress == 100) {
sleep += createBackupOperationExecutionTime % 100;
}
Thread.sleep(sleep);
if (operation != null) {
CreateBackupMetadata metadata =
operation.getMetadata().unpack(CreateBackupMetadata.class);
metadata =
metadata
.toBuilder()
.setProgress(
metadata.getProgress().toBuilder().setProgressPercent(progress).build())
.build();
operations.update(
operation
.toBuilder()
.setMetadata(Any.pack(metadata))
.setResponse(Any.pack(proto))
.build());
}
}
backup.state = Backup.State.READY;
proto = backup.toProto();
if (operation != null) {
CreateBackupMetadata metadata = operation.getMetadata().unpack(CreateBackupMetadata.class);
metadata =
metadata
.toBuilder()
.setProgress(
metadata
.getProgress()
.toBuilder()
.setProgressPercent(100)
.setEndTime(currentTime())
.build())
.build();
operations.update(
operation
.toBuilder()
.setDone(true)
.setMetadata(Any.pack(metadata))
.setResponse(Any.pack(proto))
.build());
}
return proto;
}
}
private final class RestoreDatabaseCallable implements Callable<Database> {
private final String operationName;
private final String name;
private RestoreDatabaseCallable(String operationName, String name) {
this.operationName = operationName;
this.name = name;
}
@Override
public Database call() throws Exception {
MockDatabase db = databases.get(name);
db.state = State.READY_OPTIMIZING;
Database proto = db.toProto();
Operation operation = operations.get(operationName);
for (int progress = 1; progress <= 100; progress++) {
long sleep = restoreDatabaseOperationExecutionTime / 100;
if (progress == 100) {
sleep += restoreDatabaseOperationExecutionTime % 100;
}
Thread.sleep(sleep);
if (operation != null) {
RestoreDatabaseMetadata metadata =
operation.getMetadata().unpack(RestoreDatabaseMetadata.class);
metadata =
metadata
.toBuilder()
.setProgress(
metadata.getProgress().toBuilder().setProgressPercent(progress).build())
.build();
operations.update(
operation
.toBuilder()
.setMetadata(Any.pack(metadata))
.setResponse(Any.pack(proto))
.build());
}
}
db.state = State.READY_OPTIMIZING;
proto = db.toProto();
if (operation != null) {
RestoreDatabaseMetadata metadata =
operation.getMetadata().unpack(RestoreDatabaseMetadata.class);
metadata =
metadata
.toBuilder()
.setProgress(
metadata
.getProgress()
.toBuilder()
.setEndTime(currentTime())
.setProgressPercent(100)
.build())
.build();
operations.update(
operation
.toBuilder()
.setDone(true)
.setMetadata(Any.pack(metadata))
.setResponse(Any.pack(proto))
.build());
}
return proto;
}
}
private final class OptimizeDatabaseCallable implements Callable<Database> {
private final String operationName;
private final String restoreOperationName;
private final String name;
private OptimizeDatabaseCallable(
String operationName, String restoreOperationName, String name) {
this.operationName = operationName;
this.restoreOperationName = restoreOperationName;
this.name = name;
}
@Override
public Database call() throws Exception {
MockDatabase db = databases.get(name);
Operation operation = operations.get(operationName);
try {
// Wait until the restore operation has finished.
Operation restoreOperation = operations.get(restoreOperationName);
while (!restoreOperation.getDone()) {
Thread.sleep(10L);
restoreOperation = operations.get(restoreOperationName);
}
Thread.sleep(optimizeDatabaseOperationExecutionTime);
db.state = State.READY;
Database proto = db.toProto();
if (operation != null) {
operations.update(
operation.toBuilder().setDone(true).setResponse(Any.pack(proto)).build());
}
return proto;
} catch (Exception e) {
if (operation != null) {
Database proto = db.toProto();
operations.update(
operation
.toBuilder()
.setDone(true)
.setError(fromException(e))
.setResponse(Any.pack(proto))
.build());
}
throw e;
}
}
}
private com.google.rpc.Status fromException(Exception e) {
int code = Code.UNKNOWN_VALUE;
if (e instanceof InterruptedException) {
code = Code.CANCELLED_VALUE;
}
return com.google.rpc.Status.newBuilder().setCode(code).setMessage(e.getMessage()).build();
}
private final Queue<AbstractMessage> requests = new ConcurrentLinkedQueue<>();
private ConcurrentMap<String, Policy> policies = new ConcurrentHashMap<>();
private static final String EXPIRE_TIME_MASK = "expire_time";
private static final Random RND = new Random();
private final Queue<Exception> exceptions = new ConcurrentLinkedQueue<>();
private volatile CountDownLatch freezeLock = new CountDownLatch(0);
private final ConcurrentMap<String, MockDatabase> databases = new ConcurrentHashMap<>();
private final ConcurrentMap<String, MockBackup> backups = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Set<String>> filterMatches = new ConcurrentHashMap<>();
private final MockOperationsServiceImpl operations;
private long createBackupOperationExecutionTime;
private long restoreDatabaseOperationExecutionTime;
private long optimizeDatabaseOperationExecutionTime;
private SimulatedExecutionTime createBackupStartupExecutionTime = SimulatedExecutionTime.none();
private SimulatedExecutionTime createBackupResponseExecutionTime = SimulatedExecutionTime.none();
private SimulatedExecutionTime createDatabaseStartupExecutionTime = SimulatedExecutionTime.none();
private SimulatedExecutionTime createDatabaseResponseExecutionTime =
SimulatedExecutionTime.none();
private SimulatedExecutionTime getDatabaseExecutionTime = SimulatedExecutionTime.none();
private SimulatedExecutionTime restoreDatabaseStartupExecutionTime =
SimulatedExecutionTime.none();
private SimulatedExecutionTime restoreDatabaseResponseExecutionTime =
SimulatedExecutionTime.none();
public MockDatabaseAdminServiceImpl(MockOperationsServiceImpl operations) {
this.operations = operations;
}
@Override
public void createDatabase(
CreateDatabaseRequest request, StreamObserver<Operation> responseObserver) {
requests.add(request);
try {
createDatabaseStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
String id = request.getCreateStatement().replace("CREATE DATABASE ", "");
if (id.startsWith("`") && id.endsWith("`")) {
id = id.substring(1, id.length() - 1);
}
String name = String.format("%s/databases/%s", request.getParent(), id);
MockDatabase db = new MockDatabase(name, request.getExtraStatementsList(), null);
if (databases.putIfAbsent(name, db) == null) {
CreateDatabaseMetadata metadata =
CreateDatabaseMetadata.newBuilder().setDatabase(name).build();
Database database = Database.newBuilder().setName(name).setState(db.state).build();
Operation operation =
Operation.newBuilder()
.setMetadata(Any.pack(metadata))
.setResponse(Any.pack(database))
.setDone(false)
.setName(operations.generateOperationName(name))
.build();
operations.addOperation(operation, new CreateDatabaseCallable(operation.getName(), name));
createDatabaseResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
responseObserver.onNext(operation);
responseObserver.onCompleted();
} else {
responseObserver.onError(
Status.ALREADY_EXISTS
.withDescription(String.format("Database with name %s already exists", name))
.asRuntimeException());
}
} catch (Throwable t) {
responseObserver.onError(t);
}
}
@Override
public void dropDatabase(DropDatabaseRequest request, StreamObserver<Empty> responseObserver) {
requests.add(request);
MockDatabase db = databases.get(request.getDatabase());
if (databases.remove(request.getDatabase(), db)) {
responseObserver.onNext(Empty.getDefaultInstance());
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
}
@Override
public void getDatabase(GetDatabaseRequest request, StreamObserver<Database> responseObserver) {
requests.add(request);
try {
getDatabaseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
MockDatabase db = databases.get(request.getName());
if (db != null) {
responseObserver.onNext(
Database.newBuilder()
.setName(request.getName())
.setCreateTime(db.createTime)
.setState(State.READY)
.build());
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
} catch (Throwable t) {
responseObserver.onError(t);
}
}
@Override
public void getDatabaseDdl(
GetDatabaseDdlRequest request, StreamObserver<GetDatabaseDdlResponse> responseObserver) {
requests.add(request);
MockDatabase db = databases.get(request.getDatabase());
if (db != null) {
responseObserver.onNext(GetDatabaseDdlResponse.newBuilder().addAllStatements(db.ddl).build());
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
}
@Override
public void listDatabases(
ListDatabasesRequest request, StreamObserver<ListDatabasesResponse> responseObserver) {
requests.add(request);
List<Database> dbs = new ArrayList<>(databases.size());
for (Entry<String, MockDatabase> entry : databases.entrySet()) {
dbs.add(
Database.newBuilder()
.setName(entry.getKey())
.setCreateTime(entry.getValue().createTime)
.setState(State.READY)
.build());
}
responseObserver.onNext(ListDatabasesResponse.newBuilder().addAllDatabases(dbs).build());
responseObserver.onCompleted();
}
@Override
public void listDatabaseOperations(
ListDatabaseOperationsRequest request,
StreamObserver<ListDatabaseOperationsResponse> responseObserver) {
requests.add(request);
ListDatabaseOperationsResponse.Builder builder = ListDatabaseOperationsResponse.newBuilder();
try {
for (Operation op : operations.iterable()) {
if (op.getName().matches(".*?/databases\\/.*?/operations/.*?")
&& op.getName().startsWith(request.getParent())) {
if (matchesFilter(op, request.getFilter())) {
builder.addOperations(op);
}
}
}
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
} catch (Exception e) {
responseObserver.onError(e);
}
}
private boolean matchesFilter(Object obj, String filter) throws Exception {
if (!Strings.isNullOrEmpty(filter)) {
Set<String> matches = filterMatches.get(filter);
if (matches != null) {
String name = (String) obj.getClass().getMethod("getName").invoke(obj);
return matches.contains(name);
}
if (obj instanceof Operation) {
Operation operation = (Operation) obj;
Pattern pattern =
Pattern.compile(
"(?:\\(metadata.@type:type.googleapis.com/(.*)\\)) AND (?:\\(metadata.(?:name|database):(.*)\\)|\\(name:(.*)/operations/\\))");
Matcher matcher = pattern.matcher(filter);
if (matcher.matches()) {
String type = matcher.group(1);
String objectName = matcher.group(2);
if (objectName == null) {
objectName = matcher.group(3);
}
Any anyMetadata = operation.getMetadata();
if (anyMetadata.getTypeUrl().endsWith(type)) {
if (type.equals(CreateBackupMetadata.getDescriptor().getFullName())) {
CreateBackupMetadata metadata =
operation.getMetadata().unpack(CreateBackupMetadata.class);
return metadata.getName().equals(objectName);
} else if (type.equals(CreateDatabaseMetadata.getDescriptor().getFullName())) {
CreateDatabaseMetadata metadata =
operation.getMetadata().unpack(CreateDatabaseMetadata.class);
return metadata.getDatabase().equals(objectName);
} else if (type.equals(RestoreDatabaseMetadata.getDescriptor().getFullName())) {
RestoreDatabaseMetadata metadata =
operation.getMetadata().unpack(RestoreDatabaseMetadata.class);
return metadata.getName().equals(objectName);
}
}
}
}
return false;
}
return true;
}
@Override
public void updateDatabaseDdl(
UpdateDatabaseDdlRequest request, StreamObserver<Operation> responseObserver) {
requests.add(request);
MockDatabase db = databases.get(request.getDatabase());
if (db != null) {
db.ddl.addAll(request.getStatementsList());
UpdateDatabaseDdlMetadata metadata =
UpdateDatabaseDdlMetadata.newBuilder()
.setDatabase(request.getDatabase())
.addAllStatements(request.getStatementsList())
.build();
Operation operation =
Operation.newBuilder()
.setMetadata(Any.pack(metadata))
.setResponse(Any.pack(Empty.getDefaultInstance()))
.setDone(true)
.setName(operations.generateOperationName(request.getDatabase()))
.build();
operations.addOperation(operation, new UpdateDatabaseDdlCallable(operation.getName()));
responseObserver.onNext(operation);
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
}
@Override
public void createBackup(
CreateBackupRequest request, StreamObserver<Operation> responseObserver) {
requests.add(request);
try {
createBackupStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
String name = String.format("%s/backups/%s", request.getParent(), request.getBackupId());
MockDatabase db = databases.get(request.getBackup().getDatabase());
if (db == null) {
responseObserver.onError(
Status.NOT_FOUND
.withDescription(
String.format(
"Database with name %s not found", request.getBackup().getDatabase()))
.asRuntimeException());
return;
}
MockBackup bck = new MockBackup(name, request.getBackup(), db);
if (backups.putIfAbsent(name, bck) == null) {
CreateBackupMetadata metadata =
CreateBackupMetadata.newBuilder()
.setName(name)
.setDatabase(bck.database)
.setProgress(
OperationProgress.newBuilder()
.setStartTime(
Timestamp.newBuilder()
.setSeconds(System.currentTimeMillis() / 1000L)
.build())
.setProgressPercent(0))
.build();
Operation operation =
Operation.newBuilder()
.setMetadata(Any.pack(metadata))
.setResponse(Any.pack(bck.toProto()))
.setName(operations.generateOperationName(name))
.build();
operations.addOperation(operation, new CreateBackupCallable(operation.getName(), name));
createBackupResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
responseObserver.onNext(operation);
responseObserver.onCompleted();
} else {
responseObserver.onError(
Status.ALREADY_EXISTS
.withDescription(String.format("Backup with name %s already exists", name))
.asRuntimeException());
}
} catch (Throwable t) {
responseObserver.onError(t);
}
}
@Override
public void deleteBackup(DeleteBackupRequest request, StreamObserver<Empty> responseObserver) {
requests.add(request);
MockBackup bck = backups.get(request.getName());
if (backups.remove(request.getName(), bck)) {
responseObserver.onNext(Empty.getDefaultInstance());
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
}
@Override
public void getBackup(GetBackupRequest request, StreamObserver<Backup> responseObserver) {
requests.add(request);
MockBackup bck = backups.get(request.getName());
if (bck != null) {
responseObserver.onNext(
Backup.newBuilder()
.setName(request.getName())
.setCreateTime(bck.createTime)
.setDatabase(bck.database)
.setExpireTime(bck.expireTime)
.setSizeBytes(bck.size)
.setState(Backup.State.READY)
.build());
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
}
@Override
public void listBackups(
ListBackupsRequest request, StreamObserver<ListBackupsResponse> responseObserver) {
requests.add(request);
List<Backup> bcks = new ArrayList<>(backups.size());
try {
for (Entry<String, MockBackup> entry : backups.entrySet()) {
if (matchesFilter(entry.getValue(), request.getFilter())) {
bcks.add(
Backup.newBuilder()
.setName(entry.getKey())
.setCreateTime(entry.getValue().createTime)
.setDatabase(entry.getValue().database)
.setExpireTime(entry.getValue().expireTime)
.setSizeBytes(entry.getValue().size)
.setState(Backup.State.READY)
.build());
}
}
responseObserver.onNext(ListBackupsResponse.newBuilder().addAllBackups(bcks).build());
responseObserver.onCompleted();
} catch (Exception e) {
responseObserver.onError(e);
}
}
@Override
public void listBackupOperations(
ListBackupOperationsRequest request,
StreamObserver<ListBackupOperationsResponse> responseObserver) {
requests.add(request);
ListBackupOperationsResponse.Builder builder = ListBackupOperationsResponse.newBuilder();
try {
for (Operation op : operations.iterable()) {
if (op.getName().matches(".*?/backups/.*?/operations/.*?")
&& op.getName().startsWith(request.getParent())) {
if (matchesFilter(op, request.getFilter())) {
builder.addOperations(op);
}
}
}
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
} catch (Exception e) {
responseObserver.onError(e);
}
}
@Override
public void updateBackup(UpdateBackupRequest request, StreamObserver<Backup> responseObserver) {
requests.add(request);
MockBackup bck = backups.get(request.getBackup().getName());
if (bck != null) {
if (request.getUpdateMask().getPathsList().contains(EXPIRE_TIME_MASK)) {
bck.expireTime = request.getBackup().getExpireTime();
}
responseObserver.onNext(
Backup.newBuilder()
.setName(bck.name)
.setCreateTime(bck.createTime)
.setDatabase(bck.database)
.setExpireTime(bck.expireTime)
.setSizeBytes(bck.size)
.setState(Backup.State.READY)
.build());
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
}
@Override
public void restoreDatabase(
RestoreDatabaseRequest request, StreamObserver<Operation> responseObserver) {
requests.add(request);
try {
restoreDatabaseStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
MockBackup bck = backups.get(request.getBackup());
if (bck != null) {
String name =
String.format("%s/databases/%s", request.getParent(), request.getDatabaseId());
MockDatabase db =
new MockDatabase(
name,
bck.ddl,
RestoreInfo.newBuilder()
.setBackupInfo(bck.toBackupInfo())
.setSourceType(RestoreSourceType.BACKUP)
.build());
if (databases.putIfAbsent(name, db) == null) {
bck.referencingDatabases.add(db.name);
Operation optimizeOperation =
Operation.newBuilder()
.setDone(false)
.setName(operations.generateOperationName(name))
.setMetadata(
Any.pack(
OptimizeRestoredDatabaseMetadata.newBuilder()
.setName(name)
.setProgress(
OperationProgress.newBuilder()
.setStartTime(currentTime())
.setProgressPercent(0)
.build())
.build()))
.setResponse(Any.pack(db.toProto()))
.build();
RestoreDatabaseMetadata metadata =
RestoreDatabaseMetadata.newBuilder()
.setBackupInfo(bck.toBackupInfo())
.setName(name)
.setProgress(
OperationProgress.newBuilder()
.setStartTime(currentTime())
.setProgressPercent(0)
.build())
.setOptimizeDatabaseOperationName(optimizeOperation.getName())
.setSourceType(RestoreSourceType.BACKUP)
.build();
Operation operation =
Operation.newBuilder()
.setMetadata(Any.pack(metadata))
.setResponse(Any.pack(db.toProto()))
.setDone(false)
.setName(operations.generateOperationName(name))
.build();
operations.addOperation(
operation, new RestoreDatabaseCallable(operation.getName(), name));
operations.addOperation(
optimizeOperation,
new OptimizeDatabaseCallable(optimizeOperation.getName(), operation.getName(), name));
restoreDatabaseResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
responseObserver.onNext(operation);
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.ALREADY_EXISTS.asRuntimeException());
}
} else {
responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
}
} catch (Throwable t) {
responseObserver.onError(t);
}
}
@Override
public void getIamPolicy(GetIamPolicyRequest request, StreamObserver<Policy> responseObserver) {
requests.add(request);
Policy policy = policies.get(request.getResource());
if (policy != null) {
responseObserver.onNext(policy);
} else {
responseObserver.onNext(Policy.getDefaultInstance());
}
responseObserver.onCompleted();
}
@Override
public void setIamPolicy(SetIamPolicyRequest request, StreamObserver<Policy> responseObserver) {
requests.add(request);
policies.put(request.getResource(), request.getPolicy());
responseObserver.onNext(request.getPolicy());
responseObserver.onCompleted();
}
@Override
public void testIamPermissions(
TestIamPermissionsRequest request,
StreamObserver<TestIamPermissionsResponse> responseObserver) {
requests.add(request);
// Just return the same permissions as in the request, as we don't have any credentials.
responseObserver.onNext(
TestIamPermissionsResponse.newBuilder()
.addAllPermissions(request.getPermissionsList())
.build());
responseObserver.onCompleted();
}
@Override
public List<AbstractMessage> getRequests() {
return new ArrayList<>(requests);
}
public void clearRequests() {
requests.clear();
}
public int countRequestsOfType(final Class<? extends AbstractMessage> type) {
return Collections2.filter(getRequests(), input -> input.getClass().equals(type)).size();
}
@Override
public void addResponse(AbstractMessage response) {
throw new UnsupportedOperationException();
}
@Override
public void addException(Exception exception) {
exceptions.add(exception);
}
public void addFilterMatches(String filter, String... names) {
Set<String> matches = filterMatches.computeIfAbsent(filter, k -> new HashSet<>());
matches.addAll(Arrays.asList(names));
}
public void clearFilterMatches() {
filterMatches.clear();
}
@Override
public ServerServiceDefinition getServiceDefinition() {
return bindService();
}
@Override
public void reset() {
requests.clear();
exceptions.clear();
policies.clear();
databases.clear();
backups.clear();
filterMatches.clear();
}
public void removeAllExecutionTimes() {
createBackupStartupExecutionTime = SimulatedExecutionTime.none();
createBackupResponseExecutionTime = SimulatedExecutionTime.none();
createBackupOperationExecutionTime = 0L;
createDatabaseStartupExecutionTime = SimulatedExecutionTime.none();
createDatabaseResponseExecutionTime = SimulatedExecutionTime.none();
restoreDatabaseStartupExecutionTime = SimulatedExecutionTime.none();
restoreDatabaseResponseExecutionTime = SimulatedExecutionTime.none();
restoreDatabaseOperationExecutionTime = 0L;
}
private Timestamp currentTime() {
return Timestamp.newBuilder().setSeconds(System.currentTimeMillis() * 1000L).build();
}
public void setCreateBackupStartupExecutionTime(SimulatedExecutionTime exec) {
this.createBackupStartupExecutionTime = exec;
}
public void setCreateBackupResponseExecutionTime(SimulatedExecutionTime exec) {
this.createBackupResponseExecutionTime = exec;
}
public void setCreateDatabaseStartupExecutionTime(SimulatedExecutionTime exec) {
this.createDatabaseStartupExecutionTime = exec;
}
public void setCreateDatabaseResponseExecutionTime(SimulatedExecutionTime exec) {
this.createDatabaseResponseExecutionTime = exec;
}
public void setRestoreDatabaseStartupExecutionTime(SimulatedExecutionTime exec) {
this.restoreDatabaseStartupExecutionTime = exec;
}
public void setRestoreDatabaseResponseExecutionTime(SimulatedExecutionTime exec) {
this.restoreDatabaseResponseExecutionTime = exec;
}