-
Notifications
You must be signed in to change notification settings - Fork 306
/
topology_dm.go
839 lines (750 loc) · 24 KB
/
topology_dm.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package spec
import (
"crypto/tls"
"fmt"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/creasty/defaults"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/api"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/set"
)
const (
statusQueryTimeout = 2 * time.Second
)
var (
globalOptionTypeName = reflect.TypeOf(GlobalOptions{}).Name()
monitorOptionTypeName = reflect.TypeOf(MonitoredOptions{}).Name()
serverConfigsTypeName = reflect.TypeOf(DMServerConfigs{}).Name()
)
func setDefaultDir(parent, role, port string, field reflect.Value) {
if field.String() != "" {
return
}
if defaults.CanUpdate(field.Interface()) {
dir := fmt.Sprintf("%s-%s", role, port)
field.Set(reflect.ValueOf(filepath.Join(parent, dir)))
}
}
func findField(v reflect.Value, fieldName string) (int, bool) {
for i := 0; i < v.NumField(); i++ {
if v.Type().Field(i).Name == fieldName {
return i, true
}
}
return -1, false
}
// Skip global/monitored/job options
func isSkipField(field reflect.Value) bool {
if field.Kind() == reflect.Ptr {
if field.IsZero() {
return true
}
field = field.Elem()
}
tp := field.Type().Name()
return tp == globalOptionTypeName || tp == monitorOptionTypeName || tp == serverConfigsTypeName
}
type (
// GlobalOptions of spec.
GlobalOptions = spec.GlobalOptions
// MonitoredOptions is the spec of Monitored
MonitoredOptions = spec.MonitoredOptions
// PrometheusSpec is the spec of Prometheus
PrometheusSpec = spec.PrometheusSpec
// GrafanaSpec is the spec of Grafana
GrafanaSpec = spec.GrafanaSpec
// AlertmanagerSpec is the spec of Alertmanager
AlertmanagerSpec = spec.AlertmanagerSpec
// ResourceControl is the spec of ResourceControl
ResourceControl = meta.ResourceControl
)
type (
// DMServerConfigs represents the server runtime configuration
DMServerConfigs struct {
Master map[string]interface{} `yaml:"master"`
Worker map[string]interface{} `yaml:"worker"`
}
// Specification represents the specification of topology.yaml
Specification struct {
GlobalOptions GlobalOptions `yaml:"global,omitempty" validate:"global:editable"`
MonitoredOptions *MonitoredOptions `yaml:"monitored,omitempty" validate:"monitored:editable"`
ServerConfigs DMServerConfigs `yaml:"server_configs,omitempty" validate:"server_configs:ignore"`
Masters []*MasterSpec `yaml:"master_servers"`
Workers []*WorkerSpec `yaml:"worker_servers"`
Monitors []*spec.PrometheusSpec `yaml:"monitoring_servers"`
Grafanas []*spec.GrafanaSpec `yaml:"grafana_servers,omitempty"`
Alertmanagers []*spec.AlertmanagerSpec `yaml:"alertmanager_servers,omitempty"`
}
)
// AllDMComponentNames contains the names of all dm components.
// should include all components in ComponentsByStartOrder
func AllDMComponentNames() (roles []string) {
tp := &Specification{}
tp.IterComponent(func(c Component) {
roles = append(roles, c.Name())
})
return
}
// MasterSpec represents the Master topology specification in topology.yaml
type MasterSpec struct {
Host string `yaml:"host"`
SSHPort int `yaml:"ssh_port,omitempty" validate:"ssh_port:editable"`
Imported bool `yaml:"imported,omitempty"`
Patched bool `yaml:"patched,omitempty"`
IgnoreExporter bool `yaml:"ignore_exporter,omitempty"`
// Use Name to get the name with a default value if it's empty.
Name string `yaml:"name,omitempty"`
Port int `yaml:"port,omitempty" default:"8261"`
PeerPort int `yaml:"peer_port,omitempty" default:"8291"`
DeployDir string `yaml:"deploy_dir,omitempty"`
DataDir string `yaml:"data_dir,omitempty"`
LogDir string `yaml:"log_dir,omitempty"`
NumaNode string `yaml:"numa_node,omitempty" validate:"numa_node:editable"`
Config map[string]interface{} `yaml:"config,omitempty" validate:"config:ignore"`
ResourceControl ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"`
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
V1SourcePath string `yaml:"v1_source_path,omitempty"`
}
// Status queries current status of the instance
func (s *MasterSpec) Status(tlsCfg *tls.Config, _ ...string) string {
addr := fmt.Sprintf("%s:%d", s.Host, s.Port)
dc := api.NewDMMasterClient([]string{addr}, statusQueryTimeout, tlsCfg)
isFound, isActive, isLeader, err := dc.GetMaster(s.Name)
if err != nil {
return "Down"
}
if !isFound {
return "N/A"
}
if !isActive {
return "Unhealthy"
}
res := "Healthy"
if isLeader {
res += "|L"
}
return res
}
// Role returns the component role of the instance
func (s *MasterSpec) Role() string {
return ComponentDMMaster
}
// SSH returns the host and SSH port of the instance
func (s *MasterSpec) SSH() (string, int) {
return s.Host, s.SSHPort
}
// GetMainPort returns the main port of the instance
func (s *MasterSpec) GetMainPort() int {
return s.Port
}
// IsImported returns if the node is imported from TiDB-Ansible
func (s *MasterSpec) IsImported() bool {
return s.Imported
}
// IgnoreMonitorAgent returns if the node does not have monitor agents available
func (s *MasterSpec) IgnoreMonitorAgent() bool {
return s.IgnoreExporter
}
// WorkerSpec represents the Master topology specification in topology.yaml
type WorkerSpec struct {
Host string `yaml:"host"`
SSHPort int `yaml:"ssh_port,omitempty" validate:"ssh_port:editable"`
Imported bool `yaml:"imported,omitempty"`
Patched bool `yaml:"patched,omitempty"`
IgnoreExporter bool `yaml:"ignore_exporter,omitempty"`
// Use Name to get the name with a default value if it's empty.
Name string `yaml:"name,omitempty"`
Port int `yaml:"port,omitempty" default:"8262"`
DeployDir string `yaml:"deploy_dir,omitempty"`
DataDir string `yaml:"data_dir,omitempty"`
LogDir string `yaml:"log_dir,omitempty"`
NumaNode string `yaml:"numa_node,omitempty" validate:"numa_node:editable"`
Config map[string]interface{} `yaml:"config,omitempty" validate:"config:ignore"`
ResourceControl ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"`
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
}
// Status queries current status of the instance
func (s *WorkerSpec) Status(tlsCfg *tls.Config, masterList ...string) string {
if len(masterList) < 1 {
return "N/A"
}
dc := api.NewDMMasterClient(masterList, statusQueryTimeout, tlsCfg)
stage, err := dc.GetWorker(s.Name)
if err != nil {
return "Down"
}
if stage == "" {
return "N/A"
}
return stage
}
// Role returns the component role of the instance
func (s *WorkerSpec) Role() string {
return ComponentDMWorker
}
// SSH returns the host and SSH port of the instance
func (s *WorkerSpec) SSH() (string, int) {
return s.Host, s.SSHPort
}
// GetMainPort returns the main port of the instance
func (s *WorkerSpec) GetMainPort() int {
return s.Port
}
// IsImported returns if the node is imported from TiDB-Ansible
func (s *WorkerSpec) IsImported() bool {
return s.Imported
}
// IgnoreMonitorAgent returns if the node does not have monitor agents available
func (s *WorkerSpec) IgnoreMonitorAgent() bool {
return s.IgnoreExporter
}
// UnmarshalYAML sets default values when unmarshaling the topology file
func (s *Specification) UnmarshalYAML(unmarshal func(interface{}) error) error {
type topology Specification
if err := unmarshal((*topology)(s)); err != nil {
return err
}
if err := defaults.Set(s); err != nil {
return errors.Trace(err)
}
if s.MonitoredOptions != nil {
// Set monitored options
if s.MonitoredOptions.DeployDir == "" {
s.MonitoredOptions.DeployDir = filepath.Join(s.GlobalOptions.DeployDir,
fmt.Sprintf("%s-%d", spec.RoleMonitor, s.MonitoredOptions.NodeExporterPort))
}
if s.MonitoredOptions.DataDir == "" {
s.MonitoredOptions.DataDir = filepath.Join(s.GlobalOptions.DataDir,
fmt.Sprintf("%s-%d", spec.RoleMonitor, s.MonitoredOptions.NodeExporterPort))
}
if s.MonitoredOptions.LogDir == "" {
s.MonitoredOptions.LogDir = "log"
}
if !strings.HasPrefix(s.MonitoredOptions.LogDir, "/") &&
!strings.HasPrefix(s.MonitoredOptions.LogDir, s.MonitoredOptions.DeployDir) {
s.MonitoredOptions.LogDir = filepath.Join(s.MonitoredOptions.DeployDir, s.MonitoredOptions.LogDir)
}
}
if err := fillDMCustomDefaults(&s.GlobalOptions, s); err != nil {
return err
}
return s.Validate()
}
// platformConflictsDetect checks for conflicts in topology for different OS / Arch
// for set to the same host / IP
func (s *Specification) platformConflictsDetect() error {
type (
conflict struct {
os string
arch string
cfg string
}
)
platformStats := map[string]conflict{}
topoSpec := reflect.ValueOf(s).Elem()
topoType := reflect.TypeOf(s).Elem()
for i := 0; i < topoSpec.NumField(); i++ {
if isSkipField(topoSpec.Field(i)) {
continue
}
compSpecs := topoSpec.Field(i)
for index := 0; index < compSpecs.Len(); index++ {
compSpec := reflect.Indirect(compSpecs.Index(index))
// skip nodes imported from TiDB-Ansible
if compSpec.Addr().Interface().(InstanceSpec).IsImported() {
continue
}
// check hostname
host := compSpec.FieldByName("Host").String()
cfg := topoType.Field(i).Tag.Get("yaml")
if host == "" {
return errors.Errorf("`%s` contains empty host field", cfg)
}
// platform conflicts
stat := conflict{
cfg: cfg,
}
if j, found := findField(compSpec, "OS"); found {
stat.os = compSpec.Field(j).String()
}
if j, found := findField(compSpec, "Arch"); found {
stat.arch = compSpec.Field(j).String()
}
prev, exist := platformStats[host]
if exist {
if prev.os != stat.os || prev.arch != stat.arch {
return &meta.ValidateErr{
Type: meta.TypeMismatch,
Target: "platform",
LHS: fmt.Sprintf("%s:%s/%s", prev.cfg, prev.os, prev.arch),
RHS: fmt.Sprintf("%s:%s/%s", stat.cfg, stat.os, stat.arch),
Value: host,
}
}
}
platformStats[host] = stat
}
}
return nil
}
func (s *Specification) portConflictsDetect() error {
type (
usedPort struct {
host string
port int
}
conflict struct {
tp string
cfg string
}
)
portTypes := []string{
"Port",
"StatusPort",
"PeerPort",
"ClientPort",
"WebPort",
"TCPPort",
"HTTPPort",
"ClusterPort",
}
portStats := map[usedPort]conflict{}
uniqueHosts := set.NewStringSet()
topoSpec := reflect.ValueOf(s).Elem()
topoType := reflect.TypeOf(s).Elem()
for i := 0; i < topoSpec.NumField(); i++ {
if isSkipField(topoSpec.Field(i)) {
continue
}
compSpecs := topoSpec.Field(i)
for index := 0; index < compSpecs.Len(); index++ {
compSpec := reflect.Indirect(compSpecs.Index(index))
// skip nodes imported from TiDB-Ansible
if compSpec.Addr().Interface().(InstanceSpec).IsImported() {
continue
}
// check hostname
host := compSpec.FieldByName("Host").String()
cfg := topoType.Field(i).Tag.Get("yaml")
if host == "" {
return errors.Errorf("`%s` contains empty host field", cfg)
}
uniqueHosts.Insert(host)
// Ports conflicts
for _, portType := range portTypes {
if j, found := findField(compSpec, portType); found {
item := usedPort{
host: host,
port: int(compSpec.Field(j).Int()),
}
tp := compSpec.Type().Field(j).Tag.Get("yaml")
prev, exist := portStats[item]
if exist {
return &meta.ValidateErr{
Type: meta.TypeConflict,
Target: "port",
LHS: fmt.Sprintf("%s:%s.%s", prev.cfg, item.host, prev.tp),
RHS: fmt.Sprintf("%s:%s.%s", cfg, item.host, tp),
Value: item.port,
}
}
portStats[item] = conflict{
tp: tp,
cfg: cfg,
}
}
}
}
}
// Port conflicts in monitored components
monitoredPortTypes := []string{
"NodeExporterPort",
"BlackboxExporterPort",
}
monitoredOpt := topoSpec.FieldByName(monitorOptionTypeName)
if monitoredOpt.IsZero() {
return nil
}
monitoredOpt = monitoredOpt.Elem()
for host := range uniqueHosts {
cfg := "monitored"
for _, portType := range monitoredPortTypes {
f := monitoredOpt.FieldByName(portType)
item := usedPort{
host: host,
port: int(f.Int()),
}
ft, found := monitoredOpt.Type().FieldByName(portType)
if !found {
return errors.Errorf("incompatible change `%s.%s`", monitorOptionTypeName, portType)
}
// `yaml:"node_exporter_port,omitempty"`
tp := strings.Split(ft.Tag.Get("yaml"), ",")[0]
prev, exist := portStats[item]
if exist {
return &meta.ValidateErr{
Type: meta.TypeConflict,
Target: "port",
LHS: fmt.Sprintf("%s:%s.%s", prev.cfg, item.host, prev.tp),
RHS: fmt.Sprintf("%s:%s.%s", cfg, item.host, tp),
Value: item.port,
}
}
portStats[item] = conflict{
tp: tp,
cfg: cfg,
}
}
}
return nil
}
func (s *Specification) dirConflictsDetect() error {
type (
usedDir struct {
host string
dir string
}
conflict struct {
tp string
cfg string
}
)
dirTypes := []string{
"DataDir",
"DeployDir",
}
// usedInfo => type
var (
dirStats = map[usedDir]conflict{}
uniqueHosts = set.NewStringSet()
)
topoSpec := reflect.ValueOf(s).Elem()
topoType := reflect.TypeOf(s).Elem()
for i := 0; i < topoSpec.NumField(); i++ {
if isSkipField(topoSpec.Field(i)) {
continue
}
compSpecs := topoSpec.Field(i)
for index := 0; index < compSpecs.Len(); index++ {
compSpec := reflect.Indirect(compSpecs.Index(index))
// skip nodes imported from TiDB-Ansible
if compSpec.Addr().Interface().(InstanceSpec).IsImported() {
continue
}
// check hostname
host := compSpec.FieldByName("Host").String()
cfg := topoType.Field(i).Tag.Get("yaml")
if host == "" {
return errors.Errorf("`%s` contains empty host field", cfg)
}
uniqueHosts.Insert(host)
// Directory conflicts
for _, dirType := range dirTypes {
if j, found := findField(compSpec, dirType); found {
item := usedDir{
host: host,
dir: compSpec.Field(j).String(),
}
// data_dir is relative to deploy_dir by default, so they can be with
// same (sub) paths as long as the deploy_dirs are different
if item.dir != "" && !strings.HasPrefix(item.dir, "/") {
continue
}
// `yaml:"data_dir,omitempty"`
tp := strings.Split(compSpec.Type().Field(j).Tag.Get("yaml"), ",")[0]
prev, exist := dirStats[item]
if exist {
return &meta.ValidateErr{
Type: meta.TypeConflict,
Target: "directory",
LHS: fmt.Sprintf("%s:%s.%s", prev.cfg, item.host, prev.tp),
RHS: fmt.Sprintf("%s:%s.%s", cfg, item.host, tp),
Value: item.dir,
}
}
dirStats[item] = conflict{
tp: tp,
cfg: cfg,
}
}
}
}
}
return nil
}
// CountDir counts for dir paths used by any instance in the cluster with the same
// prefix, useful to find potential path conflicts
func (s *Specification) CountDir(targetHost, dirPrefix string) int {
dirTypes := []string{
"DataDir",
"DeployDir",
"LogDir",
}
// host-path -> count
dirStats := make(map[string]int)
count := 0
topoSpec := reflect.ValueOf(s).Elem()
dirPrefix = spec.Abs(s.GlobalOptions.User, dirPrefix)
for i := 0; i < topoSpec.NumField(); i++ {
if isSkipField(topoSpec.Field(i)) {
continue
}
compSpecs := topoSpec.Field(i)
for index := 0; index < compSpecs.Len(); index++ {
compSpec := reflect.Indirect(compSpecs.Index(index))
// Directory conflicts
for _, dirType := range dirTypes {
if j, found := findField(compSpec, dirType); found {
dir := compSpec.Field(j).String()
host := compSpec.FieldByName("Host").String()
switch dirType { // the same as in logic.go for (*instance)
case "DataDir":
deployDir := compSpec.FieldByName("DeployDir").String()
// the default data_dir is relative to deploy_dir
if dir != "" && !strings.HasPrefix(dir, "/") {
dir = filepath.Join(deployDir, dir)
}
case "LogDir":
deployDir := compSpec.FieldByName("DeployDir").String()
field := compSpec.FieldByName("LogDir")
if field.IsValid() {
dir = field.Interface().(string)
}
if dir == "" {
dir = "log"
}
if !strings.HasPrefix(dir, "/") {
dir = filepath.Join(deployDir, dir)
}
}
dir = spec.Abs(s.GlobalOptions.User, dir)
dirStats[host+dir]++
}
}
}
}
for k, v := range dirStats {
if k == targetHost+dirPrefix || strings.HasPrefix(k, targetHost+dirPrefix+"/") {
count += v
}
}
return count
}
// TLSConfig generates a tls.Config for the specification as needed
func (s *Specification) TLSConfig(dir string) (*tls.Config, error) {
if !s.GlobalOptions.TLSEnabled {
return nil, nil
}
return spec.LoadClientCert(dir)
}
// Validate validates the topology specification and produce error if the
// specification invalid (e.g: port conflicts or directory conflicts)
func (s *Specification) Validate() error {
if err := s.platformConflictsDetect(); err != nil {
return err
}
if err := s.portConflictsDetect(); err != nil {
return err
}
if err := s.dirConflictsDetect(); err != nil {
return err
}
return spec.RelativePathDetect(s, isSkipField)
}
// Type implements Topology interface.
func (s *Specification) Type() string {
return spec.TopoTypeDM
}
// BaseTopo implements Topology interface.
func (s *Specification) BaseTopo() *spec.BaseTopo {
return &spec.BaseTopo{
GlobalOptions: &s.GlobalOptions,
MonitoredOptions: s.GetMonitoredOptions(),
MasterList: s.GetMasterList(),
Monitors: s.Monitors,
Grafanas: s.Grafanas,
Alertmanagers: s.Alertmanagers,
}
}
// NewPart implements ScaleOutTopology interface.
func (s *Specification) NewPart() spec.Topology {
return &Specification{
GlobalOptions: s.GlobalOptions,
MonitoredOptions: s.MonitoredOptions,
ServerConfigs: s.ServerConfigs,
}
}
// MergeTopo implements ScaleOutTopology interface.
func (s *Specification) MergeTopo(rhs spec.Topology) spec.Topology {
other, ok := rhs.(*Specification)
if !ok {
panic("topo should be DM Topology")
}
return s.Merge(other)
}
// GetMasterList returns a list of Master API hosts of the current cluster
func (s *Specification) GetMasterList() []string {
var masterList []string
for _, master := range s.Masters {
masterList = append(masterList, fmt.Sprintf("%s:%d", master.Host, master.Port))
}
return masterList
}
// FillHostArch fills the topology with the given host->arch
func (s *Specification) FillHostArch(hostArch map[string]string) error {
return spec.FillHostArch(s, hostArch)
}
// Merge returns a new Topology which sum old ones
func (s *Specification) Merge(that spec.Topology) spec.Topology {
spec := that.(*Specification)
return &Specification{
GlobalOptions: s.GlobalOptions,
MonitoredOptions: s.MonitoredOptions,
ServerConfigs: s.ServerConfigs,
Masters: append(s.Masters, spec.Masters...),
Workers: append(s.Workers, spec.Workers...),
Monitors: append(s.Monitors, spec.Monitors...),
Grafanas: append(s.Grafanas, spec.Grafanas...),
Alertmanagers: append(s.Alertmanagers, spec.Alertmanagers...),
}
}
// fillDefaults tries to fill custom fields to their default values
func fillDMCustomDefaults(globalOptions *GlobalOptions, data interface{}) error {
v := reflect.ValueOf(data).Elem()
t := v.Type()
var err error
for i := 0; i < t.NumField(); i++ {
if err = setDMCustomDefaults(globalOptions, v.Field(i)); err != nil {
return err
}
}
return nil
}
func setDMCustomDefaults(globalOptions *GlobalOptions, field reflect.Value) error {
if !field.CanSet() || isSkipField(field) {
return nil
}
switch field.Kind() {
case reflect.Slice:
for i := 0; i < field.Len(); i++ {
if err := setDMCustomDefaults(globalOptions, field.Index(i)); err != nil {
return err
}
}
case reflect.Struct:
ref := reflect.New(field.Type())
ref.Elem().Set(field)
if err := fillDMCustomDefaults(globalOptions, ref.Interface()); err != nil {
return err
}
field.Set(ref.Elem())
case reflect.Ptr:
if err := setDMCustomDefaults(globalOptions, field.Elem()); err != nil {
return err
}
}
if field.Kind() != reflect.Struct {
return nil
}
for j := 0; j < field.NumField(); j++ {
switch field.Type().Field(j).Name {
case "SSHPort":
if field.Field(j).Int() != 0 {
continue
}
field.Field(j).Set(reflect.ValueOf(globalOptions.SSHPort))
case "Name":
if field.Field(j).String() != "" {
continue
}
host := field.FieldByName("Host").String()
port := field.FieldByName("Port").Int()
field.Field(j).Set(reflect.ValueOf(fmt.Sprintf("dm-%s-%d", host, port)))
case "DataDir":
dataDir := field.Field(j).String()
if dataDir != "" { // already have a value, skip filling default values
continue
}
// If the data dir in global options is an obsolute path, it appends to
// the global and has a comp-port sub directory
if strings.HasPrefix(globalOptions.DataDir, "/") {
field.Field(j).Set(reflect.ValueOf(filepath.Join(
globalOptions.DataDir,
fmt.Sprintf("%s-%s", field.Addr().Interface().(InstanceSpec).Role(), getPort(field)),
)))
continue
}
// If the data dir in global options is empty or a relative path, keep it be relative
// Our run_*.sh start scripts are run inside deploy_path, so the final location
// will be deploy_path/global.data_dir
// (the default value of global.data_dir is "data")
if globalOptions.DataDir == "" {
field.Field(j).Set(reflect.ValueOf("data"))
} else {
field.Field(j).Set(reflect.ValueOf(globalOptions.DataDir))
}
case "DeployDir":
setDefaultDir(globalOptions.DeployDir, field.Addr().Interface().(InstanceSpec).Role(), getPort(field), field.Field(j))
case "LogDir":
if field.Field(j).String() == "" && defaults.CanUpdate(field.Field(j).Interface()) {
field.Field(j).Set(reflect.ValueOf(globalOptions.LogDir))
}
case "Arch":
// default values of globalOptions are set before fillCustomDefaults in Unmarshal
// so the globalOptions.Arch already has its default value set, no need to check again
if field.Field(j).String() == "" {
field.Field(j).Set(reflect.ValueOf(globalOptions.Arch))
}
switch strings.ToLower(field.Field(j).String()) {
// replace "x86_64" with amd64, they are the same in our repo
case "x86_64":
field.Field(j).Set(reflect.ValueOf("amd64"))
// replace "aarch64" with arm64
case "aarch64":
field.Field(j).Set(reflect.ValueOf("arm64"))
}
// convert to lower case
if field.Field(j).String() != "" {
field.Field(j).Set(reflect.ValueOf(strings.ToLower(field.Field(j).String())))
}
case "OS":
// default value of globalOptions.OS is already set, same as "Arch"
if field.Field(j).String() == "" {
field.Field(j).Set(reflect.ValueOf(globalOptions.OS))
}
// convert to lower case
if field.Field(j).String() != "" {
field.Field(j).Set(reflect.ValueOf(strings.ToLower(field.Field(j).String())))
}
}
}
return nil
}
func getPort(v reflect.Value) string {
for i := 0; i < v.NumField(); i++ {
switch v.Type().Field(i).Name {
case "Port", "ClientPort", "WebPort", "TCPPort", "NodeExporterPort":
return fmt.Sprintf("%d", v.Field(i).Int())
}
}
return ""
}