1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.worker;
1920import java.util.ArrayList;
21import java.util.List;
2223import com.google.common.base.Preconditions;
2425import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
2627/**28 * Information about all workers, their WorkerInfo values, and indices.29 */30publicclassAllWorkersInfo {
31/** List of workers in current superstep, sorted by task id */32privatefinal List<WorkerInfo> workerInfos;
33/** My worker index */34privatefinalint myWorkerIndex;
35/** Map of taskId to worker index */36privatefinal Int2IntOpenHashMap taskIdToIndex;
3738/**39 * Constructor40 * @param workers Ordered list of workers41 * @param myWorker My worker42 */43publicAllWorkersInfo(List<WorkerInfo> workers, WorkerInfo myWorker) {
44 workerInfos = new ArrayList<>(workers);
4546 taskIdToIndex = new Int2IntOpenHashMap(workerInfos.size());
47 taskIdToIndex.defaultReturnValue(-1);
48for (int i = 0; i < workerInfos.size(); i++) {
49int task = workerInfos.get(i).getTaskId();
50if (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);
55int 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 }
6061 myWorkerIndex = getWorkerIndex(myWorker);
62 }
6364/**65 * List of WorkerInfos66 *67 * @return List of WorkerInfos68 */69public List<WorkerInfo> getWorkerList() {
70return workerInfos;
71 }
7273/**74 * Get number of workers75 *76 * @return Number of workers77 */78publicint getWorkerCount() {
79return workerInfos.size();
80 }
8182/**83 * Get index for this worker84 *85 * @return Index of this worker86 */87publicint getMyWorkerIndex() {
88return myWorkerIndex;
89 }
9091/**92 * For every worker this method returns unique number93 * 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 ID96 * is not necessarily continuous97 * @param workerInfo worker info object98 * @return worker number99 */100publicint getWorkerIndex(WorkerInfo workerInfo) {
101return taskIdToIndex.get(workerInfo.getTaskId());
102 }
103 }