Exemple #1
0
        /// <inheritdocs/>
        public void WriteLeaderSnapshot(long lastIncludedIndex, long lastTerm, byte[] chunk, long offsetInFile, bool isFinal)
        {
            var    fileName = _snapMgr.GetTempFileNameForIndexAndTerm(lastIncludedIndex, lastTerm);
            Stream stream   = null;

            if (File.Exists(fileName))
            {
                var info = new FileInfo(fileName);
                if (offsetInFile != info.Length)
                {
                    throw new InvalidOperationException($"Bad position. Snapshot chunk is at {offsetInFile} but file has a size of {info.Length}.");
                }
                stream          = new FileStream(fileName, FileMode.Open);
                stream.Position = info.Length;
            }
            else
            {
                if (offsetInFile != 0)
                {
                    throw new InvalidOperationException($"Bad position. Snapshot chunk is at {offsetInFile} but file has a size of zero.");
                }

                stream = new FileStream(fileName, FileMode.OpenOrCreate);
            }

            stream.Write(chunk, 0, chunk.Length);
            stream.Close();

            if (isFinal)
            {
                var newOffset = lastIncludedIndex + 1;
                FinaliseSnapshot(lastIncludedIndex, lastTerm);
            }
        }
Exemple #2
0
        public void IsCleverInFindingLastWhenOneExistsWithOneTemp()
        {
            var index = 42L;
            var term  = 2L;

            File.WriteAllText(_mgr.GetTempFileNameForIndexAndTerm(index, term), "");
            Assert.Null(_mgr.GetLastSnapshot());
        }