-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathdblab.go
More file actions
255 lines (210 loc) · 6.71 KB
/
dblab.go
File metadata and controls
255 lines (210 loc) · 6.71 KB
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
/*
2025 © PostgresAI
*/
package rdsrefresh
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"gitlab.com/postgres-ai/database-lab/v3/pkg/client/dblabapi"
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
"gitlab.com/postgres-ai/database-lab/v3/pkg/models"
"gitlab.com/postgres-ai/database-lab/v3/pkg/util/projection"
)
const (
defaultRequestTimeout = 60 * time.Second
)
// DBLabClient wraps the dblabapi client with additional methods for config management.
type DBLabClient struct {
client *dblabapi.Client
}
// NewDBLabClient creates a new DBLab API client wrapper.
func NewDBLabClient(cfg *DBLabConfig) (*DBLabClient, error) {
if cfg.Insecure {
log.Warn("TLS certificate verification is disabled. This is insecure for production use.")
}
client, err := dblabapi.NewClient(dblabapi.Options{
Host: cfg.APIEndpoint,
VerificationToken: cfg.Token,
Insecure: cfg.Insecure,
RequestTimeout: defaultRequestTimeout,
})
if err != nil {
return nil, fmt.Errorf("failed to create DBLab API client: %w", err)
}
return &DBLabClient{
client: client,
}, nil
}
// GetStatus returns the current DBLab Engine instance status.
func (c *DBLabClient) GetStatus(ctx context.Context) (*models.InstanceStatus, error) {
return c.client.Status(ctx)
}
// Health checks if the DBLab Engine is healthy.
func (c *DBLabClient) Health(ctx context.Context) error {
_, err := c.client.Health(ctx)
return err
}
// TriggerFullRefresh triggers a full data refresh on the DBLab Engine.
func (c *DBLabClient) TriggerFullRefresh(ctx context.Context) error {
_, err := c.client.FullRefresh(ctx)
return err
}
// IsRefreshInProgress checks if a refresh is currently in progress.
func (c *DBLabClient) IsRefreshInProgress(ctx context.Context) (bool, error) {
status, err := c.GetStatus(ctx)
if err != nil {
return false, err
}
switch status.Retrieving.Status {
case models.Refreshing, models.Snapshotting, models.Pending, models.Renewed:
return true, nil
default:
return false, nil
}
}
// WaitForRefreshComplete polls the DBLab status until refresh is complete or timeout.
// It first waits for the refresh to start (status changes from finished/inactive),
// then waits for it to complete. This prevents race conditions where stale status
// from a previous refresh could cause premature return.
func (c *DBLabClient) WaitForRefreshComplete(ctx context.Context, pollInterval, timeout time.Duration) error {
timeoutTimer := time.NewTimer(timeout)
defer timeoutTimer.Stop()
refreshStarted := false
// checkStatus handles status evaluation and returns (done, error)
checkStatus := func() (bool, error) {
status, err := c.GetStatus(ctx)
if err != nil {
return false, fmt.Errorf("failed to get status: %w", err)
}
switch status.Retrieving.Status {
case models.Refreshing, models.Snapshotting, models.Renewed, models.Pending:
refreshStarted = true
return false, nil
case models.Finished:
if !refreshStarted {
return false, nil
}
return true, nil
case models.Failed:
if !refreshStarted {
return false, nil
}
if alert, ok := status.Retrieving.Alerts[models.RefreshFailed]; ok {
return false, fmt.Errorf("refresh failed: %s", alert.Message)
}
// fallback to any available alert if RefreshFailed not present
for _, alert := range status.Retrieving.Alerts {
return false, fmt.Errorf("refresh failed: %s", alert.Message)
}
return false, fmt.Errorf("refresh failed (no details available)")
case models.Inactive:
if refreshStarted {
return false, fmt.Errorf("refresh stopped unexpectedly (status: inactive)")
}
return false, nil
default:
return false, nil
}
}
// immediate first check
done, err := checkStatus()
if err != nil {
return err
}
if done {
return nil
}
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-timeoutTimer.C:
if !refreshStarted {
return fmt.Errorf("timeout waiting for refresh to start after %v", timeout)
}
return fmt.Errorf("timeout waiting for refresh to complete after %v", timeout)
case <-ticker.C:
done, err := checkStatus()
if err != nil {
return err
}
if done {
return nil
}
}
}
}
// SourceConfigUpdate contains source database connection parameters for config update.
type SourceConfigUpdate struct {
Host string
Port int
DBName string
Username string
Password string
// RDSIAMDBInstance is the RDS DB instance identifier for IAM auth. When empty, this field is omitted from the config update.
RDSIAMDBInstance string
// DumpParallelJobs sets the -j flag for pg_dump. When zero, the existing value is preserved.
DumpParallelJobs int
// RestoreParallelJobs sets the -j flag for pg_restore. When zero, the existing value is preserved.
RestoreParallelJobs int
}
// UpdateSourceConfig updates the source database connection in DBLab config.
// DBLab automatically reloads the configuration after the update.
func (c *DBLabClient) UpdateSourceConfig(ctx context.Context, update SourceConfigUpdate) error {
port64 := int64(update.Port)
proj := models.ConfigProjection{
Host: &update.Host,
Port: &port64,
DBName: &update.DBName,
Username: &update.Username,
Password: &update.Password,
}
if update.RDSIAMDBInstance != "" {
proj.RDSIAMDBInstance = &update.RDSIAMDBInstance
}
if update.DumpParallelJobs > 0 {
dumpJobs := int64(update.DumpParallelJobs)
proj.DumpParallelJobs = &dumpJobs
}
if update.RestoreParallelJobs > 0 {
restoreJobs := int64(update.RestoreParallelJobs)
proj.RestoreParallelJobs = &restoreJobs
}
nested := map[string]interface{}{}
// defensive error check: StoreJSON only fails if target is not an addressable struct,
// which cannot happen here since proj is always a valid ConfigProjection value.
if err := projection.StoreJSON(&proj, nested, projection.StoreOptions{
Groups: []string{"default", "sensitive"},
}); err != nil {
return fmt.Errorf("failed to build config projection: %w", err)
}
bodyBytes, err := json.Marshal(nested)
if err != nil {
return fmt.Errorf("failed to marshal config update: %w", err)
}
url := c.client.URL("/admin/config")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url.String(), bytes.NewReader(bodyBytes))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(ctx, req)
if err != nil {
return fmt.Errorf("failed to update DBLab config: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode >= http.StatusBadRequest {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("failed to update DBLab config: HTTP %d: %s", resp.StatusCode, string(body))
}
return nil
}