Skip to content

Commit

Permalink
Register/unregister player graph samplers on join/leave
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraLS3 committed Feb 22, 2025
1 parent a198d42 commit 62f7191
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.djrapitops.plan.component.ComponentSvc;
import com.djrapitops.plan.extension.builder.ExtensionDataBuilder;
import com.djrapitops.plan.extension.implementation.CallerImplementation;
import com.djrapitops.plan.extension.implementation.ExtensionMethodErrorTracker;
import com.djrapitops.plan.extension.implementation.ExtensionRegister;
import com.djrapitops.plan.extension.implementation.ExtensionWrapper;
import com.djrapitops.plan.extension.implementation.builder.ExtDataBuilder;
Expand Down Expand Up @@ -168,6 +169,11 @@ private boolean shouldNotAllowRegistration(String pluginName) {
public void updatePlayerValues(UUID playerUUID, String playerName, CallEvents event) {
if (!enabled.get()) return; // Plugin is disabling
for (DataValueGatherer gatherer : extensionGatherers.values()) {
if (event == CallEvents.PLAYER_JOIN) {
graphSamplers.registerPlayerGraphSamplers(gatherer.getExtension(), playerUUID, playerName);
} else if (event == CallEvents.PLAYER_LEAVE) {
graphSamplers.unregisterPlayerSamplers(playerUUID);
}
updatePlayerValues(gatherer, playerUUID, playerName, event);
}
}
Expand Down Expand Up @@ -203,5 +209,6 @@ public void updateServerValues(DataValueGatherer gatherer, CallEvents event) {

public void disableUpdates() {
enabled.set(false);
ExtensionMethodErrorTracker.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation;

import com.djrapitops.plan.extension.extractor.ExtensionMethod;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* In charge of tracking which extension methods got disabled due to an error.
*
* @author AuroraLS3
*/
public class ExtensionMethodErrorTracker {

private static final Set<String> disabledExtensionPlayerGraphMethods = Collections.newSetFromMap(new ConcurrentHashMap<>());

private ExtensionMethodErrorTracker() {
/* Static storage class. */
}

public static void clear() {
disabledExtensionPlayerGraphMethods.clear();
}

public static boolean isDisabled(ExtensionWrapper extension, ExtensionMethod method) {
String name = extension.getPluginName() + "." + method.getMethodName();
return disabledExtensionPlayerGraphMethods.contains(name);
}

public static void errored(ExtensionWrapper extension, ExtensionMethod method) {
String name = extension.getPluginName() + "." + method.getMethodName();
disabledExtensionPlayerGraphMethods.add(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public DataValueGatherer(
this.brokenMethods = new HashSet<>();
}

public ExtensionWrapper getExtension() {
return extension;
}

public boolean shouldSkipEvent(CallEvents event) {
if (event == CallEvents.MANUAL) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* This is a utility class for managing extension graph sampling.
Expand All @@ -51,6 +51,8 @@ public class GraphSamplers {
private final RunnableFactory runnableFactory;
private final ErrorLogger errorLogger;

private final Map<UUID, Set<PlayerGraphSampler>> activePlayerGraphSamplers = new ConcurrentHashMap<>();

@Inject
public GraphSamplers(ServerInfo serverInfo, DBSystem dbSystem, RunnableFactory runnableFactory, ErrorLogger errorLogger) {
this.serverInfo = serverInfo;
Expand Down Expand Up @@ -97,4 +99,30 @@ public void storePlayerGraphMetadata(ExtensionWrapper extension) {
storeGraphMetadata(extension, graphPointProvider);
}
}

public void registerPlayerGraphSamplers(ExtensionWrapper extension, UUID playerUUID, String playerName) {
Parameters parameters = Parameters.player(serverInfo.getServerUUID(), playerUUID, playerName);
Map<ExtensionMethod.ParameterType, ExtensionMethods> methods = extension.getMethods();
for (ExtensionMethod graphPointProvider : methods.get(ExtensionMethod.ParameterType.PLAYER_UUID).getGraphPointProviders()) {
PlayerGraphSampler sampler = new PlayerGraphSampler(extension, graphPointProvider, dbSystem, parameters,
new ProviderIdentifier(serverInfo.getServerUUID(), extension.getPluginName(), graphPointProvider.getMethodName()), errorLogger);
activePlayerGraphSamplers.computeIfAbsent(playerUUID, u -> Collections.newSetFromMap(new ConcurrentHashMap<>()))
.add(sampler);
sampler.register(runnableFactory);
}
for (ExtensionMethod graphPointProvider : methods.get(ExtensionMethod.ParameterType.PLAYER_STRING).getGraphPointProviders()) {
PlayerGraphSampler sampler = new PlayerGraphSampler(extension, graphPointProvider, dbSystem, parameters,
new ProviderIdentifier(serverInfo.getServerUUID(), extension.getPluginName(), graphPointProvider.getMethodName()), errorLogger);
activePlayerGraphSamplers.computeIfAbsent(playerUUID, u -> Collections.newSetFromMap(new ConcurrentHashMap<>()))
.add(sampler);
sampler.register(runnableFactory);
}
}

public void unregisterPlayerSamplers(UUID playerUUID) {
Set<PlayerGraphSampler> samplers = activePlayerGraphSamplers.getOrDefault(playerUUID, Set.of());
samplers.forEach(PlayerGraphSampler::unregister);
samplers.clear();
activePlayerGraphSamplers.remove(playerUUID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.djrapitops.plan.extension.annotation.GraphPointProvider;
import com.djrapitops.plan.extension.extractor.ExtensionMethod;
import com.djrapitops.plan.extension.graph.DataPoint;
import com.djrapitops.plan.extension.implementation.ExtensionMethodErrorTracker;
import com.djrapitops.plan.extension.implementation.ExtensionWrapper;
import com.djrapitops.plan.extension.implementation.providers.MethodWrapper;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
Expand All @@ -40,11 +41,11 @@ public class PlayerGraphSampler extends TaskSystem.Task {
private final DBSystem dbSystem;
private final ExtensionMethod provider;
private final GraphPointProvider annotation;
private final Parameters.PlayerParameters parameters;
private final Parameters parameters;
private final ProviderIdentifier providerIdentifier;
private final ErrorLogger errorLogger;

public PlayerGraphSampler(ExtensionWrapper extension, ExtensionMethod provider, DBSystem dbSystem, Parameters.PlayerParameters parameters, ProviderIdentifier providerIdentifier, ErrorLogger errorLogger) {
public PlayerGraphSampler(ExtensionWrapper extension, ExtensionMethod provider, DBSystem dbSystem, Parameters parameters, ProviderIdentifier providerIdentifier, ErrorLogger errorLogger) {
this.extension = extension;
this.provider = provider;
annotation = provider.getExistingAnnotation(GraphPointProvider.class);
Expand All @@ -61,6 +62,8 @@ private DataPoint callMethod() {

@Override
public void register(RunnableFactory runnableFactory) {
if (ExtensionMethodErrorTracker.isDisabled(extension, provider)) return;

runnableFactory.create(this)
.runTaskTimerAsynchronously(0, annotation.sampleInterval(), annotation.sampleIntervalUnit());
}
Expand All @@ -80,7 +83,7 @@ public void run() {
.related(providerIdentifier)
.whatToDo("Player Graph sampler for " + providerIdentifier.getPluginName() + "." + providerIdentifier.getProviderName() + " ran into error and was disabled. You can disable the plugin from Plan config and report this.")
.build());
// TODO #2544 Prevent further registrations of the player graph sampler.
ExtensionMethodErrorTracker.errored(extension, provider);
cancel();
}
}
Expand Down

0 comments on commit 62f7191

Please sign in to comment.