Skip to content

Commit

Permalink
Merge pull request #9253 from knz/zoneset-disable-repl
Browse files Browse the repository at this point in the history
cli: fix the doc for 'zone set' and add a new flag --disable-replication
  • Loading branch information
knz authored Sep 10, 2016
2 parents 029cc21 + 06d24c9 commit 1e61d60
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
23 changes: 21 additions & 2 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func captureOutput(f func()) (out string, err error) {

func (c cliTest) RunWithArgs(a []string) {
sqlCtx.execStmts = nil
zoneConfig = ""
zoneDisableReplication = false

var args []string
args = append(args, a[0])
Expand Down Expand Up @@ -497,6 +499,8 @@ func Example_zone() {
c.Run("zone rm .default")
c.Run("zone set .default --file=./testdata/zone_range_max_bytes.yaml")
c.Run("zone get system")
c.Run("zone set .default --disable-replication")
c.Run("zone get system")

// Output:
// zone ls
Expand Down Expand Up @@ -526,15 +530,15 @@ func Example_zone() {
// range_max_bytes: 134217728
// gc:
// ttlseconds: 86400
// num_replicas: 1
// num_replicas: 3
// constraints: [us-east-1a, ssd]
// zone get system
// system
// range_min_bytes: 1048576
// range_max_bytes: 134217728
// gc:
// ttlseconds: 86400
// num_replicas: 1
// num_replicas: 3
// constraints: [us-east-1a, ssd]
// zone rm system
// DELETE 1
Expand All @@ -547,6 +551,21 @@ func Example_zone() {
// range_max_bytes: 134217728
// gc:
// ttlseconds: 86400
// num_replicas: 3
// constraints: []
// zone get system
// .default
// range_min_bytes: 1048576
// range_max_bytes: 134217728
// gc:
// ttlseconds: 86400
// num_replicas: 3
// constraints: []
// zone set .default --disable-replication
// range_min_bytes: 1048576
// range_max_bytes: 134217728
// gc:
// ttlseconds: 86400
// num_replicas: 1
// constraints: []
// zone get system
Expand Down
7 changes: 7 additions & 0 deletions cli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ tiers and order must be the same on all nodes. For example:
File to read the zone configuration from. Specify "-" to read from standard input.`,
}

ZoneDisableReplication = FlagInfo{
Name: "disable-replication",
Description: `
Disable replication in the zone by setting the desired replica count to 1.
Equivalent to setting 'num_replicas: 1' via -f.`,
}

Background = FlagInfo{
Name: "background",
Description: `
Expand Down
5 changes: 4 additions & 1 deletion cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var maxResults int64

var connURL string
var connUser, connHost, connPort, httpPort, httpAddr, connDBName, zoneConfig string
var zoneDisableReplication bool
var startBackground bool
var undoFreezeCluster bool

Expand Down Expand Up @@ -364,7 +365,9 @@ func init() {
boolFlag(f, &cliCtx.prettyFmt, cliflags.Pretty, isInteractive)
}

stringFlag(setZoneCmd.Flags(), &zoneConfig, cliflags.ZoneConfig, "")
zf := setZoneCmd.Flags()
stringFlag(zf, &zoneConfig, cliflags.ZoneConfig, "")
boolFlag(zf, &zoneDisableReplication, cliflags.ZoneDisableReplication, false)

varFlag(sqlShellCmd.Flags(), &sqlCtx.execStmts, cliflags.Execute)

Expand Down
1 change: 1 addition & 0 deletions cli/testdata/zone_range_max_bytes.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
range_max_bytes: 134217728
num_replicas: 3
39 changes: 26 additions & 13 deletions cli/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,19 +401,18 @@ specified zone-config.
The zone config format has the following YAML schema:
replicas:
- attrs: [comma-separated attribute list]
- attrs: ...
num_replicas: <num>
constraints: [comma-separated attribute list]
range_min_bytes: <size-in-bytes>
range_max_bytes: <size-in-bytes>
gc:
ttlseconds: <time-in-seconds>
For example, to set the zone config for the system database, run:
cockroach zone set system "replicas:
- attrs: [us-east-1a, ssd]
- attrs: [us-east-1b, ssd]
- attrs: [us-west-1b, ssd]"
$ cockroach zone set system -f - << EOF
num_replicas: 3
constraints: [ssd, -mem]
EOF
Note that the specified zone config is merged with the existing zone config for
the database or table.
Expand All @@ -422,6 +421,25 @@ the database or table.
RunE: runSetZone,
}

func readZoneConfig() (conf []byte, err error) {
if zoneDisableReplication {
if zoneConfig != "" {
return nil, fmt.Errorf("cannot specify --disable-replication and -f at the same time")
}
conf = []byte("num_replicas: 1")
} else {
switch zoneConfig {
case "":
err = fmt.Errorf("no filename specified with -f")
case "-":
conf, err = ioutil.ReadAll(os.Stdin)
default:
conf, err = ioutil.ReadFile(zoneConfig)
}
}
return conf, err
}

// runSetZone parses the yaml input file, converts it to proto, and inserts it
// in the system.zones table.
func runSetZone(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -463,12 +481,7 @@ func runSetZone(cmd *cobra.Command, args []string) error {
// understood format.
// Read zoneConfig file to conf.

var conf []byte
if zoneConfig == "-" {
conf, err = ioutil.ReadAll(os.Stdin)
} else {
conf, err = ioutil.ReadFile(zoneConfig)
}
conf, err := readZoneConfig()
if err != nil {
return fmt.Errorf("error reading zone config: %s", err)
}
Expand Down

0 comments on commit 1e61d60

Please sign in to comment.