Skip to content

Remove old spilling tmp files #7108

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

Merged
merged 14 commits into from
Jul 30, 2024
Merged
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
6 changes: 4 additions & 2 deletions ydb/core/kqp/proxy_service/kqp_proxy_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
#include <library/cpp/lwtrace/mon/mon_lwtrace.h>
#include <library/cpp/monlib/service/pages/templates.h>
#include <library/cpp/resource/resource.h>
#include <util/generic/guid.h>

#include <util/folder/dirut.h>

namespace NKikimr::NKqp {

Expand Down Expand Up @@ -236,7 +237,8 @@ class TKqpProxyService : public TActorBootstrapped<TKqpProxyService> {
if (auto& cfg = TableServiceConfig.GetSpillingServiceConfig().GetLocalFileConfig(); cfg.GetEnable()) {
TString spillingRoot = cfg.GetRoot();
if (spillingRoot.empty()) {
spillingRoot = TStringBuilder() << "/tmp/ydb_spilling_" << CreateGuidAsString() << "/";
spillingRoot = NYql::NDq::GetTmpSpillingRootForCurrentUser();
MakeDirIfNotExist(spillingRoot);
}

SpillingService = TlsActivationContext->ExecutorThread.RegisterActor(NYql::NDq::CreateDqLocalFileSpillingService(
Expand Down
74 changes: 73 additions & 1 deletion ydb/library/yql/dq/actors/spilling/spilling_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#include <util/folder/path.h>
#include <util/stream/file.h>
#include <util/thread/pool.h>
#include <util/generic/guid.h>
#include <util/folder/iterator.h>
#include <util/generic/vector.h>
#include <util/folder/dirut.h>
#include <util/system/user.h>

namespace NYql::NDq {

Expand Down Expand Up @@ -159,6 +164,7 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
EvCloseFileResponse = TEvDqSpillingLocalFile::EEv::LastEvent + 1,
EvWriteFileResponse,
EvReadFileResponse,
EvRemoveOldTmp,

LastEvent
};
Expand Down Expand Up @@ -189,6 +195,15 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
bool Removed = false;
TMaybe<TString> Error;
};

struct TEvRemoveOldTmp : public TEventLocal<TEvRemoveOldTmp, EvRemoveOldTmp> {
TFsPath TmpRoot;
ui32 NodeId;
TString SpillingSessionId;

TEvRemoveOldTmp(TFsPath tmpRoot, ui32 nodeId, TString spillingSessionId)
: TmpRoot(std::move(tmpRoot)), NodeId(nodeId), SpillingSessionId(std::move(spillingSessionId)) {}
};
};

struct TFileDesc;
Expand All @@ -206,8 +221,11 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli

void Bootstrap() {
Root_ = Config_.Root;
Root_ /= (TStringBuilder() << "node_" << SelfId().NodeId());
const auto rootToRemoveOldTmp = Root_;
const auto sessionId = Config_.SpillingSessionId;
const auto nodeId = SelfId().NodeId();

Root_ /= (TStringBuilder() << NodePrefix_ << "_" << nodeId << "_" << sessionId);
LOG_I("Init DQ local file spilling service at " << Root_ << ", actor: " << SelfId());

try {
Expand All @@ -221,6 +239,8 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
Become(&TDqLocalFileSpillingService::BrokenState);
return;
}

Send(SelfId(), MakeHolder<TEvPrivate::TEvRemoveOldTmp>(rootToRemoveOldTmp, nodeId, sessionId));

Become(&TDqLocalFileSpillingService::WorkState);
}
Expand Down Expand Up @@ -271,6 +291,7 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
hFunc(TEvPrivate::TEvWriteFileResponse, HandleWork)
hFunc(TEvDqSpilling::TEvRead, HandleWork)
hFunc(TEvPrivate::TEvReadFileResponse, HandleWork)
hFunc(TEvPrivate::TEvRemoveOldTmp, HandleWork)
hFunc(NMon::TEvHttpInfo, HandleWork)
cFunc(TEvents::TEvPoison::EventType, PassAway)
);
Expand Down Expand Up @@ -712,6 +733,50 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
Send(ev->Sender, new NMon::TEvHttpInfoRes(s.Str()));
}

void HandleWork(TEvPrivate::TEvRemoveOldTmp::TPtr& ev) {
const auto& msg = *ev->Get();
const auto& root = msg.TmpRoot;
const auto nodeIdString = ToString(msg.NodeId);
const auto& sessionId = msg.SpillingSessionId;
const auto& nodePrefix = this->NodePrefix_;

LOG_I("[RemoveOldTmp] removing at root: " << root);

const auto isDirOldTmp = [&nodePrefix, &nodeIdString, &sessionId](const TString& dirName) -> bool {
// dirName: node_<nodeId>_<sessionId>
TVector<TString> parts;
StringSplitter(dirName).Split('_').Limit(3).Collect(&parts);

if (parts.size() < 3) {
return false;
}
return parts[0] == nodePrefix && parts[1] == nodeIdString && parts[2] != sessionId;
};

try {
TDirIterator iter(root, TDirIterator::TOptions().SetMaxLevel(1));

TVector<TString> oldTmps;
for (const auto& dirEntry : iter) {
if (dirEntry.fts_info == FTS_DP) {
continue;
}

const auto dirName = dirEntry.fts_name;
if (isDirOldTmp(dirName)) {
LOG_D("[RemoveOldTmp] found old temporary at " << (root / dirName));
oldTmps.emplace_back(std::move(dirName));
}
}

for (const auto& dirName : oldTmps) {
(root / dirName).ForceDelete();
}
} catch (const yexception& e) {
LOG_E("[RemoveOldTmp] removing failed due to: " << e.what());
}
}

private:
void RunOp(TStringBuf opName, THolder<IObjectInQueue> op, TFileDesc& fd) {
if (fd.HasActiveOp) {
Expand Down Expand Up @@ -941,6 +1006,7 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli

private:
const TFileSpillingServiceConfig Config_;
const TString NodePrefix_ = "node";
TFsPath Root_;
TIntrusivePtr<TSpillingCounters> Counters_;

Expand All @@ -952,6 +1018,12 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli

} // anonymous namespace

TFsPath GetTmpSpillingRootForCurrentUser() {
auto root = TFsPath{GetSystemTempDir()};
root /= "spilling-tmp-" + GetUsername();
return root;
}

IActor* CreateDqLocalFileSpillingActor(TTxId txId, const TString& details, const TActorId& client,
bool removeBlobsAfterRead)
{
Expand Down
5 changes: 5 additions & 0 deletions ydb/library/yql/dq/actors/spilling/spilling_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#include <util/system/types.h>
#include <util/generic/strbuf.h>
#include <util/folder/path.h>
#include <util/generic/guid.h>

namespace NYql::NDq {

struct TFileSpillingServiceConfig {
TString Root;
TString SpillingSessionId = CreateGuidAsString();
ui64 MaxTotalSize = 0;
ui64 MaxFileSize = 0;
ui64 MaxFilePartSize = 0;
Expand All @@ -26,6 +29,8 @@ inline NActors::TActorId MakeDqLocalFileSpillingServiceID(ui32 nodeId) {
return NActors::TActorId(nodeId, TStringBuf(name, 12));
}

TFsPath GetTmpSpillingRootForCurrentUser();

NActors::IActor* CreateDqLocalFileSpillingActor(TTxId txId, const TString& details, const NActors::TActorId& client, bool removeBlobsAfterRead);

NActors::IActor* CreateDqLocalFileSpillingService(const TFileSpillingServiceConfig& config, TIntrusivePtr<TSpillingCounters> counters);
Expand Down
15 changes: 10 additions & 5 deletions ydb/library/yql/dq/actors/spilling/spilling_file_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ class TTestActorRuntime: public TTestActorRuntimeBase {
return str;
}

const TString& GetSpillingSessionId() const {
return SpillingSessionId_;
}

TActorId StartSpillingService(ui64 maxTotalSize = 1000, ui64 maxFileSize = 500,
ui64 maxFilePartSize = 100, const TFsPath& root = TFsPath::Cwd() / GetSpillingPrefix())
{
SpillingRoot_ = root;
SpillingSessionId_ = CreateGuidAsString();

auto config = TFileSpillingServiceConfig{
.Root = root.GetPath(),
.SpillingSessionId = SpillingSessionId_,
.MaxTotalSize = maxTotalSize,
.MaxFileSize = maxFileSize,
.MaxFilePartSize = maxFilePartSize
Expand Down Expand Up @@ -91,6 +97,7 @@ class TTestActorRuntime: public TTestActorRuntimeBase {

private:
TFsPath SpillingRoot_;
TString SpillingSessionId_;
};

TBuffer CreateBlob(ui32 size, char symbol) {
Expand Down Expand Up @@ -303,8 +310,7 @@ Y_UNIT_TEST_SUITE(DqSpillingFileTests) {
auto spillingActor = runtime.StartSpillingActor(tester);

runtime.WaitBootstrap();

const TString filePrefix = TStringBuilder() << runtime.GetSpillingRoot().GetPath() << "/node_" << runtime.GetNodeId() << "/1_test_";
const TString filePrefix = TStringBuilder() << runtime.GetSpillingRoot().GetPath() << "/node_" << runtime.GetNodeId() << "_" << runtime.GetSpillingSessionId() << "/1_test_";

for (ui32 i = 0; i < 5; ++i) {
// Cerr << "---- store blob #" << i << Endl;
Expand Down Expand Up @@ -346,7 +352,7 @@ Y_UNIT_TEST_SUITE(DqSpillingFileTests) {

runtime.WaitBootstrap();

const TString filePrefix = TStringBuilder() << runtime.GetSpillingRoot().GetPath() << "/node_" << runtime.GetNodeId() << "/1_test_";
const TString filePrefix = TStringBuilder() << runtime.GetSpillingRoot().GetPath() << "/node_" << runtime.GetNodeId() << "_" << runtime.GetSpillingSessionId() << "/1_test_";

for (ui32 i = 0; i < 5; ++i) {
// Cerr << "---- store blob #" << i << Endl;
Expand Down Expand Up @@ -393,8 +399,7 @@ Y_UNIT_TEST_SUITE(DqSpillingFileTests) {
auto resp = runtime.GrabEdgeEvent<TEvDqSpilling::TEvWriteResult>(tester);
UNIT_ASSERT_VALUES_EQUAL(0, resp->Get()->BlobId);
}

auto nodePath = TFsPath("node_" + std::to_string(spillingSvc.NodeId()));
auto nodePath = TFsPath("node_" + std::to_string(spillingSvc.NodeId()) + "_" + runtime.GetSpillingSessionId());
(runtime.GetSpillingRoot() / nodePath / "1_test_0").ForceDelete();

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ class TLocalServiceHolder {
TActorSetupCmd(resman, TMailboxType::Simple, 0));

if (withSpilling) {
char tempDir[MAX_PATH];
if (MakeTempDir(tempDir, nullptr) != 0)
ythrow yexception() << "LocalServiceHolder: Can't create temporary directory " << tempDir;
auto tempDir = NDq::GetTmpSpillingRootForCurrentUser();
MakeDirIfNotExist(tempDir);

auto spillingActor = NDq::CreateDqLocalFileSpillingService(NDq::TFileSpillingServiceConfig{.Root = tempDir, .CleanupOnShutdown = true}, MakeIntrusive<NDq::TSpillingCounters>(lwmGroup));

Expand Down
Loading