Search in sources :

Example 11 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CacheBackupTest method entrySuccessfullyRetrievedFromBackup.

private void entrySuccessfullyRetrievedFromBackup(int backupCount, boolean sync) {
    final String KEY = "key";
    final String VALUE = "value";
    final int nodeCount = backupCount + 1;
    final TestHazelcastInstanceFactory instanceFactory = createHazelcastInstanceFactory(nodeCount);
    final HazelcastInstance[] instances = new HazelcastInstance[nodeCount];
    for (int i = 0; i < instances.length; i++) {
        instances[i] = instanceFactory.newHazelcastInstance();
    }
    final HazelcastInstance hz = instances[0];
    final CachingProvider cachingProvider = HazelcastServerCachingProvider.createCachingProvider(hz);
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    final String cacheName = randomName();
    final CacheConfig cacheConfig = new CacheConfig().setName(cacheName);
    if (sync) {
        cacheConfig.setBackupCount(backupCount);
    } else {
        cacheConfig.setAsyncBackupCount(backupCount);
    }
    final Cache cache = cacheManager.createCache(cacheName, cacheConfig);
    warmUpPartitions(instances);
    waitAllForSafeState(instances);
    cache.put(KEY, VALUE);
    final Node node = getNode(hz);
    final InternalPartitionService partitionService = node.getPartitionService();
    final int keyPartitionId = partitionService.getPartitionId(KEY);
    for (int i = 1; i <= backupCount; i++) {
        final Node backupNode = getNode(instances[i]);
        final SerializationService serializationService = backupNode.getSerializationService();
        final ICacheService cacheService = backupNode.getNodeEngine().getService(ICacheService.SERVICE_NAME);
        if (sync) {
            checkSavedRecordOnBackup(KEY, VALUE, cacheName, keyPartitionId, serializationService, cacheService);
        } else {
            assertTrueEventually(new AssertTask() {

                @Override
                public void run() throws Exception {
                    checkSavedRecordOnBackup(KEY, VALUE, cacheName, keyPartitionId, serializationService, cacheService);
                }
            });
        }
    }
}
Also used : InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) Node(com.hazelcast.instance.Node) SerializationService(com.hazelcast.spi.serialization.SerializationService) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICacheService(com.hazelcast.cache.impl.ICacheService) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) Cache(javax.cache.Cache)

Example 12 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CachePutAllTest method testPutAll.

@Test
public void testPutAll() {
    ICache<String, String> cache = createCache();
    String cacheName = cache.getName();
    Map<String, String> entries = createAndFillEntries();
    cache.putAll(entries);
    // Verify that put-all works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        String actualValue = cache.get(key);
        assertEquals(expectedValue, actualValue);
    }
    Node node = getNode(hazelcastInstance);
    InternalPartitionService partitionService = node.getPartitionService();
    SerializationService serializationService = node.getSerializationService();
    // Verify that backup of put-all works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        Data keyData = serializationService.toData(key);
        int keyPartitionId = partitionService.getPartitionId(keyData);
        for (int i = 0; i < INSTANCE_COUNT; i++) {
            Node n = getNode(hazelcastInstances[i]);
            ICacheService cacheService = n.getNodeEngine().getService(ICacheService.SERVICE_NAME);
            ICacheRecordStore recordStore = cacheService.getRecordStore("/hz/" + cacheName, keyPartitionId);
            assertNotNull(recordStore);
            String actualValue = serializationService.toObject(recordStore.get(keyData, null));
            assertEquals(expectedValue, actualValue);
        }
    }
}
Also used : InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) ICacheService(com.hazelcast.cache.impl.ICacheService) Node(com.hazelcast.instance.Node) SerializationService(com.hazelcast.spi.serialization.SerializationService) Data(com.hazelcast.nio.serialization.Data) ICacheRecordStore(com.hazelcast.cache.impl.ICacheRecordStore) HashMap(java.util.HashMap) Map(java.util.Map) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 13 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CacheRecordStoreTestSupport method createCacheRecordStore.

protected ICacheRecordStore createCacheRecordStore(HazelcastInstance instance, String cacheName, int partitionId, InMemoryFormat inMemoryFormat) {
    NodeEngine nodeEngine = getNodeEngine(instance);
    ICacheService cacheService = getCacheService(instance);
    CacheConfig cacheConfig = createCacheConfig(cacheName, inMemoryFormat);
    cacheService.putCacheConfigIfAbsent(cacheConfig);
    return new CacheRecordStore(CACHE_NAME_PREFIX + cacheName, partitionId, nodeEngine, (AbstractCacheService) cacheService);
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) ICacheRecordStore(com.hazelcast.cache.impl.ICacheRecordStore) CacheRecordStore(com.hazelcast.cache.impl.CacheRecordStore) ICacheService(com.hazelcast.cache.impl.ICacheService) CacheConfig(com.hazelcast.config.CacheConfig)

Example 14 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CacheLoadAllOperation method run.

@Override
public void run() throws Exception {
    int partitionId = getPartitionId();
    IPartitionService partitionService = getNodeEngine().getPartitionService();
    Set<Data> filteredKeys = null;
    if (keys != null) {
        filteredKeys = new HashSet<Data>();
        for (Data k : keys) {
            if (partitionService.getPartitionId(k) == partitionId) {
                filteredKeys.add(k);
            }
        }
    }
    if (filteredKeys == null || filteredKeys.isEmpty()) {
        return;
    }
    try {
        ICacheService service = getService();
        cache = service.getOrCreateRecordStore(name, partitionId);
        Set<Data> keysLoaded = cache.loadAll(filteredKeys, replaceExistingValues);
        int loadedKeyCount = keysLoaded.size();
        if (loadedKeyCount > 0) {
            backupRecords = new HashMap<Data, CacheRecord>(loadedKeyCount);
            for (Data key : keysLoaded) {
                CacheRecord record = cache.getRecord(key);
                // So if the loaded key is evicted, don't send it to backup.
                if (record != null) {
                    backupRecords.put(key, record);
                }
            }
            shouldBackup = !backupRecords.isEmpty();
        }
    } catch (CacheException e) {
        response = new CacheClearResponse(e);
    }
}
Also used : CacheClearResponse(com.hazelcast.cache.impl.CacheClearResponse) CacheRecord(com.hazelcast.cache.impl.record.CacheRecord) ICacheService(com.hazelcast.cache.impl.ICacheService) CacheException(javax.cache.CacheException) IPartitionService(com.hazelcast.spi.partition.IPartitionService) Data(com.hazelcast.nio.serialization.Data)

Example 15 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CacheClearOperation method beforeRun.

@Override
public void beforeRun() throws Exception {
    ICacheService service = getService();
    cache = service.getRecordStore(name, getPartitionId());
}
Also used : ICacheService(com.hazelcast.cache.impl.ICacheService)

Aggregations

ICacheService (com.hazelcast.cache.impl.ICacheService)24 ICacheRecordStore (com.hazelcast.cache.impl.ICacheRecordStore)9 CacheConfig (com.hazelcast.config.CacheConfig)8 Data (com.hazelcast.nio.serialization.Data)8 QuickTest (com.hazelcast.test.annotation.QuickTest)7 Test (org.junit.Test)7 Node (com.hazelcast.instance.Node)5 SerializationService (com.hazelcast.spi.serialization.SerializationService)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 InternalPartitionService (com.hazelcast.internal.partition.InternalPartitionService)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 CacheRecord (com.hazelcast.cache.impl.record.CacheRecord)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 AssertTask (com.hazelcast.test.AssertTask)3 CacheNotExistsException (com.hazelcast.cache.CacheNotExistsException)2 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)2 Config (com.hazelcast.config.Config)2 NodeEngine (com.hazelcast.spi.NodeEngine)2 HashSet (java.util.HashSet)2