YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rpc/rpc_controller.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_RPC_RPC_CONTROLLER_H
33
#define YB_RPC_RPC_CONTROLLER_H
34
35
#include <memory>
36
37
#include <glog/logging.h>
38
39
#include "yb/gutil/macros.h"
40
#include "yb/rpc/rpc_fwd.h"
41
#include "yb/util/locks.h"
42
#include "yb/util/monotime.h"
43
#include "yb/util/status_fwd.h"
44
45
namespace yb {
46
47
namespace rpc {
48
49
class ErrorStatusPB;
50
51
// Controller for managing properties of a single RPC call, on the client side.
52
//
53
// An RpcController maps to exactly one call and is not thread-safe. RpcController can be reused
54
// for another call to avoid extra destruction/construction, see RpcController::Reset.
55
// The client may use this class prior to sending an RPC in order to set properties such
56
// as the call's timeout.
57
//
58
// After the call has been sent (e.g using Proxy::AsyncRequest()) the user
59
// may invoke methods on the RpcController object in order to probe the status
60
// of the call.
61
class RpcController {
62
 public:
63
  RpcController();
64
  ~RpcController();
65
66
  RpcController(RpcController&& rhs) noexcept;
67
  void operator=(RpcController&& rhs) noexcept;
68
69
  // Swap the state of the controller (including ownership of sidecars, buffers,
70
  // etc) with another one.
71
  void Swap(RpcController* other);
72
73
  // Reset this controller so it may be used with another call.
74
  // Note that reset doesn't reset controller's properties except the call itself.
75
  void Reset();
76
77
  // Return true if the call has finished.
78
  // A call is finished if the server has responded, or if the call
79
  // has timed out.
80
  bool finished() const;
81
82
  // Return the current status of a call.
83
  //
84
  // A call is "OK" status until it finishes, at which point it may
85
  // either remain in "OK" status (if the call was successful), or
86
  // change to an error status. Error status indicates that there was
87
  // some RPC-layer issue with making the call, for example, one of:
88
  //
89
  // * failed to establish a connection to the server
90
  // * the server was too busy to handle the request
91
  // * the server was unable to interpret the request (eg due to a version
92
  //   mismatch)
93
  // * a network error occurred which caused the connection to be torn
94
  //   down
95
  // * the call timed out
96
  CHECKED_STATUS status() const;
97
98
  CHECKED_STATUS thread_pool_failure() const;
99
100
  // If status() returns a RemoteError object, then this function returns
101
  // the error response provided by the server. Service implementors may
102
  // use protobuf Extensions to add application-specific data to this PB.
103
  //
104
  // If Status was not a RemoteError, this returns NULL.
105
  // The returned pointer is only valid as long as the controller object.
106
  const ErrorStatusPB* error_response() const;
107
108
  // Set the timeout for the call to be made with this RPC controller.
109
  //
110
  // The configured timeout applies to the entire time period between
111
  // the AsyncRequest() method call and getting a response. For example,
112
  // if it takes too long to establish a connection to the remote host,
113
  // or to DNS-resolve the remote host, those will be accounted as part
114
  // of the timeout period.
115
  //
116
  // Timeouts must be set prior to making the request -- the timeout may
117
  // not currently be adjusted for an already-sent call.
118
  //
119
  // Using an uninitialized timeout will result in a call which never
120
  // times out (not recommended!)
121
  void set_timeout(const MonoDelta& timeout);
122
123
  // Like a timeout, but based on a fixed point in time instead of a delta.
124
  //
125
  // Using an uninitialized deadline means the call won't time out.
126
  void set_deadline(const MonoTime& deadline);
127
128
  void set_deadline(CoarseTimePoint deadline);
129
130
5.99M
  void set_allow_local_calls_in_curr_thread(bool al) { allow_local_calls_in_curr_thread_ = al; }
131
5.59M
  bool allow_local_calls_in_curr_thread() const { return allow_local_calls_in_curr_thread_; }
132
133
  // Sets where to invoke callback on receiving response to the async call.
134
  // For sync calls callback is always executed on reactor thread.
135
11.2M
  void set_invoke_callback_mode(InvokeCallbackMode invoke_callback_mode) {
136
11.2M
    invoke_callback_mode_ = invoke_callback_mode;
137
11.2M
  }
138
139
19.9M
  InvokeCallbackMode invoke_callback_mode() { return invoke_callback_mode_; }
140
141
  // Return the configured timeout.
142
  MonoDelta timeout() const;
143
144
  // Returns the slice pointing to the i-th sidecar upon success.
145
  //
146
  // Should only be called if the call's finished, but the controller has not
147
  // been Reset().
148
  //
149
  // May fail if index is invalid.
150
  Result<Slice> GetSidecar(int idx) const;
151
  Result<SidecarPtr> GetSidecarPtr(int idx) const;
152
  Result<SidecarHolder> GetSidecarHolder(int idx) const;
153
154
  int32_t call_id() const;
155
156
 private:
157
  friend class OutboundCall;
158
  friend class Proxy;
159
160
  MonoDelta timeout_;
161
162
  mutable simple_spinlock lock_;
163
164
  // Once the call is sent, it is tracked here.
165
  OutboundCallPtr call_;
166
  bool allow_local_calls_in_curr_thread_ = false;
167
  InvokeCallbackMode invoke_callback_mode_ = InvokeCallbackMode::kThreadPoolNormal;
168
169
  DISALLOW_COPY_AND_ASSIGN(RpcController);
170
};
171
172
} // namespace rpc
173
} // namespace yb
174
175
#endif // YB_RPC_RPC_CONTROLLER_H