jihoonson commented on a change in pull request #8901: Support inputFormat and 
inputSource for sampler
URL: https://github.com/apache/incubator-druid/pull/8901#discussion_r348271923
 
 

 ##########
 File path: 
core/src/main/java/org/apache/druid/data/input/impl/TimedShutoffInputSourceReader.java
 ##########
 @@ -0,0 +1,116 @@
+/*
+ * 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.druid.data.input.impl;
+
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.InputRowListPlusRawValues;
+import org.apache.druid.data.input.InputSourceReader;
+import org.apache.druid.java.util.common.concurrent.Execs;
+import org.apache.druid.java.util.common.io.Closer;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.java.util.common.parsers.CloseableIterator;
+import org.joda.time.DateTime;
+
+import java.io.IOException;
+import java.util.NoSuchElementException;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+public class TimedShutoffInputSourceReader implements InputSourceReader
+{
+  private static final Logger LOG = new 
Logger(TimedShutoffInputSourceReader.class);
+
+  private final InputSourceReader delegate;
+  private final DateTime shutoffTime;
+
+  public TimedShutoffInputSourceReader(InputSourceReader delegate, DateTime 
shutoffTime)
+  {
+    this.delegate = delegate;
+    this.shutoffTime = shutoffTime;
+  }
+
+  @Override
+  public CloseableIterator<InputRow> read() throws IOException
+  {
+    final ScheduledExecutorService shutdownExec = 
Execs.scheduledSingleThreaded("timed-shutoff-reader-%d");
+    final CloseableIterator<InputRow> delegateIterator = delegate.read();
+    return decorateShutdownTimeout(shutdownExec, delegateIterator);
+  }
+
+  @Override
+  public CloseableIterator<InputRowListPlusRawValues> sample() throws 
IOException
+  {
+    final ScheduledExecutorService shutdownExec = 
Execs.scheduledSingleThreaded("timed-shutoff-reader-%d");
+    final CloseableIterator<InputRowListPlusRawValues> delegateIterator = 
delegate.sample();
+    return decorateShutdownTimeout(shutdownExec, delegateIterator);
+  }
+
+  private <T> CloseableIterator<T> decorateShutdownTimeout(
+      ScheduledExecutorService exec,
+      CloseableIterator<T> delegateIterator
+  )
+  {
+    final Closer closer = Closer.create();
+    closer.register(delegateIterator);
+    closer.register(exec::shutdownNow);
+    final CloseableIterator<T> wrappingIterator = new CloseableIterator<T>()
+    {
+      volatile boolean closed;
+
+      @Override
+      public boolean hasNext()
+      {
+        return !closed && delegateIterator.hasNext();
+      }
+
+      @Override
+      public T next()
+      {
+        if (!hasNext()) {
+          throw new NoSuchElementException();
 
 Review comment:
   Hmm, good point. I fixed to cache the next item in `hasNext()`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to