Added: incubator/helix/site-content/xref/org/apache/helix/examples/Quickstart.html URL: http://svn.apache.org/viewvc/incubator/helix/site-content/xref/org/apache/helix/examples/Quickstart.html?rev=1406467&view=auto ============================================================================== --- incubator/helix/site-content/xref/org/apache/helix/examples/Quickstart.html (added) +++ incubator/helix/site-content/xref/org/apache/helix/examples/Quickstart.html Wed Nov 7 05:11:27 2012 @@ -0,0 +1,287 @@ + + + + +Quickstart xref + + + +
View Javadoc
+
+1   package org.apache.helix.examples;
+2   
+3   import java.io.File;
+4   import java.util.ArrayList;
+5   import java.util.List;
+6   import java.util.Map;
+7   import java.util.TreeSet;
+8   
+9   import org.I0Itec.zkclient.IDefaultNameSpace;
+10  import org.I0Itec.zkclient.ZkClient;
+11  import org.I0Itec.zkclient.ZkServer;
+12  import org.apache.helix.HelixAdmin;
+13  import org.apache.helix.HelixManager;
+14  import org.apache.helix.HelixManagerFactory;
+15  import org.apache.helix.InstanceType;
+16  import org.apache.helix.controller.HelixControllerMain;
+17  import org.apache.helix.manager.zk.ZKHelixAdmin;
+18  import org.apache.helix.manager.zk.ZNRecordSerializer;
+19  import org.apache.helix.model.ExternalView;
+20  import org.apache.helix.model.InstanceConfig;
+21  import org.apache.helix.model.StateModelDefinition;
+22  import org.apache.helix.participant.StateMachineEngine;
+23  
+24  public class Quickstart
+25  {
+26  
+27    private static String ZK_ADDRESS = "localhost:2199";
+28    private static String CLUSTER_NAME = "HELIX_QUICKSTART";
+29    private static int NUM_NODES = 2;
+30    private static final String RESOURCE_NAME = "MyResource";
+31    private static final int NUM_PARTITIONS = 6;
+32    private static final int NUM_REPLICAS = 2;
+33  
+34    private static final String STATE_MODEL_NAME = "MyStateModel";
+35  
+36    // states
+37    private static final String SLAVE = "SLAVE";
+38    private static final String OFFLINE = "OFFLINE";
+39    private static final String MASTER = "MASTER";
+40    private static final String DROPPED = "DROPPED";
+41  
+42    private static List<InstanceConfig> INSTANCE_CONFIG_LIST;
+43    private static List<MyProcess> PROCESS_LIST;
+44    private static HelixAdmin admin;
+45    static
+46    {
+47      INSTANCE_CONFIG_LIST = new ArrayList<InstanceConfig>();
+48      PROCESS_LIST = new ArrayList<Quickstart.MyProcess>();
+49      for (int i = 0; i < NUM_NODES; i++)
+50      {
+51        int port = 12000 + i;
+52        InstanceConfig instanceConfig = new InstanceConfig("localhost_" + port);
+53        instanceConfig.setHostName("localhost");
+54        instanceConfig.setPort("" + port);
+55        instanceConfig.setInstanceEnabled(true);
+56        INSTANCE_CONFIG_LIST.add(instanceConfig);
+57      }
+58  
+59    }
+60  
+61    public static void setup()
+62    {
+63      admin = new ZKHelixAdmin(ZK_ADDRESS);
+64      // create cluster
+65      echo("Creating cluster: " + CLUSTER_NAME);
+66      admin.addCluster(CLUSTER_NAME, true);
+67  
+68      // Add nodes to the cluster
+69      echo("Adding " + NUM_NODES + " participants to the cluster");
+70      for (int i = 0; i < NUM_NODES; i++)
+71      {
+72        admin.addInstance(CLUSTER_NAME, INSTANCE_CONFIG_LIST.get(i));
+73        echo("\t Added participant: "
+74            + INSTANCE_CONFIG_LIST.get(i).getInstanceName());
+75      }
+76  
+77      // Add a state model
+78      StateModelDefinition myStateModel = defineStateModel();
+79      echo("Configuring StateModel: " + "MyStateModel  with 1 Master and 1 Slave");
+80      admin.addStateModelDef(CLUSTER_NAME, STATE_MODEL_NAME, myStateModel);
+81  
+82      // Add a resource with 6 partitions and 2 replicas
+83      echo("Adding a resource MyResource: " + "with 6 partitions and 2 replicas");
+84      admin.addResource(CLUSTER_NAME, RESOURCE_NAME, NUM_PARTITIONS,
+85          STATE_MODEL_NAME, "AUTO");
+86      // this will set up the ideal state, it calculates the preference list for
+87      // each partition similar to consistent hashing
+88      admin.rebalance(CLUSTER_NAME, RESOURCE_NAME, NUM_REPLICAS);
+89    }
+90  
+91    private static StateModelDefinition defineStateModel()
+92    {
+93      StateModelDefinition.Builder builder = new StateModelDefinition.Builder(
+94          STATE_MODEL_NAME);
+95      // Add states and their rank to indicate priority. Lower the rank higher the
+96      // priority
+97      builder.addState(MASTER, 1);
+98      builder.addState(SLAVE, 2);
+99      builder.addState(OFFLINE);
+100     builder.addState(DROPPED);
+101     // Set the initial state when the node starts
+102     builder.initialState(OFFLINE);
+103 
+104     // Add transitions between the states.
+105     builder.addTransition(OFFLINE, SLAVE);
+106     builder.addTransition(SLAVE, OFFLINE);
+107     builder.addTransition(SLAVE, MASTER);
+108     builder.addTransition(MASTER, SLAVE);
+109     builder.addTransition(OFFLINE, DROPPED);
+110 
+111     // set constraints on states.
+112     // static constraint
+113     builder.upperBound(MASTER, 1);
+114     // dynamic constraint, R means it should be derived based on the replication
+115     // factor.
+116     builder.dynamicUpperBound(SLAVE, "R");
+117 
+118     StateModelDefinition statemodelDefinition = builder.build();
+119     return statemodelDefinition;
+120   }
+121 
+122   public static void startController()
+123   {
+124     // start controller
+125     echo("Starting Helix Controller");
+126     HelixControllerMain.startHelixController(ZK_ADDRESS, CLUSTER_NAME,
+127         "localhost_9100", HelixControllerMain.STANDALONE);
+128   }
+129 
+130   public static void startNodes() throws Exception
+131   {
+132     echo("Starting Participants");
+133     for (int i = 0; i < NUM_NODES; i++)
+134     {
+135       MyProcess process = new MyProcess(INSTANCE_CONFIG_LIST.get(i).getId());
+136       PROCESS_LIST.add(process);
+137       process.start();
+138       echo("\t Started Participant: " + INSTANCE_CONFIG_LIST.get(i).getId());
+139     }
+140   }
+141 
+142   public static void startZookeeper()
+143   {
+144     echo("STARTING Zookeeper at " + ZK_ADDRESS);
+145     IDefaultNameSpace defaultNameSpace = new IDefaultNameSpace()
+146     {
+147       @Override
+148       public void createDefaultNameSpace(ZkClient zkClient)
+149       {
+150       }
+151     };
+152     new File("/tmp/helix-quickstart").mkdirs();
+153     // start zookeeper
+154     ZkServer server = new ZkServer("/tmp/helix-quickstart/dataDir",
+155         "/tmp/helix-quickstart/logDir", defaultNameSpace, 2199);
+156     server.start();
+157   }
+158 
+159   public static void echo(Object obj)
+160   {
+161     System.out.println(obj);
+162   }
+163 
+164   public static void main(String[] args) throws Exception
+165   {
+166     startZookeeper();
+167     setup();
+168     startNodes();
+169     startController();
+170     Thread.sleep(5000);
+171     printState("After starting 2 nodes");
+172     addNode();
+173     Thread.sleep(5000);
+174     printState("After adding a third node");
+175     stopNode();
+176     Thread.sleep(5000);
+177     printState("After the 3rd node stops/crashes");
+178     Thread.currentThread().join();
+179     System.exit(0);
+180   }
+181 
+182   private static void addNode() throws Exception
+183   {
+184 
+185     NUM_NODES = NUM_NODES + 1;
+186     int port = 12000 + NUM_NODES - 1;
+187     InstanceConfig instanceConfig = new InstanceConfig("localhost_" + port);
+188     instanceConfig.setHostName("localhost");
+189     instanceConfig.setPort("" + port);
+190     instanceConfig.setInstanceEnabled(true);
+191     echo("ADDING NEW NODE :" + instanceConfig.getInstanceName()+ ". Partitions will move from old nodes to the new node.");
+192     admin.addInstance(CLUSTER_NAME, instanceConfig);
+193     INSTANCE_CONFIG_LIST.add(instanceConfig);
+194     MyProcess process = new MyProcess(instanceConfig.getInstanceName());
+195     PROCESS_LIST.add(process);
+196     admin.rebalance(CLUSTER_NAME, RESOURCE_NAME, 3);
+197     process.start();
+198   }
+199 
+200   private static void stopNode()
+201   {
+202     int nodeId = NUM_NODES - 1;
+203     echo("STOPPING " + INSTANCE_CONFIG_LIST.get(nodeId).getInstanceName()+". Mastership will be transferred to the remaining nodes");
+204     PROCESS_LIST.get(nodeId).stop();
+205   }
+206 
+207   private static void printState(String msg)
+208   {
+209     System.out.println("CLUSTER STATE: "+ msg);
+210     ExternalView resourceExternalView = admin.getResourceExternalView(
+211         CLUSTER_NAME, RESOURCE_NAME);
+212     TreeSet<String> sortedSet = new TreeSet<String>(
+213         resourceExternalView.getPartitionSet());
+214     StringBuilder sb = new StringBuilder("\t\t");
+215     for (int i = 0; i < NUM_NODES; i++)
+216     {
+217       sb.append(INSTANCE_CONFIG_LIST.get(i).getInstanceName()).append("\t");
+218     }
+219     System.out.println(sb);
+220     for (String partitionName : sortedSet)
+221     {
+222       sb.delete(0, sb.length() - 1);
+223       sb.append(partitionName).append("\t");
+224       for (int i = 0; i < NUM_NODES; i++)
+225       {
+226         Map<String, String> stateMap = resourceExternalView
+227             .getStateMap(partitionName);
+228         if (stateMap != null
+229             && stateMap.containsKey(INSTANCE_CONFIG_LIST.get(i)
+230                 .getInstanceName()))
+231         {
+232           sb.append(
+233               stateMap.get(INSTANCE_CONFIG_LIST.get(i).getInstanceName())
+234                   .charAt(0)).append("\t\t");
+235         } else
+236         {
+237           sb.append("-").append("\t\t");
+238         }
+239       }
+240       System.out.println(sb);
+241     }
+242     System.out.println("###################################################################");
+243   }
+244 
+245   static final class MyProcess
+246   {
+247     private final String instanceName;
+248     private HelixManager manager;
+249 
+250     public MyProcess(String instanceName)
+251     {
+252       this.instanceName = instanceName;
+253     }
+254 
+255     public void start() throws Exception
+256     {
+257       manager = HelixManagerFactory.getZKHelixManager(CLUSTER_NAME,
+258           instanceName, InstanceType.PARTICIPANT, ZK_ADDRESS);
+259 
+260       MasterSlaveStateModelFactory stateModelFactory = new MasterSlaveStateModelFactory(
+261           instanceName);
+262 
+263       StateMachineEngine stateMach = manager.getStateMachineEngine();
+264       stateMach.registerStateModelFactory(STATE_MODEL_NAME, stateModelFactory);
+265       manager.connect();
+266     }
+267 
+268     public void stop()
+269     {
+270       manager.disconnect();
+271     }
+272   }
+273 
+274 }
+
+
+ Modified: incubator/helix/site-content/xref/org/apache/helix/examples/package-frame.html URL: http://svn.apache.org/viewvc/incubator/helix/site-content/xref/org/apache/helix/examples/package-frame.html?rev=1406467&r1=1406466&r2=1406467&view=diff ============================================================================== --- incubator/helix/site-content/xref/org/apache/helix/examples/package-frame.html (original) +++ incubator/helix/site-content/xref/org/apache/helix/examples/package-frame.html Wed Nov 7 05:11:27 2012 @@ -64,11 +64,17 @@ MasterSlaveStateModelFactory
  • + MyProcess +
  • +
  • OnlineOfflineStateModel
  • OnlineOfflineStateModelFactory
  • +
  • + Quickstart +
  • Modified: incubator/helix/site-content/xref/org/apache/helix/examples/package-summary.html URL: http://svn.apache.org/viewvc/incubator/helix/site-content/xref/org/apache/helix/examples/package-summary.html?rev=1406467&r1=1406466&r2=1406467&view=diff ============================================================================== --- incubator/helix/site-content/xref/org/apache/helix/examples/package-summary.html (original) +++ incubator/helix/site-content/xref/org/apache/helix/examples/package-summary.html Wed Nov 7 05:11:27 2012 @@ -117,6 +117,11 @@ + MyProcess + + + + OnlineOfflineStateModel @@ -125,6 +130,11 @@ OnlineOfflineStateModelFactory + + + Quickstart + + Modified: incubator/helix/site-content/xref/org/apache/helix/manager/file/FileHelixAdmin.html URL: http://svn.apache.org/viewvc/incubator/helix/site-content/xref/org/apache/helix/manager/file/FileHelixAdmin.html?rev=1406467&r1=1406466&r2=1406467&view=diff ============================================================================== --- incubator/helix/site-content/xref/org/apache/helix/manager/file/FileHelixAdmin.html (original) +++ incubator/helix/site-content/xref/org/apache/helix/manager/file/FileHelixAdmin.html Wed Nov 7 05:11:27 2012 @@ -438,67 +438,65 @@ 428 @Override 429 public void setConfig(ConfigScope scope, Map<String, String> properties) 430 { -431 // TODO Auto-generated method stub -432 throw new UnsupportedOperationException("unsupported operation"); +431 throw new UnsupportedOperationException("unsupported operation"); +432 } 433 -434 } -435 -436 @Override -437 public Map<String, String> getConfig(ConfigScope scope, Set<String> keys) -438 { -439 // TODO Auto-generated method stub -440 throw new UnsupportedOperationException("unsupported operation"); -441 } -442 -443 @Override -444 public List<String> getConfigKeys(ConfigScopeProperty scope, -445 String clusterName, -446 String... keys) -447 { -448 // TODO Auto-generated method stub -449 throw new UnsupportedOperationException("unsupported operation"); -450 } -451 -452 @Override -453 public void removeConfig(ConfigScope scope, Set<String> keys) -454 { -455 // TODO Auto-generated method stub -456 throw new UnsupportedOperationException("unsupported operation"); -457 -458 } -459 -460 @Override -461 public void rebalance(String clusterName, String resourceName, int replica) -462 { -463 // TODO Auto-generated method stub +434 @Override +435 public Map<String, String> getConfig(ConfigScope scope, Set<String> keys) +436 { +437 throw new UnsupportedOperationException("unsupported operation"); +438 } +439 +440 @Override +441 public List<String> getConfigKeys(ConfigScopeProperty scope, +442 String clusterName, +443 String... keys) +444 { +445 throw new UnsupportedOperationException("unsupported operation"); +446 } +447 +448 @Override +449 public void removeConfig(ConfigScope scope, Set<String> keys) +450 { +451 throw new UnsupportedOperationException("unsupported operation"); +452 +453 } +454 +455 @Override +456 public void rebalance(String clusterName, String resourceName, int replica) +457 { +458 throw new UnsupportedOperationException("unsupported operation"); +459 } +460 +461 @Override +462 public void addIdealState(String clusterName, String resourceName, String idealStateFile) throws IOException +463 { 464 throw new UnsupportedOperationException("unsupported operation"); 465 } 466 467 @Override -468 public void addIdealState(String clusterName, String resourceName, String idealStateFile) throws IOException -469 { -470 // TODO Auto-generated method stub -471 throw new UnsupportedOperationException("unsupported operation"); -472 } -473 -474 @Override -475 public void addStateModelDef(String clusterName, -476 String stateModelDefName, -477 String stateModelDefFile) throws IOException -478 { -479 // TODO Auto-generated method stub +468 public void addStateModelDef(String clusterName, +469 String stateModelDefName, +470 String stateModelDefFile) throws IOException +471 { +472 throw new UnsupportedOperationException("unsupported operation"); +473 } +474 +475 @Override +476 public void addMessageConstraint(String clusterName, +477 String constraintId, +478 Map<String, String> constraints) +479 { 480 throw new UnsupportedOperationException("unsupported operation"); 481 } 482 483 @Override -484 public void addMessageConstraint(String clusterName, -485 String constraintId, -486 Map<String, String> constraints) -487 { -488 // TODO Auto-generated method stub -489 throw new UnsupportedOperationException("unsupported operation"); -490 } -491 } +484 public void addResource(String clusterName, String resourceName, +485 IdealState idealstate) +486 { +487 throw new UnsupportedOperationException("unsupported operation"); +488 } +489 }