Skip to content

Commit

Permalink
Revert prev workaround for LiteDB and make it flush instead
Browse files Browse the repository at this point in the history
It turns out that LiteDB's data corruption
(mbdavid/LiteDB#1268) seems to happen
due to sudden termination of program.  So the workaround made in the
previous patch (planetarium#386)
was reverted for the most part, and a new option named `flush` was
added to `LiteDBStore()` constructor.
  • Loading branch information
dahlia committed Aug 2, 2019
1 parent da9eea5 commit 89015df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
7 changes: 5 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ To be released.
- `StoreExtension.LookupStateReference<T>()` method became to return
`Tuple<HashDigest<SHA256>, long>` which is a nullable tuple of `Block<T>.Hash`
and `Block<T>.Index`. [[#350]]
- `LiteDBStore()` constructor became to have a new option named `flush` and turned on by default.
[[#387], [LiteDB #1268]]

### Added interfaces

Expand Down Expand Up @@ -45,7 +47,7 @@ To be released.
- Fixed a bug where the `LiteDBStore.IterateStagedTransactionIds()` returns
duplicated transaction ids. [[#366]]
- Fixed a `LiteDBStore` bug that blocks or transactions had got corrupted
sometimes. [[#386], [LiteDB #1268]]
sometimes. [[#386], [#387], [LiteDB #1268]]

[#343]: https://github.com/planetarium/libplanet/pull/343
[#350]: https://github.com/planetarium/libplanet/pull/350
Expand All @@ -54,7 +56,8 @@ To be released.
[#366]: https://github.com/planetarium/libplanet/pull/366
[#384]: https://github.com/planetarium/libplanet/issues/384
[#385]: https://github.com/planetarium/libplanet/pull/385
[#386]: https://github.com/planetarium/libplanet/pull/366
[#386]: https://github.com/planetarium/libplanet/pull/386
[#387]: https://github.com/planetarium/libplanet/pull/387
[LiteDB #1268]: https://github.com/mbdavid/LiteDB/issues/1268


Expand Down
43 changes: 13 additions & 30 deletions Libplanet/Store/LiteDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ public class LiteDBStore : BaseStore, IDisposable
/// Enables or disables double write check to ensure durability.
/// </param>
/// <param name="cacheSize">Max number of pages in the cache.</param>
public LiteDBStore(string path, bool journal = true, int cacheSize = 50000)
/// <param name="flush">Writes data direct to disk avoiding OS cache. Turned on by default.
/// </param>
public LiteDBStore(
string path,
bool journal = true,
int cacheSize = 50000,
bool flush = true
)
{
if (path is null)
{
Expand All @@ -52,6 +59,7 @@ public LiteDBStore(string path, bool journal = true, int cacheSize = 50000)
Filename = path,
Journal = journal,
CacheSize = cacheSize,
Flush = flush,
};

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
Expand Down Expand Up @@ -298,7 +306,8 @@ public override void SetBlockStates(
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, states);
UploadFile(
stream.Seek(0, SeekOrigin.Begin);
_db.FileStorage.Upload(
BlockStateFileId(blockHash),
ByteUtil.Hex(blockHash.ToByteArray()),
stream
Expand Down Expand Up @@ -558,38 +567,12 @@ IEnumerable transactions
.Where(tx => tx != null);
}

// As LiteDB's file storage seems unstable, we need to repeat trying to save a file
// until we ensure it's actually saved.
// https://github.com/mbdavid/LiteDB/issues/1268
private void UploadFile(string fileId, string filename, Stream stream)
{
bool IsFiledUploaded()
{
if (_db.FileStorage.FindById(fileId) is LiteFileInfo file && file.Length > 0)
{
using (LiteFileStream f = file.OpenRead())
{
var buffer = new byte[1];
return f.Read(buffer, 0, 1) > 0;
}
}

return false;
}

do
{
stream.Seek(0, SeekOrigin.Begin);
_db.FileStorage.Upload(fileId, filename, stream);
}
while (!IsFiledUploaded());
}

private void UploadFile(string fileId, string filename, byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
UploadFile(fileId, filename, stream);
stream.Seek(0, SeekOrigin.Begin);
_db.FileStorage.Upload(fileId, filename, stream);
}
}

Expand Down

0 comments on commit 89015df

Please sign in to comment.