MCPcopy
hub / github.com/dgraph-io/dgraph / AssignIds

Method AssignIds

dgraph/cmd/zero/assign.go:176–279  ·  view source on GitHub ↗

AssignIds is used to assign new ids (UIDs, NsIDs) by communicating with the leader of the RAFT group responsible for handing out ids. If bump is set to true in the request then the lease for the given id type is bumped to num.Val and {startId, endId} of the newly leased ids in the process of bump is

(ctx context.Context, num *pb.Num)

Source from the content-addressed store, hash-verified

174// lease for the given id type is bumped to num.Val and {startId, endId} of the newly leased ids
175// in the process of bump is returned.
176func (s *Server) AssignIds(ctx context.Context, num *pb.Num) (*pb.AssignedIds, error) {
177 if ctx.Err() != nil {
178 return &emptyAssignedIds, ctx.Err()
179 }
180 ctx, span := otel.Tracer("").Start(ctx, "Zero.AssignIds")
181 defer span.End()
182
183 rateLimit := func() error {
184 if s.rateLimiter == nil {
185 return nil
186 }
187 if num.GetType() != pb.Num_UID {
188 // We only rate limit lease of UIDs.
189 return nil
190 }
191 ns, err := x.ExtractNamespace(ctx)
192 if err != nil || ns == x.RootNamespace {
193 // There is no rate limiting for RootNamespace. Also, we allow the requests which do
194 // not contain namespace into context.
195 return nil
196 }
197 if num.Val > opts.limiterConfig.UidLeaseLimit {
198 return errors.Errorf("Requested UID lease(%d) is greater than allowed(%d).",
199 num.Val, opts.limiterConfig.UidLeaseLimit)
200 }
201
202 if !s.rateLimiter.Allow(ns, int64(num.Val)) {
203 // Return error after random delay.
204 //nolint:gosec // random generator in closed set does not require cryptographic precision
205 delay := rand.Intn(int(opts.limiterConfig.RefillAfter))
206 time.Sleep(time.Duration(delay))
207 return errors.Errorf("Cannot lease UID because UID lease for the namespace %#x is "+
208 "exhausted. Please retry after some time.", ns)
209 }
210 return nil
211 }
212
213 reply := &emptyAssignedIds
214 lease := func() error {
215 var err error
216 if s.Node.AmLeader() {
217 if err := rateLimit(); err != nil {
218 return err
219 }
220 span.SetAttributes(attribute.String("tablet", "predicate"))
221 span.AddEvent(fmt.Sprintf("Zero leader leasing %d ids", num.GetVal()))
222 reply, err = s.lease(ctx, num)
223 return err
224 }
225 span.SetAttributes(attribute.String("Not Zero leader", "true"))
226 span.AddEvent("Not Zero leader")
227 // I'm not the leader and this request was forwarded to me by a peer, who thought I'm the
228 // leader.
229 if num.Forwarded {
230 return errors.Errorf("Invalid Zero received AssignIds request forward. Please retry")
231 }
232 // This is an original request. Forward it to the leader.
233 pl := s.Leader(0)

Callers

nothing calls this directly

Calls 15

leaseMethod · 0.95
LeaderMethod · 0.95
AssignIdsMethod · 0.95
ExtractNamespaceFunction · 0.92
NewZeroClientFunction · 0.92
AllowMethod · 0.80
GetBumpMethod · 0.80
StartMethod · 0.65
GetMethod · 0.65
GetTypeMethod · 0.45
ErrorfMethod · 0.45
AmLeaderMethod · 0.45

Tested by

no test coverage detected