use of java.nio.channels.Channel in project ignite by apache.
the class GridIoManager method start.
/**
* {@inheritDoc}
*/
@Override
public void start() throws IgniteCheckedException {
ctx.addNodeAttribute(DIRECT_PROTO_VER_ATTR, DIRECT_PROTO_VER);
MessageFormatter[] formatterExt = ctx.plugins().extensions(MessageFormatter.class);
if (formatterExt != null && formatterExt.length > 0) {
if (formatterExt.length > 1)
throw new IgniteCheckedException("More than one MessageFormatter extension is defined. Check your " + "plugins configuration and make sure that only one of them provides custom message format.");
formatter = formatterExt[0];
} else {
formatter = new MessageFormatter() {
@Override
public MessageWriter writer(UUID rmtNodeId) throws IgniteCheckedException {
assert rmtNodeId != null;
return new DirectMessageWriter(U.directProtocolVersion(ctx, rmtNodeId));
}
@Override
public MessageReader reader(UUID rmtNodeId, MessageFactory msgFactory) throws IgniteCheckedException {
return new DirectMessageReader(msgFactory, rmtNodeId != null ? U.directProtocolVersion(ctx, rmtNodeId) : DIRECT_PROTO_VER);
}
};
}
MessageFactory[] msgs = ctx.plugins().extensions(MessageFactory.class);
if (msgs == null)
msgs = EMPTY;
List<MessageFactory> compMsgs = new ArrayList<>();
compMsgs.add(new GridIoMessageFactory());
for (IgniteComponentType compType : IgniteComponentType.values()) {
MessageFactory f = compType.messageFactory();
if (f != null)
compMsgs.add(f);
}
if (!compMsgs.isEmpty())
msgs = F.concat(msgs, compMsgs.toArray(new MessageFactory[compMsgs.size()]));
msgFactory = new IgniteMessageFactoryImpl(msgs);
CommunicationSpi<Serializable> spi = getSpi();
if ((CommunicationSpi<?>) spi instanceof TcpCommunicationSpi)
getTcpCommunicationSpi().setConnectionRequestor(invConnHandler);
startSpi();
MetricRegistry ioMetric = ctx.metric().registry(COMM_METRICS);
ioMetric.register(OUTBOUND_MSG_QUEUE_CNT, spi::getOutboundMessagesQueueSize, "Outbound messages queue size.");
ioMetric.register(SENT_MSG_CNT, spi::getSentMessagesCount, "Sent messages count.");
ioMetric.register(SENT_BYTES_CNT, spi::getSentBytesCount, "Sent bytes count.");
ioMetric.register(RCVD_MSGS_CNT, spi::getReceivedMessagesCount, "Received messages count.");
ioMetric.register(RCVD_BYTES_CNT, spi::getReceivedBytesCount, "Received bytes count.");
getSpi().setListener(commLsnr = new CommunicationListenerEx<Serializable>() {
@Override
public void onMessage(UUID nodeId, Serializable msg, IgniteRunnable msgC) {
try {
onMessage0(nodeId, (GridIoMessage) msg, msgC);
} catch (ClassCastException ignored) {
U.error(log, "Communication manager received message of unknown type (will ignore): " + msg.getClass().getName() + ". Most likely GridCommunicationSpi is being used directly, " + "which is illegal - make sure to send messages only via GridProjection API.");
}
}
@Override
public void onDisconnected(UUID nodeId) {
for (GridDisconnectListener lsnr : disconnectLsnrs) lsnr.onNodeDisconnected(nodeId);
}
@Override
public void onChannelOpened(UUID rmtNodeId, Serializable initMsg, Channel channel) {
try {
onChannelOpened0(rmtNodeId, (GridIoMessage) initMsg, channel);
} catch (ClassCastException ignored) {
U.error(log, "Communication manager received message of unknown type (will ignore): " + initMsg.getClass().getName() + ". Most likely GridCommunicationSpi is being used directly, " + "which is illegal - make sure to send messages only via GridProjection API.");
}
}
});
if (log.isDebugEnabled())
log.debug(startInfo());
addMessageListener(GridTopic.TOPIC_IO_TEST, new GridMessageListener() {
@Override
public void onMessage(UUID nodeId, Object msg, byte plc) {
ClusterNode node = ctx.discovery().node(nodeId);
if (node == null)
return;
IgniteIoTestMessage msg0 = (IgniteIoTestMessage) msg;
msg0.senderNodeId(nodeId);
if (msg0.request()) {
IgniteIoTestMessage res = new IgniteIoTestMessage(msg0.id(), false, null);
res.flags(msg0.flags());
res.onRequestProcessed();
res.copyDataFromRequest(msg0);
try {
sendToGridTopic(node, GridTopic.TOPIC_IO_TEST, res, GridIoPolicy.SYSTEM_POOL);
} catch (IgniteCheckedException e) {
U.error(log, "Failed to send IO test response [msg=" + msg0 + "]", e);
}
} else {
IoTestFuture fut = ioTestMap().get(msg0.id());
msg0.onResponseProcessed();
if (fut == null)
U.warn(log, "Failed to find IO test future [msg=" + msg0 + ']');
else
fut.onResponse(msg0);
}
}
});
}
use of java.nio.channels.Channel in project ignite by apache.
the class IgniteWalIteratorSwitchSegmentTest method checkSwitchReadingSegmentDuringIteration.
/**
* @param serVer WAL serializer version.
* @throws Exception If some thing failed.
*/
private void checkSwitchReadingSegmentDuringIteration(int serVer) throws Exception {
String workDir = U.defaultWorkDirectory();
T2<IgniteWriteAheadLogManager, RecordSerializer> initTup = initiate(serVer, workDir);
IgniteWriteAheadLogManager walMgr = initTup.get1();
RecordSerializer recordSerializer = initTup.get2();
MetastoreDataRecord rec = new MetastoreDataRecord("0", new byte[100]);
int recSize = recordSerializer.size(rec);
// Add more record for rollover to the next segment.
int recordsToWrite = SEGMENT_SIZE / recSize + 100;
SegmentAware segmentAware = GridTestUtils.getFieldValue(walMgr, "segmentAware");
// Guard from archiving before iterator would be created.
assertTrue(segmentAware.lock(0));
for (int i = 0; i < recordsToWrite; i++) walMgr.log(new MetastoreDataRecord(rec.key(), rec.value()));
walMgr.flush(null, true);
AtomicInteger actualRecords = new AtomicInteger(0);
AtomicReference<String> startedSegmentPath = new AtomicReference<>();
AtomicReference<String> finishedSegmentPath = new AtomicReference<>();
CountDownLatch startedIterLatch = new CountDownLatch(1);
CountDownLatch finishedArchivedLatch = new CountDownLatch(1);
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
// Check that switch segment works as expected and all record is reachable.
try (WALIterator it = walMgr.replay(null)) {
Object handle = getFieldValueHierarchy(it, "currWalSegment");
FileInput in = getFieldValueHierarchy(handle, "in");
Object delegate = getFieldValueHierarchy(in.io(), "delegate");
Channel ch = getFieldValueHierarchy(delegate, "ch");
String path = getFieldValueHierarchy(ch, "path");
startedSegmentPath.set(path);
startedIterLatch.countDown();
while (it.hasNext()) {
IgniteBiTuple<WALPointer, WALRecord> tup = it.next();
WALRecord rec0 = tup.get2();
if (rec0.type() == METASTORE_DATA_RECORD)
actualRecords.incrementAndGet();
finishedArchivedLatch.await();
}
in = getFieldValueHierarchy(handle, "in");
delegate = getFieldValueHierarchy(in.io(), "delegate");
ch = getFieldValueHierarchy(delegate, "ch");
path = getFieldValueHierarchy(ch, "path");
finishedSegmentPath.set(path);
}
return null;
});
startedIterLatch.await();
segmentAware.unlock(0);
waitForCondition(() -> segmentAware.lastArchivedAbsoluteIndex() == 0, 5000);
finishedArchivedLatch.countDown();
fut.get();
// should started iteration from work directory but finish from archive directory.
assertEquals(workDir + WORK_SUB_DIR + File.separator + "0000000000000000.wal", startedSegmentPath.get());
assertEquals(workDir + ARCHIVE_SUB_DIR + File.separator + "0000000000000000.wal", finishedSegmentPath.get());
Assert.assertEquals("Not all records read during iteration.", recordsToWrite, actualRecords.get());
}
use of java.nio.channels.Channel in project ignite by apache.
the class InboundConnectionHandler method onMessage.
/**
* {@inheritDoc}
*/
@Override
public void onMessage(final GridNioSession ses, Message msg) {
Span span = MTC.span();
span.addLog(() -> "Communication received");
span.addTag(SpanTags.MESSAGE, () -> traceName(msg));
ConnectionKey connKey = ses.meta(CONN_IDX_META);
if (connKey == null) {
assert ses.accepted() : ses;
if (!connectGate.tryEnter()) {
if (log.isDebugEnabled())
log.debug("Close incoming connection, failed to enter gateway.");
ses.send(new RecoveryLastReceivedMessage(NODE_STOPPING)).listen(fut -> ses.close());
return;
}
try {
onFirstMessage(ses, msg);
} finally {
connectGate.leave();
}
} else {
if (isChannelConnIdx(connKey.connectionIndex())) {
if (ses.meta(CHANNEL_FUT_META) == null)
nioSrvWrapper.onChannelCreate((GridSelectorNioSessionImpl) ses, connKey, msg);
else {
GridFutureAdapter<Channel> fut = ses.meta(CHANNEL_FUT_META);
GridSelectorNioSessionImpl ses0 = (GridSelectorNioSessionImpl) ses;
ses0.closeSocketOnSessionClose(false);
ses0.close().listen(f -> {
if (f.error() != null) {
fut.onDone(f.error());
return;
}
fut.onDone(ses0.key().channel());
});
}
return;
}
Object consistentId = ses.meta(CONSISTENT_ID_META);
assert consistentId != null;
if (msg instanceof RecoveryLastReceivedMessage) {
metricsLsnr.onMessageReceived(msg, consistentId);
GridNioRecoveryDescriptor recovery = ses.outRecoveryDescriptor();
if (recovery != null) {
RecoveryLastReceivedMessage msg0 = (RecoveryLastReceivedMessage) msg;
if (log.isDebugEnabled()) {
log.debug("Received recovery acknowledgement [rmtNode=" + connKey.nodeId() + ", connIdx=" + connKey.connectionIndex() + ", rcvCnt=" + msg0.received() + ']');
}
recovery.ackReceived(msg0.received());
}
return;
} else {
GridNioRecoveryDescriptor recovery = ses.inRecoveryDescriptor();
if (recovery != null) {
long rcvCnt = recovery.onReceived();
if (rcvCnt % cfg.ackSendThreshold() == 0) {
if (log.isDebugEnabled()) {
log.debug("Send recovery acknowledgement [rmtNode=" + connKey.nodeId() + ", connIdx=" + connKey.connectionIndex() + ", rcvCnt=" + rcvCnt + ']');
}
ses.systemMessage(new RecoveryLastReceivedMessage(rcvCnt));
recovery.lastAcknowledged(rcvCnt);
}
} else if (connKey.dummy()) {
assert msg instanceof NodeIdMessage : msg;
TcpCommunicationNodeConnectionCheckFuture fut = ses.meta(SES_FUT_META);
assert fut != null : msg;
fut.onConnected(U.bytesToUuid(((NodeIdMessage) msg).nodeIdBytes(), 0));
nioSrvWrapper.nio().closeFromWorkerThread(ses);
return;
}
}
metricsLsnr.onMessageReceived(msg, consistentId);
IgniteRunnable c;
if (cfg.messageQueueLimit() > 0) {
GridNioMessageTracker tracker = ses.meta(TRACKER_META);
if (tracker == null) {
GridNioMessageTracker old = ses.addMeta(TRACKER_META, tracker = new GridNioMessageTracker(ses, cfg.messageQueueLimit()));
assert old == null;
}
tracker.onMessageReceived();
c = tracker;
} else
c = NOOP;
lsnr.onMessage(connKey.nodeId(), msg, c);
}
}
Aggregations