HDFS-12313. Ozone: SCM: move container/pipeline StateMachine to the right package. Contributed by Xiaoyu Yao.

This commit is contained in:
Xiaoyu Yao 2017-08-16 22:07:16 -07:00 committed by Owen O'Malley
parent ad1d8197fc
commit 795ba1b3b7
5 changed files with 66 additions and 16 deletions

View File

@ -0,0 +1,21 @@
/**
* 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.common;
/**
ozone common shared by SCM, KSM, etc.
**/

View File

@ -16,7 +16,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.apache.hadoop.scm.container.common.helpers.StateMachine; package org.apache.hadoop.ozone.common.statemachine;
/** /**
* Class wraps invalid state transition exception. * Class wraps invalid state transition exception.

View File

@ -16,12 +16,12 @@
* limitations under the License. * limitations under the License.
*/ */
package org.apache.hadoop.scm.container.common.helpers.StateMachine; package org.apache.hadoop.ozone.common.statemachine;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -36,7 +36,7 @@ public class StateMachine<STATE extends Enum<?>, EVENT extends Enum<?>> {
private STATE initialState; private STATE initialState;
private Set<STATE> finalStates; private Set<STATE> finalStates;
private final Cache<EVENT, Map<STATE, STATE>> transitions = private final LoadingCache<EVENT, Map<STATE, STATE>> transitions =
CacheBuilder.newBuilder().build( CacheBuilder.newBuilder().build(
CacheLoader.from((Supplier<Map<STATE, STATE>>) () -> new HashMap())); CacheLoader.from((Supplier<Map<STATE, STATE>>) () -> new HashMap()));
@ -62,8 +62,7 @@ public STATE getNextState(STATE from, EVENT e)
return target; return target;
} }
public void addTransition(STATE from, STATE to, EVENT e) public void addTransition(STATE from, STATE to, EVENT e) {
throws InvalidStateTransitionException {
transitions.getUnchecked(e).put(from, to); transitions.getUnchecked(e).put(from, to);
} }
} }

View File

@ -0,0 +1,21 @@
/**
* 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.common.statemachine;
/**
state machine template class for ozone.
**/

View File

@ -15,11 +15,11 @@
* the License. * the License.
*/ */
package org.apache.hadoop.scm; package org.apache.hadoop.ozone.common;
import org.apache.commons.collections.SetUtils; import org.apache.commons.collections.SetUtils;
import org.apache.hadoop.scm.container.common.helpers.StateMachine.InvalidStateTransitionException; import org.apache.hadoop.ozone.common.statemachine.InvalidStateTransitionException;
import org.apache.hadoop.scm.container.common.helpers.StateMachine.StateMachine; import org.apache.hadoop.ozone.common.statemachine.StateMachine;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -28,17 +28,26 @@
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import static org.apache.hadoop.scm.TestStateMachine.STATES.INIT; import static org.apache.hadoop.ozone.common.TestStateMachine.STATES.INIT;
import static org.apache.hadoop.scm.TestStateMachine.STATES.CREATING; import static org.apache.hadoop.ozone.common.TestStateMachine.STATES.CREATING;
import static org.apache.hadoop.scm.TestStateMachine.STATES.OPERATIONAL; import static org.apache.hadoop.ozone.common.TestStateMachine.STATES.OPERATIONAL;
import static org.apache.hadoop.scm.TestStateMachine.STATES.CLOSED; import static org.apache.hadoop.ozone.common.TestStateMachine.STATES.CLOSED;
import static org.apache.hadoop.scm.TestStateMachine.STATES.CLEANUP; import static org.apache.hadoop.ozone.common.TestStateMachine.STATES.CLEANUP;
import static org.apache.hadoop.scm.TestStateMachine.STATES.FINAL; import static org.apache.hadoop.ozone.common.TestStateMachine.STATES.FINAL;
/**
* This class is to test ozone common state machine.
*/
public class TestStateMachine { public class TestStateMachine {
/**
* STATES used by the test state machine.
*/
public enum STATES {INIT, CREATING, OPERATIONAL, CLOSED, CLEANUP, FINAL}; public enum STATES {INIT, CREATING, OPERATIONAL, CLOSED, CLEANUP, FINAL};
/**
* EVENTS used by the test state machine.
*/
public enum EVENTS {ALLOCATE, CREATE, UPDATE, CLOSE, DELETE, TIMEOUT}; public enum EVENTS {ALLOCATE, CREATE, UPDATE, CLOSE, DELETE, TIMEOUT};
@Rule @Rule
@ -75,7 +84,7 @@ public void testStateMachineStates() throws InvalidStateTransitionException {
CLEANUP, stateMachine.getNextState(CREATING, EVENTS.TIMEOUT)); CLEANUP, stateMachine.getNextState(CREATING, EVENTS.TIMEOUT));
Assert.assertEquals("STATE should be CLOSED after being closed", Assert.assertEquals("STATE should be CLOSED after being closed",
CLOSED, stateMachine.getNextState(OPERATIONAL, EVENTS.CLOSE)); CLOSED, stateMachine.getNextState(OPERATIONAL, EVENTS.CLOSE));
// Negative cases: invalid transition // Negative cases: invalid transition
expectException(); expectException();
stateMachine.getNextState(OPERATIONAL, EVENTS.CREATE); stateMachine.getNextState(OPERATIONAL, EVENTS.CREATE);