jdeppe-pivotal commented on a change in pull request #6467: URL: https://github.com/apache/geode/pull/6467#discussion_r633607799
########## File path: geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/SlotAdvisor.java ########## @@ -0,0 +1,210 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.apache.geode.redis.internal; + +import static org.apache.geode.redis.internal.RegionProvider.REDIS_REGION_BUCKETS; +import static org.apache.geode.redis.internal.RegionProvider.REDIS_SLOTS_PER_BUCKET; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.tuple.Pair; +import org.apache.logging.log4j.Logger; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.execute.FunctionService; +import org.apache.geode.cache.execute.ResultCollector; +import org.apache.geode.cache.partition.PartitionRegionHelper; +import org.apache.geode.distributed.DistributedMember; +import org.apache.geode.distributed.internal.membership.InternalDistributedMember; +import org.apache.geode.internal.cache.PartitionedRegion; +import org.apache.geode.logging.internal.log4j.api.LogService; +import org.apache.geode.redis.internal.cluster.RedisMemberInfo; +import org.apache.geode.redis.internal.cluster.RedisMemberInfoRetrievalFunction; +import org.apache.geode.redis.internal.data.RedisData; +import org.apache.geode.redis.internal.data.RedisKey; + +public class SlotAdvisor { + + private static final Logger logger = LogService.getLogger(); + private static final int HOSTPORT_RETRIEVAL_ATTEMPTS = 10; + private static final int HOSTPORT_RETRIEVAL_INTERVAL = 6_000; + + /** + * Cache of member ids to member IP address and redis listening port + */ + private final Map<DistributedMember, Pair<String, Integer>> hostPorts = new HashMap<>(); + private final PartitionedRegion dataRegion; + + SlotAdvisor(Region<RedisKey, RedisData> dataRegion) { + this.dataRegion = (PartitionedRegion) dataRegion; + } + + public boolean isLocal(RedisKey key) { + return dataRegion.getRegionAdvisor().getBucket(key.getBucketId()).getBucketAdvisor() + .isPrimary(); + } + + public Pair<String, Integer> getHostAndPortForKey(RedisKey key) { + return getHostPort(key.getBucketId()); + } + + public Map<String, List<Integer>> getMemberBuckets() { + initializeBucketsIfNecessary(); + + Map<String, List<Integer>> memberBuckets = new HashMap<>(); + for (int bucketId = 0; bucketId < REDIS_REGION_BUCKETS; bucketId++) { + String memberId = getOrCreateMember(bucketId).getUniqueId(); + memberBuckets.computeIfAbsent(memberId, k -> new ArrayList<>()).add(bucketId); + } + + return memberBuckets; + } + + /** + * This returns a list of {@link MemberBucketSlot}s where each entry corresponds to a bucket. If + * the details for a given bucket cannot be determined, that entry will contain {@code null}. + */ + public synchronized List<MemberBucketSlot> getBucketSlots() { + initializeBucketsIfNecessary(); + + List<MemberBucketSlot> memberBucketSlots = new ArrayList<>(RegionProvider.REDIS_REGION_BUCKETS); + for (int bucketId = 0; bucketId < REDIS_REGION_BUCKETS; bucketId++) { + Pair<String, Integer> hostPort = getHostPort(bucketId); + if (hostPort != null) { + memberBucketSlots.add( + new MemberBucketSlot(bucketId, hostPort.getLeft(), hostPort.getRight())); + } + } + + return memberBucketSlots; + } + + private InternalDistributedMember getOrCreateMember(int bucketId) { + return dataRegion.getOrCreateNodeForBucketWrite(bucketId, null); + } + + private void initializeBucketsIfNecessary() { Review comment: Deleted! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org