This project has retired. For details please refer to its Attic page.
SendWorkerDataRequest 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.comm.requests;
20  
21  import org.apache.giraph.utils.VertexIdData;
22  import org.apache.giraph.utils.PairList;
23  import org.apache.hadoop.io.WritableComparable;
24  import org.apache.log4j.Logger;
25  
26  import java.io.DataInput;
27  import java.io.DataOutput;
28  import java.io.IOException;
29  
30  /**
31   * Abstract request to send a collection of data, indexed by vertex id,
32   * for a partition.
33   *
34   * @param <I> Vertex id
35   * @param <T> Data
36   * @param <B> Specialization of
37   * {@link org.apache.giraph.utils.VertexIdData} for T
38   */
39  @SuppressWarnings("unchecked")
40  public abstract class SendWorkerDataRequest<I extends WritableComparable, T,
41      B extends VertexIdData<I, T>>
42      extends WritableRequest implements WorkerRequest {
43    /** Class logger */
44    private static final Logger LOG =
45        Logger.getLogger(SendWorkerDataRequest.class);
46    /**
47     * All data for a group of vertices, organized by partition, which
48     * are owned by a single (destination) worker. This data is all
49     * destined for this worker.
50     * */
51    protected PairList<Integer, B> partitionVertexData;
52  
53    /**
54     * Constructor used for reflection only
55     */
56    public SendWorkerDataRequest() { }
57  
58    /**
59     * Constructor used to send request.
60     *
61     * @param partVertData Map of remote partitions =&gt; VertexIdData
62     */
63    public SendWorkerDataRequest(
64        PairList<Integer, B> partVertData) {
65      this.partitionVertexData = partVertData;
66    }
67  
68    /**
69     * Create a new {@link org.apache.giraph.utils.VertexIdData}
70     * specialized for the use case.
71     *
72     * @return A new instance of
73     * {@link org.apache.giraph.utils.VertexIdData}
74     */
75    public abstract B createVertexIdData();
76  
77    @Override
78    public void readFieldsRequest(DataInput input) throws IOException {
79      int numPartitions = input.readInt();
80      partitionVertexData = new PairList<Integer, B>();
81      partitionVertexData.initialize(numPartitions);
82      while (numPartitions-- > 0) {
83        final int partitionId = input.readInt();
84        B vertexIdData = createVertexIdData();
85        vertexIdData.setConf(getConf());
86        vertexIdData.readFields(input);
87        partitionVertexData.add(partitionId, vertexIdData);
88      }
89    }
90  
91    @Override
92    public void writeRequest(DataOutput output) throws IOException {
93      output.writeInt(partitionVertexData.getSize());
94      PairList<Integer, B>.Iterator
95          iterator = partitionVertexData.getIterator();
96      while (iterator.hasNext()) {
97        iterator.next();
98        output.writeInt(iterator.getCurrentFirst());
99        iterator.getCurrentSecond().write(output);
100     }
101   }
102 
103   @Override
104   public int getSerializedSize() {
105     int size = super.getSerializedSize() + 4;
106     PairList<Integer, B>.Iterator iterator = partitionVertexData.getIterator();
107     while (iterator.hasNext()) {
108       iterator.next();
109       size += 4 + iterator.getCurrentSecond().getSerializedSize();
110     }
111     return size;
112   }
113 }
114