This project has retired. For details please refer to its Attic page.
AllWorkersInfo xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.worker;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import com.google.common.base.Preconditions;
24  
25  import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
26  
27  /**
28   * Information about all workers, their WorkerInfo values, and indices.
29   */
30  public class AllWorkersInfo {
31    /** List of workers in current superstep, sorted by task id */
32    private final List<WorkerInfo> workerInfos;
33    /** My worker index */
34    private final int myWorkerIndex;
35    /** Map of taskId to worker index */
36    private final Int2IntOpenHashMap taskIdToIndex;
37  
38    /**
39     * Constructor
40     * @param workers Ordered list of workers
41     * @param myWorker My worker
42     */
43    public AllWorkersInfo(List<WorkerInfo> workers, WorkerInfo myWorker) {
44      workerInfos = new ArrayList<>(workers);
45  
46      taskIdToIndex = new Int2IntOpenHashMap(workerInfos.size());
47      taskIdToIndex.defaultReturnValue(-1);
48      for (int i = 0; i < workerInfos.size(); i++) {
49        int task = workerInfos.get(i).getTaskId();
50        if (i > 0) {
51          Preconditions.checkState(
52              task > workerInfos.get(i - 1).getTaskId(), "Tasks not ordered");
53        }
54        Preconditions.checkState(task >= 0, "Task not specified, %d", task);
55        int old = taskIdToIndex.put(task, i);
56        Preconditions.checkState(old == -1,
57            "Task with %d id found twice (positions %d and %d)",
58            task, i, old);
59      }
60  
61      myWorkerIndex = getWorkerIndex(myWorker);
62    }
63  
64    /**
65     * List of WorkerInfos
66     *
67     * @return List of WorkerInfos
68     */
69    public List<WorkerInfo> getWorkerList() {
70      return workerInfos;
71    }
72  
73    /**
74     * Get number of workers
75     *
76     * @return Number of workers
77     */
78    public int getWorkerCount() {
79      return workerInfos.size();
80    }
81  
82    /**
83     * Get index for this worker
84     *
85     * @return Index of this worker
86     */
87    public int getMyWorkerIndex() {
88      return myWorkerIndex;
89    }
90  
91    /**
92     * For every worker this method returns unique number
93     * between 0 and N, where N is the total number of workers.
94     * This number stays the same throughout the computation.
95     * TaskID may be different from this number and task ID
96     * is not necessarily continuous
97     * @param workerInfo worker info object
98     * @return worker number
99     */
100   public int getWorkerIndex(WorkerInfo workerInfo) {
101     return taskIdToIndex.get(workerInfo.getTaskId());
102   }
103 }