Skip to content

Commit

Permalink
fix: do not get item from an unsync backend (#5836)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Jun 14, 2021
1 parent 8d4f75e commit dde3834
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions engine/cdn/cdn_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ func (s *Service) getRandomItemUnitIDByItemID(ctx context.Context, itemID string

itemUnits = s.Units.FilterItemUnitReaderByType(itemUnits)
itemUnits = s.Units.FilterItemUnitFromBuffer(itemUnits)
itemUnits = s.Units.FilterNotSyncBackend(itemUnits)

if len(itemUnits) == 0 {
return "", "", sdk.WithStack(fmt.Errorf("unable to find item units for item with id: %s", itemID))
Expand Down
3 changes: 3 additions & 0 deletions engine/cdn/cdn_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func (s *Service) ComputeMetrics(ctx context.Context) {

var storageStats []storage.Stat
for _, su := range s.Units.Storages {
if !su.CanSync() {
continue
}
storageStats = append(storageStats, s.countItemsForUnit(ctx, su)...)
}

Expand Down
20 changes: 20 additions & 0 deletions engine/cdn/storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,26 @@ func (x RunningStorageUnits) GetBuffer(bufferType sdk.CDNItemType) BufferUnit {
}
}

func (x *RunningStorageUnits) CanSync(unitID string) bool {
for _, unit := range x.Storages {
if unit.ID() == unitID {
return unit.CanSync()
}
}
return false
}

func (x *RunningStorageUnits) FilterNotSyncBackend(ius []sdk.CDNItemUnit) []sdk.CDNItemUnit {
itemsUnits := make([]sdk.CDNItemUnit, 0, len(ius))
for _, u := range ius {
if !x.CanSync(u.UnitID) {
continue
}
itemsUnits = append(itemsUnits, u)
}
return itemsUnits
}

func (x *RunningStorageUnits) FilterItemUnitFromBuffer(ius []sdk.CDNItemUnit) []sdk.CDNItemUnit {
itemsUnits := make([]sdk.CDNItemUnit, 0, len(ius))
for _, u := range ius {
Expand Down
7 changes: 7 additions & 0 deletions engine/sql/cdn/014_cdn_type_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +migrate Up
SELECT create_index('item', 'idx_item_type_status', 'type,status');
SELECT create_index('storage_unit_item', 'idx_storage_unit_type_item_unit_id', 'type,unit_id');

-- +migrate Down
DROP INDEX "idx_item_type_status";
DROP INDEX "idx_storage_unit_type_item_unit_id"

0 comments on commit dde3834

Please sign in to comment.