mirror of
https://github.com/deepseek-ai/3FS
synced 2025-06-26 18:16:45 +00:00
Initial commit
This commit is contained in:
2
src/simple_example/CMakeLists.txt
Normal file
2
src/simple_example/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
target_add_lib(simple_example core-app core-user core-service fdb simple_example-fbs mgmtd-client storage-client memory-common analytics)
|
||||
target_add_bin(simple_example_main "main.cpp" simple_example)
|
||||
16
src/simple_example/README.md
Normal file
16
src/simple_example/README.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# How to create a new service
|
||||
|
||||
1. Copy `src/simple_example` to a new subfolder in `src/`.
|
||||
2. Copy `src/fbs/simple_example` to a new subfolder in `src/fbs/`.
|
||||
3. Rename `simple_example` to `your_service_name`.
|
||||
4. Rename `SimpleExample` to `YourSimpleExample`.
|
||||
5. Add the new cmake projects to `CMakeLists.txt` files in `src/` and `src/fbs/`.
|
||||
|
||||
```bash
|
||||
svr_name='migration'
|
||||
SrvName='Migration'
|
||||
mkdir -p "src/$svr_name" && pushd src/simple_example && cp -rf --parents . "../$svr_name" && popd
|
||||
mkdir -p "src/fbs/$svr_name" && pushd src/fbs/simple_example && cp -rf --parents . "../$svr_name" && popd
|
||||
find "src/$svr_name" "src/fbs/$svr_name" -type f | xargs sed -i "s/simple_example/$svr_name/g"
|
||||
find "src/$svr_name" "src/fbs/$svr_name" -type f | xargs sed -i "s/SimpleExample/$SrvName/g"
|
||||
```
|
||||
8
src/simple_example/main.cpp
Normal file
8
src/simple_example/main.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "common/app/TwoPhaseApplication.h"
|
||||
#include "memory/common/OverrideCppNewDelete.h"
|
||||
#include "simple_example/service/Server.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
using namespace hf3fs;
|
||||
return TwoPhaseApplication<simple_example::server::SimpleExampleServer>().run(argc, argv);
|
||||
}
|
||||
68
src/simple_example/service/Server.cc
Normal file
68
src/simple_example/service/Server.cc
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "simple_example/service/Server.h"
|
||||
|
||||
#include <folly/experimental/coro/BlockingWait.h>
|
||||
#include <folly/logging/xlog.h>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include "common/app/ApplicationBase.h"
|
||||
#include "core/service/CoreService.h"
|
||||
#include "simple_example/service/Service.h"
|
||||
#include "stubs/common/RealStubFactory.h"
|
||||
#include "stubs/mgmtd/MgmtdServiceStub.h"
|
||||
|
||||
namespace hf3fs::simple_example::server {
|
||||
SimpleExampleServer::SimpleExampleServer(const SimpleExampleServer::Config &config)
|
||||
: net::Server(config.base()),
|
||||
config_(config) {}
|
||||
|
||||
SimpleExampleServer::~SimpleExampleServer() { XLOGF(INFO, "Destroying SimpleExampleServer"); }
|
||||
|
||||
Result<Void> SimpleExampleServer::beforeStart() {
|
||||
if (!backgroundClient_) {
|
||||
backgroundClient_ = std::make_unique<net::Client>(config_.background_client());
|
||||
RETURN_ON_ERROR(backgroundClient_->start());
|
||||
}
|
||||
|
||||
if (!mgmtdClient_) {
|
||||
auto ctxCreator = [this](net::Address addr) { return backgroundClient_->serdeCtx(addr); };
|
||||
mgmtdClient_ = std::make_shared<::hf3fs::client::MgmtdClientForServer>(
|
||||
appInfo().clusterId,
|
||||
std::make_unique<stubs::RealStubFactory<mgmtd::MgmtdServiceStub>>(std::move(ctxCreator)),
|
||||
config_.mgmtd_client());
|
||||
}
|
||||
|
||||
mgmtdClient_->setAppInfoForHeartbeat(appInfo());
|
||||
mgmtdClient_->setConfigListener(ApplicationBase::updateConfig);
|
||||
mgmtdClient_->updateHeartbeatPayload(flat::MetaHeartbeatInfo{});
|
||||
folly::coro::blockingWait(mgmtdClient_->start(&tpg().bgThreadPool().randomPick()));
|
||||
auto mgmtdClientRefreshRes = folly::coro::blockingWait(mgmtdClient_->refreshRoutingInfo(/*force=*/false));
|
||||
XLOGF_IF(FATAL, !mgmtdClientRefreshRes, "Failed to refresh initial routing info!");
|
||||
|
||||
auto storageClient = storage::client::StorageClient::create(ClientId::random(appInfo().hostname),
|
||||
config_.storage_client(),
|
||||
*mgmtdClient_);
|
||||
XLOGF_IF(FATAL, !storageClient, "Failed to create storage client!");
|
||||
|
||||
auto appInfo = ApplicationBase::getAppInfo();
|
||||
XLOGF_IF(FATAL, !appInfo, "AppInfo not set!");
|
||||
XLOGF_IF(FATAL, !appInfo->nodeId, "Invalid nodeId {}", appInfo->nodeId);
|
||||
RETURN_ON_ERROR(addSerdeService(std::make_unique<SimpleExampleService>(), true));
|
||||
RETURN_ON_ERROR(addSerdeService(std::make_unique<core::CoreService>()));
|
||||
|
||||
return Void{};
|
||||
}
|
||||
|
||||
Result<Void> SimpleExampleServer::beforeStop() {
|
||||
folly::coro::blockingWait([this]() -> CoTask<void> {
|
||||
if (mgmtdClient_) {
|
||||
co_await mgmtdClient_->stop();
|
||||
}
|
||||
}());
|
||||
if (backgroundClient_) {
|
||||
backgroundClient_->stopAndJoin();
|
||||
}
|
||||
return Void{};
|
||||
}
|
||||
|
||||
} // namespace hf3fs::simple_example::server
|
||||
72
src/simple_example/service/Server.h
Normal file
72
src/simple_example/service/Server.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "client/mgmtd/MgmtdClientForServer.h"
|
||||
#include "client/storage/StorageClient.h"
|
||||
#include "common/logging/LogConfig.h"
|
||||
#include "common/net/Client.h"
|
||||
#include "common/net/Server.h"
|
||||
#include "common/utils/BackgroundRunner.h"
|
||||
#include "common/utils/ConfigBase.h"
|
||||
#include "core/app/ServerAppConfig.h"
|
||||
#include "core/app/ServerLauncher.h"
|
||||
#include "core/app/ServerLauncherConfig.h"
|
||||
#include "core/app/ServerMgmtdClientFetcher.h"
|
||||
|
||||
namespace hf3fs::simple_example::server {
|
||||
|
||||
class SimpleExampleServer : public net::Server {
|
||||
public:
|
||||
static constexpr auto kName = "SimpleExample";
|
||||
static constexpr auto kNodeType = flat::NodeType::CLIENT;
|
||||
|
||||
struct CommonConfig : public ApplicationBase::Config {
|
||||
CommonConfig() {
|
||||
using logging::LogConfig;
|
||||
log().set_categories({LogConfig::makeRootCategoryConfig(), LogConfig::makeEventCategoryConfig()});
|
||||
log().set_handlers({LogConfig::makeNormalHandlerConfig(),
|
||||
LogConfig::makeErrHandlerConfig(),
|
||||
LogConfig::makeFatalHandlerConfig(),
|
||||
LogConfig::makeEventHandlerConfig()});
|
||||
}
|
||||
};
|
||||
|
||||
using AppConfig = core::ServerAppConfig;
|
||||
struct LauncherConfig : public core::ServerLauncherConfig {
|
||||
LauncherConfig() { mgmtd_client() = hf3fs::client::MgmtdClientForServer::Config{}; }
|
||||
};
|
||||
using RemoteConfigFetcher = core::launcher::ServerMgmtdClientFetcher;
|
||||
using Launcher = core::ServerLauncher<SimpleExampleServer>;
|
||||
|
||||
struct Config : public ConfigBase<Config> {
|
||||
CONFIG_OBJ(base, net::Server::Config, [](net::Server::Config &c) {
|
||||
c.set_groups_length(2);
|
||||
c.groups(0).listener().set_listen_port(8000);
|
||||
c.groups(0).set_services({"SimpleExampleSerde"});
|
||||
|
||||
c.groups(1).set_network_type(net::Address::TCP);
|
||||
c.groups(1).listener().set_listen_port(9000);
|
||||
c.groups(1).set_use_independent_thread_pool(true);
|
||||
c.groups(1).set_services({"Core"});
|
||||
});
|
||||
CONFIG_OBJ(background_client, net::Client::Config);
|
||||
CONFIG_OBJ(mgmtd_client, ::hf3fs::client::MgmtdClientForServer::Config);
|
||||
CONFIG_OBJ(storage_client, storage::client::StorageClient::Config);
|
||||
};
|
||||
|
||||
SimpleExampleServer(const Config &config);
|
||||
~SimpleExampleServer() override;
|
||||
|
||||
Result<Void> beforeStart() final;
|
||||
|
||||
Result<Void> beforeStop() final;
|
||||
|
||||
private:
|
||||
const Config &config_;
|
||||
|
||||
std::unique_ptr<net::Client> backgroundClient_;
|
||||
std::shared_ptr<::hf3fs::client::MgmtdClientForServer> mgmtdClient_;
|
||||
};
|
||||
|
||||
} // namespace hf3fs::simple_example::server
|
||||
21
src/simple_example/service/Service.cc
Normal file
21
src/simple_example/service/Service.cc
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "Service.h"
|
||||
|
||||
#include "common/serde/CallContext.h"
|
||||
#include "fbs/simple_example/SerdeService.h"
|
||||
|
||||
namespace hf3fs::simple_example::server {
|
||||
|
||||
#define DEFINE_SERVICE_METHOD(METHOD, REQ, RESP) \
|
||||
CoTryTask<RESP> SimpleExampleService::METHOD(serde::CallContext &, const REQ &req)
|
||||
|
||||
SimpleExampleService::SimpleExampleService() {}
|
||||
|
||||
DEFINE_SERVICE_METHOD(echo, SimpleExampleReq, SimpleExampleRsp) {
|
||||
SimpleExampleRsp resp;
|
||||
resp.message = req.message;
|
||||
co_return resp;
|
||||
}
|
||||
|
||||
#undef DEFINE_SERVICE_METHOD
|
||||
|
||||
} // namespace hf3fs::simple_example::server
|
||||
21
src/simple_example/service/Service.h
Normal file
21
src/simple_example/service/Service.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/serde/CallContext.h"
|
||||
#include "fbs/simple_example/SerdeService.h"
|
||||
|
||||
namespace hf3fs::simple_example::server {
|
||||
|
||||
class SimpleExampleService : public serde::ServiceWrapper<SimpleExampleService, SimpleExampleSerde> {
|
||||
public:
|
||||
SimpleExampleService();
|
||||
|
||||
#define DECLARE_SERVICE_METHOD(METHOD, REQ, RESP) CoTryTask<RESP> METHOD(serde::CallContext &, const REQ &req)
|
||||
|
||||
DECLARE_SERVICE_METHOD(echo, SimpleExampleReq, SimpleExampleRsp);
|
||||
|
||||
#undef DECLARE_SERVICE_METHOD
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
} // namespace hf3fs::simple_example::server
|
||||
Reference in New Issue
Block a user