ob_mysql_packet.h 22.2 KB
Newer Older
O
oceanbase-admin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/**
 * 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_MYSQL_PACKET_H_
#define _OB_MYSQL_PACKET_H_

#include "rpc/ob_packet.h"

W
wangzelin.wzl 已提交
18 19 20 21
namespace oceanbase
{
namespace obmysql
{
O
oceanbase-admin 已提交
22 23

#define OBPROXY_MYSQL_CMD_START 64
W
wangzelin.wzl 已提交
24
#define PREXECUTE_CMD 161
O
oceanbase-admin 已提交
25

W
wangzelin.wzl 已提交
26
static const int64_t OB_MYSQL_MAX_PACKET_LENGTH = (1L << 24); //3bytes , 16M
O
oceanbase-admin 已提交
27 28
static const int64_t OB_MYSQL_MAX_PAYLOAD_LENGTH = (OB_MYSQL_MAX_PACKET_LENGTH - 1);
// EASY_IO_BUFFER_SIZE is 16k, reserve 3k for libeasy header and request
W
wangzelin.wzl 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
static const int64_t OB_MULTI_RESPONSE_BUF_SIZE = (1L << 14) - 3 * 1024; //13k, for result
static const int64_t OB_ONE_RESPONSE_BUF_SIZE = (1L << 14); //16k, error/ok(proxy)

enum ObMySQLCmd
{
  COM_SLEEP,
  COM_QUIT,
  COM_INIT_DB,
  COM_QUERY,
  COM_FIELD_LIST,

  COM_CREATE_DB,
  COM_DROP_DB,
  COM_REFRESH,
  COM_SHUTDOWN,
  COM_STATISTICS,

  COM_PROCESS_INFO,
  COM_CONNECT,
  COM_PROCESS_KILL,
  COM_DEBUG,
  COM_PING,

  COM_TIME,
  COM_DELAYED_INSERT,
  COM_CHANGE_USER,
  COM_BINLOG_DUMP,

  COM_TABLE_DUMP,
  COM_CONNECT_OUT,
  COM_REGISTER_SLAVE,

  COM_STMT_PREPARE,
  COM_STMT_EXECUTE,
  COM_STMT_SEND_LONG_DATA,
  COM_STMT_CLOSE,

  COM_STMT_RESET,
  COM_SET_OPTION,
  COM_STMT_FETCH,
  COM_DAEMON,

  COM_BINLOG_DUMP_GTID,

  COM_RESET_CONNECTION,
  COM_END,

O
oceanbase-admin 已提交
76 77

  // for obproxy
W
wangzelin.wzl 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91
  // COM_DELETE_SESSION is not a standard mysql package type. This is a package used to process delete session
  // When the connection is disconnected, the session needs to be deleted, but at this time it may not be obtained in the callback function disconnect
  // Session lock, at this time, an asynchronous task will be added to the obmysql queue
  COM_DELETE_SESSION = OBPROXY_MYSQL_CMD_START,
  // COM_HANDSHAKE and COM_LOGIN are not standard mysql package types, they are used in ObProxy
  // COM_HANDSHAKE represents client---->on_connect && observer--->hand shake or error
  // COM_LOGIN represents client---->hand shake response && observer---> ok or error
  COM_HANDSHAKE,
  COM_LOGIN,

  COM_STMT_PREXECUTE = PREXECUTE_CMD,
  COM_STMT_SEND_PIECE_DATA,
  COM_STMT_GET_PIECE_DATA,
  COM_MAX_NUM
O
oceanbase-admin 已提交
92 93
};

O
obdev 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
enum class ObMySQLPacketType
{
  INVALID_PKT = 0,
  PKT_MYSQL,     // 1 -> mysql packet;
  PKT_OKP,       // 2 -> okp;
  PKT_ERR,       // 3 -> error packet;
  PKT_EOF,       // 4 -> eof packet;
  PKT_ROW,       // 5 -> row packet;
  PKT_FIELD,     // 6 -> field packet;
  PKT_PIECE,     // 7 -> piece packet;
  PKT_STR,       // 8 -> string packet;
  PKT_PREPARE,   // 9 -> prepare packet;
  PKT_RESHEAD,   // 10 -> result header packet
  PKT_PREXEC,    // 11 -> prepare execute packet;
  PKT_END        // 12 -> end of packet type
};

W
wangzelin.wzl 已提交
111 112 113 114
union ObServerStatusFlags
{
  ObServerStatusFlags() : flags_(0) {}
  explicit ObServerStatusFlags(uint16_t flag) : flags_(flag) {}
O
oceanbase-admin 已提交
115
  uint16_t flags_;
W
wangzelin.wzl 已提交
116 117 118 119 120 121 122 123 124
  //ref:http://dev.mysql.com/doc/internals/en/status-flags.html
  struct ServerStatusFlags
  {
    uint16_t OB_SERVER_STATUS_IN_TRANS:             1;  // a transaction is active
    uint16_t OB_SERVER_STATUS_AUTOCOMMIT:           1;  // auto-commit is enabled
    uint16_t OB_SERVER_STATUS_RESERVED_OR_ORACLE_MODE: 1;  // used for oracle_mode for login, reserved for other case
    uint16_t OB_SERVER_MORE_RESULTS_EXISTS:         1;
    uint16_t OB_SERVER_STATUS_NO_GOOD_INDEX_USED:   1;
    uint16_t OB_SERVER_STATUS_NO_INDEX_USED:        1;
O
oceanbase-admin 已提交
125
    // used by Binary Protocol Resultset to signal that
W
wangzelin.wzl 已提交
126 127 128 129 130 131 132 133 134 135
    // COM_STMT_FETCH has to be used to fetch the row-data.
    uint16_t OB_SERVER_STATUS_CURSOR_EXISTS:        1;
    uint16_t OB_SERVER_STATUS_LAST_ROW_SENT:        1;
    uint16_t OB_SERVER_STATUS_DB_DROPPED:           1;
    uint16_t OB_SERVER_STATUS_NO_BACKSLASH_ESCAPES: 1;
    uint16_t OB_SERVER_STATUS_METADATA_CHANGED:     1;
    uint16_t OB_SERVER_QUERY_WAS_SLOW:              1;
    uint16_t OB_SERVER_PS_OUT_PARAMS:               1;
    uint16_t OB_SERVER_STATUS_IN_TRANS_READONLY:    1;  // in a read-only transaction
    uint16_t OB_SERVER_SESSION_STATE_CHANGED:       1;  // connection state information has changed
O
oceanbase-admin 已提交
136 137 138 139
  } status_flags_;
};

// used for proxy, OCJ and observer to negotiate new features
W
wangzelin.wzl 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
union ObProxyCapabilityFlags
{
  ObProxyCapabilityFlags() : capability_(0) {}
  explicit ObProxyCapabilityFlags(uint64_t cap) : capability_(cap) {}
  bool is_checksum_support() const { return 1 == cap_flags_.OB_CAP_CHECKSUM; }
  bool is_safe_weak_read_support() const { return 1 == cap_flags_.OB_CAP_SAFE_WEAK_READ; }
  bool is_priority_hit_support() const { return 1 == cap_flags_.OB_CAP_PRIORITY_HIT; }
  bool is_checksum_swittch_support() const { return 1 == cap_flags_.OB_CAP_CHECKSUM_SWITCH; }
  bool is_extra_ok_packet_for_statistics_support() const { return 1 == cap_flags_.OB_CAP_EXTRA_OK_PACKET_FOR_STATISTICS; }
  bool is_cap_used() const { return 0 != capability_; }
  bool is_extra_ok_packet_for_ocj_support() const { return 1 == cap_flags_.OB_CAP_EXTRA_OK_PACKET_FOR_OCJ; }
  bool is_ob_protocol_v2_support() const { return 1 == cap_flags_.OB_CAP_OB_PROTOCOL_V2; }
  bool is_abundant_feedback_support() const { return 1 == cap_flags_.OB_CAP_ABUNDANT_FEEDBACK; }
  bool is_pl_route_support() const { return 1 == cap_flags_.OB_CAP_PL_ROUTE; }
  bool is_proxy_reroute_support() const { return 1 == cap_flags_.OB_CAP_PROXY_REROUTE; }
  bool is_full_link_trace_support() const { return 1 == cap_flags_.OB_CAP_PROXY_FULL_LINK_TRACING
                                                        && is_ob_protocol_v2_support(); }
  bool is_new_extra_info_support() const { return 1 == cap_flags_.OB_CAP_PROXY_NEW_EXTRA_INFO
                                                        && is_ob_protocol_v2_support(); }
159 160
  bool is_session_var_sync_support() const { return 1 == cap_flags_.OB_CAP_PROXY_SESSION_VAR_SYNC
                                                        && is_ob_protocol_v2_support(); }
O
oceanbase-admin 已提交
161 162

  uint64_t capability_;
W
wangzelin.wzl 已提交
163 164 165 166 167 168 169 170 171 172
  struct CapabilityFlags
  {
    uint64_t OB_CAP_PARTITION_TABLE:                   1;
    uint64_t OB_CAP_CHANGE_USER:                       1;
    uint64_t OB_CAP_READ_WEAK:                         1;
    uint64_t OB_CAP_CHECKSUM:                          1;
    uint64_t OB_CAP_SAFE_WEAK_READ:                    1;
    uint64_t OB_CAP_PRIORITY_HIT:                      1;
    uint64_t OB_CAP_CHECKSUM_SWITCH:                   1;
    uint64_t OB_CAP_EXTRA_OK_PACKET_FOR_OCJ:           1;
O
oceanbase-admin 已提交
173
    // used since oceanbase 2.0 and aimed to replace mysql compress protocol for its low performance
W
wangzelin.wzl 已提交
174 175 176
    uint64_t OB_CAP_OB_PROTOCOL_V2:                    1;
    // whether following an extra ok packet at the end of COM_STATISTICS response
    uint64_t OB_CAP_EXTRA_OK_PACKET_FOR_STATISTICS:    1;
O
oceanbase-admin 已提交
177
    // for more abundant inforation in feedback
W
wangzelin.wzl 已提交
178
    uint64_t OB_CAP_ABUNDANT_FEEDBACK:                 1;
O
oceanbase-admin 已提交
179
    // for pl route
W
wangzelin.wzl 已提交
180 181 182 183 184 185 186 187 188
    uint64_t OB_CAP_PL_ROUTE:                          1;

    uint64_t OB_CAP_PROXY_REROUTE:                     1;
   
    // for session_info sync
    uint64_t OB_CAP_PROXY_SESSIOIN_SYNC:               1;
    // for full trace_route
    uint64_t OB_CAP_PROXY_FULL_LINK_TRACING:           1;
    uint64_t OB_CAP_PROXY_NEW_EXTRA_INFO:              1;
189 190
    uint64_t OB_CAP_PROXY_SESSION_VAR_SYNC:            1;
    uint64_t OB_CAP_RESERVED_NOT_USE:                 47;
O
oceanbase-admin 已提交
191 192 193
  } cap_flags_;
};

W
wangzelin.wzl 已提交
194 195 196 197
union ObMySQLCapabilityFlags
{
  ObMySQLCapabilityFlags() : capability_(0) {}
  explicit ObMySQLCapabilityFlags(uint32_t cap) : capability_(cap) {}
O
oceanbase-admin 已提交
198
  uint32_t capability_;
W
wangzelin.wzl 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  //ref:http://dev.mysql.com/doc/internals/en/capability-flags.html
  struct CapabilityFlags
  {
    uint32_t OB_CLIENT_LONG_PASSWORD:                   1;
    uint32_t OB_CLIENT_FOUND_ROWS:                      1;
    uint32_t OB_CLIENT_LONG_FLAG:                       1;
    uint32_t OB_CLIENT_CONNECT_WITH_DB:                 1;
    uint32_t OB_CLIENT_NO_SCHEMA:                       1;
    uint32_t OB_CLIENT_COMPRESS:                        1;
    uint32_t OB_CLIENT_ODBC:                            1;
    uint32_t OB_CLIENT_LOCAL_FILES:                     1;
    uint32_t OB_CLIENT_IGNORE_SPACE:                    1;
    uint32_t OB_CLIENT_PROTOCOL_41:                     1;
    uint32_t OB_CLIENT_INTERACTIVE:                     1;
    uint32_t OB_CLIENT_SSL:                             1;
    uint32_t OB_CLIENT_IGNORE_SIGPIPE:                  1;
    uint32_t OB_CLIENT_TRANSACTIONS:                    1;
    uint32_t OB_CLIENT_RESERVED:                        1;
    uint32_t OB_CLIENT_SECURE_CONNECTION:               1;
    uint32_t OB_CLIENT_MULTI_STATEMENTS:                1;
    uint32_t OB_CLIENT_MULTI_RESULTS:                   1;
    uint32_t OB_CLIENT_PS_MULTI_RESULTS:                1;
    uint32_t OB_CLIENT_PLUGIN_AUTH:                     1;
    uint32_t OB_CLIENT_CONNECT_ATTRS:                   1;
    uint32_t OB_CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA:  1;
    uint32_t OB_CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS:    1;
    uint32_t OB_CLIENT_SESSION_TRACK:                   1;
    uint32_t OB_CLIENT_DEPRECATE_EOF:                   1;
    uint32_t OB_CLIENT_RESERVED_NOT_USE:                2;
    uint32_t OB_CLIENT_SUPPORT_ORACLE_MODE:             1;
    uint32_t OB_CLIENT_RETURN_HIDDEN_ROWID:             1;
    uint32_t OB_CLIENT_USE_LOB_LOCATOR:                 1;
    uint32_t OB_CLIENT_SSL_VERIFY_SERVER_CERT:          1;
    uint32_t OB_CLIENT_REMEMBER_OPTIONS:                1;
O
oceanbase-admin 已提交
233 234 235
  } cap_flags_;
};

W
wangzelin.wzl 已提交
236 237
enum ObClientCapabilityPos
{
O
oceanbase-admin 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  OB_CLIENT_LONG_PASSWORD_POS = 0,
  OB_CLIENT_FOUND_ROWS_POS,
  OB_CLIENT_LONG_FLAG_POS,
  OB_CLIENT_CONNECT_WITH_DB_POS,
  OB_CLIENT_NO_SCHEMA_POS,
  OB_CLIENT_COMPRESS_POS,
  OB_CLIENT_ODBC_POS,
  OB_CLIENT_LOCAL_FILES_POS,
  OB_CLIENT_IGNORE_SPACE_POS,
  OB_CLIENT_PROTOCOL_41_POS,
  OB_CLIENT_INTERACTIVE_POS,
  OB_CLIENT_SSL_POS,
  OB_CLIENT_IGNORE_SIGPIPE_POS,
  OB_CLIENT_TRANSACTION_POS,
  OB_CLIENT_RESERVED_POS,
  OB_CLIENT_SECURE_CONNECTION_POS,
  OB_CLIENT_MULTI_STATEMENTS_POS,
  OB_CLIENT_MULTI_RESULTS_POS,
  OB_CLIENT_PS_MULTI_RESULTS_POS,
  OB_CLIENT_PLUGIN_AUTH_POS,
  OB_CLIENT_CONNECT_ATTRS_POS,
  OB_CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA_POS,
  OB_CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS_POS,
  OB_CLIENT_SESSION_TRACK_POS,
  OB_CLIENT_DEPRECATE_EOF_POS,
W
wangzelin.wzl 已提交
263
  //RESERVED 2
O
oceanbase-admin 已提交
264 265 266 267 268 269 270
  OB_CLIENT_SUPPORT_ORACLE_MODE_POS = 27,
  OB_CLIENT_RETURN_ROWID_POS = 28,
  OB_CLIENT_USE_LOB_LOCATOR_POS = 29,
  OB_CLIENT_SSL_VERIFY_SERVER_CERT_POS = 30,
  OB_CLIENT_REMEMBER_OPTIONS_POS = 31,
};

W
wangzelin.wzl 已提交
271 272
enum ObServerStatusFlagsPos
{
O
oceanbase-admin 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  OB_SERVER_STATUS_IN_TRANS_POS = 0,
  OB_SERVER_STATUS_AUTOCOMMIT_POS,
  OB_SERVER_STATUS_RESERVED_OR_ORACLE_MODE_POS,
  OB_SERVER_MORE_RESULTS_EXISTS_POS,
  OB_SERVER_STATUS_NO_GOOD_INDEX_USED_POS,
  OB_SERVER_STATUS_NO_INDEX_USED_POS,
  OB_SERVER_STATUS_CURSOR_EXISTS_POS,
  OB_SERVER_STATUS_LAST_ROW_SENT_POS,
  OB_SERVER_STATUS_DB_DROPPED_POS,
  OB_SERVER_STATUS_NO_BACKSLASH_ESCAPES_POS,
  OB_SERVER_STATUS_METADATA_CHANGED_POS,
  OB_SERVER_QUERY_WAS_SLOW_POS,
  OB_SERVER_PS_OUT_PARAMS_POS,
  OB_SERVER_STATUS_IN_TRANS_READONLY_POS,
  OB_SERVER_SESSION_STATE_CHANGED_POS,
};

W
wangzelin.wzl 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
char const *get_mysql_cmd_str(ObMySQLCmd mysql_cmd);

//http://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_current-user
enum ObInformationFunctions
{
  BENCHMARK_FUNC = 0,     // Repeatedly execute an expression
  CHARSET_FUNC,           // Return the character set of the argument
  COERCIBILITY_FUNC,      // Return the collation coercibility value of the string argument
  COLLATION_FUNC,         // Return the collation of the string argument
  CONNECTION_ID_FUNC,     // Return the connection ID (thread ID) for the connection
  CURRENT_USER_FUNC,      // The authenticated user name and host name
  DATABASE_FUNC,          // Return the default (current) database name
  FOUND_ROWS_FUNC,        // For a SELECT with a LIMIT clause, the number of rows
                          // that would be returned were there no LIMIT clause
  LAST_INSERT_ID_FUNC,    // Value of the AUTOINCREMENT column for the last INSERT
  ROW_COUNT_FUNC,         // The number of rows updated
  SCHEAM_FUNC,            // Synonym for DATABASE()
  SESSION_USER_FUNC,      // Synonym for USER()
  SYSTEM_USER_FUNC,       // Synonym for USER()
  USER_FUNC,              // The user name and host name provided by the client
  VERSION_FUNC,           // Return a string that indicates the MySQL server version
  MAX_INFO_FUNC           // end
O
oceanbase-admin 已提交
312 313
};

W
wangzelin.wzl 已提交
314
char const *get_info_func_name(const ObInformationFunctions func);
O
oceanbase-admin 已提交
315

W
wangzelin.wzl 已提交
316 317 318
template<class K, class V>
class ObCommonKV
{
G
gm 已提交
319
public:
W
wangzelin.wzl 已提交
320
  ObCommonKV() : key_(), value_() {}
O
oceanbase-admin 已提交
321 322 323 324 325 326 327 328 329 330
  void reset()
  {
    key_.reset();
    value_.reset();
  }
  K key_;
  V value_;
  TO_STRING_KV(K_(key), K_(value));
};

W
wangzelin.wzl 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
struct Ob20ExtraInfo
{
public:
  uint32_t extra_len_;
  bool exist_trace_info_;
  ObString trace_info_;
  // add key name
  static constexpr const char SYNC_SESSION_INFO[] = "sess_inf";
  static constexpr const char FULL_LINK_TRACE[] = "full_trc";

  // def value
  ObString sync_sess_info_;
  ObString full_link_trace_;

public:
  Ob20ExtraInfo() : extra_len_(0), exist_trace_info_(false) {}
  ~Ob20ExtraInfo() {}
  void reset() {
    extra_len_ = 0;
    exist_trace_info_ = false;
    trace_info_.reset();
    sync_sess_info_.reset();
    full_link_trace_.reset();
  }
  bool exist_sync_sess_info() { return !sync_sess_info_.empty(); }
  bool exist_full_link_trace() { return !full_link_trace_.empty(); }
  ObString& get_sync_sess_info() { return sync_sess_info_; }
  ObString& get_full_link_trace() { return full_link_trace_; }
  bool exist_sync_sess_info() const { return !sync_sess_info_.empty(); }
  bool exist_full_link_trace() const { return !full_link_trace_.empty(); }
  const ObString& get_sync_sess_info() const { return sync_sess_info_; }
  const ObString& get_full_link_trace() const { return full_link_trace_; }
363 364 365 366 367
  bool exist_extra_info() {return !sync_sess_info_.empty() || !full_link_trace_.empty() || exist_trace_info_;}
  bool exist_extra_info() const {return !sync_sess_info_.empty() || !full_link_trace_.empty() || exist_trace_info_;}
  int assign(const Ob20ExtraInfo &other, char* buf, int64_t len);
  int64_t get_total_len() {return trace_info_.length() + sync_sess_info_.length() + full_link_trace_.length();}
  int64_t get_total_len() const {return trace_info_.length() + sync_sess_info_.length() + full_link_trace_.length();}
W
wangzelin.wzl 已提交
368 369 370 371
  TO_STRING_KV(K_(extra_len), K_(exist_trace_info), K_(trace_info),
               K_(sync_sess_info), K_(full_link_trace));
};

O
oceanbase-admin 已提交
372 373 374 375
typedef ObCommonKV<common::ObString, common::ObString> ObStringKV;

static const int64_t MAX_STORE_LENGTH = 9;

W
wangzelin.wzl 已提交
376 377
class ObMySQLPacketHeader
{
G
gm 已提交
378
public:
W
wangzelin.wzl 已提交
379 380 381
  ObMySQLPacketHeader()
      : len_(0), seq_(0)
  { }
O
oceanbase-admin 已提交
382

W
wangzelin.wzl 已提交
383
  void reset() {
O
oceanbase-admin 已提交
384 385 386 387 388 389
    len_ = 0;
    seq_ = 0;
  }

  TO_STRING_KV("length", len_, "sequence", seq_);

G
gm 已提交
390
public:
W
wangzelin.wzl 已提交
391 392
  uint32_t len_;         // MySQL packet length not include packet header
  uint8_t  seq_;         // MySQL packet sequence
O
oceanbase-admin 已提交
393 394 395 396 397 398 399 400
};

/*
 * when use compress, packet header looks like:
 *  3B  length of compressed payload
 *  1B  compressed sequence id
 *  3B  length of payload before compression
 */
W
wangzelin.wzl 已提交
401 402
class ObMySQLCompressedPacketHeader
{
G
gm 已提交
403
public:
W
wangzelin.wzl 已提交
404 405 406
  ObMySQLCompressedPacketHeader()
      : comp_len_(0), comp_seq_(0), uncomp_len(0)
  { }
O
oceanbase-admin 已提交
407

W
wangzelin.wzl 已提交
408 409 410
  TO_STRING_KV("compressed_length", comp_len_,
               "compressed_sequence", comp_seq_,
               "length_before_compression", uncomp_len);
O
oceanbase-admin 已提交
411

G
gm 已提交
412
public:
W
wangzelin.wzl 已提交
413 414 415
  uint32_t comp_len_;         // length of compressed payload, not include packet header
  uint8_t  comp_seq_;         // compressed sequence id
  uint32_t uncomp_len;        // length of payload before compressio
O
oceanbase-admin 已提交
416 417
};

W
wangzelin.wzl 已提交
418 419 420
class ObMySQLPacket
    : public rpc::ObPacket
{
G
gm 已提交
421
public:
W
wangzelin.wzl 已提交
422 423
  ObMySQLPacket()
      : hdr_(), cdata_(NULL), is_packed_(false)
O
oceanbase-admin 已提交
424
  {}
W
wangzelin.wzl 已提交
425
  virtual ~ObMySQLPacket() {}
O
oceanbase-admin 已提交
426

W
wangzelin.wzl 已提交
427 428 429
  static int store_string_kv(char *buf, int64_t len, const ObStringKV &str, int64_t &pos);
  static uint64_t get_kv_encode_len(const ObStringKV &string_kv);
  inline static ObStringKV get_separator_kv(); // separator for system variables and user variables
O
oceanbase-admin 已提交
430 431 432

  inline void set_seq(uint8_t seq);
  inline uint8_t get_seq(void) const;
W
wangzelin.wzl 已提交
433 434
  inline void set_content(const char *content, uint32_t len);
  inline const char *get_cdata() const { return cdata_; }
O
oceanbase-admin 已提交
435 436

  virtual int64_t get_serialize_size() const;
W
wangzelin.wzl 已提交
437 438
  int encode(char *buffer, int64_t length, int64_t &pos, int64_t &pkt_count) const;
  int encode(char *buffer, int64_t length, int64_t &pos);
O
obdev 已提交
439
  int get_pkt_len() { return hdr_.len_; }
W
wangzelin.wzl 已提交
440
  virtual int decode() { return common::OB_NOT_SUPPORTED; }
O
obdev 已提交
441
  virtual ObMySQLPacketType get_mysql_packet_type() { return ObMySQLPacketType::INVALID_PKT; }
O
oceanbase-admin 已提交
442 443 444 445 446 447 448

  virtual void reset()
  {
    hdr_.reset();
    cdata_ = NULL;
  }

W
wangzelin.wzl 已提交
449
  virtual void assign(const ObMySQLPacket &other) {
O
oceanbase-admin 已提交
450 451 452 453 454 455 456
    cdata_ = other.cdata_;
    hdr_.len_ = other.hdr_.len_;
    hdr_.seq_ = other.hdr_.seq_;
  }

  VIRTUAL_TO_STRING_KV("header", hdr_);

G
gm 已提交
457
protected:
W
wangzelin.wzl 已提交
458

O
oceanbase-admin 已提交
459 460 461 462 463
  virtual int serialize(char*, const int64_t, int64_t&) const
  {
    return common::OB_NOT_SUPPORTED;
  }

G
gm 已提交
464
public:
O
oceanbase-admin 已提交
465 466 467 468 469 470 471 472 473
  static const int64_t HANDSHAKE_RESPONSE_RESERVED_SIZE = 23;
  // 4: capability flags
  // 4: max-packet size
  // 1: character set
  static const int64_t HANDSHAKE_RESPONSE_MIN_SIZE = 9 + HANDSHAKE_RESPONSE_RESERVED_SIZE;
  // 4: capability flags
  static const int64_t JDBC_SSL_MIN_SIZE = 4;
  // 2: capability flags
  static const int64_t MIN_CAPABILITY_SIZE = 2;
W
wangzelin.wzl 已提交
474 475
  bool is_packed() const { return is_packed_; }
  void set_is_packed(const bool is_packed) { is_packed_ = is_packed; }
G
gm 已提交
476
protected:
O
oceanbase-admin 已提交
477
  ObMySQLPacketHeader hdr_;
W
wangzelin.wzl 已提交
478 479 480
  const char *cdata_;
  //parallel encoding of output_expr in advance to speed up packet response
  bool is_packed_;
O
oceanbase-admin 已提交
481 482
};

W
wangzelin.wzl 已提交
483 484 485
class ObMySQLRawPacket
    : public ObMySQLPacket
{
G
gm 已提交
486
public:
W
wangzelin.wzl 已提交
487 488
  ObMySQLRawPacket() : ObMySQLPacket(), cmd_(COM_MAX_NUM),
                       can_reroute_pkt_(false),
489
                       is_weak_read_(false),
490
                       txn_free_route_(false),
W
wangzelin.wzl 已提交
491
                       extra_info_()
O
oceanbase-admin 已提交
492 493 494
  {}

  explicit ObMySQLRawPacket(obmysql::ObMySQLCmd cmd)
W
wangzelin.wzl 已提交
495 496
    : ObMySQLPacket(), cmd_(cmd),
      can_reroute_pkt_(false),
497
      is_weak_read_(false),
498
      txn_free_route_(false),
W
wangzelin.wzl 已提交
499
      extra_info_()
O
oceanbase-admin 已提交
500 501
  {}

W
wangzelin.wzl 已提交
502
  virtual ~ObMySQLRawPacket() {}
O
oceanbase-admin 已提交
503 504 505 506

  inline void set_cmd(ObMySQLCmd cmd);
  inline ObMySQLCmd get_cmd() const;

W
wangzelin.wzl 已提交
507
  inline const char *get_cdata() const;
O
oceanbase-admin 已提交
508 509 510 511 512
  inline uint32_t get_clen() const;

  inline void set_can_reroute_pkt(const bool can_rerute);
  inline bool can_reroute_pkt() const;

513 514 515
  inline void set_is_weak_read(const bool v) { is_weak_read_ = v; }
  inline bool is_weak_read() const { return is_weak_read_; }

516 517 518
  inline void set_txn_free_route(const bool txn_free_route);
  inline bool txn_free_route() const;

W
wangzelin.wzl 已提交
519 520
  inline const Ob20ExtraInfo &get_extra_info() const { return extra_info_; }
  bool exist_trace_info() const { return extra_info_.exist_trace_info_; }
521
  bool exist_extra_info() const { return extra_info_.exist_extra_info(); }
W
wangzelin.wzl 已提交
522
  const common::ObString &get_trace_info() const { return extra_info_.trace_info_; }
O
oceanbase-admin 已提交
523 524
  virtual int64_t get_serialize_size() const;

W
wangzelin.wzl 已提交
525
  virtual void reset() {
O
oceanbase-admin 已提交
526
    ObMySQLPacket::reset();
W
wangzelin.wzl 已提交
527
    cmd_ = COM_MAX_NUM;
O
oceanbase-admin 已提交
528
    can_reroute_pkt_ = false;
529
    is_weak_read_ = false;
530
    txn_free_route_ = false;
W
wangzelin.wzl 已提交
531
    extra_info_.reset();
O
oceanbase-admin 已提交
532 533
  }

W
wangzelin.wzl 已提交
534
  virtual void assign(const ObMySQLRawPacket &other)
O
oceanbase-admin 已提交
535 536 537 538
  {
    ObMySQLPacket::assign(other);
    cmd_ = other.cmd_;
    can_reroute_pkt_ = other.can_reroute_pkt_;
539
    is_weak_read_ = other.is_weak_read_;
540
    txn_free_route_ = other.txn_free_route_;
W
wangzelin.wzl 已提交
541
    extra_info_ = other.extra_info_;
O
oceanbase-admin 已提交
542 543
  }

544
  TO_STRING_KV("header", hdr_, "can_reroute", can_reroute_pkt_, "weak_read", is_weak_read_);
G
gm 已提交
545
protected:
O
oceanbase-admin 已提交
546 547
  virtual int serialize(char*, const int64_t, int64_t&) const;

G
gm 已提交
548
private:
O
oceanbase-admin 已提交
549
  void set_len(uint32_t len);
G
gm 已提交
550
private:
O
oceanbase-admin 已提交
551 552
  ObMySQLCmd cmd_;
  bool can_reroute_pkt_;
553
  bool is_weak_read_;
554
  bool txn_free_route_;
555
public:
W
wangzelin.wzl 已提交
556
  Ob20ExtraInfo extra_info_;
O
oceanbase-admin 已提交
557 558
};

W
wangzelin.wzl 已提交
559 560 561
class ObMySQLCompressedPacket
    : public rpc::ObPacket
{
G
gm 已提交
562
public:
W
wangzelin.wzl 已提交
563 564
  ObMySQLCompressedPacket()
      : hdr_(), cdata_(NULL)
O
oceanbase-admin 已提交
565
  {}
W
wangzelin.wzl 已提交
566 567 568 569 570 571 572 573
  virtual ~ObMySQLCompressedPacket() {}

  inline uint32_t get_comp_len() const { return hdr_.comp_len_; }
  inline uint8_t get_comp_seq() const { return hdr_.comp_seq_; }
  inline uint32_t get_uncomp_len() const { return hdr_.uncomp_len; }
  inline const char *get_cdata() const { return cdata_; }
  inline void set_content(const char *content, const uint32_t comp_len,
                          const uint8_t comp_seq, const uint32_t uncomp_len)
O
oceanbase-admin 已提交
574 575 576 577 578 579 580 581 582
  {
    cdata_ = content;
    hdr_.comp_len_ = comp_len;
    hdr_.comp_seq_ = comp_seq;
    hdr_.uncomp_len = uncomp_len;
  }

  VIRTUAL_TO_STRING_KV("header", hdr_);

G
gm 已提交
583
protected:
O
oceanbase-admin 已提交
584
  ObMySQLCompressedPacketHeader hdr_;
W
wangzelin.wzl 已提交
585
  const char *cdata_;
O
oceanbase-admin 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
};

ObStringKV ObMySQLPacket::get_separator_kv()
{
  static ObStringKV separator_kv;
  separator_kv.key_ = common::ObString::make_string("__NULL");
  separator_kv.value_ = common::ObString::make_string("__NULL");
  return separator_kv;
}

void ObMySQLPacket::set_seq(uint8_t seq)
{
  hdr_.seq_ = seq;
}

uint8_t ObMySQLPacket::get_seq(void) const
{
  return hdr_.seq_;
}

W
wangzelin.wzl 已提交
606
void ObMySQLPacket::set_content(const char *content, uint32_t len)
O
oceanbase-admin 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
{
  cdata_ = content;
  hdr_.len_ = len;
}

void ObMySQLRawPacket::set_cmd(ObMySQLCmd cmd)
{
  cmd_ = cmd;
}

ObMySQLCmd ObMySQLRawPacket::get_cmd() const
{
  return cmd_;
}

W
wangzelin.wzl 已提交
622
inline const char *ObMySQLRawPacket::get_cdata() const
O
oceanbase-admin 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
{
  return cdata_;
}

inline uint32_t ObMySQLRawPacket::get_clen() const
{
  return hdr_.len_;
}

inline void ObMySQLRawPacket::set_can_reroute_pkt(const bool can_reroute)
{
  can_reroute_pkt_ = can_reroute;
}

inline bool ObMySQLRawPacket::can_reroute_pkt() const
{
  return can_reroute_pkt_;
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655
union ObClientAttributeCapabilityFlags
{
  ObClientAttributeCapabilityFlags() : capability_(0) {}
  explicit ObClientAttributeCapabilityFlags(uint64_t cap) : capability_(cap) {}
  bool is_support_lob_locatorv2() const { return 1 == cap_flags_.OB_CLIENT_CAP_OB_LOB_LOCATOR_V2; }

  uint64_t capability_;
  struct CapabilityFlags
  {
    uint64_t OB_CLIENT_CAP_OB_LOB_LOCATOR_V2:       1;
    uint64_t OB_CLIENT_CAP_RESERVED_NOT_USE:       63;
  } cap_flags_;
};

656 657 658 659 660 661 662 663 664 665
inline void ObMySQLRawPacket::set_txn_free_route(const bool txn_free_route)
{
  txn_free_route_ = txn_free_route;
}

inline bool ObMySQLRawPacket::txn_free_route() const
{
  return txn_free_route_;
}

W
wangzelin.wzl 已提交
666 667
} // end of namespace obmysql
} // end of namespace oceanbase
O
oceanbase-admin 已提交
668 669

#endif /* _OB_MYSQL_PACKET_H_ */