forked from membase/ep-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vbucket.cc
123 lines (103 loc) · 4 KB
/
vbucket.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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include <functional>
#include "vbucket.hh"
#include "ep_engine.h"
VBucketFilter VBucketFilter::filter_diff(const VBucketFilter &other) const {
std::vector<uint16_t> tmp(acceptable.size() + other.size());
std::vector<uint16_t>::iterator end;
end = std::set_symmetric_difference(acceptable.begin(),
acceptable.end(),
other.acceptable.begin(),
other.acceptable.end(),
tmp.begin());
return VBucketFilter(std::vector<uint16_t>(tmp.begin(), end));
}
VBucketFilter VBucketFilter::filter_intersection(const VBucketFilter &other) const {
std::vector<uint16_t> tmp(acceptable.size() + other.size());
std::vector<uint16_t>::iterator end;
end = std::set_intersection(acceptable.begin(), acceptable.end(),
other.acceptable.begin(), other.acceptable.end(),
tmp.begin());
return VBucketFilter(std::vector<uint16_t>(tmp.begin(), end));
}
static bool isRange(std::vector<uint16_t>::const_iterator it,
const std::vector<uint16_t>::const_iterator &end,
size_t &length)
{
length = 0;
for (uint16_t val = *it;
it != end && (val + length) == *it;
++it, ++length) {
// empty
}
--length;
return length > 1;
}
std::ostream& operator <<(std::ostream &out, const VBucketFilter &filter)
{
bool needcomma = false;
std::vector<uint16_t>::const_iterator it;
if (filter.acceptable.empty()) {
out << "{ empty }";
} else {
out << "{ ";
for (it = filter.acceptable.begin();
it != filter.acceptable.end();
++it) {
if (needcomma) {
out << ", ";
}
size_t length;
if (isRange(it, filter.acceptable.end(), length)) {
out << "[" << *it << "," << *(it + length) << "]";
it += length;
} else {
out << *it;
}
needcomma = true;
}
out << " }";
}
return out;
}
const vbucket_state_t VBucket::ACTIVE = static_cast<vbucket_state_t>(htonl(vbucket_state_active));
const vbucket_state_t VBucket::REPLICA = static_cast<vbucket_state_t>(htonl(vbucket_state_replica));
const vbucket_state_t VBucket::PENDING = static_cast<vbucket_state_t>(htonl(vbucket_state_pending));
const vbucket_state_t VBucket::DEAD = static_cast<vbucket_state_t>(htonl(vbucket_state_dead));
void VBucket::fireAllOps(EventuallyPersistentEngine &engine, ENGINE_ERROR_CODE code) {
if (pendingOpsStart > 0) {
hrtime_t now = gethrtime();
if (now > pendingOpsStart) {
hrtime_t d = (now - pendingOpsStart) / 1000;
stats.pendingOpsHisto.add(d);
stats.pendingOpsMaxDuration.setIfBigger(d);
}
}
pendingOpsStart = 0;
stats.pendingOps.decr(pendingOps.size());
stats.pendingOpsMax.setIfBigger(pendingOps.size());
engine.notifyIOComplete(pendingOps, code);
pendingOps.clear();
getLogger()->log(EXTENSION_LOG_INFO, NULL,
"Fired pendings ops for vbucket %d in state %s\n",
id, VBucket::toString(state));
}
void VBucket::fireAllOps(EventuallyPersistentEngine &engine) {
LockHolder lh(pendingOpLock);
if (state == vbucket_state_active) {
fireAllOps(engine, ENGINE_SUCCESS);
} else if (state == vbucket_state_pending) {
// Nothing
} else {
fireAllOps(engine, ENGINE_NOT_MY_VBUCKET);
}
}
void VBucket::setState(vbucket_state_t to, SERVER_HANDLE_V1 *sapi) {
assert(sapi);
vbucket_state_t oldstate(state);
getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
"transitioning vbucket %d from %s to %s\n",
id, VBucket::toString(oldstate), VBucket::toString(to));
state = to;
}