helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ka...@apache.org
Subject [1/4] git commit: [HELIX-448] Call onCallback for CustomCodeCallbackHandler for FINALIZE
Date Tue, 15 Jul 2014 18:17:20 GMT
Repository: helix
Updated Branches:
  refs/heads/master 639f2f8a6 -> a9e96ea06


[HELIX-448] Call onCallback for CustomCodeCallbackHandler for FINALIZE


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

Branch: refs/heads/master
Commit: 67be36e02cf8197c9fa19986040636b960463a3f
Parents: 639f2f8
Author: Kanak Biscuitwala <kanak@apache.org>
Authored: Wed May 28 15:37:48 2014 -0700
Committer: Kanak Biscuitwala <kanak@apache.org>
Committed: Tue Jul 15 10:35:31 2014 -0700

----------------------------------------------------------------------
 .../helix/participant/CustomCodeInvoker.java    | 53 +++++++++-----------
 .../TestDisableCustomCodeRunner.java            | 23 ++++++---
 2 files changed, 41 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/67be36e0/helix-core/src/main/java/org/apache/helix/participant/CustomCodeInvoker.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/participant/CustomCodeInvoker.java
b/helix-core/src/main/java/org/apache/helix/participant/CustomCodeInvoker.java
index 04375f1..a736d71 100644
--- a/helix-core/src/main/java/org/apache/helix/participant/CustomCodeInvoker.java
+++ b/helix-core/src/main/java/org/apache/helix/participant/CustomCodeInvoker.java
@@ -47,41 +47,38 @@ public class CustomCodeInvoker implements LiveInstanceChangeListener,
ConfigChan
   }
 
   private void callParticipantCode(NotificationContext context) {
-    // System.out.println("callback invoked. type:" + context.getType().toString());
-    if (context.getType() == Type.INIT || context.getType() == Type.CALLBACK) {
-      // since ZkClient.unsubscribe() does not immediately remove listeners
-      // from zk, it is possible that two listeners exist when leadership transfers
-      // therefore, double check to make sure only one participant invokes the code
-      if (context.getType() == Type.CALLBACK) {
-        HelixManager manager = context.getManager();
-        // DataAccessor accessor = manager.getDataAccessor();
-        HelixDataAccessor accessor = manager.getHelixDataAccessor();
-        Builder keyBuilder = accessor.keyBuilder();
+    // since ZkClient.unsubscribe() does not immediately remove listeners
+    // from zk, it is possible that two listeners exist when leadership transfers
+    // therefore, double check to make sure only one participant invokes the code
+    if (context.getType() == Type.CALLBACK) {
+      HelixManager manager = context.getManager();
+      // DataAccessor accessor = manager.getDataAccessor();
+      HelixDataAccessor accessor = manager.getHelixDataAccessor();
+      Builder keyBuilder = accessor.keyBuilder();
 
-        String instance = manager.getInstanceName();
-        String sessionId = manager.getSessionId();
+      String instance = manager.getInstanceName();
+      String sessionId = manager.getSessionId();
 
-        // get resource name from partition key: "PARTICIPANT_LEADER_XXX_0"
-        String resourceName = _partitionKey.substring(0, _partitionKey.lastIndexOf('_'));
+      // get resource name from partition key: "PARTICIPANT_LEADER_XXX_0"
+      String resourceName = _partitionKey.substring(0, _partitionKey.lastIndexOf('_'));
 
-        CurrentState curState =
-            accessor.getProperty(keyBuilder.currentState(instance, sessionId, resourceName));
-        if (curState == null) {
-          return;
-        }
-
-        String state = curState.getState(_partitionKey);
-        if (state == null || !state.equalsIgnoreCase("LEADER")) {
-          return;
-        }
+      CurrentState curState =
+          accessor.getProperty(keyBuilder.currentState(instance, sessionId, resourceName));
+      if (curState == null) {
+        return;
       }
 
-      try {
-        _callback.onCallback(context);
-      } catch (Exception e) {
-        LOG.error("Error invoking callback:" + _callback, e);
+      String state = curState.getState(_partitionKey);
+      if (state == null || !state.equalsIgnoreCase("LEADER")) {
+        return;
       }
     }
+
+    try {
+      _callback.onCallback(context);
+    } catch (Exception e) {
+      LOG.error("Error invoking callback:" + _callback, e);
+    }
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/helix/blob/67be36e0/helix-core/src/test/java/org/apache/helix/integration/TestDisableCustomCodeRunner.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestDisableCustomCodeRunner.java
b/helix-core/src/test/java/org/apache/helix/integration/TestDisableCustomCodeRunner.java
index 3223e48..b9097b3 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestDisableCustomCodeRunner.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestDisableCustomCodeRunner.java
@@ -24,9 +24,9 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.helix.BaseDataAccessor;
 import org.apache.helix.HelixAdmin;
 import org.apache.helix.HelixConstants.ChangeType;
-import org.apache.helix.BaseDataAccessor;
 import org.apache.helix.HelixDataAccessor;
 import org.apache.helix.NotificationContext;
 import org.apache.helix.PropertyKey;
@@ -72,6 +72,10 @@ public class TestDisableCustomCodeRunner extends ZkUnitTestBase {
     public boolean isCallbackTypeInvoked() {
       return _callbackInvokeMap.containsKey(NotificationContext.Type.CALLBACK);
     }
+
+    public boolean isFinalizeTypeInvoked() {
+      return _callbackInvokeMap.containsKey(NotificationContext.Type.FINALIZE);
+    }
   }
 
   @Test
@@ -100,8 +104,7 @@ public class TestDisableCustomCodeRunner extends ZkUnitTestBase {
         new HashMap<String, MockParticipantManager>();
     Map<String, HelixCustomCodeRunner> customCodeRunners =
         new HashMap<String, HelixCustomCodeRunner>();
-    Map<String, DummyCallback> callbacks =
-        new HashMap<String, DummyCallback>();
+    Map<String, DummyCallback> callbacks = new HashMap<String, DummyCallback>();
     for (int i = 0; i < N; i++) {
       String instanceName = "localhost_" + (12918 + i);
 
@@ -113,15 +116,14 @@ public class TestDisableCustomCodeRunner extends ZkUnitTestBase {
       callbacks.put(instanceName, new DummyCallback());
 
       customCodeRunners.get(instanceName).invoke(callbacks.get(instanceName))
-          .on(ChangeType.LIVE_INSTANCE)
-          .usingLeaderStandbyModel("TestParticLeader").start();
+          .on(ChangeType.LIVE_INSTANCE).usingLeaderStandbyModel("TestParticLeader").start();
       participants.get(instanceName).syncStart();
     }
 
     boolean result =
         ClusterStateVerifier
             .verifyByZkCallback(new ClusterStateVerifier.BestPossAndExtViewZkVerifier(ZK_ADDR,
-            clusterName));
+                clusterName));
     Assert.assertTrue(result);
 
     // Make sure callback is registered
@@ -191,9 +193,16 @@ public class TestDisableCustomCodeRunner extends ZkUnitTestBase {
     accessor.setProperty(keyBuilder.liveInstance("fakeInstance"), fakeInstance);
     Thread.sleep(1000);
 
-    for (DummyCallback callback : callbacks.values()) {
+    for (Map.Entry<String, DummyCallback> e : callbacks.entrySet()) {
+      String instance = e.getKey();
+      DummyCallback callback = e.getValue();
       Assert.assertFalse(callback.isInitTypeInvoked());
       Assert.assertFalse(callback.isCallbackTypeInvoked());
+
+      // Ensure that we were told that a leader stopped being the leader
+      if (instance.equals(leader)) {
+        Assert.assertTrue(callback.isFinalizeTypeInvoked());
+      }
     }
 
     // Remove fake instance


Mime
View raw message