/
logstream_g.go
228 lines (189 loc) · 5.05 KB
/
logstream_g.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
/*
* Copyright 2022 Holoinsight Project Authors. Licensed under Apache-2.0.
*/
package logstream
import (
"fmt"
"github.com/traas-stack/holoinsight-agent/pkg/logger"
"github.com/traas-stack/holoinsight-agent/pkg/transfer"
"go.uber.org/zap"
"sync"
"sync/atomic"
"time"
)
type (
GLogStream struct {
Key string
Mutex sync.RWMutex
Cache sync.Map
Listeners []Listener
Cursor int64
PendingBytes int64
PendingReads int32
sub SubLogStream
}
SubLogStream interface {
transfer.StatefulInput
Start()
Stop()
Read(resp *ReadResponse) error
CreateResponse(cursor int64) *ReadResponse
}
)
var (
_ LogStream = &GLogStream{}
)
func (f *GLogStream) GetKey() string {
return f.Key
}
func (f *GLogStream) Start() {
f.sub.Start()
}
func (f *GLogStream) Stop() {
f.sub.Stop()
}
func (f *GLogStream) UpdatePending(resp *ReadResponse, add bool) {
if add {
atomic.AddInt64(&f.PendingBytes, resp.Bytes)
atomic.AddInt32(&f.PendingReads, 1)
} else {
atomic.AddInt64(&f.PendingBytes, -resp.Bytes)
atomic.AddInt32(&f.PendingReads, -1)
}
}
func (f *GLogStream) getFromCache(cursor int64) *ReadResponse {
if c, ok := f.Cache.Load(cursor); ok {
cached := c.(*cachedRead)
resp := cached.resp
if atomic.AddInt32(&cached.pendingReads, -1) == 0 {
f.Cache.Delete(cursor)
f.UpdatePending(resp, false)
}
if !resp.HasMore {
fc := atomic.LoadInt64(&f.Cursor)
if resp.Cursor+1 < fc {
resp.HasMore = true
}
}
return resp
} else {
return nil
}
}
func (f *GLogStream) Read(reqCursor int64) (*ReadResponse, int64, error) {
//logger.Debugz("debug read",
// zap.Uintptr("f", uintptr(unsafe.Pointer(f))),
// zap.String("key", f.Key),
// zap.Int64("cursor", reqCursor),
// zap.Int("listeners", len(f.Listeners)))
// check cache
if c := f.getFromCache(reqCursor); c != nil {
return c, c.Cursor + 1, c.error
}
f.Mutex.Lock()
defer f.Mutex.Unlock()
// double check cache
if c := f.getFromCache(reqCursor); c != nil {
return c, c.Cursor + 1, c.error
}
if reqCursor != f.Cursor {
return nil, f.Cursor, fmt.Errorf("stale cursor request=[%d] current=[%d]", reqCursor, f.Cursor)
}
resp := f.sub.CreateResponse(f.Cursor)
if resp.IOStartTime.IsZero() {
resp.IOStartTime = time.Now()
}
err := f.sub.Read(resp)
if resp.IOEndTime.IsZero() {
resp.IOEndTime = time.Now()
}
resp.error = err
// save to cache
if len(f.Listeners) > 1 {
f.UpdatePending(resp, true)
f.Cache.Store(resp.Cursor, &cachedRead{
pendingReads: int32(len(f.Listeners) - 1),
resp: resp,
})
}
atomic.StoreInt64(&f.Cursor, f.Cursor+1)
f.fire(f.Cursor - 1)
return resp, resp.Cursor + 1, resp.error
}
func (f *GLogStream) fire(cursor int64) {
listeners := f.Listeners
for _, l := range listeners {
l.Changed(f, cursor)
}
}
func (f *GLogStream) AddListener(listener Listener) int64 {
f.Mutex.Lock()
defer f.Mutex.Unlock()
f.Listeners = append(f.Listeners, listener)
return f.Cursor
}
func (f *GLogStream) RemoveListener(listener Listener, cursor int64) {
f.Mutex.Lock()
defer f.Mutex.Unlock()
listeners := make([]Listener, 0, len(f.Listeners))
for _, l := range f.Listeners {
// The implementations of Listener must not be empty structs.
// They will result in same object address when their instances are converted to Listener interface,
// which leads to the following 'l!=listener' always returns false
if l != listener {
listeners = append(listeners, l)
}
}
if x := len(f.Listeners) - len(listeners); x > 1 {
logger.Errorz("remove multi listeners", zap.String("key", f.Key), zap.Int("old", len(f.Listeners)), zap.Int("new", len(listeners)), zap.Stack("stack"))
}
f.Listeners = listeners
for i := cursor; i < f.Cursor; i++ {
f.getFromCache(i)
}
}
func (f *GLogStream) Clean() {
f.Mutex.Lock()
defer f.Mutex.Unlock()
expiredTime := time.Now().Add(-expireTimeout)
f.Cache.Range(func(key, value interface{}) bool {
cached := value.(*cachedRead)
resp := cached.resp
if resp.IOStartTime.Before(expiredTime) {
f.Cache.Delete(key)
f.UpdatePending(resp, false)
logger.Errorz("clean cursor cache", //
zap.String("key", f.Key), //
zap.Int64("cursor", resp.Cursor), //
zap.Int32("pendingReads", cached.pendingReads), //
zap.Time("ioStartTime", resp.IOStartTime)) //
}
return true
})
}
func (f *GLogStream) Stat() Stat {
return Stat{
LatestCursor: atomic.LoadInt64(&f.Cursor),
PendingBytes: atomic.LoadInt64(&f.PendingBytes),
PendingReads: atomic.LoadInt32(&f.PendingReads),
}
}
func (f *GLogStream) LoadReadState(cursor int64) error {
if cursor > f.Cursor {
return fmt.Errorf("invalid cursor now=[%d] requested=[%d]", f.Cursor, cursor)
}
for index := cursor; index < f.Cursor; index++ {
cached, ok := f.Cache.Load(index)
if !ok {
return fmt.Errorf("no cache for cursor now=[%d] requested=[%d]", f.Cursor, cursor)
}
cached.(*cachedRead).pendingReads++
}
return nil
}
func (f *GLogStream) SaveState() (interface{}, error) {
return f.sub.SaveState()
}
func (f *GLogStream) LoadState(i interface{}) error {
return f.sub.LoadState(i)
}