use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class TransactionsWithWriteBehind_whenNoCoalescingQueueIsFullTest method name.
@Test
public void name() {
String mapName = "map";
Config config = getConfig(mapName, 50);
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
HazelcastInstance node1 = factory.newHazelcastInstance(config);
HazelcastInstance node2 = factory.newHazelcastInstance(config);
for (int ii = 0; ii < 100; ii++) {
TransactionContext context = newTransactionContext(node1, TWO_PHASE);
context.beginTransaction();
TransactionalMap map = context.getMap("map");
for (int i = 0; i < 51; i++) {
map.put("item-" + i, "value");
}
try {
context.commitTransaction();
} catch (TransactionException e) {
context.rollbackTransaction();
}
}
HazelcastInstance node3 = factory.newHazelcastInstance(config);
assertTrueEventually(() -> {
Collection<HazelcastInstance> instances = factory.getAllHazelcastInstances();
for (HazelcastInstance node : instances) {
assertWriteBehindQueuesEmpty(mapName, Collections.singletonList(node));
assertEquals(0, getNodeWideUsedCapacity(node));
assertEquals(0, getTotalNumOfTxnReservedCapacity(mapName, node));
}
}, 30);
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate_releasesBackupLock.
@Test
public void testGetForUpdate_releasesBackupLock() {
Config config = getConfig();
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = factory.newHazelcastInstance(config);
HazelcastInstance instance2 = factory.newHazelcastInstance(config);
final String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
instance1.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> map = context.getMap(randomString());
map.getForUpdate(keyOwnedByInstance2);
return null;
}
});
NodeEngine nodeEngine = getNodeEngineImpl(instance1);
Data keyData = nodeEngine.toData(keyOwnedByInstance2);
LockSupportService lockService = nodeEngine.getService(LockSupportService.SERVICE_NAME);
for (LockResource lockResource : lockService.getAllLocks()) {
if (keyData.equals(lockResource.getKey())) {
assertEquals(0, lockResource.getLockCount());
}
}
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate_LoadsKeyFromMapLoader_whenKeyExistsInDb.
@Test
public void testGetForUpdate_LoadsKeyFromMapLoader_whenKeyExistsInDb() {
final String mapName = randomMapName();
final String valueFromDB = randomString();
final MapStoreAdapter mock = mock(MapStoreAdapter.class);
when(mock.load(anyObject())).thenReturn(valueFromDB);
Config config = new Config();
MapStoreConfig storeConfig = new MapStoreConfig();
storeConfig.setEnabled(true).setImplementation(mock);
config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
HazelcastInstance instance = createHazelcastInstance(config);
instance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> map = context.getMap(mapName);
Object value = map.getForUpdate(1);
assertEquals(valueFromDB, value);
verify(mock, times(1)).load(anyObject());
return null;
}
});
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testReplace_whenSame.
@Test
public void testReplace_whenSame() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap map1 = h1.getMap("default");
final IMap map2 = h2.getMap("default");
map2.put("1", "1");
map2.put("2", "2");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
assertTrue(txMap.replace("1", "1", "11"));
assertFalse(txMap.replace("5", "5", "55"));
assertFalse(txMap.replace("2", "1", "22"));
assertEquals("1", map1.get("1"));
assertEquals("11", txMap.get("1"));
assertEquals("2", map1.get("2"));
assertEquals("2", txMap.get("2"));
return true;
}
});
assertTrue(b);
assertEquals("11", map1.get("1"));
assertEquals("11", map2.get("1"));
assertEquals("2", map1.get("2"));
assertEquals("2", map2.get("2"));
}
use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.
the class MapTransactionTest method testReplace_whenNullKey.
// ========================= replace =====================
@Test(expected = NullPointerException.class)
public void testReplace_whenNullKey() throws TransactionException {
final HazelcastInstance hz = createHazelcastInstance();
hz.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> txMap = context.getMap("default");
txMap.replace(null, "oldvalue", "newvalue");
return true;
}
});
}
Aggregations