Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Bond <[email protected]>
  • Loading branch information
loshz committed May 9, 2023
1 parent 7ede1e5 commit 4d52023
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
33 changes: 18 additions & 15 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ const (
// defaultQueryTime is the amount of time we block waiting for a change
// if no time is specified. Previously we would wait the maxQueryTime.
defaultQueryTime = 300 * time.Second

// Name of the file to store a server's last seen timestamp.
serverLastSeenFile = "server_last_seen"
)

var (
Expand Down Expand Up @@ -633,6 +630,9 @@ func (a *Agent) Start(ctx context.Context) error {
return err
}

// Start writing last seen timestamps to file in order to age on next startup.
go a.persistServerMetadata()

incomingRPCLimiter := consul.ConfiguredIncomingRPCLimiter(
&lib.StopChannelContext{StopCh: a.shutdownCh},
serverLogger,
Expand Down Expand Up @@ -759,10 +759,6 @@ func (a *Agent) Start(ctx context.Context) error {
},
)

// Start writing last seen timestamps to file in order to age on next startup.
// TODO: maybe we should do this earlier above in the "if c.ServerMode" block?
go a.persistServerLastSeen()

// Start watching for critical services to deregister, based on their
// checks.
go a.reapServices()
Expand Down Expand Up @@ -4542,11 +4538,10 @@ func (a *Agent) proxyDataSources() proxycfg.DataSources {
return sources
}

// persistServerLastSeen writes a server's last seen Unix timestamp to a file
// in the configured data directory, and then periodically updates the timestamp
// every hour.
func (a *Agent) persistServerLastSeen() {
file := filepath.Join(a.config.DataDir, serverLastSeenFile)
// persistServerMetadata periodically writes a server's metadata to a file
// in the configured data directory.
func (a *Agent) persistServerMetadata() {
file := filepath.Join(a.config.DataDir, consul.ServerMetadataFile)

// Create a timer with no initial tick to allow the timestamp to be written immediately.
t := time.NewTimer(0)
Expand Down Expand Up @@ -4580,8 +4575,8 @@ func (a *Agent) persistServerLastSeen() {

// checkServerLastSeen is a safety check for preventing old servers from rejoining an existing cluster.
//
// It attempts to read a server's last seen file and check the Unix timestamp against a
// configurable max age. If the last seen file does not exist, we treat this as an initial startup
// It attempts to read a server's metadata file and check the last seen Unix timestamp against a
// configurable max age. If the metadata file does not exist, we treat this as an initial startup
// and return no error.
//
// Example: if the server recorded a last seen timestamp of now-7d, and we configure a max age
Expand All @@ -4592,10 +4587,18 @@ func (a *Agent) checkServerLastSeen() error {
// Read server metadata file.
md, err := consul.ReadServerMetadata(filename)
if err != nil {
// Return early if it doesn't as this indicates the server is starting for the first time.
if err == os.ErrNotExist {
return nil
}
return fmt.Errorf("error reading server metadata: %w", err)
}

return md.CheckLastSeen(a.config.ServerRejoinAgeMax)
if err := md.CheckLastSeen(a.config.ServerRejoinAgeMax); err != nil {
return fmt.Errorf("error: server will not rejoin: %w", err)
}

return nil
}

func listenerPortKey(svcID structs.ServiceID, checkID structs.CheckID) string {
Expand Down
4 changes: 0 additions & 4 deletions agent/consul/server_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ func OpenServerMetadata(filename string) (io.WriteCloser, error) {
func ReadServerMetadata(filename string) (*ServerMetadata, error) {
b, err := os.ReadFile(filename)
if err != nil {
// Return early if it doesn't as this indicates the server is starting for the first time.
if err == os.ErrNotExist {
return nil, nil
}
return nil, err
}

Expand Down

0 comments on commit 4d52023

Please sign in to comment.