You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@uniffle.apache.org by "smallzhongfeng (via GitHub)" <gi...@apache.org> on 2023/04/10 12:03:49 UTC

[GitHub] [incubator-uniffle] smallzhongfeng commented on a diff in pull request #785: [#291][part-1] feat(client): make read handler thread safe

smallzhongfeng commented on code in PR #785:
URL: https://github.com/apache/incubator-uniffle/pull/785#discussion_r1161661615


##########
storage/src/main/java/org/apache/uniffle/storage/handler/impl/DataSkippableReadHandler.java:
##########
@@ -28,61 +29,86 @@
 import org.apache.uniffle.common.ShuffleDataResult;
 import org.apache.uniffle.common.ShuffleDataSegment;
 import org.apache.uniffle.common.ShuffleIndexResult;
+import org.apache.uniffle.common.segment.SegmentSplitter;
 import org.apache.uniffle.common.segment.SegmentSplitterFactory;
 
 public abstract class DataSkippableReadHandler extends AbstractClientReadHandler {
   private static final Logger LOG = LoggerFactory.getLogger(DataSkippableReadHandler.class);
 
-  protected List<ShuffleDataSegment> shuffleDataSegments = Lists.newArrayList();
-  protected int segmentIndex = 0;
+  private volatile int segmentSize = 0;
+  private volatile ConcurrentLinkedQueue<ShuffleDataSegment> shuffleDataSegmentsQueue;
 
   protected Roaring64NavigableMap expectBlockIds;
   protected Roaring64NavigableMap processBlockIds;
 
-  protected ShuffleDataDistributionType distributionType;
-  protected Roaring64NavigableMap expectTaskIds;
+  private SegmentSplitter segmentSplitter;
 
-  public DataSkippableReadHandler(
+  @VisibleForTesting
+  DataSkippableReadHandler(
       String appId,
       int shuffleId,
       int partitionId,
       int readBufferSize,
       Roaring64NavigableMap expectBlockIds,
       Roaring64NavigableMap processBlockIds,
-      ShuffleDataDistributionType distributionType,
-      Roaring64NavigableMap expectTaskIds) {
+      SegmentSplitter segmentSplitter) {
     this.appId = appId;
     this.shuffleId = shuffleId;
     this.partitionId = partitionId;
     this.readBufferSize = readBufferSize;
     this.expectBlockIds = expectBlockIds;
     this.processBlockIds = processBlockIds;
-    this.distributionType = distributionType;
-    this.expectTaskIds = expectTaskIds;
+    this.segmentSplitter = segmentSplitter;
+  }
+
+  public DataSkippableReadHandler(
+      String appId,
+      int shuffleId,
+      int partitionId,
+      int readBufferSize,
+      Roaring64NavigableMap expectBlockIds,
+      Roaring64NavigableMap processBlockIds,
+      ShuffleDataDistributionType distributionType,
+      Roaring64NavigableMap expectTaskIds) {
+    this(
+        appId,
+        shuffleId,
+        partitionId,
+        readBufferSize,
+        expectBlockIds,
+        processBlockIds,
+        SegmentSplitterFactory
+            .getInstance()
+            .get(distributionType, expectTaskIds, readBufferSize)
+    );
   }
 
   protected abstract ShuffleIndexResult readShuffleIndex();
 
   protected abstract ShuffleDataResult readShuffleData(ShuffleDataSegment segment);
 
+  // Thread safe
   public ShuffleDataResult readShuffleData() {
-    if (shuffleDataSegments.isEmpty()) {
-      ShuffleIndexResult shuffleIndexResult = readShuffleIndex();
-      if (shuffleIndexResult == null || shuffleIndexResult.isEmpty()) {
-        return null;
+    if (shuffleDataSegmentsQueue == null) {
+      synchronized (this) {
+        if (shuffleDataSegmentsQueue == null) {
+          ShuffleIndexResult shuffleIndexResult = readShuffleIndex();
+          if (shuffleIndexResult == null || shuffleIndexResult.isEmpty()) {
+            return null;
+          }
+          List<ShuffleDataSegment> shuffleDataSegments = segmentSplitter.split(shuffleIndexResult);
+          segmentSize = shuffleDataSegments.size();
+          final ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue();

Review Comment:
   Can `queue` become a global variable?



##########
storage/src/test/java/org/apache/uniffle/storage/handler/impl/ComposedClientReadHandlerTest.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.uniffle.storage.handler.impl;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.stream.IntStream;
+
+import org.junit.jupiter.api.Test;
+
+import org.apache.uniffle.common.ShuffleDataResult;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class ComposedClientReadHandlerTest extends ClientHandlerTestBase {
+
+  @Test
+  public void testSyncRead() {
+    FakeHandler hotHandler = createFakeHandler();
+    FakeHandler warmHandler = createFakeHandler();
+    FakeHandler coldHandler = createFakeHandler();
+    ComposedClientReadHandler composedClientReadHandler =
+        new ComposedClientReadHandler(null, hotHandler, warmHandler, coldHandler);
+
+    for (int i = 0; i < 300; i++) {
+      ShuffleDataResult dataResult = composedClientReadHandler.readShuffleData();
+      assertNotNull(dataResult);
+    }
+    assertNull(composedClientReadHandler.readShuffleData());
+    assertEquals(100, hotHandler.blockIds.size());
+    assertEquals(100, warmHandler.blockIds.size());
+    assertEquals(100, coldHandler.blockIds.size());
+  }
+
+  @Test
+  public void testAsyncRead() throws Exception {
+    FakeHandler hotHandler = createFakeHandler();
+    FakeHandler warmHandler = createFakeHandler();
+    FakeHandler coldHandler = createFakeHandler();
+
+    ComposedClientReadHandler composedClientReadHandler =
+        new ComposedClientReadHandler(null, hotHandler, warmHandler, coldHandler);
+
+    ExecutorService executorService = Executors.newFixedThreadPool(10);
+    CountDownLatch latch = new CountDownLatch(10);

Review Comment:
   Maybe`Awaitility.await().timeout()` is better than CountDownLatch in test. WDYT



##########
storage/src/main/java/org/apache/uniffle/storage/handler/impl/MemoryClientReadHandler.java:
##########
@@ -54,8 +54,9 @@ public MemoryClientReadHandler(
     this.expectTaskIds = expectTaskIds;
   }
 
+  // todo: make thread safe
   @Override
-  public ShuffleDataResult readShuffleData() {
+  public synchronized ShuffleDataResult readShuffleData() {

Review Comment:
   Will read write locks perform better than `synchronized`.



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@uniffle.apache.org
For additional commands, e-mail: issues-help@uniffle.apache.org