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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include "storage.h"
#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/threading/future/async.h>
#include <util/datetime/base.h>
#include <util/folder/path.h>
#include <util/generic/algorithm.h>
#include <util/generic/yexception.h>
#include <util/stream/file.h>
#include <util/stream/input.h>
#include <util/stream/str.h>
using namespace NYql;
using namespace NThreading;
static TString DATA = "1234567890";
static TString DATA_MD5 = "e807f1fcf82d132f9bb018ca6738a19f";
Y_UNIT_TEST_SUITE(TStorageTests) {
class TTestDir {
private:
TFsPath Path_;
public:
TTestDir(const TString& name) {
Y_ENSURE(name.length() > 0, "have to specify name");
Y_ENSURE(name.find('.') == TString::npos, "must be simple name");
Y_ENSURE(name.find('/') == TString::npos, "must be simple name");
Y_ENSURE(name.find('\\') == TString::npos, "must be simple name");
Path_ = TFsPath(name);
Path_.ForceDelete();
Path_.MkDir();
}
~TTestDir() {
Path_.ForceDelete();
}
TFsPath GetFsPath() const {
return Path_;
}
};
TFileLinkPtr PutFile(const TString& name, TStorage& storage, const TString& tmpOutName = {}, int sleepSeconds = 0) {
return storage.Put(name, tmpOutName, DATA_MD5, [&](const TFsPath& storagePath) {
Sleep(TDuration::Seconds(sleepSeconds));
TStringInput in(DATA);
TUnbufferedFileOutput out(storagePath);
return std::make_pair(TransferData(&in, &out), DATA_MD5);
});
}
TFileLinkPtr PutFileWithSleep(const TString& name, TStorage& storage) {
return PutFile(name, storage, {}, 1);
}
Y_UNIT_TEST(Put) {
THolder<TStorage> storage = MakeHolder<TStorage>(10, 100);
TFileLinkPtr fileInStorage = PutFile("test.file", *storage, "somename");
TFsPath rootPath(fileInStorage->GetPath());
UNIT_ASSERT(rootPath.Exists());
UNIT_ASSERT_VALUES_EQUAL(rootPath.GetName(), "somename");
UNIT_ASSERT_VALUES_EQUAL(fileInStorage->GetStorageFileName(), "test.file");
UNIT_ASSERT(fileInStorage->GetPath().IsSubpathOf(storage->GetRoot().Fix()));
UNIT_ASSERT_VALUES_EQUAL(TIFStream(fileInStorage->GetPath()).ReadAll(), DATA);
UNIT_ASSERT_EQUAL(storage->GetCount(), 1);
UNIT_ASSERT_EQUAL(storage->GetOccupiedSize(), DATA.size());
fileInStorage.Reset();
UNIT_ASSERT(false == rootPath.Exists());
}
Y_UNIT_TEST(ParallelPut) {
THolder<TStorage> storage = MakeHolder<TStorage>(10, 100);
TThreadPool queue;
queue.Start(10);
TVector<TFuture<TFileLinkPtr>> res;
res.reserve(10);
for (size_t i = 0; i < 10; ++i) {
res.emplace_back(Async([&]() {
return PutFileWithSleep("test.file", *storage);
}, queue));
}
for (auto& f: res) {
f.Wait();
TFileLinkPtr fileInStorage = f.GetValue();
UNIT_ASSERT(fileInStorage->GetPath().Exists());
UNIT_ASSERT_VALUES_EQUAL(fileInStorage->GetStorageFileName(), "test.file");
UNIT_ASSERT(fileInStorage->GetPath().IsSubpathOf(storage->GetRoot().Fix()));
UNIT_ASSERT_VALUES_EQUAL(TIFStream(fileInStorage->GetPath()).ReadAll(), DATA);
UNIT_ASSERT_EQUAL(storage->GetCount(), 1);
UNIT_ASSERT_EQUAL(storage->GetOccupiedSize(), DATA.size());
}
}
Y_UNIT_TEST(CleanUp) {
THolder<TStorage> storage = MakeHolder<TStorage>(10, 100);
TFileLinkPtr fileInStorage = PutFile("test.file", *storage);
UNIT_ASSERT(fileInStorage->GetPath().Exists());
auto rootPath = storage->GetRoot();
storage.Destroy();
UNIT_ASSERT(!fileInStorage->GetPath().Exists());
UNIT_ASSERT(!rootPath.Exists());
}
Y_UNIT_TEST(DisplaceByCount) {
ui64 maxCount = 2;
THolder<TStorage> storage = MakeHolder<TStorage>(maxCount, 100);
TFileLinkPtr file1 = PutFile("test1.file", *storage);
TFileLinkPtr file2 = PutFile("test2.file", *storage);
UNIT_ASSERT_VALUES_EQUAL(storage->GetCount(), 2);
UNIT_ASSERT_VALUES_EQUAL(storage->GetOccupiedSize(), 2 * DATA.size());
UNIT_ASSERT(file1->GetPath().Exists());
UNIT_ASSERT(file2->GetPath().Exists());
TFileLinkPtr file3 = PutFileWithSleep("test3.file", *storage);
// after putting 3rd file we exceeded the limit by count
// and cleaned up half of the original maxCount files
UNIT_ASSERT_VALUES_EQUAL(storage->GetCount(), maxCount / 2);
UNIT_ASSERT_VALUES_EQUAL(storage->GetOccupiedSize(), (maxCount / 2) * DATA.size());
// despite of file1 and file2 are displaced from storage, we still
// have temporary hardlinks to it
UNIT_ASSERT(file1->GetPath().Exists());
UNIT_ASSERT(file2->GetPath().Exists());
UNIT_ASSERT(file3->GetPath().Exists());
TVector<TString> filesInStorage;
storage->GetRoot().ListNames(filesInStorage);
UNIT_ASSERT_EQUAL(filesInStorage.size(), 2); // 1 file + 1 hardlink directory
auto beg = filesInStorage.begin(),
end = filesInStorage.end();
// file1 and file2 were displaced
UNIT_ASSERT(Find(beg, end, file1->GetStorageFileName()) == end);
UNIT_ASSERT(Find(beg, end, file2->GetStorageFileName()) == end);
UNIT_ASSERT(Find(beg, end, file3->GetStorageFileName()) != end);
}
Y_UNIT_TEST(DisplaceBySize) {
ui64 maxSize = 25;
THolder<TStorage> storage = MakeHolder<TStorage>(10, maxSize);
TFileLinkPtr file1 = PutFile("test1.file", *storage);
TFileLinkPtr file2 = PutFile("test2.file", *storage);
UNIT_ASSERT_VALUES_EQUAL(storage->GetCount(), 2);
UNIT_ASSERT_VALUES_EQUAL(storage->GetOccupiedSize(), 2 * DATA.size());
UNIT_ASSERT(file1->GetPath().Exists());
UNIT_ASSERT(file2->GetPath().Exists());
TFileLinkPtr file3 = PutFileWithSleep("test3.file", *storage);
// after putting 3rd file we exceeded the limit by size
// and cleaned up files till we get half of the original maxSize
UNIT_ASSERT_VALUES_EQUAL(storage->GetCount(), 1);
UNIT_ASSERT_VALUES_EQUAL(storage->GetOccupiedSize(), DATA.size());
// despite of file1 and file2 are displaced from storage, we still
// have temporary hardlinks to it
UNIT_ASSERT(file1->GetPath().Exists());
UNIT_ASSERT(file2->GetPath().Exists());
UNIT_ASSERT(file3->GetPath().Exists());
TVector<TString> filesInStorage;
storage->GetRoot().ListNames(filesInStorage);
UNIT_ASSERT_EQUAL(filesInStorage.size(), 2); // 1 file + 1 hardlink directory
auto beg = filesInStorage.begin(),
end = filesInStorage.end();
// file1 and file2 were displaced
UNIT_ASSERT(Find(beg, end, file1->GetStorageFileName()) == end);
UNIT_ASSERT(Find(beg, end, file2->GetStorageFileName()) == end);
UNIT_ASSERT(Find(beg, end, file3->GetStorageFileName()) != end);
}
Y_UNIT_TEST(PersistStorage) {
TTestDir dir("PersistStorage");
THolder<TStorage> storage = MakeHolder<TStorage>(100, 100, dir.GetFsPath());
auto rootPath = storage->GetRoot();
TFileLinkPtr fileInStorage = PutFile("test.file", *storage);
UNIT_ASSERT_EQUAL(storage->GetCount(), 1);
UNIT_ASSERT_EQUAL(storage->GetOccupiedSize(), DATA.size());
storage.Destroy();
UNIT_ASSERT(!fileInStorage->GetPath().Exists()); // hardlink was deleted
UNIT_ASSERT(rootPath.Exists());
UNIT_ASSERT((rootPath / fileInStorage->GetStorageFileName()).Exists());
storage.Reset(new TStorage(100, 100, dir.GetFsPath()));
UNIT_ASSERT_EQUAL(storage->GetCount(), 1);
UNIT_ASSERT_EQUAL(storage->GetOccupiedSize(), DATA.size());
}
}
|