1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
10 "git.curoverse.com/arvados.git/sdk/go/arvados"
13 // Replica is a file on disk (or object in an S3 bucket, or blob in an
14 // Azure storage container, etc.) as reported in a keepstore index
21 // BlockState indicates the desired storage class and number of
22 // replicas (according to the collections we know about) and the
23 // replicas actually stored (according to the keepstore indexes we
25 type BlockState struct {
28 Desired map[string]int
29 // TODO: Support combinations of classes ("private + durable")
30 // by replacing the map[string]int with a map[*[]string]int
31 // here, where the map keys come from a pool of semantically
32 // distinct class combinations.
34 // TODO: Use a pool of semantically distinct Desired maps to
35 // conserve memory (typically there are far more BlockState
36 // objects in memory than distinct Desired profiles).
39 var defaultClasses = []string{"default"}
41 func (bs *BlockState) addReplica(r Replica) {
42 bs.Replicas = append(bs.Replicas, r)
45 func (bs *BlockState) increaseDesired(classes []string, n int) {
47 if len(classes) == 0 {
48 classes = defaultClasses
50 for _, class := range classes {
51 if bs.Desired == nil {
52 bs.Desired = map[string]int{class: n}
53 } else if d, ok := bs.Desired[class]; !ok || d < n {
59 // BlockStateMap is a goroutine-safe wrapper around a
60 // map[arvados.SizedDigest]*BlockState.
61 type BlockStateMap struct {
62 entries map[arvados.SizedDigest]*BlockState
66 // NewBlockStateMap returns a newly allocated BlockStateMap.
67 func NewBlockStateMap() *BlockStateMap {
68 return &BlockStateMap{
69 entries: make(map[arvados.SizedDigest]*BlockState),
73 // return a BlockState entry, allocating a new one if needed. (Private
74 // method: not goroutine-safe.)
75 func (bsm *BlockStateMap) get(blkid arvados.SizedDigest) *BlockState {
76 // TODO? Allocate BlockState structs a slice at a time,
77 // instead of one at a time.
78 blk := bsm.entries[blkid]
81 bsm.entries[blkid] = blk
86 // Apply runs f on each entry in the map.
87 func (bsm *BlockStateMap) Apply(f func(arvados.SizedDigest, *BlockState)) {
89 defer bsm.mutex.Unlock()
91 for blkid, blk := range bsm.entries {
96 // AddReplicas updates the map to indicate that mnt has a replica of
98 func (bsm *BlockStateMap) AddReplicas(mnt *KeepMount, idx []arvados.KeepServiceIndexEntry) {
100 defer bsm.mutex.Unlock()
102 for _, ent := range idx {
103 bsm.get(ent.SizedDigest).addReplica(Replica{
110 // IncreaseDesired updates the map to indicate the desired replication
111 // for the given blocks in the given storage class is at least n.
112 func (bsm *BlockStateMap) IncreaseDesired(classes []string, n int, blocks []arvados.SizedDigest) {
114 defer bsm.mutex.Unlock()
116 for _, blkid := range blocks {
117 bsm.get(blkid).increaseDesired(classes, n)