use of javax.cache.Cache in project ignite by apache.
the class IgniteCacheQueryNodeRestartSelfTest method testRestarts.
/**
* JUnit.
*
* @throws Exception If failed.
*/
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testRestarts() throws Exception {
int duration = 60 * 1000;
int qryThreadNum = 10;
final int logFreq = 50;
final IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
grid(0).cluster().baselineAutoAdjustEnabled(false);
assert cache != null;
for (int i = 0; i < KEY_CNT; i++) cache.put(i, i);
assertEquals(KEY_CNT, cache.size());
final AtomicInteger qryCnt = new AtomicInteger();
final AtomicBoolean done = new AtomicBoolean();
IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {
@Override
public void applyx() throws IgniteCheckedException {
while (!done.get()) {
Collection<Cache.Entry<Integer, Integer>> res = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "true")).getAll();
Set<Integer> keys = new HashSet<>();
for (Cache.Entry<Integer, Integer> entry : res) keys.add(entry.getKey());
if (KEY_CNT > keys.size()) {
for (int i = 0; i < KEY_CNT; i++) {
if (!keys.contains(i))
assertEquals(Integer.valueOf(i), cache.get(i));
}
fail("res size: " + res.size());
}
assertEquals(KEY_CNT, keys.size());
int c = qryCnt.incrementAndGet();
if (c % logFreq == 0)
info("Executed queries: " + c);
}
}
}, qryThreadNum, "query-thread");
final AtomicInteger restartCnt = new AtomicInteger();
CollectingEventListener lsnr = new CollectingEventListener();
for (int i = 0; i < GRID_CNT; i++) grid(i).events().localListen(lsnr, EventType.EVT_CACHE_REBALANCE_STOPPED);
IgniteInternalFuture<?> fut2 = createRestartAction(done, restartCnt);
Thread.sleep(duration);
info("Stopping..");
done.set(true);
fut2.get();
info("Restarts stopped.");
fut1.get();
info("Queries stopped.");
info("Awaiting rebalance events [restartCnt=" + restartCnt.get() + ']');
boolean success = lsnr.awaitEvents(countRebalances(GRID_CNT, restartCnt.get()), 15000);
for (int i = 0; i < GRID_CNT; i++) grid(i).events().stopLocalListen(lsnr, EventType.EVT_CACHE_REBALANCE_STOPPED);
assert success;
}
use of javax.cache.Cache in project ignite by apache.
the class DynamicColumnsAbstractConcurrentSelfTest method testQueryConsistencyMultithreaded.
/**
* Make sure that contended operations on the same table from different nodes do not hang when we issue both
* ADD/DROP COLUMN and SELECT statements.
*
* @throws Exception If failed.
*/
@Test
public void testQueryConsistencyMultithreaded() throws Exception {
final int KEY_COUNT = 5000;
// Start complex topology.
ignitionStart(serverConfiguration(1));
ignitionStart(serverConfiguration(2));
ignitionStart(serverConfiguration(3, true));
Ignite cli = ignitionStart(clientConfiguration(4));
createSqlCache(cli);
run(cli, createSql);
put(cli, 0, KEY_COUNT);
final AtomicBoolean stopped = new AtomicBoolean();
final AtomicInteger dynColCnt = new AtomicInteger();
final GridConcurrentHashSet<Integer> fields = new GridConcurrentHashSet<>();
IgniteInternalFuture fut = multithreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
while (!stopped.get()) {
Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
IgniteInternalFuture fut;
int fieldNum = ThreadLocalRandom.current().nextInt(0, dynColCnt.get() + 1);
boolean removed = fields.remove(fieldNum);
if (removed)
fut = dropCols(node, QueryUtils.DFLT_SCHEMA, "newCol" + fieldNum);
else {
fieldNum = dynColCnt.getAndIncrement();
fut = addCols(node, QueryUtils.DFLT_SCHEMA, c("newCol" + fieldNum, Integer.class.getName()));
}
try {
fut.get();
if (!removed)
fields.add(fieldNum);
} catch (SchemaOperationException e) {
// No-op.
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
}
return null;
}
}, 1);
IgniteInternalFuture qryFut = multithreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
while (!stopped.get()) {
try {
Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
IgniteCache<BinaryObject, BinaryObject> cache = node.cache(CACHE_NAME).withKeepBinary();
String valTypeName = ((IgniteEx) node).context().query().types(CACHE_NAME).iterator().next().valueTypeName();
List<Cache.Entry<BinaryObject, BinaryObject>> res = cache.query(new SqlQuery<BinaryObject, BinaryObject>(valTypeName, "from " + TBL_NAME)).getAll();
assertEquals(KEY_COUNT, res.size());
} catch (Exception e) {
// Swallow retry exceptions.
if (X.cause(e, QueryRetryException.class) == null)
throw e;
}
}
return null;
}
}, 8);
Thread.sleep(TEST_DUR);
stopped.set(true);
// Make sure nothing hanged.
fut.get();
qryFut.get();
}
use of javax.cache.Cache in project ignite by apache.
the class DynamicColumnsAbstractConcurrentSelfTest method testConcurrentPutRemove.
/**
* PUT/REMOVE data from cache and add/drop column concurrently.
*
* @throws Exception If failed,
*/
@Test
public void testConcurrentPutRemove() throws Exception {
CountDownLatch finishLatch = new CountDownLatch(4);
// Start several nodes.
IgniteEx srv1 = ignitionStart(serverConfiguration(1), finishLatch);
ignitionStart(serverConfiguration(2), finishLatch);
ignitionStart(serverConfiguration(3), finishLatch);
ignitionStart(serverConfiguration(4), finishLatch);
awaitPartitionMapExchange();
createSqlCache(srv1);
run(srv1, createSql4Cols);
// Start data change operations from several threads.
final AtomicBoolean stopped = new AtomicBoolean();
IgniteInternalFuture updateFut = multithreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
while (!stopped.get()) {
Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
int key = ThreadLocalRandom.current().nextInt(0, LARGE_CACHE_SIZE);
int val = ThreadLocalRandom.current().nextInt();
IgniteCache<Object, BinaryObject> cache = node.cache(CACHE_NAME);
if (ThreadLocalRandom.current().nextBoolean())
cache.put(key(key), val(node, val));
else
cache.remove(key(key));
}
return null;
}
}, 4);
// Let some to arrive.
Thread.sleep(500L);
addCols(srv1, QueryUtils.DFLT_SCHEMA, c("v", Integer.class.getName())).get();
dropCols(srv1, QueryUtils.DFLT_SCHEMA, "CITY").get();
// Stop updates once index is ready.
stopped.set(true);
updateFut.get();
finishLatch.await();
// Make sure new column is there.
checkTableState(srv1, QueryUtils.DFLT_SCHEMA, TBL_NAME, c("AGE", Integer.class.getName()), c("v", Integer.class.getName()));
run(srv1, "update person set \"v\" = case when mod(id, 2) <> 0 then substring(name, 7, length(name) - 6) " + "else null end");
// Get expected values.
Set<Integer> expKeys = new HashSet<>();
IgniteCache<Object, BinaryObject> cache = srv1.cache(CACHE_NAME).withKeepBinary();
for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
Object key = key(i);
BinaryObject val = cache.get(key);
if (val != null) {
int id = (Integer) key;
assertEquals(i, id);
if (id % 2 != 0)
expKeys.add(i);
}
}
String valTypeName = (srv1).context().query().types(CACHE_NAME).iterator().next().valueTypeName();
// Validate query result.
for (Ignite node : Ignition.allGrids()) {
IgniteCache<Object, BinaryObject> nodeCache = node.cache(CACHE_NAME).withKeepBinary();
SqlQuery qry = new SqlQuery(valTypeName, "from " + TBL_NAME + " where mod(id, 2) <> 0");
List<Cache.Entry<Object, BinaryObject>> res = nodeCache.query(qry).getAll();
assertEquals("Cache size mismatch [exp=" + expKeys.size() + ", actual=" + res.size() + ']', expKeys.size(), res.size());
for (Cache.Entry<Object, BinaryObject> entry : res) {
int key = (Integer) entry.getKey();
int v = entry.getValue().field("v");
String name = entry.getValue().field("NAME");
assertTrue("Expected key is not in result set: " + key, expKeys.contains(key));
assertEquals(Integer.parseInt(name.substring(6)), v);
}
}
}
use of javax.cache.Cache in project ignite by apache.
the class CacheScanQueryFailoverTest method queryCachesWithFailedPredicates.
/**
* @param ignite Ignite instance.
* @param configs Cache configurations.
*/
private void queryCachesWithFailedPredicates(Ignite ignite, CacheConfiguration... configs) {
if (configs == null)
return;
for (CacheConfiguration cfg : configs) {
IgniteCache cache = ignite.getOrCreateCache(cfg);
populateCache(ignite, cache.getName());
// Check that exception propagates to client from filter failure.
GridTestUtils.assertThrowsAnyCause(log, () -> {
try (QueryCursor<Cache.Entry<Integer, BinaryObject>> cursor = cache.withKeepBinary().query(new ScanQuery<>(filter))) {
for (Cache.Entry<Integer, BinaryObject> entry : cursor) log.info("Entry " + entry.toString());
}
return null;
}, Error.class, "Poison pill");
// Check that exception propagates to client from transformer failure.
GridTestUtils.assertThrowsAnyCause(log, () -> {
try (QueryCursor<Cache.Entry<Integer, BinaryObject>> cursor = cache.withKeepBinary().query(new ScanQuery<>(), transformer)) {
for (Cache.Entry<Integer, BinaryObject> entry : cursor) log.info("Entry " + entry.toString());
}
return null;
}, Error.class, "Poison pill");
}
}
use of javax.cache.Cache in project ignite by apache.
the class IndexingSpiQuerySelfTest method testNonBinaryIndexingSpi.
/**
* @throws Exception If failed.
*/
@Test
public void testNonBinaryIndexingSpi() throws Exception {
System.setProperty(IgniteSystemProperties.IGNITE_UNWRAP_BINARY_FOR_INDEXING_SPI, "true");
try {
indexingSpi = new MyIndexingSpi();
Ignite ignite = startGrid(0);
CacheConfiguration<PersonKey, Person> ccfg = cacheConfiguration(DEFAULT_CACHE_NAME);
IgniteCache<PersonKey, Person> cache = ignite.createCache(ccfg);
for (int i = 0; i < 10; i++) {
PersonKey key = new PersonKey(i);
cache.put(key, new Person("John Doe " + i));
}
QueryCursor<Cache.Entry<PersonKey, Person>> cursor = cache.query(new SpiQuery<PersonKey, Person>().setArgs(new PersonKey(2), new PersonKey(5)));
for (Cache.Entry<PersonKey, Person> entry : cursor) System.out.println(entry);
cache.remove(new PersonKey(9));
} finally {
System.clearProperty(IgniteSystemProperties.IGNITE_UNWRAP_BINARY_FOR_INDEXING_SPI);
}
}
Aggregations