Skip to content
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

Inclusive naming: master->primary in topo package #9182

Merged
merged 1 commit into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions go/vt/topo/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,21 @@ type Conn interface {
Watch(ctx context.Context, filePath string) (current *WatchData, changes <-chan *WatchData, cancel CancelFunc)

//
// Master election methods. This is meant to have a small
// Leader election methods. This is meant to have a small
// number of processes elect a primary within a group. The
// backend storage for this can either be the global topo
// server, or a resilient quorum of individual cells, to
// reduce the load / dependency on the global topo server.
//

// NewMasterParticipation creates a MasterParticipation
// object, used to become the Master in an election for the
// NewLeaderParticipation creates a LeaderParticipation
// object, used to become the Leader in an election for the
// provided group name. Id is the name of the local process,
// passing in the hostname:port of the current process as id
// is the common usage. Id must be unique for each process
// calling this, for a given name. Calling this function does
// not make the current process a candidate for the election.
NewMasterParticipation(name, id string) (MasterParticipation, error)
NewLeaderParticipation(name, id string) (LeaderParticipation, error)

// Close closes the connection to the server.
Close()
Expand Down Expand Up @@ -270,14 +270,14 @@ type WatchData struct {
Err error
}

// MasterParticipation is the object returned by NewMasterParticipation.
// LeaderParticipation is the object returned by NewLeaderParticipation.
// Sample usage:
//
// mp := server.NewMasterParticipation("vtctld", "hostname:8080")
// mp := server.NewLeaderParticipation("vtctld", "hostname:8080")
// job := NewJob()
// go func() {
// for {
// ctx, err := mp.WaitForMastership()
// ctx, err := mp.WaitForLeadership()
// switch err {
// case nil:
// job.RunUntilContextDone(ctx)
Expand All @@ -294,34 +294,34 @@ type WatchData struct {
// if job.Running() {
// job.WriteStatus(w, r)
// } else {
// http.Redirect(w, r, mp.GetCurrentMasterID(context.Background()), http.StatusFound)
// http.Redirect(w, r, mp.GetCurrentLeaderID(context.Background()), http.StatusFound)
// }
// })
//
// servenv.OnTermSync(func() {
// mp.Stop()
// })
type MasterParticipation interface {
// WaitForMastership makes the current process a candidate
type LeaderParticipation interface {
// WaitForLeadership makes the current process a candidate
// for election, and waits until this process is the primary.
// After we become the primary, we may lose primaryship. In that case,
// the returned context will be canceled. If Stop was called,
// WaitForMastership will return nil, ErrInterrupted.
WaitForMastership() (context.Context, error)
// WaitForLeadership will return nil, ErrInterrupted.
WaitForLeadership() (context.Context, error)

// Stop is called when we don't want to participate in the
// primary election any more. Typically, that is when the
// hosting process is terminating. We will relinquish
// primaryship at that point, if we had it. Stop should
// not return until everything has been done.
// The MasterParticipation object should be discarded
// after Stop has been called. Any call to WaitForMastership
// The LeaderParticipation object should be discarded
// after Stop has been called. Any call to WaitForLeadership
// after Stop() will return nil, ErrInterrupted.
// If WaitForMastership() was running, it will return
// If WaitForLeadership() was running, it will return
// nil, ErrInterrupted as soon as possible.
Stop()

// GetCurrentMasterID returns the current primary id.
// GetCurrentLeaderID returns the current primary id.
// This may not work after Stop has been called.
GetCurrentMasterID(ctx context.Context) (string, error)
GetCurrentLeaderID(ctx context.Context) (string, error)
}
30 changes: 15 additions & 15 deletions go/vt/topo/consultopo/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
"vitess.io/vitess/go/vt/topo"
)

// NewMasterParticipation is part of the topo.Server interface
func (s *Server) NewMasterParticipation(name, id string) (topo.MasterParticipation, error) {
return &consulMasterParticipation{
// NewLeaderParticipation is part of the topo.Server interface
func (s *Server) NewLeaderParticipation(name, id string) (topo.LeaderParticipation, error) {
return &consulLeaderParticipation{
s: s,
name: name,
id: id,
Expand All @@ -38,15 +38,15 @@ func (s *Server) NewMasterParticipation(name, id string) (topo.MasterParticipati
}, nil
}

// consulMasterParticipation implements topo.MasterParticipation.
// consulLeaderParticipation implements topo.LeaderParticipation.
//
// We use a key with name <global>/elections/<name> for the lock,
// that contains the id.
type consulMasterParticipation struct {
type consulLeaderParticipation struct {
// s is our parent consul topo Server
s *Server

// name is the name of this MasterParticipation
// name is the name of this LeaderParticipation
name string

// id is the process's current id.
Expand All @@ -59,8 +59,8 @@ type consulMasterParticipation struct {
done chan struct{}
}

// WaitForMastership is part of the topo.MasterParticipation interface.
func (mp *consulMasterParticipation) WaitForMastership() (context.Context, error) {
// WaitForLeadership is part of the topo.LeaderParticipation interface.
func (mp *consulLeaderParticipation) WaitForLeadership() (context.Context, error) {

electionPath := path.Join(mp.s.root, electionsPath, mp.name)
l, err := mp.s.client.LockOpts(&api.LockOptions{
Expand All @@ -74,7 +74,7 @@ func (mp *consulMasterParticipation) WaitForMastership() (context.Context, error
// If Stop was already called, mp.done is closed, so we are interrupted.
select {
case <-mp.done:
return nil, topo.NewError(topo.Interrupted, "mastership")
return nil, topo.NewError(topo.Interrupted, "Leadership")
default:
}

Expand All @@ -98,15 +98,15 @@ func (mp *consulMasterParticipation) WaitForMastership() (context.Context, error
lockCancel()
// We could have lost the lock. Per consul API, explicitly call Unlock to make sure that session will not be renewed.
if err := l.Unlock(); err != nil {
log.Errorf("master election(%v) Unlock failed: %v", mp.name, err)
log.Errorf("Leader election(%v) Unlock failed: %v", mp.name, err)
}
case <-mp.stop:
// Stop was called. We stop the context first,
// so the running process is not thinking it
// is the primary any more, then we unlock.
lockCancel()
if err := l.Unlock(); err != nil {
log.Errorf("master election(%v) Unlock failed: %v", mp.name, err)
log.Errorf("Leader election(%v) Unlock failed: %v", mp.name, err)
}
close(mp.done)
}
Expand All @@ -115,14 +115,14 @@ func (mp *consulMasterParticipation) WaitForMastership() (context.Context, error
return lockCtx, nil
}

// Stop is part of the topo.MasterParticipation interface
func (mp *consulMasterParticipation) Stop() {
// Stop is part of the topo.LeaderParticipation interface
func (mp *consulLeaderParticipation) Stop() {
close(mp.stop)
<-mp.done
}

// GetCurrentMasterID is part of the topo.MasterParticipation interface
func (mp *consulMasterParticipation) GetCurrentMasterID(ctx context.Context) (string, error) {
// GetCurrentLeaderID is part of the topo.LeaderParticipation interface
func (mp *consulLeaderParticipation) GetCurrentLeaderID(ctx context.Context) (string, error) {
electionPath := path.Join(mp.s.root, electionsPath, mp.name)
pair, _, err := mp.s.kv.Get(electionPath, nil)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions go/vt/topo/consultopo/server_flaky_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func startConsul(t *testing.T, authToken string) (*exec.Cmd, string, string) {
},
}

// TODO(deepthi): this is the legacy ACL format. We run v1.4.0 by default in which this has been deprecated.
// We should start using the new format
// https://learn.hashicorp.com/tutorials/consul/access-control-replication-multiple-datacenters?in=consul/security-operations
if authToken != "" {
config["datacenter"] = "vitess"
config["acl_datacenter"] = "vitess"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gedgar @sonne5 is this something one of you can fix?

Expand Down
26 changes: 13 additions & 13 deletions go/vt/topo/etcd2topo/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
"vitess.io/vitess/go/vt/topo"
)

// NewMasterParticipation is part of the topo.Server interface
func (s *Server) NewMasterParticipation(name, id string) (topo.MasterParticipation, error) {
return &etcdMasterParticipation{
// NewLeaderParticipation is part of the topo.Server interface
func (s *Server) NewLeaderParticipation(name, id string) (topo.LeaderParticipation, error) {
return &etcdLeaderParticipation{
s: s,
name: name,
id: id,
Expand All @@ -38,16 +38,16 @@ func (s *Server) NewMasterParticipation(name, id string) (topo.MasterParticipati
}, nil
}

// etcdMasterParticipation implements topo.MasterParticipation.
// etcdLeaderParticipation implements topo.LeaderParticipation.
//
// We use a directory (in global election path, with the name) with
// ephemeral files in it, that contains the id. The oldest revision
// wins the election.
type etcdMasterParticipation struct {
type etcdLeaderParticipation struct {
// s is our parent etcd topo Server
s *Server

// name is the name of this MasterParticipation
// name is the name of this LeaderParticipation
name string

// id is the process's current id.
Expand All @@ -60,12 +60,12 @@ type etcdMasterParticipation struct {
done chan struct{}
}

// WaitForMastership is part of the topo.MasterParticipation interface.
func (mp *etcdMasterParticipation) WaitForMastership() (context.Context, error) {
// WaitForLeadership is part of the topo.LeaderParticipation interface.
func (mp *etcdLeaderParticipation) WaitForLeadership() (context.Context, error) {
// If Stop was already called, mp.done is closed, so we are interrupted.
select {
case <-mp.done:
return nil, topo.NewError(topo.Interrupted, "mastership")
return nil, topo.NewError(topo.Interrupted, "Leadership")
default:
}

Expand Down Expand Up @@ -99,14 +99,14 @@ func (mp *etcdMasterParticipation) WaitForMastership() (context.Context, error)
return lockCtx, nil
}

// Stop is part of the topo.MasterParticipation interface
func (mp *etcdMasterParticipation) Stop() {
// Stop is part of the topo.LeaderParticipation interface
func (mp *etcdLeaderParticipation) Stop() {
close(mp.stop)
<-mp.done
}

// GetCurrentMasterID is part of the topo.MasterParticipation interface
func (mp *etcdMasterParticipation) GetCurrentMasterID(ctx context.Context) (string, error) {
// GetCurrentLeaderID is part of the topo.LeaderParticipation interface
func (mp *etcdLeaderParticipation) GetCurrentLeaderID(ctx context.Context) (string, error) {
electionPath := path.Join(mp.s.root, electionsPath, mp.name)

// Get the keys in the directory, older first.
Expand Down
2 changes: 1 addition & 1 deletion go/vt/topo/etcd2topo/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

var (
leaseTTL = flag.Int("topo_etcd_lease_ttl", 30, "Lease TTL for locks and master election. The client will use KeepAlive to keep the lease going.")
leaseTTL = flag.Int("topo_etcd_lease_ttl", 30, "Lease TTL for locks and leader election. The client will use KeepAlive to keep the lease going.")
)

// newUniqueEphemeralKV creates a new file in the provided directory.
Expand Down
6 changes: 3 additions & 3 deletions go/vt/topo/helpers/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func createSetup(ctx context.Context, t *testing.T) (*topo.Server, *topo.Server)
Cell: "test_cell",
Uid: 123,
},
Hostname: "masterhost",
MysqlHostname: "masterhost",
Hostname: "primaryhost",
MysqlHostname: "primaryhost",
PortMap: map[string]int32{
"vt": 8101,
"gprc": 8102,
Expand All @@ -63,7 +63,7 @@ func createSetup(ctx context.Context, t *testing.T) (*topo.Server, *topo.Server)
}
tablet1.MysqlPort = 3306
if err := fromTS.CreateTablet(ctx, tablet1); err != nil {
t.Fatalf("cannot create master tablet: %v", err)
t.Fatalf("cannot create primary tablet: %v", err)
}
tablet2 := &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Expand Down
8 changes: 4 additions & 4 deletions go/vt/topo/helpers/tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// topo.Server to another.
//
// - primary: we read everything from it, and write to it. We also create
// MasterParticipation from it.
// LeaderParticipation from it.
// - secondary: we write to it as well, but we usually don't fail.
// - we lock primary/secondary if reverseLockOrder is False,
// or secondary/primary if reverseLockOrder is True.
Expand Down Expand Up @@ -224,7 +224,7 @@ func (ld *teeTopoLockDescriptor) Unlock(ctx context.Context) error {
return ferr
}

// NewMasterParticipation is part of the topo.Conn interface.
func (c *TeeConn) NewMasterParticipation(name, id string) (topo.MasterParticipation, error) {
return c.primary.NewMasterParticipation(name, id)
// NewLeaderParticipation is part of the topo.Conn interface.
func (c *TeeConn) NewLeaderParticipation(name, id string) (topo.LeaderParticipation, error) {
return c.primary.NewLeaderParticipation(name, id)
}
28 changes: 14 additions & 14 deletions go/vt/topo/k8stopo/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (

const electionsPath = "elections"

// NewMasterParticipation is part of the topo.Server interface
func (s *Server) NewMasterParticipation(name, id string) (topo.MasterParticipation, error) {
return &kubernetesMasterParticipation{
// NewLeaderParticipation is part of the topo.Server interface
func (s *Server) NewLeaderParticipation(name, id string) (topo.LeaderParticipation, error) {
return &kubernetesLeaderParticipation{
s: s,
name: name,
id: id,
Expand All @@ -38,16 +38,16 @@ func (s *Server) NewMasterParticipation(name, id string) (topo.MasterParticipati
}, nil
}

// kubernetesMasterParticipation implements topo.MasterParticipation.
// kubernetesLeaderParticipation implements topo.LeaderParticipation.
//
// We use a directory (in global election path, with the name) with
// ephemeral files in it, that contains the id. The oldest revision
// wins the election.
type kubernetesMasterParticipation struct {
type kubernetesLeaderParticipation struct {
// s is our parent kubernetes topo Server
s *Server

// name is the name of this MasterParticipation
// name is the name of this LeaderParticipation
name string

// id is the process's current id.
Expand All @@ -60,16 +60,16 @@ type kubernetesMasterParticipation struct {
done chan struct{}
}

func (mp *kubernetesMasterParticipation) getElectionPath() string {
func (mp *kubernetesLeaderParticipation) getElectionPath() string {
return path.Join(mp.s.root, electionsPath, mp.name)
}

// WaitForMastership is part of the topo.MasterParticipation interface.
func (mp *kubernetesMasterParticipation) WaitForMastership() (context.Context, error) {
// WaitForLeadership is part of the topo.LeaderParticipation interface.
func (mp *kubernetesLeaderParticipation) WaitForLeadership() (context.Context, error) {
// If Stop was already called, mp.done is closed, so we are interrupted.
select {
case <-mp.done:
return nil, topo.NewError(topo.Interrupted, "mastership")
return nil, topo.NewError(topo.Interrupted, "Leadership")
default:
}

Expand Down Expand Up @@ -103,14 +103,14 @@ func (mp *kubernetesMasterParticipation) WaitForMastership() (context.Context, e
return lockCtx, nil
}

// Stop is part of the topo.MasterParticipation interface
func (mp *kubernetesMasterParticipation) Stop() {
// Stop is part of the topo.LeaderParticipation interface
func (mp *kubernetesLeaderParticipation) Stop() {
close(mp.stop)
<-mp.done
}

// GetCurrentMasterID is part of the topo.MasterParticipation interface
func (mp *kubernetesMasterParticipation) GetCurrentMasterID(ctx context.Context) (string, error) {
// GetCurrentLeaderID is part of the topo.LeaderParticipation interface
func (mp *kubernetesLeaderParticipation) GetCurrentLeaderID(ctx context.Context) (string, error) {
id, _, err := mp.s.Get(ctx, mp.getElectionPath())
if err != nil {
// NoNode means nobody is the primary
Expand Down
1 change: 1 addition & 0 deletions go/vt/topo/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (ki *KeyspaceInfo) GetServedFrom(tabletType topodatapb.TabletType) *topodat
func (ki *KeyspaceInfo) CheckServedFromMigration(tabletType topodatapb.TabletType, cells []string, keyspace string, remove bool) error {
// primary is a special case with a few extra checks
if tabletType == topodatapb.TabletType_PRIMARY {
// TODO(deepthi): these master references will go away when we delete legacy resharding
if !remove {
return vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "cannot add master back to %v", ki.keyspace)
}
Expand Down
3 changes: 2 additions & 1 deletion go/vt/topo/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)

// This file tests the keyspace related object functionnalities.
// This file tests the keyspace related object functionalities.

func TestUpdateServedFromMap(t *testing.T) {
// TODO(deepthi): delete this test once legacy resharding code is deleted
ki := &KeyspaceInfo{
keyspace: "ks",
version: nil,
Expand Down
Loading