This project has retired. For details please refer to its Attic page.
TestComputationCombinerTypes 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.master;
20  
21  import java.io.IOException;
22  
23  import org.apache.giraph.combiner.MessageCombiner;
24  import org.apache.giraph.conf.GiraphConfiguration;
25  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
26  import org.apache.giraph.graph.AbstractComputation;
27  import org.apache.giraph.graph.Computation;
28  import org.apache.giraph.graph.Vertex;
29  import org.apache.giraph.utils.IntNoOpComputation;
30  import org.apache.hadoop.io.DoubleWritable;
31  import org.apache.hadoop.io.IntWritable;
32  import org.apache.hadoop.io.LongWritable;
33  import org.apache.hadoop.io.Writable;
34  import org.apache.hadoop.io.WritableComparable;
35  import org.junit.Test;
36  
37  /** Test type verification when switching computation and combiner types */
38  public class TestComputationCombinerTypes {
39    private void testConsecutiveComp(
40        Class<? extends Computation> firstComputationClass,
41        Class<? extends Computation> secondComputationClass) {
42      testConsecutiveComp(firstComputationClass, secondComputationClass, null);
43    }
44  
45    private void testConsecutiveComp(
46        Class<? extends Computation> firstComputationClass,
47        Class<? extends Computation> secondComputationClass,
48        Class<? extends MessageCombiner> messageCombinerClass) {
49      ImmutableClassesGiraphConfiguration conf =
50          createConfiguration(firstComputationClass);
51      SuperstepClasses classes = SuperstepClasses.createAndExtractTypes(conf);
52      classes.setComputationClass(secondComputationClass);
53      classes.setMessageCombinerClass(messageCombinerClass);
54      classes.verifyTypesMatch(true);
55    }
56  
57    @Test
58    public void testAllMatchWithoutCombiner() {
59      testConsecutiveComp(IntNoOpComputation.class, IntNoOpComputation.class);
60    }
61  
62    @Test
63    public void testAllMatchWithCombiner() {
64      testConsecutiveComp(
65          IntIntIntIntLongComputation.class,
66          IntIntIntLongDoubleComputation.class,
67          IntDoubleMessageCombiner.class);
68    }
69  
70    @Test(expected = IllegalStateException.class)
71    public void testDifferentIdTypes() {
72      testConsecutiveComp(
73          IntIntIntIntLongComputation.class, LongIntIntLongIntComputation.class);
74    }
75  
76    @Test(expected = IllegalStateException.class)
77    public void testDifferentVertexValueTypes() {
78      testConsecutiveComp(
79          IntIntIntIntLongComputation.class, IntLongIntLongIntComputation.class);
80    }
81  
82    @Test(expected = IllegalStateException.class)
83    public void testDifferentEdgeDataTypes() {
84      testConsecutiveComp(
85          IntIntIntIntLongComputation.class, IntIntLongLongIntComputation.class);
86    }
87  
88    @Test(expected = IllegalStateException.class)
89    public void testDifferentMessageTypes() {
90      testConsecutiveComp(
91          IntIntIntLongDoubleComputation.class, IntIntIntIntLongComputation.class);
92    }
93  
94    @Test(expected = IllegalStateException.class)
95    public void testDifferentCombinerIdType() {
96      testConsecutiveComp(
97          IntIntIntIntLongComputation.class,
98          IntIntIntLongDoubleComputation.class,
99          DoubleDoubleMessageCombiner.class);
100   }
101 
102   @Test(expected = IllegalStateException.class)
103   public void testDifferentCombinerMessageType() {
104     testConsecutiveComp(
105         IntIntIntIntLongComputation.class,
106         IntIntIntLongDoubleComputation.class,
107         IntLongMessageCombiner.class);
108   }
109 
110   private static ImmutableClassesGiraphConfiguration createConfiguration(
111       Class<? extends Computation> computationClass) {
112     GiraphConfiguration conf = new GiraphConfiguration();
113     conf.setComputationClass(computationClass);
114     return new ImmutableClassesGiraphConfiguration(conf);
115   }
116 
117   public static class NoOpComputation<I extends WritableComparable,
118       V extends Writable, E extends Writable, M1 extends Writable,
119       M2 extends Writable> extends AbstractComputation<I, V, E, M1, M2> {
120     @Override
121     public void compute(Vertex<I, V, E> vertex,
122         Iterable<M1> messages) throws IOException {
123     }
124   }
125 
126   private static class IntIntIntIntLongComputation extends
127       NoOpComputation<IntWritable, IntWritable, IntWritable, IntWritable,
128           LongWritable> { }
129 
130   private static class IntIntIntLongDoubleComputation extends
131       NoOpComputation<IntWritable, IntWritable, IntWritable, LongWritable,
132           DoubleWritable> { }
133 
134   private static class LongIntIntLongIntComputation extends
135       NoOpComputation<LongWritable, IntWritable, IntWritable, LongWritable,
136           IntWritable> { }
137 
138   private static class IntLongIntLongIntComputation extends
139       NoOpComputation<IntWritable, LongWritable, IntWritable, LongWritable,
140           IntWritable> { }
141 
142   private static class IntIntLongLongIntComputation extends
143       NoOpComputation<IntWritable, IntWritable, LongWritable, LongWritable,
144           IntWritable> { }
145 
146   private static class NoOpMessageCombiner<I extends WritableComparable,
147       M extends Writable> implements MessageCombiner<I, M> {
148     @Override
149     public void combine(I vertexIndex, M originalMessage, M messageToCombine) {
150     }
151 
152     @Override
153     public M createInitialMessage() {
154       return null;
155     }
156   }
157 
158   private static class IntDoubleMessageCombiner
159       extends NoOpMessageCombiner<IntWritable,
160                   DoubleWritable> { }
161 
162   private static class DoubleDoubleMessageCombiner
163       extends NoOpMessageCombiner<DoubleWritable,
164                   DoubleWritable> { }
165 
166   private static class IntLongMessageCombiner
167       extends NoOpMessageCombiner<IntWritable,
168                   LongWritable> { }
169 }