Search in sources :

Example 16 with VersionStamp

use of org.apache.geode.internal.cache.versions.VersionStamp in project geode by apache.

the class AbstractRegionEntry method basicProcessVersionTag.

protected void basicProcessVersionTag(LocalRegion region, VersionTag tag, boolean isTombstoneFromGII, boolean deltaCheck, VersionSource dmId, InternalDistributedMember sender, boolean checkForConflict) {
    if (tag != null) {
        VersionStamp stamp = getVersionStamp();
        StringBuilder verbose = null;
        if (logger.isTraceEnabled(LogMarker.TOMBSTONE)) {
            VersionTag stampTag = stamp.asVersionTag();
            if (stampTag.hasValidVersion() && checkForConflict) {
                // only be verbose here if there's a possibility we might reject the operation
                verbose = new StringBuilder();
                verbose.append("processing tag for key ").append(getKey()).append(", stamp=").append(stamp.asVersionTag()).append(", tag=").append(tag).append(", checkForConflict=").append(checkForConflict);
            }
        }
        if (stamp == null) {
            throw new IllegalStateException("message contained a version tag but this region has no version storage");
        }
        boolean apply = true;
        try {
            if (checkForConflict) {
                apply = checkForConflict(region, stamp, tag, isTombstoneFromGII, deltaCheck, dmId, sender, verbose);
            }
        } catch (ConcurrentCacheModificationException e) {
            // applied there
            if (!tag.isGatewayTag() && stamp.getDistributedSystemId() == tag.getDistributedSystemId() && tag.getVersionTimeStamp() > stamp.getVersionTimeStamp()) {
                stamp.setVersionTimeStamp(tag.getVersionTimeStamp());
                tag.setTimeStampApplied(true);
                if (verbose != null) {
                    verbose.append("\nThough in conflict the tag timestamp was more recent and was recorded.");
                }
            }
            throw e;
        } finally {
            if (verbose != null) {
                logger.trace(LogMarker.TOMBSTONE, verbose);
            }
        }
        if (apply) {
            applyVersionTag(region, stamp, tag, sender);
        }
    }
}
Also used : VersionTag(org.apache.geode.internal.cache.versions.VersionTag) VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException)

Example 17 with VersionStamp

use of org.apache.geode.internal.cache.versions.VersionStamp in project geode by apache.

the class AbstractRegionEntry method appendFieldsToString.

protected StringBuilder appendFieldsToString(final StringBuilder sb) {
    // OFFHEAP _getValue ok: the current toString on ObjectChunk is safe to use without incing
    // refcount.
    sb.append("key=").append(getKey()).append("; rawValue=").append(_getValue());
    VersionStamp stamp = getVersionStamp();
    if (stamp != null) {
        sb.append("; version=").append(stamp.asVersionTag()).append(";member=").append(stamp.getMemberID());
    }
    return sb;
}
Also used : VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp)

Example 18 with VersionStamp

use of org.apache.geode.internal.cache.versions.VersionStamp in project geode by apache.

the class PartitionedRegion method getAllBucketEntries.

/**
   * Test Method: Get all entries for all copies of a bucket
   * 
   * This method will not work correctly if membership in the distributed system changes while the
   * result is being calculated.
   * 
   * @return a List of HashMaps, each map being a copy of the entries in a bucket
   */
public List<BucketDump> getAllBucketEntries(final int bucketId) throws ForceReattemptException {
    if (bucketId >= getTotalNumberOfBuckets()) {
        return Collections.emptyList();
    }
    ArrayList<BucketDump> ret = new ArrayList<BucketDump>();
    HashSet<InternalDistributedMember> collected = new HashSet<InternalDistributedMember>();
    for (; ; ) {
        // Collect all the candidates by re-examining the advisor...
        Set<InternalDistributedMember> owners = getRegionAdvisor().getBucketOwners(bucketId);
        // Remove ones we've already polled...
        owners.removeAll(collected);
        // Terminate if no more entries
        if (owners.isEmpty()) {
            break;
        }
        // Get first entry
        Iterator<InternalDistributedMember> ownersI = owners.iterator();
        InternalDistributedMember owner = (InternalDistributedMember) ownersI.next();
        // Remove it from our list
        collected.add(owner);
        // If it is ourself, answer directly
        if (owner.equals(getMyId())) {
            BucketRegion br = this.dataStore.handleRemoteGetEntries(bucketId);
            Map<Object, Object> m = new HashMap<Object, Object>() {

                // TODO: clean this up -- outer class is not serializable
                private static final long serialVersionUID = 0L;

                @Override
                public String toString() {
                    return "Bucket id = " + bucketId + " from local member = " + getDistributionManager().getDistributionManagerId() + ": " + super.toString();
                }
            };
            Map<Object, VersionTag> versions = new HashMap<Object, VersionTag>();
            for (Iterator<Map.Entry> it = br.entrySet().iterator(); it.hasNext(); ) {
                LocalRegion.NonTXEntry entry = (LocalRegion.NonTXEntry) it.next();
                RegionEntry re = entry.getRegionEntry();
                // OFFHEAP: incrc, deserialize, decrc
                Object value = re.getValue(br);
                VersionStamp versionStamp = re.getVersionStamp();
                VersionTag versionTag = versionStamp != null ? versionStamp.asVersionTag() : null;
                if (versionTag != null) {
                    versionTag.replaceNullIDs(br.getVersionMember());
                }
                if (Token.isRemoved(value)) {
                    continue;
                } else if (Token.isInvalid(value)) {
                    value = null;
                } else if (value instanceof CachedDeserializable) {
                    value = ((CachedDeserializable) value).getDeserializedForReading();
                }
                m.put(re.getKey(), value);
                versions.put(re.getKey(), versionTag);
            }
            RegionVersionVector rvv = br.getVersionVector();
            rvv = rvv != null ? rvv.getCloneForTransmission() : null;
            ret.add(new BucketDump(bucketId, owner, rvv, m, versions));
            continue;
        }
        // Send a message
        try {
            final FetchEntriesResponse r;
            r = FetchEntriesMessage.send(owner, this, bucketId);
            ret.add(r.waitForEntries());
        } catch (ForceReattemptException ignore) {
        // node has departed? Ignore.
        }
    }
    return ret;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) RegionVersionVector(org.apache.geode.internal.cache.versions.RegionVersionVector) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) HashSet(java.util.HashSet) VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp) FetchEntriesResponse(org.apache.geode.internal.cache.partitioned.FetchEntriesMessage.FetchEntriesResponse) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember)

Example 19 with VersionStamp

use of org.apache.geode.internal.cache.versions.VersionStamp in project geode by apache.

the class UpdateVersionJUnitTest method testUpdateVersionAfterUpdateOnPR.

@Test
public void testUpdateVersionAfterUpdateOnPR() {
    Cache cache = new CacheFactory().set(MCAST_PORT, "0").create();
    Region region = cache.createRegionFactory(RegionShortcut.PARTITION).create(regionName);
    try {
        region.create("key-1", "value-1");
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
        region.put("key-1", "value-2");
        Entry entry = region.getEntry("key-1");
        assertTrue(entry instanceof EntrySnapshot);
        RegionEntry regionEntry = ((EntrySnapshot) entry).getRegionEntry();
        VersionStamp stamp = regionEntry.getVersionStamp();
        // Create a duplicate entry version tag from stamp with newer time-stamp.
        VersionTag tag = VersionTag.create(stamp.getMemberID());
        int entryVersion = stamp.getEntryVersion();
        VersionSource member = stamp.getMemberID();
        int dsid = stamp.getDistributedSystemId();
        long time = System.currentTimeMillis();
        tag.setEntryVersion(entryVersion);
        tag.setDistributedSystemId(dsid);
        tag.setVersionTimeStamp(time);
        tag.setIsGatewayTag(true);
        assertTrue(region instanceof PartitionedRegion);
        EntryEventImpl event = createNewEvent((PartitionedRegion) region, tag, entry.getKey());
        ((PartitionedRegion) region).basicUpdateEntryVersion(event);
        // Verify the new stamp
        entry = region.getEntry("key-1");
        assertTrue(entry instanceof EntrySnapshot);
        regionEntry = ((EntrySnapshot) entry).getRegionEntry();
        stamp = regionEntry.getVersionStamp();
        assertEquals("Time stamp did NOT get updated by UPDATE_VERSION operation on LocalRegion", time, stamp.getVersionTimeStamp());
        assertEquals(++entryVersion, stamp.getEntryVersion());
        assertEquals(member, stamp.getMemberID());
        assertEquals(dsid, stamp.getDistributedSystemId());
    } finally {
        region.destroyRegion();
        cache.close();
    }
}
Also used : VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp) NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) Entry(org.apache.geode.cache.Region.Entry) VersionSource(org.apache.geode.internal.cache.versions.VersionSource) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 20 with VersionStamp

use of org.apache.geode.internal.cache.versions.VersionStamp in project geode by apache.

the class UpdateVersionJUnitTest method testUpdateVersionAfterUpdate.

@Test
public void testUpdateVersionAfterUpdate() {
    Cache cache = new CacheFactory().set(MCAST_PORT, "0").create();
    Region region = cache.createRegionFactory(RegionShortcut.REPLICATE).create(regionName);
    try {
        region.create("key-1", "value-1");
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
        region.put("key-1", "value-2");
        Entry entry = region.getEntry("key-1");
        assertTrue(entry instanceof NonTXEntry);
        RegionEntry regionEntry = ((NonTXEntry) entry).getRegionEntry();
        VersionStamp stamp = regionEntry.getVersionStamp();
        // Create a duplicate entry version tag from stamp with newer time-stamp.
        VersionTag tag = VersionTag.create(stamp.getMemberID());
        int entryVersion = stamp.getEntryVersion();
        VersionSource member = stamp.getMemberID();
        int dsid = stamp.getDistributedSystemId();
        // Just in case if clock hasn't ticked.
        long time = System.currentTimeMillis() + 1;
        tag.setEntryVersion(entryVersion);
        tag.setDistributedSystemId(dsid);
        tag.setVersionTimeStamp(time);
        tag.setIsGatewayTag(true);
        assertTrue(region instanceof LocalRegion);
        EntryEventImpl event = createNewEvent((LocalRegion) region, tag, entry.getKey());
        ((LocalRegion) region).basicUpdateEntryVersion(event);
        // Verify the new stamp
        entry = region.getEntry("key-1");
        assertTrue(entry instanceof NonTXEntry);
        regionEntry = ((NonTXEntry) entry).getRegionEntry();
        stamp = regionEntry.getVersionStamp();
        assertEquals("Time stamp did NOT get updated by UPDATE_VERSION operation on LocalRegion", time, stamp.getVersionTimeStamp());
        assertEquals(++entryVersion, stamp.getEntryVersion());
        assertEquals(member, stamp.getMemberID());
        assertEquals(dsid, stamp.getDistributedSystemId());
    } finally {
        region.destroyRegion();
        cache.close();
    }
}
Also used : NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp) NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) Entry(org.apache.geode.cache.Region.Entry) VersionSource(org.apache.geode.internal.cache.versions.VersionSource) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

VersionStamp (org.apache.geode.internal.cache.versions.VersionStamp)22 VersionTag (org.apache.geode.internal.cache.versions.VersionTag)19 VersionSource (org.apache.geode.internal.cache.versions.VersionSource)13 NonTXEntry (org.apache.geode.internal.cache.LocalRegion.NonTXEntry)11 Test (org.junit.Test)11 Entry (org.apache.geode.cache.Region.Entry)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)6 Cache (org.apache.geode.cache.Cache)4 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)4 Region (org.apache.geode.cache.Region)4 ConcurrentCacheModificationException (org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException)4 Host (org.apache.geode.test.dunit.Host)4 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)4 VM (org.apache.geode.test.dunit.VM)4 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)4 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 PRLocallyDestroyedException (org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException)3 ArrayList (java.util.ArrayList)2 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)2 RegionVersionVector (org.apache.geode.internal.cache.versions.RegionVersionVector)2