2018-10-29 01:49:45 +03:00
|
|
|
package server
|
2016-07-15 22:22:48 +03:00
|
|
|
|
|
|
|
import (
|
2017-03-29 22:50:04 +03:00
|
|
|
"math/rand"
|
2016-07-15 22:22:48 +03:00
|
|
|
"time"
|
|
|
|
|
2019-10-29 21:04:07 +03:00
|
|
|
"github.com/tidwall/rhh"
|
|
|
|
"github.com/tidwall/tile38/internal/log"
|
2016-07-15 22:22:48 +03:00
|
|
|
)
|
|
|
|
|
2017-03-29 22:50:04 +03:00
|
|
|
// clearIDExpires clears a single item from the expires list.
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) clearIDExpires(key, id string) (cleared bool) {
|
2019-10-29 21:04:07 +03:00
|
|
|
if c.expires.Len() > 0 {
|
|
|
|
if idm, ok := c.expires.Get(key); ok {
|
|
|
|
if _, ok := idm.(*rhh.Map).Delete(id); ok {
|
|
|
|
if idm.(*rhh.Map).Len() == 0 {
|
|
|
|
c.expires.Delete(key)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
2019-10-29 21:04:07 +03:00
|
|
|
return false
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 22:50:04 +03:00
|
|
|
// clearKeyExpires clears all items that are marked as expires from a single key.
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) clearKeyExpires(key string) {
|
2019-10-29 21:04:07 +03:00
|
|
|
c.expires.Delete(key)
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-28 04:15:53 +03:00
|
|
|
// moveKeyExpires moves all items that are marked as expires from a key to a newKey.
|
|
|
|
func (c *Server) moveKeyExpires(key, newKey string) {
|
2019-10-29 21:04:07 +03:00
|
|
|
if idm, ok := c.expires.Delete(key); ok {
|
|
|
|
c.expires.Set(newKey, idm)
|
|
|
|
}
|
2018-12-28 04:15:53 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 22:50:04 +03:00
|
|
|
// expireAt marks an item as expires at a specific time.
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) expireAt(key, id string, at time.Time) {
|
2019-10-29 21:04:07 +03:00
|
|
|
idm, ok := c.expires.Get(key)
|
|
|
|
if !ok {
|
|
|
|
idm = rhh.New(0)
|
|
|
|
c.expires.Set(key, idm)
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
2019-10-29 21:04:07 +03:00
|
|
|
idm.(*rhh.Map).Set(id, at.UnixNano())
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 22:50:04 +03:00
|
|
|
// getExpires returns the when an item expires.
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) getExpires(key, id string) (at time.Time, ok bool) {
|
2019-10-29 21:04:07 +03:00
|
|
|
if c.expires.Len() > 0 {
|
|
|
|
if idm, ok := c.expires.Get(key); ok {
|
|
|
|
if atv, ok := idm.(*rhh.Map).Get(id); ok {
|
|
|
|
return time.Unix(0, atv.(int64)), true
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
2019-10-29 21:04:07 +03:00
|
|
|
return time.Time{}, false
|
2017-03-29 22:50:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// hasExpired returns true if an item has expired.
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) hasExpired(key, id string) bool {
|
2019-10-29 21:04:07 +03:00
|
|
|
if at, ok := c.getExpires(key, id); ok {
|
|
|
|
return time.Now().After(at)
|
2017-03-29 22:50:04 +03:00
|
|
|
}
|
2019-10-29 21:04:07 +03:00
|
|
|
return false
|
2017-03-29 22:50:04 +03:00
|
|
|
}
|
|
|
|
|
2019-10-29 21:04:07 +03:00
|
|
|
const bgExpireDelay = time.Second / 10
|
|
|
|
const bgExpireSegmentSize = 20
|
|
|
|
|
|
|
|
// expirePurgeSweep is ran from backgroundExpiring operation and performs
|
|
|
|
// segmented sweep of the expires list
|
|
|
|
func (c *Server) expirePurgeSweep(rng *rand.Rand) (purged int) {
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
if c.expires.Len() == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
for i := 0; i < bgExpireSegmentSize; i++ {
|
|
|
|
if key, idm, ok := c.expires.GetPos(rng.Uint64()); ok {
|
|
|
|
id, atv, ok := idm.(*rhh.Map).GetPos(rng.Uint64())
|
|
|
|
if ok {
|
|
|
|
if now > atv.(int64) {
|
|
|
|
// expired, purge from database
|
2018-10-29 01:49:45 +03:00
|
|
|
msg := &Message{}
|
2019-10-29 21:04:07 +03:00
|
|
|
msg.Args = []string{"del", key, id}
|
2017-03-29 22:50:04 +03:00
|
|
|
_, d, err := c.cmdDel(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
if err := c.writeAOF(msg.Args, &d); err != nil {
|
2017-03-29 22:50:04 +03:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-29 21:04:07 +03:00
|
|
|
purged++
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-29 22:50:04 +03:00
|
|
|
}
|
2019-10-29 21:04:07 +03:00
|
|
|
// recycle the lock
|
|
|
|
c.mu.Unlock()
|
|
|
|
c.mu.Lock()
|
|
|
|
}
|
|
|
|
return purged
|
|
|
|
}
|
|
|
|
|
|
|
|
// backgroundExpiring watches for when items that have expired must be purged
|
|
|
|
// from the database. It's executes 10 times a seconds.
|
|
|
|
func (c *Server) backgroundExpiring() {
|
|
|
|
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
for {
|
|
|
|
if c.stopServer.on() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
purged := c.expirePurgeSweep(rng)
|
|
|
|
if purged > bgExpireSegmentSize/4 {
|
|
|
|
// do another purge immediately
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
// back off
|
|
|
|
time.Sleep(bgExpireDelay)
|
|
|
|
}
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
|
|
|
}
|