-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_object_store.rs
302 lines (236 loc) · 10.1 KB
/
test_object_store.rs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#[cfg(feature = "integration-test")]
mod test {
use bytes::{Buf, BufMut, BytesMut};
use hdfs_native::{
minidfs::{DfsFeatures, MiniDfs},
Client, WriteOptions,
};
use hdfs_native_object_store::{HdfsErrorConvert, HdfsObjectStore};
use object_store::{PutMode, PutOptions, PutPayload};
use serial_test::serial;
use std::{collections::HashSet, sync::Arc};
pub const TEST_FILE_INTS: usize = 64 * 1024 * 1024;
#[tokio::test]
#[serial]
async fn test_object_store() -> object_store::Result<()> {
let dfs = MiniDfs::with_features(&HashSet::from([DfsFeatures::HA]));
let client = Client::new(&dfs.url).to_object_store_err()?;
// Create a test file with the client directly to sanity check reads and lists
let mut file = client
.create("/testfile", WriteOptions::default())
.await
.unwrap();
let mut buf = BytesMut::new();
for i in 0..TEST_FILE_INTS as i32 {
buf.put_i32(i);
}
file.write(buf.freeze()).await.unwrap();
file.close().await.unwrap();
client.mkdirs("/testdir", 0o755, true).await.unwrap();
let store = HdfsObjectStore::new(Arc::new(client));
test_object_store_head(&store).await?;
test_object_store_list(&store).await?;
test_object_store_rename(&store).await?;
test_object_store_read(&store).await?;
test_object_store_write(&store).await?;
test_object_store_write_multipart(&store).await?;
test_object_store_copy(&store).await?;
Ok(())
}
async fn test_object_store_head(store: &HdfsObjectStore) -> object_store::Result<()> {
use object_store::{path::Path, ObjectStore};
let head = store.head(&Path::from("/testfile")).await?;
assert_eq!(head.location, Path::from("/testfile"));
assert_eq!(head.size, TEST_FILE_INTS * 4);
assert!(store.head(&Path::from("/testfile2")).await.is_err());
assert!(store.head(&Path::from("/testdir")).await.is_err());
Ok(())
}
async fn test_object_store_list(store: &HdfsObjectStore) -> object_store::Result<()> {
use futures::StreamExt;
use object_store::{path::Path, ObjectMeta, ObjectStore};
let list: Vec<object_store::Result<ObjectMeta>> =
store.list(Some(&Path::from("/"))).collect().await;
assert_eq!(list.len(), 1);
assert_eq!(list[0].as_ref().unwrap().location, Path::from("/testfile"));
// Listing of a prefix that doesn't exist should return an empty result, not an error
assert_eq!(
store.list(Some(&Path::from("/doesnt/exist"))).count().await,
0
);
let list = store
.list_with_delimiter(Some(&Path::from("/doesnt/exist")))
.await?;
assert_eq!(list.common_prefixes.len(), 0);
assert_eq!(list.objects.len(), 0);
Ok(())
}
async fn test_object_store_rename(store: &HdfsObjectStore) -> object_store::Result<()> {
use object_store::{path::Path, ObjectStore};
store
.rename(&Path::from("/testfile"), &Path::from("/testfile2"))
.await?;
assert!(store.head(&Path::from("/testfile2")).await.is_ok());
assert!(store.head(&Path::from("/testfile")).await.is_err());
store
.rename(&Path::from("/testfile2"), &Path::from("/testfile"))
.await?;
assert!(store.head(&Path::from("/testfile")).await.is_ok());
assert!(store.head(&Path::from("/testfile2")).await.is_err());
Ok(())
}
async fn test_object_store_read(store: &HdfsObjectStore) -> object_store::Result<()> {
use object_store::{path::Path, ObjectStore};
let location = Path::from("/testfile");
let mut buf = store.get(&location).await?.bytes().await?;
for i in 0..TEST_FILE_INTS as i32 {
assert_eq!(buf.get_i32(), i);
}
// Read a single integer from the file
let offset = TEST_FILE_INTS / 2 * 4;
let mut buf = store.get_range(&location, offset..(offset + 4)).await?;
assert_eq!(buf.get_i32(), TEST_FILE_INTS as i32 / 2);
Ok(())
}
async fn test_object_store_write(store: &HdfsObjectStore) -> object_store::Result<()> {
use object_store::{path::Path, ObjectStore};
store
.put(&Path::from("/newfile"), PutPayload::new())
.await?;
store.head(&Path::from("/newfile")).await?;
// PutMode = Create should fail for existing file
match store
.put_opts(
&Path::from("/newfile"),
PutPayload::new(),
PutOptions {
mode: PutMode::Create,
..Default::default()
},
)
.await
{
Err(object_store::Error::AlreadyExists { .. }) => (),
Err(e) => panic!("Wrong error was thrown for put without overewrite: {:?}", e),
Ok(_) => panic!("No error was thrown for put without overwrite for existing file"),
}
// Check a small files, a file that is exactly one block, and a file slightly bigger than a block
for size_to_check in [16i32, 128 * 1024 * 1024, 130 * 1024 * 1024] {
let ints_to_write = size_to_check / 4;
let mut data = BytesMut::with_capacity(size_to_check as usize);
for i in 0..ints_to_write {
data.put_i32(i);
}
let buf = data.freeze();
store
.put(&Path::from("/newfile"), PutPayload::from_bytes(buf.clone()))
.await?;
assert_eq!(
store.head(&Path::from("/newfile")).await?.size,
size_to_check as usize
);
let read_data = store.get(&Path::from("/newfile")).await?.bytes().await?;
assert_eq!(buf.len(), read_data.len());
for pos in 0..buf.len() {
assert_eq!(
buf[pos], read_data[pos],
"data is different as position {} for size {}",
pos, size_to_check
);
}
}
store.delete(&Path::from("/newfile")).await?;
Ok(())
}
async fn test_object_store_write_multipart(
store: &HdfsObjectStore,
) -> object_store::Result<()> {
use object_store::{path::Path, ObjectStore};
let mut uploader = store.put_multipart(&"/newfile".into()).await?;
uploader.complete().await?;
store.head(&Path::from("/newfile")).await?;
// Check a small files, a file that is exactly one block, and a file slightly bigger than a block
for size_to_check in [16i32, 128 * 1024 * 1024, 130 * 1024 * 1024] {
let ints_to_write = size_to_check / 4;
let mut uploader = store.put_multipart(&"/newfile".into()).await?;
let mut data = BytesMut::with_capacity(size_to_check as usize);
for i in 0..ints_to_write {
data.put_i32(i);
}
// Write in 10 MiB chunks
let mut buf = data.freeze();
while !buf.is_empty() {
let to_write = usize::min(buf.len(), 10 * 1024 * 1024);
uploader.put_part(buf.split_to(to_write).into()).await?;
}
uploader.complete().await?;
assert_eq!(
store.head(&Path::from("/newfile")).await?.size,
size_to_check as usize
);
let mut read_data = store.get(&Path::from("/newfile")).await?.bytes().await?;
assert_eq!(size_to_check as usize, read_data.len());
for pos in 0..ints_to_write {
assert_eq!(
pos,
read_data.get_i32(),
"data is different at integer {} for size {}",
pos,
size_to_check
);
}
}
store.delete(&Path::from("/newfile")).await?;
// Test aborting
let mut uploader = store.put_multipart(&"/newfile".into()).await?;
assert!(store.head(&"/.newfile.tmp.1".into()).await.is_ok());
uploader.abort().await?;
assert!(store.head(&"/.newfile.tmp.1".into()).await.is_err());
assert!(store.head(&"/newfile".into()).await.is_err());
// Test multiple uploads to the same destination, default is to overwrite
let mut uploader1 = store.put_multipart(&"/newfile".into()).await?;
let mut uploader2 = store.put_multipart(&"/newfile".into()).await?;
uploader1.put_part(vec![1].into()).await?;
uploader2.put_part(vec![2].into()).await?;
uploader1.complete().await?;
uploader2.complete().await?;
let result = store.get(&"/newfile".into()).await?;
assert!(result.bytes().await?.to_vec() == vec![2]);
Ok(())
}
async fn test_object_store_copy(store: &HdfsObjectStore) -> object_store::Result<()> {
use object_store::{path::Path, ObjectStore};
store
.put(&Path::from("/newfile"), PutPayload::new())
.await?;
let size_to_check = 128 * 1024 * 1024;
let ints_to_write = size_to_check / 4;
let mut data = BytesMut::with_capacity(size_to_check as usize);
for i in 0..ints_to_write {
data.put_i32(i);
}
let buf = data.freeze();
store
.put(&Path::from("/newfile"), buf.clone().into())
.await?;
store
.copy(&Path::from("/newfile"), &Path::from("/newfile2"))
.await?;
let read_data = store.get(&Path::from("/newfile2")).await?.bytes().await?;
assert_eq!(buf.len(), read_data.len());
for pos in 0..buf.len() {
assert_eq!(
buf[pos], read_data[pos],
"data is different as position {} for size {}",
pos, size_to_check
);
}
assert!(store
.copy_if_not_exists(&Path::from("/newfile"), &Path::from("/newfile2"))
.await
.is_err());
store.delete(&Path::from("/newfile")).await?;
store.delete(&Path::from("/newfile2")).await?;
Ok(())
}
}