mirror of
https://github.com/deepseek-ai/3FS
synced 2025-06-26 18:16:45 +00:00
Initial commit
This commit is contained in:
5
src/stubs/CMakeLists.txt
Normal file
5
src/stubs/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_subdirectory(common)
|
||||
#add_subdirectory(ClientAgentService)
|
||||
add_subdirectory(MetaService)
|
||||
add_subdirectory(mgmtd)
|
||||
add_subdirectory(core)
|
||||
1
src/stubs/MetaService/CMakeLists.txt
Normal file
1
src/stubs/MetaService/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_add_lib(meta-stub stubs-common meta-fbs)
|
||||
48
src/stubs/MetaService/IMetaServiceStub.h
Normal file
48
src/stubs/MetaService/IMetaServiceStub.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/serde/ClientContext.h"
|
||||
#include "common/serde/MessagePacket.h"
|
||||
#include "common/utils/Coroutine.h"
|
||||
#include "fbs/meta/Common.h"
|
||||
#include "fbs/meta/Service.h"
|
||||
|
||||
namespace hf3fs::meta {
|
||||
|
||||
class IMetaServiceStub {
|
||||
public:
|
||||
using InterfaceType = IMetaServiceStub;
|
||||
|
||||
virtual ~IMetaServiceStub() = default;
|
||||
|
||||
#define IMETA_STUB_METHOD(NAME, REQ, RESP) \
|
||||
virtual CoTryTask<RESP> NAME(const REQ &req, \
|
||||
const net::UserRequestOptions &options, \
|
||||
serde::Timestamp *timestamp = nullptr) = 0
|
||||
|
||||
IMETA_STUB_METHOD(statFs, StatFsReq, StatFsRsp);
|
||||
IMETA_STUB_METHOD(stat, StatReq, StatRsp);
|
||||
IMETA_STUB_METHOD(create, CreateReq, CreateRsp);
|
||||
IMETA_STUB_METHOD(mkdirs, MkdirsReq, MkdirsRsp);
|
||||
IMETA_STUB_METHOD(symlink, SymlinkReq, SymlinkRsp);
|
||||
IMETA_STUB_METHOD(hardLink, HardLinkReq, HardLinkRsp);
|
||||
IMETA_STUB_METHOD(remove, RemoveReq, RemoveRsp);
|
||||
IMETA_STUB_METHOD(open, OpenReq, OpenRsp);
|
||||
IMETA_STUB_METHOD(sync, SyncReq, SyncRsp);
|
||||
IMETA_STUB_METHOD(close, CloseReq, CloseRsp);
|
||||
IMETA_STUB_METHOD(rename, RenameReq, RenameRsp);
|
||||
IMETA_STUB_METHOD(list, ListReq, ListRsp);
|
||||
IMETA_STUB_METHOD(truncate, TruncateReq, TruncateRsp);
|
||||
IMETA_STUB_METHOD(getRealPath, GetRealPathReq, GetRealPathRsp);
|
||||
IMETA_STUB_METHOD(setAttr, SetAttrReq, SetAttrRsp);
|
||||
IMETA_STUB_METHOD(pruneSession, PruneSessionReq, PruneSessionRsp);
|
||||
IMETA_STUB_METHOD(dropUserCache, DropUserCacheReq, DropUserCacheRsp);
|
||||
IMETA_STUB_METHOD(authenticate, AuthReq, AuthRsp);
|
||||
IMETA_STUB_METHOD(lockDirectory, LockDirectoryReq, LockDirectoryRsp);
|
||||
IMETA_STUB_METHOD(testRpc, TestRpcReq, TestRpcRsp);
|
||||
IMETA_STUB_METHOD(batchStat, BatchStatReq, BatchStatRsp);
|
||||
IMETA_STUB_METHOD(batchStatByPath, BatchStatByPathReq, BatchStatByPathRsp);
|
||||
|
||||
#undef IMETA_STUB_METHOD
|
||||
};
|
||||
|
||||
} // namespace hf3fs::meta
|
||||
44
src/stubs/MetaService/MetaServiceStub.cc
Normal file
44
src/stubs/MetaService/MetaServiceStub.cc
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "MetaServiceStub.h"
|
||||
|
||||
#include "common/serde/ClientContext.h"
|
||||
#include "common/serde/ClientMockContext.h"
|
||||
#include "common/utils/Coroutine.h"
|
||||
#include "common/utils/UtcTimeSerde.h"
|
||||
#include "fbs/meta/Service.h"
|
||||
|
||||
#define IMPL_META_STUB_METHOD(NAME, REQ, RESP) \
|
||||
template <typename Context> \
|
||||
CoTryTask<RESP> MetaServiceStub<Context>::NAME(const REQ &req, \
|
||||
const net::UserRequestOptions &options, \
|
||||
hf3fs::serde::Timestamp *timestamp) { \
|
||||
co_return co_await MetaSerde<>::NAME<Context>(context_, req, &options, timestamp); \
|
||||
}
|
||||
|
||||
namespace hf3fs::meta {
|
||||
|
||||
IMPL_META_STUB_METHOD(statFs, StatFsReq, StatFsRsp);
|
||||
IMPL_META_STUB_METHOD(stat, StatReq, StatRsp);
|
||||
IMPL_META_STUB_METHOD(create, CreateReq, CreateRsp);
|
||||
IMPL_META_STUB_METHOD(mkdirs, MkdirsReq, MkdirsRsp);
|
||||
IMPL_META_STUB_METHOD(symlink, SymlinkReq, SymlinkRsp);
|
||||
IMPL_META_STUB_METHOD(hardLink, HardLinkReq, HardLinkRsp);
|
||||
IMPL_META_STUB_METHOD(remove, RemoveReq, RemoveRsp);
|
||||
IMPL_META_STUB_METHOD(open, OpenReq, OpenRsp);
|
||||
IMPL_META_STUB_METHOD(sync, SyncReq, SyncRsp);
|
||||
IMPL_META_STUB_METHOD(close, CloseReq, CloseRsp);
|
||||
IMPL_META_STUB_METHOD(rename, RenameReq, RenameRsp);
|
||||
IMPL_META_STUB_METHOD(list, ListReq, ListRsp);
|
||||
IMPL_META_STUB_METHOD(truncate, TruncateReq, TruncateRsp);
|
||||
IMPL_META_STUB_METHOD(getRealPath, GetRealPathReq, GetRealPathRsp);
|
||||
IMPL_META_STUB_METHOD(setAttr, SetAttrReq, SetAttrRsp);
|
||||
IMPL_META_STUB_METHOD(pruneSession, PruneSessionReq, PruneSessionRsp);
|
||||
IMPL_META_STUB_METHOD(dropUserCache, DropUserCacheReq, DropUserCacheRsp);
|
||||
IMPL_META_STUB_METHOD(authenticate, AuthReq, AuthRsp);
|
||||
IMPL_META_STUB_METHOD(lockDirectory, LockDirectoryReq, LockDirectoryRsp);
|
||||
IMPL_META_STUB_METHOD(testRpc, TestRpcReq, TestRpcRsp);
|
||||
IMPL_META_STUB_METHOD(batchStat, BatchStatReq, BatchStatRsp);
|
||||
IMPL_META_STUB_METHOD(batchStatByPath, BatchStatByPathReq, BatchStatByPathRsp);
|
||||
|
||||
template class MetaServiceStub<serde::ClientContext>;
|
||||
template class MetaServiceStub<serde::ClientMockContext>;
|
||||
} // namespace hf3fs::meta
|
||||
48
src/stubs/MetaService/MetaServiceStub.h
Normal file
48
src/stubs/MetaService/MetaServiceStub.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/serde/ClientContext.h"
|
||||
#include "fbs/meta/Service.h"
|
||||
#include "stubs/MetaService/IMetaServiceStub.h"
|
||||
|
||||
namespace hf3fs::meta {
|
||||
|
||||
template <typename Ctx>
|
||||
class MetaServiceStub : public IMetaServiceStub {
|
||||
public:
|
||||
explicit MetaServiceStub(Ctx ctx)
|
||||
: context_(std::move(ctx)) {}
|
||||
|
||||
#define META_STUB_METHOD(NAME, REQ, RESP) \
|
||||
CoTryTask<RESP> NAME(const REQ &req, const net::UserRequestOptions &options, serde::Timestamp *timestamp = nullptr) \
|
||||
override
|
||||
|
||||
META_STUB_METHOD(statFs, StatFsReq, StatFsRsp);
|
||||
META_STUB_METHOD(stat, StatReq, StatRsp);
|
||||
META_STUB_METHOD(create, CreateReq, CreateRsp);
|
||||
META_STUB_METHOD(mkdirs, MkdirsReq, MkdirsRsp);
|
||||
META_STUB_METHOD(symlink, SymlinkReq, SymlinkRsp);
|
||||
META_STUB_METHOD(hardLink, HardLinkReq, HardLinkRsp);
|
||||
META_STUB_METHOD(remove, RemoveReq, RemoveRsp);
|
||||
META_STUB_METHOD(open, OpenReq, OpenRsp);
|
||||
META_STUB_METHOD(sync, SyncReq, SyncRsp);
|
||||
META_STUB_METHOD(close, CloseReq, CloseRsp);
|
||||
META_STUB_METHOD(rename, RenameReq, RenameRsp);
|
||||
META_STUB_METHOD(list, ListReq, ListRsp);
|
||||
META_STUB_METHOD(truncate, TruncateReq, TruncateRsp);
|
||||
META_STUB_METHOD(getRealPath, GetRealPathReq, GetRealPathRsp);
|
||||
META_STUB_METHOD(setAttr, SetAttrReq, SetAttrRsp);
|
||||
META_STUB_METHOD(pruneSession, PruneSessionReq, PruneSessionRsp);
|
||||
META_STUB_METHOD(dropUserCache, DropUserCacheReq, DropUserCacheRsp);
|
||||
META_STUB_METHOD(authenticate, AuthReq, AuthRsp);
|
||||
META_STUB_METHOD(lockDirectory, LockDirectoryReq, LockDirectoryRsp);
|
||||
META_STUB_METHOD(testRpc, TestRpcReq, TestRpcRsp);
|
||||
META_STUB_METHOD(batchStat, BatchStatReq, BatchStatRsp);
|
||||
META_STUB_METHOD(batchStatByPath, BatchStatByPathReq, BatchStatByPathRsp);
|
||||
|
||||
#undef META_STUB_METHOD
|
||||
|
||||
private:
|
||||
Ctx context_;
|
||||
};
|
||||
|
||||
} // namespace hf3fs::meta
|
||||
148
src/stubs/MetaService/MockMetaServiceStub.h
Normal file
148
src/stubs/MetaService/MockMetaServiceStub.h
Normal file
@@ -0,0 +1,148 @@
|
||||
#pragma once
|
||||
|
||||
#include <folly/concurrency/AtomicSharedPtr.h>
|
||||
#include <optional>
|
||||
|
||||
#include "common/serde/ClientContext.h"
|
||||
#include "common/serde/MessagePacket.h"
|
||||
#include "common/utils/Coroutine.h"
|
||||
#include "common/utils/Result.h"
|
||||
#include "fbs/meta/Service.h"
|
||||
#include "fbs/mgmtd/ChainRef.h"
|
||||
#include "stubs/MetaService/IMetaServiceStub.h"
|
||||
#include "stubs/MetaService/MetaServiceStub.h"
|
||||
#include "stubs/common/Stub.h"
|
||||
|
||||
namespace hf3fs::meta {
|
||||
|
||||
using flat::ChainTableId;
|
||||
using flat::Uid;
|
||||
using flat::UserInfo;
|
||||
|
||||
class DummyMetaServiceStub : public IMetaServiceStub {
|
||||
public:
|
||||
~DummyMetaServiceStub() override = default;
|
||||
|
||||
#define NOT_IMPLEMENTED_FUNC(NAME, REQ, RESP) \
|
||||
CoTryTask<RESP> NAME(const REQ &req, const net::UserRequestOptions &, serde::Timestamp *) override { \
|
||||
co_return co_await NAME(req); \
|
||||
} \
|
||||
virtual CoTryTask<RESP> NAME(const REQ &) { co_return makeError(StatusCode::kNotImplemented); }
|
||||
|
||||
NOT_IMPLEMENTED_FUNC(statFs, StatFsReq, StatFsRsp);
|
||||
NOT_IMPLEMENTED_FUNC(stat, StatReq, StatRsp);
|
||||
NOT_IMPLEMENTED_FUNC(create, CreateReq, CreateRsp);
|
||||
NOT_IMPLEMENTED_FUNC(mkdirs, MkdirsReq, MkdirsRsp);
|
||||
NOT_IMPLEMENTED_FUNC(symlink, SymlinkReq, SymlinkRsp);
|
||||
NOT_IMPLEMENTED_FUNC(hardLink, HardLinkReq, HardLinkRsp);
|
||||
NOT_IMPLEMENTED_FUNC(remove, RemoveReq, RemoveRsp);
|
||||
NOT_IMPLEMENTED_FUNC(open, OpenReq, OpenRsp);
|
||||
NOT_IMPLEMENTED_FUNC(sync, SyncReq, SyncRsp);
|
||||
NOT_IMPLEMENTED_FUNC(close, CloseReq, CloseRsp);
|
||||
NOT_IMPLEMENTED_FUNC(rename, RenameReq, RenameRsp);
|
||||
NOT_IMPLEMENTED_FUNC(list, ListReq, ListRsp);
|
||||
NOT_IMPLEMENTED_FUNC(truncate, TruncateReq, TruncateRsp);
|
||||
NOT_IMPLEMENTED_FUNC(getRealPath, GetRealPathReq, GetRealPathRsp);
|
||||
NOT_IMPLEMENTED_FUNC(setAttr, SetAttrReq, SetAttrRsp);
|
||||
NOT_IMPLEMENTED_FUNC(pruneSession, PruneSessionReq, PruneSessionRsp);
|
||||
NOT_IMPLEMENTED_FUNC(dropUserCache, DropUserCacheReq, DropUserCacheRsp);
|
||||
NOT_IMPLEMENTED_FUNC(lockDirectory, LockDirectoryReq, LockDirectoryRsp);
|
||||
NOT_IMPLEMENTED_FUNC(testRpc, TestRpcReq, TestRpcRsp);
|
||||
NOT_IMPLEMENTED_FUNC(batchStat, BatchStatReq, BatchStatRsp);
|
||||
NOT_IMPLEMENTED_FUNC(batchStatByPath, BatchStatByPathReq, BatchStatByPathRsp);
|
||||
|
||||
virtual CoTryTask<AuthRsp> authenticate(const AuthReq &req) { co_return AuthRsp{req.user}; }
|
||||
CoTryTask<AuthRsp> authenticate(const AuthReq &req, const net::UserRequestOptions &, serde::Timestamp *) override {
|
||||
co_return co_await authenticate(req);
|
||||
}
|
||||
|
||||
#undef NOT_IMPLEMENTED_FUNC
|
||||
};
|
||||
|
||||
struct MockMetaStubHolder {
|
||||
void setStub(std::unique_ptr<DummyMetaServiceStub> st) { stub = std::move(st); }
|
||||
|
||||
folly::atomic_shared_ptr<IMetaServiceStub> stub;
|
||||
};
|
||||
|
||||
class DummyMetaServiceStubWithInode : public DummyMetaServiceStub {
|
||||
public:
|
||||
DummyMetaServiceStubWithInode(std::variant<File, Directory, Symlink> data)
|
||||
: inode_({InodeId(0x10de1d), {data, Acl{Uid(0), Gid(0), Permission(0777)}}}) {}
|
||||
|
||||
CoTryTask<CreateRsp> create(const CreateReq &) override { co_return CreateRsp(inode_, false); }
|
||||
CoTryTask<OpenRsp> open(const OpenReq &) override { co_return OpenRsp(inode_, false); }
|
||||
CoTryTask<StatRsp> stat(const StatReq &) override { co_return StatRsp(inode_); }
|
||||
|
||||
protected:
|
||||
Inode inode_;
|
||||
};
|
||||
class DummyMetaServiceStubWithDir : public DummyMetaServiceStubWithInode {
|
||||
public:
|
||||
DummyMetaServiceStubWithDir()
|
||||
: DummyMetaServiceStubWithInode(Directory{InodeId(0x10de1dff), Layout::newEmpty(ChainTableId(1), 512 << 10, 8)}) {
|
||||
}
|
||||
};
|
||||
class DummyMetaServiceStubWithFile : public DummyMetaServiceStubWithInode {
|
||||
public:
|
||||
DummyMetaServiceStubWithFile()
|
||||
: DummyMetaServiceStubWithInode(File(Layout::newEmpty(ChainTableId(1), 512 << 10, 8))) {}
|
||||
};
|
||||
class DummyMetaServiceStubWithSymlink : public DummyMetaServiceStubWithInode {
|
||||
public:
|
||||
DummyMetaServiceStubWithSymlink()
|
||||
: DummyMetaServiceStubWithInode(Symlink{"/b"}) {}
|
||||
};
|
||||
} // namespace hf3fs::meta
|
||||
|
||||
template <>
|
||||
struct ::hf3fs::stubs::StubMockContext<hf3fs::meta::IMetaServiceStub> {
|
||||
std::shared_ptr<meta::MockMetaStubHolder> stub;
|
||||
};
|
||||
|
||||
namespace hf3fs::meta {
|
||||
|
||||
template <>
|
||||
class MetaServiceStub<hf3fs::stubs::StubMockContext<IMetaServiceStub>> : public IMetaServiceStub {
|
||||
public:
|
||||
using ContextType = hf3fs::stubs::StubMockContext<IMetaServiceStub>;
|
||||
|
||||
MetaServiceStub(ContextType ctx)
|
||||
: stub_(std::move(ctx.stub)) {}
|
||||
~MetaServiceStub() override = default;
|
||||
|
||||
#define FORWARD_RPC_FUNC(NAME, REQ, RESP) \
|
||||
CoTryTask<RESP> NAME(const REQ &req, const net::UserRequestOptions &opts, serde::Timestamp *timestamp) override { \
|
||||
co_return co_await stub_->stub.load()->NAME(req, opts, timestamp); \
|
||||
}
|
||||
|
||||
FORWARD_RPC_FUNC(statFs, StatFsReq, StatFsRsp);
|
||||
FORWARD_RPC_FUNC(stat, StatReq, StatRsp);
|
||||
FORWARD_RPC_FUNC(create, CreateReq, CreateRsp);
|
||||
FORWARD_RPC_FUNC(mkdirs, MkdirsReq, MkdirsRsp);
|
||||
FORWARD_RPC_FUNC(symlink, SymlinkReq, SymlinkRsp);
|
||||
FORWARD_RPC_FUNC(hardLink, HardLinkReq, HardLinkRsp);
|
||||
FORWARD_RPC_FUNC(remove, RemoveReq, RemoveRsp);
|
||||
FORWARD_RPC_FUNC(open, OpenReq, OpenRsp);
|
||||
FORWARD_RPC_FUNC(sync, SyncReq, SyncRsp);
|
||||
FORWARD_RPC_FUNC(close, CloseReq, CloseRsp);
|
||||
FORWARD_RPC_FUNC(rename, RenameReq, RenameRsp);
|
||||
FORWARD_RPC_FUNC(list, ListReq, ListRsp);
|
||||
FORWARD_RPC_FUNC(truncate, TruncateReq, TruncateRsp);
|
||||
FORWARD_RPC_FUNC(getRealPath, GetRealPathReq, GetRealPathRsp);
|
||||
FORWARD_RPC_FUNC(setAttr, SetAttrReq, SetAttrRsp);
|
||||
FORWARD_RPC_FUNC(pruneSession, PruneSessionReq, PruneSessionRsp);
|
||||
FORWARD_RPC_FUNC(dropUserCache, DropUserCacheReq, DropUserCacheRsp);
|
||||
FORWARD_RPC_FUNC(authenticate, AuthReq, AuthRsp);
|
||||
FORWARD_RPC_FUNC(lockDirectory, LockDirectoryReq, LockDirectoryRsp);
|
||||
FORWARD_RPC_FUNC(testRpc, TestRpcReq, TestRpcRsp);
|
||||
FORWARD_RPC_FUNC(batchStat, BatchStatReq, BatchStatRsp);
|
||||
FORWARD_RPC_FUNC(batchStatByPath, BatchStatByPathReq, BatchStatByPathRsp);
|
||||
|
||||
#undef FORWARD_RPC_FUNC
|
||||
|
||||
private:
|
||||
std::shared_ptr<MockMetaStubHolder> stub_;
|
||||
};
|
||||
|
||||
} // namespace hf3fs::meta
|
||||
1
src/stubs/common/CMakeLists.txt
Normal file
1
src/stubs/common/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_add_lib(stubs-common common meta-fbs mgmtd-fbs core-service-fbs)
|
||||
16
src/stubs/common/IStubFactory.h
Normal file
16
src/stubs/common/IStubFactory.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "common/utils/Address.h"
|
||||
|
||||
namespace hf3fs::stubs {
|
||||
template <typename IStub>
|
||||
class IStubFactory {
|
||||
public:
|
||||
using Stub = IStub;
|
||||
|
||||
virtual ~IStubFactory() = default;
|
||||
virtual std::unique_ptr<IStub> create(net::Address addr) = 0;
|
||||
};
|
||||
} // namespace hf3fs::stubs
|
||||
26
src/stubs/common/RealStubFactory.h
Normal file
26
src/stubs/common/RealStubFactory.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "IStubFactory.h"
|
||||
#include "common/serde/ClientContext.h"
|
||||
|
||||
namespace hf3fs::stubs {
|
||||
using ClientContextCreator = std::function<serde::ClientContext(net::Address)>;
|
||||
|
||||
template <template <typename Ctx> typename Stub>
|
||||
requires requires { typename Stub<serde::ClientContext>::InterfaceType; }
|
||||
class RealStubFactory : public IStubFactory<typename Stub<serde::ClientContext>::InterfaceType> {
|
||||
public:
|
||||
using StubType = Stub<serde::ClientContext>;
|
||||
using InterfaceType = typename StubType::InterfaceType;
|
||||
|
||||
explicit RealStubFactory(ClientContextCreator creator)
|
||||
: creator_(std::move(creator)) {}
|
||||
|
||||
std::unique_ptr<InterfaceType> create(net::Address addr) override {
|
||||
return std::make_unique<StubType>(creator_(addr));
|
||||
}
|
||||
|
||||
private:
|
||||
ClientContextCreator creator_;
|
||||
};
|
||||
} // namespace hf3fs::stubs
|
||||
46
src/stubs/common/Stub.h
Normal file
46
src/stubs/common/Stub.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "common/serde/ClientContext.h"
|
||||
#include "common/serde/ClientMockContext.h"
|
||||
#include "fbs/mgmtd/MgmtdTypes.h"
|
||||
|
||||
namespace hf3fs::stubs {
|
||||
using flat::NodeId;
|
||||
|
||||
template <typename IStub>
|
||||
struct StubMockContext {};
|
||||
|
||||
template <template <typename Ctx> typename StubImpl, bool SupportsStubStub = false>
|
||||
class SerdeStub {
|
||||
public:
|
||||
using RealStub = StubImpl<serde::ClientContext>;
|
||||
using MockStub = StubImpl<serde::ClientMockContext>;
|
||||
using IStub = typename RealStub::InterfaceType;
|
||||
using MockContext = std::conditional_t<SupportsStubStub, stubs::StubMockContext<IStub>, int>;
|
||||
using StubStub = std::conditional_t<SupportsStubStub, StubImpl<MockContext>, void>;
|
||||
|
||||
template <typename Ctx>
|
||||
SerdeStub(Ctx ctx, NodeId node = NodeId(0), std::string_view host = "mock")
|
||||
: nodeId(node),
|
||||
hostname(host),
|
||||
stub_{std::in_place_type<StubImpl<Ctx>>, std::move(ctx)} {}
|
||||
|
||||
IStub *operator->() { return get(); }
|
||||
|
||||
IStub *get() {
|
||||
return std::visit([](auto &v) -> IStub * { return &v; }, stub_);
|
||||
}
|
||||
|
||||
public:
|
||||
NodeId nodeId{0};
|
||||
std::string hostname;
|
||||
|
||||
private:
|
||||
std::conditional_t<SupportsStubStub, std::variant<RealStub, MockStub, StubStub>, std::variant<RealStub, MockStub>>
|
||||
stub_;
|
||||
};
|
||||
|
||||
} // namespace hf3fs::stubs
|
||||
70
src/stubs/common/StubFactory.h
Normal file
70
src/stubs/common/StubFactory.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "common/serde/ClientContext.h"
|
||||
#include "common/utils/RobinHood.h"
|
||||
#include "stubs/common/Stub.h"
|
||||
|
||||
namespace hf3fs::stubs {
|
||||
|
||||
template <template <typename Ctx> typename StubImpl, bool SupportsStubStub = false>
|
||||
class SerdeStubFactory {
|
||||
public:
|
||||
using Stub = stubs::SerdeStub<StubImpl, SupportsStubStub>;
|
||||
using MockStub = typename Stub::MockStub;
|
||||
using IStub = typename Stub::IStub;
|
||||
using MockContext = typename Stub::MockContext;
|
||||
using StubStub = typename Stub::StubStub;
|
||||
|
||||
using SerdeContextCreator = std::function<serde::ClientContext(net::Address)>;
|
||||
explicit SerdeStubFactory(SerdeContextCreator creator)
|
||||
: ctx_(std::move(creator)) {}
|
||||
explicit SerdeStubFactory(serde::ClientMockContext context)
|
||||
: ctx_(std::move(context)) {}
|
||||
explicit SerdeStubFactory(robin_hood::unordered_map<net::Address, serde::ClientMockContext> contextMap)
|
||||
: ctx_(std::move(contextMap)) {}
|
||||
explicit SerdeStubFactory(MockContext ctx)
|
||||
: ctx_(std::move(ctx)) {}
|
||||
|
||||
Stub create(net::Address addr, NodeId node = NodeId(0), std::string_view host = "mock") {
|
||||
struct MakeStub {
|
||||
MakeStub(net::Address addr, NodeId node, std::string_view host)
|
||||
: addr_(addr),
|
||||
node_(node),
|
||||
host_(host) {}
|
||||
Stub operator()(SerdeContextCreator &creator) { return Stub(creator(addr_), node_, host_); }
|
||||
Stub operator()(serde::ClientMockContext &ctx) { return Stub(ctx); }
|
||||
Stub operator()(robin_hood::unordered_map<net::Address, serde::ClientMockContext> &map) {
|
||||
return Stub(map[addr_], node_, host_);
|
||||
}
|
||||
Stub operator()(MockContext &ctx) {
|
||||
if constexpr (SupportsStubStub) {
|
||||
return Stub(ctx);
|
||||
} else {
|
||||
return Stub{};
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
net::Address addr_;
|
||||
NodeId node_;
|
||||
std::string_view host_;
|
||||
};
|
||||
|
||||
return std::visit(MakeStub(addr, node, host), ctx_);
|
||||
}
|
||||
|
||||
private:
|
||||
std::conditional_t<SupportsStubStub,
|
||||
std::variant<SerdeContextCreator,
|
||||
serde::ClientMockContext,
|
||||
robin_hood::unordered_map<net::Address, serde::ClientMockContext>,
|
||||
MockContext>,
|
||||
std::variant<SerdeContextCreator,
|
||||
serde::ClientMockContext,
|
||||
robin_hood::unordered_map<net::Address, serde::ClientMockContext>>>
|
||||
ctx_;
|
||||
};
|
||||
|
||||
} // namespace hf3fs::stubs
|
||||
1
src/stubs/core/CMakeLists.txt
Normal file
1
src/stubs/core/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_add_lib(core-stub stubs-common)
|
||||
17
src/stubs/core/CoreServiceStub.cc
Normal file
17
src/stubs/core/CoreServiceStub.cc
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "CoreServiceStub.h"
|
||||
|
||||
#include "common/serde/ClientContext.h"
|
||||
#include "common/serde/ClientMockContext.h"
|
||||
#include "fbs/core/service/CoreServiceClient.h"
|
||||
|
||||
namespace hf3fs::core {
|
||||
#define DEFINE_SERDE_SERVICE_METHOD_FULL(svc, name, Name, id, reqtype, rsptype) \
|
||||
template <typename Ctx> \
|
||||
CoTryTask<rsptype> svc##ServiceStub<Ctx>::name(const reqtype &req) { \
|
||||
co_return co_await svc##ServiceClient<>::send<#name>(ctx_, req); \
|
||||
}
|
||||
#include "fbs/core/service/CoreServiceDef.h"
|
||||
|
||||
template class CoreServiceStub<serde::ClientContext>;
|
||||
template class CoreServiceStub<serde::ClientMockContext>;
|
||||
} // namespace hf3fs::core
|
||||
21
src/stubs/core/CoreServiceStub.h
Normal file
21
src/stubs/core/CoreServiceStub.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "ICoreServiceStub.h"
|
||||
|
||||
namespace hf3fs::core {
|
||||
template <typename Ctx>
|
||||
class CoreServiceStub : public ICoreServiceStub {
|
||||
public:
|
||||
using ContextType = Ctx;
|
||||
explicit CoreServiceStub(ContextType ctx)
|
||||
: ctx_(std::move(ctx)) {}
|
||||
|
||||
#define DEFINE_SERDE_SERVICE_METHOD_FULL(svc, name, Name, id, reqtype, rsptype) \
|
||||
CoTryTask<rsptype> name(const reqtype &req) override;
|
||||
|
||||
#include "fbs/core/service/CoreServiceDef.h"
|
||||
|
||||
private:
|
||||
ContextType ctx_;
|
||||
};
|
||||
} // namespace hf3fs::core
|
||||
17
src/stubs/core/ICoreServiceStub.h
Normal file
17
src/stubs/core/ICoreServiceStub.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "fbs/core/service/Rpc.h"
|
||||
|
||||
namespace hf3fs::core {
|
||||
|
||||
class ICoreServiceStub {
|
||||
public:
|
||||
using InterfaceType = ICoreServiceStub;
|
||||
|
||||
virtual ~ICoreServiceStub() = default;
|
||||
|
||||
#define DEFINE_SERDE_SERVICE_METHOD_FULL(svc, name, Name, id, reqtype, rsptype) \
|
||||
virtual CoTryTask<rsptype> name(const reqtype &req) = 0;
|
||||
#include "fbs/core/service/CoreServiceDef.h"
|
||||
};
|
||||
} // namespace hf3fs::core
|
||||
1
src/stubs/mgmtd/CMakeLists.txt
Normal file
1
src/stubs/mgmtd/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_add_lib(mgmtd-stub stubs-common)
|
||||
17
src/stubs/mgmtd/IMgmtdServiceStub.h
Normal file
17
src/stubs/mgmtd/IMgmtdServiceStub.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "fbs/mgmtd/Rpc.h"
|
||||
|
||||
namespace hf3fs::mgmtd {
|
||||
|
||||
class IMgmtdServiceStub {
|
||||
public:
|
||||
using InterfaceType = IMgmtdServiceStub;
|
||||
|
||||
virtual ~IMgmtdServiceStub() = default;
|
||||
|
||||
#define DEFINE_SERDE_SERVICE_METHOD_FULL(svc, name, Name, id, reqtype, rsptype) \
|
||||
virtual CoTryTask<rsptype> name(const reqtype &req) = 0;
|
||||
#include "fbs/mgmtd/MgmtdServiceDef.h"
|
||||
};
|
||||
} // namespace hf3fs::mgmtd
|
||||
17
src/stubs/mgmtd/MgmtdServiceStub.cc
Normal file
17
src/stubs/mgmtd/MgmtdServiceStub.cc
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "MgmtdServiceStub.h"
|
||||
|
||||
#include "common/serde/ClientContext.h"
|
||||
#include "common/serde/ClientMockContext.h"
|
||||
#include "fbs/mgmtd/MgmtdServiceClient.h"
|
||||
|
||||
namespace hf3fs::mgmtd {
|
||||
#define DEFINE_SERDE_SERVICE_METHOD_FULL(svc, name, Name, id, reqtype, rsptype) \
|
||||
template <typename Ctx> \
|
||||
CoTryTask<rsptype> svc##ServiceStub<Ctx>::name(const reqtype &req) { \
|
||||
co_return co_await svc##ServiceClient<>::send<#name>(ctx_, req); \
|
||||
}
|
||||
#include "fbs/mgmtd/MgmtdServiceDef.h"
|
||||
|
||||
template class MgmtdServiceStub<serde::ClientContext>;
|
||||
template class MgmtdServiceStub<serde::ClientMockContext>;
|
||||
} // namespace hf3fs::mgmtd
|
||||
21
src/stubs/mgmtd/MgmtdServiceStub.h
Normal file
21
src/stubs/mgmtd/MgmtdServiceStub.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "IMgmtdServiceStub.h"
|
||||
|
||||
namespace hf3fs::mgmtd {
|
||||
template <typename Ctx>
|
||||
class MgmtdServiceStub : public IMgmtdServiceStub {
|
||||
public:
|
||||
using ContextType = Ctx;
|
||||
explicit MgmtdServiceStub(ContextType ctx)
|
||||
: ctx_(std::move(ctx)) {}
|
||||
|
||||
#define DEFINE_SERDE_SERVICE_METHOD_FULL(svc, name, Name, id, reqtype, rsptype) \
|
||||
CoTryTask<rsptype> name(const reqtype &req) override;
|
||||
|
||||
#include "fbs/mgmtd/MgmtdServiceDef.h"
|
||||
|
||||
private:
|
||||
ContextType ctx_;
|
||||
};
|
||||
} // namespace hf3fs::mgmtd
|
||||
Reference in New Issue
Block a user