Skip to content

Commit 0f30065

Browse files
committed
MB-67175: Add termination reason to log description
Extend the description with the termination reason if it is set: 2025-10-16T11:13:31.741832+02:00 INFO Removing connection {"conn_id":925, "bucket":"travel-sample", "dcp":"producer", "dcp_name":"eq_dcpq:DCPT:INIT_309d2c13b63794168603c93e5ee53d84:travel-sample:inventory:airline:11100504861856972217:##4:3", "description":{ "peer":{ "ip":"127.0.0.1", "port":44664}, "socket":{ "ip":"127.0.0.1", "port":11210}, "termination_reason":"Client closed connection: EOF", "user":{ "name":"@Projector", "system":true}}} Previously the description was set when the connection was created and only updated as part of authentication (and in those contexts it wouldn't be used from other threads). DCP connections will log the description from other threads than the connections front end thread so with this change we need to protect the data with a lock. Change-Id: I2fd4d730bbf55d42696d994fa3ba89d1422299c4 Reviewed-on: https://review.couchbase.org/c/kv_engine/+/235358 Tested-by: Trond Norbye <[email protected]> Reviewed-by: Jim Walker <[email protected]>
1 parent b973936 commit 0f30065

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

daemon/connection.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -436,22 +436,26 @@ cb::engine_errc Connection::remapErrorCode(cb::engine_errc code) {
436436
}
437437

438438
void Connection::updateDescription() {
439-
description = {
439+
nlohmann::json desc = {
440440
{"peer", peername},
441441
{"socket", sockname},
442442
};
443443
if (isAuthenticated()) {
444-
nlohmann::json user{{"name", getUser().getSanitizedName()}};
444+
nlohmann::json me{{"name", getUser().getSanitizedName()}};
445445
if (isInternal()) {
446-
user["system"] = true;
446+
me["system"] = true;
447447
}
448448
if (getUser().domain == cb::sasl::Domain::External) {
449-
user["ldap"] = true;
449+
me["ldap"] = true;
450450
}
451-
description["user"] = std::move(user);
451+
desc["user"] = std::move(me);
452452
} else {
453-
description["user"] = nullptr;
453+
desc["user"] = nullptr;
454454
}
455+
if (!terminationReason.empty()) {
456+
desc["termination_reason"] = terminationReason;
457+
}
458+
description = desc;
455459
}
456460

457461
void Connection::setBucketIndex(int index, Cookie* cookie) {
@@ -1340,6 +1344,9 @@ void Connection::setTerminationReason(std::string reason) {
13401344
terminationReason.append(";");
13411345
terminationReason.append(reason);
13421346
}
1347+
description.withLock([this](auto& json) {
1348+
json["termination_reason"] = terminationReason;
1349+
});
13431350
}
13441351

13451352
void Connection::setAgentName(std::string_view name) {

daemon/connection.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <cbsasl/server.h>
1919
#include <daemon/protocol/mcbp/command_context.h>
20+
#include <folly/Synchronized.h>
2021
#include <mcbp/protocol/unsigned_leb128.h>
2122
#include <memcached/connection_iface.h>
2223
#include <memcached/dcp.h>
@@ -96,8 +97,10 @@ class Connection : public ConnectionIface, public DcpMessageProducersIface {
9697
return uint32_t(socketDescriptor);
9798
}
9899

99-
const nlohmann::json& getDescription() const override {
100-
return description;
100+
nlohmann::json getDescription() const override {
101+
nlohmann::json desc;
102+
description.copyInto(desc);
103+
return desc;
101104
}
102105

103106
/**
@@ -1098,7 +1101,7 @@ class Connection : public ConnectionIface, public DcpMessageProducersIface {
10981101
} authContextLifetime;
10991102

11001103
/// The description of the connection
1101-
nlohmann::json description;
1104+
folly::Synchronized<nlohmann::json, std::mutex> description;
11021105

11031106
/// The reason why the session was terminated
11041107
std::string terminationReason;

include/memcached/connection_iface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ConnectionIface {
6767
* Note that the description may be modified in the context of the front end
6868
* thread as part of authentication so the description should not be cached.
6969
*/
70-
virtual const nlohmann::json& getDescription() const = 0;
70+
virtual nlohmann::json getDescription() const = 0;
7171

7272
/**
7373
* Get the username this connection is authenticated as (should only be

include/test/dummy_connection.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class DummyConnection : public ConnectionIface {
4040
}
4141
void setPriority(ConnectionPriority value) override {
4242
}
43-
const nlohmann::json& getDescription() const override {
44-
return description;
43+
nlohmann::json getDescription() const override {
44+
return {{"dummy", true}};
4545
}
4646
const cb::rbac::UserIdent& getUser() const override {
4747
return user;
@@ -51,6 +51,5 @@ class DummyConnection : public ConnectionIface {
5151

5252
protected:
5353
cb::rbac::UserIdent user{"dummy", cb::rbac::Domain::Local};
54-
nlohmann::json description{{"dummy", true}};
5554
};
5655
} // namespace cb::test

programs/engine_testapp/mock_cookie.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ void MockConnection::scheduleDcpStep() {
2525
void MockConnection::setUserScheduleDcpStep(std::function<void()> func) {
2626
userScheduleDcpStep = std::move(func);
2727
}
28-
const nlohmann::json& MockConnection::getDescription() const {
29-
static nlohmann::json description{{"peer", "you"}, {"socket", "me"}};
30-
return description;
28+
nlohmann::json MockConnection::getDescription() const {
29+
return {{"peer", "you"}, {"socket", "me"}};
3130
}
3231
const cb::rbac::UserIdent& MockConnection::getUser() const {
3332
return user;

programs/engine_testapp/mock_cookie.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MockConnection : public ConnectionIface {
4343
priority = value;
4444
}
4545

46-
const nlohmann::json& getDescription() const override;
46+
nlohmann::json getDescription() const override;
4747
const cb::rbac::UserIdent& getUser() const override;
4848

4949
void setUser(const std::string& newUser) {

0 commit comments

Comments
 (0)