-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: use zone configs to disable replication
As established in #39382 it is not safe to modify the global default zone config objects to non-standard values. This commit changes `start-single-node` and `demo` to use zone configs via SQL instead. Also as requested by @piyush-singh it now informs the user the replication was disabled, for example: ``` * * INFO: Replication was disabled for this cluster. * When/if adding nodes in the future, update zone configurations to increase the replication factor. * ``` Release note: None
- Loading branch information
Showing
7 changed files
with
149 additions
and
42 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,52 @@ | ||
// Copyright 2019 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/server" | ||
"github.com/cockroachdb/cockroach/pkg/sql" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
) | ||
|
||
// cliDisableReplication changes the replication factor on | ||
// all defined zones to become 1. This is used by start-single-node | ||
// and demo to define single-node clusters, so as to avoid | ||
// churn in the log files. | ||
// | ||
// The change is effected using the internal SQL interface of the | ||
// given server object. | ||
func cliDisableReplication(ctx context.Context, s *server.Server) error { | ||
return s.RunLocalSQL(ctx, | ||
func(ctx context.Context, ie *sql.InternalExecutor) error { | ||
rows, err := ie.Query(ctx, "get-zones", nil, | ||
"SELECT target FROM crdb_internal.zones") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, row := range rows { | ||
zone := string(*row[0].(*tree.DString)) | ||
if _, err := ie.Exec(ctx, "set-zone", nil, | ||
fmt.Sprintf("ALTER %s CONFIGURE ZONE USING num_replicas = 1", zone)); err != nil { | ||
return err | ||
} | ||
} | ||
log.Shout(ctx, log.Severity_INFO, | ||
"Replication was disabled for this cluster.\n"+ | ||
"When/if adding nodes in the future, update zone configurations to increase the replication factor.") | ||
|
||
return nil | ||
}) | ||
} |
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,51 @@ | ||
#! /usr/bin/env expect -f | ||
|
||
source [file join [file dirname $argv0] common.tcl] | ||
|
||
spawn /bin/bash | ||
send "PS1=':''/# '\r" | ||
eexpect ":/# " | ||
|
||
start_test "Check that demo disables replication properly" | ||
send "$argv demo -e 'show zone configuration for range default'\r" | ||
eexpect "num_replicas = 1" | ||
eexpect ":/# " | ||
end_test | ||
|
||
start_test "Check that start-single-node disables replication properly" | ||
system "rm -rf logs/db" | ||
start_server $argv | ||
send "$argv sql -e 'show zone configuration for range default'\r" | ||
eexpect "num_replicas = 1" | ||
eexpect ":/# " | ||
end_test | ||
|
||
start_test "Check that it remains possible to reset the replication factor" | ||
send "$argv sql -e 'alter range default configure zone using num_replicas = 3'\r" | ||
eexpect "CONFIGURE ZONE" | ||
eexpect ":/# " | ||
stop_server $argv | ||
start_server $argv | ||
send "$argv sql -e 'show zone configuration for range default'\r" | ||
eexpect "num_replicas = 3" | ||
eexpect ":/# " | ||
end_test | ||
|
||
stop_server $argv | ||
|
||
start_test "Check that start-single-node on a regular cluster does not reset the replication factor" | ||
# make a fresh server but using the regular 'start' | ||
system "rm -rf logs/db" | ||
system "$argv start --insecure --pid-file=server_pid --background -s=path=logs/db >>logs/expect-cmd.log 2>&1; | ||
$argv sql -e 'select 1'" | ||
# restart with start-single-node | ||
stop_server $argv | ||
start_server $argv | ||
# check that the replication factor was unchanged | ||
send "$argv sql -e 'show zone configuration for range default'\r" | ||
eexpect "num_replicas = 3" | ||
eexpect ":/# " | ||
end_test | ||
|
||
send "exit 0\r" | ||
eexpect eof |
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
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