2016-03-05 02:08:16 +03:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2016-11-09 23:43:26 +03:00
|
|
|
"bufio"
|
2016-03-05 02:08:16 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-09-11 17:49:48 +03:00
|
|
|
"github.com/tidwall/buntdb"
|
2016-03-28 18:57:41 +03:00
|
|
|
"github.com/tidwall/resp"
|
2016-03-06 17:55:00 +03:00
|
|
|
"github.com/tidwall/tile38/controller/log"
|
2016-03-28 18:57:41 +03:00
|
|
|
"github.com/tidwall/tile38/controller/server"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
2016-04-03 05:16:36 +03:00
|
|
|
// AsyncHooks indicates that the hooks should happen in the background.
|
2016-04-02 17:20:30 +03:00
|
|
|
|
2016-03-19 17:16:19 +03:00
|
|
|
type errAOFHook struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err errAOFHook) Error() string {
|
|
|
|
return fmt.Sprintf("hook: %v", err.err)
|
|
|
|
}
|
|
|
|
|
2016-11-09 23:43:26 +03:00
|
|
|
var errInvalidAOF = errors.New("invalid aof file")
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
func (c *Controller) loadAOF() error {
|
2016-04-01 02:26:36 +03:00
|
|
|
fi, err := c.f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
start := time.Now()
|
|
|
|
var count int
|
|
|
|
defer func() {
|
|
|
|
d := time.Now().Sub(start)
|
|
|
|
ps := float64(count) / (float64(d) / float64(time.Second))
|
2016-04-01 02:26:36 +03:00
|
|
|
suf := []string{"bytes/s", "KB/s", "MB/s", "GB/s", "TB/s"}
|
|
|
|
bps := float64(fi.Size()) / (float64(d) / float64(time.Second))
|
|
|
|
for i := 0; bps > 1024; i++ {
|
|
|
|
if len(suf) == 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
bps /= 1024
|
|
|
|
suf = suf[1:]
|
|
|
|
}
|
|
|
|
byteSpeed := fmt.Sprintf("%.0f %s", bps, suf[0])
|
|
|
|
log.Infof("AOF loaded %d commands: %.2fs, %.0f/s, %s",
|
|
|
|
count, float64(d)/float64(time.Second), ps, byteSpeed)
|
2016-03-05 02:08:16 +03:00
|
|
|
}()
|
2016-11-09 23:43:26 +03:00
|
|
|
var msg server.Message
|
|
|
|
rd := bufio.NewReader(c.f)
|
2016-03-05 02:08:16 +03:00
|
|
|
for {
|
2016-11-09 23:43:26 +03:00
|
|
|
var nn int
|
|
|
|
ch, err := rd.ReadByte()
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-11-09 23:43:26 +03:00
|
|
|
nn += 1
|
|
|
|
if ch != '*' {
|
|
|
|
return errInvalidAOF
|
|
|
|
}
|
|
|
|
ns, err := rd.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nn += len(ns)
|
|
|
|
if len(ns) < 2 || ns[len(ns)-2] != '\r' {
|
|
|
|
return errInvalidAOF
|
|
|
|
}
|
|
|
|
n, err := strconv.ParseUint(ns[:len(ns)-2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-03-19 17:16:19 +03:00
|
|
|
}
|
2016-11-09 23:43:26 +03:00
|
|
|
if int(n) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg.Values = msg.Values[:0]
|
|
|
|
for i := 0; i < int(n); i++ {
|
|
|
|
ch, err := rd.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ch != '$' {
|
|
|
|
return errInvalidAOF
|
|
|
|
}
|
|
|
|
ns, err := rd.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(ns) < 2 || ns[len(ns)-2] != '\r' {
|
|
|
|
return errInvalidAOF
|
|
|
|
}
|
|
|
|
n, err := strconv.ParseUint(ns[:len(ns)-2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b := make([]byte, int(n))
|
|
|
|
_, err = io.ReadFull(rd, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ch, err := rd.ReadByte(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if ch != '\r' {
|
|
|
|
return errInvalidAOF
|
|
|
|
}
|
|
|
|
if ch, err := rd.ReadByte(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if ch != '\n' {
|
|
|
|
return errInvalidAOF
|
|
|
|
}
|
|
|
|
msg.Values = append(msg.Values, resp.BytesValue(b))
|
|
|
|
if i == 0 {
|
|
|
|
msg.Command = qlower(b)
|
|
|
|
}
|
|
|
|
nn += 1 + len(ns) + int(n) + 2
|
2016-03-19 17:16:19 +03:00
|
|
|
}
|
2016-11-09 23:43:26 +03:00
|
|
|
if _, _, err := c.command(&msg, nil); err != nil {
|
2016-03-30 19:32:38 +03:00
|
|
|
if commandErrIsFatal(err) {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-11-09 23:43:26 +03:00
|
|
|
c.aofsz += nn
|
2016-03-05 02:08:16 +03:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 23:43:26 +03:00
|
|
|
func qlower(s []byte) string {
|
|
|
|
if len(s) == 3 {
|
|
|
|
if s[0] == 'S' && s[1] == 'E' && s[2] == 'T' {
|
|
|
|
return "set"
|
|
|
|
}
|
|
|
|
if s[0] == 'D' && s[1] == 'E' && s[2] == 'L' {
|
|
|
|
return "del"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] >= 'A' || s[i] <= 'Z' {
|
|
|
|
return strings.ToLower(string(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(s)
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
|
2016-03-30 19:32:38 +03:00
|
|
|
func commandErrIsFatal(err error) bool {
|
|
|
|
// FSET (and other writable commands) may return errors that we need
|
|
|
|
// to ignore during the loading process. These errors may occur (though unlikely)
|
|
|
|
// due to the aof rewrite operation.
|
|
|
|
switch err {
|
|
|
|
case errKeyNotFound, errIDNotFound:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-03-28 18:57:41 +03:00
|
|
|
func (c *Controller) writeAOF(value resp.Value, d *commandDetailsT) error {
|
2016-03-19 17:16:19 +03:00
|
|
|
if d != nil {
|
2016-03-28 18:57:41 +03:00
|
|
|
if !d.updated {
|
|
|
|
return nil // just ignore writes if the command did not update
|
|
|
|
}
|
2016-04-01 03:58:02 +03:00
|
|
|
if c.config.FollowHost == "" {
|
|
|
|
// process hooks, for leader only
|
2016-09-11 17:49:48 +03:00
|
|
|
if err := c.queueHooks(d); err != nil {
|
2016-04-03 00:13:20 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-03-19 17:16:19 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 02:24:26 +03:00
|
|
|
if c.shrinking {
|
|
|
|
var values []string
|
|
|
|
for _, value := range value.Array() {
|
|
|
|
values = append(values, value.String())
|
|
|
|
}
|
|
|
|
c.shrinklog = append(c.shrinklog, values)
|
|
|
|
}
|
2016-03-28 18:57:41 +03:00
|
|
|
data, err := value.MarshalRESP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err := c.f.Write(data)
|
2016-03-20 04:31:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
c.aofsz += n
|
|
|
|
|
|
|
|
// notify aof live connections that we have new data
|
|
|
|
c.fcond.L.Lock()
|
|
|
|
c.fcond.Broadcast()
|
|
|
|
c.fcond.L.Unlock()
|
|
|
|
|
|
|
|
if d != nil {
|
2016-03-19 17:16:19 +03:00
|
|
|
// write to live connection streams
|
2016-03-05 02:08:16 +03:00
|
|
|
c.lcond.L.Lock()
|
|
|
|
c.lstack = append(c.lstack, d)
|
|
|
|
c.lcond.Broadcast()
|
|
|
|
c.lcond.L.Unlock()
|
|
|
|
}
|
2016-04-02 17:20:30 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-03-19 17:16:19 +03:00
|
|
|
|
2016-09-11 17:49:48 +03:00
|
|
|
func (c *Controller) queueHooks(d *commandDetailsT) error {
|
|
|
|
// big list of all of the messages
|
|
|
|
var hmsgs []string
|
|
|
|
var hooks []*Hook
|
|
|
|
// find the hook by the key
|
2016-04-02 17:20:30 +03:00
|
|
|
if hm, ok := c.hookcols[d.key]; ok {
|
|
|
|
for _, hook := range hm {
|
2016-09-11 17:49:48 +03:00
|
|
|
// match the fence
|
|
|
|
msgs := FenceMatch(hook.Name, hook.ScanWriter, hook.Fence, d)
|
|
|
|
if len(msgs) > 0 {
|
|
|
|
// append each msg to the big list
|
|
|
|
hmsgs = append(hmsgs, msgs...)
|
|
|
|
hooks = append(hooks, hook)
|
|
|
|
}
|
2016-04-02 17:20:30 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-11 17:49:48 +03:00
|
|
|
if len(hmsgs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// queue the message in the buntdb database
|
|
|
|
err := c.qdb.Update(func(tx *buntdb.Tx) error {
|
|
|
|
for _, msg := range hmsgs {
|
|
|
|
c.qidx++ // increment the log id
|
|
|
|
key := hookLogPrefix + uint64ToString(c.qidx)
|
|
|
|
_, _, err := tx.Set(key, msg, hookLogSetDefaults())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugf("queued hook: %d", c.qidx)
|
|
|
|
}
|
|
|
|
_, _, err := tx.Set("hook:idx", uint64ToString(c.qidx), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// all the messages have been queued.
|
|
|
|
// notify the hooks
|
|
|
|
for _, hook := range hooks {
|
|
|
|
hook.Signal()
|
|
|
|
}
|
2016-03-05 02:08:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-11 17:49:48 +03:00
|
|
|
// Converts string to an integer
|
|
|
|
func stringToUint64(s string) uint64 {
|
|
|
|
n, _ := strconv.ParseUint(s, 10, 64)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts a uint to a string
|
|
|
|
func uint64ToString(u uint64) string {
|
|
|
|
s := strings.Repeat("0", 20) + strconv.FormatUint(u, 10)
|
|
|
|
return s[len(s)-20:]
|
|
|
|
}
|
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
type liveAOFSwitches struct {
|
|
|
|
pos int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s liveAOFSwitches) Error() string {
|
|
|
|
return "going live"
|
|
|
|
}
|
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
func (c *Controller) cmdAOFMD5(msg *server.Message) (res string, err error) {
|
2016-03-05 02:08:16 +03:00
|
|
|
start := time.Now()
|
2016-04-01 02:26:36 +03:00
|
|
|
vs := msg.Values[1:]
|
|
|
|
var ok bool
|
2016-03-05 02:08:16 +03:00
|
|
|
var spos, ssize string
|
2016-04-01 02:26:36 +03:00
|
|
|
if vs, spos, ok = tokenval(vs); !ok || spos == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
return "", errInvalidNumberOfArguments
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if vs, ssize, ok = tokenval(vs); !ok || ssize == "" {
|
2016-03-05 02:08:16 +03:00
|
|
|
return "", errInvalidNumberOfArguments
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if len(vs) != 0 {
|
2016-03-05 02:08:16 +03:00
|
|
|
return "", errInvalidNumberOfArguments
|
|
|
|
}
|
|
|
|
pos, err := strconv.ParseInt(spos, 10, 64)
|
|
|
|
if err != nil || pos < 0 {
|
|
|
|
return "", errInvalidArgument(spos)
|
|
|
|
}
|
|
|
|
size, err := strconv.ParseInt(ssize, 10, 64)
|
|
|
|
if err != nil || size < 0 {
|
|
|
|
return "", errInvalidArgument(ssize)
|
|
|
|
}
|
|
|
|
sum, err := c.checksum(pos, size)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
switch msg.OutputType {
|
|
|
|
case server.JSON:
|
|
|
|
res = fmt.Sprintf(`{"ok":true,"md5":"%s","elapsed":"%s"}`, sum, time.Now().Sub(start))
|
|
|
|
case server.RESP:
|
|
|
|
data, err := resp.SimpleStringValue(sum).MarshalRESP()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
res = string(data)
|
|
|
|
}
|
|
|
|
return res, nil
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
func (c *Controller) cmdAOF(msg *server.Message) (res string, err error) {
|
|
|
|
vs := msg.Values[1:]
|
|
|
|
var ok bool
|
2016-03-05 02:08:16 +03:00
|
|
|
var spos string
|
2016-04-01 02:26:36 +03:00
|
|
|
if vs, spos, ok = tokenval(vs); !ok || spos == "" {
|
|
|
|
return "", errInvalidNumberOfArguments
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
if len(vs) != 0 {
|
|
|
|
return "", errInvalidNumberOfArguments
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
pos, err := strconv.ParseInt(spos, 10, 64)
|
|
|
|
if err != nil || pos < 0 {
|
2016-04-01 02:26:36 +03:00
|
|
|
return "", errInvalidArgument(spos)
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
f, err := os.Open(c.f.Name())
|
|
|
|
if err != nil {
|
2016-04-01 02:26:36 +03:00
|
|
|
return "", err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
n, err := f.Seek(0, 2)
|
|
|
|
if err != nil {
|
2016-04-01 02:26:36 +03:00
|
|
|
return "", err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
if n < pos {
|
2016-04-01 02:26:36 +03:00
|
|
|
return "", errors.New("pos is too big, must be less that the aof_size of leader")
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
var s liveAOFSwitches
|
|
|
|
s.pos = pos
|
2016-04-01 02:26:36 +03:00
|
|
|
return "", s
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
func (c *Controller) liveAOF(pos int64, conn net.Conn, rd *server.AnyReaderWriter, msg *server.Message) error {
|
2016-04-01 04:20:42 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
c.aofconnM[conn] = true
|
|
|
|
c.mu.Unlock()
|
|
|
|
defer func() {
|
|
|
|
c.mu.Lock()
|
|
|
|
delete(c.aofconnM, conn)
|
|
|
|
c.mu.Unlock()
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
|
2016-04-01 02:26:36 +03:00
|
|
|
if _, err := conn.Write([]byte("+OK\r\n")); err != nil {
|
|
|
|
return err
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
|
2016-03-05 02:08:16 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
f, err := os.Open(c.f.Name())
|
|
|
|
c.mu.RUnlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if _, err := f.Seek(pos, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cond := sync.NewCond(&sync.Mutex{})
|
|
|
|
var mustQuit bool
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
cond.L.Lock()
|
|
|
|
mustQuit = true
|
|
|
|
cond.Broadcast()
|
|
|
|
cond.L.Unlock()
|
|
|
|
}()
|
|
|
|
for {
|
2016-04-01 02:26:36 +03:00
|
|
|
v, err := rd.ReadMessage()
|
2016-03-05 02:08:16 +03:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2016-04-01 02:26:36 +03:00
|
|
|
switch v.Command {
|
|
|
|
default:
|
2016-03-05 02:08:16 +03:00
|
|
|
log.Error("received a live command that was not QUIT")
|
|
|
|
return
|
2016-04-01 02:26:36 +03:00
|
|
|
case "quit", "":
|
|
|
|
return
|
2016-03-05 02:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
cond.L.Lock()
|
|
|
|
mustQuit = true
|
|
|
|
cond.Broadcast()
|
|
|
|
cond.L.Unlock()
|
|
|
|
}()
|
|
|
|
err := func() error {
|
|
|
|
_, err := io.Copy(conn, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-01 03:58:02 +03:00
|
|
|
|
|
|
|
b := make([]byte, 4096)
|
|
|
|
// The reader needs to be OK with the eof not
|
2016-03-05 02:08:16 +03:00
|
|
|
for {
|
2016-04-01 03:58:02 +03:00
|
|
|
n, err := f.Read(b)
|
|
|
|
if err != io.EOF && n > 0 {
|
2016-03-28 18:57:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-01 03:58:02 +03:00
|
|
|
if _, err := conn.Write(b[:n]); err != nil {
|
2016-03-05 02:08:16 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.fcond.L.Lock()
|
|
|
|
c.fcond.Wait()
|
|
|
|
c.fcond.L.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "use of closed network connection") &&
|
|
|
|
!strings.Contains(err.Error(), "bad file descriptor") {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
cond.L.Lock()
|
|
|
|
if mustQuit {
|
|
|
|
cond.L.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cond.Wait()
|
|
|
|
cond.L.Unlock()
|
|
|
|
}
|
|
|
|
}
|