/
bgfetcher.cc
164 lines (142 loc) · 5.24 KB
/
bgfetcher.cc
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2012 Couchbase, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include <algorithm>
#include <vector>
#include "bgfetcher.h"
#include "ep.h"
#include "kvshard.h"
#include "executorthread.h"
const double BgFetcher::sleepInterval = MIN_SLEEP_TIME;
void BgFetcher::start() {
bool inverse = false;
pendingFetch.compare_exchange_strong(inverse, true);
ExecutorPool* iom = ExecutorPool::get();
ExTask task = new MultiBGFetcherTask(&(store->getEPEngine()), this, false);
this->setTaskId(task->getId());
iom->schedule(task, READER_TASK_IDX);
cb_assert(taskId > 0);
}
void BgFetcher::stop() {
bool inverse = true;
pendingFetch.compare_exchange_strong(inverse, false);
cb_assert(taskId > 0);
ExecutorPool::get()->cancel(taskId);
}
void BgFetcher::notifyBGEvent(void) {
++stats.numRemainingBgJobs;
bool inverse = false;
if (pendingFetch.compare_exchange_strong(inverse, true)) {
cb_assert(taskId > 0);
ExecutorPool::get()->wake(taskId);
}
}
size_t BgFetcher::doFetch(uint16_t vbId) {
hrtime_t startTime(gethrtime());
LOG(EXTENSION_LOG_DEBUG, "BgFetcher is fetching data, vBucket = %d "
"numDocs = %d, startTime = %lld\n", vbId, items2fetch.size(),
startTime/1000000);
shard->getROUnderlying()->getMulti(vbId, items2fetch);
size_t totalfetches = 0;
std::vector<bgfetched_item_t> fetchedItems;
vb_bgfetch_queue_t::iterator itr = items2fetch.begin();
for (; itr != items2fetch.end(); ++itr) {
std::list<VBucketBGFetchItem *> &requestedItems = (*itr).second;
std::list<VBucketBGFetchItem *>::iterator itm = requestedItems.begin();
for(; itm != requestedItems.end(); ++itm) {
const std::string &key = (*itr).first;
fetchedItems.push_back(std::make_pair(key, *itm));
++totalfetches;
}
}
if (totalfetches > 0) {
store->completeBGFetchMulti(vbId, fetchedItems, startTime);
stats.getMultiHisto.add((gethrtime()-startTime)/1000, totalfetches);
}
// failed requests will get requeued for retry within clearItems()
clearItems(vbId);
return totalfetches;
}
void BgFetcher::clearItems(uint16_t vbId) {
vb_bgfetch_queue_t::iterator itr = items2fetch.begin();
for(; itr != items2fetch.end(); ++itr) {
// every fetched item belonging to the same key shares
// a single data buffer, just delete it from the first fetched item
std::list<VBucketBGFetchItem *> &doneItems = (*itr).second;
VBucketBGFetchItem *firstItem = doneItems.front();
firstItem->delValue();
std::list<VBucketBGFetchItem *>::iterator dItr = doneItems.begin();
for (; dItr != doneItems.end(); ++dItr) {
delete *dItr;
}
}
}
bool BgFetcher::run(GlobalTask *task) {
size_t num_fetched_items = 0;
bool inverse = true;
pendingFetch.compare_exchange_strong(inverse, false);
std::vector<uint16_t> bg_vbs;
LockHolder lh(queueMutex);
std::set<uint16_t>::iterator it = pendingVbs.begin();
for (; it != pendingVbs.end(); ++it) {
bg_vbs.push_back(*it);
}
pendingVbs.clear();
lh.unlock();
std::vector<uint16_t>::iterator ita = bg_vbs.begin();
for (; ita != bg_vbs.end(); ++ita) {
uint16_t vbId = *ita;
if (store->getVBuckets().isBucketCreation(vbId)) {
// Requeue the bg fetch task if a vbucket DB file is not
// created yet.
lh.lock();
pendingVbs.insert(vbId);
lh.unlock();
bool inverse = false;
pendingFetch.compare_exchange_strong(inverse, true);
continue;
}
RCPtr<VBucket> vb = shard->getBucket(vbId);
if (vb && vb->getBGFetchItems(items2fetch)) {
num_fetched_items += doFetch(vbId);
items2fetch.clear();
}
}
stats.numRemainingBgJobs.fetch_sub(num_fetched_items);
if (!pendingFetch.load()) {
// wait a bit until next fetch request arrives
double sleep = std::max(store->getBGFetchDelay(), sleepInterval);
task->snooze(sleep);
if (pendingFetch.load()) {
// check again a new fetch request could have arrived
// right before calling above snooze()
task->snooze(0);
}
}
return true;
}
bool BgFetcher::pendingJob() {
std::vector<int> vbIds = shard->getVBuckets();
size_t numVbuckets = vbIds.size();
for (size_t i = 0; i < numVbuckets; ++i) {
RCPtr<VBucket> vb = shard->getBucket(vbIds[i]);
if (vb && vb->hasPendingBGFetchItems()) {
return true;
}
}
return false;
}