1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.partition;
19
20 import org.apache.giraph.edge.ArrayListEdges;
21 import org.apache.giraph.edge.EdgeFactory;
22 import org.apache.giraph.edge.OutEdges;
23 import org.apache.giraph.graph.DefaultVertex;
24 import org.apache.giraph.graph.GiraphTransferRegulator;
25 import org.apache.giraph.graph.Vertex;
26 import org.apache.giraph.job.GiraphJob;
27 import org.apache.giraph.utils.NoOpComputation;
28 import org.apache.hadoop.io.DoubleWritable;
29 import org.apache.hadoop.io.FloatWritable;
30 import org.apache.hadoop.io.IntWritable;
31 import org.apache.hadoop.io.LongWritable;
32 import org.junit.Before;
33 import org.junit.Test;
34
35 import java.io.IOException;
36
37 import static org.junit.Assert.assertFalse;
38 import static org.junit.Assert.assertTrue;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41
42
43
44
45 public class TestGiraphTransferRegulator {
46
47 private GiraphJob job;
48
49 private Vertex<IntWritable, FloatWritable, DoubleWritable>
50 vertex = new DefaultVertex<IntWritable, FloatWritable, DoubleWritable>();
51
52
53
54
55 public static class TestComputation extends NoOpComputation<IntWritable,
56 FloatWritable, DoubleWritable, LongWritable> { }
57
58 @Before
59 public void setUp() {
60 try {
61 job = new GiraphJob("TestGiraphTransferRegulator");
62 } catch (IOException e) {
63 throw new RuntimeException("setUp: Failed", e);
64 }
65 job.getConfiguration().setComputationClass(TestComputation.class);
66 }
67
68 @Test
69 public void testGiraphTransferRegulator() {
70 job.getConfiguration()
71 .setInt(GiraphTransferRegulator.MAX_VERTICES_PER_TRANSFER, 1);
72 job.getConfiguration()
73 .setInt(GiraphTransferRegulator.MAX_EDGES_PER_TRANSFER, 3);
74 OutEdges<IntWritable, DoubleWritable> edges =
75 new ArrayListEdges<IntWritable, DoubleWritable>();
76 edges.initialize(3);
77 edges.add(EdgeFactory.create(new IntWritable(2), new DoubleWritable(22)));
78 edges.add(EdgeFactory.create(new IntWritable(3), new DoubleWritable(33)));
79 edges.add(EdgeFactory.create(new IntWritable(4), new DoubleWritable(44)));
80 vertex.initialize(new IntWritable(1), new FloatWritable(1), edges);
81 GiraphTransferRegulator gtr =
82 new GiraphTransferRegulator(job.getConfiguration());
83 PartitionOwner owner = mock(PartitionOwner.class);
84 when(owner.getPartitionId()).thenReturn(57);
85 assertFalse(gtr.transferThisPartition(owner));
86 gtr.incrementCounters(owner, vertex);
87 assertTrue(gtr.transferThisPartition(owner));
88 }
89
90 }