use of org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper in project ignite by apache.
the class IgniteCommunicationBalanceTest method testBalance1.
/**
* @throws Exception If failed.
*/
@Test
public void testBalance1() throws Exception {
if (sslEnabled())
return;
System.setProperty(IgniteSystemProperties.IGNITE_IO_BALANCE_PERIOD, "5000");
try {
selectors = 4;
final int SRVS = 6;
startGridsMultiThreaded(SRVS);
final Ignite client = startClientGrid(SRVS);
for (int i = 0; i < SRVS; i++) {
ClusterNode node = client.cluster().node(ignite(i).cluster().localNode().id());
client.compute(client.cluster().forNode(node)).call(new DummyCallable(null));
}
waitNioBalanceStop(Collections.singletonList(client), 10_000);
final GridNioServer srv = ((GridNioServerWrapper) GridTestUtils.getFieldValue(client.configuration().getCommunicationSpi(), "nioSrvWrapper")).nio();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
long readMoveCnt1 = srv.readerMoveCount();
long writeMoveCnt1 = srv.writerMoveCount();
int prevNodeIdx = -1;
for (int iter = 0; iter < 10; iter++) {
int nodeIdx = rnd.nextInt(SRVS);
while (prevNodeIdx == nodeIdx) nodeIdx = rnd.nextInt(SRVS);
prevNodeIdx = nodeIdx;
log.info("Iteration [iter=" + iter + ", node=" + nodeIdx + ']');
final long readMoveCnt = readMoveCnt1;
final long writeMoveCnt = writeMoveCnt1;
final int nodeIdx0 = nodeIdx;
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
byte[] data = new byte[100_000];
for (int j = 0; j < 10; j++) {
for (int i = 0; i < SRVS; i++) {
ClusterNode node = client.cluster().node(ignite(i).cluster().localNode().id());
IgniteCompute compute = client.compute(client.cluster().forNode(node));
compute.call(new DummyCallable(i == nodeIdx0 ? data : null));
}
}
if (usePairedConnections())
return srv.readerMoveCount() > readMoveCnt && srv.writerMoveCount() > writeMoveCnt;
else
return srv.readerMoveCount() > readMoveCnt || srv.writerMoveCount() > writeMoveCnt;
}
}, 30_000);
waitNioBalanceStop(Collections.singletonList(client), 30_000);
long readMoveCnt2 = srv.readerMoveCount();
long writeMoveCnt2 = srv.writerMoveCount();
log.info("Move counts [rc1=" + readMoveCnt1 + ", wc1=" + writeMoveCnt1 + ", rc2=" + readMoveCnt2 + ", wc2=" + writeMoveCnt2 + ']');
if (usePairedConnections()) {
assertTrue(readMoveCnt2 > readMoveCnt1);
assertTrue(writeMoveCnt2 > writeMoveCnt1);
} else
assertTrue(readMoveCnt2 > readMoveCnt1 || writeMoveCnt2 > writeMoveCnt1);
readMoveCnt1 = readMoveCnt2;
writeMoveCnt1 = writeMoveCnt2;
}
waitNioBalanceStop(G.allGrids(), 10_000);
} finally {
System.setProperty(IgniteSystemProperties.IGNITE_IO_BALANCE_PERIOD, "");
}
}
use of org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper in project ignite by apache.
the class IgniteCommunicationBalanceTest method waitNioBalanceStop.
/**
* @param nodes Node.
* @param timeout Timeout.
* @throws Exception If failed.
*/
private void waitNioBalanceStop(List<Ignite> nodes, long timeout) throws Exception {
final List<GridNioServer> srvs = new ArrayList<>();
for (Ignite node : nodes) {
TcpCommunicationSpi spi = (TcpCommunicationSpi) node.configuration().getCommunicationSpi();
GridNioServer srv = ((GridNioServerWrapper) GridTestUtils.getFieldValue(spi, "nioSrvWrapper")).nio();
srvs.add(srv);
}
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@Override
public boolean applyx() throws IgniteCheckedException {
List<Long> rCnts = new ArrayList<>();
List<Long> wCnts = new ArrayList<>();
for (GridNioServer srv : srvs) {
long readerMovCnt1 = srv.readerMoveCount();
long writerMovCnt1 = srv.writerMoveCount();
rCnts.add(readerMovCnt1);
wCnts.add(writerMovCnt1);
}
U.sleep(2000);
for (int i = 0; i < srvs.size(); i++) {
GridNioServer srv = srvs.get(i);
long readerMovCnt1 = rCnts.get(i);
long writerMovCnt1 = wCnts.get(i);
long readerMovCnt2 = srv.readerMoveCount();
long writerMovCnt2 = srv.writerMoveCount();
if (readerMovCnt1 != readerMovCnt2) {
log.info("Readers balance is in progress [node=" + i + ", cnt1=" + readerMovCnt1 + ", cnt2=" + readerMovCnt2 + ']');
return false;
}
if (writerMovCnt1 != writerMovCnt2) {
log.info("Writers balance is in progress [node=" + i + ", cnt1=" + writerMovCnt1 + ", cnt2=" + writerMovCnt2 + ']');
return false;
}
}
return true;
}
}, timeout));
}
use of org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper in project ignite by apache.
the class TcpCommunicationSpi method spiStart.
/**
* {@inheritDoc}
*/
@Override
public void spiStart(String igniteInstanceName) throws IgniteSpiException {
final Function<UUID, ClusterNode> nodeGetter = (nodeId) -> getSpiContext().node(nodeId);
final Supplier<ClusterNode> locNodeSupplier = () -> getSpiContext().localNode();
final Supplier<Ignite> igniteExSupplier = this::ignite;
final Function<UUID, Boolean> pingNode = (nodeId) -> getSpiContext().pingNode(nodeId);
final Supplier<FailureProcessor> failureProcessorSupplier = () -> ignite instanceof IgniteEx ? ((IgniteEx) ignite).context().failure() : null;
final Supplier<Boolean> isStopped = () -> getSpiContext().isStopping();
this.igniteInstanceName = igniteInstanceName;
cfg.failureDetectionTimeout(ignite.configuration().getFailureDetectionTimeout());
attributeNames = new AttributeNames(createSpiAttributeName(ATTR_PAIRED_CONN), createSpiAttributeName(ATTR_ADDRS), createSpiAttributeName(ATTR_HOST_NAMES), createSpiAttributeName(ATTR_EXT_ADDRS), createSpiAttributeName(ATTR_PORT), createSpiAttributeName(ATTR_FORCE_CLIENT_SERVER_CONNECTIONS));
boolean client = Boolean.TRUE.equals(ignite().configuration().isClientMode());
this.stateProvider = new ClusterStateProvider(ignite, locNodeSupplier, this, isStopped, () -> super.getSpiContext(), log, igniteExSupplier);
try {
cfg.localHost(U.resolveLocalHost(cfg.localAddress()));
} catch (IOException e) {
throw new IgniteSpiException("Failed to initialize local address: " + cfg.localAddress(), e);
}
if (cfg.connectionsPerNode() > 1)
connPlc = new RoundRobinConnectionPolicy(cfg);
else
connPlc = new FirstConnectionPolicy();
this.srvLsnr = resolve(ignite, new InboundConnectionHandler(log, cfg, nodeGetter, locNodeSupplier, stateProvider, clientPool, commWorker, connectGate, failureProcessorSupplier, attributeNames, metricsLsnr, nioSrvWrapper, ctxInitLatch, client, igniteExSupplier, new CommunicationListener<Message>() {
@Override
public void onMessage(UUID nodeId, Message msg, IgniteRunnable msgC) {
notifyListener(nodeId, msg, msgC);
}
@Override
public void onDisconnected(UUID nodeId) {
if (lsnr != null)
lsnr.onDisconnected(nodeId);
}
}));
TcpHandshakeExecutor tcpHandshakeExecutor = resolve(ignite, new TcpHandshakeExecutor(log, stateProvider, cfg.directBuffer()));
this.nioSrvWrapper = resolve(ignite, new GridNioServerWrapper(log, cfg, attributeNames, tracing, nodeGetter, locNodeSupplier, connectGate, stateProvider, this::getExceptionRegistry, commWorker, ignite.configuration(), this.srvLsnr, getName(), getWorkersRegistry(ignite), ignite instanceof IgniteEx ? ((IgniteEx) ignite).context().metric() : null, this::createTcpClient, new CommunicationListenerEx<Message>() {
@Override
public void onMessage(UUID nodeId, Message msg, IgniteRunnable msgC) {
notifyListener(nodeId, msg, msgC);
}
@Override
public void onDisconnected(UUID nodeId) {
if (lsnr != null)
lsnr.onDisconnected(nodeId);
}
@Override
public void onChannelOpened(UUID rmtNodeId, Message initMsg, Channel channel) {
if (lsnr instanceof CommunicationListenerEx)
((CommunicationListenerEx<Message>) lsnr).onChannelOpened(rmtNodeId, initMsg, channel);
}
}, tcpHandshakeExecutor));
this.srvLsnr.setNioSrvWrapper(nioSrvWrapper);
this.clientPool = resolve(ignite, new ConnectionClientPool(cfg, attributeNames, log, metricsLsnr, locNodeSupplier, nodeGetter, null, getWorkersRegistry(ignite), this, stateProvider, nioSrvWrapper, getName()));
this.srvLsnr.setClientPool(clientPool);
nioSrvWrapper.clientPool(clientPool);
discoLsnr = new CommunicationDiscoveryEventListener(clientPool, metricsLsnr);
try {
// This method potentially resets local port to the value
// local node was bound to.
nioSrvWrapper.nio(nioSrvWrapper.resetNioServer());
} catch (IgniteCheckedException e) {
throw new IgniteSpiException("Failed to initialize TCP server: " + cfg.localHost(), e);
}
boolean forceClientToSrvConnections = forceClientToServerConnections() || cfg.localPort() == -1;
if (cfg.usePairedConnections() && forceClientToSrvConnections) {
throw new IgniteSpiException("Node using paired connections " + "is not allowed to start in forced client to server connections mode.");
}
assert cfg.localHost() != null;
// Start SPI start stopwatch.
startStopwatch();
if (log.isDebugEnabled()) {
log.debug(configInfo("locAddr", cfg.localAddress()));
log.debug(configInfo("locPort", cfg.localPort()));
log.debug(configInfo("locPortRange", cfg.localPortRange()));
log.debug(configInfo("idleConnTimeout", cfg.idleConnectionTimeout()));
log.debug(configInfo("directBuf", cfg.directBuffer()));
log.debug(configInfo("directSendBuf", cfg.directSendBuffer()));
log.debug(configInfo("selectorsCnt", cfg.selectorsCount()));
log.debug(configInfo("tcpNoDelay", cfg.tcpNoDelay()));
log.debug(configInfo("sockSndBuf", cfg.socketSendBuffer()));
log.debug(configInfo("sockRcvBuf", cfg.socketReceiveBuffer()));
log.debug(configInfo("msgQueueLimit", cfg.messageQueueLimit()));
log.debug(configInfo("connectionsPerNode", cfg.connectionsPerNode()));
if (failureDetectionTimeoutEnabled()) {
log.debug(configInfo("connTimeout", cfg.connectionTimeout()));
log.debug(configInfo("maxConnTimeout", cfg.maxConnectionTimeout()));
log.debug(configInfo("reconCnt", cfg.reconCount()));
} else
log.debug(configInfo("failureDetectionTimeout", failureDetectionTimeout()));
log.debug(configInfo("sockWriteTimeout", cfg.socketWriteTimeout()));
log.debug(configInfo("ackSndThreshold", cfg.ackSendThreshold()));
log.debug(configInfo("unackedMsgsBufSize", cfg.unackedMsgsBufferSize()));
}
if (!cfg.tcpNoDelay())
U.quietAndWarn(log, "'TCP_NO_DELAY' for communication is off, which should be used with caution " + "since may produce significant delays with some scenarios.");
if (cfg.slowClientQueueLimit() > 0 && cfg.messageQueueLimit() > 0 && cfg.slowClientQueueLimit() >= cfg.messageQueueLimit()) {
U.quietAndWarn(log, "Slow client queue limit is set to a value greater than or equal to message " + "queue limit (slow client queue limit will have no effect) [msgQueueLimit=" + cfg.messageQueueLimit() + ", slowClientQueueLimit=" + cfg.slowClientQueueLimit() + ']');
}
if (cfg.messageQueueLimit() == 0)
U.quietAndWarn(log, "Message queue limit is set to 0 which may lead to " + "potential OOMEs when running cache operations in FULL_ASYNC or PRIMARY_SYNC modes " + "due to message queues growth on sender and receiver sides.");
nioSrvWrapper.start();
this.commWorker = new CommunicationWorker(igniteInstanceName, log, cfg, attributeNames, clientPool, failureProcessorSupplier, nodeGetter, pingNode, this::getExceptionRegistry, nioSrvWrapper, getWorkersRegistry(ignite), getName());
this.srvLsnr.communicationWorker(commWorker);
this.nioSrvWrapper.communicationWorker(commWorker);
new IgniteSpiThread(igniteInstanceName, commWorker.name(), log) {
@Override
protected void body() {
commWorker.run();
}
}.start();
// Ack start.
if (log.isDebugEnabled())
log.debug(startInfo());
}
use of org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper in project ignite by apache.
the class IgniteSlowClientDetectionSelfTest method testSlowClient.
/**
* @throws Exception If failed.
*/
@Test
public void testSlowClient() throws Exception {
final IgniteEx slowClient = grid(nodeCount() - 1);
final ClusterNode slowClientNode = slowClient.localNode();
final CountDownLatch evtSegmentedLatch = new CountDownLatch(1);
slowClient.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assertEquals("Unexpected event: " + evt, evt.type(), EventType.EVT_NODE_SEGMENTED);
DiscoveryEvent evt0 = (DiscoveryEvent) evt;
assertEquals(slowClientNode, evt0.eventNode());
assertEquals(5L, evt0.topologyVersion());
evtSegmentedLatch.countDown();
return false;
}
}, EventType.EVT_NODE_SEGMENTED);
final CountDownLatch evtFailedLatch = new CountDownLatch(nodeCount() - 1);
for (int i = 0; i < nodeCount() - 1; i++) {
grid(i).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assertEquals("Unexpected event: " + evt, evt.type(), EventType.EVT_NODE_FAILED);
DiscoveryEvent evt0 = (DiscoveryEvent) evt;
assertEquals(slowClientNode, evt0.eventNode());
assertEquals(6L, evt0.topologyVersion());
assertEquals(4, evt0.topologyNodes().size());
evtFailedLatch.countDown();
return false;
}
}, EventType.EVT_NODE_FAILED);
}
assertTrue(slowClient.cluster().localNode().isClient());
IgniteCache<Object, Object> cache = slowClient.getOrCreateCache(PARTITIONED);
IgniteEx client0 = grid(nodeCount() - 2);
assertTrue(client0.cluster().localNode().isClient());
IgniteCache<Object, Object> cache0 = client0.getOrCreateCache(PARTITIONED);
cache.query(new ContinuousQuery<>().setLocalListener(new Listener()));
for (int i = 0; i < 100; i++) cache0.put(0, i);
GridIoManager ioMgr = slowClient.context().io();
TcpCommunicationSpi commSpi = (TcpCommunicationSpi) ((Object[]) U.field(ioMgr, "spis"))[0];
GridNioServer nioSrvr = ((GridNioServerWrapper) GridTestUtils.getFieldValue(commSpi, "nioSrvWrapper")).nio();
GridTestUtils.setFieldValue(nioSrvr, "skipRead", true);
// Initiate messages for client.
for (int i = 0; i < 100; i++) cache0.put(0, new byte[10 * 1024]);
boolean wait = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return Ignition.state(slowClient.name()) == IgniteState.STOPPED_ON_SEGMENTATION;
}
}, getTestTimeout());
assertTrue(wait);
assertTrue("Failed to wait for client failed event", evtFailedLatch.await(5000, MILLISECONDS));
assertTrue("Failed to wait for client segmented event", evtSegmentedLatch.await(5000, MILLISECONDS));
}
use of org.apache.ignite.spi.communication.tcp.internal.GridNioServerWrapper in project ignite by apache.
the class TxDeadlockOnEntryToStringTest method resolve.
/**
* The method reference implementation of {@link DependencyResolver}. It adds an additional behavior to {@link
* InboundConnectionHandler}.
*
* @param instance Delegated instance.
*/
private <T> T resolve(T instance) {
if (instance instanceof InboundConnectionHandler) {
InboundConnectionHandler hnd = (InboundConnectionHandler) instance;
return (T) (new InboundConnectionHandler(null, null, null, null, null, null, null, null, null, null, null, null, null, false, null, null) {
@Override
public void setNioSrvWrapper(GridNioServerWrapper nioSrvWrapper) {
hnd.setNioSrvWrapper(nioSrvWrapper);
}
@Override
public void setClientPool(ConnectionClientPool pool) {
hnd.setClientPool(pool);
}
@Override
public void onSessionWriteTimeout(GridNioSession ses) {
hnd.onSessionWriteTimeout(ses);
}
@Override
public void onConnected(GridNioSession ses) {
hnd.onConnected(ses);
}
@Override
public void onMessageSent(GridNioSession ses, Message msg) {
hnd.onMessageSent(ses, msg);
}
@Override
public void onMessage(GridNioSession ses, Message msg) {
if (rejectHandshake.get() && msg instanceof HandshakeMessage2) {
rejectHandshake.set(false);
ses.close();
return;
}
hnd.onMessage(ses, msg);
}
@Override
public void onFailure(FailureType failureType, Throwable failure) {
hnd.onFailure(failureType, failure);
}
@Override
public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
hnd.onDisconnected(ses, e);
}
@Override
public void stop() {
hnd.stop();
}
@Override
public void communicationWorker(CommunicationWorker commWorker) {
hnd.communicationWorker(commWorker);
}
@Override
public void onSessionIdleTimeout(GridNioSession ses) {
hnd.onSessionIdleTimeout(ses);
}
@Override
public void metricsListener(@Nullable TcpCommunicationMetricsListener metricsLsnr) {
hnd.metricsListener(metricsLsnr);
}
});
}
return instance;
}
Aggregations