
com.hazelcast.map.impl.tx.TransactionalMapProxySupport Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2008-2024, 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.mapstore.writebehind.TxnReservedCapacityCounter;
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.UUID;
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 {
protected final Map 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(), mapConfig.getPartitioningAttributeConfigs());
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy