-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
TransactionalMapProxySupport.java
462 lines (401 loc) · 18.1 KB
/
TransactionalMapProxySupport.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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 com.hazelcast.map.impl.tx;
import com.hazelcast.config.MapConfig;
import com.hazelcast.internal.monitor.impl.LocalMapStatsImpl;
import com.hazelcast.internal.nearcache.NearCache;
import com.hazelcast.internal.nearcache.impl.CompositeRemoteCallHook;
import com.hazelcast.internal.nearcache.impl.RemoteCallHook;
import com.hazelcast.internal.partition.IPartitionService;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.serialization.InternalSerializationService;
import com.hazelcast.internal.util.ThreadUtil;
import com.hazelcast.internal.util.Timer;
import com.hazelcast.internal.util.comparators.ValueComparator;
import com.hazelcast.map.impl.InterceptorRegistry;
import com.hazelcast.map.impl.MapService;
import com.hazelcast.map.impl.MapServiceContext;
import com.hazelcast.map.impl.nearcache.MapNearCacheManager;
import com.hazelcast.map.impl.operation.MapOperation;
import com.hazelcast.map.impl.operation.MapOperationProvider;
import com.hazelcast.partition.PartitioningStrategy;
import com.hazelcast.spi.impl.NodeEngine;
import com.hazelcast.spi.impl.TransactionalDistributedObject;
import com.hazelcast.spi.impl.operationservice.Operation;
import com.hazelcast.spi.impl.operationservice.OperationFactory;
import com.hazelcast.spi.impl.operationservice.OperationService;
import com.hazelcast.transaction.TransactionNotActiveException;
import com.hazelcast.transaction.TransactionOptions.TransactionType;
import com.hazelcast.transaction.TransactionTimedOutException;
import com.hazelcast.transaction.impl.Transaction;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static com.hazelcast.internal.nearcache.NearCache.CACHED_AS_NULL;
import static com.hazelcast.internal.nearcache.NearCache.NOT_CACHED;
import static com.hazelcast.internal.util.ExceptionUtil.rethrow;
import static com.hazelcast.map.impl.MapOperationStatsUpdater.incrementTxnOperationStats;
import static com.hazelcast.map.impl.MapService.SERVICE_NAME;
import static com.hazelcast.map.impl.record.Record.UNSET;
/**
* Base class contains proxy helper methods for {@link com.hazelcast.map.impl.tx.TransactionalMapProxy}
*/
public abstract class TransactionalMapProxySupport extends TransactionalDistributedObject<MapService> {
protected final Map<Data, VersionedValue> valueMap = new HashMap<>();
protected final String name;
protected final boolean statisticsEnabled;
protected final MapServiceContext mapServiceContext;
protected final MapNearCacheManager mapNearCacheManager;
protected final MapOperationProvider operationProvider;
protected final PartitioningStrategy partitionStrategy;
protected final IPartitionService partitionService;
protected final OperationService operationService;
protected final InternalSerializationService ss;
private final boolean serializeKeys;
private final boolean nearCacheEnabled;
private final ValueComparator valueComparator;
private final LocalMapStatsImpl localMapStats;
TransactionalMapProxySupport(String name, MapService mapService,
NodeEngine nodeEngine, Transaction transaction) {
super(nodeEngine, mapService, transaction);
this.name = name;
this.mapServiceContext = mapService.getMapServiceContext();
this.mapNearCacheManager = mapServiceContext.getMapNearCacheManager();
MapConfig mapConfig = nodeEngine.getConfig().findMapConfig(name);
this.operationProvider = mapServiceContext.getMapOperationProvider(name);
this.partitionStrategy = mapServiceContext.getPartitioningStrategy(name,
mapConfig.getPartitioningStrategyConfig());
this.partitionService = nodeEngine.getPartitionService();
this.operationService = nodeEngine.getOperationService();
this.ss = ((InternalSerializationService) nodeEngine.getSerializationService());
this.nearCacheEnabled = mapConfig.isNearCacheEnabled();
this.serializeKeys = nearCacheEnabled && mapConfig.getNearCacheConfig().isSerializeKeys();
this.valueComparator = mapServiceContext.getValueComparatorOf(mapConfig.getInMemoryFormat());
this.statisticsEnabled = mapConfig.isStatisticsEnabled();
this.localMapStats = mapServiceContext.getLocalMapStatsProvider().getLocalMapStatsImpl(name);
}
@Override
public String getName() {
return name;
}
@Override
public final String getServiceName() {
return SERVICE_NAME;
}
boolean isEquals(Object value1, Object value2) {
return valueComparator.isEqual(value1, value2, ss);
}
void checkTransactionState() {
if (!tx.getState().equals(Transaction.State.ACTIVE)) {
throw new TransactionNotActiveException("Transaction is not active!");
}
}
boolean containsKeyInternal(Data dataKey, Object objectKey, boolean skipNearCacheLookup) {
if (!skipNearCacheLookup && nearCacheEnabled) {
Object nearCacheKey = serializeKeys ? dataKey : objectKey;
Object cachedValue = getCachedValue(nearCacheKey, false);
if (cachedValue != NOT_CACHED) {
return cachedValue != null;
}
}
MapOperation operation = operationProvider.createContainsKeyOperation(name, dataKey);
operation.setThreadId(ThreadUtil.getThreadId());
int partitionId = partitionService.getPartitionId(dataKey);
try {
Future future = operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);
Object result = future.get();
incrementOtherOperationsStat();
return (Boolean) result;
} catch (Throwable t) {
throw rethrow(t);
}
}
Object getInternal(Object nearCacheKey, Data keyData, boolean skipNearCacheLookup, long startNanos) {
if (!skipNearCacheLookup && nearCacheEnabled) {
Object value = getCachedValue(nearCacheKey, true);
if (value != NOT_CACHED) {
return value;
}
}
MapOperation operation = operationProvider.createGetOperation(name, keyData);
operation.setThreadId(ThreadUtil.getThreadId());
int partitionId = partitionService.getPartitionId(keyData);
try {
Future future = operationService.createInvocationBuilder(SERVICE_NAME, operation, partitionId)
.setResultDeserialized(false)
.invoke();
Object result = future.get();
if (statisticsEnabled) {
updateOpStats(operation, startNanos);
}
return result;
} catch (Throwable t) {
throw rethrow(t);
}
}
final Object toNearCacheKeyWithStrategy(Object key) {
if (!nearCacheEnabled) {
return key;
}
return serializeKeys ? ss.toData(key, partitionStrategy) : key;
}
final void invalidateNearCache(Object nearCacheKey) {
if (!nearCacheEnabled) {
return;
}
if (nearCacheKey == null) {
return;
}
NearCache<Object, Object> nearCache = mapNearCacheManager.getNearCache(name);
if (nearCache == null) {
return;
}
nearCache.invalidate(nearCacheKey);
}
private Object getCachedValue(Object nearCacheKey, boolean deserializeValue) {
NearCache<Object, Object> nearCache = mapNearCacheManager.getNearCache(name);
if (nearCache == null) {
return NOT_CACHED;
}
Object value = nearCache.get(nearCacheKey);
if (value == null) {
return NOT_CACHED;
}
if (value == CACHED_AS_NULL) {
return null;
}
InterceptorRegistry interceptorRegistry = mapServiceContext.getMapContainer(name).getInterceptorRegistry();
mapServiceContext.interceptAfterGet(interceptorRegistry, value);
return deserializeValue ? ss.toObject(value) : value;
}
Object getForUpdateInternal(Data key) {
VersionedValue versionedValue = lockAndGet(key, tx.getTimeoutMillis(), true);
addUnlockTransactionRecord(key, versionedValue.version);
return versionedValue.value;
}
int sizeInternal() {
try {
OperationFactory sizeOperationFactory = operationProvider.createMapSizeOperationFactory(name);
Map<Integer, Object> results = operationService.invokeOnAllPartitions(SERVICE_NAME, sizeOperationFactory);
int total = 0;
for (Object result : results.values()) {
Integer size = getNodeEngine().toObject(result);
total += size;
}
incrementOtherOperationsStat();
return total;
} catch (Throwable t) {
throw rethrow(t);
}
}
Data putInternal(Data key, Data value, long ttl, TimeUnit timeUnit, RemoteCallHook hook) {
VersionedValue versionedValue = lockAndGet(key, tx.getTimeoutMillis());
long timeInMillis = getTimeInMillis(ttl, timeUnit);
MapOperation op = operationProvider.createTxnSetOperation(name, key, value, versionedValue.version, timeInMillis);
tx.add(new MapTransactionLogRecord(name, key, getPartitionId(key), op, tx.getOwnerUuid(), tx.getTxnId(), hook));
return versionedValue.value;
}
Data putIfAbsentInternal(Data key, Data value, RemoteCallHook hook) {
boolean unlockImmediately = !valueMap.containsKey(key);
VersionedValue versionedValue = lockAndGet(key, tx.getTimeoutMillis());
if (versionedValue.value != null) {
if (unlockImmediately) {
unlock(key, versionedValue);
return versionedValue.value;
}
addUnlockTransactionRecord(key, versionedValue.version);
return versionedValue.value;
}
MapOperation operation = operationProvider.createTxnSetOperation(name, key, value,
versionedValue.version, UNSET);
tx.add(new MapTransactionLogRecord(name, key, getPartitionId(key),
operation, tx.getOwnerUuid(), tx.getTxnId(), hook));
return versionedValue.value;
}
Data replaceInternal(Data key, Data value, RemoteCallHook hook) {
boolean unlockImmediately = !valueMap.containsKey(key);
VersionedValue versionedValue = lockAndGet(key, tx.getTimeoutMillis());
if (versionedValue.value == null) {
if (unlockImmediately) {
unlock(key, versionedValue);
return null;
}
addUnlockTransactionRecord(key, versionedValue.version);
return null;
}
MapOperation operation = operationProvider.createTxnSetOperation(name, key, value,
versionedValue.version, UNSET);
tx.add(new MapTransactionLogRecord(name, key, getPartitionId(key), operation,
tx.getOwnerUuid(), tx.getTxnId(), hook));
return versionedValue.value;
}
boolean replaceIfSameInternal(Data key, Object oldValue,
Data newValue, RemoteCallHook hook) {
boolean unlockImmediately = !valueMap.containsKey(key);
VersionedValue versionedValue = lockAndGet(key, tx.getTimeoutMillis());
if (!isEquals(oldValue, versionedValue.value)) {
if (unlockImmediately) {
unlock(key, versionedValue);
return false;
}
addUnlockTransactionRecord(key, versionedValue.version);
return false;
}
MapOperation operation = operationProvider.createTxnSetOperation(name, key, newValue,
versionedValue.version, UNSET);
tx.add(new MapTransactionLogRecord(name, key, getPartitionId(key), operation,
tx.getOwnerUuid(), tx.getTxnId(), hook));
return true;
}
Data removeInternal(Data key, RemoteCallHook hook) {
VersionedValue versionedValue = lockAndGet(key, tx.getTimeoutMillis());
tx.add(new MapTransactionLogRecord(name, key, getPartitionId(key),
operationProvider.createTxnDeleteOperation(name, key, versionedValue.version),
tx.getOwnerUuid(), tx.getTxnId(), hook));
return versionedValue.value;
}
boolean removeIfSameInternal(Data key, Object value, RemoteCallHook hook) {
boolean unlockImmediately = !valueMap.containsKey(key);
VersionedValue versionedValue = lockAndGet(key, tx.getTimeoutMillis());
if (!isEquals(versionedValue.value, value)) {
if (unlockImmediately) {
unlock(key, versionedValue);
return false;
}
addUnlockTransactionRecord(key, versionedValue.version);
return false;
}
tx.add(new MapTransactionLogRecord(name, key, getPartitionId(key),
operationProvider.createTxnDeleteOperation(name, key, versionedValue.version),
tx.getOwnerUuid(), tx.getTxnId(), hook));
return true;
}
private void unlock(Data key, VersionedValue versionedValue) {
try {
TxnUnlockOperation unlockOperation = new TxnUnlockOperation(name, key, versionedValue.version);
unlockOperation.setThreadId(ThreadUtil.getThreadId());
unlockOperation.setOwnerUuid(tx.getOwnerUuid());
int partitionId = partitionService.getPartitionId(key);
Future<VersionedValue> future
= operationService.invokeOnPartition(SERVICE_NAME, unlockOperation, partitionId);
future.get();
valueMap.remove(key);
} catch (Throwable t) {
throw rethrow(t);
}
}
private void addUnlockTransactionRecord(Data key, long version) {
TxnUnlockOperation operation = new TxnUnlockOperation(name, key, version);
tx.add(new MapTransactionLogRecord(name, key, getPartitionId(key),
operation, tx.getOwnerUuid(), tx.getTxnId(), RemoteCallHook.EMPTY_HOOK));
}
/**
* Locks the key on the partition owner and returns
* the value with the version. Does not invokes
* maploader if the key is missing in memory.
*
* @param key serialized key
* @param timeout timeout in millis
* @return VersionedValue wrapper for value/version pair.
*/
private VersionedValue lockAndGet(Data key, long timeout) {
return lockAndGet(key, timeout, false);
}
private VersionedValue lockAndGet(Data key, long timeout, boolean shouldLoad) {
VersionedValue versionedValue = valueMap.get(key);
if (versionedValue != null) {
return versionedValue;
}
boolean blockReads = tx.getTransactionType() == TransactionType.ONE_PHASE;
MapOperation operation = operationProvider.createTxnLockAndGetOperation(name, key, timeout, timeout,
tx.getOwnerUuid(), shouldLoad, blockReads);
operation.setThreadId(ThreadUtil.getThreadId());
try {
int partitionId = partitionService.getPartitionId(key);
Future<VersionedValue> future = operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);
versionedValue = future.get();
if (versionedValue == null) {
throw new TransactionTimedOutException("Transaction couldn't obtain lock for the key: " + toObjectIfNeeded(key));
}
valueMap.put(key, versionedValue);
return versionedValue;
} catch (Throwable t) {
throw rethrow(t);
}
}
private static long getTimeInMillis(long time, TimeUnit timeunit) {
return timeunit != null ? timeunit.toMillis(time) : time;
}
protected RemoteCallHook newRemoteCallHook() {
CompositeRemoteCallHook hook = null;
if (nearCacheEnabled) {
hook = new CompositeRemoteCallHook();
hook.add(new InvalidationHook());
}
if (statisticsEnabled) {
hook = hook != null ? hook : new CompositeRemoteCallHook();
hook.add(new StatsUpdaterHook());
}
return hook != null ? hook : RemoteCallHook.EMPTY_HOOK;
}
/**
* Hook to be used by local map stats updates.
*/
private class StatsUpdaterHook implements RemoteCallHook {
private long startNanos;
@Override
public void beforeRemoteCall(Object key, Data keyData,
Object value, Data valueData) {
startNanos = Timer.nanos();
}
@Override
public void onRemoteCallSuccess(Operation remoteCall) {
updateOpStats(remoteCall, startNanos);
}
@Override
public void onRemoteCallFailure() {
}
}
protected void updateOpStats(Operation op, long startNanos) {
assert statisticsEnabled;
incrementTxnOperationStats(op, localMapStats, startNanos);
}
protected void incrementOtherOperationsStat() {
if (!statisticsEnabled) {
return;
}
localMapStats.incrementOtherOperations();
}
/**
* Hook for near cahe invalidations.
*/
private class InvalidationHook implements RemoteCallHook {
private Object nearCacheKey;
@Override
public void beforeRemoteCall(Object key, Data keyData, Object value, Data valueData) {
nearCacheKey = serializeKeys ? keyData : mapServiceContext.toObject(key);
}
@Override
public void onRemoteCallSuccess(Operation remoteCall) {
invalidateNearCache(nearCacheKey);
}
@Override
public void onRemoteCallFailure() {
invalidateNearCache(nearCacheKey);
}
}
}