forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_hj_batch.cpp
More file actions
228 lines (209 loc) · 7.05 KB
/
Copy pathob_hj_batch.cpp
File metadata and controls
228 lines (209 loc) · 7.05 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* 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.
*/
#define USING_LOG_PREFIX SQL_ENG
#include "ob_hj_batch.h"
#include "ob_hj_partition.h"
using namespace oceanbase;
using namespace sql;
using namespace common;
using namespace join;
using namespace blocksstable;
ObHJBatch::~ObHJBatch()
{
int ret = OB_SUCCESS;
if (OB_FAIL(close())) {
LOG_WARN("close hash join batch failed", K(ret));
}
}
int ObHJBatch::rescan()
{
int ret = OB_SUCCESS;
row_store_iter_.reset();
chunk_iter_.reset();
return ret;
}
int ObHJBatch::load_next_chunk()
{
int ret = OB_SUCCESS;
if (OB_FAIL(chunk_iter_.load_next_chunk(row_store_iter_))) {
LOG_WARN("faild to load next chunk", K(ret));
}
return ret;
}
int ObHJBatch::finish_dump(bool memory_need_dump)
{
int ret = OB_SUCCESS;
if (OB_FAIL(chunk_row_store_.finish_add_row(memory_need_dump))) {
LOG_WARN("failed to finish chunk row store", K(ret));
} else if (memory_need_dump && 0 != chunk_row_store_.get_mem_used()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expect memroy is 0", K(ret), K(chunk_row_store_.get_mem_used()));
}
return ret;
}
int ObHJBatch::dump(bool all_dump)
{
int ret = OB_SUCCESS;
if (0 >= chunk_row_store_.get_mem_used()) {
// ret = OB_ERR_UNEXPECTED;
// LOG_WARN("unexpect chunk row store use memory", K(ret), K(chunk_row_store_.get_mem_used()));
} else if (OB_FAIL(chunk_row_store_.dump(false, all_dump))) {
LOG_WARN("failed to dump data to chunk row store", K(ret));
}
return ret;
}
int ObHJBatch::add_row(const common::ObNewRow &row, ObStoredJoinRow *&stored_row)
{
int ret = OB_SUCCESS;
ObChunkRowStore::StoredRow *hash_store_row = nullptr;
if (OB_FAIL(chunk_row_store_.add_row(row, &hash_store_row))) {
LOG_WARN("failed to add row", K(ret));
} else {
stored_row = static_cast<ObStoredJoinRow *>(hash_store_row);
++n_add_rows_;
}
return ret;
}
int ObHJBatch::convert_row(const common::ObNewRow *&row, const ObStoredJoinRow *&stored_row)
{
int ret = OB_SUCCESS;
ObNewRow *inner_row = nullptr;
if (OB_FAIL(row_store_iter_.convert_to_row_full(inner_row, stored_row))) {
LOG_WARN("failed to convert row", K(ret));
} else {
row = static_cast<const ObNewRow *>(inner_row);
}
return ret;
}
int ObHJBatch::get_next_row(const common::ObNewRow *&row, const ObStoredJoinRow *&stored_row)
{
int ret = OB_SUCCESS;
stored_row = nullptr;
row = nullptr;
const ObChunkRowStore::StoredRow *inner_stored_row = nullptr;
while (OB_SUCC(ret) && nullptr == inner_stored_row) {
if (OB_FAIL(row_store_iter_.get_next_row(inner_stored_row))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else if (!is_chunk_iter_) {
ret = OB_SUCCESS;
if (OB_FAIL(chunk_iter_.load_next_chunk(row_store_iter_))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else if (n_get_rows_ != chunk_row_store_.get_row_cnt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Got row count is not match with row count of chunk row store", K(ret), K(n_get_rows_), K(chunk_row_store_.get_row_cnt()));
} else {
// LOG_TRACE("trace get row count", K(part_level_), K(batchno_), K(ret), K(n_get_rows_), K(chunk_row_store_.get_row_cnt()));
}
}
}
} else {
++n_get_rows_;
stored_row = static_cast<const ObStoredJoinRow *>(inner_stored_row);
if (OB_FAIL(convert_row(row, stored_row))) {
LOG_WARN("failed to convert row", K(ret));
}
}
}
return ret;
}
int ObHJBatch::get_next_row(const ObStoredJoinRow *&stored_row)
{
int ret = OB_SUCCESS;
stored_row = nullptr;
const ObChunkRowStore::StoredRow *inner_stored_row = nullptr;
while (OB_SUCC(ret) && nullptr == inner_stored_row) {
if (OB_FAIL(row_store_iter_.get_next_row(inner_stored_row))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else if (!is_chunk_iter_) {
ret = OB_SUCCESS;
if (OB_FAIL(chunk_iter_.load_next_chunk(row_store_iter_))) {
if (OB_ITER_END != ret) {
LOG_WARN("failed to get next row", K(ret));
} else if (n_get_rows_ != chunk_row_store_.get_row_cnt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Got row count is not match with row count of chunk row store", K(ret), K(n_get_rows_), K(chunk_row_store_.get_row_cnt()));
} else {
LOG_TRACE("trace get row count", K(part_level_), K(batchno_), K(ret), K(n_get_rows_), K(chunk_row_store_.get_row_cnt()));
}
}
}
} else {
++n_get_rows_;
stored_row = static_cast<const ObStoredJoinRow *>(inner_stored_row);
}
}
return ret;
}
// 可能会读多次,所以每次都应该set iterator,同时reset
int ObHJBatch::set_iterator(bool is_chunk_iter)
{
int ret = OB_SUCCESS;
int64_t chunk_size = 0;
row_store_iter_.reset();
chunk_iter_.reset();
if (OB_ISNULL(buf_mgr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("buf_mgr_ is null", K(ret));
} else if (is_chunk_iter) {
// 上层逻辑手动调load_next_chunk来驱动获取一批数据
chunk_size = buf_mgr_->get_reserve_memory_size();
if (buf_mgr_->get_reserve_memory_size() > chunk_row_store_.get_mem_used() + chunk_row_store_.get_file_size()) {
chunk_size = chunk_row_store_.get_mem_used() + chunk_row_store_.get_file_size();
}
if (buf_mgr_->get_page_size() > chunk_size) {
chunk_size = buf_mgr_->get_page_size();
}
if (OB_FAIL(chunk_row_store_.begin(chunk_iter_, chunk_size))) {
LOG_WARN("failed to set chunk iter", K(ret), K(buf_mgr_->get_reserve_memory_size()));
}
} else {
chunk_size = buf_mgr_->get_page_size();
if (OB_FAIL(chunk_row_store_.begin(chunk_iter_, chunk_size))) {
LOG_WARN("failed to set row store iterator", K(ret), K(chunk_size));
} else if (OB_FAIL(load_next_chunk())) {
LOG_WARN("failed to load next chunk", K(ret));
}
}
n_get_rows_ = 0;
is_chunk_iter_ = is_chunk_iter;
LOG_TRACE("set iterator: ", K(chunk_size), K(is_chunk_iter));
return ret;
}
int ObHJBatch::init()
{
int ret = OB_SUCCESS;
if (OB_FAIL(chunk_row_store_.init(0, tenant_id_,
common::ObCtxIds::WORK_AREA,
common::ObModIds::OB_ARENA_HASH_JOIN,
true, ObChunkRowStore::STORE_MODE::FULL, 8))) {
LOG_WARN("failed to init chunk row store", K(ret));
} else {
chunk_row_store_.set_callback(buf_mgr_);
}
return ret;
}
int ObHJBatch::open()
{
return OB_SUCCESS;
}
int ObHJBatch::close()
{
int ret = OB_SUCCESS;
row_store_iter_.reset();
chunk_iter_.reset();
chunk_row_store_.reset();
buf_mgr_ = nullptr;
return ret;
}