-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out activity and log snapshot states and mutexes
This improves consistency of when these snapshots run, because they no longer conflict with each other, or the full snapshot run. Note that for now, the state file does not contain the activity and log states. This could be implemented in the future, as long as its ensured that there is a write mutex when writing the state file, as this could happen concurrently now.
- Loading branch information
Showing
10 changed files
with
105 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package state | ||
|
||
import ( | ||
"encoding/gob" | ||
"os" | ||
|
||
"github.com/pganalyze/collector/config" | ||
"github.com/pganalyze/collector/util" | ||
) | ||
|
||
func WriteStateFile(servers []Server, globalCollectionOpts CollectionOpts, logger *util.Logger) { | ||
stateOnDisk := StateOnDisk{PrevStateByServer: make(map[config.ServerIdentifier]PersistedState), FormatVersion: StateOnDiskFormatVersion} | ||
|
||
for _, server := range servers { | ||
stateOnDisk.PrevStateByServer[server.Config.Identifier] = server.PrevState | ||
} | ||
|
||
file, err := os.Create(globalCollectionOpts.StateFilename) | ||
if err != nil { | ||
logger.PrintWarning("Could not write out state file to %s because of error: %s", globalCollectionOpts.StateFilename, err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
encoder := gob.NewEncoder(file) | ||
encoder.Encode(stateOnDisk) | ||
} | ||
|
||
// ReadStateFile - This reads in the prevState structs from the state file - only run this on initial bootup and SIGHUP! | ||
func ReadStateFile(servers []Server, globalCollectionOpts CollectionOpts, logger *util.Logger) { | ||
var stateOnDisk StateOnDisk | ||
|
||
file, err := os.Open(globalCollectionOpts.StateFilename) | ||
if err != nil { | ||
logger.PrintVerbose("Did not open state file: %s", err) | ||
return | ||
} | ||
decoder := gob.NewDecoder(file) | ||
err = decoder.Decode(&stateOnDisk) | ||
if err != nil { | ||
logger.PrintVerbose("Could not decode state file: %s", err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
if stateOnDisk.FormatVersion < StateOnDiskFormatVersion { | ||
logger.PrintVerbose("Ignoring state file since the on-disk format has changed") | ||
return | ||
} | ||
|
||
for idx, server := range servers { | ||
prevState, exist := stateOnDisk.PrevStateByServer[server.Config.Identifier] | ||
if exist { | ||
prefixedLogger := logger.WithPrefix(server.Config.SectionName) | ||
prefixedLogger.PrintVerbose("Successfully recovered state from on-disk file") | ||
servers[idx].PrevState = prevState | ||
} | ||
} | ||
} |