Ejemplo n.º 1
0
 // Open opens and initializes the Log. Open can recover from previous unclosed shutdowns.
 public void Open()
 {
     if (EnableTraceLogging)
     {
         Logger.Info(string.Format("tsm1 WAL starting with {0} segment size", SegmentSize));
         Logger.Info(string.Format("tsm1 WAL writing to {0}", Path));
     }
     lock (lockthis)
     {
         if (!Directory.Exists(Path))
         {
             Directory.CreateDirectory(Path);
         }
         List <string> segments = SegmentFileNames(Path);
         if (segments != null && segments.Count > 0)
         {
             string lastSegment = segments[segments.Count - 1];
             int    id          = idFromFileName(lastSegment, out string err);
             if (err != null)
             {
                 return;
             }
             currentSegmentID = id;
             FileInfo stat = new FileInfo(lastSegment);
             if (stat.Length == 0)
             {
                 try
                 {
                     File.Delete(lastSegment);
                     segments.RemoveAt(segments.Count - 1);
                 }
                 catch (Exception ex)
                 {
                     Logger.Error("Wal Open Error: " + ex.Message);
                 }//删除正在使用的wal会抛出异常
             }
             else
             {
                 FileStream fd = stat.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
                 currentSegmentWriter = new WalSegmentWriter(fd);
                 Interlocked.Add(ref stats.CurrentBytes, stat.Length);
             }
             long totalOldDiskSize = 0;
             foreach (string seg in segments)
             {
                 stat              = new FileInfo(seg);
                 totalOldDiskSize += stat.Length;
                 if (stat.LastWriteTime.Ticks > LastWriteTime)
                 {
                     LastWriteTime = stat.LastWriteTime.Ticks;
                 }
             }
             Interlocked.Add(ref stats.OldBytes, totalOldDiskSize);
         }
     }
 }
Ejemplo n.º 2
0
 // CloseSegment closes the current segment if it is non-empty.
 public void CloseSegment()
 {
     lock (lockthis)
     {
         if (currentSegmentWriter != null)
         {
             currentSegmentWriter.Flush();
             currentSegmentWriter.Close();
             Interlocked.Add(ref stats.OldBytes, currentSegmentWriter.Size);
             currentSegmentWriter = null;
         }
     }
 }
Ejemplo n.º 3
0
 public void Close()
 {
     lock (lockthis)
     {
         if (EnableTraceLogging)
         {
             Logger.Info(string.Format("Closing {0}", Path));
         }
         if (currentSegmentWriter != null)
         {
             currentSegmentWriter.Flush();
             currentSegmentWriter.Close();
             currentSegmentWriter = null;
         }
     }
 }
Ejemplo n.º 4
0
        // newSegmentFile will close the current segment file and open a new one, updating bookkeeping info on the log.
        private void newSegmentFile()
        {
            currentSegmentID++;
            if (currentSegmentWriter != null)
            {
                currentSegmentWriter.Flush();
                currentSegmentWriter.Close();
                Interlocked.Add(ref stats.OldBytes, currentSegmentWriter.Size);
            }

            string     fileName = string.Format("{0}{1}{2:D5}.{3}", Path, Constants.WALFilePrefix, currentSegmentID, Constants.WALFileExtension);
            FileStream fd       = File.Create(fileName);

            currentSegmentWriter = new WalSegmentWriter(fd);

            LastWriteTime = DateTime.Now.Ticks;

            // Reset the current segment size stat
            Interlocked.Add(ref stats.CurrentBytes, 0);
        }