use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.
the class JdbcThinTcpIo method handshake.
/**
* Used for versions: 2.1.5 and 2.3.0. The protocol version is changed but handshake format isn't changed.
*
* @param ver JDBC client version.
* @throws IOException On IO error.
* @throws SQLException On connection reject.
*/
private HandshakeResult handshake(ClientListenerProtocolVersion ver) throws IOException, SQLException {
BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), null);
BinaryMarshaller marsh = new BinaryMarshaller();
marsh.setContext(new MarshallerContextImpl(null, null));
ctx.configure(marsh);
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, new BinaryHeapOutputStream(HANDSHAKE_MSG_SIZE), null, null);
writer.writeByte((byte) ClientListenerRequest.HANDSHAKE);
writer.writeShort(ver.major());
writer.writeShort(ver.minor());
writer.writeShort(ver.maintenance());
writer.writeByte(ClientListenerNioListener.JDBC_CLIENT);
writer.writeBoolean(connProps.isDistributedJoins());
writer.writeBoolean(connProps.isEnforceJoinOrder());
writer.writeBoolean(connProps.isCollocated());
writer.writeBoolean(connProps.isReplicatedOnly());
writer.writeBoolean(connProps.isAutoCloseServerCursor());
writer.writeBoolean(connProps.isLazy());
writer.writeBoolean(connProps.isSkipReducerOnUpdate());
if (ver.compareTo(VER_2_7_0) >= 0)
writer.writeString(connProps.nestedTxMode());
if (ver.compareTo(VER_2_8_0) >= 0) {
writer.writeByte(nullableBooleanToByte(connProps.isDataPageScanEnabled()));
JdbcUtils.writeNullableInteger(writer, connProps.getUpdateBatchSize());
}
if (ver.compareTo(VER_2_9_0) >= 0) {
String userAttrs = connProps.getUserAttributesFactory();
if (F.isEmpty(userAttrs))
writer.writeMap(null);
else {
try {
Class<Factory<Map<String, String>>> cls = (Class<Factory<Map<String, String>>>) JdbcThinSSLUtil.class.getClassLoader().loadClass(userAttrs);
Map<String, String> attrs = cls.newInstance().create();
writer.writeMap(attrs);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new SQLException("Could not found user attributes factory class: " + userAttrs, SqlStateCode.CLIENT_CONNECTION_FAILED, e);
}
}
writer.writeByteArray(ThinProtocolFeature.featuresAsBytes(enabledFeatures()));
}
if (!F.isEmpty(connProps.getUsername())) {
assert ver.compareTo(VER_2_5_0) >= 0 : "Authentication is supported since 2.5";
writer.writeString(connProps.getUsername());
writer.writeString(connProps.getPassword());
}
send(writer.array());
BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new BinaryHeapInputStream(read()), null, null, false);
boolean accepted = reader.readBoolean();
if (accepted) {
HandshakeResult handshakeRes = new HandshakeResult();
if (reader.available() > 0) {
byte maj = reader.readByte();
byte min = reader.readByte();
byte maintenance = reader.readByte();
String stage = reader.readString();
long ts = reader.readLong();
byte[] hash = reader.readByteArray();
if (ver.compareTo(VER_2_8_0) >= 0)
handshakeRes.nodeId(reader.readUuid());
handshakeRes.igniteVersion(new IgniteProductVersion(maj, min, maintenance, stage, ts, hash));
if (ver.compareTo(VER_2_9_0) >= 0) {
byte[] srvFeatures = reader.readByteArray();
EnumSet<JdbcThinFeature> features = JdbcThinFeature.enumSet(srvFeatures);
handshakeRes.features(features);
}
} else {
handshakeRes.igniteVersion(new IgniteProductVersion((byte) 2, (byte) 0, (byte) 0, "Unknown", 0L, null));
}
handshakeRes.serverProtocolVersion(ver);
return handshakeRes;
} else {
short maj = reader.readShort();
short min = reader.readShort();
short maintenance = reader.readShort();
String err = reader.readString();
ClientListenerProtocolVersion srvProtoVer0 = ClientListenerProtocolVersion.create(maj, min, maintenance);
if (srvProtoVer0.compareTo(VER_2_5_0) < 0 && !F.isEmpty(connProps.getUsername())) {
throw new SQLException("Authentication doesn't support by remote server[driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + srvProtoVer0 + ", err=" + err + ", url=" + connProps.getUrl() + " address=" + sockAddr + ']', SqlStateCode.CONNECTION_REJECTED);
}
if (VER_2_8_0.equals(srvProtoVer0) || VER_2_7_0.equals(srvProtoVer0) || VER_2_5_0.equals(srvProtoVer0) || VER_2_4_0.equals(srvProtoVer0) || VER_2_3_0.equals(srvProtoVer0) || VER_2_1_5.equals(srvProtoVer0))
return handshake(srvProtoVer0);
else if (VER_2_1_0.equals(srvProtoVer0))
return handshake_2_1_0();
else {
throw new SQLException("Handshake failed [driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + srvProtoVer0 + ", err=" + err + ']', SqlStateCode.CONNECTION_REJECTED);
}
}
}
use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.
the class JdbcThinTcpIo method sendRequestRaw.
/**
* @param req Request.
* @throws IOException In case of IO error.
*/
private void sendRequestRaw(JdbcRequest req) throws IOException {
int cap = guessCapacity(req);
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, new BinaryHeapOutputStream(cap), BinaryThreadLocalContext.get().schemaHolder(), null);
req.writeBinary(writer, protoCtx);
synchronized (connMux) {
send(writer.array());
}
}
use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.
the class JdbcThinTcpIo method handshake_2_1_0.
/**
* Compatibility handshake for server version 2.1.0
*
* @throws IOException On IO error.
* @throws SQLException On connection reject.
*/
private HandshakeResult handshake_2_1_0() throws IOException, SQLException {
BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(HANDSHAKE_MSG_SIZE), null, null);
writer.writeByte((byte) ClientListenerRequest.HANDSHAKE);
writer.writeShort(VER_2_1_0.major());
writer.writeShort(VER_2_1_0.minor());
writer.writeShort(VER_2_1_0.maintenance());
writer.writeByte(ClientListenerNioListener.JDBC_CLIENT);
writer.writeBoolean(connProps.isDistributedJoins());
writer.writeBoolean(connProps.isEnforceJoinOrder());
writer.writeBoolean(connProps.isCollocated());
writer.writeBoolean(connProps.isReplicatedOnly());
writer.writeBoolean(connProps.isAutoCloseServerCursor());
send(writer.array());
BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
boolean accepted = reader.readBoolean();
if (accepted) {
HandshakeResult handshakeRes = new HandshakeResult();
handshakeRes.igniteVersion(new IgniteProductVersion((byte) 2, (byte) 1, (byte) 0, "Unknown", 0L, null));
handshakeRes.serverProtocolVersion(VER_2_1_0);
return handshakeRes;
} else {
short maj = reader.readShort();
short min = reader.readShort();
short maintenance = reader.readShort();
String err = reader.readString();
ClientListenerProtocolVersion ver = ClientListenerProtocolVersion.create(maj, min, maintenance);
throw new SQLException("Handshake failed [driverProtocolVer=" + CURRENT_VER + ", remoteNodeProtocolVer=" + ver + ", err=" + err + ']', SqlStateCode.CONNECTION_REJECTED);
}
}
use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.
the class ClientMessageParser method encode.
/**
* {@inheritDoc}
*/
@Override
public ClientMessage encode(ClientListenerResponse resp) {
assert resp != null;
BinaryHeapOutputStream outStream = new BinaryHeapOutputStream(32, BinaryMemoryAllocator.POOLED.chunk());
BinaryRawWriterEx writer = marsh.writer(outStream);
assert resp instanceof ClientOutgoingMessage : "Unexpected response type: " + resp.getClass();
((ClientOutgoingMessage) resp).encode(ctx, writer);
return new ClientMessage(outStream);
}
use of org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream in project ignite by apache.
the class BinaryMarshallerSelfTest method testReadDetachObjectProperly.
/**
* Perform action on binary object after unmarshalling from offheap data, when offheap memory chunk cleared.
*
* @param obj Object to marshal-unmarshal.
* @param action Action to perform on object.
* @throws Exception If failed.
*/
private void testReadDetachObjectProperly(Object obj, IgniteThrowableConsumer<Object> action, boolean deserialize) throws Exception {
BinaryMarshaller marsh = binaryMarshaller();
BinaryHeapOutputStream os = new BinaryHeapOutputStream(1024);
BinaryWriterExImpl writer = marsh.binaryMarshaller().writer(os);
writer.writeObject(obj);
BinaryHeapInputStream is = new BinaryHeapInputStream(os.array());
BinaryReaderExImpl reader = marsh.binaryMarshaller().reader(is);
Object bObj = reader.readObjectDetached(deserialize);
Arrays.fill(os.array(), (byte) 0);
action.accept(bObj);
}
Aggregations