Repository: storm Updated Branches: refs/heads/master d187a20e2 -> 561cecaab
translating timer Project: http://git-wip-us.apache.org/repos/asf/storm/repo Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/7f582529 Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/7f582529 Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/7f582529 Branch: refs/heads/master Commit: 7f582529a2a896786b809ce7b1619ab52f45431a Parents: 0a5813a Author: Boyang Jerry Peng <[email protected]> Authored: Tue Feb 9 09:41:47 2016 -0600 Committer: Boyang Jerry Peng <[email protected]> Committed: Thu Feb 18 10:32:05 2016 -0600 ---------------------------------------------------------------------- .../src/jvm/org/apache/storm/StormTimer.java | 199 +++++++++++++++++++ storm-core/src/jvm/org/apache/storm/Timer.java | 50 ----- 2 files changed, 199 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/storm/blob/7f582529/storm-core/src/jvm/org/apache/storm/StormTimer.java ---------------------------------------------------------------------- diff --git a/storm-core/src/jvm/org/apache/storm/StormTimer.java b/storm-core/src/jvm/org/apache/storm/StormTimer.java new file mode 100644 index 0000000..5267335 --- /dev/null +++ b/storm-core/src/jvm/org/apache/storm/StormTimer.java @@ -0,0 +1,199 @@ +/** + * 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; + +import org.apache.storm.utils.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Comparator; +import java.util.Random; +import java.util.concurrent.PriorityBlockingQueue; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicBoolean; + +public class StormTimer { + private static final Logger LOG = LoggerFactory.getLogger(StormTimer.class); + + public interface TimerFunc { + public void run(Object o); + } + + public static class StormTimerTask extends Thread { + + private PriorityBlockingQueue<Long> queue = new PriorityBlockingQueue<Long>(10, new Comparator() { + @Override + public int compare(Object o1, Object o2) { + return 0; + } + }); + + private AtomicBoolean active = new AtomicBoolean(false); + + private TimerFunc onKill; + + private TimerFunc afn; + + private Random random = new Random(); + + private Semaphore cancelNotifier = new Semaphore(0); + + private Object lock = new Object(); + + @Override + public void run() { + LOG.info("in run..."); + while (this.active.get()) { + try { + Long endTimeMillis; + synchronized (this.lock) { + endTimeMillis = this.queue.peek(); + } + if ((endTimeMillis != null) && (currentTimeMillis() >= endTimeMillis)) { + synchronized (this.lock) { + this.queue.poll(); + } + LOG.info("About to run function..."); + this.afn.run(null); + } else if (endTimeMillis != null) { + Time.sleep(Math.min(1000, (endTimeMillis - currentTimeMillis()))); + } else { + Time.sleep(1000); + } + } catch (Throwable t) { + this.onKill.run(t); + } + } + this.cancelNotifier.release(); + } + + public void setOnKillFunc(TimerFunc onKill) { + this.onKill = onKill; + } + + public void setFunc(TimerFunc func) { + this.afn = func; + } + + public void setActive(boolean flag) { + this.active.set(flag); + } + + public boolean isActive() { + return this.active.get(); + } + + public void add(long endTime) { + this.queue.add(endTime); + } + } + + public static StormTimerTask mkTimer(TimerFunc onKill, String name) { + LOG.info("making Timer..."); + StormTimerTask task = new StormTimerTask(); + task.setOnKillFunc(onKill); + task.setActive(true); + + task.setDaemon(true); + task.setPriority(Thread.MAX_PRIORITY); + task.start(); + return task; + } + public static void schedule(StormTimerTask task, int delaySecs, TimerFunc afn, boolean checkActive, int jitterMs) { + long endTimeMs = currentTimeMillis() + secsToMillisLong(delaySecs); + if (jitterMs > 0) { + endTimeMs = task.random.nextInt(jitterMs) + endTimeMs; + } + task.setFunc(afn); + synchronized (task.lock) { + task.add(endTimeMs); + } + } + public static void schedule(StormTimerTask task, int delaySecs, TimerFunc afn) { + schedule(task, delaySecs, afn, true, 0); + } + + public static void scheduleRecurring(final StormTimerTask task, int delaySecs, final int recurSecs, final TimerFunc afn) { + schedule(task, delaySecs, new TimerFunc() { + @Override + public void run(Object o) { + LOG.info("scheduleRecurring running..."); + afn.run(null); + LOG.info("scheduleRecurring schedule again..."); + + schedule(task, recurSecs, this, false, 0); + } + }); + } + + public static void scheduleRecurringWithJitter(final StormTimerTask task, int delaySecs, final int recurSecs, final int jitterMs, final TimerFunc afn) { + schedule(task, delaySecs, new TimerFunc() { + @Override + public void run(Object o) { + LOG.info("scheduleRecurringWithJitter running..."); + afn.run(null); + LOG.info("scheduleRecurringWithJitter schedule again..."); + + schedule(task, recurSecs, this, false, jitterMs); + } + }); + } + + public static void checkActive(StormTimerTask task) { + if (!task.isActive()) { + throw new IllegalStateException("Timer is not active"); + } + } + + public static void cancelTimer(StormTimerTask task) throws InterruptedException { + checkActive(task); + synchronized (task.lock) { + task.setActive(false); + task.interrupt(); + } + task.cancelNotifier.acquire(); + } + + public static boolean isTimerWaiting(StormTimerTask task) { + return Time.isThreadWaiting(task); + } + + /** + * function in util that haven't be translated to java + */ + + public static long secsToMillisLong(long secs) { + return secs * 1000; + } + + public static long currentTimeMillis() { + return Time.currentTimeMillis(); + } + + + public static void main(String[] argv) { + mkTimer(new TimerFunc() { + @Override + public void run(Object o) { + + } + }, "erer"); + } + +} http://git-wip-us.apache.org/repos/asf/storm/blob/7f582529/storm-core/src/jvm/org/apache/storm/Timer.java ---------------------------------------------------------------------- diff --git a/storm-core/src/jvm/org/apache/storm/Timer.java b/storm-core/src/jvm/org/apache/storm/Timer.java deleted file mode 100644 index c9a8868..0000000 --- a/storm-core/src/jvm/org/apache/storm/Timer.java +++ /dev/null @@ -1,50 +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; - -import java.util.Comparator; -import java.util.PriorityQueue; -import java.util.TimerTask; - -public class Timer { - - public interface KillFunc { - public void onKill(Throwable throwable); - } - - public static class StormTimerTask extends TimerTask { - - PriorityQueue queue = new PriorityQueue(10, new Comparator() { - @Override - public int compare(Object o1, Object o2) { - return 0; - } - }); - - @Override - public void run() { - - } - } - - public TimerTask mkTimer(String name) { - - } - -}
