Search in sources :

Example 1 with AtlasService

use of com.palantir.atlasdb.backup.api.AtlasService in project atlasdb by palantir.

the class AtlasBackupService method completeBackup.

/**
 * Completes backup for the given set of atlas services.
 * This will store metadata about the completed backup via the BackupPersister.
 * <p>
 * In order to do this, we must unlock the immutable timestamp for each service. If {@link #prepareBackup(Set)}
 * was not called, we will not have a record of the in-progress backup (and will not have been refreshing
 * its lock anyway). Thus, we attempt to complete backup only for those atlas services where we have the in-progress
 * backup stored.
 *
 * @return the atlas services whose backups were successfully completed
 */
public Set<AtlasService> completeBackup(Set<AtlasService> atlasServices) {
    throwIfClosed(atlasServices);
    AtlasServices.throwIfAtlasServicesCollide(atlasServices);
    Map<AtlasService, InProgressBackupToken> knownBackups = KeyedStream.of(atlasServices).map((Function<AtlasService, InProgressBackupToken>) inProgressBackups::remove).filter(Objects::nonNull).collectToMap();
    Set<InProgressBackupToken> tokens = ImmutableSet.copyOf(knownBackups.values());
    if (tokens.isEmpty()) {
        log.error("Complete backup called, but no in progress backups were found.", SafeArg.of("atlasServices", atlasServices));
        return ImmutableSet.of();
    }
    lockRefresher.unregisterLocks(tokens);
    Set<AtlasService> atlasServicesWithInProgressBackups = knownBackups.keySet();
    if (tokens.size() < atlasServices.size()) {
        Set<AtlasService> atlasServicesWithNoInProgressBackup = Sets.difference(atlasServices, atlasServicesWithInProgressBackups);
        log.error("In progress backups were not found for some atlasServices. We will not complete backup for these.", SafeArg.of("numAtlasServicesWithBackup", tokens.size()), SafeArg.of("numAtlasServicesWithoutBackup", atlasServicesWithNoInProgressBackup.size()), SafeArg.of("atlasServicesWithoutBackup", atlasServicesWithNoInProgressBackup), SafeArg.of("allRequestedAtlasServices", atlasServices));
    }
    Map<Namespace, AtlasService> namespaceToServices = KeyedStream.of(atlasServices).mapKeys(AtlasService::getNamespace).collectToMap();
    CompleteBackupRequest request = CompleteBackupRequest.of(tokens);
    CompleteBackupResponse response = atlasBackupClient.completeBackup(authHeader, request);
    Map<AtlasService, CompletedBackup> successfulBackups = KeyedStream.of(response.getSuccessfulBackups()).mapKeys(token -> namespaceToServices.get(token.getNamespace())).collectToMap();
    Set<AtlasService> successfullyStoredBackups = storeCompletedBackups(successfulBackups);
    if (successfullyStoredBackups.size() < atlasServicesWithInProgressBackups.size()) {
        Set<AtlasService> failedAtlasServices = Sets.difference(atlasServicesWithInProgressBackups, successfullyStoredBackups);
        log.error("Backup did not complete successfully for all atlasServices. Check TimeLock logs to debug.", SafeArg.of("failedAtlasServices", failedAtlasServices), SafeArg.of("successfulAtlasServices", successfullyStoredBackups), SafeArg.of("atlasServicesWithoutBackup", atlasServicesWithInProgressBackups));
    }
    return successfulBackups.keySet();
}
Also used : PrepareBackupResponse(com.palantir.atlasdb.backup.api.PrepareBackupResponse) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) SafeLoggerFactory(com.palantir.logsafe.logger.SafeLoggerFactory) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AtlasBackupClient(com.palantir.atlasdb.backup.api.AtlasBackupClient) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) AtlasBackupClientBlocking(com.palantir.atlasdb.backup.api.AtlasBackupClientBlocking) AuthHeader(com.palantir.tokens.auth.AuthHeader) CompletedBackup(com.palantir.atlasdb.backup.api.CompletedBackup) Function(java.util.function.Function) SafeLogger(com.palantir.logsafe.logger.SafeLogger) SafeArg(com.palantir.logsafe.SafeArg) CompleteBackupResponse(com.palantir.atlasdb.backup.api.CompleteBackupResponse) PTExecutors(com.palantir.common.concurrent.PTExecutors) UserAgent(com.palantir.conjure.java.api.config.service.UserAgent) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtlasFutures(com.palantir.atlasdb.futures.AtlasFutures) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Refreshable(com.palantir.refreshable.Refreshable) ImmutableSet(com.google.common.collect.ImmutableSet) NamedThreadFactory(com.palantir.common.concurrent.NamedThreadFactory) CompleteBackupRequest(com.palantir.atlasdb.backup.api.CompleteBackupRequest) KeyedStream(com.palantir.common.streams.KeyedStream) PrepareBackupRequest(com.palantir.atlasdb.backup.api.PrepareBackupRequest) ReloadingFactory(com.palantir.dialogue.clients.DialogueClients.ReloadingFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) InProgressBackupToken(com.palantir.atlasdb.backup.api.InProgressBackupToken) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) Namespace(com.palantir.atlasdb.timelock.api.Namespace) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) RefreshBackupRequest(com.palantir.atlasdb.backup.api.RefreshBackupRequest) ServicesConfigBlock(com.palantir.conjure.java.api.config.service.ServicesConfigBlock) Futures(com.google.common.util.concurrent.Futures) LockLeaseRefresher(com.palantir.lock.v2.LockLeaseRefresher) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) AtlasDbRemotingConstants(com.palantir.atlasdb.http.AtlasDbRemotingConstants) Closeable(java.io.Closeable) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AtlasService(com.palantir.atlasdb.backup.api.AtlasService) DialogueClients(com.palantir.dialogue.clients.DialogueClients) LockRefresher(com.palantir.lock.client.LockRefresher) CompleteBackupResponse(com.palantir.atlasdb.backup.api.CompleteBackupResponse) AtlasService(com.palantir.atlasdb.backup.api.AtlasService) CompleteBackupRequest(com.palantir.atlasdb.backup.api.CompleteBackupRequest) CompletedBackup(com.palantir.atlasdb.backup.api.CompletedBackup) InProgressBackupToken(com.palantir.atlasdb.backup.api.InProgressBackupToken) Namespace(com.palantir.atlasdb.timelock.api.Namespace)

Example 2 with AtlasService

use of com.palantir.atlasdb.backup.api.AtlasService in project atlasdb by palantir.

the class AtlasBackupService method storeCompletedBackups.

private Set<AtlasService> storeCompletedBackups(Map<AtlasService, CompletedBackup> successfulBackups) {
    ExecutorService executorService = PTExecutors.newFixedThreadPool(completeBackupNumThreads);
    Map<AtlasService, ListenableFuture<Optional<Boolean>>> storedBackups = KeyedStream.stream(successfulBackups).map((atlasService, completeBackup) -> Futures.submit(() -> Optional.of(storeCompletedBackup(atlasService, completeBackup)), executorService)).collectToMap();
    ListenableFuture<Map<AtlasService, Boolean>> allStorageTasks = AtlasFutures.allAsMap(storedBackups, MoreExecutors.directExecutor());
    // Waits for future to complete
    Map<AtlasService, Boolean> storageTaskResults = AtlasFutures.getUnchecked(allStorageTasks);
    executorService.shutdown();
    return KeyedStream.stream(storageTaskResults).filter(Boolean::booleanValue).keys().collect(Collectors.toSet());
}
Also used : PrepareBackupResponse(com.palantir.atlasdb.backup.api.PrepareBackupResponse) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) SafeLoggerFactory(com.palantir.logsafe.logger.SafeLoggerFactory) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AtlasBackupClient(com.palantir.atlasdb.backup.api.AtlasBackupClient) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) AtlasBackupClientBlocking(com.palantir.atlasdb.backup.api.AtlasBackupClientBlocking) AuthHeader(com.palantir.tokens.auth.AuthHeader) CompletedBackup(com.palantir.atlasdb.backup.api.CompletedBackup) Function(java.util.function.Function) SafeLogger(com.palantir.logsafe.logger.SafeLogger) SafeArg(com.palantir.logsafe.SafeArg) CompleteBackupResponse(com.palantir.atlasdb.backup.api.CompleteBackupResponse) PTExecutors(com.palantir.common.concurrent.PTExecutors) UserAgent(com.palantir.conjure.java.api.config.service.UserAgent) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtlasFutures(com.palantir.atlasdb.futures.AtlasFutures) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Refreshable(com.palantir.refreshable.Refreshable) ImmutableSet(com.google.common.collect.ImmutableSet) NamedThreadFactory(com.palantir.common.concurrent.NamedThreadFactory) CompleteBackupRequest(com.palantir.atlasdb.backup.api.CompleteBackupRequest) KeyedStream(com.palantir.common.streams.KeyedStream) PrepareBackupRequest(com.palantir.atlasdb.backup.api.PrepareBackupRequest) ReloadingFactory(com.palantir.dialogue.clients.DialogueClients.ReloadingFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) InProgressBackupToken(com.palantir.atlasdb.backup.api.InProgressBackupToken) TransactionManager(com.palantir.atlasdb.transaction.api.TransactionManager) Namespace(com.palantir.atlasdb.timelock.api.Namespace) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) RefreshBackupRequest(com.palantir.atlasdb.backup.api.RefreshBackupRequest) ServicesConfigBlock(com.palantir.conjure.java.api.config.service.ServicesConfigBlock) Futures(com.google.common.util.concurrent.Futures) LockLeaseRefresher(com.palantir.lock.v2.LockLeaseRefresher) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) AtlasDbRemotingConstants(com.palantir.atlasdb.http.AtlasDbRemotingConstants) Closeable(java.io.Closeable) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AtlasService(com.palantir.atlasdb.backup.api.AtlasService) DialogueClients(com.palantir.dialogue.clients.DialogueClients) LockRefresher(com.palantir.lock.client.LockRefresher) AtlasService(com.palantir.atlasdb.backup.api.AtlasService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with AtlasService

use of com.palantir.atlasdb.backup.api.AtlasService in project atlasdb by palantir.

the class AtlasRestoreService method prepareRestore.

/**
 * Disables TimeLock on all nodes for the given atlasServices.
 * This will fail if any atlasService is already disabled, unless it was disabled with the provided backupId.
 * AtlasServices for which we don't have a recorded backup will be ignored.
 *
 * @param restoreRequests the requests to prepare.
 * @param backupId a unique identifier for this request (uniquely identifies the backup to which we're restoring)
 *
 * @return the atlasServices successfully disabled.
 */
public Set<AtlasService> prepareRestore(Set<RestoreRequest> restoreRequests, String backupId) {
    validateRestoreRequests(restoreRequests);
    Map<RestoreRequest, CompletedBackup> completedBackups = getCompletedBackups(restoreRequests);
    Set<AtlasService> atlasServicesToRestore = getAtlasServicesToRestore(completedBackups);
    Preconditions.checkArgument(atlasServicesToRestore.size() == completedBackups.size(), "Attempting to restore multiple atlasServices into the same atlasService! " + "This will cause severe data corruption.", SafeArg.of("restoreRequests", restoreRequests));
    Set<Namespace> namespacesToRestore = atlasServicesToRestore.stream().map(AtlasService::getNamespace).collect(Collectors.toSet());
    DisableNamespacesRequest request = DisableNamespacesRequest.of(namespacesToRestore, backupId);
    DisableNamespacesResponse response = timeLockManagementService.disableTimelock(authHeader, request);
    return response.accept(new DisableNamespacesResponse.Visitor<>() {

        @Override
        public Set<AtlasService> visitSuccessful(SuccessfulDisableNamespacesResponse value) {
            return atlasServicesToRestore;
        }

        @Override
        public Set<AtlasService> visitUnsuccessful(UnsuccessfulDisableNamespacesResponse value) {
            log.error("Failed to disable namespaces prior to restore", SafeArg.of("requests", restoreRequests), SafeArg.of("response", value));
            return ImmutableSet.of();
        }

        @Override
        public Set<AtlasService> visitUnknown(String unknownType) {
            throw new SafeIllegalStateException("Unknown DisableNamespacesResponse", SafeArg.of("unknownType", unknownType));
        }
    });
}
Also used : AtlasService(com.palantir.atlasdb.backup.api.AtlasService) SuccessfulDisableNamespacesResponse(com.palantir.atlasdb.timelock.api.SuccessfulDisableNamespacesResponse) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Namespace(com.palantir.atlasdb.timelock.api.Namespace) UnsuccessfulDisableNamespacesResponse(com.palantir.atlasdb.timelock.api.UnsuccessfulDisableNamespacesResponse) SuccessfulDisableNamespacesResponse(com.palantir.atlasdb.timelock.api.SuccessfulDisableNamespacesResponse) UnsuccessfulDisableNamespacesResponse(com.palantir.atlasdb.timelock.api.UnsuccessfulDisableNamespacesResponse) DisableNamespacesResponse(com.palantir.atlasdb.timelock.api.DisableNamespacesResponse) CompletedBackup(com.palantir.atlasdb.backup.api.CompletedBackup) CompleteRestoreRequest(com.palantir.atlasdb.backup.api.CompleteRestoreRequest) SafeIllegalStateException(com.palantir.logsafe.exceptions.SafeIllegalStateException) DisableNamespacesRequest(com.palantir.atlasdb.timelock.api.DisableNamespacesRequest)

Example 4 with AtlasService

use of com.palantir.atlasdb.backup.api.AtlasService in project atlasdb by palantir.

the class AtlasRestoreService method repairTransactionsTables.

private void repairTransactionsTables(RestoreRequest restoreRequest, CompletedBackup completedBackup, BiConsumer<String, RangesForRepair> repairTable) {
    Map<FullyBoundedTimestampRange, Integer> coordinationMap = getCoordinationMap(restoreRequest.oldAtlasService(), completedBackup);
    List<TransactionsTableInteraction> transactionsTableInteractions = TransactionsTableInteraction.getTransactionTableInteractions(coordinationMap, DefaultRetryPolicy.INSTANCE);
    AtlasService atlasService = restoreRequest.newAtlasService();
    cassandraRepairHelper.repairTransactionsTables(atlasService, transactionsTableInteractions, repairTable);
    cassandraRepairHelper.cleanTransactionsTables(atlasService, completedBackup.getBackupStartTimestamp(), transactionsTableInteractions);
}
Also used : FullyBoundedTimestampRange(com.palantir.timestamp.FullyBoundedTimestampRange) AtlasService(com.palantir.atlasdb.backup.api.AtlasService) TransactionsTableInteraction(com.palantir.atlasdb.cassandra.backup.transaction.TransactionsTableInteraction)

Example 5 with AtlasService

use of com.palantir.atlasdb.backup.api.AtlasService in project atlasdb by palantir.

the class AtlasBackupServiceTest method prepareBackupThrowsIfNamespacesCollide.

@Test
public void prepareBackupThrowsIfNamespacesCollide() {
    AtlasService collidingAtlasService = AtlasService.of(ServiceId.of("c"), ATLAS_SERVICE.getNamespace());
    assertThatLoggableExceptionThrownBy(() -> atlasBackupService.prepareBackup(ImmutableSet.of(ATLAS_SERVICE, collidingAtlasService))).isInstanceOf(SafeIllegalArgumentException.class).hasMessageContaining("Duplicated namespaces");
}
Also used : AtlasService(com.palantir.atlasdb.backup.api.AtlasService) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) Test(org.junit.Test)

Aggregations

AtlasService (com.palantir.atlasdb.backup.api.AtlasService)23 Test (org.junit.Test)16 Namespace (com.palantir.atlasdb.timelock.api.Namespace)13 CompletedBackup (com.palantir.atlasdb.backup.api.CompletedBackup)11 CompleteRestoreRequest (com.palantir.atlasdb.backup.api.CompleteRestoreRequest)9 ImmutableSet (com.google.common.collect.ImmutableSet)8 Optional (java.util.Optional)8 Set (java.util.Set)8 KeyedStream (com.palantir.common.streams.KeyedStream)6 SafeIllegalArgumentException (com.palantir.logsafe.exceptions.SafeIllegalArgumentException)6 AuthHeader (com.palantir.tokens.auth.AuthHeader)6 Map (java.util.Map)6 DisableNamespacesRequest (com.palantir.atlasdb.timelock.api.DisableNamespacesRequest)5 DisableNamespacesResponse (com.palantir.atlasdb.timelock.api.DisableNamespacesResponse)5 SuccessfulDisableNamespacesResponse (com.palantir.atlasdb.timelock.api.SuccessfulDisableNamespacesResponse)5 UnsuccessfulDisableNamespacesResponse (com.palantir.atlasdb.timelock.api.UnsuccessfulDisableNamespacesResponse)5 SafeIllegalStateException (com.palantir.logsafe.exceptions.SafeIllegalStateException)5 Refreshable (com.palantir.refreshable.Refreshable)5 Function (java.util.function.Function)5 Collectors (java.util.stream.Collectors)5