mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-28 19:35:23 +00:00
[AB-166] added locking for runtime storage
This commit is contained in:
@@ -33,15 +33,20 @@ public class EmotePersistingJob extends QuartzJobBean {
|
||||
@Override
|
||||
@Transactional
|
||||
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
|
||||
Map<Long, Map<Long, List<PersistingEmote>>> runtimeConfig = trackedEmoteRuntimeService.getRuntimeConfig();
|
||||
log.info("Running statistic persisting job.");
|
||||
Long pastMinute = getPastMinute();
|
||||
if(runtimeConfig.containsKey(pastMinute)) {
|
||||
Map<Long, List<PersistingEmote>> foundStatistics = runtimeConfig.get(pastMinute);
|
||||
log.info("Found emote statistics from {} servers to persist.", foundStatistics.size());
|
||||
trackedEmoteService.storeEmoteStatistics(foundStatistics);
|
||||
runtimeConfig.remove(pastMinute);
|
||||
checkForPastEmoteStats(pastMinute, runtimeConfig);
|
||||
trackedEmoteRuntimeService.takeLock();
|
||||
try {
|
||||
Map<Long, Map<Long, List<PersistingEmote>>> runtimeConfig = trackedEmoteRuntimeService.getRuntimeConfig();
|
||||
log.info("Running statistic persisting job.");
|
||||
Long pastMinute = getPastMinute();
|
||||
if(runtimeConfig.containsKey(pastMinute)) {
|
||||
Map<Long, List<PersistingEmote>> foundStatistics = runtimeConfig.get(pastMinute);
|
||||
log.info("Found emote statistics from {} servers to persist.", foundStatistics.size());
|
||||
trackedEmoteService.storeEmoteStatistics(foundStatistics);
|
||||
runtimeConfig.remove(pastMinute);
|
||||
checkForPastEmoteStats(pastMinute, runtimeConfig);
|
||||
}
|
||||
} finally {
|
||||
trackedEmoteRuntimeService.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,29 +32,34 @@ public class TrackedEmoteRuntimeServiceBean implements TrackedEmoteRuntimeServic
|
||||
|
||||
@Override
|
||||
public void addEmoteForServer(Emote emote, Guild guild, Long count, boolean external) {
|
||||
Long key = getKey();
|
||||
PersistingEmote newPersistentEmote = createFromEmote(guild, emote, count, external);
|
||||
if(trackedEmoteRunTimeStorage.contains(key)) {
|
||||
Map<Long, List<PersistingEmote>> elementsForKey = trackedEmoteRunTimeStorage.get(key);
|
||||
if(elementsForKey.containsKey(guild.getIdLong())) {
|
||||
List<PersistingEmote> persistingEmotes = elementsForKey.get(guild.getIdLong());
|
||||
Optional<PersistingEmote> existingEmote = persistingEmotes
|
||||
.stream()
|
||||
.filter(persistingEmote -> persistingEmote.getEmoteId().equals(emote.getIdLong()))
|
||||
.findFirst();
|
||||
existingEmote.ifPresent(persistingEmote -> persistingEmote.setCount(persistingEmote.getCount() + count));
|
||||
if(!existingEmote.isPresent()) {
|
||||
persistingEmotes.add(newPersistentEmote);
|
||||
takeLock();
|
||||
try {
|
||||
Long key = getKey();
|
||||
PersistingEmote newPersistentEmote = createFromEmote(guild, emote, count, external);
|
||||
if (trackedEmoteRunTimeStorage.contains(key)) {
|
||||
Map<Long, List<PersistingEmote>> elementsForKey = trackedEmoteRunTimeStorage.get(key);
|
||||
if (elementsForKey.containsKey(guild.getIdLong())) {
|
||||
List<PersistingEmote> persistingEmotes = elementsForKey.get(guild.getIdLong());
|
||||
Optional<PersistingEmote> existingEmote = persistingEmotes
|
||||
.stream()
|
||||
.filter(persistingEmote -> persistingEmote.getEmoteId().equals(emote.getIdLong()))
|
||||
.findFirst();
|
||||
existingEmote.ifPresent(persistingEmote -> persistingEmote.setCount(persistingEmote.getCount() + count));
|
||||
if (!existingEmote.isPresent()) {
|
||||
persistingEmotes.add(newPersistentEmote);
|
||||
}
|
||||
} else {
|
||||
log.trace("Adding emote {} to list of server {}.", newPersistentEmote.getEmoteId(), guild.getIdLong());
|
||||
elementsForKey.put(guild.getIdLong(), new ArrayList<>(Arrays.asList(newPersistentEmote)));
|
||||
}
|
||||
} else {
|
||||
log.trace("Adding emote {} to list of server {}.", newPersistentEmote.getEmoteId(), guild.getIdLong());
|
||||
elementsForKey.put(guild.getIdLong(), new ArrayList<>(Arrays.asList(newPersistentEmote)));
|
||||
HashMap<Long, List<PersistingEmote>> serverEmotes = new HashMap<>();
|
||||
serverEmotes.put(guild.getIdLong(), new ArrayList<>(Arrays.asList(newPersistentEmote)));
|
||||
log.trace("Adding emote map entry for server {}.", guild.getIdLong());
|
||||
trackedEmoteRunTimeStorage.put(key, serverEmotes);
|
||||
}
|
||||
} else {
|
||||
HashMap<Long, List<PersistingEmote>> serverEmotes = new HashMap<>();
|
||||
serverEmotes.put(guild.getIdLong(), new ArrayList<>(Arrays.asList(newPersistentEmote)));
|
||||
log.trace("Adding emote map entry for server {}.", guild.getIdLong());
|
||||
trackedEmoteRunTimeStorage.put(key, serverEmotes);
|
||||
} finally {
|
||||
releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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