/
factory.go
193 lines (156 loc) · 4.92 KB
/
factory.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
// Copyright 2022-2023 Tigris Data, Inc.
//
// 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 realtime
import (
"context"
"sync"
"time"
"github.com/rs/zerolog/log"
"github.com/tigrisdata/tigris/server/metadata"
"github.com/tigrisdata/tigris/store/cache"
)
const monitorChannelDuration = 2 * time.Minute
type ChannelFactory struct {
sync.RWMutex
cache cache.Cache
encoder metadata.CacheEncoder
heartbeatF *HeartbeatFactory
channels map[string]*Channel
}
func NewChannelFactory(cache cache.Cache, encoder metadata.CacheEncoder, heartbeatF *HeartbeatFactory) *ChannelFactory {
factory := &ChannelFactory{
cache: cache,
encoder: encoder,
heartbeatF: heartbeatF,
channels: make(map[string]*Channel),
}
go factory.monitorStreams()
return factory
}
func (factory *ChannelFactory) monitorStreams() {
ticker := time.NewTicker(monitorChannelDuration)
defer ticker.Stop()
for range ticker.C {
for _, c := range factory.channels {
_ = factory.deleteChannelIfInactive(c)
}
}
}
func (factory *ChannelFactory) deleteChannelIfInactive(c *Channel) error {
groups, err := c.stream.GetConsumerGroups(context.TODO())
if err != nil {
log.Err(err).Str("channel", c.encName).Msg("reading consumers failed")
return err
}
groupsName := make([]string, len(groups))
for i, g := range groups {
groupsName[i] = g.Name
}
heartbeat := factory.heartbeatF.GetHeartbeatTable(c.tenant, c.project)
if heartbeat.GroupsExpired(groupsName) {
factory.DeleteChannel(context.TODO(), c)
}
return nil
}
func (factory *ChannelFactory) getChannel(encStream string) (*Channel, bool) {
factory.RLock()
defer factory.RUnlock()
ch, ok := factory.channels[encStream]
return ch, ok
}
func (factory *ChannelFactory) getOrCreateChannelFromCache(ctx context.Context, encStream string) (*Channel, error) {
stream, err := factory.cache.CreateOrGetStream(ctx, encStream)
if err != nil {
return nil, err
}
return NewChannel(encStream, stream), nil
}
func (factory *ChannelFactory) ListChannels(ctx context.Context, tenantId uint32, projId uint32, prefix string) ([]string, error) {
encProj, err := factory.encoder.EncodeCacheTableName(tenantId, projId, prefix)
if err != nil {
return nil, err
}
streams, err := factory.cache.ListStreams(ctx, encProj)
if err != nil {
return nil, err
}
channelNames := make([]string, len(streams))
for i, s := range streams {
_, _, ch, cacheStream := factory.encoder.DecodeCacheTableName(s)
if cacheStream {
channelNames[i] = ch
}
}
return channelNames, nil
}
func (factory *ChannelFactory) GetChannel(ctx context.Context, tenantId uint32, projId uint32, channelName string) (*Channel, error) {
encStream, err := factory.encoder.EncodeCacheTableName(tenantId, projId, channelName)
if err != nil {
return nil, err
}
if ch, ok := factory.getChannel(encStream); ok {
return ch, nil
}
stream, err := factory.cache.GetStream(ctx, encStream)
if err != nil {
return nil, err
}
ch := NewChannel(encStream, stream)
factory.Lock()
factory.channels[encStream] = ch
factory.Unlock()
return ch, nil
}
func (factory *ChannelFactory) GetOrCreateChannel(ctx context.Context, tenantId uint32, projId uint32, channelName string) (*Channel, error) {
encStream, err := factory.encoder.EncodeCacheTableName(tenantId, projId, channelName)
if err != nil {
return nil, err
}
if ch, ok := factory.getChannel(encStream); ok {
return ch, nil
}
factory.Lock()
defer factory.Unlock()
ch, err := factory.getOrCreateChannelFromCache(ctx, encStream)
if err != nil {
return nil, err
}
factory.channels[encStream] = ch
return ch, nil
}
// CreateChannel will throw an error if stream already exists. Use CreateOrGet to create if not exists primitive.
func (factory *ChannelFactory) CreateChannel(ctx context.Context, tenantId uint32, projId uint32, channelName string) (*Channel, error) {
encStream, err := factory.encoder.EncodeCacheTableName(tenantId, projId, channelName)
if err != nil {
return nil, err
}
if _, ok := factory.getChannel(encStream); ok {
return nil, cache.ErrStreamAlreadyExists
}
factory.Lock()
defer factory.Unlock()
stream, err := factory.cache.CreateStream(ctx, encStream)
if err != nil {
return nil, err
}
ch := NewChannel(encStream, stream)
factory.channels[ch.encName] = ch
return ch, nil
}
func (factory *ChannelFactory) DeleteChannel(ctx context.Context, ch *Channel) {
factory.Lock()
defer factory.Unlock()
ch.Close(ctx)
delete(factory.channels, ch.encName)
}