All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.drill.exec.planner.fragment.ParallelizationInfo Maven / Gradle / Ivy

/**
 * 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.planner.fragment;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.apache.drill.exec.physical.EndpointAffinity;
import org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint;

import java.util.List;
import java.util.Map;

/**
 * Captures parallelization parameters for a given operator/fragments. It consists of min and max width of
 * parallelization and affinity to drillbit endpoints.
 */
public class ParallelizationInfo {

  /* Default parallelization width is [1, Integer.MAX_VALUE] and no endpoint affinity. */
  public static final ParallelizationInfo UNLIMITED_WIDTH_NO_ENDPOINT_AFFINITY =
      ParallelizationInfo.create(1, Integer.MAX_VALUE);

  private final Map affinityMap;
  private final int minWidth;
  private final int maxWidth;

  private ParallelizationInfo(int minWidth, int maxWidth, Map affinityMap) {
    this.minWidth = minWidth;
    this.maxWidth = maxWidth;
    this.affinityMap = ImmutableMap.copyOf(affinityMap);
  }

  public static ParallelizationInfo create(int minWidth, int maxWidth) {
    return create(minWidth, maxWidth, ImmutableList.of());
  }

  public static ParallelizationInfo create(int minWidth, int maxWidth, List endpointAffinities) {
    Map affinityMap = Maps.newHashMap();

    for(EndpointAffinity epAffinity : endpointAffinities) {
      affinityMap.put(epAffinity.getEndpoint(), epAffinity);
    }

    return new ParallelizationInfo(minWidth, maxWidth, affinityMap);
  }

  public int getMinWidth() {
    return minWidth;
  }

  public int getMaxWidth() {
    return maxWidth;
  }

  public Map getEndpointAffinityMap() {
    return affinityMap;
  }

  @Override
  public String toString() {
    return getDigest(minWidth, maxWidth, affinityMap);
  }

  private static String getDigest(int minWidth, int maxWidth, Map affinityMap) {
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("[minWidth = %d, maxWidth=%d, epAff=[", minWidth, maxWidth));
    sb.append(Joiner.on(",").join(affinityMap.values()));
    sb.append("]]");

    return sb.toString();
  }

  /**
   * Collects/merges one or more ParallelizationInfo instances.
   */
  public static class ParallelizationInfoCollector {
    private int minWidth = 1;
    private int maxWidth = Integer.MAX_VALUE;
    private final Map affinityMap = Maps.newHashMap();

    public void add(ParallelizationInfo parallelizationInfo) {
      minWidth = Math.max(minWidth, parallelizationInfo.minWidth);
      maxWidth = Math.min(maxWidth, parallelizationInfo.maxWidth);

      Map affinityMap = parallelizationInfo.getEndpointAffinityMap();
      for(Map.Entry epAff : affinityMap.entrySet()) {
        addEndpointAffinity(epAff.getValue());
      }
    }

    public void addMaxWidth(int newMaxWidth) {
      maxWidth = Math.min(maxWidth, newMaxWidth);
    }

    public void addMinWidth(int newMinWidth) {
      minWidth = Math.max(minWidth, newMinWidth);
    }

    public void addEndpointAffinities(List endpointAffinities) {
      for(EndpointAffinity epAff : endpointAffinities) {
        addEndpointAffinity(epAff);
      }
    }

    // Helper method to add the given EndpointAffinity to the global affinity map
    private void addEndpointAffinity(EndpointAffinity epAff) {
      if (affinityMap.containsKey(epAff.getEndpoint())) {
        affinityMap.get(epAff.getEndpoint()).addAffinity(epAff.getAffinity());
      } else {
        affinityMap.put(epAff.getEndpoint(), epAff);
      }
    }

    /**
     * Get a ParallelizationInfo instance based on the current state of collected info.
     */
    public ParallelizationInfo get() {
      return new ParallelizationInfo(minWidth, maxWidth, affinityMap);
    }

    @Override
    public String toString() {
      return getDigest(minWidth, maxWidth, affinityMap);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy