/// <summary> /// Iterate over all the provided strategies trying to perform a successful backup. /// Will also do consistency checks if specified in <seealso cref="OnlineBackupContext"/> /// </summary> /// <param name="onlineBackupContext"> filesystem, command arguments and configuration </param> /// <exception cref="CommandFailed"> when backup failed or there were issues with consistency checks </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: public void performBackup(OnlineBackupContext onlineBackupContext) throws org.neo4j.commandline.admin.CommandFailed public virtual void PerformBackup(OnlineBackupContext onlineBackupContext) { // Convenience OnlineBackupRequiredArguments requiredArgs = onlineBackupContext.RequiredArguments; Path destination = onlineBackupContext.ResolvedLocationFromName; ConsistencyFlags consistencyFlags = onlineBackupContext.ConsistencyFlags; Fallible <BackupStrategyOutcome> throwableWithState = null; IList <Exception> causesOfFailure = new List <Exception>(); foreach (BackupStrategyWrapper backupStrategy in _strategies) { throwableWithState = backupStrategy.DoBackup(onlineBackupContext); if (throwableWithState.State == BackupStrategyOutcome.Success) { break; } if (throwableWithState.State == BackupStrategyOutcome.CorrectStrategyFailed) { throw CommandFailedWithCause(throwableWithState).get(); } throwableWithState.Cause.ifPresent(causesOfFailure.add); } if (throwableWithState == null || !BackupStrategyOutcome.Success.Equals(throwableWithState.State)) { CommandFailed commandFailed = new CommandFailed("Failed to run a backup using the available strategies."); causesOfFailure.ForEach(commandFailed.addSuppressed); throw commandFailed; } if (requiredArgs.DoConsistencyCheck) { PerformConsistencyCheck(onlineBackupContext.Config, requiredArgs, consistencyFlags, DatabaseLayout.of(destination.toFile())); } }
/// <summary> /// This will perform a full backup with some directory renaming if necessary. /// <para> /// If there is no existing backup, then no renaming will occur. /// Otherwise the full backup will be done into a temporary directory and renaming /// will occur if everything was successful. /// </para> /// </summary> /// <param name="onlineBackupContext"> command line arguments, config etc. </param> /// <returns> outcome of full backup </returns> private Fallible <BackupStageOutcome> FullBackupWithTemporaryFolderResolutions(OnlineBackupContext onlineBackupContext) { Path userSpecifiedBackupLocation = onlineBackupContext.ResolvedLocationFromName; Path temporaryFullBackupLocation = _backupCopyService.findAnAvailableLocationForNewFullBackup(userSpecifiedBackupLocation); OptionalHostnamePort address = onlineBackupContext.RequiredArguments.Address; Fallible <BackupStageOutcome> state = _backupStrategy.performFullBackup(DatabaseLayout.of(temporaryFullBackupLocation.toFile()), _config, address); // NOTE temporaryFullBackupLocation can be equal to desired bool backupWasMadeToATemporaryLocation = !userSpecifiedBackupLocation.Equals(temporaryFullBackupLocation); if (BackupStageOutcome.Success.Equals(state.State)) { _backupRecoveryService.recoverWithDatabase(temporaryFullBackupLocation, _pageCache, _config); if (backupWasMadeToATemporaryLocation) { try { RenameTemporaryBackupToExpected(temporaryFullBackupLocation, userSpecifiedBackupLocation); } catch (IOException e) { return(new Fallible <BackupStageOutcome>(BackupStageOutcome.UnrecoverableFailure, e)); } } ClearIdFiles(userSpecifiedBackupLocation); } return(state); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void failureDuringMoveCausesAbsoluteFailure() throws java.io.IOException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void FailureDuringMoveCausesAbsoluteFailure() { // given moves fail doThrow(typeof(IOException)).when(_backupCopyService).moveBackupLocation(any(), any()); // and fallback to full _requiredArguments = _requiredArguments(true); _onlineBackupContext = new OnlineBackupContext(_requiredArguments, _config, ConsistencyFlags()); // and backup exists when(_backupCopyService.backupExists(any())).thenReturn(true); // and incremental fails when(_backupStrategyImplementation.performIncrementalBackup(any(), any(), any())).thenReturn(new Fallible <>(BackupStageOutcome.Failure, null)); // and full passes when(_backupStrategyImplementation.performFullBackup(any(), any(), any())).thenReturn(new Fallible <>(BackupStageOutcome.Success, null)); // when Fallible <BackupStrategyOutcome> state = _subject.doBackup(_onlineBackupContext); // then result was catastrophic and contained reason assertEquals(BackupStrategyOutcome.AbsoluteFailure, state.State); assertEquals(typeof(IOException), state.Cause.get().GetType()); // and full backup was definitely performed verify(_backupStrategyImplementation).performFullBackup(any(), any(), any()); }
private static System.Func <CommandFailed> CommandFailedWithCause(Fallible <BackupStrategyOutcome> cause) { if (cause.Cause.Present) { return(() => new CommandFailed("Execution of backup failed", cause.Cause.get())); } return(() => new CommandFailed("Execution of backup failed")); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void incrementalBackupsAreDoneAgainstResolvedAddress() public virtual void IncrementalBackupsAreDoneAgainstResolvedAddress() { // when Fallible <BackupStageOutcome> state = Subject.performIncrementalBackup(Backuplayout, Config, UserSpecifiedHostname); // then verify(_backupProtocolService).doIncrementalBackup(eq(HostnamePort.Host), eq(HostnamePort.Port), any(), eq(ConsistencyCheck.NONE), anyLong(), any()); assertEquals(SUCCESS, state.State); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void fullBackupUsesResolvedAddress() public virtual void FullBackupUsesResolvedAddress() { // when Fallible state = Subject.performFullBackup(Backuplayout, Config, UserSpecifiedHostname); // then verify(_backupProtocolService).doFullBackup(any(), anyInt(), any(), eq(ConsistencyCheck.NONE), any(), anyLong(), anyBoolean()); assertEquals(BackupStageOutcome.Success, state.State); }
/// <summary> /// Try to do a backup using the given strategy (ex. BackupProtocol). This covers all stages (starting with incremental and falling back to a a full backup). /// The end result of this method will either be a successful backup or any other return type with the reason why the backup wasn't successful /// </summary> /// <param name="onlineBackupContext"> the command line arguments, configuration, flags </param> /// <returns> the ultimate outcome of trying to do a backup with the given strategy </returns> internal virtual Fallible <BackupStrategyOutcome> DoBackup(OnlineBackupContext onlineBackupContext) { LifeSupport lifeSupport = new LifeSupport(); lifeSupport.Add(_backupStrategy); lifeSupport.Start(); Fallible <BackupStrategyOutcome> state = PerformBackupWithoutLifecycle(onlineBackupContext); lifeSupport.Shutdown(); return(state); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void fullBackupFailsWhenTargetHasStoreId() throws java.io.IOException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void FullBackupFailsWhenTargetHasStoreId() { // given when(StoreFiles.readStoreId(any())).thenReturn(ExpectedStoreId); // when Fallible <BackupStageOutcome> state = Subject.performFullBackup(DesiredBackupLayout, Config, UserProvidedAddress); // then assertEquals(typeof(StoreIdDownloadFailedException), state.Cause.get().GetType()); assertEquals(BackupStageOutcome.Failure, state.State); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void exceptionWhenStoreMismatch() throws java.io.IOException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ExceptionWhenStoreMismatch() { // given when(StoreFiles.readStoreId(any())).thenReturn(new StoreId(5, 4, 3, 2)); // when Fallible <BackupStageOutcome> state = Subject.performIncrementalBackup(DesiredBackupLayout, Config, UserProvidedAddress); // then assertEquals(typeof(StoreIdDownloadFailedException), state.Cause.get().GetType()); assertEquals(BackupStageOutcome.Failure, state.State); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void failingToCopyStoresCausesFailWithStatus_incrementalBackup() throws org.neo4j.causalclustering.catchup.storecopy.StoreIdDownloadFailedException, org.neo4j.causalclustering.catchup.storecopy.StoreCopyFailedException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void FailingToCopyStoresCausesFailWithStatusIncrementalBackup() { // given when(BackupDelegator.tryCatchingUp(any(), eq(ExpectedStoreId), any())).thenThrow(typeof(StoreCopyFailedException)); // when Fallible state = Subject.performIncrementalBackup(DesiredBackupLayout, Config, UserProvidedAddress); // then assertEquals(BackupStageOutcome.Failure, state.State); assertEquals(typeof(StoreCopyFailedException), state.Cause.get().GetType()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void fullBackupFailsWithCauseOnException() public virtual void FullBackupFailsWithCauseOnException() { // given full backup fails with a protocol/network exception when(_backupProtocolService.doFullBackup(any(), anyInt(), any(), any(), any(), anyLong(), anyBoolean())).thenThrow(typeof(ComException)); // when Fallible state = Subject.performFullBackup(Backuplayout, Config, UserSpecifiedHostname); // then assertEquals(BackupStageOutcome.WrongProtocol, state.State); assertEquals(typeof(ComException), state.Cause.get().GetType()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void incrementalBackupsEndingInUnacceptedCatchupStateCauseFailures() throws org.neo4j.causalclustering.catchup.storecopy.StoreCopyFailedException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void IncrementalBackupsEndingInUnacceptedCatchupStateCauseFailures() { // given when(BackupDelegator.tryCatchingUp(any(), any(), any())).thenReturn(CatchupResult.E_STORE_UNAVAILABLE); // when Fallible <BackupStageOutcome> state = Subject.performIncrementalBackup(DesiredBackupLayout, Config, UserProvidedAddress); // then assertEquals(BackupStageOutcome.Failure, state.State); assertEquals(typeof(StoreCopyFailedException), state.Cause.get().GetType()); assertEquals("End state of catchup was not a successful end of stream", state.Cause.get().Message); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void failingToRetrieveStoreIdCausesFailWithStatus_fullBackup() throws org.neo4j.causalclustering.catchup.storecopy.StoreIdDownloadFailedException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void FailingToRetrieveStoreIdCausesFailWithStatusFullBackup() { // given StoreIdDownloadFailedException storeIdDownloadFailedException = new StoreIdDownloadFailedException("Expected description"); when(BackupDelegator.fetchStoreId(any())).thenThrow(storeIdDownloadFailedException); // when Fallible state = Subject.performFullBackup(DesiredBackupLayout, Config, UserProvidedAddress); // then assertEquals(BackupStageOutcome.WrongProtocol, state.State); assertEquals(storeIdDownloadFailedException, state.Cause.get()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void exceptionsDuringIncrementalBackupAreMarkedAsFailedBackups() public virtual void ExceptionsDuringIncrementalBackupAreMarkedAsFailedBackups() { // given incremental backup will fail IncrementalBackupNotPossibleException expectedException = new IncrementalBackupNotPossibleException("Expected test message", new Exception("Expected cause")); when(_backupProtocolService.doIncrementalBackup(any(), anyInt(), any(), eq(ConsistencyCheck.NONE), anyLong(), any())).thenThrow(expectedException); // when Fallible state = Subject.performIncrementalBackup(Backuplayout, Config, UserSpecifiedHostname); // then assertEquals(BackupStageOutcome.Failure, state.State); assertEquals(expectedException, state.Cause.get()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void failingToCopyStoresCausesFailWithStatus_fullBackup() throws org.neo4j.causalclustering.catchup.storecopy.StoreCopyFailedException, java.io.IOException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void FailingToCopyStoresCausesFailWithStatusFullBackup() { // given doThrow(typeof(StoreCopyFailedException)).when(BackupDelegator).copy(any(), any(), any()); // and when(StoreFiles.readStoreId(any())).thenThrow(typeof(IOException)); // when Fallible state = Subject.performFullBackup(DesiredBackupLayout, Config, UserProvidedAddress); // then assertEquals(BackupStageOutcome.Failure, state.State); Console.WriteLine(state.Cause); assertEquals(typeof(StoreCopyFailedException), state.Cause.get().GetType()); }
private Fallible <BackupStrategyOutcome> PerformBackupWithoutLifecycle(OnlineBackupContext onlineBackupContext) { Path backupLocation = onlineBackupContext.ResolvedLocationFromName; OptionalHostnamePort userSpecifiedAddress = onlineBackupContext.RequiredArguments.Address; _log.debug("User specified address is %s:%s", userSpecifiedAddress.Hostname.ToString(), userSpecifiedAddress.Port.ToString()); Config config = onlineBackupContext.Config; bool previousBackupExists = _backupCopyService.backupExists(DatabaseLayout.of(backupLocation.toFile())); if (previousBackupExists) { _log.info("Previous backup found, trying incremental backup."); Fallible <BackupStageOutcome> state = _backupStrategy.performIncrementalBackup(DatabaseLayout.of(backupLocation.toFile()), config, userSpecifiedAddress); bool fullBackupWontWork = BackupStageOutcome.WrongProtocol.Equals(state.State); bool incrementalWasSuccessful = BackupStageOutcome.Success.Equals(state.State); if (incrementalWasSuccessful) { _backupRecoveryService.recoverWithDatabase(backupLocation, _pageCache, config); } if (fullBackupWontWork || incrementalWasSuccessful) { ClearIdFiles(backupLocation); return(DescribeOutcome(state)); } if (!onlineBackupContext.RequiredArguments.FallbackToFull) { return(DescribeOutcome(state)); } } if (onlineBackupContext.RequiredArguments.FallbackToFull) { if (!previousBackupExists) { _log.info("Previous backup not found, a new full backup will be performed."); } return(DescribeOutcome(FullBackupWithTemporaryFolderResolutions(onlineBackupContext))); } return(new Fallible <BackupStrategyOutcome>(BackupStrategyOutcome.IncorrectStrategy, null)); }
private static Fallible <BackupStrategyOutcome> DescribeOutcome(Fallible <BackupStageOutcome> strategyStageOutcome) { BackupStageOutcome stageOutcome = strategyStageOutcome.State; if (stageOutcome == BackupStageOutcome.Success) { return(new Fallible <BackupStrategyOutcome>(BackupStrategyOutcome.Success, null)); } if (stageOutcome == BackupStageOutcome.WrongProtocol) { return(new Fallible <BackupStrategyOutcome>(BackupStrategyOutcome.IncorrectStrategy, strategyStageOutcome.Cause.orElse(null))); } if (stageOutcome == BackupStageOutcome.Failure) { return(new Fallible <BackupStrategyOutcome>(BackupStrategyOutcome.CorrectStrategyFailed, strategyStageOutcome.Cause.orElse(null))); } if (stageOutcome == BackupStageOutcome.UnrecoverableFailure) { return(new Fallible <BackupStrategyOutcome>(BackupStrategyOutcome.AbsoluteFailure, strategyStageOutcome.Cause.orElse(null))); } throw new Exception("Not all enums covered: " + stageOutcome); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void successfulFullBackupsMoveExistingBackup() throws java.io.IOException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void SuccessfulFullBackupsMoveExistingBackup() { // given backup exists _desiredBackupLayout = TestDirectory.databaseLayout("some-preexisting-backup"); when(_backupCopyService.backupExists(_desiredBackupLayout)).thenReturn(true); // and fallback to full flag has been set _requiredArguments = _requiredArguments(true); _onlineBackupContext = new OnlineBackupContext(_requiredArguments, _config, ConsistencyFlags()); // and a new location for the existing backup is found Path newLocationForExistingBackup = TestDirectory.directory("new-backup-location").toPath(); when(_backupCopyService.findNewBackupLocationForBrokenExisting(_desiredBackupLayout.databaseDirectory().toPath())).thenReturn(newLocationForExistingBackup); // and there is a generated location for where to store a new full backup so the original is not destroyed Path temporaryFullBackupLocation = TestDirectory.directory("temporary-full-backup").toPath(); when(_backupCopyService.findAnAvailableLocationForNewFullBackup(_desiredBackupLayout.databaseDirectory().toPath())).thenReturn(temporaryFullBackupLocation); // and incremental fails when(_backupStrategyImplementation.performIncrementalBackup(any(), any(), any())).thenReturn(new Fallible <>(BackupStageOutcome.Failure, null)); // and full passes when(_backupStrategyImplementation.performFullBackup(any(), any(), any())).thenReturn(new Fallible <>(BackupStageOutcome.Success, null)); // when Fallible <BackupStrategyOutcome> state = _subject.doBackup(_onlineBackupContext); // then original existing backup is moved to err directory verify(_backupCopyService).moveBackupLocation(eq(_desiredBackupLayout.databaseDirectory().toPath()), eq(newLocationForExistingBackup)); // and new successful backup is renamed to original expected name verify(_backupCopyService).moveBackupLocation(eq(temporaryFullBackupLocation), eq(_desiredBackupLayout.databaseDirectory().toPath())); // and backup was successful assertEquals(BackupStrategyOutcome.Success, state.State); }