2018-10-29 01:49:45 +03:00
|
|
|
package server
|
2016-07-15 22:22:48 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-10-29 21:04:07 +03:00
|
|
|
"github.com/tidwall/tile38/internal/log"
|
2016-07-15 22:22:48 +03:00
|
|
|
)
|
|
|
|
|
2019-10-29 21:04:07 +03:00
|
|
|
const bgExpireDelay = time.Second / 10
|
|
|
|
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
// backgroundExpiring deletes expired items from the database.
|
|
|
|
// It's executes every 1/10 of a second.
|
2019-10-30 20:17:59 +03:00
|
|
|
func (s *Server) backgroundExpiring() {
|
2019-10-29 21:04:07 +03:00
|
|
|
for {
|
2019-10-30 20:17:59 +03:00
|
|
|
if s.stopServer.on() {
|
2019-10-29 21:04:07 +03:00
|
|
|
return
|
|
|
|
}
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
func() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-09-12 19:55:58 +03:00
|
|
|
now := time.Now()
|
|
|
|
s.backgroundExpireObjects(now)
|
|
|
|
s.backgroundExpireHooks(now)
|
Update expiration logic
This commit changes the logic for managing the expiration of
objects in the database.
Before: There was a server-wide hashmap that stored the
collection key, id, and expiration timestamp for all objects
that had a TTL. The hashmap was occasionally probed at 20
random positions, looking for objects that have expired. Those
expired objects were immediately deleted, and if there was 5
or more objects deleted, then the probe happened again, with
no delay. If the number of objects was less than 5 then the
there was a 1/10th of a second delay before the next probe.
Now: Rather than a server-wide hashmap, each collection has
its own ordered priority queue that stores objects with TTLs.
Rather than probing, there is a background routine that
executes every 1/10th of a second, which pops the expired
objects from the collection queues, and deletes them.
The collection/queue method is a more stable approach than
the hashmap/probing method. With probing, we can run into
major cache misses for some cases where there is wide
TTL duration, such as in the hours or days. This may cause
the system to occasionally fall behind, leaving should-be
expired objects in memory. Using a queue, there is no
cache misses, all objects that should be expired will be
right away, regardless of the TTL durations.
Fixes #616
2021-07-12 23:37:50 +03:00
|
|
|
}()
|
|
|
|
time.Sleep(bgExpireDelay)
|
2016-07-15 22:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-12 19:55:58 +03:00
|
|
|
|
|
|
|
func (s *Server) backgroundExpireObjects(now time.Time) {
|
|
|
|
nano := now.UnixNano()
|
|
|
|
var ids []string
|
|
|
|
var msgs []*Message
|
|
|
|
s.cols.Ascend(nil, func(v interface{}) bool {
|
|
|
|
col := v.(*collectionKeyContainer)
|
|
|
|
ids = col.col.Expired(nano, ids[:0])
|
|
|
|
for _, id := range ids {
|
|
|
|
msgs = append(msgs, &Message{
|
|
|
|
Args: []string{"del", col.key, id},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
for _, msg := range msgs {
|
|
|
|
_, d, err := s.cmdDel(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := s.writeAOF(msg.Args, &d); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(msgs) > 0 {
|
|
|
|
log.Debugf("Expired %d objects\n", len(msgs))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) backgroundExpireHooks(now time.Time) {
|
|
|
|
var msgs []*Message
|
|
|
|
s.hookExpires.Ascend(nil, func(v interface{}) bool {
|
|
|
|
h := v.(*Hook)
|
|
|
|
if h.expires.After(now) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
msg := &Message{}
|
|
|
|
if h.channel {
|
|
|
|
msg.Args = []string{"delchan", h.Name}
|
|
|
|
} else {
|
|
|
|
msg.Args = []string{"delhook", h.Name}
|
|
|
|
}
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
_, d, err := s.cmdDelHook(msg, msg.Args[0] == "delchan")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := s.writeAOF(msg.Args, &d); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(msgs) > 0 {
|
|
|
|
log.Debugf("Expired %d hooks\n", len(msgs))
|
|
|
|
}
|
|
|
|
}
|