forked from hazelcast/hazelcast
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces SPI for services that need to execute some code before locking a key. Fixes hazelcast#13272
- Loading branch information
Showing
7 changed files
with
232 additions
and
36 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
29 changes: 29 additions & 0 deletions
29
hazelcast/src/main/java/com/hazelcast/spi/LockInterceptorService.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,29 @@ | ||
/* | ||
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. | ||
* | ||
* Licensed 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 com.hazelcast.spi; | ||
|
||
/** | ||
* Interface to be implemented by services that need to intercept lock operation | ||
* for distributed objects it manages. | ||
* | ||
* @param <T> type of key | ||
*/ | ||
public interface LockInterceptorService<T> { | ||
|
||
void onBeforeLock(String distributedObjectName, T key); | ||
|
||
} |
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
159 changes: 159 additions & 0 deletions
159
hazelcast/src/test/java/com/hazelcast/spi/LockInterceptorServiceTest.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,159 @@ | ||
/* | ||
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved. | ||
* | ||
* Licensed 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 com.hazelcast.spi; | ||
|
||
import com.hazelcast.concurrent.lock.LockProxySupport; | ||
import com.hazelcast.concurrent.lock.LockService; | ||
import com.hazelcast.concurrent.lock.LockStoreInfo; | ||
import com.hazelcast.config.Config; | ||
import com.hazelcast.config.ServiceConfig; | ||
import com.hazelcast.core.HazelcastInstance; | ||
import com.hazelcast.nio.serialization.Data; | ||
import com.hazelcast.spi.serialization.SerializationService; | ||
import com.hazelcast.test.HazelcastSerialClassRunner; | ||
import com.hazelcast.test.HazelcastTestSupport; | ||
import com.hazelcast.test.annotation.ParallelTest; | ||
import com.hazelcast.test.annotation.QuickTest; | ||
import com.hazelcast.util.ConstructorFunction; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.Properties; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static com.hazelcast.util.ConcurrencyUtil.getOrPutIfAbsent; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(HazelcastSerialClassRunner.class) | ||
@Category({QuickTest.class, ParallelTest.class}) | ||
public class LockInterceptorServiceTest extends HazelcastTestSupport { | ||
|
||
private static final ConcurrentMap<String, AtomicInteger> LOCK_COUNTER = new ConcurrentHashMap<>(); | ||
|
||
@Before | ||
public void setup() { | ||
LOCK_COUNTER.clear(); | ||
} | ||
|
||
@Test | ||
public void testLockInterceptorServiceIsConsulted() { | ||
LockInterceptingService implementation = new LockInterceptingService(false); | ||
|
||
testLockingInterceptor(implementation); | ||
|
||
assertLockCount(1); | ||
} | ||
|
||
@Test | ||
public void testObjectIsNotLocked_whenLockInterceptorThrowsException() { | ||
LockInterceptingService implementation = new LockInterceptingService(true); | ||
|
||
testLockingInterceptor(implementation); | ||
|
||
assertLockCount(0); | ||
} | ||
|
||
private void testLockingInterceptor(LockInterceptingService implementation) { | ||
Config config = new Config(); | ||
config.getServicesConfig().addServiceConfig(new ServiceConfig() | ||
.setEnabled(true) | ||
.setName(LockInterceptingService.SERVICE_NAME) | ||
.setImplementation(implementation)); | ||
|
||
HazelcastInstance member = createHazelcastInstance(config); | ||
NodeEngine nodeEngine = getNodeEngineImpl(member); | ||
implementation.serializationService = getSerializationService(member); | ||
|
||
LockProxySupport lockProxySupport = new LockProxySupport( | ||
new DistributedObjectNamespace(LockInterceptingService.SERVICE_NAME, "test-object"), 10000); | ||
|
||
for (int i = 0; i < 100; i++) { | ||
try { | ||
Data key = getSerializationService(member).toData("key" + i); | ||
lockProxySupport.lock(nodeEngine, key); | ||
} catch (RuntimeException e) { | ||
ignore(e); | ||
} | ||
} | ||
} | ||
|
||
private void assertLockCount(int expectedCount) { | ||
for (int i = 0; i < 100; i++) { | ||
assertEquals(expectedCount, LOCK_COUNTER.get("key" + i).get()); | ||
} | ||
} | ||
|
||
public static class LockInterceptingService implements LockInterceptorService<Data>, ManagedService { | ||
|
||
public static final String SERVICE_NAME = "test-lock-intercepting-service"; | ||
|
||
private final boolean throwException; | ||
private volatile SerializationService serializationService; | ||
|
||
public LockInterceptingService(boolean throwException) { | ||
this.throwException = throwException; | ||
} | ||
|
||
@Override | ||
public void onBeforeLock(String distributedObjectName, Data key) { | ||
String stringKey = serializationService.toObject(key); | ||
AtomicInteger counter = getOrPutIfAbsent(LOCK_COUNTER, stringKey, arg -> new AtomicInteger()); | ||
if (throwException) { | ||
throw new RuntimeException("failed"); | ||
} | ||
counter.getAndIncrement(); | ||
} | ||
|
||
@Override | ||
public void init(NodeEngine nodeEngine, Properties properties) { | ||
final LockService lockService = nodeEngine.getSharedService(LockService.SERVICE_NAME); | ||
if (lockService != null) { | ||
lockService.registerLockStoreConstructor(SERVICE_NAME, new LockStoreInfoConstructor()); | ||
} | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
} | ||
|
||
@Override | ||
public void shutdown(boolean terminate) { | ||
} | ||
} | ||
|
||
public static class LockStoreInfoConstructor implements ConstructorFunction<ObjectNamespace, LockStoreInfo> { | ||
@Override | ||
public LockStoreInfo createNew(ObjectNamespace arg) { | ||
return new LockStoreInfo() { | ||
@Override | ||
public int getBackupCount() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getAsyncBackupCount() { | ||
return 0; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
} |