2020-08-09 01:31:16 +02:00
|
|
|
|
namespace Giants.Services
|
|
|
|
|
{
|
|
|
|
|
using System;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
using System.Collections.Generic;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
using System.Linq;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
using Giants.Services.Core;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
using Microsoft.Azure.Cosmos;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-08-09 02:26:41 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
|
|
|
|
|
public class CosmosDbServerRegistryStore : IServerRegistryStore
|
|
|
|
|
{
|
2020-08-09 02:26:41 +02:00
|
|
|
|
private readonly ILogger<CosmosDbServerRegistryStore> logger;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
private readonly IConfiguration configuration;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
private readonly IMemoryCache memoryCache;
|
|
|
|
|
private readonly IDateTimeProvider dateTimeProvider;
|
2022-09-09 07:31:24 +02:00
|
|
|
|
private readonly CosmosDbClient client;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
private readonly TimeSpan timeoutPeriod;
|
2020-08-12 07:15:13 +02:00
|
|
|
|
|
|
|
|
|
private const int ServerRefreshIntervalInMinutes = 1;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
|
2020-08-09 02:26:41 +02:00
|
|
|
|
public CosmosDbServerRegistryStore(
|
|
|
|
|
ILogger<CosmosDbServerRegistryStore> logger,
|
2020-08-12 06:03:02 +02:00
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
IMemoryCache memoryCache,
|
2022-09-09 07:31:24 +02:00
|
|
|
|
IDateTimeProvider dateTimeProvider,
|
|
|
|
|
CosmosDbClient client)
|
2020-08-09 01:31:16 +02:00
|
|
|
|
{
|
2020-08-09 02:26:41 +02:00
|
|
|
|
this.logger = logger;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
this.configuration = configuration;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
this.memoryCache = memoryCache;
|
|
|
|
|
this.dateTimeProvider = dateTimeProvider;
|
2022-09-09 07:31:24 +02:00
|
|
|
|
this.client = client;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
this.timeoutPeriod = TimeSpan.FromMinutes(Convert.ToDouble(this.configuration["ServerTimeoutPeriodInMinutes"]));
|
2020-08-09 01:31:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<ServerInfo>> GetServerInfos(
|
2020-08-09 02:26:41 +02:00
|
|
|
|
Expression<Func<ServerInfo, bool>> whereExpression = null,
|
2020-08-16 10:47:25 +02:00
|
|
|
|
bool includeExpired = false,
|
2020-08-09 02:26:41 +02:00
|
|
|
|
string partitionKey = null)
|
2020-08-09 01:31:16 +02:00
|
|
|
|
{
|
2022-09-05 05:29:59 +02:00
|
|
|
|
ConcurrentDictionary<string, IList<ServerInfo>> serverInfo = await this.memoryCache.GetOrCreateAsync(CacheKeys.ServerInfo, this.PopulateCache);
|
2020-08-12 06:03:02 +02:00
|
|
|
|
|
|
|
|
|
IQueryable<ServerInfo> serverInfoQuery = serverInfo
|
|
|
|
|
.Values
|
2022-09-05 05:29:59 +02:00
|
|
|
|
.SelectMany(s => s)
|
2020-08-12 06:03:02 +02:00
|
|
|
|
.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (whereExpression != null)
|
|
|
|
|
{
|
|
|
|
|
serverInfoQuery = serverInfoQuery.Where(whereExpression);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 10:47:25 +02:00
|
|
|
|
if (!includeExpired)
|
|
|
|
|
{
|
|
|
|
|
serverInfoQuery = serverInfoQuery.Where(c => c.LastHeartbeat > this.dateTimeProvider.UtcNow - this.timeoutPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serverInfoQuery
|
2020-08-12 06:03:02 +02:00
|
|
|
|
.ToList();
|
2020-08-09 01:31:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 02:26:41 +02:00
|
|
|
|
public async Task<IEnumerable<TSelect>> GetServerInfos<TSelect>(
|
|
|
|
|
Expression<Func<ServerInfo, TSelect>> selectExpression,
|
2020-08-16 10:47:25 +02:00
|
|
|
|
bool includeExpired = false,
|
2020-08-09 02:26:41 +02:00
|
|
|
|
Expression<Func<ServerInfo, bool>> whereExpression = null,
|
|
|
|
|
string partitionKey = null)
|
|
|
|
|
{
|
2022-09-05 05:29:59 +02:00
|
|
|
|
ConcurrentDictionary<string, IList<ServerInfo>> serverInfo = await this.memoryCache.GetOrCreateAsync(CacheKeys.ServerInfo, this.PopulateCache);
|
2020-08-12 06:03:02 +02:00
|
|
|
|
|
|
|
|
|
IQueryable<ServerInfo> serverInfoQuery = serverInfo
|
|
|
|
|
.Values
|
2022-09-05 05:29:59 +02:00
|
|
|
|
.SelectMany(s => s)
|
2020-08-12 06:03:02 +02:00
|
|
|
|
.AsQueryable();
|
2020-08-09 02:52:26 +02:00
|
|
|
|
|
2020-08-12 06:03:02 +02:00
|
|
|
|
if (serverInfoQuery != null)
|
|
|
|
|
{
|
|
|
|
|
serverInfoQuery = serverInfoQuery.Where(whereExpression);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 10:47:25 +02:00
|
|
|
|
if (!includeExpired)
|
|
|
|
|
{
|
|
|
|
|
serverInfoQuery = serverInfoQuery.Where(c => c.LastHeartbeat > this.dateTimeProvider.UtcNow - this.timeoutPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return serverInfoQuery
|
2020-08-12 06:03:02 +02:00
|
|
|
|
.Select(selectExpression)
|
|
|
|
|
.ToList();
|
2020-08-09 02:26:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 01:31:16 +02:00
|
|
|
|
public async Task UpsertServerInfo(ServerInfo serverInfo)
|
|
|
|
|
{
|
2020-08-09 02:52:26 +02:00
|
|
|
|
ArgumentUtility.CheckForNull(serverInfo, nameof(serverInfo));
|
|
|
|
|
|
2020-08-12 06:03:02 +02:00
|
|
|
|
// Check cache before we write to store
|
2022-09-05 05:29:59 +02:00
|
|
|
|
ConcurrentDictionary<string, IList<ServerInfo>> allServerInfo = await this.memoryCache.GetOrCreateAsync(CacheKeys.ServerInfo, this.PopulateCache);
|
2020-08-12 06:03:02 +02:00
|
|
|
|
|
|
|
|
|
if (allServerInfo.ContainsKey(serverInfo.HostIpAddress))
|
|
|
|
|
{
|
2022-09-05 05:29:59 +02:00
|
|
|
|
IList<ServerInfo> serverInfoForHostIp = allServerInfo[serverInfo.HostIpAddress];
|
|
|
|
|
ServerInfo existingServerInfo = FindExistingServerForHostIp(serverInfoForHostIp, serverInfo);
|
2020-08-12 07:15:13 +02:00
|
|
|
|
|
|
|
|
|
// DDOS protection: skip write to Cosmos if parameters have not changed,
|
|
|
|
|
// or it's not been long enough.
|
2022-09-05 05:29:59 +02:00
|
|
|
|
if (existingServerInfo != null && Math.Abs((existingServerInfo.LastHeartbeat - serverInfo.LastHeartbeat).TotalMinutes) < ServerRefreshIntervalInMinutes)
|
2020-08-12 06:03:02 +02:00
|
|
|
|
{
|
|
|
|
|
this.logger.LogInformation("State for {IPAddress} is unchanged. Skipping write to store.", serverInfo.HostIpAddress);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
this.logger.LogInformation("State for {IPAddress} has changed. Committing update to store.", serverInfo.HostIpAddress);
|
|
|
|
|
await this.client.UpsertItem(
|
|
|
|
|
item: serverInfo,
|
|
|
|
|
partitionKey: new PartitionKey(serverInfo.DocumentType));
|
2020-08-12 06:03:02 +02:00
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
// Update cache
|
|
|
|
|
if (existingServerInfo != null)
|
|
|
|
|
{
|
|
|
|
|
var newServerInfo = serverInfoForHostIp.Where(s => !s.Equals(serverInfo)).ToList();
|
|
|
|
|
newServerInfo.Add(serverInfo);
|
|
|
|
|
allServerInfo[serverInfo.HostIpAddress] = newServerInfo;
|
2020-08-12 06:03:02 +02:00
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
this.logger.LogInformation("Updating cache for request from {IPAddress} (replaced existing server).", serverInfo.HostIpAddress);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
allServerInfo[serverInfo.HostIpAddress].Add(serverInfo);
|
|
|
|
|
this.logger.LogInformation("Updating cache for request from {IPAddress} (added new server).", serverInfo.HostIpAddress);
|
|
|
|
|
}
|
2020-08-12 06:03:02 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-09-05 05:29:59 +02:00
|
|
|
|
// Update cache
|
|
|
|
|
await this.client.UpsertItem(
|
|
|
|
|
item: serverInfo,
|
|
|
|
|
partitionKey: new PartitionKey(serverInfo.DocumentType));
|
|
|
|
|
|
|
|
|
|
this.logger.LogInformation("Updating cache for request from {IPAddress} (no existing servers).", serverInfo.HostIpAddress);
|
|
|
|
|
allServerInfo.TryAdd(serverInfo.HostIpAddress, new List<ServerInfo>() { serverInfo });
|
2020-08-12 06:03:02 +02:00
|
|
|
|
}
|
2020-08-09 02:26:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 07:11:10 +02:00
|
|
|
|
public async Task DeleteServer(string id, string partitionKey = null)
|
|
|
|
|
{
|
|
|
|
|
await this.client.DeleteItem<ServerInfo>(id, partitionKey);
|
2020-08-12 06:03:02 +02:00
|
|
|
|
|
|
|
|
|
// Remove from cache
|
2022-09-05 05:29:59 +02:00
|
|
|
|
ConcurrentDictionary<string, IList<ServerInfo>> allServerInfo = await this.memoryCache.GetOrCreateAsync(CacheKeys.ServerInfo, this.PopulateCache);
|
|
|
|
|
if (allServerInfo.ContainsKey(id))
|
|
|
|
|
{
|
|
|
|
|
var serverInfoCopy = allServerInfo[id].Where(s => s.id != id).ToList();
|
|
|
|
|
if (!serverInfoCopy.Any())
|
|
|
|
|
{
|
|
|
|
|
// No remaining servers, remove the key
|
|
|
|
|
allServerInfo.TryRemove(id, out IList<ServerInfo> _);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Shallow-copy and replace to keep thread safety guarantee
|
|
|
|
|
allServerInfo[id] = serverInfoCopy;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-10 07:11:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 02:26:41 +02:00
|
|
|
|
public async Task DeleteServers(IEnumerable<string> ids, string partitionKey = null)
|
|
|
|
|
{
|
2020-08-09 02:52:26 +02:00
|
|
|
|
ArgumentUtility.CheckEnumerableForNullOrEmpty(ids, nameof(ids));
|
|
|
|
|
|
2020-08-09 02:26:41 +02:00
|
|
|
|
foreach (string id in ids)
|
|
|
|
|
{
|
|
|
|
|
this.logger.LogInformation("Deleting server for host IP {IPAddress}", id);
|
|
|
|
|
|
2020-08-12 06:03:02 +02:00
|
|
|
|
await this.DeleteServer(id, partitionKey);
|
2020-08-09 02:26:41 +02:00
|
|
|
|
}
|
2020-08-09 01:31:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
private async Task<ConcurrentDictionary<string, IList<ServerInfo>>> PopulateCache(ICacheEntry entry)
|
2020-08-12 06:03:02 +02:00
|
|
|
|
{
|
|
|
|
|
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
|
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
var allServerInfo = (await this.client.GetItems<ServerInfo>());
|
|
|
|
|
var serverInfoDictionary = new ConcurrentDictionary<string, IList<ServerInfo>>();
|
|
|
|
|
|
|
|
|
|
foreach (var serverInfo in allServerInfo)
|
|
|
|
|
{
|
|
|
|
|
if (!serverInfoDictionary.ContainsKey(serverInfo.HostIpAddress))
|
|
|
|
|
{
|
|
|
|
|
serverInfoDictionary[serverInfo.HostIpAddress] = new List<ServerInfo>() { serverInfo };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
serverInfoDictionary[serverInfo.HostIpAddress].Add(serverInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-12 06:03:02 +02:00
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
return serverInfoDictionary;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ServerInfo FindExistingServerForHostIp(IList<ServerInfo> serverInfoForHostIp, ServerInfo candidateServerInfo)
|
|
|
|
|
{
|
|
|
|
|
return serverInfoForHostIp.FirstOrDefault(s => s.Equals(candidateServerInfo));
|
2020-08-12 06:03:02 +02:00
|
|
|
|
}
|
2020-08-09 01:31:16 +02:00
|
|
|
|
}
|
2020-08-09 02:26:41 +02:00
|
|
|
|
}
|