#include <cstring>
#include <limits>
#include <random>
#include <seastar/core/app-template.hh>
#include <seastar/core/aligned_buffer.hh>
#include <seastar/core/file.hh>
#include <seastar/core/seastar.hh>
#include <seastar/core/sstring.hh>
#include <seastar/core/temporary_buffer.hh>
#include <seastar/core/loop.hh>
#include <seastar/core/io_intent.hh>
#include <seastar/util/log.hh>
#include <seastar/util/tmp_file.hh>
constexpr size_t aligned_size = 4096;
return f.
dma_read(0, rbuf.
get_write(), aligned_size).then([&rbuf, &wbuf] (
size_t count) {
assert(count == aligned_size);
fmt::print(" verifying {} bytes\n", count);
assert(!memcmp(rbuf.
get(), wbuf.
get(), aligned_size));
});
}
fmt::print(" retrieving data filename from {}\n", meta_filename);
assert(count == aligned_size);
auto data_filename = sstring(rbuf.get());
fmt::print(" opening {}\n", data_filename);
return open_file_dma(data_filename, open_flags::ro);
});
});
}
fmt::print("Demonstrating with_file():\n");
return tmp_dir::do_with_thread([] (
tmp_dir& t) {
auto rnd = std::mt19937(std::random_device()());
auto dist = std::uniform_int_distribution<int>(0, std::numeric_limits<char>::max());
sstring meta_filename = (t.
get_path() /
"meta_file").native();
sstring data_filename = (t.get_path() / "data_file").native();
return f.
dma_write(0, wbuf.get(), aligned_size);
}).get();
assert(count == aligned_size);
};
std::fill(wbuf.get_write(), wbuf.get_write() + aligned_size, 0);
std::copy(data_filename.cbegin(), data_filename.cend(), wbuf.get_write());
fmt::print(" writing \"{}\" into {}\n", data_filename, meta_filename);
write_to_file(meta_filename, wbuf);
fmt::print(" writing random data into {}\n", data_filename);
std::generate(wbuf.get_write(), wbuf.get_write() + aligned_size, [&dist, &rnd] { return dist(rnd); });
write_to_file(data_filename, wbuf);
fmt::print(" verifying data...\n");
with_file(open_data_file(meta_filename, rbuf), [&rbuf, &wbuf] (
file& f) {
return verify_data_file(f, rbuf, wbuf);
}).get();
});
}
future<> demo_with_file_close_on_failure() {
fmt::print("\nDemonstrating with_file_close_on_failure():\n");
return tmp_dir::do_with_thread([] (
tmp_dir& t) {
auto rnd = std::mt19937(std::random_device()());
auto dist = std::uniform_int_distribution<int>(0, std::numeric_limits<char>::max());
sstring meta_filename = (t.get_path() / "meta_file").native();
sstring data_filename = (t.get_path() / "data_file").native();
auto make_output_stream = [] (std::string_view filename) {
});
};
}).finally([&o] {
});
};
std::fill(wbuf.get_write(), wbuf.get_write() + aligned_size, 0);
std::copy(data_filename.cbegin(), data_filename.cend(), wbuf.get_write());
fmt::print(" writing \"{}\" into {}\n", data_filename, meta_filename);
write_to_stream(o, wbuf).get();
fmt::print(" writing random data into {}\n", data_filename);
std::generate(wbuf.get_write(), wbuf.get_write() + aligned_size, [&dist, &rnd] { return dist(rnd); });
o = make_output_stream(data_filename).get();
write_to_stream(o, wbuf).get();
fmt::print(" verifying data...\n");
with_file(open_data_file(meta_filename, rbuf), [&rbuf, &wbuf] (
file& f) {
return verify_data_file(f, rbuf, wbuf);
}).get();
});
}
static constexpr size_t half_aligned_size = aligned_size / 2;
fmt::print("\nDemonstrating demo_with_io_intent():\n");
return tmp_dir::do_with_thread([] (
tmp_dir& t) {
sstring filename = (t.get_path() / "testfile.tmp").native();
auto f =
open_file_dma(filename, open_flags::rw | open_flags::create).get();
auto rnd = std::mt19937(std::random_device()());
auto dist = std::uniform_int_distribution<int>(0, std::numeric_limits<char>::max());
fmt::print(" writing random data into {}\n", filename);
std::generate(wbuf.get_write(), wbuf.get_write() + aligned_size, [&dist, &rnd] { return dist(rnd); });
f.
dma_write(0, wbuf.get(), aligned_size).get();
fmt::print(" starting to overwrite {} with other random data in two steps\n", filename);
std::generate(wbuf_n.get_write(), wbuf_n.get_write() + aligned_size, [&dist, &rnd] { return dist(rnd); });
auto f1 = f.
dma_write(0, wbuf_n.get(), half_aligned_size);
auto f2 = f.
dma_write(half_aligned_size, wbuf_n.get() + half_aligned_size, half_aligned_size, &intent);
fmt::print(" cancel the 2nd overwriting\n");
fmt::print(" wait for overwriting IOs to complete\n");
f1.get();
bool cancelled = false;
try {
f2.get();
fmt::print(" 2nd write won the race with cancellation\n");
cancelled = true;
}
fmt::print(" verifying data...\n");
auto rbuf = allocate_aligned_buffer<unsigned char>(aligned_size, aligned_size);
assert(!memcmp(rbuf.
get(), wbuf_n.get(), half_aligned_size));
if (cancelled) {
assert(!memcmp(rbuf.
get() + half_aligned_size, wbuf.get() + half_aligned_size, half_aligned_size));
} else {
assert(!memcmp(rbuf.
get() + half_aligned_size, wbuf.get() + half_aligned_size, half_aligned_size));
}
});
}
int main(int ac, char** av) {
app_template app;
return app.run(ac, av, [] {
return demo_with_file().then([] {
return demo_with_file_close_on_failure().then([] {
return demo_with_io_intent();
});
});
});
}
An exception Cancelled IOs resolve their future into (see io_intent)
Definition: file.hh:691
future< size_t > dma_write(uint64_t pos, const CharType *buffer, size_t len, io_intent *intent=nullptr) noexcept
Definition: file.hh:358
future< size_t > dma_read(uint64_t aligned_pos, CharType *aligned_buffer, size_t aligned_len, io_intent *intent=nullptr) noexcept
Definition: file.hh:284
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
Definition: io_intent.hh:44
void cancel() noexcept
Definition: io_intent.hh:93
future close() noexcept
Definition: iostream-impl.hh:496
const CharType * get() const noexcept
Gets a pointer to the beginning of the buffer.
Definition: temporary_buffer.hh:125
CharType * get_write() noexcept
Definition: temporary_buffer.hh:128
Definition: tmp_file.hh:119
auto with_file_close_on_failure(future< file > file_fut, Func func) noexcept
Helper for ensuring a file is closed if func fails.
Definition: file.hh:639
future< file > open_file_dma(std::string_view name, open_flags flags) noexcept
auto with_file(future< file > file_fut, Func func) noexcept
Helper for ensuring a file is closed after func is called.
Definition: file.hh:612
future do_for_each(Iterator begin, Sentinel end, AsyncAction action) noexcept
Call a function for each item in a range, sequentially (iterator version).
Definition: loop.hh:465
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
future< output_stream< char > > make_file_output_stream(file file, uint64_t buffer_size=8192) noexcept