helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zzh...@apache.org
Subject git commit: use separate thread to monitor process for helix-agent
Date Mon, 15 Apr 2013 18:35:03 GMT
Updated Branches:
  refs/heads/master 6b70b7783 -> 8fa068344


use separate thread to monitor process for helix-agent


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

Branch: refs/heads/master
Commit: 8fa068344e0f8029a9fee6f41c1c3838ba86cdf1
Parents: 6b70b77
Author: zzhang <zzhang5@uci.edu>
Authored: Mon Apr 15 11:34:03 2013 -0700
Committer: zzhang <zzhang5@uci.edu>
Committed: Mon Apr 15 11:34:03 2013 -0700

----------------------------------------------------------------------
 .../org/apache/helix/agent/AgentStateModel.java    |   18 +----
 .../apache/helix/agent/ProcessMonitorThread.java   |   63 +++++++++++++++
 .../java/org/apache/helix/agent/SystemUtil.java    |    2 +-
 .../resources/cluster-manager-version.properties   |    2 +-
 4 files changed, 67 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/8fa06834/helix-agent/src/main/java/org/apache/helix/agent/AgentStateModel.java
----------------------------------------------------------------------
diff --git a/helix-agent/src/main/java/org/apache/helix/agent/AgentStateModel.java b/helix-agent/src/main/java/org/apache/helix/agent/AgentStateModel.java
index aeda1db..26e7620 100644
--- a/helix-agent/src/main/java/org/apache/helix/agent/AgentStateModel.java
+++ b/helix-agent/src/main/java/org/apache/helix/agent/AgentStateModel.java
@@ -23,20 +23,15 @@ import java.io.File;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
-import java.util.Random;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.helix.ExternalCommand;
 import org.apache.helix.HelixManager;
 import org.apache.helix.NotificationContext;
-import org.apache.helix.ZNRecord;
-import org.apache.helix.agent.SystemUtil.ProcessStateCode;
-import org.apache.helix.model.ConfigScope;
 import org.apache.helix.model.HelixConfigScope;
 import org.apache.helix.model.Message;
 import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty;
-import org.apache.helix.model.builder.ConfigScopeBuilder;
 import org.apache.helix.model.builder.HelixConfigScopeBuilder;
 import org.apache.helix.participant.statemachine.StateModel;
 import org.apache.helix.participant.statemachine.StateModelInfo;
@@ -47,7 +42,6 @@ import org.apache.log4j.Logger;
 public class AgentStateModel extends StateModel
 {
   private static final Logger _logger = Logger.getLogger(AgentStateModel.class);
-  private static final int MONITOR_PERIOD_BASE = 1000;  // 1 second
   private static Pattern pattern = Pattern.compile("(\\{.+?\\})");
 
   private static String buildKey(String fromState, String toState, CommandAttribute attribute)
{
@@ -162,15 +156,7 @@ public class AgentStateModel extends StateModel
     
     String pidFileValue = instantiateByMessage(pidFile, message);
     String pid = SystemUtil.getPidFromFile(new File(pidFileValue));
-    
-    // monitor pid
-    ProcessStateCode processState = SystemUtil.getProcessState(pid);
-    while (processState != null) {
-      if (processState == ProcessStateCode.Z) {
-        throw new Exception("process: " + pid + " is in zombie state");
-      }
-      Thread.sleep(new Random().nextInt(MONITOR_PERIOD_BASE) + MONITOR_PERIOD_BASE);
-      processState = SystemUtil.getProcessState(pid);
-    }
+
+    new ProcessMonitorThread(pid).start();
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/8fa06834/helix-agent/src/main/java/org/apache/helix/agent/ProcessMonitorThread.java
----------------------------------------------------------------------
diff --git a/helix-agent/src/main/java/org/apache/helix/agent/ProcessMonitorThread.java b/helix-agent/src/main/java/org/apache/helix/agent/ProcessMonitorThread.java
new file mode 100644
index 0000000..1e3e177
--- /dev/null
+++ b/helix-agent/src/main/java/org/apache/helix/agent/ProcessMonitorThread.java
@@ -0,0 +1,63 @@
+package org.apache.helix.agent;
+
+import java.io.File;
+import java.util.Random;
+
+import org.apache.helix.agent.SystemUtil.ProcessStateCode;
+import org.apache.log4j.Logger;
+
+/*
+ * 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.
+ */
+
+/**
+ * thread for monitoring a pid 
+ *
+ */
+public class ProcessMonitorThread extends Thread {
+  private static final Logger LOG = Logger.getLogger(ProcessMonitorThread.class);
+  private static final int MONITOR_PERIOD_BASE = 1000;  // 1 second
+
+  private final String _pid;
+  
+  public ProcessMonitorThread(String pid) {
+    _pid = pid;
+  }
+  
+  @Override
+  public void run() {
+    
+    // monitor pid
+    try {
+      ProcessStateCode processState = SystemUtil.getProcessState(_pid);
+      while (processState != null) {
+        if (processState == ProcessStateCode.Z) {
+          LOG.error("process: " + _pid + " is in zombie state");
+          break;
+        }
+        Thread.sleep(new Random().nextInt(MONITOR_PERIOD_BASE) + MONITOR_PERIOD_BASE);
+        processState = SystemUtil.getProcessState(_pid);
+      }
+    } catch (Exception e) {
+      LOG.error("fail to monitor process: " + _pid, e);
+    }
+    
+    // TODO need to find the exit value of pid and kill the pid on timeout
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/8fa06834/helix-agent/src/main/java/org/apache/helix/agent/SystemUtil.java
----------------------------------------------------------------------
diff --git a/helix-agent/src/main/java/org/apache/helix/agent/SystemUtil.java b/helix-agent/src/main/java/org/apache/helix/agent/SystemUtil.java
index b256581..b53565e 100644
--- a/helix-agent/src/main/java/org/apache/helix/agent/SystemUtil.java
+++ b/helix-agent/src/main/java/org/apache/helix/agent/SystemUtil.java
@@ -62,7 +62,7 @@ public class SystemUtil {
       cmd.waitFor();
       
       // split by new lines
-      // should get 2 lines for existing process, 1 line for non-existing process
+      // should return 2 lines for an existing process, or 1 line for a non-existing process
       String lines[] = cmd.getStringOutput().split("[\\r\\n]+");
       if (lines.length != 2) {
         LOG.info("process: " + processId + " not exist");

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/8fa06834/helix-core/src/main/resources/cluster-manager-version.properties
----------------------------------------------------------------------
diff --git a/helix-core/src/main/resources/cluster-manager-version.properties b/helix-core/src/main/resources/cluster-manager-version.properties
index 765ad63..7087176 100644
--- a/helix-core/src/main/resources/cluster-manager-version.properties
+++ b/helix-core/src/main/resources/cluster-manager-version.properties
@@ -17,4 +17,4 @@
 # under the License.
 #
 
-clustermanager.version=${pom.version}
+clustermanager.version=${project.version}


Mime
View raw message