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

chore: Adding logging mechanism to find out context of redis git locks #33895

Merged
merged 1 commit into from
May 31, 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 @@ -5,4 +5,6 @@
public class GitConstants extends GitConstantsCE {

public class GitMetricConstants extends GitMetricConstantsCE {}

public class GitCommandConstants extends GitCommandConstantsCE {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ public class GitMetricConstantsCE {
public static final String ACTION_COLLECTION_BODY = "ActionCollectionBody";
public static final String NEW_ACTION_BODY = "NewActionBody";
}

public class GitCommandConstantsCE {
public static final String METADATA = "metadata";
public static final String AUTO_COMMIT = "autoCommit";
public static final String PULL = "pull";
public static final String PUSH = "push";
public static final String STATUS = "status";
public static final String FETCH_REMOTE = "fetchRemote";
public static final String COMMIT = "commit";
public static final String CREATE_BRANCH = "createBranch";
public static final String CHECKOUT_BRANCH = "checkoutBranch";
public static final String SYNC_BRANCH = "syncBranch";
public static final String LIST_BRANCH = "listBranch";
public static final String MERGE_BRANCH = "mergeBranch";
public static final String DELETE = "delete";
public static final String DISCARD = "discard";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ public enum AppsmithError {
GIT_FILE_IN_USE(
500,
AppsmithErrorCode.GIT_FILE_IN_USE.getCode(),
"We were unable to place a lock on the file system to perform #commandName command. This error can occur when another operation is in progress. Please try again later.",
"We were unable to place a lock on the file system to perform {0} command. This error can occur when another command {1}, which is in progress. Please try again later.",
AppsmithErrorAction.DEFAULT,
"Git repo is locked",
ErrorType.GIT_ACTION_EXECUTION_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.dtos.ModifiedResources;
import com.appsmith.external.git.GitExecutor;
import com.appsmith.external.git.constants.GitConstants.GitCommandConstants;
import com.appsmith.server.configurations.ProjectProperties;
import com.appsmith.server.constants.ArtifactType;
import com.appsmith.server.constants.FieldName;
Expand All @@ -27,7 +28,6 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.util.retry.Retry;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -38,14 +38,13 @@
import java.util.Set;

import static com.appsmith.external.git.constants.GitConstants.PAGE_LIST;
import static com.appsmith.server.helpers.GitUtils.MAX_RETRIES;
import static com.appsmith.server.helpers.GitUtils.RETRY_DELAY;
import static java.lang.Boolean.TRUE;

@RequiredArgsConstructor
@Slf4j
public class AutoCommitEventHandlerCEImpl implements AutoCommitEventHandlerCE {
private final ApplicationEventPublisher applicationEventPublisher;
private final GitRedisUtils gitRedisUtils;
private final RedisUtils redisUtils;
private final DSLMigrationUtils dslMigrationUtils;
private final GitFileUtils fileUtils;
Expand Down Expand Up @@ -84,19 +83,6 @@ public void handle(AutoCommitEvent event) {
"Error during auto-commit for application: {}", event.getApplicationId(), error));
}

private Mono<Boolean> addFileLock(String defaultApplicationId) {
return redisUtils
.addFileLock(defaultApplicationId)
.retryWhen(Retry.fixedDelay(MAX_RETRIES, RETRY_DELAY)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
throw new AppsmithException(AppsmithError.GIT_FILE_IN_USE);
}));
}

private Mono<Boolean> releaseFileLock(String defaultApplicationId) {
return redisUtils.releaseFileLock(defaultApplicationId);
}

private <T> Mono<T> setProgress(T result, String applicationId, int progress) {
return redisUtils.setAutoCommitProgress(applicationId, progress).thenReturn(result);
}
Expand Down Expand Up @@ -133,7 +119,8 @@ private Mono<Path> saveApplicationJsonToFileSystem(
}

public Mono<Boolean> autoCommitDSLMigration(AutoCommitEvent autoCommitEvent) {
return addFileLock(autoCommitEvent.getApplicationId())
return gitRedisUtils
.addFileLock(autoCommitEvent.getApplicationId(), GitCommandConstants.AUTO_COMMIT)
.flatMap(fileLocked ->
redisUtils.startAutoCommit(autoCommitEvent.getApplicationId(), autoCommitEvent.getBranchName()))
.flatMap(autoCommitLocked -> dslMigrationUtils.getLatestDslVersion())
Expand Down Expand Up @@ -175,7 +162,7 @@ private Mono<Boolean> cleanUp(AutoCommitEvent autoCommitEvent, boolean isCommitM
return redisUtils
.finishAutoCommit(autoCommitEvent.getApplicationId())
.flatMap(r -> setProgress(r, autoCommitEvent.getApplicationId(), 100))
.flatMap(r -> releaseFileLock(autoCommitEvent.getApplicationId()))
.flatMap(r -> gitRedisUtils.releaseFileLock(autoCommitEvent.getApplicationId()))
.thenReturn(isCommitMade);
}

Expand Down Expand Up @@ -298,7 +285,8 @@ public Mono<Boolean> autoCommitServerMigration(AutoCommitEvent autoCommitEvent)
// push to remote
// release file lock

return addFileLock(defaultApplicationId)
return gitRedisUtils
.addFileLock(defaultApplicationId, GitCommandConstants.AUTO_COMMIT)
.flatMap(isFileLocked -> redisUtils.startAutoCommit(defaultApplicationId, branchName))
.flatMap(r -> setProgress(r, defaultApplicationId, 10))
.flatMap(autoCommitLocked -> resetUncommittedChanges(autoCommitEvent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class AutoCommitEventHandlerImpl extends AutoCommitEventHandlerCEImpl imp

public AutoCommitEventHandlerImpl(
ApplicationEventPublisher applicationEventPublisher,
GitRedisUtils gitRedisUtils,
sondermanish marked this conversation as resolved.
Show resolved Hide resolved
RedisUtils redisUtils,
DSLMigrationUtils dslMigrationUtils,
GitFileUtils fileUtils,
Expand All @@ -24,6 +25,7 @@ public AutoCommitEventHandlerImpl(
AnalyticsService analyticsService) {
super(
applicationEventPublisher,
gitRedisUtils,
redisUtils,
dslMigrationUtils,
fileUtils,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
package com.appsmith.server.git;

import com.appsmith.external.git.constants.GitSpan;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.helpers.RedisUtils;
import io.micrometer.observation.ObservationRegistry;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.observability.micrometer.Micrometer;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import static com.appsmith.server.helpers.GitUtils.MAX_RETRIES;
import static com.appsmith.server.helpers.GitUtils.RETRY_DELAY;

@Slf4j
@Component
@RequiredArgsConstructor
public class GitRedisUtils {

private final RedisUtils redisUtils;
private final ObservationRegistry observationRegistry;

public Mono<Boolean> addFileLock(String defaultApplicationId, Boolean isRetryAllowed) {
public Mono<Boolean> addFileLock(String defaultApplicationId, String commandName, Boolean isRetryAllowed) {
long numberOfRetries = Boolean.TRUE.equals(isRetryAllowed) ? MAX_RETRIES : 0L;

log.info(
"Git command {} is trying to acquire the lock for application id {}",
commandName,
defaultApplicationId);
return redisUtils
.addFileLock(defaultApplicationId)
.addFileLock(defaultApplicationId, commandName)
.retryWhen(Retry.fixedDelay(numberOfRetries, RETRY_DELAY)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> {
throw new AppsmithException(AppsmithError.GIT_FILE_IN_USE);
}));
if (retrySignal.failure() instanceof AppsmithException) {
throw (AppsmithException) retrySignal.failure();
}

throw new AppsmithException(AppsmithError.GIT_FILE_IN_USE, commandName);
}))
.name(GitSpan.ADD_FILE_LOCK)
.tap(Micrometer.observation(observationRegistry));
}

public Mono<Boolean> addFileLock(String defaultApplicationId) {
return addFileLock(defaultApplicationId, Boolean.TRUE);
public Mono<Boolean> addFileLock(String defaultApplicationId, String commandName) {
return addFileLock(defaultApplicationId, commandName, true);
}

public Mono<Boolean> releaseFileLock(String defaultApplicationId) {
return redisUtils.releaseFileLock(defaultApplicationId);
return redisUtils
.releaseFileLock(defaultApplicationId)
.name(GitSpan.RELEASE_FILE_LOCK)
.tap(Micrometer.observation(observationRegistry));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.appsmith.external.annotations.FeatureFlagged;
import com.appsmith.external.enums.FeatureFlagEnum;
import com.appsmith.external.git.constants.GitConstants.GitCommandConstants;
import com.appsmith.server.constants.ArtifactType;
import com.appsmith.server.domains.GitArtifactMetadata;
import com.appsmith.server.domains.Layout;
Expand Down Expand Up @@ -55,7 +56,7 @@ public Mono<Boolean> isServerAutoCommitRequired(String workspaceId, GitArtifactM
.defaultIfEmpty(FALSE)
.cache();

return Mono.defer(() -> gitRedisUtils.addFileLock(defaultApplicationId, FALSE))
return Mono.defer(() -> gitRedisUtils.addFileLock(defaultApplicationId, GitCommandConstants.METADATA, false))
.then(Mono.defer(() -> isServerMigrationRequiredMonoCached))
.then(Mono.defer(() -> gitRedisUtils.releaseFileLock(defaultApplicationId)))
.then(Mono.defer(() -> isServerMigrationRequiredMonoCached))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

import java.time.Duration;

import static com.appsmith.external.git.constants.ce.GitConstantsCE.GitCommandConstantsCE.AUTO_COMMIT;
import static org.springframework.util.StringUtils.hasText;

@Component
@RequiredArgsConstructor
public class RedisUtils {
Expand All @@ -23,8 +26,18 @@ public class RedisUtils {

private static final Duration AUTO_COMMIT_TIME_LIMIT = Duration.ofMinutes(3);

public Mono<Boolean> addFileLock(String key) {
return this.addFileLock(key, FILE_LOCK_TIME_LIMIT, new AppsmithException(AppsmithError.GIT_FILE_IN_USE));
public Mono<Boolean> addFileLock(String key, String gitCommand) {
String command = hasText(gitCommand) ? gitCommand : REDIS_FILE_LOCK_VALUE;
return redisOperations.hasKey(key).flatMap(isKeyPresent -> {
if (!Boolean.TRUE.equals(isKeyPresent)) {
return redisOperations.opsForValue().set(key, gitCommand, FILE_LOCK_TIME_LIMIT);
}
return redisOperations
.opsForValue()
.get(key)
.flatMap(commandName ->
Mono.error(new AppsmithException(AppsmithError.GIT_FILE_IN_USE, command, commandName)));
});
}

public Mono<Boolean> addFileLock(String key, Duration expirationPeriod, AppsmithException exception) {
Expand All @@ -48,7 +61,7 @@ public Mono<Boolean> startAutoCommit(String defaultApplicationId, String branchN
String key = String.format(AUTO_COMMIT_KEY_FORMAT, defaultApplicationId);
return redisOperations.hasKey(key).flatMap(isKeyPresent -> {
if (Boolean.TRUE.equals(isKeyPresent)) {
return Mono.error(new AppsmithException(AppsmithError.GIT_FILE_IN_USE));
return Mono.error(new AppsmithException(AppsmithError.GIT_FILE_IN_USE, AUTO_COMMIT, AUTO_COMMIT));
}
return redisOperations.opsForValue().set(key, branchName, AUTO_COMMIT_TIME_LIMIT);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.appsmith.server.configurations.EmailConfig;
import com.appsmith.server.domains.Application;
import com.appsmith.server.exports.internal.ExportService;
import com.appsmith.server.git.GitRedisUtils;
import com.appsmith.server.git.autocommit.helpers.GitAutoCommitHelper;
import com.appsmith.server.helpers.CommonGitFileUtils;
import com.appsmith.server.helpers.GitPrivateRepoHelper;
import com.appsmith.server.helpers.RedisUtils;
import com.appsmith.server.imports.internal.ImportService;
import com.appsmith.server.repositories.GitDeployKeysRepository;
import com.appsmith.server.services.ce_compatible.CommonGitServiceCECompatibleImpl;
Expand All @@ -27,7 +27,7 @@ public CommonGitServiceImpl(
GitDeployKeysRepository gitDeployKeysRepository,
GitPrivateRepoHelper gitPrivateRepoHelper,
CommonGitFileUtils commonGitFileUtils,
RedisUtils redisUtils,
GitRedisUtils gitRedisUtils,
SessionUserService sessionUserService,
UserDataService userDataService,
UserService userService,
Expand All @@ -44,7 +44,7 @@ public CommonGitServiceImpl(
gitDeployKeysRepository,
gitPrivateRepoHelper,
commonGitFileUtils,
redisUtils,
gitRedisUtils,
sessionUserService,
userDataService,
userService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import com.appsmith.server.configurations.EmailConfig;
import com.appsmith.server.datasources.base.DatasourceService;
import com.appsmith.server.exports.internal.ExportService;
import com.appsmith.server.git.GitRedisUtils;
import com.appsmith.server.git.autocommit.helpers.GitAutoCommitHelper;
import com.appsmith.server.helpers.GitFileUtils;
import com.appsmith.server.helpers.GitPrivateRepoHelper;
import com.appsmith.server.helpers.RedisUtils;
import com.appsmith.server.helpers.ResponseUtils;
import com.appsmith.server.imports.internal.ImportService;
import com.appsmith.server.newactions.base.NewActionService;
Expand Down Expand Up @@ -54,7 +54,7 @@ public GitServiceImpl(
ApplicationPermission applicationPermission,
WorkspacePermission workspacePermission,
WorkspaceService workspaceService,
RedisUtils redisUtils,
GitRedisUtils gitRedisUtils,
ObservationRegistry observationRegistry,
GitPrivateRepoHelper gitPrivateRepoHelper,
TransactionalOperator transactionalOperator,
Expand Down Expand Up @@ -82,7 +82,7 @@ public GitServiceImpl(
applicationPermission,
workspacePermission,
workspaceService,
redisUtils,
gitRedisUtils,
observationRegistry,
gitPrivateRepoHelper,
transactionalOperator,
Expand Down
Loading
Loading