Skip to content

Commit dde3834

Browse files
authored
fix: do not get item from an unsync backend (#5836)
1 parent 8d4f75e commit dde3834

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

engine/cdn/cdn_item.go

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ func (s *Service) getRandomItemUnitIDByItemID(ctx context.Context, itemID string
351351

352352
itemUnits = s.Units.FilterItemUnitReaderByType(itemUnits)
353353
itemUnits = s.Units.FilterItemUnitFromBuffer(itemUnits)
354+
itemUnits = s.Units.FilterNotSyncBackend(itemUnits)
354355

355356
if len(itemUnits) == 0 {
356357
return "", "", sdk.WithStack(fmt.Errorf("unable to find item units for item with id: %s", itemID))

engine/cdn/cdn_metrics.go

+3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ func (s *Service) ComputeMetrics(ctx context.Context) {
122122

123123
var storageStats []storage.Stat
124124
for _, su := range s.Units.Storages {
125+
if !su.CanSync() {
126+
continue
127+
}
125128
storageStats = append(storageStats, s.countItemsForUnit(ctx, su)...)
126129
}
127130

engine/cdn/storage/types.go

+20
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ func (x RunningStorageUnits) GetBuffer(bufferType sdk.CDNItemType) BufferUnit {
286286
}
287287
}
288288

289+
func (x *RunningStorageUnits) CanSync(unitID string) bool {
290+
for _, unit := range x.Storages {
291+
if unit.ID() == unitID {
292+
return unit.CanSync()
293+
}
294+
}
295+
return false
296+
}
297+
298+
func (x *RunningStorageUnits) FilterNotSyncBackend(ius []sdk.CDNItemUnit) []sdk.CDNItemUnit {
299+
itemsUnits := make([]sdk.CDNItemUnit, 0, len(ius))
300+
for _, u := range ius {
301+
if !x.CanSync(u.UnitID) {
302+
continue
303+
}
304+
itemsUnits = append(itemsUnits, u)
305+
}
306+
return itemsUnits
307+
}
308+
289309
func (x *RunningStorageUnits) FilterItemUnitFromBuffer(ius []sdk.CDNItemUnit) []sdk.CDNItemUnit {
290310
itemsUnits := make([]sdk.CDNItemUnit, 0, len(ius))
291311
for _, u := range ius {

engine/sql/cdn/014_cdn_type_index.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- +migrate Up
2+
SELECT create_index('item', 'idx_item_type_status', 'type,status');
3+
SELECT create_index('storage_unit_item', 'idx_storage_unit_type_item_unit_id', 'type,unit_id');
4+
5+
-- +migrate Down
6+
DROP INDEX "idx_item_type_status";
7+
DROP INDEX "idx_storage_unit_type_item_unit_id"

0 commit comments

Comments
 (0)