szetszwo commented on code in PR #1372: URL: https://github.com/apache/ratis/pull/1372#discussion_r3399331535
########## ratis-server/src/test/java/org/apache/ratis/server/storage/SnapshotManagerTest.java: ########## @@ -0,0 +1,174 @@ +/* + * 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.ratis.server.storage; + +import org.apache.ratis.proto.RaftProtos.FileChunkProto; +import org.apache.ratis.proto.RaftProtos.InstallSnapshotRequestProto; +import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.server.protocol.TermIndex; +import org.apache.ratis.statemachine.SnapshotInfo; +import org.apache.ratis.statemachine.SnapshotRetentionPolicy; +import org.apache.ratis.statemachine.StateMachine; +import org.apache.ratis.statemachine.StateMachineStorage; +import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; +import org.apache.ratis.util.FileUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.security.MessageDigest; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class SnapshotManagerTest { Review Comment: To be consistent with the other tests, - rename it to "TestSnapshotManager" - move it to ratis-test. ########## ratis-server/src/test/java/org/apache/ratis/server/storage/SnapshotManagerTest.java: ########## @@ -0,0 +1,174 @@ +/* + * 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.ratis.server.storage; + +import org.apache.ratis.proto.RaftProtos.FileChunkProto; +import org.apache.ratis.proto.RaftProtos.InstallSnapshotRequestProto; +import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.server.protocol.TermIndex; +import org.apache.ratis.statemachine.SnapshotInfo; +import org.apache.ratis.statemachine.SnapshotRetentionPolicy; +import org.apache.ratis.statemachine.StateMachine; +import org.apache.ratis.statemachine.StateMachineStorage; +import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; +import org.apache.ratis.util.FileUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.security.MessageDigest; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class SnapshotManagerTest { + private static final class TestRaftStorageDirectory implements RaftStorageDirectory { Review Comment: Let's use RaftStorageDirectoryImpl. ########## ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java: ########## @@ -470,11 +470,16 @@ RaftStorageImpl getStorage() { return raftStorage.getUnchecked(); } - void installSnapshot(InstallSnapshotRequestProto request) throws IOException { + void appendSnapshot(InstallSnapshotRequestProto request) throws IOException { // TODO: verify that we need to install the snapshot - StateMachine sm = server.getStateMachine(); - sm.pause(); // pause the SM to prepare for install snapshot - snapshotManager.installSnapshot(request, sm); + snapshotManager.appendSnapshot(request, server.getStateMachine()); + } + + void finalizeSnapshot(InstallSnapshotRequestProto request) throws IOException { + final StateMachine sm = server.getStateMachine(); + sm.pause(); // pause the SM right before publishing the snapshot atomically + // TODO: if there is a failure here, we need to rollback the snapshot installation. Review Comment: What do you mean by "rollback the snapshot installation" ? ########## ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java: ########## @@ -164,9 +168,16 @@ public void installSnapshot(InstallSnapshotRequestProto request, StateMachine st } } - if (snapshotChunkRequest.getDone()) { - rename(tmpDir, snapshotDir.get()); + } + + public void finalizeSnapshot(InstallSnapshotRequestProto request) throws IOException { + final InstallSnapshotRequestProto.SnapshotChunkProto snapshotChunkRequest = request.getSnapshotChunk(); + if (!snapshotChunkRequest.getDone()) { + throw new IOException("Cannot finalize incomplete snapshot request: " + + ServerStringUtils.toInstallSnapshotRequestString(request)); } Review Comment: Let's use Preconditions: ```java Preconditions.assertTrue(snapshotChunkRequest.getDone()); ``` ########## ratis-server/src/main/java/org/apache/ratis/server/impl/SnapshotInstallationHandler.java: ########## @@ -214,17 +214,18 @@ private CompletableFuture<InstallSnapshotReplyProto> checkAndInstallSnapshot(Ins return future.thenApply(dummy -> reply); } - //TODO: We should only update State with installed snapshot once the request is done. - state.installSnapshot(request); - - final int expectedChunkIndex = nextChunkIndex.getAndIncrement(); + final int expectedChunkIndex = nextChunkIndex.get(); if (expectedChunkIndex != snapshotChunkRequest.getRequestIndex()) { throw new IOException("Unexpected request chunk index: " + snapshotChunkRequest.getRequestIndex() + " (the expected index is " + expectedChunkIndex + ")"); } + // Append chunks to a temporary location first. Publish only when done=true. + state.appendSnapshot(request); + nextChunkIndex.incrementAndGet(); // update the committed index // re-load the state machine if this is the last chunk if (snapshotChunkRequest.getDone()) { + state.finalizeSnapshot(request); state.reloadStateMachine(lastIncluded); chunk0CallId.set(-1); Review Comment: Let's move snapshotManager to SnapshotInstallationHandler. It is only used here. ```java final int expectedChunkIndex = nextChunkIndex.get(); if (expectedChunkIndex != snapshotChunkRequest.getRequestIndex()) { throw new IOException("Unexpected request chunk index: " + snapshotChunkRequest.getRequestIndex() + " (the expected index is " + expectedChunkIndex + ")"); } // Append chunks to a temporary location first. Publish only when done=true. final StateMachine stateMachine = server.getStateMachine(); snapshotManager.appendSnapshot(request, stateMachine); nextChunkIndex.incrementAndGet(); // update the committed index // re-load the state machine if this is the last chunk if (snapshotChunkRequest.getDone()) { stateMachine.pause(); // pause the SM right before publishing the snapshot atomically snapshotManager.finalizeSnapshot(request); state.reloadStateMachine(lastIncluded); chunk0CallId.set(-1); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
