Search in sources :

Example 11 with VersionSource

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

the class HAGIIDUnitTest method tombstonegc.

/** queue a tombstone GC message for the client. See bug #46832 */
public static void tombstonegc() throws Exception {
    LocalRegion r = (LocalRegion) cache.getRegion("/" + REGION_NAME);
    assertNotNull(r);
    DistributedMember id = r.getCache().getDistributedSystem().getDistributedMember();
    RegionEventImpl regionEvent = new RegionEventImpl(r, Operation.REGION_DESTROY, null, true, id);
    FilterInfo clientRouting = r.getFilterProfile().getLocalFilterRouting(regionEvent);
    assertTrue(clientRouting.getInterestedClients().size() > 0);
    regionEvent.setLocalFilterInfo(clientRouting);
    Map<VersionSource, Long> map = Collections.emptyMap();
    ClientTombstoneMessage message = ClientTombstoneMessage.gc(r, map, new EventID(r.getCache().getDistributedSystem()));
    CacheClientNotifier.notifyClients(regionEvent, message);
}
Also used : VersionSource(org.apache.geode.internal.cache.versions.VersionSource) ClientTombstoneMessage(org.apache.geode.internal.cache.tier.sockets.ClientTombstoneMessage) RegionEventImpl(org.apache.geode.internal.cache.RegionEventImpl) DistributedMember(org.apache.geode.distributed.DistributedMember) EventID(org.apache.geode.internal.cache.EventID) LocalRegion(org.apache.geode.internal.cache.LocalRegion) FilterInfo(org.apache.geode.internal.cache.FilterRoutingInfo.FilterInfo)

Example 12 with VersionSource

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

the class UpdateVersionDUnitTest method testUpdateVersionAfterCreateWithParallelSender.

@Test
public void testUpdateVersionAfterCreateWithParallelSender() {
    Host host = Host.getHost(0);
    // server1 site1
    VM vm0 = host.getVM(0);
    // server2 site1
    VM vm1 = host.getVM(1);
    // server1 site2
    VM vm2 = host.getVM(2);
    // server2 site2
    VM vm3 = host.getVM(3);
    // Site 1
    Integer lnPort = (Integer) vm0.invoke(() -> UpdateVersionDUnitTest.createFirstLocatorWithDSId(1));
    final String key = "key-1";
    vm0.invoke(() -> UpdateVersionDUnitTest.createCache(lnPort));
    vm0.invoke(() -> UpdateVersionDUnitTest.createSender("ln1", 2, true, 10, 1, false, false, null, true));
    vm0.invoke(() -> UpdateVersionDUnitTest.createPartitionedRegion(regionName, "ln1", 1, 1));
    vm0.invoke(() -> UpdateVersionDUnitTest.startSender("ln1"));
    vm0.invoke(() -> UpdateVersionDUnitTest.waitForSenderRunningState("ln1"));
    // Site 2
    Integer nyPort = (Integer) vm2.invoke(() -> UpdateVersionDUnitTest.createFirstRemoteLocator(2, lnPort));
    Integer nyRecPort = (Integer) vm2.invoke(() -> UpdateVersionDUnitTest.createReceiver(nyPort));
    vm2.invoke(() -> UpdateVersionDUnitTest.createPartitionedRegion(regionName, "", 1, 1));
    vm3.invoke(() -> UpdateVersionDUnitTest.createCache(nyPort));
    vm3.invoke(() -> UpdateVersionDUnitTest.createPartitionedRegion(regionName, "", 1, 1));
    final VersionTag tag = (VersionTag) vm0.invoke(new SerializableCallable("Put a single entry and get its version") {

        @Override
        public Object call() throws CacheException {
            Cache cache = CacheFactory.getAnyInstance();
            Region region = cache.getRegion(regionName);
            assertTrue(region instanceof PartitionedRegion);
            region.put(key, "value-1");
            region.put(key, "value-2");
            Entry entry = region.getEntry(key);
            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.
            VersionSource memberId = (VersionSource) cache.getDistributedSystem().getDistributedMember();
            VersionTag tag = VersionTag.create(memberId);
            int entryVersion = stamp.getEntryVersion() - 1;
            int dsid = stamp.getDistributedSystemId();
            long time = System.currentTimeMillis();
            tag.setEntryVersion(entryVersion);
            tag.setDistributedSystemId(dsid);
            tag.setVersionTimeStamp(time);
            tag.setIsRemoteForTesting();
            EntryEventImpl event = createNewEvent((PartitionedRegion) region, tag, entry.getKey(), "value-3");
            ((LocalRegion) region).basicUpdate(event, false, true, 0L, false);
            // Verify the new stamp
            entry = region.getEntry(key);
            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(dsid, stamp.getDistributedSystemId());
            return stamp.asVersionTag();
        }
    });
    VersionTag remoteTag = (VersionTag) vm3.invoke(new SerializableCallable("Get timestamp from remote site") {

        @Override
        public Object call() throws Exception {
            Cache cache = CacheFactory.getAnyInstance();
            final PartitionedRegion region = (PartitionedRegion) cache.getRegion(regionName);
            // wait for entry to be received
            WaitCriterion wc = new WaitCriterion() {

                public boolean done() {
                    Entry<?, ?> entry = null;
                    try {
                        entry = region.getDataStore().getEntryLocally(0, key, false, false);
                    } catch (EntryNotFoundException e) {
                    // expected
                    } catch (ForceReattemptException e) {
                    // expected
                    } catch (PRLocallyDestroyedException e) {
                        throw new RuntimeException("unexpected exception", e);
                    }
                    if (entry != null) {
                        LogWriterUtils.getLogWriter().info("found entry " + entry);
                    }
                    return (entry != null);
                }

                public String description() {
                    return "Expected key-1 to be received on remote WAN site";
                }
            };
            Wait.waitForCriterion(wc, 30000, 500, true);
            wc = new WaitCriterion() {

                public boolean done() {
                    Entry entry = region.getEntry(key);
                    assertTrue(entry instanceof EntrySnapshot);
                    RegionEntry regionEntry = ((EntrySnapshot) entry).getRegionEntry();
                    return regionEntry.getVersionStamp().getVersionTimeStamp() == tag.getVersionTimeStamp();
                }

                public String description() {
                    return "waiting for timestamp to be updated";
                }
            };
            Wait.waitForCriterion(wc, 30000, 500, true);
            Entry entry = region.getEntry(key);
            assertTrue(entry instanceof EntrySnapshot);
            RegionEntry regionEntry = ((EntrySnapshot) entry).getRegionEntry();
            VersionStamp stamp = regionEntry.getVersionStamp();
            return stamp.asVersionTag();
        }
    });
    assertEquals("Local and remote site have different timestamps", tag.getVersionTimeStamp(), remoteTag.getVersionTimeStamp());
}
Also used : Host(org.apache.geode.test.dunit.Host) VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp) Entry(org.apache.geode.cache.Region.Entry) NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VersionSource(org.apache.geode.internal.cache.versions.VersionSource) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) PRLocallyDestroyedException(org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 13 with VersionSource

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

the class UpdateVersionDUnitTest method testUpdateVersionAfterCreateWithSerialSenderOnDR.

@Test
public void testUpdateVersionAfterCreateWithSerialSenderOnDR() {
    Host host = Host.getHost(0);
    // server1 site1
    VM vm0 = host.getVM(0);
    // server2 site1
    VM vm1 = host.getVM(1);
    // server1 site2
    VM vm2 = host.getVM(2);
    // server2 site2
    VM vm3 = host.getVM(3);
    final String key = "key-1";
    // Site 1
    Integer lnPort = (Integer) vm0.invoke(() -> UpdateVersionDUnitTest.createFirstLocatorWithDSId(1));
    vm0.invoke(() -> UpdateVersionDUnitTest.createCache(lnPort));
    vm0.invoke(() -> UpdateVersionDUnitTest.createSender("ln1", 2, false, 10, 1, false, false, null, true));
    vm0.invoke(() -> UpdateVersionDUnitTest.createReplicatedRegion(regionName, "ln1"));
    vm0.invoke(() -> UpdateVersionDUnitTest.startSender("ln1"));
    vm0.invoke(() -> UpdateVersionDUnitTest.waitForSenderRunningState("ln1"));
    // Site 2
    Integer nyPort = (Integer) vm2.invoke(() -> UpdateVersionDUnitTest.createFirstRemoteLocator(2, lnPort));
    Integer nyRecPort = (Integer) vm2.invoke(() -> UpdateVersionDUnitTest.createReceiver(nyPort));
    vm2.invoke(() -> UpdateVersionDUnitTest.createReplicatedRegion(regionName, ""));
    vm3.invoke(() -> UpdateVersionDUnitTest.createCache(nyPort));
    vm3.invoke(() -> UpdateVersionDUnitTest.createReplicatedRegion(regionName, ""));
    final VersionTag tag = (VersionTag) vm0.invoke(new SerializableCallable("Update a single entry and get its version") {

        @Override
        public Object call() throws CacheException {
            Cache cache = CacheFactory.getAnyInstance();
            Region region = cache.getRegion(regionName);
            assertTrue(region instanceof DistributedRegion);
            region.put(key, "value-1");
            region.put(key, "value-2");
            Entry entry = region.getEntry(key);
            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.
            VersionSource memberId = (VersionSource) cache.getDistributedSystem().getDistributedMember();
            VersionTag tag = VersionTag.create(memberId);
            int entryVersion = stamp.getEntryVersion() - 1;
            int dsid = stamp.getDistributedSystemId();
            long time = System.currentTimeMillis();
            tag.setEntryVersion(entryVersion);
            tag.setDistributedSystemId(dsid);
            tag.setVersionTimeStamp(time);
            tag.setIsRemoteForTesting();
            EntryEventImpl event = createNewEvent((DistributedRegion) region, tag, entry.getKey(), "value-3");
            ((LocalRegion) region).basicUpdate(event, false, true, 0L, false);
            // Verify the new stamp
            entry = region.getEntry(key);
            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 + 1, stamp.getEntryVersion());
            assertEquals(dsid, stamp.getDistributedSystemId());
            return stamp.asVersionTag();
        }
    });
    VersionTag remoteTag = (VersionTag) vm3.invoke(new SerializableCallable("Get timestamp from remote site") {

        @Override
        public Object call() throws Exception {
            Cache cache = CacheFactory.getAnyInstance();
            final Region region = cache.getRegion(regionName);
            // wait for entry to be received
            WaitCriterion wc = new WaitCriterion() {

                public boolean done() {
                    return (region.getEntry(key) != null);
                }

                public String description() {
                    return "Expected key-1 to be received on remote WAN site";
                }
            };
            Wait.waitForCriterion(wc, 30000, 500, true);
            wc = new WaitCriterion() {

                public boolean done() {
                    Entry entry = region.getEntry(key);
                    assertTrue(entry instanceof NonTXEntry);
                    RegionEntry regionEntry = ((NonTXEntry) entry).getRegionEntry();
                    return regionEntry.getVersionStamp().getVersionTimeStamp() == tag.getVersionTimeStamp();
                }

                public String description() {
                    return "waiting for timestamp to be updated";
                }
            };
            Wait.waitForCriterion(wc, 30000, 500, true);
            Entry entry = region.getEntry(key);
            assertTrue(entry instanceof NonTXEntry);
            RegionEntry regionEntry = ((NonTXEntry) entry).getRegionEntry();
            VersionStamp stamp = regionEntry.getVersionStamp();
            return stamp.asVersionTag();
        }
    });
    assertEquals("Local and remote site have different timestamps", tag.getVersionTimeStamp(), remoteTag.getVersionTimeStamp());
}
Also used : NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) Host(org.apache.geode.test.dunit.Host) VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp) Entry(org.apache.geode.cache.Region.Entry) NonTXEntry(org.apache.geode.internal.cache.LocalRegion.NonTXEntry) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VersionSource(org.apache.geode.internal.cache.versions.VersionSource) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 14 with VersionSource

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

the class AbstractRegionMap method clear.

/**
   * Clear the region and, if an RVV is given, return a collection of the version sources in all
   * remaining tags
   */
public Set<VersionSource> clear(RegionVersionVector rvv) {
    Set<VersionSource> result = new HashSet<VersionSource>();
    if (!_isOwnerALocalRegion()) {
        // Fix for #41333. Just clear the the map
        // if we failed during initialization.
        _mapClear();
        return null;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Clearing entries for {} rvv={}", _getOwner(), " rvv=" + rvv);
    }
    LocalRegion lr = _getOwner();
    RegionVersionVector localRvv = lr.getVersionVector();
    incClearCount(lr);
    // lock for size calcs if the region might have tombstones
    Object lockObj = lr.getConcurrencyChecksEnabled() ? lr.getSizeGuard() : new Object();
    synchronized (lockObj) {
        if (rvv == null) {
            int delta = 0;
            try {
                // TODO soplog need to determine if stats should
                delta = sizeInVM();
            // reflect only size in memory or the complete thing
            } catch (GemFireIOException e) {
            // ignore rather than throwing an exception during cache close
            }
            int tombstones = lr.getTombstoneCount();
            _mapClear();
            _getOwner().updateSizeOnClearRegion(delta - tombstones);
            _getOwner().incTombstoneCount(-tombstones);
            if (delta != 0) {
                incEntryCount(-delta);
            }
        } else {
            int delta = 0;
            int tombstones = 0;
            VersionSource myId = _getOwner().getVersionMember();
            if (localRvv != rvv) {
                localRvv.recordGCVersions(rvv);
            }
            final boolean isTraceEnabled = logger.isTraceEnabled();
            for (RegionEntry re : regionEntries()) {
                synchronized (re) {
                    Token value = re.getValueAsToken();
                    // if it's already being removed or the entry is being created we leave it alone
                    if (value == Token.REMOVED_PHASE1 || value == Token.REMOVED_PHASE2) {
                        continue;
                    }
                    VersionSource id = re.getVersionStamp().getMemberID();
                    if (id == null) {
                        id = myId;
                    }
                    if (rvv.contains(id, re.getVersionStamp().getRegionVersion())) {
                        if (isTraceEnabled) {
                            logger.trace("region clear op is removing {} {}", re.getKey(), re.getVersionStamp());
                        }
                        boolean tombstone = re.isTombstone();
                        // note: it.remove() did not reliably remove the entry so we use remove(K,V) here
                        if (_getMap().remove(re.getKey(), re)) {
                            if (OffHeapRegionEntryHelper.doesClearNeedToCheckForOffHeap()) {
                                // OFFHEAP _getValue ok
                                GatewaySenderEventImpl.release(re._getValue());
                            }
                            // disk at this point.
                            try {
                                re.removePhase1(lr, true);
                            } catch (RegionClearedException e) {
                            // do nothing, it's already cleared.
                            }
                            re.removePhase2();
                            lruEntryDestroy(re);
                            if (tombstone) {
                                _getOwner().incTombstoneCount(-1);
                                tombstones += 1;
                            } else {
                                delta += 1;
                            }
                        }
                    } else {
                        // rvv does not contain this entry so it is retained
                        result.add(id);
                    }
                }
            }
            _getOwner().updateSizeOnClearRegion(delta);
            incEntryCount(-delta);
            incEntryCount(-tombstones);
            if (logger.isDebugEnabled()) {
                logger.debug("Size after clearing = {}", _getMap().size());
            }
            if (isTraceEnabled && _getMap().size() < 20) {
                _getOwner().dumpBackingMap();
            }
        }
    }
    return result;
}
Also used : VersionSource(org.apache.geode.internal.cache.versions.VersionSource) RegionVersionVector(org.apache.geode.internal.cache.versions.RegionVersionVector) StoredObject(org.apache.geode.internal.offheap.StoredObject) GemFireIOException(org.apache.geode.GemFireIOException) HashSet(java.util.HashSet)

Example 15 with VersionSource

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

the class AbstractRegionEntry method generateVersionTag.

/**
   * This generates version tags for outgoing messages for all subclasses supporting concurrency
   * versioning. It also sets the entry's version stamp to the tag's values.
   */
@Override
public VersionTag generateVersionTag(VersionSource member, boolean withDelta, LocalRegion region, EntryEventImpl event) {
    VersionStamp stamp = this.getVersionStamp();
    if (stamp != null && region.getServerProxy() == null) {
        // clients do not generate versions
        int v = stamp.getEntryVersion() + 1;
        if (v > 0xFFFFFF) {
            // roll-over
            v -= 0x1000000;
        }
        VersionSource previous = stamp.getMemberID();
        // space.
        if (member == null) {
            VersionSource regionMember = region.getVersionMember();
            if (regionMember instanceof DiskStoreID) {
                member = regionMember;
            }
        }
        VersionTag tag = VersionTag.create(member);
        tag.setEntryVersion(v);
        if (region.getVersionVector() != null) {
            // Use region version if already provided, else generate
            long nextRegionVersion = event.getNextRegionVersion();
            if (nextRegionVersion != -1) {
                // Set on the tag and record it locally
                tag.setRegionVersion(nextRegionVersion);
                RegionVersionVector rvv = region.getVersionVector();
                rvv.recordVersion(rvv.getOwnerId(), nextRegionVersion);
                if (logger.isDebugEnabled()) {
                    logger.debug("recorded region version {}; region={}", nextRegionVersion, region.getFullPath());
                }
            } else {
                tag.setRegionVersion(region.getVersionVector().getNextVersion());
            }
        }
        if (withDelta) {
            tag.setPreviousMemberID(previous);
        }
        VersionTag remoteTag = event.getVersionTag();
        if (remoteTag != null && remoteTag.isGatewayTag()) {
            // if this event was received from a gateway we use the remote system's
            // timestamp and dsid.
            tag.setVersionTimeStamp(remoteTag.getVersionTimeStamp());
            tag.setDistributedSystemId(remoteTag.getDistributedSystemId());
            tag.setAllowedByResolver(remoteTag.isAllowedByResolver());
        } else {
            long time = region.cacheTimeMillis();
            int dsid = region.getDistributionManager().getDistributedSystemId();
            // one received from a wan gateway, so fake a timestamp if necessary
            if (time <= stamp.getVersionTimeStamp() && dsid != tag.getDistributedSystemId()) {
                time = stamp.getVersionTimeStamp() + 1;
            }
            tag.setVersionTimeStamp(time);
            tag.setDistributedSystemId(dsid);
        }
        stamp.setVersions(tag);
        stamp.setMemberID(member);
        event.setVersionTag(tag);
        if (logger.isDebugEnabled()) {
            logger.debug("generated tag {}; key={}; oldvalue={} newvalue={} client={} region={}; rvv={}", tag, event.getKey(), event.getOldValueStringForm(), event.getNewValueStringForm(), event.getContext() == null ? "none" : event.getContext().getDistributedMember().getName(), region.getFullPath(), region.getVersionVector());
        }
        return tag;
    }
    return null;
}
Also used : VersionSource(org.apache.geode.internal.cache.versions.VersionSource) VersionTag(org.apache.geode.internal.cache.versions.VersionTag) RegionVersionVector(org.apache.geode.internal.cache.versions.RegionVersionVector) DiskStoreID(org.apache.geode.internal.cache.persistence.DiskStoreID) VersionStamp(org.apache.geode.internal.cache.versions.VersionStamp)

Aggregations

VersionSource (org.apache.geode.internal.cache.versions.VersionSource)34 VersionTag (org.apache.geode.internal.cache.versions.VersionTag)16 VersionStamp (org.apache.geode.internal.cache.versions.VersionStamp)13 Entry (org.apache.geode.cache.Region.Entry)10 NonTXEntry (org.apache.geode.internal.cache.LocalRegion.NonTXEntry)10 Test (org.junit.Test)10 RegionVersionVector (org.apache.geode.internal.cache.versions.RegionVersionVector)6 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)6 Host (org.apache.geode.test.dunit.Host)5 VM (org.apache.geode.test.dunit.VM)5 HashMap (java.util.HashMap)4 Cache (org.apache.geode.cache.Cache)4 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)4 Region (org.apache.geode.cache.Region)4 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)4 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)4 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 Map (java.util.Map)3 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)3 PRLocallyDestroyedException (org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException)3