-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
6 1 #2470
Draft
ltetak
wants to merge
16
commits into
mbdavid:master
Choose a base branch
from
ltetak:6_1
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.
Draft
6 1 #2470
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ff4862f
better logging
ltetak f8c9c4f
Don't capture synchronization context in DiskWriterQueue
18dfe4a
Fixed writing pages in the same thread that does EnqueuePage
3fb34cc
Lazy string interpolation in ENSURE
6ecf9e8
Use more conventional way to postpone formatting
5ad23ed
Merge pull request #1 from alexbereznikov/2451-fix-disk-writer-queue
ltetak 902954c
Merge pull request #4 from alexbereznikov/2446-fix-disk-writer-queue-…
ltetak d310c0d
Merge pull request #2 from alexbereznikov/2451-lazy-string-interpolat…
ltetak eab53ac
Merge branch '6_1' into 2451-fix-disk-writer-queue
ltetak 1737846
Merge pull request #5 from ltetak/2451-fix-disk-writer-queue
ltetak f57da86
Better logging
ltetak 0544754
LiteEngine notify on disposed
ltetak a3b10c0
Fail hard on any error in DiskWriterQueue
ltetak 399651b
Log full exception
ltetak 368dad5
Free pages on DiskWriterQueue failure
ltetak 2b2e19d
Publish ILiteEngine + IsDisposed states
ltetak 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
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
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
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 |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
|
||
namespace LiteDB.Engine | ||
{ | ||
using LiteDB.Utils.Extensions; | ||
|
||
/// <summary> | ||
/// Implement disk write queue and async writer thread - used only for write on LOG file | ||
/// [ThreadSafe] | ||
|
@@ -23,7 +25,7 @@ internal class DiskWriterQueue : IDisposable | |
|
||
private readonly ConcurrentQueue<PageBuffer> _queue = new ConcurrentQueue<PageBuffer>(); | ||
private readonly object _queueSync = new object(); | ||
private readonly AsyncManualResetEvent _queueHasItems = new AsyncManualResetEvent(); | ||
private readonly ManualResetEventSlim _queueHasItems = new ManualResetEventSlim(); | ||
private readonly ManualResetEventSlim _queueIsEmpty = new ManualResetEventSlim(true); | ||
|
||
private Exception _exception = null; // store last exception in async running task | ||
|
@@ -48,7 +50,7 @@ public void EnqueuePage(PageBuffer page) | |
ENSURE(page.Origin == FileOrigin.Log, "async writer must use only for Log file"); | ||
|
||
// throw last exception that stop running queue | ||
if (_exception != null) throw _exception; | ||
if (_exception != null) throw new LiteException(0, _exception, "DiskWriterQueue error"); | ||
|
||
lock (_queueSync) | ||
{ | ||
|
@@ -58,7 +60,7 @@ public void EnqueuePage(PageBuffer page) | |
|
||
if (_task == null) | ||
{ | ||
_task = Task.Factory.StartNew(ExecuteQueue, TaskCreationOptions.LongRunning); | ||
_task = Task.Factory.StartNew(ExecuteQueue, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); | ||
} | ||
} | ||
} | ||
|
@@ -100,14 +102,23 @@ private async Task ExecuteQueue() | |
|
||
_stream.FlushToDisk(); | ||
|
||
await _queueHasItems.WaitAsync(); | ||
await _queueHasItems.WaitHandle.WaitAsync().ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_state.Handle(ex); | ||
_state.Handle(LiteException.InvalidDatafileState(ex, "DiskWriterQueue failed")); | ||
_exception = ex; | ||
ExhaustQueue(); | ||
} | ||
} | ||
|
||
private void ExhaustQueue() | ||
{ | ||
while (_queue.TryDequeue(out var page)) | ||
{ | ||
page.Release(); | ||
} | ||
} | ||
Comment on lines
+113
to
123
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. If anything fails here we need to drop this object - this means all pages must be released (including the one that caused the failure - see the finally case below) |
||
|
||
|
@@ -117,22 +128,28 @@ private void WritePageToStream(PageBuffer page) | |
|
||
ENSURE(page.ShareCounter > 0, "page must be shared at least 1"); | ||
|
||
// set stream position according to page | ||
_stream.Position = page.Position; | ||
try | ||
{ | ||
// set stream position according to page | ||
_stream.Position = page.Position; | ||
|
||
#if DEBUG | ||
_state.SimulateDiskWriteFail?.Invoke(page); | ||
_state.SimulateDiskWriteFail?.Invoke(page); | ||
#endif | ||
|
||
_stream.Write(page.Array, page.Offset, PAGE_SIZE); | ||
_stream.Write(page.Array, page.Offset, PAGE_SIZE); | ||
|
||
// release page here (no page use after this) | ||
page.Release(); | ||
} | ||
finally | ||
{ | ||
// release page here (no page use after this) | ||
page.Release(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
LOG($"disposing disk writer queue (with {_queue.Count} pages in queue)", "DISK"); | ||
Logging.LOG($"disposing disk writer queue (with {_queue.Count} pages in queue)", "DISK"); | ||
|
||
_shouldClose = true; | ||
_queueHasItems.Set(); // unblock the running loop in case there are no items | ||
|
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
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
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
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
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.
make DiskWriterQueue exceptions "fatal" - these can't be survived and it is best to tear down the entire engine and let the outside observer to restart the engine (possibly with a rebuild)