-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HDDS-1786 : Datanodes takeSnapshot should delete previously created s… #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
48fc5d4
HDDS-1786 : Datanodes takeSnapshot should delete previously created s…
ba07c33
HDDS-1786 : Datanodes takeSnapshot should delete previously created s…
bff7703
Merge remote-tracking branch 'upstream/trunk' into HDDS-1786-trunk
77ae6a2
Merge remote-tracking branch 'upstream/trunk' into HDDS-1786-trunk
b3de768
Updated to use configurable Snapshot retention policy from RATIS-543.
35cfe19
Remove added newline.
80a9fb8
Added unit test.
fc6a3eb
Merge remote-tracking branch 'upstream/trunk' into HDDS-1786-trunk
0ae12ba
Fix rat check failure.
4464d43
Fix unit test edge case.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
112 changes: 112 additions & 0 deletions
112
...pache/hadoop/ozone/container/common/transport/server/ratis/TestContainerStateMachine.java
This file contains hidden or 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,112 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.hadoop.ozone.container.common.transport.server.ratis; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.lang.reflect.Field; | ||
| import java.nio.file.Paths; | ||
|
|
||
| import org.apache.ratis.server.protocol.TermIndex; | ||
| import org.apache.ratis.server.storage.FileInfo; | ||
| import org.apache.ratis.statemachine.impl.SimpleStateMachineStorage; | ||
| import org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo; | ||
| import org.junit.Assert; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
|
|
||
| /** | ||
| * Unit test methods of ContainerStateMachine. | ||
| */ | ||
| public class TestContainerStateMachine { | ||
|
|
||
| @Rule | ||
| public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
|
||
| @Test | ||
| public void testDeleteOldSnapshot() throws Exception { | ||
|
|
||
| File firstSnapshotFile = temporaryFolder.newFile(); | ||
| File secondSnapshotFile = temporaryFolder.newFile(); | ||
| File thirdSnapshotFile = temporaryFolder.newFile(); | ||
|
|
||
| FileInfo fileInfoMock = mock(FileInfo.class); | ||
| when(fileInfoMock.getPath()).thenReturn(firstSnapshotFile.toPath()) | ||
| .thenReturn(secondSnapshotFile.toPath()); | ||
|
|
||
| SingleFileSnapshotInfo singleFileSnapshotInfoMock = mock( | ||
| SingleFileSnapshotInfo.class); | ||
| when(singleFileSnapshotInfoMock.getFile()).thenReturn(fileInfoMock); | ||
|
|
||
| TermIndex termIndexMock = mock(TermIndex.class); | ||
| when(termIndexMock.getIndex()).thenReturn(1L); | ||
| when(termIndexMock.getTerm()).thenReturn(1L); | ||
|
|
||
| SimpleStateMachineStorage simpleStateMachineStorageMock = | ||
| mock(SimpleStateMachineStorage.class); | ||
| when(simpleStateMachineStorageMock.findLatestSnapshot()) | ||
| .thenReturn(singleFileSnapshotInfoMock); | ||
| when(simpleStateMachineStorageMock.getSnapshotFile(1L, 1L)) | ||
| .thenReturn(secondSnapshotFile) | ||
| .thenReturn(thirdSnapshotFile) | ||
| //Return non-existent file while taking 3rd snapshot. | ||
| .thenReturn(Paths.get("NonExistentDir", "NonExistentFile") | ||
| .toFile()); | ||
|
|
||
| ContainerStateMachine containerStateMachine = | ||
| mock(ContainerStateMachine.class); | ||
| when(containerStateMachine.getLastAppliedTermIndex()).thenReturn( | ||
| termIndexMock); | ||
| when(containerStateMachine.takeSnapshot()).thenCallRealMethod(); | ||
|
|
||
| // Have to use reflections here since storage is baked into | ||
| // ContainerStateMachine class. | ||
| Field f1 = containerStateMachine.getClass().getSuperclass() | ||
| .getDeclaredField("storage"); | ||
| f1.setAccessible(true); | ||
| f1.set(containerStateMachine, simpleStateMachineStorageMock); | ||
|
|
||
| // Verify last snapshot deletion while calling takeSnapshot() API. | ||
| assertTrue(firstSnapshotFile.exists()); | ||
| containerStateMachine.takeSnapshot(); | ||
| assertFalse(firstSnapshotFile.exists()); | ||
|
|
||
| // Verify current snapshot deletion while calling takeSnapshot() API once | ||
| // more. | ||
| assertTrue(secondSnapshotFile.exists()); | ||
| containerStateMachine.takeSnapshot(); | ||
| assertFalse(secondSnapshotFile.exists()); | ||
|
|
||
| // Now, takeSnapshot throws IOException. | ||
| try { | ||
| containerStateMachine.takeSnapshot(); | ||
| Assert.fail(); | ||
| } catch (IOException ioEx) { | ||
| //Verify the old snapshot file still exists. | ||
| assertTrue(thirdSnapshotFile.exists()); | ||
| } | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.