Skip to content

Commit

Permalink
Don't stop the broker if unable to restore persisted state. (#6063)
Browse files Browse the repository at this point in the history
When broker is forcefully terminated, it is possible that the broker state file can be corrupted. Right now the corrupted state file prevents broker from restarting. This PR changes the behavior: now we log the error and ignore the corrupted state file. 

Even though the loss of up-to-date state happens in any circumstance with forced shutdown, the downside of this approach is that loss of state can happen unnoticeable. But it seems like it is preferable for a broker to continue to operate, rather that a user to go and cleanup state file(s).

This functionality is not covered by unit tests. The manual testing has been performed.

This fixes #6004

## Azure IoT Edge PR checklist:
  • Loading branch information
vadim-kovalyov authored Feb 7, 2022
1 parent fcd4d00 commit ca22a6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions mqtt/mqttd/src/app/edgehub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ impl Bootstrap for EdgeHubBootstrap {

fs::create_dir_all(state_dir.clone())?;
let mut persistor = FilePersistor::new(state_dir, VersionedFileFormat::default());
let state = persistor.load().await?;
info!("state loaded.");
let state = match persistor.load().await {
Ok(state) => {
info!("state loaded.");
state
}
Err(e) => {
error!("failed to load broker state, most likely the broker was forcefully shut down and state file is corrupted: {}", e);
None
}
};

let device_id = env::var(DEVICE_ID_ENV).context(DEVICE_ID_ENV)?;
let iothub_id = env::var(IOTHUB_HOSTNAME_ENV).context(IOTHUB_HOSTNAME_ENV)?;
Expand Down
12 changes: 10 additions & 2 deletions mqtt/mqttd/src/app/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ impl Bootstrap for GenericBootstrap {

fs::create_dir_all(state_dir.clone())?;
let mut persistor = FilePersistor::new(state_dir, VersionedFileFormat::default());
let state = persistor.load().await?;
info!("state loaded.");
let state = match persistor.load().await {
Ok(state) => {
info!("state loaded.");
state
}
Err(e) => {
error!("failed to load broker state, most likely the broker was forcefully shut down and state file is corrupted: {}", e);
None
}
};

let broker = BrokerBuilder::default()
.with_authorizer(AllowAll)
Expand Down

0 comments on commit ca22a6b

Please sign in to comment.