Skip to content

Commit

Permalink
Merge pull request #498 from LykkeBusiness/LT-4926-potentially-corupt…
Browse files Browse the repository at this point in the history
…ed-snapshot

LT-4926: Potential corrupted snapshot
  • Loading branch information
lykke-vashetsin authored Mar 21, 2024
2 parents 3dc9b2c + 62d7a5b commit 0948591
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
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 @@ -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 @@ -34,7 +34,7 @@ CASE [Status]
ELSE 99
END as StatusOrder
FROM [{0}] oh (NOLOCK)
WHERE oh.ModifiedTimestamp > @Timestamp
WHERE oh.ModifiedTimestamp > @From AND (@To IS NULL OR oh.ModifiedTimestamp <= @To)
),
filteredOrderHistWithRowNumber AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY
Expand All @@ -54,11 +54,11 @@ public OrdersHistoryRepository(string connectionString, string tableName, int ge
_getLastSnapshotTimeoutS = getLastSnapshotTimeoutS;
}

public async Task<IReadOnlyList<IOrderHistory>> GetLastSnapshot(DateTime @from)
public async Task<IReadOnlyList<IOrderHistory>> GetLastSnapshot(DateTime @from, DateTime? @to = null)
{
using (var conn = new SqlConnection(_connectionString))
{
var data = await conn.QueryAsync<OrderHistoryEntity>(_select, new { Timestamp = @from }, commandTimeout: _getLastSnapshotTimeoutS);
var data = await conn.QueryAsync<OrderHistoryEntity>(_select, new { From = @from, To = @to }, commandTimeout: _getLastSnapshotTimeoutS);

return data.Cast<IOrderHistory>().ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public void SetUp()
_orderCacheMock.Setup(o => o.GetPositions())
.Returns(() => _currentPositions.ToImmutableArray());

_ordersHistoryRepositoryMock.Setup(o => o.GetLastSnapshot(It.IsAny<DateTime>()))
.ReturnsAsync((DateTime date) => _ordersHistory);
_ordersHistoryRepositoryMock.Setup(o => o.GetLastSnapshot(It.IsAny<DateTime>(), It.IsAny<DateTime?>()))
.ReturnsAsync((DateTime from, DateTime? to) => _ordersHistory);

_positionsHistoryRepositoryMock.Setup(o => o.GetLastSnapshot(It.IsAny<DateTime>()))
.ReturnsAsync((DateTime date) => _positionsHistory);
Expand Down
2 changes: 1 addition & 1 deletion tests/MarginTradingTests/Modules/MockRepositoriesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override void Load(ContainerBuilder builder)
blobRepository.Setup(s => s.ReadWithTimestampAsync<List<Position>>(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync((new List<Position>(), DateTime.UtcNow));
var orderHistoryRepository = new Mock<IOrdersHistoryRepository>();
orderHistoryRepository.Setup(s => s.GetLastSnapshot(It.IsAny<DateTime>()))
orderHistoryRepository.Setup(s => s.GetLastSnapshot(It.IsAny<DateTime>(), It.IsAny<DateTime?>()))
.ReturnsAsync(new List<IOrderHistory>());
var positionHistoryRepository = new Mock<IPositionsHistoryRepository>();
var accountHistoryRepository = new Mock<IAccountHistoryRepository>();
Expand Down

0 comments on commit 0948591

Please sign in to comment.