-
Notifications
You must be signed in to change notification settings - Fork 338
cluster: honor ignore_exporter when destroying a cluster #2737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kaaaaaaang
merged 3 commits into
pingcap:master
from
ekexium:fix/destroy-ignore-exporter
Sep 8, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,128 @@ | ||
| // Copyright 2026 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" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/pingcap/tiup/pkg/cluster/ctxt" | ||
| "github.com/pingcap/tiup/pkg/cluster/spec" | ||
| logprinter "github.com/pingcap/tiup/pkg/logger/printer" | ||
| "github.com/stretchr/testify/require" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| type destroyExecutor struct { | ||
| commands []string | ||
| } | ||
|
|
||
| func (e *destroyExecutor) Execute(_ context.Context, cmd string, _ bool, _ ...time.Duration) ([]byte, []byte, error) { | ||
| e.commands = append(e.commands, cmd) | ||
| // Empty ss output models stopped ports, so an unexpected exporter cleanup | ||
| // is caught by the assertions without waiting for a timeout. | ||
| return nil, nil, nil | ||
| } | ||
|
|
||
| func (*destroyExecutor) Transfer(_ context.Context, _, _ string, _ bool, _ int, _ bool) error { | ||
| return nil | ||
| } | ||
|
|
||
| func TestDestroyIgnoreExporter(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| ignoreExporter bool | ||
| multipleInstances bool | ||
| force bool | ||
| }{ | ||
| {name: "shared", ignoreExporter: true}, | ||
| {name: "shared_multiple_instances", ignoreExporter: true, multipleInstances: true}, | ||
| {name: "shared_force", ignoreExporter: true, force: true}, | ||
| {name: "managed"}, | ||
| {name: "managed_multiple_instances", multipleInstances: true}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| topology := fmt.Sprintf(` | ||
| global: | ||
| user: tidb | ||
| deploy_dir: /tidb-deploy | ||
| data_dir: /tidb-data | ||
| monitored: | ||
| deploy_dir: /tidb-deploy/monitor-9100 | ||
| data_dir: /tidb-data/monitor-9100 | ||
| log_dir: /tidb-log/monitor-9100 | ||
| pd_servers: | ||
| - host: 192.0.2.1 | ||
| ignore_exporter: %t | ||
| `, tc.ignoreExporter) | ||
| if tc.multipleInstances { | ||
| topology += fmt.Sprintf(` | ||
| tidb_servers: | ||
| - host: 192.0.2.1 | ||
| ignore_exporter: %t | ||
| `, tc.ignoreExporter) | ||
| } | ||
| var topo spec.Specification | ||
| require.NoError(t, yaml.Unmarshal([]byte(topology), &topo)) | ||
|
|
||
| exec := &destroyExecutor{} | ||
| ctx := ctxt.New(context.Background(), 1, logprinter.NewLogger("")) | ||
| inner := ctxt.GetInner(ctx) | ||
| inner.SetExecutor("192.0.2.1", exec) | ||
| inner.PublicKeyPath = filepath.Join(t.TempDir(), "id_rsa.pub") | ||
| require.NoError(t, os.WriteFile(inner.PublicKeyPath, []byte("ssh-ed25519 test-key"), 0600)) | ||
|
|
||
| require.NoError(t, Destroy(ctx, &topo, Options{Force: tc.force})) | ||
|
|
||
| // Destroy must still remove the cluster's own components. | ||
| require.Contains(t, exec.commands, "rm -rf /etc/systemd/system/pd-2379.service;") | ||
| if tc.multipleInstances { | ||
| require.Contains(t, exec.commands, "rm -rf /etc/systemd/system/tidb-4000.service;") | ||
| } | ||
|
|
||
| wantDeletes := 1 | ||
| if tc.ignoreExporter { | ||
| wantDeletes = 0 | ||
| } | ||
| for _, path := range []string{ | ||
| "/tidb-deploy/monitor-9100", | ||
| "/tidb-data/monitor-9100", | ||
| "/tidb-log/monitor-9100", | ||
| "/etc/systemd/system/node_exporter-9100.service", | ||
| "/etc/systemd/system/blackbox_exporter-9115.service", | ||
| } { | ||
| deletes := 0 | ||
| for _, cmd := range exec.commands { | ||
| if strings.HasPrefix(cmd, "rm -rf ") && strings.Contains(cmd, path) { | ||
| deletes++ | ||
| } | ||
| } | ||
| require.Equal(t, wantDeletes, deletes, "deletions of %s: %v", path, exec.commands) | ||
| } | ||
|
|
||
| portChecks := 0 | ||
| for _, cmd := range exec.commands { | ||
| if cmd == "ss -ltn" { | ||
| portChecks++ | ||
| } | ||
| } | ||
| require.Equal(t, 2*wantDeletes, portChecks, "each managed exporter port is checked once") | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Aggregate
ignore_exporterby host before monitored cleanup.DestroyMonitoredruns once per host, but Line 61 checks only the last processed instance. If one instance on a shared host hasignore_exporter: trueand a later instance does not, component order can still delete the shared exporter files and units.Record ignored hosts during the initial
cluster.IterInstancepass. Skip monitored cleanup when any instance on that host ignores the monitor agent. Add mixed-value cases toTestDestroyIgnoreExporter.Proposed fix
instCount := map[string]int{} +noAgentHosts := set.NewStringSet() cluster.IterInstance(func(inst spec.Instance) { instCount[inst.GetManageHost()]++ + if inst.IgnoreMonitorAgent() { + noAgentHosts.Insert(inst.GetManageHost()) + } }) ... - if cluster.GetMonitoredOptions() != nil && !inst.IgnoreMonitorAgent() { + if cluster.GetMonitoredOptions() != nil && !noAgentHosts.Exist(inst.GetManageHost()) {🤖 Prompt for AI Agents