You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2021/05/21 09:49:55 UTC

[GitHub] [iceberg] jshmchenxi commented on a change in pull request #2577: Spark: Add read.locality.enabled to TableProperties to support disabl…

jshmchenxi commented on a change in pull request #2577:
URL: https://github.com/apache/iceberg/pull/2577#discussion_r636788729



##########
File path: spark2/src/main/java/org/apache/iceberg/spark/source/Reader.java
##########
@@ -227,15 +233,58 @@ public StructType readSchema() {
     Broadcast<Table> tableBroadcast = sparkContext.broadcast(SerializableTable.copyOf(table));
 
     List<InputPartition<InternalRow>> readTasks = Lists.newArrayList();
-    for (CombinedScanTask task : tasks()) {
-      readTasks.add(new ReadTask<>(
-          task, tableBroadcast, expectedSchemaString, caseSensitive,
-          localityPreferred, InternalRowReaderFactory.INSTANCE));
-    }
+
+    initializeReadTasks(readTasks, tableBroadcast, expectedSchemaString, () -> InternalRowReaderFactory.INSTANCE);
 
     return readTasks;
   }
 
+  /**
+   * Initialize ReadTasks with multi threads as get block locations can be slow
+   *
+   * @param readTasks Result list to return
+   */
+  private <T> void initializeReadTasks(List<InputPartition<T>> readTasks,
+      Broadcast<Table> tableBroadcast, String expectedSchemaString, Supplier<ReaderFactory<T>> supplier) {
+    int taskInitThreads = Math.max(1, PropertyUtil.propertyAsInt(table.properties(), LOCALITY_TASK_INITIALIZE_THREADS,
+        LOCALITY_TASK_INITIALIZE_THREADS_DEFAULT));
+
+    if (!localityPreferred || taskInitThreads == 1) {
+      for (CombinedScanTask task : tasks()) {
+        readTasks.add(new ReadTask<>(
+            task, tableBroadcast, expectedSchemaString, caseSensitive,
+            localityPreferred, supplier.get()));
+      }
+      return;
+    }
+
+    List<Future<ReadTask<T>>> futures = new ArrayList<>();
+
+    final ExecutorService pool = Executors.newFixedThreadPool(
+        taskInitThreads,
+        new ThreadFactoryBuilder()
+            .setDaemon(true)
+            .setNameFormat("Init-ReadTask-%d")
+            .build());
+
+    List<CombinedScanTask> scanTasks = tasks();
+    for (int i = 0; i < scanTasks.size(); i++) {
+      final int curIndex = i;
+      futures.add(pool.submit(() -> new ReadTask<>(scanTasks.get(curIndex), tableBroadcast,
+          expectedSchemaString, caseSensitive, true, supplier.get())));
+    }

Review comment:
       Thanks! I will try to use iceberg utils in another PR and this will focus on adding the ability to disable locality.




-- 
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: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org