/// <summary> /// Loads a CommitPoint structure from a stream /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>A new CommitPoint structure constructed from the data in the stream</returns> /// <exception cref="BrightstarInternalException">Raised if the data could not be validated as a CommitPoint structure</exception> public static CommitPoint Load(Stream stream) { var ret = new CommitPoint(); ret.Read(stream); ret.NextCommitNumber = ret.CommitNumber + 1; return ret; }
public IEnumerable <CommitPoint> GetCommitPoints() { using (var inputStream = _persistenceManager.GetInputStream(_masterFilePath)) { if (inputStream.Length <= HeaderSize) { yield break; } long recordPos = inputStream.Length - CommitPoint.RecordSize; while (recordPos >= HeaderSize) { inputStream.Seek(recordPos, SeekOrigin.Begin); CommitPoint commitPoint = null; try { commitPoint = CommitPoint.Load(inputStream); } catch (InvalidCommitPointException) { Logging.LogError(BrightstarEventId.CommitPointReadError, "Could not read commit point at offset {0} in master file '{1}'", inputStream.Position, _masterFilePath); } if (commitPoint != null) { yield return(commitPoint); } recordPos = recordPos - CommitPoint.RecordSize; } } }
public ulong AppendCommitPoint(CommitPoint newCommitPoint, bool overwrite = false) { if (overwrite) { Logging.LogDebug("AppendCommitPoint: Overwrite requested. Deleting existing master file at '{0}'", _masterFilePath); _persistenceManager.DeleteFile(_masterFilePath); _commitPoints.Clear(); } if (!_persistenceManager.FileExists(_masterFilePath)) { Logging.LogDebug("AppendCommitPoint: Master file not found at '{0}'. Creating new master file.", _masterFilePath); using (var stream = _persistenceManager.GetOutputStream(_masterFilePath, FileMode.Create)) { Save(stream); } } using (var stream = _persistenceManager.GetOutputStream(_masterFilePath, FileMode.Open)) { stream.Seek(0, SeekOrigin.End); newCommitPoint.Save(stream); _commitPoints.TryAdd(stream.Length - CommitPoint.RecordSize, newCommitPoint); return(newCommitPoint.CommitNumber); } }
/// <summary> /// Loads a CommitPoint structure from a stream /// </summary> /// <param name="stream">The stream to read from</param> /// <returns>A new CommitPoint structure constructed from the data in the stream</returns> /// <exception cref="BrightstarInternalException">Raised if the data could not be validated as a CommitPoint structure</exception> public static CommitPoint Load(Stream stream) { var ret = new CommitPoint(); ret.Read(stream); ret.NextCommitNumber = ret.CommitNumber + 1; return(ret); }
public CommitPoint GetCommitPoint(ulong commitPointLocation) { if (commitPointLocation < HeaderSize || ((commitPointLocation - HeaderSize) % (ulong)CommitPoint.RecordSize) != 0) { throw new ArgumentException("Invalid commit point offset", "commitPointLocation"); } using (var inputStream = _persistenceManager.GetInputStream(_masterFilePath)) { inputStream.Seek((long)commitPointLocation, SeekOrigin.Begin); return(CommitPoint.Load(inputStream)); } }
private CommitPoint _GetCommitPoint(long commitPointLocation, Stream stream) { CommitPoint ret; if (_commitPoints.TryGetValue(commitPointLocation, out ret)) { return(ret); } stream.Seek(commitPointLocation, SeekOrigin.Begin); ret = CommitPoint.Load(stream); _commitPoints.TryAdd(commitPointLocation, ret); return(ret); }
public CommitPoint GetLatestCommitPoint(int skipRecords = 0) { try { using (var stream = _persistenceManager.GetInputStream(_masterFilePath)) { if (stream.Length == HeaderSize) { if (skipRecords > 0) { throw new StoreManagerException(_directoryPath, "Master file is corrupt and no valid commit point information could be read."); } return(null); } long recordStart; if ((stream.Length - HeaderSize) % CommitPoint.RecordSize != 0) { recordStart = stream.Length - ((stream.Length - HeaderSize) % CommitPoint.RecordSize) - ((skipRecords + 1) * CommitPoint.RecordSize); } else { recordStart = stream.Length - ((skipRecords + 1) * CommitPoint.RecordSize); } stream.Seek(recordStart, SeekOrigin.Begin); var commitPoint = CommitPoint.Load(stream); if (skipRecords > 0) { // We skipped over one or more invalid records, so we record a higher next transaction id number in the commit point we return commitPoint.NextCommitNumber = commitPoint.NextCommitNumber + (ulong)skipRecords; // Master file is not truncated here like in the old implementation, // because this could potentially interfere with a master file write operation for a store commit // instead we will need to just always skip corrupt transactions. } return(commitPoint); } } catch (InvalidCommitPointException) { Logging.LogWarning(BrightstarEventId.CommitPointReadError, String.Format( "Failed to read valid commit point from master file '{0}'. Rewinding to previous commit point", _masterFilePath)); return(GetLatestCommitPoint(skipRecords + 1)); } }
public void RevertToCommitPoint(string storeId, ulong commitPointLocation) { // TODO: Validate that this is a proper commit point location //var commitPoint = _storeManager.GetCommitPoint(_baseLocation + "\\" + storeId, commitPointLocation); var lastCommit = _storeManager.GetMasterFile(Path.Combine(_baseLocation, storeId)).GetLatestCommitPoint(); var commitPoint = new CommitPoint(commitPointLocation, lastCommit.NextCommitNumber, DateTime.UtcNow, Guid.Empty); var storeWorker = GetStoreWorker(storeId); storeWorker.WriteStore.RevertToCommitPoint(commitPoint); storeWorker.InvalidateReadStore(); }
public ulong AppendCommitPoint(CommitPoint newCommitPoint, bool overwrite = false) { if (overwrite) { Logging.LogDebug("AppendCommitPoint: Overwrite requested. Deleting existing master file at '{0}'", _masterFilePath); _persistenceManager.DeleteFile(_masterFilePath); } if (!_persistenceManager.FileExists(_masterFilePath)) { Logging.LogDebug("AppendCommitPoint: Master file not found at '{0}'. Creating new master file.", _masterFilePath); using (var stream = _persistenceManager.GetOutputStream(_masterFilePath, FileMode.Create)) { Save(stream); } } using (var stream = _persistenceManager.GetOutputStream(_masterFilePath, FileMode.Open)) { stream.Seek(0, SeekOrigin.End); newCommitPoint.Save(stream); return newCommitPoint.CommitNumber; } }