This project has retired. For details please refer to its
        
        Attic page.
      
1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.giraph.job;
20  
21  import static org.apache.giraph.conf.GiraphConstants.VERTEX_EDGES_CLASS;
22  import static org.apache.giraph.conf.GiraphConstants.VERTEX_RESOLVER_CLASS;
23  import static org.apache.giraph.utils.ReflectionUtils.getTypeArguments;
24  
25  import org.apache.giraph.combiner.MessageCombiner;
26  import org.apache.giraph.conf.GiraphConstants;
27  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
28  import org.apache.giraph.edge.OutEdges;
29  import org.apache.giraph.factories.DefaultVertexValueFactory;
30  import org.apache.giraph.factories.VertexValueFactory;
31  import org.apache.giraph.graph.DefaultVertexResolver;
32  import org.apache.giraph.graph.VertexResolver;
33  import org.apache.giraph.graph.VertexValueCombiner;
34  import org.apache.giraph.io.EdgeInputFormat;
35  import org.apache.giraph.io.EdgeOutputFormat;
36  import org.apache.giraph.io.VertexInputFormat;
37  import org.apache.giraph.io.VertexOutputFormat;
38  import org.apache.hadoop.conf.Configuration;
39  import org.apache.hadoop.io.Writable;
40  import org.apache.hadoop.io.WritableComparable;
41  import org.apache.log4j.Logger;
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  public class GiraphConfigurationValidator<I extends WritableComparable,
56      V extends Writable, E extends Writable, M1 extends Writable,
57      M2 extends Writable> {
58    
59  
60  
61    private static Logger LOG =
62      Logger.getLogger(GiraphConfigurationValidator.class);
63  
64    
65    private static final int ID_PARAM_INDEX = 0;
66    
67    private static final int VALUE_PARAM_INDEX = 1;
68    
69    private static final int EDGE_PARAM_INDEX = 2;
70    
71    private static final int MSG_COMBINER_PARAM_INDEX = 1;
72    
73    private static final int EDGE_PARAM_EDGE_INPUT_FORMAT_INDEX = 1;
74    
75    private static final int EDGE_PARAM_OUT_EDGES_INDEX = 1;
76    
77    private static final int VALUE_PARAM_VERTEX_VALUE_FACTORY_INDEX = 0;
78    
79    private static final int VALUE_PARAM_VERTEX_VALUE_COMBINER_INDEX = 0;
80  
81    
82  
83  
84    private final ImmutableClassesGiraphConfiguration conf;
85  
86    
87  
88  
89  
90  
91  
92    public GiraphConfigurationValidator(Configuration conf) {
93      this.conf = new ImmutableClassesGiraphConfiguration(conf);
94    }
95  
96    
97  
98  
99  
100 
101   private Class<? extends WritableComparable> vertexIndexType() {
102     return conf.getGiraphTypes().getVertexIdClass();
103   }
104 
105   
106 
107 
108 
109 
110   private Class<? extends Writable> vertexValueType() {
111     return conf.getGiraphTypes().getVertexValueClass();
112   }
113 
114   
115 
116 
117 
118 
119   private Class<? extends Writable> edgeValueType() {
120     return conf.getGiraphTypes().getEdgeValueClass();
121   }
122 
123   
124 
125 
126 
127 
128   private Class<? extends Writable> outgoingMessageValueType() {
129     return conf.getOutgoingMessageValueClass();
130   }
131 
132   
133 
134 
135 
136 
137 
138   public void validateConfiguration() {
139     checkConfiguration();
140     verifyOutEdgesGenericTypes();
141     verifyVertexInputFormatGenericTypes();
142     verifyEdgeInputFormatGenericTypes();
143     verifyVertexOutputFormatGenericTypes();
144     verifyEdgeOutputFormatGenericTypes();
145     verifyVertexResolverGenericTypes();
146     verifyVertexValueCombinerGenericTypes();
147     verifyMessageCombinerGenericTypes();
148     verifyVertexValueFactoryGenericTypes();
149   }
150 
151   
152 
153 
154 
155   private void checkConfiguration() {
156     if (conf.getMaxWorkers() < 0) {
157       throw new RuntimeException("checkConfiguration: No valid " +
158           GiraphConstants.MAX_WORKERS);
159     }
160     if (conf.getMinPercentResponded() <= 0.0f ||
161         conf.getMinPercentResponded() > 100.0f) {
162       throw new IllegalArgumentException(
163           "checkConfiguration: Invalid " + conf.getMinPercentResponded() +
164               " for " + GiraphConstants.MIN_PERCENT_RESPONDED.getKey());
165     }
166     if (conf.getMinWorkers() < 0) {
167       throw new IllegalArgumentException("checkConfiguration: No valid " +
168           GiraphConstants.MIN_WORKERS);
169     }
170     conf.createComputationFactory().checkConfiguration(conf);
171     if (conf.getVertexInputFormatClass() == null &&
172         conf.getEdgeInputFormatClass() == null) {
173       throw new IllegalArgumentException("checkConfiguration: One of " +
174           GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.getKey() + " and " +
175           GiraphConstants.EDGE_INPUT_FORMAT_CLASS.getKey() +
176           " must be non-null");
177     }
178     if (conf.getVertexResolverClass() == null) {
179       if (LOG.isInfoEnabled()) {
180         LOG.info("checkConfiguration: No class found for " +
181             VERTEX_RESOLVER_CLASS.getKey() +
182             ", defaulting to " +
183             VERTEX_RESOLVER_CLASS.getDefaultClass().getCanonicalName());
184       }
185     }
186     if (conf.getOutEdgesClass() == null) {
187       if (LOG.isInfoEnabled()) {
188         LOG.info("checkConfiguration: No class found for " +
189             VERTEX_EDGES_CLASS.getKey() + ", defaulting to " +
190             VERTEX_EDGES_CLASS.getDefaultClass().getCanonicalName());
191       }
192     }
193   }
194 
195   
196 
197 
198 
199 
200   private void verifyOutEdgesGenericTypesClass(
201       Class<? extends OutEdges<I, E>> outEdgesClass) {
202     Class<?>[] classList = getTypeArguments(OutEdges.class, outEdgesClass);
203     checkAssignable(classList, ID_PARAM_INDEX, vertexIndexType(),
204         OutEdges.class, "vertex index");
205     checkAssignable(classList, EDGE_PARAM_OUT_EDGES_INDEX, edgeValueType(),
206         OutEdges.class, "edge value");
207   }
208 
209   
210   private void verifyOutEdgesGenericTypes() {
211     Class<? extends OutEdges<I, E>> outEdgesClass =
212         conf.getOutEdgesClass();
213     Class<? extends OutEdges<I, E>> inputOutEdgesClass =
214         conf.getInputOutEdgesClass();
215     verifyOutEdgesGenericTypesClass(outEdgesClass);
216     verifyOutEdgesGenericTypesClass(inputOutEdgesClass);
217   }
218 
219   
220   private void verifyVertexInputFormatGenericTypes() {
221     Class<? extends VertexInputFormat<I, V, E>> vertexInputFormatClass =
222       conf.getVertexInputFormatClass();
223     if (vertexInputFormatClass != null) {
224       Class<?>[] classList =
225           getTypeArguments(VertexInputFormat.class, vertexInputFormatClass);
226       checkAssignable(classList, ID_PARAM_INDEX, vertexIndexType(),
227           VertexInputFormat.class, "vertex index");
228       checkAssignable(classList, VALUE_PARAM_INDEX, vertexValueType(),
229           VertexInputFormat.class, "vertex value");
230       checkAssignable(classList, EDGE_PARAM_INDEX, edgeValueType(),
231           VertexInputFormat.class, "edge value");
232     }
233   }
234 
235   
236   private void verifyEdgeInputFormatGenericTypes() {
237     Class<? extends EdgeInputFormat<I, E>> edgeInputFormatClass =
238         conf.getEdgeInputFormatClass();
239     if (edgeInputFormatClass != null) {
240       Class<?>[] classList =
241           getTypeArguments(EdgeInputFormat.class, edgeInputFormatClass);
242       checkAssignable(classList, ID_PARAM_INDEX, vertexIndexType(),
243           EdgeInputFormat.class, "vertex index");
244       checkAssignable(classList, EDGE_PARAM_EDGE_INPUT_FORMAT_INDEX,
245           edgeValueType(), EdgeInputFormat.class, "edge value");
246     }
247   }
248 
249   
250 
251 
252 
253   private void verifyVertexValueCombinerGenericTypes() {
254     Class<? extends VertexValueCombiner<V>> vertexValueCombiner =
255         conf.getVertexValueCombinerClass();
256     if (vertexValueCombiner != null) {
257       Class<?>[] classList =
258           getTypeArguments(VertexValueCombiner.class, vertexValueCombiner);
259       checkAssignable(classList, VALUE_PARAM_VERTEX_VALUE_COMBINER_INDEX,
260           vertexValueType(), VertexValueCombiner.class, "vertex value");
261     }
262   }
263 
264   
265 
266 
267 
268   private void verifyMessageCombinerGenericTypes() {
269     MessageCombiner<I, M2> messageCombiner =
270       conf.createOutgoingMessageCombiner();
271     if (messageCombiner != null) {
272       Class<?>[] classList =
273           getTypeArguments(MessageCombiner.class, messageCombiner.getClass());
274       checkEquals(classList, ID_PARAM_INDEX, vertexIndexType(),
275           MessageCombiner.class, "vertex index");
276       checkEquals(classList, MSG_COMBINER_PARAM_INDEX,
277           outgoingMessageValueType(), MessageCombiner.class, "message value");
278     }
279   }
280 
281   
282   private void verifyVertexOutputFormatGenericTypes() {
283     Class<? extends EdgeOutputFormat<I, V, E>>
284       edgeOutputFormatClass = conf.getEdgeOutputFormatClass();
285     if (conf.hasEdgeOutputFormat()) {
286       Class<?>[] classList =
287         getTypeArguments(EdgeOutputFormat.class, edgeOutputFormatClass);
288       checkAssignable(classList, ID_PARAM_INDEX, vertexIndexType(),
289           VertexOutputFormat.class, "vertex index");
290       checkAssignable(classList, VALUE_PARAM_INDEX, vertexValueType(),
291           VertexOutputFormat.class, "vertex value");
292       checkAssignable(classList, EDGE_PARAM_INDEX, edgeValueType(),
293           VertexOutputFormat.class, "edge value");
294     }
295   }
296 
297   
298   private void verifyEdgeOutputFormatGenericTypes() {
299     Class<? extends VertexOutputFormat<I, V, E>>
300       vertexOutputFormatClass = conf.getVertexOutputFormatClass();
301     if (conf.hasVertexOutputFormat()) {
302       Class<?>[] classList =
303         getTypeArguments(VertexOutputFormat.class, vertexOutputFormatClass);
304       checkAssignable(classList, ID_PARAM_INDEX, vertexIndexType(),
305           VertexOutputFormat.class, "vertex index");
306       checkAssignable(classList, VALUE_PARAM_INDEX, vertexValueType(),
307           VertexOutputFormat.class, "vertex value");
308       checkAssignable(classList, EDGE_PARAM_INDEX, edgeValueType(),
309           VertexOutputFormat.class, "edge value");
310     }
311   }
312 
313   
314   private void verifyVertexValueFactoryGenericTypes() {
315     Class<? extends VertexValueFactory<V>>
316         vvfClass = conf.getVertexValueFactoryClass();
317     if (DefaultVertexValueFactory.class.equals(vvfClass)) {
318       return;
319     }
320     Class<?>[] classList = getTypeArguments(VertexValueFactory.class, vvfClass);
321     checkEquals(classList, VALUE_PARAM_VERTEX_VALUE_FACTORY_INDEX,
322         vertexValueType(), VertexValueFactory.class, "vertex value");
323   }
324 
325   
326 
327 
328 
329   private void verifyVertexResolverGenericTypes() {
330     Class<? extends VertexResolver<I, V, E>>
331         vrClass = conf.getVertexResolverClass();
332     if (DefaultVertexResolver.class.equals(vrClass)) {
333       return;
334     }
335     Class<?>[] classList =
336         getTypeArguments(VertexResolver.class, vrClass);
337     checkEquals(classList, ID_PARAM_INDEX, vertexIndexType(),
338         VertexResolver.class, "vertex index");
339     checkEquals(classList, VALUE_PARAM_INDEX, vertexValueType(),
340         VertexResolver.class, "vertex value");
341     checkEquals(classList, EDGE_PARAM_INDEX, edgeValueType(),
342         VertexResolver.class, "edge value");
343   }
344 
345   
346 
347 
348 
349 
350 
351 
352 
353 
354   private static void checkEquals(Class<?>[] classList, int index,
355       Class<?> classFromComputation, Class klass, String typeName) {
356     if (classList[index] == null) {
357       LOG.warn(klass.getSimpleName() + " " + typeName + " type is not known");
358     } else if (!classList[index].equals(classFromComputation)) {
359       throw new IllegalStateException(
360           "checkClassTypes: " + typeName + " types not equal, " +
361               "computation - " + classFromComputation +
362               ", " + klass.getSimpleName() + " - " +
363               classList[index]);
364     }
365   }
366 
367   
368 
369 
370 
371 
372 
373 
374 
375 
376   private static void checkAssignable(Class<?>[] classList, int index,
377       Class<?> classFromComputation, Class klass, String typeName) {
378     if (classList[index] == null) {
379       LOG.warn(klass.getSimpleName() + " " + typeName + " type is not known");
380     } else if (!classList[index].isAssignableFrom(classFromComputation)) {
381       throw new IllegalStateException(
382           "checkClassTypes: " + typeName + " types not assignable, " +
383               "computation - " + classFromComputation +
384               ", " + klass.getSimpleName() + " - " +
385               classList[EDGE_PARAM_EDGE_INPUT_FORMAT_INDEX]);
386     }
387   }
388 }
389