BumpTo can be used to make Zero allocate UIDs up to this given number. Attempts are made to ensure all future allocations of UIDs be higher than this one, but results are not guaranteed.
(uid uint64)
| 305 | // BumpTo can be used to make Zero allocate UIDs up to this given number. Attempts are made to |
| 306 | // ensure all future allocations of UIDs be higher than this one, but results are not guaranteed. |
| 307 | func (m *XidMap) BumpTo(uid uint64) { |
| 308 | // If we have a cluster that cannot lease out new UIDs because it has already leased upto its |
| 309 | // max limit. Now, we try to live load the data with the given UIDs and the AssignIds complains |
| 310 | // that the limit has reached. Hence, update the xidmap's maxSeenUid and make progress. |
| 311 | updateLease := func(msg string) { |
| 312 | if !strings.Contains(msg, "limit has reached. currMax:") { |
| 313 | return |
| 314 | } |
| 315 | matches := maxLeaseRegex.FindAllStringSubmatch(msg, 1) |
| 316 | if len(matches) == 0 { |
| 317 | return |
| 318 | } |
| 319 | maxUidLeased, err := strconv.ParseUint(matches[0][1], 10, 64) |
| 320 | if err != nil { |
| 321 | glog.Errorf("While parsing currMax %+v", err) |
| 322 | return |
| 323 | } |
| 324 | m.updateMaxSeen(maxUidLeased) |
| 325 | } |
| 326 | |
| 327 | for { |
| 328 | curMax := atomic.LoadUint64(&m.maxUidSeen) |
| 329 | if uid <= curMax { |
| 330 | return |
| 331 | } |
| 332 | glog.V(1).Infof("Bumping up to %v", uid) |
| 333 | num := x.Max(uid-curMax, 1e4) |
| 334 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 335 | ctx = m.attachNamespace(ctx) |
| 336 | |
| 337 | var err error |
| 338 | var assigned *pb.AssignedIds |
| 339 | if m.zc == nil { |
| 340 | assigned = &pb.AssignedIds{} |
| 341 | assigned.StartId, assigned.EndId, err = m.dg.AllocateUIDs(ctx, num) |
| 342 | } else { |
| 343 | assigned, err = m.zc.AssignIds(ctx, &pb.Num{Val: num, Type: pb.Num_UID}) |
| 344 | } |
| 345 | cancel() |
| 346 | if err == nil { |
| 347 | glog.V(1).Infof("Requested bump: %d. Got assigned: %v", uid, assigned) |
| 348 | m.updateMaxSeen(assigned.EndId) |
| 349 | return |
| 350 | } |
| 351 | updateLease(err.Error()) |
| 352 | glog.Errorf("While requesting AssignUids(%d): %v", num, err) |
| 353 | if x.IsJwtExpired(err) { |
| 354 | if err := m.relogin(); err != nil { |
| 355 | glog.Errorf("While trying to relogin: %v", err) |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // AllocateUid gives a single uid without creating an xid to uid mapping. |
| 362 | func (m *XidMap) AllocateUid() uint64 { |