AMashenkov commented on a change in pull request #191: URL: https://github.com/apache/ignite-3/pull/191#discussion_r674729985
########## File path: modules/client-handler/src/main/java/org/apache/ignite/client/handler/package-info.java ########## @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Ignite thin client handler (server-side connector). + */ +package org.apache.ignite.client.handler; Review comment: ```suggestion package org.apache.ignite.client.handler; ``` ########## File path: modules/client-common/src/main/java/org/apache/ignite/client/package-info.java ########## @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Ignite thin client shared logic (client and server: serialization, op codes, etc). + */ +package org.apache.ignite.client; Review comment: ```suggestion package org.apache.ignite.client; ``` ########## File path: modules/client-common/src/main/java/org/apache/ignite/client/ClientMsgPackType.java ########## @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.client; + +/** + * Ignite-specific extension type codes. Review comment: It is unclear why these types are in separate class and seems have separate codes starting for 1. Is my understanding correct that there is no native support for these types in MsgPack lib? so, these codes uses are registered/used in kind of extended type system inside MsgPack. thus, any additional type must be added to this class only, to avoid any confusions? ########## File path: modules/client-common/src/main/java/org/apache/ignite/client/package-info.java ########## @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Ignite thin client shared logic (client and server: serialization, op codes, etc). + */ +package org.apache.ignite.client; Review comment: Also, if we intend to have a Java client as well, "client" package will be more suitable place for the Java client classes. Protocol common classe can be moved to "org.apache.ignite.client.proto" package. WDYT? ########## File path: modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientInboundMessageHandler.java ########## @@ -0,0 +1,557 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.client.handler; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import org.apache.ignite.app.Ignite; +import org.apache.ignite.client.ClientDataType; +import org.apache.ignite.client.ClientErrorCode; +import org.apache.ignite.client.ClientMessagePacker; +import org.apache.ignite.client.ClientMessageType; +import org.apache.ignite.client.ClientMessageUnpacker; +import org.apache.ignite.client.ClientOp; +import org.apache.ignite.client.ProtocolVersion; +import org.apache.ignite.configuration.schemas.table.TableChange; +import org.apache.ignite.internal.schema.Column; +import org.apache.ignite.internal.schema.NativeTypeSpec; +import org.apache.ignite.internal.schema.SchemaAware; +import org.apache.ignite.internal.schema.SchemaDescriptor; +import org.apache.ignite.internal.table.IgniteTablesInternal; +import org.apache.ignite.internal.table.TableImpl; +import org.apache.ignite.internal.table.TupleBuilderImpl; +import org.apache.ignite.lang.IgniteException; +import org.apache.ignite.table.Table; +import org.apache.ignite.table.Tuple; +import org.msgpack.core.MessageFormat; +import org.msgpack.core.buffer.ByteBufferInput; +import org.slf4j.Logger; + +import java.io.IOException; +import java.math.BigDecimal; +import java.nio.ByteBuffer; +import java.util.BitSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; + +/** + * Handles messages from thin clients. + */ +@SuppressWarnings({"rawtypes", "unchecked"}) +public class ClientInboundMessageHandler extends ChannelInboundHandlerAdapter { + /** Logger. */ + private final Logger log; + + /** API entry point. */ + private final Ignite ignite; + + /** Context. */ + private ClientContext clientContext; + + /** + * Constructor. + * + * @param ignite Ignite API entry point. + * @param log Logger. + */ + public ClientInboundMessageHandler(Ignite ignite, Logger log) { + assert ignite != null; + assert log != null; + + this.ignite = ignite; + this.log = log; + } + + /** {@inheritDoc} */ + @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException { + var buf = (ByteBuffer) msg; + + var unpacker = getUnpacker(buf); + var packer = getPacker(); + + if (clientContext == null) + handshake(ctx, unpacker, packer); + else + processOperation(ctx, unpacker, packer); + } + + private void handshake(ChannelHandlerContext ctx, ClientMessageUnpacker unpacker, ClientMessagePacker packer) + throws IOException { + try { + var clientVer = ProtocolVersion.unpack(unpacker); + + if (!clientVer.equals(ProtocolVersion.LATEST_VER)) + throw new IgniteException("Unsupported version: " + + clientVer.major() + "." + clientVer.minor() + "." + clientVer.patch()); + + var clientCode = unpacker.unpackInt(); + var featuresLen = unpacker.unpackBinaryHeader(); + var features = BitSet.valueOf(unpacker.readPayload(featuresLen)); + + clientContext = new ClientContext(clientVer, clientCode, features); + + log.debug("Handshake: " + clientContext); + + var extensionsLen = unpacker.unpackMapHeader(); + unpacker.skipValue(extensionsLen); + + // Response. + ProtocolVersion.LATEST_VER.pack(packer); + + packer.packInt(ClientErrorCode.SUCCESS) + .packBinaryHeader(0) // Features. + .packMapHeader(0); // Extensions. + + write(packer, ctx); + } catch (Throwable t) { + packer = getPacker(); + + ProtocolVersion.LATEST_VER.pack(packer); + packer.packInt(ClientErrorCode.FAILED).packString(t.getMessage()); + + write(packer, ctx); + } + } + + private void write(ClientMessagePacker packer, ChannelHandlerContext ctx) { + var buf = packer.toMessageBuffer().sliceAsByteBuffer(); + + ctx.writeAndFlush(buf); + } + + private void writeError(int requestId, Throwable err, ChannelHandlerContext ctx) { + try { + assert err != null; + + ClientMessagePacker packer = getPacker(); + packer.packInt(ClientMessageType.RESPONSE); + packer.packInt(requestId); + packer.packInt(ClientErrorCode.FAILED); + + String msg = err.getMessage(); + + if (msg == null) + msg = err.getClass().getName(); + + packer.packString(msg); + + write(packer, ctx); + } catch (Throwable t) { + exceptionCaught(ctx, t); + } + } + + private ClientMessagePacker getPacker() { + return new ClientMessagePacker(); + } + + private ClientMessageUnpacker getUnpacker(ByteBuffer buf) { + return new ClientMessageUnpacker(new ByteBufferInput(buf)); + } + + private void processOperation(ChannelHandlerContext ctx, ClientMessageUnpacker unpacker, ClientMessagePacker packer) throws IOException { + var opCode = unpacker.unpackInt(); + var requestId = unpacker.unpackInt(); + + packer.packInt(ClientMessageType.RESPONSE) + .packInt(requestId) + .packInt(ClientErrorCode.SUCCESS); + + try { + var fut = processOperation(unpacker, packer, opCode); + + if (fut == null) { + // Operation completed synchronously. + write(packer, ctx); + } else { + fut.whenComplete((Object res, Object err) -> { + if (err != null) + writeError(requestId, (Throwable) err, ctx); + else + write(packer, ctx); + }); + } + + } catch (Throwable t) { + writeError(requestId, t, ctx); + } + } + + private CompletableFuture processOperation(ClientMessageUnpacker unpacker, ClientMessagePacker packer, int opCode) + throws IOException { + // TODO: Handle all operations asynchronously (add async table API). + switch (opCode) { + case ClientOp.TABLE_CREATE: { + TableImpl table = createTable(unpacker); + + packer.packUuid(table.tableId()); + + break; + } + + case ClientOp.TABLE_DROP: { + var tableName = unpacker.unpackString(); + + ignite.tables().dropTable(tableName); + + break; + } + + case ClientOp.TABLES_GET: { + List<Table> tables = ignite.tables().tables(); + + packer.packMapHeader(tables.size()); + + for (var table : tables) { + var tableImpl = (TableImpl) table; + + packer.packUuid(tableImpl.tableId()); + packer.packString(table.tableName()); + } + + break; + } + + case ClientOp.SCHEMAS_GET: { + var table = readTable(unpacker); + + if (unpacker.getNextFormat() == MessageFormat.NIL) { + // Return the latest schema. + packer.packMapHeader(1); + + var schema = table.schemaView().schema(); + + if (schema == null) + throw new IgniteException("Schema registry is not initialized."); + + writeSchema(packer, schema.version(), schema); + } else { + var cnt = unpacker.unpackArrayHeader(); + packer.packMapHeader(cnt); + + for (var i = 0; i < cnt; i++) { + var schemaVer = unpacker.unpackInt(); + var schema = table.schemaView().schema(schemaVer); + writeSchema(packer, schemaVer, schema); + } + } + + break; + } + + case ClientOp.TABLE_GET: { + String tableName = unpacker.unpackString(); + Table table = ignite.tables().table(tableName); + + if (table == null) + packer.packNil(); + else + packer.packUuid(((TableImpl) table).tableId()); + + break; + } + + case ClientOp.TUPLE_UPSERT: { + var table = readTable(unpacker); + var tuple = readTuple(unpacker, table, false); + + return table.upsertAsync(tuple); + } + + case ClientOp.TUPLE_UPSERT_SCHEMALESS: { + var table = readTable(unpacker); + var tuple = readTupleSchemaless(unpacker, table); + + return table.upsertAsync(tuple); + } + + case ClientOp.TUPLE_GET: { + var table = readTable(unpacker); + var keyTuple = readTuple(unpacker, table, true); + + return table.getAsync(keyTuple).thenAccept(t -> writeTuple(packer, t)); + } + + default: + throw new IgniteException("Unexpected operation code: " + opCode); + } + + return null; + } + + private void writeSchema(ClientMessagePacker packer, int schemaVer, SchemaDescriptor schema) throws IOException { + packer.packInt(schemaVer); + + if (schema == null) { + packer.packNil(); + return; + } + + var colCnt = schema.columnNames().size(); + packer.packArrayHeader(colCnt); + + for (var colIdx = 0; colIdx < colCnt; colIdx++) { + var col = schema.column(colIdx); + + packer.packArrayHeader(4); + packer.packString(col.name()); + packer.packInt(getClientDataType(col.type().spec())); + packer.packBoolean(schema.isKeyColumn(colIdx)); + packer.packBoolean(col.nullable()); + } + } + + private void writeTuple(ClientMessagePacker packer, Tuple tuple) { + try { + if (tuple == null) { + packer.packNil(); + + return; + } + + var schema = ((SchemaAware) tuple).schema(); + + packer.packInt(schema.version()); + + for (var col : schema.keyColumns().columns()) + writeColumnValue(packer, tuple, col); + + for (var col : schema.valueColumns().columns()) + writeColumnValue(packer, tuple, col); + } catch (Throwable t) { + throw new IgniteException("Failed to serialize tuple", t); + } + } + + private Tuple readTuple(ClientMessageUnpacker unpacker, TableImpl table, boolean keyOnly) throws IOException { + var schemaId = unpacker.unpackInt(); + var schema = table.schemaView().schema(schemaId); + var builder = (TupleBuilderImpl) table.tupleBuilder(); + + var cnt = keyOnly ? schema.keyColumns().length() : schema.length(); + + for (int i = 0; i < cnt; i++) { + if (unpacker.getNextFormat() == MessageFormat.NIL) { + unpacker.skipValue(); + continue; + } + + readAndSetColumnValue(unpacker, builder, schema.column(i)); + } + + return builder.build(); + } + + private Tuple readTupleSchemaless(ClientMessageUnpacker unpacker, TableImpl table) throws IOException { + var cnt = unpacker.unpackMapHeader(); + var builder = table.tupleBuilder(); + + for (int i = 0; i < cnt; i++) { + var colName = unpacker.unpackString(); + + builder.set(colName, unpacker.unpackValue()); + } + + return builder.build(); + } + + private TableImpl readTable(ClientMessageUnpacker unpacker) throws IOException { + var tableId = unpacker.unpackUuid(); + return ((IgniteTablesInternal)ignite.tables()).table(tableId); + } + + private void readAndSetColumnValue(ClientMessageUnpacker unpacker, TupleBuilderImpl builder, Column col) + throws IOException { + builder.set(col.name(), unpacker.unpackObject(getClientDataType(col.type().spec()))); + } + + private static int getClientDataType(NativeTypeSpec spec) { + switch (spec) { + case INT8: + return ClientDataType.INT8; + + case INT16: + return ClientDataType.INT16; + + case INT32: + return ClientDataType.INT32; + + case INT64: + return ClientDataType.INT64; + + case FLOAT: + return ClientDataType.FLOAT; + + case DOUBLE: + return ClientDataType.DOUBLE; + + case DECIMAL: + return ClientDataType.DECIMAL; + + case UUID: + return ClientDataType.UUID; + + case STRING: + return ClientDataType.STRING; + + case BYTES: + return ClientDataType.BYTES; + + case BITMASK: + return ClientDataType.BITMASK; + } + + throw new IgniteException("Unsupported native type: " + spec); + } + + private void writeColumnValue(ClientMessagePacker packer, Tuple tuple, Column col) throws IOException { + var val = tuple.value(col.name()); + + if (val == null) { + packer.packNil(); + return; + } + + switch (col.type().spec()) { + case INT8: + packer.packByte((byte) val); + break; + + case INT16: + packer.packShort((short) val); + break; + + case INT32: + packer.packInt((int) val); + break; + case INT64: + packer.packLong((long) val); + break; + + case FLOAT: + packer.packFloat((float) val); + break; + + case DOUBLE: + packer.packDouble((double) val); + break; + + case DECIMAL: + packer.packDecimal((BigDecimal) val); + break; + + case UUID: + packer.packUuid((UUID) val); + break; + + case STRING: + packer.packString((String) val); + break; + + case BYTES: + byte[] bytes = (byte[]) val; + packer.packBinaryHeader(bytes.length); + packer.writePayload(bytes); + break; + + case BITMASK: + packer.packBitSet((BitSet)val); + break; + + default: + throw new IgniteException("Data type not supported: " + col.type()); + } + } + + private TableImpl createTable(ClientMessageUnpacker unpacker) throws IOException { + var size = unpacker.unpackMapHeader(); + String name = null; + var settings = new Object() { + Integer replicas = null; + Integer partitions = null; + Map<String, Map<String, Object>> columns = null; + }; + + for (int i = 0; i < size; i++) { Review comment: Is it really necessary to hardcode all the configuration param names? AFAIR, we thought about code generation for Ignite configuration classes on all the platforms. If it is still so, then is it possible to could transfer HOCON changes to the server side with some general way, like serializing/deserializing raw Map pairs disregarding the concrete property names/types. ########## File path: modules/client/src/main/java/org/apache/ignite/internal/client/HostAndPortRange.java ########## @@ -0,0 +1,279 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.client; + +import java.io.Serializable; +import java.net.Inet6Address; +import java.net.UnknownHostException; +import org.apache.ignite.lang.IgniteException; + +/** + * Represents address along with port range. + */ +public class HostAndPortRange implements Serializable { + /** */ + private static final long serialVersionUID = 0L; + + /** Host. */ + private final String host; + + /** Port from. */ + private final int portFrom; + + /** Port to. */ + private final int portTo; + + /** + * Parse string into host and port pair. + * + * @param addrStr String. + * @param dfltPortFrom Default port from. + * @param dfltPortTo Default port to. + * @param errMsgPrefix Error message prefix. + * @return Result. + * @throws IgniteException If failed. + */ + public static HostAndPortRange parse(String addrStr, int dfltPortFrom, int dfltPortTo, String errMsgPrefix) + throws IgniteException { + assert dfltPortFrom <= dfltPortTo; + + String host; + + String portStr; + int portFrom; + int portTo; + + if (addrStr == null || addrStr.isEmpty()) + throw createParseError(addrStr, errMsgPrefix, "Address is empty"); + + if (addrStr.charAt(0) == '[') { // IPv6 with port(s) + int hostEndIdx = addrStr.indexOf(']'); + + if (hostEndIdx == -1) + throw createParseError(addrStr, errMsgPrefix, "Failed to parse IPv6 address, missing ']'"); + + host = addrStr.substring(1, hostEndIdx); + + if (hostEndIdx == addrStr.length() - 1) { // no port specified, using default + portFrom = dfltPortFrom; + portTo = dfltPortTo; + } + else { // port specified + portStr = addrStr.substring(hostEndIdx + 2); + + int[] ports = verifyPortStr(addrStr, errMsgPrefix, portStr); + portFrom = ports[0]; + portTo = ports[1]; + } + } + else { // IPv4 || IPv6 without port || empty host + final int colIdx = addrStr.lastIndexOf(':'); + + if (colIdx > 0) { + if (addrStr.lastIndexOf(':', colIdx - 1) != -1) { // IPv6 without [] and port + try { + Inet6Address.getByName(addrStr); + host = addrStr; + portFrom = dfltPortFrom; + portTo = dfltPortTo; + } + catch (UnknownHostException e) { + throw createParseError(addrStr, errMsgPrefix, "IPv6 is incorrect", e); + } + } + else { + host = addrStr.substring(0, colIdx); + portStr = addrStr.substring(colIdx + 1); + int[] ports = verifyPortStr(addrStr, errMsgPrefix, portStr); + portFrom = ports[0]; + portTo = ports[1]; + } + } + else if (colIdx == 0) + throw createParseError(addrStr, errMsgPrefix, "Host name is empty"); + else { // Port is not specified, use defaults. + host = addrStr; + + portFrom = dfltPortFrom; + portTo = dfltPortTo; + } + } + + return new HostAndPortRange(host, portFrom, portTo); + } + + /** + * Verifies string containing single port or ports range. + * + * @param addrStr Address String. + * @param errMsgPrefix Error message prefix. + * @param portStr Port or port range string. + * @return Array of int[portFrom, portTo]. + * @throws IgniteException If failed. + */ + private static int[] verifyPortStr(String addrStr, String errMsgPrefix, String portStr) + throws IgniteException { + String portFromStr; + String portToStr; + + if (portStr == null || portStr.isEmpty()) + throw createParseError(addrStr, errMsgPrefix, "port range is not specified"); + + int portRangeIdx = portStr.indexOf(".."); + + if (portRangeIdx >= 0) { + // Port range is specified. + portFromStr = portStr.substring(0, portRangeIdx); + portToStr = portStr.substring(portRangeIdx + 2); + } + else { + // Single port is specified. + portFromStr = portStr; + portToStr = portStr; + } + + int portFrom = parsePort(portFromStr, addrStr, errMsgPrefix); + int portTo = parsePort(portToStr, addrStr, errMsgPrefix); + + if (portFrom > portTo) + throw createParseError(addrStr, errMsgPrefix, "start port cannot be less than end port"); + + return new int[] {portFrom, portTo}; + } + + /** + * Parse port. + * + * @param portStr Port string. + * @param addrStr Address string. + * @param errMsgPrefix Error message prefix. + * @return Parsed port. + * @throws IgniteException If failed. + */ + private static int parsePort(String portStr, String addrStr, String errMsgPrefix) throws IgniteException { + try { + int port = Integer.parseInt(portStr); + + if (port <= 0 || port > 65535) + throw createParseError(addrStr, errMsgPrefix, "port range contains invalid port " + portStr); + + return port; + } + catch (NumberFormatException ignored) { + throw createParseError(addrStr, errMsgPrefix, "port range contains invalid port " + portStr); + } + } + + /** + * Create parse error. + * + * @param addrStr Address string. + * @param errMsgPrefix Error message prefix. + * @param errMsg Error message. + * @return Exception. + */ + private static IgniteException createParseError(String addrStr, String errMsgPrefix, String errMsg) { + return new IgniteException(errMsgPrefix + " (" + errMsg + "): " + addrStr); + } + + /** + * Create parse error with cause - nested exception. + * + * @param addrStr Address string. + * @param errMsgPrefix Error message prefix. + * @param errMsg Error message. + * @param cause Cause exception. + * @return Exception. + */ + private static IgniteException createParseError(String addrStr, String errMsgPrefix, String errMsg, Throwable cause) { + return new IgniteException(errMsgPrefix + " (" + errMsg + "): " + addrStr, cause); + } + + /** + * Constructor. + * + * @param host Host. + * @param port Port. + */ + public HostAndPortRange(String host, int port) { + this(host, port, port); + } + + /** + * Constructor. + * + * @param host Host. + * @param portFrom Port from. + * @param portTo Port to. + */ + public HostAndPortRange(String host, int portFrom, int portTo) { + assert host != null && !host.isEmpty(); + assert portFrom <= portTo; Review comment: ```suggestion assert portFrom <= portTo && portFrom > 0 && portTo < 65535; ``` ########## File path: modules/client/src/main/java/org/apache/ignite/internal/client/io/ClientMessageHandler.java ########## @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.client.io; + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * Handles thin client responses and server to client notifications. + */ +public interface ClientMessageHandler { + /** + * Handles messages from the server. Review comment: ```suggestion * Handles messages from the server. * ``` ########## File path: modules/client/src/main/java/org/apache/ignite/internal/client/table/ClientTable.java ########## @@ -0,0 +1,439 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.client.table; + +import org.apache.ignite.client.ClientMessageUnpacker; +import org.apache.ignite.client.ClientOp; +import org.apache.ignite.client.IgniteClientException; +import org.apache.ignite.internal.client.PayloadInputChannel; +import org.apache.ignite.internal.client.PayloadOutputChannel; +import org.apache.ignite.internal.client.ReliableChannel; +import org.apache.ignite.internal.tostring.IgniteToStringBuilder; +import org.apache.ignite.lang.IgniteBiTuple; +import org.apache.ignite.table.InvokeProcessor; +import org.apache.ignite.table.KeyValueBinaryView; +import org.apache.ignite.table.KeyValueView; +import org.apache.ignite.table.RecordView; +import org.apache.ignite.table.Table; +import org.apache.ignite.table.Tuple; +import org.apache.ignite.table.TupleBuilder; +import org.apache.ignite.table.mapper.KeyMapper; +import org.apache.ignite.table.mapper.RecordMapper; +import org.apache.ignite.table.mapper.ValueMapper; +import org.apache.ignite.tx.Transaction; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.msgpack.core.MessageFormat; + +import java.io.IOException; +import java.io.Serializable; +import java.util.Collection; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Client table API implementation. + */ +public class ClientTable implements Table { + /** */ + private final UUID id; + + /** */ + private final String name; + + /** */ + private final ReliableChannel ch; + + /** */ + private final ConcurrentHashMap<Integer, ClientSchema> schemas = new ConcurrentHashMap<>(); + + /** */ + private volatile int latestSchemaVer = -1; + + /** */ + private final Object latestSchemaLock = new Object(); + + /** + * Constructor. + * + * @param ch Channel. + * @param id Table id. + * @param name Table name. + */ + public ClientTable(ReliableChannel ch, UUID id, String name) { + assert ch != null; + assert id != null; + assert name != null && !name.isEmpty(); + + this.ch = ch; + this.id = id; + this.name = name; + } + + /** + * Gets the table id. + * + * @return Table id. + */ + public UUID tableId() { + return id; + } + + /** {@inheritDoc} */ + @Override public @NotNull String tableName() { + return name; + } + + /** {@inheritDoc} */ + @Override public <R> RecordView<R> recordView(RecordMapper<R> recMapper) { + return null; + } + + /** {@inheritDoc} */ + @Override public <K, V> KeyValueView<K, V> kvView(KeyMapper<K> keyMapper, ValueMapper<V> valMapper) { + return null; + } + + /** {@inheritDoc} */ + @Override public KeyValueBinaryView kvView() { + return null; + } + + /** {@inheritDoc} */ + @Override public Table withTransaction(Transaction tx) { + return null; + } + + /** {@inheritDoc} */ + @Override public TupleBuilder tupleBuilder() { + return new ClientTupleBuilder(); + } + + /** {@inheritDoc} */ + @Override public Tuple get(@NotNull Tuple keyRec) { + return getAsync(keyRec).join(); + } + + /** {@inheritDoc} */ + @Override public @NotNull CompletableFuture<Tuple> getAsync(@NotNull Tuple keyRec) { + return getLatestSchema().thenCompose(schema -> + ch.serviceAsync(ClientOp.TUPLE_GET, w -> writeTuple(keyRec, schema, w, true), r -> { + if (r.in().getNextFormat() == MessageFormat.NIL) + return null; + + var schemaVer = r.in().unpackInt(); + + return new IgniteBiTuple<>(r, schemaVer); + })).thenCompose(biTuple -> { + if (biTuple == null) + return CompletableFuture.completedFuture(null); + + assert biTuple.getKey() != null; + assert biTuple.getValue() != null; + + return getSchema(biTuple.getValue()).thenApply(schema -> readTuple(schema, biTuple.getKey())); + }); + } + + /** {@inheritDoc} */ + @Override public Collection<Tuple> getAll(@NotNull Collection<Tuple> keyRecs) { + return null; Review comment: UnsupportedOperationException("Not implemented yet.") is a common approach. ########## File path: modules/client/src/main/java/org/apache/ignite/client/package-info.java ########## @@ -0,0 +1,21 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Ignite thin client. + */ +package org.apache.ignite.client; Review comment: ```suggestion package org.apache.ignite.client; ``` ########## File path: modules/client-common/src/main/java/org/apache/ignite/client/ProtocolVersion.java ########## @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.client; + +import java.io.IOException; +import org.apache.ignite.internal.tostring.S; + +/** Thin client protocol version. */ +public final class ProtocolVersion implements Comparable<ProtocolVersion> { + /** Protocol version: 3.0.0. */ + public static final ProtocolVersion V3_0_0 = new ProtocolVersion((short)3, (short)0, (short)0); + + /** The most actual version. */ + public static final ProtocolVersion LATEST_VER = V3_0_0; + + /** Major. */ + private final short major; + + /** Minor. */ + private final short minor; + + /** Patch. */ + private final short patch; + + /** + * Constructor. + * + * @param major Major part. + * @param minor Minor part. + * @param patch Patch part. + */ + public ProtocolVersion(short major, short minor, short patch) { + this.major = major; + this.minor = minor; + this.patch = patch; + } + + /** + * Reads version from unpacker. + * + * @param unpacker Unpacker. + * @return Version. + * @throws IOException when underlying input throws IOException. + */ + public static ProtocolVersion unpack(ClientMessageUnpacker unpacker) throws IOException { + return new ProtocolVersion(unpacker.unpackShort(), unpacker.unpackShort(), unpacker.unpackShort()); + } + + /** + * Writes this instance to the specified packer. + * + * @param packer Packer. + * @throws IOException when underlying output throws IOException. + */ + public void pack(ClientMessagePacker packer) throws IOException { + packer.packShort(major).packShort(minor).packShort(patch); + } + + /** + * Gets the major part. + * + * @return Major. + */ + public short major() { + return major; + } + + /** + * Gets the minor part. + * + * @return Minor. + */ + public short minor() { + return minor; + } + + /** + * Gets the patch part. + * + * @return Patch. + */ + public short patch() { + return patch; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object obj) { + if (!(obj instanceof ProtocolVersion)) + return false; + + ProtocolVersion other = (ProtocolVersion)obj; + + return major == other.major && + minor == other.minor && + patch == other.patch; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + int res = 11; + res = 31 * res + major; + res = 31 * res + minor; + res = 31 * res + patch; Review comment: ```suggestion int res = 31 * major; res += ((minor & 0xFFFF) << 16) & (patch & 0xFFFF); ``` Two shorts can be concated to int. ########## File path: modules/client/src/main/java/org/apache/ignite/internal/client/PayloadReader.java ########## @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.client; + +/** + * Payload reader. + */ +@FunctionalInterface +public interface PayloadReader<T> { + /** + * Reads the payload from the channel. Review comment: ```suggestion * Reads the payload from the channel. * ``` ########## File path: modules/client/src/main/java/org/apache/ignite/internal/client/io/ClientConnectionStateHandler.java ########## @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.client.io; + +import org.jetbrains.annotations.Nullable; + +/** + * Handles thin client connection state. + */ +public interface ClientConnectionStateHandler { + /** + * Handles connection loss. Review comment: ```suggestion * Handles connection loss. * ``` ########## File path: modules/client/src/main/java/org/apache/ignite/internal/client/PayloadWriter.java ########## @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.client; + +/** + * Payload writer. + */ +@FunctionalInterface +public interface PayloadWriter { + /** + * Writes the payload to the channel. Review comment: ```suggestion * Writes the payload to the channel. * ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
