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

Refactor GlobalLockDefinition #34128

Merged
merged 1 commit into from
Dec 23, 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 @@ -119,7 +119,7 @@ private void prepareAndCheckTargetWithLock(final MigrationJobItemContext jobItem
if (!jobItemManager.getProgress(jobId, jobItemContext.getShardingItem()).isPresent()) {
jobItemManager.persistProgress(jobItemContext);
}
LockDefinition lockDefinition = new GlobalLockDefinition(String.format(GlobalLockNames.PREPARE.getLockName(), jobConfig.getJobId()));
LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.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 @@ -41,7 +41,7 @@
*/
public final class GlobalClockTransactionHook implements TransactionHook<GlobalClockRule> {

private final LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.GLOBAL_LOCK.getLockName());
private final LockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.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 @@ -33,4 +33,8 @@ public final class GlobalLockDefinition implements LockDefinition {
public GlobalLockDefinition(final String lockName) {
lockKey = String.format(KEY_PATTERN, lockName);
}

public GlobalLockDefinition(final GlobalLockNames 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 @@ -84,7 +84,7 @@ public void refresh() {
}

private void collectAndRefresh() {
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.STATISTICS.getLockName());
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.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("foo_lock").getLockKey(), is("/lock/exclusive/locks/foo_lock"));
assertThat(new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK).getLockKey(), is("/lock/exclusive/locks/cluster_lock"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +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.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -32,7 +33,7 @@
@ExtendWith(MockitoExtension.class)
class ClusterLockContextTest {

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

@Mock
private LockPersistService<GlobalLockDefinition> lockPersistService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +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.repository.cluster.ClusterPersistRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -37,15 +38,15 @@ class GlobalLockPersistServiceTest {

@Test
void assertTryLock() {
when(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/foo_lock").tryLock(1000L)).thenReturn(true);
GlobalLockDefinition lockDefinition = new GlobalLockDefinition("foo_lock");
when(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/cluster_lock").tryLock(1000L)).thenReturn(true);
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
assertTrue(new GlobalLockPersistService(repository).tryLock(lockDefinition, 1000L));
}

@Test
void assertUnlock() {
GlobalLockDefinition lockDefinition = new GlobalLockDefinition("foo_lock");
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
new GlobalLockPersistService(repository).unlock(lockDefinition);
verify(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/foo_lock")).unlock();
verify(repository.getDistributedLockHolder().getDistributedLock("/lock/exclusive/locks/cluster_lock")).unlock();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.getLockName());
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
if (lockContext.tryLock(lockDefinition, 3000L)) {
try {
checkState(contextManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.getLockName());
GlobalLockDefinition lockDefinition = new GlobalLockDefinition(GlobalLockNames.CLUSTER_LOCK);
if (lockContext.tryLock(lockDefinition, 3000L)) {
try {
checkState(contextManager);
Expand Down
Loading