-
Notifications
You must be signed in to change notification settings - Fork 131
Add REST API to fall back an encrypted repository to a file-based repository #1276
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright 2026 LY Corporation | ||
| * | ||
| * LY Corporation licenses this file to you under the Apache License, | ||
| * version 2.0 (the "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at: | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package com.linecorp.centraldogma.server.command; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.google.common.base.MoreObjects.ToStringHelper; | ||
|
|
||
| import com.linecorp.centraldogma.common.Author; | ||
|
|
||
| /** | ||
| * A {@link Command} which is used for falling back a repository from an encrypted repository | ||
| * to a file-based repository. | ||
| */ | ||
| public final class FallbackToFileRepositoryCommand extends ProjectCommand<Void> { | ||
|
|
||
| private final String repositoryName; | ||
|
|
||
| @JsonCreator | ||
| FallbackToFileRepositoryCommand(@JsonProperty("timestamp") @Nullable Long timestamp, | ||
| @JsonProperty("author") @Nullable Author author, | ||
| @JsonProperty("projectName") String projectName, | ||
| @JsonProperty("repositoryName") String repositoryName) { | ||
| super(CommandType.FALLBACK_TO_FILE_REPOSITORY, timestamp, author, projectName); | ||
| this.repositoryName = requireNonNull(repositoryName, "repositoryName"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the repository name. | ||
| */ | ||
| @JsonProperty | ||
| public String repositoryName() { | ||
| return repositoryName; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
|
|
||
| if (!(obj instanceof FallbackToFileRepositoryCommand)) { | ||
| return false; | ||
| } | ||
|
|
||
| final FallbackToFileRepositoryCommand that = (FallbackToFileRepositoryCommand) obj; | ||
| return super.equals(obj) && repositoryName.equals(that.repositoryName); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return repositoryName.hashCode() * 31 + super.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| ToStringHelper toStringHelper() { | ||
| return super.toStringHelper().add("repositoryName", repositoryName); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,6 +176,62 @@ public void migrateToEncryptedRepository(String repositoryName) { | |
| projectRepositoryName(repositoryName) + " is migrated to an encrypted repository. Try again.")); | ||
| } | ||
|
|
||
| @Override | ||
| public void fallbackToFileRepository(String repositoryName) { | ||
| logger.info("Starting to fallback the repository '{}' to a file-based repository.", | ||
| projectRepositoryName(repositoryName)); | ||
| final long startTime = System.nanoTime(); | ||
| final Repository encryptedRepository = get(repositoryName); | ||
| final File repoDir = encryptedRepository.repoDir(); | ||
|
|
||
| // Delete the placeholder file so that the original file-based git data is recognized again. | ||
| // migrateToEncryptedRepository() preserves the original git files in repoDir, | ||
| // so we can simply reopen the existing file-based repository. | ||
| try { | ||
| Files.delete(Paths.get(repoDir.getPath(), ENCRYPTED_REPO_PLACEHOLDER_FILE)); | ||
| } catch (IOException e) { | ||
| throw new StorageException("failed to delete the encrypted repository placeholder file at: " + | ||
| repoDir, e); | ||
| } | ||
minwoox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Reopen the existing file-based git repository from repoDir. | ||
| final GitRepository fileRepository; | ||
| try { | ||
| fileRepository = openFileRepository(parent, repoDir, repositoryWorker, cache); | ||
| } catch (Throwable t) { | ||
| // Restore the placeholder file so the manager stays in a consistent state. | ||
| try { | ||
| Files.createFile(Paths.get(repoDir.getPath(), ENCRYPTED_REPO_PLACEHOLDER_FILE)); | ||
| } catch (IOException ex) { | ||
| logger.warn("Failed to restore the encrypted repository placeholder file at: {}", | ||
| repoDir, ex); | ||
| } | ||
minwoox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new StorageException("failed to reopen the file-based repository after fallback. " + | ||
| "repositoryName: " + projectRepositoryName(repositoryName), t); | ||
| } | ||
|
|
||
| if (!replaceChild(repositoryName, encryptedRepository, fileRepository)) { | ||
| fileRepository.internalClose(); | ||
| try { | ||
| Files.createFile(Paths.get(repoDir.getPath(), ENCRYPTED_REPO_PLACEHOLDER_FILE)); | ||
| } catch (IOException ex) { | ||
| logger.warn("Failed to restore the encrypted repository placeholder file at: {}", | ||
| repoDir, ex); | ||
| } | ||
| throw new StorageException( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note) I wasn't able to find where these exceptions would be logged - no problem as long as they are logged somewhere
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's logged here for the instance And for other replicas: |
||
| "failed to replace the encrypted repository with the file-based repository. " + | ||
| "repositoryName: " + projectRepositoryName(repositoryName)); | ||
| } | ||
|
|
||
| logger.info("Fallback the repository '{}' to a file-based repository in {} seconds.", | ||
| projectRepositoryName(repositoryName), | ||
| TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime)); | ||
| ((GitRepository) encryptedRepository).close(() -> new CentralDogmaException( | ||
| projectRepositoryName(repositoryName) + | ||
| " is fallback to a file-based repository. Try again.")); | ||
| encryptionStorageManager().deleteRepositoryData(parent.name(), repositoryName); | ||
minwoox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| protected Repository openChild(File childDir) throws Exception { | ||
| requireNonNull(childDir, "childDir"); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.