-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
PlatformCompute.java
448 lines (357 loc) · 13.7 KB
/
PlatformCompute.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* 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.compute;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteCompute;
import org.apache.ignite.binary.BinaryObject;
import org.apache.ignite.cluster.ClusterGroup;
import org.apache.ignite.compute.ComputeTaskFuture;
import org.apache.ignite.internal.IgniteComputeImpl;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.binary.BinaryObjectImpl;
import org.apache.ignite.internal.binary.BinaryRawReaderEx;
import org.apache.ignite.internal.binary.BinaryRawWriterEx;
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.processors.platform.utils.PlatformListenable;
import org.apache.ignite.internal.util.future.IgniteFutureImpl;
import org.apache.ignite.lang.IgniteClosure;
import org.apache.ignite.lang.IgniteInClosure;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static org.apache.ignite.internal.processors.task.GridTaskThreadContextKey.TC_SUBGRID;
/**
* Interop compute.
*/
@SuppressWarnings({"unchecked", "ThrowableResultOfMethodCallIgnored", "UnusedDeclaration"})
public class PlatformCompute extends PlatformAbstractTarget {
/** */
private static final int OP_AFFINITY = 1;
/** */
private static final int OP_BROADCAST = 2;
/** */
private static final int OP_EXEC = 3;
/** */
private static final int OP_EXEC_ASYNC = 4;
/** */
private static final int OP_UNICAST = 5;
/** */
private static final int OP_WITH_NO_FAILOVER = 6;
/** */
private static final int OP_WITH_TIMEOUT = 7;
/** */
private static final int OP_EXEC_NATIVE = 8;
/** Compute instance. */
private final IgniteComputeImpl compute;
/** Compute instance for platform-only nodes. */
private final IgniteComputeImpl computeForPlatform;
/** Future for previous asynchronous operation. */
protected ThreadLocal<IgniteInternalFuture> curFut = new ThreadLocal<>();
/**
* Constructor.
*
* @param platformCtx Context.
* @param grp Cluster group.
*/
public PlatformCompute(PlatformContext platformCtx, ClusterGroup grp, String platformAttr) {
super(platformCtx);
assert grp != null;
assert platformAttr != null;
compute = (IgniteComputeImpl)grp.ignite().compute(grp);
ClusterGroup platformGrp = grp.forAttribute(platformAttr, platformCtx.platform());
computeForPlatform = (IgniteComputeImpl)grp.ignite().compute(platformGrp);
}
/** {@inheritDoc} */
@Override protected Object processInStreamOutObject(int type, BinaryRawReaderEx reader)
throws IgniteCheckedException {
switch (type) {
case OP_UNICAST:
return processClosures(reader.readLong(), reader, false, false);
case OP_BROADCAST:
return processClosures(reader.readLong(), reader, true, false);
case OP_AFFINITY:
return processClosures(reader.readLong(), reader, false, true);
case OP_EXEC_NATIVE: {
long taskPtr = reader.readLong();
long topVer = reader.readLong();
final PlatformFullTask task = new PlatformFullTask(platformCtx, computeForPlatform, taskPtr, topVer);
return executeNative0(task);
}
default:
return super.processInStreamOutObject(type, reader);
}
}
/** {@inheritDoc} */
@Override protected long processInLongOutLong(int type, long val) throws IgniteCheckedException {
switch (type) {
case OP_WITH_TIMEOUT: {
compute.withTimeout(val);
computeForPlatform.withTimeout(val);
return TRUE;
}
case OP_WITH_NO_FAILOVER: {
compute.withNoFailover();
computeForPlatform.withNoFailover();
return TRUE;
}
}
return super.processInLongOutLong(type, val);
}
/**
* Process closure execution request.
* @param taskPtr Task pointer.
* @param reader Reader.
* @param broadcast broadcast flag.
*/
private PlatformListenable processClosures(long taskPtr, BinaryRawReaderEx reader, boolean broadcast,
boolean affinity) {
PlatformAbstractTask task;
int size = reader.readInt();
if (size == 1) {
if (broadcast) {
PlatformBroadcastingSingleClosureTask task0 =
new PlatformBroadcastingSingleClosureTask(platformCtx, taskPtr);
task0.job(nextClosureJob(task0, reader));
task = task0;
}
else if (affinity) {
PlatformBalancingSingleClosureAffinityTask task0 =
new PlatformBalancingSingleClosureAffinityTask(platformCtx, taskPtr);
task0.job(nextClosureJob(task0, reader));
task0.affinity(reader.readString(), reader.readObjectDetached(), platformCtx.kernalContext());
task = task0;
}
else {
PlatformBalancingSingleClosureTask task0 = new PlatformBalancingSingleClosureTask(platformCtx, taskPtr);
task0.job(nextClosureJob(task0, reader));
task = task0;
}
}
else {
if (broadcast)
task = new PlatformBroadcastingMultiClosureTask(platformCtx, taskPtr);
else
task = new PlatformBalancingMultiClosureTask(platformCtx, taskPtr);
Collection<PlatformJob> jobs = new ArrayList<>(size);
for (int i = 0; i < size; i++)
jobs.add(nextClosureJob(task, reader));
if (broadcast)
((PlatformBroadcastingMultiClosureTask)task).jobs(jobs);
else
((PlatformBalancingMultiClosureTask)task).jobs(jobs);
}
platformCtx.kernalContext().task().setThreadContext(TC_SUBGRID, computeForPlatform.clusterGroup().nodes());
return executeNative0(task);
}
/**
* Read the next closure job from the reader.
*
* @param task Task.
* @param reader Reader.
* @return Closure job.
*/
private PlatformJob nextClosureJob(PlatformAbstractTask task, BinaryRawReaderEx reader) {
return platformCtx.createClosureJob(task, reader.readLong(), reader.readObjectDetached());
}
/** {@inheritDoc} */
@Override protected void processInStreamOutStream(int type, BinaryRawReaderEx reader, BinaryRawWriterEx writer)
throws IgniteCheckedException {
switch (type) {
case OP_EXEC:
writer.writeObjectDetached(executeJavaTask(reader, false));
break;
case OP_EXEC_ASYNC:
writer.writeObjectDetached(executeJavaTask(reader, true));
break;
default:
super.processInStreamOutStream(type, reader, writer);
}
}
/** <inheritDoc /> */
@Override protected IgniteInternalFuture currentFuture() throws IgniteCheckedException {
IgniteInternalFuture fut = curFut.get();
if (fut == null)
throw new IllegalStateException("Asynchronous operation not started.");
return fut;
}
/**
* Execute task.
*
* @param task Task.
*/
private PlatformListenable executeNative0(final PlatformAbstractTask task) {
IgniteInternalFuture fut = computeForPlatform.executeAsync(task, null);
fut.listen(new IgniteInClosure<IgniteInternalFuture>() {
private static final long serialVersionUID = 0L;
@Override public void apply(IgniteInternalFuture fut) {
try {
fut.get();
task.onDone(null);
}
catch (IgniteCheckedException e) {
task.onDone(e);
}
}
});
return PlatformFutureUtils.getListenable(fut);
}
/**
* Execute task taking arguments from the given reader.
*
* @param reader Reader.
* @return Task result.
*/
protected Object executeJavaTask(BinaryRawReaderEx reader, boolean async) {
String taskName = reader.readString();
boolean keepBinary = reader.readBoolean();
Object arg = reader.readObjectDetached();
Collection<UUID> nodeIds = readNodeIds(reader);
IgniteCompute compute0 = computeForTask(nodeIds);
if (async)
compute0 = compute0.withAsync();
if (!keepBinary && arg instanceof BinaryObjectImpl)
arg = ((BinaryObject)arg).deserialize();
Object res = compute0.execute(taskName, arg);
if (async) {
curFut.set(new ComputeConvertingFuture(compute0.future()));
return null;
}
else
return toBinary(res);
}
/**
* Convert object to binary form.
*
* @param src Source object.
* @return Result.
*/
private Object toBinary(Object src) {
return platformCtx.kernalContext().grid().binary().toBinary(src);
}
/**
* Read node IDs.
*
* @param reader Reader.
* @return Node IDs.
*/
protected Collection<UUID> readNodeIds(BinaryRawReaderEx reader) {
if (reader.readBoolean()) {
int len = reader.readInt();
List<UUID> res = new ArrayList<>(len);
for (int i = 0; i < len; i++)
res.add(reader.readUuid());
return res;
}
else
return null;
}
/**
* Get compute object for the given node IDs.
*
* @param nodeIds Node IDs.
* @return Compute object.
*/
protected IgniteCompute computeForTask(Collection<UUID> nodeIds) {
return nodeIds == null ? compute :
platformCtx.kernalContext().grid().compute(compute.clusterGroup().forNodeIds(nodeIds));
}
/**
* Wraps ComputeTaskFuture as IgniteInternalFuture.
*/
protected class ComputeConvertingFuture implements IgniteInternalFuture {
/** */
private final IgniteInternalFuture fut;
/**
* Ctor.
*
* @param fut Future to wrap.
*/
public ComputeConvertingFuture(ComputeTaskFuture fut) {
this.fut = ((IgniteFutureImpl)fut).internalFuture();
}
/** {@inheritDoc} */
@Override public Object get() throws IgniteCheckedException {
return convertResult(fut.get());
}
/** {@inheritDoc} */
@Override public Object get(long timeout) throws IgniteCheckedException {
return convertResult(fut.get(timeout));
}
/** {@inheritDoc} */
@Override public Object get(long timeout, TimeUnit unit) throws IgniteCheckedException {
return convertResult(fut.get(timeout, unit));
}
/** {@inheritDoc} */
@Override public Object getUninterruptibly() throws IgniteCheckedException {
return convertResult(fut.get());
}
/** {@inheritDoc} */
@Override public boolean cancel() throws IgniteCheckedException {
return fut.cancel();
}
/** {@inheritDoc} */
@Override public boolean isDone() {
return fut.isDone();
}
/** {@inheritDoc} */
@Override public boolean isCancelled() {
return fut.isCancelled();
}
/** {@inheritDoc} */
@Override public long startTime() {
return fut.startTime();
}
/** {@inheritDoc} */
@Override public long duration() {
return fut.duration();
}
/** {@inheritDoc} */
@Override public void listen(final IgniteInClosure lsnr) {
fut.listen(new IgniteInClosure<IgniteInternalFuture>() {
private static final long serialVersionUID = 0L;
@Override public void apply(IgniteInternalFuture fut0) {
lsnr.apply(ComputeConvertingFuture.this);
}
});
}
/** {@inheritDoc} */
@Override public IgniteInternalFuture chain(IgniteClosure doneCb) {
throw new UnsupportedOperationException("Chain operation is not supported.");
}
/** {@inheritDoc} */
@Override public Throwable error() {
return fut.error();
}
/** {@inheritDoc} */
@Override public Object result() {
return convertResult(fut.result());
}
/**
* Converts future result.
*
* @param obj Object to convert.
* @return Result.
*/
protected Object convertResult(Object obj) {
return toBinary(obj);
}
}
}