forked from LykkeCity/MT
-
Notifications
You must be signed in to change notification settings - Fork 1
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-5770: [Trading Core] Potential Deduplication Lock Failure #560
Open
lykke-vashetsin
wants to merge
3
commits into
master
Choose a base branch
from
LT-5770-fix-deduplication
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,15 @@ | |
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Lykke.Common.Log; | ||
|
||
using MarginTrading.Backend.Core.Settings; | ||
using MarginTrading.Common.Services; | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
using StackExchange.Redis; | ||
|
||
namespace MarginTrading.Backend.Services.Infrastructure | ||
|
@@ -19,17 +25,18 @@ public class StartupDeduplicationService : IDisposable | |
{ | ||
private const string LockKey = "TradingEngine:DeduplicationLock"; | ||
private readonly string _lockValue = Environment.MachineName; | ||
private readonly WebHostProcessTerminator _processTerminator; | ||
private readonly IWebHostEnvironment _hostingEnvironment; | ||
private readonly MarginTradingSettings _marginTradingSettings; | ||
private readonly IDatabase _database; | ||
|
||
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); | ||
|
||
public StartupDeduplicationService( | ||
WebHostProcessTerminator processTerminator, | ||
IWebHostEnvironment hostingEnvironment, | ||
MarginTradingSettings marginTradingSettings, | ||
IConnectionMultiplexer redis) | ||
{ | ||
_processTerminator = processTerminator; | ||
_hostingEnvironment = hostingEnvironment; | ||
_marginTradingSettings = marginTradingSettings; | ||
_database = redis.GetDatabase(); | ||
|
@@ -60,34 +67,38 @@ public void HoldLock() | |
// exception is logged by the global handler | ||
} | ||
|
||
Exception workerException = null; | ||
// ReSharper disable once PossibleNullReferenceException | ||
_cancellationTokenSource.Token.Register(() => throw workerException); | ||
|
||
Task.Run(async () => | ||
{ | ||
try | ||
Task.Run( | ||
async () => | ||
{ | ||
while (true) | ||
var run = true; | ||
while (run) | ||
{ | ||
// wait and extend lock | ||
await Task.Delay(_marginTradingSettings.DeduplicationLockExtensionPeriod); | ||
try | ||
{ | ||
// wait and extend lock | ||
await Task.Delay(_marginTradingSettings.DeduplicationLockExtensionPeriod); | ||
|
||
await _database.LockExtendAsync(LockKey, _lockValue, | ||
_marginTradingSettings.DeduplicationLockExpiryPeriod); | ||
var extendResult = await _database.LockExtendAsync( | ||
LockKey, | ||
_lockValue, | ||
_marginTradingSettings.DeduplicationLockExpiryPeriod); | ||
if (!extendResult) | ||
{ | ||
LogLocator.CommonLog?.Error("DeduplicationService", message: "Lock is taken already."); | ||
_processTerminator.TerminateProcess(); | ||
run = false; | ||
} | ||
} | ||
catch (Exception exception) | ||
{ | ||
LogLocator.CommonLog?.Warning("DeduplicationService","Failed to extend lock.", exception); | ||
} | ||
} | ||
} | ||
catch (Exception exception) | ||
{ | ||
workerException = exception; | ||
_cancellationTokenSource.Cancel(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
public void Dispose() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you still need deduplication service to implement Idisposable interface? |
||
{ | ||
_cancellationTokenSource.Dispose(); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/MarginTrading.Backend.Services/Infrastructure/WebHostProcessTerminator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) 2019 Lykke Corp. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
namespace MarginTrading.Backend.Services.Infrastructure | ||
{ | ||
public sealed class WebHostProcessTerminator : IDisposable | ||
{ | ||
private readonly CancellationTokenSource _cancellationTokenSource; | ||
|
||
public WebHostProcessTerminator() | ||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
} | ||
|
||
public CancellationToken CancellationToken => _cancellationTokenSource.Token; | ||
|
||
public void TerminateProcess() | ||
{ | ||
_cancellationTokenSource?.Cancel(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_cancellationTokenSource?.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need
StartupDeduplicationService
still to implementIDisposable
interface?