1 package org.apache.helix.participant.statemachine;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.helix.NotificationContext;
23 import org.apache.helix.model.Message;
24 import org.apache.log4j.Logger;
25
26
27 public abstract class StateModel
28 {
29 static final String DEFAULT_INITIAL_STATE = "OFFLINE";
30 Logger logger = Logger.getLogger(StateModel.class);
31
32 // TODO Get default state from implementation or from state model annotation
33 // StateModel with initial state other than OFFLINE should override this field
34 protected String _currentState = DEFAULT_INITIAL_STATE;
35
36 public String getCurrentState()
37 {
38 return _currentState;
39 }
40
41 // @transition(from='from', to='to')
42 public void defaultTransitionHandler()
43 {
44 logger
45 .error("Default default handler. The idea is to invoke this if no transition method is found. Yet to be implemented");
46 }
47
48 public boolean updateState(String newState)
49 {
50 _currentState = newState;
51 return true;
52 }
53
54 /**
55 * Called when error occurs in state transition
56 *
57 * TODO:enforce subclass to write this
58 * @param message
59 * @param context
60 * @param error
61 */
62 public void rollbackOnError(Message message, NotificationContext context,
63 StateTransitionError error)
64 {
65
66 logger.error("Default rollback method invoked on error. Error Code: "
67 + error.getCode());
68
69 }
70
71 /**
72 * Called when the state model is reset
73 */
74 public void reset()
75 {
76 logger.warn("Default reset method invoked. Either because the process longer own this resource or session timedout");
77 }
78
79 /**
80 * default transition for drop partition in error state
81 *
82 * @param message
83 * @param context
84 * @throws InterruptedException
85 */
86 @Transition(to = "DROPPED", from = "ERROR")
87 public void defaultOnBecomeDroppedFromError(Message message, NotificationContext context)
88 {
89 logger.info("Default ERROR->DROPPED transition invoked.");
90 }
91
92 }