/
storage_unit_io.go
130 lines (111 loc) · 3.13 KB
/
storage_unit_io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package storage
import (
"context"
"fmt"
"io"
"math/rand"
"time"
"github.com/rockbears/log"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/sdk"
)
var (
rs = rand.NewSource(time.Now().Unix())
rnd = rand.New(rs)
)
type Source interface {
NewReader(context.Context) (io.ReadCloser, error)
Read(io.Reader, io.Writer) error
Name() string
SyncBandwidth() float64
}
type source interface {
NewReader(context.Context, sdk.CDNItemUnit) (io.ReadCloser, error)
Read(sdk.CDNItemUnit, io.Reader, io.Writer) error
Name() string
SyncBandwidth() float64
}
type iuSource struct {
iu sdk.CDNItemUnit
source source
}
func (s *iuSource) NewReader(ctx context.Context) (io.ReadCloser, error) {
return s.source.NewReader(ctx, s.iu)
}
func (s *iuSource) Read(r io.Reader, w io.Writer) error {
return s.source.Read(s.iu, r, w)
}
func (s *iuSource) Name() string {
return s.source.Name()
}
func (s *iuSource) SyncBandwidth() float64 {
return s.source.SyncBandwidth()
}
func (r RunningStorageUnits) GetSource(ctx context.Context, i *sdk.CDNItem) (Source, error) {
bufferUnit := r.GetBuffer(i.Type)
ok, err := bufferUnit.ItemExists(ctx, r.m, r.db, *i)
if err != nil {
return nil, err
}
if ok {
iu, err := LoadItemUnitByUnit(ctx, r.m, r.db, bufferUnit.ID(), i.ID, gorpmapper.GetOptions.WithDecryption)
if err != nil {
return nil, err
}
return &iuSource{iu: *iu, source: bufferUnit}, nil
}
// Find a storage unit where the item is complete
itemUnits, err := LoadAllItemUnitsByItemIDs(ctx, r.m, r.db, i.ID, gorpmapper.GetOptions.WithDecryption)
if err != nil {
return nil, err
}
if len(itemUnits) == 0 {
log.Warn(ctx, "item %s can't be found. No unit knows it...", i.ID)
return nil, sdk.WithStack(sdk.ErrNotFound)
}
itemUnits = r.FilterItemUnitReaderByType(itemUnits)
// Random pick a unit
idx := 0
if len(itemUnits) > 1 {
idx = rnd.Intn(len(itemUnits))
}
refItemUnit := itemUnits[idx]
refUnitID := refItemUnit.UnitID
refUnit, err := LoadUnitByID(ctx, r.m, r.db, refUnitID)
if err != nil {
return nil, err
}
var unit source = r.Storage(refUnit.Name)
if unit == nil {
if bufferUnit.Name() == refUnit.Name {
unit = bufferUnit
} else {
return nil, sdk.WithStack(fmt.Errorf("unable to find unit %s", refUnit.Name))
}
}
return &iuSource{iu: refItemUnit, source: unit}, nil
}
func (r RunningStorageUnits) NewSource(ctx context.Context, refItemUnit sdk.CDNItemUnit) (Source, error) {
refUnit, err := LoadUnitByID(ctx, r.m, r.db, refItemUnit.UnitID)
if err != nil {
return nil, err
}
var unit source = r.Storage(refUnit.Name)
if unit == nil {
for _, bu := range r.Buffers {
if bu.Name() == refUnit.Name {
unit = bu
break
}
}
if unit == nil {
return nil, sdk.WithStack(fmt.Errorf("unable to find unit %s", refUnit.Name))
}
}
return &iuSource{iu: refItemUnit, source: unit}, nil
}
func (r RunningStorageUnits) GetItemUnitByLocatorByUnit(locator string, unitID string, itemType sdk.CDNItemType) (bool, error) {
// Load all the itemUnit for the unit and the same hashLocator
hashLocator := r.HashLocator(locator)
return HasItemUnitsByUnitAndHashLocator(r.db, unitID, hashLocator, itemType)
}