Search in sources :

Example 6 with ServerBucketProfile

use of org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile in project geode by apache.

the class PartitionedRegion method executeOnSingleNode.

/**
   * Single key execution on single node
   * 
   * @since GemFire 6.0
   */
private ResultCollector executeOnSingleNode(final Function function, final PartitionedRegionFunctionExecutor execution, ResultCollector rc, boolean isPRSingleHop, boolean isBucketSetAsFilter) {
    final Set routingKeys = execution.getFilter();
    final Object key = routingKeys.iterator().next();
    final Integer bucketId;
    if (isBucketSetAsFilter) {
        bucketId = (Integer) key;
    } else {
        bucketId = PartitionedRegionHelper.getHashKey(this, Operation.FUNCTION_EXECUTION, key, null, null);
    }
    InternalDistributedMember targetNode = null;
    if (function.optimizeForWrite()) {
        targetNode = createBucket(bucketId, 0, null);
        HeapMemoryMonitor hmm = ((InternalResourceManager) cache.getResourceManager()).getHeapMonitor();
        if (hmm.isMemberHeapCritical(targetNode) && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
            Set<DistributedMember> sm = Collections.singleton((DistributedMember) targetNode);
            throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(function.getId(), sm), sm);
        }
    } else {
        targetNode = getOrCreateNodeForBucketRead(bucketId);
    }
    final DistributedMember localVm = getMyId();
    if (targetNode != null && isPRSingleHop && !localVm.equals(targetNode)) {
        Set<ServerBucketProfile> profiles = this.getRegionAdvisor().getClientBucketProfiles(bucketId);
        if (profiles != null) {
            for (ServerBucketProfile profile : profiles) {
                if (profile.getDistributedMember().equals(targetNode)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("FunctionServiceSingleHop: Found remote node.{}", localVm);
                    }
                    throw new InternalFunctionInvocationTargetException(LocalizedStrings.PartitionedRegion_MULTIPLE_TARGET_NODE_FOUND_FOR.toLocalizedString());
                }
            }
        }
    }
    if (targetNode == null) {
        throw new FunctionException(LocalizedStrings.PartitionedRegion_NO_TARGET_NODE_FOUND_FOR_KEY_0.toLocalizedString(key));
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Executing Function: {} setArguments={} on {}", function.getId(), execution.getArguments(), targetNode);
    }
    while (!execution.getFailedNodes().isEmpty()) {
        RetryTimeKeeper retryTime = new RetryTimeKeeper(this.retryTimeout);
        if (execution.getFailedNodes().contains(targetNode.getId())) {
            /*
         * if (retryTime.overMaximum()) { PRHARedundancyProvider.timedOut(this, null, null,
         * "doing function execution", this.retryTimeout); // NOTREACHED }
         */
            // Fix for Bug # 40083
            targetNode = null;
            while (targetNode == null) {
                if (retryTime.overMaximum()) {
                    PRHARedundancyProvider.timedOut(this, null, null, "doing function execution", this.retryTimeout);
                // NOTREACHED
                }
                retryTime.waitToRetryNode();
                if (function.optimizeForWrite()) {
                    targetNode = getOrCreateNodeForBucketWrite(bucketId, retryTime);
                } else {
                    targetNode = getOrCreateNodeForBucketRead(bucketId);
                }
            }
            if (targetNode == null) {
                throw new FunctionException(LocalizedStrings.PartitionedRegion_NO_TARGET_NODE_FOUND_FOR_KEY_0.toLocalizedString(key));
            }
        } else {
            execution.clearFailedNodes();
        }
    }
    final HashSet<Integer> buckets = new HashSet<Integer>();
    buckets.add(bucketId);
    final Set<InternalDistributedMember> singleMember = Collections.singleton(targetNode);
    execution.validateExecution(function, singleMember);
    execution.setExecutionNodes(singleMember);
    LocalResultCollector<?, ?> localRC = execution.getLocalResultCollector(function, rc);
    if (targetNode.equals(localVm)) {
        final DM dm = getDistributionManager();
        PartitionedRegionFunctionResultSender resultSender = new PartitionedRegionFunctionResultSender(dm, PartitionedRegion.this, 0, localRC, execution.getServerResultSender(), true, false, execution.isForwardExceptions(), function, buckets);
        final FunctionContext context = new RegionFunctionContextImpl(function.getId(), PartitionedRegion.this, execution.getArgumentsForMember(localVm.getId()), routingKeys, ColocationHelper.constructAndGetAllColocatedLocalDataSet(PartitionedRegion.this, buckets), buckets, resultSender, execution.isReExecute());
        execution.executeFunctionOnLocalPRNode(function, context, resultSender, dm, isTX());
        return localRC;
    } else {
        return executeFunctionOnRemoteNode(targetNode, function, execution.getArgumentsForMember(targetNode.getId()), routingKeys, function.isHA() ? rc : localRC, buckets, execution.getServerResultSender(), execution);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) FunctionException(org.apache.geode.cache.execute.FunctionException) EmptyRegionFunctionException(org.apache.geode.cache.execute.EmptyRegionFunctionException) DM(org.apache.geode.distributed.internal.DM) HeapMemoryMonitor(org.apache.geode.internal.cache.control.HeapMemoryMonitor) FunctionContext(org.apache.geode.cache.execute.FunctionContext) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerBucketProfile(org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) RegionFunctionContextImpl(org.apache.geode.internal.cache.execute.RegionFunctionContextImpl) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) PartitionedRegionFunctionResultSender(org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultSender) LowMemoryException(org.apache.geode.cache.LowMemoryException) HashSet(java.util.HashSet)

Example 7 with ServerBucketProfile

use of org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile in project geode by apache.

the class Shipment method verifyMetadata.

public static void verifyMetadata(Map<Integer, List<BucketServerLocation66>> clientMap) {
    final PartitionedRegion pr = (PartitionedRegion) region;
    ConcurrentHashMap<Integer, Set<ServerBucketProfile>> serverMap = pr.getRegionAdvisor().getAllClientBucketProfilesTest();
    assertEquals(clientMap.size(), serverMap.size());
    assertTrue(clientMap.keySet().containsAll(serverMap.keySet()));
    for (Map.Entry<Integer, List<BucketServerLocation66>> entry : clientMap.entrySet()) {
        int bucketId = entry.getKey();
        List<BucketServerLocation66> list = entry.getValue();
        BucketServerLocation66 primaryBSL = null;
        int primaryCnt = 0;
        for (BucketServerLocation66 bsl : list) {
            if (bsl.isPrimary()) {
                primaryBSL = bsl;
                primaryCnt++;
            }
        }
        assertTrue(primaryCnt == 1);
        Set<ServerBucketProfile> set = serverMap.get(bucketId);
        assertEquals(list.size(), set.size());
        primaryCnt = 0;
        for (ServerBucketProfile bp : set) {
            ServerLocation sl = (ServerLocation) bp.bucketServerLocations.toArray()[0];
            assertTrue(list.contains(sl));
            // should be only one primary
            if (bp.isPrimary) {
                primaryCnt++;
                assertTrue(primaryBSL.equals(sl));
            }
        }
        assertTrue(primaryCnt == 1);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ServerBucketProfile(org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 8 with ServerBucketProfile

use of org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile in project geode by apache.

the class Shipment method testMetadataIsSameOnAllServersAndClients.

// GEODE-1763
@Category(FlakyTest.class)
@Test
public void testMetadataIsSameOnAllServersAndClients() {
    Integer port0 = (Integer) member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.createServer(3, 4));
    Integer port1 = (Integer) member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.createServer(3, 4));
    Integer port2 = (Integer) member2.invoke(() -> PartitionedRegionSingleHopDUnitTest.createServer(3, 4));
    Integer port3 = (Integer) member3.invoke(() -> PartitionedRegionSingleHopDUnitTest.createServer(3, 4));
    createClient(port0, port1, port2, port3);
    put();
    member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    member2.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    member3.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    ClientMetadataService cms = ((GemFireCacheImpl) cache).getClientMetadataService();
    cms.getClientPRMetadata((LocalRegion) region);
    Map<String, ClientPartitionAdvisor> regionMetaData = cms.getClientPRMetadata_TEST_ONLY();
    assertEquals(1, regionMetaData.size());
    assertTrue(regionMetaData.containsKey(region.getFullPath()));
    ClientPartitionAdvisor prMetaData = regionMetaData.get(region.getFullPath());
    final Map<Integer, List<BucketServerLocation66>> clientMap = prMetaData.getBucketServerLocationsMap_TEST_ONLY();
    WaitCriterion wc = new WaitCriterion() {

        public boolean done() {
            return (clientMap.size() == 4);
        }

        public String description() {
            return "expected no metadata to be refreshed";
        }
    };
    Wait.waitForCriterion(wc, 60000, 1000, true);
    for (Entry entry : clientMap.entrySet()) {
        assertEquals(4, ((List) entry.getValue()).size());
    }
    member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(clientMap));
    member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(clientMap));
    member2.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(clientMap));
    member3.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(clientMap));
    member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.stopServer());
    member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.stopServer());
    member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.startServerOnPort(port0));
    member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.startServerOnPort(port1));
    put();
    member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    member2.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    member3.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    cms = ((GemFireCacheImpl) cache).getClientMetadataService();
    wc = new WaitCriterion() {

        public boolean done() {
            ClientMetadataService cms = ((GemFireCacheImpl) cache).getClientMetadataService();
            Map<String, ClientPartitionAdvisor> regionMetaData = cms.getClientPRMetadata_TEST_ONLY();
            assertEquals(1, regionMetaData.size());
            assertTrue(regionMetaData.containsKey(region.getFullPath()));
            ClientPartitionAdvisor prMetaData = regionMetaData.get(region.getFullPath());
            Map<Integer, List<BucketServerLocation66>> clientMap = prMetaData.getBucketServerLocationsMap_TEST_ONLY();
            assertEquals(4, /* numBuckets */
            clientMap.size());
            boolean finished = true;
            for (Entry entry : clientMap.entrySet()) {
                List list = (List) entry.getValue();
                if (list.size() < 4) {
                    LogWriterUtils.getLogWriter().info("still waiting for 4 bucket owners in " + entry.getKey() + ": " + list);
                    finished = false;
                    break;
                }
            }
            return finished;
        }

        public String description() {
            return "bucket copies are not created";
        }
    };
    Wait.waitForCriterion(wc, 60000, 400, true);
    cms = ((GemFireCacheImpl) cache).getClientMetadataService();
    cms.getClientPRMetadata((LocalRegion) region);
    regionMetaData = cms.getClientPRMetadata_TEST_ONLY();
    assertEquals(1, regionMetaData.size());
    assertTrue(regionMetaData.containsKey(region.getFullPath()));
    prMetaData = regionMetaData.get(region.getFullPath());
    final Map<Integer, List<BucketServerLocation66>> clientMap2 = prMetaData.getBucketServerLocationsMap_TEST_ONLY();
    wc = new WaitCriterion() {

        public boolean done() {
            return (clientMap2.size() == 4);
        }

        public String description() {
            return "expected no metadata to be refreshed";
        }
    };
    Wait.waitForCriterion(wc, 60000, 1000, true);
    for (Entry entry : clientMap.entrySet()) {
        assertEquals(4, ((List) entry.getValue()).size());
    }
    member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(clientMap));
    member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(clientMap));
    member2.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(clientMap));
    member3.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(clientMap));
    member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.closeCacheAndDisconnect());
    member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.closeCacheAndDisconnect());
    // member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.createServerOnPort(3,4,port0 ));
    // member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.createServerOnPort(3,4,port1 ));
    put();
    member2.invoke(new CacheSerializableRunnable("aba") {

        @Override
        public void run2() throws CacheException {
            final PartitionedRegion pr = (PartitionedRegion) region;
            ConcurrentHashMap<Integer, Set<ServerBucketProfile>> serverMap = pr.getRegionAdvisor().getAllClientBucketProfilesTest();
        }
    });
    member3.invoke(new CacheSerializableRunnable("aba") {

        @Override
        public void run2() throws CacheException {
            final PartitionedRegion pr = (PartitionedRegion) region;
            ConcurrentHashMap<Integer, Set<ServerBucketProfile>> serverMap = pr.getRegionAdvisor().getAllClientBucketProfilesTest();
        }
    });
    // member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    // member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    member2.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    member3.invoke(() -> PartitionedRegionSingleHopDUnitTest.waitForLocalBucketsCreation(4));
    cms = ((GemFireCacheImpl) cache).getClientMetadataService();
    cms.getClientPRMetadata((LocalRegion) region);
    regionMetaData = cms.getClientPRMetadata_TEST_ONLY();
    assertEquals(1, regionMetaData.size());
    assertTrue(regionMetaData.containsKey(region.getFullPath()));
    prMetaData = regionMetaData.get(region.getFullPath());
    final Map<Integer, List<BucketServerLocation66>> clientMap3 = prMetaData.getBucketServerLocationsMap_TEST_ONLY();
    Awaitility.waitAtMost(60, TimeUnit.SECONDS).until(() -> (clientMap3.size() == 4));
    for (Entry entry : clientMap.entrySet()) {
        assertEquals(2, ((List) entry.getValue()).size());
    }
    final Map<Integer, List<BucketServerLocation66>> fclientMap = clientMap;
    Wait.waitForCriterion(new WaitCriterion() {

        public boolean done() {
            try {
                // member0.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(fclientMap));
                // member1.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(fclientMap));
                member2.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(fclientMap));
                member3.invoke(() -> PartitionedRegionSingleHopDUnitTest.verifyMetadata(fclientMap));
            } catch (Exception e) {
                LogWriterUtils.getLogWriter().info("verification failed", e);
                return false;
            }
            return true;
        }

        public String description() {
            return "verification of metadata on all members";
        }
    }, 20000, 2000, true);
}
Also used : CacheException(org.apache.geode.cache.CacheException) ClientMetadataService(org.apache.geode.cache.client.internal.ClientMetadataService) IgnoredException(org.apache.geode.test.dunit.IgnoredException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) ServerBucketProfile(org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile) Entry(java.util.Map.Entry) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) ClientPartitionAdvisor(org.apache.geode.cache.client.internal.ClientPartitionAdvisor) List(java.util.List) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Aggregations

ServerBucketProfile (org.apache.geode.internal.cache.BucketAdvisor.ServerBucketProfile)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 EmptyRegionFunctionException (org.apache.geode.cache.execute.EmptyRegionFunctionException)3 ResultsSet (org.apache.geode.cache.query.internal.ResultsSet)3 DM (org.apache.geode.distributed.internal.DM)3 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)3 InternalFunctionInvocationTargetException (org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException)3 PartitionedRegionFunctionResultSender (org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultSender)3 RegionFunctionContextImpl (org.apache.geode.internal.cache.execute.RegionFunctionContextImpl)3 Iterator (java.util.Iterator)2 Entry (java.util.Map.Entry)2 CacheException (org.apache.geode.cache.CacheException)2 LowMemoryException (org.apache.geode.cache.LowMemoryException)2