ob_dup_table_lease.h 9.3 KB
Newer Older
K
KyrielightWei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Copyright (c) 2021 OceanBase
// OceanBase 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 OCEANBASE_TRANSACTION_DUP_TABLE_LEASE_H
#define OCEANBASE_TRANSACTION_DUP_TABLE_LEASE_H

#include "lib/hash/ob_hashmap.h"
#include "lib/list/ob_dlist.h"
#include "lib/lock/ob_spin_rwlock.h"
#include "lib/net/ob_addr.h"
#include "storage/tx/ob_dup_table_base.h"
#include "storage/tx/ob_dup_table_stat.h"


namespace oceanbase
{
namespace logservice
{
class ObLogHandler;
}
namespace transaction
{

class ObDupTableLSHandler;
class ObDupTableLeaseRequest;
33
class ObLSDupTableMeta;
K
KyrielightWei 已提交
34 35 36 37 38 39 40 41

class ObDupTableLSLeaseMgr
{
public:
  OB_UNIS_VERSION(1);

public:
  static const int64_t LEASE_UNIT;
42
  static int64_t DEFAULT_LEASE_INTERVAL;
K
KyrielightWei 已提交
43 44 45 46 47 48
  static const int64_t MIN_LEASE_INTERVAL;

  TO_STRING_KV(K(leader_lease_map_.size()), K(follower_lease_info_));

public:
  ObDupTableLSLeaseMgr() : lease_diag_info_log_buf_(nullptr) { reset(); }
49
  ~ObDupTableLSLeaseMgr() { destroy(); }
K
KyrielightWei 已提交
50 51

  int init(ObDupTableLSHandler *dup_ls_handle);
52
  int offline();
K
KyrielightWei 已提交
53 54 55 56 57 58 59 60 61 62 63
  void destroy() { reset(); };
  void reset();
  bool is_master() { return ATOMIC_LOAD(&is_master_); }

  // handle lease requests
  int leader_handle(bool &need_log);
  // post lease requests
  int follower_handle();

  int follower_try_acquire_lease(const share::SCN &lease_log_scn);

64
  int receive_lease_request(const ObDupTableLeaseRequest &lease_req);
K
KyrielightWei 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  int leader_takeover(bool is_resume);
  int leader_revoke();

  int prepare_serialize(int64_t &max_ser_size, DupTableLeaseItemArray &lease_header_array);
  int serialize_lease_log(const DupTableLeaseItemArray &unique_id_array,
                          char *buf,
                          const int64_t buf_len,
                          int64_t &pos);
  int deserialize_lease_log(DupTableLeaseItemArray &lease_header_array,
                            const char *buf,
                            const int64_t data_len,
                            int64_t &pos);

  int lease_log_submitted(const bool submit_result,
                          const share::SCN &lease_log_scn,
                          const bool for_replay,
                          const DupTableLeaseItemArray &lease_header_array);
  int lease_log_synced(const bool sync_result,
                       const share::SCN &lease_log_scn,
                       const bool for_replay,
                       const DupTableLeaseItemArray &lease_header_array);

  // int log_cb_success();
  // int log_cb_failure();

  int get_lease_valid_array(LeaseAddrArray &lease_array);

93 94
  bool is_follower_lease_valid();

K
KyrielightWei 已提交
95 96 97 98 99 100 101
  bool check_follower_lease_serving(const bool election_is_leader,
                                    const share::SCN &max_replayed_scn);

  void print_lease_diag_info_log(const bool is_master);

  int get_lease_mgr_stat(FollowerLeaseMgrStatArr &collect_arr);

102
  int recover_lease_from_ckpt(const ObDupTableLSCheckpoint::ObLSDupTableMeta &dup_ls_meta);
K
KyrielightWei 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
private:
  bool can_grant_lease_(const common::ObAddr &addr,
                        const share::SCN &local_max_applyed_scn,
                        const DupTableLeaderLeaseInfo &lease_info);
  int update_durable_lease_info_(DupTableLeaderLeaseInfo &single_lease_info);
  int handle_lease_req_cache_(int64_t loop_start_time,
                              const share::SCN &local_max_applyed_scn,
                              const common::ObAddr &addr,
                              DupTableLeaderLeaseInfo &single_lease_info);

  int submit_lease_log_();

  // update request_ts_
  // 1. the follower try to get lease for th first time (request_ts_ = 0)
  // 2. the follower get lease failed for a long time
  void update_request_ts_(int64_t loop_start_time);

  bool need_post_lease_request_(int64_t loop_start_time);

  bool need_retry_lease_operation_(const int64_t cur_time, const int64_t last_time);

private:
  class LeaseReqCacheHandler
  {
  public:
    LeaseReqCacheHandler(ObDupTableLSLeaseMgr *lease_mgr,
                         int64_t loop_start_time,
                         const share::SCN &max_applyed_scn,
                         DupTableLeaseItemArray &item_array)
        : lease_item_array_(item_array)
    {
      lease_mgr_ptr_ = lease_mgr;
      lease_item_array_.reuse();
      renew_lease_count_ = 0;
      max_ser_size_ = 0;
      local_max_applyed_scn_ = max_applyed_scn;
      loop_start_time_ = loop_start_time;
      error_ret = OB_SUCCESS;
    }
    bool operator()(common::hash::HashMapPair<common::ObAddr, DupTableLeaderLeaseInfo> &hash_pair);
    int64_t get_max_ser_size() { return max_ser_size_; }
    int get_error_ret() { return error_ret; }
    int64_t get_renew_lease_count() { return renew_lease_count_; }

147 148 149 150 151 152
    void clear_ser_content()
    {
      lease_item_array_.reuse();
      max_ser_size_ = 0;
    }

K
KyrielightWei 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 233 234 235 236 237 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    TO_STRING_KV(K(renew_lease_count_),
                 K(max_ser_size_),
                 K(loop_start_time_),
                 K(local_max_applyed_scn_),
                 K(error_ret));

  private:
    ObDupTableLSLeaseMgr *lease_mgr_ptr_;
    DupTableLeaseItemArray &lease_item_array_;
    int64_t renew_lease_count_;
    int64_t max_ser_size_;
    int64_t loop_start_time_;
    share::SCN local_max_applyed_scn_;
    int error_ret;
  };

  class GetLeaseValidAddrFunctor
  {
  public:
    GetLeaseValidAddrFunctor(LeaseAddrArray &addr_arr) : addr_arr_(addr_arr), cur_time_(INT64_MAX)
    {}
    int operator()(common::hash::HashMapPair<common::ObAddr, DupTableLeaderLeaseInfo> &hash_pair);

  private:
    LeaseAddrArray &addr_arr_;
    int64_t cur_time_;
  };

  class DiagInfoGenerator
  {
  public:
    DiagInfoGenerator(bool need_cache, char *info_buf, int64_t info_buf_len, int64_t cur_time)
        : need_cache_(need_cache), info_buf_(info_buf), info_buf_len_(info_buf_len),
          info_buf_pos_(0), cur_time_(cur_time)
    {}

    int64_t get_buf_pos() { return info_buf_pos_; }

    int
    operator()(const common::hash::HashMapPair<common::ObAddr, DupTableLeaderLeaseInfo> &hash_pair);

  private:
    bool need_cache_;
    char *info_buf_;
    int64_t info_buf_len_;
    int64_t info_buf_pos_;
    int64_t cur_time_;
  };

  class LeaderActiveLeaseFunctor
  {
  public:
    LeaderActiveLeaseFunctor() : cur_time_(INT64_MAX) {}
    int operator()(common::hash::HashMapPair<common::ObAddr, DupTableLeaderLeaseInfo> &hash_pair);

  private:
    int64_t cur_time_;
  };

  // class LeaseDurableHandler
  // {
  // public:
  //   LeaseDurableHandler(bool success, const DupTableLeaseItemArray &lease_array)
  //       : is_success_(success), item_array_(lease_array)
  //   {}
  //
  //   int operator()(common::hash::HashMapPair<common::ObAddr, DupTableLeaderLeaseInfo>
  //   &hash_pair);
  //
  // private:
  //   bool is_success_;
  //   const DupTableLeaseItemArray &item_array_;
  // };

  class LeaderLeaseInfoSerCallBack : public IHashSerCallBack
  {
  public:
    LeaderLeaseInfoSerCallBack(char *buf, int64_t buf_len, int64_t pos)
        : IHashSerCallBack(buf, buf_len, pos)
    {}
    int
    operator()(const common::hash::HashMapPair<common::ObAddr, DupTableLeaderLeaseInfo> &hash_pair);
  };

  class LeaderLeaseInfoDeSerCallBack : public IHashDeSerCallBack
  {
  public:
    LeaderLeaseInfoDeSerCallBack(const char *buf, int64_t buf_len, int64_t pos)
        : IHashDeSerCallBack(buf, buf_len, pos)
    {}
    int operator()(DupTableLeaderLeaseMap &lease_map);
  };

  class LeaderLeaseInfoGetSizeCallBack
  {
  public:
    int64_t
    operator()(const common::hash::HashMapPair<common::ObAddr, DupTableLeaderLeaseInfo> &hash_pair);
  };

  class LeaderLeaseMgrStatFunctor
  {
  public:
    LeaderLeaseMgrStatFunctor(FollowerLeaseMgrStatArr &collect_arr,
                              const uint64_t tenant_id,
                              const int64_t collect_ts,
                              const ObAddr &leader_addr,
                              const share::ObLSID ls_id)
                              :
                              collect_arr_(collect_arr),
                              tenant_id_(tenant_id),
                              collect_ts_(collect_ts),
                              leader_addr_(leader_addr),
                              ls_id_(ls_id),
                              cnt_(0) {}
    int operator()(const common::hash::HashMapPair<common::ObAddr, DupTableLeaderLeaseInfo> &hash_pair);

  private:
    FollowerLeaseMgrStatArr &collect_arr_;
    uint64_t tenant_id_;
    int64_t collect_ts_;
    const ObAddr leader_addr_;
    share::ObLSID ls_id_;
    int cnt_;
  };

private:
  SpinRWLock lease_lock_;

  share::ObLSID ls_id_;
  bool is_master_;
  bool is_stopped_;

  // bool is_serializing_; // TODO use lease array to serialize

  ObDupTableLSHandler *dup_ls_handle_ptr_;

  DupTableLeaderLeaseMap leader_lease_map_;

  // int64_t self_request_ts_;
  // DupTableLeaseInfo follower_lease_info_;
  DupTableFollowerLeaseInfo follower_lease_info_;

  int64_t last_lease_req_post_time_;
  int64_t last_lease_req_cache_handle_time_;

  char *lease_diag_info_log_buf_;
};

} // namespace transaction
} // namespace oceanbase
#endif