Repository: storm Updated Branches: refs/heads/master 8052a8c78 -> 4f2d3a96b
port HealthCheck to java Project: http://git-wip-us.apache.org/repos/asf/storm/repo Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/0f4b7522 Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/0f4b7522 Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/0f4b7522 Branch: refs/heads/master Commit: 0f4b7522dfeeae33fdadf6ec59ace97769b9ffe4 Parents: 12ceb09 Author: xiaojian.fxj <[email protected]> Authored: Tue Feb 16 17:18:35 2016 +0800 Committer: xiaojian.fxj <[email protected]> Committed: Tue Feb 16 17:18:35 2016 +0800 ---------------------------------------------------------------------- .../org/apache/storm/command/healthcheck.clj | 90 -------------- .../clj/org/apache/storm/daemon/supervisor.clj | 4 +- .../org/apache/storm/command/HealthCheck.java | 124 +++++++++++++++++++ 3 files changed, 126 insertions(+), 92 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/storm/blob/0f4b7522/storm-core/src/clj/org/apache/storm/command/healthcheck.clj ---------------------------------------------------------------------- diff --git a/storm-core/src/clj/org/apache/storm/command/healthcheck.clj b/storm-core/src/clj/org/apache/storm/command/healthcheck.clj deleted file mode 100644 index 138c7d8..0000000 --- a/storm-core/src/clj/org/apache/storm/command/healthcheck.clj +++ /dev/null @@ -1,90 +0,0 @@ -;; 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. -(ns org.apache.storm.command.healthcheck - (:require [org.apache.storm - [config :refer :all] - [util :refer :all] - [log :refer :all]] - [clojure.java [io :as io]] - [clojure [string :refer [split]]]) - (:import [org.apache.storm.utils ConfigUtils]) - (:gen-class)) - -(defn interrupter - "Interrupt a given thread after ms milliseconds." - [thread ms] - (let [interrupter (Thread. - (fn [] - (try - (Thread/sleep ms) - (.interrupt thread) - (catch InterruptedException e))))] - (.start interrupter) - interrupter)) - -(defn check-output [lines] - (if (some #(.startsWith % "ERROR") lines) - :failed - :success)) - -(defn process-script [conf script] - (let [script-proc (. (Runtime/getRuntime) (exec script)) - curthread (Thread/currentThread) - interrupter-thread (interrupter curthread - (conf STORM-HEALTH-CHECK-TIMEOUT-MS))] - (try - (.waitFor script-proc) - (.interrupt interrupter-thread) - (if (not (= (.exitValue script-proc) 0)) - :failed_with_exit_code - (check-output (split - (slurp (.getInputStream script-proc)) - #"\n+"))) - (catch InterruptedException e - (println "Script" script "timed out.") - :timeout) - (catch Exception e - (println "Script failed with exception: " e) - :failed_with_exception) - (finally (.interrupt interrupter-thread))))) - -(defn health-check [conf] - (let [health-dir (ConfigUtils/absoluteHealthCheckDir conf) - health-files (file-seq (io/file health-dir)) - health-scripts (filter #(and (.canExecute %) - (not (.isDirectory %))) - health-files) - results (->> health-scripts - (map #(.getAbsolutePath %)) - (map (partial process-script conf)))] - (log-message - (pr-str (map #'vector - (map #(.getAbsolutePath %) health-scripts) - results))) - ; failed_with_exit_code is OK. We're mimicing Hadoop's health checks. - ; We treat non-zero exit codes as indicators that the scripts failed - ; to execute properly, not that the system is unhealthy, in which case - ; we don't want to start killing things. - (if (every? #(or (= % :failed_with_exit_code) - (= % :success)) - results) - 0 - 1))) - -(defn -main [& args] - (let [conf (clojurify-structure (ConfigUtils/readStormConfig))] - (System/exit - (health-check conf)))) http://git-wip-us.apache.org/repos/asf/storm/blob/0f4b7522/storm-core/src/clj/org/apache/storm/daemon/supervisor.clj ---------------------------------------------------------------------- diff --git a/storm-core/src/clj/org/apache/storm/daemon/supervisor.clj b/storm-core/src/clj/org/apache/storm/daemon/supervisor.clj index ae9e92f..b1a8693 100644 --- a/storm-core/src/clj/org/apache/storm/daemon/supervisor.clj +++ b/storm-core/src/clj/org/apache/storm/daemon/supervisor.clj @@ -32,7 +32,7 @@ (:import [org.apache.storm.generated WorkerResources ProfileAction]) (:import [org.apache.storm.localizer LocalResource]) (:use [org.apache.storm.daemon common]) - (:require [org.apache.storm.command [healthcheck :as healthcheck]]) + (:import [org.apache.storm.command HealthCheck]) (:require [org.apache.storm.daemon [worker :as worker]] [org.apache.storm [process-simulator :as psim] [cluster :as cluster] [event :as event]] [clojure.set :as set]) @@ -835,7 +835,7 @@ (schedule-recurring (:event-timer supervisor) (* 60 5) (* 60 5) - (fn [] (let [health-code (healthcheck/health-check conf) + (fn [] (let [health-code (HealthCheck/healthCheck conf) ids (my-worker-ids conf)] (if (not (= health-code 0)) (do http://git-wip-us.apache.org/repos/asf/storm/blob/0f4b7522/storm-core/src/jvm/org/apache/storm/command/HealthCheck.java ---------------------------------------------------------------------- diff --git a/storm-core/src/jvm/org/apache/storm/command/HealthCheck.java b/storm-core/src/jvm/org/apache/storm/command/HealthCheck.java new file mode 100644 index 0000000..05890d6 --- /dev/null +++ b/storm-core/src/jvm/org/apache/storm/command/HealthCheck.java @@ -0,0 +1,124 @@ +/** + * 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. + */ +package org.apache.storm.command; + +import org.apache.commons.lang.StringUtils; +import org.apache.storm.Config; +import org.apache.storm.utils.ConfigUtils; + +import java.io.BufferedReader; +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class HealthCheck { + private static final String FAILED = "failed"; + private static final String SUCCESS = "success"; + private static final String TIMEOUT = "timeout"; + private static final String FAILED_WITH_EXIT_CODE = "failed_with_exit_code"; + + public static int healthCheck(Map conf) { + String healthDir = ConfigUtils.absoluteHealthCheckDir(conf); + List<String> results = new ArrayList<>(); + if (healthDir != null) { + File parentFile = new File(healthDir); + List<String> healthScripts = new ArrayList<String>(); + if (parentFile.exists()) { + File[] list = parentFile.listFiles(); + for (File f : list) { + if (!f.isDirectory() && f.canExecute()) + healthScripts.add(f.getAbsolutePath()); + } + } + for (String script : healthScripts) { + String result = processScript(conf, script); + results.add(result); + } + } + + // failed_with_exit_code is OK. We're mimicing Hadoop's health checks. + // We treat non-zero exit codes as indicators that the scripts failed + // to execute properly, not that the system is unhealthy, in which case + // we don't want to start killing things. + + if (results.contains(FAILED) || results.contains(TIMEOUT)) { + return 1; + } else { + return 0; + } + + } + + public static String processScript(Map conf, String script) { + Thread interruptThread = null; + try { + Process process = Runtime.getRuntime().exec(script); + final long timeout = (long) (conf.get(Config.STORM_HEALTH_CHECK_TIMEOUT_MS)); + final Thread curThread = Thread.currentThread(); + // kill process when timeout + interruptThread = new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(timeout); + curThread.interrupt(); + } catch (InterruptedException e) { + + } + } + }); + interruptThread.start(); + process.waitFor(); + interruptThread.interrupt(); + + if (process.exitValue() != 0) { + String str; + InputStream stdin = process.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(stdin)); + while ((str = reader.readLine()) != null) { + if (StringUtils.isBlank(str)) { + continue; + } + if (str.startsWith("ERROR")) { + return FAILED; + } + } + return SUCCESS; + } + return FAILED_WITH_EXIT_CODE; + } catch (InterruptedException e) { + System.out.println("Script " + script + "timed out."); + return TIMEOUT; + } catch (Exception e) { + System.out.println("Script failed with exception: " + e); + return FAILED_WITH_EXIT_CODE; + } finally { + if (interruptThread != null) + interruptThread.interrupt(); + } + } + + public static void main(String[] args) { + Map conf = ConfigUtils.readStormConfig(); + System.exit(healthCheck(conf)); + } + +} \ No newline at end of file
