1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.curoverse.com/arvados.git/sdk/go/arvados"
21 "git.curoverse.com/arvados.git/sdk/go/keepclient"
24 // CheckConfig returns an error if anything is wrong with the given
25 // config and runOptions.
26 func CheckConfig(config Config, runOptions RunOptions) error {
27 if len(config.KeepServiceList.Items) > 0 && config.KeepServiceTypes != nil {
28 return fmt.Errorf("cannot specify both KeepServiceList and KeepServiceTypes in config")
30 if !runOptions.Once && config.RunPeriod == arvados.Duration(0) {
31 return fmt.Errorf("you must either use the -once flag, or specify RunPeriod in config")
36 // Balancer compares the contents of keepstore servers with the
37 // collections stored in Arvados, and issues pull/trash requests
38 // needed to get (closer to) the optimal data layout.
40 // In the optimal data layout: every data block referenced by a
41 // collection is replicated at least as many times as desired by the
42 // collection; there are no unreferenced data blocks older than
43 // BlobSignatureTTL; and all N existing replicas of a given data block
44 // are in the N best positions in rendezvous probe order.
45 type Balancer struct {
47 KeepServices map[string]*KeepService
48 DefaultReplication int
55 mountsByClass map[string]map[*KeepMount]bool
57 serviceRoots map[string]string
63 // Run performs a balance operation using the given config and
64 // runOptions, and returns RunOptions suitable for passing to a
65 // subsequent balance operation.
67 // Run should only be called once on a given Balancer object.
71 // runOptions, err = (&Balancer{}).Run(config, runOptions)
72 func (bal *Balancer) Run(config Config, runOptions RunOptions) (nextRunOptions RunOptions, err error) {
73 nextRunOptions = runOptions
75 bal.Dumper = runOptions.Dumper
76 bal.Logger = runOptions.Logger
77 if bal.Logger == nil {
78 bal.Logger = log.New(os.Stderr, "", log.LstdFlags)
81 defer timeMe(bal.Logger, "Run")()
83 if len(config.KeepServiceList.Items) > 0 {
84 err = bal.SetKeepServices(config.KeepServiceList)
86 err = bal.DiscoverKeepServices(&config.Client, config.KeepServiceTypes)
92 for _, srv := range bal.KeepServices {
93 err = srv.discoverMounts(&config.Client)
99 if err = bal.CheckSanityEarly(&config.Client); err != nil {
102 rs := bal.rendezvousState()
103 if runOptions.CommitTrash && rs != runOptions.SafeRendezvousState {
104 if runOptions.SafeRendezvousState != "" {
105 bal.logf("notice: KeepServices list has changed since last run")
107 bal.logf("clearing existing trash lists, in case the new rendezvous order differs from previous run")
108 if err = bal.ClearTrashLists(&config.Client); err != nil {
111 // The current rendezvous state becomes "safe" (i.e.,
112 // OK to compute changes for that state without
113 // clearing existing trash lists) only now, after we
114 // succeed in clearing existing trash lists.
115 nextRunOptions.SafeRendezvousState = rs
117 if err = bal.GetCurrentState(&config.Client, config.CollectionBatchSize, config.CollectionBuffers); err != nil {
120 bal.ComputeChangeSets()
121 bal.PrintStatistics()
122 if err = bal.CheckSanityLate(); err != nil {
125 if runOptions.CommitPulls {
126 err = bal.CommitPulls(&config.Client)
128 // Skip trash if we can't pull. (Too cautious?)
132 if runOptions.CommitTrash {
133 err = bal.CommitTrash(&config.Client)
138 // SetKeepServices sets the list of KeepServices to operate on.
139 func (bal *Balancer) SetKeepServices(srvList arvados.KeepServiceList) error {
140 bal.KeepServices = make(map[string]*KeepService)
141 for _, srv := range srvList.Items {
142 bal.KeepServices[srv.UUID] = &KeepService{
144 ChangeSet: &ChangeSet{},
150 // DiscoverKeepServices sets the list of KeepServices by calling the
151 // API to get a list of all services, and selecting the ones whose
152 // ServiceType is in okTypes.
153 func (bal *Balancer) DiscoverKeepServices(c *arvados.Client, okTypes []string) error {
154 bal.KeepServices = make(map[string]*KeepService)
155 ok := make(map[string]bool)
156 for _, t := range okTypes {
159 return c.EachKeepService(func(srv arvados.KeepService) error {
160 if ok[srv.ServiceType] {
161 bal.KeepServices[srv.UUID] = &KeepService{
163 ChangeSet: &ChangeSet{},
166 bal.logf("skipping %v with service type %q", srv.UUID, srv.ServiceType)
172 // CheckSanityEarly checks for configuration and runtime errors that
173 // can be detected before GetCurrentState() and ComputeChangeSets()
176 // If it returns an error, it is pointless to run GetCurrentState or
177 // ComputeChangeSets: after doing so, the statistics would be
178 // meaningless and it would be dangerous to run any Commit methods.
179 func (bal *Balancer) CheckSanityEarly(c *arvados.Client) error {
180 u, err := c.CurrentUser()
182 return fmt.Errorf("CurrentUser(): %v", err)
184 if !u.IsActive || !u.IsAdmin {
185 return fmt.Errorf("current user (%s) is not an active admin user", u.UUID)
187 for _, srv := range bal.KeepServices {
188 if srv.ServiceType == "proxy" {
189 return fmt.Errorf("config error: %s: proxy servers cannot be balanced", srv)
195 // rendezvousState returns a fingerprint (e.g., a sorted list of
196 // UUID+host+port) of the current set of keep services.
197 func (bal *Balancer) rendezvousState() string {
198 srvs := make([]string, 0, len(bal.KeepServices))
199 for _, srv := range bal.KeepServices {
200 srvs = append(srvs, srv.String())
203 return strings.Join(srvs, "; ")
206 // ClearTrashLists sends an empty trash list to each keep
207 // service. Calling this before GetCurrentState avoids races.
209 // When a block appears in an index, we assume that replica will still
210 // exist after we delete other replicas on other servers. However,
211 // it's possible that a previous rebalancing operation made different
212 // decisions (e.g., servers were added/removed, and rendezvous order
213 // changed). In this case, the replica might already be on that
214 // server's trash list, and it might be deleted before we send a
215 // replacement trash list.
217 // We avoid this problem if we clear all trash lists before getting
218 // indexes. (We also assume there is only one rebalancing process
219 // running at a time.)
220 func (bal *Balancer) ClearTrashLists(c *arvados.Client) error {
221 for _, srv := range bal.KeepServices {
222 srv.ChangeSet = &ChangeSet{}
224 return bal.CommitTrash(c)
227 // GetCurrentState determines the current replication state, and the
228 // desired replication level, for every block that is either
229 // retrievable or referenced.
231 // It determines the current replication state by reading the block index
232 // from every known Keep service.
234 // It determines the desired replication level by retrieving all
235 // collection manifests in the database (API server).
237 // It encodes the resulting information in BlockStateMap.
238 func (bal *Balancer) GetCurrentState(c *arvados.Client, pageSize, bufs int) error {
239 defer timeMe(bal.Logger, "GetCurrentState")()
240 bal.BlockStateMap = NewBlockStateMap()
242 dd, err := c.DiscoveryDocument()
246 bal.DefaultReplication = dd.DefaultCollectionReplication
247 bal.MinMtime = time.Now().UnixNano() - dd.BlobSignatureTTL*1e9
249 errs := make(chan error, 2+len(bal.KeepServices))
250 wg := sync.WaitGroup{}
252 // Start one goroutine for each KeepService: retrieve the
253 // index, and add the returned blocks to BlockStateMap.
254 for _, srv := range bal.KeepServices {
256 go func(srv *KeepService) {
258 bal.logf("%s: retrieve indexes", srv)
259 for _, mount := range srv.mounts {
260 bal.logf("%s: retrieve index", mount)
261 idx, err := srv.IndexMount(c, mount.UUID, "")
263 errs <- fmt.Errorf("%s: retrieve index: %v", mount, err)
267 // Some other goroutine encountered an
268 // error -- any further effort here
272 bal.logf("%s: add %d replicas to map", mount, len(idx))
273 bal.BlockStateMap.AddReplicas(mount, idx)
274 bal.logf("%s: done", mount)
276 bal.logf("%s: done", srv)
280 // collQ buffers incoming collections so we can start fetching
281 // the next page without waiting for the current page to
282 // finish processing.
283 collQ := make(chan arvados.Collection, bufs)
285 // Start a goroutine to process collections. (We could use a
286 // worker pool here, but even with a single worker we already
287 // process collections much faster than we can retrieve them.)
291 for coll := range collQ {
292 err := bal.addCollection(coll)
303 // Start a goroutine to retrieve all collections from the
304 // Arvados database and send them to collQ for processing.
308 err = EachCollection(c, pageSize,
309 func(coll arvados.Collection) error {
312 // some other GetCurrentState
313 // error happened: no point
316 return fmt.Errorf("")
319 }, func(done, total int) {
320 bal.logf("collections: %d/%d", done, total)
335 func (bal *Balancer) addCollection(coll arvados.Collection) error {
336 blkids, err := coll.SizedDigests()
339 bal.errors = append(bal.errors, fmt.Errorf("%v: %v", coll.UUID, err))
343 repl := bal.DefaultReplication
344 if coll.ReplicationDesired != nil {
345 repl = *coll.ReplicationDesired
347 debugf("%v: %d block x%d", coll.UUID, len(blkids), repl)
348 bal.BlockStateMap.IncreaseDesired(coll.StorageClassesDesired, repl, blkids)
352 // ComputeChangeSets compares, for each known block, the current and
353 // desired replication states. If it is possible to get closer to the
354 // desired state by copying or deleting blocks, it adds those changes
355 // to the relevant KeepServices' ChangeSets.
357 // It does not actually apply any of the computed changes.
358 func (bal *Balancer) ComputeChangeSets() {
359 // This just calls balanceBlock() once for each block, using a
360 // pool of worker goroutines.
361 defer timeMe(bal.Logger, "ComputeChangeSets")()
362 bal.setupLookupTables()
364 type balanceTask struct {
365 blkid arvados.SizedDigest
368 nWorkers := 1 + runtime.NumCPU()
369 todo := make(chan balanceTask, nWorkers)
370 results := make(chan balanceResult, 16)
371 var wg sync.WaitGroup
372 for i := 0; i < nWorkers; i++ {
375 for work := range todo {
376 results <- bal.balanceBlock(work.blkid, work.blk)
381 bal.BlockStateMap.Apply(func(blkid arvados.SizedDigest, blk *BlockState) {
392 bal.collectStatistics(results)
395 func (bal *Balancer) setupLookupTables() {
396 bal.serviceRoots = make(map[string]string)
397 bal.classes = []string{"default"}
398 bal.mountsByClass = map[string]map[*KeepMount]bool{"default": {}}
400 for _, srv := range bal.KeepServices {
401 bal.serviceRoots[srv.UUID] = srv.UUID
402 for _, mnt := range srv.mounts {
405 // All mounts on a read-only service are
406 // effectively read-only.
407 mnt.ReadOnly = mnt.ReadOnly || srv.ReadOnly
409 if len(mnt.StorageClasses) == 0 {
410 bal.mountsByClass["default"][mnt] = true
413 for _, class := range mnt.StorageClasses {
414 if mbc := bal.mountsByClass[class]; mbc == nil {
415 bal.classes = append(bal.classes, class)
416 bal.mountsByClass[class] = map[*KeepMount]bool{mnt: true}
423 // Consider classes in lexicographic order to avoid flapping
424 // between balancing runs. The outcome of the "prefer a mount
425 // we're already planning to use for a different storage
426 // class" case in balanceBlock depends on the order classes
428 sort.Strings(bal.classes)
438 var changeName = map[int]string{
441 changeTrash: "trash",
445 type balanceResult struct {
447 blkid arvados.SizedDigest
452 // balanceBlock compares current state to desired state for a single
453 // block, and makes the appropriate ChangeSet calls.
454 func (bal *Balancer) balanceBlock(blkid arvados.SizedDigest, blk *BlockState) balanceResult {
455 debugf("balanceBlock: %v %+v", blkid, blk)
458 mnt *KeepMount // never nil
459 repl *Replica // replica already stored here (or nil)
460 want bool // we should pull/leave a replica here
463 // Build a list of all slots (one per mounted volume).
464 slots := make([]slot, 0, bal.mounts)
465 for _, srv := range bal.KeepServices {
466 for _, mnt := range srv.mounts {
468 for r := range blk.Replicas {
469 if blk.Replicas[r].KeepMount == mnt {
470 repl = &blk.Replicas[r]
473 // Initial value of "want" is "have, and can't
474 // delete". These untrashable replicas get
475 // prioritized when sorting slots: otherwise,
476 // non-optimal readonly copies would cause us
478 slots = append(slots, slot{
481 want: repl != nil && (mnt.ReadOnly || repl.Mtime >= bal.MinMtime),
486 uuids := keepclient.NewRootSorter(bal.serviceRoots, string(blkid[:32])).GetSortedRoots()
487 srvRendezvous := make(map[*KeepService]int, len(uuids))
488 for i, uuid := range uuids {
489 srv := bal.KeepServices[uuid]
490 srvRendezvous[srv] = i
493 // Below we set underreplicated=true if we find any storage
494 // class that's currently underreplicated -- in that case we
495 // won't want to trash any replicas.
496 underreplicated := false
498 unsafeToDelete := make(map[int64]bool, len(slots))
499 for _, class := range bal.classes {
500 desired := blk.Desired[class]
504 // Sort the slots by desirability.
505 sort.Slice(slots, func(i, j int) bool {
506 si, sj := slots[i], slots[j]
507 if classi, classj := bal.mountsByClass[class][si.mnt], bal.mountsByClass[class][sj.mnt]; classi != classj {
508 // Prefer a mount that satisfies the
510 return bal.mountsByClass[class][si.mnt]
511 } else if wanti, wantj := si.want, si.want; wanti != wantj {
512 // Prefer a mount that will have a
513 // replica no matter what we do here
514 // -- either because it already has an
515 // untrashable replica, or because we
516 // already need it to satisfy a
517 // different storage class.
519 } else if orderi, orderj := srvRendezvous[si.mnt.KeepService], srvRendezvous[sj.mnt.KeepService]; orderi != orderj {
520 // Prefer a better rendezvous
522 return orderi < orderj
523 } else if repli, replj := si.repl != nil, sj.repl != nil; repli != replj {
524 // Prefer a mount that already has a
528 // If pull/trash turns out to be
529 // needed, distribute the
530 // new/remaining replicas uniformly
531 // across qualifying mounts on a given
533 return rendezvousLess(si.mnt.DeviceID, sj.mnt.DeviceID, blkid)
537 // Servers and mounts (with or without existing
538 // replicas) that are part of the best achievable
539 // layout for this storage class.
540 wantSrv := map[*KeepService]bool{}
541 wantMnt := map[*KeepMount]bool{}
542 // Positions (with existing replicas) that have been
543 // protected (via unsafeToDelete) to ensure we don't
544 // reduce replication below desired level when
545 // trashing replicas that aren't optimal positions for
546 // any storage class.
547 protMnt := map[*KeepMount]bool{}
549 // trySlot tries using a slot to meet requirements,
550 // and returns true if all requirements are met.
551 trySlot := func(i int) bool {
553 if len(protMnt) < desired && slot.repl != nil {
554 unsafeToDelete[slot.repl.Mtime] = true
555 protMnt[slot.mnt] = true
557 if len(wantMnt) < desired && (slot.repl != nil || !slot.mnt.ReadOnly) {
559 wantSrv[slot.mnt.KeepService] = true
560 wantMnt[slot.mnt] = true
562 return len(protMnt) >= desired && len(wantMnt) >= desired
565 // First try to achieve desired replication without
566 // using the same server twice.
568 for i := 0; i < len(slots) && !done; i++ {
569 if !wantSrv[slots[i].mnt.KeepService] {
574 // If that didn't suffice, do another pass without the
575 // "distinct services" restriction. (Achieving the
576 // desired volume replication on fewer than the
577 // desired number of services is better than
578 // underreplicating.)
579 for i := 0; i < len(slots) && !done; i++ {
583 if !underreplicated {
585 for _, slot := range slots {
586 if slot.repl == nil || !bal.mountsByClass[class][slot.mnt] {
589 if safe++; safe >= desired {
593 underreplicated = safe < desired
597 // TODO: If multiple replicas are trashable, prefer the oldest
598 // replica that doesn't have a timestamp collision with
602 for _, slot := range slots {
606 if slot.repl != nil {
612 for _, slot := range slots {
613 // TODO: request a Touch if Mtime is duplicated.
616 case !underreplicated && slot.repl != nil && !slot.want && !unsafeToDelete[slot.repl.Mtime]:
617 slot.mnt.KeepService.AddTrash(Trash{
619 Mtime: slot.repl.Mtime,
623 case len(blk.Replicas) == 0:
625 case slot.repl == nil && slot.want && !slot.mnt.ReadOnly:
626 slot.mnt.KeepService.AddPull(Pull{
628 From: blk.Replicas[0].KeepMount.KeepService,
635 if bal.Dumper != nil {
637 if slot.repl != nil {
638 mtime = slot.repl.Mtime
640 srv := slot.mnt.KeepService
641 changes = append(changes, fmt.Sprintf("%s:%d/%s=%s,%d", srv.ServiceHost, srv.ServicePort, slot.mnt.UUID, changeName[change], mtime))
644 if bal.Dumper != nil {
645 bal.Dumper.Printf("%s have=%d want=%v %s", blkid, have, want, strings.Join(changes, " "))
647 return balanceResult{
655 type blocksNBytes struct {
661 func (bb blocksNBytes) String() string {
662 return fmt.Sprintf("%d replicas (%d blocks, %d bytes)", bb.replicas, bb.blocks, bb.bytes)
665 type balancerStats struct {
666 lost, overrep, unref, garbage, underrep, justright blocksNBytes
667 desired, current blocksNBytes
672 func (bal *Balancer) collectStatistics(results <-chan balanceResult) {
674 s.replHistogram = make([]int, 2)
675 for result := range results {
676 surplus := result.have - result.want
677 bytes := result.blkid.Size()
679 case result.have == 0 && result.want > 0:
680 s.lost.replicas -= surplus
682 s.lost.bytes += bytes * int64(-surplus)
684 s.underrep.replicas -= surplus
686 s.underrep.bytes += bytes * int64(-surplus)
687 case surplus > 0 && result.want == 0:
688 counter := &s.garbage
689 for _, r := range result.blk.Replicas {
690 if r.Mtime >= bal.MinMtime {
695 counter.replicas += surplus
697 counter.bytes += bytes * int64(surplus)
699 s.overrep.replicas += surplus
701 s.overrep.bytes += bytes * int64(len(result.blk.Replicas)-result.want)
703 s.justright.replicas += result.want
705 s.justright.bytes += bytes * int64(result.want)
709 s.desired.replicas += result.want
711 s.desired.bytes += bytes * int64(result.want)
713 if len(result.blk.Replicas) > 0 {
714 s.current.replicas += len(result.blk.Replicas)
716 s.current.bytes += bytes * int64(len(result.blk.Replicas))
719 for len(s.replHistogram) <= len(result.blk.Replicas) {
720 s.replHistogram = append(s.replHistogram, 0)
722 s.replHistogram[len(result.blk.Replicas)]++
724 for _, srv := range bal.KeepServices {
725 s.pulls += len(srv.ChangeSet.Pulls)
726 s.trashes += len(srv.ChangeSet.Trashes)
731 // PrintStatistics writes statistics about the computed changes to
732 // bal.Logger. It should not be called until ComputeChangeSets has
734 func (bal *Balancer) PrintStatistics() {
736 bal.logf("%s lost (0=have<want)", bal.stats.lost)
737 bal.logf("%s underreplicated (0<have<want)", bal.stats.underrep)
738 bal.logf("%s just right (have=want)", bal.stats.justright)
739 bal.logf("%s overreplicated (have>want>0)", bal.stats.overrep)
740 bal.logf("%s unreferenced (have>want=0, new)", bal.stats.unref)
741 bal.logf("%s garbage (have>want=0, old)", bal.stats.garbage)
743 bal.logf("%s total commitment (excluding unreferenced)", bal.stats.desired)
744 bal.logf("%s total usage", bal.stats.current)
746 for _, srv := range bal.KeepServices {
747 bal.logf("%s: %v\n", srv, srv.ChangeSet)
750 bal.printHistogram(60)
754 func (bal *Balancer) printHistogram(hashColumns int) {
755 bal.logf("Replication level distribution (counting N replicas on a single server as N):")
757 for _, count := range bal.stats.replHistogram {
758 if maxCount < count {
762 hashes := strings.Repeat("#", hashColumns)
763 countWidth := 1 + int(math.Log10(float64(maxCount+1)))
764 scaleCount := 10 * float64(hashColumns) / math.Floor(1+10*math.Log10(float64(maxCount+1)))
765 for repl, count := range bal.stats.replHistogram {
766 nHashes := int(scaleCount * math.Log10(float64(count+1)))
767 bal.logf("%2d: %*d %s", repl, countWidth, count, hashes[:nHashes])
771 // CheckSanityLate checks for configuration and runtime errors after
772 // GetCurrentState() and ComputeChangeSets() have finished.
774 // If it returns an error, it is dangerous to run any Commit methods.
775 func (bal *Balancer) CheckSanityLate() error {
776 if bal.errors != nil {
777 for _, err := range bal.errors {
778 bal.logf("deferred error: %v", err)
780 return fmt.Errorf("cannot proceed safely after deferred errors")
783 if bal.collScanned == 0 {
784 return fmt.Errorf("received zero collections")
788 bal.BlockStateMap.Apply(func(_ arvados.SizedDigest, blk *BlockState) {
789 for _, desired := range blk.Desired {
797 return fmt.Errorf("zero blocks have desired replication>0")
800 if dr := bal.DefaultReplication; dr < 1 {
801 return fmt.Errorf("Default replication (%d) is less than 1", dr)
804 // TODO: no two services have identical indexes
805 // TODO: no collisions (same md5, different size)
809 // CommitPulls sends the computed lists of pull requests to the
810 // keepstore servers. This has the effect of increasing replication of
811 // existing blocks that are either underreplicated or poorly
812 // distributed according to rendezvous hashing.
813 func (bal *Balancer) CommitPulls(c *arvados.Client) error {
814 return bal.commitAsync(c, "send pull list",
815 func(srv *KeepService) error {
816 return srv.CommitPulls(c)
820 // CommitTrash sends the computed lists of trash requests to the
821 // keepstore servers. This has the effect of deleting blocks that are
822 // overreplicated or unreferenced.
823 func (bal *Balancer) CommitTrash(c *arvados.Client) error {
824 return bal.commitAsync(c, "send trash list",
825 func(srv *KeepService) error {
826 return srv.CommitTrash(c)
830 func (bal *Balancer) commitAsync(c *arvados.Client, label string, f func(srv *KeepService) error) error {
831 errs := make(chan error)
832 for _, srv := range bal.KeepServices {
833 go func(srv *KeepService) {
835 defer func() { errs <- err }()
836 label := fmt.Sprintf("%s: %v", srv, label)
837 defer timeMe(bal.Logger, label)()
840 err = fmt.Errorf("%s: %v", label, err)
845 for range bal.KeepServices {
846 if err := <-errs; err != nil {
855 func (bal *Balancer) logf(f string, args ...interface{}) {
856 if bal.Logger != nil {
857 bal.Logger.Printf(f, args...)
861 // Rendezvous hash sort function. Less efficient than sorting on
862 // precomputed rendezvous hashes, but also rarely used.
863 func rendezvousLess(i, j string, blkid arvados.SizedDigest) bool {
864 a := md5.Sum([]byte(string(blkid[:32]) + i))
865 b := md5.Sum([]byte(string(blkid[:32]) + j))
866 return bytes.Compare(a[:], b[:]) < 0