This project has retired. For details please refer to its Attic page.
TestGiraphTransferRegulator 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  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   * Test the GiraphTransferRegulator.
44   */
45  public class TestGiraphTransferRegulator {
46    /** Job filled in by setup() */
47    private GiraphJob job;
48    /** Instantiated vertex filled in from setup() */
49    private Vertex<IntWritable, FloatWritable, DoubleWritable>
50        vertex = new DefaultVertex<IntWritable, FloatWritable, DoubleWritable>();
51  
52    /**
53     * Dummy vertex.
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  }