-
Notifications
You must be signed in to change notification settings - Fork 612
/
LockControlCommand.java
243 lines (211 loc) · 7.57 KB
/
LockControlCommand.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
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.infinispan.commands.control;
import org.infinispan.commands.Visitor;
import org.infinispan.commands.tx.AbstractTransactionBoundaryCommand;
import org.infinispan.context.InvocationContext;
import org.infinispan.context.impl.RemoteTxInvocationContext;
import org.infinispan.context.impl.TxInvocationContext;
import org.infinispan.marshall.Ids;
import org.infinispan.marshall.Marshallable;
import org.infinispan.marshall.exts.ReplicableCommandExternalizer;
import org.infinispan.remoting.transport.Address;
import org.infinispan.transaction.xa.GlobalTransaction;
import org.infinispan.transaction.xa.RemoteTransaction;
import org.infinispan.util.Util;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* LockControlCommand is a command that enables distributed locking across infinispan nodes.
* <p/>
* For more details refer to: https://jira.jboss.org/jira/browse/ISPN-70 https://jira.jboss.org/jira/browse/ISPN-48
*
* @author Vladimir Blagojevic (<a href="mailto:vblagoje@redhat.com">vblagoje@redhat.com</a>)
* @author Mircea.Markus@jboss.com
* @since 4.0
*/
@Marshallable(externalizer = ReplicableCommandExternalizer.class, id = Ids.LOCK_CONTROL_COMMAND)
public class LockControlCommand extends AbstractTransactionBoundaryCommand {
private static Log log = LogFactory.getLog(LockControlCommand.class);
public static final int COMMAND_ID = 3;
private Set<Object> keys;
private Object singleKey;
private boolean implicit = false;
private boolean unlock = false;
public LockControlCommand() {
}
public LockControlCommand(Collection<Object> keys, String cacheName) {
this(keys, cacheName, false);
}
public LockControlCommand(Collection<Object> keys, String cacheName, boolean implicit) {
this.cacheName = cacheName;
this.keys = null;
this.singleKey = null;
if (keys != null && !keys.isEmpty()) {
if (keys.size() == 1) {
for (Object k: keys) this.singleKey = k;
} else {
// defensive copy
this.keys = new HashSet<Object>(keys);
}
}
this.implicit = implicit;
}
public void attachGlobalTransaction(GlobalTransaction gtx) {
globalTx = gtx;
}
public Set<Object> getKeys() {
if (keys == null) {
if (singleKey == null)
return Collections.emptySet();
else
return Collections.singleton(singleKey);
}
return keys;
}
public void replaceKey(Object oldKey, Object replacement) {
if (singleKey != null && singleKey.equals(oldKey)) {
singleKey = replacement;
} else {
if (keys != null) {
if (keys.remove(oldKey)) keys.add(replacement);
}
}
}
public void replaceKeys(Map<Object, Object> replacements) {
for (Map.Entry<Object, Object> e: replacements.entrySet()) replaceKey(e.getKey(), e.getValue());
}
public boolean multipleKeys() {
return keys != null && keys.size() > 1;
}
public Object getSingleKey() {
if (singleKey == null) {
if (keys != null) {
for (Object sk: keys) return sk;
return null;
} else {
return null;
}
} else {
return singleKey;
}
}
public boolean isImplicit() {
return implicit;
}
public boolean isExplicit() {
return !isImplicit();
}
public Object acceptVisitor(InvocationContext ctx, Visitor visitor) throws Throwable {
return visitor.visitLockControlCommand((TxInvocationContext) ctx, this);
}
@Override
public Object perform(InvocationContext ignored) throws Throwable {
if (ignored != null)
throw new IllegalStateException("Expected null context!");
RemoteTxInvocationContext ctxt = icc.createRemoteTxInvocationContext();
RemoteTransaction transaction = txTable.getRemoteTransaction(globalTx);
if (transaction == null) {
if (unlock) {
if (log.isTraceEnabled()) {
log.trace("Unlock for non-existant transaction " + globalTx + ". Not doing anything.");
}
return null;
}
//create a remote tx without any modifications (we do not know modifications ahead of time)
transaction = txTable.createRemoteTransaction(globalTx);
}
ctxt.setRemoteTransaction(transaction);
return invoker.invoke(ctxt, this);
}
public byte getCommandId() {
return COMMAND_ID;
}
public Object[] getParameters() {
if (keys == null || keys.isEmpty()) {
if (singleKey == null)
return new Object[]{globalTx, cacheName, unlock, (byte) 1};
else
return new Object[]{globalTx, cacheName, unlock, (byte) 2, singleKey};
}
return new Object[]{globalTx, cacheName, unlock, (byte) 3, keys};
}
@SuppressWarnings("unchecked")
public void setParameters(int commandId, Object[] args) {
if (commandId != COMMAND_ID)
throw new IllegalStateException("Unusupported command id:" + commandId);
globalTx = (GlobalTransaction) args[0];
cacheName = (String) args[1];
unlock = (Boolean)args[2];
keys = null;
singleKey = null;
byte mode = (Byte) args[3];
switch (mode) {
case 1:
break; // do nothing
case 2:
singleKey = args[4];
break;
case 3:
keys = (Set<Object>) args[4];
break;
}
}
public boolean isUnlock() {
return unlock;
}
public void setUnlock(boolean unlock) {
this.unlock = unlock;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
LockControlCommand that = (LockControlCommand) o;
if (!super.equals(that))
return false;
return keys.equals(that.keys) && Util.safeEquals(singleKey, that.singleKey) && (unlock == that.unlock);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (keys != null ? keys.hashCode() : 0);
return 31 * result + (singleKey != null ? singleKey.hashCode() : 0);
}
@Override
public String toString() {
return "LockControlCommand {" +
"gtx=" + globalTx +
", cacheName='" + cacheName +
", implicit='" + implicit +
", keys=" + keys +
", unlock=" + unlock +
", singleKey=" + singleKey + '}';
}
}