-
Notifications
You must be signed in to change notification settings - Fork 312
/
tispark.go
490 lines (437 loc) · 15.5 KB
/
tispark.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
// 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 (
"context"
"crypto/tls"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/google/uuid"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/checkpoint"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"github.com/pingcap/tiup/pkg/cluster/template/config"
"github.com/pingcap/tiup/pkg/cluster/template/scripts"
system "github.com/pingcap/tiup/pkg/cluster/template/systemd"
"github.com/pingcap/tiup/pkg/meta"
"go.uber.org/zap"
)
// TiSparkMasterSpec is the topology specification for TiSpark master node
type TiSparkMasterSpec struct {
Host string `yaml:"host"`
ListenHost string `yaml:"listen_host,omitempty"`
SSHPort int `yaml:"ssh_port,omitempty" validate:"ssh_port:editable"`
Imported bool `yaml:"imported,omitempty"`
Patched bool `yaml:"patched,omitempty"`
IgnoreExporter bool `yaml:"ignore_exporter,omitempty"`
Port int `yaml:"port" default:"7077"`
WebPort int `yaml:"web_port" default:"8080"`
DeployDir string `yaml:"deploy_dir,omitempty"`
JavaHome string `yaml:"java_home,omitempty" validate:"java_home:editable"`
SparkConfigs map[string]interface{} `yaml:"spark_config,omitempty" validate:"spark_config:ignore"`
SparkEnvs map[string]string `yaml:"spark_env,omitempty" validate:"spark_env:ignore"`
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
}
// Role returns the component role of the instance
func (s *TiSparkMasterSpec) Role() string {
return RoleTiSparkMaster
}
// SSH returns the host and SSH port of the instance
func (s *TiSparkMasterSpec) SSH() (string, int) {
return s.Host, s.SSHPort
}
// GetMainPort returns the main port of the instance
func (s *TiSparkMasterSpec) GetMainPort() int {
return s.Port
}
// IsImported returns if the node is imported from TiDB-Ansible
func (s *TiSparkMasterSpec) IsImported() bool {
return s.Imported
}
// IgnoreMonitorAgent returns if the node does not have monitor agents available
func (s *TiSparkMasterSpec) IgnoreMonitorAgent() bool {
return s.IgnoreExporter
}
// TiSparkWorkerSpec is the topology specification for TiSpark slave nodes
type TiSparkWorkerSpec struct {
Host string `yaml:"host"`
ListenHost string `yaml:"listen_host,omitempty"`
SSHPort int `yaml:"ssh_port,omitempty" validate:"ssh_port:editable"`
Imported bool `yaml:"imported,omitempty"`
Patched bool `yaml:"patched,omitempty"`
IgnoreExporter bool `yaml:"ignore_exporter,omitempty"`
Port int `yaml:"port" default:"7078"`
WebPort int `yaml:"web_port" default:"8081"`
DeployDir string `yaml:"deploy_dir,omitempty"`
JavaHome string `yaml:"java_home,omitempty" validate:"java_home:editable"`
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
}
// Role returns the component role of the instance
func (s *TiSparkWorkerSpec) Role() string {
return RoleTiSparkWorker
}
// SSH returns the host and SSH port of the instance
func (s *TiSparkWorkerSpec) SSH() (string, int) {
return s.Host, s.SSHPort
}
// GetMainPort returns the main port of the instance
func (s *TiSparkWorkerSpec) GetMainPort() int {
return s.Port
}
// IsImported returns if the node is imported from TiDB-Ansible
func (s *TiSparkWorkerSpec) IsImported() bool {
return s.Imported
}
// IgnoreMonitorAgent returns if the node does not have monitor agents available
func (s *TiSparkWorkerSpec) IgnoreMonitorAgent() bool {
return s.IgnoreExporter
}
// TiSparkMasterComponent represents TiSpark master component.
type TiSparkMasterComponent struct{ Topology *Specification }
// Name implements Component interface.
func (c *TiSparkMasterComponent) Name() string {
return ComponentTiSpark
}
// Role implements Component interface.
func (c *TiSparkMasterComponent) Role() string {
return RoleTiSparkMaster
}
// Instances implements Component interface.
func (c *TiSparkMasterComponent) Instances() []Instance {
ins := make([]Instance, 0, len(c.Topology.TiSparkMasters))
for _, s := range c.Topology.TiSparkMasters {
s := s
ins = append(ins, &TiSparkMasterInstance{
BaseInstance: BaseInstance{
InstanceSpec: s,
Name: c.Name(),
Host: s.Host,
Port: s.Port,
SSHP: s.SSHPort,
Ports: []int{
s.Port,
s.WebPort,
},
Dirs: []string{
s.DeployDir,
},
StatusFn: func(_ context.Context, tlsCfg *tls.Config, _ ...string) string {
return statusByHost(s.Host, s.WebPort, "", tlsCfg)
},
UptimeFn: func(_ context.Context, tlsCfg *tls.Config) time.Duration {
return 0
},
},
topo: c.Topology,
})
}
return ins
}
// TiSparkMasterInstance represent the TiSpark master instance
type TiSparkMasterInstance struct {
BaseInstance
topo Topology
}
// GetCustomFields get custom spark configs of the instance
func (i *TiSparkMasterInstance) GetCustomFields() map[string]interface{} {
v := reflect.Indirect(reflect.ValueOf(i.InstanceSpec)).FieldByName("SparkConfigs")
if !v.IsValid() {
return nil
}
return v.Interface().(map[string]interface{})
}
// GetCustomEnvs get custom spark envionment variables of the instance
func (i *TiSparkMasterInstance) GetCustomEnvs() map[string]string {
v := reflect.Indirect(reflect.ValueOf(i.InstanceSpec)).FieldByName("SparkEnvs")
if !v.IsValid() {
return nil
}
return v.Interface().(map[string]string)
}
// GetJavaHome returns the java_home value in spec
func (i *TiSparkMasterInstance) GetJavaHome() string {
return reflect.Indirect(reflect.ValueOf(i.InstanceSpec)).FieldByName("JavaHome").String()
}
// InitConfig implement Instance interface
func (i *TiSparkMasterInstance) InitConfig(
ctx context.Context,
e ctxt.Executor,
clusterName,
clusterVersion,
deployUser string,
paths meta.DirPaths,
) (err error) {
// generate systemd service to invoke spark's start/stop scripts
comp := i.Role()
host := i.GetHost()
port := i.GetPort()
topo := i.topo.(*Specification)
sysCfg := filepath.Join(paths.Cache, fmt.Sprintf("%s-%s-%d.service", comp, host, port))
// insert checkpoint
point := checkpoint.Acquire(ctx, CopyConfigFile, map[string]interface{}{"config-file": sysCfg})
defer func() {
point.Release(err, zap.String("config-file", sysCfg))
}()
if point.Hit() != nil {
return nil
}
systemCfg := system.NewTiSparkConfig(comp, deployUser, paths.Deploy, i.GetJavaHome())
if err := systemCfg.ConfigToFile(sysCfg); err != nil {
return errors.Trace(err)
}
tgt := filepath.Join("/tmp", comp+"_"+uuid.New().String()+".service")
if err := e.Transfer(ctx, sysCfg, tgt, false, 0, false); err != nil {
return errors.Annotatef(err, "transfer from %s to %s failed", sysCfg, tgt)
}
cmd := fmt.Sprintf("mv %s /etc/systemd/system/%s-%d.service", tgt, comp, port)
if _, _, err := e.Execute(ctx, cmd, true); err != nil {
return errors.Annotatef(err, "execute: %s", cmd)
}
// transfer default config
pdList := make([]string, 0)
for _, pd := range topo.Endpoints(deployUser) {
pdList = append(pdList, fmt.Sprintf("%s:%d", pd.IP, pd.ClientPort))
}
masterList := make([]string, 0)
for _, master := range topo.TiSparkMasters {
masterList = append(masterList, fmt.Sprintf("%s:%d", master.Host, master.Port))
}
cfg := config.NewTiSparkConfig(pdList).WithMasters(strings.Join(masterList, ",")).
WithCustomFields(i.GetCustomFields())
// transfer spark-defaults.conf
fp := filepath.Join(paths.Cache, fmt.Sprintf("spark-defaults-%s-%d.conf", host, port))
if err := cfg.ConfigToFile(fp); err != nil {
return err
}
dst := filepath.Join(paths.Deploy, "conf", "spark-defaults.conf")
if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil {
return err
}
env := scripts.NewTiSparkEnv(host).
WithLocalIP(i.GetListenHost()).
WithMaster(host).
WithMasterPorts(i.Ports[0], i.Ports[1]).
WithCustomEnv(i.GetCustomEnvs())
// transfer spark-env.sh file
fp = filepath.Join(paths.Cache, fmt.Sprintf("spark-env-%s-%d.sh", host, port))
if err := env.ScriptToFile(fp); err != nil {
return err
}
// tispark files are all in a "spark" sub-directory of deploy dir
dst = filepath.Join(paths.Deploy, "conf", "spark-env.sh")
if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil {
return err
}
// transfer log4j config (it's not a template but a static file)
fp = filepath.Join(paths.Cache, fmt.Sprintf("spark-log4j-%s-%d.properties", host, port))
log4jFile, err := config.GetConfig("spark-log4j.properties.tpl")
if err != nil {
return err
}
if err := os.WriteFile(fp, log4jFile, 0644); err != nil {
return err
}
dst = filepath.Join(paths.Deploy, "conf", "log4j.properties")
return e.Transfer(ctx, fp, dst, false, 0, false)
}
// ScaleConfig deploy temporary config on scaling
func (i *TiSparkMasterInstance) ScaleConfig(
ctx context.Context,
e ctxt.Executor,
topo Topology,
clusterName,
clusterVersion,
deployUser string,
paths meta.DirPaths,
) error {
s := i.topo
defer func() { i.topo = s }()
cluster := mustBeClusterTopo(topo)
i.topo = cluster.Merge(i.topo)
return i.InitConfig(ctx, e, clusterName, clusterVersion, deployUser, paths)
}
// TiSparkWorkerComponent represents TiSpark slave component.
type TiSparkWorkerComponent struct{ Topology *Specification }
// Name implements Component interface.
func (c *TiSparkWorkerComponent) Name() string {
return ComponentTiSpark
}
// Role implements Component interface.
func (c *TiSparkWorkerComponent) Role() string {
return RoleTiSparkWorker
}
// Instances implements Component interface.
func (c *TiSparkWorkerComponent) Instances() []Instance {
ins := make([]Instance, 0, len(c.Topology.TiSparkWorkers))
for _, s := range c.Topology.TiSparkWorkers {
ins = append(ins, &TiSparkWorkerInstance{
BaseInstance: BaseInstance{
InstanceSpec: s,
Name: c.Name(),
Host: s.Host,
Port: s.Port,
SSHP: s.SSHPort,
Ports: []int{
s.Port,
s.WebPort,
},
Dirs: []string{
s.DeployDir,
},
StatusFn: func(_ context.Context, tlsCfg *tls.Config, _ ...string) string {
return statusByHost(s.Host, s.WebPort, "", tlsCfg)
},
UptimeFn: func(_ context.Context, tlsCfg *tls.Config) time.Duration {
return 0
},
},
topo: c.Topology,
})
}
return ins
}
// TiSparkWorkerInstance represent the TiSpark slave instance
type TiSparkWorkerInstance struct {
BaseInstance
topo Topology
}
// GetJavaHome returns the java_home value in spec
func (i *TiSparkWorkerInstance) GetJavaHome() string {
return reflect.Indirect(reflect.ValueOf(i.InstanceSpec)).FieldByName("JavaHome").String()
}
// InitConfig implement Instance interface
func (i *TiSparkWorkerInstance) InitConfig(
ctx context.Context,
e ctxt.Executor,
clusterName,
clusterVersion,
deployUser string,
paths meta.DirPaths,
) (err error) {
// generate systemd service to invoke spark's start/stop scripts
comp := i.Role()
host := i.GetHost()
port := i.GetPort()
topo := i.topo.(*Specification)
sysCfg := filepath.Join(paths.Cache, fmt.Sprintf("%s-%s-%d.service", comp, host, port))
// insert checkpoint
point := checkpoint.Acquire(ctx, CopyConfigFile, map[string]interface{}{"config-file": sysCfg})
defer func() {
point.Release(err, zap.String("config-file", sysCfg))
}()
if point.Hit() != nil {
return nil
}
systemCfg := system.NewTiSparkConfig(comp, deployUser, paths.Deploy, i.GetJavaHome())
if err := systemCfg.ConfigToFile(sysCfg); err != nil {
return errors.Trace(err)
}
tgt := filepath.Join("/tmp", comp+"_"+uuid.New().String()+".service")
if err := e.Transfer(ctx, sysCfg, tgt, false, 0, false); err != nil {
return errors.Annotatef(err, "transfer from %s to %s failed", sysCfg, tgt)
}
cmd := fmt.Sprintf("mv %s /etc/systemd/system/%s-%d.service", tgt, comp, port)
if _, _, err := e.Execute(ctx, cmd, true); err != nil {
return errors.Annotatef(err, "execute: %s", cmd)
}
// transfer default config
pdList := make([]string, 0)
for _, pd := range topo.Endpoints(deployUser) {
pdList = append(pdList, fmt.Sprintf("%s:%d", pd.IP, pd.ClientPort))
}
masterList := make([]string, 0)
for _, master := range topo.TiSparkMasters {
masterList = append(masterList, fmt.Sprintf("%s:%d", master.Host, master.Port))
}
cfg := config.NewTiSparkConfig(pdList).WithMasters(strings.Join(masterList, ",")).
WithCustomFields(topo.TiSparkMasters[0].SparkConfigs)
// doesn't work
if _, err := i.setTLSConfig(ctx, false, nil, paths); err != nil {
return err
}
// transfer spark-defaults.conf
fp := filepath.Join(paths.Cache, fmt.Sprintf("spark-defaults-%s-%d.conf", host, port))
if err := cfg.ConfigToFile(fp); err != nil {
return err
}
dst := filepath.Join(paths.Deploy, "conf", "spark-defaults.conf")
if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil {
return err
}
env := scripts.NewTiSparkEnv(host).
WithLocalIP(i.GetListenHost()).
WithMaster(topo.TiSparkMasters[0].Host).
WithMasterPorts(topo.TiSparkMasters[0].Port, topo.TiSparkMasters[0].WebPort).
WithWorkerPorts(i.Ports[0], i.Ports[1]).
WithCustomEnv(topo.TiSparkMasters[0].SparkEnvs)
// transfer spark-env.sh file
fp = filepath.Join(paths.Cache, fmt.Sprintf("spark-env-%s-%d.sh", host, port))
if err := env.ScriptToFile(fp); err != nil {
return err
}
// tispark files are all in a "spark" sub-directory of deploy dir
dst = filepath.Join(paths.Deploy, "conf", "spark-env.sh")
if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil {
return err
}
// transfer start-slave.sh
fp = filepath.Join(paths.Cache, fmt.Sprintf("start-tispark-slave-%s-%d.sh", host, port))
slaveSh, err := env.SlaveScriptWithTemplate()
if err != nil {
return err
}
if err := os.WriteFile(fp, slaveSh, 0755); err != nil {
return err
}
dst = filepath.Join(paths.Deploy, "sbin", "start-slave.sh")
if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil {
return err
}
// transfer log4j config (it's not a template but a static file)
fp = filepath.Join(paths.Cache, fmt.Sprintf("spark-log4j-%s-%d.properties", host, port))
log4jFile, err := config.GetConfig("spark-log4j.properties.tpl")
if err != nil {
return err
}
if err := os.WriteFile(fp, log4jFile, 0644); err != nil {
return err
}
dst = filepath.Join(paths.Deploy, "conf", "log4j.properties")
return e.Transfer(ctx, fp, dst, false, 0, false)
}
// setTLSConfig set TLS Config to support enable/disable TLS
// TiSparkWorkerInstance no need to configure TLS
func (i *TiSparkWorkerInstance) setTLSConfig(ctx context.Context, enableTLS bool, configs map[string]interface{}, paths meta.DirPaths) (map[string]interface{}, error) {
return nil, nil
}
// ScaleConfig deploy temporary config on scaling
func (i *TiSparkWorkerInstance) ScaleConfig(
ctx context.Context,
e ctxt.Executor,
topo Topology,
clusterName,
clusterVersion,
deployUser string,
paths meta.DirPaths,
) error {
s := i.topo
defer func() { i.topo = s }()
i.topo = topo.Merge(i.topo)
return i.InitConfig(ctx, e, clusterName, clusterVersion, deployUser, paths)
}