-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
151 lines (123 loc) · 3.01 KB
/
manager.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
package main
import (
"context"
"fmt"
"log"
"sync"
"time"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"
)
type LogstreamManagerClient interface {
LogstreamClient
ListLogGroups(request *model.ListLogGroupsRequest) (*model.ListLogGroupsResponse, error)
ListLogStream(request *model.ListLogStreamRequest) (*model.ListLogStreamResponse, error)
}
type LogstreamManager struct {
client LogstreamManagerClient
streams map[LogstreamID]*Logstream
queue chan *Logstream
maxEndFromNow time.Duration
maxFetchRange time.Duration
minFetchRange time.Duration
maxLag time.Duration
}
func (m LogstreamManager) Start(ctx context.Context, routine int) (err error) {
if err = m.SyncStreamList(ctx); err != nil {
return fmt.Errorf("fail to initialy sync log group and streams: %w", err)
}
wg := sync.WaitGroup{}
wg.Add(routine + 1)
defer wg.Wait()
go func() {
defer wg.Done()
for {
select {
case <-time.After(time.Minute):
case <-ctx.Done():
return
}
err := m.SyncStreamList(ctx)
if err != nil {
log.Fatalf("[WARN] fail to sync log groups and streams: %v\n", err)
}
}
}()
for i := 0; i < routine; i++ {
go func() {
defer wg.Done()
var s *Logstream
for {
select {
case s = <-m.queue:
case <-ctx.Done():
return
}
if _, ok := m.streams[s.ID()]; !ok {
continue
}
s.SkiptoCatchUp(m.maxLag)
end := time.Now().Add(-m.maxEndFromNow)
if e := s.start.Add(m.maxFetchRange); e.Before(end) {
end = e
}
err := s.Stream(ctx, end)
if err != nil {
log.Printf("[WARN] got error when streaming: %v\n", err)
}
go m.Queue(ctx, s)
}
}()
}
return
}
func (m LogstreamManager) SyncStreamList(ctx context.Context) (err error) {
groups, err := m.client.ListLogGroups(&model.ListLogGroupsRequest{})
if err != nil {
return fmt.Errorf("fail to fetch groups: %w", err)
}
fetched := map[LogstreamID]struct{}{}
for _, group := range *groups.LogGroups {
req := &model.ListLogStreamRequest{LogGroupId: group.LogGroupId}
streams, err := m.client.ListLogStream(req)
if err != nil {
return fmt.Errorf("fail to fetch streams: %w", err)
}
for _, stream := range *streams.LogStreams {
if _, ok := stream.Tag[streamExclusionTag]; ok {
log.Printf("[WARN] stream '%s' of group '%s' is excluded.\n", stream.LogStreamName, group.LogGroupName)
continue
}
s := NewLogstream(m.client, group, stream, stdout)
fetched[s.ID()] = struct{}{}
if v, ok := m.streams[s.ID()]; !ok {
m.streams[s.ID()] = s
go m.Queue(ctx, s)
} else {
v.UpdateTags(group, stream)
}
}
}
for k, s := range m.streams {
if _, ok := fetched[k]; ok {
continue
}
s.terminated = true
delete(m.streams, k)
}
return
}
func (m LogstreamManager) Queue(ctx context.Context, s *Logstream) {
d := s.start.Sub(time.Now().
Add(-m.maxEndFromNow).
Add(-m.minFetchRange))
select {
case <-time.After(d):
case <-ctx.Done():
return
}
select {
case m.queue <- s:
case <-ctx.Done():
return
}
}