use of org.apache.ignite.internal.processors.cache.IgniteCacheProxy in project ignite by apache.
the class TxDeadlockDetectionMessageMarshallingTest method testMessageUnmarshallWithoutCacheContext.
/**
* @throws Exception If failed.
*/
@Test
public void testMessageUnmarshallWithoutCacheContext() throws Exception {
try {
Ignite ignite = startGrid(0);
CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(ccfg);
Ignite client = startClientGrid(1);
final GridCacheSharedContext<Object, Object> clientCtx = ((IgniteKernal) client).context().cache().context();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean res = new AtomicBoolean();
clientCtx.gridIO().addMessageListener(TOPIC, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
if (msg instanceof TxLocksResponse) {
try {
((TxLocksResponse) msg).finishUnmarshal(clientCtx, clientCtx.deploy().globalLoader());
res.set(true);
} catch (Exception e) {
log.error("Message unmarshal failed", e);
} finally {
latch.countDown();
}
}
}
});
GridCacheContext cctx = ((IgniteCacheProxy) cache).context();
KeyCacheObject key = cctx.toCacheKeyObject(1);
TxLocksResponse msg = new TxLocksResponse();
msg.addKey(cctx.txKey(key));
msg.prepareMarshal(cctx.shared());
((IgniteKernal) ignite).context().cache().context().gridIO().sendToCustomTopic(((IgniteKernal) client).localNode(), TOPIC, msg, GridIoPolicy.PUBLIC_POOL);
boolean await = latch.await(1, TimeUnit.SECONDS);
assertTrue(await && res.get());
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.processors.cache.IgniteCacheProxy in project ignite by apache.
the class CacheMvccBackupsAbstractTest method allVersions.
/**
* Retrieves all versions of all keys from cache.
*
* @param cache Cache.
* @return {@link Map} of keys to its versions.
* @throws IgniteCheckedException If failed.
*/
private Map<KeyCacheObject, List<CacheDataRow>> allVersions(IgniteCache cache) throws IgniteCheckedException {
IgniteCacheProxy cache0 = (IgniteCacheProxy) cache;
GridCacheContext cctx = cache0.context();
assert cctx.mvccEnabled();
Map<KeyCacheObject, List<CacheDataRow>> vers = new HashMap<>();
for (Object e : cache) {
IgniteBiTuple entry = (IgniteBiTuple) e;
KeyCacheObject key = cctx.toCacheKeyObject(entry.getKey());
GridCursor<CacheDataRow> cur = cctx.offheap().mvccAllVersionsCursor(cctx, key, null);
List<CacheDataRow> rows = new ArrayList<>();
while (cur.next()) {
CacheDataRow row = cur.get();
rows.add(row);
}
vers.put(key, rows);
}
return vers;
}
use of org.apache.ignite.internal.processors.cache.IgniteCacheProxy in project ignite by apache.
the class CacheMvccSqlUpdateCountersTest method testUpdateCountersMultithreaded.
/**
* @throws Exception If failed.
*/
@Test
public void testUpdateCountersMultithreaded() throws Exception {
final int writers = 4;
final int readers = 0;
int parts = 8;
int keys = 20;
final Map<Integer, AtomicLong> tracker = new ConcurrentHashMap<>();
for (int i = 0; i < keys; i++) tracker.put(i, new AtomicLong(1));
final IgniteInClosure<IgniteCache<Object, Object>> init = new IgniteInClosure<IgniteCache<Object, Object>>() {
@Override
public void apply(IgniteCache<Object, Object> cache) {
final IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO MvccTestAccount(_key, val, updateCnt) VALUES " + "(?, 0, 1)");
for (int i = 0; i < keys; i++) {
try (FieldsQueryCursor<List<?>> cur = cache.query(qry.setArgs(i))) {
assertEquals(1L, cur.iterator().next().get(0));
}
tx.commit();
}
}
}
};
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
Map<Integer, AtomicLong> acc = new HashMap<>();
int v = 0;
while (!stop.get()) {
int cnt = rnd.nextInt(keys / 3);
if (cnt == 0)
cnt = 2;
// Generate key set to be changed in tx.
while (acc.size() < cnt) acc.put(rnd.nextInt(cnt), new AtomicLong());
TestCache<Integer, Integer> cache = randomCache(caches, rnd);
boolean success = true;
try {
IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
Map<Integer, MvccTestAccount> allVals = readAllByMode(cache.cache, tracker.keySet(), SQL, ACCOUNT_CODEC);
boolean rmv = allVals.size() > keys * 2 / 3;
for (Map.Entry<Integer, AtomicLong> e : acc.entrySet()) {
int key = e.getKey();
AtomicLong accCntr = e.getValue();
boolean exists = allVals.containsKey(key);
int delta = 0;
boolean createdInTx = false;
if (rmv && rnd.nextBoolean()) {
if (exists)
delta = 1;
SqlFieldsQuery qry = new SqlFieldsQuery("DELETE FROM MvccTestAccount WHERE _key=" + key);
cache.cache.query(qry).getAll();
} else {
delta = 1;
if (!exists)
createdInTx = true;
SqlFieldsQuery qry = new SqlFieldsQuery("MERGE INTO MvccTestAccount " + "(_key, val, updateCnt) VALUES (" + key + ", " + rnd.nextInt(100) + ", 1)");
cache.cache.query(qry).getAll();
}
if (rnd.nextBoolean()) {
if (createdInTx)
// Do not count cases when key created and removed in the same tx.
delta = 0;
SqlFieldsQuery qry = new SqlFieldsQuery("DELETE FROM MvccTestAccount WHERE _key=" + key);
cache.cache.query(qry).getAll();
} else {
delta = 1;
SqlFieldsQuery qry = new SqlFieldsQuery("MERGE INTO MvccTestAccount " + "(_key, val, updateCnt) VALUES (" + key + ", " + rnd.nextInt(100) + ", 1)");
cache.cache.query(qry).getAll();
}
accCntr.addAndGet(delta);
}
tx.commit();
}
} catch (Exception e) {
handleTxException(e);
success = false;
int r = 0;
for (Map.Entry<Integer, AtomicLong> en : acc.entrySet()) {
if (((IgniteCacheProxy) cache.cache).context().affinity().partition(en.getKey()) == 0)
r += en.getValue().intValue();
}
} finally {
cache.readUnlock();
if (success) {
v++;
for (Map.Entry<Integer, AtomicLong> e : acc.entrySet()) {
int k = e.getKey();
long updCntr = e.getValue().get();
tracker.get(k).addAndGet(updCntr);
}
int r = 0;
for (Map.Entry<Integer, AtomicLong> en : acc.entrySet()) {
if (((IgniteCacheProxy) cache.cache).context().affinity().partition(en.getKey()) == 0)
r += en.getValue().intValue();
}
}
acc.clear();
}
}
info("Writer done, updates: " + v);
}
};
GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {
@Override
public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
// No-op.
}
};
readWriteTest(null, 4, 1, 2, parts, writers, readers, DFLT_TEST_TIME, new InitIndexing(Integer.class, MvccTestAccount.class), init, writer, reader);
Map<Integer, AtomicLong> updPerParts = new HashMap<>(parts);
Affinity aff = grid(1).cachex(DEFAULT_CACHE_NAME).affinity();
for (Map.Entry<Integer, AtomicLong> e : tracker.entrySet()) {
int k = e.getKey();
long updCntr = e.getValue().get();
int p = aff.partition(k);
AtomicLong cntr = updPerParts.get(p);
if (cntr == null) {
cntr = new AtomicLong();
updPerParts.putIfAbsent(p, cntr);
}
cntr.addAndGet(updCntr);
}
for (Map.Entry<Integer, AtomicLong> e : updPerParts.entrySet()) checkUpdateCounters(DEFAULT_CACHE_NAME, e.getKey(), e.getValue().get());
}
Aggregations