diff --git a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java index 3f18ee538b..51ffecae13 100644 --- a/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java +++ b/ratis-server/src/main/java/org/apache/ratis/statemachine/impl/BaseStateMachine.java @@ -101,10 +101,14 @@ public SnapshotInfo getLatestSnapshot() { @Override public void pause() { + lifeCycle.transitionIfValid(LifeCycle.State.PAUSING); + lifeCycle.transitionIfValid(LifeCycle.State.PAUSED); } @Override public void reinitialize() throws IOException { + lifeCycle.transitionIfValid(LifeCycle.State.STARTING); + lifeCycle.transitionIfValid(LifeCycle.State.RUNNING); } @Override diff --git a/ratis-server/src/test/java/org/apache/ratis/statemachine/impl/TestBaseStateMachine.java b/ratis-server/src/test/java/org/apache/ratis/statemachine/impl/TestBaseStateMachine.java new file mode 100644 index 0000000000..6610a02062 --- /dev/null +++ b/ratis-server/src/test/java/org/apache/ratis/statemachine/impl/TestBaseStateMachine.java @@ -0,0 +1,37 @@ +/* + * 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.statemachine.impl; + +import org.apache.ratis.util.LifeCycle; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestBaseStateMachine { + @Test + public void testPauseAndReinitializeUpdateLifecycle() throws Exception { + final BaseStateMachine stateMachine = new BaseStateMachine(); + stateMachine.getLifeCycle().transition(LifeCycle.State.STARTING); + stateMachine.getLifeCycle().transition(LifeCycle.State.RUNNING); + + stateMachine.pause(); + Assertions.assertEquals(LifeCycle.State.PAUSED, stateMachine.getLifeCycleState()); + + stateMachine.reinitialize(); + Assertions.assertEquals(LifeCycle.State.RUNNING, stateMachine.getLifeCycleState()); + } +}