forked from NVIDIA/aistore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
351 lines (302 loc) · 11 KB
/
metric.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
// Package dsort provides distributed massively parallel resharding for very large datasets.
/*
* Copyright (c) 2018-2022, NVIDIA CORPORATION. All rights reserved.
*/
package dsort
import (
"math"
"sync"
"time"
"github.com/artashesbalabekyan/aistore/cmn/atomic"
"github.com/artashesbalabekyan/aistore/cmn/cos"
jsoniter "github.com/json-iterator/go"
)
const (
ExtractionPhase = "extraction"
SortingPhase = "sorting"
CreationPhase = "creation"
)
// TimeStats contains statistics about time spent on specific task. It calculates
// min, max and avg times.
type TimeStats struct {
// Total contains total number of milliseconds spend on
// specific task.
Total int64 `json:"total_ms,string"`
// Count contains number of time specific task was triggered.
Count int64 `json:"count,string"`
MinMs int64 `json:"min_ms,string"`
MaxMs int64 `json:"max_ms,string"`
AvgMs int64 `json:"avg_ms,string"`
}
// ThroughputStats contains statistics about throughput of specific task.
type ThroughputStats struct {
total int64
count int64
MinTp int64 `json:"min_throughput,string"`
MaxTp int64 `json:"max_throughput,string"`
AvgTp int64 `json:"avg_throughput,string"`
}
// DetailedStats contains time and throughput statistics .
type DetailedStats struct {
*TimeStats
*ThroughputStats
}
func newTimeStats() *TimeStats {
return &TimeStats{
MinMs: math.MaxInt64,
}
}
func (ts *TimeStats) updateTime(newTime time.Duration) {
t := newTime.Nanoseconds() / int64(time.Millisecond)
ts.Total += t
ts.Count++
ts.MinMs = cos.MinI64(ts.MinMs, t)
ts.MaxMs = cos.MaxI64(ts.MaxMs, t)
ts.AvgMs = ts.Total / ts.Count
}
func newThroughputStats() *ThroughputStats {
return &ThroughputStats{
MinTp: math.MaxInt64,
}
}
func (tps *ThroughputStats) updateThroughput(size int64, dur time.Duration) {
throughput := int64(float64(size) / dur.Seconds())
tps.total += throughput
tps.count++
tps.MinTp = cos.MinI64(tps.MinTp, throughput)
tps.MaxTp = cos.MaxI64(tps.MaxTp, throughput)
tps.AvgTp = tps.total / tps.count
}
func newDetailedStats() *DetailedStats {
return &DetailedStats{
newTimeStats(),
newThroughputStats(),
}
}
// PhaseInfo contains general stats and state for given phase. It is base struct
// which is extended by actual phases structs.
type PhaseInfo struct {
Start time.Time `json:"started_time"`
End time.Time `json:"end_time"`
// Elapsed time (in seconds) from start to given point of time or end when
// phase has finished.
Elapsed time.Duration `json:"elapsed"`
// Running specifies if phase is in progress.
Running bool `json:"running"`
// Finished specifies if phase has finished. If running and finished is
// false this means that the phase did not have started yet.
Finished bool `json:"finished"`
//
// private
//
mu sync.Mutex `json:"-"`
}
// begin marks phase as in progress.
func (pi *PhaseInfo) begin() {
pi.mu.Lock()
pi.Running = true
pi.Start = time.Now()
pi.mu.Unlock()
}
// finish marks phase as finished.
func (pi *PhaseInfo) finish() {
pi.mu.Lock()
pi.Running = false
pi.Finished = true
pi.End = time.Now()
pi.Elapsed = pi.End.Sub(pi.Start) / time.Second
pi.mu.Unlock()
}
// LocalExtraction contains metrics for first phase of DSort.
type LocalExtraction struct {
PhaseInfo
// TotalCnt is the number of shards DSort has to process in total.
TotalCnt int64 `json:"total_count,string"`
// ExtractedCnt describes number of extracted shards to given moment. At the
// end, number should be roughly equal to TotalCnt/#Targets.
ExtractedCnt int64 `json:"extracted_count,string"`
// ExtractedSize describes uncompressed size of extracted shards to given moment.
ExtractedSize int64 `json:"extracted_size,string"`
// ExtractedRecordCnt describes number of records extracted from all shards.
ExtractedRecordCnt int64 `json:"extracted_record_count,string"`
// ExtractedToDiskCnt describes number of shards extracted to the disk. To
// compute the number shards extracted to memory just subtract it from
// ExtractedCnt.
ExtractedToDiskCnt int64 `json:"extracted_to_disk_count,string"`
// ExtractedToDiskSize describes uncompressed size of extracted shards to disk
// to given moment.
ExtractedToDiskSize int64 `json:"extracted_to_disk_size,string"`
// ShardExtractionStats describes time statistics about single shard extraction.
ShardExtractionStats *DetailedStats `json:"single_shard_stats,omitempty"`
}
// MetaSorting contains metrics for second phase of DSort.
type MetaSorting struct {
PhaseInfo
// SentStats describes time statistics about records sending to another target
SentStats *TimeStats `json:"sent_stats,omitempty"`
// RecvStats describes time statistics about records receiving from another target
RecvStats *TimeStats `json:"recv_stats,omitempty"`
}
// ShardCreation contains metrics for third and last phase of DSort.
type ShardCreation struct {
PhaseInfo
// ToCreate specifies number of shards that have to be created in this phase.
ToCreate int64 `json:"to_create,string"`
// CreatedCnt specifies the number of shards that have been so far created.
// Should match ToCreate when phase is finished.
CreatedCnt int64 `json:"created_count,string"`
// MovedShardCnt specifies the number of shards that have migrated from this
// to another target in the cluster. Applies only when dealing with compressed
// data. Sometimes it is faster to create a shard on a specific target and send it
// over (rather than creating on a destination target).
MovedShardCnt int64 `json:"moved_shard_count,string"`
// RequestStats describes time statistics about request to other target.
RequestStats *TimeStats `json:"req_stats,omitempty"`
// ResponseStats describes time statistics about response to other target.
ResponseStats *TimeStats `json:"resp_stats,omitempty"`
// LocalSendStats describes time statistics about sending record content to other target.
LocalSendStats *DetailedStats `json:"local_send_stats,omitempty"`
// LocalRecvStats describes time statistics about receiving record content from other target.
LocalRecvStats *DetailedStats `json:"local_recv_stats,omitempty"`
// ShardCreationStats describes time statistics about single shard creation.
ShardCreationStats *DetailedStats `json:"single_shard_stats,omitempty"`
}
// Metrics is general struct which contains all stats about DSort run.
type Metrics struct {
Extraction *LocalExtraction `json:"local_extraction,omitempty"`
Sorting *MetaSorting `json:"meta_sorting,omitempty"`
Creation *ShardCreation `json:"shard_creation,omitempty"`
// Aborted specifies if the DSort has been aborted or not.
Aborted atomic.Bool `json:"aborted,omitempty"`
// Archived specifies if the DSort has been archived to persistent storage.
Archived atomic.Bool `json:"archived,omitempty"`
// Description of the job.
Description string `json:"description,omitempty"`
// Warnings which were produced during the job.
Warnings []string `json:"warnings,omitempty"`
// Errors which happened during the job.
Errors []string `json:"errors,omitempty"`
// extended determines if we should calculate and send extended metrics like
// request/response times.
extended bool
}
// newMetrics creates new Metrics instance.
func newMetrics(description string, extended bool) *Metrics {
extraction := &LocalExtraction{}
sorting := &MetaSorting{}
creation := &ShardCreation{}
if extended {
extraction.ShardExtractionStats = newDetailedStats()
creation.RequestStats = newTimeStats()
creation.ResponseStats = newTimeStats()
creation.LocalSendStats = newDetailedStats()
creation.LocalRecvStats = newDetailedStats()
creation.ShardCreationStats = newDetailedStats()
}
sorting.SentStats = newTimeStats()
sorting.RecvStats = newTimeStats()
return &Metrics{
extended: extended,
Description: description,
Extraction: extraction,
Sorting: sorting,
Creation: creation,
}
}
// setAbortedTo updates aborted state of DSort.
func (m *Metrics) setAbortedTo(b bool) {
m.Aborted.Store(b)
}
// Lock locks all phases to make sure that all of them can be updated.
func (m *Metrics) lock() {
m.Extraction.mu.Lock()
m.Sorting.mu.Lock()
m.Creation.mu.Lock()
}
// Unlock unlocks all phases.
func (m *Metrics) unlock() {
m.Creation.mu.Unlock()
m.Sorting.mu.Unlock()
m.Extraction.mu.Unlock()
}
func (m *Metrics) ElapsedTime() time.Duration {
return m.Creation.End.Sub(m.Extraction.Start)
}
// update updates elapsed time for all the metrics.
//
// NOTE: This should be done every time Metrics are about to be marshaled and
// sent through the network.
func (m *Metrics) update() {
m.lock()
if m.Extraction.End.IsZero() && !m.Extraction.Start.IsZero() {
m.Extraction.Elapsed = time.Since(m.Extraction.Start) / time.Second
}
if m.Sorting.End.IsZero() && !m.Sorting.Start.IsZero() {
m.Sorting.Elapsed = time.Since(m.Sorting.Start) / time.Second
}
if m.Creation.End.IsZero() && !m.Creation.Start.IsZero() {
m.Creation.Elapsed = time.Since(m.Creation.Start) / time.Second
}
m.unlock()
}
func (m *Metrics) Marshal() []byte {
m.lock()
b, err := jsoniter.Marshal(m)
m.unlock()
cos.AssertNoErr(err)
return b
}
// JobInfo is a struct that contains stats that represent the DSort run in a list
type JobInfo struct {
ID string `json:"id"`
Description string `json:"description"`
StartedTime time.Time `json:"started_time,omitempty"`
FinishTime time.Time `json:"finish_time,omitempty"`
ExtractedDuration time.Duration `json:"started_meta_sorting,omitempty"`
SortingDuration time.Duration `json:"started_shard_creation,omitempty"`
CreationDuration time.Duration `json:"finished_shard_creation,omitempty"`
Aborted bool `json:"aborted"`
Archived bool `json:"archived"`
}
func (m *Metrics) ToJobInfo(id string) JobInfo {
return JobInfo{
ID: id,
StartedTime: m.Extraction.Start,
FinishTime: m.Creation.End,
ExtractedDuration: m.Extraction.Elapsed,
SortingDuration: m.Sorting.Elapsed,
CreationDuration: m.Creation.Elapsed,
Aborted: m.Aborted.Load(),
Archived: m.Archived.Load(),
Description: m.Description,
}
}
func (j *JobInfo) Aggregate(other *JobInfo) {
j.StartedTime = startTime(j.StartedTime, other.StartedTime)
j.FinishTime = stopTime(j.FinishTime, other.FinishTime)
j.ExtractedDuration = cos.MaxDuration(j.ExtractedDuration, other.ExtractedDuration)
j.SortingDuration = cos.MaxDuration(j.SortingDuration, other.SortingDuration)
j.CreationDuration = cos.MaxDuration(j.CreationDuration, other.CreationDuration)
j.Aborted = j.Aborted || other.Aborted
j.Archived = j.Archived && other.Archived
}
func (j *JobInfo) IsRunning() bool {
return !j.Aborted && !j.Archived
}
func (j *JobInfo) IsFinished() bool {
return !j.IsRunning()
}
// startTime returns the start time of a,b. If either is zero, the other takes precedence.
func startTime(a, b time.Time) time.Time {
if (a.Before(b) && !a.IsZero()) || b.IsZero() {
return a
}
return b
}
// stopTime returns the stop time of a,b. If either is zero it's unknown and returns 0.
func stopTime(a, b time.Time) time.Time {
if (a.After(b) && !b.IsZero()) || a.IsZero() {
return a
}
return b
}