Skip to content

[WIP] (string_family): Add support for MC GAT #5199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/facade/memcache_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ TEST_F(MCParserTest, Meta) {
EXPECT_TRUE(cmd_.return_hit);
}

TEST_F(MCParserTest, Gat) {
auto res = parser_.Parse("gat 1000 foo bar baz\r\n", &consumed_, &cmd_);
EXPECT_EQ(MemcacheParser::OK, res);
EXPECT_EQ(consumed_, 22);
EXPECT_EQ(cmd_.type, MemcacheParser::GAT);
EXPECT_EQ(cmd_.key, "foo");
EXPECT_THAT(cmd_.keys_ext, UnorderedElementsAre("bar", "baz"));
EXPECT_EQ(cmd_.expire_ts, 1000);

cmd_ = {};
res = parser_.Parse("gat foo bar\r\n", &consumed_, &cmd_);
EXPECT_EQ(MemcacheParser::BAD_INT, res);
}

class MCParserNoreplyTest : public MCParserTest {
protected:
void RunTest(string_view str, bool noreply) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/acl/acl_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ TEST_F(AclFamilyTest, TestCat) {
UnorderedElementsAre("GETSET", "GETRANGE", "INCRBYFLOAT", "GETDEL", "DECRBY",
"PREPEND", "SETEX", "MSET", "SET", "PSETEX", "SUBSTR", "DECR",
"STRLEN", "INCR", "INCRBY", "MGET", "GET", "SETNX", "GETEX",
"APPEND", "MSETNX", "SETRANGE"));
"APPEND", "MSETNX", "SETRANGE", "GAT"));
}

TEST_F(AclFamilyTest, TestGetUser) {
Expand Down
7 changes: 5 additions & 2 deletions src/server/conn_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ struct ConnectionState {

enum MCGetMask {
FETCH_CAS_VER = 1,
SET_EXPIRY = 2,
};

size_t UsedMemory() const;
Expand Down Expand Up @@ -257,8 +258,10 @@ struct ConnectionState {

// used for memcache set/get commands.
// For set op - it's the flag value we are storing along with the value.
// For get op - we use it as a mask of MCGetMask values.
uint32_t memcache_flag = 0;
// For get op - we use it as a mask of MCGetMask values stored in the highest two bits
// For GAT and GATS - the expiry in seconds supplied is stored in the top 62 bits along with the
// MCGetMask values in the lower two bits
uint64_t memcache_flag = 0;

ExecInfo exec_info;
ReplicationInfo replication_info;
Expand Down
19 changes: 19 additions & 0 deletions src/server/dragonfly_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,25 @@ TEST_F(DflyEngineTest, Memcache) {
EXPECT_THAT(resp, ElementsAre("VALUE key 1 3 0", "bar", "VALUE key2 2 8 0", "bar2val2", "END"));
}

TEST_F(DflyEngineTest, MemcacheGat) {
using m = MemcacheParser;

EXPECT_THAT(GetMC(m::GAT, {"1000", "foo", "bar"}), ElementsAre("END"));
EXPECT_THAT(RunMC(m::SET, "exp-key", "exp-val"), ElementsAre("STORED"));
EXPECT_THAT(GetMC(m::GAT, {"1", "exp-key"}), ElementsAre("VALUE exp-key 0 7", "exp-val", "END"));

AdvanceTime(2 * 1000);
EXPECT_THAT(RunMC(m::GET, "exp-key"), ElementsAre("END"));

EXPECT_THAT(RunMC(m::SET, "a", "a-val"), ElementsAre("STORED"));
EXPECT_THAT(RunMC(m::SET, "x", "x-val"), ElementsAre("STORED"));
EXPECT_THAT(GetMC(m::GAT, {"3", "a", "x", "z", "foo"}),
ElementsAre("VALUE a 0 5", "a-val", "VALUE x 0 5", "x-val", "END"));

AdvanceTime(2 * 1000);
EXPECT_THAT(GetMC(m::GAT, {"3", "a", "x", "z", "foo"}), ElementsAre("END"));
}

TEST_F(DflyEngineTest, MemcacheFlags) {
using MP = MemcacheParser;

Expand Down
21 changes: 15 additions & 6 deletions src/server/main_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,9 @@ void Service::DispatchMC(const MemcacheParser::Command& cmd, std::string_view va
case MemcacheParser::QUIT:
strcpy(cmd_name, "QUIT");
break;
case MemcacheParser::GAT:
strcpy(cmd_name, "GAT");
break;
case MemcacheParser::STATS:
server_family_.StatsMC(cmd.key, mc_builder);
return;
Expand All @@ -1616,6 +1619,13 @@ void Service::DispatchMC(const MemcacheParser::Command& cmd, std::string_view va

ConnectionContext* dfly_cntx = static_cast<ConnectionContext*>(cntx);

// if expire_ts is greater than month it's a unix timestamp
// https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L139
constexpr uint32_t kExpireLimit = 60 * 60 * 24 * 30;
const uint64_t expire_ts = cmd.expire_ts && cmd.expire_ts <= kExpireLimit
? cmd.expire_ts + time(nullptr)
: cmd.expire_ts;

if (MemcacheParser::IsStoreCmd(cmd.type)) {
char* v = const_cast<char*>(value.data());
args.emplace_back(v, value.size());
Expand All @@ -1624,12 +1634,6 @@ void Service::DispatchMC(const MemcacheParser::Command& cmd, std::string_view va
args.emplace_back(store_opt, strlen(store_opt));
}

// if expire_ts is greater than month it's a unix timestamp
// https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L139
constexpr uint32_t kExpireLimit = 60 * 60 * 24 * 30;
const uint64_t expire_ts = cmd.expire_ts && cmd.expire_ts <= kExpireLimit
? cmd.expire_ts + time(nullptr)
: cmd.expire_ts;
if (expire_ts && memcmp(cmd_name, "SET", 3) == 0) {
char* next = absl::numbers_internal::FastIntToBuffer(expire_ts, ttl);
args.emplace_back(ttl_op, 4);
Expand All @@ -1641,6 +1645,11 @@ void Service::DispatchMC(const MemcacheParser::Command& cmd, std::string_view va
char* key = const_cast<char*>(s.data());
args.emplace_back(key, s.size());
}

if (cmd.type == MemcacheParser::GAT) {
dfly_cntx->conn_state.memcache_flag |= ConnectionState::SET_EXPIRY;
dfly_cntx->conn_state.memcache_flag |= expire_ts << 2;
}
if (cmd.type == MemcacheParser::GETS) {
dfly_cntx->conn_state.memcache_flag |= ConnectionState::FETCH_CAS_VER;
}
Expand Down
Loading
Loading