Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
configuration: Add utility functions to backup/restore config.
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Borderé <[email protected]>
  • Loading branch information
Mathieu Borderé committed Jan 5, 2023
1 parent bbe8802 commit 01b7a57
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,43 @@ void configurationTrace(const struct raft *r, struct raft_configuration *c, cons
}
tracef("=== CONFIG END ===");
}

int configurationBackupCurrent(struct raft *r)
{
int rv;
struct raft_configuration current;

/* Copy the configuration to an intermediate configuration because the copy
* can fail and we don't want to be left without the previous configuration. */
configurationInit(&current);
rv = configurationCopy(&r->configuration, &current);
if (rv != 0) {
return rv;
}
configurationClose(&r->configuration_previous);
r->configuration_previous = current;
return 0;
}

int configurationRestorePrevious(struct raft *r)
{
int rv;
struct raft_configuration prev;

if (r->configuration_previous.n == 0) {
tracef("There is no previous configuration.");
return RAFT_INVALID;
}

/* Copy the configuration to an intermediate configuration because the copy
* can fail and we don't want to be left without a configuration. */
configurationInit(&prev);
rv = configurationCopy(&r->configuration_previous, &prev);
if (rv != 0) {
return rv;
}
configurationClose(&r->configuration);
r->configuration = prev;
return 0;
}
#undef tracef
7 changes: 7 additions & 0 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ int configurationDecode(const struct raft_buffer *buf,

/* Output the configuration to the raft tracer */
void configurationTrace(const struct raft *r, struct raft_configuration *c, const char *msg);

/* Replaces the previous configuration with a copy of the current configuration */
int configurationBackupCurrent(struct raft *r);

/* Replaces the current configuration with a copy of the previous configuration. */
int configurationRestorePrevious(struct raft *r);

#endif /* CONFIGURATION_H_ */

0 comments on commit 01b7a57

Please sign in to comment.