internal ulong GetLatestStorePositionFromMasterFile(string masterFilePath) { try { Logging.LogDebug("Retrieving latest store position from masterfile : {0}", masterFilePath); using (var fs = _persistenceManager.GetInputStream(masterFilePath)) { Logging.LogDebug("Masterfile stream length is {0}", fs.Length); Logging.LogDebug("Attempting to seek to {0} bytes from end of stream.", CommitPoint.RecordSize); fs.Seek(-CommitPoint.RecordSize, SeekOrigin.End); Logging.LogDebug("Seek completed ok. Attempting to load commit point"); var commitPoint = CommitPoint.Load(fs); Logging.LogDebug("Commit point load completed OK. Returning commit point offset as {0}", commitPoint.LocationOffset); return(commitPoint.LocationOffset); } } catch (InvalidCommitPointException icp) { Logging.LogInfo("Caught InvalidCommitPointException: {0}", icp); // start reading from the start of the file until we get to the dud one. // truncate the file at this point. log it and try again. var count = 0; CommitPoint validCommitPoint = null; const int headerSize = MasterfileHeaderLongCount * 8; while (true) { using (var fs = _persistenceManager.GetInputStream(masterFilePath)) { try { Logging.LogInfo("Reading commit point at " + CommitPoint.RecordSize * count); fs.Seek((CommitPoint.RecordSize * count) + headerSize, SeekOrigin.Begin); var commitPoint = CommitPoint.Load(fs); validCommitPoint = commitPoint; count++; } catch (BrightstarInternalException) { var startOfBadCommit = (CommitPoint.RecordSize * count) + headerSize; Logging.LogInfo("Truncating file at " + startOfBadCommit); // truncate file. using (var stream = _persistenceManager.GetOutputStream(masterFilePath, FileMode.Truncate)) { stream.SetLength(startOfBadCommit); } // return last good commit return(validCommitPoint.LocationOffset); } } } } catch (Exception ex) { throw new BrightstarInternalException("Error while trying to recover to last valid commit point.", ex); } }
public CommitPoint GetCommitPoint(string storeLocation, ulong offset) { var masterFileLocation = Path.Combine(storeLocation, MasterFileName); using (var fs = _persistenceManager.GetInputStream(masterFileLocation)) { fs.Seek((long)offset, SeekOrigin.Begin); var commitPoint = CommitPoint.Load(fs); return(commitPoint); } }
public IEnumerable <CommitPoint> GetCommitPoints(string storeLocation) { var masterFileLocation = Path.Combine(storeLocation, MasterFileName); var pos = 1; using (var fs = _persistenceManager.GetInputStream(masterFileLocation)) { while ((pos * CommitPoint.RecordSize) + MasterfileHeaderSize <= fs.Length) { fs.Seek(-(pos * CommitPoint.RecordSize), SeekOrigin.End); var commitPoint = CommitPoint.Load(fs); pos++; yield return(commitPoint); } } }