From 73cefb69c94d755399dc6ce89198ba432346c236 Mon Sep 17 00:00:00 2001 From: cnr Date: Fri, 23 Dec 2016 22:28:27 -0700 Subject: [PATCH] Use redis pipeline for community player statuses --- .../storage/CommunityRepository.java | 28 ++++++++++------ .../serverdata/data/DataRepository.java | 3 ++ .../serverdata/redis/RedisDataRepository.java | 32 ++++++++++++++++++- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index c8087bf32..8c86d5b86 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -2,14 +2,17 @@ package mineplex.core.communities.storage; import java.sql.Connection; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.stream.Collectors; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.common.timing.TimingManager; import mineplex.core.common.util.Callback; import mineplex.core.communities.Community; import mineplex.core.communities.CommunityJoinRequestInfo; @@ -91,11 +94,6 @@ public class CommunityRepository extends MinecraftRepository boolean readingChat = memberSet.getBoolean("readingChat"); CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); - PlayerStatus status = _repo.getElement(name.toLowerCase()); - if (status != null) - { - info.update(lastLogin, true, status.getServer()); - } info.ReadingChat = readingChat; Community community = communityMap.get(communityId); @@ -152,11 +150,16 @@ public class CommunityRepository extends MinecraftRepository { return; } + TimingManager.start("member elements"); for (Community c : communities) { - for (CommunityMemberInfo info : c.getMembers().values()) + List members = new ArrayList<>(c.getMembers().values()); + List statuses = _repo.getElementsSequential(members.stream().map(info -> info.UUID.toString()).collect(Collectors.toList())); + for (int i = 0; i < members.size(); i++) { - PlayerStatus status = _repo.getElement(info.UUID.toString()); + CommunityMemberInfo info = members.get(i); + PlayerStatus status = statuses.get(i); + boolean online = false; String server = ""; if (status != null) @@ -178,6 +181,7 @@ public class CommunityRepository extends MinecraftRepository } } } + TimingManager.stop("member elements"); } public void updateJoinRequests(LinkedList communities) @@ -186,11 +190,16 @@ public class CommunityRepository extends MinecraftRepository { return; } + TimingManager.start("request elements"); for (Community c : communities) { - for (CommunityJoinRequestInfo info : c.getJoinRequests().values()) + List requests = new ArrayList<>(c.getJoinRequests().values()); + List statuses = _repo.getElementsSequential(requests.stream().map(info -> info.UUID.toString()).collect(Collectors.toList())); + for (int i = 0; i < requests.size(); i++) { - PlayerStatus status = _repo.getElement(info.UUID.toString()); + CommunityJoinRequestInfo info = requests.get(i); + PlayerStatus status = statuses.get(i); + if (status != null) { if (!info.Name.equals(status.getName())) @@ -200,6 +209,7 @@ public class CommunityRepository extends MinecraftRepository } } } + TimingManager.stop("request elements"); } public void loadInvites(int accountId, List invites) diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/DataRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/DataRepository.java index fb9f9e7c4..cdac8d117 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/DataRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/DataRepository.java @@ -1,6 +1,7 @@ package mineplex.serverdata.data; import java.util.Collection; +import java.util.List; /** * DataRepository is used to store {@link Data} objects in a central database @@ -18,6 +19,8 @@ public interface DataRepository public Collection getElements(Collection dataIds); + public List getElementsSequential(List dataIds); + public void addElement(T element, int timeout); public void addElement(T element); diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java index 5ccfd291a..46e74fc6b 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java @@ -102,7 +102,37 @@ public class RedisDataRepository extends RedisRepository impleme return elements; } - + + @Override + public List getElementsSequential(List dataIds) + { + List elements = new ArrayList<>(); + + try(Jedis jedis = getResource(false)) + { + Pipeline pipeline = jedis.pipelined(); + + List> responses = new ArrayList<>(); + for (String dataId : dataIds) + { + responses.add(pipeline.get(generateKey(dataId))); + } + + // Block until all requests have received pipelined responses + pipeline.sync(); + + for (Response response : responses) + { + String serializedData = response.get(); + T element = deserialize(serializedData); + + elements.add(element); + } + } + + return elements; + } + @Override public T getElement(String dataId) {