This project has retired. For details please refer to its Attic page.
TestComputationTypes xref
View Javadoc

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.vertex;
20  
21  import static org.apache.giraph.conf.GiraphConstants.VERTEX_VALUE_FACTORY_CLASS;
22  
23  import org.apache.giraph.combiner.MessageCombiner;
24  import org.apache.giraph.conf.GiraphConstants;
25  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
26  import org.apache.giraph.edge.ByteArrayEdges;
27  import org.apache.giraph.examples.SimpleSuperstepComputation.SimpleSuperstepVertexInputFormat;
28  import org.apache.giraph.factories.VertexValueFactory;
29  import org.apache.giraph.io.formats.GeneratedVertexInputFormat;
30  import org.apache.giraph.io.formats.JsonBase64VertexInputFormat;
31  import org.apache.giraph.io.formats.JsonBase64VertexOutputFormat;
32  import org.apache.giraph.job.GiraphConfigurationValidator;
33  import org.apache.giraph.utils.NoOpComputation;
34  import org.apache.hadoop.conf.Configuration;
35  import org.apache.hadoop.io.DoubleWritable;
36  import org.apache.hadoop.io.FloatWritable;
37  import org.apache.hadoop.io.IntWritable;
38  import org.apache.hadoop.io.LongWritable;
39  import org.junit.Test;
40  
41  
42  public class TestComputationTypes {
43  
44      /**
45       * Matches the {@link GeneratedVertexInputFormat}
46       */
47      private static class GeneratedComputationMatch extends NoOpComputation<
48          LongWritable, IntWritable, FloatWritable, FloatWritable> { }
49  
50      /**
51       * Matches the {@link GeneratedVertexInputFormat}
52       */
53      private static class DerivedComputationMatch extends
54          GeneratedComputationMatch {
55      }
56  
57      /**
58       * Mismatches the {@link GeneratedVertexInputFormat}
59       */
60      private static class GeneratedComputationMismatch extends NoOpComputation<
61          LongWritable, FloatWritable, FloatWritable, FloatWritable> { }
62  
63      /**
64       * Matches the {@link GeneratedComputationMatch}
65       */
66      public static class GeneratedVertexMatchMessageCombiner
67          implements
68          MessageCombiner<LongWritable, FloatWritable> {
69        @Override
70        public void combine(LongWritable vertexIndex,
71            FloatWritable originalMessage,
72            FloatWritable messageToCombine) {
73        }
74  
75        @Override
76        public FloatWritable createInitialMessage() {
77          return new FloatWritable();
78        }
79      }
80  
81      /**
82       * Mismatches the {@link GeneratedComputationMatch}
83       */
84      public static class GeneratedVertexMismatchMessageCombiner
85          implements
86          MessageCombiner<LongWritable, DoubleWritable> {
87        @Override
88        public void combine(LongWritable vertexIndex,
89            DoubleWritable originalMessage,
90            DoubleWritable messageToCombine) {
91        }
92  
93        @Override
94        public DoubleWritable createInitialMessage() {
95          return new DoubleWritable();
96        }
97      }
98  
99      /**
100      * Mismatches the {@link GeneratedComputationMatch}
101      */
102     public static class GeneratedVertexMismatchValueFactory implements
103         VertexValueFactory<DoubleWritable> {
104       @Override
105       public DoubleWritable newInstance() {
106         return new DoubleWritable();
107       }
108     }
109 
110     /**
111      * Just populate a conf with testing defaults that won't
112      * upset the GiraphConfigurationValidator.
113      * */
114     private Configuration getDefaultTestConf() {
115       Configuration conf = new Configuration();
116       conf.setInt(GiraphConstants.MAX_WORKERS, 1);
117       conf.setInt(GiraphConstants.MIN_WORKERS, 1);
118       conf.set(GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.getKey(),
119         "org.apache.giraph.io.formats.DUMMY_TEST_VALUE");
120       return conf;
121     }
122 
123     @Test
124     public void testMatchingType() throws SecurityException,
125             NoSuchMethodException, NoSuchFieldException {
126         Configuration conf = getDefaultTestConf();
127         GiraphConstants.COMPUTATION_CLASS.set(conf,
128             GeneratedComputationMatch.class);
129         GiraphConstants.VERTEX_EDGES_CLASS.set(conf, ByteArrayEdges.class);
130         GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.set(conf,
131             SimpleSuperstepVertexInputFormat.class);
132         GiraphConstants.MESSAGE_COMBINER_CLASS.set(conf,
133             GeneratedVertexMatchMessageCombiner.class);
134       @SuppressWarnings("rawtypes")
135       GiraphConfigurationValidator<?, ?, ?, ?, ?> validator =
136         new GiraphConfigurationValidator(conf);
137 
138       ImmutableClassesGiraphConfiguration gc = new
139           ImmutableClassesGiraphConfiguration(conf);
140 
141 
142       validator.validateConfiguration();
143     }
144 
145     @Test
146     public void testDerivedMatchingType() throws SecurityException,
147             NoSuchMethodException, NoSuchFieldException {
148         Configuration conf = getDefaultTestConf() ;
149         GiraphConstants.COMPUTATION_CLASS.set(conf,
150             DerivedComputationMatch.class);
151         GiraphConstants.VERTEX_EDGES_CLASS.set(conf, ByteArrayEdges.class);
152         GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.set(conf,
153             SimpleSuperstepVertexInputFormat.class);
154         @SuppressWarnings("rawtypes")
155         GiraphConfigurationValidator<?, ?, ?, ?, ?> validator =
156           new GiraphConfigurationValidator(conf);
157         validator.validateConfiguration();
158     }
159 
160     @Test
161     public void testDerivedInputFormatType() throws SecurityException,
162             NoSuchMethodException, NoSuchFieldException {
163         Configuration conf = getDefaultTestConf() ;
164         GiraphConstants.COMPUTATION_CLASS.set(conf,
165             DerivedComputationMatch.class);
166         GiraphConstants.VERTEX_EDGES_CLASS.set(conf, ByteArrayEdges.class);
167         GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.set(conf,
168             SimpleSuperstepVertexInputFormat.class);
169       @SuppressWarnings("rawtypes")
170       GiraphConfigurationValidator<?, ?, ?, ?, ?> validator =
171         new GiraphConfigurationValidator(conf);
172       validator.validateConfiguration();
173     }
174 
175     @Test(expected = IllegalStateException.class)
176     public void testMismatchingVertex() throws SecurityException,
177       NoSuchMethodException, NoSuchFieldException {
178       Configuration conf = getDefaultTestConf() ;
179       GiraphConstants.COMPUTATION_CLASS.set(conf,
180           GeneratedComputationMismatch.class);
181       GiraphConstants.VERTEX_EDGES_CLASS.set(conf, ByteArrayEdges.class);
182       GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.set(conf,
183         SimpleSuperstepVertexInputFormat.class);
184       @SuppressWarnings("rawtypes")
185       GiraphConfigurationValidator<?, ?, ?, ?, ?> validator =
186         new GiraphConfigurationValidator(conf);
187       validator.validateConfiguration();
188     }
189 
190     @Test(expected = IllegalStateException.class)
191     public void testMismatchingCombiner() throws SecurityException,
192       NoSuchMethodException, NoSuchFieldException {
193       Configuration conf = getDefaultTestConf() ;
194       GiraphConstants.COMPUTATION_CLASS.set(conf,
195           GeneratedComputationMatch.class);
196       GiraphConstants.VERTEX_EDGES_CLASS.set(conf, ByteArrayEdges.class);
197       GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.set(conf,
198         SimpleSuperstepVertexInputFormat.class);
199       GiraphConstants.MESSAGE_COMBINER_CLASS.set(conf,
200         GeneratedVertexMismatchMessageCombiner.class);
201       @SuppressWarnings("rawtypes")
202       GiraphConfigurationValidator<?, ?, ?, ?, ?> validator =
203         new GiraphConfigurationValidator(conf);
204       validator.validateConfiguration();
205     }
206 
207     @Test(expected = IllegalStateException.class)
208     public void testMismatchingVertexValueFactory() throws SecurityException,
209         NoSuchMethodException, NoSuchFieldException {
210       Configuration conf = getDefaultTestConf() ;
211       GiraphConstants.COMPUTATION_CLASS.set(conf,
212           GeneratedComputationMatch.class);
213       GiraphConstants.VERTEX_EDGES_CLASS.set(conf, ByteArrayEdges.class);
214       GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.set(conf,
215           SimpleSuperstepVertexInputFormat.class);
216       VERTEX_VALUE_FACTORY_CLASS.set(conf,
217           GeneratedVertexMismatchValueFactory.class);
218       @SuppressWarnings("rawtypes")
219       GiraphConfigurationValidator<?, ?, ?, ?, ?> validator =
220           new GiraphConfigurationValidator(conf);
221       validator.validateConfiguration();
222     }
223 
224     @Test
225     public void testJsonBase64FormatType() throws SecurityException,
226             NoSuchMethodException, NoSuchFieldException {
227         Configuration conf = getDefaultTestConf() ;
228         GiraphConstants.COMPUTATION_CLASS.set(conf,
229             GeneratedComputationMatch.class);
230         GiraphConstants.VERTEX_EDGES_CLASS.set(conf, ByteArrayEdges.class);
231         GiraphConstants.VERTEX_INPUT_FORMAT_CLASS.set(conf,
232             JsonBase64VertexInputFormat.class);
233         GiraphConstants.VERTEX_OUTPUT_FORMAT_CLASS.set(conf,
234             JsonBase64VertexOutputFormat.class);
235         @SuppressWarnings("rawtypes")
236         GiraphConfigurationValidator<?, ?, ?, ?, ?> validator =
237           new GiraphConfigurationValidator(conf);
238         validator.validateConfiguration();
239     }
240 }