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 {
27 Desired map[string]int
28 // TODO: Support combinations of classes ("private + durable")
29 // by replacing the map[string]int with a map[*[]string]int
30 // here, where the map keys come from a pool of semantically
31 // distinct class combinations.
33 // TODO: Use a pool of semantically distinct Desired maps to
34 // conserve memory (typically there are far more BlockState
35 // objects in memory than distinct Desired profiles).
38 var defaultClasses = []string{"default"}
40 func (bs *BlockState) addReplica(r Replica) {
41 bs.Replicas = append(bs.Replicas, r)
44 func (bs *BlockState) increaseDesired(classes []string, n int) {
45 if len(classes) == 0 {
46 classes = defaultClasses
48 for _, class := range classes {
49 if bs.Desired == nil {
50 bs.Desired = map[string]int{class: n}
51 } else if d, ok := bs.Desired[class]; !ok || d < n {
57 // BlockStateMap is a goroutine-safe wrapper around a
58 // map[arvados.SizedDigest]*BlockState.
59 type BlockStateMap struct {
60 entries map[arvados.SizedDigest]*BlockState
64 // NewBlockStateMap returns a newly allocated BlockStateMap.
65 func NewBlockStateMap() *BlockStateMap {
66 return &BlockStateMap{
67 entries: make(map[arvados.SizedDigest]*BlockState),
71 // return a BlockState entry, allocating a new one if needed. (Private
72 // method: not goroutine-safe.)
73 func (bsm *BlockStateMap) get(blkid arvados.SizedDigest) *BlockState {
74 // TODO? Allocate BlockState structs a slice at a time,
75 // instead of one at a time.
76 blk := bsm.entries[blkid]
79 bsm.entries[blkid] = blk
84 // Apply runs f on each entry in the map.
85 func (bsm *BlockStateMap) Apply(f func(arvados.SizedDigest, *BlockState)) {
87 defer bsm.mutex.Unlock()
89 for blkid, blk := range bsm.entries {
94 // AddReplicas updates the map to indicate that mnt has a replica of
96 func (bsm *BlockStateMap) AddReplicas(mnt *KeepMount, idx []arvados.KeepServiceIndexEntry) {
98 defer bsm.mutex.Unlock()
100 for _, ent := range idx {
101 bsm.get(ent.SizedDigest).addReplica(Replica{
108 // IncreaseDesired updates the map to indicate the desired replication
109 // for the given blocks in the given storage class is at least n.
110 func (bsm *BlockStateMap) IncreaseDesired(classes []string, n int, blocks []arvados.SizedDigest) {
112 defer bsm.mutex.Unlock()
114 for _, blkid := range blocks {
115 bsm.get(blkid).increaseDesired(classes, n)