tile38/controller/controller.go

388 lines
9.2 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package controller
import (
"bufio"
"bytes"
"crypto/rand"
"errors"
"fmt"
"io"
"os"
2016-03-29 01:50:18 +03:00
"runtime"
2016-03-08 03:37:39 +03:00
"strings"
2016-03-05 02:08:16 +03:00
"sync"
"time"
"github.com/google/btree"
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/collection"
"github.com/tidwall/tile38/controller/log"
"github.com/tidwall/tile38/controller/server"
2016-03-05 02:08:16 +03:00
"github.com/tidwall/tile38/core"
"github.com/tidwall/tile38/geojson"
)
type collectionT struct {
Key string
Collection *collection.Collection
}
type commandDetailsT struct {
command string
key, id string
field string
value float64
obj geojson.Object
fields []float64
oldObj geojson.Object
oldFields []float64
2016-03-28 18:57:41 +03:00
updated bool
2016-03-05 02:08:16 +03:00
}
func (col *collectionT) Less(item btree.Item) bool {
return col.Key < item.(*collectionT).Key
}
// Controller is a tile38 controller
type Controller struct {
mu sync.RWMutex
2016-03-05 23:39:36 +03:00
host string
2016-03-05 02:08:16 +03:00
port int
f *os.File
cols *btree.BTree // use both tree and map. provide ordering.
colsm map[string]*collection.Collection // use both tree and map. provide performance.
aofsz int
dir string
config Config
followc uint64 // counter increases when follow property changes
follows map[*bytes.Buffer]bool
fcond *sync.Cond
lstack []*commandDetailsT
lives map[*liveBuffer]bool
lcond *sync.Cond
2016-03-19 17:16:19 +03:00
fcup bool // follow caught up
shrinking bool // aof shrinking flag
hooks map[string]*Hook // hook name
hookcols map[string]map[string]*Hook // col key
2016-03-05 02:08:16 +03:00
}
// ListenAndServe starts a new tile38 server
2016-03-05 23:39:36 +03:00
func ListenAndServe(host string, port int, dir string) error {
2016-03-05 02:08:16 +03:00
log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA)
c := &Controller{
2016-03-19 17:16:19 +03:00
host: host,
port: port,
dir: dir,
cols: btree.New(16),
colsm: make(map[string]*collection.Collection),
follows: make(map[*bytes.Buffer]bool),
fcond: sync.NewCond(&sync.Mutex{}),
lives: make(map[*liveBuffer]bool),
lcond: sync.NewCond(&sync.Mutex{}),
hooks: make(map[string]*Hook),
hookcols: make(map[string]map[string]*Hook),
2016-03-05 02:08:16 +03:00
}
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
if err := c.loadConfig(); err != nil {
return err
}
f, err := os.OpenFile(dir+"/aof", os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return err
}
c.f = f
if err := c.loadAOF(); err != nil {
return err
}
c.mu.Lock()
if c.config.FollowHost != "" {
go c.follow(c.config.FollowHost, c.config.FollowPort, c.followc)
}
c.mu.Unlock()
defer func() {
c.mu.Lock()
c.followc++ // this will force any follow communication to die
c.mu.Unlock()
}()
go c.processLives()
2016-03-28 18:57:41 +03:00
handler := func(conn *server.Conn, msg *server.Message, rd *bufio.Reader, w io.Writer, websocket bool) error {
err := c.handleInputCommand(conn, msg, w)
2016-03-05 02:08:16 +03:00
if err != nil {
if err.Error() == "going live" {
return c.goLive(err, conn, rd, websocket)
}
return err
}
return nil
}
2016-03-08 03:37:39 +03:00
protected := func() bool {
2016-03-08 16:11:03 +03:00
if core.ProtectedMode == "no" {
2016-03-08 03:37:39 +03:00
// --protected-mode no
return false
}
if host != "" && host != "127.0.0.1" && host != "::1" && host != "localhost" {
// -h address
return false
}
c.mu.RLock()
is := c.config.ProtectedMode != "no" && c.config.RequirePass == ""
c.mu.RUnlock()
return is
}
return server.ListenAndServe(host, port, protected, handler)
2016-03-05 02:08:16 +03:00
}
func (c *Controller) setCol(key string, col *collection.Collection) {
c.cols.ReplaceOrInsert(&collectionT{Key: key, Collection: col})
c.colsm[key] = col
}
func (c *Controller) getCol(key string) *collection.Collection {
col, ok := c.colsm[key]
if !ok {
return nil
}
return col
}
func (c *Controller) deleteCol(key string) *collection.Collection {
delete(c.colsm, key)
i := c.cols.Delete(&collectionT{Key: key})
if i == nil {
return nil
}
return i.(*collectionT).Collection
}
func isReservedFieldName(field string) bool {
switch field {
case "z", "lat", "lon":
return true
}
return false
}
2016-03-28 18:57:41 +03:00
func (c *Controller) handleInputCommand(conn *server.Conn, msg *server.Message, w io.Writer) error {
var words []string
for _, v := range msg.Values {
words = append(words, v.String())
2016-03-05 02:08:16 +03:00
}
2016-03-28 18:57:41 +03:00
// line := strings.Join(words, " ")
// if core.ShowDebugMessages && line != "pInG" {
// log.Debug(line)
// }
2016-03-05 02:08:16 +03:00
start := time.Now()
2016-03-28 18:57:41 +03:00
2016-03-08 03:37:39 +03:00
// Ping. Just send back the response. No need to put through the pipeline.
2016-03-28 18:57:41 +03:00
if msg.Command == "ping" {
switch msg.OutputType {
case server.JSON:
w.Write([]byte(`{"ok":true,"ping":"pong","elapsed":"` + time.Now().Sub(start).String() + `"}`))
case server.RESP:
io.WriteString(w, "+PONG\r\n")
}
2016-03-05 02:08:16 +03:00
return nil
}
writeErr := func(err error) error {
2016-03-28 18:57:41 +03:00
switch msg.OutputType {
case server.JSON:
io.WriteString(w, `{"ok":false,"err":`+jsonString(err.Error())+`,"elapsed":"`+time.Now().Sub(start).String()+"\"}")
case server.RESP:
if err == errInvalidNumberOfArguments {
io.WriteString(w, "-ERR wrong number of arguments for '"+msg.Command+"' command\r\n")
} else {
v, _ := resp.ErrorValue(errors.New("ERR " + err.Error())).MarshalRESP()
io.WriteString(w, string(v))
}
2016-03-05 02:08:16 +03:00
}
return nil
}
var write bool
2016-03-08 03:37:39 +03:00
2016-03-28 18:57:41 +03:00
if !conn.Authenticated || msg.Command == "auth" {
2016-03-08 03:37:39 +03:00
c.mu.RLock()
requirePass := c.config.RequirePass
c.mu.RUnlock()
if requirePass != "" {
// This better be an AUTH command.
2016-03-28 18:57:41 +03:00
if msg.Command != "auth" {
2016-03-08 03:37:39 +03:00
// Just shut down the pipeline now. The less the client connection knows the better.
return writeErr(errors.New("authentication required"))
}
2016-03-28 18:57:41 +03:00
password := ""
if len(msg.Values) > 1 {
password = msg.Values[1].String()
}
2016-03-08 18:35:43 +03:00
if requirePass != strings.TrimSpace(password) {
2016-03-08 03:37:39 +03:00
return writeErr(errors.New("invalid password"))
}
2016-03-08 18:35:43 +03:00
conn.Authenticated = true
w.Write([]byte(`{"ok":true,"elapsed":"` + time.Now().Sub(start).String() + "\"}"))
return nil
2016-03-28 18:57:41 +03:00
} else if msg.Command == "auth" {
2016-03-08 18:35:43 +03:00
return writeErr(errors.New("invalid password"))
2016-03-08 03:37:39 +03:00
}
}
2016-03-05 02:08:16 +03:00
// choose the locking strategy
2016-03-28 18:57:41 +03:00
switch msg.Command {
2016-03-05 02:08:16 +03:00
default:
c.mu.RLock()
defer c.mu.RUnlock()
2016-03-20 18:24:20 +03:00
case "set", "del", "drop", "fset", "flushdb", "sethook", "delhook":
2016-03-05 02:08:16 +03:00
// write operations
write = true
c.mu.Lock()
defer c.mu.Unlock()
if c.config.FollowHost != "" {
return writeErr(errors.New("not the leader"))
}
if c.config.ReadOnly {
return writeErr(errors.New("read only"))
}
2016-03-19 17:16:19 +03:00
case "get", "keys", "scan", "nearby", "within", "intersects", "hooks":
2016-03-05 02:08:16 +03:00
// read operations
c.mu.RLock()
defer c.mu.RUnlock()
if c.config.FollowHost != "" && !c.fcup {
return writeErr(errors.New("catching up to leader"))
}
2016-03-08 03:37:39 +03:00
case "follow", "readonly", "config":
2016-03-05 02:08:16 +03:00
// system operations
// does not write to aof, but requires a write lock.
c.mu.Lock()
defer c.mu.Unlock()
case "massinsert":
// dev operation
// ** danger zone **
// no locks! DEV MODE ONLY
}
2016-03-28 18:57:41 +03:00
res, d, err := c.command(msg, w)
2016-03-05 02:08:16 +03:00
if err != nil {
if err.Error() == "going live" {
return err
}
return writeErr(err)
}
if write {
2016-03-28 18:57:41 +03:00
if err := c.writeAOF(resp.ArrayValue(msg.Values), &d); err != nil {
2016-03-19 17:16:19 +03:00
if _, ok := err.(errAOFHook); ok {
return writeErr(err)
}
2016-03-05 02:08:16 +03:00
log.Fatal(err)
return err
}
}
2016-03-28 18:57:41 +03:00
if res != "" {
if _, err := io.WriteString(w, res); err != nil {
2016-03-05 02:08:16 +03:00
return err
}
}
return nil
}
func randomKey(n int) string {
b := make([]byte, n)
nn, err := rand.Read(b)
if err != nil {
panic(err)
}
if nn != n {
panic("random failed")
}
return fmt.Sprintf("%x", b)
}
func (c *Controller) reset() {
c.aofsz = 0
c.cols = btree.New(16)
}
2016-03-28 18:57:41 +03:00
func (c *Controller) command(msg *server.Message, w io.Writer) (res string, d commandDetailsT, err error) {
2016-03-05 02:08:16 +03:00
start := time.Now()
okResp := func() string {
2016-03-28 18:57:41 +03:00
if w != nil {
switch msg.OutputType {
case server.JSON:
return `{"ok":true,"elapsed":"` + time.Now().Sub(start).String() + "\"}"
case server.RESP:
return "+OK\r\n"
}
2016-03-05 02:08:16 +03:00
}
2016-03-28 18:57:41 +03:00
return ""
2016-03-05 02:08:16 +03:00
}
2016-03-28 18:57:41 +03:00
switch msg.Command {
2016-03-05 02:08:16 +03:00
default:
2016-03-28 18:57:41 +03:00
err = fmt.Errorf("unknown command '%s'", msg.Values[0])
2016-03-05 02:08:16 +03:00
return
// lock
case "set":
2016-03-28 18:57:41 +03:00
res, d, err = c.cmdSet(msg)
2016-03-05 02:08:16 +03:00
case "fset":
2016-03-28 18:57:41 +03:00
res, d, err = c.cmdFset(msg)
2016-03-05 02:08:16 +03:00
case "del":
2016-03-28 18:57:41 +03:00
res, d, err = c.cmdDel(msg)
2016-03-05 02:08:16 +03:00
case "drop":
2016-03-28 18:57:41 +03:00
res, d, err = c.cmdDrop(msg)
2016-03-05 02:08:16 +03:00
case "flushdb":
2016-03-28 18:57:41 +03:00
res, d, err = c.cmdFlushDB(msg)
// case "sethook":
// err = c.cmdSetHook(nline)
// resp = okResp()
// case "delhook":
// err = c.cmdDelHook(nline)
// resp = okResp()
// case "hooks":
// err = c.cmdHooks(nline, w)
// case "massinsert":
// if !core.DevMode {
// err = fmt.Errorf("unknown command '%s'", cmd)
// return
// }
// err = c.cmdMassInsert(nline)
// resp = okResp()
// case "follow":
// err = c.cmdFollow(nline)
// resp = okResp()
// case "config":
// resp, err = c.cmdConfig(nline)
// case "readonly":
// err = c.cmdReadOnly(nline)
// resp = okResp()
// case "stats":
// resp, err = c.cmdStats(nline)
2016-03-29 01:50:18 +03:00
case "server":
res, err = c.cmdServer(msg)
2016-03-29 00:16:21 +03:00
case "scan":
res, err = c.cmdScan(msg)
case "nearby":
res, err = c.cmdNearby(msg)
case "within":
res, err = c.cmdWithin(msg)
case "intersects":
res, err = c.cmdIntersects(msg)
2016-03-05 02:08:16 +03:00
case "get":
2016-03-28 18:57:41 +03:00
res, err = c.cmdGet(msg)
2016-03-29 01:22:30 +03:00
case "keys":
res, err = c.cmdKeys(msg)
2016-03-29 01:50:18 +03:00
// case "aof":
// err = c.cmdAOF(nline, w)
// case "aofmd5":
// resp, err = c.cmdAOFMD5(nline)
case "gc":
go runtime.GC()
res = okResp()
2016-03-28 18:57:41 +03:00
// resp = okResp()
// case "aofshrink":
// go c.aofshrink()
// resp = okResp()
2016-03-05 02:08:16 +03:00
}
return
}