Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.cache.impl;
import com.hazelcast.cache.HazelcastCacheManager;
import com.hazelcast.cache.impl.event.CachePartitionLostEventFilter;
import com.hazelcast.cache.impl.event.CachePartitionLostListener;
import com.hazelcast.cache.impl.event.InternalCachePartitionLostListenerAdapter;
import com.hazelcast.cache.impl.operation.CacheListenerRegistrationOperation;
import com.hazelcast.cache.impl.operation.MutableOperation;
import com.hazelcast.cluster.Member;
import com.hazelcast.config.CacheConfig;
import com.hazelcast.config.CachePartitionLostListenerConfig;
import com.hazelcast.config.ListenerConfig;
import com.hazelcast.core.ManagedContext;
import com.hazelcast.internal.namespace.NamespaceUtil;
import com.hazelcast.internal.nio.ClassLoaderUtil;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.internal.util.ExceptionUtil;
import com.hazelcast.internal.util.FutureUtil;
import com.hazelcast.internal.util.collection.PartitionIdSet;
import com.hazelcast.internal.util.executor.CompletableFutureTask;
import com.hazelcast.logging.ILogger;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.spi.impl.AbstractDistributedObject;
import com.hazelcast.spi.impl.NodeEngine;
import com.hazelcast.spi.impl.eventservice.EventFilter;
import com.hazelcast.spi.impl.executionservice.ExecutionService;
import com.hazelcast.spi.impl.operationservice.Operation;
import com.hazelcast.spi.impl.operationservice.OperationFactory;
import com.hazelcast.spi.impl.operationservice.OperationService;
import com.hazelcast.spi.impl.operationservice.impl.InvocationFuture;
import com.hazelcast.internal.partition.IPartitionService;
import javax.annotation.Nonnull;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.expiry.ExpiryPolicy;
import javax.cache.integration.CompletionListener;
import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EventListener;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static com.hazelcast.cache.impl.CacheProxyUtil.validateNotNull;
import static com.hazelcast.cache.impl.operation.MutableOperation.IGNORE_COMPLETION;
import static com.hazelcast.internal.util.ConcurrencyUtil.CALLER_RUNS;
import static com.hazelcast.internal.util.ExceptionUtil.rethrow;
import static com.hazelcast.internal.util.ExceptionUtil.rethrowAllowedTypeFirst;
import static com.hazelcast.internal.util.SetUtil.createHashSet;
import com.hazelcast.spi.tenantcontrol.DestroyEventContext;
/**
* Abstract {@link com.hazelcast.cache.ICache} implementation which provides shared internal implementations
* of cache operations like put, replace, remove and invoke. These internal implementations are delegated
* by actual cache methods.
*
*
Note: this partial implementation is used by server or embedded mode cache.
*
* @param the type of key.
* @param the type of value.
* @see com.hazelcast.cache.impl.CacheProxy
* @see com.hazelcast.cache.ICache
*/
@SuppressWarnings({"checkstyle:methodcount", "checkstyle:classfanoutcomplexity"})
abstract class CacheProxySupport
extends AbstractDistributedObject
implements ICacheInternal, CacheSyncListenerCompleter {
private static final int TIMEOUT = 10;
protected final ILogger logger;
protected CacheConfig cacheConfig;
protected final String name;
protected final String nameWithPrefix;
protected final ICacheService cacheService;
protected final SerializationService serializationService;
protected final CacheOperationProvider operationProvider;
protected final IPartitionService partitionService;
private final CopyOnWriteArrayList loadAllTasks = new CopyOnWriteArrayList<>();
private final AtomicReference cacheManagerRef = new AtomicReference<>();
private final AtomicBoolean isClosed = new AtomicBoolean(false);
private final AtomicBoolean isDestroyed = new AtomicBoolean(false);
private final CacheProxySyncListenerCompleter listenerCompleter = new CacheProxySyncListenerCompleter(this);
CacheProxySupport(CacheConfig cacheConfig, NodeEngine nodeEngine, ICacheService cacheService) {
super(nodeEngine, cacheService);
this.name = cacheConfig.getName();
this.nameWithPrefix = cacheConfig.getNameWithPrefix();
this.cacheConfig = cacheConfig;
this.logger = nodeEngine.getLogger(getClass());
this.partitionService = nodeEngine.getPartitionService();
this.cacheService = cacheService;
this.serializationService = nodeEngine.getSerializationService();
this.operationProvider =
cacheService.getCacheOperationProvider(nameWithPrefix, cacheConfig.getInMemoryFormat());
List configs = cacheConfig.getPartitionLostListenerConfigs();
for (CachePartitionLostListenerConfig listenerConfig : configs) {
CachePartitionLostListener listener = initializeListener(listenerConfig);
if (listener != null) {
EventFilter filter = new CachePartitionLostEventFilter();
CacheEventListener listenerAdapter = new InternalCachePartitionLostListenerAdapter(listener);
getService().getNodeEngine().getEventService()
.registerListener(AbstractCacheService.SERVICE_NAME, name, filter, listenerAdapter);
}
}
}
@Override
public String getName() {
return name;
}
@Override
protected String getDistributedObjectName() {
return nameWithPrefix;
}
@Override
public String getPrefixedName() {
return nameWithPrefix;
}
@Override
public String getServiceName() {
return ICacheService.SERVICE_NAME;
}
@Override
public void open() {
if (isDestroyed.get()) {
throw new IllegalStateException("Cache is already destroyed! Cannot be reopened");
}
isClosed.compareAndSet(true, false);
}
@Override
public void close() {
close0(false);
}
@Override
public @Nonnull DestroyEventContext getDestroyContextForTenant() {
return () -> cacheConfig = ((CacheService) cacheService).reSerializeCacheConfig(cacheConfig);
}
@Override
protected boolean preDestroy() {
close0(true);
if (!isDestroyed.compareAndSet(false, true)) {
return false;
}
isClosed.set(true);
return true;
}
@Override
public boolean isClosed() {
return isClosed.get();
}
@Override
public boolean isDestroyed() {
return isDestroyed.get();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
CacheProxySupport that = (CacheProxySupport) o;
if (nameWithPrefix != null ? !nameWithPrefix.equals(that.nameWithPrefix) : that.nameWithPrefix != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
return nameWithPrefix != null ? nameWithPrefix.hashCode() : 0;
}
@Override
public String toString() {
return getClass().getName() + '{' + "name=" + name + ", nameWithPrefix=" + nameWithPrefix + '}';
}
@Override
public CacheManager getCacheManager() {
return cacheManagerRef.get();
}
@Override
public void setCacheManager(HazelcastCacheManager cacheManager) {
assert cacheManager instanceof HazelcastServerCacheManager;
// optimistically assume the CacheManager is already set
if (cacheManagerRef.get() == cacheManager) {
return;
}
if (!this.cacheManagerRef.compareAndSet(null, (HazelcastServerCacheManager) cacheManager)) {
if (cacheManagerRef.get() == cacheManager) {
// some other thread managed to set the same CacheManager, we are good
return;
}
throw new IllegalStateException("Cannot overwrite a Cache's CacheManager.");
}
}
@Override
public void resetCacheManager() {
cacheManagerRef.set(null);
}
@Override
protected void postDestroy() {
CacheManager cacheManager = cacheManagerRef.get();
if (cacheManager != null) {
cacheManager.destroyCache(getName());
}
resetCacheManager();
}
@Override
public void countDownCompletionLatch(int countDownLatchId) {
listenerCompleter.countDownCompletionLatch(countDownLatchId);
}
protected void ensureOpen() {
if (isClosed()) {
throw new IllegalStateException("Cache operations can not be performed. The cache closed");
}
}
@SuppressWarnings("unchecked")
protected void createAndSubmitLoadAllTask(Set keysData, boolean replaceExistingValues,
CompletionListener completionListener) {
try {
CacheProxyLoadAllTask loadAllTask = new CacheProxyLoadAllTask(getNodeEngine(), operationProvider, keysData,
replaceExistingValues, completionListener, getServiceName());
ExecutionService executionService = getNodeEngine().getExecutionService();
final CompletableFutureTask