use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class IgfsAbstractSelfTest method checkDeadlocks.
/**
* Check deadlocks by creating complex directories structure and then executing chaotic operations on it. A lot of
* exception are expected here. We are not interested in them. Instead, we want to ensure that no deadlocks occur
* during execution.
*
* @param lvlCnt Total levels in folder hierarchy.
* @param childrenDirPerLvl How many children directories to create per level.
* @param childrenFilePerLvl How many children file to create per level.
* @param primaryLvlCnt How many levels will exist in the primary file system before check start.
* @param renCnt How many renames to perform.
* @param delCnt How many deletes to perform.
* @param updateCnt How many updates to perform.
* @param mkdirsCnt How many directory creations to perform.
* @param createCnt How many file creations to perform.
* @throws Exception If failed.
*/
@SuppressWarnings("ConstantConditions")
private void checkDeadlocks(final int lvlCnt, final int childrenDirPerLvl, final int childrenFilePerLvl, int primaryLvlCnt, int renCnt, int delCnt, int updateCnt, int mkdirsCnt, int createCnt) throws Exception {
assert childrenDirPerLvl > 0;
// First define file system structure.
final Map<Integer, List<IgfsPath>> dirPaths = new HashMap<>();
final Map<Integer, List<IgfsPath>> filePaths = new HashMap<>();
Queue<IgniteBiTuple<Integer, IgfsPath>> queue = new ArrayDeque<>();
// Add root directory.
queue.add(F.t(0, IgfsPath.ROOT));
while (!queue.isEmpty()) {
IgniteBiTuple<Integer, IgfsPath> entry = queue.poll();
int lvl = entry.getKey();
if (lvl < lvlCnt) {
int newLvl = lvl + 1;
for (int i = 0; i < childrenDirPerLvl; i++) {
IgfsPath path = new IgfsPath(entry.getValue(), "dir-" + newLvl + "-" + i);
queue.add(F.t(newLvl, path));
if (!dirPaths.containsKey(newLvl))
dirPaths.put(newLvl, new ArrayList<IgfsPath>());
dirPaths.get(newLvl).add(path);
}
for (int i = 0; i < childrenFilePerLvl; i++) {
IgfsPath path = new IgfsPath(entry.getValue(), "file-" + newLvl + "-" + i);
if (!filePaths.containsKey(newLvl))
filePaths.put(newLvl, new ArrayList<IgfsPath>());
filePaths.get(newLvl).add(path);
}
}
}
// Now as we have all paths defined, plan operations on them.
final Random rand = new Random(SEED);
final int totalOpCnt = renCnt + delCnt + updateCnt + mkdirsCnt + createCnt;
if (totalOpCnt == 0)
throw new RuntimeException("Operations count is zero.");
final CyclicBarrier barrier = new CyclicBarrier(totalOpCnt);
Collection<Thread> threads = new ArrayList<>(totalOpCnt);
// Renames.
for (int i = 0; i < renCnt; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
int fromLvl = rand.nextInt(lvlCnt) + 1;
int toLvl = rand.nextInt(lvlCnt) + 1;
List<IgfsPath> fromPaths;
List<IgfsPath> toPaths;
if (rand.nextInt(childrenDirPerLvl + childrenFilePerLvl) < childrenDirPerLvl) {
// Rename directories.
fromPaths = dirPaths.get(fromLvl);
toPaths = dirPaths.get(toLvl);
} else {
// Rename files.
fromPaths = filePaths.get(fromLvl);
toPaths = filePaths.get(toLvl);
}
IgfsPath fromPath = fromPaths.get(rand.nextInt(fromPaths.size()));
IgfsPath toPath = toPaths.get(rand.nextInt(toPaths.size()));
U.awaitQuiet(barrier);
igfs.rename(fromPath, toPath);
} catch (IgniteException ignore) {
// No-op.
}
}
};
threads.add(new Thread(r));
}
// Deletes.
for (int i = 0; i < delCnt; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
int lvl = rand.nextInt(lvlCnt) + 1;
IgfsPath path = rand.nextInt(childrenDirPerLvl + childrenFilePerLvl) < childrenDirPerLvl ? dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size())) : filePaths.get(lvl).get(rand.nextInt(filePaths.get(lvl).size()));
U.awaitQuiet(barrier);
igfs.delete(path, true);
} catch (IgniteException ignore) {
// No-op.
}
}
};
threads.add(new Thread(r));
}
// Updates.
for (int i = 0; i < updateCnt; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
int lvl = rand.nextInt(lvlCnt) + 1;
IgfsPath path = rand.nextInt(childrenDirPerLvl + childrenFilePerLvl) < childrenDirPerLvl ? dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size())) : filePaths.get(lvl).get(rand.nextInt(filePaths.get(lvl).size()));
U.awaitQuiet(barrier);
igfs.update(path, properties("owner", "group", null));
} catch (IgniteException ignore) {
// No-op.
}
}
};
threads.add(new Thread(r));
}
// Directory creations.
final AtomicInteger dirCtr = new AtomicInteger();
for (int i = 0; i < mkdirsCnt; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
int lvl = rand.nextInt(lvlCnt) + 1;
IgfsPath parentPath = dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size()));
IgfsPath path = new IgfsPath(parentPath, "newDir-" + dirCtr.incrementAndGet());
U.awaitQuiet(barrier);
igfs.mkdirs(path);
} catch (IgniteException ignore) {
// No-op.
}
}
};
threads.add(new Thread(r));
}
// File creations.
final AtomicInteger fileCtr = new AtomicInteger();
for (int i = 0; i < createCnt; i++) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
int lvl = rand.nextInt(lvlCnt) + 1;
IgfsPath parentPath = dirPaths.get(lvl).get(rand.nextInt(dirPaths.get(lvl).size()));
IgfsPath path = new IgfsPath(parentPath, "newFile-" + fileCtr.incrementAndGet());
U.awaitQuiet(barrier);
IgfsOutputStream os = null;
try {
os = igfs.create(path, true);
os.write(chunk);
} finally {
U.closeQuiet(os);
}
} catch (IOException | IgniteException ignore) {
// No-op.
}
}
};
threads.add(new Thread(r));
}
// Create file/directory structure.
for (int i = 0; i < lvlCnt; i++) {
int lvl = i + 1;
boolean targetToPrimary = !dual || lvl <= primaryLvlCnt;
IgfsPath[] dirs = dirPaths.get(lvl).toArray(new IgfsPath[dirPaths.get(lvl).size()]);
IgfsPath[] files = filePaths.get(lvl).toArray(new IgfsPath[filePaths.get(lvl).size()]);
if (targetToPrimary)
create(igfs, dirs, files);
else
create(igfsSecondary, dirs, files);
}
// Start all threads and wait for them to finish.
for (Thread thread : threads) thread.start();
U.joinThreads(threads, null);
}
use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestAtomicStamped.
/**
* Tests atomic stamped value.
*
* @param topWorker Topology change worker.
* @throws Exception If failed.
*/
private void doTestAtomicStamped(ConstantTopologyChangeWorker topWorker) throws Exception {
try (IgniteAtomicStamped<Integer, Integer> s = grid(0).atomicStamped(STRUCTURE_NAME, 1, 1, true)) {
IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() {
@Override
public Object apply(Ignite ignite) {
IgniteBiTuple<Integer, Integer> t = ignite.atomicStamped(STRUCTURE_NAME, 1, 1, false).get();
assert t.get1() > 0;
assert t.get2() > 0;
return null;
}
});
int val = s.value();
while (!fut.isDone()) {
IgniteBiTuple<Integer, Integer> t = s.get();
assertEquals(val, (int) t.get1());
assertEquals(val, (int) t.get2());
++val;
s.set(val, val);
}
fut.get();
for (Ignite g : G.allGrids()) {
IgniteBiTuple<Integer, Integer> t = g.atomicStamped(STRUCTURE_NAME, 1, 1, false).get();
assertEquals(val, (int) t.get1());
assertEquals(val, (int) t.get2());
}
}
}
use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class GridEventConsumeSelfTest method testMultithreadedWithNodeRestart.
/**
* @throws Exception If failed.
*/
public void testMultithreadedWithNodeRestart() throws Exception {
final AtomicBoolean stop = new AtomicBoolean();
final BlockingQueue<IgniteBiTuple<Integer, UUID>> queue = new LinkedBlockingQueue<>();
final Collection<UUID> started = new GridConcurrentHashSet<>();
final Collection<UUID> stopped = new GridConcurrentHashSet<>();
final Random rnd = new Random();
IgniteInternalFuture<?> starterFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
for (int i = 0; i < CONSUME_CNT; i++) {
int idx = rnd.nextInt(GRID_CNT);
try {
IgniteEvents evts = grid(idx).events();
UUID consumeId = evts.remoteListenAsync(new P2<UUID, Event>() {
@Override
public boolean apply(UUID uuid, Event evt) {
return true;
}
}, null, EVT_JOB_STARTED).get(3000);
started.add(consumeId);
queue.add(F.t(idx, consumeId));
} catch (ClusterTopologyException ignored) {
// No-op.
}
U.sleep(10);
}
stop.set(true);
return null;
}
}, 8, "consume-starter");
IgniteInternalFuture<?> stopperFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
IgniteBiTuple<Integer, UUID> t = queue.poll(1, SECONDS);
if (t == null)
continue;
int idx = t.get1();
UUID consumeId = t.get2();
try {
IgniteEvents evts = grid(idx).events();
evts.stopRemoteListenAsync(consumeId).get(3000);
stopped.add(consumeId);
} catch (ClusterTopologyException ignored) {
// No-op.
}
}
return null;
}
}, 4, "consume-stopper");
IgniteInternalFuture<?> nodeRestarterFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
startGrid("anotherGrid");
stopGrid("anotherGrid");
}
return null;
}
}, 1, "node-restarter");
IgniteInternalFuture<?> jobRunnerFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (!stop.get()) {
int idx = rnd.nextInt(GRID_CNT);
try {
grid(idx).compute().runAsync(F.noop()).get(3000);
} catch (IgniteException ignored) {
// Ignore all job execution related errors.
}
}
return null;
}
}, 1, "job-runner");
starterFut.get();
stopperFut.get();
nodeRestarterFut.get();
jobRunnerFut.get();
IgniteBiTuple<Integer, UUID> t;
while ((t = queue.poll()) != null) {
int idx = t.get1();
UUID consumeId = t.get2();
grid(idx).events().stopRemoteListenAsync(consumeId).get(3000);
stopped.add(consumeId);
}
Collection<UUID> notStopped = F.lose(started, true, stopped);
assertEquals("Not stopped IDs: " + notStopped, 0, notStopped.size());
}
use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.
the class GridRestProcessor method start.
/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
if (isRestEnabled()) {
if (notStartOnClient()) {
U.quietAndInfo(log, "REST protocols do not start on client node. " + "To start the protocols on client node set '-DIGNITE_REST_START_ON_CLIENT=true' system property.");
return;
}
// Register handlers.
addHandler(new GridCacheCommandHandler(ctx));
addHandler(new GridTaskCommandHandler(ctx));
addHandler(new GridTopologyCommandHandler(ctx));
addHandler(new GridVersionCommandHandler(ctx));
addHandler(new DataStructuresCommandHandler(ctx));
addHandler(new QueryCommandHandler(ctx));
addHandler(new GridLogCommandHandler(ctx));
addHandler(new GridChangeStateCommandHandler(ctx));
// Start protocols.
startTcpProtocol();
startHttpProtocol();
for (GridRestProtocol proto : protos) {
Collection<IgniteBiTuple<String, Object>> props = proto.getProperties();
if (props != null) {
for (IgniteBiTuple<String, Object> p : props) {
String key = p.getKey();
if (key == null)
continue;
if (ctx.hasNodeAttribute(key))
throw new IgniteCheckedException("Node attribute collision for attribute [processor=GridRestProcessor, attr=" + key + ']');
ctx.addNodeAttribute(key, p.getValue());
}
}
}
}
}
Aggregations