Skip to content

Commit dc58d27

Browse files
authored
Merge 8b1dd39 into ae7146c
2 parents ae7146c + 8b1dd39 commit dc58d27

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

ydb/core/kqp/proxy_service/kqp_proxy_service.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
#include <library/cpp/lwtrace/mon/mon_lwtrace.h>
4545
#include <library/cpp/monlib/service/pages/templates.h>
4646
#include <library/cpp/resource/resource.h>
47-
#include <util/generic/guid.h>
48-
4947
namespace NKikimr::NKqp {
5048

5149
namespace {
@@ -236,7 +234,7 @@ class TKqpProxyService : public TActorBootstrapped<TKqpProxyService> {
236234
if (auto& cfg = TableServiceConfig.GetSpillingServiceConfig().GetLocalFileConfig(); cfg.GetEnable()) {
237235
TString spillingRoot = cfg.GetRoot();
238236
if (spillingRoot.empty()) {
239-
spillingRoot = TStringBuilder() << "/tmp/ydb_spilling_" << CreateGuidAsString() << "/";
237+
spillingRoot = NYql::NDq::GetTmpSpillingRootForCurrentUser();
240238
}
241239

242240
SpillingService = TlsActivationContext->ExecutorThread.RegisterActor(NYql::NDq::CreateDqLocalFileSpillingService(

ydb/library/yql/dq/actors/spilling/spilling_file.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
#include <util/folder/path.h>
1414
#include <util/stream/file.h>
1515
#include <util/thread/pool.h>
16+
#include <util/generic/guid.h>
17+
#include <util/folder/iterator.h>
18+
#include <util/generic/vector.h>
19+
#include <util/folder/dirut.h>
20+
#include <util/system/user.h>
1621

1722
namespace NYql::NDq {
1823

@@ -159,6 +164,7 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
159164
EvCloseFileResponse = TEvDqSpillingLocalFile::EEv::LastEvent + 1,
160165
EvWriteFileResponse,
161166
EvReadFileResponse,
167+
EvRemoveOldTmp,
162168

163169
LastEvent
164170
};
@@ -189,6 +195,15 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
189195
bool Removed = false;
190196
TMaybe<TString> Error;
191197
};
198+
199+
struct TEvRemoveOldTmp : public TEventLocal<TEvRemoveOldTmp, EvRemoveOldTmp> {
200+
TFsPath TmpRoot;
201+
ui32 NodeId;
202+
TString GuidString;
203+
204+
TEvRemoveOldTmp(TFsPath tmpRoot, ui32 nodeId, TString guidString)
205+
: TmpRoot(std::move(tmpRoot)), NodeId(nodeId), GuidString(std::move(guidString)) {}
206+
};
192207
};
193208

194209
struct TFileDesc;
@@ -206,8 +221,13 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
206221

207222
void Bootstrap() {
208223
Root_ = Config_.Root;
209-
Root_ /= (TStringBuilder() << "node_" << SelfId().NodeId());
224+
const auto nodeId = SelfId().NodeId();
225+
const auto guidString = TGUID::Create().AsGuidString();
210226

227+
Send(SelfId(), MakeHolder<TEvPrivate::TEvRemoveOldTmp>(Root_, nodeId, guidString));
228+
229+
Root_ /= (TStringBuilder() << "node_" << nodeId << "_" << guidString);
230+
Cerr << "Root from config: " << Config_.Root << ", actual root: " << Root_ << "\n";
211231
LOG_I("Init DQ local file spilling service at " << Root_ << ", actor: " << SelfId());
212232

213233
try {
@@ -271,6 +291,7 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
271291
hFunc(TEvPrivate::TEvWriteFileResponse, HandleWork)
272292
hFunc(TEvDqSpilling::TEvRead, HandleWork)
273293
hFunc(TEvPrivate::TEvReadFileResponse, HandleWork)
294+
hFunc(TEvPrivate::TEvRemoveOldTmp, HandleWork)
274295
hFunc(NMon::TEvHttpInfo, HandleWork)
275296
cFunc(TEvents::TEvPoison::EventType, PassAway)
276297
);
@@ -712,6 +733,41 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
712733
Send(ev->Sender, new NMon::TEvHttpInfoRes(s.Str()));
713734
}
714735

736+
void HandleWork(TEvPrivate::TEvRemoveOldTmp::TPtr& ev) {
737+
auto& msg = *ev->Get();
738+
auto& root = msg.TmpRoot;
739+
auto nodeIdString = ToString(msg.NodeId);
740+
auto& guidString = msg.GuidString;
741+
742+
LOG_I("[RemoveOldTmp] removing at root: " << root);
743+
744+
try {
745+
TDirIterator iter(root, TDirIterator::TOptions().SetMaxLevel(1));
746+
747+
TVector<TString> oldTmps;
748+
for (const auto &dirEntry : iter) {
749+
if (dirEntry.fts_info == FTS_DP) {
750+
// skip postorder visit
751+
continue;
752+
}
753+
TString dirName = dirEntry.fts_name;
754+
TVector<TString> parts;
755+
StringSplitter(dirName).Split('_').Collect(&parts);
756+
757+
if (parts.size() == 3 && parts[0] == "node" && parts[1] == nodeIdString && parts[2] != guidString) {
758+
LOG_D("[RemoveOldTmp] found old temporary at " << (root / dirName));
759+
oldTmps.emplace_back(std::move(dirName));
760+
}
761+
}
762+
763+
ForEach(oldTmps.begin(), oldTmps.end(), [&root](const auto& dirName) {
764+
(root / dirName).ForceDelete();
765+
});
766+
} catch (const yexception& e) {
767+
LOG_E("[RemoveOldTmp] removing failed due to: " << e.what());
768+
}
769+
}
770+
715771
private:
716772
void RunOp(TStringBuf opName, THolder<IObjectInQueue> op, TFileDesc& fd) {
717773
if (fd.HasActiveOp) {
@@ -952,6 +1008,13 @@ class TDqLocalFileSpillingService : public TActorBootstrapped<TDqLocalFileSpilli
9521008

9531009
} // anonymous namespace
9541010

1011+
TFsPath GetTmpSpillingRootForCurrentUser() {
1012+
auto root = TFsPath{GetSystemTempDir()};
1013+
root /= "spilling-tmp-" + GetUsername();
1014+
MakeDirIfNotExist(root);
1015+
return root;
1016+
}
1017+
9551018
IActor* CreateDqLocalFileSpillingActor(TTxId txId, const TString& details, const TActorId& client,
9561019
bool removeBlobsAfterRead)
9571020
{

ydb/library/yql/dq/actors/spilling/spilling_file.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <util/system/types.h>
99
#include <util/generic/strbuf.h>
10+
#include <util/folder/path.h>
1011

1112
namespace NYql::NDq {
1213

@@ -26,6 +27,8 @@ inline NActors::TActorId MakeDqLocalFileSpillingServiceID(ui32 nodeId) {
2627
return NActors::TActorId(nodeId, TStringBuf(name, 12));
2728
}
2829

30+
TFsPath GetTmpSpillingRootForCurrentUser();
31+
2932
NActors::IActor* CreateDqLocalFileSpillingActor(TTxId txId, const TString& details, const NActors::TActorId& client, bool removeBlobsAfterRead);
3033

3134
NActors::IActor* CreateDqLocalFileSpillingService(const TFileSpillingServiceConfig& config, TIntrusivePtr<TSpillingCounters> counters);

ydb/library/yql/providers/dq/local_gateway/yql_dq_gateway_local.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ class TLocalServiceHolder {
8080
TActorSetupCmd(resman, TMailboxType::Simple, 0));
8181

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

87-
auto spillingActor = NDq::CreateDqLocalFileSpillingService(NDq::TFileSpillingServiceConfig{.Root = tempDir, .CleanupOnShutdown = true}, MakeIntrusive<NDq::TSpillingCounters>(lwmGroup));
85+
auto spillingActor = NDq::CreateDqLocalFileSpillingService(NDq::TFileSpillingServiceConfig{.Root = tempDir, .CleanupOnShutdown = false}, MakeIntrusive<NDq::TSpillingCounters>(lwmGroup));
8886

8987
ServiceNode->AddLocalService(
9088
NDq::MakeDqLocalFileSpillingServiceID(nodeId),

0 commit comments

Comments
 (0)