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

git commit: HELIX-30: ZkHelixManager.carryOverPreviousCurrentState() should use a special merge logic

Updated Branches:
  refs/heads/master 3bb94bf77 -> 1e36a882b


HELIX-30: ZkHelixManager.carryOverPreviousCurrentState() should use a special merge logic


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

Branch: refs/heads/master
Commit: 1e36a882bb8c17bfd0538b9496e8609b88482395
Parents: 3bb94bf
Author: zzhang <zz...@uci.edu>
Authored: Tue Mar 26 18:24:58 2013 -0700
Committer: zzhang <zz...@uci.edu>
Committed: Tue Mar 26 18:24:58 2013 -0700

----------------------------------------------------------------------
 .../helix/manager/zk/CurStateCarryOverUpdater.java |   70 +++++++++++
 .../apache/helix/manager/zk/ZKHelixManager.java    |   89 +++++++--------
 .../integration/TestCarryOverBadCurState.java      |    1 +
 3 files changed, 114 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/1e36a882/helix-core/src/main/java/org/apache/helix/manager/zk/CurStateCarryOverUpdater.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/CurStateCarryOverUpdater.java b/helix-core/src/main/java/org/apache/helix/manager/zk/CurStateCarryOverUpdater.java
new file mode 100644
index 0000000..f35f2e8
--- /dev/null
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/CurStateCarryOverUpdater.java
@@ -0,0 +1,70 @@
+package org.apache.helix.manager.zk;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.I0Itec.zkclient.DataUpdater;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.model.CurrentState;
+
+/**
+ * updater for carrying over last current states
+ * 
+ * @see HELIX-30: ZkHelixManager.carryOverPreviousCurrentState() should use a special merge logic
+ * 
+ * because carryOver() is performed after addLiveInstance(). it's possible that carryOver()
+ * overwrites current-state updates performed by current session. so carryOver() should be 
+ * performed only when current-state is empty for the partition
+ *
+ */
+class CurStateCarryOverUpdater implements DataUpdater<ZNRecord> {
+  final String _curSessionId;
+  final String _initState;
+  final CurrentState _lastCurState;
+  
+  public CurStateCarryOverUpdater(String curSessionId, String initState, CurrentState lastCurState) {
+    if (curSessionId == null || initState == null || lastCurState == null) {
+      throw new IllegalArgumentException("missing curSessionId|initState|lastCurState for carry-over");
+    }
+    _curSessionId = curSessionId;
+    _initState = initState;
+    _lastCurState = lastCurState;
+  }
+  
+  @Override
+  public ZNRecord update(ZNRecord currentData) {
+    CurrentState curState = null;
+    if (currentData == null) {
+      curState = new CurrentState(_lastCurState.getId());
+      curState.setSessionId(_curSessionId);
+    } else
+    {
+      curState = new CurrentState(currentData);
+    }
+    
+    for (String partitionName : _lastCurState.getPartitionStateMap().keySet()) {
+      // carry-over only when current-state not exist
+      if (curState.getState(partitionName) == null) {
+        curState.setState(partitionName, _initState);
+      }
+    }
+    return curState.getRecord();
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/1e36a882/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
index 84d1874..09e6137 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
@@ -22,6 +22,7 @@ package org.apache.helix.manager.zk;
 import java.lang.management.ManagementFactory;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.security.AccessControlContext;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedList;
@@ -35,6 +36,7 @@ import org.apache.helix.ClusterMessagingService;
 import org.apache.helix.ConfigAccessor;
 import org.apache.helix.ConfigChangeListener;
 import org.apache.helix.model.ConfigScope.ConfigScopeProperty;
+import org.apache.helix.AccessOption;
 import org.apache.helix.ControllerChangeListener;
 import org.apache.helix.CurrentStateChangeListener;
 import org.apache.helix.ExternalViewChangeListener;
@@ -882,61 +884,56 @@ public class ZKHelixManager implements HelixManager
     return true;
   }
 
+  /**
+   * carry over current-states from last sessions
+   * set to initial state for current session only when the state doesn't exist in current session
+   */
   private void carryOverPreviousCurrentState()
   {
     Builder keyBuilder = _helixAccessor.keyBuilder();
-
-    List<String> subPaths =
-        _helixAccessor.getChildNames(keyBuilder.sessions(_instanceName));
-    for (String previousSessionId : subPaths)
-    {
-      List<CurrentState> previousCurrentStates =
-          _helixAccessor.getChildValues(keyBuilder.currentStates(_instanceName,
-                                                                 previousSessionId));
-
-      for (CurrentState previousCurrentState : previousCurrentStates)
-      {
-        if (!previousSessionId.equalsIgnoreCase(_sessionId))
+    List<String> sessions = _helixAccessor.getChildNames(keyBuilder.sessions(_instanceName));
+    
+    // carry-over
+    for (String session : sessions) {
+      if (session.equals(_sessionId)) {
+        continue;
+      }
+      
+      List<CurrentState> lastCurStates = 
+          _helixAccessor.getChildValues(keyBuilder.currentStates(_instanceName, session));
+
+      for (CurrentState lastCurState : lastCurStates) {
+        logger.info("Carrying over old session: " + session + ", resource: "
+            + lastCurState.getId() + " to current session: " + _sessionId);
+        String stateModelDefRef = lastCurState.getStateModelDefRef();
+        if (stateModelDefRef == null)
         {
-          logger.info("Carrying over old session:" + previousSessionId + " resource "
-              + previousCurrentState.getId() + " to new session:" + _sessionId);
-          String stateModelDefRef = previousCurrentState.getStateModelDefRef();
-          if (stateModelDefRef == null)
-          {
-            logger.error("pervious current state doesn't have a state model def. skip it. prevCS: "
-                + previousCurrentState);
-            continue;
-          }
-          StateModelDefinition stateModel =
-              _helixAccessor.getProperty(keyBuilder.stateModelDef(stateModelDefRef));
-          for (String partitionName : previousCurrentState.getPartitionStateMap()
-                                                          .keySet())
-          {
-
-            previousCurrentState.setState(partitionName, stateModel.getInitialState());
-          }
-          previousCurrentState.setSessionId(_sessionId);
-          _helixAccessor.setProperty(keyBuilder.currentState(_instanceName,
-                                                             _sessionId,
-                                                             previousCurrentState.getId()),
-                                     previousCurrentState);
+          logger.error("skip carry-over because previous current state doesn't have a state model definition. previous current-state: "
+              + lastCurState);
+          continue;
         }
+        StateModelDefinition stateModel =
+            _helixAccessor.getProperty(keyBuilder.stateModelDef(stateModelDefRef));
+
+        String curStatePath = keyBuilder.currentState(_instanceName, _sessionId, lastCurState.getResourceName()).getPath();
+        _helixAccessor.getBaseDataAccessor().update(curStatePath, 
+           new CurStateCarryOverUpdater(_sessionId, stateModel.getInitialState(), lastCurState), AccessOption.PERSISTENT);
       }
     }
-    // Deleted old current state
-    for (String previousSessionId : subPaths)
+    
+    // remove previous current states
+    for (String session : sessions)
     {
-      if (!previousSessionId.equalsIgnoreCase(_sessionId))
-      {
-        String path =
-            _helixAccessor.keyBuilder()
-                          .currentStates(_instanceName, previousSessionId)
-                          .getPath();
-        logger.info("Deleting previous current state. path: " + path + "/"
-            + previousSessionId);
-        _zkClient.deleteRecursive(path);
-
+      if (session.equals(_sessionId)) {
+        continue;
       }
+
+      String path =
+          _helixAccessor.keyBuilder()
+                        .currentStates(_instanceName, session)
+                        .getPath();
+      logger.info("Removing current states from previous sessions. path: " + path);
+      _zkClient.deleteRecursive(path);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/1e36a882/helix-core/src/test/java/org/apache/helix/integration/TestCarryOverBadCurState.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestCarryOverBadCurState.java b/helix-core/src/test/java/org/apache/helix/integration/TestCarryOverBadCurState.java
index e04099d..8df8520 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestCarryOverBadCurState.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestCarryOverBadCurState.java
@@ -1,3 +1,4 @@
+
 package org.apache.helix.integration;
 
 /*