/
putxaction.go
253 lines (225 loc) · 6.41 KB
/
putxaction.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Package ec provides erasure coding (EC) based data protection for AIStore.
/*
* Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
*/
package ec
import (
"fmt"
"sync"
"time"
"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/cmn"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/cmn/debug"
"github.com/NVIDIA/aistore/cmn/nlog"
"github.com/NVIDIA/aistore/core"
"github.com/NVIDIA/aistore/core/meta"
"github.com/NVIDIA/aistore/fs"
"github.com/NVIDIA/aistore/xact"
"github.com/NVIDIA/aistore/xact/xreg"
)
type (
putFactory struct {
xreg.RenewBase
xctn *XactPut
}
// Erasure coding runner: accepts requests and dispatches them to
// a correct mountpath runner. Runner uses dedicated to EC memory manager
// inherited by dependent mountpath runners
XactPut struct {
xactECBase
xactReqBase
putJoggers map[string]*putJogger // mountpath joggers for PUT/DEL
}
// extended x-ec-put statistics
ExtECPutStats struct {
AvgEncodeTime cos.Duration `json:"ec.encode.ns"`
AvgDeleteTime cos.Duration `json:"ec.delete.ns"`
EncodeCount int64 `json:"ec.encode.n,string"`
DeleteCount int64 `json:"ec.delete.n,string"`
EncodeSize int64 `json:"ec.encode.size,string"`
EncodeErrCount int64 `json:"ec.encode.err.n,string"`
DeleteErrCount int64 `json:"ec.delete.err.n,string"`
AvgObjTime cos.Duration `json:"ec.obj.process.ns"`
AvgQueueLen float64 `json:"ec.queue.len.f"`
IsIdle bool `json:"is_idle"`
}
)
// interface guard
var (
_ xact.Demand = (*XactPut)(nil)
_ xreg.Renewable = (*putFactory)(nil)
)
////////////////
// putFactory //
////////////////
func (*putFactory) New(_ xreg.Args, bck *meta.Bck) xreg.Renewable {
p := &putFactory{RenewBase: xreg.RenewBase{Bck: bck}}
return p
}
func (p *putFactory) Start() error {
xec := ECM.NewPutXact(p.Bck.Bucket())
xec.DemandBase.Init(cos.GenUUID(), p.Kind(), p.Bck, 0 /*use default*/)
p.xctn = xec
go xec.Run(nil)
return nil
}
func (*putFactory) Kind() string { return apc.ActECPut }
func (p *putFactory) Get() core.Xact { return p.xctn }
func (p *putFactory) WhenPrevIsRunning(xprev xreg.Renewable) (xreg.WPR, error) {
debug.Assertf(false, "%s vs %s", p.Str(p.Kind()), xprev) // xreg.usePrev() must've returned true
return xreg.WprUse, nil
}
/////////////
// XactPut //
/////////////
func newPutXact(bck *cmn.Bck, mgr *Manager) *XactPut {
var (
avail, disabled = fs.Get()
totalPaths = len(avail) + len(disabled)
config = cmn.GCO.Get()
xctn = &XactPut{
putJoggers: make(map[string]*putJogger, totalPaths),
}
)
xctn.xactECBase.init(config, bck, mgr)
xctn.xactReqBase.init()
// create all runners but do not start them until Run is called
for mpath := range avail {
putJog := xctn.newPutJogger(mpath)
xctn.putJoggers[mpath] = putJog
}
for mpath := range disabled {
putJog := xctn.newPutJogger(mpath)
xctn.putJoggers[mpath] = putJog
}
return xctn
}
func (r *XactPut) newPutJogger(mpath string) *putJogger {
j := &putJogger{
parent: r,
mpath: mpath,
putCh: make(chan *request, requestBufSizeFS),
xactCh: make(chan *request, requestBufSizeEncode),
}
j.stopCh.Init()
return j
}
func (r *XactPut) dispatchRequest(req *request, lom *core.LOM) error {
debug.Assert(req.Action == ActDelete || req.Action == ActSplit, req.Action)
debug.Assert(req.ErrCh == nil, "ec-put does not support ErrCh")
if !r.ecRequestsEnabled() {
return ErrorECDisabled
}
switch req.Action {
case ActSplit:
r.stats.updateEncode(lom.SizeBytes())
case ActDelete:
r.stats.updateDelete()
default:
return fmt.Errorf("invalid request's action %s for putxaction", req.Action)
}
jogger, ok := r.putJoggers[lom.Mountpath().Path]
if !ok {
debug.Assert(false, "invalid "+lom.Mountpath().String())
}
if cmn.Rom.FastV(4, cos.SmoduleEC) {
nlog.Infof("ECPUT (bg queue = %d): dispatching object %s....", len(jogger.putCh), lom)
}
if req.rebuild {
jogger.xactCh <- req
} else {
r.stats.updateQueue(len(jogger.putCh))
jogger.putCh <- req
}
return nil
}
func (r *XactPut) Run(*sync.WaitGroup) {
nlog.Infoln(r.Name())
var wg sync.WaitGroup
for _, jog := range r.putJoggers {
wg.Add(1)
go jog.run(&wg)
}
ticker := time.NewTicker(r.config.Periodic.StatsTime.D())
r.mainLoop(ticker)
ticker.Stop()
wg.Wait()
// not closing stream bundles as they are shared across EC xactions
r.Finish()
}
// all requests are equal, throttle TODO
func (r *XactPut) mainLoop(ticker *time.Ticker) {
for {
select {
case <-ticker.C:
if cmn.Rom.FastV(4, cos.SmoduleEC) {
if s := fmt.Sprintf("%v", r.Snap()); s != "" {
nlog.Infoln(s)
}
}
case <-r.IdleTimer():
// It's OK not to notify ecmanager, it'll just have stopped xctn in a map.
r.stop()
return
case msg := <-r.controlCh:
if msg.Action == ActEnableRequests {
r.setEcRequestsEnabled()
break
}
debug.Assert(msg.Action == ActClearRequests, msg.Action)
r.setEcRequestsDisabled()
r.stop()
return
case <-r.ChanAbort():
r.stop()
return
}
}
}
func (r *XactPut) Stop(err error) { r.Abort(err) }
func (r *XactPut) stop() {
r.DemandBase.Stop()
for _, jog := range r.putJoggers {
jog.stop()
}
// Don't close bundles, they are shared between bucket's EC actions
r.Finish()
}
// Encode schedules FQN for erasure coding process
func (r *XactPut) encode(req *request, lom *core.LOM) {
now := time.Now()
req.putTime, req.tm = now, now
if err := r.dispatchRequest(req, lom); err != nil {
nlog.Errorf("Failed to encode %s: %v", lom, err)
freeReq(req)
}
}
// Cleanup deletes all object slices or copies after the main object is removed
func (r *XactPut) cleanup(req *request, lom *core.LOM) {
now := time.Now()
req.putTime, req.tm = now, now
if err := r.dispatchRequest(req, lom); err != nil {
nlog.Errorf("Failed to cleanup %s: %v", lom, err)
freeReq(req)
}
}
func (r *XactPut) Snap() (snap *core.Snap) {
snap = r.baseSnap()
st := r.stats.stats()
snap.Ext = &ExtECPutStats{
AvgEncodeTime: cos.Duration(st.EncodeTime),
EncodeSize: st.EncodeSize,
EncodeCount: st.PutReq,
EncodeErrCount: st.EncodeErr,
AvgDeleteTime: cos.Duration(st.DeleteTime),
DeleteErrCount: st.DeleteErr,
DeleteCount: st.DelReq,
AvgObjTime: cos.Duration(st.ObjTime),
AvgQueueLen: st.QueueLen,
IsIdle: r.Pending() == 0,
}
snap.Stats.Objs = st.PutReq + st.DelReq // TODO: support in and out
snap.Stats.Bytes = st.EncodeSize
return
}