Simplified connection attempts

This commit is contained in:
Rsl1122 2018-01-28 14:54:49 +02:00
parent b3ac110dfe
commit a55b17246a
10 changed files with 54 additions and 67 deletions

View File

@ -4,6 +4,7 @@
*/ */
package com.djrapitops.plan.system.info; package com.djrapitops.plan.system.info;
import com.djrapitops.plan.api.exceptions.connection.NoServersException;
import com.djrapitops.plan.api.exceptions.connection.WebException; import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.system.info.connection.BukkitConnectionSystem; import com.djrapitops.plan.system.info.connection.BukkitConnectionSystem;
import com.djrapitops.plan.system.info.request.CacheNetworkPageContentRequest; import com.djrapitops.plan.system.info.request.CacheNetworkPageContentRequest;
@ -26,7 +27,7 @@ public class BukkitInfoSystem extends InfoSystem {
@Override @Override
public void runLocally(InfoRequest infoRequest) throws WebException { public void runLocally(InfoRequest infoRequest) throws WebException {
if (infoRequest instanceof SetupRequest) { if (infoRequest instanceof SetupRequest) {
return; throw new NoServersException("Set-up requests can not be run locally.");
} }
infoRequest.runLocally(); infoRequest.runLocally();
} }

View File

@ -30,7 +30,7 @@ public class BungeeInfoSystem extends InfoSystem {
@Override @Override
public void runLocally(InfoRequest infoRequest) throws WebException { public void runLocally(InfoRequest infoRequest) throws WebException {
// runLocally is called when ConnectionSystem has no servers. // runLocally is called when ConnectionSystem has no servers.
throw new NoServersException("No servers were available to process this request."); throw new NoServersException("No servers were available to process this request (Local attempt): " + infoRequest.getClass().getSimpleName());
} }
@Override @Override

View File

@ -66,10 +66,16 @@ public abstract class InfoSystem implements SubSystem {
try { try {
if (!connectionSystem.isServerAvailable()) { if (!connectionSystem.isServerAvailable()) {
runLocally(infoRequest); runLocally(infoRequest);
return;
} }
connectionSystem.sendInfoRequest(infoRequest); connectionSystem.sendInfoRequest(infoRequest);
} catch (NoServersException e) { } catch (WebException original) {
try {
// Attempt to run locally.
runLocally(infoRequest); runLocally(infoRequest);
} catch (NoServersException e2) {
throw original;
}
} }
} }

View File

@ -17,8 +17,6 @@ import com.djrapitops.plan.system.webserver.WebServerSystem;
import com.djrapitops.plan.utilities.MiscUtils; import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@ -39,7 +37,7 @@ public class BukkitConnectionSystem extends ConnectionSystem {
} }
private void refreshServerMap() { private void refreshServerMap() {
if (latestServerMapRefresh < MiscUtils.getTime() - TimeAmount.MINUTE.ms() * 2L) { if (latestServerMapRefresh < MiscUtils.getTime() - TimeAmount.SECOND.ms() * 15L) {
try { try {
Database database = Database.getActive(); Database database = Database.getActive();
Optional<Server> bungeeInformation = database.fetch().getBungeeInformation(); Optional<Server> bungeeInformation = database.fetch().getBungeeInformation();
@ -57,7 +55,7 @@ public class BukkitConnectionSystem extends ConnectionSystem {
refreshServerMap(); refreshServerMap();
if (mainServer == null && bukkitServers.isEmpty()) { if (mainServer == null && bukkitServers.isEmpty()) {
throw new NoServersException("No Servers available to process requests."); throw new NoServersException("Zero servers available to process requests.");
} }
Server server = null; Server server = null;
@ -81,7 +79,7 @@ public class BukkitConnectionSystem extends ConnectionSystem {
@Override @Override
public void sendWideInfoRequest(WideRequest infoRequest) throws NoServersException { public void sendWideInfoRequest(WideRequest infoRequest) throws NoServersException {
if (bukkitServers.isEmpty()) { if (bukkitServers.isEmpty()) {
throw new NoServersException("No Servers Available to make process request."); throw new NoServersException("No Servers available to make wide-request: " + infoRequest.getClass().getSimpleName());
} }
for (Server server : bukkitServers.values()) { for (Server server : bukkitServers.values()) {
WebExceptionLogger.logIfOccurs(this.getClass(), () -> sendInfoRequest(infoRequest, server)); WebExceptionLogger.logIfOccurs(this.getClass(), () -> sendInfoRequest(infoRequest, server));
@ -90,7 +88,7 @@ public class BukkitConnectionSystem extends ConnectionSystem {
@Override @Override
public boolean isServerAvailable() { public boolean isServerAvailable() {
return ConnectionLog.hasConnectionSucceeded(mainServer); return mainServer != null && Settings.BUNGEE_OVERRIDE_STANDALONE_MODE.isFalse();
} }
@Override @Override
@ -102,12 +100,6 @@ public class BukkitConnectionSystem extends ConnectionSystem {
@Override @Override
public void enable() { public void enable() {
refreshServerMap(); refreshServerMap();
RunnableFactory.createNew("Server List Update Task", new AbsRunnable() {
@Override
public void run() {
refreshServerMap();
}
}).runTaskTimerAsynchronously(TimeAmount.SECOND.ticks() * 30L, TimeAmount.MINUTE.ticks() * 5L);
boolean usingBungeeWebServer = ConnectionSystem.getInstance().isServerAvailable(); boolean usingBungeeWebServer = ConnectionSystem.getInstance().isServerAvailable();
boolean usingAlternativeIP = Settings.SHOW_ALTERNATIVE_IP.isTrue(); boolean usingAlternativeIP = Settings.SHOW_ALTERNATIVE_IP.isTrue();

View File

@ -9,13 +9,10 @@ import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.info.request.*; import com.djrapitops.plan.system.info.request.*;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.webserver.WebServerSystem; import com.djrapitops.plan.system.webserver.WebServerSystem;
import com.djrapitops.plan.utilities.MiscUtils; import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
@ -34,7 +31,7 @@ public class BungeeConnectionSystem extends ConnectionSystem {
} }
private void refreshServerMap() { private void refreshServerMap() {
if (latestServerMapRefresh < MiscUtils.getTime() - TimeAmount.MINUTE.ms() * 2L) { if (latestServerMapRefresh < MiscUtils.getTime() - TimeAmount.SECOND.ms() * 15L) {
try { try {
bukkitServers = Database.getActive().fetch().getBukkitServers(); bukkitServers = Database.getActive().fetch().getBukkitServers();
latestServerMapRefresh = MiscUtils.getTime(); latestServerMapRefresh = MiscUtils.getTime();
@ -46,6 +43,7 @@ public class BungeeConnectionSystem extends ConnectionSystem {
@Override @Override
protected Server selectServerForRequest(InfoRequest infoRequest) throws NoServersException { protected Server selectServerForRequest(InfoRequest infoRequest) throws NoServersException {
refreshServerMap();
Server server = null; Server server = null;
if (infoRequest instanceof CacheRequest) { if (infoRequest instanceof CacheRequest) {
throw new NoServersException("Bungee should not send Cache requests."); throw new NoServersException("Bungee should not send Cache requests.");
@ -55,7 +53,7 @@ public class BungeeConnectionSystem extends ConnectionSystem {
} else if (infoRequest instanceof GenerateInspectPageRequest) { } else if (infoRequest instanceof GenerateInspectPageRequest) {
Optional<UUID> serverUUID = getServerWherePlayerIsOnline((GenerateInspectPageRequest) infoRequest); Optional<UUID> serverUUID = getServerWherePlayerIsOnline((GenerateInspectPageRequest) infoRequest);
if (serverUUID.isPresent()) { if (serverUUID.isPresent()) {
server = bukkitServers.getOrDefault(serverUUID.get(), ServerInfo.getServer()); server = bukkitServers.getOrDefault(serverUUID.get(), getOneBukkitServer());
} }
} }
if (server == null) { if (server == null) {
@ -64,10 +62,16 @@ public class BungeeConnectionSystem extends ConnectionSystem {
return server; return server;
} }
private Server getOneBukkitServer() {
Optional<Server> first = bukkitServers.values().stream().findAny();
return first.orElse(null);
}
@Override @Override
public void sendWideInfoRequest(WideRequest infoRequest) throws NoServersException { public void sendWideInfoRequest(WideRequest infoRequest) throws NoServersException {
refreshServerMap();
if (bukkitServers.isEmpty()) { if (bukkitServers.isEmpty()) {
throw new NoServersException("No Servers Available to make process request."); throw new NoServersException("No Servers available to make wide-request: " + infoRequest.getClass().getSimpleName());
} }
for (Server server : bukkitServers.values()) { for (Server server : bukkitServers.values()) {
WebExceptionLogger.logIfOccurs(this.getClass(), () -> sendInfoRequest(infoRequest, server)); WebExceptionLogger.logIfOccurs(this.getClass(), () -> sendInfoRequest(infoRequest, server));
@ -87,12 +91,6 @@ public class BungeeConnectionSystem extends ConnectionSystem {
@Override @Override
public void enable() { public void enable() {
refreshServerMap(); refreshServerMap();
RunnableFactory.createNew("Server List Update Task", new AbsRunnable() {
@Override
public void run() {
refreshServerMap();
}
}).runTaskTimerAsynchronously(TimeAmount.SECOND.ticks() * 30L, TimeAmount.MINUTE.ticks() * 5L);
} }
@Override @Override

View File

@ -7,7 +7,6 @@ package com.djrapitops.plan.system.info.connection;
import com.djrapitops.plan.system.info.request.InfoRequest; import com.djrapitops.plan.system.info.request.InfoRequest;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.utilities.MiscUtils; import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plugin.utilities.Verify;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -37,35 +36,6 @@ public class ConnectionLog {
log.put(toServer, requestMap); log.put(toServer, requestMap);
} }
public static boolean isConnectionPlausible(Server server) {
if (server == null) {
return false;
}
Map<String, Entry> serverLog = getInstance().getLog().get(server);
return Verify.isEmpty(serverLog) || hasConnectionSucceeded(serverLog);
}
public static boolean hasConnectionSucceeded(Server server) {
if (server == null) {
return false;
}
Map<String, Entry> serverLog = getInstance().getLog().get(server);
return !Verify.isEmpty(serverLog)
&& hasConnectionSucceeded(serverLog);
}
private static boolean hasConnectionSucceeded(Map<String, Entry> serverLog) {
for (Entry entry : serverLog.values()) {
if (entry.responseCode == 200) {
return true;
}
}
return false;
}
private static ConnectionLog getInstance() { private static ConnectionLog getInstance() {
return ConnectionSystem.getInstance().getConnectionLog(); return ConnectionSystem.getInstance().getConnectionLog();
} }

View File

@ -17,10 +17,7 @@ import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.utilities.NullCheck; import com.djrapitops.plan.utilities.NullCheck;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
import java.util.HashMap; import java.util.*;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
/** /**
* ConnectionSystem manages out- and inbound InfoRequest connections. * ConnectionSystem manages out- and inbound InfoRequest connections.
@ -121,4 +118,8 @@ public abstract class ConnectionSystem implements SubSystem {
putRequest(requests, SendDBSettingsRequest.createHandler()); putRequest(requests, SendDBSettingsRequest.createHandler());
return requests; return requests;
} }
public List<Server> getBukkitServers() {
return new ArrayList<>(bukkitServers.values());
}
} }

View File

@ -87,10 +87,10 @@ public class ResponseHandler extends TreePageHandler {
} }
try { try {
return getResponse(request, targetString, target); return getResponse(request, targetString, target);
} catch (NoServersException | NotFoundException e) {
return new NotFoundResponse(e.getMessage());
} catch (WebUserAuthException e) { } catch (WebUserAuthException e) {
return PromptAuthorizationResponse.getBasicAuthResponse(e); return PromptAuthorizationResponse.getBasicAuthResponse(e);
} catch (NotFoundException e) {
return new NotFoundResponse(e.getMessage());
} catch (ForbiddenException e) { } catch (ForbiddenException e) {
return new ForbiddenResponse(e.getMessage()); return new ForbiddenResponse(e.getMessage());
} catch (BadRequestException e) { } catch (BadRequestException e) {

View File

@ -2,11 +2,14 @@ package com.djrapitops.plan.system.webserver.response.pages;
import com.djrapitops.plan.api.exceptions.connection.WebException; import com.djrapitops.plan.api.exceptions.connection.WebException;
import com.djrapitops.plan.system.info.InfoSystem; import com.djrapitops.plan.system.info.InfoSystem;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.processing.Processor; import com.djrapitops.plan.system.processing.Processor;
import com.djrapitops.plan.system.webserver.response.Response; import com.djrapitops.plan.system.webserver.response.Response;
import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse; import com.djrapitops.plan.system.webserver.response.errors.ErrorResponse;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
import java.util.UUID;
/** /**
* @author Rsl1122 * @author Rsl1122
* @since 3.5.2 * @since 3.5.2
@ -14,9 +17,13 @@ import com.djrapitops.plugin.api.utility.log.Log;
public class AnalysisPageResponse extends Response { public class AnalysisPageResponse extends Response {
public static AnalysisPageResponse refreshNow() { public static AnalysisPageResponse refreshNow() {
return refreshNow(ServerInfo.getServerUUID());
}
public static AnalysisPageResponse refreshNow(UUID serverUUID) {
Processor.queue(() -> { Processor.queue(() -> {
try { try {
InfoSystem.getInstance().generateAnalysisPageOfThisServer(); InfoSystem.getInstance().generateAnalysisPage(serverUUID);
} catch (WebException e) { } catch (WebException e) {
// TODO Exception handling // TODO Exception handling
Log.toLog(AnalysisPageResponse.class, e); Log.toLog(AnalysisPageResponse.class, e);

View File

@ -8,6 +8,7 @@ import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.database.databases.sql.SQLDB; import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.info.connection.ConnectionLog; import com.djrapitops.plan.system.info.connection.ConnectionLog;
import com.djrapitops.plan.system.info.connection.ConnectionSystem;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.info.server.ServerProperties; import com.djrapitops.plan.system.info.server.ServerProperties;
@ -66,8 +67,8 @@ public class DebugPageResponse extends ErrorResponse {
try { try {
Map<Server, Map<String, ConnectionLog.Entry>> logEntries = ConnectionLog.getLogEntries(); Map<Server, Map<String, ConnectionLog.Entry>> logEntries = ConnectionLog.getLogEntries();
content.append("<pre>"); content.append("<pre>**Connection Log:**<br>");
content.append("Server Address | Request Type | Sent | Response<br>") content.append("Server Address | Request Type | Response | Sent<br>")
.append("-- | -- | -- | --<br>"); .append("-- | -- | -- | --<br>");
for (Map.Entry<Server, Map<String, ConnectionLog.Entry>> entry : logEntries.entrySet()) { for (Map.Entry<Server, Map<String, ConnectionLog.Entry>> entry : logEntries.entrySet()) {
Server server = entry.getKey(); Server server = entry.getKey();
@ -85,6 +86,17 @@ public class DebugPageResponse extends ErrorResponse {
} }
content.append("</pre>"); content.append("</pre>");
content.append("<pre>**Servers:**<br>");
List<Server> servers = ConnectionSystem.getInstance().getBukkitServers();
content.append("Server Name | Address | UUID <br>")
.append("-- | -- | --<br>");
for (Server server : servers) {
content.append(server.getName()).append(" | ")
.append(server.getWebAddress()).append(" | ")
.append(server.getUuid()).append(" | ").append("<br>");
}
content.append("</pre>");
} catch (Exception e) { } catch (Exception e) {
Log.toLog(this.getClass(), e); Log.toLog(this.getClass(), e);
} }