Skip to content
Draft
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 @@ -6,15 +6,8 @@ namespace ServiceControl.Audit.Persistence.RavenDB;
using System.Threading;
using System.Threading.Tasks;

class MemoryInformationRetriever(DatabaseConfiguration databaseConfiguration)
class MemoryInformationRetriever(IRavenDocumentStoreProvider documentStoreProvider)
{
// Connection string is composed of the server URL. The ?? operator is needed because ServerUrl
// is populated when running embedded and connection string when running in external mode.
// However, the tricky part is that when tests are run they behave like if it was external mode.
// Only one of ConnectionString and ServerUrl will be non-null, so we'll check ConnectionString first
// to be consistent with the error instance implementation, where ServerUrl always has a value.
readonly HttpClient client = new() { BaseAddress = new Uri(databaseConfiguration.ServerConfiguration.ConnectionString ?? databaseConfiguration.ServerConfiguration.ServerUrl) };

record ResponseDto
{
public MemoryInformation MemoryInformation { get; set; }
Expand All @@ -28,7 +21,10 @@ record MemoryInformation

public async Task<(bool IsHighDirty, string DirtyMemory)> GetMemoryInformation(CancellationToken cancellationToken = default)
{
var httpResponse = await client.GetAsync("/admin/debug/memory/stats?includeThreads=false&includeMappings=false", cancellationToken);
var documentStore = await documentStoreProvider.GetDocumentStore(cancellationToken);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var documentStore = await documentStoreProvider.GetDocumentStore(cancellationToken);
using var documentStore = await documentStoreProvider.GetDocumentStore(cancellationToken);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to dispose it, IDcumentStore is a singleton and is disposed only when the persister is disposed at shutdown.

var client = documentStore.GetRequestExecutor().HttpClient;
var requestUrl = documentStore.Urls[0].TrimEnd('/') + "/admin/debug/memory/stats?includeThreads=false&includeMappings=false";
var httpResponse = await client.GetAsync(requestUrl, cancellationToken);
var responseDto = JsonSerializer.Deserialize<ResponseDto>(await httpResponse.Content.ReadAsStringAsync(cancellationToken));

return responseDto.MemoryInformation is null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@ namespace ServiceControl.Persistence.RavenDB;
using System.Threading;
using System.Threading.Tasks;

class MemoryInformationRetriever(RavenPersisterSettings persisterSettings)
class MemoryInformationRetriever(IRavenDocumentStoreProvider documentStoreProvider)
{
// Connection string is composed of the server URL. The ?? operator is needed because ServerUrl
// is populated when running embedded and connection string when running in external mode.
// However, the tricky part is that when tests are run they behave like if it was external mode.
// ServerUrl is always populated by the persister settings, hence the code first checks for the
// presence of a connection string, and if null, falls back to ServerUrl
readonly HttpClient client = new() { BaseAddress = new Uri(persisterSettings.ConnectionString ?? persisterSettings.ServerUrl) };

record ResponseDto
{
public MemoryInformation MemoryInformation { get; set; }
Expand All @@ -28,7 +21,10 @@ record MemoryInformation

public async Task<(bool IsHighDirty, string DirtyMemory)> GetMemoryInformation(CancellationToken cancellationToken = default)
{
var httpResponse = await client.GetAsync("/admin/debug/memory/stats?includeThreads=false&includeMappings=false", cancellationToken);
var documentStore = await documentStoreProvider.GetDocumentStore(cancellationToken);
var client = documentStore.GetRequestExecutor().HttpClient;
var requestUrl = documentStore.Urls[0].TrimEnd('/') + "/admin/debug/memory/stats?includeThreads=false&includeMappings=false";
var httpResponse = await client.GetAsync(requestUrl, cancellationToken);
var responseDto = JsonSerializer.Deserialize<ResponseDto>(await httpResponse.Content.ReadAsStringAsync(cancellationToken));

return responseDto.MemoryInformation is null
Expand Down