|
16 | 16 |
|
17 | 17 | package com.hazelcast.client.cp.internal.datastructures.lock; |
18 | 18 |
|
| 19 | +import com.hazelcast.client.cp.internal.session.SessionManagerProvider; |
19 | 20 | import com.hazelcast.client.impl.clientside.ClientMessageDecoder; |
20 | 21 | import com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl; |
21 | 22 | import com.hazelcast.client.impl.protocol.ClientMessage; |
|
27 | 28 | import com.hazelcast.cp.internal.RaftGroupIdImpl; |
28 | 29 | import com.hazelcast.cp.internal.datastructures.lock.RaftLockOwnershipState; |
29 | 30 | import com.hazelcast.cp.internal.datastructures.lock.proxy.AbstractRaftFencedLockProxy; |
30 | | -import com.hazelcast.client.cp.internal.session.SessionManagerProvider; |
31 | 31 | import com.hazelcast.cp.internal.datastructures.spi.client.RaftGroupTaskFactoryProvider; |
| 32 | +import com.hazelcast.nio.Bits; |
32 | 33 | import com.hazelcast.spi.InternalCompletableFuture; |
33 | 34 |
|
34 | 35 | import java.util.UUID; |
35 | 36 |
|
| 37 | +import static com.hazelcast.client.cp.internal.ClientAccessor.getClient; |
36 | 38 | import static com.hazelcast.client.impl.protocol.util.ParameterUtil.calculateDataSize; |
| 39 | +import static com.hazelcast.cp.internal.RaftGroupIdImpl.dataSize; |
37 | 40 | import static com.hazelcast.cp.internal.RaftService.getObjectNameForProxy; |
38 | 41 | import static com.hazelcast.cp.internal.datastructures.lock.client.LockMessageTaskFactoryProvider.DESTROY_TYPE; |
39 | 42 | import static com.hazelcast.cp.internal.datastructures.lock.client.LockMessageTaskFactoryProvider.FORCE_UNLOCK_TYPE; |
40 | 43 | import static com.hazelcast.cp.internal.datastructures.lock.client.LockMessageTaskFactoryProvider.LOCK_OWNERSHIP_STATE; |
41 | 44 | import static com.hazelcast.cp.internal.datastructures.lock.client.LockMessageTaskFactoryProvider.LOCK_TYPE; |
42 | 45 | import static com.hazelcast.cp.internal.datastructures.lock.client.LockMessageTaskFactoryProvider.TRY_LOCK_TYPE; |
43 | 46 | import static com.hazelcast.cp.internal.datastructures.lock.client.LockMessageTaskFactoryProvider.UNLOCK_TYPE; |
44 | | -import static com.hazelcast.client.cp.internal.datastructures.lock.RaftLockProxy.BOOLEAN_RESPONSE_DECODER; |
45 | | -import static com.hazelcast.client.cp.internal.datastructures.lock.RaftLockProxy.LOCK_OWNERSHIP_STATE_RESPONSE_DECODER; |
46 | | -import static com.hazelcast.client.cp.internal.datastructures.lock.RaftLockProxy.encodeRequest; |
47 | | -import static com.hazelcast.client.cp.internal.datastructures.lock.RaftLockProxy.invoke; |
48 | | -import static com.hazelcast.client.cp.internal.datastructures.lock.RaftLockProxy.prepareClientMessage; |
49 | | -import static com.hazelcast.client.cp.internal.ClientAccessor.getClient; |
50 | 47 |
|
51 | 48 | /** |
52 | 49 | * TODO: Javadoc Pending... |
53 | 50 | */ |
54 | 51 | public class RaftFencedLockProxy extends AbstractRaftFencedLockProxy { |
55 | 52 |
|
| 53 | + static final ClientMessageDecoder BOOLEAN_RESPONSE_DECODER = new BooleanResponseDecoder(); |
| 54 | + static final ClientMessageDecoder LOCK_OWNERSHIP_STATE_RESPONSE_DECODER = new RaftLockOwnershipStateResponseDecoder(); |
| 55 | + |
56 | 56 | public static AbstractRaftFencedLockProxy create(HazelcastInstance instance, String name) { |
57 | 57 | int dataSize = ClientMessage.HEADER_SIZE + calculateDataSize(name); |
58 | 58 | ClientMessage msg = ClientMessage.createForEncode(dataSize); |
@@ -132,4 +132,88 @@ public void destroy() { |
132 | 132 | invoke(client, name, msg, BOOLEAN_RESPONSE_DECODER).join(); |
133 | 133 | } |
134 | 134 |
|
| 135 | + static <T> InternalCompletableFuture<T> invoke(HazelcastClientInstanceImpl client, String name, ClientMessage msg, |
| 136 | + ClientMessageDecoder decoder) { |
| 137 | + ClientInvocationFuture future = new ClientInvocation(client, msg, name).invoke(); |
| 138 | + return new ClientDelegatingFuture<T>(future, client.getSerializationService(), decoder); |
| 139 | + } |
| 140 | + |
| 141 | + static ClientMessage encodeRequest(int messageTypeId, RaftGroupId groupId, String name, long sessionId, |
| 142 | + long threadId, UUID invUid) { |
| 143 | + int dataSize = ClientMessage.HEADER_SIZE |
| 144 | + + dataSize(groupId) + calculateDataSize(name) + Bits.LONG_SIZE_IN_BYTES * 4; |
| 145 | + ClientMessage msg = prepareClientMessage(groupId, name, dataSize, messageTypeId); |
| 146 | + setRequestParams(msg, sessionId, threadId, invUid); |
| 147 | + msg.updateFrameLength(); |
| 148 | + return msg; |
| 149 | + } |
| 150 | + |
| 151 | + static ClientMessage encodeRequest(int messageTypeId, RaftGroupId groupId, String name, long sessionId, |
| 152 | + long threadId, UUID invUid, int val) { |
| 153 | + int dataSize = ClientMessage.HEADER_SIZE |
| 154 | + + dataSize(groupId) + calculateDataSize(name) + Bits.LONG_SIZE_IN_BYTES * 4 + Bits.INT_SIZE_IN_BYTES; |
| 155 | + ClientMessage msg = prepareClientMessage(groupId, name, dataSize, messageTypeId); |
| 156 | + setRequestParams(msg, sessionId, threadId, invUid); |
| 157 | + msg.set(val); |
| 158 | + msg.updateFrameLength(); |
| 159 | + return msg; |
| 160 | + } |
| 161 | + |
| 162 | + static ClientMessage encodeRequest(int messageTypeId, RaftGroupId groupId, String name, long sessionId, |
| 163 | + long threadId, UUID invUid, long val) { |
| 164 | + |
| 165 | + int dataSize = ClientMessage.HEADER_SIZE |
| 166 | + + dataSize(groupId) + calculateDataSize(name) + Bits.LONG_SIZE_IN_BYTES * 5; |
| 167 | + ClientMessage msg = prepareClientMessage(groupId, name, dataSize, messageTypeId); |
| 168 | + setRequestParams(msg, sessionId, threadId, invUid); |
| 169 | + msg.set(val); |
| 170 | + msg.updateFrameLength(); |
| 171 | + return msg; |
| 172 | + } |
| 173 | + |
| 174 | + private static void setRequestParams(ClientMessage msg, long sessionId, long threadId, UUID invUid) { |
| 175 | + msg.set(sessionId); |
| 176 | + msg.set(threadId); |
| 177 | + msg.set(invUid.getLeastSignificantBits()); |
| 178 | + msg.set(invUid.getMostSignificantBits()); |
| 179 | + } |
| 180 | + |
| 181 | + static ClientMessage encodeRequest(int messageTypeId, RaftGroupId groupId, String name, long sessionId, long threadId) { |
| 182 | + int dataSize = ClientMessage.HEADER_SIZE |
| 183 | + + dataSize(groupId) + calculateDataSize(name) + Bits.LONG_SIZE_IN_BYTES * 2; |
| 184 | + ClientMessage msg = prepareClientMessage(groupId, name, dataSize, messageTypeId); |
| 185 | + msg.set(sessionId); |
| 186 | + msg.set(threadId); |
| 187 | + msg.updateFrameLength(); |
| 188 | + return msg; |
| 189 | + } |
| 190 | + |
| 191 | + static ClientMessage prepareClientMessage(RaftGroupId groupId, String name, int dataSize, int messageTypeId) { |
| 192 | + ClientMessage msg = ClientMessage.createForEncode(dataSize); |
| 193 | + msg.setMessageType(messageTypeId); |
| 194 | + msg.setRetryable(false); |
| 195 | + msg.setOperationName(""); |
| 196 | + RaftGroupIdImpl.writeTo(groupId, msg); |
| 197 | + msg.set(name); |
| 198 | + return msg; |
| 199 | + } |
| 200 | + |
| 201 | + private static class BooleanResponseDecoder implements ClientMessageDecoder { |
| 202 | + @Override |
| 203 | + public Boolean decodeClientMessage(ClientMessage msg) { |
| 204 | + return msg.getBoolean(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private static class RaftLockOwnershipStateResponseDecoder implements ClientMessageDecoder { |
| 209 | + @Override |
| 210 | + public RaftLockOwnershipState decodeClientMessage(ClientMessage msg) { |
| 211 | + long fence = msg.getLong(); |
| 212 | + int lockCount = msg.getInt(); |
| 213 | + long sessionId = msg.getLong(); |
| 214 | + long threadId = msg.getLong(); |
| 215 | + return new RaftLockOwnershipState(fence, lockCount, sessionId, threadId); |
| 216 | + } |
| 217 | + } |
| 218 | + |
135 | 219 | } |
0 commit comments