use of org.apache.ignite.internal.binary.streams.BinaryOutputStream in project ignite by apache.
the class TimeoutTest method testServerClosesThinClientConnectionOnHandshakeTimeout.
/**
* Test that server closes thin client connection in case of handshake timeout.
*/
@Test
public void testServerClosesThinClientConnectionOnHandshakeTimeout() {
try (Ignite ignite = startGrid(0)) {
long ts0 = System.currentTimeMillis();
Socket s = new Socket();
s.connect(new InetSocketAddress(clientHost(ignite.cluster().localNode()), clientPort(ignite.cluster().localNode())), 0);
s.setSoTimeout(TIMEOUT * 2);
OutputStream os = s.getOutputStream();
try (BinaryOutputStream bos = new BinaryHeapOutputStream(32)) {
// Size.
bos.writeInt(1000);
os.write(bos.arrayCopy());
os.flush();
InputStream is = s.getInputStream();
// Connection and stream closed by server after timeout.
assertEquals(-1, is.read());
long ts1 = System.currentTimeMillis();
assertTrue("Unexpected timeout [ts0=" + ts0 + ", ts1=" + ts1 + ']', ts1 - ts0 >= TIMEOUT && ts1 - ts0 < TIMEOUT * 2);
} finally {
s.close();
}
} catch (Exception e) {
fail("Exception while sending message: " + e.getMessage());
}
}
use of org.apache.ignite.internal.binary.streams.BinaryOutputStream in project ignite by apache.
the class TcpClientChannel method send.
/**
* @param op Operation.
* @param payloadWriter Payload writer to stream or {@code null} if request has no payload.
* @return Request future.
*/
private ClientRequestFuture send(ClientOperation op, Consumer<PayloadOutputChannel> payloadWriter) throws ClientException {
long id = reqId.getAndIncrement();
PayloadOutputChannel payloadCh = new PayloadOutputChannel(this);
try {
if (closed())
throw new ClientConnectionException("Channel is closed");
ClientRequestFuture fut = new ClientRequestFuture();
pendingReqs.put(id, fut);
BinaryOutputStream req = payloadCh.out();
// Reserve an integer for the request size.
req.writeInt(0);
req.writeShort(op.code());
req.writeLong(id);
if (payloadWriter != null)
payloadWriter.accept(payloadCh);
// Actual size.
req.writeInt(0, req.position() - 4);
write(req.array(), req.position(), payloadCh::close);
return fut;
} catch (Throwable t) {
pendingReqs.remove(id);
// Potential double-close is handled in PayloadOutputChannel.
payloadCh.close();
throw t;
}
}
use of org.apache.ignite.internal.binary.streams.BinaryOutputStream in project ignite by apache.
the class ClientCacheAffinityMapping method writeRequest.
/**
* Writes caches affinity request to the output channel.
*
* @param ch Output channel.
* @param cacheIds Cache IDs.
*/
public static void writeRequest(PayloadOutputChannel ch, Collection<Integer> cacheIds) {
BinaryOutputStream out = ch.out();
out.writeInt(cacheIds.size());
for (int cacheId : cacheIds) out.writeInt(cacheId);
}
use of org.apache.ignite.internal.binary.streams.BinaryOutputStream in project ignite by apache.
the class TcpClientCache method writeCacheInfo.
/**
* Write cache ID and flags.
*/
private void writeCacheInfo(PayloadOutputChannel payloadCh) {
BinaryOutputStream out = payloadCh.out();
out.writeInt(cacheId);
byte flags = keepBinary ? KEEP_BINARY_FLAG_MASK : 0;
TcpClientTransaction tx = transactions.tx();
if (expiryPlc != null) {
ProtocolContext protocolCtx = payloadCh.clientChannel().protocolCtx();
if (!protocolCtx.isFeatureSupported(EXPIRY_POLICY)) {
throw new ClientProtocolError(String.format("Expire policies are not supported by the server " + "version %s, required version %s", protocolCtx.version(), EXPIRY_POLICY.verIntroduced()));
}
flags |= WITH_EXPIRY_POLICY_FLAG_MASK;
}
if (tx != null) {
if (tx.clientChannel() != payloadCh.clientChannel()) {
throw new ClientException("Transaction context has been lost due to connection errors. " + "Cache operations are prohibited until current transaction closed.");
}
flags |= TRANSACTIONAL_FLAG_MASK;
}
out.writeByte(flags);
if ((flags & WITH_EXPIRY_POLICY_FLAG_MASK) != 0) {
out.writeLong(convertDuration(expiryPlc.getExpiryForCreation()));
out.writeLong(convertDuration(expiryPlc.getExpiryForUpdate()));
out.writeLong(convertDuration(expiryPlc.getExpiryForAccess()));
}
if ((flags & TRANSACTIONAL_FLAG_MASK) != 0)
out.writeInt(tx.txId());
}
use of org.apache.ignite.internal.binary.streams.BinaryOutputStream in project ignite by apache.
the class ClientCacheEntryListenerHandler method startListen.
/**
* Send request to the server and start
*/
public synchronized void startListen(CacheEntryUpdatedListener<K, V> locLsnr, ClientDisconnectListener disconnectLsnr, Factory<? extends CacheEntryEventFilter<? super K, ? super V>> rmtFilterFactory, int pageSize, long timeInterval, boolean includeExpired) {
assert locLsnr != null;
if (clientCh != null)
throw new IllegalStateException("Listener was already started");
this.locLsnr = locLsnr;
this.disconnectLsnr = disconnectLsnr;
Consumer<PayloadOutputChannel> qryWriter = payloadCh -> {
BinaryOutputStream out = payloadCh.out();
out.writeInt(ClientUtils.cacheId(jCacheAdapter.getName()));
out.writeByte(keepBinary ? KEEP_BINARY_FLAG_MASK : 0);
out.writeInt(pageSize);
out.writeLong(timeInterval);
out.writeBoolean(includeExpired);
if (rmtFilterFactory == null)
out.writeByte(GridBinaryMarshaller.NULL);
else {
utils.writeObject(out, rmtFilterFactory);
out.writeByte(JAVA_PLATFORM);
}
};
Function<PayloadInputChannel, T2<ClientChannel, Long>> qryReader = payloadCh -> {
ClientChannel ch = payloadCh.clientChannel();
Long rsrcId = payloadCh.in().readLong();
ch.addNotificationListener(CONTINUOUS_QUERY_EVENT, rsrcId, this);
return new T2<>(ch, rsrcId);
};
try {
T2<ClientChannel, Long> params = ch.service(ClientOperation.QUERY_CONTINUOUS, qryWriter, qryReader);
clientCh = params.get1();
rsrcId = params.get2();
} catch (ClientError e) {
throw new ClientException(e);
}
}
Aggregations