2021-06-21 12:17:00 +00:00
|
|
|
package splitstore
|
|
|
|
|
|
|
|
import (
|
2021-06-29 13:15:45 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2021-06-22 07:10:24 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime/debug"
|
2021-06-29 13:15:45 +00:00
|
|
|
"strings"
|
2021-06-22 07:10:24 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/multierr"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2021-06-21 12:17:00 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type debugLog struct {
|
2021-06-29 13:15:45 +00:00
|
|
|
readPath, writePath, movePath, stackPath string
|
|
|
|
readMx, writeMx, moveMx, stackMx sync.Mutex
|
|
|
|
readLog, writeLog, moveLog, stackLog *os.File
|
|
|
|
readCnt, writeCnt, moveCnt, stackCnt int
|
|
|
|
stackMap map[string]struct{}
|
2021-06-21 12:17:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
func openDebugLog(path string) (*debugLog, error) {
|
|
|
|
basePath := filepath.Join(path, "debug")
|
|
|
|
err := os.MkdirAll(basePath, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error creating debug log directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
readPath := filepath.Join(basePath, "read.log")
|
|
|
|
readFile, err := os.OpenFile(readPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error opening read log: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writePath := filepath.Join(basePath, "write.log")
|
|
|
|
writeFile, err := os.OpenFile(writePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
_ = readFile.Close()
|
|
|
|
return nil, xerrors.Errorf("error opening write log: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
movePath := filepath.Join(basePath, "move.log")
|
|
|
|
moveFile, err := os.OpenFile(movePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
_ = readFile.Close()
|
|
|
|
_ = writeFile.Close()
|
|
|
|
return nil, xerrors.Errorf("error opening move log: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
stackPath := filepath.Join(basePath, "stack.log")
|
|
|
|
stackFile, err := os.OpenFile(stackPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
_ = readFile.Close()
|
|
|
|
_ = writeFile.Close()
|
|
|
|
_ = moveFile.Close()
|
|
|
|
return nil, xerrors.Errorf("error opening stack log: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
return &debugLog{
|
|
|
|
readPath: readPath,
|
|
|
|
writePath: writePath,
|
|
|
|
movePath: movePath,
|
2021-06-29 13:15:45 +00:00
|
|
|
stackPath: stackPath,
|
2021-06-22 07:10:24 +00:00
|
|
|
readLog: readFile,
|
|
|
|
writeLog: writeFile,
|
|
|
|
moveLog: moveFile,
|
2021-06-29 13:15:45 +00:00
|
|
|
stackLog: stackFile,
|
|
|
|
stackMap: make(map[string]struct{}),
|
2021-06-22 07:10:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugLog) LogReadMiss(curTs *types.TipSet, cid cid.Cid) {
|
2021-06-21 12:17:00 +00:00
|
|
|
if d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
stack := d.getStack()
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
var epoch abi.ChainEpoch
|
|
|
|
if curTs != nil {
|
|
|
|
epoch = curTs.Height()
|
|
|
|
}
|
|
|
|
|
|
|
|
d.readMx.Lock()
|
|
|
|
defer d.readMx.Unlock()
|
|
|
|
|
|
|
|
d.readCnt++
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
_, err := fmt.Fprintf(d.readLog, "%s %d %s %s\n", time.Now(), epoch, cid, stack)
|
2021-06-22 07:10:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error writing read log: %s", err)
|
|
|
|
}
|
2021-06-21 12:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugLog) LogWrite(curTs *types.TipSet, blk blocks.Block, writeEpoch abi.ChainEpoch) {
|
|
|
|
if d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
var stack string
|
|
|
|
if enableDebugLogWriteTraces {
|
|
|
|
stack = " " + d.getStack()
|
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
d.writeMx.Lock()
|
|
|
|
defer d.writeMx.Unlock()
|
|
|
|
|
|
|
|
d.writeCnt++
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
_, err := fmt.Fprintf(d.writeLog, "%s %d %s %d%s\n", time.Now(), curTs.Height(), blk.Cid(), writeEpoch, stack)
|
2021-06-22 07:10:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error writing write log: %s", err)
|
|
|
|
}
|
2021-06-21 12:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugLog) LogWriteMany(curTs *types.TipSet, blks []blocks.Block, writeEpoch abi.ChainEpoch) {
|
|
|
|
if d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
var stack string
|
|
|
|
if enableDebugLogWriteTraces {
|
|
|
|
stack = " " + d.getStack()
|
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
d.writeMx.Lock()
|
|
|
|
defer d.writeMx.Unlock()
|
|
|
|
|
|
|
|
d.writeCnt += len(blks)
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
now := time.Now()
|
2021-06-22 07:10:24 +00:00
|
|
|
for _, blk := range blks {
|
2021-06-29 13:15:45 +00:00
|
|
|
_, err := fmt.Fprintf(d.writeLog, "%s %d %s %d%s\n", now, curTs.Height(), blk.Cid(), writeEpoch, stack)
|
2021-06-22 07:10:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error writing write log: %s", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-06-21 12:17:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 07:07:45 +00:00
|
|
|
func (d *debugLog) LogMove(curTs *types.TipSet, cid cid.Cid) {
|
2021-06-21 12:17:00 +00:00
|
|
|
if d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
d.moveMx.Lock()
|
|
|
|
defer d.moveMx.Unlock()
|
|
|
|
|
|
|
|
d.moveCnt++
|
|
|
|
|
2021-06-25 07:07:45 +00:00
|
|
|
_, err := fmt.Fprintf(d.moveLog, "%d %s\n", curTs.Height(), cid)
|
2021-06-22 07:10:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error writing move log: %s", err)
|
|
|
|
}
|
2021-06-21 12:17:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
func (d *debugLog) Flush() {
|
2021-06-22 06:21:19 +00:00
|
|
|
if d == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
// rotate non-empty logs
|
|
|
|
d.rotateReadLog()
|
|
|
|
d.rotateWriteLog()
|
|
|
|
d.rotateMoveLog()
|
2021-06-29 13:15:45 +00:00
|
|
|
d.rotateStackLog()
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugLog) rotateReadLog() {
|
|
|
|
d.readMx.Lock()
|
|
|
|
defer d.readMx.Unlock()
|
|
|
|
|
|
|
|
if d.readCnt == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.rotate(d.readLog, d.readPath)
|
|
|
|
if err != nil {
|
2021-06-29 13:15:45 +00:00
|
|
|
log.Warnf("error rotating read log: %s", err)
|
2021-06-22 07:17:26 +00:00
|
|
|
return
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.readLog, err = os.OpenFile(d.readPath, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
2021-06-29 13:15:45 +00:00
|
|
|
log.Warnf("error opening log file: %s", err)
|
2021-06-22 07:17:26 +00:00
|
|
|
return
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
2021-06-22 07:17:26 +00:00
|
|
|
|
|
|
|
d.readCnt = 0
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugLog) rotateWriteLog() {
|
|
|
|
d.writeMx.Lock()
|
|
|
|
defer d.writeMx.Unlock()
|
|
|
|
|
|
|
|
if d.writeCnt == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.rotate(d.writeLog, d.writePath)
|
|
|
|
if err != nil {
|
2021-06-29 13:15:45 +00:00
|
|
|
log.Warnf("error rotating write log: %s", err)
|
2021-06-22 07:17:26 +00:00
|
|
|
return
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.writeLog, err = os.OpenFile(d.writePath, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
2021-06-29 13:15:45 +00:00
|
|
|
log.Warnf("error opening write log file: %s", err)
|
2021-06-22 07:17:26 +00:00
|
|
|
return
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
2021-06-22 07:17:26 +00:00
|
|
|
|
|
|
|
d.writeCnt = 0
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugLog) rotateMoveLog() {
|
|
|
|
d.moveMx.Lock()
|
|
|
|
defer d.moveMx.Unlock()
|
|
|
|
|
|
|
|
if d.moveCnt == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.rotate(d.moveLog, d.movePath)
|
|
|
|
if err != nil {
|
2021-06-29 13:15:45 +00:00
|
|
|
log.Warnf("error rotating move log: %s", err)
|
2021-06-22 07:17:26 +00:00
|
|
|
return
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.moveLog, err = os.OpenFile(d.movePath, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
2021-06-29 13:15:45 +00:00
|
|
|
log.Warnf("error opening move log file: %s", err)
|
2021-06-22 07:17:26 +00:00
|
|
|
return
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
2021-06-22 07:17:26 +00:00
|
|
|
|
|
|
|
d.moveCnt = 0
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
func (d *debugLog) rotateStackLog() {
|
|
|
|
d.stackMx.Lock()
|
|
|
|
defer d.stackMx.Unlock()
|
|
|
|
|
|
|
|
if d.stackCnt == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.rotate(d.stackLog, d.stackPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error rotating stack log: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.stackLog, err = os.OpenFile(d.stackPath, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error opening stack log file: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d.stackCnt = 0
|
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
func (d *debugLog) rotate(f *os.File, path string) error {
|
|
|
|
err := f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error closing file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
arxivPath := fmt.Sprintf("%s-%d", path, time.Now().Unix())
|
|
|
|
err = os.Rename(path, arxivPath)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error moving file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
cmd := exec.Command("gzip", arxivPath)
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2021-06-29 13:15:45 +00:00
|
|
|
log.Warnf("error compressing log: %s", err)
|
2021-06-22 07:10:24 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
2021-06-22 06:21:19 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 12:17:00 +00:00
|
|
|
func (d *debugLog) Close() error {
|
|
|
|
if d == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-22 07:10:24 +00:00
|
|
|
d.readMx.Lock()
|
|
|
|
err1 := d.readLog.Close()
|
|
|
|
d.readMx.Unlock()
|
|
|
|
|
|
|
|
d.writeMx.Lock()
|
|
|
|
err2 := d.writeLog.Close()
|
|
|
|
d.writeMx.Unlock()
|
|
|
|
|
|
|
|
d.moveMx.Lock()
|
|
|
|
err3 := d.moveLog.Close()
|
|
|
|
d.moveMx.Unlock()
|
|
|
|
|
2021-06-29 13:15:45 +00:00
|
|
|
d.stackMx.Lock()
|
|
|
|
err4 := d.stackLog.Close()
|
|
|
|
d.stackMx.Unlock()
|
|
|
|
|
|
|
|
return multierr.Combine(err1, err2, err3, err4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugLog) getStack() string {
|
|
|
|
sk := d.getNormalizedStackTrace()
|
|
|
|
hash := sha256.Sum256([]byte(sk))
|
|
|
|
key := string(hash[:])
|
|
|
|
repr := hex.EncodeToString(hash[:])
|
|
|
|
|
|
|
|
d.stackMx.Lock()
|
|
|
|
_, ok := d.stackMap[key]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
_, err := fmt.Fprintf(d.stackLog, "%s\n%s\n", repr, sk)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("error writing stack trace: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.stackMap[key] = struct{}{}
|
|
|
|
d.stackCnt++
|
|
|
|
d.stackMx.Unlock()
|
|
|
|
|
|
|
|
return repr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *debugLog) getNormalizedStackTrace() string {
|
|
|
|
sk := string(debug.Stack())
|
|
|
|
|
|
|
|
// Normalization for deduplication
|
|
|
|
// skip first line -- it's the goroutine
|
|
|
|
// for each line that ends in a ), remove the call args -- these are the registers
|
|
|
|
lines := strings.Split(sk, "\n")[1:]
|
|
|
|
for i, line := range lines {
|
|
|
|
if line[len(line)-1] == ')' {
|
|
|
|
idx := strings.LastIndex(line, "(")
|
2021-06-29 13:18:25 +00:00
|
|
|
if idx < 0 {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-29 13:15:45 +00:00
|
|
|
lines[i] = line[:idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(lines, "\n")
|
2021-06-21 12:17:00 +00:00
|
|
|
}
|