use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheScanPartitionQueryFallbackSelfTest method testScanFallbackOnRebalancingCursor1.
/**
* Scan should activate fallback mechanism when new nodes join topology and rebalancing happens in parallel with
* scan query.
*
* @throws Exception In case of error.
*/
@Test
public void testScanFallbackOnRebalancingCursor1() throws Exception {
cacheMode = CacheMode.PARTITIONED;
backups = 1;
commSpiFactory = new TestFallbackOnRebalancingCommunicationSpiFactory();
try {
Ignite ignite = startGrids(GRID_CNT);
fillCache(ignite);
final AtomicBoolean done = new AtomicBoolean(false);
IgniteInternalFuture fut1 = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
for (int i = 0; i < 5; i++) {
startGrid(GRID_CNT + i);
U.sleep(500);
}
done.set(true);
return null;
}
}, 1);
final AtomicInteger nodeIdx = new AtomicInteger();
IgniteInternalFuture fut2 = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int nodeId = nodeIdx.getAndIncrement();
IgniteCache<Integer, Integer> cache = grid(nodeId).cache(DEFAULT_CACHE_NAME);
int cntr = 0;
while (!done.get()) {
int part = ThreadLocalRandom.current().nextInt(ignite(nodeId).affinity(DEFAULT_CACHE_NAME).partitions());
if (cntr++ % 100 == 0)
info("Running query [node=" + nodeId + ", part=" + part + ']');
try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(new ScanQuery<Integer, Integer>(part).setPageSize(5))) {
doTestScanQueryCursor(cur, part);
}
}
return null;
}
}, GRID_CNT);
fut1.get();
fut2.get();
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheScanPartitionQueryFallbackSelfTest method scanFallbackOnRebalancing.
/**
* @param cur If {@code true} tests query cursor.
* @throws Exception In case of error.
*/
private void scanFallbackOnRebalancing(final boolean cur) throws Exception {
cacheMode = CacheMode.PARTITIONED;
backups = 2;
commSpiFactory = new TestFallbackOnRebalancingCommunicationSpiFactory();
syncRebalance = true;
try {
Ignite ignite = startGrids(GRID_CNT);
fillCache(ignite);
final AtomicBoolean done = new AtomicBoolean(false);
final AtomicInteger idx = new AtomicInteger(GRID_CNT);
IgniteInternalFuture fut1 = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int id = idx.getAndIncrement();
while (!done.get()) {
startGrid(id);
Thread.sleep(3000);
info("Will stop grid: " + getTestIgniteInstanceName(id));
stopGrid(id);
if (done.get())
return null;
Thread.sleep(3000);
}
return null;
}
}, 2);
final AtomicInteger nodeIdx = new AtomicInteger();
IgniteInternalFuture fut2 = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
int nodeId = nodeIdx.getAndIncrement();
IgniteCache<Integer, Integer> cache = grid(nodeId).cache(DEFAULT_CACHE_NAME);
int cntr = 0;
while (!done.get()) {
int part = ThreadLocalRandom.current().nextInt(ignite(nodeId).affinity(DEFAULT_CACHE_NAME).partitions());
if (cntr++ % 100 == 0)
info("Running query [node=" + nodeId + ", part=" + part + ']');
try (QueryCursor<Cache.Entry<Integer, Integer>> cur0 = cache.query(new ScanQuery<Integer, Integer>(part))) {
if (cur)
doTestScanQueryCursor(cur0, part);
else
doTestScanQuery(cur0, part);
}
}
return null;
}
}, GRID_CNT);
// Test for one minute
Thread.sleep(60 * 1000);
done.set(true);
fut2.get();
fut1.get();
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteCacheRandomOperationBenchmark method doSqlQuery.
/**
* @param cache Ignite cache.
* @throws Exception If failed.
*/
private void doSqlQuery(IgniteCache<Object, Object> cache) {
List<SqlCacheDescriptor> descriptors = cacheSqlDescriptors.get(cache.getName());
if (descriptors != null) {
Query sq = null;
if (queries.isEmpty()) {
if (!descriptors.isEmpty()) {
SqlCacheDescriptor randomDesc = descriptors.get(nextRandom(descriptors.size()));
int id = nextRandom(args.range());
sq = nextBoolean() ? randomDesc.getSqlQuery(id) : randomDesc.getSqlFieldsQuery(id);
}
} else {
TestQuery qry = queries.get(nextRandom(queries.size()));
String sql = randomizeSql(qry.sql);
sq = new SqlFieldsQuery(sql);
((SqlFieldsQuery) sq).setDistributedJoins(qry.distributedJoin);
}
if (sq != null)
try (QueryCursor cursor = cache.query(sq)) {
for (Object obj : cursor) {
// No-op.
}
}
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class IgniteClientReconnectQueriesTest method scanQueryReconnectInProgress.
/**
* @param setPart If {@code true} sets partition for scan query.
* @throws Exception If failed.
*/
private void scanQueryReconnectInProgress(boolean setPart) throws Exception {
Ignite cln = grid(serverCount());
assertTrue(cln.cluster().localNode().isClient());
final Ignite srv = clientRouter(cln);
final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);
clnCache.put(1, new Person(1, "name1", "surname1"));
clnCache.put(2, new Person(2, "name2", "surname2"));
clnCache.put(3, new Person(3, "name3", "surname3"));
final ScanQuery<Integer, Person> scanQry = new ScanQuery<>();
scanQry.setPageSize(1);
scanQry.setFilter(new IgniteBiPredicate<Integer, Person>() {
@Override
public boolean apply(Integer integer, Person person) {
return true;
}
});
if (setPart)
scanQry.setPartition(1);
blockMessage(GridCacheQueryResponse.class);
final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
QueryCursor<Cache.Entry<Integer, Person>> qryCursor = clnCache.query(scanQry);
qryCursor.getAll();
} catch (CacheException e) {
checkAndWait(e);
return true;
}
return false;
}
});
// Check that client waiting operation.
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
return fut.get(200);
}
}, IgniteFutureTimeoutCheckedException.class, null);
assertNotDone(fut);
unblockMessage();
reconnectClientNode(cln, srv, null);
assertTrue((Boolean) fut.get(2, SECONDS));
QueryCursor<Cache.Entry<Integer, Person>> qryCursor2 = clnCache.query(scanQry);
List<Cache.Entry<Integer, Person>> entries = qryCursor2.getAll();
assertEquals(setPart ? 1 : 3, entries.size());
for (Cache.Entry<Integer, Person> entry : entries) {
assertEquals(Integer.class, entry.getKey().getClass());
assertEquals(Person.class, entry.getValue().getClass());
}
}
use of org.apache.ignite.cache.query.QueryCursor in project ignite by apache.
the class CacheMvccBasicContinuousQueryTest method checkUpdateCountersGapIsProcessedSimple.
/**
* @throws Exception if failed.
*/
private void checkUpdateCountersGapIsProcessedSimple(CacheMode cacheMode) throws Exception {
testSpi = true;
final int srvCnt = 4;
final int backups = srvCnt - 1;
startGridsMultiThreaded(srvCnt);
client = true;
IgniteEx nearNode = startGrid(srvCnt);
IgniteCache<Object, Object> cache = nearNode.createCache(cacheConfiguration(cacheMode, FULL_SYNC, backups, srvCnt).setIndexedTypes(Integer.class, Integer.class));
IgniteEx primary = grid(0);
List<Integer> keys = primaryKeys(primary.cache(DEFAULT_CACHE_NAME), 3);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
List<CacheEntryEvent> arrivedEvts = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(2);
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent e : evts) {
arrivedEvts.add(e);
latch.countDown();
}
}
});
QueryCursor<Cache.Entry<Integer, Integer>> cur = nearNode.cache(DEFAULT_CACHE_NAME).query(qry);
// Initial value.
cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(keys.get(0))).getAll();
// prevent first transaction prepare on backups
TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(primary);
final AtomicInteger dhtPrepMsgLimiter = new AtomicInteger();
spi.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {
@Override
public boolean apply(ClusterNode node, Message msg) {
if (msg instanceof GridDhtTxPrepareRequest)
return dhtPrepMsgLimiter.getAndIncrement() < backups;
if (msg instanceof GridContinuousMessage)
return true;
return false;
}
});
// First tx. Expect it will be prepared only on the primary node and GridDhtTxPrepareRequests to remotes
// will be swallowed.
Transaction txA = nearNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(keys.get(1))).getAll();
txA.commitAsync();
// Wait until first tx changes it's status to PREPARING.
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
boolean preparing = nearNode.context().cache().context().tm().activeTransactions().stream().allMatch(tx -> tx.state() == PREPARING);
boolean allPrepsSwallowed = dhtPrepMsgLimiter.get() == backups;
return preparing && allPrepsSwallowed;
}
}, 3_000);
// Second tx.
GridTestUtils.runAsync(() -> {
try (Transaction txB = nearNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(keys.get(2)));
txB.commit();
}
}).get();
long primaryUpdCntr = getUpdateCounter(primary, keys.get(0));
// There were three updates: init, first and second.
assertEquals(3, primaryUpdCntr);
// drop primary
stopGrid(primary.name());
// Wait all txs are rolled back.
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
boolean allRolledBack = true;
for (int i = 1; i < srvCnt; i++) {
boolean rolledBack = grid(i).context().cache().context().tm().activeTransactions().stream().allMatch(tx -> tx.state() == ROLLED_BACK);
allRolledBack &= rolledBack;
}
return allRolledBack;
}
}, 3_000);
for (int i = 1; i < srvCnt; i++) {
IgniteCache backupCache = grid(i).cache(DEFAULT_CACHE_NAME);
int size = backupCache.query(new SqlFieldsQuery("select * from Integer")).getAll().size();
long backupCntr = getUpdateCounter(grid(i), keys.get(0));
assertEquals(2, size);
assertEquals(primaryUpdCntr, backupCntr);
}
assertTrue(latch.await(3, SECONDS));
assertEquals(2, arrivedEvts.size());
assertEquals(keys.get(0), arrivedEvts.get(0).getKey());
assertEquals(keys.get(2), arrivedEvts.get(1).getKey());
cur.close();
nearNode.close();
}
Aggregations