-
Notifications
You must be signed in to change notification settings - Fork 7
/
cluster.go
296 lines (243 loc) · 7.65 KB
/
cluster.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
package materialize
import (
"database/sql"
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
)
// DDL
type ClusterBuilder struct {
ddl Builder
clusterName string
replicationFactor *int
size string
disk bool
availabilityZones []string
introspectionInterval string
introspectionDebugging bool
schedulingConfig SchedulingConfig
}
func NewClusterBuilder(conn *sqlx.DB, obj MaterializeObject) *ClusterBuilder {
return &ClusterBuilder{
ddl: Builder{conn, Cluster},
clusterName: obj.Name,
}
}
type SchedulingConfig struct {
OnRefresh OnRefreshConfig
}
type OnRefreshConfig struct {
Enabled bool
RehydrationTimeEstimate string
}
func GetSchedulingConfig(v interface{}) SchedulingConfig {
if v == nil {
return SchedulingConfig{}
}
configSlice, ok := v.([]interface{})
if !ok || len(configSlice) == 0 {
return SchedulingConfig{}
}
configMap, ok := configSlice[0].(map[string]interface{})
if !ok {
return SchedulingConfig{}
}
onRefreshSlice, ok := configMap["on_refresh"].([]interface{})
if !ok || len(onRefreshSlice) == 0 {
return SchedulingConfig{}
}
onRefreshMap, ok := onRefreshSlice[0].(map[string]interface{})
if !ok {
return SchedulingConfig{}
}
return SchedulingConfig{
OnRefresh: OnRefreshConfig{
Enabled: onRefreshMap["enabled"].(bool),
RehydrationTimeEstimate: onRefreshMap["rehydration_time_estimate"].(string),
},
}
}
func (b *ClusterBuilder) QualifiedName() string {
return QualifiedName(b.clusterName)
}
func (b *ClusterBuilder) ReplicationFactor(r *int) *ClusterBuilder {
b.replicationFactor = r
return b
}
func (b *ClusterBuilder) Size(s string) *ClusterBuilder {
b.size = s
return b
}
func (b *ClusterBuilder) Disk(disk bool) *ClusterBuilder {
b.disk = disk
return b
}
func (b *ClusterBuilder) AvailabilityZones(z []string) *ClusterBuilder {
b.availabilityZones = z
return b
}
func (b *ClusterBuilder) IntrospectionInterval(i string) *ClusterBuilder {
b.introspectionInterval = i
return b
}
func (b *ClusterBuilder) IntrospectionDebugging() *ClusterBuilder {
b.introspectionDebugging = true
return b
}
func (b *ClusterBuilder) Scheduling(v []interface{}) *ClusterBuilder {
b.schedulingConfig = GetSchedulingConfig(v)
return b
}
func (b *ClusterBuilder) Create() error {
q := strings.Builder{}
q.WriteString(fmt.Sprintf(`CREATE CLUSTER %s`, b.QualifiedName()))
// Only create empty clusters, manage replicas with separate resource if size is not set
if b.size != "" {
q.WriteString(fmt.Sprintf(` SIZE %s`, QuoteString(b.size)))
var p []string
if b.disk {
i := fmt.Sprintf(` DISK`)
p = append(p, i)
}
if b.replicationFactor != nil {
i := fmt.Sprintf(` REPLICATION FACTOR %v`, *b.replicationFactor)
p = append(p, i)
}
if len(b.availabilityZones) > 0 {
var az []string
for _, z := range b.availabilityZones {
f := QuoteString(z)
az = append(az, f)
}
a := fmt.Sprintf(` AVAILABILITY ZONES = [%s]`, strings.Join(az[:], ","))
p = append(p, a)
}
if b.introspectionInterval != "" {
i := fmt.Sprintf(` INTROSPECTION INTERVAL = %s`, QuoteString(b.introspectionInterval))
p = append(p, i)
}
if b.introspectionDebugging {
p = append(p, ` INTROSPECTION DEBUGGING = TRUE`)
}
if b.schedulingConfig.OnRefresh.Enabled {
scheduleStatement := " SCHEDULE = ON REFRESH"
if b.schedulingConfig.OnRefresh.RehydrationTimeEstimate != "" {
scheduleStatement += fmt.Sprintf(" (REHYDRATION TIME ESTIMATE = %s)", QuoteString(b.schedulingConfig.OnRefresh.RehydrationTimeEstimate))
}
p = append(p, scheduleStatement)
}
if len(p) > 0 {
p := strings.Join(p[:], ",")
q.WriteString(fmt.Sprintf(`,%s`, p))
}
} else {
q.WriteString(` REPLICAS ()`)
}
q.WriteString(`;`)
return b.ddl.exec(q.String())
}
func (b *ClusterBuilder) Drop() error {
qn := b.QualifiedName()
return b.ddl.drop(qn)
}
func (b *ClusterBuilder) Resize(newSize string) error {
q := fmt.Sprintf(`ALTER CLUSTER %s SET (SIZE '%s');`, b.QualifiedName(), newSize)
return b.ddl.exec(q)
}
func (b *ClusterBuilder) SetDisk(disk bool) error {
q := fmt.Sprintf(`ALTER CLUSTER %s SET (DISK %t);`, b.QualifiedName(), disk)
return b.ddl.exec(q)
}
func (b *ClusterBuilder) SetReplicationFactor(newReplicationFactor int) error {
q := fmt.Sprintf(`ALTER CLUSTER %s SET (REPLICATION FACTOR %d);`, b.QualifiedName(), newReplicationFactor)
return b.ddl.exec(q)
}
func (b *ClusterBuilder) SetAvailabilityZones(availabilityZones []string) error {
az := strings.Join(availabilityZones[:], ",")
q := fmt.Sprintf(`ALTER CLUSTER %s SET (AVAILABILITY ZONES = [%s]);`, b.QualifiedName(), az)
return b.ddl.exec(q)
}
func (b *ClusterBuilder) SetIntrospectionInterval(introspectionInterval string) error {
q := fmt.Sprintf(`ALTER CLUSTER %s SET (INTROSPECTION INTERVAL %s);`, b.QualifiedName(), QuoteString(introspectionInterval))
return b.ddl.exec(q)
}
func (b *ClusterBuilder) SetIntrospectionDebugging(introspectionDebugging bool) error {
q := fmt.Sprintf(`ALTER CLUSTER %s SET (INTROSPECTION DEBUGGING %t);`, b.QualifiedName(), introspectionDebugging)
return b.ddl.exec(q)
}
func (b *ClusterBuilder) SetSchedulingConfig(s interface{}) error {
schedulingConfig := GetSchedulingConfig(s)
var scheduleStatement string
var q string
// Check if the scheduling is enabled and set the appropriate SQL command.
if schedulingConfig.OnRefresh.Enabled {
scheduleStatement = "SCHEDULE = ON REFRESH"
if schedulingConfig.OnRefresh.RehydrationTimeEstimate != "" {
scheduleStatement += fmt.Sprintf(" (REHYDRATION TIME ESTIMATE = %s)", QuoteString(schedulingConfig.OnRefresh.RehydrationTimeEstimate))
}
q = fmt.Sprintf("ALTER CLUSTER %s SET (%s);", b.QualifiedName(), scheduleStatement)
} else {
// Reset the schedule settings if not enabled.
q = fmt.Sprintf("ALTER CLUSTER %s RESET (SCHEDULE);", b.QualifiedName())
}
return b.ddl.exec(q)
}
// DML
type ClusterParams struct {
ClusterId sql.NullString `db:"id"`
ClusterName sql.NullString `db:"name"`
Managed sql.NullBool `db:"managed"`
Size sql.NullString `db:"size"`
ReplicationFactor sql.NullInt64 `db:"replication_factor"`
Disk sql.NullBool `db:"disk"`
AvailabilityZones pq.StringArray `db:"availability_zones"`
Comment sql.NullString `db:"comment"`
OwnerName sql.NullString `db:"owner_name"`
Privileges pq.StringArray `db:"privileges"`
}
var clusterQuery = NewBaseQuery(`
SELECT
mz_clusters.id,
mz_clusters.name,
mz_clusters.managed,
mz_clusters.size,
mz_clusters.replication_factor,
mz_clusters.disk,
mz_clusters.availability_zones,
comments.comment AS comment,
mz_roles.name AS owner_name,
mz_clusters.privileges
FROM mz_clusters
JOIN mz_roles
ON mz_clusters.owner_id = mz_roles.id
LEFT JOIN (
SELECT id, comment
FROM mz_internal.mz_comments
WHERE object_type = 'cluster'
) comments
ON mz_clusters.id = comments.id`)
func ClusterId(conn *sqlx.DB, obj MaterializeObject) (string, error) {
q := clusterQuery.QueryPredicate(map[string]string{"mz_clusters.name": obj.Name})
var c ClusterParams
if err := conn.Get(&c, q); err != nil {
return "", err
}
return c.ClusterId.String, nil
}
func ScanCluster(conn *sqlx.DB, id string) (ClusterParams, error) {
q := clusterQuery.QueryPredicate(map[string]string{"mz_clusters.id": id})
var c ClusterParams
if err := conn.Get(&c, q); err != nil {
return c, err
}
return c, nil
}
func ListClusters(conn *sqlx.DB) ([]ClusterParams, error) {
q := clusterQuery.QueryPredicate(map[string]string{})
var c []ClusterParams
if err := conn.Select(&c, q); err != nil {
return c, err
}
return c, nil
}