github.com/m3db/m3@v1.5.0/src/cmd/tools/read_commitlog/main/main.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package main
    22  
    23  import (
    24  	"encoding/base64"
    25  	"fmt"
    26  	"log"
    27  	"os"
    28  	"sort"
    29  	"time"
    30  
    31  	"github.com/pborman/getopt"
    32  	"go.uber.org/zap"
    33  
    34  	"github.com/m3db/m3/src/x/ident"
    35  	xtime "github.com/m3db/m3/src/x/time"
    36  )
    37  
    38  func main() {
    39  	var (
    40  		path         = getopt.StringLong("path", 'p', "", "file path [e.g. /var/lib/m3db/commitlogs/commitlog-0-161023.db]")
    41  		idFilter     = getopt.StringLong("id-filter", 'f', "", "ID Contains Filter (optional)")
    42  		idSizeFilter = getopt.IntLong("id-size-filter", 's', 0, "ID Size (bytes) Filter (optional)")
    43  		mode         = getopt.StringLong("mode", 'm', "", "Action [print,summary]. Defaults to 'print'")
    44  		top          = getopt.IntLong("top", 't', 0, "Print out only top N IDs")
    45  	)
    46  	getopt.Parse()
    47  
    48  	rawLogger, err := zap.NewDevelopment()
    49  	if err != nil {
    50  		log.Fatalf("unable to create logger: %+v", err)
    51  	}
    52  	logger := rawLogger.Sugar()
    53  
    54  	if *path == "" {
    55  		getopt.Usage()
    56  		os.Exit(1)
    57  	}
    58  
    59  	reader, err := newFilteringReader(*path, idFilter, idSizeFilter)
    60  	if err != nil {
    61  		logger.Fatalf("unable to open reader: %v", err)
    62  	}
    63  	defer reader.Close()
    64  
    65  	switch *mode {
    66  	case "summary":
    67  		err = printSummary(reader, top)
    68  	default:
    69  		err = printMetrics(reader)
    70  	}
    71  	if err != nil {
    72  		logger.Fatalf("error while reading commitlog: %v", err)
    73  	}
    74  }
    75  
    76  func printMetrics(reader *filteringReader) error {
    77  	var (
    78  		entryCount          uint32
    79  		annotationSizeTotal uint64
    80  		start               = time.Now()
    81  	)
    82  
    83  	for {
    84  		entry, found, err := reader.Read()
    85  		if err != nil {
    86  			return err
    87  		}
    88  		if !found {
    89  			break
    90  		}
    91  
    92  		series := entry.Series
    93  		fmt.Printf("{id: %s, dp: %+v, ns: %s, shard: %d", // nolint: forbidigo
    94  			series.ID, entry.Datapoint, entry.Series.Namespace, entry.Series.Shard)
    95  		if len(entry.Annotation) > 0 {
    96  			fmt.Printf(", annotation: %s", // nolint: forbidigo
    97  				base64.StdEncoding.EncodeToString(entry.Annotation))
    98  			annotationSizeTotal += uint64(len(entry.Annotation))
    99  		}
   100  		fmt.Println("}") // nolint: forbidigo
   101  
   102  		entryCount++
   103  	}
   104  
   105  	runTime := time.Since(start)
   106  
   107  	fmt.Printf("\nRunning time: %s\n", runTime)                          // nolint: forbidigo
   108  	fmt.Printf("%d entries read\n", entryCount)                          // nolint: forbidigo
   109  	fmt.Printf("Total annotation size: %d bytes\n", annotationSizeTotal) // nolint: forbidigo
   110  	return nil
   111  }
   112  
   113  func printSummary(reader *filteringReader, top *int) error {
   114  	var (
   115  		entryCount        uint32
   116  		start             = time.Now()
   117  		datapointCount    = map[ident.ID]uint32{}
   118  		totalIDSize       uint64
   119  		earliestDatapoint xtime.UnixNano
   120  		oldestDatapoint   xtime.UnixNano
   121  	)
   122  
   123  	for {
   124  		entry, found, err := reader.Read()
   125  		if err != nil {
   126  			return err
   127  		}
   128  		if !found {
   129  			break
   130  		}
   131  		dp := entry.Datapoint
   132  
   133  		if earliestDatapoint == 0 || earliestDatapoint > dp.TimestampNanos {
   134  			earliestDatapoint = dp.TimestampNanos
   135  		}
   136  		if oldestDatapoint == 0 || oldestDatapoint < dp.TimestampNanos {
   137  			oldestDatapoint = dp.TimestampNanos
   138  		}
   139  
   140  		datapointCount[entry.Series.ID]++
   141  
   142  		entryCount++
   143  	}
   144  
   145  	runTime := time.Since(start)
   146  
   147  	fmt.Printf("\nRunning time: %s\n", runTime)                                              // nolint: forbidigo
   148  	fmt.Printf("%d entries read\n", entryCount)                                              // nolint: forbidigo
   149  	fmt.Printf("time range [%s:%s]\n", earliestDatapoint.String(), oldestDatapoint.String()) // nolint: forbidigo
   150  
   151  	datapointCountArr := idPairs{}
   152  	sizeArr := idPairs{}
   153  	for ID, count := range datapointCount {
   154  		IDSize := len(ID.Bytes())
   155  		totalIDSize += uint64(IDSize)
   156  		datapointCountArr = append(datapointCountArr, idPair{ID: ID, Value: count})
   157  		sizeArr = append(sizeArr, idPair{ID: ID, Value: uint32(IDSize)})
   158  	}
   159  
   160  	sort.Sort(sort.Reverse(datapointCountArr))
   161  	sort.Sort(sort.Reverse(sizeArr))
   162  
   163  	fmt.Printf("total ID size: %d bytes\n", totalIDSize)                  // nolint: forbidigo
   164  	fmt.Printf("total distinct number of IDs %d \n", len(datapointCount)) // nolint: forbidigo
   165  
   166  	limit := len(datapointCountArr)
   167  	if *top > 0 && *top < limit {
   168  		limit = *top
   169  	}
   170  	fmt.Printf("ID datapoint counts: \n") // nolint: forbidigo
   171  	for i := 0; i < limit; i++ {
   172  		pair := datapointCountArr[i]
   173  		fmt.Printf("%-10d %s\n", pair.Value, pair.ID.String()) // nolint: forbidigo
   174  	}
   175  
   176  	fmt.Printf("ID sizes(bytes): \n") // nolint: forbidigo
   177  	for i := 0; i < limit; i++ {
   178  		pair := sizeArr[i]
   179  		fmt.Printf("%-10d %s\n", pair.Value, pair.ID.String()) // nolint: forbidigo
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  type idPair struct {
   186  	ID    ident.ID
   187  	Value uint32
   188  }
   189  
   190  type idPairs []idPair
   191  
   192  func (p idPairs) Len() int           { return len(p) }
   193  func (p idPairs) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
   194  func (p idPairs) Less(i, j int) bool { return p[i].Value < p[j].Value }