View Javadoc

1   package org.apache.helix.examples;
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.HelixManager;
23  import org.apache.helix.HelixManagerFactory;
24  import org.apache.helix.InstanceType;
25  import org.apache.helix.NotificationContext;
26  import org.apache.helix.model.Message;
27  import org.apache.helix.participant.StateMachineEngine;
28  import org.apache.helix.participant.statemachine.StateModel;
29  import org.apache.helix.participant.statemachine.StateModelFactory;
30  import org.apache.helix.participant.statemachine.StateModelInfo;
31  import org.apache.helix.participant.statemachine.Transition;
32  
33  public class DummyParticipant
34  {
35    // dummy master-slave state model
36    @StateModelInfo(initialState = "OFFLINE", states = { "MASTER", "SLAVE", "ERROR" })
37    public static class DummyMSStateModel extends StateModel
38    {
39      @Transition(to = "SLAVE", from = "OFFLINE")
40      public void onBecomeSlaveFromOffline(Message message, NotificationContext context)
41      {
42        String partitionName = message.getPartitionName();
43        String instanceName = message.getTgtName();
44        System.out.println(instanceName + " becomes SLAVE from OFFLINE for " + partitionName);
45      }
46  
47      @Transition(to = "MASTER", from = "SLAVE")
48      public void onBecomeMasterFromSlave(Message message, NotificationContext context)
49      {
50        String partitionName = message.getPartitionName();
51        String instanceName = message.getTgtName();
52        System.out.println(instanceName + " becomes MASTER from SLAVE for " + partitionName);
53      }
54  
55      @Transition(to = "SLAVE", from = "MASTER")
56      public void onBecomeSlaveFromMaster(Message message, NotificationContext context)
57      {
58        String partitionName = message.getPartitionName();
59        String instanceName = message.getTgtName();
60        System.out.println(instanceName + " becomes SLAVE from MASTER for " + partitionName);
61      }
62  
63      @Transition(to = "OFFLINE", from = "SLAVE")
64      public void onBecomeOfflineFromSlave(Message message, NotificationContext context)
65      {
66        String partitionName = message.getPartitionName();
67        String instanceName = message.getTgtName();
68        System.out.println(instanceName + " becomes OFFLINE from SLAVE for " + partitionName);
69      }
70  
71      @Transition(to = "DROPPED", from = "OFFLINE")
72      public void onBecomeDroppedFromOffline(Message message, NotificationContext context)
73      {
74        String partitionName = message.getPartitionName();
75        String instanceName = message.getTgtName();
76        System.out.println(instanceName + " becomes DROPPED from OFFLINE for " + partitionName);
77      }
78  
79      @Transition(to = "OFFLINE", from = "ERROR")
80      public void onBecomeOfflineFromError(Message message, NotificationContext context)
81      {
82        String partitionName = message.getPartitionName();
83        String instanceName = message.getTgtName();
84        System.out.println(instanceName + " becomes OFFLINE from ERROR for " + partitionName);
85      }
86  
87      @Override
88      public void reset()
89      {
90        System.out.println("Default MockMSStateModel.reset() invoked");
91      }
92    }
93  
94    // dummy master slave state model factory
95    public static class DummyMSModelFactory extends StateModelFactory<DummyMSStateModel>
96    {
97      @Override
98      public DummyMSStateModel createNewStateModel(String partitionName)
99      {
100       DummyMSStateModel model = new DummyMSStateModel();
101       return model;
102     }
103   }
104 
105   public static void main(String[] args)
106   {
107     if (args.length < 3)
108     {
109       System.err.println("USAGE: DummyParticipant zkAddress clusterName instanceName");
110       System.exit(1);
111     }
112 
113     String zkAddr = args[0];
114     String clusterName = args[1];
115     String instanceName = args[2];
116 
117     HelixManager manager = null;
118     try
119     {
120       manager = HelixManagerFactory.getZKHelixManager(clusterName, instanceName,
121           InstanceType.PARTICIPANT, zkAddr);
122 
123       StateMachineEngine stateMach = manager.getStateMachineEngine();
124       DummyMSModelFactory msModelFactory = new DummyMSModelFactory();
125       stateMach.registerStateModelFactory("MasterSlave", msModelFactory);
126 
127       manager.connect();
128 
129       Thread.currentThread().join();
130     } catch (Exception e)
131     {
132       // TODO Auto-generated catch block
133       e.printStackTrace();
134     } finally
135     {
136       if (manager != null)
137       {
138         manager.disconnect();
139       }
140     }
141   }
142 }