use of com.alibaba.maxgraph.sdkcommon.common.BackupInfo in project GraphScope by alibaba.
the class FrontendRpcTest method testBackupClient.
@Test
void testBackupClient() throws IOException {
BackupGrpc.BackupBlockingStub stub = mock(BackupGrpc.BackupBlockingStub.class);
when(stub.createNewBackup(any())).thenReturn(CreateNewBackupResponse.newBuilder().setGlobalBackupId(1).build());
when(stub.deleteBackup(any())).thenReturn(DeleteBackupResponse.newBuilder().build());
when(stub.purgeOldBackups(any())).thenReturn(PurgeOldBackupsResponse.newBuilder().build());
when(stub.restoreFromBackup(any())).thenReturn(RestoreFromBackupResponse.newBuilder().build());
when(stub.verifyBackup(any())).thenReturn(VerifyBackupResponse.newBuilder().build());
BackupInfo backupInfo = new BackupInfo(1, 10L, GraphDef.newBuilder().setVersion(10L).build().toProto().toByteArray(), new ArrayList<>(), new HashMap<>());
when(stub.getBackupInfo(any())).thenReturn(GetBackupInfoResponse.newBuilder().addBackupInfoList(backupInfo.toProto()).build());
BackupClient backupClient = new BackupClient(stub);
int newBackupId = backupClient.createNewBackup();
assertEquals(newBackupId, 1);
assertDoesNotThrow(() -> backupClient.deleteBackup(1));
assertDoesNotThrow(() -> backupClient.purgeOldBackups(1));
assertDoesNotThrow(() -> backupClient.restoreFromBackup(1, "restore_meta", "restore_store"));
assertDoesNotThrow(() -> backupClient.verifyBackup(1));
List<BackupInfoPb> infoPbList = backupClient.getBackupInfo();
assertEquals(infoPbList.size(), 1);
assertEquals(BackupInfo.parseProto(infoPbList.get(0)), backupInfo);
}
use of com.alibaba.maxgraph.sdkcommon.common.BackupInfo in project GraphScope by alibaba.
the class CoordinatorRpcTest method testBackupService.
@Test
void testBackupService() throws IOException {
BackupManager mockBackupManger = mock(BackupManager.class);
BackupService backupService = new BackupService(mockBackupManger);
when(mockBackupManger.createNewBackup()).thenReturn(6);
StreamObserver<CreateNewBackupResponse> mockCreateObserver = mock(StreamObserver.class);
backupService.createNewBackup(CreateNewBackupRequest.newBuilder().build(), mockCreateObserver);
verify(mockCreateObserver).onNext(CreateNewBackupResponse.newBuilder().setGlobalBackupId(6).build());
verify(mockCreateObserver).onCompleted();
StreamObserver<DeleteBackupResponse> mockDeleteObserver = mock(StreamObserver.class);
backupService.deleteBackup(DeleteBackupRequest.newBuilder().setGlobalBackupId(8).build(), mockDeleteObserver);
verify(mockBackupManger).deleteBackup(8);
verify(mockDeleteObserver).onNext(DeleteBackupResponse.newBuilder().build());
verify(mockDeleteObserver).onCompleted();
StreamObserver<PurgeOldBackupsResponse> mockPurgeObserver = mock(StreamObserver.class);
backupService.purgeOldBackups(PurgeOldBackupsRequest.newBuilder().setKeepAliveNumber(5).build(), mockPurgeObserver);
verify(mockBackupManger).purgeOldBackups(5);
verify(mockPurgeObserver).onNext(PurgeOldBackupsResponse.newBuilder().build());
verify(mockPurgeObserver).onCompleted();
StreamObserver<RestoreFromBackupResponse> mockRestoreObserver = mock(StreamObserver.class);
backupService.restoreFromBackup(RestoreFromBackupRequest.newBuilder().setGlobalBackupId(9).setMetaRestorePath("restore_meta").setStoreRestorePath("restore_store").build(), mockRestoreObserver);
verify(mockBackupManger).restoreFromBackup(9, "restore_meta", "restore_store");
verify(mockRestoreObserver).onNext(RestoreFromBackupResponse.newBuilder().build());
verify(mockRestoreObserver).onCompleted();
StreamObserver<VerifyBackupResponse> mockVerifyObserver = mock(StreamObserver.class);
backupService.verifyBackup(VerifyBackupRequest.newBuilder().setGlobalBackupId(7).build(), mockVerifyObserver);
verify(mockBackupManger).verifyBackup(7);
verify(mockVerifyObserver).onNext(VerifyBackupResponse.newBuilder().build());
verify(mockVerifyObserver).onCompleted();
BackupInfo backupInfo1 = new BackupInfo(1, 10L, GraphDef.newBuilder().setVersion(1L).build().toProto().toByteArray(), new ArrayList<>(), new HashMap<>());
BackupInfo backupInfo2 = new BackupInfo(2, 10L, GraphDef.newBuilder().setVersion(2L).build().toProto().toByteArray(), new ArrayList<>(), new HashMap<>());
when(mockBackupManger.getBackupInfoList()).thenReturn(Arrays.asList(backupInfo1, backupInfo2));
StreamObserver<GetBackupInfoResponse> mockGetInfoObserver = mock(StreamObserver.class);
backupService.getBackupInfo(GetBackupInfoRequest.newBuilder().build(), mockGetInfoObserver);
verify(mockGetInfoObserver).onNext(GetBackupInfoResponse.newBuilder().addBackupInfoList(backupInfo1.toProto()).addBackupInfoList(backupInfo2.toProto()).build());
verify(mockGetInfoObserver).onCompleted();
}
use of com.alibaba.maxgraph.sdkcommon.common.BackupInfo in project GraphScope by alibaba.
the class BackupManager method recover.
private void recover() throws IOException {
checkMetaPath(GLOBAL_BACKUP_ID_PATH);
checkMetaPath(BACKUP_INFO_PATH);
byte[] globalBackupIdBytes = this.metaStore.read(GLOBAL_BACKUP_ID_PATH);
this.globalBackupId = this.objectMapper.readValue(globalBackupIdBytes, Integer.class);
byte[] backupInfoBytes = this.metaStore.read(BACKUP_INFO_PATH);
List<BackupInfo> recoveredBackupInfoList = this.objectMapper.readValue(backupInfoBytes, new TypeReference<List<BackupInfo>>() {
});
this.globalBackupIdToInfo = new ConcurrentHashMap<>(recoveredBackupInfoList.size());
for (BackupInfo bi : recoveredBackupInfoList) {
this.globalBackupIdToInfo.put(bi.getGlobalBackupId(), bi);
}
}
use of com.alibaba.maxgraph.sdkcommon.common.BackupInfo in project GraphScope by alibaba.
the class BackupService method getBackupInfo.
@Override
public void getBackupInfo(GetBackupInfoRequest request, StreamObserver<GetBackupInfoResponse> responseObserver) {
try {
List<BackupInfo> infoList = this.backupManager.getBackupInfoList();
List<BackupInfoPb> infoPbList = new ArrayList<>(infoList.size());
for (BackupInfo info : this.backupManager.getBackupInfoList()) {
infoPbList.add(info.toProto());
}
responseObserver.onNext(GetBackupInfoResponse.newBuilder().addAllBackupInfoList(infoPbList).build());
responseObserver.onCompleted();
} catch (Exception e) {
responseObserver.onError(e);
}
}
use of com.alibaba.maxgraph.sdkcommon.common.BackupInfo in project GraphScope by alibaba.
the class BackupManagerTest method testBackupManager.
@Test
void testBackupManager() throws IOException, InterruptedException {
// init config
Configs configs = Configs.newBuilder().put(CommonConfig.STORE_NODE_COUNT.getKey(), "2").put(BackupConfig.BACKUP_ENABLE.getKey(), "true").put(BackupConfig.BACKUP_CREATION_BUFFER_MAX_COUNT.getKey(), "4").put(BackupConfig.BACKUP_GC_INTERVAL_HOURS.getKey(), "24").put(BackupConfig.BACKUP_AUTO_SUBMIT.getKey(), "false").put(BackupConfig.BACKUP_AUTO_SUBMIT_INTERVAL_HOURS.getKey(), "24").build();
// init data
long querySnapshotId = 10L;
GraphDef graphDef = GraphDef.newBuilder().setVersion(10L).build();
SnapshotWithSchema snapshotWithSchema = new SnapshotWithSchema(querySnapshotId, graphDef);
List<Long> queueOffsets = new ArrayList<>();
Map<Integer, Integer> partitionToBackupId1 = new HashMap<>();
partitionToBackupId1.put(0, 1);
partitionToBackupId1.put(1, 1);
Map<Integer, Integer> partitionToBackupId2 = new HashMap<>();
partitionToBackupId2.put(0, 2);
partitionToBackupId2.put(1, 2);
BackupInfo backupInfo1 = new BackupInfo(1, querySnapshotId, graphDef.toProto().toByteArray(), queueOffsets, partitionToBackupId1);
BackupInfo backupInfo2 = new BackupInfo(2, querySnapshotId, graphDef.toProto().toByteArray(), queueOffsets, partitionToBackupId2);
// mock MetaStore behaviours
ObjectMapper objectMapper = new ObjectMapper();
MetaStore mockMetaStore = mock(MetaStore.class);
when(mockMetaStore.exists(anyString())).thenReturn(true);
when(mockMetaStore.read(GLOBAL_BACKUP_ID_PATH)).thenReturn(objectMapper.writeValueAsBytes(0));
when(mockMetaStore.read(BACKUP_INFO_PATH)).thenReturn(objectMapper.writeValueAsBytes(new ArrayList<BackupInfo>()));
// mock MetaService behaviours
MetaService mockMetaService = mock(MetaService.class);
when(mockMetaService.getPartitionCount()).thenReturn(2);
when(mockMetaService.getStoreIdByPartition(0)).thenReturn(0);
when(mockMetaService.getStoreIdByPartition(1)).thenReturn(1);
// mock SnapshotManager behaviours
SnapshotManager mockSnapshotManager = mock(SnapshotManager.class);
when(mockSnapshotManager.getQueueOffsets()).thenReturn(queueOffsets);
// mock SchemaManager
SchemaManager mockSchemaManager = mock(SchemaManager.class);
when(mockSchemaManager.getGraphDef()).thenReturn(graphDef);
// mock SnapshotCache
SnapshotCache mockSnapshotCache = mock(SnapshotCache.class);
when(mockSnapshotCache.getSnapshotWithSchema()).thenReturn(snapshotWithSchema);
// mock StoreBackupTaskSender behaviours
StoreBackupTaskSender mockStoreBackupTaskSender = mock(StoreBackupTaskSender.class);
doAnswer(invocation -> {
int partitionOrStoreId = invocation.getArgument(0);
int globalBackupId = invocation.getArgument(1);
CompletionCallback<StoreBackupId> callback = invocation.getArgument(2);
StoreBackupId storeBackupId = new StoreBackupId(globalBackupId);
storeBackupId.addPartitionBackupId(partitionOrStoreId, globalBackupId);
callback.onCompleted(storeBackupId);
return null;
}).when(mockStoreBackupTaskSender).createStoreBackup(anyInt(), anyInt(), any());
doAnswer(invocation -> {
CompletionCallback<Void> callback = invocation.getArgument(3);
callback.onCompleted(null);
return null;
}).when(mockStoreBackupTaskSender).restoreFromStoreBackup(anyInt(), any(), anyString(), any());
doAnswer(invocation -> {
CompletionCallback<Void> callback = invocation.getArgument(2);
callback.onCompleted(null);
return null;
}).when(mockStoreBackupTaskSender).verifyStoreBackup(anyInt(), any(), any());
BackupManager backupManager = new BackupManager(configs, mockMetaService, mockMetaStore, mockSnapshotManager, mockSchemaManager, mockSnapshotCache, mockStoreBackupTaskSender);
backupManager.start();
verify(mockSnapshotManager).addListener(any());
// create the first backup
CountDownLatch updateBackupIdLatch1 = new CountDownLatch(1);
CountDownLatch updateBackupInfoByCreation1Latch = new CountDownLatch(1);
doAnswer(invocation -> {
updateBackupIdLatch1.countDown();
return null;
}).when(mockMetaStore).write(GLOBAL_BACKUP_ID_PATH, objectMapper.writeValueAsBytes(1));
doAnswer(invocation -> {
byte[] backupInfoBytes = invocation.getArgument(1);
List<BackupInfo> backupInfoList = objectMapper.readValue(backupInfoBytes, new TypeReference<List<BackupInfo>>() {
});
assertEquals(backupInfoList.size(), 1);
assertEquals(backupInfoList.get(0), backupInfo1);
updateBackupInfoByCreation1Latch.countDown();
return null;
}).when(mockMetaStore).write(BACKUP_INFO_PATH, objectMapper.writeValueAsBytes(Collections.singletonList(backupInfo1)));
int backupId1 = backupManager.createNewBackup();
assertEquals(backupId1, 1);
assertTrue(updateBackupIdLatch1.await(5L, TimeUnit.SECONDS));
assertTrue(updateBackupInfoByCreation1Latch.await(5L, TimeUnit.SECONDS));
// create the second backup
CountDownLatch updateBackupIdLatch2 = new CountDownLatch(1);
CountDownLatch updateBackupInfoByCreation2Latch = new CountDownLatch(1);
doAnswer(invocation -> {
updateBackupIdLatch2.countDown();
return null;
}).when(mockMetaStore).write(GLOBAL_BACKUP_ID_PATH, objectMapper.writeValueAsBytes(2));
doAnswer(invocation -> {
byte[] backupInfoBytes = invocation.getArgument(1);
List<BackupInfo> backupInfoList = objectMapper.readValue(backupInfoBytes, new TypeReference<List<BackupInfo>>() {
});
backupInfoList.sort(new Comparator<BackupInfo>() {
@Override
public int compare(BackupInfo o1, BackupInfo o2) {
return o1.getGlobalBackupId() - o2.getGlobalBackupId();
}
});
assertEquals(backupInfoList.size(), 2);
assertEquals(backupInfoList.get(0), backupInfo1);
assertEquals(backupInfoList.get(1), backupInfo2);
updateBackupInfoByCreation2Latch.countDown();
return null;
}).when(mockMetaStore).write(BACKUP_INFO_PATH, objectMapper.writeValueAsBytes(Arrays.asList(backupInfo1, backupInfo2)));
int backupId2 = backupManager.createNewBackup();
assertEquals(backupId2, 2);
assertTrue(updateBackupIdLatch2.await(5L, TimeUnit.SECONDS));
assertTrue(updateBackupInfoByCreation2Latch.await(5L, TimeUnit.SECONDS));
// get backup info list and check
assertEquals(backupManager.getBackupInfoList().size(), 2);
// verify backups
try {
backupManager.verifyBackup(backupId1);
backupManager.verifyBackup(backupId2);
} catch (Exception e) {
fail("should not have thrown any exception during backup verification");
}
// restore from the second backup
try {
backupManager.restoreFromBackup(backupId2, "restore_meta", "restore_store");
assertTrue(Files.exists(Paths.get("restore_meta", "query_snapshot_id")));
assertTrue(Files.exists(Paths.get("restore_meta", "graph_def_proto_bytes")));
assertTrue(Files.exists(Paths.get("restore_meta", "queue_offsets")));
} catch (Exception e) {
fail("should not have thrown any exception during backup restoring");
} finally {
FileUtils.deleteDirectory(new File("restore_meta"));
}
// purge 1 old backup
CountDownLatch updateBackupInfoByPurgingLatch = new CountDownLatch(1);
doAnswer(invocation -> {
byte[] backupInfoBytes = invocation.getArgument(1);
List<BackupInfo> backupInfoList = objectMapper.readValue(backupInfoBytes, new TypeReference<List<BackupInfo>>() {
});
assertEquals(backupInfoList.size(), 1);
assertEquals(backupInfoList.get(0), backupInfo2);
updateBackupInfoByPurgingLatch.countDown();
return null;
}).when(mockMetaStore).write(BACKUP_INFO_PATH, objectMapper.writeValueAsBytes(Collections.singletonList(backupInfo2)));
backupManager.purgeOldBackups(1);
assertTrue(updateBackupInfoByPurgingLatch.await(5L, TimeUnit.SECONDS));
// get backup info list and check
assertEquals(backupManager.getBackupInfoList().size(), 1);
// delete the remaining backup '2'
CountDownLatch updateBackupInfoByDeletionLatch = new CountDownLatch(1);
doAnswer(invocation -> {
byte[] backupInfoBytes = invocation.getArgument(1);
List<BackupInfo> backupInfoList = objectMapper.readValue(backupInfoBytes, new TypeReference<List<BackupInfo>>() {
});
assertTrue(backupInfoList.isEmpty());
updateBackupInfoByDeletionLatch.countDown();
return null;
}).when(mockMetaStore).write(BACKUP_INFO_PATH, objectMapper.writeValueAsBytes(new ArrayList<BackupInfo>()));
backupManager.deleteBackup(2);
assertTrue(updateBackupInfoByDeletionLatch.await(5L, TimeUnit.SECONDS));
// get backup info list and check
assertTrue(backupManager.getBackupInfoList().isEmpty());
backupManager.stop();
verify(mockSnapshotManager).removeListener(any());
}
Aggregations