Github user roshannaik commented on a diff in the pull request:

    https://github.com/apache/storm/pull/2270#discussion_r132325729
  
    --- Diff: 
storm-client/src/jvm/org/apache/storm/grouping/LocalityAwareShuffleGrouping.java
 ---
    @@ -0,0 +1,80 @@
    +/*
    + * 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.storm.grouping;
    +
    +import java.io.Serializable;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.List;
    +import java.util.Random;
    +import java.util.concurrent.ConcurrentHashMap;
    +import org.apache.storm.generated.GlobalStreamId;
    +import org.apache.storm.task.WorkerTopologyContext;
    +
    +public class LocalityAwareShuffleGrouping implements CustomStreamGrouping, 
Serializable {
    +    private ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, Double>> 
taskNetworkDistance;
    +    private List<Integer> targetTasks;
    +    private Random rand;
    +
    +    @Override
    +    public void prepare(WorkerTopologyContext context, GlobalStreamId 
stream, List<Integer> targetTasks) {
    +        rand = new Random();
    +        taskNetworkDistance = context.getTaskNetworkDistance();
    +        this.targetTasks = targetTasks;
    +    }
    +
    +    /**
    +     * Choose the target task with the minimum distance.
    +     * If there are more than one task with the minimum distance, randomly 
pick one.
    +     * @param taskId The source taskId
    +     * @param values the values to group on
    +     * @return The chosen tasks
    +     */
    +    @Override
    +    public List<Integer> chooseTasks(int taskId, List<Object> values) {
    +        if (taskNetworkDistance == null || taskNetworkDistance.isEmpty() 
|| !taskNetworkDistance.containsKey(taskId)) {
    +            return new 
ArrayList<>(targetTasks.get(rand.nextInt(targetTasks.size())));
    +        }
    +
    +        ConcurrentHashMap<Integer, Double> taskIdToDistance = 
taskNetworkDistance.get(taskId);
    +
    +        int sameDistanceAppearanceCount = 1;
    +
    +        double minDistance = Double.MAX_VALUE;
    +        Integer closestTargetTask = targetTasks.get(0);
    +        for(Integer targetTaskId: targetTasks) {
    --- End diff --
    
    Since this is critical path and called several thousand/million times per 
second. We should use index based loops instead of iterator based loops here. 
It allocates an iterator object.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to