-
Notifications
You must be signed in to change notification settings - Fork 210
/
mgr-JobMgr.go
368 lines (317 loc) · 14.5 KB
/
mgr-JobMgr.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright © 2017 Microsoft <wastore@microsoft.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package ste
import (
"context"
"fmt"
"sync"
"sync/atomic"
"github.com/Azure/azure-pipeline-go/pipeline"
"github.com/Azure/azure-storage-azcopy/common"
)
var _ IJobMgr = &jobMgr{}
type PartNumber = common.PartNumber
// InMemoryTransitJobState defines job state transit in memory, and not in JobPartPlan file.
// Note: InMemoryTransitJobState should only be set when request come from cmd(FE) module to STE module.
// In memory CredentialInfo is currently maintained per job in STE, as FE could have many-to-one relationship with STE,
// i.e. different jobs could have different OAuth tokens requested from FE, and these jobs can run at same time in STE.
// This can be optimized if FE would no more be another module vs STE module.
type InMemoryTransitJobState struct {
credentialInfo common.CredentialInfo
}
type IJobMgr interface {
JobID() common.JobID
JobPartMgr(partNum PartNumber) (IJobPartMgr, bool)
//Throughput() XferThroughput
AddJobPart(partNum PartNumber, planFile JobPartPlanFileName, sourceSAS string,
destinationSAS string, scheduleTransfers bool) IJobPartMgr
SetIncludeExclude(map[string]int, map[string]int)
IncludeExclude() (map[string]int, map[string]int)
ResumeTransfers(appCtx context.Context)
AllTransfersScheduled() bool
ConfirmAllTransfersScheduled()
ResetAllTransfersScheduled()
PipelineLogInfo() pipeline.LogOptions
ReportJobPartDone() uint32
Context() context.Context
Cancel()
// TODO: added for debugging purpose. remove later
OccupyAConnection()
// TODO: added for debugging purpose. remove later
ReleaseAConnection()
// TODO: added for debugging purpose. remove later
ActiveConnections() int64
//Close()
getInMemoryTransitJobState() InMemoryTransitJobState // get in memory transit job state saved in this job.
setInMemoryTransitJobState(state InMemoryTransitJobState) // set in memory transit job state saved in this job.
common.ILoggerCloser
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func newJobMgr(appLogger common.ILogger, jobID common.JobID, appCtx context.Context, level common.LogLevel, commandString string, logFileFolder string) IJobMgr {
// atomicAllTransfersScheduled is set to 1 since this api is also called when new job part is ordered.
jm := jobMgr{jobID: jobID, jobPartMgrs: newJobPartToJobPartMgr(), include: map[string]int{}, exclude: map[string]int{},
logger: common.NewJobLogger(jobID, level, appLogger, logFileFolder) /*Other fields remain zero-value until this job is scheduled */}
jm.reset(appCtx, commandString)
return &jm
}
func (jm *jobMgr) reset(appCtx context.Context, commandString string) IJobMgr {
jm.logger.OpenLog()
// log the user given command to the job log file.
// since the log file is opened in case of resume, list and many other operations
// for which commandString passed is empty, the length check is added
if len(commandString) > 0 {
jm.logger.Log(pipeline.LogInfo, fmt.Sprintf("Job-Command %s", commandString))
}
jm.ctx, jm.cancel = context.WithCancel(appCtx)
atomic.StoreUint64(&jm.atomicNumberOfBytesCovered, 0)
atomic.StoreUint64(&jm.atomicTotalBytesToXfer, 0)
jm.partsDone = 0
return jm
}
// jobMgr represents the runtime information for a Job
type jobMgr struct {
logger common.ILoggerResetable
jobID common.JobID // The Job's unique ID
ctx context.Context
cancel context.CancelFunc
jobPartMgrs jobPartToJobPartMgr // The map of part #s to JobPartMgrs
// partsDone keep the count of completed part of the Job.
partsDone uint32
//throughput common.CountPerSecond // TODO: Set LastCheckedTime to now
inMemoryTransitJobState InMemoryTransitJobState
// list of transfer mentioned to include only then while resuming the job
include map[string]int
// list of transfer mentioned to exclude while resuming the job
exclude map[string]int
finalPartOrdered bool
// atomicAllTransfersScheduled defines whether all job parts have been iterated and resumed or not
// atomicAllTransfersScheduled is int32 since atomic load and store operations have to be performed
atomicAllTransfersScheduled int32
atomicNumberOfBytesCovered uint64
atomicTotalBytesToXfer uint64
// atomicCurrentConcurrentConnections defines the number of active goroutines performing the transfer / executing the chunk func
// TODO: added for debugging purpose. remove later
atomicCurrentConcurrentConnections int64
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func (jm *jobMgr) Progress() (uint64, uint64) {
return atomic.LoadUint64(&jm.atomicNumberOfBytesCovered),
atomic.LoadUint64(&jm.atomicTotalBytesToXfer)
}
//func (jm *jobMgr) Throughput() XferThroughput { return jm.throughput }
// JobID returns the JobID that this jobMgr managers
func (jm *jobMgr) JobID() common.JobID { return jm.jobID }
// JobPartMgr looks up a job's part
func (jm *jobMgr) JobPartMgr(partNumber PartNumber) (IJobPartMgr, bool) {
return jm.jobPartMgrs.Get(partNumber)
}
// Add 1 to the active number of goroutine performing the transfer or executing the chunkFunc
// TODO: added for debugging purpose. remove later
func (jm *jobMgr) OccupyAConnection() {
atomic.AddInt64(&jm.atomicCurrentConcurrentConnections, 1)
}
// Sub 1 from the active number of goroutine performing the transfer or executing the chunkFunc
// TODO: added for debugging purpose. remove later
func (jm *jobMgr) ReleaseAConnection() {
atomic.AddInt64(&jm.atomicCurrentConcurrentConnections, -1)
}
// returns the number of goroutines actively performing the transfer / executing the chunkFunc
// TODO: added for debugging purpose. remove later
func (jm *jobMgr) ActiveConnections() int64 {
return atomic.LoadInt64(&jm.atomicCurrentConcurrentConnections)
}
// initializeJobPartPlanInfo func initializes the JobPartPlanInfo handler for given JobPartOrder
func (jm *jobMgr) AddJobPart(partNum PartNumber, planFile JobPartPlanFileName, sourceSAS string,
destinationSAS string, scheduleTransfers bool) IJobPartMgr {
jpm := &jobPartMgr{jobMgr: jm, filename: planFile, sourceSAS: sourceSAS,
destinationSAS: destinationSAS, pacer: JobsAdmin.(*jobsAdmin).pacer}
jpm.planMMF = jpm.filename.Map()
jm.jobPartMgrs.Set(partNum, jpm)
jm.finalPartOrdered = jpm.planMMF.Plan().IsFinalPart
if scheduleTransfers {
// If the schedule transfer is set to true
// Instead of the scheduling the Transfer for given JobPart
// JobPart is put into the partChannel
// from where it is picked up and scheduled
//jpm.ScheduleTransfers(jm.ctx, make(map[string]int), make(map[string]int))
JobsAdmin.QueueJobParts(jpm)
}
return jpm
}
// SetIncludeExclude sets the include / exclude list of transfers
// supplied with resume command to include or exclude mentioned transfers
func (jm *jobMgr) SetIncludeExclude(include, exclude map[string]int) {
jm.include = include
jm.exclude = exclude
}
// Returns the list of transfer mentioned to include / exclude
func (jm *jobMgr) IncludeExclude() (map[string]int, map[string]int) {
return jm.include, jm.exclude
}
// ScheduleTransfers schedules this job part's transfers. It is called when a new job part is ordered & is also called to resume a paused Job
func (jm *jobMgr) ResumeTransfers(appCtx context.Context) {
jm.reset(appCtx, "")
// Since while creating the JobMgr, atomicAllTransfersScheduled is set to true
// reset it to false while resuming it
//jm.ResetAllTransfersScheduled()
jm.jobPartMgrs.Iterate(false, func(p common.PartNumber, jpm IJobPartMgr) {
JobsAdmin.QueueJobParts(jpm)
//jpm.ScheduleTransfers(jm.ctx, includeTransfer, excludeTransfer)
})
}
// AllTransfersScheduled returns whether Job has completely resumed or not
func (jm *jobMgr) AllTransfersScheduled() bool {
return atomic.LoadInt32(&jm.atomicAllTransfersScheduled) == 1
}
// ConfirmAllTransfersScheduled sets the atomicAllTransfersScheduled to true
func (jm *jobMgr) ConfirmAllTransfersScheduled() {
atomic.StoreInt32(&jm.atomicAllTransfersScheduled, 1)
}
// ResetAllTransfersScheduled sets the ResetAllTransfersScheduled to false
func (jm *jobMgr) ResetAllTransfersScheduled() {
atomic.StoreInt32(&jm.atomicAllTransfersScheduled, 0)
}
// ReportJobPartDone is called to report that a job part completed or failed
func (jm *jobMgr) ReportJobPartDone() uint32 {
shouldLog := jm.ShouldLog(pipeline.LogInfo)
partsDone := atomic.AddUint32(&jm.partsDone, 1)
// If the last part is still awaited or other parts all still not complete,
// JobPart 0 status is not changed.
if partsDone != jm.jobPartMgrs.Count() || !jm.finalPartOrdered {
if shouldLog {
jm.Log(pipeline.LogInfo, fmt.Sprintf("is part of Job which %d total number of parts done ", partsDone))
}
return partsDone
}
if shouldLog {
jm.Log(pipeline.LogInfo, fmt.Sprintf("all parts of Job %s successfully completed, cancelled or paused", jm.jobID.String()))
}
jobPart0Mgr, ok := jm.jobPartMgrs.Get(0)
if !ok {
jm.Panic(fmt.Errorf("Failed to find Job %v, Part #0", jm.jobID))
}
switch part0Plan := jobPart0Mgr.Plan(); part0Plan.JobStatus() {
case common.EJobStatus.Cancelling():
part0Plan.SetJobStatus(common.EJobStatus.Cancelled())
if shouldLog {
jm.Log(pipeline.LogInfo, fmt.Sprintf("all parts of Job %v successfully cancelled; cleaning up the Job", jm.jobID))
}
//jm.jobsInfo.cleanUpJob(jm.jobID)
case common.EJobStatus.InProgress():
part0Plan.SetJobStatus((common.EJobStatus).Completed())
}
return partsDone
}
func (jm *jobMgr) getInMemoryTransitJobState() InMemoryTransitJobState {
return jm.inMemoryTransitJobState
}
// Note: InMemoryTransitJobState should only be set when request come from cmd(FE) module to STE module.
// And the state should no more be changed inside STE module.
func (jm *jobMgr) setInMemoryTransitJobState(state InMemoryTransitJobState) {
jm.inMemoryTransitJobState = state
}
func (jm *jobMgr) Context() context.Context { return jm.ctx }
func (jm *jobMgr) Cancel() { jm.cancel() }
func (jm *jobMgr) ShouldLog(level pipeline.LogLevel) bool { return jm.logger.ShouldLog(level) }
func (jm *jobMgr) Log(level pipeline.LogLevel, msg string) { jm.logger.Log(level, msg) }
func (jm *jobMgr) PipelineLogInfo() pipeline.LogOptions {
return pipeline.LogOptions{
Log: jm.Log,
ShouldLog: func(level pipeline.LogLevel) bool { return level <= jm.logger.MinimumLogLevel() },
}
}
func (jm *jobMgr) Panic(err error) { jm.logger.Panic(err) }
func (jm *jobMgr) CloseLog() { jm.logger.CloseLog() }
// PartsDone returns the number of the Job's parts that are either completed or failed
//func (jm *jobMgr) PartsDone() uint32 { return atomic.LoadUint32(&jm.partsDone) }
// SetPartsDone sets the number of Job's parts that are done (completed or failed)
//func (jm *jobMgr) SetPartsDone(partsDone uint32) { atomic.StoreUint32(&jm.partsDone, partsDone) }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type jobPartToJobPartMgr struct {
nocopy common.NoCopy
lock sync.RWMutex
m map[PartNumber]IJobPartMgr
}
func newJobPartToJobPartMgr() jobPartToJobPartMgr {
return jobPartToJobPartMgr{m: make(map[PartNumber]IJobPartMgr)}
}
func (m *jobPartToJobPartMgr) Count() uint32 {
m.nocopy.Check()
m.lock.RLock()
count := uint32(len(m.m))
m.lock.RUnlock()
return count
}
func (m *jobPartToJobPartMgr) Set(key common.PartNumber, value IJobPartMgr) {
m.nocopy.Check()
m.lock.Lock()
m.m[key] = value
m.lock.Unlock()
}
func (m *jobPartToJobPartMgr) Get(key common.PartNumber) (value IJobPartMgr, ok bool) {
m.nocopy.Check()
m.lock.RLock()
value, ok = m.m[key]
m.lock.RUnlock()
return
}
func (m *jobPartToJobPartMgr) Delete(key common.PartNumber) {
m.nocopy.Check()
m.lock.Lock()
delete(m.m, key)
m.lock.Unlock()
}
// We purposely disallow len
func (m *jobPartToJobPartMgr) Iterate(readonly bool, f func(k common.PartNumber, v IJobPartMgr)) {
m.nocopy.Check()
locker := sync.Locker(&m.lock)
if readonly {
locker = m.lock.RLocker()
}
locker.Lock()
for k, v := range m.m {
f(k, v)
}
locker.Unlock()
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ThroughputState struct holds the attribute to monitor the through of an existing JobOrder
//type XferThroughput struct {
// lastCheckedTime time.Time
// lastCheckedBytes int64
// currentBytes int64
//}
// getLastCheckedTime api returns the lastCheckedTime of ThroughputState instance in thread-safe manner
/*func (t *XferThroughput) LastCheckedTime() time.Time { return t.lastCheckedTime }
// updateLastCheckTime api updates the lastCheckedTime of ThroughputState instance in thread-safe manner
func (t *XferThroughput) SetLastCheckTime(currentTime time.Time) { t.lastCheckedTime = currentTime }
// getLastCheckedBytes api returns the lastCheckedBytes of ThroughputState instance in thread-safe manner
func (t *XferThroughput) LastCheckedBytes() int64 { return atomic.LoadInt64(&t.lastCheckedBytes) }
// updateLastCheckedBytes api updates the lastCheckedBytes of ThroughputState instance in thread-safe manner
func (t *XferThroughput) SetLastCheckedBytes(bytes int64) {
atomic.StoreInt64(&t.lastCheckedBytes, bytes)
}
// getCurrentBytes api returns the currentBytes of ThroughputState instance in thread-safe manner
func (t *XferThroughput) CurrentBytes() int64 { return atomic.LoadInt64(&t.currentBytes) }
// updateCurrentBytes api adds the value in currentBytes of ThroughputState instance in thread-safe manner
func (t *XferThroughput) SetCurrentBytes(bytes int64) int64 {
return atomic.AddInt64(&t.currentBytes, bytes)
}
*/