@@ -13,6 +13,7 @@ import (
13
13
"github.com/buildbarn/bb-storage/pkg/blobstore/readcaching"
14
14
"github.com/buildbarn/bb-storage/pkg/blobstore/readfallback"
15
15
"github.com/buildbarn/bb-storage/pkg/blobstore/sharding"
16
+ "github.com/buildbarn/bb-storage/pkg/blobstore/sharding/legacy"
16
17
"github.com/buildbarn/bb-storage/pkg/blockdevice"
17
18
"github.com/buildbarn/bb-storage/pkg/clock"
18
19
"github.com/buildbarn/bb-storage/pkg/digest"
@@ -85,41 +86,92 @@ func (nc *simpleNestedBlobAccessCreator) newNestedBlobAccessBare(configuration *
85
86
DigestKeyFormat : slow .DigestKeyFormat ,
86
87
}, "read_caching" , nil
87
88
case * pb.BlobAccessConfiguration_Sharding :
88
- backends := make ([]blobstore.BlobAccess , 0 , len (backend .Sharding .Shards ))
89
- weights := make ([]uint32 , 0 , len (backend .Sharding .Shards ))
90
- var combinedDigestKeyFormat * digest.KeyFormat
91
- for _ , shard := range backend .Sharding .Shards {
92
- if shard .Backend == nil {
93
- // Drained backend.
94
- backends = append (backends , nil )
95
- } else {
96
- // Undrained backend.
97
- backend , err := nc .NewNestedBlobAccess (shard .Backend , creator )
98
- if err != nil {
99
- return BlobAccessInfo {}, "" , err
89
+ if backend .Sharding .Legacy != nil {
90
+ backends := make ([]blobstore.BlobAccess , 0 , len (backend .Sharding .Legacy .ShardOrder ))
91
+ weights := make ([]uint32 , 0 , len (backend .Sharding .Legacy .ShardOrder ))
92
+ var combinedDigestKeyFormat * digest.KeyFormat
93
+ for _ , key := range backend .Sharding .Legacy .ShardOrder {
94
+ shard , exists := backend .Sharding .Shards [key ]
95
+ if ! exists {
96
+ return BlobAccessInfo {}, "" , status .Errorf (codes .InvalidArgument , "Legacy sharding blob access refers to non-existing key %s" , key )
100
97
}
101
- backends = append ( backends , backend . BlobAccess )
102
- if combinedDigestKeyFormat == nil {
103
- combinedDigestKeyFormat = & backend . DigestKeyFormat
98
+ if shard . Backend == nil {
99
+ // Drained backend
100
+ backends = append ( backends , nil )
104
101
} else {
105
- newDigestKeyFormat := combinedDigestKeyFormat .Combine (backend .DigestKeyFormat )
106
- combinedDigestKeyFormat = & newDigestKeyFormat
102
+ // Undrained backend
103
+ backend , err := nc .NewNestedBlobAccess (shard .Backend , creator )
104
+ if err != nil {
105
+ return BlobAccessInfo {}, "" , err
106
+ }
107
+ backends = append (backends , backend .BlobAccess )
108
+ if combinedDigestKeyFormat == nil {
109
+ combinedDigestKeyFormat = & backend .DigestKeyFormat
110
+ } else {
111
+ newDigestKeyFormat := combinedDigestKeyFormat .Combine (backend .DigestKeyFormat )
112
+ combinedDigestKeyFormat = & newDigestKeyFormat
113
+ }
107
114
}
115
+
116
+ if shard .Weight == 0 {
117
+ return BlobAccessInfo {}, "" , status .Errorf (codes .InvalidArgument , "Shards must have positive weights" )
118
+ }
119
+ weights = append (weights , shard .Weight )
120
+ }
121
+
122
+ if combinedDigestKeyFormat == nil {
123
+ return BlobAccessInfo {}, "" , status .Errorf (codes .InvalidArgument , "Cannot create sharding blob access without any undrained backends" )
124
+ }
125
+ return BlobAccessInfo {
126
+ BlobAccess : legacy .NewShardingBlobAccess (
127
+ backends ,
128
+ legacy .NewWeightedShardPermuter (weights ),
129
+ backend .Sharding .Legacy .HashInitialization ,
130
+ ),
131
+ DigestKeyFormat : * combinedDigestKeyFormat ,
132
+ }, "sharding" , nil
133
+ }
134
+ backends := make ([]sharding.ShardBackend , 0 , len (backend .Sharding .Shards ))
135
+ shards := make ([]sharding.Shard , 0 , len (backend .Sharding .Shards ))
136
+ keys := make ([]string , 0 , len (backend .Sharding .Shards ))
137
+ var combinedDigestKeyFormat * digest.KeyFormat
138
+ for key , shard := range backend .Sharding .Shards {
139
+ if shard .Backend == nil {
140
+ return BlobAccessInfo {}, "" , status .Errorf (codes .InvalidArgument , "Shard '%s' has an undefined backend, drained backends are only allowed when running in Legacy mode" , key )
141
+ }
142
+ backend , err := nc .NewNestedBlobAccess (shard .Backend , creator )
143
+ if err != nil {
144
+ return BlobAccessInfo {}, "" , err
145
+ }
146
+ backends = append (backends , sharding.ShardBackend {Backend : backend .BlobAccess , Key : key })
147
+ if combinedDigestKeyFormat == nil {
148
+ combinedDigestKeyFormat = & backend .DigestKeyFormat
149
+ } else {
150
+ newDigestKeyFormat := combinedDigestKeyFormat .Combine (backend .DigestKeyFormat )
151
+ combinedDigestKeyFormat = & newDigestKeyFormat
108
152
}
109
153
110
154
if shard .Weight == 0 {
111
155
return BlobAccessInfo {}, "" , status .Errorf (codes .InvalidArgument , "Shards must have positive weights" )
112
156
}
113
- weights = append (weights , shard .Weight )
157
+ shards = append (shards , sharding.Shard {
158
+ Key : key ,
159
+ Weight : shard .Weight ,
160
+ })
161
+ keys = append (keys , key )
114
162
}
115
163
if combinedDigestKeyFormat == nil {
116
- return BlobAccessInfo {}, "" , status .Errorf (codes .InvalidArgument , "Cannot create sharding blob access without any undrained backends" )
164
+ return BlobAccessInfo {}, "" , status .Errorf (codes .InvalidArgument , "Cannot create sharding blob access without any backends" )
165
+ }
166
+ shardSelector , err := sharding .NewRendezvousShardSelector (shards )
167
+ if err != nil {
168
+ return BlobAccessInfo {}, "" , status .Errorf (codes .InvalidArgument , "Could not create rendezvous shard selector" )
117
169
}
118
170
return BlobAccessInfo {
119
171
BlobAccess : sharding .NewShardingBlobAccess (
120
172
backends ,
121
- sharding . NewWeightedShardPermuter ( weights ) ,
122
- backend . Sharding . HashInitialization ),
173
+ shardSelector ,
174
+ ),
123
175
DigestKeyFormat : * combinedDigestKeyFormat ,
124
176
}, "sharding" , nil
125
177
case * pb.BlobAccessConfiguration_Mirrored :
0 commit comments