-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StandaloneLockContext and ClusterLockContext (#34115)
* Add StandaloneLockContext and ClusterLockContext * Add StandaloneLockContext and ClusterLockContext
- Loading branch information
Showing
10 changed files
with
156 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...src/main/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 | ||
* | ||
* http://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 org.apache.shardingsphere.mode.manager.cluster.lock; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.shardingsphere.infra.lock.LockContext; | ||
import org.apache.shardingsphere.mode.lock.LockPersistService; | ||
import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition; | ||
|
||
/** | ||
* Cluster global lock context. | ||
*/ | ||
@RequiredArgsConstructor | ||
public final class ClusterLockContext implements LockContext<GlobalLockDefinition> { | ||
|
||
private final LockPersistService<GlobalLockDefinition> globalLockPersistService; | ||
|
||
@Override | ||
public boolean tryLock(final GlobalLockDefinition lockDefinition, final long timeoutMillis) { | ||
return globalLockPersistService.tryLock(lockDefinition, timeoutMillis); | ||
} | ||
|
||
@Override | ||
public void unlock(final GlobalLockDefinition lockDefinition) { | ||
globalLockPersistService.unlock(lockDefinition); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...test/java/org/apache/shardingsphere/mode/manager/cluster/lock/ClusterLockContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 | ||
* | ||
* http://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 org.apache.shardingsphere.mode.manager.cluster.lock; | ||
|
||
import org.apache.shardingsphere.mode.lock.LockPersistService; | ||
import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class ClusterLockContextTest { | ||
|
||
private final GlobalLockDefinition lockDefinition = new GlobalLockDefinition("foo_lock"); | ||
|
||
@Mock | ||
private LockPersistService<GlobalLockDefinition> lockPersistService; | ||
|
||
private ClusterLockContext lockContext; | ||
|
||
@BeforeEach | ||
void init() { | ||
lockContext = new ClusterLockContext(lockPersistService); | ||
} | ||
|
||
@Test | ||
void assertTryLock() { | ||
when(lockPersistService.tryLock(lockDefinition, 3000L)).thenReturn(true); | ||
assertTrue(lockContext.tryLock(lockDefinition, 3000L)); | ||
} | ||
|
||
@Test | ||
void assertUnlock() { | ||
lockContext.unlock(lockDefinition); | ||
verify(lockPersistService).unlock(lockDefinition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...in/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF 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 | ||
* | ||
* http://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 org.apache.shardingsphere.mode.manager.standalone.lock; | ||
|
||
import org.apache.shardingsphere.infra.lock.LockContext; | ||
import org.apache.shardingsphere.infra.lock.LockDefinition; | ||
|
||
/** | ||
* Standalone global lock context. | ||
*/ | ||
public final class StandaloneLockContext implements LockContext<LockDefinition> { | ||
|
||
@Override | ||
public boolean tryLock(final LockDefinition lockDefinition, final long timeoutMillis) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void unlock(final LockDefinition lockDefinition) { | ||
} | ||
} |