-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
playground: add dm support for playground (#2465)
* add dm support for playground Signed-off-by: Siddon Tang <[email protected]> * support command-line args, scale in, alive check * fix exported * fix cognitive-complexity * fix name * fix command-line args * remove debug comment --------- Signed-off-by: Siddon Tang <[email protected]> Co-authored-by: Siddon Tang <[email protected]>
- Loading branch information
1 parent
964b40b
commit 57e845b
Showing
5 changed files
with
364 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package instance | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pingcap/tiup/pkg/utils" | ||
) | ||
|
||
// DMMaster represent a DM master instance. | ||
type DMMaster struct { | ||
instance | ||
Process | ||
initEndpoints []*DMMaster | ||
} | ||
|
||
var _ Instance = &DMMaster{} | ||
|
||
// NewDMMaster create a new DMMaster instance. | ||
func NewDMMaster(binPath string, dir, host, configPath string, portOffset int, id int, port int) *DMMaster { | ||
if port <= 0 { | ||
port = 8261 | ||
} | ||
return &DMMaster{ | ||
instance: instance{ | ||
BinPath: binPath, | ||
ID: id, | ||
Dir: dir, | ||
Host: host, | ||
Port: utils.MustGetFreePort(host, 8291, portOffset), | ||
// Similar like PD's client port, here use StatusPort for Master Port. | ||
StatusPort: utils.MustGetFreePort(host, port, portOffset), | ||
ConfigPath: configPath, | ||
}, | ||
} | ||
} | ||
|
||
// Name return the name of the instance. | ||
func (m *DMMaster) Name() string { | ||
return fmt.Sprintf("dm-master-%d", m.ID) | ||
} | ||
|
||
// Start starts the instance. | ||
func (m *DMMaster) Start(ctx context.Context) error { | ||
args := []string{ | ||
fmt.Sprintf("--name=%s", m.Name()), | ||
fmt.Sprintf("--master-addr=http://%s", utils.JoinHostPort(m.Host, m.StatusPort)), | ||
fmt.Sprintf("--advertise-addr=http://%s", utils.JoinHostPort(AdvertiseHost(m.Host), m.StatusPort)), | ||
fmt.Sprintf("--peer-urls=http://%s", utils.JoinHostPort(m.Host, m.Port)), | ||
fmt.Sprintf("--advertise-peer-urls=http://%s", utils.JoinHostPort(AdvertiseHost(m.Host), m.Port)), | ||
fmt.Sprintf("--log-file=%s", m.LogFile()), | ||
} | ||
|
||
endpoints := make([]string, 0) | ||
for _, master := range m.initEndpoints { | ||
endpoints = append(endpoints, fmt.Sprintf("%s=http://%s", master.Name(), utils.JoinHostPort(master.Host, master.Port))) | ||
} | ||
args = append(args, fmt.Sprintf("--initial-cluster=%s", strings.Join(endpoints, ","))) | ||
|
||
if m.ConfigPath != "" { | ||
args = append(args, fmt.Sprintf("--config=%s", m.ConfigPath)) | ||
} | ||
|
||
m.Process = &process{cmd: PrepareCommand(ctx, m.BinPath, args, nil, m.Dir)} | ||
|
||
logIfErr(m.Process.SetOutputFile(m.LogFile())) | ||
return m.Process.Start() | ||
} | ||
|
||
// SetInitEndpoints set the initial endpoints for the DM master. | ||
func (m *DMMaster) SetInitEndpoints(endpoints []*DMMaster) { | ||
m.initEndpoints = endpoints | ||
} | ||
|
||
// Component return the component of the instance. | ||
func (m *DMMaster) Component() string { | ||
return "dm-master" | ||
} | ||
|
||
// LogFile return the log file path of the instance. | ||
func (m *DMMaster) LogFile() string { | ||
return filepath.Join(m.Dir, "dm-master.log") | ||
} | ||
|
||
// Addr return the address of the instance. | ||
func (m *DMMaster) Addr() string { | ||
return utils.JoinHostPort(m.Host, m.StatusPort) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package instance | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pingcap/tiup/pkg/utils" | ||
) | ||
|
||
// DMWorker represent a DM worker instance. | ||
type DMWorker struct { | ||
instance | ||
Process | ||
|
||
masters []*DMMaster | ||
} | ||
|
||
var _ Instance = &DMWorker{} | ||
|
||
// NewDMWorker create a DMWorker instance. | ||
func NewDMWorker(binPath string, dir, host, configPath string, portOffset int, id int, port int, masters []*DMMaster) *DMWorker { | ||
if port <= 0 { | ||
port = 8262 | ||
} | ||
return &DMWorker{ | ||
instance: instance{ | ||
BinPath: binPath, | ||
ID: id, | ||
Dir: dir, | ||
Host: host, | ||
Port: utils.MustGetFreePort(host, port, portOffset), | ||
ConfigPath: configPath, | ||
}, | ||
masters: masters, | ||
} | ||
} | ||
|
||
// MasterAddrs return the master addresses. | ||
func (w *DMWorker) MasterAddrs() []string { | ||
var addrs []string | ||
for _, master := range w.masters { | ||
addrs = append(addrs, utils.JoinHostPort(AdvertiseHost(master.Host), master.StatusPort)) | ||
} | ||
return addrs | ||
} | ||
|
||
// Name return the name of the instance. | ||
func (w *DMWorker) Name() string { | ||
return fmt.Sprintf("dm-worker-%d", w.ID) | ||
} | ||
|
||
// Start starts the instance. | ||
func (w *DMWorker) Start(ctx context.Context) error { | ||
args := []string{ | ||
fmt.Sprintf("--name=%s", w.Name()), | ||
fmt.Sprintf("--worker-addr=%s", utils.JoinHostPort(w.Host, w.Port)), | ||
fmt.Sprintf("--advertise-addr=%s", utils.JoinHostPort(AdvertiseHost(w.Host), w.Port)), | ||
fmt.Sprintf("--join=%s", strings.Join(w.MasterAddrs(), ",")), | ||
fmt.Sprintf("--log-file=%s", w.LogFile()), | ||
} | ||
|
||
if w.ConfigPath != "" { | ||
args = append(args, fmt.Sprintf("--config=%s", w.ConfigPath)) | ||
} | ||
|
||
w.Process = &process{cmd: PrepareCommand(ctx, w.BinPath, args, nil, w.Dir)} | ||
|
||
logIfErr(w.Process.SetOutputFile(w.LogFile())) | ||
|
||
return w.Process.Start() | ||
} | ||
|
||
// Component return the component of the instance. | ||
func (w *DMWorker) Component() string { | ||
return "dm-worker" | ||
} | ||
|
||
// LogFile return the log file of the instance. | ||
func (w *DMWorker) LogFile() string { | ||
return filepath.Join(w.Dir, "dm-worker.log") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.