You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by ka...@apache.org on 2013/11/07 02:19:34 UTC

[26/53] [abbrv] git commit: [HELIX-240] Add update examples to the existing example class

[HELIX-240] Add update examples to the existing example class


Project: http://git-wip-us.apache.org/repos/asf/incubator-helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-helix/commit/31725b6f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-helix/tree/31725b6f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-helix/diff/31725b6f

Branch: refs/heads/master
Commit: 31725b6f284dce6dfa5cf298e68b5263c5d69f21
Parents: 644ef67
Author: Kanak Biscuitwala <ka...@apache.org>
Authored: Thu Sep 26 18:03:09 2013 -0700
Committer: Kanak Biscuitwala <ka...@apache.org>
Committed: Wed Nov 6 13:17:35 2013 -0800

----------------------------------------------------------------------
 .../helix/api/accessor/ParticipantAccessor.java | 26 ++++++----------
 .../apache/helix/examples/NewModelExample.java  | 32 ++++++++++++++++++++
 2 files changed, 42 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/31725b6f/helix-core/src/main/java/org/apache/helix/api/accessor/ParticipantAccessor.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/accessor/ParticipantAccessor.java b/helix-core/src/main/java/org/apache/helix/api/accessor/ParticipantAccessor.java
index c1a9250..ca7cf3c 100644
--- a/helix-core/src/main/java/org/apache/helix/api/accessor/ParticipantAccessor.java
+++ b/helix-core/src/main/java/org/apache/helix/api/accessor/ParticipantAccessor.java
@@ -38,7 +38,6 @@ import org.apache.helix.api.Participant;
 import org.apache.helix.api.RunningInstance;
 import org.apache.helix.api.config.ParticipantConfig;
 import org.apache.helix.api.config.UserConfig;
-import org.apache.helix.api.id.ClusterId;
 import org.apache.helix.api.id.MessageId;
 import org.apache.helix.api.id.ParticipantId;
 import org.apache.helix.api.id.PartitionId;
@@ -58,10 +57,8 @@ public class ParticipantAccessor {
 
   private final HelixDataAccessor _accessor;
   private final PropertyKey.Builder _keyBuilder;
-  private final ClusterId _clusterId;
 
-  public ParticipantAccessor(ClusterId clusterId, HelixDataAccessor accessor) {
-    _clusterId = clusterId;
+  public ParticipantAccessor(HelixDataAccessor accessor) {
     _accessor = accessor;
     _keyBuilder = accessor.keyBuilder();
   }
@@ -74,8 +71,7 @@ public class ParticipantAccessor {
   void enableParticipant(ParticipantId participantId, boolean isEnabled) {
     String participantName = participantId.stringify();
     if (_accessor.getProperty(_keyBuilder.instanceConfig(participantName)) == null) {
-      LOG.error("Config for participant: " + participantId + " does NOT exist in cluster: "
-          + _clusterId);
+      LOG.error("Config for participant: " + participantId + " does NOT exist in cluster");
       return;
     }
 
@@ -167,17 +163,15 @@ public class ParticipantAccessor {
     // check instanceConfig exists
     PropertyKey instanceConfigKey = _keyBuilder.instanceConfig(participantName);
     if (_accessor.getProperty(instanceConfigKey) == null) {
-      LOG.error("Config for participant: " + participantId + " does NOT exist in cluster: "
-          + _clusterId);
+      LOG.error("Config for participant: " + participantId + " does NOT exist in cluster");
       return;
     }
 
     // check resource exist. warn if not
     IdealState idealState = _accessor.getProperty(_keyBuilder.idealState(resourceName));
     if (idealState == null) {
-      LOG.warn("Disable partitions: " + partitionIdSet + " but Cluster: " + _clusterId
-          + ", resource: " + resourceId
-          + " does NOT exists. probably disable it during ERROR->DROPPED transtition");
+      LOG.warn("Disable partitions: " + partitionIdSet + ", resource: " + resourceId
+          + " does NOT exist. probably disable it during ERROR->DROPPED transtition");
 
     } else {
       // check partitions exist. warn if not
@@ -186,8 +180,8 @@ public class ParticipantAccessor {
             .getPreferenceList(partitionId) == null)
             || (idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED && idealState
                 .getParticipantStateMap(partitionId) == null)) {
-          LOG.warn("Cluster: " + _clusterId + ", resource: " + resourceId + ", partition: "
-              + partitionId + ", partition does NOT exist in ideal state");
+          LOG.warn("Resource: " + resourceId + ", partition: " + partitionId
+              + ", partition does NOT exist in ideal state");
         }
       }
     }
@@ -205,8 +199,7 @@ public class ParticipantAccessor {
       @Override
       public ZNRecord update(ZNRecord currentData) {
         if (currentData == null) {
-          throw new HelixException("Cluster: " + _clusterId + ", instance: " + participantId
-              + ", participant config is null");
+          throw new HelixException("Instance: " + participantId + ", participant config is null");
         }
 
         // TODO: merge with InstanceConfig.setInstanceEnabledForPartition
@@ -382,7 +375,8 @@ public class ParticipantAccessor {
   public Participant readParticipant(ParticipantId participantId) {
     // read physical model
     String participantName = participantId.stringify();
-    InstanceConfig instanceConfig = _accessor.getProperty(_keyBuilder.instance(participantName));
+    InstanceConfig instanceConfig =
+        _accessor.getProperty(_keyBuilder.instanceConfig(participantName));
 
     if (instanceConfig == null) {
       LOG.error("Participant " + participantId + " is not present on the cluster");

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/31725b6f/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java
----------------------------------------------------------------------
diff --git a/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java b/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java
index cb5906f..2de8dda 100644
--- a/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java
+++ b/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java
@@ -1,6 +1,7 @@
 package org.apache.helix.examples;
 
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.helix.BaseDataAccessor;
@@ -10,6 +11,8 @@ import org.apache.helix.api.Partition;
 import org.apache.helix.api.Scope;
 import org.apache.helix.api.State;
 import org.apache.helix.api.accessor.ClusterAccessor;
+import org.apache.helix.api.accessor.ParticipantAccessor;
+import org.apache.helix.api.accessor.ResourceAccessor;
 import org.apache.helix.api.config.ClusterConfig;
 import org.apache.helix.api.config.ParticipantConfig;
 import org.apache.helix.api.config.ResourceConfig;
@@ -30,6 +33,7 @@ import org.apache.helix.model.Transition;
 import org.apache.log4j.Logger;
 
 import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -105,6 +109,34 @@ public class NewModelExample {
 
     // create the cluster
     createCluster(cluster, accessor);
+
+    // update the resource
+    updateResource(resource, accessor);
+
+    // update the participant
+    updateParticipant(participant, accessor);
+  }
+
+  private static void updateParticipant(ParticipantConfig participant,
+      HelixDataAccessor helixAccessor) {
+    // add a tag to the participant and change the hostname, then update it using a delta
+    ParticipantAccessor accessor = new ParticipantAccessor(helixAccessor);
+    ParticipantConfig.Delta delta =
+        new ParticipantConfig.Delta(participant.getId()).addTag("newTag").setHostName("newHost");
+    accessor.updateParticipant(participant.getId(), delta);
+  }
+
+  private static void updateResource(ResourceConfig resource, HelixDataAccessor helixAccessor) {
+    // add some fields to the resource user config, then update it using the resource config delta
+    ResourceAccessor accessor = new ResourceAccessor(helixAccessor);
+    UserConfig userConfig = resource.getUserConfig();
+    Map<String, String> mapField = Maps.newHashMap();
+    mapField.put("k1", "v1");
+    mapField.put("k2", "v2");
+    userConfig.setMapField("sampleMap", mapField);
+    ResourceConfig.Delta delta =
+        new ResourceConfig.Delta(resource.getId()).setUserConfig(userConfig);
+    accessor.updateResource(resource.getId(), delta);
   }
 
   private static void createCluster(ClusterConfig cluster, HelixDataAccessor helixAccessor) {