@SuppressWarnings("unchecked") @Test public void testXAStoreProviderStatefulSerializer() { OffHeapStore.Provider underlyingStoreProvider = new OffHeapStore.Provider(); when(configuration.getValueSerializer()).thenReturn(valueSerializer); underlyingStoreProvider.start(serviceProvider);
@Override public void releaseStore(Store<?, ?> resource) { if (!createdStores.contains(resource)) { throw new IllegalArgumentException("Given store is not managed by this provider : " + resource); } OffHeapStore<?, ?> offHeapStore = (OffHeapStore<?, ?>) resource; close(offHeapStore); StatisticsManager.nodeFor(offHeapStore).clean(); tierOperationStatistics.remove(offHeapStore); }
@Override protected OffHeapStore<String, byte[]> createAndInitStore(TimeSource timeSource, ExpiryPolicy<? super String, ? super byte[]> expiry, EvictionAdvisor<? super String, ? super byte[]> evictionAdvisor) { try { SerializationProvider serializationProvider = new DefaultSerializationProvider(null); serializationProvider.start(providerContaining()); ClassLoader classLoader = getClass().getClassLoader(); Serializer<String> keySerializer = serializationProvider.createValueSerializer(String.class, classLoader); Serializer<byte[]> valueSerializer = serializationProvider.createValueSerializer(byte[].class, classLoader); StoreConfigurationImpl<String, byte[]> storeConfiguration = new StoreConfigurationImpl<>(String.class, byte[].class, evictionAdvisor, getClass().getClassLoader(), expiry, null, 0, keySerializer, valueSerializer); OffHeapStore<String, byte[]> offHeapStore = new OffHeapStore<>(storeConfiguration, timeSource, new TestStoreEventDispatcher<>(), MemoryUnit.MB .toBytes(1)); OffHeapStore.Provider.init(offHeapStore); return offHeapStore; } catch (UnsupportedTypeException e) { throw new AssertionError(e); } }
private <K, V> OffHeapStore<K, V> createStoreInternal(Configuration<K, V> storeConfig, StoreEventDispatcher<K, V> eventDispatcher, ServiceConfiguration<?>... serviceConfigs) { if (serviceProvider == null) { throw new NullPointerException("ServiceProvider is null in OffHeapStore.Provider."); } TimeSource timeSource = serviceProvider.getService(TimeSourceService.class).getTimeSource(); SizedResourcePool offHeapPool = storeConfig.getResourcePools().getPoolForResource(getResourceType()); if (!(offHeapPool.getUnit() instanceof MemoryUnit)) { throw new IllegalArgumentException("OffHeapStore only supports resources with memory unit"); } MemoryUnit unit = (MemoryUnit)offHeapPool.getUnit(); OffHeapStore<K, V> offHeapStore = new OffHeapStore<>(storeConfig, timeSource, eventDispatcher, unit.toBytes(offHeapPool .getSize())); createdStores.add(offHeapStore); return offHeapStore; }
@Override public void initStore(Store<?, ?> resource) { if (!createdStores.contains(resource)) { throw new IllegalArgumentException("Given store is not managed by this provider : " + resource); } OffHeapStore<?, ?> offHeapStore = (OffHeapStore<?, ?>) resource; Serializer<?> keySerializer = offHeapStore.keySerializer; if (keySerializer instanceof StatefulSerializer) { ((StatefulSerializer)keySerializer).init(new TransientStateRepository()); } Serializer<?> valueSerializer = offHeapStore.valueSerializer; if (valueSerializer instanceof StatefulSerializer) { ((StatefulSerializer)valueSerializer).init(new TransientStateRepository()); } init(offHeapStore); }
@Override public OffHeapStore.Provider create(ServiceCreationConfiguration<OffHeapStore.Provider> configuration) { return new OffHeapStore.Provider(); }
@Override public void initAuthoritativeTier(AuthoritativeTier<?, ?> resource) { initStore(resource); }
@Override public void releaseAuthoritativeTier(AuthoritativeTier<?, ?> resource) { releaseStore(resource); }
@Override public void releaseStore(Store<?, ?> resource) { if (!createdStores.contains(resource)) { throw new IllegalArgumentException("Given store is not managed by this provider : " + resource); } OffHeapStore<?, ?> offHeapStore = (OffHeapStore<?, ?>) resource; close(offHeapStore); StatisticsManager.nodeFor(offHeapStore).clean(); tierOperationStatistics.remove(offHeapStore); }
@Override public void initCachingTier(LowerCachingTier<?, ?> resource) { if (!createdStores.contains(resource)) { throw new IllegalArgumentException("Given caching tier is not managed by this provider : " + resource); } init((OffHeapStore<?, ?>) resource); } }
@Override public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Configuration<K, V> storeConfig, ServiceConfiguration<?>... serviceConfigs) { OffHeapStore<K, V> authoritativeTier = createStoreInternal(storeConfig, new ThreadLocalStoreEventDispatcher<>(storeConfig .getDispatcherConcurrency()), serviceConfigs); tierOperationStatistics.put(authoritativeTier, new OperationStatistic<?>[] { createTranslatedStatistic(authoritativeTier, "get", TierOperationOutcomes.GET_AND_FAULT_TRANSLATION, "getAndFault"), createTranslatedStatistic(authoritativeTier, "eviction", TierOperationOutcomes.EVICTION_TRANSLATION, "eviction") }); return authoritativeTier; }
@Override public <K, V> LowerCachingTier<K, V> createCachingTier(Configuration<K, V> storeConfig, ServiceConfiguration<?>... serviceConfigs) { OffHeapStore<K, V> lowerCachingTier = createStoreInternal(storeConfig, NullStoreEventDispatcher.nullStoreEventDispatcher(), serviceConfigs); tierOperationStatistics.put(lowerCachingTier, new OperationStatistic<?>[] { createTranslatedStatistic(lowerCachingTier, "get", TierOperationOutcomes.GET_AND_REMOVE_TRANSLATION, "getAndRemove"), createTranslatedStatistic(lowerCachingTier, "eviction", TierOperationOutcomes.EVICTION_TRANSLATION, "eviction") }); return lowerCachingTier; }
@Override @SuppressWarnings("unchecked") public void releaseCachingTier(LowerCachingTier<?, ?> resource) { if (!createdStores.contains(resource)) { throw new IllegalArgumentException("Given caching tier is not managed by this provider : " + resource); } flushToLowerTier((OffHeapStore<Object, ?>) resource); releaseStore((Store<?, ?>) resource); }
@Override public <K, V> OffHeapStore<K, V> createStore(Configuration<K, V> storeConfig, ServiceConfiguration<?>... serviceConfigs) { OffHeapStore<K, V> store = createStoreInternal(storeConfig, new ThreadLocalStoreEventDispatcher<>(storeConfig.getDispatcherConcurrency()), serviceConfigs); tierOperationStatistics.put(store, new OperationStatistic<?>[] { createTranslatedStatistic(store, "get", TierOperationOutcomes.GET_TRANSLATION, "get"), createTranslatedStatistic(store, "eviction", TierOperationOutcomes.EVICTION_TRANSLATION, "eviction") }); return store; }
.with(provider) .with(new ClusteredStore.Provider()) .with(new OnHeapStore.Provider()) .with(new OffHeapStore.Provider()) .with(new OffHeapDiskStore.Provider())
@Test public void testSupportsMultipleAuthoritativeTierProviders() throws Exception { ServiceLocator.DependencySet dependencySet = dependencySet(); OnHeapStore.Provider cachingTierProvider = new OnHeapStore.Provider(); OffHeapStore.Provider authoritativeTierProvider = new OffHeapStore.Provider(); OffHeapDiskStore.Provider diskStoreProvider = new OffHeapDiskStore.Provider(); dependencySet.with(cachingTierProvider); dependencySet.with(authoritativeTierProvider); dependencySet.with(diskStoreProvider); dependencySet.with(mock(DiskResourceService.class)); ServiceLocator serviceLocator = dependencySet.build(); serviceLocator.startAllServices(); assertThat(serviceLocator.getServicesOfType(CachingTier.Provider.class), IsCollectionContaining.<CachingTier.Provider>hasItem(IsSame.<CachingTier.Provider>sameInstance(cachingTierProvider))); assertThat(serviceLocator.getServicesOfType(AuthoritativeTier.Provider.class), IsCollectionContaining.<AuthoritativeTier.Provider>hasItem(IsSame.<AuthoritativeTier.Provider>sameInstance(authoritativeTierProvider))); assertThat(serviceLocator.getServicesOfType(OffHeapDiskStore.Provider.class), IsCollectionContaining.<OffHeapDiskStore.Provider>hasItem(IsSame.<OffHeapDiskStore.Provider>sameInstance(diskStoreProvider))); } }
@Test public void testRank() { ClusteredWriteBehindStore.Provider provider = new ClusteredWriteBehindStore.Provider(); ServiceLocator serviceLocator = dependencySet() .with(new TieredStore.Provider()) .with(new OnHeapStore.Provider()) .with(new OffHeapStore.Provider()) .with(mock(DiskResourceService.class)) .with(new OffHeapDiskStore.Provider()) .with(mock(ClusteringService.class)).build(); provider.start(serviceLocator); assertThat(provider.rank(new HashSet<>(Collections.singletonList(ClusteredResourceType.Types.DEDICATED)), Arrays.asList(cacheLoaderWriterConfiguration, writeBehindConfiguration)), is(3)); assertThat(provider.rank(new HashSet<>(Collections.singletonList(ClusteredResourceType.Types.DEDICATED)), Collections.singletonList(cacheLoaderWriterConfiguration)), is(0)); assertThat(provider.rank(new HashSet<>(Collections.singletonList(new ClusteredStoreProviderTest.UnmatchedResourceType())), Arrays.asList(cacheLoaderWriterConfiguration, writeBehindConfiguration)), is(0)); }
@Override protected OffHeapStore<String, String> createAndInitStore(TimeSource timeSource, ExpiryPolicy<? super String, ? super String> expiry) { try { SerializationProvider serializationProvider = new DefaultSerializationProvider(null); serializationProvider.start(providerContaining()); ClassLoader classLoader = getClass().getClassLoader(); Serializer<String> keySerializer = serializationProvider.createKeySerializer(String.class, classLoader); Serializer<String> valueSerializer = serializationProvider.createValueSerializer(String.class, classLoader); StoreConfigurationImpl<String, String> storeConfiguration = new StoreConfigurationImpl<>(String.class, String.class, null, classLoader, expiry, null, 0, keySerializer, valueSerializer); OffHeapStore<String, String> offHeapStore = new OffHeapStore<>(storeConfiguration, timeSource, new TestStoreEventDispatcher<>(), MemoryUnit.MB .toBytes(1)); OffHeapStore.Provider.init(offHeapStore); return offHeapStore; } catch (UnsupportedTypeException e) { throw new AssertionError(e); } }
@Test public void testRank() { ClusteredLoaderWriterStore.Provider provider = new ClusteredLoaderWriterStore.Provider(); ServiceLocator serviceLocator = dependencySet() .with(new TieredStore.Provider()) .with(new OnHeapStore.Provider()) .with(new OffHeapStore.Provider()) .with(mock(DiskResourceService.class)) .with(new OffHeapDiskStore.Provider()) .with(mock(ClusteringService.class)).build(); provider.start(serviceLocator); assertThat(provider.rank(new HashSet<>(Collections.singletonList(ClusteredResourceType.Types.DEDICATED)), Collections.singletonList(cacheLoaderWriterConfiguration)), is(2)); assertThat(provider.rank(new HashSet<>(Collections.singletonList(ClusteredResourceType.Types.DEDICATED)), Collections.emptyList()), is(0)); assertThat(provider.rank(new HashSet<>(Collections.singletonList(new ClusteredStoreProviderTest.UnmatchedResourceType())), Collections.singletonList(cacheLoaderWriterConfiguration)), is(0)); }
@Override public void initStore(Store<?, ?> resource) { if (!createdStores.contains(resource)) { throw new IllegalArgumentException("Given store is not managed by this provider : " + resource); } OffHeapStore<?, ?> offHeapStore = (OffHeapStore<?, ?>) resource; Serializer<?> keySerializer = offHeapStore.keySerializer; if (keySerializer instanceof StatefulSerializer) { ((StatefulSerializer)keySerializer).init(new TransientStateRepository()); } Serializer<?> valueSerializer = offHeapStore.valueSerializer; if (valueSerializer instanceof StatefulSerializer) { ((StatefulSerializer)valueSerializer).init(new TransientStateRepository()); } init(offHeapStore); }