Try simple refactor of bootstrap

This commit is contained in:
Philip O'Toole
2022-01-07 23:07:29 -05:00
parent 0d8804b1ca
commit f5c03d4b60
3 changed files with 39 additions and 34 deletions

View File

@@ -243,11 +243,9 @@ func main() {
}
// Any prexisting node state?
var enableBootstrap bool
isNew := store.IsNewNode(dataPath)
if isNew {
log.Printf("no preexisting node state detected in %s, node may be bootstrapping", dataPath)
enableBootstrap = true // New node, so we may be bootstrapping
} else {
log.Printf("preexisting node state detected in %s", dataPath)
}
@@ -259,17 +257,8 @@ func main() {
log.Fatalf("unable to determine join addresses: %s", err.Error())
}
// Supplying join addresses means bootstrapping a new cluster won't
// be required.
if len(joins) > 0 {
enableBootstrap = false
log.Println("join addresses specified, node is not bootstrapping")
} else {
log.Println("no join addresses set")
}
// Now, open store.
if err := str.Open(enableBootstrap); err != nil {
if err := str.Open(); err != nil {
log.Fatalf("failed to open store: %s", err.Error())
}
@@ -344,7 +333,12 @@ func main() {
} else {
log.Println("successfully joined cluster at", j)
}
} else if isNew {
// No prexisting state, and no joins to do. Node needs bootstrap itself.
log.Println("bootstraping single new node")
if err := str.Bootstrap(store.NewServer(str.ID(), str.Addr(), true)); err != nil {
log.Fatalf("failed to bootstrap single new node: %s", err.Error())
}
}
// Wait until the store is in full consensus.

View File

@@ -7,6 +7,19 @@ type Server struct {
Suffrage string `json:"suffrage,omitempty"`
}
// NewServer returns an initialized Server.
func NewServer(id, addr string, voter bool) *Server {
v := "voter"
if !voter {
v = "Nonvoter"
}
return &Server{
ID: id,
Addr: addr,
Suffrage: v,
}
}
// Servers is a set of Servers.
type Servers []*Server

View File

@@ -229,11 +229,8 @@ func New(ln Listener, c *Config) *Store {
}
}
// Open opens the Store. If enableSingleNode is set, then this node becomes a
// standalone node. If not set, then the calling layer must know that this
// node has pre-existing state, or the calling layer will trigger a join
// operation after opening the Store.
func (s *Store) Open(enableSingleNode bool) error {
// Open opens the Store.
func (s *Store) Open() error {
s.openT = time.Now()
s.logger.Printf("opening store with node ID %s", s.raftID)
@@ -341,27 +338,28 @@ func (s *Store) Open(enableSingleNode bool) error {
if err != nil {
return fmt.Errorf("new raft: %s", err)
}
if enableSingleNode {
s.logger.Printf("executing single-node bootstrap")
configuration := raft.Configuration{
Servers: []raft.Server{
{
ID: config.LocalID,
Address: s.raftTn.LocalAddr(),
},
},
}
ra.BootstrapCluster(configuration)
} else {
s.logger.Printf("single-node bootstrap not requested")
}
s.raft = ra
return nil
}
// Bootstrap executes a cluster bootstrap on this node, using the given
// Servers as the configuration.
func (s *Store) Bootstrap(servers ...*Server) error {
raftServers := make([]raft.Server, len(servers))
for i := range servers {
raftServers[i] = raft.Server{
ID: raft.ServerID(servers[i].ID),
Address: raft.ServerAddress(servers[i].Addr),
}
}
s.raft.BootstrapCluster(raft.Configuration{
Servers: raftServers,
})
return nil
}
// Close closes the store. If wait is true, waits for a graceful shutdown.
func (s *Store) Close(wait bool) error {
f := s.raft.Shutdown()