6 "git.curoverse.com/arvados.git/sdk/go/arvados"
9 // Replica is a file on disk (or object in an S3 bucket, or blob in an
10 // Azure storage container, etc.) as reported in a keepstore index
17 // BlockState indicates the number of desired replicas (according to
18 // the collections we know about) and the replicas actually stored
19 // (according to the keepstore indexes we know about).
20 type BlockState struct {
25 func (bs *BlockState) addReplica(r Replica) {
26 bs.Replicas = append(bs.Replicas, r)
29 func (bs *BlockState) increaseDesired(n int) {
35 // BlockStateMap is a goroutine-safe wrapper around a
36 // map[arvados.SizedDigest]*BlockState.
37 type BlockStateMap struct {
38 entries map[arvados.SizedDigest]*BlockState
42 // NewBlockStateMap returns a newly allocated BlockStateMap.
43 func NewBlockStateMap() *BlockStateMap {
44 return &BlockStateMap{
45 entries: make(map[arvados.SizedDigest]*BlockState),
49 // return a BlockState entry, allocating a new one if needed. (Private
50 // method: not goroutine-safe.)
51 func (bsm *BlockStateMap) get(blkid arvados.SizedDigest) *BlockState {
52 // TODO? Allocate BlockState structs a slice at a time,
53 // instead of one at a time.
54 blk := bsm.entries[blkid]
57 bsm.entries[blkid] = blk
62 // Apply runs f on each entry in the map.
63 func (bsm *BlockStateMap) Apply(f func(arvados.SizedDigest, *BlockState)) {
65 defer bsm.mutex.Unlock()
67 for blkid, blk := range bsm.entries {
72 // AddReplicas updates the map to indicate srv has a replica of each
74 func (bsm *BlockStateMap) AddReplicas(srv *KeepService, idx []arvados.KeepServiceIndexEntry) {
76 defer bsm.mutex.Unlock()
78 for _, ent := range idx {
79 bsm.get(ent.SizedDigest).addReplica(Replica{
86 // IncreaseDesired updates the map to indicate the desired replication
87 // for the given blocks is at least n.
88 func (bsm *BlockStateMap) IncreaseDesired(n int, blocks []arvados.SizedDigest) {
90 defer bsm.mutex.Unlock()
92 for _, blkid := range blocks {
93 bsm.get(blkid).increaseDesired(n)