-
Notifications
You must be signed in to change notification settings - Fork 2
/
tail_tracker.go
226 lines (182 loc) · 4.88 KB
/
tail_tracker.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
package storage
import (
"fmt"
"github.com/ActiveState/log"
"sync"
"sync/atomic"
"time"
)
type Tracker interface {
Update(instKey string, childKey string, childVal int64)
LoadTailers()
Remove(key string)
RegisterInstance(instKey string)
InitializeChildNode(instKey string, childkey string, offSet int64)
Commit() error
StartSubmissionTimer(persistInterval time.Duration)
IsInstanceRegistered(instKey string) bool
IsChildNodeInitialized(instKey string, childkey string) bool
GetFileCachedOffset(instkey string, fname string) int64
getBuffer() ([]byte, error)
CleanUp(validIds map[string]bool)
}
type BoxedInt64 struct{ V int64 }
type TailNode map[string]*BoxedInt64
type Tailer struct {
Instances map[string]TailNode
}
type tracker struct {
storage Storage
Cached *Tailer // do not expose this, it should ONLY be updated via Tracker methods
mux *sync.Mutex
timerStopChan chan struct{} // used to send quit signal to time,
debug bool
}
func NewTracker(s Storage, debug bool) Tracker {
return &tracker{
storage: s,
mux: &sync.Mutex{},
Cached: &Tailer{
Instances: make(map[string]TailNode),
},
timerStopChan: make(chan struct{}),
debug: debug,
}
}
func (t *tracker) StartSubmissionTimer(persistInterval time.Duration) {
ticker := time.NewTicker(persistInterval)
go func() {
for {
select {
case <-ticker.C:
err := t.Commit()
if err != nil {
log.Fatal(err)
}
case <-t.timerStopChan:
ticker.Stop()
return
}
}
}()
}
func (t *tracker) RegisterInstance(instKey string) {
t.mux.Lock()
if _, instance_exist := t.Cached.Instances[instKey]; !instance_exist {
t.Cached.Instances[instKey] = TailNode{}
t.dumpState("Current Status")
}
t.mux.Unlock()
}
// this is mainly used for testing since we are not exposing Cached via interface
func (t *tracker) IsInstanceRegistered(instKey string) bool {
t.mux.Lock()
defer t.mux.Unlock()
_, exist := t.Cached.Instances[instKey]
return exist
}
func (t *tracker) IsChildNodeInitialized(instKey string, childkey string) bool {
var exist bool
t.mux.Lock()
if tailNode, instance_exist := t.Cached.Instances[instKey]; instance_exist {
if _, childNode_exist := tailNode[childkey]; childNode_exist {
exist = childNode_exist
}
}
t.mux.Unlock()
return exist
}
func (t *tracker) InitializeChildNode(instKey string, childkey string, offSet int64) {
t.mux.Lock()
if tailNode, instance_exist := t.Cached.Instances[instKey]; instance_exist {
if _, childNode_exist := tailNode[childkey]; !childNode_exist {
tailNode[childkey] = &BoxedInt64{V: offSet}
t.Cached.Instances[instKey] = tailNode
t.dumpState("Current Status")
}
}
t.mux.Unlock()
}
func (t *tracker) GetFileCachedOffset(instkey string, fname string) int64 {
var offset int64
t.mux.Lock()
defer t.mux.Unlock()
if tailNode, instance_exist := t.Cached.Instances[instkey]; instance_exist {
offset = atomic.LoadInt64(&tailNode[fname].V)
}
return offset
}
func (t *tracker) Update(instKey string, childKey string, childVal int64) {
if tailNode, instance_exist := t.Cached.Instances[instKey]; instance_exist {
if _, childNode_exist := tailNode[childKey]; childNode_exist {
atomic.StoreInt64(&tailNode[childKey].V, childVal)
}
}
}
func (t *tracker) CleanUp(validIds map[string]bool) {
cachedIds := make(map[string]bool)
t.mux.Lock()
for key := range t.Cached.Instances {
cachedIds[key] = true
}
instancesToRemove := getInvalidInstances(cachedIds, validIds)
for key := range instancesToRemove {
delete(t.Cached.Instances, key)
}
t.dumpState("Cleaned up")
t.mux.Unlock()
}
func (t *tracker) Remove(key string) {
if t.debug {
log.Infof("Removing the following key %s from cached instances", key)
}
t.mux.Lock()
delete(t.Cached.Instances, key)
t.mux.Unlock()
if err := t.Commit(); err != nil {
log.Fatal(err)
}
}
func (t *tracker) LoadTailers() {
t.mux.Lock()
_ = t.storage.Load(&t.Cached)
t.dumpState("Loaded")
t.mux.Unlock()
}
func (t *tracker) getBuffer() ([]byte, error) {
t.mux.Lock()
defer t.mux.Unlock()
return t.storage.Encode(t.Cached)
}
func (t *tracker) Commit() error {
bytes, err := t.getBuffer()
if err != nil {
return err
}
//t.dumpState("Storing")
err = t.storage.Write(bytes)
if err != nil {
return err
}
return nil
}
func (t *tracker) dumpState(ops string) {
// Important to wrap this in a mutex since it's accessing shared resource
if t.debug {
for k, v := range t.Cached.Instances {
message := fmt.Sprintf("[%s] ContainerId: %s", ops, k)
for fname, buffer := range v {
log.Infof(message+" File: %s --> TailOffset: %d", fname, buffer)
}
}
}
}
func getInvalidInstances(dockerIds map[string]bool, allInstances map[string]bool) map[string]bool {
invalidInstances := make(map[string]bool)
for id, _ := range dockerIds {
if _, exist := allInstances[id]; !exist {
invalidInstances[id] = true
}
}
return invalidInstances
}