forked from Cloud-Foundations/scotty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
249 lines (223 loc) · 5.58 KB
/
collector.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
package scotty
import (
"github.com/Symantec/scotty/hostid"
"github.com/Symantec/scotty/metrics"
"github.com/Symantec/scotty/sources"
"runtime"
"syscall"
"time"
)
const (
kPreferenceMemory = 100
)
var (
concurrentConnects = allowedConnectionCount()
connectSemaphore = make(chan bool, concurrentConnects)
concurrentPolls = allowedPollCount()
pollSemaphore = make(chan bool, concurrentPolls)
)
func allowedPollCount() uint {
numCpus := runtime.NumCPU()
if numCpus < 1 {
numCpus = 1
}
return uint(numCpus * 2)
}
func allowedConnectionCount() uint {
var rlim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
panic(err)
}
if rlim.Cur <= 50 {
return 1
}
return uint(rlim.Cur - 50)
}
func setConcurrentConnects(x uint) {
close(connectSemaphore)
concurrentConnects = x
connectSemaphore = make(chan bool, concurrentConnects)
}
func setConcurrentPolls(x uint) {
if pollSemaphore != nil {
close(pollSemaphore)
}
concurrentPolls = x
if x > 0 {
pollSemaphore = make(chan bool, concurrentPolls)
} else {
pollSemaphore = nil
}
}
func waitingToConnect(sweepStartTime time.Time) *State {
return &State{
timestamp: sweepStartTime,
sweepStartTime: sweepStartTime,
status: WaitingToConnect}
}
func (s *State) goToConnecting(t time.Time, connectorName string) *State {
result := *s
dur := t.Sub(result.timestamp)
result.timestamp = t
result.waitToConnectDuration = dur
result.connectorName = connectorName
result.status = Connecting
return &result
}
func (s *State) finishedConnecting(t time.Time, next Status) *State {
result := *s
dur := t.Sub(result.timestamp)
result.timestamp = t
result.connectDuration = dur
result.status = next
return &result
}
func (s *State) goToWaitingToPoll(t time.Time) *State {
return s.finishedConnecting(t, WaitingToPoll)
}
func (s *State) goToFailedToConnect(t time.Time) *State {
return s.finishedConnecting(t, FailedToConnect)
}
func (s *State) goToPolling(t time.Time) *State {
result := *s
dur := t.Sub(result.timestamp)
result.timestamp = t
result.waitToPollDuration = dur
result.status = Polling
return &result
}
func (s *State) finishedPolling(t time.Time, next Status) *State {
result := *s
dur := t.Sub(result.timestamp)
result.timestamp = t
result.pollDuration = dur
result.status = next
return &result
}
func (s *State) goToSynced(t time.Time) *State {
return s.finishedPolling(t, Synced)
}
func (s *State) goToFailedToPoll(t time.Time) *State {
return s.finishedPolling(t, FailedToPoll)
}
func newEndpoint(
hostId *hostid.HostID,
appName string,
connector sources.Connector) *Endpoint {
return &Endpoint{
hostId: hostId,
name: appName,
conn: sources.AsResourceConnector(connector),
onePollAtATime: make(chan bool, 1),
}
}
func (e *Endpoint) pollWithResource(
state *State,
logger Logger,
resource sources.Resource) bool {
state = state.goToConnecting(time.Now(), e.conn.Name())
e.logState(state, logger)
conn, err := e.conn.ResourceConnect(resource)
if err != nil {
state = state.goToFailedToConnect(time.Now())
e.logError(err, state, logger)
return false
}
defer conn.Close()
state = state.goToWaitingToPoll(time.Now())
e.logState(state, logger)
if pollSemaphore != nil {
pollSemaphore <- true
defer func() {
<-pollSemaphore
}()
}
state = state.goToPolling(time.Now())
e.logState(state, logger)
metrics, err := conn.Poll()
if err != nil {
state = state.goToFailedToPoll(time.Now())
e.logError(err, state, logger)
return false
}
e.logMetrics(metrics, state, logger)
return true
}
func (e *Endpoint) getResource(isTls bool, port uint) sources.Resource {
e.lock.Lock()
defer e.lock.Unlock()
if e.resource == nil || port != e.resourcePort || isTls != e.isTls {
e.resource = e.conn.NewResource(
e.hostId.ConnectID(), port, sources.Config{IsTls: isTls})
e.resourcePort = port
e.isTls = isTls
}
return e.resource
}
func (e *Endpoint) poll(sweepStartTime time.Time, isTls bool, port uint, logger Logger) {
select {
case e.onePollAtATime <- true:
state := waitingToConnect(sweepStartTime)
e.logState(state, logger)
connectSemaphore <- true
go func(state *State) {
defer func() {
<-e.onePollAtATime
}()
defer func() {
<-connectSemaphore
}()
e.pollWithResource(state, logger, e.getResource(isTls, port))
}(state)
default:
return
}
}
func (e *Endpoint) logState(state *State, logger Logger) {
oldState := e._logState(state)
if logger != nil {
logger.LogStateChange(e, oldState, state)
}
}
func (e *Endpoint) logMetrics(metrics metrics.List, state *State, logger Logger) {
syncTime := time.Now()
if logger != nil {
err := logger.LogResponse(e, metrics, syncTime)
if err != nil {
newState := state.goToFailedToPoll(syncTime)
e.logError(err, newState, logger)
return
}
}
newState := state.goToSynced(syncTime)
e.logNoError(newState, logger)
}
func (e *Endpoint) logNoError(state *State, logger Logger) {
oldState, hadError := e._setError(state, false)
if logger != nil {
logger.LogStateChange(e, oldState, state)
if hadError {
logger.LogError(e, nil, state)
}
}
}
func (e *Endpoint) logError(err error, state *State, logger Logger) {
oldState, _ := e._setError(state, true)
if logger != nil {
logger.LogStateChange(e, oldState, state)
logger.LogError(e, err, state)
}
}
func (e *Endpoint) _logState(state *State) *State {
result := e.state
e.state = state
return result
}
func (e *Endpoint) _setError(state *State, hasError bool) (
oldState *State, hadError bool) {
oldState = e.state
hadError = e.errored
e.state = state
e.errored = hasError
return
}