sender_routine.cpp 6.1 KB
Newer Older
W
init  
wangyunlai.wyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase Migration Service LogProxy 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.
 */

#include <assert.h>

#include "LogMsgBuf.h"
16
#include "LogRecord.h"
W
init  
wangyunlai.wyl 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30

#include "common/log.h"
#include "common/config.h"
#include "common/counter.h"
#include "codec/encoder.h"
#include "communication/communicator.h"
#include "oblogreader/oblogreader.h"
#include "oblogreader/sender_routine.h"

namespace oceanbase {
namespace logproxy {

static Config& _s_config = Config::instance();

31 32 33 34
#ifdef LOGMSG_BY_LIBOBLOG
static __thread LogMsgBuf* _t_s_lmb = nullptr;
#endif

F
Fankux 已提交
35 36
SenderRoutine::SenderRoutine(ObLogReader& reader, OblogAccess& oblog, BlockingQueue<ILogRecord*>& rqueue)
    : Thread("SenderRoutine"), _reader(reader), _oblog(oblog), _rqueue(rqueue)
W
init  
wangyunlai.wyl 已提交
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 76
{}

int SenderRoutine::init(MessageVersion packet_version, Channel* ch)
{
  _packet_version = packet_version;
  _client_peer = ch->peer();

  if (_s_config.readonly.val()) {
    return OMS_OK;
  }

  int ret = _comm.init();
  if (ret != OMS_OK) {
    OMS_ERROR << "Failed to init Sender Routine caused by failed to init communication, ret: " << ret;
    return ret;
  }

  ret = _comm.add_channel(_client_peer, ch);
  if (ret == OMS_FAILED) {
    OMS_ERROR << "Failed to init Sender Routine caused by failed to add channel, ret: " << ret
              << ", peer: " << _client_peer.id();
    return OMS_FAILED;
  }
  return OMS_OK;
}

void SenderRoutine::stop()
{
  if (is_run()) {
    Thread::stop();
    if (_s_config.readonly.val()) {
      return;
    }
    _comm.clear_channels();
    _comm.stop();
  }
}

void SenderRoutine::run()
{
77
  LogMsgLocalInit;
W
init  
wangyunlai.wyl 已提交
78 79 80 81 82

  std::vector<ILogRecord*> records;
  records.reserve(_s_config.read_wait_num.val());

  while (is_run()) {
F
Fankux 已提交
83

W
init  
wangyunlai.wyl 已提交
84 85 86 87 88 89 90 91 92
    if (!_s_config.readonly.val()) {
      int ret = _comm.poll();
      if (ret != OMS_OK) {
        OMS_ERROR << "Failed to run Sender Routine caused by failed to poll communication, ret: " << ret;
        stop();
        break;
      }
    }

F
Fankux 已提交
93
    _stage_timer.reset();
W
init  
wangyunlai.wyl 已提交
94 95 96 97
    records.clear();
    while (!_rqueue.poll(records, _s_config.read_timeout_us.val()) || records.empty()) {
      OMS_INFO << "send transfer queue empty, retry...";
    }
F
Fankux 已提交
98 99
    int64_t poll_us = _stage_timer.elapsed();
    Counter::instance().count_key(Counter::SENDER_POLL_US, poll_us);
W
init  
wangyunlai.wyl 已提交
100 101 102 103 104 105 106 107

    for (size_t i = 0; i < records.size(); ++i) {
      ILogRecord* record = records[i];
      assert(record != nullptr);

      if (_s_config.verbose_packet.val()) {
        OMS_INFO << "fetch a records(" << (i + 1) << "/" << records.size() << ") from liboblog: "
                 << "size:" << record->getRealSize() << ", record_type:" << record->recordType()
F
Fankux 已提交
108
                 << ", timestamp:" << record->getTimestamp() << ", checkpoint:" << record->getCheckpoint1()
W
init  
wangyunlai.wyl 已提交
109 110 111 112 113 114
                 << ", dbname:" << record->dbname() << ", tbname:" << record->tbname()
                 << ", queue size:" << _rqueue.size(false);
      }
      if (_s_config.readonly.val()) {
        Counter::instance().count_write(1);
        Counter::instance().mark_timestamp(record->getTimestamp());
F
Fankux 已提交
115
        Counter::instance().mark_checkpoint(record->getCheckpoint1());
W
init  
wangyunlai.wyl 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129
        _oblog.release(record);
      }
    }

    if (_s_config.readonly.val()) {
      continue;
    }

    size_t packet_size = 0;
    size_t offset = 0;
    size_t i = 0;
    for (i = 0; i < records.size(); ++i) {
      ILogRecord* r = records[i];
      size_t size = 0;
130 131 132
#ifdef LOGMSG_BY_LIBOBLOG
      const char* rbuf = r->toString(&size, _t_s_lmb, true);
#else
F
Fankux 已提交
133
      const char* rbuf = r->toString(&size, true);
134
#endif
F
Fankux 已提交
135 136 137 138 139
      if (rbuf == nullptr) {
        OMS_ERROR << "failed parse logmsg Record, !!!EXIT!!!";
        stop();
        break;
      }
W
init  
wangyunlai.wyl 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 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

      if (packet_size + size > _s_config.max_packet_bytes.val()) {
        if (packet_size == 0) {
          OMS_WARN << "Huge package occured, size of: " << size
                   << " just exceed max_packet_bytes: " << _s_config.max_packet_bytes.val() << ", try to send";
          if (do_send(records, i, 1) != OMS_OK) {
            OMS_ERROR << "Failed to write LogMessage to client: " << _client_peer.to_string();
            stop();
            break;
          }
          offset = i + 1;

        } else {

          if (do_send(records, offset, i - offset) != OMS_OK) {
            OMS_ERROR << "Failed to write LogMessage to client: " << _client_peer.to_string();
            stop();
            break;
          }

          offset = i;
        }
        packet_size = 0;
        continue;
      }
      packet_size += size;
    }

    if (packet_size > 0) {
      if (do_send(records, offset, i - offset) != OMS_OK) {
        OMS_ERROR << "Failed to write LogMessage to client: " << _client_peer.to_string();
        stop();
      }
    }

    for (ILogRecord* r : records) {
      _oblog.release(r);
    }
  }

180
  LogMsgLocalDestroy;
F
Fankux 已提交
181
  _reader.stop();
W
init  
wangyunlai.wyl 已提交
182 183 184 185 186 187 188
}

int SenderRoutine::do_send(const std::vector<ILogRecord*>& records, size_t offset, size_t count)
{
  if (_s_config.verbose.val()) {
    OMS_DEBUG << "send record range[" << offset << ", " << offset + count << ")";
  }
F
Fankux 已提交
189 190

  _stage_timer.reset();
W
init  
wangyunlai.wyl 已提交
191 192
  RecordDataMessage msg(records, offset, count);
  msg.set_version(_packet_version);
193
  msg.compress_type = CompressType::LZ4;
F
Fankux 已提交
194
  msg.idx = _msg_seq;
W
init  
wangyunlai.wyl 已提交
195
  int ret = _comm.send_message(_client_peer, msg, true);
F
Fankux 已提交
196 197
  Counter::instance().count_key(Counter::SENDER_SEND_US, _stage_timer.elapsed());

F
Fankux 已提交
198 199
  _msg_seq += count;

W
init  
wangyunlai.wyl 已提交
200 201 202 203
  if (ret == OMS_OK) {
    ILogRecord* last = records[offset + count - 1];
    Counter::instance().count_write(count);
    Counter::instance().mark_timestamp(last->getTimestamp());
F
Fankux 已提交
204
    Counter::instance().mark_checkpoint(last->getCheckpoint1());
W
init  
wangyunlai.wyl 已提交
205 206 207 208 209 210 211 212
  } else {
    OMS_WARN << "Failed to send record data message to client. peer=" << _client_peer.id();
  }
  return ret;
}

}  // namespace logproxy
}  // namespace oceanbase