-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
PlatformDataStreamer.java
292 lines (223 loc) · 8.88 KB
/
PlatformDataStreamer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.processors.platform.datastreamer;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.events.DiscoveryEvent;
import org.apache.ignite.events.Event;
import org.apache.ignite.internal.managers.discovery.GridDiscoveryManager;
import org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener;
import org.apache.ignite.internal.binary.BinaryRawReaderEx;
import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
import org.apache.ignite.internal.processors.datastreamer.DataStreamerImpl;
import org.apache.ignite.internal.processors.platform.PlatformAbstractTarget;
import org.apache.ignite.internal.processors.platform.PlatformContext;
import org.apache.ignite.internal.processors.platform.utils.PlatformFutureUtils;
import org.apache.ignite.internal.util.lang.GridMapEntry;
import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
/**
* Interop data streamer wrapper.
*/
@SuppressWarnings({"UnusedDeclaration", "unchecked"})
public class PlatformDataStreamer extends PlatformAbstractTarget {
/** Policy: continue. */
private static final int PLC_CONTINUE = 0;
/** Policy: close. */
private static final int PLC_CLOSE = 1;
/** Policy: cancel and close. */
private static final int PLC_CANCEL_CLOSE = 2;
/** Policy: do flush. */
private static final int PLC_FLUSH = 3;
/** */
private static final int OP_UPDATE = 1;
/** */
private static final int OP_RECEIVER = 2;
/** */
private static final int OP_ALLOW_OVERWRITE = 3;
/** */
private static final int OP_SET_ALLOW_OVERWRITE = 4;
/** */
private static final int OP_SKIP_STORE = 5;
/** */
private static final int OP_SET_SKIP_STORE = 6;
/** */
private static final int OP_PER_NODE_BUFFER_SIZE = 7;
/** */
private static final int OP_SET_PER_NODE_BUFFER_SIZE = 8;
/** */
private static final int OP_PER_NODE_PARALLEL_OPS = 9;
/** */
private static final int OP_SET_PER_NODE_PARALLEL_OPS = 10;
/** */
private static final int OP_LISTEN_TOPOLOGY = 11;
/** Cache name. */
private final String cacheName;
/** Data streamer. */
private final DataStreamerImpl ldr;
/** Binary flag. */
private final boolean keepBinary;
/** Topology update event listener. */
private volatile GridLocalEventListener lsnr;
/**
* Constructor.
*
* @param platformCtx Context.
* @param ldr Data streamer.
*/
public PlatformDataStreamer(PlatformContext platformCtx, String cacheName, DataStreamerImpl ldr,
boolean keepBinary) {
super(platformCtx);
this.cacheName = cacheName;
this.ldr = ldr;
this.keepBinary = keepBinary;
}
/** {@inheritDoc} */
@Override protected long processInStreamOutLong(int type, BinaryRawReaderEx reader) throws IgniteCheckedException {
switch (type) {
case OP_UPDATE:
int plc = reader.readInt();
if (plc == PLC_CANCEL_CLOSE) {
// Close with cancel.
platformCtx.kernalContext().event().removeLocalEventListener(lsnr);
ldr.close(true);
}
else {
final long futPtr = reader.readLong();
int valsCnt = reader.readInt();
if (valsCnt > 0) {
Collection<GridMapEntry> vals = new ArrayList<>(valsCnt);
for (int i = 0; i < valsCnt; i++)
vals.add(new GridMapEntry(reader.readObjectDetached(), reader.readObjectDetached()));
PlatformFutureUtils.listen(platformCtx, ldr.addData(vals), futPtr,
PlatformFutureUtils.TYP_OBJ, this);
}
if (plc == PLC_CLOSE) {
platformCtx.kernalContext().event().removeLocalEventListener(lsnr);
ldr.close(false);
}
else if (plc == PLC_FLUSH)
ldr.tryFlush();
else
assert plc == PLC_CONTINUE;
}
return TRUE;
case OP_RECEIVER: {
long ptr = reader.readLong();
Object rec = reader.readObjectDetached();
ldr.receiver(platformCtx.createStreamReceiver(rec, ptr, keepBinary));
return TRUE;
}
default:
return super.processInStreamOutLong(type, reader);
}
}
/** {@inheritDoc} */
@Override protected long processInLongOutLong(int type, final long val) throws IgniteCheckedException {
switch (type) {
case OP_SET_ALLOW_OVERWRITE:
ldr.allowOverwrite(val == TRUE);
return TRUE;
case OP_SET_PER_NODE_BUFFER_SIZE:
ldr.perNodeBufferSize((int) val);
return TRUE;
case OP_SET_SKIP_STORE:
ldr.skipStore(val == TRUE);
return TRUE;
case OP_SET_PER_NODE_PARALLEL_OPS:
ldr.perNodeParallelOperations((int) val);
return TRUE;
case OP_LISTEN_TOPOLOGY: {
lsnr = new GridLocalEventListener() {
@Override public void onEvent(Event evt) {
DiscoveryEvent discoEvt = (DiscoveryEvent)evt;
long topVer = discoEvt.topologyVersion();
int topSize = platformCtx.kernalContext().discovery().cacheNodes(
cacheName, new AffinityTopologyVersion(topVer)).size();
platformCtx.gateway().dataStreamerTopologyUpdate(val, topVer, topSize);
}
};
platformCtx.kernalContext().event().addLocalEventListener(lsnr,
EVT_NODE_JOINED, EVT_NODE_FAILED, EVT_NODE_LEFT);
GridDiscoveryManager discoMgr = platformCtx.kernalContext().discovery();
AffinityTopologyVersion topVer = discoMgr.topologyVersionEx();
int topSize = discoMgr.cacheNodes(cacheName, topVer).size();
platformCtx.gateway().dataStreamerTopologyUpdate(val, topVer.topologyVersion(), topSize);
return TRUE;
}
case OP_ALLOW_OVERWRITE:
return ldr.allowOverwrite() ? TRUE : FALSE;
case OP_PER_NODE_BUFFER_SIZE:
return ldr.perNodeBufferSize();
case OP_SKIP_STORE:
return ldr.skipStore() ? TRUE : FALSE;
case OP_PER_NODE_PARALLEL_OPS:
return ldr.perNodeParallelOperations();
}
return super.processInLongOutLong(type, val);
}
/**
* @return Allow-overwrite flag.
*/
public boolean allowOverwrite() {
return ldr.allowOverwrite();
}
/**
* @param val Allow-overwrite flag.
*/
public void allowOverwrite(boolean val) {
ldr.allowOverwrite(val);
}
/**
* @return Skip store flag.
*/
public boolean skipStore() {
return ldr.skipStore();
}
/**
* @param skipStore Skip store flag.
*/
public void skipStore(boolean skipStore) {
ldr.skipStore(skipStore);
}
/**
* @return Per-node buffer size.
*/
public int perNodeBufferSize() {
return ldr.perNodeBufferSize();
}
/**
* @param val Per-node buffer size.
*/
public void perNodeBufferSize(int val) {
ldr.perNodeBufferSize(val);
}
/**
* @return Per-node parallel load operations.
*/
public int perNodeParallelOperations() {
return ldr.perNodeParallelOperations();
}
/**
* @param val Per-node parallel load operations.
*/
public void perNodeParallelOperations(int val) {
ldr.perNodeParallelOperations(val);
}
}