YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/redis/redisserver/redis_commands.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#ifndef YB_YQL_REDIS_REDISSERVER_REDIS_COMMANDS_H
15
#define YB_YQL_REDIS_REDISSERVER_REDIS_COMMANDS_H
16
17
#include <functional>
18
#include <string>
19
#include <vector>
20
#include <unordered_set>
21
22
#include "yb/client/client_fwd.h"
23
24
#include "yb/rpc/rpc_fwd.h"
25
#include "yb/rpc/service_if.h"
26
27
#include "yb/yql/redis/redisserver/redis_fwd.h"
28
#include "yb/yql/redis/redisserver/redis_server.h"
29
30
namespace yb {
31
namespace redisserver {
32
33
typedef boost::function<void(const Status&)> StatusFunctor;
34
typedef boost::function<void(int i)> IntFunctor;
35
36
class RedisConnectionContext;
37
38
YB_STRONGLY_TYPED_BOOL(AsPattern);
39
40
class RedisServiceData {
41
 public:
42
  // Used for Monitor.
43
  virtual void AppendToMonitors(rpc::Connection* conn) = 0;
44
  virtual void RemoveFromMonitors(rpc::Connection* conn) = 0;
45
  virtual void LogToMonitors(
46
      const std::string& end, const std::string& db, const RedisClientCommand& cmd) = 0;
47
48
  // Used for PubSub.
49
  virtual void AppendToSubscribers(
50
      AsPattern type, const std::vector<std::string>& channels, rpc::Connection* conn,
51
      std::vector<size_t>* subs) = 0;
52
  virtual void RemoveFromSubscribers(
53
      AsPattern type, const std::vector<std::string>& channels, rpc::Connection* conn,
54
      std::vector<size_t>* subs) = 0;
55
  virtual size_t NumSubscribers(AsPattern type, const std::string& channel) = 0;
56
  virtual void CleanUpSubscriptions(rpc::Connection* conn) = 0;
57
  virtual std::unordered_set<std::string> GetSubscriptions(
58
      AsPattern type, rpc::Connection* conn) = 0;
59
  virtual std::unordered_set<std::string> GetAllSubscriptions(AsPattern type) = 0;
60
  virtual void ForwardToInterestedProxies(
61
      const std::string& channel, const std::string& message, const IntFunctor& f) = 0;
62
63
  // Used for Auth.
64
  virtual CHECKED_STATUS GetRedisPasswords(std::vector<std::string>* passwords) = 0;
65
66
  // Used for Select.
67
  virtual yb::Result<std::shared_ptr<client::YBTable>> GetYBTableForDB(
68
      const std::string& db_name) = 0;
69
70
  static client::YBTableName GetYBTableNameForRedisDatabase(const std::string& db_name);
71
72
0
  virtual ~RedisServiceData() {}
73
};
74
75
YB_STRONGLY_TYPED_BOOL(ManualResponse);
76
77
// Context for batch of Redis commands.
78
class BatchContext : public RefCountedThreadSafe<BatchContext> {
79
 public:
80
  virtual std::shared_ptr<client::YBTable> table() = 0;
81
  virtual const RedisClientCommand& command(size_t idx) const = 0;
82
  virtual const std::shared_ptr<RedisInboundCall>& call() const = 0;
83
  virtual client::YBClient* client() const = 0;
84
  virtual const RedisServer* server() = 0;
85
  virtual RedisServiceData* service_data() = 0;
86
  virtual void CleanYBTableFromCache() = 0;
87
88
  virtual void Apply(
89
      size_t index,
90
      std::shared_ptr<client::YBRedisReadOp> operation,
91
      const rpc::RpcMethodMetrics& metrics) = 0;
92
93
  virtual void Apply(
94
      size_t index,
95
      std::shared_ptr<client::YBRedisWriteOp> operation,
96
      const rpc::RpcMethodMetrics& metrics) = 0;
97
98
  virtual void Apply(
99
      size_t index,
100
      std::function<bool(client::YBSession*, const StatusFunctor&)> functor,
101
      std::string partition_key,
102
      const rpc::RpcMethodMetrics& metrics,
103
      ManualResponse manual_response) = 0;
104
105
104k
  virtual ~BatchContext() {}
106
};
107
108
typedef scoped_refptr<BatchContext> BatchContextPtr;
109
110
// Information about RedisCommand(s) that we support.
111
struct RedisCommandInfo {
112
  std::string name;
113
  // The following arguments should be passed to this functor:
114
  // Info about its command.
115
  // Index of call in batch.
116
  // Batch context.
117
  std::function<void(const RedisCommandInfo&,
118
                     size_t,
119
                     BatchContext*)> functor;
120
  // Positive arity means that we expect exactly arity-1 arguments and negative arity means
121
  // that we expect at least -arity-1 arguments.
122
  int arity;
123
  yb::rpc::RpcMethodMetrics metrics;
124
};
125
126
typedef std::shared_ptr<RedisCommandInfo> RedisCommandInfoPtr;
127
128
void RespondWithFailure(
129
    std::shared_ptr<RedisInboundCall> call,
130
    size_t idx,
131
    const std::string& error,
132
    const char* error_code = "ERR");
133
134
void FillRedisCommands(const scoped_refptr<MetricEntity>& metric_entity,
135
                       const std::function<void(const RedisCommandInfo& info)>& setup_method);
136
137
#define YB_REDIS_METRIC(name) \
138
121k
    BOOST_PP_CAT(METRIC_handler_latency_yb_redisserver_RedisServerService_, name)
139
140
#define DEFINE_REDIS_histogram_EX(name_identifier, label_str, desc_str) \
141
  METRIC_DEFINE_histogram_with_percentiles( \
142
      server, BOOST_PP_CAT(handler_latency_yb_redisserver_RedisServerService_, name_identifier), \
143
      (label_str), yb::MetricUnit::kMicroseconds, \
144
      "Microseconds spent handling " desc_str " RPC requests", \
145
      60000000LU, 2)
146
147
#define DEFINE_REDIS_histogram(name_identifier, capitalized_name_str) \
148
  DEFINE_REDIS_histogram_EX( \
149
      name_identifier, \
150
      "yb.redisserver.RedisServerService." BOOST_STRINGIZE(name_identifier) " RPC Time", \
151
      "yb.redisserver.RedisServerService." capitalized_name_str "Command()")
152
153
} // namespace redisserver
154
} // namespace yb
155
156
#endif // YB_YQL_REDIS_REDISSERVER_REDIS_COMMANDS_H