dbw9580 commented on a change in pull request #2084:
URL: https://github.com/apache/drill/pull/2084#discussion_r446224224



##########
File path: 
contrib/storage-ipfs/src/main/java/org/apache/drill/exec/store/ipfs/IPFSGroupScan.java
##########
@@ -0,0 +1,456 @@
+/*
+ * Copyright (c) 2018-2020 Bowen Ding, Yuedong Xu, Liang Wang
+ *
+ * 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.drill.exec.store.ipfs;
+
+
+import com.fasterxml.jackson.annotation.JacksonInject;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import org.apache.drill.shaded.guava.com.google.common.cache.LoadingCache;
+import org.apache.drill.shaded.guava.com.google.common.base.Preconditions;
+import 
org.apache.drill.shaded.guava.com.google.common.collect.ArrayListMultimap;
+import org.apache.drill.shaded.guava.com.google.common.collect.ImmutableList;
+import org.apache.drill.shaded.guava.com.google.common.collect.ListMultimap;
+import org.apache.drill.shaded.guava.com.google.common.collect.Lists;
+import io.ipfs.api.MerkleNode;
+import io.ipfs.multihash.Multihash;
+import org.apache.drill.common.exceptions.ExecutionSetupException;
+import org.apache.drill.common.expression.SchemaPath;
+import org.apache.drill.common.util.DrillVersionInfo;
+import org.apache.drill.exec.coord.ClusterCoordinator;
+import org.apache.drill.exec.physical.EndpointAffinity;
+import org.apache.drill.exec.physical.base.AbstractGroupScan;
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.apache.drill.exec.physical.base.ScanStats;
+import org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint;
+import org.apache.drill.exec.store.StoragePluginRegistry;
+import org.apache.drill.exec.store.schedule.AffinityCreator;
+import org.apache.drill.exec.store.schedule.AssignmentCreator;
+import org.apache.drill.exec.store.schedule.CompleteWork;
+import org.apache.drill.exec.store.schedule.EndpointByteMap;
+import org.apache.drill.exec.store.schedule.EndpointByteMapImpl;
+import org.apache.drill.shaded.guava.com.google.common.base.Stopwatch;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Random;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RecursiveTask;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import static 
org.apache.drill.exec.store.ipfs.IPFSStoragePluginConfig.IPFSTimeOut.FETCH_DATA;
+
+@JsonTypeName("ipfs-scan")
+public class IPFSGroupScan extends AbstractGroupScan {
+  private static final Logger logger = 
LoggerFactory.getLogger(IPFSGroupScan.class);
+  private IPFSContext ipfsContext;
+  private IPFSScanSpec ipfsScanSpec;
+  private IPFSStoragePluginConfig config;
+  private List<SchemaPath> columns;
+
+  private static long DEFAULT_NODE_SIZE = 1000l;
+
+  private ListMultimap<Integer, IPFSWork> assignments;
+  private List<IPFSWork> ipfsWorkList = Lists.newArrayList();
+  private Map<String, List<IPFSWork>> endpointWorksMap;
+  private List<EndpointAffinity> affinities;
+
+  @JsonCreator
+  public IPFSGroupScan(@JsonProperty("IPFSScanSpec") IPFSScanSpec ipfsScanSpec,
+                       @JsonProperty("IPFSStoragePluginConfig") 
IPFSStoragePluginConfig ipfsStoragePluginConfig,
+                       @JsonProperty("columns") List<SchemaPath> columns,
+                       @JacksonInject StoragePluginRegistry pluginRegistry) 
throws IOException, ExecutionSetupException {
+    this(
+        ((IPFSStoragePlugin) 
pluginRegistry.getPlugin(ipfsStoragePluginConfig)).getIPFSContext(),
+        ipfsScanSpec,
+        columns
+    );
+  }
+
+  public IPFSGroupScan(IPFSContext ipfsContext,
+                       IPFSScanSpec ipfsScanSpec,
+                       List<SchemaPath> columns) {
+    super((String) null);
+    this.ipfsContext = ipfsContext;
+    this.ipfsScanSpec = ipfsScanSpec;
+    this.config = ipfsContext.getStoragePluginConfig();
+    logger.debug("GroupScan constructor called with columns {}", columns);
+    this.columns = columns == null || columns.size() == 0? ALL_COLUMNS : 
columns;
+    init();
+  }
+
+  private void init() {
+    IPFSHelper ipfsHelper = ipfsContext.getIPFSHelper();
+    ipfsHelper.setMaxPeersPerLeaf(config.getMaxNodesPerLeaf());
+    ipfsHelper.setTimeouts(config.getIpfsTimeouts());
+    endpointWorksMap = new HashMap<>();
+
+    Multihash topHash = ipfsScanSpec.getTargetHash(ipfsHelper);
+    LoadingCache<Multihash, IPFSPeer> peerMap = ipfsContext.getIPFSPeerCache();
+
+    try {
+      //TODO detect and warn about loops/recursions in a malformed tree
+      class IPFSTreeFlattener extends RecursiveTask<Map<Multihash, String>> {
+        private Multihash hash;
+        private boolean isProvider;
+        private Map<Multihash, String> ret = new LinkedHashMap<>();
+
+        public IPFSTreeFlattener(Multihash hash, boolean isProvider) {
+          this.hash = hash;
+          this.isProvider = isProvider;
+        }
+
+        @Override
+        public Map<Multihash, String> compute() {
+          try {
+            if (isProvider) {
+              IPFSPeer peer = peerMap.getUnchecked(hash);
+              ret.put(hash, peer.hasDrillbitAddress() ? 
peer.getDrillbitAddress().get() : null);
+              return ret;
+            }
+
+            MerkleNode metaOrSimpleNode = 
ipfsHelper.timedFailure(ipfsHelper.getClient().object::links, hash, 
config.getIpfsTimeout(FETCH_DATA));
+            if (metaOrSimpleNode.links.size() > 0) {
+              logger.debug("{} is a meta node", hash);
+              //TODO do something useful with leaf size, e.g. hint Drill about 
operation costs
+              List<Multihash> intermediates = 
metaOrSimpleNode.links.stream().map(x -> x.hash).collect(Collectors.toList());
+
+              ImmutableList.Builder<IPFSTreeFlattener> builder = 
ImmutableList.builder();
+              for (Multihash intermediate : intermediates.subList(1, 
intermediates.size())) {
+                builder.add(new IPFSTreeFlattener(intermediate, false));
+              }
+              ImmutableList<IPFSTreeFlattener> subtasks = builder.build();
+              subtasks.forEach(IPFSTreeFlattener::fork);
+
+              IPFSTreeFlattener first = new 
IPFSTreeFlattener(intermediates.get(0), false);
+              ret.putAll(first.compute());
+              subtasks.reverse().forEach(
+                  subtask -> ret.putAll(subtask.join())
+              );
+
+            } else {
+              logger.debug("{} is a simple node", hash);
+              List<IPFSPeer> providers = 
ipfsHelper.findprovsTimeout(hash).stream()
+                  .map(id ->
+                    peerMap.getUnchecked(id)
+                  )
+                  .collect(Collectors.toList());
+              //FIXME isDrillReady may block threads

Review comment:
       `isDrillReady` queries the IPFS DHT to check if a peer is also a running 
Drillbit, so that we can send query plans over to have the peer execute it. 
However, DHT queries are very time-consuming, so sometimes it appears to block 
the threads. I think it is desirable to add timeout control over the whole 
`IPFSTreeFlattener` execution.




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


Reply via email to