mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-07-13 15:32:45 +00:00
[AB-166] added locking for runtime storage
This commit is contained in:
@@ -2,6 +2,7 @@ package dev.sheldan.abstracto.experience.job;
|
||||
|
||||
import dev.sheldan.abstracto.experience.models.ServerExperience;
|
||||
import dev.sheldan.abstracto.experience.service.AUserExperienceService;
|
||||
import dev.sheldan.abstracto.experience.service.RunTimeExperienceService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.quartz.DisallowConcurrentExecution;
|
||||
import org.quartz.JobExecutionContext;
|
||||
@@ -30,17 +31,27 @@ public class ExperiencePersistingJob extends QuartzJobBean {
|
||||
@Autowired
|
||||
private AUserExperienceService userExperienceService;
|
||||
|
||||
@Autowired
|
||||
private RunTimeExperienceService runTimeExperienceService;
|
||||
|
||||
@Override
|
||||
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
|
||||
Map<Long, List<ServerExperience>> runtimeExperience = userExperienceService.getRuntimeExperience();
|
||||
runTimeExperienceService.takeLock();
|
||||
try {
|
||||
Map<Long, List<ServerExperience>> runtimeExperience = runTimeExperienceService.getRuntimeExperience();
|
||||
log.info("Running experience persisting job.");
|
||||
Long pastMinute = (Instant.now().getEpochSecond() / 60) - 1;
|
||||
if(runtimeExperience.containsKey(pastMinute)) {
|
||||
List<ServerExperience> foundServers = runtimeExperience.get(pastMinute);
|
||||
log.info("Found experience from {} servers to persist.", foundServers.size());
|
||||
userExperienceService.handleExperienceGain(foundServers).thenAccept(aVoid ->
|
||||
runtimeExperience.remove(pastMinute)
|
||||
);
|
||||
userExperienceService.handleExperienceGain(foundServers).thenAccept(aVoid -> {
|
||||
runTimeExperienceService.takeLock();
|
||||
runTimeExperienceService.getRuntimeExperience().remove(pastMinute);
|
||||
runTimeExperienceService.releaseLock();
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
runTimeExperienceService.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -88,6 +88,8 @@ public class AUserExperienceServiceBean implements AUserExperienceService {
|
||||
*/
|
||||
@Override
|
||||
public void addExperience(AUserInAServer userInAServer) {
|
||||
runTimeExperienceService.takeLock();
|
||||
try {
|
||||
Long minute = Instant.now().getEpochSecond() / 60;
|
||||
Map<Long, List<ServerExperience>> runtimeExperience = runTimeExperienceService.getRuntimeExperience();
|
||||
if(runtimeExperience.containsKey(minute)) {
|
||||
@@ -110,11 +112,9 @@ public class AUserExperienceServiceBean implements AUserExperienceService {
|
||||
serverExperience.getUserInServerIds().add(userInAServer.getUserInServerId());
|
||||
runtimeExperience.put(minute, new ArrayList<>(Arrays.asList(serverExperience)));
|
||||
}
|
||||
} finally {
|
||||
runTimeExperienceService.releaseLock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, List<ServerExperience>> getRuntimeExperience() {
|
||||
return runTimeExperienceService.getRuntimeExperience();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,13 +6,23 @@ import org.springframework.stereotype.Component;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@Component
|
||||
public class RunTimeExperienceService {
|
||||
private Map<Long, List<ServerExperience>> runtimeExperience = new HashMap<>();
|
||||
|
||||
private static final Lock lock = new ReentrantLock();
|
||||
public Map<Long, List<ServerExperience>> getRuntimeExperience() {
|
||||
return runtimeExperience;
|
||||
}
|
||||
|
||||
public void takeLock() {
|
||||
lock.lock();
|
||||
}
|
||||
|
||||
public void releaseLock() {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import dev.sheldan.abstracto.experience.models.database.AExperienceLevel;
|
||||
import dev.sheldan.abstracto.experience.models.database.AExperienceRole;
|
||||
import dev.sheldan.abstracto.experience.models.database.AUserExperience;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
@@ -31,13 +30,6 @@ public interface AUserExperienceService {
|
||||
*/
|
||||
void addExperience(AUserInAServer userInAServer);
|
||||
|
||||
/**
|
||||
* The current representation of the run time experience. Basically a HashMap of minutes to a list of {@link AServer}
|
||||
* containing a list of {@link AUserInAServer} which should gain experience in the minute used as key in the HashMap
|
||||
* @return
|
||||
*/
|
||||
Map<Long, List<ServerExperience>> getRuntimeExperience();
|
||||
|
||||
/**
|
||||
* Calculates the appropriate level of the given {@link AUserExperience} according to the given {@link AExperienceLevel}
|
||||
* configuration.
|
||||
|
||||
@@ -33,6 +33,8 @@ public class EmotePersistingJob extends QuartzJobBean {
|
||||
@Override
|
||||
@Transactional
|
||||
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
|
||||
trackedEmoteRuntimeService.takeLock();
|
||||
try {
|
||||
Map<Long, Map<Long, List<PersistingEmote>>> runtimeConfig = trackedEmoteRuntimeService.getRuntimeConfig();
|
||||
log.info("Running statistic persisting job.");
|
||||
Long pastMinute = getPastMinute();
|
||||
@@ -43,6 +45,9 @@ public class EmotePersistingJob extends QuartzJobBean {
|
||||
runtimeConfig.remove(pastMinute);
|
||||
checkForPastEmoteStats(pastMinute, runtimeConfig);
|
||||
}
|
||||
} finally {
|
||||
trackedEmoteRuntimeService.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
private void checkForPastEmoteStats(Long minuteToCheck, Map<Long, Map<Long, List<PersistingEmote>>> runtimeConfig) {
|
||||
|
||||
@@ -9,6 +9,8 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -16,6 +18,7 @@ public class TrackedEmoteRuntimeServiceBean implements TrackedEmoteRuntimeServic
|
||||
|
||||
@Autowired
|
||||
private TrackedEmoteRunTimeStorage trackedEmoteRunTimeStorage;
|
||||
private static final Lock runTimeLock = new ReentrantLock();
|
||||
|
||||
@Override
|
||||
public Map<Long, Map<Long, List<PersistingEmote>>> getRuntimeConfig() {
|
||||
@@ -29,6 +32,8 @@ public class TrackedEmoteRuntimeServiceBean implements TrackedEmoteRuntimeServic
|
||||
|
||||
@Override
|
||||
public void addEmoteForServer(Emote emote, Guild guild, Long count, boolean external) {
|
||||
takeLock();
|
||||
try {
|
||||
Long key = getKey();
|
||||
PersistingEmote newPersistentEmote = createFromEmote(guild, emote, count, external);
|
||||
if (trackedEmoteRunTimeStorage.contains(key)) {
|
||||
@@ -53,6 +58,9 @@ public class TrackedEmoteRuntimeServiceBean implements TrackedEmoteRuntimeServic
|
||||
log.trace("Adding emote map entry for server {}.", guild.getIdLong());
|
||||
trackedEmoteRunTimeStorage.put(key, serverEmotes);
|
||||
}
|
||||
} finally {
|
||||
releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,4 +87,15 @@ public class TrackedEmoteRuntimeServiceBean implements TrackedEmoteRuntimeServic
|
||||
.serverId(guild.getIdLong())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void takeLock() {
|
||||
runTimeLock.lock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void releaseLock() {
|
||||
runTimeLock.unlock();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,4 +14,6 @@ public interface TrackedEmoteRuntimeService {
|
||||
Long getKey();
|
||||
PersistingEmote createFromEmote(Guild guild, Emote emote, boolean external);
|
||||
PersistingEmote createFromEmote(Guild guild, Emote emote, Long count, boolean external);
|
||||
void takeLock();
|
||||
void releaseLock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user