Skip to content

Commit

Permalink
Add redis config
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Jun 14, 2024
1 parent 0cd4244 commit f193395
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
6 changes: 5 additions & 1 deletion NineChronicles.Headless.Executable/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public class Configuration

public int ArenaParticipantsSyncInterval { get; set; } = 1000;

public string RedisConnectionString { get; set; } = "";

public void Overwrite(
string? appProtocolVersionString,
string[]? trustedAppProtocolVersionSignerStrings,
Expand Down Expand Up @@ -150,7 +152,8 @@ public void Overwrite(
string? sentryDsn,
double? sentryTraceSampleRate,
int? arenaParticipantsSyncInterval,
bool? remoteKeyValueService
bool? remoteKeyValueService,
string? redisConnectionString
)
{
AppProtocolVersionString = appProtocolVersionString ?? AppProtocolVersionString;
Expand Down Expand Up @@ -205,6 +208,7 @@ public void Overwrite(
SentryTraceSampleRate = sentryTraceSampleRate ?? SentryTraceSampleRate;
ArenaParticipantsSyncInterval = arenaParticipantsSyncInterval ?? ArenaParticipantsSyncInterval;
RemoteKeyValueService = remoteKeyValueService ?? RemoteKeyValueService;
RedisConnectionString = redisConnectionString ?? RedisConnectionString;
}
}
}
19 changes: 17 additions & 2 deletions NineChronicles.Headless.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
using OpenTelemetry;
using OpenTelemetry.Metrics;
using Nekoyume;
using StackExchange.Redis;
using ITransaction = Sentry.ITransaction;

namespace NineChronicles.Headless.Executable
{
Expand Down Expand Up @@ -224,6 +226,8 @@ public async Task Run(
bool arenaParticipantsSync = true,
[Option(Description = "[DANGER] Turn on RemoteKeyValueService to debug.")]
bool remoteKeyValueService = false,
[Option(Description = "redis cache connection string")]
string? redisConnectionString = "localhost:6379",
[Ignore] CancellationToken? cancellationToken = null
)
{
Expand Down Expand Up @@ -310,7 +314,7 @@ public async Task Run(
txLifeTime, messageTimeout, tipTimeout, demandBuffer, skipPreload,
minimumBroadcastTarget, bucketSize, chainTipStaleBehaviorType, txQuotaPerSigner, maximumPollPeers,
consensusPort, consensusPrivateKeyString, consensusSeedStrings, consensusTargetBlockIntervalMilliseconds, consensusProposeSecondBase,
maxTransactionPerBlock, sentryDsn, sentryTraceSampleRate, arenaParticipantsSyncInterval, remoteKeyValueService
maxTransactionPerBlock, sentryDsn, sentryTraceSampleRate, arenaParticipantsSyncInterval, remoteKeyValueService, redisConnectionString
);

#if SENTRY || ! DEBUG
Expand Down Expand Up @@ -474,6 +478,16 @@ IActionLoader MakeSingleActionLoader()
MaxTransactionPerBlock = headlessConfig.MaxTransactionPerBlock
};
var arenaMemoryCache = new StateMemoryCache();
var configurationOptions = new ConfigurationOptions
{
EndPoints = { headlessConfig.RedisConnectionString },
ConnectTimeout = 500,
SyncTimeout = 500,
};

var redis = await ConnectionMultiplexer.ConnectAsync(configurationOptions);
var db = redis.GetDatabase();

hostBuilder.ConfigureServices(services =>
{
services.AddSingleton(_ => standaloneContext);
Expand All @@ -487,9 +501,10 @@ IActionLoader MakeSingleActionLoader()
// worker
if (arenaParticipantsSync)
{
services.AddHostedService(_ => new ArenaParticipantsWorker(arenaMemoryCache, standaloneContext, headlessConfig.ArenaParticipantsSyncInterval));
services.AddHostedService(_ => new ArenaParticipantsWorker(standaloneContext, headlessConfig.ArenaParticipantsSyncInterval, db));
}
services.AddSingleton(arenaMemoryCache);
services.AddSingleton(db);
});

NineChroniclesNodeService service =
Expand Down
2 changes: 0 additions & 2 deletions NineChronicles.Headless.Tests/GraphTypes/StateQueryTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
Expand All @@ -22,7 +21,6 @@
using Nekoyume.TableData;
using NineChronicles.Headless.GraphTypes;
using NineChronicles.Headless.GraphTypes.States;
using NineChronicles.Headless.Tests.Common;
using Xunit;
using static NineChronicles.Headless.Tests.GraphQLTestUtils;

Expand Down
22 changes: 13 additions & 9 deletions NineChronicles.Headless/ArenaParticipant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ namespace NineChronicles.Headless;

public class ArenaParticipant
{
public readonly Address AvatarAddr;
public readonly int Score;
public readonly int Rank;
public int WinScore;
public int LoseScore;
public readonly int Cp;
public readonly int PortraitId;
public readonly string NameWithHash;
public readonly int Level;
public Address AvatarAddr { get; set; }
public int Score { get; set; }
public int Rank { get; set; }
public int WinScore { get; set; }
public int LoseScore { get; set; }
public int Cp { get; set; }
public int PortraitId { get; set; }
public string NameWithHash { get; set; } = "";
public int Level { get; set; }

public ArenaParticipant()
{
}

public ArenaParticipant(
Address avatarAddr,
Expand Down
14 changes: 8 additions & 6 deletions NineChronicles.Headless/ArenaParticipantsWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
using Nekoyume;
using Nekoyume.Battle;
Expand All @@ -20,23 +20,25 @@
using Nekoyume.TableData;
using Nekoyume.TableData.Rune;
using NineChronicles.Headless.GraphTypes;
using NRedisStack.RedisStackCommands;
using Serilog;
using StackExchange.Redis;

namespace NineChronicles.Headless;

public class ArenaParticipantsWorker : BackgroundService
{
private ILogger _logger;

private StateMemoryCache _cache;
private IDatabase _db;

private StandaloneContext _context;

private int _interval;

public ArenaParticipantsWorker(StateMemoryCache memoryCache, StandaloneContext context, int interval)
public ArenaParticipantsWorker(StandaloneContext context, int interval, IDatabase database)
{
_cache = memoryCache;
_db = database;
_context = context;
_logger = Log.Logger.ForContext<ArenaParticipantsWorker>();
_interval = interval;
Expand Down Expand Up @@ -315,7 +317,7 @@ public void PrepareArenaParticipants()
var cacheKey = $"{currentRoundData.ChampionshipId}_{currentRoundData.Round}";
if (participants is null)
{
_cache.ArenaParticipantsCache.Set(cacheKey, new List<ArenaParticipant>());
_db.StringSet(cacheKey, JsonSerializer.Serialize(new List<ArenaParticipant>()));
_logger.Information("[ArenaParticipantsWorker] participants({CacheKey}) is null. set empty list", cacheKey);
return;
}
Expand All @@ -327,7 +329,7 @@ public void PrepareArenaParticipants()
var characterSheet = worldState.GetSheet<CharacterSheet>();
var runeOptionSheet = worldState.GetSheet<RuneOptionSheet>();
var result = GetArenaParticipants(worldState, avatarAddrList, avatarAddrAndScoresWithRank, runeListSheet, costumeStatSheet, characterSheet, runeOptionSheet);
_cache.ArenaParticipantsCache.Set(cacheKey, result, TimeSpan.FromHours(1));
_db.StringSet(cacheKey, JsonSerializer.Serialize(result), TimeSpan.FromHours(1));
sw.Stop();
_logger.Information("[ArenaParticipantsWorker]Set Arena Cache[{CacheKey}]: {Elapsed}", cacheKey, sw.Elapsed);
}
Expand Down
13 changes: 9 additions & 4 deletions NineChronicles.Headless/GraphTypes/StateQuery.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Bencodex;
using Bencodex.Types;
using GraphQL;
Expand Down Expand Up @@ -29,16 +30,20 @@
using NineChronicles.Headless.GraphTypes.States.Models.Item;
using NineChronicles.Headless.GraphTypes.States.Models.Item.Enum;
using NineChronicles.Headless.GraphTypes.States.Models.Table;
using StackExchange.Redis;

namespace NineChronicles.Headless.GraphTypes
{
public partial class StateQuery : ObjectGraphType<StateContext>
{
private readonly Codec _codec = new Codec();

public StateQuery()
private readonly IDatabase Database;

public StateQuery(IDatabase database)
{
Name = "StateQuery";
Database = database;

AvatarStateType.AvatarStateContext? GetAvatarState(StateContext context, Address address)
{
Expand Down Expand Up @@ -679,10 +684,10 @@ public StateQuery()
{
playerScore = (Integer)scores[1];
}
if (context.Source.StateMemoryCache.ArenaParticipantsCache.TryGetValue(cacheKey,
out var cachedResult))

if (Database.StringGet(cacheKey) is { } cachedResult)
{
result = (cachedResult as List<ArenaParticipant>)!;
result = JsonSerializer.Deserialize<List<ArenaParticipant>>(cachedResult.ToString())!;
foreach (var arenaParticipant in result)
{
var (win, lose, _) = ArenaHelper.GetScores(playerScore, arenaParticipant.Score);
Expand Down

0 comments on commit f193395

Please sign in to comment.