You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/05/31 09:12:05 UTC

[GitHub] [ignite-3] isapego commented on a diff in pull request #837: IGNITE-14972 Java thin: Implement SQL API

isapego commented on code in PR #837:
URL: https://github.com/apache/ignite-3/pull/837#discussion_r885391633


##########
modules/client/src/main/java/org/apache/ignite/internal/client/sql/ClientAsyncResultSet.java:
##########
@@ -0,0 +1,196 @@
+/*
+ * 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.sql;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.client.IgniteClientException;
+import org.apache.ignite.internal.client.ClientChannel;
+import org.apache.ignite.internal.client.proto.ClientMessageUnpacker;
+import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Client async result set.
+ */
+class ClientAsyncResultSet implements AsyncResultSet {
+    /** Channel. */
+    private final ClientChannel ch;
+
+    /** Resource id. */
+    private final Long resourceId;
+
+    /** Row set flag. */
+    private final boolean hasRowSet;
+
+    /** Applied flag. */
+    private final boolean wasApplied;
+
+    /** Affected rows. */
+    private final long affectedRows;
+
+    /** Metadata. */
+    private final ResultSetMetadata metadata;
+
+    /** Rows. */
+    private volatile List<SqlRow> rows;
+
+    /** More pages flag. */
+    private volatile boolean hasMorePages;
+
+    /** Closed flag. */
+    private volatile boolean closed;
+
+    /**
+     * Constructor.
+     *
+     * @param ch Channel.
+     * @param in Unpacker.
+     */
+    public ClientAsyncResultSet(ClientChannel ch, ClientMessageUnpacker in) {
+        this.ch = ch;
+
+        resourceId = in.tryUnpackNil() ? null : in.unpackLong();
+        hasRowSet = in.unpackBoolean();
+        hasMorePages = in.unpackBoolean();
+        wasApplied = in.unpackBoolean();
+        affectedRows = in.unpackLong();
+
+        metadata = new ClientResultSetMetadata(in);
+
+        if (hasRowSet) {
+            readRows(in);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        return metadata;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return hasRowSet;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        return affectedRows;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        return wasApplied;
+    }
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType")
+    @Override
+    public Iterable<SqlRow> currentPage() {
+        requireResultSet();
+
+        return rows;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public int currentPageSize() {
+        requireResultSet();
+
+        return rows.size();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<? extends AsyncResultSet> fetchNextPage() {
+        requireResultSet();
+
+        if (closed) {
+            return CompletableFuture.failedFuture(new IgniteClientException("Cursor is closed."));
+        }
+
+        if (!hasMorePages || resourceId == null) {
+            return CompletableFuture.failedFuture(new IgniteClientException("No more pages."));
+        }

Review Comment:
   The check does not match the one in `hasMorePages()`, which may result in user calling `hasMorePages()`, getting `true`, trying fetch next page and getting an exception as a result (if `resourceId == null`). I know that maybe this is not possible scenario, but anyway, lets use `hasMorePages()` call here, and move `resourceId == null` check to this method.



##########
modules/client-handler/src/main/java/org/apache/ignite/client/handler/requests/sql/ClientSqlCursorNextPageRequest.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.requests.sql;
+
+import static org.apache.ignite.client.handler.requests.sql.ClientSqlCommon.packCurrentPage;
+
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.client.handler.ClientResourceRegistry;
+import org.apache.ignite.internal.client.proto.ClientMessagePacker;
+import org.apache.ignite.internal.client.proto.ClientMessageUnpacker;
+import org.apache.ignite.lang.IgniteException;
+import org.apache.ignite.lang.IgniteInternalCheckedException;
+import org.apache.ignite.sql.async.AsyncResultSet;
+
+/**
+ * Client SQL cursor next page request.
+ */
+public class ClientSqlCursorNextPageRequest {
+    /**
+     * Processes the request.
+     *
+     * @param in  Unpacker.
+     * @param out Packer.
+     * @return Future.
+     */
+    public static CompletableFuture<Void> process(
+            ClientMessageUnpacker in,
+            ClientMessagePacker out,
+            ClientResourceRegistry resources)
+            throws IgniteInternalCheckedException {
+        long resourceId = in.unpackLong();
+
+        AsyncResultSet asyncResultSet = resources.get(resourceId).get(AsyncResultSet.class);
+
+        return asyncResultSet.fetchNextPage()
+                .thenCompose(r -> {
+                    packCurrentPage(out, r);
+                    out.packBoolean(r.hasMorePages());
+
+                    if (!r.hasMorePages()) {
+                        try {
+                            resources.remove(resourceId);
+                        } catch (IgniteInternalCheckedException e) {
+                            throw new IgniteException(e);
+                        }

Review Comment:
   This exception handling block may results in returning an error to a user during some weird glitch while closing resource, while we actually have data and can return it to a user. Maybe it would be better to log an error here and do not fail operation itself? After all, though closing a resource error may be a serious problem and may result in memory leak, it does not look that critical to me to interrupt data fetching.



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