Skip to content

Commit bc7499c

Browse files
BenHuddlestondaverigby
authored andcommitted
MB-33873: Revert "MB-33739: Use reference in ConnMap (and DcpConnMap) for vbConns"
This reverts commit 9c9825d. Change-Id: Iaf6f5aef53dfc56178641c11e5acc2671d4a35c3 Reviewed-on: http://review.couchbase.org/108152 Tested-by: Build Bot <[email protected]> Reviewed-by: Dave Rigby <[email protected]>
1 parent aa97267 commit bc7499c

File tree

6 files changed

+65
-35
lines changed

6 files changed

+65
-35
lines changed

engines/ep/src/connmap.cc

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,16 @@ class ConnManager : public GlobalTask {
8282
size_t snoozeTime;
8383
};
8484

85-
ConnMap::ConnMap(EventuallyPersistentEngine& theEngine)
86-
: vbConnLocks(vbConnLockNum),
87-
vbConns(theEngine.getConfiguration().getMaxVbuckets()),
88-
engine(theEngine),
89-
connNotifier_(nullptr) {
85+
ConnMap::ConnMap(EventuallyPersistentEngine &theEngine)
86+
: vbConnLocks(vbConnLockNum),
87+
engine(theEngine),
88+
connNotifier_(nullptr) {
89+
90+
Configuration &config = engine.getConfiguration();
91+
size_t max_vbs = config.getMaxVbuckets();
92+
for (size_t i = 0; i < max_vbs; ++i) {
93+
vbConns.push_back(std::list<std::weak_ptr<ConnHandler>>());
94+
}
9095
}
9196

9297
void ConnMap::initialize() {
@@ -153,17 +158,27 @@ void ConnMap::processPendingNotifications() {
153158
}
154159
}
155160

156-
void ConnMap::addVBConnByVBId(ConnHandler& conn, Vbid vbid) {
161+
void ConnMap::addVBConnByVBId(std::shared_ptr<ConnHandler> conn, Vbid vbid) {
162+
if (!conn.get()) {
163+
return;
164+
}
165+
157166
size_t lock_num = vbid.get() % vbConnLockNum;
158167
std::lock_guard<std::mutex> lh(vbConnLocks[lock_num]);
159-
vbConns[vbid.get()].emplace_back(conn);
168+
vbConns[vbid.get()].emplace_back(std::move(conn));
160169
}
161170

162171
void ConnMap::removeVBConnByVBId_UNLOCKED(const void* connCookie, Vbid vbid) {
163-
auto& vb_conns = vbConns[vbid.get()];
164-
vb_conns.remove_if([connCookie](ConnHandler& conn) {
165-
return connCookie == conn.getCookie();
166-
});
172+
std::list<std::weak_ptr<ConnHandler>>& vb_conns = vbConns[vbid.get()];
173+
for (auto itr = vb_conns.begin(); itr != vb_conns.end(); ++itr) {
174+
auto connection = (*itr).lock();
175+
// Erase if we cannot lock, or if the cookie matches
176+
if (!connection ||
177+
(connection && connCookie == connection->getCookie())) {
178+
vb_conns.erase(itr);
179+
break;
180+
}
181+
}
167182
}
168183

169184
void ConnMap::removeVBConnByVBId(const void* connCookie, Vbid vbid) {

engines/ep/src/connmap.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class ConnMap {
6767
/**
6868
* Adds the given connection to the set of connections associated
6969
* with the given vbucket.
70-
* @param conn Connection to add to the set.
70+
* @param conn Connection to add to the set. Refcount is retained.
7171
* @param vbid vBucket to add to.
7272
*/
73-
void addVBConnByVBId(ConnHandler& conn, Vbid vbid);
73+
void addVBConnByVBId(std::shared_ptr<ConnHandler> conn, Vbid vbid);
7474

7575
void removeVBConnByVBId_UNLOCKED(const void* connCookie, Vbid vbid);
7676

@@ -122,7 +122,7 @@ class ConnMap {
122122
CookieToConnectionMap map_;
123123

124124
std::vector<std::mutex> vbConnLocks;
125-
std::vector<std::list<std::reference_wrapper<ConnHandler>>> vbConns;
125+
std::vector<std::list<std::weak_ptr<ConnHandler>>> vbConns;
126126

127127
/* Handle to the engine who owns us */
128128
EventuallyPersistentEngine &engine;

engines/ep/src/dcp/consumer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1817,7 +1817,7 @@ void DcpConsumer::setDisconnect() {
18171817
void DcpConsumer::registerStream(std::shared_ptr<PassiveStream> stream) {
18181818
auto vbid = stream->getVBucket();
18191819
streams.insert({vbid, stream});
1820-
engine_.getDcpConnMap().addVBConnByVBId(*this, vbid);
1820+
engine_.getDcpConnMap().addVBConnByVBId(shared_from_this(), vbid);
18211821
}
18221822

18231823
void DcpConsumer::removeStream(Vbid vbid) {

engines/ep/src/dcp/dcpconnmap.cc

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,12 @@ bool DcpConnMap::handleSlowStream(Vbid vbid, const CheckpointCursor* cursor) {
249249
size_t lock_num = vbid.get() % vbConnLockNum;
250250
std::lock_guard<std::mutex> lh(vbConnLocks[lock_num]);
251251

252-
for (const auto& connection : vbConns[vbid.get()]) {
253-
auto* producer = dynamic_cast<DcpProducer*>(&connection.get());
254-
255-
// Check that this connection is actually a producer
252+
for (const auto& weakPtr : vbConns[vbid.get()]) {
253+
auto connection = weakPtr.lock();
254+
if (!connection) {
255+
continue;
256+
}
257+
auto* producer = dynamic_cast<DcpProducer*>(connection.get());
256258
if (producer && producer->handleSlowStream(vbid, cursor)) {
257259
return true;
258260
}
@@ -396,20 +398,28 @@ void DcpConnMap::removeVBConnections(DcpProducer& prod) {
396398
size_t lock_num = vbid.get() % vbConnLockNum;
397399
std::lock_guard<std::mutex> lh(vbConnLocks[lock_num]);
398400
auto& vb_conns = vbConns[vbid.get()];
399-
auto* cookie = prod.getCookie();
400-
vb_conns.remove_if([cookie](ConnHandler& conn) {
401-
return cookie == (conn.getCookie());
402-
});
401+
for (auto itr = vb_conns.begin(); itr != vb_conns.end(); ++itr) {
402+
auto connection = (*itr).lock();
403+
// Erase if we cannot lock, or if the cookie matches
404+
if (!connection ||
405+
(connection && prod.getCookie() == connection->getCookie())) {
406+
vb_conns.erase(itr);
407+
break;
408+
}
409+
}
403410
}
404411
}
405412

406413
void DcpConnMap::notifyVBConnections(Vbid vbid, uint64_t bySeqno) {
407414
size_t lock_num = vbid.get() % vbConnLockNum;
408415
std::lock_guard<std::mutex> lh(vbConnLocks[lock_num]);
409416

410-
for (auto& connection : vbConns[vbid.get()]) {
411-
auto* producer = dynamic_cast<DcpProducer*>(&connection.get());
412-
// Check that this connection is actually a producer
417+
for (auto& weakPtr : vbConns[vbid.get()]) {
418+
auto connection = weakPtr.lock();
419+
if (!connection) {
420+
continue;
421+
}
422+
auto* producer = dynamic_cast<DcpProducer*>(connection.get());
413423
if (producer) {
414424
producer->notifySeqnoAvailable(vbid, bySeqno);
415425
}
@@ -425,9 +435,12 @@ void DcpConnMap::seqnoAckVBPassiveStream(Vbid vbid) {
425435
// only Producers).
426436
// @todo-durability: not clear yet if for Consumers we can simplify by
427437
// keeping a 1-to-1 VB-to-Consumer mapping
428-
for (auto& connection : vbConns[vbid.get()]) {
429-
auto* consumer = dynamic_cast<DcpConsumer*>(&connection.get());
430-
// Check that this connection is actually a consumer
438+
for (auto& weakPtr : vbConns[vbid.get()]) {
439+
auto connection = weakPtr.lock();
440+
if (!connection) {
441+
continue;
442+
}
443+
auto* consumer = dynamic_cast<DcpConsumer*>(connection.get());
431444
if (consumer) {
432445
// Note: Sync Repl enabled at Consumer only if Producer supports it.
433446
// This is to prevent that 6.5 Consumers send DCP_SEQNO_ACK to

engines/ep/src/dcp/producer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ ENGINE_ERROR_CODE DcpProducer::streamRequest(
553553
notifyStreamReady(vbucket);
554554

555555
if (add_vb_conn_map) {
556-
engine_.getDcpConnMap().addVBConnByVBId(*this, vbucket);
556+
engine_.getDcpConnMap().addVBConnByVBId(shared_from_this(), vbucket);
557557
}
558558

559559
return rv;

engines/ep/tests/mock/mock_dcp_conn_map.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ class MockDcpConnMap : public DcpConnMap {
5454
/// return if the named handler exists for the vbid in the vbConns structure
5555
bool doesConnHandlerExist(Vbid vbid, const std::string& name) const {
5656
const auto& list = vbConns[vbid.get()];
57-
return std::find_if(list.begin(),
58-
list.end(),
59-
[&name](const ConnHandler& c) -> bool {
60-
return c.getName() == name;
61-
}) != list.end();
57+
return std::find_if(
58+
list.begin(),
59+
list.end(),
60+
[&name](const std::weak_ptr<ConnHandler>& c) -> bool {
61+
auto p = c.lock();
62+
return p && p->getName() == name;
63+
}) != list.end();
6264
}
6365

6466
protected:

0 commit comments

Comments
 (0)