This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
657 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apply from: "../gradle/shared.gradle" | ||
|
||
dependencies { | ||
implementation project(':atlasdb-config') | ||
implementation project(':timelock-api:timelock-api-objects') | ||
|
||
implementation 'com.palantir.safe-logging:safe-logging' | ||
|
||
testImplementation 'org.mockito:mockito-core' | ||
} |
87 changes: 87 additions & 0 deletions
87
atlasdb-backup/src/main/java/com/palantir/atlasdb/backup/AtlasBackupService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.atlasdb.backup; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.palantir.atlasdb.backup.api.AtlasBackupClientBlocking; | ||
import com.palantir.atlasdb.timelock.api.CompleteBackupRequest; | ||
import com.palantir.atlasdb.timelock.api.CompleteBackupResponse; | ||
import com.palantir.atlasdb.timelock.api.CompletedBackup; | ||
import com.palantir.atlasdb.timelock.api.InProgressBackupToken; | ||
import com.palantir.atlasdb.timelock.api.Namespace; | ||
import com.palantir.atlasdb.timelock.api.PrepareBackupRequest; | ||
import com.palantir.atlasdb.timelock.api.PrepareBackupResponse; | ||
import com.palantir.conjure.java.api.config.service.ServicesConfigBlock; | ||
import com.palantir.dialogue.clients.DialogueClients; | ||
import com.palantir.dialogue.clients.DialogueClients.ReloadingFactory; | ||
import com.palantir.refreshable.Refreshable; | ||
import com.palantir.tokens.auth.AuthHeader; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
public final class AtlasBackupService { | ||
private final AuthHeader authHeader; | ||
private final AtlasBackupClientBlocking atlasBackupClientBlocking; | ||
private final Map<Namespace, InProgressBackupToken> storedTokens; | ||
|
||
@VisibleForTesting | ||
AtlasBackupService(AuthHeader authHeader, AtlasBackupClientBlocking atlasBackupClientBlocking) { | ||
this.authHeader = authHeader; | ||
this.atlasBackupClientBlocking = atlasBackupClientBlocking; | ||
this.storedTokens = new ConcurrentHashMap<>(); | ||
} | ||
|
||
public static AtlasBackupService create( | ||
AuthHeader authHeader, Refreshable<ServicesConfigBlock> servicesConfigBlock, String serviceName) { | ||
ReloadingFactory reloadingFactory = DialogueClients.create(servicesConfigBlock); | ||
AtlasBackupClientBlocking atlasBackupClientBlocking = | ||
reloadingFactory.get(AtlasBackupClientBlocking.class, serviceName); | ||
return new AtlasBackupService(authHeader, atlasBackupClientBlocking); | ||
} | ||
|
||
public Set<Namespace> prepareBackup(Set<Namespace> namespaces) { | ||
PrepareBackupRequest request = PrepareBackupRequest.of(namespaces); | ||
PrepareBackupResponse response = atlasBackupClientBlocking.prepareBackup(authHeader, request); | ||
|
||
return response.getSuccessful().stream() | ||
.peek(this::storeBackupToken) | ||
.map(InProgressBackupToken::getNamespace) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private void storeBackupToken(InProgressBackupToken backupToken) { | ||
storedTokens.put(backupToken.getNamespace(), backupToken); | ||
} | ||
|
||
// TODO(gs): actually persist the token using a persister passed into this class. | ||
// Then we have an atlas-side implementation of the persister that conforms with the current backup story | ||
public Set<Namespace> completeBackup(Set<Namespace> namespaces) { | ||
Set<InProgressBackupToken> tokens = namespaces.stream() | ||
.map(storedTokens::remove) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toSet()); | ||
CompleteBackupRequest request = CompleteBackupRequest.of(tokens); | ||
CompleteBackupResponse response = atlasBackupClientBlocking.completeBackup(authHeader, request); | ||
|
||
return response.getSuccessfulBackups().stream() | ||
.map(CompletedBackup::getNamespace) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
atlasdb-backup/src/test/java/com/palantir/atlasdb/backup/AtlasBackupServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.atlasdb.backup; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import com.palantir.atlasdb.backup.api.AtlasBackupClientBlocking; | ||
import com.palantir.atlasdb.timelock.api.CompleteBackupRequest; | ||
import com.palantir.atlasdb.timelock.api.CompleteBackupResponse; | ||
import com.palantir.atlasdb.timelock.api.CompletedBackup; | ||
import com.palantir.atlasdb.timelock.api.InProgressBackupToken; | ||
import com.palantir.atlasdb.timelock.api.Namespace; | ||
import com.palantir.atlasdb.timelock.api.PrepareBackupRequest; | ||
import com.palantir.atlasdb.timelock.api.PrepareBackupResponse; | ||
import com.palantir.lock.v2.LockToken; | ||
import com.palantir.tokens.auth.AuthHeader; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class AtlasBackupServiceTest { | ||
private static final Namespace NAMESPACE = Namespace.of("foo"); | ||
private static final Namespace OTHER_NAMESPACE = Namespace.of("other"); | ||
private static final InProgressBackupToken IN_PROGRESS = inProgressBackupToken(NAMESPACE); | ||
|
||
@Mock | ||
private AuthHeader authHeader; | ||
|
||
@Mock | ||
private AtlasBackupClientBlocking atlasBackupClient; | ||
|
||
private AtlasBackupService atlasBackupService; | ||
|
||
@Before | ||
public void setup() { | ||
atlasBackupService = new AtlasBackupService(authHeader, atlasBackupClient); | ||
} | ||
|
||
@Test | ||
public void prepareBackupReturnsSuccessfulNamespaces() { | ||
when(atlasBackupClient.prepareBackup( | ||
authHeader, PrepareBackupRequest.of(ImmutableSet.of(NAMESPACE, OTHER_NAMESPACE)))) | ||
.thenReturn(PrepareBackupResponse.of(ImmutableSet.of(IN_PROGRESS))); | ||
|
||
assertThat(atlasBackupService.prepareBackup(ImmutableSet.of(NAMESPACE, OTHER_NAMESPACE))) | ||
.containsExactly(NAMESPACE); | ||
} | ||
|
||
@Test | ||
public void completeBackupDoesNotRunUnpreparedNamespaces() { | ||
when(atlasBackupClient.completeBackup(authHeader, CompleteBackupRequest.of(ImmutableSet.of()))) | ||
.thenReturn(CompleteBackupResponse.of(ImmutableSet.of())); | ||
|
||
assertThat(atlasBackupService.completeBackup(ImmutableSet.of(OTHER_NAMESPACE))) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void completeBackupReturnsSuccessfulNamespaces() { | ||
InProgressBackupToken otherInProgress = inProgressBackupToken(OTHER_NAMESPACE); | ||
Set<Namespace> namespaces = ImmutableSet.of(NAMESPACE, OTHER_NAMESPACE); | ||
|
||
when(atlasBackupClient.prepareBackup(authHeader, PrepareBackupRequest.of(namespaces))) | ||
.thenReturn(PrepareBackupResponse.of(ImmutableSet.of(IN_PROGRESS, otherInProgress))); | ||
|
||
CompletedBackup completedBackup = CompletedBackup.builder() | ||
.namespace(NAMESPACE) | ||
.backupStartTimestamp(2L) | ||
.backupEndTimestamp(3L) | ||
.build(); | ||
when(atlasBackupClient.completeBackup( | ||
authHeader, CompleteBackupRequest.of(ImmutableSet.of(IN_PROGRESS, otherInProgress)))) | ||
.thenReturn(CompleteBackupResponse.of(ImmutableSet.of(completedBackup))); | ||
|
||
atlasBackupService.prepareBackup(namespaces); | ||
|
||
assertThat(atlasBackupService.completeBackup(namespaces)).containsExactly(NAMESPACE); | ||
} | ||
|
||
private static InProgressBackupToken inProgressBackupToken(Namespace namespace) { | ||
return InProgressBackupToken.builder() | ||
.namespace(namespace) | ||
.immutableTimestamp(1L) | ||
.backupStartTimestamp(2L) | ||
.lockToken(LockToken.of(UUID.randomUUID())) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type: feature | ||
feature: | ||
description: Add AtlasBackupService, which encapsulates the steps of the backup | ||
process where communication with Timelock and/or knowledge of AtlasDB internals | ||
is required. | ||
links: | ||
- https://github.com/palantir/atlasdb/pull/5708 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.