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