-
Notifications
You must be signed in to change notification settings - Fork 22
/
bulker.cpp
83 lines (66 loc) · 1.93 KB
/
bulker.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
#include "bulker.hpp"
#include "exceptions.hpp"
namespace eosio {
bulker::~bulker() {
ilog("draining bulker, size: ${n}", ("n", body_size));
if ( !body->empty() ) {
perform( std::move(body) );
}
}
size_t bulker::size() {
return body_size;
}
void bulker::perform( std::unique_ptr<std::string> &&body) {
std::unique_ptr<std::string> bulk( std::move(body) );
// dlog("bulk size: ${s}", ("s", bulk->size() ));
std::lock_guard<std::mutex> guard(client_mtx);
try {
es_client.bulk_perform( *bulk );
} catch (... ) {
handle_elasticsearch_exception( "bulk exception", __LINE__ );
}
}
void bulker::append_document( std::string action, std::string source ) {
bool trigger = false;
std::unique_ptr<std::string> temp( new std::string() );
std::string doc( std::move(action) );
doc.push_back('\n');
doc.append( std::move(source) );
doc.push_back('\n');
{
std::lock_guard<std::mutex> guard(body_mtx);
body->append( doc );
body_size = body->size();
if ( body_size >= bulk_size ) {
body.swap( temp );
body_size = 0;
trigger = true;
}
}
if ( trigger ) {
perform( std::move(temp) );
}
}
bulker_pool::bulker_pool(size_t size, size_t bulk_size,
const std::vector<std::string> url_list,
const std::string &user, const std::string &password): pool_size(size), bulk_size(bulk_size)
{
for (int i = 0; i < pool_size; ++i) {
bulkers.emplace_back( new bulker(bulk_size, url_list, user, password) );
}
}
bulker& bulker_pool::get() {
if ( pool_size == 0 ) {
EOS_THROW(chain::empty_bulker_pool_exception, "empty pool");
}
size_t cur_idx = index % pool_size;
auto ptr = bulkers[cur_idx].get();
if ( ptr->size() >= bulk_size ) {
cur_idx = (cur_idx + 1) % pool_size;
index = cur_idx;
return *bulkers[cur_idx].get();
} else {
return *ptr;
}
}
}