View Javadoc

1   package org.apache.helix.filestore;
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.manager.zk.ZKHelixAdmin;
23  import org.apache.helix.manager.zk.ZNRecordSerializer;
24  import org.apache.helix.manager.zk.ZkClient;
25  import org.apache.helix.model.IdealState.IdealStateModeProperty;
26  import org.apache.helix.model.InstanceConfig;
27  import org.apache.helix.model.StateModelDefinition;
28  import org.apache.helix.tools.StateModelConfigGenerator;
29  
30  public class SetupCluster
31  {
32    public static final String DEFAULT_CLUSTER_NAME = "file-store-test";
33    public static final String DEFAULT_RESOURCE_NAME = "repository";
34    public static final int DEFAULT_PARTITION_NUMBER = 1;
35    public static final String DEFAULT_STATE_MODEL = "MasterSlave";
36  
37    public static void main(String[] args)
38    {
39      if (args.length < 2)
40      {
41        System.err
42            .println("USAGE: java SetupCluster zookeeperAddress(e.g. localhost:2181) numberOfNodes");
43        System.exit(1);
44      }
45  
46      final String zkAddr = args[0];
47      final int numNodes = Integer.parseInt(args[1]);
48      final String clusterName = DEFAULT_CLUSTER_NAME;
49  
50      ZkClient zkclient = null;
51      try
52      {
53        zkclient = new ZkClient(zkAddr, ZkClient.DEFAULT_SESSION_TIMEOUT,
54            ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
55        ZKHelixAdmin admin = new ZKHelixAdmin(zkclient);
56  
57        // add cluster
58        admin.addCluster(clusterName, true);
59  
60        // add state model definition
61        StateModelConfigGenerator generator = new StateModelConfigGenerator();
62        admin.addStateModelDef(clusterName, DEFAULT_STATE_MODEL,
63            new StateModelDefinition(generator.generateConfigForOnlineOffline()));
64        // addNodes
65        for (int i = 0; i < numNodes; i++)
66        {
67          String port = "" +(12001+ i);
68          String serverId = "localhost_"+ port;
69          InstanceConfig config = new InstanceConfig(serverId);
70          config.setHostName("localhost");
71          config.setPort(port);
72          config.setInstanceEnabled(true);
73          admin.addInstance(clusterName, config);
74        }
75        // add resource "repository" which has 1 partition
76        String resourceName = DEFAULT_RESOURCE_NAME;
77        admin.addResource(clusterName, resourceName, DEFAULT_PARTITION_NUMBER,
78            DEFAULT_STATE_MODEL, IdealStateModeProperty.AUTO.toString());
79        admin.rebalance(clusterName, resourceName, 1);
80  
81      } finally
82      {
83        if (zkclient != null)
84        {
85          zkclient.close();
86        }
87      }
88    }
89  }