View Javadoc

1   package org.apache.helix.lockmanager;
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 java.util.List;
23  
24  import org.apache.helix.HelixManager;
25  import org.apache.helix.HelixManagerFactory;
26  import org.apache.helix.InstanceType;
27  import org.apache.helix.controller.HelixControllerMain;
28  import org.apache.helix.manager.zk.ZKHelixAdmin;
29  import org.apache.helix.model.InstanceConfig;
30  
31  public class LockProcess
32  {
33    private String clusterName;
34    private String zkAddress;
35    private String instanceName;
36    private HelixManager participantManager;
37    private boolean startController;
38    private HelixManager controllerManager;
39  
40    LockProcess(String clusterName, String zkAddress, String instanceName,
41        boolean startController)
42    {
43      this.clusterName = clusterName;
44      this.zkAddress = zkAddress;
45      this.instanceName = instanceName;
46      this.startController = startController;
47  
48    }
49  
50    public void start() throws Exception
51    {
52      System.out.println("STARTING "+ instanceName);
53      configureInstance(instanceName);
54      participantManager = HelixManagerFactory.getZKHelixManager(clusterName,
55          instanceName, InstanceType.PARTICIPANT, zkAddress);
56      participantManager.getStateMachineEngine().registerStateModelFactory(
57          "OnlineOffline", new LockFactory());
58      participantManager.connect();
59      if (startController)
60      {
61        startController();
62      }
63      System.out.println("STARTED "+ instanceName);
64  
65    }
66  
67    private void startController()
68    {
69      controllerManager = HelixControllerMain.startHelixController(zkAddress,
70          clusterName, "controller", HelixControllerMain.STANDALONE);
71    }
72  
73    /**
74     * Configure the instance, the configuration of each node is available to
75     * other nodes.
76     * 
77     * @param instanceName
78     */
79    private void configureInstance(String instanceName)
80    {
81      ZKHelixAdmin helixAdmin = new ZKHelixAdmin(zkAddress);
82  
83      List<String> instancesInCluster = helixAdmin
84          .getInstancesInCluster(clusterName);
85      if (instancesInCluster == null || !instancesInCluster.contains(instanceName))
86      {
87        InstanceConfig config = new InstanceConfig(instanceName);
88        config.setHostName("localhost");
89        config.setPort("12000");
90        helixAdmin.addInstance(clusterName, config);
91      }
92    }
93  
94    public void stop()
95    {
96      if (participantManager != null)
97      {
98        participantManager.disconnect();
99      }
100     
101     if (controllerManager != null)
102     {
103       controllerManager.disconnect();
104     }
105   }
106 
107   public static void main(String[] args) throws Exception
108   {
109     String zkAddress = "localhost:2199";
110     String clusterName = "lock-manager-demo";
111     // Give a unique id to each process, most commonly used format hostname_port
112     final String instanceName = "localhost_12000";
113     boolean startController = true;
114     final LockProcess lockProcess = new LockProcess(clusterName, zkAddress,
115         instanceName, startController);
116     Runtime.getRuntime().addShutdownHook(new Thread()
117     {
118       @Override
119       public void run()
120       {
121         System.out.println("Shutting down " + instanceName);
122         lockProcess.stop();
123       }
124     });
125     lockProcess.start();
126     Thread.currentThread().join();
127   }
128 }