2021-11-28 07:29:26 +00:00
|
|
|
// Copyright 2021, Roman Gershman. All rights reserved.
|
|
|
|
// See LICENSE for licensing terms.
|
2021-11-17 14:38:32 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-02-08 15:56:47 +00:00
|
|
|
#include <absl/container/flat_hash_set.h>
|
|
|
|
|
2021-11-24 12:09:53 +00:00
|
|
|
#include "server/common_types.h"
|
2021-12-20 20:46:51 +00:00
|
|
|
#include "server/reply_builder.h"
|
2021-11-17 14:38:32 +00:00
|
|
|
|
|
|
|
namespace dfly {
|
|
|
|
|
|
|
|
class Connection;
|
|
|
|
class EngineShardSet;
|
2022-01-06 13:48:51 +00:00
|
|
|
|
|
|
|
struct StoredCmd {
|
|
|
|
const CommandId* descr;
|
|
|
|
std::vector<std::string> cmd;
|
|
|
|
|
|
|
|
StoredCmd(const CommandId* d = nullptr) : descr(d) {
|
|
|
|
}
|
|
|
|
};
|
2021-11-17 14:38:32 +00:00
|
|
|
|
2021-12-20 09:42:55 +00:00
|
|
|
struct ConnectionState {
|
2021-12-26 15:25:49 +00:00
|
|
|
DbIndex db_index = 0;
|
|
|
|
|
2022-01-06 13:48:51 +00:00
|
|
|
enum ExecState { EXEC_INACTIVE, EXEC_COLLECT, EXEC_ERROR };
|
|
|
|
|
|
|
|
ExecState exec_state = EXEC_INACTIVE;
|
|
|
|
std::vector<StoredCmd> exec_body;
|
|
|
|
|
2021-12-20 09:42:55 +00:00
|
|
|
enum Mask : uint32_t {
|
|
|
|
ASYNC_DISPATCH = 1, // whether a command is handled via async dispatch.
|
|
|
|
CONN_CLOSING = 2, // could be because of unrecoverable error or planned action.
|
2022-01-31 19:40:40 +00:00
|
|
|
|
|
|
|
// Whether this connection belongs to replica, i.e. a dragonfly slave is connected to this
|
|
|
|
// host (master) via this connection to sync from it.
|
2022-02-05 16:20:06 +00:00
|
|
|
REPL_CONNECTION = 4,
|
2022-02-20 11:49:37 +00:00
|
|
|
REQ_AUTH = 8,
|
|
|
|
AUTHENTICATED = 0x10,
|
2021-12-20 09:42:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t mask = 0; // A bitmask of Mask values.
|
2022-02-21 18:27:18 +00:00
|
|
|
|
|
|
|
enum MCGetMask {
|
|
|
|
FETCH_CAS_VER = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
2021-12-20 09:42:55 +00:00
|
|
|
|
|
|
|
bool IsClosing() const {
|
|
|
|
return mask & CONN_CLOSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsRunViaDispatch() const {
|
|
|
|
return mask & ASYNC_DISPATCH;
|
|
|
|
}
|
2022-02-05 16:20:06 +00:00
|
|
|
|
|
|
|
// Lua-script related data.
|
|
|
|
struct Script {
|
|
|
|
bool is_write = true;
|
2022-02-08 15:56:47 +00:00
|
|
|
|
|
|
|
absl::flat_hash_set<std::string_view> keys;
|
2022-02-05 16:20:06 +00:00
|
|
|
};
|
|
|
|
std::optional<Script> script_info;
|
2021-12-20 09:42:55 +00:00
|
|
|
};
|
|
|
|
|
2022-02-02 22:43:09 +00:00
|
|
|
class ConnectionContext {
|
2021-11-17 14:38:32 +00:00
|
|
|
public:
|
2021-11-23 10:39:35 +00:00
|
|
|
ConnectionContext(::io::Sink* stream, Connection* owner);
|
2021-11-17 14:38:32 +00:00
|
|
|
|
2022-01-03 09:20:08 +00:00
|
|
|
struct DebugInfo {
|
|
|
|
uint32_t shards_count = 0;
|
|
|
|
TxClock clock = 0;
|
2022-01-11 17:55:41 +00:00
|
|
|
bool is_ooo = false;
|
2022-01-03 09:20:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DebugInfo last_command_debug;
|
|
|
|
|
2021-11-17 14:38:32 +00:00
|
|
|
// TODO: to introduce proper accessors.
|
2021-12-20 20:46:51 +00:00
|
|
|
Transaction* transaction = nullptr;
|
2021-11-18 16:38:20 +00:00
|
|
|
const CommandId* cid = nullptr;
|
2021-11-17 14:38:32 +00:00
|
|
|
EngineShardSet* shard_set = nullptr;
|
|
|
|
|
2021-11-23 10:39:35 +00:00
|
|
|
Connection* owner() {
|
|
|
|
return owner_;
|
|
|
|
}
|
2021-11-17 14:38:32 +00:00
|
|
|
|
2021-11-23 15:49:40 +00:00
|
|
|
Protocol protocol() const;
|
|
|
|
|
2021-12-26 15:25:49 +00:00
|
|
|
DbIndex db_index() const {
|
|
|
|
return conn_state.db_index;
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:09:53 +00:00
|
|
|
ConnectionState conn_state;
|
|
|
|
|
2022-02-02 22:43:09 +00:00
|
|
|
// A convenient proxy for redis interface.
|
|
|
|
RedisReplyBuilder* operator->();
|
|
|
|
|
|
|
|
ReplyBuilderInterface* reply_builder() {
|
|
|
|
return rbuilder_.get();
|
|
|
|
}
|
|
|
|
|
2022-02-19 11:34:45 +00:00
|
|
|
// Allows receiving the output data from the commands called from scripts.
|
2022-02-05 19:04:32 +00:00
|
|
|
ReplyBuilderInterface* Inject(ReplyBuilderInterface* new_i) {
|
|
|
|
ReplyBuilderInterface* res = rbuilder_.release();
|
|
|
|
rbuilder_.reset(new_i);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-11-17 14:38:32 +00:00
|
|
|
private:
|
|
|
|
Connection* owner_;
|
2022-02-02 22:43:09 +00:00
|
|
|
std::unique_ptr<ReplyBuilderInterface> rbuilder_;
|
2021-11-17 14:38:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dfly
|