Initial commit

This commit is contained in:
dev
2025-02-27 21:53:53 +08:00
commit 815e55e4c0
1291 changed files with 185445 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
target_add_lib(migration core-app core-user core-service fdb migration-fbs mgmtd-client storage-client memory-common analytics)
target_add_bin(migration_main "main.cpp" migration)

7
src/migration/main.cpp Normal file
View File

@@ -0,0 +1,7 @@
#include "common/app/OnePhaseApplication.h"
#include "memory/common/OverrideCppNewDelete.h"
#include "migration/service/Server.h"
int main(int argc, char *argv[]) {
return hf3fs::OnePhaseApplication<hf3fs::migration::server::MigrationServer>::instance().run(argc, argv);
}

View File

@@ -0,0 +1,82 @@
#include "migration/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 "migration/service/Service.h"
#include "stubs/common/RealStubFactory.h"
#include "stubs/mgmtd/MgmtdServiceStub.h"
namespace hf3fs::migration::server {
MigrationServer::MigrationServer(const MigrationServer::Config &config)
: net::Server(config.base()),
config_(config),
clientId_(ClientId::random()) {}
MigrationServer::~MigrationServer() { XLOGF(INFO, "Destroying MigrationServer"); }
Result<Void> MigrationServer::beforeStart() {
if (!backgroundClient_) {
backgroundClient_ = std::make_unique<net::Client>(config_.background_client());
RETURN_ON_ERROR(backgroundClient_->start());
}
auto appInfo = this->appInfo();
RETURN_ON_ERROR(addSerdeService(std::make_unique<MigrationService>(), true));
RETURN_ON_ERROR(addSerdeService(std::make_unique<core::CoreService>()));
auto stubFactory = std::make_unique<hf3fs::stubs::RealStubFactory<hf3fs::mgmtd::MgmtdServiceStub>>(
hf3fs::stubs::ClientContextCreator{[this](net::Address addr) { return backgroundClient_->serdeCtx(addr); }});
auto mgmtdClient = std::make_unique<hf3fs::client::MgmtdClientForClient>(appInfo.clusterId,
std::move(stubFactory),
config_.mgmtd_client());
auto physicalHostnameRes = SysResource::hostname(/*physicalMachineName=*/true);
if (!physicalHostnameRes) {
XLOGF(ERR, "getHostname(true) failed: {}", physicalHostnameRes.error());
return makeError(StatusCode::kInvalidConfig);
}
auto containerHostnameRes = SysResource::hostname(/*physicalMachineName=*/false);
if (!containerHostnameRes) {
XLOGF(ERR, "getHostname(false) failed: {}", containerHostnameRes.error());
return makeError(StatusCode::kInvalidConfig);
}
mgmtdClient->setClientSessionPayload({clientId_.uuid.toHexString(),
flat::NodeType::CLIENT,
flat::ClientSessionData::create(
/*universalId=*/*physicalHostnameRes,
/*description=*/fmt::format("Migration: {}", *containerHostnameRes),
/*serviceGroups=*/std::vector<flat::ServiceGroupInfo>{},
flat::ReleaseVersion::fromVersionInfo()),
flat::UserInfo{}});
folly::coro::blockingWait(mgmtdClient->start(&backgroundClient_->tpg().bgThreadPool().randomPick()));
mgmtdClient_ = std::move(mgmtdClient);
auto storageClient = storage::client::StorageClient::create(clientId_, config_.storage_client(), *mgmtdClient_);
if (!storageClient) {
XLOGF(ERR, "Failed to create storage client!");
return makeError(StatusCode::kInvalidConfig);
}
return Void{};
}
Result<Void> MigrationServer::beforeStop() {
folly::coro::blockingWait([this]() -> CoTask<void> {
if (mgmtdClient_) {
co_await mgmtdClient_->stop();
}
}());
if (backgroundClient_) {
backgroundClient_->stopAndJoin();
}
return Void{};
}
} // namespace hf3fs::migration::server

View File

@@ -0,0 +1,66 @@
#pragma once
#include <memory>
#include "client/mgmtd/MgmtdClientForClient.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::migration::server {
class MigrationServer : public net::Server {
public:
static constexpr auto kName = "Migration";
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()});
}
};
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({"MigrationSerde"});
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::MgmtdClientForClient::Config);
CONFIG_OBJ(storage_client, storage::client::StorageClient::Config);
};
MigrationServer(const Config &config);
~MigrationServer() override;
Result<Void> beforeStart() final;
Result<Void> beforeStop() final;
private:
const Config &config_;
const ClientId clientId_;
std::unique_ptr<net::Client> backgroundClient_;
std::shared_ptr<::hf3fs::client::IMgmtdClientForClient> mgmtdClient_;
};
} // namespace hf3fs::migration::server

View File

@@ -0,0 +1,19 @@
#include "Service.h"
#include "common/serde/CallContext.h"
#include "fbs/migration/SerdeService.h"
namespace hf3fs::migration::server {
#define DEFINE_SERVICE_METHOD(METHOD, REQ, RESP) \
CoTryTask<RESP> MigrationService::METHOD(serde::CallContext &, const REQ &req)
MigrationService::MigrationService() {}
DEFINE_SERVICE_METHOD(start, StartJobReq, StartJobRsp) { co_return makeError(StatusCode::kNotImplemented); }
DEFINE_SERVICE_METHOD(stop, StopJobReq, StopJobRsp) { co_return makeError(StatusCode::kNotImplemented); }
DEFINE_SERVICE_METHOD(list, ListJobsReq, ListJobsRsp) { co_return makeError(StatusCode::kNotImplemented); }
#undef DEFINE_SERVICE_METHOD
} // namespace hf3fs::migration::server

View File

@@ -0,0 +1,23 @@
#pragma once
#include "common/serde/CallContext.h"
#include "fbs/migration/SerdeService.h"
namespace hf3fs::migration::server {
class MigrationService : public serde::ServiceWrapper<MigrationService, MigrationSerde> {
public:
MigrationService();
#define DECLARE_SERVICE_METHOD(METHOD, REQ, RESP) CoTryTask<RESP> METHOD(serde::CallContext &, const REQ &req)
DECLARE_SERVICE_METHOD(start, StartJobReq, StartJobRsp);
DECLARE_SERVICE_METHOD(stop, StopJobReq, StopJobRsp);
DECLARE_SERVICE_METHOD(list, ListJobsReq, ListJobsRsp);
#undef DECLARE_SERVICE_METHOD
private:
};
} // namespace hf3fs::migration::server