Skip to content

Commit c647721

Browse files
authored
Merge pull request #824 from openconfig/coll-target-state
reflect target last error in a consistent way
2 parents 6ddf6fe + 8ea13d2 commit c647721

File tree

4 files changed

+117
-68
lines changed

4 files changed

+117
-68
lines changed

pkg/api/target/subscribe.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,7 @@ func (t *Target) attemptSubscription(ctx context.Context, req *gnmi.SubscribeReq
210210
if isCancellationError(err) {
211211
return false
212212
}
213-
sendError(errCh, ctx, subscriptionName,
214-
fmt.Errorf("failed to create subscribe client, target='%s', retry in %s: %w",
215-
t.Config.Name, t.Config.RetryTimer, err))
213+
sendError(errCh, ctx, subscriptionName, err)
216214
return true
217215
}
218216

@@ -264,8 +262,6 @@ func (t *Target) handleSTREAMMode(nctx, ctx context.Context, client gnmi.GNMI_Su
264262
}
265263

266264
sendError(errCh, ctx, subscriptionName, err)
267-
sendError(errCh, ctx, subscriptionName,
268-
fmt.Errorf("retrying in %s", t.Config.RetryTimer))
269265
return true
270266
}
271267
return false
@@ -288,8 +284,6 @@ func (t *Target) handleONCEMode(nctx, ctx context.Context, client gnmi.GNMI_Subs
288284
return false
289285
}
290286

291-
sendError(errCh, ctx, subscriptionName,
292-
fmt.Errorf("retrying in %s", t.Config.RetryTimer))
293287
return true
294288
}
295289
return false
@@ -315,8 +309,8 @@ func (t *Target) handlePOLLMode(nctx, ctx context.Context, client gnmi.GNMI_Subs
315309
}
316310

317311
sendError(errCh, ctx, subscriptionName, err)
318-
sendError(errCh, ctx, subscriptionName,
319-
fmt.Errorf("retrying in %s", t.Config.RetryTimer))
312+
// sendError(errCh, ctx, subscriptionName,
313+
// fmt.Errorf("retrying in %s", t.Config.RetryTimer))
320314
return true
321315
}
322316
return false

pkg/api/target/target.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@ func (t *Target) Close() error {
281281
return nil
282282
}
283283

284+
// SubscribeClientStates returns current subscription states.
285+
// based on the SubscribeClients map.
286+
func (t *Target) SubscribeClientStates() map[string]bool {
287+
t.m.Lock()
288+
defer t.m.Unlock()
289+
if len(t.Subscriptions) == 0 {
290+
return nil
291+
}
292+
states := make(map[string]bool, len(t.Subscriptions))
293+
for name := range t.Subscriptions {
294+
_, ok := t.SubscribeClients[name]
295+
states[name] = ok
296+
}
297+
return states
298+
}
299+
284300
func (t *Target) ConnState() string {
285301
if t.conn == nil {
286302
return ""

pkg/collector/api/server/subscriptions.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,19 @@ func (s *Server) handleSubscriptionsGet(w http.ResponseWriter, r *http.Request)
169169
}
170170
// Collect all subscriptions from targets
171171
s.targetsManager.ForEach(func(mt *targets_manager.ManagedTarget) {
172-
for _, sub := range mt.T.Subscriptions {
173-
if subscriptionsMap[sub.Name] == nil {
174-
subscriptionsMap[sub.Name] = &SubscriptionResponse{
175-
Name: sub.Name,
176-
Config: sub,
172+
subStates := mt.T.SubscribeClientStates()
173+
for name, active := range subStates {
174+
if subscriptionsMap[name] == nil {
175+
subscriptionsMap[name] = &SubscriptionResponse{
176+
Name: name,
177177
Targets: make(map[string]*TargetStateInfo),
178178
}
179179
}
180-
181-
// Determine state for this subscription on this target
182180
state := "stopped"
183-
if _, ok := mt.T.SubscribeClients[sub.Name]; ok {
181+
if active {
184182
state = "running"
185183
}
186-
187-
subscriptionsMap[sub.Name].Targets[mt.Name] = &TargetStateInfo{
184+
subscriptionsMap[name].Targets[mt.Name] = &TargetStateInfo{
188185
Name: mt.Name,
189186
State: state,
190187
}
@@ -222,20 +219,18 @@ func (s *Server) handleSubscriptionsGet(w http.ResponseWriter, r *http.Request)
222219
Targets: make(map[string]*TargetStateInfo),
223220
}
224221
s.targetsManager.ForEach(func(mt *targets_manager.ManagedTarget) {
225-
for _, sub := range mt.T.Subscriptions {
226-
if sub.Name != id {
227-
continue
228-
}
229-
230-
// Determine state for this subscription on this target
231-
state := "stopped"
232-
if _, ok := mt.T.SubscribeClients[sub.Name]; ok {
233-
state = "running"
234-
}
235-
response.Targets[mt.Name] = &TargetStateInfo{
236-
Name: mt.Name,
237-
State: state,
238-
}
222+
subStates := mt.T.SubscribeClientStates()
223+
active, exists := subStates[id]
224+
if !exists {
225+
return
226+
}
227+
state := "stopped"
228+
if active {
229+
state = "running"
230+
}
231+
response.Targets[mt.Name] = &TargetStateInfo{
232+
Name: mt.Name,
233+
State: state,
239234
}
240235
})
241236
err = json.NewEncoder(w).Encode(response)

0 commit comments

Comments
 (0)