private boolean remove(String id, Cache<SessionCreationMetaDataKey, SessionCreationMetaDataEntry<L>> creationMetaDataCache) { SessionCreationMetaDataKey key = new SessionCreationMetaDataKey(id); try { if (!this.properties.isLockOnWrite() || (creationMetaDataCache.getAdvancedCache().getTransactionManager().getTransaction() == null) || creationMetaDataCache.getAdvancedCache().withFlags(Flag.ZERO_LOCK_ACQUISITION_TIMEOUT, Flag.FAIL_SILENTLY).lock(key)) { creationMetaDataCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(key); this.accessMetaDataCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(new SessionAccessMetaDataKey(id)); return true; } return false; } catch (SystemException e) { throw new CacheException(e); } }
@Override public XSiteAdminOperations getOperations() { return cache.getAdvancedCache().getComponentRegistry().getComponent(XSiteAdminOperations.class); } };
@Override public void mutate() { // We only ever have to perform a replace once within a batch if ((this.mutated == null) || this.mutated.compareAndSet(false, true)) { // Use FAIL_SILENTLY to prevent mutation from failing locally due to remote exceptions this.cache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES, Flag.FAIL_SILENTLY).put(this.id, this.value); } } }
@Override public boolean remove(String id) { this.cache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(new SessionAttributesKey(id)); return true; }
public void testCancelAndGet() throws Exception { DistributedExecutorService des = createDES(getCache()); List<Address> cacheMembers = getCache().getAdvancedCache().getRpcManager().getMembers(); List<Address> members = new ArrayList<>(cacheMembers); assertEquals(caches(cacheName()).size(), members.size()); members.remove(getCache().getAdvancedCache().getRpcManager().getAddress()); DistributedTaskBuilder<Integer> tb = des.createDistributedTaskBuilder(new SleepingSimpleCallable(latchHolder)); final Future<Integer> future = des.submit(members.get(0),tb.build()); future.cancel(true); expectException(CancellationException.class, () -> future.get()); latchHolder.get().open(); }
public void testTaskCancellation() throws Exception { CyclicBarrier barrier = new CyclicBarrier(2); cache(1, cacheName()).getAdvancedCache().getComponentRegistry().registerComponent(barrier, "barrier"); DistributedExecutorService des = createDES(getCache()); List<Address> cacheMembers = getCache().getAdvancedCache().getRpcManager().getMembers(); assertEquals(caches(cacheName()).size(), cacheMembers.size()); DistributedTaskBuilder<Integer> tb = des.createDistributedTaskBuilder(new LongRunningCallable()); final Future<Integer> future = des.submit(address(1), tb.build()); // Will unblock when LongRunningCallable starts running barrier.await(10, TimeUnit.SECONDS); future.cancel(true); boolean taskCancelled = false; try { future.get(); } catch (Exception e) { taskCancelled = e instanceof CancellationException; } assert taskCancelled : "Dist task not cancelled "; // Will unblock when LongRunningCallable is interrupted barrier.await(10, TimeUnit.SECONDS); assert future.isCancelled(); assert future.isDone(); //Testing whether the cancellation already happened. boolean isCanceled = future.cancel(true); assert !isCanceled; }
private void testBeforeTopology(BiFunction<FunctionalMap.ReadWriteMap<String, Integer>, String, Integer> op, int expectedIncrement) throws Exception { cache(0).put("key", 1); // Blocking on receiver side. We cannot block the StateResponseCommand on the server side since // the InternalCacheEntries in its state are the same instances of data stored in DataContainer // - therefore when the command is blocked on sender the command itself would be mutated by applying // the transaction below. BlockingStateConsumer bsc2 = TestingUtil.wrapComponent(cache(2), StateConsumer.class, BlockingStateConsumer::new); tm(2).begin(); FunctionalMap.ReadWriteMap<String, Integer> rw = ReadWriteMapImpl.create( FunctionalMapImpl.create(this.<String, Integer>cache(2).getAdvancedCache())); assertEquals(new Integer(1), op.apply(rw, "key")); Transaction tx = tm(2).suspend(); chf.setOwnerIndexes(0, 2); Future<?> future = fork(() -> { TestResourceTracker.testThreadStarted(this); addClusterEnabledCacheManager(cb).getCache(); }); bsc2.await(); DistributionInfo distributionInfo = cache(2).getAdvancedCache().getDistributionManager().getCacheTopology().getDistribution("key"); assertFalse(distributionInfo.isReadOwner()); assertTrue(distributionInfo.isWriteBackup()); tm(2).resume(tx); tm(2).commit(); bsc2.unblock(); future.get(10, TimeUnit.SECONDS); InternalCacheEntry<Object, Object> ice = cache(2).getAdvancedCache().getDataContainer().get("key"); assertEquals("Current ICE: " + ice, 1 + expectedIncrement, ice.getValue()); }
private void testAfterTopology(BiFunction<FunctionalMap.ReadWriteMap<String, Integer>, String, Integer> op, int expectedIncrement) throws Exception { cache(0).put("key", 1); // Blocking on receiver side. We cannot block the StateResponseCommand on the server side since // the InternalCacheEntries in its state are the same instances of data stored in DataContainer // - therefore when the command is blocked on sender the command itself would be mutated by applying // the transaction below. BlockingStateConsumer bsc2 = TestingUtil.wrapComponent(cache(2), StateConsumer.class, BlockingStateConsumer::new); chf.setOwnerIndexes(0, 2); Future<?> future = fork(() -> { TestResourceTracker.testThreadStarted(this); addClusterEnabledCacheManager(cb).getCache(); }); bsc2.await(); DistributionInfo distributionInfo = cache(2).getAdvancedCache().getDistributionManager().getCacheTopology().getDistribution("key"); assertFalse(distributionInfo.isReadOwner()); assertTrue(distributionInfo.isWriteBackup()); withTx(tm(2), () -> { FunctionalMap.ReadWriteMap<String, Integer> rw = ReadWriteMapImpl.create( FunctionalMapImpl.create(this.<String, Integer>cache(2).getAdvancedCache())); assertEquals(new Integer(1), op.apply(rw, "key")); return null; }); bsc2.unblock(); future.get(10, TimeUnit.SECONDS); InternalCacheEntry<Object, Object> ice = cache(2).getAdvancedCache().getDataContainer().get("key"); assertEquals("Current ICE: " + ice, 1 + expectedIncrement, ice.getValue()); }
public void testAsyncGetCleansContextFlags(Method m) throws Exception { String key = k(m), value = "value2"; Cache<Object, String> nonOwner = getFirstNonOwner(key); Cache<Object, String> owner = getFirstOwner(key); owner.put(key, value); owner.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE).clear(); Future<String> async = nonOwner.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD).getAsync(key); assertNotNull(async); assertNull(async.get()); async = nonOwner.getAdvancedCache().getAsync(key); assertNotNull(async); String returnedValue = async.get(); assertEquals(value, returnedValue); }
public void testGetKeyInGroupWithConcurrentActivation() throws TimeoutException, InterruptedException, ExecutionException { final TestCache testCache = createTestCacheAndReset(GROUP, this.caches(PERSISTENCE_PASSIVATION_CACHE)); initCache(testCache.primaryOwner); final BlockCommandInterceptor interceptor = injectIfAbsent(extractTargetCache(testCache)); interceptor.open = false; Future<Map<GroupKey, String>> future = fork(new Callable<Map<GroupKey, String>>() { @Override public Map<GroupKey, String> call() throws Exception { return testCache.testCache.getGroup(GROUP); } }); interceptor.awaitCommandBlock(); PersistenceManager persistenceManager = TestingUtil.extractComponent(extractTargetCache(testCache), PersistenceManager.class); GroupKey groupKey = (GroupKey) Flowable.fromPublisher(persistenceManager.publishKeys(null, PersistenceManager.AccessMode.BOTH)).take(1).blockingSingle(); AssertJUnit.assertNotNull(extractTargetCache(testCache).get(groupKey)); //activates the key interceptor.unblockCommand(); //it should able to pick the remove key Map<GroupKey, String> groupKeySet = future.get(); Map<GroupKey, String> expectedGroupSet = createMap(0, 10); AssertJUnit.assertEquals(expectedGroupSet, groupKeySet); }
public void testInvalidation() throws Exception { assertEquals(Collections.singletonList(address(0)), advancedCache(0).getDistributionManager().locate(k0)); assertEquals(Collections.singletonList(address(0)), advancedCache(1).getDistributionManager().locate(k0)); advancedCache(1).put(k0, "k1"); assertTrue(advancedCache(1).getDataContainer().containsKey(k0)); assertTrue(advancedCache(0).getDataContainer().containsKey(k0)); tm(0).begin(); cache(0).put(k0, "v2"); tm(0).commit(); assertFalse(advancedCache(1).getDataContainer().containsKey(k0)); }
@Override public void call() throws Exception { Cache<Object, Object> c = cm.getCache(); if (inTran) c.getAdvancedCache().getTransactionManager().begin(); c.put("key1", new SEntity(1, "name1", "surname1")); if (inTran) c.getAdvancedCache().getTransactionManager().commit(); assertEquals(searchByName("name1", c).size(), 1, "should be 1, even repeating this"); } });
public void testFailure() throws Exception { TransactionManager transactionManager = cache.getAdvancedCache().getTransactionManager(); transactionManager.begin(); try { cache.put("k", "v"); assert false; } catch (Exception e) { log.debug("Ignoring expected exception during put", e); assertEquals(transactionManager.getTransaction().getStatus(), Status.STATUS_MARKED_ROLLBACK); } } }
public void testCorrectFunctionalityOnUnconditionalWrite() { MagicKey k1 = getMagicKey(); c1.put(k1, "value"); assertIsInContainerImmortal(c1, k1); assertIsInContainerImmortal(c2, k1); assertIsNotInL1(c3, k1); assertIsNotInL1(c4, k1); assertNull(c4.getAdvancedCache().withFlags(SKIP_REMOTE_LOOKUP).put(k1, "new_val")); assertEquals(c3.get(k1), "new_val"); assertOnAllCachesAndOwnership(k1, "new_val"); }
public void testRemoteLoadFromCacheLoader() throws Exception { Cache<String, String> cache1 = cache(0, "clusteredCl"); Cache<String, String> cache2 = cache(1, "clusteredCl"); CacheWriter writer = TestingUtil.getFirstWriter(cache2); assertNull(cache1.get("key")); assertNull(cache2.get("key")); writer.write(new MarshalledEntryImpl("key", "value", null, cache2.getAdvancedCache().getComponentRegistry().getCacheMarshaller())); assertEquals(((CacheLoader)writer).load("key").getValue(), "value"); assertEquals(cache1.get("key"), "value"); } }
public void testKeySetIsEmptyAfterLocalClear() throws Exception { cache.put(1, "v1"); tm().begin(); try { cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).clear(); assertTrue(cache.keySet().isEmpty()); } finally { tm().commit(); } }
public void testValuesIsEmptyAfterLocalClear() throws Exception { cache.put(1, "v1"); tm().begin(); try { cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL).clear(); assertTrue(cache.values().isEmpty()); } finally { tm().commit(); } }
@Override public void call() throws IOException, InterruptedException { cm.getClassWhiteList().addClasses(Person.class); Cache<String, Person> cache = cm.getCache(); Marshaller marshaller = cache.getAdvancedCache().getComponentRegistry().getCacheMarshaller(); Person value = new Person(); cache.put("1", value); // Read using default valueEncoder assertEquals(cache.get("1"), value); // Read unencoded Cache<?, ?> unencodedCache = cache.getAdvancedCache().withEncoding(IdentityEncoder.class); assertEquals(unencodedCache.get(marshaller.objectToByteBuffer("1")), marshaller.objectToByteBuffer(value)); } });
@Override public void call() { GlobalComponentRegistry registry = cm.getGlobalComponentRegistry(); EncoderRegistry encoderRegistry = registry.getComponent(EncoderRegistry.class); encoderRegistry.registerEncoder(new GzipEncoder()); AdvancedCache<String, String> cache = cm.<String, String>getCache().getAdvancedCache(); AdvancedCache<String, String> compressingCache = (AdvancedCache<String, String>) cache.withEncoding(IdentityEncoder.class, GzipEncoder.class); compressingCache.put("297931749", "0412c789a37f5086f743255cfa693dd502b6a2ecb2ceee68380ff58ad15e7b56"); assertEquals(compressingCache.get("297931749"), "0412c789a37f5086f743255cfa693dd502b6a2ecb2ceee68380ff58ad15e7b56"); Object value = compressingCache.withEncoding(IdentityEncoder.class).get("297931749"); assert value instanceof byte[]; } });
public void testConditionalRemove() { Object k = init(); assertTrue(advancedCache(0).withFlags(Flag.IGNORE_RETURN_VALUES).remove(k, "v0")); assertNull(cache(0).get(k)); assertNull(cache(1).get(k)); }