Files
mapo-core/MP.IOC/Services/RedisWeightProvider.cs
T

217 lines
8.0 KiB
C#

using MP.Core.DTO;
using NLog;
using StackExchange.Redis;
using ZiggyCreatures.Caching.Fusion;
namespace MP.IOC.Services
{
public class RedisWeightProvider : IWeightProvider
{
#region Public Constructors
public RedisWeightProvider(
IConnectionMultiplexer mux,
IFusionCache cache,
IHttpClientFactory httpClientFactory,
IConfiguration config)
{
_cache = cache;
_config = config;
_httpClientFactory = httpClientFactory;
_db = mux.GetDatabase();
_mux = mux;
_defaultOld = config.GetValue<int>("RouteMan:DefaultWeightOld", 100);
_defaultNew = config.GetValue<int>("RouteMan:DefaultWeightNew", 0);
_redisBaseKey = config.GetValue<string>("ServerConf:RedisBaseKey") ?? "MP_IOC";
// Leggiamo l'URL base del gateway dal file di configurazione dell'Admin
// es. "https://10.74.83.100:5290/MP/RIOC/api/admin/cache/purge-route/"
_gatewayPurgeUrl = config.GetValue<string>("ServerConf:GatewayPurgeUrl") ?? "https://localhost:7120/MP/RIOC/api/admin/cache/purge-route/";
_keyPrefix = $"{_redisBaseKey}:route_weight:";
}
#endregion Public Constructors
#region Public Methods
public async Task<List<WeightDTO>> GetAllWeightsAsync()
{
var result = new List<WeightDTO>();
var server = _mux.GetServer(_mux.GetEndPoints().First());
if (server.IsReplica)
{
return result;
}
await foreach (var key in server.KeysAsync(pattern: $"{_keyPrefix}*"))
{
var methodName = KeyToString(key.ToString());
if (string.IsNullOrEmpty(methodName)) continue;
var oldVal = _db.HashGet(key, "old");
var newVal = _db.HashGet(key, "new");
int oldW = 100;
int newW = 0;
if (!oldVal.IsNull && int.TryParse(oldVal.ToString(), out var parsedOld))
oldW = Math.Clamp(parsedOld, 0, 100);
if (!newVal.IsNull && int.TryParse(newVal.ToString(), out var parsedNew))
newW = Math.Clamp(parsedNew, 0, 100);
result.Add(new WeightDTO { Method = methodName, OldWeight = oldW, NewWeight = newW });
}
// riordino desc x NEW poi alfabetico...
result = result
.OrderByDescending(x => x.NewWeight)
.ThenBy(x => x.Method)
.ToList();
return result;
}
// API per aggiornare i pesi a runtime (opzionale)
public void SetWeights(string method, int oldWeight, int newWeight)
{
var key = _keyPrefix + (string.IsNullOrEmpty(method) ? "unknown" : method);
_db.HashSet(key, new HashEntry[] {
new HashEntry("old", Math.Clamp(oldWeight,0,100)),
new HashEntry("new", Math.Clamp(newWeight,0,100))
});
ResetFusionCache(method);
}
public bool UpsertWeight(WeightDTO updRecord)
{
if (updRecord == null || string.IsNullOrEmpty(updRecord.Method))
return false;
// 1. Scrivi su Redis (Sorgente dati reale)
var key = _keyPrefix + updRecord.Method;
_db.HashSet(key, new HashEntry[] {
new HashEntry("old", Math.Clamp(updRecord.OldWeight, 0, 100)),
new HashEntry("new", Math.Clamp(updRecord.NewWeight, 0, 100))
});
ResetFusionCache(updRecord.Method);
return true;
}
#endregion Public Methods
#region Private Fields
private static string _keyPrefix = "route_weight:";
private static string _redisBaseKey = "";
private static Logger Log = LogManager.GetCurrentClassLogger();
private readonly IFusionCache _cache;
private readonly IConfiguration _config;
private readonly IDatabase _db;
private readonly int _defaultNew;
private readonly int _defaultOld;
private readonly string _gatewayPurgeUrl;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IConnectionMultiplexer _mux;
#endregion Private Fields
#region Private Methods
private string KeyToString(string key)
{
if (string.IsNullOrEmpty(key)) return "";
var prefix = _keyPrefix ?? "";
if (key.StartsWith(prefix))
return key.Substring(prefix.Length);
return key;
}
private void ResetFusionCache(string method)
{
//try
//{
// // Sfrutta la factory per prendere un client ottimizzato e pulito
// using var client = _httpClientFactory.CreateClient();
// // Componiamo l'URL finale pulito (es. .../purge-route/SIMUL_01)
// var finalUrl = $"{_gatewayPurgeUrl.TrimEnd('/')}/{updRecord.Method}";
// // Inviamo la POST (passiamo null come HttpContent perché non serve un Body)
// var response = client.PostAsync(finalUrl, null);
// if (response.IsSuccessStatusCode)
// {
// Log.Info($"[CACHE] Sfratto RAM notificato con successo al Gateway per il metodo: {updRecord.Method}");
// }
// else
// {
// Log.Warn($"[CACHE] Il gateway ha risposto con codice {response.StatusCode} alla purga di {updRecord.Method}");
// }
//}
//catch (Exception ex)
//{
// // Usiamo un log di errore ma non blocchiamo l'applicazione Admin
// // se il gateway in quel momento fosse offline o irraggiungibile
// Log.Error(ex, $"[CACHE] Impossibile raggiungere il gateway per spurgare la rotta: {updRecord.Method}");
//}
// Grazie al Backplane Pub/Sub, la RAM verrà azzerata istantaneamente su TUTTI i server.
var cacheKey = $"weights:{method}";
_cache.Remove(cacheKey);
}
#endregion Private Methods
#if false
/// <summary>
/// Ritorna (oldWeight, newWeight) per il metodo. Se non esiste, crea la chiave con i default.
/// </summary>
public (int oldWeight, int newWeight) GetWeightsFor(string method)
{
if (string.IsNullOrEmpty(method)) method = "unknown";
var key = _keyPrefix + method;
// Leggi entrambi i campi
var oldVal = _db.HashGet(key, "old");
var newVal = _db.HashGet(key, "new");
// Se entrambi mancanti, inizializza con default (usando HSet con When.NotExists per evitare overwrite)
if (oldVal.IsNull && newVal.IsNull)
{
// Imposta i campi singolarmente con When.NotExists per evitare overwrite
_db.HashSet(key, "old", _defaultOld, When.NotExists);
_db.HashSet(key, "new", _defaultNew, When.NotExists);
// Rileggi per essere sicuri
oldVal = _db.HashGet(key, "old");
newVal = _db.HashGet(key, "new");
}
// Se uno dei due manca, impostalo al default (non sovrascrive l'altro)
if (oldVal.IsNull)
{
_db.HashSet(key, "old", _defaultOld, When.NotExists);
oldVal = _defaultOld;
}
if (newVal.IsNull)
{
_db.HashSet(key, "new", _defaultNew, When.NotExists);
newVal = _defaultNew;
}
if (!int.TryParse(oldVal.ToString(), out var oldW)) oldW = _defaultOld;
if (!int.TryParse(newVal.ToString(), out var newW)) newW = _defaultNew;
// clamp 0..100
oldW = Math.Clamp(oldW, 0, 100);
newW = Math.Clamp(newW, 0, 100);
return (oldW, newW);
}
#endif
}
}