void IHandle <SystemMessage.WaitForChaserToCatchUp> .Handle(SystemMessage.WaitForChaserToCatchUp message) { // if we are in states, that doesn't need to wait for chaser, ignore if (_vnodeState != VNodeState.PreMaster && _vnodeState != VNodeState.PreReplica) { throw new Exception(string.Format("{0} appeared in {1} state.", message.GetType().Name, _vnodeState)); } if (Writer.Checkpoint.Read() != Writer.Checkpoint.ReadNonFlushed()) { Writer.Flush(); } var sw = Stopwatch.StartNew(); while (Db.Config.ChaserCheckpoint.Read() < Db.Config.WriterCheckpoint.Read() && sw.Elapsed < WaitForChaserSingleIterationTimeout) { Thread.Sleep(1); } if (Db.Config.ChaserCheckpoint.Read() == Db.Config.WriterCheckpoint.Read()) { Bus.Publish(new SystemMessage.ChaserCaughtUp(message.CorrelationId)); return; } var totalTime = message.TotalTimeWasted + sw.Elapsed; if (totalTime < TimeSpan.FromSeconds(5) || (int)totalTime.TotalSeconds % 30 == 0) // too verbose otherwise { Log.Debug("Still waiting for chaser to catch up already for {0}...", totalTime); } Bus.Publish(new SystemMessage.WaitForChaserToCatchUp(message.CorrelationId, totalTime)); }
public void try_read_does_not_cache_anything_and_returns_record_once_it_is_written_later() { var writerchk = new InMemoryCheckpoint(0); var chaserchk = new InMemoryCheckpoint(0); var db = new TFChunkDb(TFChunkHelper.CreateDbConfig(PathName, writerchk, chaserchk)); db.Open(); var writer = new TFChunkWriter(db); writer.Open(); var reader = new TFChunkReader(db, writerchk, 0); Assert.IsFalse(reader.TryReadNext().Success); var rec = LogRecord.SingleWrite(0, Guid.NewGuid(), Guid.NewGuid(), "ES", -1, "ET", new byte[] { 7 }, null); long tmp; Assert.IsTrue(writer.Write(rec, out tmp)); writer.Flush(); writer.Close(); var res = reader.TryReadNext(); Assert.IsTrue(res.Success); Assert.AreEqual(rec, res.LogRecord); db.Close(); }
public void try_read_does_not_cache_anything_and_returns_record_once_it_is_written_later() { var writerchk = new InMemoryCheckpoint(0); var db = new TFChunkDb(new TFChunkDbConfig(PathName, new VersionedPatternFileNamingStrategy(PathName, "chunk-"), 10000, 0, writerchk, new InMemoryCheckpoint())); db.OpenVerifyAndClean(); var writer = new TFChunkWriter(db); writer.Open(); var reader = new TFChunkSequentialReader(db, writerchk, 0); LogRecord record; Assert.IsFalse(reader.TryReadNext(out record)); var rec = LogRecord.SingleWrite(0, Guid.NewGuid(), Guid.NewGuid(), "ES", -1, "ET", new byte[] { 7 }, null); long tmp; Assert.IsTrue(writer.Write(rec, out tmp)); writer.Flush(); writer.Close(); Assert.IsTrue(reader.TryReadNext(out record)); Assert.AreEqual(rec, record); reader.Close(); db.Close(); }
private EpochRecord WriteEpoch(int epochNumber, long lastPos, Guid instanceId) { long pos = _writer.Checkpoint.ReadNonFlushed(); var epoch = new EpochRecord(pos, epochNumber, Guid.NewGuid(), lastPos, DateTime.UtcNow, instanceId); var rec = _logFormat.RecordFactory.CreateEpoch(epoch); _writer.Write(rec, out _); _writer.Flush(); return(epoch); }
private EpochRecord WriteEpoch(int epochNumber, long lastPos, Guid instanceId) { long pos = _writer.Checkpoint.ReadNonFlushed(); var epoch = new EpochRecord(pos, epochNumber, Guid.NewGuid(), lastPos, DateTime.UtcNow, instanceId); var rec = new SystemLogRecord(epoch.EpochPosition, epoch.TimeStamp, SystemRecordType.Epoch, SystemRecordSerialization.Json, epoch.AsSerialized()); _writer.Write(rec, out _); _writer.Flush(); return(epoch); }
protected bool Flush(bool force = false) { var start = _watch.ElapsedTicks; if (force || FlushMessagesInQueue == 0 || start - _lastFlushTimestamp >= _lastFlushDelay + _minFlushDelay) { var flushSize = Writer.Checkpoint.ReadNonFlushed() - Writer.Checkpoint.Read(); Writer.Flush(); var end = _watch.ElapsedTicks; var flushDelay = end - start; Interlocked.Exchange(ref _lastFlushDelay, flushDelay); Interlocked.Exchange(ref _lastFlushSize, flushSize); _lastFlushTimestamp = end; if (_statCount >= LastStatsCount) { Interlocked.Add(ref _sumFlushSize, -_lastFlushSizes[_statIndex]); Interlocked.Add(ref _sumFlushDelay, -_lastFlushDelays[_statIndex]); } else { _statCount += 1; } _lastFlushSizes[_statIndex] = flushSize; _lastFlushDelays[_statIndex] = flushDelay; Interlocked.Add(ref _sumFlushSize, flushSize); Interlocked.Add(ref _sumFlushDelay, flushDelay); Interlocked.Exchange(ref _maxFlushSize, Math.Max(Interlocked.Read(ref _maxFlushSize), flushSize)); Interlocked.Exchange(ref _maxFlushDelay, Math.Max(Interlocked.Read(ref _maxFlushDelay), flushDelay)); _statIndex = (_statIndex + 1) & (LastStatsCount - 1); return(true); } return(false); }