use of org.apache.ignite.internal.processors.cache.GridCacheEntryEx in project ignite by apache.
the class GridCacheNearOneNodeSelfTest method testOptimisticTxWriteThrough.
/**
* Test Optimistic repeatable read write-through.
*
* @throws Exception If failed.
*/
@SuppressWarnings({ "ConstantConditions" })
public void testOptimisticTxWriteThrough() throws Exception {
IgniteCache<Object, Object> near = jcache();
GridCacheAdapter<Integer, String> dht = dht();
try (Transaction tx = grid().transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) {
near.put(2, "2");
near.put(3, "3");
assert "2".equals(near.get(2));
assert "3".equals(near.get(3));
GridCacheEntryEx entry = dht.peekEx(2);
assert entry == null || entry.rawGet() == null : "Invalid entry: " + entry;
tx.commit();
}
assert "2".equals(near.get(2));
assert "3".equals(near.get(3));
assert "2".equals(dht.get(2));
assert "3".equals(dht.get(3));
assertEquals(2, near.size());
assertEquals(2, near.size());
assertEquals(2, dht.size());
assertEquals(2, dht.size());
}
use of org.apache.ignite.internal.processors.cache.GridCacheEntryEx in project ignite by apache.
the class GridCacheNearOnlyMultiNodeFullApiSelfTest method checkReaderTtl.
/**
* @param inTx If {@code true} starts explicit transaction.
* @throws Exception If failed.
*/
private void checkReaderTtl(boolean inTx) throws Exception {
int ttl = 1000;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttl));
final IgniteCache<String, Integer> c = jcache();
final String key = primaryKeysForCache(fullCache(), 1).get(0);
c.put(key, 1);
info("Finished first put.");
{
IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
assertEquals((Integer) 1, c.get(key));
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(0, (long) entryTtl.get1());
assertEquals(0, (long) entryTtl.get2());
}
long startTime = System.currentTimeMillis();
int fullIdx = nearIdx == 0 ? 1 : 0;
// Now commit transaction and check that ttl and expire time have been saved.
Transaction tx = inTx ? grid(fullIdx).transactions().txStart() : null;
try {
jcache(fullIdx).withExpiryPolicy(expiry).put(key, 1);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
long[] expireTimes = new long[gridCount()];
for (int i = 0; i < gridCount(); i++) {
info("Checking grid: " + grid(i).localNode().id());
IgnitePair<Long> entryTtl = null;
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
entryTtl = entryTtl(jcache(i), key);
else if (i == nearIdx)
entryTtl = nearEntryTtl(jcache(i), key);
if (entryTtl != null) {
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(ttl, (long) entryTtl.get1());
assertTrue(entryTtl.get2() > startTime);
expireTimes[i] = entryTtl.get2();
}
}
// One more update from the same cache entry to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? grid(fullIdx).transactions().txStart() : null;
try {
jcache(fullIdx).withExpiryPolicy(expiry).put(key, 2);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
for (int i = 0; i < gridCount(); i++) {
IgnitePair<Long> entryTtl = null;
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
entryTtl = entryTtl(jcache(i), key);
else if (i == nearIdx)
entryTtl = nearEntryTtl(jcache(i), key);
if (entryTtl != null) {
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(ttl, (long) entryTtl.get1());
assertTrue(entryTtl.get2() > startTime);
expireTimes[i] = entryTtl.get2();
}
}
// And one more update to ensure that ttl is not changed and expire time is not shifted forward.
U.sleep(100);
tx = inTx ? grid(fullIdx).transactions().txStart() : null;
try {
jcache(fullIdx).put(key, 4);
} finally {
if (tx != null)
tx.commit();
}
for (int i = 0; i < gridCount(); i++) {
IgnitePair<Long> entryTtl = null;
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
entryTtl = entryTtl(jcache(i), key);
else if (i == nearIdx)
entryTtl = nearEntryTtl(jcache(i), key);
if (entryTtl != null) {
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(ttl, (long) entryTtl.get1());
assertEquals(expireTimes[i], (long) entryTtl.get2());
}
}
// Avoid reloading from store.
storeStgy.removeFromStore(key);
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@SuppressWarnings("unchecked")
@Override
public boolean applyx() throws IgniteCheckedException {
try {
Integer val = c.get(key);
if (val != null) {
info("Value is in cache [key=" + key + ", val=" + val + ']');
return false;
}
if (!internalCache(c).context().deferredDelete()) {
GridCacheEntryEx e0 = internalCache(c).peekEx(key);
return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
} else
return true;
} catch (GridCacheEntryRemovedException ignored) {
// If e0.valueBytes() thrown this exception then entry has been removed.
return true;
}
}
}, Math.min(ttl * 10, getTestTimeout())));
// Ensure that old TTL and expire time are not longer "visible".
{
IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(0, (long) entryTtl.get1());
assertEquals(0, (long) entryTtl.get2());
}
// Ensure that next update will not pick old expire time.
tx = inTx ? transactions().txStart() : null;
try {
c.put(key, 10);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
U.sleep(2000);
{
IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(0, (long) entryTtl.get1());
assertEquals(0, (long) entryTtl.get2());
}
}
use of org.apache.ignite.internal.processors.cache.GridCacheEntryEx in project ignite by apache.
the class GridCachePartitionedMultiNodeCounterSelfTest method checkNearAndPrimaryMultiNode.
/**
* @param gridCnt Grid count.
* @throws Exception If failed.
*/
private void checkNearAndPrimaryMultiNode(int gridCnt) throws Exception {
Affinity<String> aff = affinity(grid(0).<String, Integer>cache(DEFAULT_CACHE_NAME));
Collection<ClusterNode> affNodes = aff.mapKeyToPrimaryAndBackups(CNTR_KEY);
assertEquals(1 + backups, affNodes.size());
Ignite pri = G.ignite(F.first(affNodes).id());
// Initialize.
pri.cache(DEFAULT_CACHE_NAME).put(CNTR_KEY, 0);
assertNull(near(pri).peekEx(CNTR_KEY));
GridCacheEntryEx dhtEntry = dht(pri).entryEx(CNTR_KEY);
assertNotNull(dhtEntry);
dhtEntry.unswap();
assertEquals(Integer.valueOf(0), dhtEntry.rawGet().value(dhtEntry.context().cacheObjectContext(), false));
startLatchMultiNode = new CountDownLatch(gridCnt);
globalCntrMultiNode = new AtomicInteger(0);
lockedMultiNode.set(false);
// Execute task on all grid nodes.
pri.compute().broadcast(new IncrementItemJob(pri.name()));
info("*** ");
for (int i = 0; i < gridCnt; i++) {
Ignite g = grid(i);
IgniteCache<String, Integer> cache = grid(i).cache(DEFAULT_CACHE_NAME);
int cntr = cache.localPeek(CNTR_KEY);
info("*** Cache counter [igniteInstanceName=" + g.name() + ", cntr=" + cntr + ']');
assertEquals(RETRIES * gridCnt, cntr);
}
info("*** ");
}
use of org.apache.ignite.internal.processors.cache.GridCacheEntryEx in project ignite by apache.
the class GridCacheColocatedDebugTest method checkPutsMultithreaded.
/**
* @param loc Local puts.
* @param remote Remote puts.
* @param maxIterCnt Number of iterations.
* @throws Exception If failed.
*/
public void checkPutsMultithreaded(boolean loc, boolean remote, final long maxIterCnt) throws Exception {
storeEnabled = false;
assert loc || remote;
startGridsMultiThreaded(3);
try {
final Ignite g0 = grid(0);
Ignite g1 = grid(1);
final Collection<Integer> keys = new ConcurrentLinkedQueue<>();
if (loc) {
Integer key = -1;
for (int i = 0; i < 20; i++) {
key = forPrimary(g0, key);
keys.add(key);
}
}
if (remote) {
Integer key = -1;
for (int i = 0; i < 20; i++) {
key = forPrimary(g1, key);
keys.add(key);
}
}
final AtomicLong iterCnt = new AtomicLong();
final int keysCnt = 10;
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
@Override
public void run() {
// Make thread-local copy to shuffle keys.
List<Integer> threadKeys = new ArrayList<>(keys);
long threadId = Thread.currentThread().getId();
long itNum;
while ((itNum = iterCnt.getAndIncrement()) < maxIterCnt) {
Collections.shuffle(threadKeys);
List<Integer> iterKeys = threadKeys.subList(0, keysCnt);
Collections.sort(iterKeys);
Map<Integer, String> vals = U.newLinkedHashMap(keysCnt);
for (Integer key : iterKeys) vals.put(key, String.valueOf(key) + threadId);
jcache(0).putAll(vals);
if (itNum > 0 && itNum % 5000 == 0)
info(">>> " + itNum + " iterations completed.");
}
}
}, THREAD_CNT);
fut.get();
Thread.sleep(1000);
// Check that all transactions are committed.
for (int i = 0; i < 3; i++) {
GridCacheAdapter<Object, Object> cache = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
for (Integer key : keys) {
GridCacheEntryEx entry = cache.peekEx(key);
if (entry != null) {
Collection<GridCacheMvccCandidate> locCands = entry.localCandidates();
Collection<GridCacheMvccCandidate> rmtCands = entry.remoteMvccSnapshot();
assert locCands == null || locCands.isEmpty() : "Local candidates is not empty [idx=" + i + ", entry=" + entry + ']';
assert rmtCands == null || rmtCands.isEmpty() : "Remote candidates is not empty [idx=" + i + ", entry=" + entry + ']';
}
}
}
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.processors.cache.GridCacheEntryEx in project ignite by apache.
the class GridCacheReplicatedPreloadSelfTest method testIntegrity.
/**
* @throws Exception If test failed.
*/
public void testIntegrity() throws Exception {
preloadMode = SYNC;
try {
Ignite g1 = startGrid(1);
GridCacheAdapter<Integer, String> cache1 = ((IgniteKernal) g1).internalCache(DEFAULT_CACHE_NAME);
// Cache rebalancing events should not be fired for this cache.
CacheConfiguration ccfg = cacheConfiguration(((IgniteKernal) g1).getInstanceName()).setName(DEFAULT_CACHE_NAME + "_evts_disabled").setEventsDisabled(true);
g1.getOrCreateCache(ccfg);
cache1.getAndPut(1, "val1");
cache1.getAndPut(2, "val2");
GridCacheEntryEx e1 = cache1.entryEx(1);
assertNotNull(e1);
e1.unswap();
Ignite g2 = startGrid(2);
Collection<Event> evts = null;
for (int i = 0; i < 3; i++) {
evts = g2.events().localQuery(F.<Event>alwaysTrue(), EVT_CACHE_REBALANCE_STARTED, EVT_CACHE_REBALANCE_STOPPED);
if (evts.size() != 2) {
info("Wrong events collection size (will retry in 1000 ms): " + evts.size());
Thread.sleep(1000);
} else
break;
}
assertNotNull(evts);
assertEquals("Wrong events received: " + evts, 2, evts.size());
Iterator<Event> iter = evts.iterator();
assertEquals(EVT_CACHE_REBALANCE_STARTED, iter.next().type());
assertEquals(EVT_CACHE_REBALANCE_STOPPED, iter.next().type());
IgniteCache<Integer, String> cache2 = g2.cache(DEFAULT_CACHE_NAME);
assertEquals("val1", cache2.localPeek(1));
assertEquals("val2", cache2.localPeek(2));
GridCacheAdapter<Integer, String> cacheAdapter2 = ((IgniteKernal) g2).internalCache(DEFAULT_CACHE_NAME);
GridCacheEntryEx e2 = cacheAdapter2.entryEx(1);
assertNotNull(e2);
assertNotSame(e2, e1);
e2.unswap();
assertNotNull(e2.version());
assertEquals(e1.version(), e2.version());
} finally {
stopAllGrids();
}
}
Aggregations