Skip to content

Commit

Permalink
Merge branch 'master' into LT-5012-eod-borders
Browse files Browse the repository at this point in the history
  • Loading branch information
gponomarev-lykke committed Mar 26, 2024
2 parents 7fee16a + b92a31b commit 64b9b41
Show file tree
Hide file tree
Showing 33 changed files with 780 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Version>2.31.0</Version>
<Version>2.29.4</Version>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>2.31.0</Version>
<Version>2.29.4</Version>
<PackageId>Lykke.MarginTrading.BackendSnow.Contracts</PackageId>
<LangVersion>8.0</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Version>2.31.0</Version>
<Version>2.29.4</Version>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
Expand Down
14 changes: 11 additions & 3 deletions src/MarginTrading.Backend.Core/Extensions/SagaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static bool SwitchState<TState>(this OperationDataBase<TState> data, TSta

if (Convert.ToInt32(data.State) > Convert.ToInt32(expectedState))
{
LogLocator.CommonLog.WriteWarning(nameof(SagaExtensions), nameof(SwitchState),
LogLocator.CommonLog.WriteWarning(nameof(SagaExtensions), nameof(SwitchToState),
$"Operation is already in the next state, so this event is ignored, {new {data, expectedState, nextState}.ToJson()}.");
return false;
}
Expand All @@ -38,6 +38,14 @@ public static bool SwitchState<TState>(this OperationDataBase<TState> data, TSta
return true;
}

public static bool SwitchToState(this OperationDataBase<SpecialLiquidationOperationState> data,
SpecialLiquidationOperationState nextState) =>
data.SwitchState(data.State, nextState);

public static bool SwitchToState(this IOperationExecutionInfo<SpecialLiquidationOperationData> info,
SpecialLiquidationOperationState nextState) =>
info.Data.SwitchToState(nextState);

public static bool SwitchState(this OperationDataBase<SpecialLiquidationOperationState> data,
SpecialLiquidationOperationState expectedState, SpecialLiquidationOperationState nextState)
{
Expand All @@ -55,15 +63,15 @@ public static bool SwitchState(this OperationDataBase<SpecialLiquidationOperatio

if (Convert.ToInt32(data.State) > Convert.ToInt32(expectedState))
{
LogLocator.CommonLog.WriteWarning(nameof(SagaExtensions), nameof(SwitchState),
LogLocator.CommonLog.WriteWarning(nameof(SagaExtensions), nameof(SwitchToState),
$"Operation is already in the next state, so this event is ignored, {new {data, expectedState, nextState}.ToJson()}.");
return false;
}

if (data.State == SpecialLiquidationOperationState.Failed &&
nextState == SpecialLiquidationOperationState.Cancelled)
{
LogLocator.CommonLog.WriteWarning(nameof(SagaExtensions), nameof(SwitchState),
LogLocator.CommonLog.WriteWarning(nameof(SagaExtensions), nameof(SwitchToState),
$"Cannot switch from Failed to Cancelled state (both states are final), so this event is ignored, {new {data, expectedState, nextState}.ToJson()}.");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Version>2.31.0</Version>
<Version>2.29.4</Version>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace MarginTrading.Backend.Core.Repositories
{
public interface IOrdersHistoryRepository
{
Task<IReadOnlyList<IOrderHistory>> GetLastSnapshot(DateTime @from);
Task<IReadOnlyList<IOrderHistory>> GetLastSnapshot(DateTime @from, DateTime? @to = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public static bool Initialized(this IDraftSnapshotKeeper keeper)
public static bool IsPlatformClosureEvent(this MarketStateChangedEvent evt) =>
evt.Id == LykkeConstants.PlatformMarketIdentifier && !evt.IsEnabled;

public static bool IsNotPlatformClosureEvent(this MarketStateChangedEvent evt) =>
!evt.IsPlatformClosureEvent();

private static List<T> GetOrders<T>(this TradingEngineSnapshot snapshot)
{
return string.IsNullOrWhiteSpace(snapshot.OrdersJson)
Expand Down
110 changes: 109 additions & 1 deletion src/MarginTrading.Backend.Services/Helpers/LiquidationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common;
using Lykke.Cqrs;
using MarginTrading.Backend.Contracts.Workflow.SpecialLiquidation.Commands;
using MarginTrading.Backend.Core;
using MarginTrading.Backend.Core.Exceptions;
using MarginTrading.Backend.Core.Extensions;
using MarginTrading.Backend.Core.MatchingEngines;
using MarginTrading.Backend.Core.Orders;
using MarginTrading.Backend.Core.Repositories;
using MarginTrading.Backend.Core.Settings;
using MarginTrading.Backend.Core.Trading;
using MarginTrading.Backend.Services.AssetPairs;
using MarginTrading.Backend.Services.Infrastructure;
using MarginTrading.Backend.Services.Services;
using MarginTrading.Backend.Services.Workflow.Liquidation.Commands;
using MarginTrading.Backend.Services.Workflow.SpecialLiquidation.Commands;
using MarginTrading.Common.Services;
using MoreLinq;

Expand All @@ -25,19 +33,34 @@ public class LiquidationHelper
private readonly ICqrsSender _cqrsSender;
private readonly OrdersCache _ordersCache;
private readonly IAssetPairDayOffService _assetPairDayOffService;
private readonly IAssetPairsCache _assetPairsCache;
private readonly CqrsContextNamesSettings _cqrsContextNamesSettings;
private readonly MarginTradingSettings _marginTradingSettings;
private readonly IOperationExecutionInfoRepository _operationExecutionInfoRepository;
private readonly IRfqService _specialLiquidationService;


public LiquidationHelper(IMatchingEngineRouter matchingEngineRouter,
IDateService dateService,
ICqrsSender cqrsSender,
OrdersCache ordersCache,
IAssetPairDayOffService assetPairDayOffService)
IAssetPairDayOffService assetPairDayOffService,
IAssetPairsCache assetPairsCache,
CqrsContextNamesSettings cqrsContextNamesSettings,
IOperationExecutionInfoRepository operationExecutionInfoRepository,
IRfqService specialLiquidationService,
MarginTradingSettings marginTradingSettings)
{
_matchingEngineRouter = matchingEngineRouter;
_dateService = dateService;
_cqrsSender = cqrsSender;
_ordersCache = ordersCache;
_assetPairDayOffService = assetPairDayOffService;
_assetPairsCache = assetPairsCache;
_cqrsContextNamesSettings = cqrsContextNamesSettings;
_operationExecutionInfoRepository = operationExecutionInfoRepository;
_specialLiquidationService = specialLiquidationService;
_marginTradingSettings = marginTradingSettings;
}

public bool CheckIfNetVolumeCanBeLiquidated(string assetPairId, Position[] positions,
Expand Down Expand Up @@ -138,6 +161,91 @@ public bool CheckIfNetVolumeCanBeLiquidated(string assetPairId, Position[] posit

return result;
}

public async Task<bool> FailIfInstrumentDiscontinued(IOperationExecutionInfo<SpecialLiquidationOperationData> executionInfo, ICommandSender sender)
{
var isDiscontinued = _assetPairsCache.GetAssetPairById(executionInfo.Data.Instrument).IsDiscontinued;

if (isDiscontinued)
{
if (executionInfo.Data.SwitchState(SpecialLiquidationOperationState.PriceRequested,
SpecialLiquidationOperationState.OnTheWayToFail))
{
sender.SendCommand(new FailSpecialLiquidationInternalCommand
{
OperationId = executionInfo.Id,
CreationTime = _dateService.Now(),
Reason = "Instrument discontinuation",

}, _cqrsContextNamesSettings.TradingEngine);

await _operationExecutionInfoRepository.Save(executionInfo);
}

return true;
}

return false;
}

public async Task InternalRetryPriceRequest(DateTime eventCreationTime,
ICommandSender sender,
IOperationExecutionInfo<SpecialLiquidationOperationData> executionInfo,
TimeSpan retryTimeout)
{
// fix the intention to make another price request to not let the parallel
// ongoing GetPriceForSpecialLiquidationTimeoutInternalCommand execution
// break (fail) the flow
executionInfo.Data.NextRequestNumber();
await _operationExecutionInfoRepository.Save(executionInfo);

var shouldRetryAfter = eventCreationTime.Add(retryTimeout);

var timeLeftBeforeRetry = shouldRetryAfter - _dateService.Now();

if (timeLeftBeforeRetry > TimeSpan.Zero)
{
await Task.Delay(timeLeftBeforeRetry);
}

RequestPrice(sender, executionInfo);
}

public void RequestPrice(ICommandSender sender, IOperationExecutionInfo<SpecialLiquidationOperationData>
executionInfo)
{
//hack, requested by the bank
var positionsVolume = executionInfo.Data.Volume != 0 ? executionInfo.Data.Volume : 1;

var command = new GetPriceForSpecialLiquidationCommand
{
OperationId = executionInfo.Id,
CreationTime = _dateService.Now(),
Instrument = executionInfo.Data.Instrument,
Volume = positionsVolume,
RequestNumber = executionInfo.Data.RequestNumber,
RequestedFromCorporateActions = executionInfo.Data.RequestedFromCorporateActions
};

if (_marginTradingSettings.ExchangeConnector == ExchangeConnectorType.RealExchangeConnector)
{
//send it to the Gavel
sender.SendCommand(command, _cqrsContextNamesSettings.Gavel);
}
else
{
_specialLiquidationService.SavePriceRequestForSpecialLiquidation(command);
}

//special command is sent instantly for timeout control.. it is retried until timeout occurs
sender.SendCommand(new GetPriceForSpecialLiquidationTimeoutInternalCommand
{
OperationId = executionInfo.Id,
CreationTime = _dateService.Now(),
TimeoutSeconds = _marginTradingSettings.SpecialLiquidation.PriceRequestTimeoutSec,
RequestNumber = executionInfo.Data.RequestNumber
}, _cqrsContextNamesSettings.TradingEngine);
}

public static string GetComment(LiquidationType liquidationType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ await _log.WriteInfoAsync(nameof(SnapshotValidationService), nameof(ValidateCurr
var lastOrders = GetOrders(tradingEngineSnapshot);
var lastPositions = GetPositions(tradingEngineSnapshot);

var ordersHistory = await _ordersHistoryRepository.GetLastSnapshot(tradingEngineSnapshot.Timestamp);
var latestOrder = currentOrders.MaxBy(x => x.LastModified);
var ordersHistory = await _ordersHistoryRepository.GetLastSnapshot(tradingEngineSnapshot.Timestamp, latestOrder?.LastModified);
var positionsHistory = await _positionsHistoryRepository.GetLastSnapshot(tradingEngineSnapshot.Timestamp);

var restoredOrders = RestoreOrdersCurrentStateFromHistory(lastOrders, ordersHistory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Version>2.31.0</Version>
<Version>2.29.4</Version>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
Expand All @@ -32,7 +32,7 @@
<PackageReference Include="Lykke.MarginTrading.OrderbookAggregator.Contracts" Version="1.0.1" />
<PackageReference Include="Lykke.MarginTrading.OrderBookService.Contracts" Version="1.6.1" />
<PackageReference Include="Lykke.Rocks.Caching" Version="1.0.0" />
<PackageReference Include="Lykke.Snow.Domain" Version="2.0.3" />
<PackageReference Include="Lykke.Snow.Domain" Version="2.1.4" />
<PackageReference Include="Lykke.Snow.Mdm.Contracts" Version="4.3.1" />
<PackageReference Include="LykkeBiz.Snow.Cqrs" Version="4.0.2" />
<PackageReference Include="LykkeBiz.RabbitMqBroker" Version="9.2.6" />
Expand Down
4 changes: 4 additions & 0 deletions src/MarginTrading.Backend.Services/Modules/CqrsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ protected override void Load(ContainerBuilder builder)
.SingleInstance();
builder.RegisterInstance(new CqrsContextNamesSettings()).AsSelf().SingleInstance();

builder.RegisterType<SpecialLiquidationFailedEventHandler>()
.AsImplementedInterfaces()
.SingleInstance();

// Sagas & command handlers
builder.RegisterAssemblyTypes(GetType().Assembly).Where(t =>
new[] { "Saga", "CommandsHandler", "Projection" }.Any(ending => t.Name.EndsWith(ending))).AsSelf();
Expand Down
Loading

0 comments on commit 64b9b41

Please sign in to comment.