-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Redis stream accept task to synchronize player data requests ac…
…ross servers.
- Loading branch information
Showing
7 changed files
with
116 additions
and
52 deletions.
There are no files selected for viewing
8 changes: 7 additions & 1 deletion
8
player/src/main/java/net/legacy/library/player/annotation/RedisStreamAccept.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package net.legacy.library.player.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author qwq-dev | ||
* @since 2025-01-04 20:19 | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface RedisStreamAccept { | ||
String[] redisStreamNames(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
player/src/main/java/net/legacy/library/player/task/redis/L1ToL2DataSyncTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package net.legacy.library.player.task.redis; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import de.leonhard.storage.internal.serialize.SimplixSerializer; | ||
import io.fairyproject.scheduler.ScheduledTask; | ||
import lombok.RequiredArgsConstructor; | ||
import net.legacy.library.cache.model.LockSettings; | ||
import net.legacy.library.cache.service.CacheServiceInterface; | ||
import net.legacy.library.cache.service.redis.RedisCacheServiceInterface; | ||
import net.legacy.library.commons.task.TaskInterface; | ||
import net.legacy.library.player.model.LegacyPlayerData; | ||
import net.legacy.library.player.service.LegacyPlayerDataService; | ||
import net.legacy.library.player.util.KeyUtil; | ||
import org.redisson.api.RedissonClient; | ||
|
||
import java.util.UUID; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author qwq-dev | ||
* @since 2025-01-05 12:10 | ||
*/ | ||
@RequiredArgsConstructor | ||
public class L1ToL2DataSyncTask implements TaskInterface { | ||
private final UUID uuid; | ||
private final LegacyPlayerDataService legacyPlayerDataService; | ||
|
||
public static L1ToL2DataSyncTask of(LegacyPlayerDataService legacyPlayerDataService) { | ||
return new L1ToL2DataSyncTask(null, legacyPlayerDataService); | ||
} | ||
|
||
public static L1ToL2DataSyncTask of(UUID uuid, LegacyPlayerDataService legacyPlayerDataService) { | ||
return new L1ToL2DataSyncTask(uuid, legacyPlayerDataService); | ||
} | ||
|
||
@Override | ||
public ScheduledTask<?> start() { | ||
return schedule(() -> { | ||
CacheServiceInterface<Cache<UUID, LegacyPlayerData>, LegacyPlayerData> l1Cache = | ||
legacyPlayerDataService.getL1Cache(); | ||
RedisCacheServiceInterface l2Cache = legacyPlayerDataService.getL2Cache(); | ||
RedissonClient redissonClient = l2Cache.getCache(); | ||
|
||
l1Cache.getCache().asMap().forEach((key, legacyPlayerData) -> { | ||
UUID uuid = legacyPlayerData.getUuid(); | ||
|
||
if (this.uuid != null && !this.uuid.equals(key)) { | ||
return; | ||
} | ||
|
||
String serialized = SimplixSerializer.serialize(legacyPlayerData).toString(); | ||
String bucketKey = KeyUtil.getLegacyPlayerDataServiceKey(key, legacyPlayerDataService, "bucket-key"); | ||
String syncLockKey = KeyUtil.getLegacyPlayerDataServiceKey(key, legacyPlayerDataService, "l1-l2-sync-lock"); | ||
|
||
l2Cache.execute( | ||
client -> client.getLock(syncLockKey), | ||
client -> { | ||
client.getBucket(bucketKey).set(serialized); | ||
return null; | ||
}, | ||
LockSettings.of(0, 0, TimeUnit.MILLISECONDS) | ||
); | ||
}); | ||
}); | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
player/src/main/java/net/legacy/library/player/task/redis/RedisStreamAcceptInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
package net.legacy.library.player.task.redis; | ||
|
||
import org.redisson.api.StreamMessageId; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author qwq-dev | ||
* @since 2025-01-04 20:30 | ||
*/ | ||
public interface RedisStreamAcceptInterface { | ||
boolean canAccept(StreamMessageId streamMessageId); | ||
|
||
void accept(Map<Object, Object> message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 29 additions & 9 deletions
38
.../main/java/net/legacy/library/player/task/redis/impl/PlayerDataSyncRedisStreamAccept.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,44 @@ | ||
package net.legacy.library.player.task.redis.impl; | ||
|
||
import com.google.common.reflect.TypeToken; | ||
import net.legacy.library.commons.util.GsonUtil; | ||
import net.legacy.library.player.annotation.RedisStreamAccept; | ||
import net.legacy.library.player.service.LegacyPlayerDataService; | ||
import net.legacy.library.player.task.redis.L1ToL2DataSyncTask; | ||
import net.legacy.library.player.task.redis.RedisStreamAcceptInterface; | ||
import org.redisson.api.StreamMessageId; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
/** | ||
* @author qwq-dev | ||
* @since 2025-01-04 20:59 | ||
*/ | ||
@RedisStreamAccept(redisStreamNames = "player-data-sync") | ||
@RedisStreamAccept | ||
public class PlayerDataSyncRedisStreamAccept implements RedisStreamAcceptInterface { | ||
@Override | ||
public boolean canAccept(StreamMessageId streamMessageId) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void accept(Map<Object, Object> message) { | ||
// TODO: player data sync (l1 -> l2) | ||
for (Map.Entry<Object, Object> entry : message.entrySet()) { | ||
// If the key is player-data-sync, cast the value to a map | ||
if (entry.getKey().toString().equals("player-data-sync")) { | ||
// This map key is LPDS name, value is player uuid | ||
Map<String, String> value = GsonUtil.GSON.fromJson( | ||
entry.getValue().toString(), new TypeToken<Map<String, String>>() { | ||
}.getType() | ||
); | ||
|
||
// L1 -> L2 | ||
for (Map.Entry<String, String> dataSyncEntry : value.entrySet()) { | ||
String lpdsName = dataSyncEntry.getValue(); | ||
String playerUuid = dataSyncEntry.getKey(); | ||
UUID uuid = UUID.fromString(playerUuid); | ||
|
||
Optional<LegacyPlayerDataService> legacyPlayerDataService = | ||
LegacyPlayerDataService.getLegacyPlayerDataService(lpdsName); | ||
legacyPlayerDataService.ifPresent(service -> L1ToL2DataSyncTask.of(uuid, service).start()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |