You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by xi...@apache.org on 2018/11/20 20:32:01 UTC

samza git commit: SAMZA-1707: Samza onTimer method triggering before init

Repository: samza
Updated Branches:
  refs/heads/master ebeccd203 -> b05845912


SAMZA-1707: Samza onTimer method triggering before init

Currently there was a bug when registering a timer with a very short amount of delay, it might not be invoked since it depends on the creation of the run loop. This patch fixed the problem by double checking the ready timers when run loop is created (listener is registered.)

Author: xinyuiscool <xi...@linkedin.com>

Reviewers: Prateek M <pr...@apache.org>

Closes #810 from xinyuiscool/SAMZA-1707


Project: http://git-wip-us.apache.org/repos/asf/samza/repo
Commit: http://git-wip-us.apache.org/repos/asf/samza/commit/b0584591
Tree: http://git-wip-us.apache.org/repos/asf/samza/tree/b0584591
Diff: http://git-wip-us.apache.org/repos/asf/samza/diff/b0584591

Branch: refs/heads/master
Commit: b058459121b15d428a1cdf3e9d11f2151e45cd02
Parents: ebeccd2
Author: Xinyu Liu <xi...@apache.org>
Authored: Tue Nov 20 12:31:56 2018 -0800
Committer: xinyuiscool <xi...@gmail.com>
Committed: Tue Nov 20 12:31:56 2018 -0800

----------------------------------------------------------------------
 .../apache/samza/task/EpochTimeScheduler.java   |  6 +-
 .../test/functions/TestSchedulerFunction.java   | 93 ++++++++++++++++++++
 2 files changed, 98 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/samza/blob/b0584591/samza-core/src/main/java/org/apache/samza/task/EpochTimeScheduler.java
----------------------------------------------------------------------
diff --git a/samza-core/src/main/java/org/apache/samza/task/EpochTimeScheduler.java b/samza-core/src/main/java/org/apache/samza/task/EpochTimeScheduler.java
index 3f50d9a..820a6ae 100644
--- a/samza-core/src/main/java/org/apache/samza/task/EpochTimeScheduler.java
+++ b/samza-core/src/main/java/org/apache/samza/task/EpochTimeScheduler.java
@@ -48,7 +48,7 @@ public class EpochTimeScheduler {
   private final ScheduledExecutorService executor;
   private final Map<Object, ScheduledFuture> scheduledFutures = new ConcurrentHashMap<>();
   private final Map<TimerKey<?>, ScheduledCallback> readyTimers = new ConcurrentHashMap<>();
-  private TimerListener timerListener;
+  private volatile TimerListener timerListener;
 
   public static EpochTimeScheduler create(ScheduledExecutorService executor) {
     return new EpochTimeScheduler(executor);
@@ -83,6 +83,10 @@ public class EpochTimeScheduler {
 
   void registerListener(TimerListener listener) {
     timerListener = listener;
+
+    if (!readyTimers.isEmpty()) {
+      timerListener.onTimer();
+    }
   }
 
   public Map<TimerKey<?>, ScheduledCallback> removeReadyTimers() {

http://git-wip-us.apache.org/repos/asf/samza/blob/b0584591/samza-test/src/test/java/org/apache/samza/test/functions/TestSchedulerFunction.java
----------------------------------------------------------------------
diff --git a/samza-test/src/test/java/org/apache/samza/test/functions/TestSchedulerFunction.java b/samza-test/src/test/java/org/apache/samza/test/functions/TestSchedulerFunction.java
new file mode 100644
index 0000000..ce18952
--- /dev/null
+++ b/samza-test/src/test/java/org/apache/samza/test/functions/TestSchedulerFunction.java
@@ -0,0 +1,93 @@
+/*
+ * 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.samza.test.functions;
+
+import org.apache.samza.application.StreamApplication;
+import org.apache.samza.application.descriptors.StreamApplicationDescriptor;
+import org.apache.samza.operators.Scheduler;
+import org.apache.samza.operators.functions.MapFunction;
+import org.apache.samza.operators.functions.ScheduledFunction;
+import org.apache.samza.serializers.IntegerSerde;
+import org.apache.samza.test.framework.TestRunner;
+import org.apache.samza.test.framework.system.descriptors.InMemoryInputDescriptor;
+import org.apache.samza.test.framework.system.descriptors.InMemorySystemDescriptor;
+import org.junit.Test;
+
+import java.time.Duration;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class TestSchedulerFunction {
+  static AtomicBoolean timerFired = new AtomicBoolean(false);
+
+  @Test
+  public void testImmediateTimer() {
+    final InMemorySystemDescriptor isd = new InMemorySystemDescriptor("test");
+    final InMemoryInputDescriptor<Integer> imid = isd.getInputDescriptor("test-input", new IntegerSerde());
+
+    StreamApplication app = new StreamApplication() {
+      @Override
+      public void describe(StreamApplicationDescriptor appDescriptor) {
+
+        appDescriptor.getInputStream(imid)
+            .map(new TestFunction());
+      }
+    };
+
+    TestRunner
+        .of(app)
+        .addInputStream(imid, Arrays.asList(1, 2, 3, 4, 5))
+        .run(Duration.ofSeconds(1));
+
+    assertTrue(timerFired.get());
+  }
+
+  private static class TestFunction implements MapFunction<Integer, Void>, ScheduledFunction<String, Void> {
+
+    @Override
+    public Void apply(Integer message) {
+      try {
+        // This is to avoid the container shutdown before the timer fired
+        Thread.sleep(1);
+      } catch (Exception e) {
+      }
+      return null;
+    }
+
+    @Override
+    public void schedule(Scheduler<String> scheduler) {
+      scheduler.schedule("haha", System.currentTimeMillis());
+    }
+
+    @Override
+    public Collection<Void> onCallback(String key, long timestamp) {
+      assertFalse(timerFired.get());
+
+      timerFired.compareAndSet(false, true);
+
+      return Collections.emptyList();
+    }
+  }
+}