You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@uniffle.apache.org by "jerqi (via GitHub)" <gi...@apache.org> on 2023/03/17 15:38:52 UTC
[GitHub] [incubator-uniffle] jerqi commented on a diff in pull request #714: [#706] Implement spill method to avoid memory deadlock
jerqi commented on code in PR #714:
URL: https://github.com/apache/incubator-uniffle/pull/714#discussion_r1140392392
##########
client-spark/common/src/main/java/org/apache/spark/shuffle/writer/WriteBufferManager.java:
##########
@@ -247,10 +286,61 @@ private void requestExecutorMemory(long leastMem) {
}
}
+ public List<AddBlockEvent> buildBlockEvents(List<ShuffleBlockInfo> shuffleBlockInfoList) {
+ long totalSize = 0;
+ long memoryUsed = 0;
+ List<AddBlockEvent> events = new ArrayList<>();
+ List<ShuffleBlockInfo> shuffleBlockInfosPerEvent = Lists.newArrayList();
+ for (ShuffleBlockInfo sbi : shuffleBlockInfoList) {
+ totalSize += sbi.getSize();
+ memoryUsed += sbi.getFreeMemory();
+ shuffleBlockInfosPerEvent.add(sbi);
+ // split shuffle data according to the size
+ if (totalSize > sendSizeLimit) {
+ LOG.info("Build event with " + shuffleBlockInfosPerEvent.size()
+ + " blocks and " + totalSize + " bytes");
+ final long _memoryUsed = memoryUsed;
+ events.add(
+ new AddBlockEvent(taskId, shuffleBlockInfosPerEvent, () -> freeAllocatedMemory(_memoryUsed))
+ );
+ shuffleBlockInfosPerEvent = Lists.newArrayList();
+ totalSize = 0;
+ memoryUsed = 0;
+ }
+ }
+ if (!shuffleBlockInfosPerEvent.isEmpty()) {
+ LOG.info("Build event with " + shuffleBlockInfosPerEvent.size()
+ + " blocks and " + totalSize + " bytes");
+ final long _memoryUsed = memoryUsed;
Review Comment:
Maybe we should add some comment to explain why we need to use temporary variable.
##########
client-spark/common/src/main/java/org/apache/spark/shuffle/writer/WriteBufferManager.java:
##########
@@ -247,10 +286,61 @@ private void requestExecutorMemory(long leastMem) {
}
}
+ public List<AddBlockEvent> buildBlockEvents(List<ShuffleBlockInfo> shuffleBlockInfoList) {
+ long totalSize = 0;
+ long memoryUsed = 0;
+ List<AddBlockEvent> events = new ArrayList<>();
+ List<ShuffleBlockInfo> shuffleBlockInfosPerEvent = Lists.newArrayList();
+ for (ShuffleBlockInfo sbi : shuffleBlockInfoList) {
+ totalSize += sbi.getSize();
+ memoryUsed += sbi.getFreeMemory();
+ shuffleBlockInfosPerEvent.add(sbi);
+ // split shuffle data according to the size
+ if (totalSize > sendSizeLimit) {
+ LOG.info("Build event with " + shuffleBlockInfosPerEvent.size()
+ + " blocks and " + totalSize + " bytes");
+ final long _memoryUsed = memoryUsed;
+ events.add(
+ new AddBlockEvent(taskId, shuffleBlockInfosPerEvent, () -> freeAllocatedMemory(_memoryUsed))
+ );
+ shuffleBlockInfosPerEvent = Lists.newArrayList();
+ totalSize = 0;
+ memoryUsed = 0;
+ }
+ }
+ if (!shuffleBlockInfosPerEvent.isEmpty()) {
+ LOG.info("Build event with " + shuffleBlockInfosPerEvent.size()
+ + " blocks and " + totalSize + " bytes");
+ final long _memoryUsed = memoryUsed;
+ events.add(
+ new AddBlockEvent(taskId, shuffleBlockInfosPerEvent, () -> freeAllocatedMemory(_memoryUsed))
+ );
+ }
+ return events;
+ }
+
@Override
public long spill(long size, MemoryConsumer trigger) {
- // there is no spill for such situation
- return 0;
+ List<AddBlockEvent> events = buildBlockEvents(clear());
+ List<CompletableFuture<Long>> futures = events.stream().map(x -> spillFunc.apply(x)).collect(Collectors.toList());
+ CompletableFuture<Void> allOfFutures =
+ CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
+ try {
+ allOfFutures.get(memorySpillTimeoutSec, TimeUnit.SECONDS);
+ } catch (TimeoutException timeoutException) {
+ // ignore this.
Review Comment:
Why don't we process timeout exception?
##########
client-spark/common/src/main/java/org/apache/spark/shuffle/writer/DataPusher.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.spark.shuffle.writer;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.collect.Queues;
+import com.google.common.collect.Sets;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.uniffle.client.api.ShuffleWriteClient;
+import org.apache.uniffle.client.response.SendShuffleDataResult;
+import org.apache.uniffle.common.ShuffleBlockInfo;
+import org.apache.uniffle.common.util.ThreadUtils;
+
+public class DataPusher implements Closeable {
+ private static final Logger LOGGER = LoggerFactory.getLogger(DataPusher.class);
+
+ private final ExecutorService executorService;
+
+ private final ShuffleWriteClient shuffleWriteClient;
+ // Must be thread safe
+ private final Map<String, Set<Long>> taskToSuccessBlockIds;
+ // Must be thread safe
+ private final Map<String, Set<Long>> taskToFailedBlockIds;
+ private String appId;
+ // Must be thread safe
+ private final Set<String> failedTaskIds;
+
+ public DataPusher(ShuffleWriteClient shuffleWriteClient,
+ Map<String, Set<Long>> taskToSuccessBlockIds,
+ Map<String, Set<Long>> taskToFailedBlockIds,
+ Set<String> failedTaskIds,
+ int threadPoolSize,
+ int threadKeepAliveTime) {
+ this.shuffleWriteClient = shuffleWriteClient;
+ this.taskToSuccessBlockIds = taskToSuccessBlockIds;
+ this.taskToFailedBlockIds = taskToFailedBlockIds;
+ this.failedTaskIds = failedTaskIds;
+ this.executorService = new ThreadPoolExecutor(
+ threadPoolSize,
+ threadPoolSize * 2,
+ threadKeepAliveTime,
+ TimeUnit.SECONDS,
+ Queues.newLinkedBlockingQueue(Integer.MAX_VALUE),
+ ThreadUtils.getThreadFactory(this.getClass().getName())
+ );
+ }
+
+ public CompletableFuture<Long> send(AddBlockEvent event) {
+ assert appId != null;
+ return CompletableFuture.supplyAsync(() -> {
+ String taskId = event.getTaskId();
+ List<ShuffleBlockInfo> shuffleBlockInfoList = event.getShuffleDataInfoList();
+ try {
+ SendShuffleDataResult result = shuffleWriteClient.sendShuffleData(
+ appId,
+ shuffleBlockInfoList,
+ () -> !isValidTask(taskId)
+ );
+ putBlockId(taskToSuccessBlockIds, taskId, result.getSuccessBlockIds());
+ putBlockId(taskToFailedBlockIds, taskId, result.getFailedBlockIds());
+ } finally {
+ List<Runnable> callbackChain = Optional.of(event.getProcessedCallbackChain()).orElse(Collections.EMPTY_LIST);
+ for (Runnable runnable : callbackChain) {
+ runnable.run();
+ }
+ }
+ return shuffleBlockInfoList.stream()
+ .map(x -> x.getFreeMemory())
+ .reduce((a, b) -> a + b)
+ .get();
+ }, executorService);
+ }
+
+ private synchronized void putBlockId(
+ Map<String, Set<Long>> taskToBlockIds,
+ String taskAttemptId,
+ Set<Long> blockIds) {
+ if (blockIds == null || blockIds.isEmpty()) {
+ return;
+ }
+ taskToBlockIds.computeIfAbsent(taskAttemptId, x -> Sets.newConcurrentHashSet()).addAll(blockIds);
+ }
+
+ public boolean isValidTask(String taskId) {
+ return !failedTaskIds.contains(taskId);
+ }
+
+ public void setAppId(String appId) {
+ this.appId = appId;
+ }
+
+ @Override
+ public void close() throws IOException {
+ if (executorService != null) {
+ try {
+ ThreadUtils.shutdownThreadPool(executorService, 5);
Review Comment:
Why is this value `5`?
--
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