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

Remove sync-from-repo dependency on audit table: new processed_commits table #3569

Merged
merged 2 commits into from
Jul 18, 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 @@ -117,7 +117,7 @@ default InputStream getContent(String site, String path) throws ContentNotFoundE
* @param toPath target path
* @return Commit ID if successful, null otherwise
*/
default Map<String, String> moveContent(String site, String fromPath, String toPath) throws ServiceLayerException {
default String moveContent(String site, String fromPath, String toPath) throws ServiceLayerException {
return moveContent(site, fromPath, toPath, null);
}

Expand All @@ -131,7 +131,7 @@ default Map<String, String> moveContent(String site, String fromPath, String toP
* @return Commit ID if successful, empty string otherwise
*/
// TODO: SJ: Should refactor to be from path to path without the newName param
Map<String, String> moveContent(String site, String fromPath, String toPath, String newName) throws ServiceLayerException;
String moveContent(String site, String fromPath, String toPath, String newName) throws ServiceLayerException;

/**
* copy content from PathA to pathB
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
* Copyright (C) 2007-2024 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
Expand All @@ -17,10 +17,23 @@

public interface GeneralLockService {

String MASTER_LOCK = "MASTER LOCK";

void lock(String objectId);

/**
* Convenience method to lock multiple keys at once.
* This method will lock all the keys one by one
* @param lockKeys keys to lock
*/
void lock(String... lockKeys);

/**
* Convenience method to unlock multiple keys at once.
*
* @param lockKeys keys to unlock
*/
void unlock(String... lockKeys);

void unlock(String objectId);

boolean tryLock(String objectId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2007-2024 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.craftercms.studio.api.v2.dal;

import org.apache.ibatis.annotations.Param;

/**
* Provides operations to processed_commits table
*/
public interface ProcessedCommitsDAO {
String SITE_ID = "siteId";
String COMMIT_ID = "commitId";

/**
* Check if the given commitId has been processed
* <p>
* Note that the commitId should be more recent than the last processed commit for the site. Otherwise it might
* already have been purged from the table.
*
* @param siteId the site id
* @param commitId the commit id
* @return true if the commit has been processed, false otherwise
*/
boolean isProcessed(@Param(SITE_ID) long siteId, @Param(COMMIT_ID) String commitId);

/**
* Inserts a new processed_commits record
*
* @param siteId the site id
* @param commitId the commit id
*/
void insertCommit(@Param(SITE_ID) long siteId, @Param(COMMIT_ID) String commitId);

/**
* Deletes all commits records inserted before the given commitId
*
* @param siteId the site id
* @param commitId the commit id
*/
void deleteBefore(@Param(SITE_ID) long siteId, @Param(COMMIT_ID) String commitId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,22 @@ public static Path getStudioTemporaryFilesRoot() {
return Paths.get(tempDir, STUDIO_TEMPORARY_ROOT_DIR);
}

public static String getSandboxRepoLockKey(final String site) {
return SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
/**
* Get the key for sandbox repo operations lock
*
* @param siteId the site id
* @return the lock key
*/
public static String getSandboxRepoLockKey(final String siteId) {
return SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
}

/**
* Get the key for sync-from-repo task lock
*
* @param siteId the site id
* @return the lock key
*/
public static String getSyncFromRepoLockKey(final String siteId) {
return SITE_SYNC_FROM_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ private String deleteParentFolder(Git git, Path parentFolder, boolean wasPage)
}

@Override
public Map<String, String> moveContent(String site, String fromPath, String toPath, String newName) {
Map<String, String> toRet = new TreeMap<>();
public String moveContent(String site, String fromPath, String toPath, String newName) {
String commitId = null;
String gitLockKey = helper.getSandboxRepoLockKey(site, true);
generalLockService.lock(gitLockKey);
try {
Expand Down Expand Up @@ -411,19 +411,18 @@ public Map<String, String> moveContent(String site, String fromPath, String toPa
if (result) {
StatusCommand statusCommand = git.status().addPath(gitToPath);
Status gitStatus = retryingRepositoryOperationFacade.call(statusCommand);
Set<String> changeSet = gitStatus.getAdded();
List<String> changeSet = new ArrayList<>(gitStatus.getAdded().size() * 2);
PersonIdent user = helper.getCurrentUserIdent();
String commitMsg = helper.getCommitMessage(REPO_MOVE_CONTENT_COMMIT_MESSAGE)
.replaceAll(PATTERN_FROM_PATH, fromPath)
.replaceAll(PATTERN_TO_PATH,
toPath + (StringUtils.isNotEmpty(newName) ? newName : EMPTY));

// TODO: AV - This can be easily done in a single commit
for (String pathToCommit : changeSet) {
for (String pathToCommit : gitStatus.getAdded()) {
String pathRemoved = pathToCommit.replace(gitToPath, gitFromPath);
String commitId = helper.commitFiles(repo, site, commitMsg, user, pathToCommit, pathRemoved);
toRet.put(pathToCommit, commitId);
changeSet.add(pathToCommit);
changeSet.add(pathRemoved);
}
return helper.commitFiles(repo, site, commitMsg, user, changeSet.toArray(new String[0]));
} else {
logger.error("Failed to move item in site '{}' from path '{}' to path '{}' with name '{}'",
site, fromPath, toPath, newName);
Expand All @@ -435,7 +434,7 @@ public Map<String, String> moveContent(String site, String fromPath, String toPa
} finally {
generalLockService.unlock(gitLockKey);
}
return toRet;
return commitId;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2007-2023 Crafter Software Corporation. All Rights Reserved.
* Copyright (C) 2007-2024 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
Expand All @@ -21,6 +21,8 @@
import org.slf4j.LoggerFactory;

import jakarta.validation.Valid;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -61,6 +63,20 @@ public void lock(@ValidateStringParam String objectId) {
}
}

@Override
public void lock(final String... lockKeys) {
logger.debug("Thread '{}' will attempt to lock multiple objects '{}'", Thread.currentThread().getName(), lockKeys);
Arrays.stream(lockKeys).sorted().forEach(this::lock);
sumerjabri marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void unlock(final String... lockKeys) {
logger.debug("Thread '{}' will attempt to unlock multiple objects '{}'", Thread.currentThread().getName(), lockKeys);
for (String lockKey : lockKeys) {
unlock(lockKey);
}
}

@Override
@Valid
public boolean tryLock(@ValidateStringParam String objectId) {
Expand Down
Loading