use of javax.cache.Cache in project ignite by apache.
the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSameQuery.
/**
* JUnit.
*
* @throws Exception If failed.
*/
@SuppressWarnings({ "TooBroadScope" })
@Test
public void testMultiThreadedSameQuery() throws Exception {
int threadCnt = 50;
final int keyCnt = 10;
final int logMod = 5000;
final Ignite g = grid(0);
// Put test values into cache.
final IgniteCache<Integer, Integer> c = cache(Integer.class, Integer.class);
for (int i = 0; i < keyCnt; i++) {
c.put(i, i);
c.localEvict(Arrays.asList(i));
}
final AtomicInteger cnt = new AtomicInteger();
final AtomicBoolean done = new AtomicBoolean();
IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {
@Override
public void applyx() throws IgniteCheckedException {
int iter = 0;
while (!done.get() && !Thread.currentThread().isInterrupted()) {
iter++;
Collection<Cache.Entry<Integer, Integer>> entries = c.query(new SqlQuery(Integer.class, "_val >= 0")).getAll();
assert entries != null;
assertEquals("Query results [entries=" + entries + ", aff=" + affinityNodes(entries, g) + ", iteration=" + iter + ']', keyCnt, entries.size());
if (cnt.incrementAndGet() % logMod == 0) {
GridCacheQueryManager<Object, Object> qryMgr = ((IgniteKernal) g).internalCache(c.getName()).context().queries();
assert qryMgr != null;
qryMgr.printMemoryStats();
}
}
}
}, threadCnt);
Thread.sleep(DURATION);
info("Finishing test...");
done.set(true);
fut.get();
}
use of javax.cache.Cache in project ignite by apache.
the class IntMaxValueEntriesTest method test.
/**
* {@inheritDoc}
*/
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
final IgniteCache<Integer, Object> cache = cache();
final IgniteDataStreamer<Integer, Object> stmr = ignite().dataStreamer(cache.getName());
final List<Thread> threads = new ArrayList<>(THREADS);
final LongAdder addedCnt = new LongAdder();
int delta = (int) ((KEYS_HI + Math.abs(KEYS_LO)) / THREADS);
System.out.println("Delta: " + delta);
for (int i = 0; i < THREADS; i++) {
final int lo = i == 0 ? KEYS_LO : delta * i + 1;
final int hi = i == THREADS - 1 ? (int) KEYS_HI : (int) ((long) delta * (i + 1));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
byte val = (byte) rnd.nextInt();
println("Start from " + lo + " to " + hi);
for (int j = lo, k = 0; j < hi; j++, k++) {
stmr.addData(j, val++);
addedCnt.increment();
if (k % REPORT_DELTA == 0)
println(addedCnt.sum() + " entries");
}
println("Thread finished. " + addedCnt.sum() + " entries.");
}
});
threads.add(t);
t.start();
}
for (Thread thread : threads) thread.join();
println("All threads finished. " + addedCnt.sum() + " entries.");
println("Streamer flush");
stmr.flush();
println("Streamer flushed");
println("Calculating cache size");
println("Cache size: " + cache.size());
println("Calculating long cache size");
println("Cache size long: " + cache.sizeLong());
Thread.sleep(10000);
println("Iterating started");
long cnt = 0;
for (Cache.Entry<Integer, Object> ignored : cache) {
cnt++;
if (cnt > 0 && cnt % REPORT_DELTA == 0)
println("Iterated via " + cnt + " entries");
}
println("Iterated via " + cnt + " entries");
cache.destroy();
return true;
}
use of javax.cache.Cache in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testInitialQueryAndUpdates.
/**
* @throws Exception If failed.
*/
@Test
public void testInitialQueryAndUpdates() throws Exception {
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
qry.setInitialQuery(new ScanQuery<>(new P2<Integer, Integer>() {
@Override
public boolean apply(Integer k, Integer v) {
return k >= 5;
}
}));
final Map<Integer, Integer> map = new ConcurrentHashMap<>();
final CountDownLatch latch = new CountDownLatch(2);
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
map.put(e.getKey(), e.getValue());
latch.countDown();
}
}
});
for (int i = 0; i < 10; i++) cachePut(cache, i, i);
try (QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry)) {
List<Cache.Entry<Integer, Integer>> res = cur.getAll();
Collections.sort(res, new Comparator<Cache.Entry<Integer, Integer>>() {
@Override
public int compare(Cache.Entry<Integer, Integer> e1, Cache.Entry<Integer, Integer> e2) {
return e1.getKey().compareTo(e2.getKey());
}
});
assertEquals(5, res.size());
int exp = 5;
for (Cache.Entry<Integer, Integer> e : res) {
assertEquals(exp, e.getKey().intValue());
assertEquals(exp, e.getValue().intValue());
exp++;
}
cachePut(cache, 10, 10);
cachePut(cache, 11, 11);
assert latch.await(LATCH_TIMEOUT, MILLISECONDS) : latch.getCount();
assertEquals(2, map.size());
for (int i = 11; i < 12; i++) assertEquals(i, (int) map.get(i));
}
}
use of javax.cache.Cache in project ignite by apache.
the class TcpDiscoveryMultiThreadedTest method testCustomEventNodeRestart.
/**
* @throws Exception If failed.
*/
@Ignore("https://issues.apache.org/jira/browse/IGNITE-10249")
@Test
public void testCustomEventNodeRestart() throws Exception {
clientFlagGlobal = false;
Ignite ignite = startGrid(0);
ignite.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME));
final long stopTime = System.currentTimeMillis() + 60_000;
GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {
@Override
public void apply(Integer idx) {
try {
while (System.currentTimeMillis() < stopTime) {
Ignite ignite = startGrid(idx + 1);
IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
int qryCnt = ThreadLocalRandom.current().nextInt(10) + 1;
for (int i = 0; i < qryCnt; i++) {
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
// No-op.
}
});
QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(qry);
cur.close();
}
GridTestUtils.invoke(ignite.configuration().getDiscoverySpi(), "simulateNodeFailure");
ignite.close();
}
} catch (Exception e) {
log.error("Unexpected error: " + e, e);
throw new IgniteException(e);
}
}
}, 5, "node-restart");
}
use of javax.cache.Cache in project ignite by apache.
the class IgniteBinaryObjectFieldsQuerySelfTest method checkQuery.
/**
* @throws Exception If failed.
*/
private void checkQuery(CacheMode cacheMode, CacheAtomicityMode atomicity) throws Exception {
IgniteCache<Object, Object> cache = grid(GRID_CNT - 1).getOrCreateCache(cache(cacheMode, atomicity));
try {
populate(cache);
QueryCursor<Cache.Entry<Object, Object>> cur = cache.query(new SqlQuery("Person", "order " + "by id asc"));
List<Cache.Entry<Object, Object>> all = cur.getAll();
assertEquals(100, all.size());
for (int i = 0; i < 100; i++) {
Object person = all.get(i).getValue();
assertEquals(Integer.valueOf(i), U.field(person, "id"));
assertEquals("person-" + i, U.field(person, "name"));
assertEquals("person-last-" + i, U.field(person, "lastName"));
assertEquals((double) (i * 25), U.field(person, "salary"));
}
int max = 49;
// Check local scan query with keepBinary flag set.
ScanQuery<BinaryObject, BinaryObject> scanQry = new ScanQuery<>(new PersonKeyFilter(max));
QueryCursor<Cache.Entry<BinaryObject, BinaryObject>> curs = grid(GRID_CNT - 1).cache(DEFAULT_CACHE_NAME).withKeepBinary().query(scanQry);
List<Cache.Entry<BinaryObject, BinaryObject>> records = curs.getAll();
assertEquals(50, records.size());
for (Cache.Entry<BinaryObject, BinaryObject> entry : records) {
BinaryObject key = entry.getKey();
assertTrue(key.<Integer>field("id") <= max);
assertEquals(PERSON_KEY_CLS_NAME, key.deserialize().getClass().getName());
}
} finally {
grid(GRID_CNT - 1).cache(DEFAULT_CACHE_NAME).removeAll();
grid(GRID_CNT - 1).destroyCache(DEFAULT_CACHE_NAME);
}
}
Aggregations