Search in sources :

Example 1 with ProxyBucketRegion

use of org.apache.geode.internal.cache.ProxyBucketRegion in project geode by apache.

the class ParallelQueueRemovalMessageJUnitTest method createBucketRegionQueue.

private void createBucketRegionQueue() {
    // Create InternalRegionArguments
    InternalRegionArguments ira = new InternalRegionArguments();
    ira.setPartitionedRegion(this.queueRegion);
    ira.setPartitionedRegionBucketRedundancy(1);
    BucketAdvisor ba = mock(BucketAdvisor.class);
    ira.setBucketAdvisor(ba);
    InternalRegionArguments pbrIra = new InternalRegionArguments();
    RegionAdvisor ra = mock(RegionAdvisor.class);
    when(ra.getPartitionedRegion()).thenReturn(this.queueRegion);
    pbrIra.setPartitionedRegionAdvisor(ra);
    PartitionAttributes pa = mock(PartitionAttributes.class);
    when(this.queueRegion.getPartitionAttributes()).thenReturn(pa);
    when(this.queueRegion.getBucketName(eq(BUCKET_ID))).thenAnswer(new Answer<String>() {

        @Override
        public String answer(final InvocationOnMock invocation) throws Throwable {
            return PartitionedRegionHelper.getBucketName(queueRegion.getFullPath(), BUCKET_ID);
        }
    });
    when(this.queueRegion.getDataPolicy()).thenReturn(DataPolicy.PARTITION);
    when(pa.getColocatedWith()).thenReturn(null);
    // classes cannot be mocked
    ProxyBucketRegion pbr = new ProxyBucketRegion(BUCKET_ID, this.queueRegion, pbrIra);
    when(ba.getProxyBucketRegion()).thenReturn(pbr);
    // Create RegionAttributes
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.REPLICATE);
    factory.setEvictionAttributes(EvictionAttributes.createLRUMemoryAttributes(100, null, EvictionAction.OVERFLOW_TO_DISK));
    RegionAttributes attributes = factory.create();
    // Create BucketRegionQueue
    BucketRegionQueue realBucketRegionQueue = new BucketRegionQueue(this.queueRegion.getBucketName(BUCKET_ID), attributes, this.rootRegion, this.cache, ira);
    this.bucketRegionQueue = spy(realBucketRegionQueue);
    // (this.queueRegion.getBucketName(BUCKET_ID), attributes, this.rootRegion, this.cache, ira);
    EntryEventImpl entryEvent = EntryEventImpl.create(this.bucketRegionQueue, Operation.DESTROY, mock(EventID.class), "value", null, false, mock(DistributedMember.class));
    doReturn(entryEvent).when(this.bucketRegionQueue).newDestroyEntryEvent(any(), any());
    // when(this.bucketRegionQueue.newDestroyEntryEvent(any(), any())).thenReturn();
    this.bucketRegionQueueHelper = new BucketRegionQueueHelper(this.cache, this.queueRegion, this.bucketRegionQueue);
}
Also used : RegionAdvisor(org.apache.geode.internal.cache.partitioned.RegionAdvisor) RegionAttributes(org.apache.geode.cache.RegionAttributes) EntryEventImpl(org.apache.geode.internal.cache.EntryEventImpl) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) InternalRegionArguments(org.apache.geode.internal.cache.InternalRegionArguments) BucketRegionQueueHelper(org.apache.geode.internal.cache.BucketRegionQueueHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) BucketRegionQueue(org.apache.geode.internal.cache.BucketRegionQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ProxyBucketRegion(org.apache.geode.internal.cache.ProxyBucketRegion) DistributedMember(org.apache.geode.distributed.DistributedMember) EventID(org.apache.geode.internal.cache.EventID) BucketAdvisor(org.apache.geode.internal.cache.BucketAdvisor)

Example 2 with ProxyBucketRegion

use of org.apache.geode.internal.cache.ProxyBucketRegion in project geode by apache.

the class MembershipFlushRequest method process.

@Override
protected void process(DistributionManager dm) {
    int initLevel = LocalRegion.ANY_INIT;
    int oldLevel = LocalRegion.setThreadInitLevelRequirement(initLevel);
    ReplyException exception = null;
    try {
        // get the region from the path, but do NOT wait on initialization,
        // otherwise we could have a distributed deadlock
        Cache cache = CacheFactory.getInstance(dm.getSystem());
        PartitionedRegion region = (PartitionedRegion) cache.getRegion(this.regionPath);
        if (region != null && region.getRegionAdvisor().isInitialized()) {
            ProxyBucketRegion[] proxyBuckets = region.getRegionAdvisor().getProxyBucketArray();
            // buckets are null if initPRInternals is still not complete
            if (proxyBuckets != null) {
                for (ProxyBucketRegion bucket : proxyBuckets) {
                    final BucketPersistenceAdvisor persistenceAdvisor = bucket.getPersistenceAdvisor();
                    if (persistenceAdvisor != null) {
                        persistenceAdvisor.flushMembershipChanges();
                    }
                }
            }
        }
    } catch (RegionDestroyedException e) {
    // ignore
    } catch (CancelException e) {
    // ignore
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable t) {
        SystemFailure.checkFailure();
        exception = new ReplyException(t);
    } finally {
        LocalRegion.setThreadInitLevelRequirement(oldLevel);
        ReplyMessage replyMsg = new ReplyMessage();
        replyMsg.setRecipient(getSender());
        replyMsg.setProcessorId(processorId);
        if (exception != null) {
            replyMsg.setException(exception);
        }
        dm.putOutgoing(replyMsg);
    }
}
Also used : BucketPersistenceAdvisor(org.apache.geode.internal.cache.BucketPersistenceAdvisor) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) ProxyBucketRegion(org.apache.geode.internal.cache.ProxyBucketRegion) CancelException(org.apache.geode.CancelException) ReplyException(org.apache.geode.distributed.internal.ReplyException) ReplyMessage(org.apache.geode.distributed.internal.ReplyMessage) Cache(org.apache.geode.cache.Cache)

Example 3 with ProxyBucketRegion

use of org.apache.geode.internal.cache.ProxyBucketRegion in project geode by apache.

the class MemoryThresholdsOffHeapDUnitTest method testPRLoadRejection.

/**
   * Test that a Partitioned Region loader invocation is rejected if the VM with the bucket is in a
   * critical state.
   */
// GEODE-551: waitForCriterion, memory sensitive
@Category(FlakyTest.class)
@Test
public void testPRLoadRejection() throws Exception {
    final Host host = Host.getHost(0);
    final VM accessor = host.getVM(1);
    final VM ds1 = host.getVM(2);
    final String rName = getUniqueName();
    // Make sure the desired VMs will have a fresh DS. TODO: convert these from AsyncInvocation to
    // invoke
    AsyncInvocation d0 = accessor.invokeAsync(() -> disconnectFromDS());
    AsyncInvocation d1 = ds1.invokeAsync(() -> disconnectFromDS());
    d0.join();
    assertFalse(d0.exceptionOccurred());
    d1.join();
    assertFalse(d1.exceptionOccurred());
    CacheSerializableRunnable establishConnectivity = new CacheSerializableRunnable("establishcConnectivity") {

        @Override
        public void run2() throws CacheException {
            getSystem();
        }
    };
    ds1.invoke(establishConnectivity);
    accessor.invoke(establishConnectivity);
    ds1.invoke(createPR(rName, false));
    accessor.invoke(createPR(rName, true));
    final AtomicInteger expectedInvocations = new AtomicInteger(0);
    Integer ex = (Integer) accessor.invoke(new SerializableCallable("Invoke loader from accessor, non-critical") {

        public Object call() throws Exception {
            Region<Integer, String> r = getCache().getRegion(rName);
            Integer k = new Integer(1);
            Integer expectedInvocations0 = new Integer(expectedInvocations.getAndIncrement());
            // should load for new key
            assertEquals(k.toString(), r.get(k, expectedInvocations0));
            assertTrue(r.containsKey(k));
            Integer expectedInvocations1 = new Integer(expectedInvocations.get());
            // no load
            assertEquals(k.toString(), r.get(k, expectedInvocations1));
            // no load
            assertEquals(k.toString(), r.get(k, expectedInvocations1));
            return expectedInvocations1;
        }
    });
    expectedInvocations.set(ex.intValue());
    ex = (Integer) ds1.invoke(new SerializableCallable("Invoke loader from datastore, non-critical") {

        public Object call() throws Exception {
            Region<Integer, String> r = getCache().getRegion(rName);
            Integer k = new Integer(2);
            Integer expectedInvocations1 = new Integer(expectedInvocations.getAndIncrement());
            // should load for new key
            assertEquals(k.toString(), r.get(k, expectedInvocations1));
            assertTrue(r.containsKey(k));
            Integer expectedInvocations2 = new Integer(expectedInvocations.get());
            // no load
            assertEquals(k.toString(), r.get(k, expectedInvocations2));
            // no load
            assertEquals(k.toString(), r.get(k, expectedInvocations2));
            String oldVal = r.remove(k);
            assertFalse(r.containsKey(k));
            assertEquals(k.toString(), oldVal);
            return expectedInvocations2;
        }
    });
    expectedInvocations.set(ex.intValue());
    accessor.invoke(addExpectedException);
    ds1.invoke(addExpectedException);
    ex = (Integer) ds1.invoke(new SerializableCallable("Set critical state, assert local load behavior") {

        public Object call() throws Exception {
            final OffHeapMemoryMonitor ohmm = ((InternalResourceManager) getCache().getResourceManager()).getOffHeapMonitor();
            final PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
            final RegionAdvisor advisor = pr.getRegionAdvisor();
            pr.put("oh1", new byte[838860]);
            pr.put("oh3", new byte[157287]);
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "verify critical state";
                }

                public boolean done() {
                    for (final ProxyBucketRegion bucket : advisor.getProxyBucketArray()) {
                        if (bucket.isBucketSick()) {
                            return true;
                        }
                    }
                    return false;
                }
            };
            Wait.waitForCriterion(wc, 30 * 1000, 10, true);
            // reload with same key again and again
            final Integer k = new Integer(2);
            final Integer expectedInvocations3 = new Integer(expectedInvocations.getAndIncrement());
            // load
            assertEquals(k.toString(), pr.get(k, expectedInvocations3));
            assertFalse(pr.containsKey(k));
            Integer expectedInvocations4 = new Integer(expectedInvocations.getAndIncrement());
            // load
            assertEquals(k.toString(), pr.get(k, expectedInvocations4));
            assertFalse(pr.containsKey(k));
            Integer expectedInvocations5 = new Integer(expectedInvocations.get());
            // load
            assertEquals(k.toString(), pr.get(k, expectedInvocations5));
            assertFalse(pr.containsKey(k));
            return expectedInvocations5;
        }
    });
    expectedInvocations.set(ex.intValue());
    ex = (Integer) accessor.invoke(new SerializableCallable("During critical state on datastore, assert accesor load behavior") {

        public Object call() throws Exception {
            // reload with same key again and again
            final Integer k = new Integer(2);
            Integer expectedInvocations6 = new Integer(expectedInvocations.incrementAndGet());
            Region<Integer, String> r = getCache().getRegion(rName);
            // load
            assertEquals(k.toString(), r.get(k, expectedInvocations6));
            assertFalse(r.containsKey(k));
            Integer expectedInvocations7 = new Integer(expectedInvocations.incrementAndGet());
            // load
            assertEquals(k.toString(), r.get(k, expectedInvocations7));
            assertFalse(r.containsKey(k));
            return expectedInvocations7;
        }
    });
    expectedInvocations.set(ex.intValue());
    ex = (Integer) ds1.invoke(new SerializableCallable("Set safe state on datastore, assert local load behavior") {

        public Object call() throws Exception {
            final PartitionedRegion r = (PartitionedRegion) getCache().getRegion(rName);
            r.destroy("oh3");
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "verify critical state";
                }

                public boolean done() {
                    return !r.memoryThresholdReached.get();
                }
            };
            Wait.waitForCriterion(wc, 30 * 1000, 10, true);
            // same key as previously used, this time is should stick
            Integer k = new Integer(3);
            Integer expectedInvocations8 = new Integer(expectedInvocations.incrementAndGet());
            // last load for 3
            assertEquals(k.toString(), r.get(k, expectedInvocations8));
            assertTrue(r.containsKey(k));
            return expectedInvocations8;
        }
    });
    expectedInvocations.set(ex.intValue());
    accessor.invoke(new SerializableCallable("Data store in safe state, assert load behavior, accessor sets critical state, assert load behavior") {

        public Object call() throws Exception {
            final OffHeapMemoryMonitor ohmm = ((InternalResourceManager) getCache().getResourceManager()).getOffHeapMonitor();
            assertFalse(ohmm.getState().isCritical());
            Integer k = new Integer(4);
            Integer expectedInvocations9 = new Integer(expectedInvocations.incrementAndGet());
            final PartitionedRegion r = (PartitionedRegion) getCache().getRegion(rName);
            // load for 4
            assertEquals(k.toString(), r.get(k, expectedInvocations9));
            assertTrue(r.containsKey(k));
            // no load
            assertEquals(k.toString(), r.get(k, expectedInvocations9));
            // Go critical in accessor
            r.put("oh3", new byte[157287]);
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "verify critical state";
                }

                public boolean done() {
                    return r.memoryThresholdReached.get();
                }
            };
            k = new Integer(5);
            Integer expectedInvocations10 = new Integer(expectedInvocations.incrementAndGet());
            // load for key 5
            assertEquals(k.toString(), r.get(k, expectedInvocations10));
            assertTrue(r.containsKey(k));
            // no load
            assertEquals(k.toString(), r.get(k, expectedInvocations10));
            // Clean up critical state
            r.destroy("oh3");
            wc = new WaitCriterion() {

                public String description() {
                    return "verify critical state";
                }

                public boolean done() {
                    return !ohmm.getState().isCritical();
                }
            };
            return expectedInvocations10;
        }
    });
    accessor.invoke(removeExpectedException);
    ds1.invoke(removeExpectedException);
}
Also used : OffHeapMemoryMonitor(org.apache.geode.internal.cache.control.OffHeapMemoryMonitor) RegionAdvisor(org.apache.geode.internal.cache.partitioned.RegionAdvisor) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) ProxyBucketRegion(org.apache.geode.internal.cache.ProxyBucketRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) ProxyBucketRegion(org.apache.geode.internal.cache.ProxyBucketRegion) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

ProxyBucketRegion (org.apache.geode.internal.cache.ProxyBucketRegion)3 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)2 RegionAdvisor (org.apache.geode.internal.cache.partitioned.RegionAdvisor)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CancelException (org.apache.geode.CancelException)1 AttributesFactory (org.apache.geode.cache.AttributesFactory)1 Cache (org.apache.geode.cache.Cache)1 CacheException (org.apache.geode.cache.CacheException)1 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)1 LowMemoryException (org.apache.geode.cache.LowMemoryException)1 PartitionAttributes (org.apache.geode.cache.PartitionAttributes)1 Region (org.apache.geode.cache.Region)1 RegionAttributes (org.apache.geode.cache.RegionAttributes)1 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)1 ServerOperationException (org.apache.geode.cache.client.ServerOperationException)1 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)1 DistributedMember (org.apache.geode.distributed.DistributedMember)1 ReplyException (org.apache.geode.distributed.internal.ReplyException)1 ReplyMessage (org.apache.geode.distributed.internal.ReplyMessage)1 BucketAdvisor (org.apache.geode.internal.cache.BucketAdvisor)1