-
Notifications
You must be signed in to change notification settings - Fork 217
PMM-14931 Control built-in ClickHouse config via PMM_CLICKHOUSE_CONFIG env var #5414
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
Open
4nte
wants to merge
17
commits into
v3
Choose a base branch
from
PMM-14931-clickhouse-env-vars
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8097f49
PMM-14931 Deprecate switch-config.sh script
4nte b2d13f6
PMM-14931 Specify corresponding user config expliclity
4nte 58ef0e9
PMM-14931 Implement `PMM_CLICKHOUSE_CONFIG` env var
4nte 586ada6
PMM-14931 Omit check for user config file
4nte 78502bf
PMM-14931 Add docs for `PMM_CLICKHOUSE_CONFIG` env var
4nte ee3a962
PMM-14931 Add tests
4nte 1f8c5ec
PMM-14931 Simplify validateClickhouseConfigAt
4nte e90d5f1
PMM-14931 Rename "Clickhouse" to "ClickHouse"
4nte cd8969d
PMM-14931 Add test case for when config dir is a file
4nte e1033e3
PMM-14931 Run `make format`
4nte 71e455c
PMM-14931 Add package comment
4nte 452dac1
PMM-14931 Add a don't rule to AGENTS.md
ademidoff 8a2479b
PMM-14931 Drop quotes from error message
4nte 6631f19
PMM-14931 Improve docs on low-memory config
4nte 4ecf568
PMM-14931 Fix test error message assertion
4nte ac4c17f
Merge branch 'v3' into PMM-14931-clickhouse-env-vars
4nte af3ebcc
Merge branch 'v3' into PMM-14931-clickhouse-env-vars
4nte 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
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 |
|---|---|---|
| @@ -1,55 +1,7 @@ | ||
| #!/bin/bash | ||
| # Usage: switch-config.sh [low|default] | ||
| # Switches /etc/clickhouse-server/config.xml | ||
| # switch-config.sh is deprecated and will be removed in a future PMM release. | ||
| # Use the PMM_CLICKHOUSE_CONFIG environment variable instead. | ||
|
|
||
| set -e | ||
| CONFIG_DIR="/etc/clickhouse-server" | ||
| PROFILE="$1" | ||
|
|
||
| if [ -z "$PROFILE" ]; then | ||
| echo "Usage: $0 [low|default]" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| case "$PROFILE" in | ||
| low) | ||
| CONFIG_TARGET="low-memory-config.xml" | ||
| USERS_TARGET="low-memory-users.xml" | ||
| ;; | ||
| default) | ||
| CONFIG_TARGET="default-config.xml" | ||
| USERS_TARGET="default-users.xml" | ||
| ;; | ||
| *) | ||
| echo "Usage: $0 [low|default]" >&2 | ||
| exit 1 | ||
| ;; | ||
| esac | ||
|
|
||
| if [ ! -e "$CONFIG_DIR/$CONFIG_TARGET" ]; then | ||
| echo "Config profile $CONFIG_TARGET does not exist in $CONFIG_DIR." >&2 | ||
| exit 2 | ||
| fi | ||
| if [ ! -e "$CONFIG_DIR/$USERS_TARGET" ]; then | ||
| echo "Users profile $USERS_TARGET does not exist in $CONFIG_DIR." >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| echo "Stopping clickhouse..." | ||
| if ! supervisorctl stop clickhouse; then | ||
| echo "Failed to stop clickhouse!" >&2 | ||
| exit 3 | ||
| fi | ||
|
|
||
| ln -sf "$CONFIG_TARGET" "$CONFIG_DIR/config.xml" | ||
| ln -sf "$USERS_TARGET" "$CONFIG_DIR/users.xml" | ||
| echo "Switched config.xml to $CONFIG_TARGET." | ||
| echo "Switched users.xml to $USERS_TARGET." | ||
|
|
||
| echo "Starting clickhouse..." | ||
| if ! supervisorctl start clickhouse; then | ||
| echo "Failed to start clickhouse!" >&2 | ||
| exit 4 | ||
| fi | ||
|
|
||
| exit 0 | ||
| echo "switch-config.sh is deprecated and will be removed in a future PMM release." >&2 | ||
| echo "Set PMM_CLICKHOUSE_CONFIG=default|low-memory" >&2 | ||
| exit 1 |
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
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
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,80 @@ | ||
| // Copyright (C) 2023 Percona LLC | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| // Package clickhouse provides facilities for working with clickhouse. | ||
| package clickhouse | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultClickHouseConfig = "default" | ||
| clickHouseConfigDir = "/etc/clickhouse-server" | ||
| ) | ||
|
|
||
| // GetClickHouseConfig returns the config name if the matching | ||
| // <config>-config.xml files exist on disk. | ||
| // Empty input falls back to defaultClickHouseConfig. | ||
| func GetClickHouseConfig(config string) (string, error) { | ||
| if config == "" { | ||
| return defaultClickHouseConfig, nil | ||
| } | ||
|
|
||
| return config, validateClickHouseConfigAt(config, clickHouseConfigDir) | ||
| } | ||
|
|
||
| // validateClickHouseConfigAt returns an error if configuration files are missing for given config | ||
| func validateClickHouseConfigAt(config, dir string) error { | ||
| availableConfigs, err := availableClickHouseConfigs(dir) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to get available ClickHouse configs: %w", err) | ||
| } | ||
|
|
||
| path := filepath.Join(dir, config+"-config.xml") | ||
| if _, err := os.Stat(path); err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return fmt.Errorf( | ||
| "invalid PMM_CLICKHOUSE_CONFIG=%q: %s not found; available configs: %v", | ||
|
4nte marked this conversation as resolved.
Outdated
|
||
| config, path, availableConfigs, | ||
| ) | ||
| } | ||
| return fmt.Errorf("cannot stat %s: %w", path, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // availableClickHouseConfigs lists config names that are present in the dir | ||
| func availableClickHouseConfigs(dir string) ([]string, error) { | ||
| var configs []string | ||
|
|
||
| matches, err := filepath.Glob(filepath.Join(dir, "*-config.xml")) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, m := range matches { | ||
| name := strings.TrimSuffix(filepath.Base(m), "-config.xml") | ||
| configs = append(configs, name) | ||
| } | ||
|
|
||
| sort.Strings(configs) | ||
| return configs, nil | ||
| } | ||
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 (C) 2023 Percona LLC | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| package clickhouse | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // writeConfigFiles creates the given files in dir with placeholder content. | ||
| func writeConfigFiles(t *testing.T, dir string, names ...string) { | ||
| t.Helper() | ||
| for _, name := range names { | ||
| require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("<clickhouse/>"), 0o600)) | ||
| } | ||
| } | ||
|
|
||
| func TestGetClickHouseConfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Empty input falls back to the default config. | ||
| got, err := GetClickHouseConfig("") | ||
| require.NoError(t, err) | ||
| assert.Equal(t, defaultClickHouseConfig, got) | ||
| } | ||
|
|
||
| func TestValidateClickHouseConfigAt(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir := t.TempDir() | ||
| // A config is valid as long as <name>-config.xml exists. | ||
| writeConfigFiles( | ||
| t, dir, | ||
| "default-config.xml", | ||
| "low-memory-config.xml", | ||
| ) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| config string | ||
| errContains []string | ||
| }{ | ||
| {name: "default", config: "default"}, | ||
| {name: "low-memory", config: "low-memory"}, | ||
| { | ||
| name: "missing", | ||
| config: "nonexistent", | ||
| errContains: []string{ | ||
| `invalid PMM_CLICKHOUSE_CONFIG="nonexistent"`, | ||
| "available configs:", | ||
| "default", "low-memory", | ||
| }, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := validateClickHouseConfigAt(tt.config, dir) | ||
| if tt.errContains == nil { | ||
| require.NoError(t, err) | ||
| return | ||
| } | ||
| require.Error(t, err) | ||
| for _, substr := range tt.errContains { | ||
| assert.Contains(t, err.Error(), substr) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| t.Run("invalid config dir", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| base := t.TempDir() | ||
| // "notdir" is a regular file; using it as the config dir makes os.Stat fail | ||
| require.NoError(t, os.WriteFile(filepath.Join(base, "notdir"), nil, 0o600)) | ||
|
|
||
| err := validateClickHouseConfigAt("default", filepath.Join(base, "notdir")) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "cannot stat") | ||
| assert.NotContains(t, err.Error(), "available configs:") | ||
| }) | ||
| } | ||
|
|
||
| func TestAvailableClickHouseConfigs(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("empty dir", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, err := availableClickHouseConfigs(t.TempDir()) | ||
| require.NoError(t, err) | ||
| assert.Empty(t, got) | ||
| }) | ||
|
|
||
| t.Run("lists config names sorted, ignoring non-config files", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir := t.TempDir() | ||
| writeConfigFiles( | ||
| t, dir, | ||
| "low-memory-config.xml", | ||
| "default-config.xml", | ||
| "dhparam.pem", // not a *-config.xml, must be ignored | ||
| ) | ||
|
|
||
| got, err := availableClickHouseConfigs(dir) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, []string{"default", "low-memory"}, got) | ||
| }) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.