use of javax.cache.CacheException in project ignite by apache.
the class IgniteCacheOffheapEvictQueryTest method testEvictAndRemove.
/**
* @throws Exception If failed.
*/
public void testEvictAndRemove() throws Exception {
final int KEYS_CNT = 3000;
final int THREADS_CNT = 250;
final IgniteCache<Integer, Integer> c = startGrid().cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < KEYS_CNT; i++) {
c.put(i, i);
if ((i & 1) == 0)
c.localEvict(F.asList(i));
}
X.println("___ Cache loaded...");
final CyclicBarrier b = new CyclicBarrier(THREADS_CNT, new Runnable() {
@Override
public void run() {
X.println("___ go!");
}
});
final AtomicInteger keys = new AtomicInteger(KEYS_CNT);
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
@Override
public void run() {
Random rnd = new GridRandom();
try {
b.await();
} catch (InterruptedException e) {
throw new IgniteInterruptedException(e);
} catch (BrokenBarrierException e) {
throw new IllegalStateException(e);
}
while (keys.get() > 0) {
int k = rnd.nextInt(KEYS_CNT);
try {
switch(rnd.nextInt(4)) {
case 0:
c.localEvict(F.asList(k));
break;
case 1:
c.get(k);
break;
case 2:
if (c.remove(k))
keys.decrementAndGet();
break;
case 3:
c.query(new SqlFieldsQuery("select _val from Integer where _key between ? and ?").setArgs(k, k + 20).setLocal(true)).getAll();
break;
}
} catch (CacheException e) {
String msgStart = "Failed to get value for key:";
for (Throwable th = e; th != null; th = th.getCause()) {
String msg = th.getMessage();
if (msg != null && msg.startsWith(msgStart)) {
int dot = msg.indexOf('.', msgStart.length());
assertTrue(dot != -1);
final Integer failedKey = Integer.parseInt(msg.substring(msgStart.length(), dot).trim());
X.println("___ failed key: " + failedKey);
break;
}
}
LT.warn(log, e.getMessage());
return;
}
}
}
}, THREADS_CNT);
try {
fut.get(60_000);
if (c.size(CachePeekMode.ALL) != 0)
fail("Not all keys removed.");
X.println("___ all keys removed");
} catch (IgniteFutureTimeoutCheckedException ignored) {
X.println("___ timeout");
X.println("___ keys: " + keys.get());
keys.set(0);
fut.get();
}
}
use of javax.cache.CacheException in project ignite by apache.
the class CacheSerializableTransactionsTest method incrementTx.
/**
* @param nearCache If {@code true} near cache is enabled.
* @param store If {@code true} cache store is enabled.
* @param restart If {@code true} restarts one node.
* @throws Exception If failed.
*/
private void incrementTx(boolean nearCache, boolean store, final boolean restart) throws Exception {
final Ignite srv = ignite(1);
CacheConfiguration<Integer, Integer> ccfg = cacheConfiguration(PARTITIONED, FULL_SYNC, 1, store, false);
final List<Ignite> clients = clients();
final String cacheName = srv.createCache(ccfg).getName();
final AtomicBoolean stop = new AtomicBoolean();
try {
final List<IgniteCache<Integer, Integer>> caches = new ArrayList<>();
for (Ignite client : clients) {
if (nearCache)
caches.add(client.createNearCache(cacheName, new NearCacheConfiguration<Integer, Integer>()));
else
caches.add(client.<Integer, Integer>cache(cacheName));
}
IgniteInternalFuture<?> restartFut = restart ? restartFuture(stop, null) : null;
final long stopTime = U.currentTimeMillis() + getTestTimeout() - 30_000;
for (int i = 0; i < 30; i++) {
final AtomicInteger cntr = new AtomicInteger();
final Integer key = i;
final AtomicInteger threadIdx = new AtomicInteger();
final int THREADS = 10;
final CyclicBarrier barrier = new CyclicBarrier(THREADS);
GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
int idx = threadIdx.getAndIncrement() % caches.size();
IgniteCache<Integer, Integer> cache = caches.get(idx);
Ignite ignite = cache.unwrap(Ignite.class);
IgniteTransactions txs = ignite.transactions();
log.info("Started update thread: " + ignite.name());
barrier.await();
for (int i = 0; i < 1000; i++) {
if (i % 100 == 0 && U.currentTimeMillis() > stopTime)
break;
try {
try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
Integer val = cache.get(key);
cache.put(key, val == null ? 1 : val + 1);
tx.commit();
}
cntr.incrementAndGet();
} catch (TransactionOptimisticException ignore) {
// Retry.
} catch (IgniteException | CacheException e) {
assertTrue("Unexpected exception [err=" + e + ", cause=" + e.getCause() + ']', restart && X.hasCause(e, ClusterTopologyCheckedException.class));
}
}
return null;
}
}, THREADS, "update-thread").get();
log.info("Iteration [iter=" + i + ", val=" + cntr.get() + ']');
assertTrue(cntr.get() > 0);
checkValue(key, cntr.get(), cacheName, restart);
if (U.currentTimeMillis() > stopTime)
break;
}
stop.set(true);
if (restartFut != null)
restartFut.get();
} finally {
stop.set(true);
destroyCache(cacheName);
}
}
use of javax.cache.CacheException in project ignite by apache.
the class GridCacheBasicOpAbstractTest method testOptimisticTransaction.
/**
*
* @throws IgniteCheckedException If test fails.
*/
public void testOptimisticTransaction() throws Exception {
CountDownLatch latch = new CountDownLatch(9);
IgnitePredicate<Event> lsnr = new CacheEventListener(latch);
try {
IgniteCache<String, String> cache1 = ignite1.cache(DEFAULT_CACHE_NAME);
IgniteCache<String, String> cache2 = ignite2.cache(DEFAULT_CACHE_NAME);
IgniteCache<String, String> cache3 = ignite3.cache(DEFAULT_CACHE_NAME);
ignite1.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
ignite2.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
ignite3.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
Transaction tx = ignite1.transactions().txStart(OPTIMISTIC, READ_COMMITTED, 0, 0);
try {
cache1.put("tx1", "val1");
cache1.put("tx2", "val2");
cache1.put("tx3", "val3");
assert cache2.get("tx1") == null;
assert cache2.get("tx2") == null;
assert cache2.get("tx3") == null;
assert cache3.get("tx1") == null;
assert cache3.get("tx2") == null;
assert cache3.get("tx3") == null;
tx.commit();
} catch (CacheException e) {
tx.rollback();
throw e;
}
assert latch.await(5, SECONDS);
String b1 = cache2.get("tx1");
String b2 = cache2.get("tx2");
String b3 = cache2.get("tx3");
String c1 = cache3.get("tx1");
String c2 = cache3.get("tx2");
String c3 = cache3.get("tx3");
assert b1 != null : "Invalid value: " + b1;
assert b2 != null : "Invalid value: " + b2;
assert b3 != null : "Invalid value: " + b3;
assert c1 != null : "Invalid value: " + c1;
assert c2 != null : "Invalid value: " + c2;
assert c3 != null : "Invalid value: " + c3;
assert "val1".equals(b1);
assert "val2".equals(b2);
assert "val3".equals(b3);
assert "val1".equals(c1);
assert "val2".equals(c2);
assert "val3".equals(c3);
} finally {
ignite1.events().stopLocalListen(lsnr);
ignite2.events().stopLocalListen(lsnr);
ignite3.events().stopLocalListen(lsnr);
}
}
use of javax.cache.CacheException in project ignite by apache.
the class GridH2IndexBase method toRow.
/**
* @param msg Message.
* @return Row.
*/
private Row toRow(GridH2RowMessage msg) {
if (msg == null)
return null;
GridKernalContext ctx = kernalContext();
List<GridH2ValueMessage> vals = msg.values();
assert !F.isEmpty(vals) : vals;
Value[] vals0 = new Value[vals.size()];
for (int i = 0; i < vals0.length; i++) {
try {
vals0[i] = vals.get(i).value(ctx);
} catch (IgniteCheckedException e) {
throw new CacheException(e);
}
}
return database.createRow(vals0, MEMORY_CALCULATE);
}
use of javax.cache.CacheException in project ignite by apache.
the class GridH2IndexBase method toRowMessage.
/**
* @param row Row.
* @return Row message.
*/
private GridH2RowMessage toRowMessage(Row row) {
if (row == null)
return null;
int cols = row.getColumnCount();
assert cols > 0 : cols;
List<GridH2ValueMessage> vals = new ArrayList<>(cols);
for (int i = 0; i < cols; i++) {
try {
vals.add(GridH2ValueMessageFactory.toMessage(row.getValue(i)));
} catch (IgniteCheckedException e) {
throw new CacheException(e);
}
}
GridH2RowMessage res = new GridH2RowMessage();
res.values(vals);
return res;
}
Aggregations