-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathbilling.go
181 lines (140 loc) · 4.17 KB
/
billing.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
// Package billing manages the billing data for the instance.
package billing
import (
"context"
"errors"
"fmt"
"runtime"
"sync"
"time"
"github.com/pbnjay/memory"
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/pool"
"gitlab.com/postgres-ai/database-lab/v3/pkg/client/platform"
"gitlab.com/postgres-ai/database-lab/v3/pkg/config/global"
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
"gitlab.com/postgres-ai/database-lab/v3/pkg/models"
)
const errorsSoftLimit = 2
// Billing manages the billing data.
type Billing struct {
platform *platform.Client
props *global.EngineProps
pm *pool.Manager
mu *sync.Mutex
softFails int
}
// New creates a new Billing struct.
func New(platform *platform.Client, props *global.EngineProps, pm *pool.Manager) *Billing {
return &Billing{platform: platform, props: props, pm: pm, mu: &sync.Mutex{}}
}
// Reload updates platform client.
func (b *Billing) Reload(platformSvc *platform.Client) {
b.platform = platformSvc
}
func (b *Billing) increaseFailureCounter() int {
b.mu.Lock()
defer b.mu.Unlock()
b.softFails++
return b.softFails
}
func (b *Billing) softLimitCounter() int {
b.mu.Lock()
defer b.mu.Unlock()
return b.softFails
}
func (b *Billing) isSoftLimitExceeded() bool {
b.mu.Lock()
defer b.mu.Unlock()
return b.softFails > errorsSoftLimit
}
func (b *Billing) resetSoftFailureCounter() {
b.mu.Lock()
b.softFails = 0
b.mu.Unlock()
}
// RegisterInstance registers instance on the Platform.
func (b *Billing) RegisterInstance(ctx context.Context, systemMetrics models.System) error {
if b.props.IsAWS() {
// Because billing goes through AWS Marketplace.
b.props.UpdateBilling(true)
}
if err := b.shouldSendPlatformRequests(); err != nil {
return err
}
if err := b.platform.RegisterInstance(ctx, b.props); err != nil {
return fmt.Errorf("cannot register instance: %w", err)
}
// To check billing state immediately.
return b.SendUsage(ctx, systemMetrics)
}
// CollectUsage periodically collects usage statistics of the instance.
func (b *Billing) CollectUsage(ctx context.Context, system models.System) {
if err := b.shouldSendPlatformRequests(); err != nil {
log.Msg("Skip collecting usage:", err.Error())
return
}
ticker := time.NewTicker(time.Hour)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
poolStat := b.pm.CollectPoolStat()
if err := b.SendUsage(ctx, models.System{
CPU: system.CPU,
TotalMemory: system.TotalMemory,
DataUsed: poolStat.TotalUsed,
}); err != nil {
log.Err("collecting usage:", err)
}
}
}
}
// SendUsage sends usage events.
func (b *Billing) SendUsage(ctx context.Context, systemMetrics models.System) error {
respData, err := b.platform.SendUsage(ctx, b.props, platform.InstanceUsage{
InstanceID: b.props.InstanceID,
EventData: platform.DataUsage{
CPU: systemMetrics.CPU,
TotalMemory: systemMetrics.TotalMemory,
DataSize: systemMetrics.DataUsed,
}})
if err != nil {
b.increaseFailureCounter()
if b.isSoftLimitExceeded() {
log.Msg("Billing error threshold surpassed. Certain features have been temporarily disabled.")
b.props.UpdateBilling(false)
}
return fmt.Errorf("cannot send usage event: %w. Attempts: %d", err, b.softLimitCounter())
}
if b.props.BillingActive != respData.BillingActive {
b.props.UpdateBilling(respData.BillingActive)
log.Dbg("Instance state updated. Billing is active:", respData.BillingActive)
}
if b.props.BillingActive {
b.resetSoftFailureCounter()
}
return nil
}
func (b *Billing) shouldSendPlatformRequests() error {
if b.props.IsAWS() {
return errors.New("DLE infrastructure is AWS Marketplace")
}
if b.props.GetEdition() != global.StandardEdition {
return errors.New("DLE edition is not Standard")
}
if !b.platform.HasOrgKey() {
return errors.New("organization key is empty")
}
return nil
}
// GetSystemMetrics collects system metrics significant for billing purposes.
func GetSystemMetrics(pm *pool.Manager) models.System {
poolStat := pm.CollectPoolStat()
systemMetrics := models.System{
CPU: runtime.NumCPU(),
TotalMemory: memory.TotalMemory(),
DataUsed: poolStat.TotalUsed,
}
return systemMetrics
}