update according to review comments
Project: http://git-wip-us.apache.org/repos/asf/storm/repo Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/9603e309 Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/9603e309 Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/9603e309 Branch: refs/heads/master Commit: 9603e30961dbb2391bf309031f64c29f808277f7 Parents: f54f2cf Author: basti.lj <[email protected]> Authored: Mon Feb 22 22:08:43 2016 +0800 Committer: basti.lj <[email protected]> Committed: Mon Feb 22 22:08:43 2016 +0800 ---------------------------------------------------------------------- .../src/clj/org/apache/storm/daemon/acker.clj | 58 --------- .../src/clj/org/apache/storm/daemon/common.clj | 17 ++- storm-core/src/clj/org/apache/storm/testing.clj | 11 +- .../src/jvm/org/apache/storm/daemon/Acker.java | 127 +++++++++++++++++++ .../jvm/org/apache/storm/daemon/AckerBolt.java | 127 ------------------- .../src/jvm/org/apache/storm/utils/Utils.java | 4 +- 6 files changed, 144 insertions(+), 200 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/storm/blob/9603e309/storm-core/src/clj/org/apache/storm/daemon/acker.clj ---------------------------------------------------------------------- diff --git a/storm-core/src/clj/org/apache/storm/daemon/acker.clj b/storm-core/src/clj/org/apache/storm/daemon/acker.clj deleted file mode 100644 index 9aa15ae..0000000 --- a/storm-core/src/clj/org/apache/storm/daemon/acker.clj +++ /dev/null @@ -1,58 +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.daemon.acker - (:import [org.apache.storm.task OutputCollector TopologyContext IBolt] - [org.apache.storm.utils Utils]) - (:import [org.apache.storm.tuple Tuple Fields]) - (:import [org.apache.storm.utils Container RotatingMap MutableObject]) - (:import [java.util List Map]) - (:import [org.apache.storm Constants] - (org.apache.storm.daemon AckerBolt)) - (:use [org.apache.storm config log]) - (:gen-class - :init init - :implements [org.apache.storm.task.IBolt] - :constructors {[] []} - :state state)) - -(def ACKER-COMPONENT-ID AckerBolt/ACKER_COMPONENT_ID) -(def ACKER-INIT-STREAM-ID AckerBolt/ACKER_INIT_STREAM_ID) -(def ACKER-ACK-STREAM-ID AckerBolt/ACKER_ACK_STREAM_ID) -(def ACKER-FAIL-STREAM-ID AckerBolt/ACKER_FAIL_STREAM_ID) - -(defn mk-acker-bolt [] - (let [output-collector (MutableObject.) - pending (MutableObject.)] - (AckerBolt.))) - -(defn -init [] - [[] (Container.)]) - -(defn -prepare [^org.apache.storm.daemon.acker this conf context collector] - (let [^IBolt ret (mk-acker-bolt)] - (.. this state (set ret)) - (.prepare ret conf context collector) - )) - -(defn -execute [^org.apache.storm.daemon.acker this tuple] - (let [^IBolt delegate (.. this state (get))] - (.execute delegate tuple) - )) - -(defn -cleanup [^org.apache.storm.daemon.acker this] - (let [^IBolt delegate (.. this state (get))] - (.cleanup delegate) - )) http://git-wip-us.apache.org/repos/asf/storm/blob/9603e309/storm-core/src/clj/org/apache/storm/daemon/common.clj ---------------------------------------------------------------------- diff --git a/storm-core/src/clj/org/apache/storm/daemon/common.clj b/storm-core/src/clj/org/apache/storm/daemon/common.clj index db7fd40..4076e38 100644 --- a/storm-core/src/clj/org/apache/storm/daemon/common.clj +++ b/storm-core/src/clj/org/apache/storm/daemon/common.clj @@ -29,9 +29,9 @@ (:import [java.io InterruptedIOException] [org.json.simple JSONValue]) (:import [java.util HashMap]) - (:import [org.apache.storm Thrift]) + (:import [org.apache.storm Thrift] + (org.apache.storm.daemon Acker)) (:require [clojure.set :as set]) - (:require [org.apache.storm.daemon.acker :as acker]) (:require [metrics.reporters.jmx :as jmx]) (:require [metrics.core :refer [default-registry]])) @@ -46,10 +46,10 @@ (start-metrics-reporter reporter conf))) -(def ACKER-COMPONENT-ID acker/ACKER-COMPONENT-ID) -(def ACKER-INIT-STREAM-ID acker/ACKER-INIT-STREAM-ID) -(def ACKER-ACK-STREAM-ID acker/ACKER-ACK-STREAM-ID) -(def ACKER-FAIL-STREAM-ID acker/ACKER-FAIL-STREAM-ID) +(def ACKER-COMPONENT-ID Acker/ACKER_COMPONENT_ID) +(def ACKER-INIT-STREAM-ID Acker/ACKER_INIT_STREAM_ID) +(def ACKER-ACK-STREAM-ID Acker/ACKER_ACK_STREAM_ID) +(def ACKER-FAIL-STREAM-ID Acker/ACKER_FAIL_STREAM_ID) (def SYSTEM-STREAM-ID "__system") @@ -222,10 +222,13 @@ ))] (merge spout-inputs bolt-inputs))) +(defn mk-acker-bolt [] + (Acker.)) + (defn add-acker! [storm-conf ^StormTopology ret] (let [num-executors (if (nil? (storm-conf TOPOLOGY-ACKER-EXECUTORS)) (storm-conf TOPOLOGY-WORKERS) (storm-conf TOPOLOGY-ACKER-EXECUTORS)) acker-bolt (Thrift/prepareSerializedBoltDetails (acker-inputs ret) - (new org.apache.storm.daemon.acker) + (mk-acker-bolt) {ACKER-ACK-STREAM-ID (Thrift/directOutputFields ["id"]) ACKER-FAIL-STREAM-ID (Thrift/directOutputFields ["id"]) } http://git-wip-us.apache.org/repos/asf/storm/blob/9603e309/storm-core/src/clj/org/apache/storm/testing.clj ---------------------------------------------------------------------- diff --git a/storm-core/src/clj/org/apache/storm/testing.clj b/storm-core/src/clj/org/apache/storm/testing.clj index 7817929..f04befc 100644 --- a/storm-core/src/clj/org/apache/storm/testing.clj +++ b/storm-core/src/clj/org/apache/storm/testing.clj @@ -50,7 +50,6 @@ (org.apache.storm.messaging IContext) [org.json.simple JSONValue]) (:require [org.apache.storm [zookeeper :as zk]]) - (:require [org.apache.storm.daemon.acker :as acker]) (:use [org.apache.storm cluster util config log local-state-converter]) (:use [org.apache.storm.internal thrift])) @@ -675,9 +674,9 @@ (.put "transferred" (AtomicInteger. 0)) (.put "processed" (AtomicInteger. 0)))) (with-var-roots - [acker/mk-acker-bolt - (let [old# acker/mk-acker-bolt] - (fn [& args#] (NonRichBoltTracker. (apply old# args#) id#))) + [common/mk-acker-bolt + (let [old# common/mk-acker-bolt] + (fn [& args#] (NonRichBoltTracker. (apply old# args#) id#))) ;; critical that this particular function is overridden here, ;; since the transferred stat needs to be incremented at the moment ;; of tuple emission (and not on a separate thread later) for @@ -692,8 +691,8 @@ (increment-global! id# "transferred" 1) (apply transferrer# args2#)))))] (with-simulated-time-local-cluster [~cluster-sym ~@cluster-args] - (let [~cluster-sym (assoc-track-id ~cluster-sym id#)] - ~@body))) + (let [~cluster-sym (assoc-track-id ~cluster-sym id#)] + ~@body))) (RegisteredGlobalState/clearState id#))) (defn tracked-wait http://git-wip-us.apache.org/repos/asf/storm/blob/9603e309/storm-core/src/jvm/org/apache/storm/daemon/Acker.java ---------------------------------------------------------------------- diff --git a/storm-core/src/jvm/org/apache/storm/daemon/Acker.java b/storm-core/src/jvm/org/apache/storm/daemon/Acker.java new file mode 100644 index 0000000..98f73df --- /dev/null +++ b/storm-core/src/jvm/org/apache/storm/daemon/Acker.java @@ -0,0 +1,127 @@ +/** + * 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.daemon; + +import org.apache.storm.task.IBolt; +import org.apache.storm.task.OutputCollector; +import org.apache.storm.task.TopologyContext; +import org.apache.storm.tuple.Tuple; +import org.apache.storm.tuple.Values; +import org.apache.storm.utils.RotatingMap; +import org.apache.storm.utils.TupleUtils; +import org.apache.storm.utils.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Map; + +public class Acker implements IBolt { + private static final Logger LOG = LoggerFactory.getLogger(Acker.class); + + private static final long serialVersionUID = 4430906880683183091L; + + public static final String ACKER_COMPONENT_ID = "__acker"; + public static final String ACKER_INIT_STREAM_ID = "__ack_init"; + public static final String ACKER_ACK_STREAM_ID = "__ack_ack"; + public static final String ACKER_FAIL_STREAM_ID = "__ack_fail"; + + public static final int TIMEOUT_BUCKET_NUM = 3; + + private OutputCollector collector; + private RotatingMap<Object, AckObject> pending; + + private class AckObject { + public long val = 0L; + public Integer spoutTask = null; + public boolean failed = false; + + // val xor value + public void updateAck(Long value) { + val = Utils.bitXor(val, value); + } + } + + @Override + public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { + this.collector = collector; + this.pending = new RotatingMap<Object, AckObject>(TIMEOUT_BUCKET_NUM); + } + + @Override + public void execute(Tuple input) { + if (TupleUtils.isTick(input)) { + Map<Object, AckObject> tmp = pending.rotate(); + LOG.debug("Number of timeout tuples:{}", tmp.size()); + return; + } + + String streamId = input.getSourceStreamId(); + Object id = input.getValue(0); + AckObject curr = pending.get(id); + if (ACKER_INIT_STREAM_ID.equals(streamId)) { + if (curr == null) { + curr = new AckObject(); + curr.val = input.getLong(1); + curr.spoutTask = input.getInteger(2); + pending.put(id, curr); + } else { + // If receiving bolt's ack before the init message from spout, just update the xor value. + curr.updateAck(input.getLong(1)); + curr.spoutTask = input.getInteger(2); + } + } else if (ACKER_ACK_STREAM_ID.equals(streamId)) { + if (curr != null) { + curr.updateAck(input.getLong(1)); + } else { + curr = new AckObject(); + curr.val = input.getLong(1); + pending.put(id, curr); + } + } else if (ACKER_FAIL_STREAM_ID.equals(streamId)) { + if (curr == null) { + // The tuple has been already timeout or failed. So, do nothing + return; + } + curr.failed = true; + } else { + LOG.warn("Unknown source stream {} from task-{}", streamId, input.getSourceTask()); + return; + } + + Integer task = curr.spoutTask; + if (task != null) { + if (curr.val == 0) { + pending.remove(id); + collector.emitDirect(task, ACKER_ACK_STREAM_ID, new Values(id)); + } else { + if (curr.failed) { + pending.remove(id); + collector.emitDirect(task, ACKER_FAIL_STREAM_ID, new Values(id)); + } + } + } + + collector.ack(input); + } + + @Override + public void cleanup() { + LOG.info("Acker: cleanup successfully"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/storm/blob/9603e309/storm-core/src/jvm/org/apache/storm/daemon/AckerBolt.java ---------------------------------------------------------------------- diff --git a/storm-core/src/jvm/org/apache/storm/daemon/AckerBolt.java b/storm-core/src/jvm/org/apache/storm/daemon/AckerBolt.java deleted file mode 100644 index 7c1514f..0000000 --- a/storm-core/src/jvm/org/apache/storm/daemon/AckerBolt.java +++ /dev/null @@ -1,127 +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. - */ -package org.apache.storm.daemon; - -import org.apache.storm.task.IBolt; -import org.apache.storm.task.OutputCollector; -import org.apache.storm.task.TopologyContext; -import org.apache.storm.tuple.Tuple; -import org.apache.storm.tuple.Values; -import org.apache.storm.utils.RotatingMap; -import org.apache.storm.utils.TupleUtils; -import org.apache.storm.utils.Utils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.List; -import java.util.Map; - -public class AckerBolt implements IBolt { - private static final Logger LOG = LoggerFactory.getLogger(AckerBolt.class); - - private static final long serialVersionUID = 4430906880683183091L; - - public static final String ACKER_COMPONENT_ID = "__acker"; - public static final String ACKER_INIT_STREAM_ID = "__ack_init"; - public static final String ACKER_ACK_STREAM_ID = "__ack_ack"; - public static final String ACKER_FAIL_STREAM_ID = "__ack_fail"; - - public static final int TIMEOUT_BUCKET_NUM = 3; - - private OutputCollector collector; - private RotatingMap<Object, AckObject> pending; - - private class AckObject { - public long val = 0L; - public Integer spoutTask = null; - public boolean failed = false; - - // val xor value - public void updateAck(Object value) { - val = Utils.bitXor(val, value); - } - } - - @Override - public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { - this.collector = collector; - this.pending = new RotatingMap<Object, AckObject>(TIMEOUT_BUCKET_NUM); - } - - @Override - public void execute(Tuple input) { - if (TupleUtils.isTick(input)) { - Map<Object, AckObject> tmp = pending.rotate(); - LOG.debug("Number of timeout tuples:{}", tmp.size()); - return; - } - - String streamId = input.getSourceStreamId(); - Object id = input.getValue(0); - AckObject curr = pending.get(id); - if (ACKER_INIT_STREAM_ID.equals(streamId)) { - if (curr == null) { - curr = new AckObject(); - curr.val = input.getLong(1); - curr.spoutTask = input.getInteger(2); - pending.put(id, curr); - } else { - // If receiving bolt's ack before the init message from spout, just update the xor value. - curr.updateAck(input.getLong(1)); - curr.spoutTask = input.getInteger(2); - } - } else if (ACKER_ACK_STREAM_ID.equals(streamId)) { - if (curr != null) { - curr.updateAck(input.getLong(1)); - } else { - curr = new AckObject(); - curr.val = input.getLong(1); - pending.put(id, curr); - } - } else if (ACKER_FAIL_STREAM_ID.equals(streamId)) { - if (curr == null) { - // The tuple has been already timeout or failed. So, do nothing - return; - } - curr.failed = true; - } else { - LOG.warn("Unknown source stream {} from task-{}", streamId, input.getSourceTask()); - return; - } - - Integer task = curr.spoutTask; - if (task != null) { - if (curr.val == 0) { - pending.remove(id); - collector.emitDirect(task, ACKER_ACK_STREAM_ID, new Values(id)); - } else { - if (curr.failed) { - pending.remove(id); - collector.emitDirect(task, ACKER_FAIL_STREAM_ID, new Values(id)); - } - } - } - - collector.ack(input); - } - - @Override - public void cleanup() { - LOG.info("Acker: cleanup successfully"); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/storm/blob/9603e309/storm-core/src/jvm/org/apache/storm/utils/Utils.java ---------------------------------------------------------------------- diff --git a/storm-core/src/jvm/org/apache/storm/utils/Utils.java b/storm-core/src/jvm/org/apache/storm/utils/Utils.java index c2c5e62..43c00fc 100644 --- a/storm-core/src/jvm/org/apache/storm/utils/Utils.java +++ b/storm-core/src/jvm/org/apache/storm/utils/Utils.java @@ -2278,7 +2278,7 @@ public class Utils { return process; } - public static long bitXor(Object a, Object b) { - return ((Long) a) ^ ((Long) b); + public static long bitXor(Long a, Long b) { + return a ^ b; } }
