Fix errors logged on standbys when we try to write versions to storage (#13042)

This commit is contained in:
Nick Cabatoff 2021-11-08 10:04:17 -05:00 committed by GitHub
parent 1b49591b53
commit 40640ef43f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 40 deletions

3
changelog/13042.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
core: Fix warnings logged on perf standbys re stored versions
```

View File

@ -411,6 +411,8 @@ func (a *ActivityLog) saveCurrentSegmentInternal(ctx context.Context, force bool
// be written to storage, since if we remove this code we will incur // be written to storage, since if we remove this code we will incur
// data loss for one segment's worth of TWEs. // data loss for one segment's worth of TWEs.
if len(a.currentSegment.tokenCount.CountByNamespaceID) > 0 || force { if len(a.currentSegment.tokenCount.CountByNamespaceID) > 0 || force {
// We can get away with simply using the oldest version stored because
// the storing of versions was introduced at the same time as this code.
oldestVersion, oldestUpgradeTime, err := a.core.FindOldestVersionTimestamp() oldestVersion, oldestUpgradeTime, err := a.core.FindOldestVersionTimestamp()
switch { switch {
case err != nil: case err != nil:
@ -1086,7 +1088,6 @@ func (c *Core) setupActivityLog(ctx context.Context, wg *sync.WaitGroup) error {
// stopActivityLog removes the ActivityLog from Core // stopActivityLog removes the ActivityLog from Core
// and frees any resources. // and frees any resources.
func (c *Core) stopActivityLog() { func (c *Core) stopActivityLog() {
// preSeal may run before startActivityLog got a chance to complete. // preSeal may run before startActivityLog got a chance to complete.
if c.activityLog != nil { if c.activityLog != nil {
// Shut down background worker // Shut down background worker

View File

@ -1047,17 +1047,17 @@ func NewCore(conf *CoreConfig) (*Core, error) {
// HandleVersionTimeStamps stores the current version at the current time to // HandleVersionTimeStamps stores the current version at the current time to
// storage, and then loads all versions and upgrade timestamps out from storage. // storage, and then loads all versions and upgrade timestamps out from storage.
func (c *Core) HandleVersionTimeStamps(ctx context.Context) error { func (c *Core) handleVersionTimeStamps(ctx context.Context) error {
currentTime := time.Now() currentTime := time.Now()
isUpdated, err := c.StoreVersionTimestamp(ctx, version.Version, currentTime) isUpdated, err := c.storeVersionTimestamp(ctx, version.Version, currentTime)
if err != nil { if err != nil {
return err return fmt.Errorf("error storing vault version: %w", err)
} }
if isUpdated { if isUpdated {
c.logger.Info("Recorded vault version", "vault version", version.Version, "upgrade time", currentTime) c.logger.Info("Recorded vault version", "vault version", version.Version, "upgrade time", currentTime)
} }
// Finally, load the versions into core fields // Finally, load the versions into core fields
err = c.HandleLoadVersionTimestamps(ctx) err = c.loadVersionTimestamps(ctx)
if err != nil { if err != nil {
return err return err
} }
@ -2000,6 +2000,9 @@ func (s standardUnsealStrategy) unseal(ctx context.Context, logger log.Logger, c
return err return err
} }
} }
if err := c.handleVersionTimeStamps(ctx); err != nil {
return err
}
if err := c.setupPluginCatalog(ctx); err != nil { if err := c.setupPluginCatalog(ctx); err != nil {
return err return err
} }
@ -2157,11 +2160,6 @@ func (c *Core) postUnseal(ctx context.Context, ctxCancelFunc context.CancelFunc,
c.logger.Warn("post-unseal post seal migration failed", "error", err) c.logger.Warn("post-unseal post seal migration failed", "error", err)
} }
} }
err := c.HandleVersionTimeStamps(c.activeContext)
if err != nil {
c.logger.Warn("post-unseal version timestamp setup failed", "error", err)
}
c.logger.Info("post-unseal setup complete") c.logger.Info("post-unseal setup complete")
return nil return nil
@ -2689,7 +2687,6 @@ func (c *Core) SetConfig(conf *server.Config) {
} }
func (c *Core) GetListenerCustomResponseHeaders(listenerAdd string) *ListenerCustomHeaders { func (c *Core) GetListenerCustomResponseHeaders(listenerAdd string) *ListenerCustomHeaders {
customHeaders := c.customListenerHeader.Load() customHeaders := c.customListenerHeader.Load()
if customHeaders == nil { if customHeaders == nil {
return nil return nil

View File

@ -11,9 +11,9 @@ import (
const vaultVersionPath string = "core/versions/" const vaultVersionPath string = "core/versions/"
// StoreVersionTimestamp will store the version and timestamp pair to storage only if no entry // storeVersionTimestamp will store the version and timestamp pair to storage only if no entry
// for that version already exists in storage. // for that version already exists in storage.
func (c *Core) StoreVersionTimestamp(ctx context.Context, version string, currentTime time.Time) (bool, error) { func (c *Core) storeVersionTimestamp(ctx context.Context, version string, currentTime time.Time) (bool, error) {
timeStamp, err := c.barrier.Get(ctx, vaultVersionPath+version) timeStamp, err := c.barrier.Get(ctx, vaultVersionPath+version)
if err != nil { if err != nil {
return false, err return false, err
@ -39,27 +39,6 @@ func (c *Core) StoreVersionTimestamp(ctx context.Context, version string, curren
return true, nil return true, nil
} }
// FindMostRecentVersionTimestamp loads the current vault version and associated
// upgrade time from storage.
func (c *Core) FindMostRecentVersionTimestamp() (string, time.Time, error) {
if c.VersionTimestamps == nil || len(c.VersionTimestamps) == 0 {
return "", time.Time{}, fmt.Errorf("Version timestamps are not initialized")
}
var latestUpgradeTime time.Time
var mostRecentVersion string
for version, upgradeTime := range c.VersionTimestamps {
if upgradeTime.After(latestUpgradeTime) {
mostRecentVersion = version
latestUpgradeTime = upgradeTime
}
}
// This if-case should never be hit
if mostRecentVersion == "" {
return "", latestUpgradeTime, fmt.Errorf("Empty vault version was written to storage at time: %+v", latestUpgradeTime)
}
return mostRecentVersion, latestUpgradeTime, nil
}
// FindOldestVersionTimestamp searches for the vault version with the oldest // FindOldestVersionTimestamp searches for the vault version with the oldest
// upgrade timestamp from storage. The earliest version this can be (barring // upgrade timestamp from storage. The earliest version this can be (barring
// downgrades) is 1.9.0. // downgrades) is 1.9.0.
@ -80,9 +59,9 @@ func (c *Core) FindOldestVersionTimestamp() (string, time.Time, error) {
return oldestVersion, oldestUpgradeTime, nil return oldestVersion, oldestUpgradeTime, nil
} }
// HandleLoadVersionTimestamps loads all the vault versions and associated // loadVersionTimestamps loads all the vault versions and associated
// upgrade timestamps from storage. // upgrade timestamps from storage.
func (c *Core) HandleLoadVersionTimestamps(ctx context.Context) (retErr error) { func (c *Core) loadVersionTimestamps(ctx context.Context) (retErr error) {
vaultVersions, err := c.barrier.List(ctx, vaultVersionPath) vaultVersions, err := c.barrier.List(ctx, vaultVersionPath)
if err != nil { if err != nil {
return fmt.Errorf("unable to retrieve vault versions from storage: %+w", err) return fmt.Errorf("unable to retrieve vault versions from storage: %+w", err)

View File

@ -13,7 +13,7 @@ import (
func TestStoreMultipleVaultVersions(t *testing.T) { func TestStoreMultipleVaultVersions(t *testing.T) {
c, _, _ := TestCoreUnsealed(t) c, _, _ := TestCoreUnsealed(t)
upgradeTimePlusEpsilon := time.Now() upgradeTimePlusEpsilon := time.Now()
wasStored, err := c.StoreVersionTimestamp(context.Background(), version.Version, upgradeTimePlusEpsilon.Add(30*time.Hour)) wasStored, err := c.storeVersionTimestamp(context.Background(), version.Version, upgradeTimePlusEpsilon.Add(30*time.Hour))
if err != nil || wasStored { if err != nil || wasStored {
t.Fatalf("vault version was re-stored: %v, err is: %s", wasStored, err.Error()) t.Fatalf("vault version was re-stored: %v, err is: %s", wasStored, err.Error())
} }
@ -32,9 +32,9 @@ func TestGetOldestVersion(t *testing.T) {
c, _, _ := TestCoreUnsealed(t) c, _, _ := TestCoreUnsealed(t)
upgradeTimePlusEpsilon := time.Now() upgradeTimePlusEpsilon := time.Now()
c.StoreVersionTimestamp(context.Background(), "1.9.1", upgradeTimePlusEpsilon.Add(-4*time.Hour)) c.storeVersionTimestamp(context.Background(), "1.9.1", upgradeTimePlusEpsilon.Add(-4*time.Hour))
c.StoreVersionTimestamp(context.Background(), "1.9.2", upgradeTimePlusEpsilon.Add(2*time.Hour)) c.storeVersionTimestamp(context.Background(), "1.9.2", upgradeTimePlusEpsilon.Add(2*time.Hour))
c.HandleLoadVersionTimestamps(c.activeContext) c.loadVersionTimestamps(c.activeContext)
if len(c.VersionTimestamps) != 3 { if len(c.VersionTimestamps) != 3 {
t.Fatalf("expected 3 entries in timestamps map after refresh, found: %d", len(c.VersionTimestamps)) t.Fatalf("expected 3 entries in timestamps map after refresh, found: %d", len(c.VersionTimestamps))
} }