Skip to content

Commit 3a6d750

Browse files
committed
MB-35938: Add support for "unselect bucket"
Clients may go back to the no bucket by selecting "@no bucket@" (which is an invalid bucket name so it may never be a conflict with a real bucket) Change-Id: Id83d8a7bb1968cdc9684b64b5088542c64133db5 Reviewed-on: http://review.couchbase.org/114670 Tested-by: Build Bot <[email protected]> Reviewed-by: Jim Walker <[email protected]>
1 parent 324d9cb commit 3a6d750

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

daemon/memcached.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ bool associate_bucket(Connection& connection, const char* name) {
225225
disassociate_bucket(connection);
226226

227227
/* Try to associate with the named bucket */
228-
/* @todo add auth checks!!! */
229228
for (size_t ii = 1; ii < all_buckets.size() && !found; ++ii) {
230229
Bucket &b = all_buckets.at(ii);
231230
std::lock_guard<std::mutex> guard(b.mutex);

daemon/protocol/mcbp/select_bucket_executor.cc

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,6 @@ ENGINE_ERROR_CODE select_bucket(Cookie& cookie, const std::string& bucketname) {
5252
return ENGINE_ENOTSUP;
5353
}
5454

55-
// We can't switch bucket if we've got multiple commands in flight
56-
if (connection.getNumberOfCookies() > 1) {
57-
LOG_INFO(
58-
"{}: select_bucket failed - multiple commands in flight. "
59-
R"({{"cid":"{}/{:x}","connection":"{}","bucket":"{}"}})",
60-
connection.getId(),
61-
connection.getConnectionId().data(),
62-
ntohl(cookie.getRequest().getOpaque()),
63-
connection.getDescription(),
64-
bucketname);
65-
return ENGINE_ENOTSUP;
66-
}
67-
6855
auto oldIndex = connection.getBucketIndex();
6956

7057
try {
@@ -117,7 +104,28 @@ void select_bucket_executor(Cookie& cookie) {
117104

118105
auto& connection = cookie.getConnection();
119106
cookie.logCommand();
120-
auto ret = connection.remapErrorCode(select_bucket(cookie, bucketname));
107+
108+
ENGINE_ERROR_CODE code = ENGINE_SUCCESS;
109+
110+
// We can't switch bucket if we've got multiple commands in flight
111+
if (connection.getNumberOfCookies() > 1) {
112+
LOG_INFO(
113+
"{}: select_bucket failed - multiple commands in flight. "
114+
R"({{"cid":"{}/{:x}","connection":"{}","bucket":"{}"}})",
115+
connection.getId(),
116+
connection.getConnectionId().data(),
117+
ntohl(cookie.getRequest().getOpaque()),
118+
connection.getDescription(),
119+
bucketname);
120+
code = ENGINE_ENOTSUP;
121+
} else if (bucketname == "@no bucket@") {
122+
// unselect bucket!
123+
associate_bucket(connection, "");
124+
} else {
125+
code = select_bucket(cookie, bucketname);
126+
}
127+
128+
auto ret = connection.remapErrorCode(code);
121129
cookie.logResponse(ret);
122130
if (ret == ENGINE_DISCONNECT) {
123131
connection.setState(StateMachine::State::closing);

tests/testapp/testapp_bucket.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,3 +662,15 @@ TEST_P(BucketTest, TestMemcachedBucketBigObjects)
662662
connection.get(name, Vbid(0));
663663
connection.deleteBucket("mybucket_000");
664664
}
665+
666+
TEST_P(BucketTest, SelectNoBucket) {
667+
auto& connection = getAdminConnection();
668+
connection.selectBucket("default");
669+
connection.selectBucket("@no bucket@");
670+
try {
671+
connection.get("foo", Vbid(0));
672+
FAIL() << "We should get " + to_string(cb::mcbp::Status::NoBucket);
673+
} catch (const ConnectionError& error) {
674+
EXPECT_EQ(cb::mcbp::Status::NoBucket, error.getReason());
675+
}
676+
}

0 commit comments

Comments
 (0)