2014-06-07 12:56:22 +04:00
|
|
|
package ledis
|
2014-06-03 10:08:57 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2014-06-07 12:56:22 +04:00
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
2014-06-03 10:08:57 +04:00
|
|
|
"fmt"
|
2014-07-04 09:27:57 +04:00
|
|
|
"github.com/siddontang/go-log/log"
|
2014-06-03 10:08:57 +04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-06-07 12:56:22 +04:00
|
|
|
"time"
|
2014-06-03 10:08:57 +04:00
|
|
|
)
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
const (
|
|
|
|
MaxBinLogFileSize int = 1024 * 1024 * 1024
|
|
|
|
MaxBinLogFileNum int = 10000
|
|
|
|
|
|
|
|
DefaultBinLogFileSize int = MaxBinLogFileSize
|
|
|
|
DefaultBinLogFileNum int = 10
|
2014-06-03 10:08:57 +04:00
|
|
|
)
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
/*
|
|
|
|
index file format:
|
|
|
|
ledis-bin.00001
|
|
|
|
ledis-bin.00002
|
|
|
|
ledis-bin.00003
|
|
|
|
|
|
|
|
log file format
|
|
|
|
|
|
|
|
timestamp(bigendian uint32, seconds)|PayloadLen(bigendian uint32)|PayloadData
|
2014-06-03 10:08:57 +04:00
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
type BinLogConfig struct {
|
2014-06-03 10:08:57 +04:00
|
|
|
Path string `json:"path"`
|
|
|
|
MaxFileSize int `json:"max_file_size"`
|
|
|
|
MaxFileNum int `json:"max_file_num"`
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (cfg *BinLogConfig) adjust() {
|
|
|
|
if cfg.MaxFileSize <= 0 {
|
|
|
|
cfg.MaxFileSize = DefaultBinLogFileSize
|
|
|
|
} else if cfg.MaxFileSize > MaxBinLogFileSize {
|
|
|
|
cfg.MaxFileSize = MaxBinLogFileSize
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.MaxFileNum <= 0 {
|
|
|
|
cfg.MaxFileNum = DefaultBinLogFileNum
|
|
|
|
} else if cfg.MaxFileNum > MaxBinLogFileNum {
|
|
|
|
cfg.MaxFileNum = MaxBinLogFileNum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type BinLog struct {
|
|
|
|
cfg *BinLogConfig
|
2014-06-03 10:08:57 +04:00
|
|
|
|
|
|
|
logFile *os.File
|
|
|
|
|
|
|
|
logWb *bufio.Writer
|
|
|
|
|
|
|
|
indexName string
|
|
|
|
logNames []string
|
2014-06-09 13:23:32 +04:00
|
|
|
lastLogIndex int64
|
2014-06-07 12:56:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBinLog(data json.RawMessage) (*BinLog, error) {
|
|
|
|
var cfg BinLogConfig
|
2014-06-03 10:08:57 +04:00
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-03 10:08:57 +04:00
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
return NewBinLogWithConfig(&cfg)
|
2014-06-03 10:08:57 +04:00
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func NewBinLogWithConfig(cfg *BinLogConfig) (*BinLog, error) {
|
|
|
|
cfg.adjust()
|
2014-06-03 10:08:57 +04:00
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
l := new(BinLog)
|
2014-06-03 10:08:57 +04:00
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
l.cfg = cfg
|
2014-06-06 06:34:57 +04:00
|
|
|
|
2014-06-03 10:08:57 +04:00
|
|
|
if err := os.MkdirAll(cfg.Path, os.ModePerm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.logNames = make([]string, 0, 16)
|
|
|
|
|
|
|
|
if err := l.loadIndex(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) flushIndex() error {
|
2014-06-03 10:08:57 +04:00
|
|
|
data := strings.Join(l.logNames, "\n")
|
|
|
|
|
|
|
|
bakName := fmt.Sprintf("%s.bak", l.indexName)
|
|
|
|
f, err := os.OpenFile(bakName, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("create binlog bak index error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := f.WriteString(data); err != nil {
|
|
|
|
log.Error("write binlog index error %s", err.Error())
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
if err := os.Rename(bakName, l.indexName); err != nil {
|
|
|
|
log.Error("rename binlog bak index error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) loadIndex() error {
|
2014-06-09 13:23:32 +04:00
|
|
|
l.indexName = path.Join(l.cfg.Path, fmt.Sprintf("ledis-bin.index"))
|
2014-06-03 10:08:57 +04:00
|
|
|
if _, err := os.Stat(l.indexName); os.IsNotExist(err) {
|
|
|
|
//no index file, nothing to do
|
|
|
|
} else {
|
|
|
|
indexData, err := ioutil.ReadFile(l.indexName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(indexData), "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
line = strings.Trim(line, "\r\n ")
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
if _, err := os.Stat(path.Join(l.cfg.Path, line)); err != nil {
|
2014-06-03 10:08:57 +04:00
|
|
|
log.Error("load index line %s error %s", line, err.Error())
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
l.logNames = append(l.logNames, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l.cfg.MaxFileNum > 0 && len(l.logNames) > l.cfg.MaxFileNum {
|
|
|
|
//remove oldest logfile
|
|
|
|
if err := l.Purge(len(l.logNames) - l.cfg.MaxFileNum); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if len(l.logNames) == 0 {
|
|
|
|
l.lastLogIndex = 1
|
|
|
|
} else {
|
|
|
|
lastName := l.logNames[len(l.logNames)-1]
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
if l.lastLogIndex, err = strconv.ParseInt(path.Ext(lastName)[1:], 10, 64); err != nil {
|
2014-06-03 10:08:57 +04:00
|
|
|
log.Error("invalid logfile name %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//like mysql, if server restart, a new binlog will create
|
|
|
|
l.lastLogIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) getLogFile() string {
|
2014-06-09 13:23:32 +04:00
|
|
|
return l.FormatLogFileName(l.lastLogIndex)
|
2014-06-03 10:08:57 +04:00
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) openNewLogFile() error {
|
2014-06-03 10:08:57 +04:00
|
|
|
var err error
|
|
|
|
lastName := l.getLogFile()
|
|
|
|
|
|
|
|
logPath := path.Join(l.cfg.Path, lastName)
|
|
|
|
if l.logFile, err = os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY, 0666); err != nil {
|
|
|
|
log.Error("open new logfile error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.cfg.MaxFileNum > 0 && len(l.logNames) == l.cfg.MaxFileNum {
|
|
|
|
l.purge(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
l.logNames = append(l.logNames, lastName)
|
|
|
|
|
|
|
|
if l.logWb == nil {
|
|
|
|
l.logWb = bufio.NewWriterSize(l.logFile, 1024)
|
|
|
|
} else {
|
|
|
|
l.logWb.Reset(l.logFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = l.flushIndex(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) checkLogFileSize() bool {
|
2014-06-03 10:08:57 +04:00
|
|
|
if l.logFile == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
st, _ := l.logFile.Stat()
|
|
|
|
if st.Size() >= int64(l.cfg.MaxFileSize) {
|
|
|
|
l.lastLogIndex++
|
|
|
|
|
|
|
|
l.logFile.Close()
|
|
|
|
l.logFile = nil
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) purge(n int) {
|
2014-06-03 10:08:57 +04:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
logPath := path.Join(l.cfg.Path, l.logNames[i])
|
|
|
|
os.Remove(logPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(l.logNames[0:], l.logNames[n:])
|
|
|
|
l.logNames = l.logNames[0 : len(l.logNames)-n]
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) Close() {
|
2014-06-03 10:08:57 +04:00
|
|
|
if l.logFile != nil {
|
|
|
|
l.logFile.Close()
|
|
|
|
l.logFile = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) LogNames() []string {
|
2014-06-06 04:30:17 +04:00
|
|
|
return l.logNames
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) LogFileName() string {
|
2014-06-03 10:08:57 +04:00
|
|
|
return l.getLogFile()
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) LogFilePos() int64 {
|
2014-06-03 10:08:57 +04:00
|
|
|
if l.logFile == nil {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
st, _ := l.logFile.Stat()
|
|
|
|
return st.Size()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
func (l *BinLog) LogFileIndex() int64 {
|
|
|
|
return l.lastLogIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *BinLog) FormatLogFileName(index int64) string {
|
|
|
|
return fmt.Sprintf("ledis-bin.%07d", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *BinLog) FormatLogFilePath(index int64) string {
|
2014-06-10 06:41:50 +04:00
|
|
|
return path.Join(l.cfg.Path, l.FormatLogFileName(index))
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *BinLog) LogPath() string {
|
|
|
|
return l.cfg.Path
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) Purge(n int) error {
|
2014-06-03 10:08:57 +04:00
|
|
|
if len(l.logNames) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if n >= len(l.logNames) {
|
|
|
|
n = len(l.logNames)
|
|
|
|
//can not purge current log file
|
|
|
|
if l.logNames[n-1] == l.getLogFile() {
|
|
|
|
n = n - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.purge(n)
|
|
|
|
|
|
|
|
return l.flushIndex()
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
func (l *BinLog) Log(args ...[]byte) error {
|
2014-06-03 10:08:57 +04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if l.logFile == nil {
|
|
|
|
if err = l.openNewLogFile(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
//we treat log many args as a batch, so use same createTime
|
|
|
|
createTime := uint32(time.Now().Unix())
|
2014-06-03 10:08:57 +04:00
|
|
|
|
|
|
|
for _, data := range args {
|
2014-06-07 12:56:22 +04:00
|
|
|
payLoadLen := uint32(len(data))
|
|
|
|
|
|
|
|
if err := binary.Write(l.logWb, binary.BigEndian, createTime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(l.logWb, binary.BigEndian, payLoadLen); err != nil {
|
2014-06-03 10:08:57 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
if _, err := l.logWb.Write(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-03 10:08:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = l.logWb.Flush(); err != nil {
|
|
|
|
log.Error("write log error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.checkLogFileSize()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|