2015-11-18 06:08:29 +00:00
|
|
|
package tsm1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2015-11-21 22:50:28 +00:00
|
|
|
"math"
|
2015-11-18 06:08:29 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-11-21 22:50:28 +00:00
|
|
|
"time"
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
"github.com/golang/snappy"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultSegmentSize of 2MB is the size at which segment files will be rolled over
|
2015-11-21 22:50:28 +00:00
|
|
|
DefaultSegmentSize = 10 * 1024 * 1024
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
// FileExtension is the file extension we expect for wal segments
|
|
|
|
WALFileExtension = "wal"
|
|
|
|
|
|
|
|
WALFilePrefix = "_"
|
|
|
|
|
2015-11-19 06:15:44 +00:00
|
|
|
defaultBufLen = 1024 << 10 // 1MB (sized for batches of 5000 points)
|
2015-11-21 22:50:28 +00:00
|
|
|
|
|
|
|
float64EntryType = 1
|
|
|
|
int64EntryType = 2
|
|
|
|
boolEntryType = 3
|
|
|
|
stringEntryType = 4
|
2015-11-18 06:08:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// walEntry is a byte written to a wal segment file that indicates what the following compressed block contains
|
|
|
|
type walEntryType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
WriteWALEntryType walEntryType = 0x01
|
|
|
|
DeleteWALEntryType walEntryType = 0x02
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrWALClosed = fmt.Errorf("WAL closed")
|
|
|
|
|
2015-11-21 22:50:28 +00:00
|
|
|
var (
|
|
|
|
bufPool sync.Pool
|
|
|
|
float64ValuePool sync.Pool
|
|
|
|
int64ValuePool sync.Pool
|
|
|
|
boolValuePool sync.Pool
|
|
|
|
stringValuePool sync.Pool
|
|
|
|
)
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
type WAL struct {
|
2015-11-18 22:42:48 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
|
2015-11-18 06:08:29 +00:00
|
|
|
path string
|
|
|
|
|
|
|
|
// write variables
|
2015-11-18 22:42:48 +00:00
|
|
|
currentSegmentID int
|
|
|
|
currentSegmentWriter *WALSegmentWriter
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
// cache and flush variables
|
|
|
|
closing chan struct{}
|
|
|
|
|
|
|
|
// WALOutput is the writer used by the logger.
|
|
|
|
LogOutput io.Writer
|
|
|
|
logger *log.Logger
|
|
|
|
|
|
|
|
// SegmentSize is the file size at which a segment file will be rotated
|
|
|
|
SegmentSize int
|
|
|
|
|
|
|
|
// LoggingEnabled specifies if detailed logs should be output
|
|
|
|
LoggingEnabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWAL(path string) *WAL {
|
|
|
|
return &WAL{
|
|
|
|
path: path,
|
|
|
|
|
|
|
|
// these options should be overriden by any options in the config
|
2015-11-18 22:42:48 +00:00
|
|
|
LogOutput: os.Stderr,
|
|
|
|
SegmentSize: DefaultSegmentSize,
|
|
|
|
logger: log.New(os.Stderr, "[tsm1wal] ", log.LstdFlags),
|
|
|
|
closing: make(chan struct{}),
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns the path the log was initialized with.
|
2015-11-18 22:42:48 +00:00
|
|
|
func (l *WAL) Path() string {
|
|
|
|
l.mu.RLock()
|
|
|
|
defer l.mu.RUnlock()
|
|
|
|
return l.path
|
|
|
|
}
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
// Open opens and initializes the Log. Will recover from previous unclosed shutdowns
|
|
|
|
func (l *WAL) Open() error {
|
2015-11-18 22:42:48 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
|
2015-11-18 06:08:29 +00:00
|
|
|
if l.LoggingEnabled {
|
2015-11-18 22:42:48 +00:00
|
|
|
l.logger.Printf("tsm1 WAL starting with %d segment size\n", l.SegmentSize)
|
2015-11-18 06:08:29 +00:00
|
|
|
l.logger.Printf("tsm1 WAL writing to %s\n", l.path)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(l.path, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.closing = make(chan struct{})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-21 22:50:28 +00:00
|
|
|
func (l *WAL) WritePoints(values map[string][]Value) error {
|
2015-11-18 06:08:29 +00:00
|
|
|
entry := &WriteWALEntry{
|
2015-11-21 22:50:28 +00:00
|
|
|
Values: values,
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
2015-11-21 22:50:28 +00:00
|
|
|
// This sleep is intentional to allow the go scheduler to switch this goroutine
|
|
|
|
// out of execution to allow other goroutines to run. The goroutine we want to
|
|
|
|
// give a chance to run is the compaction goroutine. Under very high write load,
|
|
|
|
// the compaction goroutine can be starved CPU cycles which causes the number of
|
|
|
|
// WAL segments to grow faster than they can be compacted. 100ms seems to sufficient
|
|
|
|
// enough time to allow the scheduling and not adversely affect write latency.
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
2015-11-18 06:08:29 +00:00
|
|
|
if err := l.writeToLog(entry); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
func (l *WAL) ClosedSegments() ([]string, error) {
|
|
|
|
l.mu.RLock()
|
2015-11-21 22:50:28 +00:00
|
|
|
var activePath string
|
|
|
|
if l.currentSegmentWriter != nil {
|
|
|
|
activePath = l.currentSegmentWriter.Path()
|
|
|
|
}
|
2015-11-18 22:42:48 +00:00
|
|
|
|
|
|
|
// Not loading files from disk so nothing to do
|
|
|
|
if l.path == "" {
|
2015-11-21 22:50:28 +00:00
|
|
|
l.mu.RUnlock()
|
2015-11-18 22:42:48 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
l.mu.RUnlock()
|
2015-11-18 06:08:29 +00:00
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
files, err := l.segmentFileNames()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var names []string
|
|
|
|
for _, fn := range files {
|
|
|
|
// Skip the active segment
|
2015-11-21 22:50:28 +00:00
|
|
|
if fn == activePath {
|
2015-11-18 22:42:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
names = append(names, fn)
|
|
|
|
}
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *WAL) writeToLog(entry WALEntry) error {
|
2015-11-19 06:15:44 +00:00
|
|
|
l.mu.RLock()
|
2015-11-18 06:08:29 +00:00
|
|
|
// Make sure the log has not been closed
|
|
|
|
select {
|
|
|
|
case <-l.closing:
|
2015-11-19 17:54:58 +00:00
|
|
|
l.mu.RUnlock()
|
2015-11-18 06:08:29 +00:00
|
|
|
return ErrWALClosed
|
|
|
|
default:
|
|
|
|
}
|
2015-11-19 17:54:58 +00:00
|
|
|
l.mu.RUnlock()
|
2015-11-18 06:08:29 +00:00
|
|
|
|
2015-11-19 17:54:58 +00:00
|
|
|
if err := l.rollSegment(); err != nil {
|
|
|
|
return fmt.Errorf("error rolling WAL segment: %v", err)
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 17:54:58 +00:00
|
|
|
l.mu.RLock()
|
|
|
|
defer l.mu.RUnlock()
|
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
if err := l.currentSegmentWriter.Write(entry); err != nil {
|
|
|
|
return fmt.Errorf("error writing WAL entry: %v", err)
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
return l.currentSegmentWriter.Sync()
|
|
|
|
}
|
2015-11-18 06:08:29 +00:00
|
|
|
|
2015-11-19 17:54:58 +00:00
|
|
|
func (l *WAL) rollSegment() error {
|
2015-11-21 22:50:28 +00:00
|
|
|
l.mu.RLock()
|
2015-11-19 17:54:58 +00:00
|
|
|
|
|
|
|
if l.currentSegmentWriter == nil || l.currentSegmentWriter.Size() > DefaultSegmentSize {
|
2015-11-21 22:50:28 +00:00
|
|
|
l.mu.RUnlock()
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2015-11-19 17:54:58 +00:00
|
|
|
if err := l.newSegmentFile(); err != nil {
|
|
|
|
// A drop database or RP call could trigger this error if writes were in-flight
|
|
|
|
// when the drop statement executes.
|
|
|
|
return fmt.Errorf("error opening new segment file for wal: %v", err)
|
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
return nil
|
2015-11-19 17:54:58 +00:00
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
l.mu.RUnlock()
|
2015-11-19 17:54:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
func (l *WAL) Delete(keys []string) error {
|
|
|
|
entry := &DeleteWALEntry{
|
|
|
|
Keys: keys,
|
|
|
|
}
|
2015-11-18 06:08:29 +00:00
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
if err := l.writeToLog(entry); err != nil {
|
2015-11-18 06:08:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close will finish any flush that is currently in process and close file handles
|
|
|
|
func (l *WAL) Close() error {
|
2015-11-18 22:42:48 +00:00
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
// Close, but don't set to nil so future goroutines can still be signaled
|
|
|
|
close(l.closing)
|
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
if l.currentSegmentWriter != nil {
|
|
|
|
l.currentSegmentWriter.Close()
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// segmentFileNames will return all files that are WAL segment files in sorted order by ascending ID
|
|
|
|
func (l *WAL) segmentFileNames() ([]string, error) {
|
|
|
|
names, err := filepath.Glob(filepath.Join(l.path, fmt.Sprintf("%s*.%s", WALFilePrefix, WALFileExtension)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSegmentFile will close the current segment file and open a new one, updating bookkeeping info on the log
|
|
|
|
func (l *WAL) newSegmentFile() error {
|
|
|
|
l.currentSegmentID++
|
2015-11-18 22:42:48 +00:00
|
|
|
if l.currentSegmentWriter != nil {
|
|
|
|
if err := l.currentSegmentWriter.Close(); err != nil {
|
2015-11-18 06:08:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := filepath.Join(l.path, fmt.Sprintf("%s%05d.%s", WALFilePrefix, l.currentSegmentID, WALFileExtension))
|
2015-11-23 21:01:09 +00:00
|
|
|
fd, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
|
2015-11-18 06:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-23 21:01:09 +00:00
|
|
|
l.currentSegmentWriter = NewWALSegmentWriter(fd)
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WALEntry is record stored in each WAL segment. Each entry has a type
|
|
|
|
// and an opaque, type dependent byte slice data attribute.
|
|
|
|
type WALEntry interface {
|
|
|
|
Type() walEntryType
|
|
|
|
Encode(dst []byte) ([]byte, error)
|
|
|
|
MarshalBinary() ([]byte, error)
|
2015-11-19 06:15:44 +00:00
|
|
|
UnmarshalBinary(b []byte) error
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteWALEntry represents a write of points.
|
|
|
|
type WriteWALEntry struct {
|
2015-11-21 22:50:28 +00:00
|
|
|
Values map[string][]Value
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteWALEntry) Encode(dst []byte) ([]byte, error) {
|
|
|
|
var n int
|
2015-11-21 22:50:28 +00:00
|
|
|
|
|
|
|
for k, v := range w.Values {
|
|
|
|
|
|
|
|
switch v[0].Value().(type) {
|
|
|
|
case float64:
|
|
|
|
dst[n] = float64EntryType
|
|
|
|
case int64:
|
|
|
|
dst[n] = int64EntryType
|
|
|
|
case bool:
|
|
|
|
dst[n] = boolEntryType
|
|
|
|
case string:
|
|
|
|
dst[n] = stringEntryType
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported value type: %#v", v[0].Value())
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
n++
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
// Make sure we have enough space in our buf before copying. If not,
|
|
|
|
// grow the buf.
|
2015-11-21 22:50:28 +00:00
|
|
|
if len(k)+2+len(v)*8+4 > len(dst)-n {
|
|
|
|
grow := make([]byte, len(dst)*2)
|
2015-11-18 06:08:29 +00:00
|
|
|
dst = append(dst, grow...)
|
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
|
|
|
|
n += copy(dst[n:], u16tob(uint16(len(k))))
|
|
|
|
n += copy(dst[n:], []byte(k))
|
|
|
|
|
|
|
|
n += copy(dst[n:], u32tob(uint32(len(v))))
|
|
|
|
|
|
|
|
for _, vv := range v {
|
|
|
|
n += copy(dst[n:], u64tob(uint64(vv.Time().UnixNano())))
|
|
|
|
switch t := vv.Value().(type) {
|
|
|
|
case float64:
|
|
|
|
n += copy(dst[n:], u64tob(uint64(math.Float64bits(t))))
|
|
|
|
case int64:
|
|
|
|
n += copy(dst[n:], u64tob(uint64(t)))
|
|
|
|
case bool:
|
|
|
|
if t {
|
|
|
|
n += copy(dst[n:], []byte{1})
|
|
|
|
} else {
|
|
|
|
n += copy(dst[n:], []byte{0})
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
n += copy(dst[n:], u32tob(uint32(len(t))))
|
|
|
|
n += copy(dst[n:], []byte(t))
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dst[:n], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteWALEntry) MarshalBinary() ([]byte, error) {
|
|
|
|
// Temp buffer to write marshaled points into
|
2015-11-19 06:15:44 +00:00
|
|
|
b := make([]byte, defaultBufLen)
|
2015-11-18 06:08:29 +00:00
|
|
|
return w.Encode(b)
|
|
|
|
}
|
|
|
|
|
2015-11-19 06:15:44 +00:00
|
|
|
func (w *WriteWALEntry) UnmarshalBinary(b []byte) error {
|
2015-11-18 06:08:29 +00:00
|
|
|
var i int
|
|
|
|
for i < len(b) {
|
2015-11-21 22:50:28 +00:00
|
|
|
typ := b[i]
|
|
|
|
i++
|
|
|
|
|
|
|
|
length := int(btou16(b[i : i+2]))
|
|
|
|
i += 2
|
|
|
|
k := string(b[i : i+length])
|
|
|
|
i += length
|
|
|
|
|
|
|
|
nvals := int(btou32(b[i : i+4]))
|
2015-11-18 06:08:29 +00:00
|
|
|
i += 4
|
|
|
|
|
2015-11-21 22:50:28 +00:00
|
|
|
var values []Value
|
|
|
|
switch typ {
|
|
|
|
case float64EntryType:
|
|
|
|
values = getFloat64Values(nvals)
|
|
|
|
case int64EntryType:
|
|
|
|
values = getInt64Values(nvals)
|
|
|
|
case boolEntryType:
|
|
|
|
values = getBoolValues(nvals)
|
|
|
|
case stringEntryType:
|
|
|
|
values = getStringValues(nvals)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported value type: %#v", typ)
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
|
|
|
|
for j := 0; j < nvals; j++ {
|
|
|
|
t := time.Unix(0, int64(btou64(b[i:i+8])))
|
|
|
|
i += 8
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case float64EntryType:
|
|
|
|
v := math.Float64frombits((btou64(b[i : i+8])))
|
|
|
|
i += 8
|
|
|
|
if fv, ok := values[j].(*FloatValue); ok {
|
|
|
|
fv.time = t
|
|
|
|
fv.value = v
|
|
|
|
}
|
|
|
|
case int64EntryType:
|
|
|
|
v := int64(btou64(b[i : i+8]))
|
|
|
|
i += 8
|
|
|
|
if fv, ok := values[j].(*Int64Value); ok {
|
|
|
|
fv.time = t
|
|
|
|
fv.value = v
|
|
|
|
}
|
|
|
|
case boolEntryType:
|
|
|
|
v := b[i]
|
|
|
|
i += 1
|
|
|
|
if fv, ok := values[j].(*BoolValue); ok {
|
|
|
|
fv.time = t
|
|
|
|
if v == 1 {
|
|
|
|
fv.value = true
|
|
|
|
} else {
|
|
|
|
fv.value = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case stringEntryType:
|
|
|
|
length := int(btou32(b[i : i+4]))
|
|
|
|
i += 4
|
|
|
|
v := string(b[i : i+length])
|
|
|
|
i += length
|
|
|
|
if fv, ok := values[j].(*StringValue); ok {
|
|
|
|
fv.time = t
|
|
|
|
fv.value = v
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported value type: %#v", typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Values[k] = values
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteWALEntry) Type() walEntryType {
|
|
|
|
return WriteWALEntryType
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteWALEntry represents the deletion of multiple series.
|
|
|
|
type DeleteWALEntry struct {
|
|
|
|
Keys []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *DeleteWALEntry) MarshalBinary() ([]byte, error) {
|
2015-11-19 06:15:44 +00:00
|
|
|
b := make([]byte, defaultBufLen)
|
2015-11-18 06:08:29 +00:00
|
|
|
return w.Encode(b)
|
|
|
|
}
|
|
|
|
|
2015-11-19 06:15:44 +00:00
|
|
|
func (w *DeleteWALEntry) UnmarshalBinary(b []byte) error {
|
2015-11-18 06:08:29 +00:00
|
|
|
w.Keys = strings.Split(string(b), "\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *DeleteWALEntry) Encode(dst []byte) ([]byte, error) {
|
|
|
|
var n int
|
|
|
|
for _, k := range w.Keys {
|
|
|
|
if len(dst)+1 > len(dst)-n {
|
2015-11-19 06:15:44 +00:00
|
|
|
grow := make([]byte, defaultBufLen)
|
2015-11-18 06:08:29 +00:00
|
|
|
dst = append(dst, grow...)
|
|
|
|
}
|
|
|
|
|
|
|
|
n += copy(dst[n:], k)
|
|
|
|
n += copy(dst[n:], "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We return n-1 to strip off the last newline so that unmarshalling the value
|
|
|
|
// does not produce an empty string
|
|
|
|
return []byte(dst[:n-1]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *DeleteWALEntry) Type() walEntryType {
|
|
|
|
return DeleteWALEntryType
|
|
|
|
}
|
|
|
|
|
|
|
|
// WALSegmentWriter writes WAL segments.
|
|
|
|
type WALSegmentWriter struct {
|
2015-11-21 22:50:28 +00:00
|
|
|
mu sync.RWMutex
|
2015-11-18 06:08:29 +00:00
|
|
|
w io.WriteCloser
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWALSegmentWriter(w io.WriteCloser) *WALSegmentWriter {
|
|
|
|
return &WALSegmentWriter{
|
|
|
|
w: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
func (w *WALSegmentWriter) Path() string {
|
2015-11-19 17:54:58 +00:00
|
|
|
w.mu.RLock()
|
|
|
|
defer w.mu.RUnlock()
|
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
if f, ok := w.w.(*os.File); ok {
|
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-11-18 06:08:29 +00:00
|
|
|
func (w *WALSegmentWriter) Write(e WALEntry) error {
|
2015-11-23 21:01:09 +00:00
|
|
|
bytes := make([]byte, defaultBufLen)
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
b, err := e.Encode(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-19 06:15:44 +00:00
|
|
|
compressed := snappy.Encode(b, b)
|
2015-11-18 06:08:29 +00:00
|
|
|
|
2015-11-23 21:01:09 +00:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
2015-11-19 06:15:44 +00:00
|
|
|
if _, err := w.w.Write([]byte{byte(e.Type())}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
|
|
|
|
if _, err = w.w.Write(u32tob(uint32(len(compressed)))); err != nil {
|
2015-11-19 06:15:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
|
|
|
|
if _, err = w.w.Write(compressed); err != nil {
|
2015-11-19 06:15:44 +00:00
|
|
|
return err
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 21:01:09 +00:00
|
|
|
// 5 is the 1 byte type + 4 byte uint32 length
|
2015-11-19 06:15:44 +00:00
|
|
|
w.size += len(compressed) + 5
|
2015-11-18 06:08:29 +00:00
|
|
|
|
2015-11-18 22:42:48 +00:00
|
|
|
return nil
|
2015-11-18 06:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync flushes the file systems in-memory copy of recently written data to disk.
|
|
|
|
func (w *WALSegmentWriter) Sync() error {
|
2015-11-19 17:54:58 +00:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
2015-11-18 06:08:29 +00:00
|
|
|
if f, ok := w.w.(*os.File); ok {
|
|
|
|
return f.Sync()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WALSegmentWriter) Size() int {
|
2015-11-19 17:54:58 +00:00
|
|
|
w.mu.RLock()
|
|
|
|
defer w.mu.RUnlock()
|
|
|
|
|
2015-11-18 06:08:29 +00:00
|
|
|
return w.size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WALSegmentWriter) Close() error {
|
2015-11-19 17:54:58 +00:00
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
|
2015-11-18 06:08:29 +00:00
|
|
|
return w.w.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WALSegmentReader reads WAL segments.
|
|
|
|
type WALSegmentReader struct {
|
|
|
|
r io.ReadCloser
|
|
|
|
entry WALEntry
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWALSegmentReader(r io.ReadCloser) *WALSegmentReader {
|
|
|
|
return &WALSegmentReader{
|
|
|
|
r: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next indicates if there is a value to read
|
|
|
|
func (r *WALSegmentReader) Next() bool {
|
2015-11-19 06:15:44 +00:00
|
|
|
b := getBuf(defaultBufLen)
|
2015-11-18 06:08:29 +00:00
|
|
|
defer putBuf(b)
|
|
|
|
|
|
|
|
// read the type and the length of the entry
|
|
|
|
_, err := io.ReadFull(r.r, b[:5])
|
|
|
|
if err == io.EOF {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
// We return true here because we want the client code to call read which
|
|
|
|
// will return the this error to be handled.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
entryType := b[0]
|
|
|
|
length := btou32(b[1:5])
|
|
|
|
|
|
|
|
// read the compressed block and decompress it
|
|
|
|
if int(length) > len(b) {
|
|
|
|
b = make([]byte, length)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.ReadFull(r.r, b[:length])
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
r.err = err
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-11-21 22:50:28 +00:00
|
|
|
data, err := snappy.Decode(nil, b[:length])
|
2015-11-18 06:08:29 +00:00
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// and marshal it and send it to the cache
|
|
|
|
switch walEntryType(entryType) {
|
|
|
|
case WriteWALEntryType:
|
2015-11-21 22:50:28 +00:00
|
|
|
r.entry = &WriteWALEntry{
|
|
|
|
Values: map[string][]Value{},
|
|
|
|
}
|
2015-11-18 06:08:29 +00:00
|
|
|
case DeleteWALEntryType:
|
|
|
|
r.entry = &DeleteWALEntry{}
|
|
|
|
default:
|
|
|
|
r.err = fmt.Errorf("unknown wal entry type: %v", entryType)
|
|
|
|
return true
|
|
|
|
}
|
2015-11-19 06:15:44 +00:00
|
|
|
r.err = r.entry.UnmarshalBinary(data)
|
2015-11-18 06:08:29 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WALSegmentReader) Read() (WALEntry, error) {
|
|
|
|
if r.err != nil {
|
|
|
|
return nil, r.err
|
|
|
|
}
|
|
|
|
return r.entry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WALSegmentReader) Error() error {
|
|
|
|
return r.err
|
|
|
|
}
|
|
|
|
|
2015-11-21 22:50:28 +00:00
|
|
|
func (r *WALSegmentReader) Close() error {
|
|
|
|
return r.r.Close()
|
|
|
|
}
|
|
|
|
|
2015-11-18 06:08:29 +00:00
|
|
|
// idFromFileName parses the segment file ID from its name
|
|
|
|
func idFromFileName(name string) (int, error) {
|
|
|
|
parts := strings.Split(filepath.Base(name), ".")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return 0, fmt.Errorf("file %s has wrong name format to have an id", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.ParseUint(parts[0][1:], 10, 32)
|
|
|
|
|
|
|
|
return int(id), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBuf returns a buffer with length size from the buffer pool.
|
|
|
|
func getBuf(size int) []byte {
|
|
|
|
x := bufPool.Get()
|
|
|
|
if x == nil {
|
|
|
|
return make([]byte, size)
|
|
|
|
}
|
|
|
|
buf := x.([]byte)
|
|
|
|
if cap(buf) < size {
|
|
|
|
return make([]byte, size)
|
|
|
|
}
|
|
|
|
return buf[:size]
|
|
|
|
}
|
|
|
|
|
|
|
|
// putBuf returns a buffer to the pool.
|
|
|
|
func putBuf(buf []byte) {
|
|
|
|
bufPool.Put(buf)
|
|
|
|
}
|
2015-11-21 22:50:28 +00:00
|
|
|
|
|
|
|
// getBuf returns a buffer with length size from the buffer pool.
|
|
|
|
func getFloat64Values(size int) []Value {
|
|
|
|
var buf []Value
|
|
|
|
x := float64ValuePool.Get()
|
|
|
|
if x == nil {
|
|
|
|
buf = make([]Value, size)
|
|
|
|
} else {
|
|
|
|
buf = x.([]Value)
|
|
|
|
}
|
|
|
|
if cap(buf) < size {
|
|
|
|
return make([]Value, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range buf {
|
|
|
|
if v == nil {
|
|
|
|
buf[i] = &FloatValue{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf[:size]
|
|
|
|
}
|
|
|
|
|
|
|
|
// putBuf returns a buffer to the pool.
|
|
|
|
func putFloat64Values(buf []Value) {
|
|
|
|
float64ValuePool.Put(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBuf returns a buffer with length size from the buffer pool.
|
|
|
|
func getInt64Values(size int) []Value {
|
|
|
|
var buf []Value
|
|
|
|
x := int64ValuePool.Get()
|
|
|
|
if x == nil {
|
|
|
|
buf = make([]Value, size)
|
|
|
|
} else {
|
|
|
|
buf = x.([]Value)
|
|
|
|
}
|
|
|
|
if cap(buf) < size {
|
|
|
|
return make([]Value, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range buf {
|
|
|
|
if v == nil {
|
|
|
|
buf[i] = &Int64Value{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf[:size]
|
|
|
|
}
|
|
|
|
|
|
|
|
// putBuf returns a buffer to the pool.
|
|
|
|
func putInt64Values(buf []Value) {
|
|
|
|
int64ValuePool.Put(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBuf returns a buffer with length size from the buffer pool.
|
|
|
|
func getBoolValues(size int) []Value {
|
|
|
|
var buf []Value
|
|
|
|
x := boolValuePool.Get()
|
|
|
|
if x == nil {
|
|
|
|
buf = make([]Value, size)
|
|
|
|
} else {
|
|
|
|
buf = x.([]Value)
|
|
|
|
}
|
|
|
|
if cap(buf) < size {
|
|
|
|
return make([]Value, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range buf {
|
|
|
|
if v == nil {
|
|
|
|
buf[i] = &BoolValue{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf[:size]
|
|
|
|
}
|
|
|
|
|
|
|
|
// putBuf returns a buffer to the pool.
|
|
|
|
func putStringValues(buf []Value) {
|
|
|
|
stringValuePool.Put(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBuf returns a buffer with length size from the buffer pool.
|
|
|
|
func getStringValues(size int) []Value {
|
|
|
|
var buf []Value
|
|
|
|
x := stringValuePool.Get()
|
|
|
|
if x == nil {
|
|
|
|
buf = make([]Value, size)
|
|
|
|
} else {
|
|
|
|
buf = x.([]Value)
|
|
|
|
}
|
|
|
|
if cap(buf) < size {
|
|
|
|
return make([]Value, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range buf {
|
|
|
|
if v == nil {
|
|
|
|
buf[i] = &StringValue{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf[:size]
|
|
|
|
}
|
|
|
|
|
|
|
|
// putBuf returns a buffer to the pool.
|
|
|
|
func putBoolValues(buf []Value) {
|
|
|
|
boolValuePool.Put(buf)
|
|
|
|
}
|
|
|
|
func putValue(buf []Value) {
|
|
|
|
if len(buf) > 0 {
|
|
|
|
switch buf[0].(type) {
|
|
|
|
case *FloatValue:
|
|
|
|
putFloat64Values(buf)
|
|
|
|
case *Int64Value:
|
|
|
|
putInt64Values(buf)
|
|
|
|
case *BoolValue:
|
|
|
|
putBoolValues(buf)
|
|
|
|
case *StringValue:
|
|
|
|
putBoolValues(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|