2018-10-29 01:49:45 +03:00
|
|
|
package server
|
2016-03-30 19:32:02 +03:00
|
|
|
|
|
|
|
import (
|
2016-07-15 23:01:15 +03:00
|
|
|
"math"
|
2016-03-30 19:32:02 +03:00
|
|
|
"os"
|
|
|
|
"sort"
|
2016-12-06 02:24:26 +03:00
|
|
|
"strconv"
|
2016-03-30 19:32:02 +03:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
"github.com/tidwall/geojson"
|
2019-10-29 21:04:07 +03:00
|
|
|
"github.com/tidwall/rhh"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/tile38/core"
|
|
|
|
"github.com/tidwall/tile38/internal/collection"
|
|
|
|
"github.com/tidwall/tile38/internal/log"
|
2016-03-30 19:32:02 +03:00
|
|
|
)
|
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
const maxkeys = 8
|
|
|
|
const maxids = 32
|
|
|
|
const maxchunk = 4 * 1024 * 1024
|
2016-03-30 19:32:02 +03:00
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
func (server *Server) aofshrink() {
|
|
|
|
if server.aof == nil {
|
2018-04-11 20:53:36 +03:00
|
|
|
return
|
|
|
|
}
|
2016-03-30 19:32:02 +03:00
|
|
|
start := time.Now()
|
2018-10-29 01:49:45 +03:00
|
|
|
server.mu.Lock()
|
|
|
|
if server.shrinking {
|
|
|
|
server.mu.Unlock()
|
2016-03-30 19:32:02 +03:00
|
|
|
return
|
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
server.shrinking = true
|
|
|
|
server.shrinklog = nil
|
|
|
|
server.mu.Unlock()
|
2016-12-06 02:24:26 +03:00
|
|
|
|
2016-03-30 19:32:02 +03:00
|
|
|
defer func() {
|
2018-10-29 01:49:45 +03:00
|
|
|
server.mu.Lock()
|
|
|
|
server.shrinking = false
|
|
|
|
server.shrinklog = nil
|
|
|
|
server.mu.Unlock()
|
2016-12-06 02:24:26 +03:00
|
|
|
log.Infof("aof shrink ended %v", time.Now().Sub(start))
|
|
|
|
return
|
|
|
|
}()
|
2016-03-30 19:32:02 +03:00
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
err := func() error {
|
2018-04-11 20:53:36 +03:00
|
|
|
f, err := os.Create(core.AppendFileName + "-shrink")
|
2016-03-30 19:32:02 +03:00
|
|
|
if err != nil {
|
2016-12-06 02:24:26 +03:00
|
|
|
return err
|
2016-03-30 19:32:02 +03:00
|
|
|
}
|
2016-12-06 02:24:26 +03:00
|
|
|
defer f.Close()
|
|
|
|
var aofbuf []byte
|
|
|
|
var values []string
|
|
|
|
var keys []string
|
|
|
|
var nextkey string
|
|
|
|
var keysdone bool
|
2016-03-30 19:32:02 +03:00
|
|
|
for {
|
2016-12-06 02:24:26 +03:00
|
|
|
if len(keys) == 0 {
|
|
|
|
// load more keys
|
|
|
|
if keysdone {
|
2016-03-30 19:32:02 +03:00
|
|
|
break
|
|
|
|
}
|
2016-12-06 02:24:26 +03:00
|
|
|
keysdone = true
|
|
|
|
func() {
|
2018-10-29 01:49:45 +03:00
|
|
|
server.mu.Lock()
|
|
|
|
defer server.mu.Unlock()
|
|
|
|
server.scanGreaterOrEqual(nextkey, func(key string, col *collection.Collection) bool {
|
2016-12-06 02:24:26 +03:00
|
|
|
if len(keys) == maxkeys {
|
|
|
|
keysdone = false
|
|
|
|
nextkey = key
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
keys = append(keys, key)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
continue
|
2016-03-30 19:32:02 +03:00
|
|
|
}
|
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
var idsdone bool
|
|
|
|
var nextid string
|
|
|
|
for {
|
|
|
|
if idsdone {
|
|
|
|
keys = keys[1:]
|
|
|
|
break
|
|
|
|
}
|
2016-03-30 19:32:02 +03:00
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
// load more objects
|
|
|
|
func() {
|
|
|
|
idsdone = true
|
2018-10-29 01:49:45 +03:00
|
|
|
server.mu.Lock()
|
|
|
|
defer server.mu.Unlock()
|
|
|
|
col := server.getCol(keys[0])
|
2016-12-06 02:24:26 +03:00
|
|
|
if col == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-10-29 21:04:07 +03:00
|
|
|
var fnames = col.FieldArr() // reload an array of field names to match each object
|
|
|
|
var exm *rhh.Map // the expiration map
|
|
|
|
if value, ok := server.expires.Get(keys[0]); ok {
|
|
|
|
exm = value.(*rhh.Map)
|
|
|
|
}
|
|
|
|
var now = time.Now() // used for expiration
|
|
|
|
var count = 0 // the object count
|
2019-04-24 15:09:41 +03:00
|
|
|
col.ScanGreaterOrEqual(nextid, false, nil, nil,
|
2016-12-06 02:24:26 +03:00
|
|
|
func(id string, obj geojson.Object, fields []float64) bool {
|
|
|
|
if count == maxids {
|
|
|
|
// we reached the max number of ids for one batch
|
|
|
|
nextid = id
|
|
|
|
idsdone = false
|
|
|
|
return false
|
|
|
|
}
|
2016-03-30 19:32:02 +03:00
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
// here we fill the values array with a new command
|
|
|
|
values = values[:0]
|
|
|
|
values = append(values, "set")
|
|
|
|
values = append(values, keys[0])
|
|
|
|
values = append(values, id)
|
|
|
|
for i, fvalue := range fields {
|
|
|
|
if fvalue != 0 {
|
|
|
|
values = append(values, "field")
|
|
|
|
values = append(values, fnames[i])
|
|
|
|
values = append(values, strconv.FormatFloat(fvalue, 'f', -1, 64))
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 23:01:15 +03:00
|
|
|
if exm != nil {
|
2019-10-29 21:04:07 +03:00
|
|
|
if at, ok := exm.Get(id); ok {
|
|
|
|
expires := at.(time.Time).Sub(now)
|
2016-12-06 02:24:26 +03:00
|
|
|
if expires > 0 {
|
|
|
|
values = append(values, "ex")
|
|
|
|
values = append(values, strconv.FormatFloat(math.Floor(float64(expires)/float64(time.Second)*10)/10, 'f', -1, 64))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 00:25:40 +03:00
|
|
|
if objIsSpatial(obj) {
|
|
|
|
values = append(values, "object")
|
|
|
|
values = append(values, string(obj.AppendJSON(nil)))
|
|
|
|
} else {
|
|
|
|
values = append(values, "string")
|
|
|
|
values = append(values, obj.String())
|
2016-07-15 23:01:15 +03:00
|
|
|
}
|
2016-03-30 19:32:02 +03:00
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
// append the values to the aof buffer
|
|
|
|
aofbuf = append(aofbuf, '*')
|
|
|
|
aofbuf = append(aofbuf, strconv.FormatInt(int64(len(values)), 10)...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
for _, value := range values {
|
|
|
|
aofbuf = append(aofbuf, '$')
|
|
|
|
aofbuf = append(aofbuf, strconv.FormatInt(int64(len(value)), 10)...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
aofbuf = append(aofbuf, value...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
}
|
2016-03-30 19:32:02 +03:00
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
// increment the object count
|
|
|
|
count++
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
}()
|
2017-12-12 03:12:15 +03:00
|
|
|
if len(aofbuf) > maxchunk {
|
|
|
|
if _, err := f.Write(aofbuf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
aofbuf = aofbuf[:0]
|
2016-03-30 19:32:02 +03:00
|
|
|
}
|
2016-12-06 02:24:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load hooks
|
|
|
|
// first load the names of the hooks
|
|
|
|
var hnames []string
|
|
|
|
func() {
|
2018-10-29 01:49:45 +03:00
|
|
|
server.mu.Lock()
|
|
|
|
defer server.mu.Unlock()
|
|
|
|
for name := range server.hooks {
|
2016-12-06 02:24:26 +03:00
|
|
|
hnames = append(hnames, name)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// sort the names for consistency
|
|
|
|
sort.Strings(hnames)
|
|
|
|
for _, name := range hnames {
|
|
|
|
func() {
|
2018-10-29 01:49:45 +03:00
|
|
|
server.mu.Lock()
|
|
|
|
defer server.mu.Unlock()
|
|
|
|
hook := server.hooks[name]
|
2016-12-06 02:24:26 +03:00
|
|
|
if hook == nil {
|
2016-03-30 19:32:02 +03:00
|
|
|
return
|
|
|
|
}
|
2018-08-14 03:05:30 +03:00
|
|
|
hook.cond.L.Lock()
|
|
|
|
defer hook.cond.L.Unlock()
|
2016-12-06 02:24:26 +03:00
|
|
|
|
|
|
|
var values []string
|
2018-08-14 03:05:30 +03:00
|
|
|
if hook.channel {
|
|
|
|
values = append(values, "setchan", name)
|
|
|
|
} else {
|
|
|
|
values = append(values, "sethook", name,
|
|
|
|
strings.Join(hook.Endpoints, ","))
|
|
|
|
values = append(values)
|
|
|
|
}
|
|
|
|
for _, meta := range hook.Metas {
|
|
|
|
values = append(values, "meta", meta.Name, meta.Value)
|
|
|
|
}
|
2018-08-14 06:27:22 +03:00
|
|
|
if !hook.expires.IsZero() {
|
|
|
|
ex := float64(hook.expires.Sub(time.Now())) /
|
|
|
|
float64(time.Second)
|
|
|
|
values = append(values, "ex",
|
|
|
|
strconv.FormatFloat(ex, 'f', 1, 64))
|
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
for _, value := range hook.Message.Args {
|
|
|
|
values = append(values, value)
|
2016-03-30 19:32:02 +03:00
|
|
|
}
|
2016-12-06 02:24:26 +03:00
|
|
|
// append the values to the aof buffer
|
|
|
|
aofbuf = append(aofbuf, '*')
|
|
|
|
aofbuf = append(aofbuf, strconv.FormatInt(int64(len(values)), 10)...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
for _, value := range values {
|
|
|
|
aofbuf = append(aofbuf, '$')
|
|
|
|
aofbuf = append(aofbuf, strconv.FormatInt(int64(len(value)), 10)...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
aofbuf = append(aofbuf, value...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
}
|
|
|
|
}()
|
2016-03-30 19:32:02 +03:00
|
|
|
}
|
2016-12-06 02:24:26 +03:00
|
|
|
if len(aofbuf) > 0 {
|
|
|
|
if _, err := f.Write(aofbuf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
aofbuf = aofbuf[:0]
|
2016-03-30 19:32:02 +03:00
|
|
|
}
|
2016-12-06 02:24:26 +03:00
|
|
|
if err := f.Sync(); err != nil {
|
|
|
|
return err
|
2016-03-30 19:32:02 +03:00
|
|
|
}
|
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
// finally grab any new data that may have been written since
|
|
|
|
// the aofshrink has started and swap out the files.
|
|
|
|
return func() error {
|
2018-10-29 01:49:45 +03:00
|
|
|
server.mu.Lock()
|
|
|
|
defer server.mu.Unlock()
|
|
|
|
|
|
|
|
// flush the aof buffer
|
2019-03-10 20:48:14 +03:00
|
|
|
server.flushAOF(false)
|
2018-10-29 01:49:45 +03:00
|
|
|
|
2016-12-06 02:24:26 +03:00
|
|
|
aofbuf = aofbuf[:0]
|
2018-10-29 01:49:45 +03:00
|
|
|
for _, values := range server.shrinklog {
|
2016-12-06 02:24:26 +03:00
|
|
|
// append the values to the aof buffer
|
|
|
|
aofbuf = append(aofbuf, '*')
|
|
|
|
aofbuf = append(aofbuf, strconv.FormatInt(int64(len(values)), 10)...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
for _, value := range values {
|
|
|
|
aofbuf = append(aofbuf, '$')
|
|
|
|
aofbuf = append(aofbuf, strconv.FormatInt(int64(len(value)), 10)...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
aofbuf = append(aofbuf, value...)
|
|
|
|
aofbuf = append(aofbuf, '\r', '\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := f.Write(aofbuf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := f.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// we now have a shrunken aof file that is fully in-sync with
|
|
|
|
// the current dataset. let's swap out the on disk files and
|
|
|
|
// point to the new file.
|
|
|
|
|
|
|
|
// anything below this point is unrecoverable. just log and exit process
|
|
|
|
// back up the live aof, just in case of fatal error
|
2018-10-29 01:49:45 +03:00
|
|
|
if err := server.aof.Close(); err != nil {
|
2017-12-13 00:05:22 +03:00
|
|
|
log.Fatalf("shrink live aof close fatal operation: %v", err)
|
2017-08-11 04:34:22 +03:00
|
|
|
}
|
2018-05-25 21:50:31 +03:00
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
log.Fatalf("shrink new aof close fatal operation: %v", err)
|
|
|
|
}
|
2018-04-11 20:53:36 +03:00
|
|
|
if err := os.Rename(core.AppendFileName, core.AppendFileName+"-bak"); err != nil {
|
2017-12-13 00:05:22 +03:00
|
|
|
log.Fatalf("shrink backup fatal operation: %v", err)
|
2016-12-06 02:24:26 +03:00
|
|
|
}
|
2018-04-11 20:53:36 +03:00
|
|
|
if err := os.Rename(core.AppendFileName+"-shrink", core.AppendFileName); err != nil {
|
2017-12-13 00:05:22 +03:00
|
|
|
log.Fatalf("shrink rename fatal operation: %v", err)
|
2016-12-06 02:24:26 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
server.aof, err = os.OpenFile(core.AppendFileName, os.O_CREATE|os.O_RDWR, 0600)
|
2016-12-06 02:24:26 +03:00
|
|
|
if err != nil {
|
2017-12-13 00:05:22 +03:00
|
|
|
log.Fatalf("shrink openfile fatal operation: %v", err)
|
2016-12-06 02:24:26 +03:00
|
|
|
}
|
|
|
|
var n int64
|
2018-10-29 01:49:45 +03:00
|
|
|
n, err = server.aof.Seek(0, 2)
|
2016-12-06 02:24:26 +03:00
|
|
|
if err != nil {
|
2017-12-13 00:05:22 +03:00
|
|
|
log.Fatalf("shrink seek end fatal operation: %v", err)
|
2016-12-06 02:24:26 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
server.aofsz = int(n)
|
2016-12-06 02:24:26 +03:00
|
|
|
|
2018-04-11 20:53:36 +03:00
|
|
|
os.Remove(core.AppendFileName + "-bak") // ignore error
|
2016-12-06 02:24:26 +03:00
|
|
|
|
|
|
|
// kill all followers connections
|
2018-10-29 01:49:45 +03:00
|
|
|
for conn := range server.aofconnM {
|
2016-12-06 02:24:26 +03:00
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("aof shrink failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-03-30 19:32:02 +03:00
|
|
|
}
|