Skip to content

Commit

Permalink
Rename GlobalLockName (#34129)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Dec 23, 2024
1 parent d681b20 commit e6c0589
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.parser.SQLParserEngine;
import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition;
import org.apache.shardingsphere.mode.lock.global.GlobalLockNames;
import org.apache.shardingsphere.mode.lock.global.GlobalLockName;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.parser.rule.SQLParserRule;

Expand Down Expand Up @@ -118,7 +118,7 @@ private void prepareAndCheckTargetWithLock(final MigrationJobItemContext jobItem
if (!jobItemManager.getProgress(jobId, jobItemContext.getShardingItem()).isPresent()) {
jobItemManager.persistProgress(jobItemContext);
}
LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.PREPARE, jobConfig.getJobId());
LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockName.PREPARE, jobConfig.getJobId());
long startTimeMillis = System.currentTimeMillis();
if (lockContext.tryLock(lockDefinition, 600 * 1000L)) {
log.info("Lock success, jobId={}, shardingItem={}, cost {} ms.", jobId, jobItemContext.getShardingItem(), System.currentTimeMillis() - startTimeMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.globalclock.rule.constant.GlobalClockOrder;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.mode.lock.global.GlobalLockNames;
import org.apache.shardingsphere.mode.lock.global.GlobalLockName;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.lock.LockDefinition;
import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
Expand All @@ -41,7 +41,7 @@
*/
public final class GlobalClockTransactionHook implements TransactionHook<GlobalClockRule> {

private final LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.GLOBAL_LOCK);
private final LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockName.GLOBAL_LOCK);

@Override
public void beforeBegin(final GlobalClockRule rule, final DatabaseType databaseType, final TransactionConnectionContext transactionContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public final class GlobalLockDefinition implements LockDefinition {

private final String lockKey;

public GlobalLockDefinition(final String lockName) {
lockKey = String.format(KEY_PATTERN, lockName);
}

public GlobalLockDefinition(final GlobalLockNames lockName, final Object... lockParams) {
public GlobalLockDefinition(final GlobalLockName lockName, final Object... lockParams) {
lockKey = String.format(KEY_PATTERN, String.format(lockName.getLockName(), lockParams));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import lombok.RequiredArgsConstructor;

/**
* Global lock names.
* Global lock name.
*/
@RequiredArgsConstructor
@Getter
public enum GlobalLockNames {
public enum GlobalLockName {

CLUSTER_LOCK("cluster_lock"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlShardingSphereRowDataSwapper;
import org.apache.shardingsphere.metadata.persist.data.AlteredShardingSphereDatabaseData;
import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition;
import org.apache.shardingsphere.mode.lock.global.GlobalLockNames;
import org.apache.shardingsphere.mode.lock.global.GlobalLockName;
import org.apache.shardingsphere.mode.manager.ContextManager;

import java.util.ArrayList;
Expand Down Expand Up @@ -84,7 +84,7 @@ public void refresh() {
}

private void collectAndRefresh() {
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.STATISTICS);
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockName.STATISTICS);
if (lockContext.tryLock(lockDefinition, 5000L)) {
try {
ShardingSphereStatistics statistics = contextManager.getMetaDataContexts().getStatistics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ class GlobalLockDefinitionTest {

@Test
void assertGetLockKey() {
assertThat(new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK).getLockKey(), is("/lock/exclusive/locks/cluster_lock"));
assertThat(new GlobalLockDefinition(GlobalLockName.CLUSTER_LOCK).getLockKey(), is("/lock/exclusive/locks/cluster_lock"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.apache.shardingsphere.mode.lock.LockPersistService;
import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition;
import org.apache.shardingsphere.mode.lock.global.GlobalLockNames;
import org.apache.shardingsphere.mode.lock.global.GlobalLockName;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -33,7 +33,7 @@
@ExtendWith(MockitoExtension.class)
class ClusterLockContextTest {

private final GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
private final GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockName.CLUSTER_LOCK);

@Mock
private LockPersistService<GlobalLockDefinition> lockPersistService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.shardingsphere.mode.manager.cluster.persist.service;

import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition;
import org.apache.shardingsphere.mode.lock.global.GlobalLockNames;
import org.apache.shardingsphere.mode.lock.global.GlobalLockName;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -39,13 +39,13 @@ class GlobalLockPersistServiceTest {
@Test
void assertTryLock() {
when(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/cluster_lock").tryLock(1000L)).thenReturn(true);
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockName.CLUSTER_LOCK);
assertTrue(new GlobalLockPersistService(repository).tryLock(lockDefinition, 1000L));
}

@Test
void assertUnlock() {
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockName.CLUSTER_LOCK);
new GlobalLockPersistService(repository).unlock(lockDefinition);
verify(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/cluster_lock")).unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.infra.algorithm.core.exception.MissingRequiredAlgorithmException;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
import org.apache.shardingsphere.mode.lock.global.GlobalLockNames;
import org.apache.shardingsphere.mode.lock.global.GlobalLockName;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
Expand All @@ -43,7 +43,7 @@ public void executeUpdate(final LockClusterStatement sqlStatement, final Context
checkState(contextManager);
checkAlgorithm(sqlStatement);
LockContext lockContext = contextManager.getComputeNodeInstanceContext().getLockContext();
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockName.CLUSTER_LOCK);
if (lockContext.tryLock(lockDefinition, 3000L)) {
try {
checkState(contextManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorClusterModeRequired;
import org.apache.shardingsphere.distsql.statement.ral.updatable.UnlockClusterStatement;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.mode.lock.global.GlobalLockNames;
import org.apache.shardingsphere.mode.lock.global.GlobalLockName;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.exception.NotLockedClusterException;
Expand All @@ -38,7 +38,7 @@ public final class UnlockClusterExecutor implements DistSQLUpdateExecutor<Unlock
public void executeUpdate(final UnlockClusterStatement sqlStatement, final ContextManager contextManager) {
checkState(contextManager);
LockContext lockContext = contextManager.getComputeNodeInstanceContext().getLockContext();
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockName.CLUSTER_LOCK);
if (lockContext.tryLock(lockDefinition, 3000L)) {
try {
checkState(contextManager);
Expand Down

0 comments on commit e6c0589

Please sign in to comment.