forked from pingcap/tiup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpd.go
More file actions
278 lines (256 loc) · 8.48 KB
/
pd.go
File metadata and controls
278 lines (256 loc) · 8.48 KB
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
// 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 instance
import (
"context"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/tidbver"
"github.com/pingcap/tiup/pkg/utils"
)
// PDRole is the role of PD.
type PDRole = string
const (
// PDRoleNormal is the default role of PD
PDRoleNormal PDRole = "pd"
// PDRoleAPI is the role of PD API
PDRoleAPI PDRole = "api"
// PDRoleTSO is the role of PD TSO
PDRoleTSO PDRole = "tso"
// PDRoleScheduling is the role of PD scheduling
PDRoleScheduling PDRole = "scheduling"
// PDRoleRouter is the role of PD router
PDRoleRouter PDRole = "router"
// PDRoleResourceManager is the role of PD resource manager
PDRoleResourceManager PDRole = "resource-manager"
)
// PDInstance represent a running pd-server
type PDInstance struct {
instance
shOpt SharedOptions
initEndpoints []*PDInstance
joinEndpoints []*PDInstance
pds []*PDInstance
kvIsSingleReplica bool
}
var _ Instance = &PDInstance{}
// NewPDInstance return a PDInstance
func NewPDInstance(role PDRole, shOpt SharedOptions, binPath, dir, host, configPath string, id int, pds []*PDInstance, port int, kvIsSingleReplica bool) *PDInstance {
if port <= 0 {
port = 2379
}
return &PDInstance{
shOpt: shOpt,
instance: instance{
BinPath: binPath,
ID: id,
Dir: dir,
Host: host,
Port: utils.MustGetFreePort(host, 2380, shOpt.PortOffset),
StatusPort: utils.MustGetFreePort(host, port, shOpt.PortOffset),
ConfigPath: configPath,
role: role,
},
pds: pds,
kvIsSingleReplica: kvIsSingleReplica,
}
}
// Join set endpoints field of PDInstance
func (inst *PDInstance) Join(pds []*PDInstance) *PDInstance {
inst.joinEndpoints = pds
return inst
}
// InitCluster set the init cluster instance.
func (inst *PDInstance) InitCluster(pds []*PDInstance) *PDInstance {
inst.initEndpoints = pds
return inst
}
// Name return the name of pd.
func (inst *PDInstance) Name() string {
switch inst.Role() {
case PDRoleTSO:
return fmt.Sprintf("tso-%d", inst.ID)
case PDRoleScheduling:
return fmt.Sprintf("scheduling-%d", inst.ID)
case PDRoleRouter:
return fmt.Sprintf("router-%d", inst.ID)
case PDRoleResourceManager:
return fmt.Sprintf("resource_manager-%d", inst.ID)
default:
return fmt.Sprintf("pd-%d", inst.ID)
}
}
// Start calls set inst.cmd and Start
func (inst *PDInstance) Start(ctx context.Context) error {
var configFile string
switch inst.role {
case PDRoleNormal, PDRoleAPI:
configFile = "pd.toml"
case PDRoleResourceManager:
configFile = "resource_manager.toml"
default:
configFile = fmt.Sprintf("%s.toml", inst.role)
}
configPath := filepath.Join(inst.Dir, configFile)
if err := prepareConfig(
configPath,
inst.ConfigPath,
inst.getConfig(),
); err != nil {
return err
}
uid := inst.Name()
var args []string
switch inst.Role() {
case PDRoleNormal, PDRoleAPI:
if inst.Role() == PDRoleAPI {
args = []string{"services", "api"}
}
args = append(args, []string{
"--name=" + uid,
fmt.Sprintf("--config=%s", configPath),
fmt.Sprintf("--data-dir=%s", filepath.Join(inst.Dir, "data")),
fmt.Sprintf("--peer-urls=http://%s", utils.JoinHostPort(inst.Host, inst.Port)),
fmt.Sprintf("--advertise-peer-urls=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.Port)),
fmt.Sprintf("--client-urls=http://%s", utils.JoinHostPort(inst.Host, inst.StatusPort)),
fmt.Sprintf("--advertise-client-urls=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
}...)
switch {
case len(inst.initEndpoints) > 0:
endpoints := make([]string, 0)
for _, pd := range inst.initEndpoints {
uid := fmt.Sprintf("pd-%d", pd.ID)
endpoints = append(endpoints, fmt.Sprintf("%s=http://%s", uid, utils.JoinHostPort(AdvertiseHost(inst.Host), pd.Port)))
}
args = append(args, fmt.Sprintf("--initial-cluster=%s", strings.Join(endpoints, ",")))
case len(inst.joinEndpoints) > 0:
endpoints := make([]string, 0)
for _, pd := range inst.joinEndpoints {
endpoints = append(endpoints, fmt.Sprintf("http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), pd.Port)))
}
args = append(args, fmt.Sprintf("--join=%s", strings.Join(endpoints, ",")))
default:
return errors.Errorf("must set the init or join instances")
}
case PDRoleTSO:
endpoints := pdEndpoints(inst.pds, true)
args = []string{
"services",
"tso",
fmt.Sprintf("--listen-addr=http://%s", utils.JoinHostPort(inst.Host, inst.StatusPort)),
fmt.Sprintf("--advertise-listen-addr=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)),
fmt.Sprintf("--backend-endpoints=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
fmt.Sprintf("--config=%s", configPath),
}
if tidbver.PDSupportMicroservicesWithName(inst.Version.String()) {
args = append(args, fmt.Sprintf("--name=%s", uid))
}
case PDRoleScheduling:
endpoints := pdEndpoints(inst.pds, true)
args = []string{
"services",
"scheduling",
fmt.Sprintf("--listen-addr=http://%s", utils.JoinHostPort(inst.Host, inst.StatusPort)),
fmt.Sprintf("--advertise-listen-addr=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)),
fmt.Sprintf("--backend-endpoints=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
fmt.Sprintf("--config=%s", configPath),
}
if tidbver.PDSupportMicroservicesWithName(inst.Version.String()) {
args = append(args, fmt.Sprintf("--name=%s", uid))
}
case PDRoleRouter:
endpoints := pdEndpoints(inst.pds, true)
args = []string{
"services",
"router",
fmt.Sprintf("--listen-addr=http://%s", utils.JoinHostPort(inst.Host, inst.StatusPort)),
fmt.Sprintf("--advertise-listen-addr=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)),
fmt.Sprintf("--backend-endpoints=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
fmt.Sprintf("--config=%s", configPath),
}
if tidbver.PDSupportMicroservicesWithName(inst.Version.String()) {
args = append(args, fmt.Sprintf("--name=%s", uid))
}
case PDRoleResourceManager:
endpoints := pdEndpoints(inst.pds, true)
args = []string{
"services",
"resource-manager",
fmt.Sprintf("--listen-addr=http://%s", utils.JoinHostPort(inst.Host, inst.StatusPort)),
fmt.Sprintf("--advertise-listen-addr=http://%s", utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)),
fmt.Sprintf("--backend-endpoints=%s", strings.Join(endpoints, ",")),
fmt.Sprintf("--log-file=%s", inst.LogFile()),
fmt.Sprintf("--config=%s", configPath),
}
if tidbver.PDSupportMicroservicesWithName(inst.Version.String()) {
args = append(args, fmt.Sprintf("--name=%s", uid))
}
}
return inst.PrepareProcess(ctx, inst.BinPath, args, nil, inst.Dir)
}
// Component return the component name.
func (inst *PDInstance) Component() string {
return PDRoleNormal
}
// LogFile return the log file.
func (inst *PDInstance) LogFile() string {
if inst.Role() == PDRoleNormal || inst.Role() == PDRoleAPI {
return filepath.Join(inst.Dir, "pd.log")
}
return filepath.Join(inst.Dir, fmt.Sprintf("%s.log", inst.Role()))
}
// Addr return the listen address of PD
func (inst *PDInstance) Addr() string {
return utils.JoinHostPort(AdvertiseHost(inst.Host), inst.StatusPort)
}
// Ready returns nil when PD is ready to serve.
func (inst *PDInstance) Ready(ctx context.Context) error {
url := fmt.Sprintf("http://%s/pd/api/v1/members", inst.Addr())
var r struct {
Header struct {
ClusterID uint64 `json:"cluster_id"`
} `json:"header"`
}
ready := func() bool {
resp, err := http.Get(url)
if err != nil {
return false
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
err = json.NewDecoder(resp.Body).Decode(&r)
return err == nil && r.Header.ClusterID != 0
}
return false
}
for {
if ready() {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
// retry
}
}
}