diff --git a/go.mod b/go.mod index da2dafbc79..2d3137fd7c 100644 --- a/go.mod +++ b/go.mod @@ -99,7 +99,6 @@ require ( github.com/rivo/uniseg v0.4.4 // indirect github.com/rs/xid v1.4.0 // indirect github.com/sirupsen/logrus v1.9.0 // indirect - github.com/stretchr/objx v0.5.2 // indirect github.com/tklauser/go-sysconf v0.3.11 // indirect github.com/tklauser/numcpus v0.6.0 // indirect github.com/vishvananda/netlink v0.0.0-20210530105856-14e832ae1e8f // indirect diff --git a/go.sum b/go.sum index 5316ac9032..b3001521ff 100644 --- a/go.sum +++ b/go.sum @@ -214,8 +214,6 @@ github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUq github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/pkg/cluster/operation/upgrade.go b/pkg/cluster/operation/upgrade.go index a5eea3fc18..5c4c420858 100644 --- a/pkg/cluster/operation/upgrade.go +++ b/pkg/cluster/operation/upgrade.go @@ -18,7 +18,6 @@ import ( "crypto/tls" "fmt" "os" - "path/filepath" "reflect" "strings" "time" @@ -26,7 +25,6 @@ import ( perrs "github.com/pingcap/errors" "github.com/pingcap/tiup/pkg/checkpoint" "github.com/pingcap/tiup/pkg/cluster/api" - "github.com/pingcap/tiup/pkg/cluster/ctxt" "github.com/pingcap/tiup/pkg/cluster/spec" logprinter "github.com/pingcap/tiup/pkg/logger/printer" "github.com/pingcap/tiup/pkg/set" @@ -146,52 +144,6 @@ func Upgrade( logger.Debugf("Deferred upgrading of PD leader %s", instance.ID()) continue } - case spec.ComponentTSO: - // if component version is not specified, use the cluster version or latest("") - ins := instance.(*spec.TSOInstance) - version, err := getVersion(ctx, ins.DeployDir(), ins.GetManageHost()) - if err != nil { - return err - } - logger.Infof(fmt.Sprintf("tso instance change startScript %s, %s", version, ins.Name)) - if tidbver.PDSupportMicroServicesWithName(version) && ins.Name != "" { - tsos := topo.(*spec.Specification).TSOServers - var name string - for _, s := range tsos { - if s.Host == ins.Host && s.Port == ins.Port { - name = s.Name - break - } - } - logger.Infof(fmt.Sprintf("tso instance change startScript %s", name)) - ins := instance.(*spec.TSOInstance) - err := spec.ModifyPDStartScriptPath(ctx, ins.Role(), ins.Host, ins.GetMainPort(), name) - if err != nil { - return err - } - } - case spec.ComponentScheduling: - ins := instance.(*spec.SchedulingInstance) - version, err := getVersion(ctx, ins.DeployDir(), ins.GetManageHost()) - if err != nil { - return err - } - logger.Infof(fmt.Sprintf("scheduling instance change startScript %s, %s", version, ins.Name)) - if tidbver.PDSupportMicroServicesWithName(version) && ins.Name != "" { - schedulings := topo.(*spec.Specification).SchedulingServers - var name string - for _, s := range schedulings { - if s.Host == ins.Host && s.Port == ins.Port { - name = s.Name - break - } - } - logger.Infof(fmt.Sprintf("scheduling instance change startScript %s", name)) - err := spec.ModifyPDStartScriptPath(ctx, ins.Role(), ins.Host, ins.GetMainPort(), name) - if err != nil { - return err - } - } case spec.ComponentCDC: ins := instance.(*spec.CDCInstance) address := ins.GetAddr() @@ -410,25 +362,3 @@ func decreaseScheduleLimit(pc *api.PDClient, origLeaderScheduleLimit, origRegion } return pc.SetReplicationConfig("region-schedule-limit", origRegionScheduleLimit) } - -func getVersion(ctx context.Context, deployDir, host string) (string, error) { - binPath := filepath.Join(deployDir, "bin/pd-server") - e, found := ctxt.GetInner(ctx).GetExecutor(host) - if !found { - return "", fmt.Errorf("no executor") - } - stdout, stderr, err := e.Execute(ctx, binPath+" --version", false) - if err != nil { - return "", perrs.Annotatef(err, "stderr: %s", string(stderr)) - } - - var version string - for _, line := range strings.Split(string(stdout), "\n") { - if strings.HasPrefix(line, "Release Version:") { - // Extract version number from the line - version = strings.TrimSpace(strings.TrimPrefix(line, "Release Version:")) - break - } - } - return version, nil -} diff --git a/pkg/cluster/operation/upgrade_test.go b/pkg/cluster/operation/upgrade_test.go deleted file mode 100644 index 98469994e0..0000000000 --- a/pkg/cluster/operation/upgrade_test.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2024 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 operator - -import ( - "context" - "testing" - "time" - - "github.com/pingcap/tiup/pkg/cluster/ctxt" - logprinter "github.com/pingcap/tiup/pkg/logger/printer" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" -) - -// MockExecutor is a mock of Executor interface -type MockExecutor struct { - mock.Mock -} - -func (m *MockExecutor) Execute(ctx context.Context, cmd string, sudo bool, timeout ...time.Duration) (stdout []byte, stderr []byte, err error) { - args := m.Called(ctx, cmd, sudo) - return args.Get(0).([]byte), args.Get(1).([]byte), args.Error(2) -} - -func (m *MockExecutor) Transfer(ctx context.Context, src, dst string, download bool, limit int, compress bool) error { - panic("implement me") -} - -func TestGetVersion(t *testing.T) { - ctx := ctxt.New(context.Background(), 0, logprinter.NewLogger("")) - deployDir := "/fake/deploy/dir" - host := "localhost" - expectedVersion := "v8.3.0-abcdef" - mockExecutor := new(MockExecutor) - mockExecutor.On("Execute", ctx, "/fake/deploy/dir/bin/pd-server --version", false).Return([]byte("Release Version: v8.3.0-abcdef\nEdition: Community\nGit Commit Hash: b235bc1152a7942d71d42222b0e078d91ccd0106\nGit Branch: heads/refs/tags/v8.3.0\nUTC Build Time: 2024-07-01 05:08:57"), []byte(""), nil) - ctxt.GetInner(ctx).SetExecutor(host, mockExecutor) - // Execute - version, err := getVersion(ctx, deployDir, host) - assert.NoError(t, err) - assert.Equal(t, expectedVersion, version) - - expectedVersion = "v8.2.0-abcdef" - mockExecutor = new(MockExecutor) - mockExecutor.On("Execute", ctx, "/fake/deploy/dir/bin/pd-server --version", false).Return([]byte("Release Version: v8.2.0-abcdef\nEdition: Community\nGit Commit Hash: b235bc1152a7942d71d42222b0e078d91ccd0106\nGit Branch: heads/refs/tags/v8.3.0\nUTC Build Time: 2024-07-01 05:08:57"), []byte(""), nil) - ctxt.GetInner(ctx).SetExecutor(host, mockExecutor) - // Execute - version, err = getVersion(ctx, deployDir, host) - assert.NoError(t, err) - assert.Equal(t, expectedVersion, version) - - expectedVersion = "nightly-abcdef" - mockExecutor = new(MockExecutor) - mockExecutor.On("Execute", ctx, "/fake/deploy/dir/bin/pd-server --version", false).Return([]byte("Release Version: nightly-abcdef\nEdition: Community\nGit Commit Hash: b235bc1152a7942d71d42222b0e078d91ccd0106\nGit Branch: heads/refs/tags/v8.3.0\nUTC Build Time: 2024-07-01 05:08:57"), []byte(""), nil) - ctxt.GetInner(ctx).SetExecutor(host, mockExecutor) - // Execute - version, err = getVersion(ctx, deployDir, host) - assert.NoError(t, err) - assert.Equal(t, expectedVersion, version) - - // Teardown - mockExecutor.AssertExpectations(t) -} diff --git a/pkg/cluster/spec/util.go b/pkg/cluster/spec/util.go index e3abb495f0..26ba27c5ed 100644 --- a/pkg/cluster/spec/util.go +++ b/pkg/cluster/spec/util.go @@ -23,8 +23,6 @@ import ( "strings" "time" - "github.com/pingcap/errors" - "github.com/pingcap/tiup/pkg/cluster/ctxt" "github.com/pingcap/tiup/pkg/utils" "github.com/pingcap/tiup/pkg/version" "github.com/prometheus/common/expfmt" @@ -233,82 +231,3 @@ func GetDMMasterPackageName(topo Topology) string { } return ComponentDMMaster } - -var systemdUnitPath = "/etc/systemd/system" - -// ModifyPDStartScriptPath modify the start *PD* script path to add `--name` flag -func ModifyPDStartScriptPath(ctx context.Context, component, host string, port int, name string) error { - e, found := ctxt.GetInner(ctx).GetExecutor(host) - if !found { - return fmt.Errorf("no executor") - } - // 1. find the script path - serviceFile := fmt.Sprintf("%s/%s-%d.service", - systemdUnitPath, - component, - port) - cmd := fmt.Sprintf("grep 'ExecStart' %s | sed 's/ExecStart=//'", serviceFile) - stdout, stderr, err := e.Execute(ctx, cmd, false) - if err != nil { - return err - } - if len(stderr) > 0 { - return errors.Errorf( - "can not detect dir paths of %s %s:%d, %s", - component, - host, - port, - stderr, - ) - } - // 2. check script content if contains `name` - path := extractScriptPath(string(stdout)) - cmd = fmt.Sprintf("cat %s", path) - stdout, stderr, err = e.Execute(ctx, cmd, false) - if err != nil { - return err - } - if len(stderr) > 0 { - return errors.Errorf( - "can not read script path of %s %s:%d, %s", - component, - host, - port, - stderr, - ) - } - - if strings.Contains(string(stdout), "name") { - return nil - } - - // 3. writing the name to the script - content := fmt.Sprintf(" \\\n --name='%s'", name) - cmd = fmt.Sprintf("sed -i '$ s|$| %s|' %s", content, path) - stdout, stderr, err = e.Execute(ctx, cmd, false) - if err != nil { - return err - } - if len(stderr) > 0 { - return errors.Errorf( - "can not modify script path of %s %s:%d, %s", - component, - host, - port, - stderr, - ) - } - - return nil -} - -// path like `/bin/bash -c '/root/tidb-deploy/scheduling-3399/scripts/run_scheduling.sh'\n` -func extractScriptPath(command string) string { - command = strings.TrimSuffix(command, "\n") - parts := strings.Split(command, " ") - if len(parts) < 3 { - return "Invalid command structure" - } - - return strings.Trim(parts[2], "'") -} diff --git a/pkg/cluster/spec/util_test.go b/pkg/cluster/spec/util_test.go deleted file mode 100644 index 69a33b1a80..0000000000 --- a/pkg/cluster/spec/util_test.go +++ /dev/null @@ -1,129 +0,0 @@ -// 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" - "fmt" - "os" - "testing" - "time" - - "github.com/pingcap/check" - "github.com/pingcap/tiup/pkg/cluster/ctxt" - logprinter "github.com/pingcap/tiup/pkg/logger/printer" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" -) - -type utilSuite struct{} - -var _ = check.Suite(&utilSuite{}) - -func (s utilSuite) TestAbs(c *check.C) { - var path string - path = Abs(" foo", "") - c.Assert(path, check.Equals, "/home/foo") - path = Abs("foo ", " ") - c.Assert(path, check.Equals, "/home/foo") - path = Abs("foo", "bar") - c.Assert(path, check.Equals, "/home/foo/bar") - path = Abs("foo", " bar") - c.Assert(path, check.Equals, "/home/foo/bar") - path = Abs("foo", "bar ") - c.Assert(path, check.Equals, "/home/foo/bar") - path = Abs("foo", " bar ") - c.Assert(path, check.Equals, "/home/foo/bar") - path = Abs("foo", "/bar") - c.Assert(path, check.Equals, "/bar") - path = Abs("foo", " /bar") - c.Assert(path, check.Equals, "/bar") - path = Abs("foo", "/bar ") - c.Assert(path, check.Equals, "/bar") - path = Abs("foo", " /bar ") - c.Assert(path, check.Equals, "/bar") -} - -func (s *utilSuite) TestMultiDirAbs(c *check.C) { - paths := MultiDirAbs("tidb", "") - c.Assert(len(paths), check.Equals, 0) - - paths = MultiDirAbs("tidb", " ") - c.Assert(len(paths), check.Equals, 0) - - paths = MultiDirAbs("tidb", "a ") - c.Assert(len(paths), check.Equals, 1) - c.Assert(paths[0], check.Equals, "/home/tidb/a") - - paths = MultiDirAbs("tidb", "a , /tmp/b") - c.Assert(len(paths), check.Equals, 2) - c.Assert(paths[0], check.Equals, "/home/tidb/a") - c.Assert(paths[1], check.Equals, "/tmp/b") -} - -func TestExtractScriptPath(t *testing.T) { - command := "/bin/bash -c '/root/tidb-deploy/scheduling-3399/scripts/run_scheduling.sh'\n" - scriptPath := extractScriptPath(command) - println("scriptPath:", scriptPath) - assert.Equal(t, "/root/tidb-deploy/scheduling-3399/scripts/run_scheduling.sh", scriptPath) -} - -// MockExecutor simulates command execution -type MockExecutor struct { - mock.Mock -} - -func (m *MockExecutor) Execute(ctx context.Context, cmd string, sudo bool, timeout ...time.Duration) (stdout []byte, stderr []byte, err error) { - args := m.Called(ctx, cmd, sudo) - return args.Get(0).([]byte), args.Get(1).([]byte), args.Error(2) -} - -func (m *MockExecutor) Transfer(ctx context.Context, src, dst string, download bool, limit int, compress bool) error { - panic("implement me") -} - -func TestModifyStartScriptPath(t *testing.T) { - ctx := ctxt.New(context.Background(), 0, logprinter.NewLogger("")) - component := "test-component" - host := "localhost" - port := 8080 - content := fmt.Sprintf(" \\\n --name='%s'", "scheudling-0") - - // Setup temporary file for testing - assert := require.New(t) - conf, err := os.CreateTemp("", "scheduling.conf") - defer os.Remove(conf.Name()) - assert.Nil(err) - scriptPath := conf.Name() - err = os.WriteFile(scriptPath, []byte(content), 0644) - assert.Nil(err) - - // Mock executor - mockExec := new(MockExecutor) - shPath := fmt.Sprintf("/bin/bash -c '%s'", conf.Name()) - mockExec.On("Execute", ctx, mock.Anything, false).Return([]byte(shPath), []byte(""), nil) - - // Inject mock executor - ctxt.GetInner(ctx).SetExecutor(host, mockExec) - - // Test function - err = ModifyStartScriptPath(ctx, component, host, port, content) - assert.Nil(err) - - // Verify file content - resultContent, err := os.ReadFile(scriptPath) - assert.Nil(err) - assert.Contains(string(resultContent), content) -}