LCOV - code coverage report
Current view: top level - pebble/tool - manifest.go (source / functions) Hit Total Coverage
Test: 2024-02-28 08:15Z 70c13977 - tests only.lcov Lines: 400 454 88.1 %
Date: 2024-02-28 08:16:23 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
       2             : // of this source code is governed by a BSD-style license that can be found in
       3             : // the LICENSE file.
       4             : 
       5             : package tool
       6             : 
       7             : import (
       8             :         "cmp"
       9             :         "fmt"
      10             :         "io"
      11             :         "slices"
      12             :         "time"
      13             : 
      14             :         "github.com/cockroachdb/pebble"
      15             :         "github.com/cockroachdb/pebble/internal/base"
      16             :         "github.com/cockroachdb/pebble/internal/humanize"
      17             :         "github.com/cockroachdb/pebble/internal/manifest"
      18             :         "github.com/cockroachdb/pebble/record"
      19             :         "github.com/cockroachdb/pebble/sstable"
      20             :         "github.com/spf13/cobra"
      21             : )
      22             : 
      23             : // manifestT implements manifest-level tools, including both configuration
      24             : // state and the commands themselves.
      25             : type manifestT struct {
      26             :         Root      *cobra.Command
      27             :         Dump      *cobra.Command
      28             :         Summarize *cobra.Command
      29             :         Check     *cobra.Command
      30             : 
      31             :         opts      *pebble.Options
      32             :         comparers sstable.Comparers
      33             :         fmtKey    keyFormatter
      34             :         verbose   bool
      35             : 
      36             :         filterStart key
      37             :         filterEnd   key
      38             : 
      39             :         summarizeDur time.Duration
      40             : }
      41             : 
      42           1 : func newManifest(opts *pebble.Options, comparers sstable.Comparers) *manifestT {
      43           1 :         m := &manifestT{
      44           1 :                 opts:         opts,
      45           1 :                 comparers:    comparers,
      46           1 :                 summarizeDur: time.Hour,
      47           1 :         }
      48           1 :         m.fmtKey.mustSet("quoted")
      49           1 : 
      50           1 :         m.Root = &cobra.Command{
      51           1 :                 Use:   "manifest",
      52           1 :                 Short: "manifest introspection tools",
      53           1 :         }
      54           1 : 
      55           1 :         // Add dump command
      56           1 :         m.Dump = &cobra.Command{
      57           1 :                 Use:   "dump <manifest-files>",
      58           1 :                 Short: "print manifest contents",
      59           1 :                 Long: `
      60           1 : Print the contents of the MANIFEST files.
      61           1 : `,
      62           1 :                 Args: cobra.MinimumNArgs(1),
      63           1 :                 Run:  m.runDump,
      64           1 :         }
      65           1 :         m.Dump.Flags().Var(&m.fmtKey, "key", "key formatter")
      66           1 :         m.Dump.Flags().Var(&m.filterStart, "filter-start", "start key filters out all version edits that only reference sstables containing keys strictly before the given key")
      67           1 :         m.Dump.Flags().Var(&m.filterEnd, "filter-end", "end key filters out all version edits that only reference sstables containing keys at or strictly after the given key")
      68           1 :         m.Root.AddCommand(m.Dump)
      69           1 :         m.Root.PersistentFlags().BoolVarP(&m.verbose, "verbose", "v", false, "verbose output")
      70           1 : 
      71           1 :         // Add summarize command
      72           1 :         m.Summarize = &cobra.Command{
      73           1 :                 Use:   "summarize <manifest-files>",
      74           1 :                 Short: "summarize manifest contents",
      75           1 :                 Long: `
      76           1 : Summarize the edits to the MANIFEST files over time.
      77           1 : `,
      78           1 :                 Args: cobra.MinimumNArgs(1),
      79           1 :                 Run:  m.runSummarize,
      80           1 :         }
      81           1 :         m.Root.AddCommand(m.Summarize)
      82           1 :         m.Summarize.Flags().DurationVar(
      83           1 :                 &m.summarizeDur, "dur", time.Hour, "bucket duration as a Go duration string (eg, '1h', '15m')")
      84           1 : 
      85           1 :         // Add check command
      86           1 :         m.Check = &cobra.Command{
      87           1 :                 Use:   "check <manifest-files>",
      88           1 :                 Short: "check manifest contents",
      89           1 :                 Long: `
      90           1 : Check the contents of the MANIFEST files.
      91           1 : `,
      92           1 :                 Args: cobra.MinimumNArgs(1),
      93           1 :                 Run:  m.runCheck,
      94           1 :         }
      95           1 :         m.Root.AddCommand(m.Check)
      96           1 :         m.Check.Flags().Var(
      97           1 :                 &m.fmtKey, "key", "key formatter")
      98           1 : 
      99           1 :         return m
     100           1 : }
     101             : 
     102           1 : func (m *manifestT) printLevels(cmp base.Compare, stdout io.Writer, v *manifest.Version) {
     103           1 :         for level := range v.Levels {
     104           1 :                 if level == 0 && len(v.L0SublevelFiles) > 0 && !v.Levels[level].Empty() {
     105           1 :                         for sublevel := len(v.L0SublevelFiles) - 1; sublevel >= 0; sublevel-- {
     106           1 :                                 fmt.Fprintf(stdout, "--- L0.%d ---\n", sublevel)
     107           1 :                                 v.L0SublevelFiles[sublevel].Each(func(f *manifest.FileMetadata) {
     108           1 :                                         if !anyOverlapFile(cmp, f, m.filterStart, m.filterEnd) {
     109           1 :                                                 return
     110           1 :                                         }
     111           1 :                                         fmt.Fprintf(stdout, "  %s:%d", f.FileNum, f.Size)
     112           1 :                                         formatSeqNumRange(stdout, f.SmallestSeqNum, f.LargestSeqNum)
     113           1 :                                         formatKeyRange(stdout, m.fmtKey, &f.Smallest, &f.Largest)
     114           1 :                                         if f.Virtual {
     115           0 :                                                 fmt.Fprintf(stdout, "(virtual:backingNum=%s)", f.FileBacking.DiskFileNum)
     116           0 :                                         }
     117           1 :                                         fmt.Fprintf(stdout, "\n")
     118             :                                 })
     119             :                         }
     120           1 :                         continue
     121             :                 }
     122           1 :                 fmt.Fprintf(stdout, "--- L%d ---\n", level)
     123           1 :                 iter := v.Levels[level].Iter()
     124           1 :                 for f := iter.First(); f != nil; f = iter.Next() {
     125           1 :                         if !anyOverlapFile(cmp, f, m.filterStart, m.filterEnd) {
     126           0 :                                 continue
     127             :                         }
     128           1 :                         fmt.Fprintf(stdout, "  %s:%d", f.FileNum, f.Size)
     129           1 :                         formatSeqNumRange(stdout, f.SmallestSeqNum, f.LargestSeqNum)
     130           1 :                         formatKeyRange(stdout, m.fmtKey, &f.Smallest, &f.Largest)
     131           1 :                         if f.Virtual {
     132           0 :                                 fmt.Fprintf(stdout, "(virtual:backingNum=%s)", f.FileBacking.DiskFileNum)
     133           0 :                         }
     134           1 :                         fmt.Fprintf(stdout, "\n")
     135             :                 }
     136             :         }
     137             : }
     138             : 
     139           1 : func (m *manifestT) runDump(cmd *cobra.Command, args []string) {
     140           1 :         stdout, stderr := cmd.OutOrStdout(), cmd.OutOrStderr()
     141           1 :         for _, arg := range args {
     142           1 :                 func() {
     143           1 :                         f, err := m.opts.FS.Open(arg)
     144           1 :                         if err != nil {
     145           0 :                                 fmt.Fprintf(stderr, "%s\n", err)
     146           0 :                                 return
     147           0 :                         }
     148           1 :                         defer f.Close()
     149           1 : 
     150           1 :                         fmt.Fprintf(stdout, "%s\n", arg)
     151           1 : 
     152           1 :                         var bve manifest.BulkVersionEdit
     153           1 :                         bve.AddedByFileNum = make(map[base.FileNum]*manifest.FileMetadata)
     154           1 :                         var comparer *base.Comparer
     155           1 :                         var editIdx int
     156           1 :                         rr := record.NewReader(f, 0 /* logNum */)
     157           1 :                         for {
     158           1 :                                 offset := rr.Offset()
     159           1 :                                 r, err := rr.Next()
     160           1 :                                 if err != nil {
     161           1 :                                         fmt.Fprintf(stdout, "%s\n", err)
     162           1 :                                         break
     163             :                                 }
     164             : 
     165           1 :                                 var ve manifest.VersionEdit
     166           1 :                                 err = ve.Decode(r)
     167           1 :                                 if err != nil {
     168           0 :                                         fmt.Fprintf(stdout, "%s\n", err)
     169           0 :                                         break
     170             :                                 }
     171           1 :                                 if err := bve.Accumulate(&ve); err != nil {
     172           0 :                                         fmt.Fprintf(stdout, "%s\n", err)
     173           0 :                                         break
     174             :                                 }
     175             : 
     176           1 :                                 if comparer != nil && !anyOverlap(comparer.Compare, &ve, m.filterStart, m.filterEnd) {
     177           1 :                                         continue
     178             :                                 }
     179             : 
     180           1 :                                 empty := true
     181           1 :                                 fmt.Fprintf(stdout, "%d/%d\n", offset, editIdx)
     182           1 :                                 if ve.ComparerName != "" {
     183           1 :                                         empty = false
     184           1 :                                         fmt.Fprintf(stdout, "  comparer:     %s", ve.ComparerName)
     185           1 :                                         comparer = m.comparers[ve.ComparerName]
     186           1 :                                         if comparer == nil {
     187           0 :                                                 fmt.Fprintf(stdout, " (unknown)")
     188           0 :                                         }
     189           1 :                                         fmt.Fprintf(stdout, "\n")
     190           1 :                                         m.fmtKey.setForComparer(ve.ComparerName, m.comparers)
     191             :                                 }
     192           1 :                                 if ve.MinUnflushedLogNum != 0 {
     193           1 :                                         empty = false
     194           1 :                                         fmt.Fprintf(stdout, "  log-num:       %d\n", ve.MinUnflushedLogNum)
     195           1 :                                 }
     196           1 :                                 if ve.ObsoletePrevLogNum != 0 {
     197           0 :                                         empty = false
     198           0 :                                         fmt.Fprintf(stdout, "  prev-log-num:  %d\n", ve.ObsoletePrevLogNum)
     199           0 :                                 }
     200           1 :                                 if ve.NextFileNum != 0 {
     201           1 :                                         empty = false
     202           1 :                                         fmt.Fprintf(stdout, "  next-file-num: %d\n", ve.NextFileNum)
     203           1 :                                 }
     204           1 :                                 if ve.LastSeqNum != 0 {
     205           1 :                                         empty = false
     206           1 :                                         fmt.Fprintf(stdout, "  last-seq-num:  %d\n", ve.LastSeqNum)
     207           1 :                                 }
     208           1 :                                 entries := make([]manifest.DeletedFileEntry, 0, len(ve.DeletedFiles))
     209           1 :                                 for df := range ve.DeletedFiles {
     210           1 :                                         empty = false
     211           1 :                                         entries = append(entries, df)
     212           1 :                                 }
     213           1 :                                 slices.SortFunc(entries, func(a, b manifest.DeletedFileEntry) int {
     214           1 :                                         if v := cmp.Compare(a.Level, b.Level); v != 0 {
     215           1 :                                                 return v
     216           1 :                                         }
     217           1 :                                         return cmp.Compare(a.FileNum, b.FileNum)
     218             :                                 })
     219           1 :                                 for _, df := range entries {
     220           1 :                                         fmt.Fprintf(stdout, "  deleted:       L%d %s\n", df.Level, df.FileNum)
     221           1 :                                 }
     222           1 :                                 for _, nf := range ve.NewFiles {
     223           1 :                                         empty = false
     224           1 :                                         fmt.Fprintf(stdout, "  added:         L%d %s:%d",
     225           1 :                                                 nf.Level, nf.Meta.FileNum, nf.Meta.Size)
     226           1 :                                         formatSeqNumRange(stdout, nf.Meta.SmallestSeqNum, nf.Meta.LargestSeqNum)
     227           1 :                                         formatKeyRange(stdout, m.fmtKey, &nf.Meta.Smallest, &nf.Meta.Largest)
     228           1 :                                         if nf.Meta.CreationTime != 0 {
     229           1 :                                                 fmt.Fprintf(stdout, " (%s)",
     230           1 :                                                         time.Unix(nf.Meta.CreationTime, 0).UTC().Format(time.RFC3339))
     231           1 :                                         }
     232           1 :                                         fmt.Fprintf(stdout, "\n")
     233             :                                 }
     234           1 :                                 if empty {
     235           0 :                                         // NB: An empty version edit can happen if we log a version edit with
     236           0 :                                         // a zero field. RocksDB does this with a version edit that contains
     237           0 :                                         // `LogNum == 0`.
     238           0 :                                         fmt.Fprintf(stdout, "  <empty>\n")
     239           0 :                                 }
     240           1 :                                 editIdx++
     241             :                         }
     242             : 
     243           1 :                         if comparer != nil {
     244           1 :                                 v, err := bve.Apply(
     245           1 :                                         nil /* version */, comparer, 0,
     246           1 :                                         m.opts.Experimental.ReadCompactionRate,
     247           1 :                                         nil, /* zombies */
     248           1 :                                 )
     249           1 :                                 if err != nil {
     250           1 :                                         fmt.Fprintf(stdout, "%s\n", err)
     251           1 :                                         return
     252           1 :                                 }
     253           1 :                                 m.printLevels(comparer.Compare, stdout, v)
     254             :                         }
     255             :                 }()
     256             :         }
     257             : }
     258             : 
     259           1 : func anyOverlap(cmp base.Compare, ve *manifest.VersionEdit, start, end key) bool {
     260           1 :         if start == nil && end == nil {
     261           1 :                 return true
     262           1 :         }
     263           1 :         for _, df := range ve.DeletedFiles {
     264           1 :                 if anyOverlapFile(cmp, df, start, end) {
     265           1 :                         return true
     266           1 :                 }
     267             :         }
     268           1 :         for _, nf := range ve.NewFiles {
     269           1 :                 if anyOverlapFile(cmp, nf.Meta, start, end) {
     270           1 :                         return true
     271           1 :                 }
     272             :         }
     273           1 :         return false
     274             : }
     275             : 
     276           1 : func anyOverlapFile(cmp base.Compare, f *manifest.FileMetadata, start, end key) bool {
     277           1 :         if f == nil {
     278           1 :                 return true
     279           1 :         }
     280           1 :         if start != nil {
     281           1 :                 if v := cmp(f.Largest.UserKey, start); v < 0 {
     282           1 :                         return false
     283           1 :                 } else if f.Largest.IsExclusiveSentinel() && v == 0 {
     284           0 :                         return false
     285           0 :                 }
     286             :         }
     287           1 :         if end != nil && cmp(f.Smallest.UserKey, end) >= 0 {
     288           1 :                 return false
     289           1 :         }
     290           1 :         return true
     291             : }
     292             : 
     293           1 : func (m *manifestT) runSummarize(cmd *cobra.Command, args []string) {
     294           1 :         for _, arg := range args {
     295           1 :                 err := m.runSummarizeOne(cmd.OutOrStdout(), arg)
     296           1 :                 if err != nil {
     297           0 :                         fmt.Fprintf(cmd.OutOrStderr(), "%s\n", err)
     298           0 :                 }
     299             :         }
     300             : }
     301             : 
     302           1 : func (m *manifestT) runSummarizeOne(stdout io.Writer, arg string) error {
     303           1 :         f, err := m.opts.FS.Open(arg)
     304           1 :         if err != nil {
     305           0 :                 return err
     306           0 :         }
     307           1 :         defer f.Close()
     308           1 :         fmt.Fprintf(stdout, "%s\n", arg)
     309           1 : 
     310           1 :         type summaryBucket struct {
     311           1 :                 bytesAdded      [manifest.NumLevels]uint64
     312           1 :                 bytesCompactOut [manifest.NumLevels]uint64
     313           1 :         }
     314           1 :         var (
     315           1 :                 bve           manifest.BulkVersionEdit
     316           1 :                 newestOverall time.Time
     317           1 :                 oldestOverall time.Time // oldest after initial version edit
     318           1 :                 buckets       = map[time.Time]*summaryBucket{}
     319           1 :                 metadatas     = map[base.FileNum]*manifest.FileMetadata{}
     320           1 :         )
     321           1 :         bve.AddedByFileNum = make(map[base.FileNum]*manifest.FileMetadata)
     322           1 :         rr := record.NewReader(f, 0 /* logNum */)
     323           1 :         for i := 0; ; i++ {
     324           1 :                 r, err := rr.Next()
     325           1 :                 if err == io.EOF {
     326           1 :                         break
     327           1 :                 } else if err != nil {
     328           0 :                         return err
     329           0 :                 }
     330             : 
     331           1 :                 var ve manifest.VersionEdit
     332           1 :                 err = ve.Decode(r)
     333           1 :                 if err != nil {
     334           0 :                         return err
     335           0 :                 }
     336           1 :                 if err := bve.Accumulate(&ve); err != nil {
     337           0 :                         return err
     338           0 :                 }
     339             : 
     340           1 :                 veNewest, veOldest := newestOverall, newestOverall
     341           1 :                 for _, nf := range ve.NewFiles {
     342           1 :                         _, seen := metadatas[nf.Meta.FileNum]
     343           1 :                         metadatas[nf.Meta.FileNum] = nf.Meta
     344           1 :                         if nf.Meta.CreationTime == 0 {
     345           0 :                                 continue
     346             :                         }
     347             : 
     348           1 :                         t := time.Unix(nf.Meta.CreationTime, 0).UTC()
     349           1 :                         if veNewest.Before(t) {
     350           1 :                                 veNewest = t
     351           1 :                         }
     352             :                         // Only update the oldest if we haven't already seen this
     353             :                         // file; it might've been moved in which case the sstable's
     354             :                         // creation time is from when it was originally created.
     355           1 :                         if veOldest.After(t) && !seen {
     356           0 :                                 veOldest = t
     357           0 :                         }
     358             :                 }
     359             :                 // Ratchet up the most recent timestamp we've seen.
     360           1 :                 if newestOverall.Before(veNewest) {
     361           1 :                         newestOverall = veNewest
     362           1 :                 }
     363             : 
     364           1 :                 if i == 0 || newestOverall.IsZero() {
     365           1 :                         continue
     366             :                 }
     367             :                 // Update oldestOverall once, when we encounter the first version edit
     368             :                 // at index >= 1. It should be approximately the start time of the
     369             :                 // manifest.
     370           1 :                 if !newestOverall.IsZero() && oldestOverall.IsZero() {
     371           1 :                         oldestOverall = newestOverall
     372           1 :                 }
     373             : 
     374           1 :                 bucketKey := newestOverall.Truncate(m.summarizeDur)
     375           1 :                 b := buckets[bucketKey]
     376           1 :                 if b == nil {
     377           1 :                         b = &summaryBucket{}
     378           1 :                         buckets[bucketKey] = b
     379           1 :                 }
     380             : 
     381             :                 // Increase `bytesAdded` for any version edits that only add files.
     382             :                 // These are either flushes or ingests.
     383           1 :                 if len(ve.NewFiles) > 0 && len(ve.DeletedFiles) == 0 {
     384           1 :                         for _, nf := range ve.NewFiles {
     385           1 :                                 b.bytesAdded[nf.Level] += nf.Meta.Size
     386           1 :                         }
     387           1 :                         continue
     388             :                 }
     389             : 
     390             :                 // Increase `bytesCompactOut` for the input level of any compactions
     391             :                 // that remove bytes from a level (excluding intra-L0 compactions).
     392             :                 // compactions.
     393           1 :                 destLevel := -1
     394           1 :                 if len(ve.NewFiles) > 0 {
     395           1 :                         destLevel = ve.NewFiles[0].Level
     396           1 :                 }
     397           1 :                 for dfe := range ve.DeletedFiles {
     398           1 :                         if dfe.Level != destLevel {
     399           1 :                                 b.bytesCompactOut[dfe.Level] += metadatas[dfe.FileNum].Size
     400           1 :                         }
     401             :                 }
     402             :         }
     403             : 
     404           1 :         formatUint64 := func(v uint64, _ time.Duration) string {
     405           1 :                 if v == 0 {
     406           1 :                         return "."
     407           1 :                 }
     408           1 :                 return humanize.Bytes.Uint64(v).String()
     409             :         }
     410           1 :         formatRate := func(v uint64, dur time.Duration) string {
     411           1 :                 if v == 0 {
     412           1 :                         return "."
     413           1 :                 }
     414           1 :                 secs := dur.Seconds()
     415           1 :                 if secs == 0 {
     416           1 :                         secs = 1
     417           1 :                 }
     418           1 :                 return humanize.Bytes.Uint64(uint64(float64(v)/secs)).String() + "/s"
     419             :         }
     420             : 
     421           1 :         if newestOverall.IsZero() {
     422           1 :                 fmt.Fprintf(stdout, "(no timestamps)\n")
     423           1 :         } else {
     424           1 :                 // NB: bt begins unaligned with the bucket duration (m.summarizeDur),
     425           1 :                 // but after the first bucket will always be aligned.
     426           1 :                 for bi, bt := 0, oldestOverall; !bt.After(newestOverall); bi, bt = bi+1, bt.Truncate(m.summarizeDur).Add(m.summarizeDur) {
     427           1 :                         // Truncate the start time to calculate the bucket key, and
     428           1 :                         // retrieve the appropriate bucket.
     429           1 :                         bk := bt.Truncate(m.summarizeDur)
     430           1 :                         var bucket summaryBucket
     431           1 :                         if buckets[bk] != nil {
     432           1 :                                 bucket = *buckets[bk]
     433           1 :                         }
     434             : 
     435           1 :                         if bi%10 == 0 {
     436           1 :                                 fmt.Fprintf(stdout, "                     ")
     437           1 :                                 fmt.Fprintf(stdout, "_______L0_______L1_______L2_______L3_______L4_______L5_______L6_____TOTAL\n")
     438           1 :                         }
     439           1 :                         fmt.Fprintf(stdout, "%s\n", bt.Format(time.RFC3339))
     440           1 : 
     441           1 :                         // Compute the bucket duration. It may < `m.summarizeDur` if this is
     442           1 :                         // the first or last bucket.
     443           1 :                         bucketEnd := bt.Truncate(m.summarizeDur).Add(m.summarizeDur)
     444           1 :                         if bucketEnd.After(newestOverall) {
     445           1 :                                 bucketEnd = newestOverall
     446           1 :                         }
     447           1 :                         dur := bucketEnd.Sub(bt)
     448           1 : 
     449           1 :                         stats := []struct {
     450           1 :                                 label  string
     451           1 :                                 format func(uint64, time.Duration) string
     452           1 :                                 vals   [manifest.NumLevels]uint64
     453           1 :                         }{
     454           1 :                                 {"Ingest+Flush", formatUint64, bucket.bytesAdded},
     455           1 :                                 {"Ingest+Flush", formatRate, bucket.bytesAdded},
     456           1 :                                 {"Compact (out)", formatUint64, bucket.bytesCompactOut},
     457           1 :                                 {"Compact (out)", formatRate, bucket.bytesCompactOut},
     458           1 :                         }
     459           1 :                         for _, stat := range stats {
     460           1 :                                 var sum uint64
     461           1 :                                 for _, v := range stat.vals {
     462           1 :                                         sum += v
     463           1 :                                 }
     464           1 :                                 fmt.Fprintf(stdout, "%20s   %8s %8s %8s %8s %8s %8s %8s %8s\n",
     465           1 :                                         stat.label,
     466           1 :                                         stat.format(stat.vals[0], dur),
     467           1 :                                         stat.format(stat.vals[1], dur),
     468           1 :                                         stat.format(stat.vals[2], dur),
     469           1 :                                         stat.format(stat.vals[3], dur),
     470           1 :                                         stat.format(stat.vals[4], dur),
     471           1 :                                         stat.format(stat.vals[5], dur),
     472           1 :                                         stat.format(stat.vals[6], dur),
     473           1 :                                         stat.format(sum, dur))
     474             :                         }
     475             :                 }
     476           1 :                 fmt.Fprintf(stdout, "%s\n", newestOverall.Format(time.RFC3339))
     477             :         }
     478             : 
     479           1 :         dur := newestOverall.Sub(oldestOverall)
     480           1 :         fmt.Fprintf(stdout, "---\n")
     481           1 :         fmt.Fprintf(stdout, "Estimated start time: %s\n", oldestOverall.Format(time.RFC3339))
     482           1 :         fmt.Fprintf(stdout, "Estimated end time:   %s\n", newestOverall.Format(time.RFC3339))
     483           1 :         fmt.Fprintf(stdout, "Estimated duration:   %s\n", dur.String())
     484           1 : 
     485           1 :         return nil
     486             : }
     487             : 
     488           1 : func (m *manifestT) runCheck(cmd *cobra.Command, args []string) {
     489           1 :         stdout, stderr := cmd.OutOrStdout(), cmd.OutOrStderr()
     490           1 :         ok := true
     491           1 :         for _, arg := range args {
     492           1 :                 func() {
     493           1 :                         f, err := m.opts.FS.Open(arg)
     494           1 :                         if err != nil {
     495           1 :                                 fmt.Fprintf(stderr, "%s\n", err)
     496           1 :                                 ok = false
     497           1 :                                 return
     498           1 :                         }
     499           1 :                         defer f.Close()
     500           1 : 
     501           1 :                         var v *manifest.Version
     502           1 :                         var cmp *base.Comparer
     503           1 :                         rr := record.NewReader(f, 0 /* logNum */)
     504           1 :                         // Contains the FileMetadata needed by BulkVersionEdit.Apply.
     505           1 :                         // It accumulates the additions since later edits contain
     506           1 :                         // deletions of earlier added files.
     507           1 :                         addedByFileNum := make(map[base.FileNum]*manifest.FileMetadata)
     508           1 :                         for {
     509           1 :                                 offset := rr.Offset()
     510           1 :                                 r, err := rr.Next()
     511           1 :                                 if err != nil {
     512           1 :                                         if err == io.EOF {
     513           1 :                                                 break
     514             :                                         }
     515           0 :                                         fmt.Fprintf(stdout, "%s: offset: %d err: %s\n", arg, offset, err)
     516           0 :                                         ok = false
     517           0 :                                         break
     518             :                                 }
     519             : 
     520           1 :                                 var ve manifest.VersionEdit
     521           1 :                                 err = ve.Decode(r)
     522           1 :                                 if err != nil {
     523           0 :                                         fmt.Fprintf(stdout, "%s: offset: %d err: %s\n", arg, offset, err)
     524           0 :                                         ok = false
     525           0 :                                         break
     526             :                                 }
     527           1 :                                 var bve manifest.BulkVersionEdit
     528           1 :                                 bve.AddedByFileNum = addedByFileNum
     529           1 :                                 if err := bve.Accumulate(&ve); err != nil {
     530           0 :                                         fmt.Fprintf(stderr, "%s\n", err)
     531           0 :                                         ok = false
     532           0 :                                         return
     533           0 :                                 }
     534             : 
     535           1 :                                 empty := true
     536           1 :                                 if ve.ComparerName != "" {
     537           1 :                                         empty = false
     538           1 :                                         cmp = m.comparers[ve.ComparerName]
     539           1 :                                         if cmp == nil {
     540           0 :                                                 fmt.Fprintf(stdout, "%s: offset: %d comparer %s not found",
     541           0 :                                                         arg, offset, ve.ComparerName)
     542           0 :                                                 ok = false
     543           0 :                                                 break
     544             :                                         }
     545           1 :                                         m.fmtKey.setForComparer(ve.ComparerName, m.comparers)
     546             :                                 }
     547           1 :                                 empty = empty && ve.MinUnflushedLogNum == 0 && ve.ObsoletePrevLogNum == 0 &&
     548           1 :                                         ve.LastSeqNum == 0 && len(ve.DeletedFiles) == 0 &&
     549           1 :                                         len(ve.NewFiles) == 0
     550           1 :                                 if empty {
     551           0 :                                         continue
     552             :                                 }
     553             :                                 // TODO(sbhola): add option to Apply that reports all errors instead of
     554             :                                 // one error.
     555           1 :                                 newv, err := bve.Apply(v, cmp, 0, m.opts.Experimental.ReadCompactionRate, nil /* zombies */)
     556           1 :                                 if err != nil {
     557           1 :                                         fmt.Fprintf(stdout, "%s: offset: %d err: %s\n",
     558           1 :                                                 arg, offset, err)
     559           1 :                                         fmt.Fprintf(stdout, "Version state before failed Apply\n")
     560           1 :                                         m.printLevels(cmp.Compare, stdout, v)
     561           1 :                                         fmt.Fprintf(stdout, "Version edit that failed\n")
     562           1 :                                         for df := range ve.DeletedFiles {
     563           0 :                                                 fmt.Fprintf(stdout, "  deleted: L%d %s\n", df.Level, df.FileNum)
     564           0 :                                         }
     565           1 :                                         for _, nf := range ve.NewFiles {
     566           1 :                                                 fmt.Fprintf(stdout, "  added: L%d %s:%d",
     567           1 :                                                         nf.Level, nf.Meta.FileNum, nf.Meta.Size)
     568           1 :                                                 formatSeqNumRange(stdout, nf.Meta.SmallestSeqNum, nf.Meta.LargestSeqNum)
     569           1 :                                                 formatKeyRange(stdout, m.fmtKey, &nf.Meta.Smallest, &nf.Meta.Largest)
     570           1 :                                                 fmt.Fprintf(stdout, "\n")
     571           1 :                                         }
     572           1 :                                         ok = false
     573           1 :                                         break
     574             :                                 }
     575           1 :                                 v = newv
     576             :                         }
     577             :                 }()
     578             :         }
     579           1 :         if ok {
     580           1 :                 fmt.Fprintf(stdout, "OK\n")
     581           1 :         }
     582             : }

Generated by: LCOV version 1.14