Skip to content

Commit 58b6baa

Browse files
committed
Use in-memory session tracking to fix slow shutdown
Signed-off-by: applenick <applenick@users.noreply.github.com>
1 parent 5e97eae commit 58b6baa

File tree

7 files changed

+47
-57
lines changed

7 files changed

+47
-57
lines changed

core/src/main/java/dev/pgm/community/sessions/VanishedSessionListener.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ public void onVanish(PlayerVanishEvent event) {
1919
Player player = event.getPlayer().getBukkit();
2020
if (sessions.isPlayerJoining(player)) return;
2121

22-
sessions
23-
.getLatestSession(player.getUniqueId(), false)
24-
.thenAcceptAsync(
25-
session -> {
26-
sessions.endSession(session);
27-
sessions.startSession(player);
28-
});
22+
Session session = sessions.getActiveSession(player.getUniqueId());
23+
if (session != null) {
24+
sessions.endSession(session);
25+
}
26+
sessions.startSession(player);
2927
}
3028
}

core/src/main/java/dev/pgm/community/sessions/feature/SessionFeature.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public interface SessionFeature extends Feature {
1010

1111
CompletableFuture<Session> getLatestSession(UUID playerId, boolean ignoreDisguised);
1212

13+
Session getActiveSession(UUID playerId);
14+
1315
Session startSession(Player player);
1416

1517
void endSession(Session session);
@@ -18,7 +20,5 @@ public interface SessionFeature extends Feature {
1820

1921
void endOngoingSessions();
2022

21-
default void endOngoingSessionsSync() {
22-
endOngoingSessions();
23-
}
23+
void endOngoingSessionsSync();
2424
}

core/src/main/java/dev/pgm/community/sessions/feature/types/SessionFeatureCore.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import dev.pgm.community.utils.PGMUtils;
1212
import dev.pgm.community.utils.VisibilityUtils;
1313
import java.time.Instant;
14-
import java.util.ArrayList;
15-
import java.util.List;
14+
import java.util.Map;
1615
import java.util.UUID;
1716
import java.util.concurrent.CompletableFuture;
17+
import java.util.concurrent.ConcurrentHashMap;
1818
import java.util.logging.Logger;
1919
import org.bukkit.Bukkit;
2020
import org.bukkit.entity.Player;
@@ -29,16 +29,16 @@
2929
public class SessionFeatureCore extends FeatureBase implements SessionFeature {
3030

3131
private final SessionStore store;
32-
private List<UUID> joiningPlayers;
32+
private final Map<UUID, Session> activeSessions;
3333
private VanishedSessionListener vanishedSessionListener;
3434
private boolean serverRestarting;
3535

3636
public SessionFeatureCore(UsersFeature users, Logger logger, SessionStore store) {
3737
super(users.getConfig(), logger, "Sessions");
3838
this.store = store;
39+
this.activeSessions = new ConcurrentHashMap<>();
3940

4041
if (getConfig().isEnabled()) {
41-
this.joiningPlayers = new ArrayList<>();
4242
enable();
4343

4444
if (PGMUtils.isPGMEnabled()) {
@@ -50,6 +50,7 @@ public SessionFeatureCore(UsersFeature users, Logger logger, SessionStore store)
5050

5151
@Override
5252
public void disable() {
53+
super.disable();
5354
if (vanishedSessionListener != null) HandlerList.unregisterAll(vanishedSessionListener);
5455
endOngoingSessionsSync();
5556
}
@@ -59,9 +60,15 @@ public CompletableFuture<Session> getLatestSession(UUID playerId, boolean ignore
5960
return store.query(new SessionQuery(playerId, ignoreDisguised));
6061
}
6162

63+
@Override
64+
public Session getActiveSession(UUID playerId) {
65+
return activeSessions.get(playerId);
66+
}
67+
6268
@Override
6369
public Session startSession(Player player) {
6470
Session session = new Session(player.getUniqueId(), VisibilityUtils.isDisguised(player));
71+
activeSessions.put(player.getUniqueId(), session);
6572
store.save(session);
6673

6774
return session;
@@ -75,34 +82,42 @@ public void endSession(Session session) {
7582

7683
@Override
7784
public void endOngoingSessions() {
78-
store.endOngoingSessions();
85+
Instant now = Instant.now();
86+
for (Session session : activeSessions.values()) {
87+
session.setEndDate(now);
88+
store.updateSessionEndTime(session);
89+
}
90+
activeSessions.clear();
7991
}
8092

8193
@Override
8294
public void endOngoingSessionsSync() {
83-
store.endOngoingSessionsSync();
84-
}
85-
86-
@EventHandler(priority = EventPriority.LOWEST)
87-
public void onJoinLowest(PlayerJoinEvent event) {
88-
joiningPlayers.add(event.getPlayer().getUniqueId());
95+
Instant now = Instant.now();
96+
for (Session session : activeSessions.values()) {
97+
session.setEndDate(now);
98+
store.updateSessionEndTimeSync(session);
99+
}
100+
activeSessions.clear();
89101
}
90102

91103
@EventHandler(priority = EventPriority.HIGHEST)
92104
public void onJoinHighest(PlayerJoinEvent event) {
93105
if (!serverRestarting) startSession(event.getPlayer());
94-
joiningPlayers.remove(event.getPlayer().getUniqueId());
95106
}
96107

97108
@EventHandler(priority = EventPriority.HIGHEST)
98109
public void onQuitHighest(PlayerQuitEvent event) {
99-
if (!serverRestarting)
100-
getLatestSession(event.getPlayer().getUniqueId(), false).thenAcceptAsync(this::endSession);
110+
if (serverRestarting) return;
111+
112+
Session session = activeSessions.remove(event.getPlayer().getUniqueId());
113+
if (session != null) {
114+
endSession(session);
115+
}
101116
}
102117

103118
@Override
104119
public boolean isPlayerJoining(Player player) {
105-
return joiningPlayers.contains(player.getUniqueId());
120+
return !activeSessions.containsKey(player.getUniqueId());
106121
}
107122

108123
@EventHandler

core/src/main/java/dev/pgm/community/sessions/services/SQLSessionService.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,20 @@ public void updateSessionEndTime(Session session) {
9090
session.getSessionId().toString());
9191
}
9292

93-
public void endOngoingSessions() {
94-
DatabaseExecutor.executeUpdateAsync(
95-
UPDATE_ONGOING_SESSION_ENDING_QUERY,
96-
Instant.now().toEpochMilli(),
97-
Community.get().getServerId());
98-
DatabaseExecutor.executeUpdateAsync(
99-
UPDATE_LATEST_ONGOING_SESSION_ENDING_QUERY,
100-
Instant.now().toEpochMilli(),
101-
Community.get().getServerId());
102-
}
103-
104-
public void endOngoingSessionsSync() {
105-
long now = Instant.now().toEpochMilli();
93+
public void updateSessionEndTimeSync(Session session) {
94+
Long endTime = session.getEndDate() == null ? null : session.getEndDate().toEpochMilli();
10695
try {
10796
DatabaseExecutor.executeUpdateAsync(
108-
UPDATE_ONGOING_SESSION_ENDING_QUERY, now, Community.get().getServerId())
97+
UPDATE_SESSION_ENDTIME_QUERY, endTime, session.getSessionId().toString())
10998
.join();
11099
DatabaseExecutor.executeUpdateAsync(
111-
UPDATE_LATEST_ONGOING_SESSION_ENDING_QUERY, now, Community.get().getServerId())
100+
UPDATE_LATEST_ENDTIME_QUERY, endTime, session.getSessionId().toString())
112101
.join();
113102
} catch (Exception exception) {
114103
Community.get()
115104
.getLogger()
116-
.warning("Failed to end ongoing sessions during shutdown: " + exception.getMessage());
105+
.warning(
106+
"Failed to end session " + session.getSessionId() + ": " + exception.getMessage());
117107
}
118108
}
119109

core/src/main/java/dev/pgm/community/sessions/services/SessionDataQuery.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ public interface SessionDataQuery {
1212
static final String UPDATE_SESSION_ENDTIME_QUERY =
1313
"UPDATE " + TABLE_NAME + " SET end_time = ? WHERE id = ?";
1414

15-
static final String UPDATE_ONGOING_SESSION_ENDING_QUERY =
16-
"UPDATE " + TABLE_NAME + " SET end_time = ? WHERE server = ? AND end_time IS NULL";
17-
1815
static final String LATEST_TABLE_NAME = "latest_sessions";
1916
static final String LATEST_TABLE_FIELDS =
2017
"(player VARCHAR(36), ignore_disguised BOOL, session_id VARCHAR(36), disguised BOOL, server VARCHAR(32), start_time BIGINT, end_time BIGINT, PRIMARY KEY (player, ignore_disguised))";
@@ -24,7 +21,4 @@ public interface SessionDataQuery {
2421

2522
static final String UPDATE_LATEST_ENDTIME_QUERY =
2623
"UPDATE " + LATEST_TABLE_NAME + " SET end_time = ? WHERE session_id = ?";
27-
28-
static final String UPDATE_LATEST_ONGOING_SESSION_ENDING_QUERY =
29-
"UPDATE " + LATEST_TABLE_NAME + " SET end_time = ? WHERE server = ? AND end_time IS NULL";
3024
}

core/src/main/java/dev/pgm/community/sessions/store/SQLSessionStore.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ public void updateSessionEndTime(Session session) {
2929
}
3030

3131
@Override
32-
public void endOngoingSessions() {
33-
service.endOngoingSessions();
34-
}
35-
36-
@Override
37-
public void endOngoingSessionsSync() {
38-
service.endOngoingSessionsSync();
32+
public void updateSessionEndTimeSync(Session session) {
33+
service.updateSessionEndTimeSync(session);
3934
}
4035
}

core/src/main/java/dev/pgm/community/sessions/store/SessionStore.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@ public interface SessionStore {
1212

1313
void updateSessionEndTime(Session session);
1414

15-
void endOngoingSessions();
16-
17-
void endOngoingSessionsSync();
15+
void updateSessionEndTimeSync(Session session);
1816
}

0 commit comments

Comments
 (0)