github.com/matrixorigin/matrixone@v1.2.0/pkg/util/export/example/main.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Bin to show how Merge Task work. Helps to debug code, or optimization.
    16  //
    17  // There are two type table data: one is dummyStatementTable, with primary key; another is dummyRawlogTable, without primary key.
    18  // Both of them are code-copied from pkg "github.com/matrixorigin/matrixone/pkg/util/trace", caused by import-cycle issue.
    19  //
    20  // If you want to run this code, you need some source data, that could copy from folder `mo-data/etl/sys/logs/...`, which generated by mo-service startup with config `etc/launch-tae-logservice`
    21  package main
    22  
    23  import (
    24  	"context"
    25  	"net/http"
    26  	_ "net/http/pprof"
    27  	"os"
    28  	"runtime"
    29  	"runtime/pprof"
    30  	"sync"
    31  	"time"
    32  
    33  	"github.com/matrixorigin/matrixone/pkg/config"
    34  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    35  	"github.com/matrixorigin/matrixone/pkg/txn/clock"
    36  	"github.com/matrixorigin/matrixone/pkg/util/metric/mometric"
    37  	"go.uber.org/zap/zapcore"
    38  
    39  	morun "github.com/matrixorigin/matrixone/pkg/common/runtime"
    40  	"github.com/matrixorigin/matrixone/pkg/defines"
    41  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    42  	"github.com/matrixorigin/matrixone/pkg/logutil"
    43  	"github.com/matrixorigin/matrixone/pkg/util/export"
    44  	"github.com/matrixorigin/matrixone/pkg/util/export/table"
    45  	"github.com/matrixorigin/matrixone/pkg/util/trace"
    46  	"github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace"
    47  )
    48  
    49  func main() {
    50  
    51  	ctx := context.Background()
    52  	logutil.SetupMOLogger(&logutil.LogConfig{
    53  		Level:      zapcore.InfoLevel.String(),
    54  		Format:     "console",
    55  		Filename:   "",
    56  		MaxSize:    512,
    57  		MaxDays:    0,
    58  		MaxBackups: 0,
    59  
    60  		DisableStore: true,
    61  	})
    62  
    63  	fs, err := fileservice.NewLocalETLFS(defines.ETLFileServiceName, "mo-data/etl")
    64  	if err != nil {
    65  		logutil.Infof("failed open fileservice: %v", err)
    66  		return
    67  	}
    68  	files, err := fs.List(ctx, "/")
    69  	if err != nil {
    70  		logutil.Infof("failed list /: %v", err)
    71  		return
    72  	}
    73  	if len(files) == 0 {
    74  		logutil.Infof("skipping, no mo-data/etl folder")
    75  		return
    76  	}
    77  
    78  	httpWG := sync.WaitGroup{}
    79  	httpWG.Add(1)
    80  	go func() {
    81  		httpWG.Done()
    82  		http.ListenAndServe("0.0.0.0:8123", nil)
    83  	}()
    84  	httpWG.Wait()
    85  	time.Sleep(time.Second)
    86  
    87  	ctx, cancel := context.WithCancel(ctx)
    88  	go traceMemStats(ctx)
    89  
    90  	SV := config.NewObservabilityParameters()
    91  	SV.SetDefaultValues("test")
    92  	SV.MergeCycle.Duration = 5 * time.Minute
    93  	if err := export.InitMerge(ctx, SV); err != nil {
    94  		panic(err)
    95  	}
    96  	dr := morun.NewRuntime(
    97  		metadata.ServiceType_CN,
    98  		"",
    99  		logutil.GetGlobalLogger(),
   100  		morun.WithClock(clock.NewHLCClock(func() int64 {
   101  			return time.Now().UTC().UnixNano()
   102  		}, 0)))
   103  	morun.SetupProcessLevelRuntime(dr)
   104  	err, _ = motrace.Init(ctx, motrace.EnableTracer(true))
   105  	if err != nil {
   106  		panic(err)
   107  	}
   108  
   109  	mergeAll(ctx, fs)
   110  
   111  	mergeTable(ctx, fs, motrace.SingleStatementTable)
   112  	mergeTable(ctx, fs, motrace.SingleRowLogTable)
   113  	mergeTable(ctx, fs, mometric.SingleMetricTable)
   114  
   115  	logutil.Infof("all done, run sleep(5)")
   116  	time.Sleep(5 * time.Second)
   117  	cancel()
   118  }
   119  
   120  func mergeAll(ctx context.Context, fs *fileservice.LocalETLFS) {
   121  	ctx, span := trace.Start(ctx, "mergeTable")
   122  	defer span.End()
   123  	var err error
   124  	var merge *export.Merge
   125  	merge, err = export.NewMerge(ctx, export.WithTable(motrace.SingleStatementTable), export.WithFileService(fs))
   126  	if err != nil {
   127  		logutil.Infof("[%v] failed to NewMerge: %v", "All", err)
   128  	}
   129  	err = merge.Main(ctx)
   130  	if err != nil {
   131  		logutil.Infof("[%v] failed to merge: %v", "All", err)
   132  	} else {
   133  		logutil.Infof("[%v] merge succeed.", "All")
   134  	}
   135  
   136  	writeAllocsProfile("All")
   137  
   138  }
   139  
   140  func mergeTable(ctx context.Context, fs *fileservice.LocalETLFS, table *table.Table) {
   141  	var err error
   142  	ctx, span := trace.Start(ctx, "mergeTable")
   143  	defer span.End()
   144  	merge, err := export.NewMerge(ctx, export.WithTable(table), export.WithFileService(fs))
   145  	logutil.Infof("[%v] create merge task, err: %v", table.GetName(), err)
   146  	ts, err := time.Parse("2006-01-02 15:04:05", "2023-01-03 00:00:00")
   147  	logutil.Infof("[%v] create ts: %v, err: %v", table.GetName(), ts, err)
   148  	err = merge.Main(ctx)
   149  	if err != nil {
   150  		logutil.Infof("[%v] failed to merge: %v", table.GetName(), err)
   151  	} else {
   152  		logutil.Infof("[%v] merge succeed.", table.GetName())
   153  	}
   154  
   155  	writeAllocsProfile(table.GetName())
   156  }
   157  
   158  func writeAllocsProfile(suffix string) {
   159  	profile := pprof.Lookup("heap")
   160  	if profile == nil {
   161  		return
   162  	}
   163  	profilePath := ""
   164  	if profilePath == "" {
   165  		profilePath = "heap-profile"
   166  	}
   167  	if len(suffix) > 0 {
   168  		profilePath = profilePath + "." + suffix
   169  	}
   170  	f, err := os.Create(profilePath)
   171  	if err != nil {
   172  		panic(err)
   173  	}
   174  	defer f.Close()
   175  	if err := profile.WriteTo(f, 0); err != nil {
   176  		panic(err)
   177  	}
   178  	logutil.Infof("Allocs profile written to %s", profilePath)
   179  }
   180  
   181  func traceMemStats(ctx context.Context) {
   182  	var ms runtime.MemStats
   183  Loop:
   184  	for {
   185  		select {
   186  		case <-ctx.Done():
   187  			break Loop
   188  		case <-time.After(time.Second):
   189  			runtime.ReadMemStats(&ms)
   190  			logutil.Infof("Alloc:%10d(bytes) HeapIdle:%10d(bytes) HeapReleased:%10d(bytes)", ms.Alloc, ms.HeapIdle, ms.HeapReleased)
   191  		}
   192  	}
   193  }