Reduce number of redis requests by communities

This commit is contained in:
cnr 2016-12-26 22:34:09 -07:00
parent 73cefb69c9
commit 0b4d217303
4 changed files with 48 additions and 42 deletions

View File

@ -85,8 +85,6 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
private volatile boolean _cycling = false; private volatile boolean _cycling = false;
private volatile boolean _updateJoinRequests = false;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public CommunityManager(JavaPlugin plugin, CoreClientManager clientManager) public CommunityManager(JavaPlugin plugin, CoreClientManager clientManager)
{ {
@ -141,13 +139,8 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
LinkedList<Community> communities = new LinkedList<>(); LinkedList<Community> communities = new LinkedList<>();
_loadedCommunities.values().forEach(community -> communities.add(community)); _loadedCommunities.values().forEach(community -> communities.add(community));
_repo.updateMembers(communities); _repo.updateMembersAndJoinRequests(communities);
if (_updateJoinRequests) }, 0L, 20 * 10);
{
_repo.updateJoinRequests(communities);
}
_updateJoinRequests = !_updateJoinRequests;
}, 0L, 20 * 7);
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () ->
{ {

View File

@ -2,13 +2,13 @@ package mineplex.core.communities.storage;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -144,21 +144,31 @@ public class CommunityRepository extends MinecraftRepository
} }
} }
public void updateMembers(LinkedList<Community> communities) public void updateMembersAndJoinRequests(List<Community> communities)
{ {
if (communities.isEmpty()) if (communities.isEmpty())
{ {
return; return;
} }
TimingManager.start("member elements");
TimingManager.start("members + join requests");
Map<String, PlayerStatus> statuses = _repo.getElementsMap(
Stream.concat(
communities.stream().flatMap(community -> community.getMembers().keySet().stream()),
communities.stream().flatMap(community -> community.getJoinRequests().keySet().stream())
)
.distinct()
.map(UUID::toString)
.collect(Collectors.toList())
);
for (Community c : communities) for (Community c : communities)
{ {
List<CommunityMemberInfo> members = new ArrayList<>(c.getMembers().values()); // Update member player status
List<PlayerStatus> statuses = _repo.getElementsSequential(members.stream().map(info -> info.UUID.toString()).collect(Collectors.toList())); for (Map.Entry<UUID,CommunityMemberInfo> entry : c.getMembers().entrySet())
for (int i = 0; i < members.size(); i++)
{ {
CommunityMemberInfo info = members.get(i); CommunityMemberInfo info = entry.getValue();
PlayerStatus status = statuses.get(i); PlayerStatus status = statuses.get(entry.getKey().toString());
boolean online = false; boolean online = false;
String server = ""; String server = "";
@ -180,25 +190,13 @@ public class CommunityRepository extends MinecraftRepository
} }
} }
} }
}
TimingManager.stop("member elements");
}
public void updateJoinRequests(LinkedList<Community> communities) // Update join request names
{ for (Map.Entry<UUID,CommunityJoinRequestInfo> entry : c.getJoinRequests().entrySet())
if (communities.isEmpty())
{
return;
}
TimingManager.start("request elements");
for (Community c : communities)
{
List<CommunityJoinRequestInfo> requests = new ArrayList<>(c.getJoinRequests().values());
List<PlayerStatus> statuses = _repo.getElementsSequential(requests.stream().map(info -> info.UUID.toString()).collect(Collectors.toList()));
for (int i = 0; i < requests.size(); i++)
{ {
CommunityJoinRequestInfo info = requests.get(i);
PlayerStatus status = statuses.get(i); CommunityJoinRequestInfo info = entry.getValue();
PlayerStatus status = statuses.get(entry.getKey().toString());
if (status != null) if (status != null)
{ {
@ -209,6 +207,15 @@ public class CommunityRepository extends MinecraftRepository
} }
} }
} }
TimingManager.stop("members + join requests");
}
public void updateJoinRequests(LinkedList<Community> communities)
{
if (communities.isEmpty())
{
return;
}
TimingManager.stop("request elements"); TimingManager.stop("request elements");
} }

View File

@ -2,6 +2,7 @@ package mineplex.serverdata.data;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* DataRepository is used to store {@link Data} objects in a central database * DataRepository is used to store {@link Data} objects in a central database
@ -19,7 +20,7 @@ public interface DataRepository<T extends Data>
public Collection<T> getElements(Collection<String> dataIds); public Collection<T> getElements(Collection<String> dataIds);
public List<T> getElementsSequential(List<String> dataIds); public Map<String,T> getElementsMap(List<String> dataIds);
public void addElement(T element, int timeout); public void addElement(T element, int timeout);

View File

@ -2,8 +2,10 @@ package mineplex.serverdata.redis;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import mineplex.serverdata.Region; import mineplex.serverdata.Region;
@ -104,9 +106,9 @@ public class RedisDataRepository<T extends Data> extends RedisRepository impleme
} }
@Override @Override
public List<T> getElementsSequential(List<String> dataIds) public Map<String,T> getElementsMap(List<String> dataIds)
{ {
List<T> elements = new ArrayList<>(); Map<String,T> elements = new HashMap<>();
try(Jedis jedis = getResource(false)) try(Jedis jedis = getResource(false))
{ {
@ -121,12 +123,15 @@ public class RedisDataRepository<T extends Data> extends RedisRepository impleme
// Block until all requests have received pipelined responses // Block until all requests have received pipelined responses
pipeline.sync(); pipeline.sync();
for (Response<String> response : responses) for (int i = 0; i < responses.size(); i++)
{ {
String key = dataIds.get(i);
Response<String> response = responses.get(i);
String serializedData = response.get(); String serializedData = response.get();
T element = deserialize(serializedData); T element = deserialize(serializedData);
elements.add(element); elements.put(key, element);
} }
} }