-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
publisher.go
159 lines (145 loc) · 3.51 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package badger
import (
"bytes"
"sync"
"github.com/dgraph-io/badger/pb"
"github.com/dgraph-io/badger/y"
)
type subscriber struct {
prefixes [][]byte
sendCh chan<- *pb.KVList
subCloser *y.Closer
}
type publisher struct {
sync.Mutex
pubCh chan requests
subscribers map[uint64]subscriber
nextID uint64
}
func newPublisher() *publisher {
return &publisher{
pubCh: make(chan requests, 1000),
subscribers: make(map[uint64]subscriber),
nextID: 0,
}
}
func (p *publisher) listenForUpdates(c *y.Closer) {
defer func() {
p.cleanSubscribers()
c.Done()
}()
slurp := func(batch []*request) {
for {
select {
case reqs := <-p.pubCh:
batch = append(batch, reqs...)
default:
p.publishUpdates(batch)
return
}
}
}
for {
select {
case <-c.HasBeenClosed():
return
case reqs := <-p.pubCh:
slurp(reqs)
}
}
}
func (p *publisher) publishUpdates(reqs requests) {
kvs := &pb.KVList{}
p.Lock()
defer func() {
p.Unlock()
// Release all the request.
reqs.DecrRef()
}()
// TODO: Optimize this, so we can figure out key -> subscriber quickly, without iterating over
// all the prefixes.
// TODO: Use trie to find subscribers.
for _, s := range p.subscribers {
// BUG: This would send out the same entry multiple times on multiple matches for the same
// subscriber.
for _, prefix := range s.prefixes {
for _, req := range reqs {
for _, e := range req.Entries {
if bytes.HasPrefix(e.Key, prefix) {
// TODO: Maybe we can optimize this by creating the KV once and sending it
// over to multiple subscribers.
k := y.SafeCopy(nil, e.Key)
kv := &pb.KV{
Key: y.ParseKey(k),
Value: y.SafeCopy(nil, e.Value),
UserMeta: []byte{e.UserMeta},
ExpiresAt: e.ExpiresAt,
Version: y.ParseTs(k),
}
kvs.Kv = append(kvs.Kv, kv)
}
}
}
}
if len(kvs.GetKv()) > 0 {
s.sendCh <- kvs
}
}
}
func (p *publisher) newSubscriber(c *y.Closer, prefixes ...[]byte) (<-chan *pb.KVList, uint64) {
p.Lock()
defer p.Unlock()
ch := make(chan *pb.KVList, 1000)
id := p.nextID
// Increment next ID.
p.nextID++
p.subscribers[id] = subscriber{
prefixes: prefixes,
sendCh: ch,
subCloser: c,
}
return ch, id
}
// cleanSubscribers stops all the subscribers. Ideally, It should be called while closing DB.
func (p *publisher) cleanSubscribers() {
p.Lock()
defer p.Unlock()
for id, s := range p.subscribers {
delete(p.subscribers, id)
s.subCloser.SignalAndWait()
}
}
func (p *publisher) deleteSubscriber(id uint64) {
p.Lock()
defer p.Unlock()
if _, ok := p.subscribers[id]; !ok {
return
}
delete(p.subscribers, id)
}
func (p *publisher) sendUpdates(reqs []*request) {
// TODO: Prefix check before pushing into pubCh.
if p.noOfSubscribers() != 0 {
p.pubCh <- reqs
}
}
func (p *publisher) noOfSubscribers() int {
p.Lock()
defer p.Unlock()
return len(p.subscribers)
}