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 number of desired replicas (according to
22 // the collections we know about) and the replicas actually stored
23 // (according to the keepstore indexes we know about).
24 type BlockState struct {
29 func (bs *BlockState) addReplica(r Replica) {
30 bs.Replicas = append(bs.Replicas, r)
33 func (bs *BlockState) increaseDesired(n int) {
39 // BlockStateMap is a goroutine-safe wrapper around a
40 // map[arvados.SizedDigest]*BlockState.
41 type BlockStateMap struct {
42 entries map[arvados.SizedDigest]*BlockState
46 // NewBlockStateMap returns a newly allocated BlockStateMap.
47 func NewBlockStateMap() *BlockStateMap {
48 return &BlockStateMap{
49 entries: make(map[arvados.SizedDigest]*BlockState),
53 // return a BlockState entry, allocating a new one if needed. (Private
54 // method: not goroutine-safe.)
55 func (bsm *BlockStateMap) get(blkid arvados.SizedDigest) *BlockState {
56 // TODO? Allocate BlockState structs a slice at a time,
57 // instead of one at a time.
58 blk := bsm.entries[blkid]
61 bsm.entries[blkid] = blk
66 // Apply runs f on each entry in the map.
67 func (bsm *BlockStateMap) Apply(f func(arvados.SizedDigest, *BlockState)) {
69 defer bsm.mutex.Unlock()
71 for blkid, blk := range bsm.entries {
76 // AddReplicas updates the map to indicate srv has a replica of each
78 func (bsm *BlockStateMap) AddReplicas(srv *KeepService, idx []arvados.KeepServiceIndexEntry) {
80 defer bsm.mutex.Unlock()
82 for _, ent := range idx {
83 bsm.get(ent.SizedDigest).addReplica(Replica{
90 // IncreaseDesired updates the map to indicate the desired replication
91 // for the given blocks is at least n.
92 func (bsm *BlockStateMap) IncreaseDesired(n int, blocks []arvados.SizedDigest) {
94 defer bsm.mutex.Unlock()
96 for _, blkid := range blocks {
97 bsm.get(blkid).increaseDesired(n)