aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/interface/temp.cpp
blob: 04250d12a647c1cddcb1638da55a079f540b6920 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "temp.h"
#include "errors.h"

#include <yt/cpp/mapreduce/interface/config.h>

#include <util/system/yassert.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

TTempTable::TTempTable(
    IClientBasePtr client,
    const TString& prefix,
    const TYPath& directory,
    const TCreateOptions& options)
    : Client_(std::move(client))
{
    Name_ = CreateTempTable(Client_, prefix, directory, options);
}

TTempTable::TTempTable(TTempTable::TPrivateConstuctorTag, IClientBasePtr client, TYPath path, const TCreateOptions& options)
    : Client_(std::move(client))
    , Name_(std::move(path))
{
    Client_->Create(Name_, NT_TABLE, options);
}

TTempTable::TTempTable(TTempTable&& sourceTable)
    : Client_(sourceTable.Client_)
    , Name_(sourceTable.Name_)
    , Owns_(sourceTable.Owns_)
{
    sourceTable.Owns_ = false;
}

TTempTable& TTempTable::operator=(TTempTable&& sourceTable)
{
    if (&sourceTable == this) {
        return *this;
    }

    if (Owns_) {
        RemoveTable();
    }

    Client_ = sourceTable.Client_;
    Name_ = sourceTable.Name_;
    Owns_ = sourceTable.Owns_;

    sourceTable.Owns_ = false;

    return *this;
}

TTempTable TTempTable::CreateAutoremovingTable(IClientBasePtr client, TYPath path, const TCreateOptions& options)
{
    return TTempTable(TTempTable::TPrivateConstuctorTag(), std::move(client), std::move(path), options);
}

void TTempTable::RemoveTable()
{
    if (TConfig::Get()->KeepTempTables) {
        return;
    }
    Client_->Remove(Name_, TRemoveOptions().Force(true));
}

TTempTable::~TTempTable()
{
    if (Owns_) {
        try {
            RemoveTable();
        } catch (...) {
        }
    }
}

TString TTempTable::Name() const &
{
    return Name_;
}

TString TTempTable::Release()
{
    Y_ASSERT(Owns_);
    Owns_ = false;
    return Name_;
}

////////////////////////////////////////////////////////////////////////////////

TYPath CreateTempTable(
    const IClientBasePtr& client,
    const TString& prefix,
    const TYPath& directory,
    const TCreateOptions& options)
{
    TYPath result;
    bool createDirectory = false;

    if (directory) {
        result = directory;
    } else {
        result = TConfig::Get()->RemoteTempTablesDirectory;

        // User might override configuration above.
        // Class used to create directory in such cases if it doesn't exist.
        // We keep this behaviour.
        createDirectory = true;
    }

    if (result == DefaultRemoteTempTablesDirectory) {
        result += "/";
        result += client->GetParentClient()->WhoAmI().Login;
        createDirectory = true;
    }

    auto resultDirectory = result;

    result += "/";
    result += prefix;
    result += CreateGuidAsString();

    if (!createDirectory) {
        client->Create(result, NT_TABLE, options);
    } else {
        // Directory we are going to create our table in can be missing.
        // We create it explicitly outside of any transactions,
        // because concurrent processes might want to create the same directory from other transactions.
        //
        // It's unlikely but directory might be removed between directory creation and table creation
        // we retry attempt if it was failed with path resolution error.
        const int maxAttempts = 3;
        for (int i = 0; i < maxAttempts; ++i) {
            client->GetParentClient()->Create(resultDirectory, NT_MAP, TCreateOptions().Recursive(true).IgnoreExisting(true));

            try {
                client->Create(result, NT_TABLE, options);
                break;
            } catch (const TErrorResponse& error) {
                if (error.IsResolveError()) {
                    if (i < maxAttempts - 1) {
                        continue;
                    }
                }
                throw;
            }
        }
    }

    return result;
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT