private SingletonCacheWriter[] extractStores(Cache[] caches) { SingletonCacheWriter[] stores = new SingletonCacheWriter[caches.length]; int i = 0; for (Cache c : caches) stores[i++] = (SingletonCacheWriter) TestingUtil.getFirstWriter(c); return stores; }
private DummyInMemoryStore cacheStore(Ownership ownership) { Cache<K, V> cache = cache(ownership); return cache != null ? getFirstWriter(cache) : null; }
protected int getCacheStoreStats(Cache<?, ?> cache, String cacheStoreMethod) { int actual; AdvancedLoadWriteStore cs = TestingUtil.getFirstWriter(cache); if (cs instanceof ComposedSegmentedLoadWriteStore) { AtomicInteger count = new AtomicInteger(); ((ComposedSegmentedLoadWriteStore) cs).forEach((store, segment) -> count.addAndGet(((DummyInMemoryStore) store).stats().get(cacheStoreMethod))); actual = count.get(); } else { actual = ((DummyInMemoryStore) cs).stats().get(cacheStoreMethod); } return actual; }
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"); } }
@Override protected EmbeddedCacheManager createCacheManager() throws Exception { ConfigurationBuilder cb = getDefaultStandaloneCacheConfig(false); configurePersistence(cb); EmbeddedCacheManager manager = TestCacheManagerFactory.createCacheManager(cb); ComponentRegistry componentRegistry = manager.getCache().getAdvancedCache().getComponentRegistry(); PersistenceManagerImpl pm = (PersistenceManagerImpl) componentRegistry.getComponent(PersistenceManager.class); sm = pm.getMarshaller(); loader = TestingUtil.getFirstLoader(manager.getCache()); writer = TestingUtil.getFirstWriter(manager.getCache()); executor = new ThreadPoolExecutor(NUM_THREADS, NUM_THREADS, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<>(), getTestThreadFactory("iteration"), new ThreadPoolExecutor.CallerRunsPolicy()); return manager; }
private static <K, V> void writeToStore(Cache<K, V> cache, K key, V value) { TestingUtil.getFirstWriter(cache).write(marshalledEntry(key, value, cache.getAdvancedCache().getComponentRegistry().getCacheMarshaller())); }
MockAsyncCacheWriter cacheStore = TestingUtil.getFirstWriter(cache); CountDownLatch modApplyLatch = cacheStore.modApplyLatch; CountDownLatch lockedWaitLatch = cacheStore.lockedWaitLatch;
public void testProcess() throws ExecutionException, InterruptedException { final int writerThreads = 2; final int numberOfKeys = 2000; Cache<String, String> cache = cacheManager.getCache(CACHE_NAME); final SingleFileStore store = TestingUtil.getFirstWriter(cache); final StreamingMarshaller marshaller = TestingUtil.extractComponentRegistry(cache).getCacheMarshaller(); assertEquals(0, store.size()); final List<String> keys = new ArrayList<>(numberOfKeys); populateStoreRandomValues(numberOfKeys, store, marshaller, keys); final CountDownLatch stopLatch = new CountDownLatch(1); Future[] writeFutures = new Future[writerThreads]; for (int i = 0; i < writerThreads; i++) { writeFutures[i] = fork(stopOnException(new WriteTask(store, marshaller, keys, stopLatch), stopLatch)); } Future processFuture = fork(stopOnException(new ProcessTask(store), stopLatch)); // Stop the writers only after we finish processing processFuture.get(); stopLatch.countDown(); for (int i = 0; i < writerThreads; i++) { writeFutures[i].get(); } }
public void testProcessWithNoDiskAccess() throws ExecutionException, InterruptedException { final int writerThreads = 2; final int numberOfKeys = 2000; Cache<String, String> cache = cacheManager.getCache(CACHE_NAME); final SingleFileStore store = TestingUtil.getFirstWriter(cache); final StreamingMarshaller marshaller = TestingUtil.extractComponentRegistry(cache).getCacheMarshaller(); assertEquals(0, store.size()); final List<String> keys = new ArrayList<>(numberOfKeys); populateStoreRandomValues(numberOfKeys, store, marshaller, keys); final CountDownLatch stopLatch = new CountDownLatch(1); Future[] writeFutures = new Future[writerThreads]; for (int i = 0; i < writerThreads; i++) { writeFutures[i] = fork(stopOnException(new WriteTask(store, marshaller, keys, stopLatch), stopLatch)); } Future processFuture = fork(stopOnException(new ProcessTaskNoDiskRead(store), stopLatch)); // Stop the writers only after we finish processing processFuture.get(); stopLatch.countDown(); for (int i = 0; i < writerThreads; i++) { writeFutures[i].get(); } }
final SingleFileStore store = TestingUtil.getFirstWriter(cache); final StreamingMarshaller marshaller = TestingUtil.extractComponentRegistry(cache).getCacheMarshaller(); assertEquals(0, store.size());
public void testReadsAndWrites() throws ExecutionException, InterruptedException { final int writerThreads = 2; final int readerThreads = 2; Cache<String, String> cache = cacheManager.getCache(CACHE_NAME); final SingleFileStore store = TestingUtil.getFirstWriter(cache); final StreamingMarshaller marshaller = TestingUtil.extractComponentRegistry(cache).getCacheMarshaller(); assertEquals(0, store.size()); final List<String> keys = populateStore(5, 0, store, marshaller); final CountDownLatch stopLatch = new CountDownLatch(1); Future[] writeFutures = new Future[writerThreads]; for (int i = 0; i < writerThreads; i++) { writeFutures[i] = fork(stopOnException(new WriteTask(store, marshaller, keys, stopLatch), stopLatch)); } Future[] readFutures = new Future[readerThreads]; for (int i = 0; i < readerThreads; i++) { readFutures[i] = fork(stopOnException(new ReadTask(store, keys, false, stopLatch), stopLatch)); } stopLatch.await(2, SECONDS); stopLatch.countDown(); for (int i = 0; i < writerThreads; i++) { writeFutures[i].get(); } for (int i = 0; i < readerThreads; i++) { readFutures[i].get(); } }
MockAsyncCacheWriter cacheStore = TestingUtil.getFirstWriter(cache); CountDownLatch modApplyLatch = cacheStore.modApplyLatch; CountDownLatch lockedWaitLatch = cacheStore.lockedWaitLatch;
@Test(expectedExceptions = StoreUnavailableException.class) public void testCacheAvailability() { Cache<Object, Object> cache = createManagerAndGetCache(0); cache.put(1, 1); DummyInMemoryStore dims = TestingUtil.getFirstWriter(cache); dims.setAvailable(false); PersistenceManager pm = TestingUtil.extractComponent(cache, PersistenceManager.class); eventually(() -> !pm.isAvailable()); try { cache.put(1, 2); } catch (Exception e) { assertEquals(1, cache.get(1)); throw e; } TestingUtil.killCaches(); }
@Override public void call() { Cache<Integer, String> cache = cm.getCache(); AdvancedAsyncCacheWriter asyncStore = TestingUtil.getFirstWriter(cache); DummyInMemoryStore dummyStore = TestingUtil.extractField(asyncStore, "actual"); cache.put(1, "uno"); cache.put(2, "dos"); cache.put(3, "tres"); eventually(new Condition() { @Override public boolean isSatisfied() throws Exception { return dummyStore.size() == 3; } }); cache.clear(); eventually(new Condition() { @Override public boolean isSatisfied() throws Exception { return dummyStore.size() == 0; } }); } });
@Override public void call() { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.persistence() .addStore(DummyInMemoryStoreConfigurationBuilder.class) .storeName(this.getClass().getName()); cm.defineConfiguration("dummyStore", builder.build()); JCacheManager jCacheManager = createJCacheManager(cm, this); Cache<Integer, String> cache = jCacheManager.getCache("dummyStore"); // Load initial data in cache store int numEntries = loadInitialData(cm); DummyInMemoryStore dummyStore = TestingUtil.getFirstWriter(cm.getCache("dummyStore")); // Load all from cache store CompletionListenerFuture future = new CompletionListenerFuture(); Set<Integer> keys = Collections.singleton(1); cache.loadAll(keys, false, future); futureGet(future); // wait for key to be loaded assertTrue(future.isDone()); assertEquals(numEntries, dummyStore.stats().get("load").intValue()); // Load from memory assertEquals("v1", cache.get(1)); // Load again from cache store, overriding in-memory contents future = new CompletionListenerFuture(); cache.loadAll(keys, true, future); futureGet(future); // wait for key to be loaded assertTrue(future.isDone()); assertEquals(numEntries * 2, dummyStore.stats().get("load").intValue()); } });
public void testStoreReconnect() { PersistenceAvailabilityListener pal = new PersistenceAvailabilityListener(); Cache<Object, Object> cache = createManagerAndGetCache(0); cache.addListener(pal); assertEquals(0, pal.availableCount.get()); assertEquals(0, pal.unavailableCount.get()); cache.put(1, 1); PersistenceManager pm = TestingUtil.extractComponent(cache, PersistenceManager.class); assertTrue(pm.isAvailable()); DummyInMemoryStore dims = TestingUtil.getFirstWriter(cache); dims.setAvailable(false); eventually(() -> !pm.isAvailable()); eventuallyEquals(1, () -> pal.unavailableCount.get()); try { cache.put(1, 2); fail("Expected " + StoreUnavailableException.class.getSimpleName()); } catch (PersistenceException ignore) { } dims.setAvailable(true); eventually(pm::isAvailable); assertEquals(1, pal.availableCount.get()); cache.put(1, 3); assertEquals(3, cache.get(1)); }
private void putIntoStore(String key, String value) throws Exception { Cache<String, String> testCache = cacheManager.getCache(CACHE_NAME); CacheWriter<String, String> writer = TestingUtil.getFirstWriter(testCache); Object writerKey = key; Object writerValue = value; if (storage == StorageType.OFF_HEAP) { GlobalMarshaller gm = TestingUtil.extractGlobalMarshaller(testCache.getCacheManager()); writerKey = new WrappedByteArray(gm.objectToByteBuffer(key)); writerValue = new WrappedByteArray(gm.objectToByteBuffer(value)); } MarshalledEntry entry = new MarshalledEntryImpl(writerKey, writerValue, null, null); writer.write(entry); }
@Test public void testWritesFailSilentlyWhenConfigured() { Cache<Object, Object> cache = createManagerAndGetCache(true, 1); AdvancedAsyncCacheWriter asyncWriter = TestingUtil.getFirstWriter(cache); DummyInMemoryStore store = (DummyInMemoryStore) TestingUtil.extractField(AdvancedAsyncCacheWriter.class, asyncWriter, "actual"); assertTrue(store.isAvailable()); cache.put(1, 1); eventually(() -> store.load(1) != null); assertEquals(1, store.size()); store.setAvailable(false); assertFalse(store.isAvailable()); cache.put(1, 2); // Should fail on the store, but complete in-memory TestingUtil.sleepThread(1000); // Sleep to ensure async write is attempted store.setAvailable(true); MarshalledEntry entry = store.load(1); assertNotNull(entry); assertEquals(1, entry.getValue()); assertEquals(2, cache.get(1)); }
/** * @param async set to true means the cache store instance is placed under an DelegatingCacheWriter */ private void test(boolean async) { ConfigurationBuilder dcc = TestCacheManagerFactory.getDefaultCacheConfiguration(true); DummyInMemoryStoreConfigurationBuilder builder = dcc.persistence().addStore(DummyInMemoryStoreConfigurationBuilder.class); if (async) { builder.async().enable(); } EmbeddedCacheManager cacheManager = TestCacheManagerFactory.createCacheManager(dcc); Cache<Object,Object> cache = cacheManager.getCache(); try { CacheLoader firstLoader = TestingUtil.getFirstLoader(cache); CacheLoader undelegatedLoader = firstLoader instanceof DelegatingCacheLoader ? ((DelegatingCacheLoader) firstLoader).undelegate() : firstLoader; CacheWriter firstWriter = TestingUtil.getFirstWriter(cache); CacheWriter undelegatedWriter = firstWriter instanceof DelegatingCacheWriter ? ((DelegatingCacheWriter) firstWriter).undelegate() : firstWriter; assertEquals(1, ((DummyInMemoryStore)undelegatedLoader).getInitCount()); assertEquals(1, ((DummyInMemoryStore)undelegatedWriter).getInitCount()); } finally { TestingUtil.killCacheManagers(cacheManager); } }
PollingPersistenceManager pm = new PollingPersistenceManager(); TestingUtil.replaceComponent(cache, PersistenceManager.class, pm, true); AdvancedAsyncCacheWriter asyncWriter = TestingUtil.getFirstWriter(cache); DummyInMemoryStore store = (DummyInMemoryStore) TestingUtil.extractField(AdvancedAsyncCacheWriter.class, asyncWriter, "actual"); store.setAvailable(true);