You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2021/04/26 07:15:10 UTC

[GitHub] [druid] maytasm commented on a change in pull request #11123: Avoid memory mapping hydrants after they are persisted & after they are merged for native batch ingestion

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



##########
File path: server/src/main/java/org/apache/druid/segment/realtime/appenderator/AppenderatorImpl.java
##########
@@ -558,35 +570,44 @@ public void clear() throws InterruptedException
     final List<Pair<FireHydrant, SegmentIdWithShardSpec>> indexesToPersist = new ArrayList<>();
     int numPersistedRows = 0;
     long bytesPersisted = 0L;
+    AtomicLong totalHydrantsCount = new AtomicLong();
+    AtomicLong totalHydrantsPersisted = new AtomicLong();
+    final long totalSinks = sinks.size();
     for (Map.Entry<SegmentIdWithShardSpec, Sink> entry : sinks.entrySet()) {
       final SegmentIdWithShardSpec identifier = entry.getKey();
       final Sink sink = entry.getValue();
       if (sink == null) {
         throw new ISE("No sink for identifier: %s", identifier);
       }
       final List<FireHydrant> hydrants = Lists.newArrayList(sink);
+      totalHydrantsCount.addAndGet(hydrants.size());
       currentHydrants.put(identifier.toString(), hydrants.size());
       numPersistedRows += sink.getNumRowsInMemory();
       bytesPersisted += sink.getBytesInMemory();
 
       final int limit = sink.isWritable() ? hydrants.size() - 1 : hydrants.size();
 
+      // gather hydrants that have not been persisted:
       for (FireHydrant hydrant : hydrants.subList(0, limit)) {
         if (!hydrant.hasSwapped()) {
           log.debug("Hydrant[%s] hasn't persisted yet, persisting. Segment[%s]", hydrant, identifier);
           indexesToPersist.add(Pair.of(hydrant, identifier));
+          totalHydrantsPersisted.addAndGet(1);
         }
       }
 
       if (sink.swappable()) {
+        // It is swappable. Get the old one to persist it and create a new one:
         indexesToPersist.add(Pair.of(sink.swap(), identifier));
+        totalHydrantsPersisted.addAndGet(1);

Review comment:
       Why is the count for hydrant increased by 1 here?

##########
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)

Review comment:
       Can you rename this to something more readable?

##########
File path: server/src/main/java/org/apache/druid/segment/realtime/appenderator/AppenderatorImpl.java
##########
@@ -558,35 +570,44 @@ public void clear() throws InterruptedException
     final List<Pair<FireHydrant, SegmentIdWithShardSpec>> indexesToPersist = new ArrayList<>();
     int numPersistedRows = 0;
     long bytesPersisted = 0L;
+    AtomicLong totalHydrantsCount = new AtomicLong();

Review comment:
       What's the reason for using AtomicLong here?

##########
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)

Review comment:
       Can you rename this to something more readable?




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



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