Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

class NotificationsManager(IAsyncDocumentSession session) : AbstractSessionManager(session), INotificationsManager
{
const string SingleDocumentId = "NotificationsSettings/All";
Comment thread
rbev marked this conversation as resolved.
static readonly TimeSpan CacheTimeoutDefault = TimeSpan.FromMinutes(5); // Raven requires this to be at least 1 second

public async Task<NotificationsSettings> LoadSettings(TimeSpan? cacheTimeout = null)
{
using var aggressivelyCacheFor = await Session.Advanced.DocumentStore.AggressivelyCacheForAsync(cacheTimeout ?? CacheTimeoutDefault);
var settings = await Session
.LoadAsync<NotificationsSettings>(NotificationsSettings.SingleDocumentId);
.LoadAsync<NotificationsSettings>(SingleDocumentId);

if (settings != null)
{
Expand All @@ -22,7 +23,7 @@ public async Task<NotificationsSettings> LoadSettings(TimeSpan? cacheTimeout = n

settings = new NotificationsSettings
{
Id = NotificationsSettings.SingleDocumentId
Id = SingleDocumentId
};

await Session.StoreAsync(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
class EndpointSettingsStore(IRavenSessionProvider sessionProvider) : IEndpointSettingsStore
{
static string MakeDocumentId(string name) =>
$"{EndpointSettings.CollectionName}/{DeterministicGuid.MakeId(name)}";
$"{CollectionName}/{DeterministicGuid.MakeId(name)}";

public async IAsyncEnumerable<EndpointSettings> GetAllEndpointSettings([EnumeratorCancellation] CancellationToken cancellationToken)
{
using IAsyncDocumentSession session = await sessionProvider.OpenSession(cancellationToken: cancellationToken);
await using IAsyncEnumerator<StreamResult<EndpointSettings>> enumerator = await session
.Advanced
.StreamAsync<EndpointSettings>($"{EndpointSettings.CollectionName}/", token: cancellationToken);
.StreamAsync<EndpointSettings>($"{CollectionName}/", token: cancellationToken);

while (await enumerator.MoveNextAsync() && !cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -46,4 +46,6 @@ public async Task UpdateEndpointSettings(EndpointSettings settings, Cancellation

await session.SaveChangesAsync(cancellationToken);
}

const string CollectionName = "EndpointSettings";
}
26 changes: 16 additions & 10 deletions src/ServiceControl.Persistence.RavenDB/ErrorMessagesDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Raven.Client.Documents.Queries;
using Raven.Client.Documents.Queries.Facets;
using Raven.Client.Documents.Session;
using Recoverability;
using ServiceControl.CompositeViews.Messages;
using ServiceControl.EventLog;
using ServiceControl.MessageFailures;
Expand Down Expand Up @@ -172,6 +173,13 @@ public async Task<FailedMessage[]> FailedMessagesFetch(Guid[] ids)
public async Task StoreFailedErrorImport(FailedErrorImport failure)
{
using var session = await sessionProvider.OpenSession();
// This object's ID is generated externally, but is not in the RavenDB format
// Check that's true to make sure that if it already is that it doesn't get double-formatted
if (!failure.Id.StartsWith(CollectionName))
{
failure.Id = MakeDocumentId(failure.Id);
}
failure.Id = MakeDocumentId(failure.Id);
Comment thread
rbev marked this conversation as resolved.
await session.StoreAsync(failure);

await session.SaveChangesAsync();
Expand Down Expand Up @@ -392,8 +400,8 @@ public async Task EditComment(string groupId, string comment)
{
using var session = await sessionProvider.OpenSession();
var groupComment =
await session.LoadAsync<GroupComment>(GroupComment.MakeId(groupId))
?? new GroupComment { Id = GroupComment.MakeId(groupId) };
await session.LoadAsync<GroupComment>(GroupsDataStore.MakeId(groupId))
?? new GroupComment { Id = GroupsDataStore.MakeId(groupId) };

groupComment.Comment = comment;

Expand All @@ -404,7 +412,7 @@ await session.LoadAsync<GroupComment>(GroupComment.MakeId(groupId))
public async Task DeleteComment(string groupId)
{
using var session = await sessionProvider.OpenSession();
session.Delete(GroupComment.MakeId(groupId));
session.Delete(GroupsDataStore.MakeId(groupId));
await session.SaveChangesAsync();
}

Expand Down Expand Up @@ -512,11 +520,6 @@ public async Task ProcessPendingRetries(DateTime periodFrom, DateTime periodTo,
}
}

class DocumentPatchResult
{
public string Document { get; set; }
}

public async Task<string[]> UnArchiveMessagesByRange(DateTime from, DateTime to)
{
const int Unresolved = (int)FailedMessageStatus.Unresolved;
Expand Down Expand Up @@ -595,7 +598,7 @@ public async Task RevertRetry(string messageUniqueId)
failedMessage?.Status = FailedMessageStatus.Unresolved;

var failedMessageRetry = await session
.LoadAsync<FailedMessageRetry>(FailedMessageRetry.MakeDocumentId(messageUniqueId));
.LoadAsync<FailedMessageRetry>(RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(messageUniqueId));
if (failedMessageRetry != null)
{
session.Delete(failedMessageRetry);
Expand All @@ -607,7 +610,7 @@ public async Task RevertRetry(string messageUniqueId)
public async Task RemoveFailedMessageRetryDocument(string uniqueMessageId)
{
using var session = await sessionProvider.OpenSession();
await session.Advanced.RequestExecutor.ExecuteAsync(new DeleteDocumentCommand(FailedMessageRetry.MakeDocumentId(uniqueMessageId), null), session.Advanced.Context);
await session.Advanced.RequestExecutor.ExecuteAsync(new DeleteDocumentCommand(RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(uniqueMessageId), null), session.Advanced.Context);
}

public async Task<string[]> GetRetryPendingMessages(DateTime from, DateTime to, string queueAddress)
Expand Down Expand Up @@ -674,5 +677,8 @@ public async Task StoreFailedMessagesForTestsOnly(params FailedMessage[] failedM

await session.SaveChangesAsync();
}

public static string MakeDocumentId(string id) => string.Join("/", CollectionName, id);
public const string CollectionName = "FailedErrorImports";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

class MessageRedirectsDataStore(IRavenSessionProvider sessionProvider) : IMessageRedirectsDataStore
{
public const string CollectionId = "messageredirects";

public async Task<MessageRedirectsCollection> GetOrCreate()
{
using var session = await sessionProvider.OpenSession();
var redirects = await session.LoadAsync<MessageRedirectsCollection>(DefaultId);
var redirects = await session.LoadAsync<MessageRedirectsCollection>(CollectionId);

if (redirects != null)
{
Expand All @@ -24,10 +26,8 @@ public async Task<MessageRedirectsCollection> GetOrCreate()
public async Task Save(MessageRedirectsCollection redirects)
{
using var session = await sessionProvider.OpenSession();
await session.StoreAsync(redirects, redirects.ETag, DefaultId);
await session.StoreAsync(redirects, redirects.ETag, CollectionId);
await session.SaveChangesAsync();
}

const string DefaultId = "messageredirects";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class RavenMonitoringDataStore(IRavenSessionProvider sessionProvider) : IMonitoringDataStore
{
public static string MakeDocumentId(Guid id) => $"{KnownEndpoint.CollectionName}/{id}";
public static string MakeDocumentId(Guid id) => $"{KnownEndpointsCollectionName}/{id}";

public async Task CreateIfNotExists(EndpointDetails endpoint)
{
Expand Down Expand Up @@ -111,5 +111,7 @@ public async Task<IReadOnlyList<KnownEndpoint>> GetAllKnownEndpoints()

return knownEndpoints;
}

public const string KnownEndpointsCollectionName = "KnownEndpoints";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public async Task<IList<FailureGroupView>> GetFailureGroupsByClassifier(string c
.Take(200)
.ToListAsync();

var commentIds = groups.Select(x => GroupComment.MakeId(x.Id)).ToArray();
var commentIds = groups.Select(x => MakeId(x.Id)).ToArray();
var comments = await session.Query<GroupComment, GroupCommentIndex>().Where(x => x.Id.In(commentIds))
.ToListAsync(CancellationToken.None);

foreach (var group in groups)
{
group.Comment = comments.FirstOrDefault(x => x.Id == GroupComment.MakeId(group.Id))?.Comment;
group.Comment = comments.FirstOrDefault(x => x.Id == MakeId(group.Id))?.Comment;
}

return groups;
Expand All @@ -41,9 +41,11 @@ public async Task<RetryBatch> GetCurrentForwardingBatch()
{
using var session = await sessionProvider.OpenSession();
var nowForwarding = await session.Include<RetryBatchNowForwarding, RetryBatch>(r => r.RetryBatchId)
.LoadAsync<RetryBatchNowForwarding>(RetryBatchNowForwarding.Id);
.LoadAsync<RetryBatchNowForwarding>(RetryDocumentDataStore.NowForwardingDocumentId);

return nowForwarding == null ? null : await session.LoadAsync<RetryBatch>(nowForwarding.RetryBatchId);
}

public static string MakeId(string groupId) => $"GroupComment/{groupId}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

class RetryHistoryDataStore(IRavenSessionProvider sessionProvider) : IRetryHistoryDataStore
{
const string DocumentId = "RetryOperations/History";

public async Task<RetryHistory> GetRetryHistory()
{
using var session = await sessionProvider.OpenSession();
var id = RetryHistory.MakeId();
var id = DocumentId;
var retryHistory = await session.LoadAsync<RetryHistory>(id);

retryHistory ??= RetryHistory.CreateNew();
retryHistory ??= new() { Id = DocumentId };

return retryHistory;
}
Expand All @@ -21,7 +23,7 @@ public async Task RecordRetryOperationCompleted(string requestId, RetryType retr
string originator, string classifier, bool messageFailed, int numberOfMessagesProcessed, DateTime lastProcessed, int retryHistoryDepth)
{
using var session = await sessionProvider.OpenSession();
var retryHistory = await session.LoadAsync<RetryHistory>(RetryHistory.MakeId()) ?? RetryHistory.CreateNew();
var retryHistory = await session.LoadAsync<RetryHistory>(DocumentId) ?? new() { Id = DocumentId };

retryHistory.AddToUnacknowledged(new UnacknowledgedRetryOperation
{
Expand Down Expand Up @@ -54,7 +56,7 @@ public async Task RecordRetryOperationCompleted(string requestId, RetryType retr
public async Task<bool> AcknowledgeRetryGroup(string groupId)
{
using var session = await sessionProvider.OpenSession();
var retryHistory = await session.LoadAsync<RetryHistory>(RetryHistory.MakeId());
var retryHistory = await session.LoadAsync<RetryHistory>(DocumentId);
if (retryHistory != null)
{
if (retryHistory.Acknowledge(groupId, RetryType.FailureGroup))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task IncrementAttemptCounter(FailedMessageRetry message)
public async Task DeleteFailedMessageRetry(string uniqueMessageId)
{
using var session = await sessionProvider.OpenSession();
await session.Advanced.RequestExecutor.ExecuteAsync(new DeleteDocumentCommand(FailedMessageRetry.MakeDocumentId(uniqueMessageId), null), session.Advanced.Context);
await session.Advanced.RequestExecutor.ExecuteAsync(new DeleteDocumentCommand(RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(uniqueMessageId), null), session.Advanced.Context);
}
}
}
7 changes: 4 additions & 3 deletions src/ServiceControl.Persistence.RavenDB/RetryBatchesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using MessageFailures;
using MessageRedirects;
using Persistence.MessageRedirects;
using Raven.Client.Documents;
using Raven.Client.Documents.Session;
Expand Down Expand Up @@ -39,7 +40,7 @@ public async Task<FailedMessage[]> GetFailedMessages(Dictionary<string, FailedMe

public async Task<RetryBatchNowForwarding> GetRetryBatchNowForwarding() =>
await Session.Include<RetryBatchNowForwarding, RetryBatch>(r => r.RetryBatchId)
.LoadAsync<RetryBatchNowForwarding>(RetryBatchNowForwarding.Id);
.LoadAsync<RetryBatchNowForwarding>(RetryDocumentDataStore.NowForwardingDocumentId);

public async Task<RetryBatch> GetRetryBatch(string retryBatchId, CancellationToken cancellationToken) =>
await Session.LoadAsync<RetryBatch>(retryBatchId, cancellationToken);
Expand All @@ -52,11 +53,11 @@ public async Task<RetryBatch> GetStagingBatch()
}

public async Task Store(RetryBatchNowForwarding retryBatchNowForwarding) =>
await Session.StoreAsync(retryBatchNowForwarding, RetryBatchNowForwarding.Id);
await Session.StoreAsync(retryBatchNowForwarding, RetryDocumentDataStore.NowForwardingDocumentId);

public async Task<MessageRedirectsCollection> GetOrCreateMessageRedirectsCollection()
{
var redirects = await Session.LoadAsync<MessageRedirectsCollection>(MessageRedirectsCollection.DefaultId);
var redirects = await Session.LoadAsync<MessageRedirectsCollection>(MessageRedirectsDataStore.CollectionId);

if (redirects != null)
{
Expand Down
11 changes: 9 additions & 2 deletions src/ServiceControl.Persistence.RavenDB/RetryDocumentDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public async Task<string> CreateBatchDocument(string retrySessionId, string requ
DateTime startTime, DateTime? last = null, string batchName = null, string classifier = null,
string initiatedById = null, string initiatedByName = null, string operationId = null)
{
var batchDocumentId = RetryBatch.MakeDocumentId(Guid.NewGuid().ToString());
var batchDocumentId = MakeDocumentId(Guid.NewGuid().ToString());
failedMessageRetryIds = failedMessageRetryIds.Select(MakeFailedMessageRetriesDocumentId).ToArray();
using var session = await sessionProvider.OpenSession();
await session.StoreAsync(new RetryBatch
{
Expand Down Expand Up @@ -117,7 +118,7 @@ static ICommandData CreateFailedMessageRetryDocument(string batchDocumentId, str
}
};

return new PatchCommandData(FailedMessageRetry.MakeDocumentId(messageId), null, patch: new PatchRequest { Script = "" }, patchIfMissing: patchRequest);
return new PatchCommandData(MakeFailedMessageRetriesDocumentId(messageId), null, patch: new PatchRequest { Script = "" }, patchIfMissing: patchRequest);
}

public async Task GetBatchesForAll(DateTime cutoff, Func<string, DateTime, Task> callback)
Expand Down Expand Up @@ -206,5 +207,11 @@ public async Task<FailureGroupView> QueryFailureGroupViewOnGroupId(string groupI
.FirstOrDefaultAsync(x => x.Id == groupId);
return group;
}

public static string MakeDocumentId(string messageUniqueId) => "RetryBatches/" + messageUniqueId;

public static string MakeFailedMessageRetriesDocumentId(string messageUniqueId) => "FailedMessageRetries/" + messageUniqueId;

public static readonly string NowForwardingDocumentId = MakeDocumentId("NowForwarding");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TrialLicenseDataProvider(IRavenSessionProvider sessionProvider) : ITrialLi
{
using var session = await sessionProvider.OpenSession(cancellationToken: cancellationToken);

var document = await session.LoadAsync<TrialMetadata>(TrialMetadata.TrialMetadataId, cancellationToken);
var document = await session.LoadAsync<TrialMetadata>(TrialMetadataId, cancellationToken);

return document?.TrialEndDate;
}
Expand All @@ -19,8 +19,10 @@ public async Task StoreTrialEndDate(DateOnly trialEndDate, CancellationToken can
{
using var session = await sessionProvider.OpenSession(cancellationToken: cancellationToken);

await session.StoreAsync(new TrialMetadata { TrialEndDate = trialEndDate }, TrialMetadata.TrialMetadataId, cancellationToken);
await session.StoreAsync(new TrialMetadata { TrialEndDate = trialEndDate }, TrialMetadataId, cancellationToken);
await session.SaveChangesAsync(cancellationToken);
}

static string TrialMetadataId = "metadata/trialinformation";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static RavenMonitoringIngestionUnitOfWork()
{
KnownEndpointMetadata = JObject.Parse($@"
{{
""@collection"": ""{KnownEndpoint.CollectionName}"",
""@collection"": ""{RavenMonitoringDataStore.KnownEndpointsCollectionName}"",
""Raven-Clr-Type"": ""{typeof(KnownEndpoint).AssemblyQualifiedName}""
}}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Task RecordFailedProcessingAttempt(
public Task RecordSuccessfulRetry(string retriedMessageUniqueId)
{
var failedMessageDocumentId = FailedMessageIdGenerator.MakeDocumentId(retriedMessageUniqueId);
var failedMessageRetryDocumentId = FailedMessageRetry.MakeDocumentId(retriedMessageUniqueId);
var failedMessageRetryDocumentId = RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(retriedMessageUniqueId);

var patchRequest = new PatchRequest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task Fail_if_failed_imports()
{
await session.StoreAsync(new FailedErrorImport
{
Id = FailedErrorImport.MakeDocumentId(Guid.NewGuid())
Id = ServiceControl.Persistence.RavenDB.ErrorMessagesDataStore.MakeDocumentId(Guid.NewGuid().ToString())
});

BlockToInspectDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using NUnit.Framework;
using Persistence.RavenDB;
using ServiceControl.Recoverability;

[TestFixture]
Expand All @@ -13,9 +14,9 @@ public void Should_Extract_Correct_FailedMessageRetryId_From_FailedMessageId()
var messageId = Guid.NewGuid().ToString();
var failedMessageId = FailedMessageIdGenerator.MakeDocumentId(messageId);

var extractedFailedMessageRetryId = FailedMessageRetry.MakeDocumentId(FailedMessageIdGenerator.GetMessageIdFromDocumentId(failedMessageId));
var extractedFailedMessageRetryId = RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(FailedMessageIdGenerator.GetMessageIdFromDocumentId(failedMessageId));

Assert.That(extractedFailedMessageRetryId, Is.EqualTo(FailedMessageRetry.MakeDocumentId(messageId)));
Assert.That(extractedFailedMessageRetryId, Is.EqualTo(RetryDocumentDataStore.MakeFailedMessageRetriesDocumentId(messageId)));
}
}
}
2 changes: 0 additions & 2 deletions src/ServiceControl.Persistence/EndpointSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ public class EndpointSettings
{
public string Name { get; set; }
public bool TrackInstances { get; set; }

public const string CollectionName = "EndpointSettings";
}
Loading
Loading