use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.
the class AtomicIntegerArrayTest method testDecrementAndGet.
/**
* decrementAndGet decrements and returns current value
*/
public void testDecrementAndGet() {
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
for (int i = 0; i < SIZE; i++) {
aa.set(i, 1);
assertEquals(0, aa.decrementAndGet(i));
assertEquals(-1, aa.decrementAndGet(i));
assertEquals(-2, aa.decrementAndGet(i));
assertEquals(-2, aa.get(i));
}
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.
the class AtomicIntegerArrayTest method testGetAndAdd.
/**
* getAndAdd returns previous value and adds given value
*/
public void testGetAndAdd() {
AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
for (int i = 0; i < SIZE; i++) {
aa.set(i, 1);
assertEquals(1, aa.getAndAdd(i, 2));
assertEquals(3, aa.get(i));
assertEquals(3, aa.getAndAdd(i, -4));
assertEquals(-1, aa.get(i));
}
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project ignite by apache.
the class IgniteCacheClientQueryReplicatedNodeRestartSelfTest method testRestarts.
/**
* @throws Exception If failed.
*/
@Test
public void testRestarts() throws Exception {
int duration = 90 * 1000;
int qryThreadNum = 5;
// 2 of 4 data nodes
int restartThreadsNum = 2;
final int nodeLifeTime = 2 * 1000;
final int logFreq = 10;
startGridsMultiThreaded(GRID_CNT);
// The last is client only.
final AtomicIntegerArray locks = new AtomicIntegerArray(GRID_CNT - 1);
fillCaches();
final List<List<?>> pRes = grid(0).cache("pu").query(new SqlFieldsQuery(QRY)).getAll();
Thread.sleep(3000);
assertEquals(pRes, grid(0).cache("pu").query(new SqlFieldsQuery(QRY)).getAll());
assertFalse(pRes.isEmpty());
final AtomicInteger qryCnt = new AtomicInteger();
final AtomicBoolean qrysDone = new AtomicBoolean();
final List<Integer> cacheSize = new ArrayList<>(4);
for (int i = 0; i < GRID_CNT - 1; i++) {
int j = 0;
for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
IgniteCache<?, ?> cache = grid(i).cache(cacheName);
assertClient(cache, false);
if (i == 0)
cacheSize.add(cache.size());
else
assertEquals(cacheSize.get(j++).intValue(), cache.size());
}
}
int j = 0;
for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
IgniteCache<?, ?> cache = grid(GRID_CNT - 1).cache(cacheName);
assertClient(cache, true);
assertEquals(cacheSize.get(j++).intValue(), cache.size());
}
final IgniteCache<?, ?> clientCache = grid(GRID_CNT - 1).cache("pu");
IgniteInternalFuture<?> fut1 = multithreadedAsync(new CAX() {
@Override
public void applyx() throws IgniteCheckedException {
GridRandom rnd = new GridRandom();
while (!qrysDone.get()) {
SqlFieldsQuery qry = new SqlFieldsQuery(QRY);
boolean smallPageSize = rnd.nextBoolean();
if (smallPageSize)
qry.setPageSize(3);
List<List<?>> res;
try {
res = clientCache.query(qry).getAll();
} catch (CacheException e) {
assertTrue("On large page size must retry.", smallPageSize);
boolean failedOnRemoteFetch = false;
for (Throwable th = e; th != null; th = th.getCause()) {
if (!(th instanceof CacheException))
continue;
if (th.getMessage() != null && th.getMessage().startsWith("Failed to fetch data from node:")) {
failedOnRemoteFetch = true;
break;
}
}
if (!failedOnRemoteFetch) {
e.printStackTrace();
fail("Must fail inside of GridResultPage.fetchNextPage or subclass.");
}
res = FAKE;
}
if (res != FAKE && !res.equals(pRes)) {
int j = 0;
// Check for data loss.
for (String cacheName : F.asList("co", "pr", "pe", "pu")) {
assertEquals(cacheName, cacheSize.get(j++).intValue(), grid(GRID_CNT - 1).cache(cacheName).size());
}
// Fail with nice message.
assertEquals(pRes, res);
}
int c = qryCnt.incrementAndGet();
if (c % logFreq == 0)
info("Executed queries: " + c);
}
}
}, qryThreadNum, "query-thread");
final AtomicInteger restartCnt = new AtomicInteger();
final AtomicBoolean restartsDone = new AtomicBoolean();
IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable<Object>() {
@SuppressWarnings({ "BusyWait" })
@Override
public Object call() throws Exception {
GridRandom rnd = new GridRandom();
while (!restartsDone.get()) {
int g;
do {
g = rnd.nextInt(locks.length());
} while (!locks.compareAndSet(g, 0, -1));
log.info("Stop node: " + g);
stopGrid(g);
Thread.sleep(rnd.nextInt(nodeLifeTime));
log.info("Start node: " + g);
startGrid(g);
Thread.sleep(rnd.nextInt(nodeLifeTime));
locks.set(g, 0);
int c = restartCnt.incrementAndGet();
if (c % logFreq == 0)
info("Node restarts: " + c);
}
return true;
}
}, restartThreadsNum, "restart-thread");
Thread.sleep(duration);
info("Stopping..");
restartsDone.set(true);
fut2.get();
info("Restarts stopped.");
qrysDone.set(true);
fut1.get();
info("Queries stopped.");
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project ignite by apache.
the class TxRollbackOnTopologyChangeTest method testRollbackOnTopologyChange.
/**
* Tests rollbacks on topology change.
*/
@Test
public void testRollbackOnTopologyChange() throws Exception {
final AtomicBoolean stop = new AtomicBoolean();
final long seed = System.currentTimeMillis();
final Random r = new Random(seed);
log.info("Using seed: " + seed);
AtomicIntegerArray reservedIdx = new AtomicIntegerArray(TOTAL_CNT);
final int keysCnt = SRV_CNT - 1;
for (int k = 0; k < keysCnt; k++) grid(0).cache(CACHE_NAME).put(k, (long) 0);
final CyclicBarrier b = new CyclicBarrier(keysCnt);
AtomicInteger idGen = new AtomicInteger();
final IgniteInternalFuture<?> txFut = multithreadedAsync(new Runnable() {
@Override
public void run() {
int key = idGen.getAndIncrement();
List<Integer> keys = new ArrayList<>();
for (int k = 0; k < keysCnt; k++) keys.add(k);
int cntr = 0;
for (int i = 0; i < ITERATIONS; i++) {
cntr++;
int nodeId;
while (!reservedIdx.compareAndSet((nodeId = r.nextInt(TOTAL_CNT)), 0, 1)) doSleep(10);
U.awaitQuiet(b);
final IgniteEx grid = grid(nodeId);
try (final Transaction tx = grid.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, 0, 0)) {
reservedIdx.set(nodeId, 0);
// Construct deadlock
grid.cache(CACHE_NAME).get(keys.get(key));
// Should block.
grid.cache(CACHE_NAME).get(keys.get((key + 1) % keysCnt));
fail("Deadlock expected");
} catch (Throwable ignore) {
// Expected.
}
if (key == 0)
log.info("Rolled back: " + cntr);
}
}
}, keysCnt, "tx-lock-thread");
final IgniteInternalFuture<?> restartFut = multithreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
while (!stop.get()) {
final int nodeId = r.nextInt(TOTAL_CNT);
if (!reservedIdx.compareAndSet(nodeId, 0, 1)) {
yield();
continue;
}
stopGrid(nodeId);
doSleep(500 + r.nextInt(1000));
if (nodeId >= SRV_CNT)
startClientGrid(nodeId);
else
startGrid(nodeId);
reservedIdx.set(nodeId, 0);
}
return null;
}
}, 1, "tx-restart-thread");
// Wait for iterations to complete.
txFut.get();
stop.set(true);
restartFut.get();
checkFutures();
}
Aggregations