Search in sources :

Example 1 with InequalVersionComparisonResult

use of org.infinispan.container.versioning.InequalVersionComparisonResult in project infinispan by infinispan.

the class ScatteredDistributionInterceptor method updateEntryIfNoChange.

private boolean updateEntryIfNoChange(RepeatableReadEntry entry) {
    // We cannot delegate the dataContainer.compute() to entry.commit() as we need to reliably
    // retrieve previous value and metadata, but the entry API does not provide these.
    dataContainer.compute(entry.getKey(), (key, oldEntry, factory) -> {
        // newMetadata is null in case of local-mode write on non-primary owners
        Metadata newMetadata = entry.getMetadata();
        if (oldEntry == null) {
            if (entry.getOldValue() != null) {
                if (log.isTraceEnabled()) {
                    log.trace("Non-null value in context, not committing");
                }
                throw new ConcurrentChangeException();
            }
            if (entry.getValue() == null && newMetadata == null) {
                if (log.isTraceEnabled()) {
                    log.trace("No previous record and this is a removal, not committing anything.");
                }
                return null;
            } else {
                if (log.isTraceEnabled()) {
                    log.trace("Committing new entry " + entry);
                }
                entry.setCommitted();
                return factory.create(entry);
            }
        }
        Metadata oldMetadata = oldEntry.getMetadata();
        EntryVersion oldVersion = oldMetadata == null ? null : oldMetadata.version();
        Metadata seenMetadata = entry.getOldMetadata();
        EntryVersion seenVersion = seenMetadata == null ? null : seenMetadata.version();
        if (oldVersion == null) {
            if (seenVersion != null) {
                if (log.isTraceEnabled()) {
                    log.tracef("Current version is null but seen version is %s, throwing", seenVersion);
                }
                throw new ConcurrentChangeException();
            }
        } else if (seenVersion == null) {
            if (oldEntry.canExpire() && oldEntry.isExpired(timeService.wallClockTime())) {
                if (log.isTraceEnabled()) {
                    log.trace("Current entry is expired and therefore we haven't seen it");
                }
            } else {
                if (log.isTraceEnabled()) {
                    log.tracef("Current version is %s but seen version is null, throwing", oldVersion);
                }
                throw new ConcurrentChangeException();
            }
        } else if (seenVersion.compareTo(oldVersion) != InequalVersionComparisonResult.EQUAL) {
            if (log.isTraceEnabled()) {
                log.tracef("Current version is %s but seen version is %s, throwing", oldVersion, seenVersion);
            }
            throw new ConcurrentChangeException();
        }
        InequalVersionComparisonResult comparisonResult;
        if (oldVersion == null || newMetadata == null || newMetadata.version() == null || (comparisonResult = oldMetadata.version().compareTo(newMetadata.version())) == InequalVersionComparisonResult.BEFORE || (oldMetadata instanceof RemoteMetadata && comparisonResult == InequalVersionComparisonResult.EQUAL)) {
            if (log.isTraceEnabled()) {
                log.tracef("Committing entry %s, replaced %s", entry, oldEntry);
            }
            entry.setCommitted();
            if (entry.getValue() != null || newMetadata != null) {
                return factory.create(entry);
            } else {
                return null;
            }
        } else {
            if (log.isTraceEnabled()) {
                log.tracef("Not committing %s, current entry is %s", entry, oldEntry);
            }
            return oldEntry;
        }
    });
    return entry.isCommitted();
}
Also used : RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) EntryVersion(org.infinispan.container.versioning.EntryVersion) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) Metadata(org.infinispan.metadata.Metadata) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult)

Example 2 with InequalVersionComparisonResult

use of org.infinispan.container.versioning.InequalVersionComparisonResult in project infinispan by infinispan.

the class ScatteredDistributionInterceptor method updateEntryIfNewer.

private boolean updateEntryIfNewer(RepeatableReadEntry entry) {
    // We cannot delegate the dataContainer.compute() to entry.commit() as we need to reliably
    // retrieve previous value and metadata, but the entry API does not provide these.
    dataContainer.compute(entry.getKey(), (key, oldEntry, factory) -> {
        // newMetadata is null in case of local-mode write
        Metadata newMetadata = entry.getMetadata();
        if (oldEntry == null) {
            if (entry.getValue() == null && newMetadata == null) {
                if (log.isTraceEnabled()) {
                    log.trace("No previous record and this is a removal, not committing anything.");
                }
                return null;
            } else {
                if (log.isTraceEnabled()) {
                    log.trace("Committing new entry " + entry);
                }
                entry.setCommitted();
                return factory.create(entry);
            }
        }
        Metadata oldMetadata = oldEntry.getMetadata();
        InequalVersionComparisonResult comparisonResult;
        if (oldMetadata == null || oldMetadata.version() == null || newMetadata == null || newMetadata.version() == null || (comparisonResult = oldMetadata.version().compareTo(newMetadata.version())) == InequalVersionComparisonResult.BEFORE || (oldMetadata instanceof RemoteMetadata && comparisonResult == InequalVersionComparisonResult.EQUAL)) {
            if (log.isTraceEnabled()) {
                log.tracef("Committing entry %s, replaced %s", entry, oldEntry);
            }
            entry.setCommitted();
            if (entry.getValue() != null || newMetadata != null) {
                return factory.create(entry);
            } else {
                return null;
            }
        } else {
            if (log.isTraceEnabled()) {
                log.tracef("Not committing %s, current entry is %s", entry, oldEntry);
            }
            return oldEntry;
        }
    });
    return entry.isCommitted();
}
Also used : RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) Metadata(org.infinispan.metadata.Metadata) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult)

Example 3 with InequalVersionComparisonResult

use of org.infinispan.container.versioning.InequalVersionComparisonResult in project infinispan by infinispan.

the class IracRestartWithoutGlobalStateTest method assertVersions.

private <K> void assertVersions(Map<K, IracEntryVersion> v1, Map<K, IracEntryVersion> v2, InequalVersionComparisonResult expected) {
    assertEquals(v1.size(), v2.size());
    Iterator<K> iterator = Stream.concat(v1.keySet().stream(), v2.keySet().stream()).distinct().iterator();
    while (iterator.hasNext()) {
        K key = iterator.next();
        IracEntryVersion version1 = v1.get(key);
        IracEntryVersion version2 = v2.get(key);
        assertNotNull(format("'%s' version is null for Map 1", key), version1);
        assertNotNull(format("'%s' version is null for Map 2", key), version2);
        InequalVersionComparisonResult result = version1.compareTo(version2);
        assertEquals(format("'%s' version mismatch: %s and %s", key, version1, version2), expected, result);
    }
}
Also used : IracEntryVersion(org.infinispan.container.versioning.irac.IracEntryVersion) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult)

Example 4 with InequalVersionComparisonResult

use of org.infinispan.container.versioning.InequalVersionComparisonResult in project infinispan by infinispan.

the class IracRestartWithGlobalStateTest method assertVersions.

private static <K> void assertVersions(Map<K, IracEntryVersion> v1, Map<K, IracEntryVersion> v2, InequalVersionComparisonResult expected) {
    assertEquals(v1.size(), v2.size());
    Iterator<K> iterator = Stream.concat(v1.keySet().stream(), v2.keySet().stream()).distinct().iterator();
    while (iterator.hasNext()) {
        K key = iterator.next();
        IracEntryVersion version1 = v1.get(key);
        IracEntryVersion version2 = v2.get(key);
        assertNotNull(format("'%s' version is null for Map 1", key), version1);
        assertNotNull(format("'%s' version is null for Map 2", key), version2);
        InequalVersionComparisonResult result = version1.compareTo(version2);
        assertEquals(format("'%s' version mismatch: %s and %s", key, version1, version2), expected, result);
    }
}
Also used : IracEntryVersion(org.infinispan.container.versioning.irac.IracEntryVersion) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult)

Example 5 with InequalVersionComparisonResult

use of org.infinispan.container.versioning.InequalVersionComparisonResult in project infinispan by infinispan.

the class InvalidateVersionsCommand method invokeAsync.

@Override
public CompletionStage<?> invokeAsync(ComponentRegistry componentRegistry) {
    StateTransferLock stateTransferLock = componentRegistry.getStateTransferLock();
    if (stateTransferLock != null) {
        stateTransferLock.acquireSharedTopologyLock();
    }
    try {
        DistributionManager distributionManager = componentRegistry.getDistributionManager();
        if (topologyId >= 0 && distributionManager != null) {
            int currentTopologyId = distributionManager.getCacheTopology().getTopologyId();
            if (topologyId > currentTopologyId) {
                if (log.isTraceEnabled()) {
                    log.tracef("Delaying command %s, current topology id %d", this, currentTopologyId);
                }
                return stateTransferLock.topologyFuture(topologyId).thenCompose(nil -> invokeAsync(componentRegistry));
            } else if (topologyId < currentTopologyId) {
                log.ignoringInvalidateVersionsFromOldTopology(this.topologyId, currentTopologyId);
                if (log.isTraceEnabled()) {
                    log.tracef("Ignored command is %s", this);
                }
                return CompletableFutures.completedNull();
            }
        }
        for (int i = 0; i < keys.length; ++i) {
            Object key = keys[i];
            if (key == null)
                break;
            SimpleClusteredVersion version = new SimpleClusteredVersion(topologyIds[i], versions[i]);
            BiasManager biasManager = componentRegistry.getBiasManager().running();
            if (biasManager != null) {
                biasManager.revokeLocalBias(key);
            }
            DataContainer dataContainer = componentRegistry.getInternalDataContainer().running();
            dataContainer.compute(key, (k, oldEntry, factory) -> {
                if (oldEntry == null) {
                    return null;
                }
                SimpleClusteredVersion localVersion = (SimpleClusteredVersion) oldEntry.getMetadata().version();
                InequalVersionComparisonResult result = localVersion.compareTo(version);
                if (result == InequalVersionComparisonResult.BEFORE || (removed && result == InequalVersionComparisonResult.EQUAL)) {
                    return null;
                } else {
                    return oldEntry;
                }
            });
        }
    } finally {
        if (stateTransferLock != null) {
            stateTransferLock.releaseSharedTopologyLock();
        }
    }
    OrderedUpdatesManager orderedUpdatesManager = componentRegistry.getOrderedUpdatesManager().running();
    return orderedUpdatesManager.invalidate(keys).thenApply(nil -> null).toCompletableFuture();
}
Also used : DataContainer(org.infinispan.container.DataContainer) LogFactory(org.infinispan.util.logging.LogFactory) ObjectOutput(java.io.ObjectOutput) OrderedUpdatesManager(org.infinispan.persistence.manager.OrderedUpdatesManager) IOException(java.io.IOException) ComponentRegistry(org.infinispan.factories.ComponentRegistry) SimpleClusteredVersion(org.infinispan.container.versioning.SimpleClusteredVersion) BaseRpcCommand(org.infinispan.commands.remote.BaseRpcCommand) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult) StateTransferLock(org.infinispan.statetransfer.StateTransferLock) CompletionStage(java.util.concurrent.CompletionStage) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Log(org.infinispan.util.logging.Log) BiasManager(org.infinispan.scattered.BiasManager) ObjectInput(java.io.ObjectInput) VisitableCommand(org.infinispan.commands.VisitableCommand) DistributionManager(org.infinispan.distribution.DistributionManager) ByteString(org.infinispan.util.ByteString) SimpleClusteredVersion(org.infinispan.container.versioning.SimpleClusteredVersion) DataContainer(org.infinispan.container.DataContainer) StateTransferLock(org.infinispan.statetransfer.StateTransferLock) BiasManager(org.infinispan.scattered.BiasManager) OrderedUpdatesManager(org.infinispan.persistence.manager.OrderedUpdatesManager) DistributionManager(org.infinispan.distribution.DistributionManager) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult)

Aggregations

InequalVersionComparisonResult (org.infinispan.container.versioning.InequalVersionComparisonResult)6 Metadata (org.infinispan.metadata.Metadata)3 RemoteMetadata (org.infinispan.container.entries.RemoteMetadata)2 EntryVersion (org.infinispan.container.versioning.EntryVersion)2 IracEntryVersion (org.infinispan.container.versioning.irac.IracEntryVersion)2 EmbeddedMetadata (org.infinispan.metadata.EmbeddedMetadata)2 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 VisitableCommand (org.infinispan.commands.VisitableCommand)1 BaseRpcCommand (org.infinispan.commands.remote.BaseRpcCommand)1 ByRef (org.infinispan.commons.util.ByRef)1 DataContainer (org.infinispan.container.DataContainer)1 SimpleClusteredVersion (org.infinispan.container.versioning.SimpleClusteredVersion)1 DistributionManager (org.infinispan.distribution.DistributionManager)1 ComponentRegistry (org.infinispan.factories.ComponentRegistry)1 OrderedUpdatesManager (org.infinispan.persistence.manager.OrderedUpdatesManager)1 BiasManager (org.infinispan.scattered.BiasManager)1