Skip to content
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

(Work in progress) Support binary protocol #207

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions mcrouter/lib/IOBufUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ coalesceIovecs(const struct iovec* iov, size_t iovcnt, size_t destCapacity) {
}
return coalesceSlow(iov, iovcnt, destCapacity);
}

}
} // facebook::memcache
33 changes: 33 additions & 0 deletions mcrouter/lib/IOBufUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,38 @@ copyAsString(const folly::IOBuf& source, const uint8_t* begin, size_t size) {
*/
folly::IOBuf
coalesceIovecs(const struct iovec* iov, size_t iovcnt, size_t destCapacity);

/**
* Trim IOBuf to reference only data from range [posStart, posEnd).
*/
inline void trimIOBufToRange(
folly::IOBuf& buffer,
const char* posStart,
const char* posEnd) {
buffer.trimStart(posStart - reinterpret_cast<const char*>(buffer.data()));
buffer.trimEnd(buffer.length() - (posEnd - posStart));
}


inline void appendKeyPiece(
const folly::IOBuf& from,
folly::IOBuf& to,
const char* posStart,
const char* posEnd) {
// No need to process empty piece.
if (UNLIKELY(posEnd == posStart)) {
return;
}

if (LIKELY(to.length() == 0)) {
from.cloneOneInto(to);
trimIOBufToRange(to, posStart, posEnd);
} else {
auto nextPiece = from.cloneOne();
trimIOBufToRange(*nextPiece, posStart, posEnd);
to.prependChain(std::move(nextPiece));
}
}

}
} // facebook::memcache
2 changes: 2 additions & 0 deletions mcrouter/lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ libmcrouter_a_SOURCES = \
network/McAsciiParser-inl.h \
network/McAsciiParser.cpp \
network/McAsciiParser.h \
network/McBinaryParser.cpp \
network/McBinaryParser.h \
network/McClientRequestContext-inl.h \
network/McClientRequestContext.cpp \
network/McClientRequestContext.h \
Expand Down
17 changes: 17 additions & 0 deletions mcrouter/lib/carbon/RequestCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ class RequestCommon {
fbtraceInfo_ = McFbtraceRef::moveRef(carbonFbtraceInfo);
}
#endif
/*
bool quiet() const {
return quiet_;
}
bool& quiet() {
return quiet_;
}
bool returnKey() const {
return returnKey_;
}
bool& returnKey() {
return returnKey_;
}*/

/**
* Tells whether or not "serializedBuffer()" is dirty, in which case it can't
Expand Down Expand Up @@ -130,6 +143,10 @@ class RequestCommon {

private:
static constexpr size_t kTraceIdSize = 11;
// bool quiet_{false};
// bool returnKey_{false};
};


const folly::IOBuf* serializedBuffer_{nullptr};

Expand Down
65 changes: 65 additions & 0 deletions mcrouter/lib/mc/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,69 @@ const char* mc_req_err_to_string(const mc_req_err_t err);
*/
const char* mc_res_to_response_string(const mc_res_t result);

typedef enum mc_opcode_e : uint8_t {
mc_opcode_noop = 0x0a,
mc_opcode_set = 0x01,
mc_opcode_setq = 0x11,
mc_opcode_add = 0x02,
mc_opcode_addq = 0x12,
mc_opcode_replace = 0x03,
mc_opcode_replaceq = 0x13,
mc_opcode_append = 0x0e,
mc_opcode_appendq = 0x19,
mc_opcode_prepend = 0x0f,
mc_opcode_prependq = 0x1a,
mc_opcode_get = 0x00,
mc_opcode_getq = 0x09,
mc_opcode_getk = 0x0c,
mc_opcode_getkq = 0x0d,
mc_opcode_delete = 0x04,
mc_opcode_deleteq = 0x14,
mc_opcode_increment = 0x05,
mc_opcode_incrementq = 0x15,
mc_opcode_decrement = 0x06,
mc_opcode_decrementq = 0x16,
mc_opcode_touch = 0x1c,
mc_opcode_gat = 0x1d,
mc_opcode_gatq = 0x1e,
mc_opcode_stat = 0x10,
mc_opcode_version = 0x0b,
mc_opcode_quit = 0x07,
mc_opcode_quitq = 0x17,
mc_opcode_flush = 0x08,
mc_opcode_flushq = 0x18,
// SASL commands
mc_opcode_sasllistmechs = 0x20,
mc_opcode_saslauth = 0x21,
mc_opcode_saslstep = 0x22,
// Range commands
mc_opcode_rset = 0x31,
mc_opcode_rsetq = 0x32,
mc_opcode_rappend = 0x33,
mc_opcode_rappendq = 0x34,
mc_opcode_rprepend = 0x35,
mc_opcode_rprependq = 0x36,
mc_opcode_rget = 0x30,
mc_opcode_rdelete = 0x37,
mc_opcode_rdeleteq = 0x38,
mc_opcode_rincr = 0x39,
mc_opcode_rincrq = 0x3a,
mc_opcode_rdecr = 0x3b,
mc_opcode_rdecrq = 0x3c,
// v1.6 proposed commands
mc_opcode_setvbucket = 0x3d,
mc_opcode_tapvbucketset = 0x45,
mc_opcode_getvbucket = 0x3e,
mc_opcode_tapdelete = 0x42,
mc_opcode_verbosity = 0x1b,
mc_opcode_tapflush = 0x43,
mc_opcode_delvbucket = 0x3f,
mc_opcode_tapconnect = 0x40,
mc_opcode_tapmutation = 0x41,
mc_opcode_tapopaque = 0x44,
mc_opcode_tapcheckpointstart = 0x46,
mc_opcode_tapcheckpointend = 0x47,
} mc_opcode_t;


__END_DECLS
5 changes: 5 additions & 0 deletions mcrouter/lib/network/ClientMcParser-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ void ClientMcParser<Callback>::handleAscii(folly::IOBuf& readBuffer) {
}
}

template <class Callback>
void ClientMcParser<Callback>::handleBinary(folly::IOBuf& readBuffer) {
LOG(ERROR) << "handleBinary() not available to client parser";
}

template <class Callback>
void ClientMcParser<Callback>::parseError(
mc_res_t result,
Expand Down
1 change: 1 addition & 0 deletions mcrouter/lib/network/ClientMcParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class ClientMcParser : private McParser::ParserCallback {
const UmbrellaMessageInfo& headerInfo,
const folly::IOBuf& buffer) final;
void handleAscii(folly::IOBuf& readBuffer) final;
void handleBinary(folly::IOBuf& readBuffer) final;
void parseError(mc_res_t result, folly::StringPiece reason) final;

bool shouldReadToAsciiBuffer() const;
Expand Down
35 changes: 1 addition & 34 deletions mcrouter/lib/network/McAsciiParser-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <arpa/inet.h>

#include "mcrouter/lib/fbi/cpp/util.h"
#include "mcrouter/lib/IOBufUtil.h"

namespace facebook {
namespace memcache {
Expand Down Expand Up @@ -136,40 +137,6 @@ void McClientAsciiParser::initializeReplyParser() {
typeid(Request).name());
}

/**
* Append piece of IOBuf in range [posStart, posEnd) to destination IOBuf.
*/
inline void McAsciiParserBase::appendKeyPiece(
const folly::IOBuf& from,
folly::IOBuf& to,
const char* posStart,
const char* posEnd) {
// No need to process empty piece.
if (UNLIKELY(posEnd == posStart)) {
return;
}

if (LIKELY(to.length() == 0)) {
from.cloneOneInto(to);
trimIOBufToRange(to, posStart, posEnd);
} else {
auto nextPiece = from.cloneOne();
trimIOBufToRange(*nextPiece, posStart, posEnd);
to.prependChain(std::move(nextPiece));
}
}

/**
* Trim IOBuf to reference only data from range [posStart, posEnd).
*/
inline void McAsciiParserBase::trimIOBufToRange(
folly::IOBuf& buffer,
const char* posStart,
const char* posEnd) {
buffer.trimStart(posStart - reinterpret_cast<const char*>(buffer.data()));
buffer.trimEnd(buffer.length() - (posEnd - posStart));
}

template <class Callback>
McServerAsciiParser::McServerAsciiParser(Callback& callback)
: callback_(
Expand Down
13 changes: 3 additions & 10 deletions mcrouter/lib/network/McAsciiParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ class McAsciiParserBase {
bool readValue(folly::IOBuf& buffer, folly::IOBuf& to);
bool readValue(folly::IOBuf& buffer, folly::Optional<folly::IOBuf>& to);

static void appendKeyPiece(
const folly::IOBuf& from,
folly::IOBuf& to,
const char* posStart,
const char* posEnd);
static void trimIOBufToRange(
folly::IOBuf& buffer,
const char* posStart,
const char* posEnd);

std::string currentErrorDescription_;

uint64_t currentUInt_{0};
Expand Down Expand Up @@ -177,6 +167,9 @@ class McClientAsciiParser : public McAsciiParserBase {
namespace detail {
template <class RequestList>
class CallbackBase;

template <class Callback, class Requests>
class CallbackWrapper;
} // detail

class McServerAsciiParser : public McAsciiParserBase {
Expand Down
1 change: 1 addition & 0 deletions mcrouter/lib/network/McAsciiParser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "mcrouter/lib/mc/msg.h"
#include "mcrouter/lib/McOperation.h"
#include "mcrouter/lib/network/gen/Memcache.h"
#include "mcrouter/lib/IOBufUtil.h"

namespace facebook { namespace memcache {

Expand Down
Loading