ob_sql_client_decorator.h 4.3 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef _OB_SQL_CLIENT_DECORATOR_H
#define _OB_SQL_CLIENT_DECORATOR_H 1
#include "lib/mysqlclient/ob_isql_client.h"
#include "lib/mysqlclient/ob_single_connection_proxy.h"
W
wangzelin.wzl 已提交
17 18 19 20
namespace oceanbase
{
namespace common
{
O
oceanbase-admin 已提交
21
// read will retry `retry_limit' times when failed
W
wangzelin.wzl 已提交
22 23
class ObSQLClientRetry: public ObISQLClient
{
G
gm 已提交
24
public:
W
wangzelin.wzl 已提交
25 26 27
  ObSQLClientRetry(ObISQLClient *sql_client, int32_t retry_limit)
      :sql_client_(sql_client),
       retry_limit_(retry_limit)
O
oceanbase-admin 已提交
28
  {}
W
wangzelin.wzl 已提交
29
  virtual ~ObSQLClientRetry() {}
O
oceanbase-admin 已提交
30

W
wangzelin.wzl 已提交
31 32 33 34 35 36
  virtual int escape(const char *from, const int64_t from_size,
      char *to, const int64_t to_size, int64_t &out_size) override;
  virtual int read(ReadResult &res, const uint64_t tenant_id, const char *sql) override;
  virtual int read(ReadResult &res, const int64_t cluster_id, const uint64_t tenant_id, const char *sql) override;
  virtual int write(const uint64_t tenant_id, const char *sql, int64_t &affected_rows) override;
  virtual sqlclient::ObISQLConnectionPool *get_pool() override;
O
obdev 已提交
37
  virtual sqlclient::ObISQLConnection *get_connection() override;
O
oceanbase-admin 已提交
38 39 40
  using ObISQLClient::read;
  using ObISQLClient::write;

W
wangzelin.wzl 已提交
41 42
  void set_retry_limit(int32_t retry_limit) { retry_limit_ = retry_limit; }
  int32_t get_retry_limit() const { return retry_limit_; }
O
oceanbase-admin 已提交
43 44 45 46
  bool is_oracle_mode() const override
  {
    return NULL == sql_client_ ? false : sql_client_->is_oracle_mode();
  }
G
gm 已提交
47
private:
O
oceanbase-admin 已提交
48 49
  // disallow copy
  DISALLOW_COPY_AND_ASSIGN(ObSQLClientRetry);
G
gm 已提交
50
private:
W
wangzelin.wzl 已提交
51
  ObISQLClient *sql_client_;
O
oceanbase-admin 已提交
52 53 54 55
  int32_t retry_limit_;
};

class ObMySQLProxy;
W
wangzelin.wzl 已提交
56 57
class ObSQLClientRetryWeak: public ObISQLClient
{
G
gm 已提交
58
public:
O
obdev 已提交
59
  // only check_sys_variable is still useful
W
wangzelin.wzl 已提交
60 61 62 63 64 65 66 67 68 69 70 71
  ObSQLClientRetryWeak(ObISQLClient *sql_client,
                       bool did_use_retry = false,
                       int64_t snapshot_timestamp = OB_INVALID_TIMESTAMP,
                       bool check_sys_variable = true)
      :sql_client_(sql_client),
       snapshot_timestamp_(snapshot_timestamp),
       check_sys_variable_(check_sys_variable),
       tenant_id_(OB_INVALID_TENANT_ID),
       table_id_(OB_INVALID_ID)
  {
    UNUSED(did_use_retry);
  }
O
obdev 已提交
72
  // not useful, it just use sql_client directly
W
wangzelin.wzl 已提交
73 74 75 76
  ObSQLClientRetryWeak(ObISQLClient *sql_client,
                       bool did_use_retry,
                       const uint64_t tenant_id,
                       const uint64_t table_id)
O
oceanbase-admin 已提交
77 78 79 80 81
      : sql_client_(sql_client),
        snapshot_timestamp_(OB_INVALID_TIMESTAMP),
        check_sys_variable_(true),
        tenant_id_(tenant_id),
        table_id_(table_id)
W
wangzelin.wzl 已提交
82 83 84 85
  {
    UNUSED(did_use_retry);
  }
  virtual ~ObSQLClientRetryWeak() {}
O
oceanbase-admin 已提交
86

W
wangzelin.wzl 已提交
87 88 89 90 91
  virtual int escape(const char *from, const int64_t from_size,
      char *to, const int64_t to_size, int64_t &out_size) override;
  virtual int read(ReadResult &res, const uint64_t tenant_id, const char *sql) override;
  virtual int read(ReadResult &res, const int64_t cluster_id, const uint64_t tenant_id, const char *sql) override;
  virtual int write(const uint64_t tenant_id, const char *sql, int64_t &affected_rows) override;
O
oceanbase-admin 已提交
92 93 94
  using ObISQLClient::read;
  using ObISQLClient::write;

W
wangzelin.wzl 已提交
95
  virtual sqlclient::ObISQLConnectionPool *get_pool() override;
O
obdev 已提交
96 97
  virtual sqlclient::ObISQLConnection *get_connection() override;

O
oceanbase-admin 已提交
98 99 100 101
  bool is_oracle_mode() const override
  {
    return NULL == sql_client_ ? false : sql_client_->is_oracle_mode();
  }
G
gm 已提交
102
private:
O
oceanbase-admin 已提交
103 104 105 106
  // disallow copy
  DISALLOW_COPY_AND_ASSIGN(ObSQLClientRetryWeak);
  // functions
  int read_without_check_sys_variable(
O
obdev 已提交
107
      sqlclient::ObISQLConnection *conn,
W
wangzelin.wzl 已提交
108 109 110
      ReadResult &res,
      const uint64_t tenant_id,
      const char *sql);
G
gm 已提交
111
private:
W
wangzelin.wzl 已提交
112
  ObISQLClient *sql_client_;
O
obdev 已提交
113
  int64_t snapshot_timestamp_;  // deprecated
O
oceanbase-admin 已提交
114
  bool check_sys_variable_;
O
obdev 已提交
115 116
  uint64_t tenant_id_;          // deprecated
  uint64_t table_id_;           // deprecated
O
oceanbase-admin 已提交
117 118
};

W
wangzelin.wzl 已提交
119 120
} // end namespace common
} // end namespace oceanbase
O
oceanbase-admin 已提交
121 122

#endif /* _OB_SQL_CLIENT_DECORATOR_H */