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 authored and longfin committed Aug 6, 2019
1 parent b603ce4 commit 6b6f89d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
7 changes: 5 additions & 2 deletions CHANGES.md
Expand Up @@ -15,6 +15,8 @@ To be released.
- Added `IBlockPolicy<T>.BlockAction` property. [[#319], [#367]]
- Removed the type parameter of `ActionEvaluation`. [[#319], [#367]]
- `ActionEvaluation.Action` became to `IAction` type. [[#319], [#367]]
- `LiteDBStore()` constructor became to have a new option named `flush` and turned on by default.
[[#387], [LiteDB #1268]]

### Added interfaces

Expand Down Expand Up @@ -56,7 +58,7 @@ To be released.
- Fixed a bug that `TurnClient` had thrown `KeyNotFoundException` and
`IOException` on startup. [[#377], [#378]]
- Fixed a `LiteDBStore` bug that blocks or transactions had got corrupted
sometimes. [[#386], [LiteDB #1268]]
sometimes. [[#386], [#387], [LiteDB #1268]]

[#319]: https://github.com/planetarium/libplanet/issues/319
[#343]: https://github.com/planetarium/libplanet/pull/343
Expand All @@ -71,7 +73,8 @@ To be released.
[#378]: https://github.com/planetarium/libplanet/pull/378
[#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
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 AddressStateMap GetBlockStates(HashDigest<SHA256> blockHash)
{
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 6b6f89d

Please sign in to comment.