abhishekagarwal87 commented on a change in pull request #11123:
URL: https://github.com/apache/druid/pull/11123#discussion_r620507152



##########
File path: 
server/src/main/java/org/apache/druid/segment/realtime/FireHydrant.java
##########
@@ -34,17 +34,53 @@
 
 import javax.annotation.Nullable;
 import java.io.Closeable;
+import java.io.File;
 import java.util.Optional;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Function;
 
 /**
+ *
  */
 public class FireHydrant
 {
   private final int count;
   private final AtomicReference<ReferenceCountingSegment> adapter;
   private volatile IncrementalIndex index;
+  private File persistedFile;
+  private SegmentId persistedSegmentId;
+
+  /**
+   * @return The persisted file path. This is needed to recreate mapped files 
before merging.
+   * it will be null for real time hydrants.
+   */
+  public @Nullable File getPersistedFile()
+  {
+    return persistedFile;
+  }
+
+  /**
+   * @return The persisted segment id. This is needed to recreate mapped files 
before merging.
+   * It will be null for real time hydrants
+   */
+  public @Nullable SegmentId getPersistedSegmentId()

Review comment:
       can you also add a note about why `persistedSegmentId` is required in 
addition to `getSegmentId()`? I understand that the latter will throw an NPE? 

##########
File path: 
indexing-service/src/test/java/org/apache/druid/indexing/appenderator/BatchAppenderatorTest.java
##########
@@ -0,0 +1,171 @@
+/*
+ * 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.indexing.appenderator;
+
+import com.google.common.base.Function;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import org.apache.druid.data.input.InputRow;
+import org.apache.druid.data.input.MapBasedInputRow;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.segment.realtime.appenderator.Appenderator;
+import org.apache.druid.segment.realtime.appenderator.AppenderatorTester;
+import org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec;
+import 
org.apache.druid.segment.realtime.appenderator.SegmentsAndCommitMetadata;
+import org.apache.druid.testing.InitializedNullHandlingTest;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.partition.LinearShardSpec;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.List;
+
+public class BatchAppenderatorTest extends InitializedNullHandlingTest
+{
+  private static final List<SegmentIdWithShardSpec> IDENTIFIERS = 
ImmutableList.of(
+      si("2000/2001", "A", 0),
+      si("2000/2001", "A", 1),
+      si("2001/2002", "A", 0)
+  );
+
+  @Test
+  public void testSimpleIngestion() throws Exception
+  {
+    try (final BatchAppenderatorTester tester = new BatchAppenderatorTester(2, 
true)) {
+      final Appenderator appenderator = tester.getAppenderator();
+      boolean thrown;
+
+      // startJob
+      Assert.assertEquals(null, appenderator.startJob());
+
+      // getDataSource
+      Assert.assertEquals(AppenderatorTester.DATASOURCE, 
appenderator.getDataSource());
+
+      // add
+      Assert.assertEquals(
+          1,
+          appenderator.add(IDENTIFIERS.get(0), ir("2000", "foo", 1), null)
+                      .getNumRowsInSegment()
+      );
+
+      Assert.assertEquals(
+          2,
+          appenderator.add(IDENTIFIERS.get(0), ir("2000", "bar", 2), null)
+                      .getNumRowsInSegment()
+      );
+
+      Assert.assertEquals(
+          1,
+          appenderator.add(IDENTIFIERS.get(1), ir("2000", "qux", 4), null)
+                      .getNumRowsInSegment()
+      );
+
+      // getSegments
+      Assert.assertEquals(IDENTIFIERS.subList(0, 2), 
sorted(appenderator.getSegments()));
+
+      // getRowCount
+      Assert.assertEquals(2, appenderator.getRowCount(IDENTIFIERS.get(0)));
+      Assert.assertEquals(1, appenderator.getRowCount(IDENTIFIERS.get(1)));
+      thrown = false;
+      try {
+        appenderator.getRowCount(IDENTIFIERS.get(2));
+      }
+      catch (IllegalStateException e) {
+        thrown = true;
+      }
+      Assert.assertTrue(thrown);
+
+      // push all
+      final SegmentsAndCommitMetadata segmentsAndCommitMetadata = 
appenderator.push(
+          appenderator.getSegments(),
+          null,
+          false
+      ).get();
+      Assert.assertEquals(
+          IDENTIFIERS.subList(0, 2),
+          sorted(
+              Lists.transform(
+                  segmentsAndCommitMetadata.getSegments(),
+                  new Function<DataSegment, SegmentIdWithShardSpec>()
+                  {
+                    @Override
+                    public SegmentIdWithShardSpec apply(DataSegment input)
+                    {
+                      return SegmentIdWithShardSpec.fromDataSegment(input);
+                    }
+                  }
+              )
+          )
+      );
+      Assert.assertEquals(sorted(tester.getPushedSegments()), 
sorted(segmentsAndCommitMetadata.getSegments()));
+
+      // clear
+      appenderator.clear();
+      Assert.assertTrue(appenderator.getSegments().isEmpty());
+    }
+  }
+
+  private static SegmentIdWithShardSpec si(String interval, String version, 
int partitionNum)
+  {
+    return new SegmentIdWithShardSpec(
+        AppenderatorTester.DATASOURCE,
+        Intervals.of(interval),
+        version,
+        new LinearShardSpec(partitionNum)
+    );
+  }
+
+  static InputRow ir(String ts, String dim, Object met)
+  {
+    return new MapBasedInputRow(
+        DateTimes.of(ts).getMillis(),
+        ImmutableList.of("dim"),
+        ImmutableMap.of(
+            "dim",
+            dim,
+            "met",
+            met
+        )
+    );
+  }
+
+  private static <T> List<T> sorted(final List<T> xs)

Review comment:
       is this method required? Since `SegmentIdWithShardSpec` and 
`DataSegment` implement `Comparable`, is calling `Collections.sort` on list 
directly possible? 

##########
File path: 
server/src/main/java/org/apache/druid/segment/realtime/appenderator/AppenderatorImpl.java
##########
@@ -813,6 +851,24 @@ private DataSegment mergeAndPush(
       Closer closer = Closer.create();
       try {
         for (FireHydrant fireHydrant : sink) {
+
+          // if batch, swap/persist did not memory map the incremental index, 
we need it mapped now:
+          if (!isRealTime()) {
+            // sanity:
+            if (fireHydrant.getPersistedFile() == null) {
+              throw new ISE("Persisted file for batch hydrant is null!");

Review comment:
       would you want any other info in this message to debug the exception? 
It's ok if not. 




-- 
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:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@druid.apache.org
For additional commands, e-mail: commits-h...@druid.apache.org

Reply via email to