-
Notifications
You must be signed in to change notification settings - Fork 312
/
spec.go
837 lines (753 loc) · 24.6 KB
/
spec.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
// 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 (
"fmt"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/creasty/defaults"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/template/scripts"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/set"
"go.etcd.io/etcd/clientv3"
)
const (
// Timeout in second when quering node status
statusQueryTimeout = 10 * time.Second
)
// general role names
var (
RoleMonitor = "monitor"
)
type (
// InstanceSpec represent a instance specification
InstanceSpec interface {
Role() string
SSH() (string, int)
GetMainPort() int
IsImported() bool
}
// GlobalOptions represents the global options for all groups in topology
// specification in topology.yaml
GlobalOptions struct {
User string `yaml:"user,omitempty" default:"tidb"`
SSHPort int `yaml:"ssh_port,omitempty" default:"22"`
DeployDir string `yaml:"deploy_dir,omitempty" default:"deploy"`
DataDir string `yaml:"data_dir,omitempty" default:"data"`
LogDir string `yaml:"log_dir,omitempty"`
ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty"`
OS string `yaml:"os,omitempty" default:"linux"`
Arch string `yaml:"arch,omitempty" default:"amd64"`
}
// MonitoredOptions represents the monitored node configuration
MonitoredOptions struct {
NodeExporterPort int `yaml:"node_exporter_port,omitempty" default:"9100"`
BlackboxExporterPort int `yaml:"blackbox_exporter_port,omitempty" default:"9115"`
DeployDir string `yaml:"deploy_dir,omitempty"`
DataDir string `yaml:"data_dir,omitempty"`
LogDir string `yaml:"log_dir,omitempty"`
NumaNode string `yaml:"numa_node,omitempty"`
ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty"`
}
// ServerConfigs represents the server runtime configuration
ServerConfigs struct {
TiDB map[string]interface{} `yaml:"tidb"`
TiKV map[string]interface{} `yaml:"tikv"`
PD map[string]interface{} `yaml:"pd"`
TiFlash map[string]interface{} `yaml:"tiflash"`
TiFlashLearner map[string]interface{} `yaml:"tiflash-learner"`
Pump map[string]interface{} `yaml:"pump"`
Drainer map[string]interface{} `yaml:"drainer"`
CDC map[string]interface{} `yaml:"cdc"`
}
// Specification represents the specification of topology.yaml
Specification struct {
GlobalOptions GlobalOptions `yaml:"global,omitempty"`
MonitoredOptions MonitoredOptions `yaml:"monitored,omitempty"`
ServerConfigs ServerConfigs `yaml:"server_configs,omitempty"`
TiDBServers []TiDBSpec `yaml:"tidb_servers"`
TiKVServers []TiKVSpec `yaml:"tikv_servers"`
TiFlashServers []TiFlashSpec `yaml:"tiflash_servers"`
PDServers []PDSpec `yaml:"pd_servers"`
PumpServers []PumpSpec `yaml:"pump_servers,omitempty"`
Drainers []DrainerSpec `yaml:"drainer_servers,omitempty"`
CDCServers []CDCSpec `yaml:"cdc_servers,omitempty"`
Monitors []PrometheusSpec `yaml:"monitoring_servers"`
Grafana []GrafanaSpec `yaml:"grafana_servers,omitempty"`
Alertmanager []AlertManagerSpec `yaml:"alertmanager_servers,omitempty"`
}
)
// Spec represents specification of the cluster.
type Spec interface {
// Instances() []Instance
ComponentsByStartOrder() []Component
GetMonitoredOptions() *MonitoredOptions
}
// GetMonitoredOptions implements Spec interface.
func (s *Specification) GetMonitoredOptions() *MonitoredOptions {
return &s.MonitoredOptions
}
// AllComponentNames contains the names of all components.
// should include all components in ComponentsByStartOrder
func AllComponentNames() (roles []string) {
tp := &Specification{}
tp.IterComponent(func(c Component) {
roles = append(roles, c.Name())
})
return
}
// 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
}
// set default values from tag
if err := defaults.Set(s); err != nil {
return errors.Trace(err)
}
// Set monitored options
if s.MonitoredOptions.DeployDir == "" {
s.MonitoredOptions.DeployDir = filepath.Join(s.GlobalOptions.DeployDir,
fmt.Sprintf("%s-%d", RoleMonitor, s.MonitoredOptions.NodeExporterPort))
}
if s.MonitoredOptions.DataDir == "" {
s.MonitoredOptions.DataDir = filepath.Join(s.GlobalOptions.DataDir,
fmt.Sprintf("%s-%d", 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)
}
// populate custom default values as needed
if err := fillCustomDefaults(&s.GlobalOptions, s); err != nil {
return err
}
return s.Validate()
}
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
}
// platformConflictsDetect checks for conflicts in topology for different OS / Arch
// 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 := compSpecs.Index(index)
// skip nodes imported from TiDB-Ansible
if compSpec.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 := compSpecs.Index(index)
// skip nodes imported from TiDB-Ansible
if compSpec.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)
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
imported bool
}
)
dirTypes := []string{
"DataDir",
"DeployDir",
}
// usedInfo => type
var dirStats = map[usedDir]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 := compSpecs.Index(index)
// 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)
}
// 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]
// not checking between imported nodes
if exist &&
!(compSpec.Interface().(InstanceSpec).IsImported() && prev.imported) {
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,
}
}
// not reporting error for nodes imported from TiDB-Ansible, but keep
// their dirs in the map to check if other nodes are using them
dirStats[item] = conflict{
tp: tp,
cfg: cfg,
imported: compSpec.Interface().(InstanceSpec).IsImported(),
}
}
}
}
}
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",
}
// path -> count
dirStats := make(map[string]int)
count := 0
topoSpec := reflect.ValueOf(s).Elem()
dirPrefix = clusterutil.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 := 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 instance.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 = clusterutil.Abs(s.GlobalOptions.User, dir)
dirStats[host+dir] += 1
}
}
}
}
for k, v := range dirStats {
if k == targetHost+dirPrefix || strings.HasPrefix(k, targetHost+dirPrefix+"/") {
count += v
}
}
return count
}
// 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
}
return s.dirConflictsDetect()
}
// GetPDList returns a list of PD API hosts of the current cluster
func (s *Specification) GetPDList() []string {
var pdList []string
for _, pd := range s.PDServers {
pdList = append(pdList, fmt.Sprintf("%s:%d", pd.Host, pd.ClientPort))
}
return pdList
}
// GetEtcdClient load EtcdClient of current cluster
func (s *Specification) GetEtcdClient() (*clientv3.Client, error) {
return clientv3.New(clientv3.Config{
Endpoints: s.GetPDList(),
})
}
// Merge returns a new Specification which sum old ones
func (s *Specification) Merge(that *Specification) *Specification {
return &Specification{
GlobalOptions: s.GlobalOptions,
MonitoredOptions: s.MonitoredOptions,
ServerConfigs: s.ServerConfigs,
TiDBServers: append(s.TiDBServers, that.TiDBServers...),
TiKVServers: append(s.TiKVServers, that.TiKVServers...),
PDServers: append(s.PDServers, that.PDServers...),
TiFlashServers: append(s.TiFlashServers, that.TiFlashServers...),
PumpServers: append(s.PumpServers, that.PumpServers...),
Drainers: append(s.Drainers, that.Drainers...),
CDCServers: append(s.CDCServers, that.CDCServers...),
Monitors: append(s.Monitors, that.Monitors...),
Grafana: append(s.Grafana, that.Grafana...),
Alertmanager: append(s.Alertmanager, that.Alertmanager...),
}
}
// fillDefaults tries to fill custom fields to their default values
func fillCustomDefaults(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 = setCustomDefaults(globalOptions, v.Field(i)); err != nil {
return err
}
}
return nil
}
var (
globalOptionTypeName = reflect.TypeOf(GlobalOptions{}).Name()
monitorOptionTypeName = reflect.TypeOf(MonitoredOptions{}).Name()
serverConfigsTypeName = reflect.TypeOf(ServerConfigs{}).Name()
)
// Skip global/monitored options
func isSkipField(field reflect.Value) bool {
tp := field.Type().Name()
return tp == globalOptionTypeName || tp == monitorOptionTypeName || tp == serverConfigsTypeName
}
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 setCustomDefaults(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 := setCustomDefaults(globalOptions, field.Index(i)); err != nil {
return err
}
}
case reflect.Struct:
ref := reflect.New(field.Type())
ref.Elem().Set(field)
if err := fillCustomDefaults(globalOptions, ref.Interface()); err != nil {
return err
}
field.Set(ref.Elem())
case reflect.Ptr:
if err := setCustomDefaults(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()
clientPort := field.FieldByName("ClientPort").Int()
field.Field(j).Set(reflect.ValueOf(fmt.Sprintf("pd-%s-%d", host, clientPort)))
case "DataDir":
if field.FieldByName("Imported").Interface().(bool) {
setDefaultDir(globalOptions.DataDir, field.Interface().(InstanceSpec).Role(), getPort(field), field.Field(j))
}
dataDir := field.Field(j).String()
// If the per-instance data_dir already have a value, skip filling default values
// and ignore any value in global data_dir, the default values are filled only
// when the pre-instance data_dir is empty
if dataDir != "" {
continue
}
// If the data dir in global options is an absolute path, append current
// value 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.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.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 ""
}
// ComponentsByStopOrder return component in the order need to stop.
func (s *Specification) ComponentsByStopOrder() (comps []Component) {
comps = s.ComponentsByStartOrder()
// revert order
i := 0
j := len(comps) - 1
for i < j {
comps[i], comps[j] = comps[j], comps[i]
i++
j--
}
return
}
// ComponentsByStartOrder return component in the order need to start.
func (s *Specification) ComponentsByStartOrder() (comps []Component) {
// "pd", "tikv", "pump", "tidb", "tiflash", "drainer", "cdc", "prometheus", "grafana", "alertmanager"
comps = append(comps, &PDComponent{s})
comps = append(comps, &TiKVComponent{s})
comps = append(comps, &PumpComponent{s})
comps = append(comps, &TiDBComponent{s})
comps = append(comps, &TiFlashComponent{s})
comps = append(comps, &DrainerComponent{s})
comps = append(comps, &CDCComponent{s})
comps = append(comps, &MonitorComponent{s})
comps = append(comps, &GrafanaComponent{s})
comps = append(comps, &AlertManagerComponent{s})
return
}
// ComponentsByUpdateOrder return component in the order need to be updated.
func (s *Specification) ComponentsByUpdateOrder() (comps []Component) {
// "tiflash", "pd", "tikv", "pump", "tidb", "drainer", "cdc", "prometheus", "grafana", "alertmanager"
comps = s.ComponentsByStartOrder()
for i, comp := range comps {
if comp.Name() == ComponentTiFlash {
comps = append([]Component{comp}, append(comps[:i], comps[i+1:]...)...)
return
}
}
return
}
// IterComponent iterates all components in component starting order
func (s *Specification) IterComponent(fn func(comp Component)) {
for _, comp := range s.ComponentsByStartOrder() {
fn(comp)
}
}
// IterInstance iterates all instances in component starting order
func (s *Specification) IterInstance(fn func(instance Instance)) {
for _, comp := range s.ComponentsByStartOrder() {
for _, inst := range comp.Instances() {
fn(inst)
}
}
}
// IterHost iterates one instance for each host
func (s *Specification) IterHost(fn func(instance Instance)) {
hostMap := make(map[string]bool)
for _, comp := range s.ComponentsByStartOrder() {
for _, inst := range comp.Instances() {
host := inst.GetHost()
_, ok := hostMap[host]
if !ok {
hostMap[host] = true
fn(inst)
}
}
}
}
// Endpoints returns the PD endpoints configurations
func (s *Specification) Endpoints(user string) []*scripts.PDScript {
var ends []*scripts.PDScript
for _, spec := range s.PDServers {
deployDir := clusterutil.Abs(user, spec.DeployDir)
// data dir would be empty for components which don't need it
dataDir := spec.DataDir
// the default data_dir is relative to deploy_dir
if dataDir != "" && !strings.HasPrefix(dataDir, "/") {
dataDir = filepath.Join(deployDir, dataDir)
}
// log dir will always be with values, but might not used by the component
logDir := clusterutil.Abs(user, spec.LogDir)
script := scripts.NewPDScript(
spec.Name,
spec.Host,
deployDir,
dataDir,
logDir).
WithClientPort(spec.ClientPort).
WithPeerPort(spec.PeerPort).
WithListenHost(spec.ListenHost)
ends = append(ends, script)
}
return ends
}
// AlertManagerEndpoints returns the AlertManager endpoints configurations
func (s *Specification) AlertManagerEndpoints(user string) []*scripts.AlertManagerScript {
var ends []*scripts.AlertManagerScript
for _, spec := range s.Alertmanager {
deployDir := clusterutil.Abs(user, spec.DeployDir)
// data dir would be empty for components which don't need it
dataDir := spec.DataDir
// the default data_dir is relative to deploy_dir
if dataDir != "" && !strings.HasPrefix(dataDir, "/") {
dataDir = filepath.Join(deployDir, dataDir)
}
// log dir will always be with values, but might not used by the component
logDir := clusterutil.Abs(user, spec.LogDir)
script := scripts.NewAlertManagerScript(
spec.Host,
deployDir,
dataDir,
logDir).
WithWebPort(spec.WebPort).
WithClusterPort(spec.ClusterPort)
ends = append(ends, script)
}
return ends
}