forked from infinispan/infinispan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrianglePerCacheInboundInvocationHandler.java
366 lines (326 loc) · 15.2 KB
/
TrianglePerCacheInboundInvocationHandler.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
package org.infinispan.remoting.inboundhandler;
import static org.infinispan.remoting.inboundhandler.DeliverOrder.NONE;
import java.util.Collection;
import org.infinispan.commands.CommandInvocationId;
import org.infinispan.commands.CommandsFactory;
import org.infinispan.commands.ReplicableCommand;
import org.infinispan.commands.remote.CacheRpcCommand;
import org.infinispan.commands.remote.SingleRpcCommand;
import org.infinispan.commands.write.BackupAckCommand;
import org.infinispan.commands.write.BackupMultiKeyAckCommand;
import org.infinispan.commands.write.BackupPutMapRpcCommand;
import org.infinispan.commands.write.BackupWriteRpcCommand;
import org.infinispan.commands.write.ExceptionAckCommand;
import org.infinispan.commands.write.PrimaryAckCommand;
import org.infinispan.commands.write.WriteCommand;
import org.infinispan.configuration.cache.BiasAcquisition;
import org.infinispan.distribution.TriangleOrderManager;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.interceptors.locking.ClusteringDependentLogic;
import org.infinispan.remoting.inboundhandler.action.Action;
import org.infinispan.remoting.inboundhandler.action.ActionState;
import org.infinispan.remoting.inboundhandler.action.ActionStatus;
import org.infinispan.remoting.inboundhandler.action.DefaultReadyAction;
import org.infinispan.remoting.inboundhandler.action.LockAction;
import org.infinispan.remoting.inboundhandler.action.ReadyAction;
import org.infinispan.remoting.inboundhandler.action.TriangleOrderAction;
import org.infinispan.remoting.rpc.RpcManager;
import org.infinispan.remoting.transport.Address;
import org.infinispan.statetransfer.StateRequestCommand;
import org.infinispan.util.concurrent.BlockingRunnable;
import org.infinispan.util.concurrent.BlockingTaskAwareExecutorService;
import org.infinispan.util.concurrent.CommandAckCollector;
import org.infinispan.util.concurrent.locks.LockListener;
import org.infinispan.util.concurrent.locks.LockManager;
import org.infinispan.util.concurrent.locks.LockState;
import org.infinispan.util.concurrent.locks.RemoteLockCommand;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
/**
* A {@link PerCacheInboundInvocationHandler} implementation for non-transactional and distributed caches that uses the
* triangle algorithm.
*
* @author Pedro Ruivo
* @since 9.0
*/
public class TrianglePerCacheInboundInvocationHandler extends BasePerCacheInboundInvocationHandler implements
LockListener, Action {
private static final Log log = LogFactory.getLog(TrianglePerCacheInboundInvocationHandler.class);
private static final boolean trace = log.isTraceEnabled();
@Inject private LockManager lockManager;
@Inject private ClusteringDependentLogic clusteringDependentLogic;
@Inject private TriangleOrderManager triangleOrderManager;
@Inject private RpcManager rpcManager;
@Inject private CommandAckCollector commandAckCollector;
@Inject private CommandsFactory commandsFactory;
private long lockTimeout;
private Address localAddress;
private boolean indirectRpc;
@Start
public void start() {
lockTimeout = configuration.locking().lockAcquisitionTimeout();
localAddress = rpcManager.getAddress();
this.indirectRpc = configuration.clustering().biasAcquisition() != BiasAcquisition.NEVER;
}
@Override
public void handle(CacheRpcCommand command, Reply reply, DeliverOrder order) {
if (order == DeliverOrder.TOTAL) {
unexpectedDeliverMode(command, order);
}
try {
switch (command.getCommandId()) {
case SingleRpcCommand.COMMAND_ID:
handleSingleRpcCommand((SingleRpcCommand) command, reply, order);
return;
case BackupWriteRpcCommand.COMMAND_ID:
handleBackupWriteRpcCommand((BackupWriteRpcCommand) command);
return;
case BackupPutMapRpcCommand.COMMAND_ID:
handleBackupPutMapRpcCommand((BackupPutMapRpcCommand) command);
return;
case BackupAckCommand.COMMAND_ID:
handleBackupAckCommand((BackupAckCommand) command);
return;
case BackupMultiKeyAckCommand.COMMAND_ID:
handleBackupMultiKeyAckCommand((BackupMultiKeyAckCommand) command);
return;
case ExceptionAckCommand.COMMAND_ID:
handleExceptionAck((ExceptionAckCommand) command);
return;
case PrimaryAckCommand.COMMAND_ID:
handlePrimaryAck((PrimaryAckCommand) command);
return;
case StateRequestCommand.COMMAND_ID:
handleStateRequestCommand((StateRequestCommand) command, reply, order);
return;
default:
handleDefaultCommand(command, reply, order);
}
} catch (Throwable throwable) {
reply.reply(exceptionHandlingCommand(command, throwable));
}
}
//lock listener interface
@Override
public void onEvent(LockState state) {
remoteCommandsExecutor.checkForReadyTasks();
}
//action interface
@Override
public ActionStatus check(ActionState state) {
return isCommandSentBeforeFirstTopology(state.getCommandTopologyId()) ?
ActionStatus.CANCELED :
ActionStatus.READY;
}
public TriangleOrderManager getTriangleOrderManager() {
return triangleOrderManager;
}
public BlockingTaskAwareExecutorService getRemoteExecutor() {
return remoteCommandsExecutor;
}
public ClusteringDependentLogic getClusteringDependentLogic() {
return clusteringDependentLogic;
}
@Override
public void onFinally(ActionState state) {
//no-op
//needed for ConditionalOperationPrimaryOwnerFailTest
//it mocks this class and when Action.onFinally is invoked, it doesn't behave well with the default implementation
//in the interface.
}
@Override
protected Log getLog() {
return log;
}
@Override
protected boolean isTraceEnabled() {
return trace;
}
private void handleStateRequestCommand(StateRequestCommand command, Reply reply, DeliverOrder order) {
if (executeOnExecutorService(order, command)) {
BlockingRunnable runnable = createDefaultRunnable(command, reply, extractCommandTopologyId(command),
TopologyMode.READY_TOPOLOGY, order.preserveOrder());
remoteCommandsExecutor.execute(runnable);
} else {
BlockingRunnable runnable = createDefaultRunnable(command, reply, extractCommandTopologyId(command),
TopologyMode.WAIT_TOPOLOGY, order.preserveOrder());
runnable.run();
}
}
private void handleDefaultCommand(CacheRpcCommand command, Reply reply, DeliverOrder order) {
if (executeOnExecutorService(order, command)) {
BlockingRunnable runnable = createDefaultRunnable(command, reply, extractCommandTopologyId(command),
TopologyMode.READY_TX_DATA, order.preserveOrder());
remoteCommandsExecutor.execute(runnable);
} else {
BlockingRunnable runnable = createDefaultRunnable(command, reply, extractCommandTopologyId(command),
TopologyMode.WAIT_TX_DATA, order.preserveOrder());
runnable.run();
}
}
private void handleBackupPutMapRpcCommand(BackupPutMapRpcCommand command) {
final int topologyId = command.getTopologyId();
ReadyAction readyAction = createTriangleOrderAction(command, topologyId, command.getSequence(),
command.getMap().keySet().iterator().next());
BlockingRunnable runnable = createBackupPutMapRunnable(command, topologyId, readyAction);
remoteCommandsExecutor.execute(runnable);
}
private void handleBackupWriteRpcCommand(BackupWriteRpcCommand command) {
final int topologyId = command.getTopologyId();
ReadyAction readyAction = createTriangleOrderAction(command, topologyId, command.getSequence(), command.getKey());
BlockingRunnable runnable = createBackupWriteRpcRunnable(command, topologyId, readyAction);
remoteCommandsExecutor.execute(runnable);
}
private void handleExceptionAck(ExceptionAckCommand command) {
command.ack();
}
private void handleBackupMultiKeyAckCommand(BackupMultiKeyAckCommand command) {
command.ack();
}
private void handleBackupAckCommand(BackupAckCommand command) {
command.ack();
}
private void handlePrimaryAck(PrimaryAckCommand command) {
command.ack();
}
private void handleSingleRpcCommand(SingleRpcCommand command, Reply reply, DeliverOrder order) {
if (executeOnExecutorService(order, command)) {
int commandTopologyId = extractCommandTopologyId(command);
BlockingRunnable runnable;
// Bias-enabled scattered cache sends WriteCommands both synchronously (from primary == originator to backup)
// and asynchronously (from originator to primary).
if (indirectRpc && reply == Reply.NO_OP) {
runnable = createIndirectRpcRunnable(command, commandTopologyId);
} else {
runnable = createReadyActionRunnable(command, reply, commandTopologyId, order.preserveOrder(),
createReadyAction(commandTopologyId, command));
}
remoteCommandsExecutor.execute(runnable);
} else {
createDefaultRunnable(command, reply, extractCommandTopologyId(command), TopologyMode.WAIT_TX_DATA,
order.preserveOrder()).run();
}
}
private BlockingRunnable createIndirectRpcRunnable(SingleRpcCommand command, int commandTopologyId) {
WriteCommand writeCommand = (WriteCommand) command.getCommand();
return new DefaultTopologyRunnable(this, command, Reply.NO_OP, TopologyMode.READY_TX_DATA, commandTopologyId, false) {
@Override
protected void onException(Throwable throwable) {
sendExceptionAck(writeCommand.getCommandInvocationId(), throwable, commandTopologyId);
}
};
}
private void sendExceptionAck(CommandInvocationId id, Throwable throwable, int topologyId) {
final Address origin = id.getAddress();
if (trace) {
log.tracef("Sending exception ack for command %s. Originator=%s.", id, origin);
}
if (origin.equals(localAddress)) {
commandAckCollector.completeExceptionally(id.getId(), throwable, topologyId);
} else {
rpcManager.sendTo(origin, commandsFactory.buildExceptionAckCommand(id.getId(), throwable, topologyId), NONE);
}
}
private void sendBackupAck(CommandInvocationId id, int topologyId) {
final Address origin = id.getAddress();
boolean isLocal = localAddress.equals(origin);
if (trace) {
log.tracef("Sending ack for command %s. isLocal? %s.", id, isLocal);
}
if (isLocal) {
commandAckCollector.backupAck(id.getId(), origin, topologyId);
} else {
rpcManager.sendTo(origin, commandsFactory.buildBackupAckCommand(id.getId(), topologyId), NONE);
}
}
private BlockingRunnable createBackupWriteRpcRunnable(BackupWriteRpcCommand command, int commandTopologyId,
ReadyAction readyAction) {
readyAction.addListener(remoteCommandsExecutor::checkForReadyTasks);
return new DefaultTopologyRunnable(this, command, Reply.NO_OP, TopologyMode.READY_TX_DATA, commandTopologyId,
false) {
@Override
public boolean isReady() {
return super.isReady() && readyAction.isReady();
}
@Override
protected void onException(Throwable throwable) {
super.onException(throwable);
readyAction.onException();
readyAction.onFinally(); //notified TriangleOrderManager before sending the ack.
sendExceptionAck(((BackupWriteRpcCommand) command).getCommandInvocationId(), throwable, commandTopologyId);
}
@Override
protected void afterInvoke() {
super.afterInvoke();
readyAction.onFinally();
sendBackupAck(((BackupWriteRpcCommand) command).getCommandInvocationId(), commandTopologyId);
}
};
}
private void sendPutMapBackupAck(CommandInvocationId id, int topologyId, int segment) {
final Address origin = id.getAddress();
if (trace) {
log.tracef("Sending ack for command %s. Originator=%s.", id, origin);
}
if (id.getAddress().equals(localAddress)) {
commandAckCollector.multiKeyBackupAck(id.getId(), localAddress, segment, topologyId);
} else {
BackupMultiKeyAckCommand command =
commandsFactory.buildBackupMultiKeyAckCommand(id.getId(), segment, topologyId);
rpcManager.sendTo(origin, command, NONE);
}
}
private BlockingRunnable createBackupPutMapRunnable(BackupPutMapRpcCommand command, int commandTopologyId,
ReadyAction readyAction) {
readyAction.addListener(remoteCommandsExecutor::checkForReadyTasks);
return new DefaultTopologyRunnable(this, command, Reply.NO_OP, TopologyMode.READY_TX_DATA, commandTopologyId,
false) {
@Override
public boolean isReady() {
return super.isReady() && readyAction.isReady();
}
@Override
protected void onException(Throwable throwable) {
super.onException(throwable);
readyAction.onException();
readyAction.onFinally();
sendExceptionAck(((BackupPutMapRpcCommand) command).getCommandInvocationId(), throwable, commandTopologyId);
}
@Override
protected void afterInvoke() {
super.afterInvoke();
readyAction.onFinally();
Object key = ((BackupPutMapRpcCommand) command).getMap().keySet().iterator().next();
int segment = clusteringDependentLogic.getCacheTopology().getDistribution(key).segmentId();
sendPutMapBackupAck(((BackupPutMapRpcCommand) command).getCommandInvocationId(), commandTopologyId,
segment);
}
};
}
private ReadyAction createReadyAction(int topologyId, RemoteLockCommand command) {
if (command.hasSkipLocking()) {
return null;
}
Collection<?> keys = command.getKeysToLock();
if (keys.isEmpty()) {
return null;
}
final long timeoutMillis = command.hasZeroLockAcquisition() ? 0 : lockTimeout;
DefaultReadyAction action = new DefaultReadyAction(new ActionState(command, topologyId, timeoutMillis),
this,
new LockAction(lockManager, clusteringDependentLogic));
action.registerListener();
return action;
}
private ReadyAction createReadyAction(int topologyId, SingleRpcCommand singleRpcCommand) {
ReplicableCommand command = singleRpcCommand.getCommand();
return command instanceof RemoteLockCommand ?
createReadyAction(topologyId, (RemoteLockCommand & ReplicableCommand) command) :
null;
}
private ReadyAction createTriangleOrderAction(ReplicableCommand command, int topologyId, long sequence, Object key) {
return new DefaultReadyAction(new ActionState(command, topologyId, 0), this,
new TriangleOrderAction(this, sequence, key));
}
}