use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class TcpCommunicationSpi method resetNioServer.
/**
* Recreates tpcSrvr socket instance.
*
* @return Server instance.
* @throws IgniteCheckedException Thrown if it's not possible to create server.
*/
private GridNioServer<Message> resetNioServer() throws IgniteCheckedException {
if (boundTcpPort >= 0)
throw new IgniteCheckedException("Tcp NIO server was already created on port " + boundTcpPort);
IgniteCheckedException lastEx = null;
// If configured TCP port is busy, find first available in range.
int lastPort = locPortRange == 0 ? locPort : locPort + locPortRange - 1;
for (int port = locPort; port <= lastPort; port++) {
try {
MessageFactory msgFactory = new MessageFactory() {
private MessageFactory impl;
@Nullable
@Override
public Message create(short type) {
if (impl == null)
impl = getSpiContext().messageFactory();
assert impl != null;
return impl.create(type);
}
};
GridNioMessageReaderFactory readerFactory = new GridNioMessageReaderFactory() {
private MessageFormatter formatter;
@Override
public MessageReader reader(GridNioSession ses, MessageFactory msgFactory) throws IgniteCheckedException {
if (formatter == null)
formatter = getSpiContext().messageFormatter();
assert formatter != null;
ConnectionKey key = ses.meta(CONN_IDX_META);
return key != null ? formatter.reader(key.nodeId(), msgFactory) : null;
}
};
GridNioMessageWriterFactory writerFactory = new GridNioMessageWriterFactory() {
private MessageFormatter formatter;
@Override
public MessageWriter writer(GridNioSession ses) throws IgniteCheckedException {
if (formatter == null)
formatter = getSpiContext().messageFormatter();
assert formatter != null;
ConnectionKey key = ses.meta(CONN_IDX_META);
return key != null ? formatter.writer(key.nodeId()) : null;
}
};
GridDirectParser parser = new GridDirectParser(log.getLogger(GridDirectParser.class), msgFactory, readerFactory);
IgnitePredicate<Message> skipRecoveryPred = new IgnitePredicate<Message>() {
@Override
public boolean apply(Message msg) {
return msg instanceof RecoveryLastReceivedMessage;
}
};
boolean clientMode = Boolean.TRUE.equals(ignite.configuration().isClientMode());
IgniteBiInClosure<GridNioSession, Integer> queueSizeMonitor = !clientMode && slowClientQueueLimit > 0 ? new CI2<GridNioSession, Integer>() {
@Override
public void apply(GridNioSession ses, Integer qSize) {
checkClientQueueSize(ses, qSize);
}
} : null;
GridNioFilter[] filters;
if (isSslEnabled()) {
GridNioSslFilter sslFilter = new GridNioSslFilter(ignite.configuration().getSslContextFactory().create(), true, ByteOrder.nativeOrder(), log);
sslFilter.directMode(true);
sslFilter.wantClientAuth(true);
sslFilter.needClientAuth(true);
filters = new GridNioFilter[] { new GridNioCodecFilter(parser, log, true), new GridConnectionBytesVerifyFilter(log), sslFilter };
} else
filters = new GridNioFilter[] { new GridNioCodecFilter(parser, log, true), new GridConnectionBytesVerifyFilter(log) };
GridNioServer<Message> srvr = GridNioServer.<Message>builder().address(locHost).port(port).listener(srvLsnr).logger(log).selectorCount(selectorsCnt).igniteInstanceName(igniteInstanceName).serverName("tcp-comm").tcpNoDelay(tcpNoDelay).directBuffer(directBuf).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(sockSndBuf).socketReceiveBufferSize(sockRcvBuf).sendQueueLimit(msgQueueLimit).directMode(true).metricsListener(metricsLsnr).writeTimeout(sockWriteTimeout).selectorSpins(selectorSpins).filters(filters).writerFactory(writerFactory).skipRecoveryPredicate(skipRecoveryPred).messageQueueSizeListener(queueSizeMonitor).readWriteSelectorsAssign(usePairedConnections).build();
boundTcpPort = port;
// Ack Port the TCP server was bound to.
if (log.isInfoEnabled()) {
log.info("Successfully bound communication NIO server to TCP port " + "[port=" + boundTcpPort + ", locHost=" + locHost + ", selectorsCnt=" + selectorsCnt + ", selectorSpins=" + srvr.selectorSpins() + ", pairedConn=" + usePairedConnections + ']');
}
srvr.idleTimeout(idleConnTimeout);
return srvr;
} catch (IgniteCheckedException e) {
if (X.hasCause(e, SSLException.class))
throw new IgniteSpiException("Failed to create SSL context. SSL factory: " + ignite.configuration().getSslContextFactory() + '.', e);
lastEx = e;
if (log.isDebugEnabled())
log.debug("Failed to bind to local port (will try next port within range) [port=" + port + ", locHost=" + locHost + ']');
onException("Failed to bind to local port (will try next port within range) [port=" + port + ", locHost=" + locHost + ']', e);
}
}
// If free port wasn't found.
throw new IgniteCheckedException("Failed to bind to any port within range [startPort=" + locPort + ", portRange=" + locPortRange + ", locHost=" + locHost + ']', lastEx);
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgfsEventsAbstractSelfTest method testFileDataEvents.
/**
* Checks events on file data transfer operations.
*
* @throws Exception If failed.
*/
public void testFileDataEvents() throws Exception {
final List<Event> evtList = new ArrayList<>();
final int evtsCnt = 5;
final CountDownLatch latch = new CountDownLatch(evtsCnt);
grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
log.info("Received event [evt=" + evt + ']');
evtList.add(evt);
latch.countDown();
return true;
}
}, EVTS_IGFS);
final IgfsPath file = new IgfsPath("/file1");
final int dataSize = 1024;
byte[] buf = new byte[dataSize];
// Will generate IGFS_FILE_CREATED, IGFS_FILE_OPENED_WRITE, IGFS_FILE_CLOSED_WRITE.
try (IgfsOutputStream os = igfs.create(file, false)) {
// Will generate no events.
os.write(buf);
}
// Will generate EVT_IGFS_FILE_OPENED_READ, IGFS_FILE_CLOSED_READ.
try (IgfsInputStream is = igfs.open(file, 256)) {
// Will generate no events.
is.readFully(0, buf);
}
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals(evtsCnt, evtList.size());
IgfsEvent evt = (IgfsEvent) evtList.get(0);
assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
assertEquals(new IgfsPath("/file1"), evt.path());
evt = (IgfsEvent) evtList.get(1);
assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
assertEquals(new IgfsPath("/file1"), evt.path());
evt = (IgfsEvent) evtList.get(2);
assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
assertEquals(new IgfsPath("/file1"), evt.path());
assertEquals((long) dataSize, evt.dataSize());
evt = (IgfsEvent) evtList.get(3);
assertEquals(EVT_IGFS_FILE_OPENED_READ, evt.type());
assertEquals(new IgfsPath("/file1"), evt.path());
evt = (IgfsEvent) evtList.get(4);
assertEquals(EVT_IGFS_FILE_CLOSED_READ, evt.type());
assertEquals(new IgfsPath("/file1"), evt.path());
assertEquals((long) dataSize, evt.dataSize());
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgfsEventsAbstractSelfTest method testTwoFiles.
/**
* Checks events on CRUD operations on 2 files.
*
* @throws Exception If failed.
*/
public void testTwoFiles() throws Exception {
final List<Event> evtList = new ArrayList<>();
final int evtsCnt = 4 + 3 + 2 + 2;
final CountDownLatch latch = new CountDownLatch(evtsCnt);
grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
log.info("Received event [evt=" + evt + ']');
evtList.add(evt);
latch.countDown();
return true;
}
}, EVTS_IGFS);
IgfsPath dir = new IgfsPath("/dir1");
IgfsPath file1 = new IgfsPath(dir, "file1");
// Will generate EVT_IGFS_FILE_CREATED event + EVT_IGFS_DIR_CREATED event + OPEN + CLOSE.
igfs.create(file1, true).close();
IgfsPath file2 = new IgfsPath(dir, "file2");
// Will generate 1 EVT_IGFS_FILE_CREATED event + OPEN + CLOSE.
igfs.create(file2, true).close();
assertTrue(igfs.exists(dir));
assertTrue(igfs.exists(file1));
assertTrue(igfs.exists(file2));
// Will generate 1 EVT_IGFS_FILE_DELETED and 1 EVT_IGFS_FILE_PURGED.
assertTrue(igfs.delete(file1, false));
// Same.
assertTrue(igfs.delete(file2, false));
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals(evtsCnt, evtList.size());
IgfsEvent evt = (IgfsEvent) evtList.get(0);
assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1"), evt.path());
assertTrue(evt.isDirectory());
evt = (IgfsEvent) evtList.get(1);
assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1/file1"), evt.path());
evt = (IgfsEvent) evtList.get(2);
assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/file1"), evt.path());
evt = (IgfsEvent) evtList.get(3);
assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/file1"), evt.path());
assertEquals(0, evt.dataSize());
evt = (IgfsEvent) evtList.get(4);
assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1/file2"), evt.path());
evt = (IgfsEvent) evtList.get(5);
assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/file2"), evt.path());
evt = (IgfsEvent) evtList.get(6);
assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/file2"), evt.path());
assertEquals(0, evt.dataSize());
assertOneToOne(evtList.subList(7, 11), new EventPredicate(EVT_IGFS_FILE_DELETED, new IgfsPath("/dir1/file1")), new EventPredicate(EVT_IGFS_FILE_PURGED, new IgfsPath("/dir1/file1")), new EventPredicate(EVT_IGFS_FILE_DELETED, new IgfsPath("/dir1/file2")), new EventPredicate(EVT_IGFS_FILE_PURGED, new IgfsPath("/dir1/file2")));
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgfsEventsAbstractSelfTest method testSingleFileNestedDirs.
/**
* Checks events on CRUD operations on a single file in nested directories.
*
* @throws Exception If failed.
*/
public void testSingleFileNestedDirs() throws Exception {
final List<Event> evtList = new ArrayList<>();
final int evtsCnt = 6 + 1 + 1;
final CountDownLatch latch = new CountDownLatch(evtsCnt);
grid(1).events().localListen(lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
log.info("Received event [evt=" + evt + ']');
evtList.add(evt);
latch.countDown();
return true;
}
}, EVTS_IGFS);
IgfsPath dir = new IgfsPath("/dir1/dir2/dir3");
IgfsPath file = new IgfsPath(dir, "file1");
// Will generate 3 EVT_IGFS_DIR_CREATED + EVT_IGFS_FILE_CREATED + EVT_IGFS_FILE_OPENED_WRITE +
// EVT_IGFS_FILE_CLOSED and a number of EVT_IGFS_META_UPDATED.
igfs.create(file, true).close();
IgfsPath mvFile = new IgfsPath(dir, "mvFile1");
// Will generate EVT_IGFS_FILE_RENAMED.
igfs.rename(file, mvFile);
// Will generate EVT_IGFS_DIR_DELETED event.
assertTrue(igfs.delete(dir.parent(), true));
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals(evtsCnt, evtList.size());
IgfsEvent evt = (IgfsEvent) evtList.get(0);
assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1"), evt.path());
assertTrue(evt.isDirectory());
evt = (IgfsEvent) evtList.get(1);
assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
evt = (IgfsEvent) evtList.get(2);
assertEquals(EVT_IGFS_DIR_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1/dir2/dir3"), evt.path());
evt = (IgfsEvent) evtList.get(3);
assertEquals(EVT_IGFS_FILE_CREATED, evt.type());
assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
assertFalse(evt.isDirectory());
evt = (IgfsEvent) evtList.get(4);
assertEquals(EVT_IGFS_FILE_OPENED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
evt = (IgfsEvent) evtList.get(5);
assertEquals(EVT_IGFS_FILE_CLOSED_WRITE, evt.type());
assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
assertEquals(0, evt.dataSize());
evt = (IgfsEvent) evtList.get(6);
assertEquals(EVT_IGFS_FILE_RENAMED, evt.type());
assertEquals(new IgfsPath("/dir1/dir2/dir3/file1"), evt.path());
assertEquals(new IgfsPath("/dir1/dir2/dir3/mvFile1"), evt.newPath());
evt = (IgfsEvent) evtList.get(7);
assertEquals(EVT_IGFS_DIR_DELETED, evt.type());
assertEquals(new IgfsPath("/dir1/dir2"), evt.path());
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class ClusterGroupAbstractTest method testExecution.
/**
* @throws Exception If test failed.
*/
public void testExecution() throws Exception {
String name = "oneMoreGrid";
Collection<IgniteBiTuple<Ignite, IgnitePredicate<Event>>> lsnrs = new LinkedList<>();
try {
final AtomicInteger cnt = new AtomicInteger();
Ignite g = startGrid(name);
IgnitePredicate<Event> lsnr;
if (!IgniteCluster.class.isAssignableFrom(projection().getClass())) {
g.events().localListen(lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt.type() == EVT_JOB_STARTED;
assert false;
return true;
}
}, EVT_JOB_STARTED);
lsnrs.add(F.t(g, lsnr));
}
for (ClusterNode node : prj.nodes()) {
g = G.ignite(node.id());
g.events().localListen(lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt.type() == EVT_JOB_STARTED;
synchronized (mux) {
cnt.incrementAndGet();
mux.notifyAll();
}
return true;
}
}, EVT_JOB_STARTED);
lsnrs.add(F.t(g, lsnr));
}
run1(cnt);
run2(cnt);
call1(cnt);
call2(cnt);
call3(cnt);
call4(cnt);
call5(cnt);
forkjoin1(cnt);
forkjoin2(cnt);
exec1(cnt);
exec2(cnt);
executorService(cnt);
checkActiveFutures();
} finally {
for (IgniteBiTuple<Ignite, IgnitePredicate<Event>> t : lsnrs) t.get1().events().stopLocalListen(t.get2(), EVT_JOB_STARTED);
stopGrid(name);
}
}
Aggregations