1 package org.apache.helix.filestore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
58 admin.addCluster(clusterName, true);
59
60
61 StateModelConfigGenerator generator = new StateModelConfigGenerator();
62 admin.addStateModelDef(clusterName, DEFAULT_STATE_MODEL,
63 new StateModelDefinition(generator.generateConfigForOnlineOffline()));
64
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
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 }