You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by bo...@apache.org on 2019/08/27 12:19:18 UTC

[impala] 02/04: IMPALA-8845: Cancel receiver's streams on exchange node's EOS

This is an automated email from the ASF dual-hosted git repository.

boroknagyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git

commit 1c4bdcede475395d1139210a5d3ddf2641efa7eb
Author: Michael Ho <kw...@cloudera.com>
AuthorDate: Tue Aug 13 17:18:15 2019 -0700

    IMPALA-8845: Cancel receiver's streams on exchange node's EOS
    
    When an exchange node reaches its row count limit,
    the current code will not notify the sender fragments
    about it. Consequently, sender fragments may keep sending
    row batches to the exchange node but they won't be dequeued
    anymore. The sender fragments may end up blocking in the
    RPC indefinitely until either the query is cancelled or
    closed.
    
    This change fixes the problem above by cancelling the
    underlying receiver's streams of an exchange node once it
    reaches the row count limit. This will unblock all senders
    whose TransmitData() RPCs haven't been replied to yet. Any
    future row batches sent to this receiver will also be immediately
    replied to with a response indicating that this receiver is
    already closed so the sender will stop sending any more row
    batches to it.
    
    Change-Id: I10c805e9d63ed8af9f458bf71e8ef5ea9376b939
    Reviewed-on: http://gerrit.cloudera.org:8080/14101
    Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
    Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
 be/src/exec/exchange-node.cc              | 18 ++++++---
 be/src/exec/exchange-node.h               |  8 ++++
 be/src/runtime/krpc-data-stream-mgr.cc    |  3 +-
 be/src/runtime/krpc-data-stream-recvr.cc  | 12 ++++--
 be/src/runtime/krpc-data-stream-recvr.h   |  8 ++--
 tests/custom_cluster/test_exchange_eos.py | 67 +++++++++++++++++++++++++++++++
 6 files changed, 103 insertions(+), 13 deletions(-)

diff --git a/be/src/exec/exchange-node.cc b/be/src/exec/exchange-node.cc
index 961cfdf..3f96432 100644
--- a/be/src/exec/exchange-node.cc
+++ b/be/src/exec/exchange-node.cc
@@ -157,12 +157,17 @@ Status ExchangeNode::FillInputRowBatch(RuntimeState* state) {
   return ret_status;
 }
 
+void ExchangeNode::ReleaseRecvrResources(RowBatch* output_batch) {
+  stream_recvr_->TransferAllResources(output_batch);
+  stream_recvr_->CancelStream();
+}
+
 Status ExchangeNode::GetNext(RuntimeState* state, RowBatch* output_batch, bool* eos) {
   SCOPED_TIMER(runtime_profile_->total_time_counter());
   ScopedGetNextEventAdder ea(this, eos);
   RETURN_IF_ERROR(ExecDebugAction(TExecNodePhase::GETNEXT, state));
   if (ReachedLimit()) {
-    stream_recvr_->TransferAllResources(output_batch);
+    ReleaseRecvrResources(output_batch);
     *eos = true;
     return Status::OK();
   } else {
@@ -195,7 +200,7 @@ Status ExchangeNode::GetNext(RuntimeState* state, RowBatch* output_batch, bool*
       COUNTER_SET(rows_returned_counter_, rows_returned());
 
       if (ReachedLimit()) {
-        stream_recvr_->TransferAllResources(output_batch);
+        ReleaseRecvrResources(output_batch);
         *eos = true;
         return Status::OK();
       }
@@ -205,7 +210,9 @@ Status ExchangeNode::GetNext(RuntimeState* state, RowBatch* output_batch, bool*
     // we need more rows
     stream_recvr_->TransferAllResources(output_batch);
     RETURN_IF_ERROR(FillInputRowBatch(state));
-    *eos = (input_batch_ == NULL);
+    *eos = (input_batch_ == nullptr);
+    // No need to call CancelStream() on the receiver here as all incoming row batches
+    // have been consumed so we should have replied to all senders already.
     if (*eos) return Status::OK();
     next_row_idx_ = 0;
     DCHECK(input_batch_->row_desc()->LayoutIsPrefixOf(*output_batch->row_desc()));
@@ -238,8 +245,9 @@ Status ExchangeNode::GetNextMerging(RuntimeState* state, RowBatch* output_batch,
   CheckLimitAndTruncateRowBatchIfNeeded(output_batch, eos);
 
   // On eos, transfer all remaining resources from the input batches maintained
-  // by the merger to the output batch.
-  if (*eos) stream_recvr_->TransferAllResources(output_batch);
+  // by the merger to the output batch. Also cancel the underlying receiver so
+  // the senders' fragments can exit early.
+  if (*eos) ReleaseRecvrResources(output_batch);
 
   COUNTER_SET(rows_returned_counter_, rows_returned());
   return Status::OK();
diff --git a/be/src/exec/exchange-node.h b/be/src/exec/exchange-node.h
index e9f1d00..cbd65a9 100644
--- a/be/src/exec/exchange-node.h
+++ b/be/src/exec/exchange-node.h
@@ -71,6 +71,14 @@ class ExchangeNode : public ExecNode {
   /// Only used when is_merging_ is false.
   Status FillInputRowBatch(RuntimeState* state);
 
+  /// Releases resources of the receiver by transferring the resource ownership of
+  /// the most recently dequeued row batch to 'output_batch'. Also cancels the underlying
+  /// receiver so all senders will get unblocked. This function is called after the
+  /// exchange node hits end-of-stream due to reaching the node's row count limit.
+  /// Please note that no more rows will be returned from the receiver once this function
+  /// is called.
+  void ReleaseRecvrResources(RowBatch* output_batch);
+
   int num_senders_;  // needed for stream_recvr_ construction
 
   /// The underlying DataStreamRecvrBase instance. Ownership is shared between this
diff --git a/be/src/runtime/krpc-data-stream-mgr.cc b/be/src/runtime/krpc-data-stream-mgr.cc
index 521f52c..855d6d0 100644
--- a/be/src/runtime/krpc-data-stream-mgr.cc
+++ b/be/src/runtime/krpc-data-stream-mgr.cc
@@ -326,7 +326,8 @@ Status KrpcDataStreamMgr::DeregisterRecvr(
 }
 
 void KrpcDataStreamMgr::Cancel(const TUniqueId& finst_id) {
-  VLOG_QUERY << "cancelling all streams for fragment_instance_id=" << PrintId(finst_id);
+  VLOG_QUERY << "cancelling active streams for fragment_instance_id="
+             << PrintId(finst_id);
   lock_guard<mutex> l(lock_);
   FragmentRecvrSet::iterator iter =
       fragment_recvr_set_.lower_bound(make_pair(finst_id, 0));
diff --git a/be/src/runtime/krpc-data-stream-recvr.cc b/be/src/runtime/krpc-data-stream-recvr.cc
index 7c2dbe1..2187c85 100644
--- a/be/src/runtime/krpc-data-stream-recvr.cc
+++ b/be/src/runtime/krpc-data-stream-recvr.cc
@@ -453,7 +453,9 @@ void KrpcDataStreamRecvr::SenderQueue::AddBatch(const TransmitDataRequestPB* req
     // responded to if we reach here.
     DCHECK_GT(num_remaining_senders_, 0);
     if (UNLIKELY(is_cancelled_)) {
-      DataStreamService::RespondRpc(Status::OK(), response, rpc_context);
+      Status cancel_status = Status::Expected(TErrorCode::DATASTREAM_RECVR_CLOSED,
+          PrintId(recvr_->fragment_instance_id()), recvr_->dest_node_id());
+      DataStreamService::RespondRpc(cancel_status, response, rpc_context);
       return;
     }
 
@@ -557,7 +559,9 @@ void KrpcDataStreamRecvr::SenderQueue::TakeOverEarlySender(
   {
     unique_lock<SpinLock> l(lock_);
     if (UNLIKELY(is_cancelled_)) {
-      DataStreamService::RespondRpc(Status::OK(), ctx->response, ctx->rpc_context);
+      Status cancel_status = Status::Expected(TErrorCode::DATASTREAM_RECVR_CLOSED,
+          PrintId(recvr_->fragment_instance_id()), recvr_->dest_node_id());
+      DataStreamService::RespondRpc(cancel_status, ctx->response, ctx->rpc_context);
       return;
     }
     // Only enqueue a deferred RPC if the sender queue is not yet cancelled.
@@ -589,7 +593,9 @@ void KrpcDataStreamRecvr::SenderQueue::Cancel() {
     // Respond to deferred RPCs.
     while (!deferred_rpcs_.empty()) {
       const unique_ptr<TransmitDataCtx>& ctx = deferred_rpcs_.front();
-      DataStreamService::RespondAndReleaseRpc(Status::OK(), ctx->response,
+      Status cancel_status = Status::Expected(TErrorCode::DATASTREAM_RECVR_CLOSED,
+          PrintId(recvr_->fragment_instance_id()), recvr_->dest_node_id());
+      DataStreamService::RespondAndReleaseRpc(cancel_status, ctx->response,
           ctx->rpc_context, recvr_->deferred_rpc_tracker());
       DequeueDeferredRpc(l);
     }
diff --git a/be/src/runtime/krpc-data-stream-recvr.h b/be/src/runtime/krpc-data-stream-recvr.h
index 063ebe1..f9fcc22 100644
--- a/be/src/runtime/krpc-data-stream-recvr.h
+++ b/be/src/runtime/krpc-data-stream-recvr.h
@@ -115,6 +115,10 @@ class KrpcDataStreamRecvr {
   /// queue to the specified batch. Called from fragment instance execution threads only.
   void TransferAllResources(RowBatch* transfer_batch);
 
+  /// Marks all sender queues as cancelled and notifies all waiting consumers of
+  /// the cancellation.
+  void CancelStream();
+
   const TUniqueId& fragment_instance_id() const { return fragment_instance_id_; }
   PlanNodeId dest_node_id() const { return dest_node_id_; }
   const RowDescriptor* row_desc() const { return row_desc_; }
@@ -156,10 +160,6 @@ class KrpcDataStreamRecvr {
   /// sender queue. Called from KrpcDataStreamMgr.
   void RemoveSender(int sender_id);
 
-  /// Marks all sender queues as cancelled and notifies all waiting consumers of
-  /// cancellation.
-  void CancelStream();
-
   /// Return true if the addition of a new batch of size 'batch_size' would exceed the
   /// total buffer limit.
   bool ExceedsLimit(int64_t batch_size) {
diff --git a/tests/custom_cluster/test_exchange_eos.py b/tests/custom_cluster/test_exchange_eos.py
new file mode 100644
index 0000000..b26739f
--- /dev/null
+++ b/tests/custom_cluster/test_exchange_eos.py
@@ -0,0 +1,67 @@
+# 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.
+
+import pytest
+
+from tests.common.custom_cluster_test_suite import CustomClusterTestSuite
+from tests.common.impala_cluster import ImpalaCluster
+from tests.verifiers.metric_verifier import MetricVerifier
+
+
+class TestExchangeEos(CustomClusterTestSuite):
+  """ Test to verify that the senders' fragments get unblocked and run to completion
+  after exchange node hits eos"""
+
+  @classmethod
+  def get_workload(cls):
+    return 'tpch'
+
+  @classmethod
+  def add_test_dimensions(cls):
+    super(CustomClusterTestSuite, cls).add_test_dimensions()
+    cls.ImpalaTestMatrix.add_constraint(lambda v:
+        v.get_value('table_format').file_format == 'parquet')
+
+  @pytest.mark.execute_serially
+  @CustomClusterTestSuite.with_args(cluster_size=9, num_exclusive_coordinators=1)
+  def test_exchange_eos(self, vector):
+    """ Test IMPALA-8845: runs with result spooling enabled and defers the fetching
+    of results until all non-coordinator fragments have completed. It aims to verify
+    that once the coordinator fragment reaches eos, the rest of the fragments will
+    get unblocked. Using a cluster of size 9 which can reliably reproduce the hang of
+    some non-coordinator fragments without the fix of IMPALA-8845.
+    """
+
+    cluster = ImpalaCluster.get_e2e_test_cluster()
+    coordinator = cluster.get_first_impalad()
+    client = coordinator.service.create_beeswax_client()
+
+    vector.get_value('exec_option')['spool_query_results'] = 'true'
+    for query in ["select * from tpch.lineitem order by l_orderkey limit 10000",
+                  "select * from tpch.lineitem limit 10000"]:
+      handle = self.execute_query_async_using_client(client, query, vector)
+      for impalad in ImpalaCluster.get_e2e_test_cluster().impalads:
+        verifier = MetricVerifier(impalad.service)
+        if impalad.get_webserver_port() == coordinator.get_webserver_port():
+          num_fragments = 1
+        else:
+          num_fragments = 0
+        verifier.wait_for_metric("impala-server.num-fragments-in-flight", num_fragments)
+      results = client.fetch(query, handle)
+      assert results.success
+      assert len(results.data) == 10000
+    client.close()