Search in sources :

Example 36 with DataConversion

use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.

the class ExceptionEvictionTest method testDistributedOverflow.

void testDistributedOverflow(boolean onPrimary) {
    if (!cacheMode.isDistributed() || nodeCount < 3) {
        // Ignore the test if it isn't distributed and doesn't have at least 3 nodes
        return;
    }
    // Now we add 2 more nodes which means we have 5 nodes and 3 owners
    addClusterEnabledCacheManager(configurationBuilder);
    addClusterEnabledCacheManager(configurationBuilder);
    try {
        waitForClusterToForm();
        LocalizedCacheTopology lct = cache(0).getAdvancedCache().getDistributionManager().getCacheTopology();
        DataConversion dc = cache(0).getAdvancedCache().getKeyDataConversion();
        // use positive numbers as protobuf encodes negative numbers as 10-bytes long
        int minKey = 1;
        int nextKey = minKey;
        Address targetNode;
        Iterator<Address> owners = lct.getWriteOwners(dc.toStorage(nextKey)).iterator();
        if (onPrimary) {
            targetNode = owners.next();
        } else {
            // Skip first one
            owners.next();
            targetNode = owners.next();
        }
        cache(0).put(nextKey, nextKey);
        // This will fill up the cache with entries that all map to owners
        for (int i = 0; i < SIZE - 1; ++i) {
            nextKey = getNextIntWithOwners(nextKey, lct, dc, targetNode, null);
            cache(0).put(nextKey, nextKey);
        }
        // We should have interceptor count equal to number of owners times how much storage takes up
        assertInterceptorCount();
        for (Cache cache : caches()) {
            if (targetNode.equals(cache.getCacheManager().getAddress())) {
                assertEquals(10, cache.getAdvancedCache().getDataContainer().size());
                break;
            }
        }
        nextKey = getNextIntWithOwners(nextKey, lct, dc, targetNode, onPrimary);
        try {
            cache(0).put(nextKey, nextKey);
            fail("Should have thrown an exception!");
        } catch (Throwable t) {
            Exceptions.assertException(ContainerFullException.class, getMostNestedSuppressedThrowable(t));
        }
        // Now that it partially failed it should have rolled back all the results
        assertInterceptorCount();
    } finally {
        killMember(3);
        killMember(3);
    }
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) Address(org.infinispan.remoting.transport.Address) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) ContainerFullException(org.infinispan.interceptors.impl.ContainerFullException) Cache(org.infinispan.Cache)

Example 37 with DataConversion

use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.

the class LocalModePassivationTest method testEntrySetWithEvictedEntriesAndFlags.

public void testEntrySetWithEvictedEntriesAndFlags() {
    final int numKeys = 300;
    for (int i = 0; i < numKeys; i++) {
        cache.put(i, i);
    }
    AdvancedCache<Object, Object> flagCache = cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD);
    DataContainer<Object, Object> dc = flagCache.getDataContainer();
    assertFalse("Data Container should not have all keys", numKeys == dc.size());
    Set<Map.Entry<Object, Object>> entrySet = flagCache.entrySet();
    assertEquals(dc.size(), entrySet.size());
    DataConversion keyDataConversion = flagCache.getAdvancedCache().getKeyDataConversion();
    DataConversion valueDataConversion = flagCache.getAdvancedCache().getValueDataConversion();
    Map<WrappedByteArray, WrappedByteArray> map = new HashMap<>(entrySet.size());
    for (Map.Entry<Object, Object> entry : entrySet) {
        WrappedByteArray storedKey = (WrappedByteArray) keyDataConversion.toStorage(entry.getKey());
        WrappedByteArray storedValue = (WrappedByteArray) valueDataConversion.toStorage(entry.getValue());
        map.put(storedKey, storedValue);
    }
    for (InternalCacheEntry entry : dc) {
        assertEquals("Key/Value mismatch!", entry.getValue(), map.get(entry.getKey()));
    }
}
Also used : WrappedByteArray(org.infinispan.commons.marshall.WrappedByteArray) HashMap(java.util.HashMap) DataConversion(org.infinispan.encoding.DataConversion) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 38 with DataConversion

use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.

the class CacheNotifierImpl method convertValue.

private V convertValue(CacheEntryListenerInvocation listenerInvocation, V value) {
    if (value == null)
        return null;
    DataConversion valueDataConversion = listenerInvocation.getValueDataConversion();
    Wrapper wrp = valueDataConversion.getWrapper();
    Object unwrappedValue = valueDataConversion.getEncoder().fromStorage(wrp.unwrap(value));
    CacheEventFilter filter = listenerInvocation.getFilter();
    CacheEventConverter converter = listenerInvocation.getConverter();
    if (filter == null && converter == null) {
        if (listenerInvocation.useStorageFormat()) {
            return (V) unwrappedValue;
        }
        // If no filter is present, convert to the requested format directly
        return (V) valueDataConversion.fromStorage(value);
    }
    MediaType convertFormat = filter == null ? converter.format() : filter.format();
    if (listenerInvocation.useStorageFormat() || convertFormat == null) {
        // Filter will be run on the storage format, return the unwrapped key
        return (V) unwrappedValue;
    }
    // Filter has a specific format to run, convert to that format
    return (V) encoderRegistry.convert(unwrappedValue, valueDataConversion.getStorageMediaType(), convertFormat);
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) Wrapper(org.infinispan.commons.dataconversion.Wrapper) CacheEventConverter(org.infinispan.notifications.cachelistener.filter.CacheEventConverter) CacheEventFilter(org.infinispan.notifications.cachelistener.filter.CacheEventFilter) MediaType(org.infinispan.commons.dataconversion.MediaType)

Example 39 with DataConversion

use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.

the class PreloadManager method doPreload.

private CompletionStage<Void> doPreload() {
    Publisher<MarshallableEntry<Object, Object>> publisher = persistenceManager.preloadPublisher();
    long start = timeService.time();
    final long maxEntries = getMaxEntries();
    final long flags = getFlagsForStateInsertion();
    AdvancedCache<?, ?> tmpCache = this.cache.wired().withStorageMediaType();
    DataConversion keyDataConversion = tmpCache.getKeyDataConversion();
    DataConversion valueDataConversion = tmpCache.getValueDataConversion();
    Transaction outerTransaction = suspendIfNeeded();
    try {
        return Flowable.fromPublisher(publisher).take(maxEntries).concatMapSingle(me -> preloadEntry(flags, me, keyDataConversion, valueDataConversion)).count().toCompletionStage().thenAccept(insertAmount -> {
            this.fullyPreloaded = insertAmount < maxEntries;
            log.debugf("Preloaded %d keys in %s", insertAmount, Util.prettyPrintTime(timeService.timeDuration(start, MILLISECONDS)));
        });
    } finally {
        resumeIfNeeded(outerTransaction);
    }
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) Single(io.reactivex.rxjava3.core.Single) LogFactory(org.infinispan.util.logging.LogFactory) InvocationHelper(org.infinispan.cache.impl.InvocationHelper) CompletionStages(org.infinispan.util.concurrent.CompletionStages) EnumUtil(org.infinispan.commons.util.EnumUtil) Start(org.infinispan.factories.annotations.Start) FlagBitSets(org.infinispan.context.impl.FlagBitSets) Scopes(org.infinispan.factories.scopes.Scopes) InternalMetadataImpl(org.infinispan.metadata.impl.InternalMetadataImpl) StoreConfiguration(org.infinispan.configuration.cache.StoreConfiguration) InvocationContext(org.infinispan.context.InvocationContext) LocalTxInvocationContext(org.infinispan.context.impl.LocalTxInvocationContext) ComponentRef(org.infinispan.factories.impl.ComponentRef) AdvancedCache(org.infinispan.AdvancedCache) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) Log(org.infinispan.util.logging.Log) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) Scope(org.infinispan.factories.scopes.Scope) InvocationContextFactory(org.infinispan.context.InvocationContextFactory) Transaction(javax.transaction.Transaction) CommandsFactory(org.infinispan.commands.CommandsFactory) Flowable(io.reactivex.rxjava3.core.Flowable) MethodHandles(java.lang.invoke.MethodHandles) Publisher(org.reactivestreams.Publisher) Util(org.infinispan.commons.util.Util) Completable(io.reactivex.rxjava3.core.Completable) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Inject(org.infinispan.factories.annotations.Inject) CompletionStage(java.util.concurrent.CompletionStage) LocalTransaction(org.infinispan.transaction.impl.LocalTransaction) TransactionCoordinator(org.infinispan.transaction.impl.TransactionCoordinator) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) Configuration(org.infinispan.configuration.cache.Configuration) DataConversion(org.infinispan.encoding.DataConversion) TransactionManager(javax.transaction.TransactionManager) FakeJTATransaction(org.infinispan.transaction.impl.FakeJTATransaction) TransactionTable(org.infinispan.transaction.impl.TransactionTable) TimeService(org.infinispan.commons.time.TimeService) PersistenceException(org.infinispan.persistence.spi.PersistenceException) Transaction(javax.transaction.Transaction) LocalTransaction(org.infinispan.transaction.impl.LocalTransaction) FakeJTATransaction(org.infinispan.transaction.impl.FakeJTATransaction) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry)

Example 40 with DataConversion

use of org.infinispan.encoding.DataConversion in project infinispan by infinispan.

the class OffHeapMultiNodeTest method testRemoveSegments.

public void testRemoveSegments() {
    Cache<String, String> cache = cache(0);
    if (cache.getCacheConfiguration().clustering().cacheMode() == CacheMode.LOCAL) {
        // Local caches don't support removing segments
        return;
    }
    String key = "some-key";
    String value = "some-value";
    DataConversion keyDataConversion = cache.getAdvancedCache().getKeyDataConversion();
    DataConversion valueDataConversion = cache.getAdvancedCache().getValueDataConversion();
    Object storedKey = keyDataConversion.toStorage(key);
    Object storedValue = valueDataConversion.toStorage(value);
    Cache<String, String> primaryOwnerCache;
    int segmentWrittenTo;
    List<Cache<String, String>> caches = caches();
    if (caches.size() == 1) {
        primaryOwnerCache = cache;
        segmentWrittenTo = 0;
    } else {
        primaryOwnerCache = DistributionTestHelper.getFirstOwner(storedKey, caches());
        KeyPartitioner keyPartitioner = TestingUtil.extractComponent(primaryOwnerCache, KeyPartitioner.class);
        segmentWrittenTo = keyPartitioner.getSegment(storedKey);
    }
    InternalDataContainer container = TestingUtil.extractComponent(primaryOwnerCache, InternalDataContainer.class);
    assertEquals(0, container.size());
    container.put(storedKey, storedValue, new EmbeddedMetadata.Builder().build());
    assertEquals(1, container.size());
    container.removeSegments(IntSets.immutableSet(segmentWrittenTo));
    assertEquals(0, container.size());
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) InternalDataContainer(org.infinispan.container.impl.InternalDataContainer) Cache(org.infinispan.Cache)

Aggregations

DataConversion (org.infinispan.encoding.DataConversion)41 MVCCEntry (org.infinispan.container.entries.MVCCEntry)9 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)9 ArrayList (java.util.ArrayList)8 AdvancedCache (org.infinispan.AdvancedCache)8 Util (org.infinispan.commons.util.Util)8 EnumUtil (org.infinispan.commons.util.EnumUtil)7 InvocationContext (org.infinispan.context.InvocationContext)7 Completable (io.reactivex.rxjava3.core.Completable)6 Flowable (io.reactivex.rxjava3.core.Flowable)6 Set (java.util.Set)6 CompletionStage (java.util.concurrent.CompletionStage)6 Function (java.util.function.Function)6 Transaction (javax.transaction.Transaction)6 TransactionManager (javax.transaction.TransactionManager)6 MediaType (org.infinispan.commons.dataconversion.MediaType)6 TimeService (org.infinispan.commons.time.TimeService)6 Configuration (org.infinispan.configuration.cache.Configuration)6 CacheEntry (org.infinispan.container.entries.CacheEntry)6 FlagBitSets (org.infinispan.context.impl.FlagBitSets)6