/
usage.go
86 lines (78 loc) · 2.81 KB
/
usage.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package queue
import (
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/erda-project/erda-proto-go/pipeline/pb"
"github.com/erda-project/erda/modules/pipeline/pipengine/queue/priorityqueue"
"github.com/erda-project/erda/pkg/numeral"
)
func (q *defaultQueue) Usage() pb.QueueUsage {
q.lock.RLock()
defer q.lock.RUnlock()
// processing
var (
inUseCPU float64
inUseMemoryMB float64
processingDetails = make([]*pb.QueueUsageItem, 0)
)
q.eq.ProcessingQueue().Range(func(item priorityqueue.Item) (stopRange bool) {
pipelineID := parsePipelineIDFromQueueItem(item)
existP := q.pipelineCaches[pipelineID]
if existP == nil {
return false
}
resources := existP.GetPipelineAppliedResources()
inUseCPU += resources.Requests.CPU
inUseMemoryMB += resources.Requests.MemoryMB
processingDetails = append(processingDetails, &pb.QueueUsageItem{
PipelineID: pipelineID,
RequestsCPU: resources.Requests.CPU,
RequestsMemoryMB: resources.Requests.MemoryMB,
Index: int64(item.Index()),
Priority: item.Priority(),
AddedTime: timestamppb.New(item.CreationTime()),
})
return false
})
// pending
var pendingDetails = make([]*pb.QueueUsageItem, 0)
q.eq.PendingQueue().Range(func(item priorityqueue.Item) (stopRange bool) {
pipelineID := parsePipelineIDFromQueueItem(item)
existP := q.pipelineCaches[pipelineID]
if existP == nil {
return false
}
resources := existP.GetPipelineAppliedResources()
pendingDetails = append(pendingDetails, &pb.QueueUsageItem{
PipelineID: pipelineID,
RequestsCPU: resources.Requests.CPU,
RequestsMemoryMB: resources.Requests.MemoryMB,
Index: int64(item.Index()),
Priority: item.Priority(),
AddedTime: timestamppb.New(time.Now()),
})
return false
})
return pb.QueueUsage{
InUseCPU: inUseCPU,
InUseMemoryMB: inUseMemoryMB,
RemainingCPU: numeral.SubFloat64(q.pq.MaxCPU, inUseCPU),
RemainingMemoryMB: numeral.SubFloat64(q.pq.MaxMemoryMB, inUseMemoryMB),
ProcessingCount: int64(len(processingDetails)),
PendingCount: int64(len(pendingDetails)),
ProcessingDetails: processingDetails,
PendingDetails: pendingDetails,
}
}