forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
114 lines (99 loc) · 3.11 KB
/
util.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
// Copyright 2019 TiKV Project Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"net/url"
"regexp"
"strings"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/tikv/pd/pkg/errs"
)
const (
// Label key consists of alphanumeric characters, '-', '_', '.' or '/', and must start and end with an
// alphanumeric character. If can also contain an extra '$' at the beginning.
keyFormat = "^[$]?[A-Za-z0-9]([-A-Za-z0-9_./]*[A-Za-z0-9])?$"
// Value key can be any combination of alphanumeric characters, '-', '_', '.' or '/'. It can also be empty to
// mark the label as deleted.
valueFormat = "^[-A-Za-z0-9_./]*$"
)
func validateFormat(s, format string) error {
isValid, _ := regexp.MatchString(format, s)
if !isValid {
return errors.Errorf("%s does not match format %q", s, format)
}
return nil
}
// ValidateLabels checks the legality of the labels.
func ValidateLabels(labels []*metapb.StoreLabel) error {
for _, label := range labels {
if err := validateFormat(label.Key, keyFormat); err != nil {
return err
}
if err := validateFormat(label.Value, valueFormat); err != nil {
return err
}
}
return nil
}
// ValidateURLWithScheme checks the format of the URL.
func ValidateURLWithScheme(rawURL string) error {
u, err := url.ParseRequestURI(rawURL)
if err != nil {
return err
}
if u.Scheme == "" || u.Host == "" {
return errors.Errorf("%s has no scheme", rawURL)
}
return nil
}
var schedulerMap = make(map[string]struct{})
// RegisterScheduler registers the scheduler type.
func RegisterScheduler(typ string) {
schedulerMap[typ] = struct{}{}
}
// IsSchedulerRegistered checks if the named scheduler type is registered.
func IsSchedulerRegistered(name string) bool {
_, ok := schedulerMap[name]
return ok
}
// NewTestOptions creates default options for testing.
func NewTestOptions() *PersistOptions {
// register default schedulers in case config check fail.
for _, d := range DefaultSchedulers {
RegisterScheduler(d.Type)
}
c := NewConfig()
c.Adjust(nil, false)
return NewPersistOptions(c)
}
// NewTestImmutableOptions creates default immutable options for testing.
func NewTestImmutableOptions() *ImmutableConfig {
c := NewConfig()
c.Adjust(nil, false)
return NewImmutableConfig(c)
}
// parseUrls parse a string into multiple urls.
func parseUrls(s string) ([]url.URL, error) {
items := strings.Split(s, ",")
urls := make([]url.URL, 0, len(items))
for _, item := range items {
u, err := url.Parse(item)
if err != nil {
return nil, errs.ErrURLParse.Wrap(err).GenWithStackByCause()
}
urls = append(urls, *u)
}
return urls, nil
}