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 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
|
// 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"
"regexp"
"sort"
"strconv"
"strings"
"github.com/pingcap/tiup/pkg/cluster/api"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/set"
"github.com/pingcap/tiup/pkg/tui"
"github.com/pingcap/tiup/pkg/utils"
"go.uber.org/zap"
)
// pre defined error types
var (
errNSDeploy = errNS.NewSubNamespace("deploy")
errDeployDirConflict = errNSDeploy.NewType("dir_conflict", utils.ErrTraitPreCheck)
errDeployDirOverlap = errNSDeploy.NewType("dir_overlap", utils.ErrTraitPreCheck)
errDeployPortConflict = errNSDeploy.NewType("port_conflict", utils.ErrTraitPreCheck)
ErrNoTiSparkMaster = errors.New("there must be a Spark master node if you want to use the TiSpark component")
ErrMultipleTiSparkMaster = errors.New("a TiSpark enabled cluster with more than 1 Spark master node is not supported")
ErrMultipleTisparkWorker = errors.New("multiple TiSpark workers on the same host is not supported by Spark")
ErrUserOrGroupInvalid = errors.New(`linux username and groupname must start with a lower case letter or an underscore, ` +
`followed by lower case letters, digits, underscores, or dashes. ` +
`Usernames may only be up to 32 characters long. ` +
`Groupnames may only be up to 16 characters long.`)
)
// Linux username and groupname must start with a lower case letter or an underscore,
// followed by lower case letters, digits, underscores, or dashes.
// ref https://man7.org/linux/man-pages/man8/useradd.8.html
// ref https://man7.org/linux/man-pages/man8/groupadd.8.html
var (
reUser = regexp.MustCompile(`^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$`)
reGroup = regexp.MustCompile(`^[a-z_]([a-z0-9_-]{0,15})$`)
)
func fixDir(topo Topology) func(string) string {
return func(dir string) string {
if dir != "" {
return Abs(topo.BaseTopo().GlobalOptions.User, dir)
}
return dir
}
}
// DirAccessor stands for a directory accessor for an instance
type DirAccessor struct {
dirKind string
accessor func(Instance, Topology) string
}
func dirAccessors() ([]DirAccessor, []DirAccessor) {
instanceDirAccessor := []DirAccessor{
{dirKind: "deploy directory", accessor: func(instance Instance, topo Topology) string { return instance.DeployDir() }},
{dirKind: "data directory", accessor: func(instance Instance, topo Topology) string { return instance.DataDir() }},
{dirKind: "log directory", accessor: func(instance Instance, topo Topology) string { return instance.LogDir() }},
}
hostDirAccessor := []DirAccessor{
{dirKind: "monitor deploy directory", accessor: func(instance Instance, topo Topology) string {
m := topo.BaseTopo().MonitoredOptions
if m == nil {
return ""
}
return m.DeployDir
}},
{dirKind: "monitor data directory", accessor: func(instance Instance, topo Topology) string {
m := topo.BaseTopo().MonitoredOptions
if m == nil {
return ""
}
return m.DataDir
}},
{dirKind: "monitor log directory", accessor: func(instance Instance, topo Topology) string {
m := topo.BaseTopo().MonitoredOptions
if m == nil {
return ""
}
return m.LogDir
}},
}
return instanceDirAccessor, hostDirAccessor
}
// DirEntry stands for a directory with attributes and instance
type DirEntry struct {
clusterName string
dirKind string
dir string
instance Instance
}
func appendEntries(name string, topo Topology, inst Instance, dirAccessor DirAccessor, targets []DirEntry) []DirEntry {
for dir := range strings.SplitSeq(fixDir(topo)(dirAccessor.accessor(inst, topo)), ",") {
targets = append(targets, DirEntry{
clusterName: name,
dirKind: dirAccessor.dirKind,
dir: dir,
instance: inst,
})
}
return targets
}
// CheckClusterDirConflict checks cluster dir conflict or overlap
func CheckClusterDirConflict(clusterList map[string]Metadata, clusterName string, topo Topology) error {
instanceDirAccessor, hostDirAccessor := dirAccessors()
currentEntries := []DirEntry{}
existingEntries := []DirEntry{}
// rebuild existing disk status
for name, metadata := range clusterList {
if name == clusterName {
continue
}
topo := metadata.GetTopology()
topo.IterInstance(func(inst Instance) {
for _, dirAccessor := range instanceDirAccessor {
existingEntries = appendEntries(name, topo, inst, dirAccessor, existingEntries)
}
})
IterHost(topo, func(inst Instance) {
for _, dirAccessor := range hostDirAccessor {
existingEntries = appendEntries(name, topo, inst, dirAccessor, existingEntries)
}
})
}
topo.IterInstance(func(inst Instance) {
for _, dirAccessor := range instanceDirAccessor {
currentEntries = appendEntries(clusterName, topo, inst, dirAccessor, currentEntries)
}
})
IterHost(topo, func(inst Instance) {
for _, dirAccessor := range hostDirAccessor {
currentEntries = appendEntries(clusterName, topo, inst, dirAccessor, currentEntries)
}
})
for _, d1 := range currentEntries {
// 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 d1.dirKind == "data directory" && !strings.HasPrefix(d1.dir, "/") {
continue
}
for _, d2 := range existingEntries {
if d1.instance.GetManageHost() != d2.instance.GetManageHost() {
continue
}
// ignore conflict in the case when both sides are monitor and either one of them
// is marked as ignore exporter.
if strings.HasPrefix(d1.dirKind, "monitor") &&
strings.HasPrefix(d2.dirKind, "monitor") &&
(d1.instance.IgnoreMonitorAgent() || d2.instance.IgnoreMonitorAgent()) {
continue
}
if d1.dir == d2.dir && d1.dir != "" {
properties := map[string]string{
"ThisDirKind": d1.dirKind,
"ThisDir": d1.dir,
"ThisComponent": d1.instance.ComponentName(),
"ThisHost": d1.instance.GetManageHost(),
"ExistCluster": d2.clusterName,
"ExistDirKind": d2.dirKind,
"ExistDir": d2.dir,
"ExistComponent": d2.instance.ComponentName(),
"ExistHost": d2.instance.GetManageHost(),
}
zap.L().Info("Meet deploy directory conflict", zap.Any("info", properties))
return errDeployDirConflict.New("Deploy directory conflicts to an existing cluster").WithProperty(tui.SuggestionFromTemplate(`
The directory you specified in the topology file is:
Directory: {{ColorKeyword}}{{.ThisDirKind}} {{.ThisDir}}{{ColorReset}}
Component: {{ColorKeyword}}{{.ThisComponent}} {{.ThisHost}}{{ColorReset}}
It conflicts to a directory in the existing cluster:
Existing Cluster Name: {{ColorKeyword}}{{.ExistCluster}}{{ColorReset}}
Existing Directory: {{ColorKeyword}}{{.ExistDirKind}} {{.ExistDir}}{{ColorReset}}
Existing Component: {{ColorKeyword}}{{.ExistComponent}} {{.ExistHost}}{{ColorReset}}
Please change to use another directory or another host.
`, properties))
}
}
}
return CheckClusterDirOverlap(currentEntries)
}
// CheckClusterDirOverlap checks cluster dir overlaps with data or log.
// this should only be used across clusters.
// we don't allow to deploy log under data, and vise versa.
// ref https://github.com/pingcap/tiup/issues/1047#issuecomment-761711508
func CheckClusterDirOverlap(entries []DirEntry) error {
ignore := func(d1, d2 DirEntry) bool {
return (d1.instance.GetManageHost() != d2.instance.GetManageHost()) ||
d1.dir == "" || d2.dir == "" ||
strings.HasSuffix(d1.dirKind, "deploy directory") ||
strings.HasSuffix(d2.dirKind, "deploy directory")
}
for i := 0; i < len(entries)-1; i++ {
d1 := entries[i]
for j := i + 1; j < len(entries); j++ {
d2 := entries[j]
if ignore(d1, d2) {
continue
}
if utils.IsSubDir(d1.dir, d2.dir) || utils.IsSubDir(d2.dir, d1.dir) {
// overlap is allowed in the case both sides are imported
if d1.instance.IsImported() && d2.instance.IsImported() {
continue
}
// overlap is allowed in the case one side is imported and the other is monitor,
// we assume that the monitor is deployed with the first instance in that host,
// it implies that the monitor is imported too.
if (strings.HasPrefix(d1.dirKind, "monitor") && d2.instance.IsImported()) ||
(d1.instance.IsImported() && strings.HasPrefix(d2.dirKind, "monitor")) {
continue
}
// overlap is allowed in the case one side is data dir of a monitor instance,
// as the *_exporter don't need data dir, the field is only kept for compatibility
// with legacy tidb-ansible deployments.
if (strings.HasPrefix(d1.dirKind, "monitor data directory")) ||
(strings.HasPrefix(d2.dirKind, "monitor data directory")) {
continue
}
properties := map[string]string{
"ThisDirKind": d1.dirKind,
"ThisDir": d1.dir,
"ThisComponent": d1.instance.ComponentName(),
"ThisHost": d1.instance.GetManageHost(),
"ThatDirKind": d2.dirKind,
"ThatDir": d2.dir,
"ThatComponent": d2.instance.ComponentName(),
"ThatHost": d2.instance.GetManageHost(),
}
zap.L().Info("Meet deploy directory overlap", zap.Any("info", properties))
return errDeployDirOverlap.New("Deploy directory overlaps to another instance").WithProperty(tui.SuggestionFromTemplate(`
The directory you specified in the topology file is:
Directory: {{ColorKeyword}}{{.ThisDirKind}} {{.ThisDir}}{{ColorReset}}
Component: {{ColorKeyword}}{{.ThisComponent}} {{.ThisHost}}{{ColorReset}}
It overlaps to another instance:
Other Directory: {{ColorKeyword}}{{.ThatDirKind}} {{.ThatDir}}{{ColorReset}}
Other Component: {{ColorKeyword}}{{.ThatComponent}} {{.ThatHost}}{{ColorReset}}
Please modify the topology file and try again.
`, properties))
}
}
}
return nil
}
// CheckClusterPortConflict checks cluster port conflict
func CheckClusterPortConflict(clusterList map[string]Metadata, clusterName string, topo Topology) error {
type Entry struct {
clusterName string
componentName string
port int
instance Instance
}
currentEntries := []Entry{}
existingEntries := []Entry{}
for name, metadata := range clusterList {
if name == clusterName {
continue
}
uniqueHosts := set.NewStringSet()
metadata.GetTopology().IterInstance(func(inst Instance) {
mOpt := metadata.GetTopology().GetMonitoredOptions()
if mOpt == nil {
return
}
nodeExporterPort := mOpt.NodeExporterPort
blackboxExporterPort := mOpt.BlackboxExporterPort
for _, port := range inst.UsedPorts() {
existingEntries = append(existingEntries, Entry{
clusterName: name,
componentName: inst.ComponentName(),
port: port,
instance: inst,
})
}
if !uniqueHosts.Exist(inst.GetManageHost()) {
uniqueHosts.Insert(inst.GetManageHost())
existingEntries = append(existingEntries,
Entry{
clusterName: name,
componentName: RoleMonitor,
port: nodeExporterPort,
instance: inst,
},
Entry{
clusterName: name,
componentName: RoleMonitor,
port: blackboxExporterPort,
instance: inst,
})
}
})
}
uniqueHosts := set.NewStringSet()
topo.IterInstance(func(inst Instance) {
for _, port := range inst.UsedPorts() {
currentEntries = append(currentEntries, Entry{
componentName: inst.ComponentName(),
port: port,
instance: inst,
})
}
mOpt := topo.GetMonitoredOptions()
if mOpt == nil {
return
}
if !uniqueHosts.Exist(inst.GetManageHost()) {
uniqueHosts.Insert(inst.GetManageHost())
currentEntries = append(currentEntries,
Entry{
componentName: RoleMonitor,
port: mOpt.NodeExporterPort,
instance: inst,
},
Entry{
componentName: RoleMonitor,
port: mOpt.BlackboxExporterPort,
instance: inst,
})
}
})
for _, p1 := range currentEntries {
for _, p2 := range existingEntries {
if p1.instance.GetManageHost() != p2.instance.GetManageHost() {
continue
}
if p1.port == p2.port {
// build the conflict info
properties := map[string]string{
"ThisPort": strconv.Itoa(p1.port),
"ThisComponent": p1.componentName,
"ThisHost": p1.instance.GetManageHost(),
"ExistCluster": p2.clusterName,
"ExistPort": strconv.Itoa(p2.port),
"ExistComponent": p2.componentName,
"ExistHost": p2.instance.GetManageHost(),
}
// if one of the instances marks itself as ignore_exporter, do not report
// the monitoring agent ports conflict and just skip
if (p1.componentName == RoleMonitor || p2.componentName == RoleMonitor) &&
(p1.instance.IgnoreMonitorAgent() || p2.instance.IgnoreMonitorAgent()) {
zap.L().Debug("Ignored deploy port conflict", zap.Any("info", properties))
continue
}
// build error message
zap.L().Info("Meet deploy port conflict", zap.Any("info", properties))
return errDeployPortConflict.New("Deploy port conflicts to an existing cluster").WithProperty(tui.SuggestionFromTemplate(`
The port you specified in the topology file is:
Port: {{ColorKeyword}}{{.ThisPort}}{{ColorReset}}
Component: {{ColorKeyword}}{{.ThisComponent}} {{.ThisHost}}{{ColorReset}}
It conflicts to a port in the existing cluster:
Existing Cluster Name: {{ColorKeyword}}{{.ExistCluster}}{{ColorReset}}
Existing Port: {{ColorKeyword}}{{.ExistPort}}{{ColorReset}}
Existing Component: {{ColorKeyword}}{{.ExistComponent}} {{.ExistHost}}{{ColorReset}}
Please change to use another port or another host.
`, properties))
}
}
}
return nil
}
// TiKVLabelError indicates that some TiKV servers don't have correct labels
type TiKVLabelError struct {
TiKVInstances map[string][]error
}
// Error implements error
func (e *TiKVLabelError) Error() string {
ids := []string{}
for id := range e.TiKVInstances {
ids = append(ids, id)
}
sort.Strings(ids)
str := ""
for _, id := range ids {
if len(e.TiKVInstances[id]) == 0 {
continue
}
errs := []string{}
for _, e := range e.TiKVInstances[id] {
errs = append(errs, e.Error())
}
sort.Strings(errs)
str += fmt.Sprintf("%s:\n", id)
for _, e := range errs {
str += fmt.Sprintf("\t%s\n", e)
}
}
return str
}
// TiKVLabelProvider provides the store labels information
type TiKVLabelProvider interface {
GetTiKVLabels() (map[string]map[string]string, []map[string]api.LabelInfo, error)
}
func getHostFromAddress(addr string) string {
host, _ := utils.ParseHostPort(addr)
return host
}
// CheckTiKVLabels will check if tikv missing label or have wrong label
func CheckTiKVLabels(pdLocLabels []string, slp TiKVLabelProvider) error {
lerr := &TiKVLabelError{
TiKVInstances: make(map[string][]error),
}
lbs := set.NewStringSet(pdLocLabels...)
hosts := make(map[string]int)
storeLabels, _, err := slp.GetTiKVLabels()
if err != nil {
return err
}
for kv := range storeLabels {
host := getHostFromAddress(kv)
hosts[host]++
}
for kv, ls := range storeLabels {
host := getHostFromAddress(kv)
if len(ls) == 0 && hosts[host] > 1 {
lerr.TiKVInstances[kv] = append(
lerr.TiKVInstances[kv],
errors.New("multiple TiKV instances are deployed at the same host but location label missing"),
)
continue
}
for lname := range ls {
if !lbs.Exist(lname) {
lerr.TiKVInstances[kv] = append(
lerr.TiKVInstances[kv],
fmt.Errorf("label name '%s' is not specified in pd config (replication.location-labels: %v)", lname, pdLocLabels),
)
}
}
}
if len(lerr.TiKVInstances) == 0 {
return nil
}
return lerr
}
// 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 := 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 := strings.Split(topoType.Field(i).Tag.Get("yaml"), ",")[0] // without meta
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) portInvalidDetect() error {
topoSpec := reflect.ValueOf(s).Elem()
topoType := reflect.TypeOf(s).Elem()
checkPort := func(idx int, compSpec reflect.Value) error {
compSpec = reflect.Indirect(compSpec)
cfg := strings.Split(topoType.Field(idx).Tag.Get("yaml"), ",")[0]
for i := 0; i < compSpec.NumField(); i++ {
if strings.HasSuffix(compSpec.Type().Field(i).Name, "Port") {
port := int(compSpec.Field(i).Int())
// for NgPort, 0 means default and -1 means disable
if compSpec.Type().Field(i).Name == "NgPort" && (port == -1 || port == 0) {
continue
}
if port < 1 || port > 65535 {
portField := strings.Split(compSpec.Type().Field(i).Tag.Get("yaml"), ",")[0]
return errors.Errorf("`%s` of %s=%d is invalid, port should be in the range [1, 65535]", cfg, portField, port)
}
}
}
return nil
}
for i := 0; i < topoSpec.NumField(); i++ {
compSpecs := topoSpec.Field(i)
// check on struct
if compSpecs.Kind() == reflect.Struct {
if err := checkPort(i, compSpecs); err != nil {
return err
}
continue
}
// check on slice
for index := 0; index < compSpecs.Len(); index++ {
compSpec := reflect.Indirect(compSpecs.Index(index))
if err := checkPort(i, compSpec); err != nil {
return err
}
}
}
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",
"FlashServicePort",
"FlashProxyPort",
"FlashProxyStatusPort",
"ClusterPort",
"NgPort",
}
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))
// check hostname
host := compSpec.FieldByName("Host").String()
cfg := strings.Split(topoType.Field(i).Tag.Get("yaml"), ",")[0] // without meta
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 := reflect.Indirect(compSpecs.Index(index))
// check hostname
host := compSpec.FieldByName("Host").String()
cfg := strings.Split(topoType.Field(i).Tag.Get("yaml"), ",")[0] // without meta
if host == "" {
return errors.Errorf("`%s` contains empty host field", cfg)
}
// Directory conflicts
for _, dirType := range dirTypes {
j, found := findField(compSpec, dirType)
if !found {
continue
}
// `yaml:"data_dir,omitempty"`
tp := strings.Split(compSpec.Type().Field(j).Tag.Get("yaml"), ",")[0]
for dir := range strings.SplitSeq(compSpec.Field(j).String(), ",") {
dir = strings.TrimSpace(dir)
item := usedDir{
host: host,
dir: dir,
}
// 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
}
prev, exist := dirStats[item]
// not checking between imported nodes
if exist &&
!(compSpec.Addr().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.Addr().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{
"DeployDir",
"DataDir",
"LogDir",
}
// path -> count
dirStats := make(map[string]int)
count := 0
topoSpec := reflect.ValueOf(s).Elem()
dirPrefix = Abs(s.GlobalOptions.User, dirPrefix)
addHostDir := func(host, deployDir, dir string) {
if !strings.HasPrefix(dir, "/") {
dir = filepath.Join(deployDir, dir)
}
dir = Abs(s.GlobalOptions.User, dir)
dirStats[host+dir]++
}
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))
deployDir := compSpec.FieldByName("DeployDir").String()
host := compSpec.FieldByName("Host").String()
if compSpec.FieldByName("ManageHost").String() != "" {
host = compSpec.FieldByName("ManageHost").String()
}
for _, dirType := range dirTypes {
j, found := findField(compSpec, dirType)
if !found {
continue
}
dir := compSpec.Field(j).String()
switch dirType { // the same as in instance.go for (*instance)
case "DeployDir":
addHostDir(host, deployDir, "")
case "DataDir":
// the default data_dir is relative to deploy_dir
if dir == "" {
addHostDir(host, deployDir, dir)
continue
}
for dataDir := range strings.SplitSeq(dir, ",") {
dataDir = strings.TrimSpace(dataDir)
if dataDir != "" {
addHostDir(host, deployDir, dataDir)
}
}
case "LogDir":
field := compSpec.FieldByName("LogDir")
if field.IsValid() {
dir = field.Interface().(string)
}
if dir == "" {
dir = "log"
}
addHostDir(host, deployDir, strings.TrimSpace(dir))
}
}
}
}
for k, v := range dirStats {
if k == targetHost+dirPrefix || strings.HasPrefix(k, targetHost+dirPrefix+"/") {
count += v
}
}
return count
}
func (s *Specification) validateTiSparkSpec() error {
// There must be a Spark master
if len(s.TiSparkMasters) == 0 {
if len(s.TiSparkWorkers) == 0 {
return nil
}
return ErrNoTiSparkMaster
}
// We only support 1 Spark master at present
if len(s.TiSparkMasters) > 1 {
return ErrMultipleTiSparkMaster
}
// Multiple workers on the same host is not supported by Spark
if len(s.TiSparkWorkers) > 1 {
cnt := make(map[string]int)
for _, w := range s.TiSparkWorkers {
if cnt[w.Host] > 0 {
return errors.Annotatef(ErrMultipleTisparkWorker, "the host %s is duplicated", w.Host)
}
cnt[w.Host]++
}
}
return nil
}
func (s *Specification) validateTLSEnabled() error {
if !s.GlobalOptions.TLSEnabled {
return nil
}
// check for component with no tls support
compList := make([]Component, 0)
s.IterComponent(func(c Component) {
if len(c.Instances()) > 0 {
compList = append(compList, c)
}
})
for _, c := range compList {
switch c.Name() {
case ComponentPD,
ComponentTSO,
ComponentScheduling,
ComponentTiDB,
ComponentTiKV,
ComponentTiFlash,
ComponentTiProxy,
ComponentPump,
ComponentDrainer,
ComponentCDC,
ComponentTiKVCDC,
ComponentDashboard,
ComponentPrometheus,
ComponentAlertmanager,
ComponentGrafana:
default:
return errors.Errorf("component %s is not supported in TLS enabled cluster", c.Name())
}
}
return nil
}
func (s *Specification) validateUserGroup() error {
gOpts := s.GlobalOptions
if user := gOpts.User; !reUser.MatchString(user) {
return errors.Annotatef(ErrUserOrGroupInvalid, "`global` of user='%s' is invalid", user)
}
// if group is nil, then we'll set it to the same as user
if group := gOpts.Group; group != "" && !reGroup.MatchString(group) {
return errors.Annotatef(ErrUserOrGroupInvalid, "`global` of group='%s' is invalid", group)
}
return nil
}
func (s *Specification) validatePDNames() error {
// check pdserver name
pdNames := set.NewStringSet()
for _, pd := range s.PDServers {
if pd.Name == "" {
continue
}
if pdNames.Exist(pd.Name) {
return errors.Errorf("component pd_servers.name is not supported duplicated, the name %s is duplicated", pd.Name)
}
pdNames.Insert(pd.Name)
}
return nil
}
func (s *Specification) validateTSONames() error {
// check tso server name
tsoNames := set.NewStringSet()
for _, tso := range s.TSOServers {
if tso.Name == "" {
continue
}
if tsoNames.Exist(tso.Name) {
return errors.Errorf("component tso_servers.name is not supported duplicated, the name %s is duplicated", tso.Name)
}
tsoNames.Insert(tso.Name)
}
return nil
}
func (s *Specification) validateSchedulingNames() error {
// check scheduling server name
schedulingNames := set.NewStringSet()
for _, scheduling := range s.SchedulingServers {
if scheduling.Name == "" {
continue
}
if schedulingNames.Exist(scheduling.Name) {
return errors.Errorf("component scheduling_servers.name is not supported duplicated, the name %s is duplicated", scheduling.Name)
}
schedulingNames.Insert(scheduling.Name)
}
return nil
}
func (s *Specification) validateTiFlashConfigs() error {
c := FindComponent(s, ComponentTiFlash)
for _, ins := range c.Instances() {
if err := ins.(*TiFlashInstance).CheckIncorrectConfigs(); err != nil {
return err
}
}
return nil
}
// validateMonitorAgent checks for conflicts in topology for different ignore_exporter
// settings for multiple instances on the same host / IP
func (s *Specification) validateMonitorAgent() error {
type (
conflict struct {
ignore bool
cfg string
}
)
agentStats := 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 := strings.Split(topoType.Field(i).Tag.Get("yaml"), ",")[0] // without meta
if host == "" {
return errors.Errorf("`%s` contains empty host field", cfg)
}
// agent conflicts
stat := conflict{}
if j, found := findField(compSpec, "IgnoreExporter"); found {
stat.ignore = compSpec.Field(j).Bool()
stat.cfg = cfg
}
prev, exist := agentStats[host]
if exist {
if prev.ignore != stat.ignore {
return &meta.ValidateErr{
Type: meta.TypeMismatch,
Target: "ignore_exporter",
LHS: fmt.Sprintf("%s:%v", prev.cfg, prev.ignore),
RHS: fmt.Sprintf("%s:%v", stat.cfg, stat.ignore),
Value: host,
}
}
}
agentStats[host] = stat
}
}
return nil
}
// Validate validates the topology specification and produce error if the
// specification invalid (e.g: port conflicts or directory conflicts)
func (s *Specification) Validate() error {
validators := []func() error{
s.validateTLSEnabled,
s.platformConflictsDetect,
s.portInvalidDetect,
s.portConflictsDetect,
s.dirConflictsDetect,
s.validateUserGroup,
s.validatePDNames,
s.validateTSONames,
s.validateSchedulingNames,
s.validateTiSparkSpec,
s.validateTiFlashConfigs,
s.validateMonitorAgent,
}
for _, v := range validators {
if err := v(); err != nil {
return err
}
}
return RelativePathDetect(s, isSkipField)
}
// RelativePathDetect detect if some specific path is relative path and report error
func RelativePathDetect(topo any, isSkipField func(reflect.Value) bool) error {
pathTypes := []string{
"ConfigFilePath",
"RuleDir",
"DashboardDir",
}
topoSpec := reflect.ValueOf(topo).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))
// Relateve path detect
for _, field := range pathTypes {
if j, found := findField(compSpec, field); found {
// `yaml:"xxxx,omitempty"`
fieldName := strings.Split(compSpec.Type().Field(j).Tag.Get("yaml"), ",")[0]
localPath := compSpec.Field(j).String()
if localPath != "" && !strings.HasPrefix(localPath, "/") {
return fmt.Errorf("relative path is not allowed for field %s: %s", fieldName, localPath)
}
}
}
}
}
return nil
}
|