Created SessionEndTransaction:

- Removed SaveOperations#session
- Removed DBSystem dependency from SessionCache, so SessionCache does
  not need to be called from async thread. (SessionCache does not save,
  responsibility now with the caller)
This commit is contained in:
Rsl1122 2019-02-16 12:42:03 +02:00
parent 40a62874a3
commit 57695d6e43
11 changed files with 101 additions and 126 deletions

View File

@ -163,7 +163,8 @@ public class PlayerOnlineListener implements Listener {
}
database.executeTransaction(new PlayerServerRegisterTransaction(uuid, player::getFirstPlayed, playerName, serverUUID));
processing.submitCritical(() -> sessionCache.cacheSession(uuid, new Session(uuid, serverUUID, time, world, gm)));
sessionCache.cacheSession(uuid, new Session(uuid, serverUUID, time, world, gm))
.ifPresent(previousSession -> database.executeTransaction(new SessionEndTransaction(previousSession)));
if (!displayName.equals(nicknameCache.getDisplayName(uuid))) {
database.executeTransaction(
@ -187,14 +188,17 @@ public class PlayerOnlineListener implements Listener {
private void actOnQuitEvent(PlayerQuitEvent event) {
long time = System.currentTimeMillis();
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
AFKListener.AFK_TRACKER.loggedOut(uuid, time);
AFKListener.AFK_TRACKER.loggedOut(playerUUID, time);
nicknameCache.removeDisplayName(uuid);
nicknameCache.removeDisplayName(playerUUID);
processing.submit(processors.player().banAndOpProcessor(uuid, player::isBanned, player.isOp()));
processing.submit(processors.player().endSessionProcessor(uuid, time));
processing.submit(processors.info().playerPageUpdateProcessor(uuid));
processing.submit(processors.player().banAndOpProcessor(playerUUID, player::isBanned, player.isOp()));
sessionCache.endSession(playerUUID, time)
.ifPresent(endedSession -> dbSystem.getDatabase().executeTransaction(new SessionEndTransaction(endedSession)));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
}
}

View File

@ -84,24 +84,23 @@ public class PlayerOnlineListener implements Listener {
public void onPostLogin(PostLoginEvent event) {
try {
ProxiedPlayer player = event.getPlayer();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
String name = player.getName();
InetAddress address = player.getAddress().getAddress();
long time = System.currentTimeMillis();
sessionCache.cacheSession(uuid, new Session(uuid, serverInfo.getServerUUID(), time, null, null));
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverInfo.getServerUUID(), time, null, null));
Database database = dbSystem.getDatabase();
boolean gatheringGeolocations = config.isTrue(DataGatheringSettings.GEOLOCATIONS);
if (gatheringGeolocations) {
database.executeTransaction(
new GeoInfoStoreTransaction(uuid, address, time, geolocationCache::getCountry)
new GeoInfoStoreTransaction(playerUUID, address, time, geolocationCache::getCountry)
);
}
database.executeTransaction(new PlayerRegisterTransaction(uuid, () -> time, name));
processing.submit(processors.info().playerPageUpdateProcessor(uuid));
database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, name));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
} catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e);
@ -112,10 +111,10 @@ public class PlayerOnlineListener implements Listener {
public void onLogout(PlayerDisconnectEvent event) {
try {
ProxiedPlayer player = event.getPlayer();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
sessionCache.endSession(uuid, System.currentTimeMillis());
processing.submit(processors.info().playerPageUpdateProcessor(uuid));
sessionCache.endSession(playerUUID, System.currentTimeMillis());
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
} catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e);
@ -126,12 +125,12 @@ public class PlayerOnlineListener implements Listener {
public void onServerSwitch(ServerSwitchEvent event) {
try {
ProxiedPlayer player = event.getPlayer();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
long time = System.currentTimeMillis();
// Replaces the current session in the cache.
sessionCache.cacheSession(uuid, new Session(uuid, serverInfo.getServerUUID(), time, null, null));
processing.submit(processors.info().playerPageUpdateProcessor(uuid));
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverInfo.getServerUUID(), time, null, null));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
} catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e);
}

View File

@ -14,33 +14,27 @@
* 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.system.processing.processors.player;
package com.djrapitops.plan.db.access.transactions.events;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.processing.CriticalRunnable;
import java.util.UUID;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.db.access.queries.DataStoreQueries;
import com.djrapitops.plan.db.access.transactions.Transaction;
/**
* Ends a session and saves it to the database.
* Transaction for storing a session after a session has ended.
*
* @author Rsl1122
*/
public class EndSessionProcessor implements CriticalRunnable {
public class SessionEndTransaction extends Transaction {
private final UUID uuid;
private final long time;
private final Session session;
private final SessionCache sessionCache;
EndSessionProcessor(UUID uuid, long time, SessionCache sessionCache) {
this.uuid = uuid;
this.time = time;
this.sessionCache = sessionCache;
public SessionEndTransaction(Session session) {
this.session = session;
}
@Override
public void run() {
sessionCache.endSession(uuid, time);
protected void performOperations() {
execute(DataStoreQueries.storeSession(session));
}
}
}

View File

@ -36,12 +36,12 @@ public class ProxySessionCache extends SessionCache {
@Inject
public ProxySessionCache(DBSystem dbSystem) {
super(dbSystem);
super();
}
@Override
public Optional<Session> endSession(UUID uuid, long time) {
removeSessionFromCache(uuid);
public Optional<Session> endSession(UUID playerUUID, long time) {
removeSessionFromCache(playerUUID);
/* Proxy should not save sessions so session is not removed.. */
return Optional.empty();
}

View File

@ -18,7 +18,6 @@ package com.djrapitops.plan.system.cache;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.keys.SessionKeys;
import com.djrapitops.plan.system.database.DBSystem;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -37,11 +36,8 @@ public class SessionCache {
private static final Map<UUID, Session> ACTIVE_SESSIONS = new HashMap<>();
protected final DBSystem dbSystem;
@Inject
public SessionCache(DBSystem dbSystem) {
this.dbSystem = dbSystem;
public SessionCache() {
}
public static Map<UUID, Session> getActiveSessions() {
@ -61,44 +57,46 @@ public class SessionCache {
/**
* Used to get the Session of the player in the sessionCache.
*
* @param uuid UUID of the player.
* @param playerUUID UUID of the player.
* @return Optional with the session inside it if found.
*/
public static Optional<Session> getCachedSession(UUID uuid) {
return Optional.ofNullable(ACTIVE_SESSIONS.get(uuid));
public static Optional<Session> getCachedSession(UUID playerUUID) {
return Optional.ofNullable(ACTIVE_SESSIONS.get(playerUUID));
}
public void cacheSession(UUID uuid, Session session) {
if (getCachedSession(uuid).isPresent()) {
endSession(uuid, System.currentTimeMillis());
/**
* Cache a new session.
*
* @param playerUUID UUID of the player
* @param session Session to cache.
* @return Optional: previous session. Recipients of this object should decide if it needs to be saved.
*/
public Optional<Session> cacheSession(UUID playerUUID, Session session) {
if (getCachedSession(playerUUID).isPresent()) {
return endSession(playerUUID, session.getUnsafe(SessionKeys.START));
}
ACTIVE_SESSIONS.put(uuid, session);
ACTIVE_SESSIONS.put(playerUUID, session);
return Optional.empty();
}
/**
* End a session and save it to database.
*
* @param uuid UUID of the player.
* @param time Time the session ended.
* @throws com.djrapitops.plan.api.exceptions.database.DBOpException If saving failed.
* @param playerUUID UUID of the player.
* @param time Time the session ended.
* @return Optional: ended session. Recipients of this object should decide if it needs to be saved.
*/
public Optional<Session> endSession(UUID uuid, long time) {
Session session = ACTIVE_SESSIONS.get(uuid);
public Optional<Session> endSession(UUID playerUUID, long time) {
Session session = ACTIVE_SESSIONS.get(playerUUID);
if (session == null || session.getUnsafe(SessionKeys.START) > time) {
return Optional.empty();
}
try {
session.endSession(time);
// Might throw a DBOpException
// TODO Refactor to use Event transactions when available.
dbSystem.getDatabase().save().session(uuid, session);
return Optional.of(session);
} finally {
removeSessionFromCache(uuid);
}
removeSessionFromCache(playerUUID);
session.endSession(time);
return Optional.of(session);
}
protected void removeSessionFromCache(UUID uuid) {
ACTIVE_SESSIONS.remove(uuid);
protected void removeSessionFromCache(UUID playerUUID) {
ACTIVE_SESSIONS.remove(playerUUID);
}
}

View File

@ -16,8 +16,6 @@
*/
package com.djrapitops.plan.system.database.databases.operation;
import com.djrapitops.plan.data.container.Session;
import java.util.UUID;
/**
@ -40,7 +38,4 @@ public interface SaveOperations {
@Deprecated
void opStatus(UUID uuid, boolean op);
@Deprecated
void session(UUID uuid, Session session);
}

View File

@ -16,10 +16,7 @@
*/
package com.djrapitops.plan.system.database.databases.sql.operation;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.queries.DataStoreQueries;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.system.database.databases.operation.SaveOperations;
import java.util.UUID;
@ -45,13 +42,4 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
userInfoTable.updateOpStatus(uuid, op);
}
@Override
public void session(UUID uuid, Session session) {
db.executeTransaction(new Transaction() {
@Override
protected void performOperations() {
execute(DataStoreQueries.storeSession(session));
}
});
}
}

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.system.processing.processors.player;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.DBSystem;
import dagger.Lazy;
@ -34,22 +33,15 @@ import java.util.function.BooleanSupplier;
public class PlayerProcessors {
private final Lazy<DBSystem> dbSystem;
private final Lazy<SessionCache> sessionCache;
@Inject
public PlayerProcessors(
Lazy<DBSystem> dbSystem,
Lazy<SessionCache> sessionCache
Lazy<DBSystem> dbSystem
) {
this.dbSystem = dbSystem;
this.sessionCache = sessionCache;
}
public BanAndOpProcessor banAndOpProcessor(UUID uuid, BooleanSupplier banned, boolean op) {
return new BanAndOpProcessor(uuid, banned, op, dbSystem.get().getDatabase());
}
public EndSessionProcessor endSessionProcessor(UUID uuid, long time) {
return new EndSessionProcessor(uuid, time, sessionCache.get());
}
}

View File

@ -38,7 +38,7 @@ public class SessionCacheTest {
public void setUp() {
session = new Session(uuid, serverUUID, 12345L, "World1", "SURVIVAL");
SessionCache sessionCache = new SessionCache(null);
SessionCache sessionCache = new SessionCache();
sessionCache.cacheSession(uuid, session);
}

View File

@ -103,19 +103,19 @@ public class SpongePlayerListener {
private void actOnLoginEvent(ClientConnectionEvent.Login event) {
GameProfile profile = event.getProfile();
UUID uuid = profile.getUniqueId();
UUID playerUUID = profile.getUniqueId();
boolean banned = isBanned(profile);
processing.submit(processors.player().banAndOpProcessor(uuid, () -> banned, false));
processing.submit(processors.player().banAndOpProcessor(playerUUID, () -> banned, false));
}
@Listener(order = Order.POST)
public void onKick(KickPlayerEvent event) {
try {
UUID uuid = event.getTargetEntity().getUniqueId();
if (!status.areKicksCounted() || SpongeAFKListener.AFK_TRACKER.isAfk(uuid)) {
UUID playerUUID = event.getTargetEntity().getUniqueId();
if (!status.areKicksCounted() || SpongeAFKListener.AFK_TRACKER.isAfk(playerUUID)) {
return;
}
dbSystem.getDatabase().executeTransaction(new KickStoreTransaction(uuid));
dbSystem.getDatabase().executeTransaction(new KickStoreTransaction(playerUUID));
} catch (Exception e) {
errorHandler.log(L.ERROR, this.getClass(), e);
}
@ -142,11 +142,11 @@ public class SpongePlayerListener {
private void actOnJoinEvent(ClientConnectionEvent.Join event) {
Player player = event.getTargetEntity();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
UUID serverUUID = serverInfo.getServerUUID();
long time = System.currentTimeMillis();
SpongeAFKListener.AFK_TRACKER.performedAction(uuid, time);
SpongeAFKListener.AFK_TRACKER.performedAction(playerUUID, time);
String world = player.getWorld().getName();
Optional<GameMode> gameMode = player.getGameModeData().get(Keys.GAME_MODE);
@ -163,20 +163,21 @@ public class SpongePlayerListener {
boolean gatheringGeolocations = config.isTrue(DataGatheringSettings.GEOLOCATIONS);
if (gatheringGeolocations) {
database.executeTransaction(
new GeoInfoStoreTransaction(uuid, address, time, geolocationCache::getCountry)
new GeoInfoStoreTransaction(playerUUID, address, time, geolocationCache::getCountry)
);
}
database.executeTransaction(new PlayerServerRegisterTransaction(uuid, () -> time, playerName, serverUUID));
processing.submitCritical(() -> sessionCache.cacheSession(uuid, new Session(uuid, serverUUID, time, world, gm)));
database.executeTransaction(new PlayerServerRegisterTransaction(playerUUID, () -> time, playerName, serverUUID));
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverUUID, time, world, gm))
.ifPresent(previousSession -> database.executeTransaction(new SessionEndTransaction(previousSession)));
if (!displayName.equals(nicknameCache.getDisplayName(uuid))) {
if (!displayName.equals(nicknameCache.getDisplayName(playerUUID))) {
database.executeTransaction(
new NicknameStoreTransaction(uuid, new Nickname(displayName, time, serverUUID))
new NicknameStoreTransaction(playerUUID, new Nickname(displayName, time, serverUUID))
);
}
processing.submitNonCritical(processors.info().playerPageUpdateProcessor(uuid));
processing.submitNonCritical(processors.info().playerPageUpdateProcessor(playerUUID));
}
@Listener(order = Order.POST)
@ -191,15 +192,18 @@ public class SpongePlayerListener {
private void actOnQuitEvent(ClientConnectionEvent.Disconnect event) {
long time = System.currentTimeMillis();
Player player = event.getTargetEntity();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
SpongeAFKListener.AFK_TRACKER.loggedOut(uuid, time);
SpongeAFKListener.AFK_TRACKER.loggedOut(playerUUID, time);
nicknameCache.removeDisplayName(uuid);
nicknameCache.removeDisplayName(playerUUID);
boolean banned = isBanned(player.getProfile());
processing.submit(processors.player().banAndOpProcessor(uuid, () -> banned, false));
processing.submit(processors.player().endSessionProcessor(uuid, time));
processing.submit(processors.info().playerPageUpdateProcessor(uuid));
processing.submit(processors.player().banAndOpProcessor(playerUUID, () -> banned, false));
sessionCache.endSession(playerUUID, time)
.ifPresent(endedSession -> dbSystem.getDatabase().executeTransaction(new SessionEndTransaction(endedSession)));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
}
}

View File

@ -87,24 +87,24 @@ public class PlayerOnlineListener {
public void onPostLogin(PostLoginEvent event) {
try {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
String name = player.getUsername();
InetAddress address = player.getRemoteAddress().getAddress();
long time = System.currentTimeMillis();
sessionCache.cacheSession(uuid, new Session(uuid, serverInfo.getServerUUID(), time, null, null));
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverInfo.getServerUUID(), time, null, null));
Database database = dbSystem.getDatabase();
boolean gatheringGeolocations = config.isTrue(DataGatheringSettings.GEOLOCATIONS);
if (gatheringGeolocations) {
database.executeTransaction(
new GeoInfoStoreTransaction(uuid, address, time, geolocationCache::getCountry)
new GeoInfoStoreTransaction(playerUUID, address, time, geolocationCache::getCountry)
);
}
database.executeTransaction(new PlayerRegisterTransaction(uuid, () -> time, name));
processing.submit(processors.info().playerPageUpdateProcessor(uuid));
database.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> time, name));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
} catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e);
@ -115,10 +115,10 @@ public class PlayerOnlineListener {
public void onLogout(DisconnectEvent event) {
try {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
sessionCache.endSession(uuid, System.currentTimeMillis());
processing.submit(processors.info().playerPageUpdateProcessor(uuid));
sessionCache.endSession(playerUUID, System.currentTimeMillis());
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
ResponseCache.clearResponse(PageId.SERVER.of(serverInfo.getServerUUID()));
} catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e);
@ -129,12 +129,13 @@ public class PlayerOnlineListener {
public void onServerSwitch(ServerConnectedEvent event) {
try {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
UUID playerUUID = player.getUniqueId();
long time = System.currentTimeMillis();
long now = System.currentTimeMillis();
// Replaces the current session in the cache.
sessionCache.cacheSession(uuid, new Session(uuid, serverInfo.getServerUUID(), now, null, null));
processing.submit(processors.info().playerPageUpdateProcessor(uuid));
sessionCache.cacheSession(playerUUID, new Session(playerUUID, serverInfo.getServerUUID(), time, null, null));
processing.submit(processors.info().playerPageUpdateProcessor(playerUUID));
} catch (Exception e) {
errorHandler.log(L.WARN, this.getClass(), e);
}