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-4926: Potential corrupted snapshot #498

Merged
merged 7 commits into from
Mar 21, 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
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
Loading