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 */1819package org.apache.giraph.master.input;
2021import org.apache.giraph.worker.WorkerInfo;
2223import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26import java.util.concurrent.atomic.AtomicInteger;
2728/**29 * Organizer for mapping splits on master. Mapping splits need all to be30 * given to all workers, unlike vertex and edge splits which are read by31 * exactly one worker each32 */33publicclassMappingInputSplitsMasterOrganizer34implementsInputSplitsMasterOrganizer {
35/** List of splits */36privatefinal List<byte[]> splits;
37/** Map from worker task id to atomic pointer in splits list */38privatefinal Map<Integer, AtomicInteger>
39 workerTaskIdToNextSplitIndexMap;
4041/**42 * Constructor43 *44 * @param serializedSplits Splits45 * @param workers List of workers46 */47publicMappingInputSplitsMasterOrganizer(List<byte[]> serializedSplits,
48 List<WorkerInfo> workers) {
49this.splits = serializedSplits;
50 workerTaskIdToNextSplitIndexMap = new HashMap<>();
51for (WorkerInfo worker : workers) {
52 workerTaskIdToNextSplitIndexMap.put(
53 worker.getTaskId(), new AtomicInteger(0));
54 }
55 }
5657 @Override
58public byte[] getSerializedSplitFor(int workerTaskId) {
59 AtomicInteger nextSplitIndex =
60 workerTaskIdToNextSplitIndexMap.get(workerTaskId);
61int splitIndex = nextSplitIndex.getAndIncrement();
62return splitIndex < splits.size() ? splits.get(splitIndex) : null;
63 }
64 }