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;
2021import org.apache.giraph.master.input.MasterInputSplitsHandler;
22import org.apache.giraph.partition.PartitionStats;
23import org.apache.giraph.reducers.ReduceOperation;
24import org.apache.giraph.utils.BlockingElementsSet;
25import org.apache.hadoop.io.Writable;
26import org.apache.hadoop.util.Progressable;
2728import com.google.common.collect.Iterables;
2930import java.util.List;
3132/**33 * Handler for all master communications34 */35publicclassMasterGlobalCommHandlerimplementsMasterGlobalCommUsage {
36/** Aggregator handler */37privatefinalMasterAggregatorHandler aggregatorHandler;
38/** Input splits handler*/39privatefinalMasterInputSplitsHandler inputSplitsHandler;
40/**Partition stats received from workers */41privatefinal BlockingElementsSet<List<PartitionStats>> partitionStats =
42new BlockingElementsSet<>();
4344/**45 * Constructor46 *47 * @param aggregatorHandler Aggregator handler48 * @param inputSplitsHandler Input splits handler49 */50publicMasterGlobalCommHandler(
51MasterAggregatorHandler aggregatorHandler,
52MasterInputSplitsHandler inputSplitsHandler) {
53this.aggregatorHandler = aggregatorHandler;
54this.inputSplitsHandler = inputSplitsHandler;
55 }
5657publicMasterAggregatorHandler getAggregatorHandler() {
58return aggregatorHandler;
59 }
6061publicMasterInputSplitsHandler getInputSplitsHandler() {
62return inputSplitsHandler;
63 }
6465 @Override
66public <S, R extends Writable> void registerReducer(String name,
67 ReduceOperation<S, R> reduceOp) {
68 aggregatorHandler.registerReducer(name, reduceOp);
69 }
7071 @Override
72public <S, R extends Writable> void registerReducer(String name,
73 ReduceOperation<S, R> reduceOp, R globalInitialValue) {
74 aggregatorHandler.registerReducer(name, reduceOp, globalInitialValue);
75 }
7677 @Override
78public <R extends Writable> R getReduced(String name) {
79return aggregatorHandler.getReduced(name);
80 }
8182 @Override
83publicvoid broadcast(String name, Writable value) {
84 aggregatorHandler.broadcast(name, value);
85 }
8687/**88 * Received partition stats from a worker89 *90 * @param partitionStats Partition stats91 */92publicvoid receivedPartitionStats(List<PartitionStats> partitionStats) {
93this.partitionStats.offer(partitionStats);
94 }
9596/**97 * Get all partition stats. Blocks until all workers have sent their stats98 *99 * @param numWorkers Number of workers to wait for100 * @param progressable Progressable to report progress to101 * @return All partition stats102 */103public Iterable<PartitionStats> getAllPartitionStats(int numWorkers,
104 Progressable progressable) {
105return Iterables.concat(
106 partitionStats.getElements(numWorkers, progressable));
107 }
108 }