use of org.apache.ignite.Ignite in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method testSemaphoreSingleNodeFailure.
/**
* @throws Exception If failed.
*/
public void testSemaphoreSingleNodeFailure() throws Exception {
final Ignite i1 = grid(0);
IgniteSemaphore sem1 = i1.semaphore(STRUCTURE_NAME, 1, false, true);
sem1.acquire();
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
boolean failed = true;
IgniteSemaphore sem2 = i1.semaphore(STRUCTURE_NAME, 1, false, true);
try {
sem2.acquire();
} catch (Exception ignored) {
failed = false;
} finally {
assertFalse(failed);
sem2.release();
}
return null;
}
});
while (!sem1.hasQueuedThreads()) {
try {
Thread.sleep(1);
} catch (InterruptedException ignored) {
fail();
}
}
i1.close();
fut.get();
}
use of org.apache.ignite.Ignite in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestReentrantLock.
/**
* @throws Exception If failed.
*/
private void doTestReentrantLock(final ConstantTopologyChangeWorker topWorker, final boolean failoverSafe, final boolean fair) throws Exception {
IgniteEx ig = grid(0);
try (IgniteLock lock = ig.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, true)) {
IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Void>() {
@Override
public Void apply(Ignite ignite) {
final IgniteLock l = ignite.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, false);
final AtomicBoolean done = new AtomicBoolean(false);
GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
l.lock();
} finally {
done.set(true);
}
return null;
}
});
// Wait until l.lock() has been called.
while (!l.hasQueuedThreads() && !done.get()) {
// No-op.
}
return null;
}
});
while (!fut.isDone()) {
try {
lock.lock();
} catch (IgniteException e) {
// Exception may happen in non-failoversafe mode.
if (failoverSafe)
throw e;
} finally {
// Broken lock cannot be used in non-failoversafe mode.
if (!lock.isBroken() || failoverSafe) {
assertTrue(lock.isHeldByCurrentThread());
lock.unlock();
assertFalse(lock.isHeldByCurrentThread());
}
}
}
fut.get();
for (Ignite g : G.allGrids()) {
IgniteLock l = g.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, false);
assertTrue(g.name(), !l.isHeldByCurrentThread() || lock.isBroken());
}
}
}
use of org.apache.ignite.Ignite in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method testAtomicSequenceInitialization.
/**
* @throws Exception If failed.
*/
public void testAtomicSequenceInitialization() throws Exception {
int threadCnt = 3;
final AtomicInteger idx = new AtomicInteger(gridCount());
IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new CA() {
@Override
public void apply() {
int id = idx.getAndIncrement();
try {
log.info("Start node: " + id);
startGrid(id);
Thread.sleep(1000);
} catch (Exception e) {
throw F.wrap(e);
} finally {
stopGrid(id);
info("Thread finished.");
}
}
}, threadCnt, "test-thread");
while (!fut.isDone()) {
grid(0).compute().call(new IgniteCallable<Object>() {
/** */
@IgniteInstanceResource
private Ignite g;
@Override
public Object call() throws Exception {
IgniteAtomicSequence seq = g.atomicSequence(STRUCTURE_NAME, 1, true);
assert seq != null;
for (int i = 0; i < 1000; i++) seq.getAndIncrement();
return null;
}
});
}
fut.get();
}
use of org.apache.ignite.Ignite in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method testUncommitedTxLeave.
/**
* @throws Exception If failed.
*/
public void testUncommitedTxLeave() throws Exception {
final int val = 10;
grid(0).atomicLong(STRUCTURE_NAME, val, true);
GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
Ignite g = startGrid(NEW_IGNITE_INSTANCE_NAME);
try {
g.transactions().txStart();
g.cache(TRANSACTIONAL_CACHE_NAME).put(1, 1);
assertEquals(val + 1, g.atomicLong(STRUCTURE_NAME, val, false).incrementAndGet());
} finally {
stopGrid(NEW_IGNITE_INSTANCE_NAME);
}
return null;
}
}).get();
waitForDiscovery(G.allGrids().toArray(new Ignite[gridCount()]));
assertEquals(val + 1, grid(0).atomicLong(STRUCTURE_NAME, val, false).get());
}
use of org.apache.ignite.Ignite in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method testAtomicLongFailsWhenServersLeft.
/**
* @throws Exception If failed.
*/
public void testAtomicLongFailsWhenServersLeft() throws Exception {
client = true;
Ignite ignite = startGrid(gridCount());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
for (int i = 0; i < gridCount(); i++) stopGrid(i);
}
}, 10_000);
long stopTime = U.currentTimeMillis() + TEST_TIMEOUT / 2;
IgniteAtomicLong atomic = ignite.atomicLong(STRUCTURE_NAME, 10, true);
try {
while (U.currentTimeMillis() < stopTime) assertEquals(10, atomic.get());
} catch (IgniteException ignore) {
// Test that client does not hang.
return;
}
fail();
}
Aggregations