-
Notifications
You must be signed in to change notification settings - Fork 19
/
sender_routine.cpp
212 lines (176 loc) · 5.98 KB
/
sender_routine.cpp
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
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
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
/**
* 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"
#include "LogRecord.h"
#include "common/log.h"
#include "common/config.h"
#include "common/counter.h"
#include "codec/encoder.h"
#include "communication/comm.h"
#include "oblogreader/oblogreader.h"
namespace oceanbase {
namespace logproxy {
static Config& _s_config = Config::instance();
#ifdef LOGMSG_BY_LIBOBLOG
static __thread LogMsgBuf* _t_s_lmb = nullptr;
#endif
SenderRoutine::SenderRoutine(ObLogReader& reader, OblogAccess& oblog, BlockingQueue<ILogRecord*>& rqueue)
: Thread("SenderRoutine"), _reader(reader), _oblog(oblog), _rqueue(rqueue)
{}
int SenderRoutine::init(MessageVersion packet_version, const Peer& peer)
{
_packet_version = packet_version;
_client_peer = 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;
}
// _comm.set_write_callback();
ret = _comm.add(peer);
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.stop();
}
}
void SenderRoutine::run()
{
LogMsgLocalInit;
std::vector<ILogRecord*> records;
records.reserve(_s_config.read_wait_num.val());
while (is_run()) {
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;
}
}
_stage_timer.reset();
records.clear();
while (!_rqueue.poll(records, _s_config.read_timeout_us.val()) || records.empty()) {
OMS_INFO << "send transfer queue empty, retry...";
}
int64_t poll_us = _stage_timer.elapsed();
Counter::instance().count_key(Counter::SENDER_POLL_US, poll_us);
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()
<< ", timestamp:" << record->getTimestamp() << ", checkpoint:" << record->getCheckpoint1()
<< ", 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());
Counter::instance().mark_checkpoint(record->getCheckpoint1());
_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;
#ifdef LOGMSG_BY_LIBOBLOG
const char* rbuf = r->toString(&size, _t_s_lmb, true);
#else
const char* rbuf = r->toString(&size, true);
#endif
if (rbuf == nullptr) {
OMS_ERROR << "failed to parse logmsg Record, !!!EXIT!!!";
stop();
break;
}
if (packet_size + size > _s_config.max_packet_bytes.val()) {
if (packet_size == 0) {
OMS_WARN << "Huge package occured with size of: " << size
<< ", exceed max_packet_bytes: " << _s_config.max_packet_bytes.val() << ", try to send directly";
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);
}
}
LogMsgLocalDestroy;
_reader.stop();
}
int SenderRoutine::do_send(std::vector<ILogRecord*>& records, size_t offset, size_t count)
{
if (_s_config.verbose.val()) {
OMS_DEBUG << "send record range[" << offset << ", " << offset + count << ")";
}
RecordDataMessage msg(records, offset, count);
msg.set_version(_packet_version);
msg.compress_type = CompressType::LZ4;
msg.idx = _msg_seq;
int ret = _comm.send_message(_client_peer, msg, true);
_msg_seq += count;
if (ret == OMS_OK) {
ILogRecord* last = records[offset + count - 1];
Counter::instance().count_write(count);
Counter::instance().mark_timestamp(last->getTimestamp());
Counter::instance().mark_checkpoint(last->getCheckpoint1());
} else {
OMS_WARN << "Failed to send record data message to client, peer: " << _client_peer.id();
}
return ret;
}
} // namespace logproxy
} // namespace oceanbase