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
19 package org.apache.giraph.utils;
20
21 import org.apache.giraph.aggregators.TextAppendAggregator;
22 import org.apache.giraph.master.MasterAggregatorUsage;
23 import org.apache.giraph.worker.WorkerAggregatorUsage;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.io.Text;
26 import org.apache.log4j.Logger;
27
28 /**
29 * Helper class for using aggregator which gathers log messages from workers
30 * 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 of
34 * 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 by
38 * switching the option.
39 */
40 public class MasterLoggingAggregator {
41 /** Whether or not to use master logging aggregator */
42 public static final String USE_MASTER_LOGGING_AGGREGATOR =
43 "giraph.useMasterLoggingAggregator";
44 /** Default is not using master logging aggregator */
45 public static final boolean USE_MASTER_LOGGING_AGGREGATOR_DEFAULT = false;
46 /** Name of aggregator which will be gathering the logs */
47 public static final String MASTER_LOGGING_AGGREGATOR_NAME =
48 "masterLoggingAggregator";
49
50 /** Class logger */
51 private static final Logger LOG =
52 Logger.getLogger(MasterLoggingAggregator.class);
53
54 /** Do not instantiate */
55 private MasterLoggingAggregator() {
56 }
57
58 /**
59 * Check if master logging aggregator is used.
60 *
61 * @param conf Configuration
62 * @return True iff master logging aggregator is used
63 */
64 public static boolean useMasterLoggingAggregator(Configuration conf) {
65 return conf.getBoolean(USE_MASTER_LOGGING_AGGREGATOR,
66 USE_MASTER_LOGGING_AGGREGATOR_DEFAULT);
67 }
68
69 /**
70 * Set whether or not master logging aggregator should be used
71 *
72 * @param useMasterLoggingAggregator Whether or not we want
73 * master logging aggregator to be used
74 * @param conf Configuration
75 */
76 public static void setUseMasterLoggingAggregator(
77 boolean useMasterLoggingAggregator, Configuration conf) {
78 conf.setBoolean(USE_MASTER_LOGGING_AGGREGATOR, useMasterLoggingAggregator);
79 }
80
81 /**
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 use
86 * in order to add message to the aggregator.
87 *
88 * @param message Message to log
89 * @param workerAggregatorUsage Worker aggregator usage
90 * (can be Vertex, WorkerContext, etc)
91 * @param conf Configuration
92 */
93 public static void aggregate(String message,
94 WorkerAggregatorUsage workerAggregatorUsage, Configuration conf) {
95 if (useMasterLoggingAggregator(conf)) {
96 workerAggregatorUsage.aggregate(
97 MASTER_LOGGING_AGGREGATOR_NAME, new Text(message));
98 }
99 }
100
101 /**
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 usage
108 * @param conf Configuration
109 */
110 public static void registerAggregator(
111 MasterAggregatorUsage masterAggregatorUsage, Configuration conf) {
112 if (useMasterLoggingAggregator(conf)) {
113 try {
114 masterAggregatorUsage.registerAggregator(MASTER_LOGGING_AGGREGATOR_NAME,
115 TextAppendAggregator.class);
116 } catch (InstantiationException e) {
117 throw new IllegalStateException("registerAggregator: " +
118 "InstantiationException occurred");
119 } catch (IllegalAccessException e) {
120 throw new IllegalStateException("registerAggregator: " +
121 "IllegalAccessException occurred");
122 }
123 }
124 }
125
126 /**
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 usage
133 * @param conf Configuration
134 */
135 public static void logAggregatedValue(
136 MasterAggregatorUsage masterAggregatorUsage, Configuration conf) {
137 if (useMasterLoggingAggregator(conf) && LOG.isInfoEnabled()) {
138 LOG.info("logAggregatedValue: \n" +
139 masterAggregatorUsage.getAggregatedValue(
140 MASTER_LOGGING_AGGREGATOR_NAME));
141 }
142 }
143 }