ignite-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [ignite-3] ptupitsyn commented on a change in pull request #191: IGNITE-14970 Implement basic java thin client for 3.0
Date Thu, 22 Jul 2021 13:55:24 GMT

ptupitsyn commented on a change in pull request #191:
URL: https://github.com/apache/ignite-3/pull/191#discussion_r674818535



##########
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)

Review comment:
       Yes, we can follow an approach similar to 2.x with inheritance to reuse parts of deserialization
logic, or something functional with composition, but for this PR I'd prefer to leave it as
is.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message