github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cli/disable_replication.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package cli
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/server"
    18  	"github.com/cockroachdb/cockroach/pkg/sql"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    20  )
    21  
    22  // cliDisableReplication changes the replication factor on
    23  // all defined zones to become 1. This is used by start-single-node
    24  // and demo to define single-node clusters, so as to avoid
    25  // churn in the log files.
    26  //
    27  // The change is effected using the internal SQL interface of the
    28  // given server object.
    29  func cliDisableReplication(ctx context.Context, s *server.Server) error {
    30  	return s.RunLocalSQL(ctx,
    31  		func(ctx context.Context, ie *sql.InternalExecutor) error {
    32  			rows, err := ie.Query(ctx, "get-zones", nil,
    33  				"SELECT target FROM crdb_internal.zones")
    34  			if err != nil {
    35  				return err
    36  			}
    37  
    38  			for _, row := range rows {
    39  				zone := string(*row[0].(*tree.DString))
    40  				if _, err := ie.Exec(ctx, "set-zone", nil,
    41  					fmt.Sprintf("ALTER %s CONFIGURE ZONE USING num_replicas = 1", zone)); err != nil {
    42  					return err
    43  				}
    44  			}
    45  
    46  			return nil
    47  		})
    48  }