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

Clean the useless versions after metadata version switched #33792

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,19 @@ public String getActiveVersionNodePath() {
/**
* Get versions node path.
*
* @param version version
* @return path of versions node
*/
public String getVersionsNodePath() {
return String.join("/", key, VERSIONS, currentActiveVersion);
public String getVersionsNodePath(final String version) {
return String.join("/", key, VERSIONS, version);
}

/**
* Get versions path.
*
* @return path of versions
*/
public String getVersionsPath() {
return String.join("/", key, VERSIONS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ void assertGetActiveVersionNodePath() {

@Test
void assertGetVersionsNodePath() {
assertThat(new MetaDataVersion("foo", "0", "1").getVersionsNodePath(), is("foo/versions/0"));
assertThat(new MetaDataVersion("foo", "0", "1").getVersionsNodePath("0"), is("foo/versions/0"));
}

@Test
void assertGetVersionsPath() {
assertThat(new MetaDataVersion("foo", "0", "1").getVersionsPath(), is("foo/versions"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public void switchActiveVersion(final Collection<MetaDataVersion> metaDataVersio
continue;
}
repository.persist(each.getActiveVersionNodePath(), each.getNextActiveVersion());
repository.delete(each.getVersionsNodePath());
getVersions(each.getVersionsPath()).stream()
.filter(version -> !version.equals(each.getNextActiveVersion()))
.forEach(version -> repository.delete(each.getVersionsNodePath(version)));
}
}

Expand All @@ -59,7 +61,7 @@ public String getVersionPathByActiveVersion(final String path, final String acti
@Override
public List<String> getVersions(final String path) {
List<String> result = repository.getChildrenKeys(path);
if (result.size() > 1) {
if (result.size() > 2) {
log.warn("There are multiple versions of :{}, please check the configuration.", path);
result.sort((v1, v2) -> Integer.compare(Integer.parseInt(v2), Integer.parseInt(v1)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void setUp() {

@Test
void assertSwitchActiveVersion() {
when(repository.getChildrenKeys("foo_db/versions")).thenReturn(Arrays.asList("1", "0"));
persistService.switchActiveVersion(Arrays.asList(new MetaDataVersion("foo_db", "0", "1"), new MetaDataVersion("bar_db", "2", "2")));
verify(repository).persist("foo_db/active_version", "1");
verify(repository).delete("foo_db/versions/0");
Expand Down