Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lt 5621 recreate snapshot #537

Merged
merged 3 commits into from
Jul 19, 2024
Merged
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
13 changes: 13 additions & 0 deletions src/MarginTrading.Backend.Core/Services/ISnapshotTrackerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2019 Lykke Corp.
// See the LICENSE file in the project root for more information.

using System.Threading.Tasks;

namespace MarginTrading.Backend.Core.Services
{
public interface ISnapshotTrackerService
{
Task SetShouldRecreateSnapshot(bool value);
Task<bool> GetShouldRecreateSnapshot();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class SnapshotService : ISnapshotService
private readonly ILog _log;
private readonly IFinalSnapshotCalculator _finalSnapshotCalculator;
private readonly ISnapshotStatusTracker _snapshotStatusTracker;
private readonly ISnapshotTrackerService _snapshotTrackerService;
private readonly MarginTradingSettings _settings;

private static readonly SemaphoreSlim Lock = new SemaphoreSlim(1, 1);
Expand All @@ -56,6 +57,7 @@ public SnapshotService(
ILog log,
IFinalSnapshotCalculator finalSnapshotCalculator,
ISnapshotStatusTracker snapshotStatusTracker,
ISnapshotTrackerService snapshotTrackerService,
MarginTradingSettings settings)
{
_scheduleSettingsCacheService = scheduleSettingsCacheService;
Expand All @@ -71,6 +73,7 @@ public SnapshotService(
_log = log;
_finalSnapshotCalculator = finalSnapshotCalculator;
_snapshotStatusTracker = snapshotStatusTracker;
_snapshotTrackerService = snapshotTrackerService;
_settings = settings;
}

Expand Down Expand Up @@ -204,6 +207,10 @@ await _log.WriteInfoAsync(nameof(SnapshotService), nameof(MakeTradingDataSnapsho
await _tradingEngineSnapshotsRepository.AddAsync(snapshot);

_snapshotStatusTracker.SnapshotCreated();
if (status == SnapshotStatus.Draft)
{
await _snapshotTrackerService.SetShouldRecreateSnapshot(false);
}

await _log.WriteInfoAsync(nameof(SnapshotService), nameof(MakeTradingDataSnapshot),
$"Trading data snapshot was written to the storage. {msg}");
Expand Down
4 changes: 4 additions & 0 deletions src/MarginTrading.Backend.Services/Modules/ServicesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<ConfigurationValidator>()
.As<IConfigurationValidator>()
.SingleInstance();

builder.RegisterType<SnapshotTrackerService>()
.As<ISnapshotTrackerService>()
.SingleInstance();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ public class PositionHistoryHandler : IPositionHistoryHandler
private readonly IRabbitMqNotifyService _rabbitMqNotifyService;
private readonly IDateService _dateService;
private readonly IAccountsCacheService _accountsCacheService;
private readonly ISnapshotTrackerService _snapshotTrackerService;

public PositionHistoryHandler(ICqrsSender cqrsSender,
IConvertService convertService,
IRabbitMqNotifyService rabbitMqNotifyService,
IDateService dateService,
IAccountsCacheService accountsCacheService)
IAccountsCacheService accountsCacheService,
ISnapshotTrackerService snapshotTrackerService)
{
_cqrsSender = cqrsSender;
_convertService = convertService;
_rabbitMqNotifyService = rabbitMqNotifyService;
_dateService = dateService;
_accountsCacheService = accountsCacheService;
_snapshotTrackerService = snapshotTrackerService;
}

public Task HandleOpenPosition(Position position, string additionalInfo, PositionOpenMetadata metadata)
Expand All @@ -46,7 +49,7 @@ public Task HandleOpenPosition(Position position, string additionalInfo, Positio
return _rabbitMqNotifyService.PositionHistory(historyEvent);
}

public Task HandleClosePosition(Position position, DealContract deal, string additionalInfo)
public async Task HandleClosePosition(Position position, DealContract deal, string additionalInfo)
{
var positionClosedEvent = CreatePositionClosedEvent(position, deal);
_cqrsSender.PublishEvent(positionClosedEvent);
Expand All @@ -55,10 +58,11 @@ public Task HandleClosePosition(Position position, DealContract deal, string add
PositionHistoryTypeContract.Close,
deal,
additionalInfo);
return _rabbitMqNotifyService.PositionHistory(historyEvent);
await _rabbitMqNotifyService.PositionHistory(historyEvent);
await _snapshotTrackerService.SetShouldRecreateSnapshot(true);
}

public Task HandlePartialClosePosition(Position position, DealContract deal, string additionalInfo)
public async Task HandlePartialClosePosition(Position position, DealContract deal, string additionalInfo)
{
var positionClosedEvent = CreatePositionClosedEvent(position, deal);
_cqrsSender.PublishEvent(positionClosedEvent);
Expand All @@ -67,7 +71,8 @@ public Task HandlePartialClosePosition(Position position, DealContract deal, str
PositionHistoryTypeContract.PartiallyClose,
deal,
additionalInfo);
return _rabbitMqNotifyService.PositionHistory(historyEvent);
await _rabbitMqNotifyService.PositionHistory(historyEvent);
await _snapshotTrackerService.SetShouldRecreateSnapshot(true);
}

private PositionHistoryEvent CreatePositionHistoryEvent(Position position,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2019 Lykke Corp.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading.Tasks;
using MarginTrading.Backend.Core.Services;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
using StackExchange.Redis;

namespace MarginTrading.Backend.Services.Services
{
public class SnapshotTrackerService : ISnapshotTrackerService
{
private readonly IDatabase _database;
private readonly AsyncRetryPolicy _retryPolicy;

private const string RedisKey = "core:snapshot:should-recreate";

public SnapshotTrackerService(IConnectionMultiplexer redis, ILogger<SnapshotTrackerService> logger)
{
_database = redis.GetDatabase();
_retryPolicy = Policy
.Handle<Exception>()
.WaitAndRetryAsync(3,
x => TimeSpan.FromMilliseconds(x * 1000),
(exception, span) => logger.LogWarning("Exception: {Message}", exception?.Message));
}

public async Task SetShouldRecreateSnapshot(bool value)
{
await _retryPolicy.ExecuteAsync(() => _database
.StringSetAsync(RedisKey, value.ToString(), when: When.Always));
}

public async Task<bool> GetShouldRecreateSnapshot()
{
var value = await _retryPolicy.ExecuteAsync(() =>
tarurar marked this conversation as resolved.
Show resolved Hide resolved
_database.StringGetAsync(RedisKey));
tarurar marked this conversation as resolved.
Show resolved Hide resolved

return value == bool.TrueString;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
using JetBrains.Annotations;
using Lykke.Cqrs;
using MarginTrading.Backend.Contracts.Prices;
using MarginTrading.Backend.Core.Repositories;
using MarginTrading.Backend.Core.Services;
using MarginTrading.Backend.Core.Snapshots;
using MarginTrading.Common.Services;
using Microsoft.Extensions.DependencyInjection;

namespace MarginTrading.Backend.Services.Workflow
{
Expand All @@ -26,19 +27,25 @@ public class EodCommandsHandler
private readonly ISnapshotService _snapshotService;
private readonly IDateService _dateService;
private readonly IDraftSnapshotKeeperFactory _draftSnapshotKeeperFactory;
private readonly IIdentityGenerator _identityGenerator;
private readonly ISnapshotTrackerService _snapshotTrackerService;
private readonly ILog _log;

public EodCommandsHandler(
IQuotesApi quotesApi,
ISnapshotService snapshotService,
IDateService dateService,
IDraftSnapshotKeeperFactory draftSnapshotKeeperFactory,
IIdentityGenerator identityGenerator,
ISnapshotTrackerService snapshotTrackerService,
ILog log)
{
_quotesApi = quotesApi;
_snapshotService = snapshotService;
_dateService = dateService;
_draftSnapshotKeeperFactory = draftSnapshotKeeperFactory;
_identityGenerator = identityGenerator;
_snapshotTrackerService = snapshotTrackerService;
_log = log;
}

Expand All @@ -55,6 +62,15 @@ private async Task Handle(CreateSnapshotCommand command, IEventPublisher publish
throw new Exception($"Could not receive quotes from BookKeeper: {quotes.ErrorCode.ToString()}");
}

var shouldRecreateSnapshot = await _snapshotTrackerService.GetShouldRecreateSnapshot();

if (shouldRecreateSnapshot && !command.IsMissing)
{
await _snapshotService.MakeTradingDataSnapshot(command.TradingDay,
_identityGenerator.GenerateGuid(),
SnapshotStatus.Draft);
}

var draftSnapshotKeeper = _draftSnapshotKeeperFactory.Create(command.TradingDay);

await _snapshotService.MakeTradingDataSnapshotFromDraft(command.OperationId,
Expand Down
5 changes: 5 additions & 0 deletions tests/MarginTradingTests/BaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using MarginTrading.AssetService.Contracts.ClientProfileSettings;
using MarginTrading.AssetService.Contracts.Scheduling;
using MarginTrading.Backend.Contracts.Events;
using MarginTrading.Backend.Core.Services;
using MarginTrading.Backend.Services.AssetPairs;
using MarginTrading.Backend.Services.Quotes;
using MarginTradingTests.Modules;
Expand Down Expand Up @@ -191,6 +192,10 @@ private void RegisterDependenciesCore(bool mockEvents = false)
builder.RegisterInstance(exchangeConnector).As<IExchangeConnectorClient>();
builder.RegisterInstance(Mock.Of<IRabbitMqService>()).As<IRabbitMqService>();
builder.RegisterInstance(Mock.Of<IRfqService>()).As<IRfqService>();

builder.RegisterInstance(Mock.Of<ISnapshotTrackerService>())
.As<ISnapshotTrackerService>()
.SingleInstance();

builder.RegisterBuildCallback(c =>
{
Expand Down
Loading