@@ -48,24 +48,24 @@ func (g *RingStreamUsageGatherer) GetStreamUsage(ctx context.Context, r GetStrea
48
48
49
49
// TODO(grobinson): Need to rename this to something more accurate.
50
50
func (g * RingStreamUsageGatherer ) forAllBackends (ctx context.Context , r GetStreamUsageRequest ) ([]GetStreamUsageResponse , error ) {
51
- replicaSet , err := g .ring .GetAllHealthy (LimitsRead )
51
+ rs , err := g .ring .GetAllHealthy (LimitsRead )
52
52
if err != nil {
53
53
return nil , err
54
54
}
55
- return g .forGivenReplicaSet (ctx , replicaSet , r )
55
+ return g .forGivenReplicaSet (ctx , rs , r )
56
56
}
57
57
58
- func (g * RingStreamUsageGatherer ) forGivenReplicaSet (ctx context.Context , replicaSet ring.ReplicationSet , r GetStreamUsageRequest ) ([]GetStreamUsageResponse , error ) {
59
- partitions , err := g .getConsumedPartitions (ctx , replicaSet )
58
+ func (g * RingStreamUsageGatherer ) forGivenReplicaSet (ctx context.Context , rs ring.ReplicationSet , r GetStreamUsageRequest ) ([]GetStreamUsageResponse , error ) {
59
+ partitions , err := g .getConsumedPartitions (ctx , rs )
60
60
if err != nil {
61
61
return nil , err
62
62
}
63
63
64
64
errg , ctx := errgroup .WithContext (ctx )
65
- responses := make ([]GetStreamUsageResponse , len (replicaSet .Instances ))
65
+ responses := make ([]GetStreamUsageResponse , len (rs .Instances ))
66
66
67
67
// TODO: We shouldn't query all instances since we know which instance holds which stream.
68
- for i , instance := range replicaSet .Instances {
68
+ for i , instance := range rs .Instances {
69
69
errg .Go (func () error {
70
70
client , err := g .pool .GetClientFor (instance .Addr )
71
71
if err != nil {
@@ -93,11 +93,17 @@ func (g *RingStreamUsageGatherer) forGivenReplicaSet(ctx context.Context, replic
93
93
return responses , nil
94
94
}
95
95
96
- func (g * RingStreamUsageGatherer ) getConsumedPartitions (ctx context.Context , replicaSet ring.ReplicationSet ) (map [string ][]int32 , error ) {
96
+ type getAssignedPartitionsResponse struct {
97
+ Addr string
98
+ Response * logproto.GetAssignedPartitionsResponse
99
+ }
100
+
101
+ func (g * RingStreamUsageGatherer ) getConsumedPartitions (ctx context.Context , rs ring.ReplicationSet ) (map [string ][]int32 , error ) {
97
102
errg , ctx := errgroup .WithContext (ctx )
98
- responses := make (map [string ]* logproto.GetAssignedPartitionsResponse )
103
+ responses := make ([]getAssignedPartitionsResponse , len (rs .Instances ))
104
+
99
105
// Get the partitions assigned to each instance.
100
- for _ , instance := range replicaSet .Instances {
106
+ for i , instance := range rs .Instances {
101
107
errg .Go (func () error {
102
108
client , err := g .pool .GetClientFor (instance .Addr )
103
109
if err != nil {
@@ -107,7 +113,9 @@ func (g *RingStreamUsageGatherer) getConsumedPartitions(ctx context.Context, rep
107
113
if err != nil {
108
114
return err
109
115
}
110
- responses [instance .Addr ] = resp
116
+ // No need for a mutex here as responses is a "Structured variable"
117
+ // as described in https://go.dev/ref/spec#Variables.
118
+ responses [i ] = getAssignedPartitionsResponse {Addr : instance .Addr , Response : resp }
111
119
return nil
112
120
})
113
121
}
@@ -122,11 +130,11 @@ func (g *RingStreamUsageGatherer) getConsumedPartitions(ctx context.Context, rep
122
130
// with the latest timestamp.
123
131
highestTimestamp := make (map [int32 ]int64 )
124
132
assigned := make (map [int32 ]string )
125
- for addr , resp := range responses {
126
- for partition , assignedAt := range resp .AssignedPartitions {
133
+ for _ , resp := range responses {
134
+ for partition , assignedAt := range resp .Response . AssignedPartitions {
127
135
if t := highestTimestamp [partition ]; t < assignedAt {
128
136
highestTimestamp [partition ] = assignedAt
129
- assigned [partition ] = addr
137
+ assigned [partition ] = resp . Addr
130
138
}
131
139
}
132
140
}
0 commit comments