Return-Path: X-Original-To: apmail-helix-commits-archive@minotaur.apache.org Delivered-To: apmail-helix-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EB6A011310 for ; Tue, 15 Jul 2014 18:17:20 +0000 (UTC) Received: (qmail 4028 invoked by uid 500); 15 Jul 2014 18:17:20 -0000 Delivered-To: apmail-helix-commits-archive@helix.apache.org Received: (qmail 3978 invoked by uid 500); 15 Jul 2014 18:17:20 -0000 Mailing-List: contact commits-help@helix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@helix.apache.org Delivered-To: mailing list commits@helix.apache.org Received: (qmail 3962 invoked by uid 99); 15 Jul 2014 18:17:20 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Jul 2014 18:17:20 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 6975798A009; Tue, 15 Jul 2014 18:17:20 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kanak@apache.org To: commits@helix.apache.org Date: Tue, 15 Jul 2014 18:17:20 -0000 Message-Id: <2ad65aadb9a44ed8bfeb8202b10126ed@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/4] git commit: [HELIX-448] Call onCallback for CustomCodeCallbackHandler for FINALIZE 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 Authored: Wed May 28 15:37:48 2014 -0700 Committer: Kanak Biscuitwala 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(); Map customCodeRunners = new HashMap(); - Map callbacks = - new HashMap(); + Map callbacks = new HashMap(); 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 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