-
Notifications
You must be signed in to change notification settings - Fork 714
/
util.go
159 lines (137 loc) · 5.08 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
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
// Copyright 2016 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 server
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/tikv/pd/pkg/errs"
"github.com/tikv/pd/pkg/etcdutil"
"github.com/tikv/pd/pkg/typeutil"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/server/versioninfo"
"go.etcd.io/etcd/clientv3"
"go.uber.org/zap"
)
const (
requestTimeout = etcdutil.DefaultRequestTimeout
)
// LogPDInfo prints the PD version information.
func LogPDInfo() {
log.Info("Welcome to Placement Driver (PD)")
log.Info("PD", zap.String("release-version", versioninfo.PDReleaseVersion))
log.Info("PD", zap.String("edition", versioninfo.PDEdition))
log.Info("PD", zap.String("git-hash", versioninfo.PDGitHash))
log.Info("PD", zap.String("git-branch", versioninfo.PDGitBranch))
log.Info("PD", zap.String("utc-build-time", versioninfo.PDBuildTS))
}
// PrintPDInfo prints the PD version information without log info.
func PrintPDInfo() {
fmt.Println("Release Version:", versioninfo.PDReleaseVersion)
fmt.Println("Edition:", versioninfo.PDEdition)
fmt.Println("Git Commit Hash:", versioninfo.PDGitHash)
fmt.Println("Git Branch:", versioninfo.PDGitBranch)
fmt.Println("UTC Build Time: ", versioninfo.PDBuildTS)
}
// PrintConfigCheckMsg prints the message about configuration checks.
func PrintConfigCheckMsg(cfg *config.Config) {
if len(cfg.WarningMsgs) == 0 {
fmt.Println("config check successful")
return
}
for _, msg := range cfg.WarningMsgs {
fmt.Println(msg)
}
}
// CheckPDVersion checks if PD needs to be upgraded.
func CheckPDVersion(opt *config.PersistOptions) {
pdVersion := versioninfo.MinSupportedVersion(versioninfo.Base)
if versioninfo.PDReleaseVersion != "None" {
pdVersion = versioninfo.MustParseVersion(versioninfo.PDReleaseVersion)
}
clusterVersion := *opt.GetClusterVersion()
log.Info("load cluster version", zap.Stringer("cluster-version", clusterVersion))
if pdVersion.LessThan(clusterVersion) {
log.Warn(
"PD version less than cluster version, please upgrade PD",
zap.String("PD-version", pdVersion.String()),
zap.String("cluster-version", clusterVersion.String()))
}
}
func initOrGetClusterID(c *clientv3.Client, key string) (uint64, error) {
ctx, cancel := context.WithTimeout(c.Ctx(), requestTimeout)
defer cancel()
// Generate a random cluster ID.
ts := uint64(time.Now().Unix())
clusterID := (ts << 32) + uint64(rand.Uint32())
value := typeutil.Uint64ToBytes(clusterID)
// Multiple PDs may try to init the cluster ID at the same time.
// Only one PD can commit this transaction, then other PDs can get
// the committed cluster ID.
resp, err := c.Txn(ctx).
If(clientv3.Compare(clientv3.CreateRevision(key), "=", 0)).
Then(clientv3.OpPut(key, string(value))).
Else(clientv3.OpGet(key)).
Commit()
if err != nil {
return 0, errs.ErrEtcdTxnInternal.Wrap(err).GenWithStackByCause()
}
// Txn commits ok, return the generated cluster ID.
if resp.Succeeded {
return clusterID, nil
}
// Otherwise, parse the committed cluster ID.
if len(resp.Responses) == 0 {
return 0, errs.ErrEtcdTxnConflict.FastGenByArgs()
}
response := resp.Responses[0].GetResponseRange()
if response == nil || len(response.Kvs) != 1 {
return 0, errs.ErrEtcdTxnConflict.FastGenByArgs()
}
return typeutil.BytesToUint64(response.Kvs[0].Value)
}
func checkBootstrapRequest(clusterID uint64, req *pdpb.BootstrapRequest) error {
// TODO: do more check for request fields validation.
storeMeta := req.GetStore()
if storeMeta == nil {
return errors.Errorf("missing store meta for bootstrap %d", clusterID)
} else if storeMeta.GetId() == 0 {
return errors.New("invalid zero store id")
}
regionMeta := req.GetRegion()
if regionMeta == nil {
return errors.Errorf("missing region meta for bootstrap %d", clusterID)
} else if len(regionMeta.GetStartKey()) > 0 || len(regionMeta.GetEndKey()) > 0 {
// first region start/end key must be empty
return errors.Errorf("invalid first region key range, must all be empty for bootstrap %d", clusterID)
} else if regionMeta.GetId() == 0 {
return errors.New("invalid zero region id")
}
peers := regionMeta.GetPeers()
if len(peers) != 1 {
return errors.Errorf("invalid first region peer count %d, must be 1 for bootstrap %d", len(peers), clusterID)
}
peer := peers[0]
if peer.GetStoreId() != storeMeta.GetId() {
return errors.Errorf("invalid peer store id %d != %d for bootstrap %d", peer.GetStoreId(), storeMeta.GetId(), clusterID)
}
if peer.GetId() == 0 {
return errors.New("invalid zero peer id")
}
return nil
}