Skip to content

Commit

Permalink
Fixes #47 - System.InvalidOperationException: Startup() can only be c…
Browse files Browse the repository at this point in the history
…alled once

If WriteLog.Startup() fails, it can never be retried as startedAt will be populated. This allows for async writes to fail temporarily and then resume once the other process has released the lock on the metastore files.
  • Loading branch information
lilith committed Mar 17, 2021
1 parent 0c3736d commit 6caf311
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Imazen.HybridCache/MetaStore/WriteLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class WriteLog
private readonly MetaStoreOptions options;
private readonly ILogger logger;
private long startedAt;
private bool startupComplete;
private FileStream writeLogStream;
private BinaryWriter binaryLogWriter;
private readonly object writeLock = new object();
Expand Down Expand Up @@ -67,10 +68,10 @@ private void CreateWriteLog(long startedAtTick)
}
}

// Returns a dictionary of the database
// Returns a dictionary of the database
public async Task<ConcurrentDictionary<string, CacheDatabaseRecord>> Startup()
{
if (startedAt != 0) throw new InvalidOperationException("Startup() can only be called once");
if (startupComplete) throw new InvalidOperationException("Startup() can only be called once");
startedAt = Stopwatch.GetTimestamp();

if (!Directory.Exists(databaseDir))
Expand Down Expand Up @@ -200,6 +201,7 @@ public async Task<ConcurrentDictionary<string, CacheDatabaseRecord>> Startup()
diskBytes += dict.Values.Sum(r => r.DiskSize);
}

startupComplete = true;
return dict;
}

Expand Down Expand Up @@ -246,7 +248,7 @@ private Task WriteLogEntry(LogEntry entry, bool flush)
lock (writeLock)
{
if (startedAt == 0)
throw new InvalidOperationException("WriteLog cannot be used before calling ReadAll()");
throw new InvalidOperationException("WriteLog cannot be used before calling Startup()");
if (writeLogStream == null)
throw new InvalidOperationException("WriteLog cannot be after StopAsync is called");
var startPos = writeLogStream.Position;
Expand Down

0 comments on commit 6caf311

Please sign in to comment.