1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.comm; |
20 | |
|
21 | |
import org.apache.giraph.comm.RequestRegistry.Type; |
22 | |
import org.apache.giraph.graph.BspUtils; |
23 | |
import org.apache.hadoop.io.Writable; |
24 | |
import org.apache.hadoop.io.WritableComparable; |
25 | |
import org.apache.log4j.Logger; |
26 | |
|
27 | |
import com.google.common.collect.Lists; |
28 | |
import com.google.common.collect.Maps; |
29 | |
|
30 | |
import java.io.DataInput; |
31 | |
import java.io.DataOutput; |
32 | |
import java.io.IOException; |
33 | |
import java.util.Collection; |
34 | |
import java.util.List; |
35 | |
import java.util.Map; |
36 | |
import java.util.Map.Entry; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
@SuppressWarnings("rawtypes") |
47 | |
public class SendPartitionMessagesRequest<I extends WritableComparable, |
48 | |
V extends Writable, E extends Writable, |
49 | |
M extends Writable> extends WritableRequest<I, V, E, M> { |
50 | |
|
51 | 1 | private static final Logger LOG = |
52 | |
Logger.getLogger(SendPartitionMessagesRequest.class); |
53 | |
|
54 | |
private int partitionId; |
55 | |
|
56 | |
private Map<I, Collection<M>> vertexIdMessages; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 19 | public SendPartitionMessagesRequest() { } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public SendPartitionMessagesRequest(int partitionId, |
70 | 1 | Map<I, Collection<M>> vertexIdMessages) { |
71 | 1 | this.partitionId = partitionId; |
72 | 1 | this.vertexIdMessages = vertexIdMessages; |
73 | 1 | } |
74 | |
|
75 | |
@Override |
76 | |
public void readFieldsRequest(DataInput input) throws IOException { |
77 | 1 | partitionId = input.readInt(); |
78 | 1 | int vertexIdMessagesSize = input.readInt(); |
79 | 1 | vertexIdMessages = Maps.newHashMapWithExpectedSize(vertexIdMessagesSize); |
80 | 7 | for (int i = 0; i < vertexIdMessagesSize; ++i) { |
81 | 6 | I vertexId = BspUtils.<I>createVertexId(getConf()); |
82 | 6 | vertexId.readFields(input); |
83 | 6 | int messageCount = input.readInt(); |
84 | 6 | List<M> messageList = Lists.newArrayListWithCapacity(messageCount); |
85 | 27 | for (int j = 0; j < messageCount; ++j) { |
86 | 21 | M message = BspUtils.<M>createMessageValue(getConf()); |
87 | 21 | message.readFields(input); |
88 | 21 | messageList.add(message); |
89 | |
} |
90 | 6 | if (vertexIdMessages.put(vertexId, messageList) != null) { |
91 | 0 | throw new IllegalStateException( |
92 | |
"readFields: Already has vertex id " + vertexId); |
93 | |
} |
94 | |
} |
95 | 1 | } |
96 | |
|
97 | |
@Override |
98 | |
public void writeRequest(DataOutput output) throws IOException { |
99 | 1 | output.writeInt(partitionId); |
100 | 1 | output.writeInt(vertexIdMessages.size()); |
101 | 1 | for (Entry<I, Collection<M>> entry : vertexIdMessages.entrySet()) { |
102 | 6 | entry.getKey().write(output); |
103 | 6 | output.writeInt(entry.getValue().size()); |
104 | 6 | for (M message : entry.getValue()) { |
105 | 21 | message.write(output); |
106 | |
} |
107 | |
} |
108 | 1 | } |
109 | |
|
110 | |
@Override |
111 | |
public Type getType() { |
112 | 10 | return Type.SEND_PARTITION_MESSAGES_REQUEST; |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public void doRequest(ServerData<I, V, E, M> serverData) { |
117 | |
try { |
118 | 1 | serverData.getIncomingMessageStore().addPartitionMessages( |
119 | |
vertexIdMessages, partitionId); |
120 | 0 | } catch (IOException e) { |
121 | 0 | throw new RuntimeException("doRequest: Got IOException ", e); |
122 | 1 | } |
123 | 1 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public int getPartitionId() { |
131 | 0 | return partitionId; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public Map<I, Collection<M>> getVertexIdMessages() { |
140 | 0 | return vertexIdMessages; |
141 | |
} |
142 | |
} |