refactoring

pull/5540/merge
Nikita Koksharov 1 year ago
parent 8bd0dece25
commit 1b8d4003b4

@ -245,7 +245,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
}
@Override
protected MasterSlaveEntry getEntry(int slot) {
public MasterSlaveEntry getEntry(int slot) {
lazyConnect();
return slot2entry.get(slot);

@ -50,6 +50,8 @@ public interface ConnectionManager {
MasterSlaveEntry getEntry(String name);
MasterSlaveEntry getEntry(int slot);
MasterSlaveEntry getWriteEntry(int slot);
MasterSlaveEntry getReadEntry(int slot);

@ -458,7 +458,7 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
return getEntry(slot);
}
protected MasterSlaveEntry getEntry(int slot) {
public MasterSlaveEntry getEntry(int slot) {
lazyConnect();
return masterSlaveEntry;

@ -24,6 +24,7 @@ import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.pubsub.PubSubStatusMessage;
import org.redisson.client.protocol.pubsub.PubSubType;
import org.redisson.connection.ConnectionManager;
import org.redisson.connection.MasterSlaveEntry;
import org.redisson.connection.ServiceManager;
import org.redisson.misc.AsyncSemaphore;
import org.redisson.misc.WrappedLock;
@ -54,6 +55,7 @@ public class PubSubConnectionEntry {
private final ServiceManager serviceManager;
private final PublishSubscribeService subscribeService;
private final MasterSlaveEntry entry;
private static final Map<PubSubType, PubSubType> SUBSCRIBE2UNSUBSCRIBE = new HashMap<>();
@ -63,14 +65,19 @@ public class PubSubConnectionEntry {
SUBSCRIBE2UNSUBSCRIBE.put(PubSubType.PSUBSCRIBE, PubSubType.PUNSUBSCRIBE);
}
public PubSubConnectionEntry(RedisPubSubConnection conn, ConnectionManager connectionManager) {
public PubSubConnectionEntry(RedisPubSubConnection conn, ConnectionManager connectionManager, MasterSlaveEntry entry) {
super();
this.conn = conn;
this.entry = entry;
this.serviceManager = connectionManager.getServiceManager();
this.subscribeService = connectionManager.getSubscribeService();
this.subscribedChannelsAmount = new AtomicInteger(serviceManager.getConfig().getSubscriptionsPerConnection());
}
public MasterSlaveEntry getEntry() {
return entry;
}
public int countListeners(ChannelName channelName) {
return channelListeners.getOrDefault(channelName, EMPTY_QUEUE).size();
}
@ -127,7 +134,7 @@ public class PubSubConnectionEntry {
}
public boolean removeListener(ChannelName channelName, int listenerId) {
Queue<RedisPubSubListener<?>> listeners = channelListeners.get(channelName);
Queue<RedisPubSubListener<?>> listeners = channelListeners.getOrDefault(channelName, EMPTY_QUEUE);
for (RedisPubSubListener<?> listener : listeners) {
if (System.identityHashCode(listener) == listenerId) {
removeListener(channelName, listener);
@ -176,7 +183,7 @@ public class PubSubConnectionEntry {
pp.whenComplete((r, e) -> {
if (e != null) {
PubSubType unsubscribeType = SUBSCRIBE2UNSUBSCRIBE.get(type);
CompletableFuture<Codec> f = subscribeService.unsubscribe(channelName, unsubscribeType);
CompletableFuture<Codec> f = subscribeService.unsubscribe(channelName, entry, unsubscribeType);
f.whenComplete((rr, ee) -> {
pm.completeExceptionally(e);
});
@ -304,7 +311,7 @@ public class PubSubConnectionEntry {
removeListener(channelName, listener);
}
if (!hasListeners(channelName)) {
subscribeService.unsubscribeLocked(type, channelName)
subscribeService.unsubscribeLocked(type, channelName, entry)
.whenComplete((r, ex) -> {
lock.release();
});

@ -440,7 +440,7 @@ public class PublishSubscribeService {
connFuture.thenAccept(conn -> {
freePubSubLock.acquire().thenAccept(c -> {
PubSubConnectionEntry entry = new PubSubConnectionEntry(conn, connectionManager);
PubSubConnectionEntry entry = new PubSubConnectionEntry(conn, connectionManager, msEntry);
int remainFreeAmount = entry.tryAcquire();
PubSubKey key = new PubSubKey(channelName, msEntry);
@ -468,7 +468,7 @@ public class PublishSubscribeService {
});
}
public CompletableFuture<Void> unsubscribeLocked(ChannelName channelName) {
CompletableFuture<Void> unsubscribeLocked(ChannelName channelName) {
PubSubType type = PubSubType.UNSUBSCRIBE;
if (shardingSupported) {
type = PubSubType.SUNSUBSCRIBE;
@ -477,7 +477,7 @@ public class PublishSubscribeService {
return unsubscribeLocked(type, channelName);
}
public CompletableFuture<Void> unsubscribeLocked(PubSubType topicType, ChannelName channelName) {
private CompletableFuture<Void> unsubscribeLocked(PubSubType topicType, ChannelName channelName) {
Collection<MasterSlaveEntry> coll = name2entry.get(channelName);
if (coll == null || coll.isEmpty()) {
RedisNodeNotFoundException ex = new RedisNodeNotFoundException("Node for name: " + channelName + " hasn't been discovered yet. Check cluster slots coverage using CLUSTER NODES command. Increase value of retryAttempts and/or retryInterval settings.");
@ -489,7 +489,7 @@ public class PublishSubscribeService {
return unsubscribeLocked(topicType, channelName, coll.iterator().next());
}
private CompletableFuture<Void> unsubscribeLocked(PubSubType topicType, ChannelName channelName, MasterSlaveEntry msEntry) {
CompletableFuture<Void> unsubscribeLocked(PubSubType topicType, ChannelName channelName, MasterSlaveEntry msEntry) {
PubSubConnectionEntry entry = name2PubSubConnection.remove(new PubSubKey(channelName, msEntry));
if (entry == null || connectionManager.getServiceManager().isShuttingDown()) {
return CompletableFuture.completedFuture(null);
@ -555,22 +555,13 @@ public class PublishSubscribeService {
public void remove(MasterSlaveEntry entry) {
entry2PubSubConnection.remove(entry);
name2entry.values().forEach(v -> v.remove(entry));
}
public CompletableFuture<Codec> unsubscribe(ChannelName channelName, PubSubType topicType) {
Collection<MasterSlaveEntry> coll = name2entry.get(channelName);
if (coll == null || coll.isEmpty()) {
RedisNodeNotFoundException ex = new RedisNodeNotFoundException("Node for name: " + channelName + " hasn't been discovered yet. Check cluster slots coverage using CLUSTER NODES command. Increase value of retryAttempts and/or retryInterval settings.");
CompletableFuture<Codec> promise = new CompletableFuture<>();
promise.completeExceptionally(ex);
return promise;
}
return unsubscribe(channelName, coll.iterator().next(), topicType);
name2entry.values().removeIf(v -> {
v.remove(entry);
return v.isEmpty();
});
}
private CompletableFuture<Codec> unsubscribe(ChannelName channelName, MasterSlaveEntry e, PubSubType topicType) {
public CompletableFuture<Codec> unsubscribe(ChannelName channelName, MasterSlaveEntry e, PubSubType topicType) {
if (connectionManager.getServiceManager().isShuttingDown()) {
return CompletableFuture.completedFuture(null);
}
@ -620,7 +611,7 @@ public class PublishSubscribeService {
public void reattachPubSub(int slot) {
name2PubSubConnection.entrySet().stream()
.filter(e -> connectionManager.calcSlot(e.getKey().getChannelName().getName()) == slot)
.filter(e -> e.getValue().getEntry().equals(connectionManager.getEntry(slot)))
.forEach(entry -> {
PubSubConnectionEntry pubSubEntry = entry.getValue();
MasterSlaveEntry ee = entry.getKey().getEntry();
@ -842,7 +833,7 @@ public class PublishSubscribeService {
}
if (entry.hasListeners(channelName)) {
CompletableFuture<Void> ff = unsubscribeLocked(type, channelName);
CompletableFuture<Void> ff = unsubscribeLocked(type, channelName, entry.getEntry());
return ff.whenComplete((r1, e1) -> {
semaphore.release();
});

Loading…
Cancel
Save