timoninmaxim commented on a change in pull request #9081:
URL: https://github.com/apache/ignite/pull/9081#discussion_r682466010



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/DistributedCacheQueryReducer.java
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.processors.cache.query;
+
+import java.util.UUID;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+
+/**
+ * Reducer for distributed cache query.
+ */
+public interface DistributedCacheQueryReducer<T> extends CacheQueryReducer<T> {
+    /**
+     * Checks whether node with provided {@code nodeId} is a map node for the 
query.
+     * Note: if all pages were received from this node, then the method will 
return {@code false}.
+     *
+     * @param nodeId Node ID.
+     * @return {@code true} if node with provided {@code nodeId} is a map node 
for the query, {@code false} otherwise.
+     */
+    public boolean mapNode(UUID nodeId);

Review comment:
       According to code style [1] we should not use "is" as prefix for 
internal API.
   
   [1] 
https://cwiki.apache.org/confluence/display/IGNITE/Coding+Guidelines#CodingGuidelines-Gettersandsetters
   
   

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/CacheQueryReducer.java
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.processors.cache.query;
+
+import java.util.Collection;
+import java.util.UUID;
+import org.apache.ignite.IgniteCheckedException;
+
+/**
+ * This class is responsible for reducing results of cache query. Query 
results are delivered via callback
+ * {@link #onPage(UUID, Collection, boolean)}.
+ *
+ * <T> is a type of cache query result item.
+ */
+public interface CacheQueryReducer<T> {
+    /**
+     * @return Next item.
+     */
+    public T next() throws IgniteCheckedException;
+
+    /**
+     * @return {@code true} if there is a next item, otherwise {@code false}.
+     */
+    public boolean hasNext() throws IgniteCheckedException;
+
+    /**
+     * Callback that invoked on receiving a new page.
+     *
+     * @param nodeId Node ID that sent this page.
+     * @param data Page data rows.
+     * @param last Whether this page is last for specified {@code nodeId}.
+     * @return {@code true} if this page is final page for query and no more 
pages are waited, otherwise {@code false}.
+     */
+    public boolean onPage(UUID nodeId, Collection<T> data, boolean last);
+
+    /**
+     * Callback in case of page with error.
+     */
+    public void onError();
+
+    /**
+     * Callback that invokes after a query future is done.
+     */
+    public void onFinish();

Review comment:
       There is a comment about it, see invokation [1]. We should release latch 
after onDone() invokation. Also I've described this in previous conversation: 
https://github.com/apache/ignite/pull/9081#discussion_r649824494
   
   [1] 
[GridCacheQueryFutureAdapter:332](https://github.com/apache/ignite/blob/0add93b488ebe7265b2a0f951fcdfd55adbcc01f/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheQueryFutureAdapter.java#L332)

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheDistributedQueryFuture.java
##########
@@ -69,205 +53,69 @@ protected 
GridCacheDistributedQueryFuture(GridCacheContext<K, V> ctx, long reqId
         GridCacheQueryManager<K, V> mgr = ctx.queries();
 
         assert mgr != null;
-
-        synchronized (this) {
-            for (ClusterNode node : nodes)
-                subgrid.add(node.id());
-        }
     }
 
     /** {@inheritDoc} */
-    @Override protected void cancelQuery() throws IgniteCheckedException {
-        final GridCacheQueryManager<K, V> qryMgr = cctx.queries();
-
-        assert qryMgr != null;
-
-        try {
-            Collection<ClusterNode> allNodes = cctx.discovery().allNodes();
-            Collection<ClusterNode> nodes;
-
-            synchronized (this) {
-                nodes = F.retain(allNodes, true,
-                    new P1<ClusterNode>() {
-                        @Override public boolean apply(ClusterNode node) {
-                            return !cctx.localNodeId().equals(node.id()) && 
subgrid.contains(node.id());
-                        }
-                    }
-                );
-
-                subgrid.clear();
-            }
-
-            final GridCacheQueryRequest req = new 
GridCacheQueryRequest(cctx.cacheId(),
-                reqId,
-                fields(),
-                qryMgr.queryTopologyVersion(),
-                cctx.deploymentEnabled());
-
-            // Process cancel query directly (without sending) for local node,
-            cctx.closures().callLocalSafe(new GridPlainCallable<Object>() {
-                @Override public Object call() {
-                    qryMgr.processQueryRequest(cctx.localNodeId(), req);
+    @Override protected void cancelQuery() {
+        reducer.onCancel();
 
-                    return null;
-                }
-            });
-
-            if (!nodes.isEmpty()) {
-                for (ClusterNode node : nodes) {
-                    try {
-                        cctx.io().send(node, req, cctx.ioPolicy());
-                    }
-                    catch (IgniteCheckedException e) {
-                        if (cctx.io().checkNodeLeft(node.id(), e, false)) {
-                            if (log.isDebugEnabled())
-                                log.debug("Failed to send cancel request, node 
failed: " + node);
-                        }
-                        else
-                            U.error(log, "Failed to send cancel request 
[node=" + node + ']', e);
-                    }
-                }
-            }
-        }
-        catch (IgniteCheckedException e) {
-            U.error(log, "Failed to send cancel request (will cancel query in 
any case).", e);
-        }
-
-        qryMgr.onQueryFutureCanceled(reqId);
+        cctx.queries().onQueryFutureCanceled(reqId);
 
         clear();
     }
 
     /** {@inheritDoc} */
-    @SuppressWarnings("NonPrivateFieldAccessedInSynchronizedContext")
     @Override protected void onNodeLeft(UUID nodeId) {
-        boolean callOnPage;
-
-        synchronized (this) {
-            callOnPage = !loc && subgrid.contains(nodeId);
-        }
+        boolean qryNode = reducer.mapNode(nodeId);
 
-        if (callOnPage)
-            onPage(nodeId, Collections.emptyList(),
+        if (qryNode)
+            onPage(nodeId, null,
                 new ClusterTopologyCheckedException("Remote node has left 
topology: " + nodeId), true);
     }
 
     /** {@inheritDoc} */
-    @Override public void awaitFirstPage() throws IgniteCheckedException {
-        try {
-            firstPageLatch.await();
+    @Override public void awaitFirstItemAvailable() throws 
IgniteCheckedException {
+        reducer.awaitInitialization();
 
-            if (isDone() && error() != null)
-                // Throw the exception if future failed.
-                get();
-        }
-        catch (InterruptedException e) {
-            Thread.currentThread().interrupt();
-
-            throw new IgniteInterruptedCheckedException(e);
-        }
+        if (isDone() && error() != null)
+            // Throw the exception if future failed.
+            get();
     }
 
     /** {@inheritDoc} */
-    @Override protected boolean onPage(UUID nodeId, boolean last) {
-        assert Thread.holdsLock(this);
-
-        if (!loc) {
-            rcvd.add(nodeId);
-
-            if (rcvd.containsAll(subgrid))
-                firstPageLatch.countDown();
-        }
-
-        boolean futFinish;
-
-        if (last) {
-            futFinish = loc || (subgrid.remove(nodeId) && subgrid.isEmpty());
-
-            if (futFinish)
-                firstPageLatch.countDown();
-        }
-        else
-            futFinish = false;
-
-        return futFinish;
+    @Override protected CacheQueryReducer<R> reducer() {
+        return reducer;
     }
 
-    /** {@inheritDoc} */
-    @Override protected void loadPage() {
-        assert !Thread.holdsLock(this);
-
-        Collection<ClusterNode> nodes = null;
-
-        synchronized (this) {
-            if (!isDone() && rcvd.containsAll(subgrid)) {
-                rcvd.clear();
-
-                nodes = nodes();
-            }
-        }
-
-        if (nodes != null)
-            cctx.queries().loadPage(reqId, qry.query(), nodes, false);
+    /** Set reducer. */
+    void reducer(DistributedCacheQueryReducer<R> reducer) {
+        this.reducer = reducer;
     }
 
     /** {@inheritDoc} */
-    @Override protected void loadAllPages() throws 
IgniteInterruptedCheckedException {
-        assert !Thread.holdsLock(this);
-
-        U.await(firstPageLatch);
-
-        Collection<ClusterNode> nodes = null;
-
-        synchronized (this) {
-            if (!isDone() && !subgrid.isEmpty())
-                nodes = nodes();
-        }
-
-        if (nodes != null)
-            cctx.queries().loadPage(reqId, qry.query(), nodes, true);
-    }
-
-    /**
-     * @return Nodes to send requests to.
-     */
-    private Collection<ClusterNode> nodes() {
-        assert Thread.holdsLock(this);
-
-        Collection<ClusterNode> nodes = new ArrayList<>(subgrid.size());
-
-        for (UUID nodeId : subgrid) {
-            ClusterNode node = cctx.discovery().node(nodeId);
-
-            if (node != null)
-                nodes.add(node);
-        }
-
-        return nodes;
+    @Override public Collection<R> get() throws IgniteCheckedException {
+        return get0();
     }
 
     /** {@inheritDoc} */
-    @Override public boolean onDone(Collection<R> res, Throwable err) {
-        boolean done = super.onDone(res, err);
-
-        // Must release the lath after onDone() in order for a waiting thread 
to see an exception, if any.
-        firstPageLatch.countDown();
-
-        return done;
+    @Override public Collection<R> get(long timeout, TimeUnit unit) throws 
IgniteCheckedException {
+        return get0();
     }
 
     /** {@inheritDoc} */
-    @Override public boolean onCancelled() {
-        firstPageLatch.countDown();
-
-        return super.onCancelled();
+    @Override public Collection<R> getUninterruptibly() throws 
IgniteCheckedException {
+        return get0();
     }
 
-    /** {@inheritDoc} */
-    @Override public void onTimeout() {
-        firstPageLatch.countDown();
+    /**
+     * Completion of distributed query future depends on user that iterates 
over query result with lazy page loading.
+     * Then {@link #get()} can lock on unpredictably long period of time. So 
we should avoid call it.
+     */
+    private Collection<R> get0() throws IgniteCheckedException {
+        if (!isDone())
+            throw new IgniteIllegalStateException("Unexpected lock on iterator 
over distributed cache query result.");

Review comment:
       The only place we use get() it's in this class, some lines above.
   
   
https://github.com/apache/ignite/blob/0add93b488ebe7265b2a0f951fcdfd55adbcc01f/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheDistributedQueryFuture.java#L82
   
   But I agree, that it a little bit misleading. So I will replace this line of 
code with `super.get()` and then we can forbid `get()` at all.




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


Reply via email to