Merge pull request #3340 from influxdata/crepererum/set_server_id_twice

fix: allow setting the same server ID twice
pull/24376/head
kodiakhq[bot] 2021-12-09 08:21:08 +00:00 committed by GitHub
commit 95f907efd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -129,7 +129,16 @@ async fn assert_set_get_server_id(server_fixture: ServerFixture) {
let got = client.get_server_id().await.expect("get ID failed");
assert_eq!(got, Some(test_id));
// setting server ID a second time should fail
// setting server ID to same ID should be OK
client
.update_server_id(test_id)
.await
.expect("set ID again failed");
let got = client.get_server_id().await.expect("get ID failed");
assert_eq!(got, Some(test_id));
// setting server ID to a different ID should fail
let result = client
.update_server_id(NonZeroU32::try_from(13).unwrap())
.await;

View File

@ -470,6 +470,15 @@ impl Server {
let mut state = self.shared.state.write();
let startup = match &**state {
ServerState::Startup(startup) => startup.clone(),
state
if state
.server_id()
.map(|existing| existing == server_id)
.unwrap_or_default() =>
{
// already set to same ID
return Ok(());
}
_ => return Err(Error::IdAlreadySet),
};
@ -2459,4 +2468,20 @@ mod tests {
])
.unwrap();
}
#[tokio::test]
async fn set_server_id_twice() {
test_helpers::maybe_start_logging();
let server = make_server(make_application());
server.set_id(ServerId::try_from(1).unwrap()).unwrap();
server.wait_for_init().await.unwrap();
server.set_id(ServerId::try_from(1).unwrap()).unwrap();
assert_error!(
server.set_id(ServerId::try_from(2).unwrap()),
Error::IdAlreadySet
);
}
}