Skip to content

Commit

Permalink
Fix admin logs duplicate id error when running tests (#16203)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSmugleaf authored May 7, 2023
1 parent c48f17a commit d072cb6
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 108 deletions.
5 changes: 0 additions & 5 deletions Content.Server/Administration/Logs/AdminLogManager.Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ private void CacheLog(AdminLog log)
CacheLog(record);
}

private void CacheLog(QueuedLog log)
{
CacheLog(log.Log);
}

private void CacheLog(SharedAdminLog log)
{
// TODO ADMIN LOGS remove redundant data and don't do a dictionary lookup per log
Expand Down
10 changes: 6 additions & 4 deletions Content.Server/Administration/Logs/AdminLogManager.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Content.Server.Administration.Logs.Converters;
using Content.Server.Database;
using Robust.Server.GameObjects;
using Robust.Server.Player;

Expand Down Expand Up @@ -32,10 +33,10 @@ private void InitializeJson()
_sawmill.Debug($"Admin log converters found: {string.Join(" ", converterNames)}");
}

private (JsonDocument json, HashSet<Guid> players, Dictionary<int, string?> entities) ToJson(
private (JsonDocument Json, HashSet<Guid> Players, List<AdminLogEntity> Entities) ToJson(
Dictionary<string, object?> properties)
{
var entities = new Dictionary<int, string?>();
var entities = new Dictionary<EntityUid, AdminLogEntity>();
var players = new HashSet<Guid>();
var parsed = new Dictionary<string, object?>();

Expand Down Expand Up @@ -69,14 +70,15 @@ private void InitializeJson()
? metadata.EntityName
: null;

entities.TryAdd((int) uid, entityName);
// TODO set the id too whenever we feel like running a migration for 10 hours
entities.TryAdd(uid, new AdminLogEntity { Name = entityName });

if (_entityManager.TryGetComponent(uid, out ActorComponent? actor))
{
players.Add(actor.PlayerSession.UserId.UserId);
}
}

return (JsonSerializer.SerializeToDocument(parsed, _jsonOptions), players, entities);
return (JsonSerializer.SerializeToDocument(parsed, _jsonOptions), players, entities.Values.ToList());
}
}
43 changes: 25 additions & 18 deletions Content.Server/Administration/Logs/AdminLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public sealed partial class AdminLogManager : SharedAdminLogManager, IAdminLogMa

// Per update
private TimeSpan _nextUpdateTime;
private readonly ConcurrentQueue<QueuedLog> _logQueue = new();
private readonly ConcurrentQueue<QueuedLog> _preRoundLogQueue = new();
private readonly ConcurrentQueue<AdminLog> _logQueue = new();
private readonly ConcurrentQueue<AdminLog> _preRoundLogQueue = new();

// Per round
private int _currentRoundId;
Expand Down Expand Up @@ -171,7 +171,7 @@ private async Task SaveLogs()
_nextUpdateTime = _timing.RealTime.Add(_queueSendDelay);

// TODO ADMIN LOGS array pool
var copy = new List<QueuedLog>(_logQueue.Count + _preRoundLogQueue.Count);
var copy = new List<AdminLog>(_logQueue.Count + _preRoundLogQueue.Count);

copy.AddRange(_logQueue);
_logQueue.Clear();
Expand All @@ -183,10 +183,10 @@ private async Task SaveLogs()
}
else
{
foreach (var queued in _preRoundLogQueue)
foreach (var log in _preRoundLogQueue)
{
queued.Log.RoundId = _currentRoundId;
CacheLog(queued);
log.RoundId = _currentRoundId;
CacheLog(log);
}

copy.AddRange(_preRoundLogQueue);
Expand Down Expand Up @@ -231,6 +231,17 @@ public void RunLevelChanged(GameRunLevel level)
{
Interlocked.Exchange(ref _currentLogId, 0);

if (!_preRoundLogQueue.IsEmpty)
{
// This technically means that you could get pre-round logs from
// a previous round passed onto the next one
// If this happens please file a complaint with your nearest lottery
foreach (var log in _preRoundLogQueue)
{
log.Id = NextLogId;
}
}

if (_metricsEnabled)
{
PreRoundQueueCapReached.Set(0);
Expand All @@ -240,30 +251,26 @@ public void RunLevelChanged(GameRunLevel level)
}
}

private async void Add(LogType type, LogImpact impact, string message, JsonDocument json, HashSet<Guid> players, Dictionary<int, string?> entities)
private void Add(LogType type, LogImpact impact, string message, JsonDocument json, HashSet<Guid> players, List<AdminLogEntity> entities)
{
var logId = NextLogId;
var date = DateTime.UtcNow;

var log = new AdminLog
{
Id = logId,
Id = NextLogId,
RoundId = _currentRoundId,
Type = type,
Impact = impact,
Date = date,
Date = DateTime.UtcNow,
Message = message,
Json = json,
Players = new List<AdminLogPlayer>(players.Count)
Players = new List<AdminLogPlayer>(players.Count),
Entities = entities
};

var queued = new QueuedLog(log, entities);

foreach (var id in players)
{
var player = new AdminLogPlayer
{
LogId = logId,
LogId = log.Id,
PlayerUserId = id
};

Expand All @@ -272,11 +279,11 @@ private async void Add(LogType type, LogImpact impact, string message, JsonDocum

if (_runLevel == GameRunLevel.PreRoundLobby)
{
_preRoundLogQueue.Enqueue(queued);
_preRoundLogQueue.Enqueue(log);
}
else
{
_logQueue.Enqueue(queued);
_logQueue.Enqueue(log);
CacheLog(log);
}
}
Expand Down
22 changes: 0 additions & 22 deletions Content.Server/Administration/Logs/QueuedLog.cs

This file was deleted.

21 changes: 2 additions & 19 deletions Content.Server/Database/ServerDbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.EntityFrameworkCore;
using Robust.Shared.Enums;
using Robust.Shared.Network;
using Robust.Shared.Utility;

namespace Content.Server.Database
{
Expand Down Expand Up @@ -717,26 +716,10 @@ public async Task UpdateAdminRankAsync(AdminRank rank, CancellationToken cancel)
return (server, false);
}

public virtual async Task AddAdminLogs(List<QueuedLog> logs)
public async Task AddAdminLogs(List<AdminLog> logs)
{
await using var db = await GetDb();

var entities = new Dictionary<int, AdminLogEntity>();

foreach (var (log, entityData) in logs)
{
var logEntities = new List<AdminLogEntity>(entityData.Count);
foreach (var (id, name) in entityData)
{
var entity = entities.GetOrNew(id);
entity.Name = name;
logEntities.Add(entity);
}

log.Entities = logEntities;
db.DbContext.AdminLog.Add(log);
}

db.DbContext.AdminLog.AddRange(logs);
await db.DbContext.SaveChangesAsync();
}

Expand Down
4 changes: 2 additions & 2 deletions Content.Server/Database/ServerDbManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Task<int> AddConnectionLogAsync(
#region Admin Logs

Task<Server> AddOrGetServer(string serverName);
Task AddAdminLogs(List<QueuedLog> logs);
Task AddAdminLogs(List<AdminLog> logs);
IAsyncEnumerable<string> GetAdminLogMessages(LogFilter? filter = null);
IAsyncEnumerable<SharedAdminLog> GetAdminLogs(LogFilter? filter = null);
IAsyncEnumerable<JsonDocument> GetAdminLogsJson(LogFilter? filter = null);
Expand Down Expand Up @@ -564,7 +564,7 @@ public async Task<Server> AddOrGetServer(string serverName)
return server;
}

public Task AddAdminLogs(List<QueuedLog> logs)
public Task AddAdminLogs(List<AdminLog> logs)
{
DbWriteOpsMetric.Inc();
return RunDbCommand(() => _db.AddAdminLogs(logs));
Expand Down
38 changes: 0 additions & 38 deletions Content.Server/Database/ServerDbSqlite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Administration.Logs;
using Content.Server.IP;
using Content.Server.Preferences.Managers;
using Content.Shared.CCVar;
using Microsoft.EntityFrameworkCore;
using Robust.Shared.Configuration;
using Robust.Shared.Network;
using Robust.Shared.Utility;

namespace Content.Server.Database
{
Expand Down Expand Up @@ -475,42 +473,6 @@ public override async Task<int> AddNewRound(Server server, params Guid[] playerI
return round.Id;
}

public override async Task AddAdminLogs(List<QueuedLog> logs)
{
await using var db = await GetDb();

var nextId = 1;
if (await db.DbContext.AdminLog.AnyAsync())
{
nextId = db.DbContext.AdminLog.Max(round => round.Id) + 1;
}

var entities = new Dictionary<int, AdminLogEntity>();

foreach (var (log, entityData) in logs)
{
log.Id = nextId++;

var logEntities = new List<AdminLogEntity>(entityData.Count);
foreach (var (id, name) in entityData)
{
var entity = entities.GetOrNew(id);
entity.Name = name;
logEntities.Add(entity);
}

foreach (var player in log.Players)
{
player.LogId = log.Id;
}

log.Entities = logEntities;
db.DbContext.AdminLog.Add(log);
}

await db.DbContext.SaveChangesAsync();
}

public override async Task<int> AddAdminNote(AdminNote note)
{
await using (var db = await GetDb())
Expand Down

0 comments on commit d072cb6

Please sign in to comment.