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.utils;
2021import org.apache.giraph.aggregators.TextAppendAggregator;
22import org.apache.giraph.master.MasterAggregatorUsage;
23import org.apache.giraph.worker.WorkerAggregatorUsage;
24import org.apache.hadoop.conf.Configuration;
25import org.apache.hadoop.io.Text;
26import org.apache.log4j.Logger;
2728/**29 * Helper class for using aggregator which gathers log messages from workers30 * and prints them on master.31 *32 * If you want to track what's going on in your application,33 * and want to have all those logs accessible in a single place in the end of34 * each superstep, you can use option from this class.35 *36 * If you use a lot of log messages this might slow down your application,37 * but it can easily be turned on/off without changing your code just by38 * switching the option.39 */40publicclassMasterLoggingAggregator {
41/** Whether or not to use master logging aggregator */42publicstaticfinal String USE_MASTER_LOGGING_AGGREGATOR =
43"giraph.useMasterLoggingAggregator";
44/** Default is not using master logging aggregator */45publicstaticfinalboolean USE_MASTER_LOGGING_AGGREGATOR_DEFAULT = false;
46/** Name of aggregator which will be gathering the logs */47publicstaticfinal String MASTER_LOGGING_AGGREGATOR_NAME =
48"masterLoggingAggregator";
4950/** Class logger */51privatestaticfinal Logger LOG =
52 Logger.getLogger(MasterLoggingAggregator.class);
5354/** Do not instantiate */55privateMasterLoggingAggregator() {
56 }
5758/**59 * Check if master logging aggregator is used.60 *61 * @param conf Configuration62 * @return True iff master logging aggregator is used63 */64publicstaticboolean useMasterLoggingAggregator(Configuration conf) {
65return conf.getBoolean(USE_MASTER_LOGGING_AGGREGATOR,
66 USE_MASTER_LOGGING_AGGREGATOR_DEFAULT);
67 }
6869/**70 * Set whether or not master logging aggregator should be used71 *72 * @param useMasterLoggingAggregator Whether or not we want73 * master logging aggregator to be used74 * @param conf Configuration75 */76publicstaticvoid setUseMasterLoggingAggregator(
77boolean useMasterLoggingAggregator, Configuration conf) {
78 conf.setBoolean(USE_MASTER_LOGGING_AGGREGATOR, useMasterLoggingAggregator);
79 }
8081/**82 * Aggregate some message to master logging aggregator,83 * if the option for using it is set in the configuration.84 *85 * This is the method application implementation should use86 * in order to add message to the aggregator.87 *88 * @param message Message to log89 * @param workerAggregatorUsage Worker aggregator usage90 * (can be Vertex, WorkerContext, etc)91 * @param conf Configuration92 */93publicstaticvoid aggregate(String message,
94WorkerAggregatorUsage workerAggregatorUsage, Configuration conf) {
95if (useMasterLoggingAggregator(conf)) {
96 workerAggregatorUsage.aggregate(
97 MASTER_LOGGING_AGGREGATOR_NAME, new Text(message));
98 }
99 }
100101/**102 * Register master logging aggregator,103 * if the option for using it is set in the configuration.104 *105 * This method will be called by Giraph infrastructure on master.106 *107 * @param masterAggregatorUsage Master aggregator usage108 * @param conf Configuration109 */110publicstaticvoid registerAggregator(
111MasterAggregatorUsage masterAggregatorUsage, Configuration conf) {
112if (useMasterLoggingAggregator(conf)) {
113try {
114 masterAggregatorUsage.registerAggregator(MASTER_LOGGING_AGGREGATOR_NAME,
115 TextAppendAggregator.class);
116 } catch (InstantiationException e) {
117thrownew IllegalStateException("registerAggregator: " +
118"InstantiationException occurred");
119 } catch (IllegalAccessException e) {
120thrownew IllegalStateException("registerAggregator: " +
121"IllegalAccessException occurred");
122 }
123 }
124 }
125126/**127 * Print value of master logging aggregator on the master log,128 * if the option for using it is set in the configuration.129 *130 * This method will be called by Giraph infrastructure on master.131 *132 * @param masterAggregatorUsage Master aggregator usage133 * @param conf Configuration134 */135publicstaticvoid logAggregatedValue(
136MasterAggregatorUsage masterAggregatorUsage, Configuration conf) {
137if (useMasterLoggingAggregator(conf) && LOG.isInfoEnabled()) {
138 LOG.info("logAggregatedValue: \n" +
139 masterAggregatorUsage.getAggregatedValue(
140 MASTER_LOGGING_AGGREGATOR_NAME));
141 }
142 }
143 }