聊聊klog的Flush

本文主要研究一下klog的Flushgit

Flush

k8s.io/klog/v2@v2.4.0/klog.gogithub

// Flush flushes all pending log I/O.
func Flush() {
    logging.lockAndFlushAll()
}
Flush方法執行的是logging.lockAndFlushAll()

init

k8s.io/klog/v2@v2.4.0/klog.goapp

// init sets up the defaults and runs flushDaemon.
func init() {
    logging.stderrThreshold = errorLog // Default stderrThreshold is ERROR.
    logging.setVState(0, nil, false)
    logging.logDir = ""
    logging.logFile = ""
    logging.logFileMaxSizeMB = 1800
    logging.toStderr = true
    logging.alsoToStderr = false
    logging.skipHeaders = false
    logging.addDirHeader = false
    logging.skipLogHeaders = false
    logging.oneOutput = false
    go logging.flushDaemon()
}
klog的init方法異步協程執行logging.flushDaemon()

logging.flushDaemon()

k8s.io/klog/v2@v2.4.0/klog.go異步

// flushDaemon periodically flushes the log file buffers.
func (l *loggingT) flushDaemon() {
    for range time.NewTicker(flushInterval).C {
        l.lockAndFlushAll()
    }
}
flushDaemon方法range新建ticker的channel,而後執行l.lockAndFlushAll()

lockAndFlushAll

k8s.io/klog/v2@v2.4.0/klog.goide

// lockAndFlushAll is like flushAll but locks l.mu first.
func (l *loggingT) lockAndFlushAll() {
    l.mu.Lock()
    l.flushAll()
    l.mu.Unlock()
}
lockAndFlushAll使用lock執行flushAll

flushAll

k8s.io/klog/v2@v2.4.0/klog.goui

const (
    infoLog severity = iota
    warningLog
    errorLog
    fatalLog
    numSeverity = 4
)

// flushAll flushes all the logs and attempts to "sync" their data to disk.
// l.mu is held.
func (l *loggingT) flushAll() {
    // Flush from fatal down, in case there's trouble flushing.
    for s := fatalLog; s >= infoLog; s-- {
        file := l.file[s]
        if file != nil {
            file.Flush() // ignore error
            file.Sync()  // ignore error
        }
    }
}
flushAll方法從fatalLog開始遞減到infoLog級別挨個執行l.file[s]的Flush及Sync方法

flushSyncWriter

k8s.io/klog/v2@v2.4.0/klog.gothis

// flushSyncWriter is the interface satisfied by logging destinations.
type flushSyncWriter interface {
    Flush() error
    Sync() error
    io.Writer
}

type Writer interface {
    Write(p []byte) (n int, err error)
}
flushSyncWriter接口定義了Flush、Sync方法,內嵌了io.Writer接口

redirectBuffer

k8s.io/klog/v2@v2.4.0/klog.gocode

// redirectBuffer is used to set an alternate destination for the logs
type redirectBuffer struct {
    w io.Writer
}

func (rb *redirectBuffer) Sync() error {
    return nil
}

func (rb *redirectBuffer) Flush() error {
    return nil
}

func (rb *redirectBuffer) Write(bytes []byte) (n int, err error) {
    return rb.w.Write(bytes)
}
redirectBuffer內嵌了io.Writer,其Write方法經過io.Writer來寫;其Sync及Flush方法都爲空操做

syncBuffer

k8s.io/klog/v2@v2.4.0/klog.goorm

// syncBuffer joins a bufio.Writer to its underlying file, providing access to the
// file's Sync method and providing a wrapper for the Write method that provides log
// file rotation. There are conflicting methods, so the file cannot be embedded.
// l.mu is held for all its methods.
type syncBuffer struct {
    logger *loggingT
    *bufio.Writer
    file     *os.File
    sev      severity
    nbytes   uint64 // The number of bytes written to this file
    maxbytes uint64 // The max number of bytes this syncBuffer.file can hold before cleaning up.
}

func (sb *syncBuffer) Sync() error {
    return sb.file.Sync()
}

func (sb *syncBuffer) Write(p []byte) (n int, err error) {
    if sb.nbytes+uint64(len(p)) >= sb.maxbytes {
        if err := sb.rotateFile(time.Now(), false); err != nil {
            sb.logger.exit(err)
        }
    }
    n, err = sb.Writer.Write(p)
    sb.nbytes += uint64(n)
    if err != nil {
        sb.logger.exit(err)
    }
    return
}
syncBuffer定義了logger、file、sev、nbytes、maxbytes屬性,內嵌了 *bufio.Writer;其Sync方法執行的是 *os.File.Sync;其Flush方法執行的是 *bufio.Writer.Flush

Flush

/usr/local/go/src/bufio/bufio.go協程

type Writer struct {
    err error
    buf []byte
    n   int
    wr  io.Writer
}

// Flush writes any buffered data to the underlying io.Writer.
func (b *Writer) Flush() error {
    if b.err != nil {
        return b.err
    }
    if b.n == 0 {
        return nil
    }
    n, err := b.wr.Write(b.buf[0:b.n])
    if n < b.n && err == nil {
        err = io.ErrShortWrite
    }
    if err != nil {
        if n > 0 && n < b.n {
            copy(b.buf[0:b.n-n], b.buf[n:b.n])
        }
        b.n -= n
        b.err = err
        return err
    }
    b.n = 0
    return nil
}
*bufio.Writer.Flush方法執行的是底層io.Writer的Write方法

rotateFile

// rotateFile closes the syncBuffer's file and starts a new one.
// The startup argument indicates whether this is the initial startup of klog.
// If startup is true, existing files are opened for appending instead of truncated.
func (sb *syncBuffer) rotateFile(now time.Time, startup bool) error {
    if sb.file != nil {
        sb.Flush()
        sb.file.Close()
    }
    var err error
    sb.file, _, err = create(severityName[sb.sev], now, startup)
    if err != nil {
        return err
    }
    if startup {
        fileInfo, err := sb.file.Stat()
        if err != nil {
            return fmt.Errorf("file stat could not get fileinfo: %v", err)
        }
        // init file size
        sb.nbytes = uint64(fileInfo.Size())
    } else {
        sb.nbytes = 0
    }
    sb.Writer = bufio.NewWriterSize(sb.file, bufferSize)

    if sb.logger.skipLogHeaders {
        return nil
    }

    // Write header.
    var buf bytes.Buffer
    fmt.Fprintf(&buf, "Log file created at: %s\n", now.Format("2006/01/02 15:04:05"))
    fmt.Fprintf(&buf, "Running on machine: %s\n", host)
    fmt.Fprintf(&buf, "Binary: Built with %s %s for %s/%s\n", runtime.Compiler, runtime.Version(), runtime.GOOS, runtime.GOARCH)
    fmt.Fprintf(&buf, "Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg\n")
    n, err := sb.file.Write(buf.Bytes())
    sb.nbytes += uint64(n)
    return err
}
syncBuffer.rotateFile方法會設置其Writer爲bufio.NewWriterSize(sb.file, bufferSize),底層writer爲syncBuffer的file

小結

klog的init方法異步協程執行logging.flushDaemon(),它內部執行的是l.lockAndFlushAll();Flush方法是執行l.lockAndFlushAll();l.lockAndFlushAll()方法使用lock執行flushAll;flushAll方法從fatalLog開始遞減到infoLog級別挨個執行l.file[s]的Flush及Sync方法;對於redirectBuffer,其Flush及Sync方法爲空操做;對於syncBuffer,其Sync方法執行的是*os.File.Sync;其Flush方法執行的是*bufio.Writer.Flush,*bufio.Writer.Flush方法執行的是底層io.Writer的Write方法,即syncBuffer的file的Write方法。

doc

相關文章
相關標籤/搜索