MCPcopy Create free account
hub / github.com/cortexproject/cortex / getOrAddQueue

Method getOrAddQueue

pkg/scheduler/queue/user_queues.go:126–208  ·  view source on GitHub ↗

Returns existing or new queue for user. MaxQueriers is used to compute which queriers should handle requests for this user. If maxQueriers is <= 0, all queriers can handle this user's requests. If maxQueriers has changed since the last call, queriers for this are recomputed. It's also responsible to

(userID string, maxQueriers int)

Source from the content-addressed store, hash-verified

124// If maxQueriers has changed since the last call, queriers for this are recomputed.
125// It's also responsible to store user configs and update the attributes if related configs have changed.
126func (q *queues) getOrAddQueue(userID string, maxQueriers int) userRequestQueue {
127 // Empty user is not allowed, as that would break our users list ("" is used for free spot).
128 if userID == "" {
129 return nil
130 }
131
132 if maxQueriers < 0 {
133 maxQueriers = 0
134 }
135
136 q.queuesMx.Lock()
137 defer q.queuesMx.Unlock()
138
139 uq := q.userQueues[userID]
140 priorityEnabled := q.limits.QueryPriority(userID).Enabled
141 maxOutstanding := q.limits.MaxOutstandingPerTenant(userID)
142 priorityList := getPriorityList(q.limits.QueryPriority(userID), maxQueriers)
143
144 if uq == nil {
145 uq = &userQueue{
146 seed: util.ShuffleShardSeed(userID, ""),
147 index: -1,
148 }
149
150 uq.queue = q.createUserRequestQueue(userID)
151 uq.maxOutstanding = q.limits.MaxOutstandingPerTenant(userID)
152 q.userQueues[userID] = uq
153
154 // Add user to the list of users... find first free spot, and put it there.
155 for ix, u := range q.users {
156 if u == "" {
157 uq.index = ix
158 q.users[ix] = userID
159 break
160 }
161 }
162
163 // ... or add to the end.
164 if uq.index < 0 {
165 uq.index = len(q.users)
166 q.users = append(q.users, userID)
167 }
168 } else if (uq.priorityEnabled != priorityEnabled) || (!priorityEnabled && uq.maxOutstanding != maxOutstanding) {
169 tmpQueue := q.createUserRequestQueue(userID)
170
171 // flush to new queue
172 // If the new limit is lower than the current number of requests,
173 // the excess requests (newest ones) will be dropped to prevent deadlocks.
174 for (uq.queue.length() > 0) && (tmpQueue.length() < maxOutstanding) {
175 tmpQueue.enqueueRequest(uq.queue.dequeueRequest(0, false))
176 }
177
178 uq.queue = tmpQueue
179 uq.maxOutstanding = q.limits.MaxOutstandingPerTenant(userID)
180 uq.priorityEnabled = priorityEnabled
181 }
182
183 if uq.maxQueriers != maxQueriers {

Calls 10

ShuffleShardSeedFunction · 0.92
getPriorityListFunction · 0.85
shuffleQueriersForUserFunction · 0.85
hasPriorityListChangedFunction · 0.85
QueryPriorityMethod · 0.65
lengthMethod · 0.65
enqueueRequestMethod · 0.65
dequeueRequestMethod · 0.65