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

Implement backup mechanism for config #288

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open

Implement backup mechanism for config #288

wants to merge 5 commits into from

Conversation

Anachor
Copy link
Contributor

@Anachor Anachor commented Oct 11, 2022

  • When loading, first tried to load from ConfigPath. If failed, tried from ConfigBackupPath. If failed again, throw exception.
  • When saving config, saved both to ConfigPath and ConfigBackupPath
  • Added a call to SaveConfig after loading to sync config and backup

@Anachor
Copy link
Contributor Author

Anachor commented Oct 11, 2022

Questions:

  • Should ConfigBackupPath be read from options instead of appending .bak?
  • Should I catch more specific exceptions?

@@ -440,6 +466,7 @@ private void _UpdateConfigToV15()
private void _SaveCurrentConfig()
{
File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(_config, Formatting.Indented));
File.WriteAllText(ConfigBackupPath, JsonConvert.SerializeObject(_config, Formatting.Indented));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think backup should be saved in e separate function after updating is done. Because the case was config was corrupted during updating the config. So, in current implementation, backup could also be corrupted, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Steps during config update:

  1. Copy old config to config backup
  2. Write new data to config
  3. Delete backup
    I don't think we need separate function for this, but step order is important

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, I did it slightly different way. I'm always keeping a backup file. This way, both the Config file and the backup file cannot be corrupted at the same time. In case there is a problem updating the config file, the backup file will still be intact.

I think this solution will handle your case and also handle any case where the config file is somehow corrupted.

catch (Exception e) {
try {
Logger.LogWarning($"Could not load config from {ConfigPath}. Trying from backup ({ConfigBackupPath})");
var configLoader = new LocalConfigLoader(ConfigBackupPath);
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case you need to copy backup file to the config file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The config will be saved to the config file in the following call to _SaveCurrentConfig

Copy link
Contributor

Choose a reason for hiding this comment

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

It is not guaranteed really (for example, exceptions on the intermediate steps). It is better to copy this file explicitly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done in the latest commit.

@sgladkov
Copy link
Contributor

One more thing. It was not in original task conditions, but save config in constructor only if it was really changed (add modify flag and set it if you change any data)

@Anachor
Copy link
Contributor Author

Anachor commented Oct 12, 2022

One more thing. It was not in original task conditions, but save config in constructor only if it was really changed (add modify flag and set it if you change any data)

I have now called save config only if something is changed.

Copy link
Contributor

@sgladkov sgladkov left a comment

Choose a reason for hiding this comment

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

Tests are failing due to the fail to find config.json.bak: https://app.travis-ci.com/github/LATOKEN/lachain/builds/256617012 Fix it please, this class should not require bak file existence

@Anachor
Copy link
Contributor Author

Anachor commented Oct 13, 2022

It seems that the tests are failing because of the new test I added. But it should not happen if tests are completely independent. In my test, I try deleting config file and backup to see if everything works correctly. This deletion is causing problem in other tests.

@sgladkov
Copy link
Contributor

I think we don't need such test in unit test really. It is enough to test it manually. Unit tests share the same setup, it is dangerous to delete common config

@Anachor
Copy link
Contributor Author

Anachor commented Oct 14, 2022

Removed the offending lines in the test. All checks pass now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants