Make GET /dumps' background thread use the Vert.x scheduler instead of being a standalone thread

This commit is contained in:
Colin McDonald 2016-06-23 00:59:18 -04:00
parent 43b59dcf2c
commit 66b2fc5951
11 changed files with 74 additions and 84 deletions

View File

@ -4,7 +4,7 @@ import com.google.common.collect.ImmutableList;
import io.vertx.core.json.JsonObject;
import lombok.Getter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public final class MaxMindResult {
@ -29,7 +29,7 @@ public final class MaxMindResult {
this.country = new MaxMindCountry(legacy.getJsonObject("country"));
this.registeredCountry = new MaxMindRegisteredCountry(legacy.getJsonObject("registered_country"));
List<MaxMindSubdivision> subdivisions = new ArrayList<>();
List<MaxMindSubdivision> subdivisions = new LinkedList<>();
for (Object subdivision : legacy.getJsonArray("subdivisions")) {
subdivisions.add(new MaxMindSubdivision((JsonObject) subdivision));

View File

@ -14,7 +14,7 @@ import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -34,11 +34,11 @@ public final class AuditLogEntry {
@Getter private Map<String, Object> metadata;
public static void findPaginated(Document query, int skip, int pageSize, SingleResultCallback<List<AuditLogEntry>> callback) {
auditLogCollection.find().sort(new Document("performedAt", -1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback);
auditLogCollection.find(query).sort(new Document("performedAt", -1)).skip(skip).limit(pageSize).into(new LinkedList<>(), callback);
}
public static void find(Document query, SingleResultCallback<List<AuditLogEntry>> callback) {
auditLogCollection.find(query).into(new ArrayList<>(), callback);
auditLogCollection.find(query).into(new LinkedList<>(), callback);
}
public AuditLogEntry() {} // For Jackson

View File

@ -12,7 +12,7 @@ import net.frozenorb.apiv3.util.MaxMindUtils;
import org.bson.Document;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@Entity
@ -26,7 +26,7 @@ public final class IpIntel {
@Getter private MaxMindResult result;
public static void findAll(SingleResultCallback<List<IpIntel>> callback) {
ipIntelCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback);
ipIntelCollection.find().sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), callback);
}
public static void findById(String id, SingleResultCallback<IpIntel> callback) {

View File

@ -14,7 +14,7 @@ import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
@ -56,7 +56,7 @@ public final class IpLogEntry {
}
public static void findAll(SingleResultCallback<List<IpLogEntry>> callback) {
ipLogCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback);
ipLogCollection.find().sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), callback);
}
public static void findById(String id, SingleResultCallback<IpLogEntry> callback) {
@ -68,7 +68,7 @@ public final class IpLogEntry {
}
public static void findByUser(UUID user, SingleResultCallback<List<IpLogEntry>> callback) {
ipLogCollection.find(new Document("user", user)).sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback);
ipLogCollection.find(new Document("user", user)).sort(new Document("lastSeenAt", -1)).into(new LinkedList<>(), callback);
}
public static void findByUserAndIp(User user, String userIp, SingleResultCallback<IpLogEntry> callback) {

View File

@ -12,7 +12,7 @@ import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.SyncUtils;
import org.bson.Document;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -30,7 +30,7 @@ public final class NotificationTemplate {
}
public static void findAll(SingleResultCallback<List<NotificationTemplate>> callback) {
notificationTemplatesCollection.find().into(new ArrayList<>(), callback);
notificationTemplatesCollection.find().into(new LinkedList<>(), callback);
}
public static void findById(String id, SingleResultCallback<NotificationTemplate> callback) {

View File

@ -12,8 +12,8 @@ import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.SyncUtils;
import org.bson.Document;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -58,7 +58,7 @@ public final class Rank {
private static void updateCacheIfNeeded() {
if (rankCache == null || (System.currentTimeMillis() - rankCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
Map<String, Rank> working = new HashMap<>();
List<Rank> workingAlt = new ArrayList<>();
List<Rank> workingAlt = new LinkedList<>();
List<Rank> allRanks = SyncUtils.blockMulti(ranksCollection.find());
allRanks.sort((a, b) -> Ints.compare(a.getWeight(), b.getWeight()));

View File

@ -61,7 +61,7 @@ public final class Server {
private static void updateCacheIfNeeded() {
if (serverCache == null || (System.currentTimeMillis() - serverCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
Map<String, Server> working = new HashMap<>();
List<Server> workingAlt = new ArrayList<>();
List<Server> workingAlt = new LinkedList<>();
for (Server server : SyncUtils.blockMulti(serversCollection.find())) {
working.put(server.getId(), server);

View File

@ -57,7 +57,7 @@ public final class ServerGroup {
private static void updateCacheIfNeeded() {
if (serverGroupCache == null || (System.currentTimeMillis() - serverGroupCacheUpdated) > TimeUnit.MINUTES.toMillis(1)) {
Map<String, ServerGroup> working = new HashMap<>();
List<ServerGroup> workingAlt = new ArrayList<>();
List<ServerGroup> workingAlt = new LinkedList<>();
for (ServerGroup serverGroup : SyncUtils.blockMulti(serverGroupsCollection.find())) {
working.put(serverGroup.getId(), serverGroup);

View File

@ -99,7 +99,7 @@ public final class User {
}
public static void findByIdGrouped(Iterable<UUID> search, SingleResultCallback<Map<UUID, User>> callback) {
usersCollection.find(new Document("_id", new Document("$in", search))).into(new ArrayList<>(), (users, error) -> {
usersCollection.find(new Document("_id", new Document("$in", search))).into(new LinkedList<>(), (users, error) -> {
if (error != null) {
callback.onResult(null, error);
} else {

View File

@ -13,76 +13,66 @@ import java.util.concurrent.TimeUnit;
public final class GETDumps implements Handler<RoutingContext> {
private List<UUID> banCache = new ArrayList<>();
private List<UUID> blacklistCache = new ArrayList<>();
private List<UUID> banCache = new LinkedList<>();
private List<UUID> blacklistCache = new LinkedList<>();
private Map<String, List<UUID>> grantCache = new HashMap<>();
public GETDumps() {
// TODO: Use vertx scheduler
Thread dumpUpdater = new Thread() {
@Override
public void run() {
int tick = 0;
while (true) {
if (tick == 0 || tick % 2 != 0) {
List<UUID> banCache = new ArrayList<>();
List<UUID> blacklistCache = new ArrayList<>();
Punishment.findByTypeSync(ImmutableSet.of(
Punishment.PunishmentType.BAN,
Punishment.PunishmentType.BLACKLIST
)).forEach((punishment) -> {
if (!punishment.isActive()) {
return;
}
if (punishment.getType() == Punishment.PunishmentType.BAN) {
banCache.add(punishment.getUser());
} else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
blacklistCache.add(punishment.getUser());
}
});
GETDumps.this.banCache = banCache;
GETDumps.this.blacklistCache = blacklistCache;
}
if (tick == 0 || tick % 2 == 0) {
Map<String, List<UUID>> grantCache = new HashMap<>();
Grant.findAllSync().forEach((grant) -> {
if (grant.isActive()) {
List<UUID> users = grantCache.get(grant.getRank());
if (users == null) {
users = new ArrayList<>();
grantCache.put(grant.getRank(), users);
}
users.add(grant.getUser());
}
});
GETDumps.this.grantCache = grantCache;
}
try {
Thread.sleep(TimeUnit.MINUTES.toMillis(3));
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
tick++;
APIv3.getVertxInstance().setPeriodic(TimeUnit.MINUTES.toMillis(5), (id) -> {
Punishment.findByType(ImmutableSet.of(
Punishment.PunishmentType.BAN,
Punishment.PunishmentType.BLACKLIST
), (punishments, error) -> {
if (error != null) {
error.printStackTrace();
return;
}
}
};
List<UUID> banCache = new LinkedList<>();
List<UUID> blacklistCache = new LinkedList<>();
dumpUpdater.setName("APIv3 - Dump Updater");
dumpUpdater.setDaemon(true);
dumpUpdater.start();
for (Punishment punishment : punishments) {
if (!punishment.isActive()) {
return;
}
if (punishment.getType() == Punishment.PunishmentType.BAN) {
banCache.add(punishment.getUser());
} else if (punishment.getType() == Punishment.PunishmentType.BLACKLIST) {
blacklistCache.add(punishment.getUser());
}
}
GETDumps.this.banCache = banCache;
GETDumps.this.blacklistCache = blacklistCache;
});
Grant.findAll((grants, error) -> {
if (error != null) {
error.printStackTrace();
return;
}
Map<String, List<UUID>> grantCache = new HashMap<>();
for (Grant grant : grants) {
if (!grant.isActive()) {
return;
}
List<UUID> users = grantCache.get(grant.getRank());
if (users == null) {
users = new LinkedList<>();
grantCache.put(grant.getRank(), users);
}
users.add(grant.getUser());
}
GETDumps.this.grantCache = grantCache;
});
});
}
public void handle(RoutingContext ctx) {
@ -96,7 +86,7 @@ public final class GETDumps implements Handler<RoutingContext> {
APIv3.respondJson(ctx, blacklistCache);
return;
case "accessdeniable": // Lowercase d because we convert to lowercase above
List<UUID> result = new ArrayList<>();
List<UUID> result = new LinkedList<>();
result.addAll(banCache);
result.addAll(blacklistCache);

View File

@ -4,7 +4,7 @@ import com.mongodb.async.client.MongoIterable;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@UtilityClass
@ -20,7 +20,7 @@ public class SyncUtils {
public static <T> List<T> blockMulti(MongoIterable<T> mongoIterable) {
BlockingCallback<List<T>> callback = new BlockingCallback<>();
mongoIterable.into(new ArrayList<>(), callback);
mongoIterable.into(new LinkedList<>(), callback);
return callback.get();
}