-
Notifications
You must be signed in to change notification settings - Fork 336
/
table_view_impl.go
281 lines (242 loc) · 6.84 KB
/
table_view_impl.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// https://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 pulsar
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
"time"
"github.com/apache/pulsar-client-go/pulsar/log"
"github.com/sirupsen/logrus"
)
type cancelReader struct {
reader Reader
cancelFunc context.CancelFunc
}
type TableViewImpl struct {
client *client
options TableViewOptions
dataMu sync.Mutex
data map[string]interface{}
readersMu sync.Mutex
cancelRaders map[string]cancelReader
listenersMu sync.Mutex
listeners []func(string, interface{}) error
logger log.Logger
closed bool
closedCh chan struct{}
}
func newTableView(client *client, options TableViewOptions) (TableView, error) {
if options.Topic == "" {
return nil, newError(TopicNotFound, "topic is required")
}
if options.Schema != nil && options.SchemaValueType == nil {
return nil, newError(InvalidConfiguration, "SchemaValueType is required when Schema is present")
}
var logger log.Logger
if options.Logger != nil {
logger = options.Logger
} else {
logger = log.NewLoggerWithLogrus(logrus.StandardLogger())
}
if options.AutoUpdatePartitionsInterval == 0 {
options.AutoUpdatePartitionsInterval = time.Minute
}
tv := TableViewImpl{
client: client,
options: options,
data: make(map[string]interface{}),
cancelRaders: make(map[string]cancelReader),
logger: logger,
closedCh: make(chan struct{}),
}
// Do an initial round of partition update check to make sure we can populate the partition readers
if err := tv.partitionUpdateCheck(); err != nil {
return nil, err
}
go tv.periodicPartitionUpdateCheck()
return &tv, nil
}
func (tv *TableViewImpl) partitionUpdateCheck() error {
partitionsArray, err := tv.client.TopicPartitions(tv.options.Topic)
if err != nil {
return fmt.Errorf("tv.client.TopicPartitions(%s) failed: %w", tv.options.Topic, err)
}
partitions := make(map[string]bool, len(partitionsArray))
for _, partition := range partitionsArray {
partitions[partition] = true
}
tv.readersMu.Lock()
defer tv.readersMu.Unlock()
for partition, cancelReader := range tv.cancelRaders {
if _, ok := partitions[partition]; !ok {
cancelReader.cancelFunc()
cancelReader.reader.Close()
delete(tv.cancelRaders, partition)
}
}
for partition := range partitions {
if _, ok := tv.cancelRaders[partition]; !ok {
reader, err := newReader(tv.client, ReaderOptions{
Topic: partition,
StartMessageID: EarliestMessageID(),
ReadCompacted: true,
// TODO: Pooling?
Schema: tv.options.Schema,
})
if err != nil {
return fmt.Errorf("create new reader failed for %s: %w", partition, err)
}
for reader.HasNext() {
msg, err := reader.Next(context.Background())
if err != nil {
tv.logger.Errorf("read next message failed for %s: %w", partition, err)
}
if msg != nil {
tv.handleMessage(msg)
}
}
ctx, cancelFunc := context.WithCancel(context.Background())
tv.cancelRaders[partition] = cancelReader{
reader: reader,
cancelFunc: cancelFunc,
}
go tv.watchReaderForNewMessages(ctx, reader)
}
}
return nil
}
func (tv *TableViewImpl) periodicPartitionUpdateCheck() {
for {
if err := tv.partitionUpdateCheck(); err != nil {
tv.logger.Errorf("failed to check for changes in number of partitions: %w", err)
}
select {
case <-tv.closedCh:
// If the TableViewImpl has been closed, stop checking for partition updates
return
case <-time.After(tv.options.AutoUpdatePartitionsInterval):
continue
}
}
}
func (tv *TableViewImpl) Size() int {
tv.dataMu.Lock()
defer tv.dataMu.Unlock()
return len(tv.data)
}
func (tv *TableViewImpl) IsEmpty() bool {
tv.dataMu.Lock()
defer tv.dataMu.Unlock()
return tv.Size() == 0
}
func (tv *TableViewImpl) ContainsKey(key string) bool {
tv.dataMu.Lock()
defer tv.dataMu.Unlock()
_, ok := tv.data[key]
return ok
}
func (tv *TableViewImpl) Get(key string) interface{} {
tv.dataMu.Lock()
defer tv.dataMu.Unlock()
return tv.data[key]
}
func (tv *TableViewImpl) Entries() map[string]interface{} {
tv.dataMu.Lock()
defer tv.dataMu.Unlock()
data := make(map[string]interface{}, len(tv.data))
for k, v := range tv.data {
data[k] = v
}
return tv.data
}
func (tv *TableViewImpl) Keys() []string {
tv.dataMu.Lock()
defer tv.dataMu.Unlock()
keys := make([]string, len(tv.data))
i := 0
for k := range tv.data {
keys[i] = k
i++
}
return keys
}
func (tv *TableViewImpl) ForEach(action func(string, interface{}) error) error {
tv.dataMu.Lock()
defer tv.dataMu.Unlock()
for k, v := range tv.data {
if err := action(k, v); err != nil {
return err
}
}
return nil
}
func (tv *TableViewImpl) ForEachAndListen(action func(string, interface{}) error) error {
tv.listenersMu.Lock()
defer tv.listenersMu.Unlock()
if err := tv.ForEach(action); err != nil {
return err
}
tv.listeners = append(tv.listeners, action)
return nil
}
func (tv *TableViewImpl) Close() {
tv.readersMu.Lock()
defer tv.readersMu.Unlock()
if !tv.closed {
tv.closed = true
for _, cancelReader := range tv.cancelRaders {
cancelReader.reader.Close()
}
close(tv.closedCh)
}
}
func (tv *TableViewImpl) handleMessage(msg Message) {
tv.dataMu.Lock()
defer tv.dataMu.Unlock()
payload := reflect.New(tv.options.SchemaValueType)
if len(msg.Payload()) == 0 {
delete(tv.data, msg.Key())
} else {
if err := msg.GetSchemaValue(payload.Interface()); err != nil {
tv.logger.Errorf("msg.GetSchemaValue() failed with %v; msg is %v", err, msg)
}
tv.data[msg.Key()] = reflect.Indirect(payload).Interface()
}
for _, listener := range tv.listeners {
if err := listener(msg.Key(), reflect.Indirect(payload).Interface()); err != nil {
tv.logger.Errorf("table view listener failed for %v: %w", msg, err)
}
}
}
func (tv *TableViewImpl) watchReaderForNewMessages(ctx context.Context, reader Reader) {
for {
msg, err := reader.Next(ctx)
if err != nil {
tv.logger.Errorf("read next message failed for %s: %w", reader.Topic(), err)
}
var e *Error
if (errors.As(err, &e) && e.Result() == ConsumerClosed) || errors.Is(err, context.Canceled) {
return
}
if msg != nil {
tv.handleMessage(msg)
}
}
}