repo
stringlengths 1
152
⌀ | file
stringlengths 15
205
| code
stringlengths 0
41.6M
| file_length
int64 0
41.6M
| avg_line_length
float64 0
1.81M
| max_line_length
int64 0
12.7M
| extension_type
stringclasses 90
values |
---|---|---|---|---|---|---|
null |
ceph-main/src/test/librados/test_shared.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*
// vim: ts=8 sw=2 smarttab
#pragma once
#include <unistd.h>
#include <chrono>
#include <map>
#include <string>
#include <thread>
#include "include/buffer_fwd.h"
// helpers shared by librados tests
std::string get_temp_pool_name(const std::string &prefix = "test-rados-api-");
void assert_eq_sparse(ceph::bufferlist& expected,
const std::map<uint64_t, uint64_t>& extents,
ceph::bufferlist& actual);
class TestAlarm
{
public:
#ifndef _WIN32
TestAlarm() {
alarm(2400);
}
~TestAlarm() {
alarm(0);
}
#else
// TODO: add a timeout mechanism for Windows as well, possibly by using
// CreateTimerQueueTimer.
TestAlarm() {
}
~TestAlarm() {
}
#endif
};
template<class Rep, class Period, typename Func, typename... Args,
typename Return = std::result_of_t<Func&&(Args&&...)>>
Return wait_until(const std::chrono::duration<Rep, Period>& rel_time,
const std::chrono::duration<Rep, Period>& step,
const Return& expected,
Func&& func, Args&&... args)
{
std::this_thread::sleep_for(rel_time - step);
for (auto& s : {step, step}) {
if (!s.count()) {
break;
}
auto ret = func(std::forward<Args>(args)...);
if (ret == expected) {
return ret;
}
std::this_thread::sleep_for(s);
}
return func(std::forward<Args>(args)...);
}
| 1,456 | 23.694915 | 78 |
h
|
null |
ceph-main/src/test/librados/testcase_cxx.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "testcase_cxx.h"
#include <errno.h>
#include <fmt/format.h>
#include "test_cxx.h"
#include "test_shared.h"
#include "crimson_utils.h"
#include "include/scope_guard.h"
using namespace librados;
namespace {
void init_rand() {
static bool seeded = false;
if (!seeded) {
seeded = true;
int seed = getpid();
std::cout << "seed " << seed << std::endl;
srand(seed);
}
}
} // anonymous namespace
std::string RadosTestPPNS::pool_name;
Rados RadosTestPPNS::s_cluster;
void RadosTestPPNS::SetUpTestCase()
{
auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
pool_name = get_temp_pool_name(pool_prefix);
ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
}
void RadosTestPPNS::TearDownTestCase()
{
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
}
void RadosTestPPNS::SetUp()
{
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
bool req;
ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
ASSERT_FALSE(req);
}
void RadosTestPPNS::TearDown()
{
if (cleanup)
cleanup_all_objects(ioctx);
ioctx.close();
}
void RadosTestPPNS::cleanup_all_objects(librados::IoCtx ioctx)
{
// remove all objects to avoid polluting other tests
ioctx.snap_set_read(librados::SNAP_HEAD);
ioctx.set_namespace(all_nspaces);
for (NObjectIterator it = ioctx.nobjects_begin();
it != ioctx.nobjects_end(); ++it) {
ioctx.locator_set_key(it->get_locator());
ioctx.set_namespace(it->get_nspace());
ASSERT_EQ(0, ioctx.remove(it->get_oid()));
}
}
std::string RadosTestParamPPNS::pool_name;
std::string RadosTestParamPPNS::cache_pool_name;
Rados RadosTestParamPPNS::s_cluster;
void RadosTestParamPPNS::SetUpTestCase()
{
auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
pool_name = get_temp_pool_name(pool_prefix);
ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
}
void RadosTestParamPPNS::TearDownTestCase()
{
if (cache_pool_name.length()) {
// tear down tiers
bufferlist inbl;
ASSERT_EQ(0, s_cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, s_cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, s_cluster.mon_command(
"{\"prefix\": \"osd pool delete\", \"pool\": \"" + cache_pool_name +
"\", \"pool2\": \"" + cache_pool_name + "\", \"yes_i_really_really_mean_it\": true}",
inbl, NULL, NULL));
cache_pool_name = "";
}
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
}
void RadosTestParamPPNS::SetUp()
{
if (!is_crimson_cluster() && strcmp(GetParam(), "cache") == 0 &&
cache_pool_name.empty()) {
auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
cache_pool_name = get_temp_pool_name();
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd pool create\", \"pool\": \"" + cache_pool_name +
"\", \"pg_num\": 4}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
}
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
bool req;
ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
ASSERT_FALSE(req);
}
void RadosTestParamPPNS::TearDown()
{
if (cleanup)
cleanup_all_objects(ioctx);
ioctx.close();
}
void RadosTestParamPPNS::cleanup_all_objects(librados::IoCtx ioctx)
{
// remove all objects to avoid polluting other tests
ioctx.snap_set_read(librados::SNAP_HEAD);
ioctx.set_namespace(all_nspaces);
for (NObjectIterator it = ioctx.nobjects_begin();
it != ioctx.nobjects_end(); ++it) {
ioctx.locator_set_key(it->get_locator());
ioctx.set_namespace(it->get_nspace());
ASSERT_EQ(0, ioctx.remove(it->get_oid()));
}
}
std::string RadosTestECPPNS::pool_name;
Rados RadosTestECPPNS::s_cluster;
void RadosTestECPPNS::SetUpTestCase()
{
auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
pool_name = get_temp_pool_name(pool_prefix);
ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
}
void RadosTestECPPNS::TearDownTestCase()
{
ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
}
void RadosTestECPPNS::SetUp()
{
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
bool req;
ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
ASSERT_TRUE(req);
ASSERT_EQ(0, ioctx.pool_required_alignment2(&alignment));
ASSERT_NE(0U, alignment);
}
void RadosTestECPPNS::TearDown()
{
if (cleanup)
cleanup_all_objects(ioctx);
ioctx.close();
}
std::string RadosTestPP::pool_name;
Rados RadosTestPP::s_cluster;
void RadosTestPP::SetUpTestCase()
{
init_rand();
auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
pool_name = get_temp_pool_name(pool_prefix);
ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
}
void RadosTestPP::TearDownTestCase()
{
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
}
void RadosTestPP::SetUp()
{
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
nspace = get_temp_pool_name();
ioctx.set_namespace(nspace);
bool req;
ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
ASSERT_FALSE(req);
}
void RadosTestPP::TearDown()
{
if (cleanup) {
cleanup_default_namespace(ioctx);
cleanup_namespace(ioctx, nspace);
}
ioctx.close();
}
void RadosTestPP::cleanup_default_namespace(librados::IoCtx ioctx)
{
// remove all objects from the default namespace to avoid polluting
// other tests
cleanup_namespace(ioctx, "");
}
void RadosTestPP::cleanup_namespace(librados::IoCtx ioctx, std::string ns)
{
ioctx.snap_set_read(librados::SNAP_HEAD);
ioctx.set_namespace(ns);
int tries = 20;
while (--tries) {
int got_enoent = 0;
for (NObjectIterator it = ioctx.nobjects_begin();
it != ioctx.nobjects_end(); ++it) {
ioctx.locator_set_key(it->get_locator());
ObjectWriteOperation op;
op.remove();
librados::AioCompletion *completion = s_cluster.aio_create_completion();
auto sg = make_scope_guard([&] { completion->release(); });
ASSERT_EQ(0, ioctx.aio_operate(it->get_oid(), completion, &op,
librados::OPERATION_IGNORE_CACHE));
completion->wait_for_complete();
if (completion->get_return_value() == -ENOENT) {
++got_enoent;
std::cout << " got ENOENT removing " << it->get_oid()
<< " in ns " << ns << std::endl;
} else {
ASSERT_EQ(0, completion->get_return_value());
}
}
if (!got_enoent) {
break;
}
std::cout << " got ENOENT on " << got_enoent
<< " objects, waiting a bit for snap"
<< " trimming before retrying " << tries << " more times..."
<< std::endl;
sleep(1);
}
if (tries == 0) {
std::cout << "failed to clean up; probably need to scrub purged_snaps."
<< std::endl;
}
}
std::string RadosTestParamPP::pool_name;
std::string RadosTestParamPP::cache_pool_name;
Rados RadosTestParamPP::s_cluster;
void RadosTestParamPP::SetUpTestCase()
{
auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
pool_name = get_temp_pool_name(pool_prefix);
ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
}
void RadosTestParamPP::TearDownTestCase()
{
if (cache_pool_name.length()) {
// tear down tiers
bufferlist inbl;
ASSERT_EQ(0, s_cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, s_cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, s_cluster.mon_command(
"{\"prefix\": \"osd pool delete\", \"pool\": \"" + cache_pool_name +
"\", \"pool2\": \"" + cache_pool_name + "\", \"yes_i_really_really_mean_it\": true}",
inbl, NULL, NULL));
cache_pool_name = "";
}
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
}
void RadosTestParamPP::SetUp()
{
if (!is_crimson_cluster() && strcmp(GetParam(), "cache") == 0 &&
cache_pool_name.empty()) {
auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
cache_pool_name = get_temp_pool_name();
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd pool create\", \"pool\": \"" + cache_pool_name +
"\", \"pg_num\": 4}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
}
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
nspace = get_temp_pool_name();
ioctx.set_namespace(nspace);
bool req;
ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
ASSERT_FALSE(req);
}
void RadosTestParamPP::TearDown()
{
if (cleanup) {
cleanup_default_namespace(ioctx);
cleanup_namespace(ioctx, nspace);
}
ioctx.close();
}
void RadosTestParamPP::cleanup_default_namespace(librados::IoCtx ioctx)
{
// remove all objects from the default namespace to avoid polluting
// other tests
cleanup_namespace(ioctx, "");
}
void RadosTestParamPP::cleanup_namespace(librados::IoCtx ioctx, std::string ns)
{
ioctx.snap_set_read(librados::SNAP_HEAD);
ioctx.set_namespace(ns);
for (NObjectIterator it = ioctx.nobjects_begin();
it != ioctx.nobjects_end(); ++it) {
ioctx.locator_set_key(it->get_locator());
ASSERT_EQ(0, ioctx.remove(it->get_oid()));
}
}
std::string RadosTestECPP::pool_name;
Rados RadosTestECPP::s_cluster;
void RadosTestECPP::SetUpTestCase()
{
SKIP_IF_CRIMSON();
auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
pool_name = get_temp_pool_name(pool_prefix);
ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
}
void RadosTestECPP::TearDownTestCase()
{
SKIP_IF_CRIMSON();
ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
}
void RadosTestECPP::SetUp()
{
SKIP_IF_CRIMSON();
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
nspace = get_temp_pool_name();
ioctx.set_namespace(nspace);
bool req;
ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
ASSERT_TRUE(req);
ASSERT_EQ(0, ioctx.pool_required_alignment2(&alignment));
ASSERT_NE(0U, alignment);
}
void RadosTestECPP::TearDown()
{
SKIP_IF_CRIMSON();
if (cleanup) {
cleanup_default_namespace(ioctx);
cleanup_namespace(ioctx, nspace);
}
ioctx.close();
}
| 12,049 | 28.534314 | 107 |
cc
|
null |
ceph-main/src/test/librados/testcase_cxx.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#pragma once
#include "gtest/gtest.h"
#include "include/rados/librados.hpp"
class RadosTestPPNS : public ::testing::Test {
public:
RadosTestPPNS(bool c=false) : cluster(s_cluster), cleanup(c) {}
~RadosTestPPNS() override {}
protected:
static void SetUpTestCase();
static void TearDownTestCase();
static void cleanup_all_objects(librados::IoCtx ioctx);
static librados::Rados s_cluster;
static std::string pool_name;
void SetUp() override;
void TearDown() override;
librados::Rados &cluster;
librados::IoCtx ioctx;
bool cleanup;
};
struct RadosTestPPNSCleanup : public RadosTestPPNS {
RadosTestPPNSCleanup() : RadosTestPPNS(true) {}
};
class RadosTestParamPPNS : public ::testing::TestWithParam<const char*> {
public:
RadosTestParamPPNS(bool c=false) : cluster(s_cluster), cleanup(c) {}
~RadosTestParamPPNS() override {}
static void SetUpTestCase();
static void TearDownTestCase();
protected:
static void cleanup_all_objects(librados::IoCtx ioctx);
static librados::Rados s_cluster;
static std::string pool_name;
static std::string cache_pool_name;
void SetUp() override;
void TearDown() override;
librados::Rados &cluster;
librados::IoCtx ioctx;
bool cleanup;
};
class RadosTestECPPNS : public RadosTestPPNS {
public:
RadosTestECPPNS(bool c=false) : cluster(s_cluster), cleanup(c) {}
~RadosTestECPPNS() override {}
protected:
static void SetUpTestCase();
static void TearDownTestCase();
static librados::Rados s_cluster;
static std::string pool_name;
void SetUp() override;
void TearDown() override;
librados::Rados &cluster;
librados::IoCtx ioctx;
uint64_t alignment = 0;
bool cleanup;
};
struct RadosTestECPPNSCleanup : public RadosTestECPPNS {
RadosTestECPPNSCleanup() : RadosTestECPPNS(true) {}
};
class RadosTestPP : public ::testing::Test {
public:
RadosTestPP(bool c=false) : cluster(s_cluster), cleanup(c) {}
~RadosTestPP() override {}
protected:
static void SetUpTestCase();
static void TearDownTestCase();
static void cleanup_default_namespace(librados::IoCtx ioctx);
static void cleanup_namespace(librados::IoCtx ioctx, std::string ns);
static librados::Rados s_cluster;
static std::string pool_name;
void SetUp() override;
void TearDown() override;
librados::Rados &cluster;
librados::IoCtx ioctx;
bool cleanup;
std::string nspace;
};
class RadosTestParamPP : public ::testing::TestWithParam<const char*> {
public:
RadosTestParamPP(bool c=false) : cluster(s_cluster), cleanup(c) {}
~RadosTestParamPP() override {}
static void SetUpTestCase();
static void TearDownTestCase();
protected:
static void cleanup_default_namespace(librados::IoCtx ioctx);
static void cleanup_namespace(librados::IoCtx ioctx, std::string ns);
static librados::Rados s_cluster;
static std::string pool_name;
static std::string cache_pool_name;
void SetUp() override;
void TearDown() override;
librados::Rados &cluster;
librados::IoCtx ioctx;
bool cleanup;
std::string nspace;
};
class RadosTestECPP : public RadosTestPP {
public:
RadosTestECPP(bool c=false) : cluster(s_cluster), cleanup(c) {}
~RadosTestECPP() override {}
protected:
static void SetUpTestCase();
static void TearDownTestCase();
static librados::Rados s_cluster;
static std::string pool_name;
void SetUp() override;
void TearDown() override;
librados::Rados &cluster;
librados::IoCtx ioctx;
bool cleanup;
std::string nspace;
uint64_t alignment = 0;
};
| 3,580 | 26.335878 | 73 |
h
|
null |
ceph-main/src/test/librados/tier_cxx.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "gtest/gtest.h"
#include "mds/mdstypes.h"
#include "include/buffer.h"
#include "include/rbd_types.h"
#include "include/rados/librados.hpp"
#include "include/stringify.h"
#include "include/types.h"
#include "global/global_context.h"
#include "common/Cond.h"
#include "common/ceph_crypto.h"
#include "test/librados/test_cxx.h"
#include "test/librados/testcase_cxx.h"
#include "json_spirit/json_spirit.h"
#include "cls/cas/cls_cas_ops.h"
#include "cls/cas/cls_cas_internal.h"
#include "osd/HitSet.h"
#include <errno.h>
#include <map>
#include <sstream>
#include <string>
#include "cls/cas/cls_cas_client.h"
#include "cls/cas/cls_cas_internal.h"
#include "crimson_utils.h"
using namespace std;
using namespace librados;
using ceph::crypto::SHA1;
typedef RadosTestPP LibRadosTierPP;
typedef RadosTestECPP LibRadosTierECPP;
void flush_evict_all(librados::Rados& cluster, librados::IoCtx& cache_ioctx)
{
bufferlist inbl;
cache_ioctx.set_namespace(all_nspaces);
for (NObjectIterator it = cache_ioctx.nobjects_begin();
it != cache_ioctx.nobjects_end(); ++it) {
cache_ioctx.locator_set_key(it->get_locator());
cache_ioctx.set_namespace(it->get_nspace());
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
cache_ioctx.aio_operate(
it->get_oid(), completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL);
completion->wait_for_complete();
completion->get_return_value();
completion->release();
}
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
cache_ioctx.aio_operate(
it->get_oid(), completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL);
completion->wait_for_complete();
completion->get_return_value();
completion->release();
}
}
}
static string _get_required_osd_release(Rados& cluster)
{
bufferlist inbl;
string cmd = string("{\"prefix\": \"osd dump\",\"format\":\"json\"}");
bufferlist outbl;
int r = cluster.mon_command(cmd, inbl, &outbl, NULL);
ceph_assert(r >= 0);
string outstr(outbl.c_str(), outbl.length());
json_spirit::Value v;
if (!json_spirit::read(outstr, v)) {
cerr <<" unable to parse json " << outstr << std::endl;
return "";
}
json_spirit::Object& o = v.get_obj();
for (json_spirit::Object::size_type i=0; i<o.size(); i++) {
json_spirit::Pair& p = o[i];
if (p.name_ == "require_osd_release") {
cout << "require_osd_release = " << p.value_.get_str() << std::endl;
return p.value_.get_str();
}
}
cerr << "didn't find require_osd_release in " << outstr << std::endl;
return "";
}
void manifest_set_chunk(Rados& cluster, librados::IoCtx& src_ioctx,
librados::IoCtx& tgt_ioctx,
uint64_t src_offset, uint64_t length,
std::string src_oid, std::string tgt_oid)
{
ObjectReadOperation op;
op.set_chunk(src_offset, length, src_ioctx, src_oid, 0,
CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, tgt_ioctx.aio_operate(tgt_oid, completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
static inline void buf_to_hex(const unsigned char *buf, int len, char *str)
{
int i;
str[0] = '\0';
for (i = 0; i < len; i++) {
sprintf(&str[i*2], "%02x", (int)buf[i]);
}
}
void check_fp_oid_refcount(librados::IoCtx& ioctx, std::string foid, uint64_t count,
std::string fp_algo = NULL)
{
bufferlist t;
int size = foid.length();
if (fp_algo == "sha1") {
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
SHA1 sha1_gen;
sha1_gen.Update((const unsigned char *)foid.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
} else if (fp_algo.empty()) {
ioctx.getxattr(foid, CHUNK_REFCOUNT_ATTR, t);
} else if (!fp_algo.empty()) {
ceph_assert(0 == "unrecognized fingerprint algorithm");
}
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(count, refs.count());
}
string get_fp_oid(string oid, std::string fp_algo = NULL)
{
if (fp_algo == "sha1") {
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
SHA1 sha1_gen;
int size = oid.length();
sha1_gen.Update((const unsigned char *)oid.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
return string(p_str);
}
return string();
}
void is_intended_refcount_state(librados::IoCtx& src_ioctx,
std::string src_oid,
librados::IoCtx& dst_ioctx,
std::string dst_oid,
int expected_refcount)
{
int src_refcount = 0, dst_refcount = 0;
bufferlist t;
int r = dst_ioctx.getxattr(dst_oid, CHUNK_REFCOUNT_ATTR, t);
if (r == -ENOENT) {
dst_refcount = 0;
} else {
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ceph_assert(0);
}
dst_refcount = refs.count();
}
int tries = 0;
for (; tries < 30; ++tries) {
r = cls_cas_references_chunk(src_ioctx, src_oid, dst_oid);
if (r == -ENOENT || r == -ENOLINK) {
src_refcount = 0;
} else if (r == -EBUSY) {
sleep(20);
continue;
} else {
src_refcount = r;
}
break;
}
ASSERT_TRUE(tries < 30);
ASSERT_TRUE(src_refcount >= 0);
ASSERT_TRUE(src_refcount == expected_refcount);
ASSERT_TRUE(src_refcount <= dst_refcount);
}
class LibRadosTwoPoolsPP : public RadosTestPP
{
public:
LibRadosTwoPoolsPP() {};
~LibRadosTwoPoolsPP() override {};
protected:
static void SetUpTestCase() {
pool_name = get_temp_pool_name();
ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
}
static void TearDownTestCase() {
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
}
static std::string cache_pool_name;
void SetUp() override {
SKIP_IF_CRIMSON();
cache_pool_name = get_temp_pool_name();
ASSERT_EQ(0, s_cluster.pool_create(cache_pool_name.c_str()));
RadosTestPP::SetUp();
ASSERT_EQ(0, cluster.ioctx_create(cache_pool_name.c_str(), cache_ioctx));
cache_ioctx.application_enable("rados", true);
cache_ioctx.set_namespace(nspace);
}
void TearDown() override {
SKIP_IF_CRIMSON();
// flush + evict cache
flush_evict_all(cluster, cache_ioctx);
bufferlist inbl;
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
RadosTestPP::TearDown();
cleanup_default_namespace(cache_ioctx);
cleanup_namespace(cache_ioctx, nspace);
cache_ioctx.close();
ASSERT_EQ(0, s_cluster.pool_delete(cache_pool_name.c_str()));
}
librados::IoCtx cache_ioctx;
};
class Completions
{
public:
Completions() = default;
librados::AioCompletion* getCompletion() {
librados::AioCompletion* comp = librados::Rados::aio_create_completion();
m_completions.push_back(comp);
return comp;
}
~Completions() {
for (auto& comp : m_completions) {
comp->release();
}
}
private:
vector<librados::AioCompletion *> m_completions;
};
Completions completions;
std::string LibRadosTwoPoolsPP::cache_pool_name;
TEST_F(LibRadosTierPP, Dirty) {
SKIP_IF_CRIMSON();
{
ObjectWriteOperation op;
op.undirty();
ASSERT_EQ(0, ioctx.operate("foo", &op)); // still get 0 if it dne
}
{
ObjectWriteOperation op;
op.create(true);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, ioctx.operate("foo", &op, NULL));
ASSERT_TRUE(dirty);
ASSERT_EQ(0, r);
}
{
ObjectWriteOperation op;
op.undirty();
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
ObjectWriteOperation op;
op.undirty();
ASSERT_EQ(0, ioctx.operate("foo", &op)); // still 0 if already clean
}
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, ioctx.operate("foo", &op, NULL));
ASSERT_FALSE(dirty);
ASSERT_EQ(0, r);
}
{
ObjectWriteOperation op;
op.truncate(0); // still a write even tho it is a no-op
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, ioctx.operate("foo", &op, NULL));
ASSERT_TRUE(dirty);
ASSERT_EQ(0, r);
}
}
TEST_F(LibRadosTwoPoolsPP, Overlay) {
SKIP_IF_CRIMSON();
// create objects
{
bufferlist bl;
bl.append("base");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("cache");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// by default, the overlay sends us to cache pool
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// unless we say otherwise
{
bufferlist bl;
ObjectReadOperation op;
op.read(0, 1, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
ASSERT_EQ('b', bl[0]);
}
}
TEST_F(LibRadosTwoPoolsPP, Promote) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
}
// read, trigger a whiteout
{
bufferlist bl;
ASSERT_EQ(-ENOENT, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ(-ENOENT, ioctx.read("bar", bl, 1, 0));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
}
TEST_F(LibRadosTwoPoolsPP, PromoteSnap) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
ObjectWriteOperation op;
op.remove();
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote on the head
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bam", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
ioctx.snap_set_read(my_snaps[0]);
// read foo snap
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// read bar snap
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// read baz snap
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("baz", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
ioctx.snap_set_read(librados::SNAP_HEAD);
// read foo
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// read bar
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// read baz
{
bufferlist bl;
ASSERT_EQ(-ENOENT, ioctx.read("baz", bl, 1, 0));
}
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
TEST_F(LibRadosTwoPoolsPP, PromoteSnapScrub) {
SKIP_IF_CRIMSON();
int num = 100;
// create objects
for (int i=0; i<num; ++i) {
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate(string("foo") + stringify(i), &op));
}
vector<uint64_t> my_snaps;
for (int snap=0; snap<4; ++snap) {
// create a snapshot, clone
vector<uint64_t> ns(1);
ns.insert(ns.end(), my_snaps.begin(), my_snaps.end());
my_snaps.swap(ns);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
cout << "my_snaps " << my_snaps << std::endl;
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
for (int i=0; i<num; ++i) {
bufferlist bl;
bl.append(string("ciao! snap") + stringify(snap));
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate(string("foo") + stringify(i), &op));
}
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote on _some_ heads to make sure we handle cases
// where snaps are present and where they are not.
cout << "promoting some heads" << std::endl;
for (int i=0; i<num; ++i) {
if (i % 5 == 0 || i > num - 3) {
bufferlist bl;
ASSERT_EQ(1, ioctx.read(string("foo") + stringify(i), bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
}
for (unsigned snap = 0; snap < my_snaps.size(); ++snap) {
cout << "promoting from clones for snap " << my_snaps[snap] << std::endl;
ioctx.snap_set_read(my_snaps[snap]);
// read some snaps, semi-randomly
for (int i=0; i<50; ++i) {
bufferlist bl;
string o = string("foo") + stringify((snap * i * 137) % 80);
//cout << o << std::endl;
ASSERT_EQ(1, ioctx.read(o, bl, 1, 0));
}
}
// ok, stop and scrub this pool (to make sure scrub can handle
// missing clones in the cache tier).
{
IoCtx cache_ioctx;
ASSERT_EQ(0, cluster.ioctx_create(cache_pool_name.c_str(), cache_ioctx));
for (int i=0; i<10; ++i) {
do {
ostringstream ss;
ss << "{\"prefix\": \"pg scrub\", \"pgid\": \""
<< cache_ioctx.get_id() << "." << i
<< "\"}";
int r = cluster.mon_command(ss.str(), inbl, NULL, NULL);
if (r == -ENOENT || // in case mgr osdmap is stale
r == -EAGAIN) {
sleep(5);
continue;
}
} while (false);
}
// give it a few seconds to go. this is sloppy but is usually enough time
cout << "waiting for scrubs..." << std::endl;
sleep(30);
cout << "done waiting" << std::endl;
}
ioctx.snap_set_read(librados::SNAP_HEAD);
//cleanup
for (unsigned snap = 0; snap < my_snaps.size(); ++snap) {
ioctx.selfmanaged_snap_remove(my_snaps[snap]);
}
}
TEST_F(LibRadosTwoPoolsPP, PromoteSnapTrimRace) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// delete the snap
ASSERT_EQ(0, ioctx.selfmanaged_snap_remove(my_snaps[0]));
ioctx.snap_set_read(my_snaps[0]);
// read foo snap. the OSD may or may not realize that this snap has
// been logically deleted; either response is valid.
{
bufferlist bl;
int r = ioctx.read("foo", bl, 1, 0);
ASSERT_TRUE(r == 1 || r == -ENOENT);
}
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
TEST_F(LibRadosTwoPoolsPP, Whiteout) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create some whiteouts, verify they behave
{
ObjectWriteOperation op;
op.assert_exists();
op.remove();
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
ObjectWriteOperation op;
op.assert_exists();
op.remove();
ASSERT_EQ(-ENOENT, ioctx.operate("bar", &op));
}
{
ObjectWriteOperation op;
op.assert_exists();
op.remove();
ASSERT_EQ(-ENOENT, ioctx.operate("bar", &op));
}
// verify the whiteouts are there in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// delete a whiteout and verify it goes away
ASSERT_EQ(-ENOENT, ioctx.remove("foo"));
{
ObjectWriteOperation op;
op.remove();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("bar", completion, &op,
librados::OPERATION_IGNORE_CACHE));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// recreate an object and verify we can read it
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
}
TEST_F(LibRadosTwoPoolsPP, WhiteoutDeleteCreate) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create an object
{
bufferlist bl;
bl.append("foo");
ASSERT_EQ(0, ioctx.write_full("foo", bl));
}
// do delete + create operation
{
ObjectWriteOperation op;
op.remove();
bufferlist bl;
bl.append("bar");
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// verify it still "exists" (w/ new content)
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('b', bl[0]);
}
}
TEST_F(LibRadosTwoPoolsPP, Evict) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
}
// read, trigger a whiteout, and a dirty object
{
bufferlist bl;
ASSERT_EQ(-ENOENT, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ(-ENOENT, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ(0, ioctx.write("bar", bl, bl.length(), 0));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// pin
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// evict the pinned object with -EPERM
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE,
NULL));
completion->wait_for_complete();
ASSERT_EQ(-EPERM, completion->get_return_value());
completion->release();
}
// unpin
{
ObjectWriteOperation op;
op.cache_unpin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify clean
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_FALSE(dirty);
ASSERT_EQ(0, r);
}
// evict
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE,
NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
}
TEST_F(LibRadosTwoPoolsPP, EvictSnap) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
ObjectWriteOperation op;
op.remove();
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote on the head
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bam", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// evict bam
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"bam", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"bam", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-ENOENT, completion->get_return_value());
completion->release();
}
// read foo snap
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// evict foo snap
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// snap is gone...
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-ENOENT, completion->get_return_value());
completion->release();
}
// head is still there...
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// promote head + snap of bar
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// evict bar head (fail)
ioctx.snap_set_read(librados::SNAP_HEAD);
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
// evict bar snap
ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// ...and then head
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
// this test case reproduces http://tracker.ceph.com/issues/8629
TEST_F(LibRadosTwoPoolsPP, EvictSnap2) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote on the head
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// evict
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify the snapdir is not present in the cache pool
{
ObjectReadOperation op;
librados::snap_set_t snapset;
op.list_snaps(&snapset, NULL);
ioctx.snap_set_read(librados::SNAP_DIR);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-ENOENT, completion->get_return_value());
completion->release();
}
}
//This test case reproduces http://tracker.ceph.com/issues/17445
TEST_F(LibRadosTwoPoolsPP, ListSnap){
SKIP_IF_CRIMSON();
// Create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// Create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
ObjectWriteOperation op;
op.remove();
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// Configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// Wait for maps to settle
cluster.wait_for_latest_osdmap();
// Read, trigger a promote on the head
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// Read foo snap
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// Evict foo snap
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// Snap is gone...
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-ENOENT, completion->get_return_value());
completion->release();
}
// Do list-snaps
ioctx.snap_set_read(CEPH_SNAPDIR);
{
snap_set_t snap_set;
int snap_ret;
ObjectReadOperation op;
op.list_snaps(&snap_set, &snap_ret);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
0, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, snap_ret);
ASSERT_LT(0u, snap_set.clones.size());
for (vector<librados::clone_info_t>::const_iterator r = snap_set.clones.begin();
r != snap_set.clones.end();
++r) {
if (r->cloneid != librados::SNAP_HEAD) {
ASSERT_LT(0u, r->snaps.size());
}
}
}
// Cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
// This test case reproduces https://tracker.ceph.com/issues/49409
TEST_F(LibRadosTwoPoolsPP, EvictSnapRollbackReadRace) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
int len = string("hi there").length() * 2;
// append more chrunk data make sure the second promote
// op coming before the first promote op finished
for (int i=0; i<4*1024*1024/len; ++i)
bl.append("hi therehi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// create two snapshot, a clone
vector<uint64_t> my_snaps(2);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[1]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote on the head
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// try more times
int retries = 50;
for (int i=0; i<retries; ++i)
{
{
librados::AioCompletion * completion = cluster.aio_create_completion();
librados::AioCompletion * completion1 = cluster.aio_create_completion();
// send a snap rollback op and a snap read op parallel
// trigger two promote(copy) to the same snap clone obj
// the second snap read op is read-ordered make sure
// op not wait for objects_blocked_on_snap_promotion
ObjectWriteOperation op;
op.selfmanaged_snap_rollback(my_snaps[0]);
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op));
ioctx.snap_set_read(my_snaps[1]);
std::map<uint64_t, uint64_t> extents;
bufferlist read_bl;
int rval = -1;
ObjectReadOperation op1;
op1.sparse_read(0, 8, &extents, &read_bl, &rval);
ASSERT_EQ(0, ioctx.aio_operate("foo", completion1, &op1, &read_bl));
ioctx.snap_set_read(librados::SNAP_HEAD);
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
completion1->wait_for_complete();
ASSERT_EQ(0, completion1->get_return_value());
completion1->release();
}
// evict foo snap
ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
ioctx.snap_set_read(librados::SNAP_HEAD);
}
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
ioctx.selfmanaged_snap_remove(my_snaps[1]);
}
TEST_F(LibRadosTwoPoolsPP, TryFlush) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// verify the object is NOT present in the base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// verify dirty
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_TRUE(dirty);
ASSERT_EQ(0, r);
}
// pin
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush the pinned object with -EPERM
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EPERM, completion->get_return_value());
completion->release();
}
// unpin
{
ObjectWriteOperation op;
op.cache_unpin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify clean
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_FALSE(dirty);
ASSERT_EQ(0, r);
}
// verify in base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it != ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// evict it
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op, librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify no longer in cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
}
TEST_F(LibRadosTwoPoolsPP, Flush) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
uint64_t user_version = 0;
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// verify the object is NOT present in the base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// verify dirty
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_TRUE(dirty);
ASSERT_EQ(0, r);
user_version = cache_ioctx.get_last_version();
}
// pin
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush the pinned object with -EPERM
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EPERM, completion->get_return_value());
completion->release();
}
// unpin
{
ObjectWriteOperation op;
op.cache_unpin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify clean
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_FALSE(dirty);
ASSERT_EQ(0, r);
}
// verify in base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it != ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// evict it
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op, librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify no longer in cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// read it again and verify the version is consistent
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("foo", bl, 1, 0));
ASSERT_EQ(user_version, cache_ioctx.get_last_version());
}
// erase it
{
ObjectWriteOperation op;
op.remove();
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush whiteout
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// evict
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op, librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify no longer in cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// or base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
}
TEST_F(LibRadosTwoPoolsPP, FlushSnap) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("a");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("b");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// and another
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("c");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// verify the object is NOT present in the base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// flush on head (should fail)
ioctx.snap_set_read(librados::SNAP_HEAD);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
// flush on recent snap (should fail)
ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
// flush on oldest snap
ioctx.snap_set_read(my_snaps[1]);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush on next oldest snap
ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush on head
ioctx.snap_set_read(librados::SNAP_HEAD);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify i can read the snaps from the cache pool
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('b', bl[0]);
}
ioctx.snap_set_read(my_snaps[1]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('a', bl[0]);
}
// remove overlay
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// verify i can read the snaps from the base pool
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('b', bl[0]);
}
ioctx.snap_set_read(my_snaps[1]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('a', bl[0]);
}
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
TEST_F(LibRadosTierPP, FlushWriteRaces) {
SKIP_IF_CRIMSON();
Rados cluster;
std::string pool_name = get_temp_pool_name();
std::string cache_pool_name = pool_name + "-cache";
ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
ASSERT_EQ(0, cluster.pool_create(cache_pool_name.c_str()));
IoCtx cache_ioctx;
ASSERT_EQ(0, cluster.ioctx_create(cache_pool_name.c_str(), cache_ioctx));
cache_ioctx.application_enable("rados", true);
IoCtx ioctx;
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create/dirty object
bufferlist bl;
bl.append("hi there");
{
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush + write
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
ObjectWriteOperation op2;
op2.write_full(bl);
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion2, &op2, 0));
completion->wait_for_complete();
completion2->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
}
int tries = 1000;
do {
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// try-flush + write
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
ObjectWriteOperation op2;
op2.write_full(bl);
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion2, &op2, 0));
completion->wait_for_complete();
completion2->wait_for_complete();
int r = completion->get_return_value();
ASSERT_TRUE(r == -EBUSY || r == 0);
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
if (r == -EBUSY)
break;
cout << "didn't get EBUSY, trying again" << std::endl;
}
ASSERT_TRUE(--tries);
} while (true);
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
ASSERT_EQ(0, cluster.pool_delete(cache_pool_name.c_str()));
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
}
TEST_F(LibRadosTwoPoolsPP, FlushTryFlushRaces) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush + flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
ObjectReadOperation op2;
op2.cache_flush();
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion2, &op2,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
completion2->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
}
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush + try-flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
ObjectReadOperation op2;
op2.cache_try_flush();
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion2, &op2,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
completion2->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
}
// create/dirty object
int tries = 1000;
do {
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// try-flush + flush
// (flush will not piggyback on try-flush)
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
ObjectReadOperation op2;
op2.cache_flush();
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion2, &op2,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
completion2->wait_for_complete();
int r = completion->get_return_value();
ASSERT_TRUE(r == -EBUSY || r == 0);
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
if (r == -EBUSY)
break;
cout << "didn't get EBUSY, trying again" << std::endl;
}
ASSERT_TRUE(--tries);
} while (true);
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// try-flush + try-flush
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
ObjectReadOperation op2;
op2.cache_try_flush();
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion2, &op2,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
completion2->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
}
}
IoCtx *read_ioctx = 0;
ceph::mutex test_lock = ceph::make_mutex("FlushReadRaces::lock");
ceph::condition_variable cond;
int max_reads = 100;
int num_reads = 0; // in progress
void flush_read_race_cb(completion_t cb, void *arg);
void start_flush_read()
{
//cout << " starting read" << std::endl;
ObjectReadOperation op;
op.stat(NULL, NULL, NULL);
librados::AioCompletion *completion = completions.getCompletion();
completion->set_complete_callback(0, flush_read_race_cb);
read_ioctx->aio_operate("foo", completion, &op, NULL);
}
void flush_read_race_cb(completion_t cb, void *arg)
{
//cout << " finished read" << std::endl;
std::lock_guard l{test_lock};
if (num_reads > max_reads) {
num_reads--;
cond.notify_all();
} else {
start_flush_read();
}
}
TEST_F(LibRadosTwoPoolsPP, TryFlushReadRace) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
bufferptr bp(4000000); // make it big!
bp.zero();
bl.append(bp);
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// start a continuous stream of reads
read_ioctx = &ioctx;
test_lock.lock();
for (int i = 0; i < max_reads; ++i) {
start_flush_read();
num_reads++;
}
test_lock.unlock();
// try-flush
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
// stop reads
std::unique_lock locker{test_lock};
max_reads = 0;
cond.wait(locker, [] { return num_reads == 0;});
}
TEST_F(LibRadosTierPP, HitSetNone) {
SKIP_IF_CRIMSON();
{
list< pair<time_t,time_t> > ls;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, ioctx.hit_set_list(123, c, &ls));
c->wait_for_complete();
ASSERT_EQ(0, c->get_return_value());
ASSERT_TRUE(ls.empty());
c->release();
}
{
bufferlist bl;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, ioctx.hit_set_get(123, c, 12345, &bl));
c->wait_for_complete();
ASSERT_EQ(-ENOENT, c->get_return_value());
c->release();
}
}
string set_pool_str(string pool, string var, string val)
{
return string("{\"prefix\": \"osd pool set\",\"pool\":\"") + pool
+ string("\",\"var\": \"") + var + string("\",\"val\": \"")
+ val + string("\"}");
}
string set_pool_str(string pool, string var, int val)
{
return string("{\"prefix\": \"osd pool set\",\"pool\":\"") + pool
+ string("\",\"var\": \"") + var + string("\",\"val\": \"")
+ stringify(val) + string("\"}");
}
TEST_F(LibRadosTwoPoolsPP, HitSetRead) {
SKIP_IF_CRIMSON();
// make it a tier
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
// enable hitset tracking for this pool
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_count", 2),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_type",
"explicit_object"),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
cache_ioctx.set_namespace("");
// keep reading until we see our object appear in the HitSet
utime_t start = ceph_clock_now();
utime_t hard_stop = start + utime_t(600, 0);
while (true) {
utime_t now = ceph_clock_now();
ASSERT_TRUE(now < hard_stop);
string name = "foo";
uint32_t hash;
ASSERT_EQ(0, cache_ioctx.get_object_hash_position2(name, &hash));
hobject_t oid(sobject_t(name, CEPH_NOSNAP), "", hash,
cluster.pool_lookup(cache_pool_name.c_str()), "");
bufferlist bl;
ASSERT_EQ(-ENOENT, cache_ioctx.read("foo", bl, 1, 0));
bufferlist hbl;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, cache_ioctx.hit_set_get(hash, c, now.sec(), &hbl));
c->wait_for_complete();
c->release();
if (hbl.length()) {
auto p = hbl.cbegin();
HitSet hs;
decode(hs, p);
if (hs.contains(oid)) {
cout << "ok, hit_set contains " << oid << std::endl;
break;
}
cout << "hmm, not in HitSet yet" << std::endl;
} else {
cout << "hmm, no HitSet yet" << std::endl;
}
sleep(1);
}
}
static int _get_pg_num(Rados& cluster, string pool_name)
{
bufferlist inbl;
string cmd = string("{\"prefix\": \"osd pool get\",\"pool\":\"")
+ pool_name
+ string("\",\"var\": \"pg_num\",\"format\": \"json\"}");
bufferlist outbl;
int r = cluster.mon_command(cmd, inbl, &outbl, NULL);
ceph_assert(r >= 0);
string outstr(outbl.c_str(), outbl.length());
json_spirit::Value v;
if (!json_spirit::read(outstr, v)) {
cerr <<" unable to parse json " << outstr << std::endl;
return -1;
}
json_spirit::Object& o = v.get_obj();
for (json_spirit::Object::size_type i=0; i<o.size(); i++) {
json_spirit::Pair& p = o[i];
if (p.name_ == "pg_num") {
cout << "pg_num = " << p.value_.get_int() << std::endl;
return p.value_.get_int();
}
}
cerr << "didn't find pg_num in " << outstr << std::endl;
return -1;
}
int make_hitset(Rados& cluster, librados::IoCtx& cache_ioctx, int num_pg,
int num, std::map<int, HitSet>& hitsets, std::string& cache_pool_name)
{
int pg = num_pg;
// do a bunch of writes
for (int i=0; i<num; ++i) {
bufferlist bl;
bl.append("a");
ceph_assert(0 == cache_ioctx.write(stringify(i), bl, 1, 0));
}
// get HitSets
for (int i=0; i<pg; ++i) {
list< pair<time_t,time_t> > ls;
AioCompletion *c = librados::Rados::aio_create_completion();
ceph_assert(0 == cache_ioctx.hit_set_list(i, c, &ls));
c->wait_for_complete();
c->release();
std::cout << "pg " << i << " ls " << ls << std::endl;
ceph_assert(!ls.empty());
// get the latest
c = librados::Rados::aio_create_completion();
bufferlist bl;
ceph_assert(0 == cache_ioctx.hit_set_get(i, c, ls.back().first, &bl));
c->wait_for_complete();
c->release();
try {
auto p = bl.cbegin();
decode(hitsets[i], p);
}
catch (buffer::error& e) {
std::cout << "failed to decode hit set; bl len is " << bl.length() << "\n";
bl.hexdump(std::cout);
std::cout << std::endl;
throw e;
}
// cope with racing splits by refreshing pg_num
if (i == pg - 1)
pg = _get_pg_num(cluster, cache_pool_name);
}
return pg;
}
TEST_F(LibRadosTwoPoolsPP, HitSetWrite) {
SKIP_IF_CRIMSON();
int num_pg = _get_pg_num(cluster, pool_name);
ceph_assert(num_pg > 0);
// make it a tier
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
// enable hitset tracking for this pool
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_count", 8),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_type",
"explicit_hash"),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
cache_ioctx.set_namespace("");
int num = 200;
std::map<int,HitSet> hitsets;
num_pg = make_hitset(cluster, cache_ioctx, num_pg, num, hitsets, cache_pool_name);
int retry = 0;
for (int i=0; i<num; ++i) {
string n = stringify(i);
uint32_t hash;
ASSERT_EQ(0, cache_ioctx.get_object_hash_position2(n, &hash));
hobject_t oid(sobject_t(n, CEPH_NOSNAP), "", hash,
cluster.pool_lookup(cache_pool_name.c_str()), "");
std::cout << "checking for " << oid << std::endl;
bool found = false;
for (int p=0; p<num_pg; ++p) {
if (hitsets[p].contains(oid)) {
found = true;
break;
}
}
if (!found && retry < 5) {
num_pg = make_hitset(cluster, cache_ioctx, num_pg, num, hitsets, cache_pool_name);
i--;
retry++;
continue;
}
ASSERT_TRUE(found);
}
}
TEST_F(LibRadosTwoPoolsPP, HitSetTrim) {
SKIP_IF_CRIMSON();
unsigned count = 3;
unsigned period = 3;
// make it a tier
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
// enable hitset tracking for this pool
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_count", count),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_period", period),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_type", "bloom"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_fpp", ".01"),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
cache_ioctx.set_namespace("");
// do a bunch of writes and make sure the hitsets rotate
utime_t start = ceph_clock_now();
utime_t hard_stop = start + utime_t(count * period * 50, 0);
time_t first = 0;
while (true) {
string name = "foo";
uint32_t hash;
ASSERT_EQ(0, cache_ioctx.get_object_hash_position2(name, &hash));
hobject_t oid(sobject_t(name, CEPH_NOSNAP), "", hash, -1, "");
bufferlist bl;
bl.append("f");
ASSERT_EQ(0, cache_ioctx.write("foo", bl, 1, 0));
list<pair<time_t, time_t> > ls;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, cache_ioctx.hit_set_list(hash, c, &ls));
c->wait_for_complete();
c->release();
cout << " got ls " << ls << std::endl;
if (!ls.empty()) {
if (!first) {
first = ls.front().first;
cout << "first is " << first << std::endl;
} else {
if (ls.front().first != first) {
cout << "first now " << ls.front().first << ", trimmed" << std::endl;
break;
}
}
}
utime_t now = ceph_clock_now();
ASSERT_TRUE(now < hard_stop);
sleep(1);
}
}
TEST_F(LibRadosTwoPoolsPP, PromoteOn2ndRead) {
SKIP_IF_CRIMSON();
// create object
for (int i=0; i<20; ++i) {
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo" + stringify(i), &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// enable hitset tracking for this pool
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_count", 2),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_type", "bloom"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "min_read_recency_for_promote", 1),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_grade_decay_rate", 20),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_search_last_n", 1),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
int fake = 0; // set this to non-zero to test spurious promotion,
// e.g. from thrashing
int attempt = 0;
string obj;
while (true) {
// 1st read, don't trigger a promote
obj = "foo" + stringify(attempt);
cout << obj << std::endl;
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read(obj.c_str(), bl, 1, 0));
if (--fake >= 0) {
sleep(1);
ASSERT_EQ(1, ioctx.read(obj.c_str(), bl, 1, 0));
sleep(1);
}
}
// verify the object is NOT present in the cache tier
{
bool found = false;
NObjectIterator it = cache_ioctx.nobjects_begin();
while (it != cache_ioctx.nobjects_end()) {
cout << " see " << it->get_oid() << std::endl;
if (it->get_oid() == string(obj.c_str())) {
found = true;
break;
}
++it;
}
if (!found)
break;
}
++attempt;
ASSERT_LE(attempt, 20);
cout << "hrm, object is present in cache on attempt " << attempt
<< ", retrying" << std::endl;
}
// Read until the object is present in the cache tier
cout << "verifying " << obj << " is eventually promoted" << std::endl;
while (true) {
bufferlist bl;
ASSERT_EQ(1, ioctx.read(obj.c_str(), bl, 1, 0));
bool there = false;
NObjectIterator it = cache_ioctx.nobjects_begin();
while (it != cache_ioctx.nobjects_end()) {
if (it->get_oid() == string(obj.c_str())) {
there = true;
break;
}
++it;
}
if (there)
break;
sleep(1);
}
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsPP, ProxyRead) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"readproxy\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read and verify the object
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// Verify 10 times the object is NOT present in the cache tier
uint32_t i = 0;
while (i++ < 10) {
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
sleep(1);
}
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsPP, CachePin) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger promote
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ(1, ioctx.read("baz", bl, 1, 0));
ASSERT_EQ(1, ioctx.read("bam", bl, 1, 0));
}
// verify the objects are present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
for (uint32_t i = 0; i < 4; i++) {
ASSERT_TRUE(it->get_oid() == string("foo") ||
it->get_oid() == string("bar") ||
it->get_oid() == string("baz") ||
it->get_oid() == string("bam"));
++it;
}
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// pin objects
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("baz", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// enable agent
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_count", 2),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_type", "bloom"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "min_read_recency_for_promote", 1),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "target_max_objects", 1),
inbl, NULL, NULL));
sleep(10);
// Verify the pinned object 'foo' is not flushed/evicted
uint32_t count = 0;
while (true) {
bufferlist bl;
ASSERT_EQ(1, ioctx.read("baz", bl, 1, 0));
count = 0;
NObjectIterator it = cache_ioctx.nobjects_begin();
while (it != cache_ioctx.nobjects_end()) {
ASSERT_TRUE(it->get_oid() == string("foo") ||
it->get_oid() == string("bar") ||
it->get_oid() == string("baz") ||
it->get_oid() == string("bam"));
++count;
++it;
}
if (count == 2) {
ASSERT_TRUE(it->get_oid() == string("foo") ||
it->get_oid() == string("baz"));
break;
}
sleep(1);
}
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsPP, SetRedirectRead) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
{
ObjectWriteOperation op;
op.set_redirect("bar", cache_ioctx, 0);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// read and verify the object
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('t', bl[0]);
}
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsPP, ManifestPromoteRead) {
SKIP_IF_CRIMSON();
// skip test if not yet mimic
if (_get_required_osd_release(cluster) < "mimic") {
GTEST_SKIP() << "cluster is not yet mimic, skipping test";
}
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("base chunk");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo-chunk", &op));
}
{
bufferlist bl;
bl.append("there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("CHUNK");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar-chunk", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// set-redirect
{
ObjectWriteOperation op;
op.set_redirect("bar", cache_ioctx, 0);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 2, "bar-chunk", "foo-chunk");
// promote
{
ObjectWriteOperation op;
op.tier_promote();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// read and verify the object (redirect)
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('t', bl[0]);
}
// promote
{
ObjectWriteOperation op;
op.tier_promote();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo-chunk", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// read and verify the object
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo-chunk", bl, 1, 0));
ASSERT_EQ('C', bl[0]);
}
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsPP, ManifestRefRead) {
SKIP_IF_CRIMSON();
// note: require >= mimic
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("base chunk");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo-chunk", &op));
}
{
bufferlist bl;
bl.append("there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("CHUNK");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar-chunk", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// set-redirect
{
ObjectWriteOperation op;
op.set_redirect("bar", cache_ioctx, 0, CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// set-chunk
{
ObjectReadOperation op;
op.set_chunk(0, 2, cache_ioctx, "bar-chunk", 0, CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo-chunk", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// redirect's refcount
{
bufferlist t;
cache_ioctx.getxattr("bar", CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_EQ(1U, refs.count());
}
// chunk's refcount
{
bufferlist t;
cache_ioctx.getxattr("bar-chunk", CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_EQ(1u, refs.count());
}
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsPP, ManifestUnset) {
SKIP_IF_CRIMSON();
// skip test if not yet nautilus
if (_get_required_osd_release(cluster) < "nautilus") {
GTEST_SKIP() << "cluster is not yet nautilus, skipping test";
}
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("base chunk");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo-chunk", &op));
}
{
bufferlist bl;
bl.append("there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("CHUNK");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar-chunk", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// set-redirect
{
ObjectWriteOperation op;
op.set_redirect("bar", cache_ioctx, 0, CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// set-chunk
{
ObjectReadOperation op;
op.set_chunk(0, 2, cache_ioctx, "bar-chunk", 0, CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo-chunk", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// redirect's refcount
{
bufferlist t;
cache_ioctx.getxattr("bar", CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_EQ(1u, refs.count());
}
// chunk's refcount
{
bufferlist t;
cache_ioctx.getxattr("bar-chunk", CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_EQ(1u, refs.count());
}
// unset-manifest for set-redirect
{
ObjectWriteOperation op;
op.unset_manifest();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// unset-manifest for set-chunk
{
ObjectWriteOperation op;
op.unset_manifest();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo-chunk", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// redirect's refcount
{
bufferlist t;
cache_ioctx.getxattr("bar-chunk", CHUNK_REFCOUNT_ATTR, t);
if (t.length() != 0U) {
ObjectWriteOperation op;
op.unset_manifest();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(-EOPNOTSUPP, completion->get_return_value());
completion->release();
}
}
// chunk's refcount
{
bufferlist t;
cache_ioctx.getxattr("bar-chunk", CHUNK_REFCOUNT_ATTR, t);
if (t.length() != 0U) {
ObjectWriteOperation op;
op.unset_manifest();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo-chunk", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(-EOPNOTSUPP, completion->get_return_value());
completion->release();
}
}
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsPP, ManifestDedupRefRead) {
SKIP_IF_CRIMSON();
// skip test if not yet nautilus
if (_get_required_osd_release(cluster) < "nautilus") {
GTEST_SKIP() << "cluster is not yet nautilus, skipping test";
}
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
string tgt_oid;
// get fp_oid
tgt_oid = get_fp_oid("There hi", "sha1");
// create object
{
bufferlist bl;
bl.append("There hi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("There hi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo-dedup", &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("There hi");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(tgt_oid, &op));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 8, tgt_oid, "foo-dedup");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 8, tgt_oid, "foo");
// chunk's refcount
{
bufferlist t;
cache_ioctx.getxattr(tgt_oid, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(2u, refs.count());
}
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsPP, ManifestSnapRefcount) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("there hi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
string er_fp_oid, hi_fp_oid, bb_fp_oid;
// get fp_oid
er_fp_oid = get_fp_oid("er", "sha1");
hi_fp_oid = get_fp_oid("hi", "sha1");
bb_fp_oid = get_fp_oid("bb", "sha1");
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("er");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(er_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("hi");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(hi_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("bb");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(bb_fp_oid, &op));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, er_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, hi_fp_oid, "foo");
// make all chunks dirty --> flush
// foo: [er] [hi]
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("er");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"er", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
cache_ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(1u, refs.count());
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// foo: [bb] [hi]
// create a clone
{
bufferlist bl;
bl.append("Thbbe");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// make clean
{
bufferlist bl;
bl.append("Thbbe");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, bb_fp_oid, "foo");
// and another
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// foo: [er] [hi]
// create a clone
{
bufferlist bl;
bl.append("There");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// make clean
{
bufferlist bl;
bl.append("There");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, er_fp_oid, "foo");
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("er");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"er", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
cache_ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(2u, refs.count());
}
// and another
my_snaps.resize(3);
my_snaps[2] = my_snaps[1];
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// foo: [bb] [hi]
// create a clone
{
bufferlist bl;
bl.append("Thbbe");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// make clean
{
bufferlist bl;
bl.append("Thbbe");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, bb_fp_oid, "foo");
/*
* snap[2]: [er] [hi]
* snap[1]: [bb] [hi]
* snap[0]: [er] [hi]
* head: [bb] [hi]
*/
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("hi");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"hi", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, p_str, 1);
}
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("er");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"er", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
cache_ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(2u, refs.count());
}
// remove snap
ioctx.selfmanaged_snap_remove(my_snaps[2]);
/*
* snap[1]: [bb] [hi]
* snap[0]: [er] [hi]
* head: [bb] [hi]
*/
sleep(10);
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("hi");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"hi", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, p_str, 1);
}
// remove snap
ioctx.selfmanaged_snap_remove(my_snaps[0]);
/*
* snap[1]: [bb] [hi]
* head: [bb] [hi]
*/
sleep(10);
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("bb");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"bb", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, p_str, 1);
}
// remove snap
ioctx.selfmanaged_snap_remove(my_snaps[1]);
/*
* snap[1]: [bb] [hi]
*/
sleep(10);
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("bb");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"bb", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, p_str, 1);
}
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("hi");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"hi", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, p_str, 1);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestSnapRefcount2) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("Thabe cdHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
string ab_fp_oid, cd_fp_oid, ef_fp_oid, BB_fp_oid;
// get fp_oid
ab_fp_oid = get_fp_oid("ab", "sha1");
cd_fp_oid = get_fp_oid("cd", "sha1");
ef_fp_oid = get_fp_oid("ef", "sha1");
BB_fp_oid = get_fp_oid("BB", "sha1");
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("ab");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(ab_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("cd");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(cd_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("ef");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(ef_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("BB");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(BB_fp_oid, &op));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, ab_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, cd_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, ef_fp_oid, "foo");
// make all chunks dirty --> flush
// foo: [ab] [cd] [ef]
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// foo: [BB] [BB] [ef]
// create a clone
{
bufferlist bl;
bl.append("ThBBe BB");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// make clean
{
bufferlist bl;
bl.append("ThBBe BB");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, BB_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, BB_fp_oid, "foo");
// and another
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// foo: [ab] [cd] [ef]
// create a clone
{
bufferlist bl;
bl.append("Thabe cd");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// make clean
{
bufferlist bl;
bl.append("Thabe cd");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, ab_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, cd_fp_oid, "foo");
/*
* snap[1]: [ab] [cd] [ef]
* snap[0]: [BB] [BB] [ef]
* head: [ab] [cd] [ef]
*/
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("ab");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"ab", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
cache_ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(2u, refs.count());
}
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("cd");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"cd", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
cache_ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(2u, refs.count());
}
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("BB");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"BB", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
cache_ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(2u, refs.count());
}
// remove snap
ioctx.selfmanaged_snap_remove(my_snaps[0]);
/*
* snap[1]: [ab] [cd] [ef]
* head: [ab] [cd] [ef]
*/
sleep(10);
// check chunk's refcount
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("BB");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"BB", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, p_str, 0);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestTestSnapCreate) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
GTEST_SKIP() << "cluster is not yet octopus, skipping test";
}
// create object
{
bufferlist bl;
bl.append("base chunk");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("CHUNKS CHUNKS");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
string ba_fp_oid, se_fp_oid, ch_fp_oid;
// get fp_oid
ba_fp_oid = get_fp_oid("ba", "sha1");
se_fp_oid = get_fp_oid("se", "sha1");
ch_fp_oid = get_fp_oid("ch", "sha1");
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("ba");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(ba_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("se");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(se_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("ch");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(ch_fp_oid, &op));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 2, ba_fp_oid, "foo");
// try to create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, se_fp_oid, "foo");
// check whether clone is created
ioctx.snap_set_read(librados::SNAP_DIR);
{
snap_set_t snap_set;
int snap_ret;
ObjectReadOperation op;
op.list_snaps(&snap_set, &snap_ret);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
0, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, snap_ret);
ASSERT_LT(0u, snap_set.clones.size());
ASSERT_EQ(1, snap_set.clones.size());
}
// create a clone
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
bl.append("B");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 0));
}
ioctx.snap_set_read(my_snaps[0]);
// set-chunk to clone
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, ch_fp_oid, "foo");
}
TEST_F(LibRadosTwoPoolsPP, ManifestRedirectAfterPromote) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
GTEST_SKIP() << "cluster is not yet octopus, skipping test";
}
// create object
{
bufferlist bl;
bl.append("base chunk");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("BASE CHUNK");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
// set-redirect
{
ObjectWriteOperation op;
op.set_redirect("bar", cache_ioctx, 0);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// promote
{
ObjectWriteOperation op;
op.tier_promote();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// write
{
bufferlist bl;
bl.append("a");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 0));
}
// read and verify the object (redirect)
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('a', bl[0]);
}
// read and verify the object (redirect)
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('B', bl[0]);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestCheckRefcountWhenModification) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
GTEST_SKIP() << "cluster is not yet octopus, skipping test";
}
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
string er_fp_oid, hi_fp_oid, HI_fp_oid, ai_fp_oid, bi_fp_oid,
Er_fp_oid, Hi_fp_oid, Si_fp_oid;
// get fp_oid
er_fp_oid = get_fp_oid("er", "sha1");
hi_fp_oid = get_fp_oid("hi", "sha1");
HI_fp_oid = get_fp_oid("HI", "sha1");
ai_fp_oid = get_fp_oid("ai", "sha1");
bi_fp_oid = get_fp_oid("bi", "sha1");
Er_fp_oid = get_fp_oid("Er", "sha1");
Hi_fp_oid = get_fp_oid("Hi", "sha1");
Si_fp_oid = get_fp_oid("Si", "sha1");
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("er");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(er_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("hi");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(hi_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("HI");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(HI_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("ai");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(ai_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("bi");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(bi_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("Er");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(Er_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("Hi");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(Hi_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("Si");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(Si_fp_oid, &op));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, er_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, hi_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, HI_fp_oid, "foo");
// foo head: [er] [hi] [HI]
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// foo snap[0]: [er] [hi] [HI]
// foo head : [er] [ai] [HI]
// create a clone
{
bufferlist bl;
bl.append("a");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 6));
}
// write
{
bufferlist bl;
bl.append("a");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 6));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, ai_fp_oid, "foo");
// foo snap[0]: [er] [hi] [HI]
// foo head : [er] [bi] [HI]
// create a clone
{
bufferlist bl;
bl.append("b");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 6));
}
// write
{
bufferlist bl;
bl.append("b");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 6));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, bi_fp_oid, "foo");
sleep(10);
// check chunk's refcount
// [ai]'s refcount should be 0
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("ai");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"ai", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, p_str, 0);
}
// foo snap[0]: [er] [hi] [HI]
// foo head : [Er] [Hi] [Si]
// create a clone
{
bufferlist bl;
bl.append("thEre HiSi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// write
{
bufferlist bl;
bl.append("thEre HiSi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, Er_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, Hi_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, Si_fp_oid, "foo");
// foo snap[0]: [er] [hi] [HI]
// foo head : [ER] [HI] [SI]
// write
{
bufferlist bl;
bl.append("thERe HISI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
sleep(10);
// check chunk's refcount
// [Er]'s refcount should be 0
{
bufferlist t;
SHA1 sha1_gen;
int size = strlen("Er");
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1];
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
sha1_gen.Update((const unsigned char *)"Er", size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, p_str, 0);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestSnapIncCount) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
// create object
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk1", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk2", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk3", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk4", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("there hiHI");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("there hiHI");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, "chunk1", "foo");
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk4", "foo");
// foo snap[1]:
// foo snap[0]:
// foo head : [chunk1] [chunk4]
ioctx.snap_set_read(my_snaps[1]);
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, "chunk2", "foo");
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk4", "foo");
// foo snap[1]: [chunk2] [chunk4]
// foo snap[0]:
// foo head : [chunk1] [chunk4]
ioctx.snap_set_read(my_snaps[0]);
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, "chunk2", "foo");
// foo snap[1]: [chunk2] [chunk4]
// foo snap[0]: [chunk2]
// foo head : [chunk1] [chunk4]
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, "chunk3", "foo");
// foo snap[1]: [chunk2] [chunk4]
// foo snap[0]: [chunk3] [chunk2]
// foo head : [chunk1] [chunk4]
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk4", "foo");
// foo snap[1]: [chunk2] [chunk4]
// foo snap[0]: [chunk3] [chunk2] [chunk4]
// foo head : [chunk1] [chunk4]
// check chunk's refcount
check_fp_oid_refcount(cache_ioctx, "chunk1", 1u, "");
// check chunk's refcount
check_fp_oid_refcount(cache_ioctx, "chunk2", 1u, "");
// check chunk's refcount
check_fp_oid_refcount(cache_ioctx, "chunk3", 1u, "");
sleep(10);
// check chunk's refcount
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk4", 1);
}
TEST_F(LibRadosTwoPoolsPP, ManifestEvict) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
// create object
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk1", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk2", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk3", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk4", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("there hiHI");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("there hiHI");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, "chunk1", "foo");
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk4", "foo");
// foo snap[1]:
// foo snap[0]:
// foo head : [chunk1] [chunk4]
ioctx.snap_set_read(my_snaps[1]);
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 10, "chunk2", "foo");
// foo snap[1]: [ chunk2 ]
// foo snap[0]:
// foo head : [chunk1] [chunk4]
ioctx.snap_set_read(my_snaps[0]);
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, "chunk2", "foo");
// foo snap[1]: [ chunk2 ]
// foo snap[0]: [chunk2]
// foo head : [chunk1] [chunk4]
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, "chunk3", "foo");
// foo snap[1]: [ chunk2 ]
// foo snap[0]: [chunk3] [chunk2]
// foo head : [chunk1] [chunk4]
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk4", "foo");
// foo snap[1]: [ chunk2 ]
// foo snap[0]: [chunk3] [chunk2] [chunk4]
// foo head : [chunk1] [chunk4]
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 2, "chunk4", "foo");
// foo snap[1]: [ chunk2 ]
// foo snap[0]: [chunk4] [chunk3] [chunk2] [chunk4]
// foo head : [chunk1] [chunk4]
manifest_set_chunk(cluster, cache_ioctx, ioctx, 4, 2, "chunk1", "foo");
// foo snap[1]: [ chunk2 ]
// foo snap[0]: [chunk4] [chunk3] [chunk1] [chunk2] [chunk4]
// foo head : [chunk1] [chunk4]
{
ObjectReadOperation op, stat_op;
uint64_t size;
op.tier_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
stat_op.stat(&size, NULL, NULL);
ASSERT_EQ(0, ioctx.operate("foo", &stat_op, NULL));
ASSERT_EQ(10, size);
}
ioctx.snap_set_read(librados::SNAP_HEAD);
{
ObjectReadOperation op, stat_op;
uint64_t size;
op.tier_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
stat_op.stat(&size, NULL, NULL);
ASSERT_EQ(0, ioctx.operate("foo", &stat_op, NULL));
ASSERT_EQ(strlen("there hiHI"), size);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestEvictPromote) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
// create object
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("EREHT hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk1", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk2", &op));
}
{
bufferlist bl;
bl.append("THERE HIHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk3", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("there");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 2, "chunk1", "foo");
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk2", "foo");
// foo snap[0]:
// foo head : [chunk1] [chunk2]
ioctx.snap_set_read(my_snaps[0]);
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 10, "chunk3", "foo");
// foo snap[0]: [ chunk3 ]
// foo head : [chunk1] [chunk2]
{
ObjectReadOperation op, stat_op;
uint64_t size;
op.tier_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
stat_op.stat(&size, NULL, NULL);
ASSERT_EQ(0, ioctx.operate("foo", &stat_op, NULL));
ASSERT_EQ(10, size);
}
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('T', bl[0]);
}
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(10, ioctx.read("foo", bl, 10, 0));
ASSERT_EQ('H', bl[8]);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestSnapSizeMismatch) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
// create object
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("chunk1", &op));
}
{
bufferlist bl;
bl.append("there HIHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("chunk2", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("There hiHI");
ASSERT_EQ(0, cache_ioctx.write("foo", bl, bl.length(), 0));
}
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("tHere hiHI");
ASSERT_EQ(0, cache_ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk
manifest_set_chunk(cluster, ioctx, cache_ioctx, 0, 10, "chunk1", "foo");
cache_ioctx.snap_set_read(my_snaps[1]);
// set-chunk
manifest_set_chunk(cluster, ioctx, cache_ioctx, 0, 10, "chunk2", "foo");
// evict
{
ObjectReadOperation op, stat_op;
op.tier_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
}
uint32_t hash;
ASSERT_EQ(0, cache_ioctx.get_object_pg_hash_position2("foo", &hash));
// scrub
{
for (int tries = 0; tries < 5; ++tries) {
bufferlist inbl;
ostringstream ss;
ss << "{\"prefix\": \"pg deep-scrub\", \"pgid\": \""
<< cache_ioctx.get_id() << "."
<< std::hex << hash
<< "\"}";
int r = cluster.mon_command(ss.str(), inbl, NULL, NULL);
if (r == -ENOENT ||
r == -EAGAIN) {
sleep(5);
continue;
}
ASSERT_EQ(0, r);
break;
}
cout << "waiting for scrubs..." << std::endl;
sleep(20);
cout << "done waiting" << std::endl;
}
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('t', bl[0]);
}
}
#include <common/CDC.h>
TEST_F(LibRadosTwoPoolsPP, DedupFlushRead) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
GTEST_SKIP() << "cluster is not yet octopus, skipping test";
}
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_tier", pool_name),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_chunk_algorithm", "fastcdc"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_cdc_chunk_size", 1024),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create object
bufferlist gbl;
{
generate_buffer(1024*8, &gbl);
ObjectWriteOperation op;
op.write_full(gbl);
ASSERT_EQ(0, cache_ioctx.operate("foo-chunk", &op));
}
{
bufferlist bl;
bl.append("DDse chunk");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar-chunk", &op));
}
// set-chunk to set manifest object
{
ObjectReadOperation op;
op.set_chunk(0, 2, ioctx, "bar-chunk", 0,
CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo-chunk", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo-chunk", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
std::unique_ptr<CDC> cdc = CDC::create("fastcdc", cbits(1024)-1);
vector<pair<uint64_t, uint64_t>> chunks;
bufferlist chunk;
cdc->calc_chunks(gbl, &chunks);
chunk.substr_of(gbl, chunks[1].first, chunks[1].second);
string tgt_oid;
{
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1] = {0};
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
SHA1 sha1_gen;
int size = chunk.length();
sha1_gen.Update((const unsigned char *)chunk.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
tgt_oid = string(p_str);
}
// read and verify the chunked object
{
bufferlist test_bl;
ASSERT_EQ(2, ioctx.read(tgt_oid, test_bl, 2, 0));
ASSERT_EQ(test_bl[1], chunk[1]);
}
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_cdc_chunk_size", 512),
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
// make a dirty chunks
{
bufferlist bl;
bl.append("hi");
ASSERT_EQ(0, cache_ioctx.write("foo-chunk", bl, bl.length(), 0));
}
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo-chunk", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
cdc = CDC::create("fastcdc", cbits(512)-1);
chunks.clear();
cdc->calc_chunks(gbl, &chunks);
bufferlist chunk_512;
chunk_512.substr_of(gbl, chunks[3].first, chunks[3].second);
{
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1] = {0};
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
SHA1 sha1_gen;
int size = chunk_512.length();
sha1_gen.Update((const unsigned char *)chunk_512.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
tgt_oid = string(p_str);
}
// read and verify the chunked object
{
bufferlist test_bl;
ASSERT_EQ(2, ioctx.read(tgt_oid, test_bl, 2, 0));
ASSERT_EQ(test_bl[1], chunk_512[1]);
}
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_cdc_chunk_size", 16384),
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
// make a dirty chunks
{
bufferlist bl;
bl.append("hi");
ASSERT_EQ(0, cache_ioctx.write("foo-chunk", bl, bl.length(), 0));
gbl.begin(0).copy_in(bl.length(), bl);
}
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo-chunk", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
cdc = CDC::create("fastcdc", cbits(16384)-1);
chunks.clear();
cdc->calc_chunks(gbl, &chunks);
bufferlist chunk_16384;
chunk_16384.substr_of(gbl, chunks[0].first, chunks[0].second);
{
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1] = {0};
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
SHA1 sha1_gen;
int size = chunk_16384.length();
sha1_gen.Update((const unsigned char *)chunk_16384.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
tgt_oid = string(p_str);
}
// read and verify the chunked object
{
bufferlist test_bl;
ASSERT_EQ(2, ioctx.read(tgt_oid, test_bl, 2, 0));
ASSERT_EQ(test_bl[0], chunk_16384[0]);
}
// less than object size
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_cdc_chunk_size", 1024),
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
// make a dirty chunks
// a chunk_info is deleted by write, which converts the manifest object to non-manifest object
{
bufferlist bl;
bl.append("hi");
ASSERT_EQ(0, cache_ioctx.write("foo-chunk", bl, bl.length(), 0));
}
// reset set-chunk
{
bufferlist bl;
bl.append("DDse chunk");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar-chunk", &op));
}
// set-chunk to set manifest object
{
ObjectReadOperation op;
op.set_chunk(0, 2, ioctx, "bar-chunk", 0,
CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo-chunk", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo-chunk", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
cdc = CDC::create("fastcdc", cbits(1024)-1);
chunks.clear();
cdc->calc_chunks(gbl, &chunks);
bufferlist small_chunk;
small_chunk.substr_of(gbl, chunks[1].first, chunks[1].second);
{
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1] = {0};
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
SHA1 sha1_gen;
int size = small_chunk.length();
sha1_gen.Update((const unsigned char *)small_chunk.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
tgt_oid = string(p_str);
}
// read and verify the chunked object
{
bufferlist test_bl;
ASSERT_EQ(2, ioctx.read(tgt_oid, test_bl, 2, 0));
ASSERT_EQ(test_bl[0], small_chunk[0]);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestFlushSnap) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_tier", pool_name),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_chunk_algorithm", "fastcdc"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_cdc_chunk_size", 1024),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create object
bufferlist gbl;
{
//bufferlist bl;
//bl.append("there hi");
generate_buffer(1024*8, &gbl);
ObjectWriteOperation op;
op.write_full(gbl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, ioctx, cache_ioctx, 2, 2, "bar", "foo");
manifest_set_chunk(cluster, ioctx, cache_ioctx, 6, 2, "bar", "foo");
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// make a dirty chunks
{
bufferlist bl;
bl.append("Thbbe");
ASSERT_EQ(0, cache_ioctx.write("foo", bl, bl.length(), 0));
}
// and another
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// make a dirty chunks
{
bufferlist bl;
bl.append("Thcce");
ASSERT_EQ(0, cache_ioctx.write("foo", bl, bl.length(), 0));
}
// flush on head (should fail)
cache_ioctx.snap_set_read(librados::SNAP_HEAD);
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
// flush on recent snap (should fail)
cache_ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
// flush on oldest snap
cache_ioctx.snap_set_read(my_snaps[1]);
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush on oldest snap
cache_ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush on oldest snap
cache_ioctx.snap_set_read(librados::SNAP_HEAD);
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// check chunk's refcount
std::unique_ptr<CDC> cdc = CDC::create("fastcdc", cbits(1024)-1);
vector<pair<uint64_t, uint64_t>> chunks;
bufferlist chunk;
cdc->calc_chunks(gbl, &chunks);
chunk.substr_of(gbl, chunks[1].first, chunks[1].second);
string tgt_oid;
{
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1] = {0};
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
SHA1 sha1_gen;
int size = chunk.length();
sha1_gen.Update((const unsigned char *)chunk.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
tgt_oid = string(p_str);
}
// read and verify the chunked object
{
bufferlist test_bl;
ASSERT_EQ(2, ioctx.read(tgt_oid, test_bl, 2, 0));
ASSERT_EQ(test_bl[1], chunk[1]);
}
cache_ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(4, cache_ioctx.read("foo", bl, 4, 0));
ASSERT_EQ('c', bl[2]);
}
cache_ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(4, cache_ioctx.read("foo", bl, 4, 0));
ASSERT_EQ('b', bl[2]);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestFlushDupCount) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_tier", pool_name),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_chunk_algorithm", "fastcdc"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_cdc_chunk_size", 1024),
inbl, NULL, NULL));
// create object
bufferlist gbl;
{
//bufferlist bl;
generate_buffer(1024*8, &gbl);
ObjectWriteOperation op;
op.write_full(gbl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// set-chunk to set manifest object
{
ObjectReadOperation op;
op.set_chunk(0, 2, ioctx, "bar", 0,
CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// make a dirty chunks
{
bufferlist bl;
bl.append("Thbbe hi");
ASSERT_EQ(0, cache_ioctx.write("foo", bl, bl.length(), 0));
}
// and another
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, cache_ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// make a dirty chunks
{
bufferlist bl;
bl.append("Thcce hi");
ASSERT_EQ(0, cache_ioctx.write("foo", bl, bl.length(), 0));
}
//flush on oldest snap
cache_ioctx.snap_set_read(my_snaps[1]);
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush on oldest snap
cache_ioctx.snap_set_read(my_snaps[0]);
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
cache_ioctx.snap_set_read(librados::SNAP_HEAD);
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
std::unique_ptr<CDC> cdc = CDC::create("fastcdc", cbits(1024)-1);
vector<pair<uint64_t, uint64_t>> chunks;
bufferlist chunk;
cdc->calc_chunks(gbl, &chunks);
chunk.substr_of(gbl, chunks[1].first, chunks[1].second);
string tgt_oid;
// check chunk's refcount
{
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1] = {0};
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
bufferlist t;
SHA1 sha1_gen;
int size = chunk.length();
sha1_gen.Update((const unsigned char *)chunk.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
tgt_oid = string(p_str);
ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(1u, refs.count());
}
bufferlist chunk2;
chunk2.substr_of(gbl, chunks[0].first, chunks[0].second);
// check chunk's refcount
{
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1] = {0};
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
bufferlist t;
SHA1 sha1_gen;
int size = chunk2.length();
sha1_gen.Update((const unsigned char *)chunk2.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
tgt_oid = string(p_str);
ioctx.getxattr(p_str, CHUNK_REFCOUNT_ATTR, t);
chunk_refs_t refs;
try {
auto iter = t.cbegin();
decode(refs, iter);
} catch (buffer::error& err) {
ASSERT_TRUE(0);
}
ASSERT_LE(1u, refs.count());
}
// make a dirty chunks
{
bufferlist bl;
bl.append("ThDDe hi");
ASSERT_EQ(0, cache_ioctx.write("foo", bl, bl.length(), 0));
}
// flush
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
bufferlist tmp;
tmp.append("Thcce hi");
gbl.begin(0).copy_in(tmp.length(), tmp);
bufferlist chunk3;
cdc->calc_chunks(gbl, &chunks);
chunk3.substr_of(gbl, chunks[0].first, chunks[0].second);
// check chunk's refcount
{
unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE + 1] = {0};
char p_str[CEPH_CRYPTO_SHA1_DIGESTSIZE*2+1] = {0};
bufferlist t;
SHA1 sha1_gen;
int size = chunk2.length();
sha1_gen.Update((const unsigned char *)chunk2.c_str(), size);
sha1_gen.Final(fingerprint);
buf_to_hex(fingerprint, CEPH_CRYPTO_SHA1_DIGESTSIZE, p_str);
is_intended_refcount_state(cache_ioctx, "foo", ioctx, p_str, 0);
}
}
TEST_F(LibRadosTwoPoolsPP, TierFlushDuringFlush) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
bufferlist inbl;
// create a new pool
std::string temp_pool_name = get_temp_pool_name() + "-test-flush";
ASSERT_EQ(0, cluster.pool_create(temp_pool_name.c_str()));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_tier", temp_pool_name),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_chunk_algorithm", "fastcdc"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_cdc_chunk_size", 1024),
inbl, NULL, NULL));
// create object
bufferlist gbl;
{
//bufferlist bl;
generate_buffer(1024*8, &gbl);
ObjectWriteOperation op;
op.write_full(gbl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// set-chunk to set manifest object
{
ObjectReadOperation op;
op.set_chunk(0, 2, ioctx, "bar", 0,
CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// delete temp pool, so flushing chunk will fail
ASSERT_EQ(0, s_cluster.pool_delete(temp_pool_name.c_str()));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// flush to check if proper error is returned
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-ENOENT, completion->get_return_value());
completion->release();
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestSnapHasChunk) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("there HIHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
string er_fp_oid, hi_fp_oid, HI_fp_oid, ai_fp_oid, bi_fp_oid,
Er_fp_oid, Hi_fp_oid, SI_fp_oid;
// get fp_oid
er_fp_oid = get_fp_oid("er", "sha1");
hi_fp_oid = get_fp_oid("hi", "sha1");
HI_fp_oid = get_fp_oid("HI", "sha1");
ai_fp_oid = get_fp_oid("ai", "sha1");
bi_fp_oid = get_fp_oid("bi", "sha1");
Er_fp_oid = get_fp_oid("Er", "sha1");
Hi_fp_oid = get_fp_oid("Hi", "sha1");
SI_fp_oid = get_fp_oid("SI", "sha1");
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("er");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(er_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("hi");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(hi_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("HI");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(HI_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("ai");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(ai_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("bi");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(bi_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("Er");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(Er_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("Hi");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(Hi_fp_oid, &op));
}
// write
{
ObjectWriteOperation op;
bufferlist bl;
bl.append("SI");
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate(SI_fp_oid, &op));
}
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, HI_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, HI_fp_oid, "foo");
// foo head: [hi] [HI]
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// create a clone
{
bufferlist bl;
bl.append("a");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 6));
}
// write
{
bufferlist bl;
bl.append("a");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 6));
}
// write
{
bufferlist bl;
bl.append("S");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 8));
}
// foo snap[0]: [hi] [HI]
// foo head : [er] [ai] [SI]
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, er_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, ai_fp_oid, "foo");
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, SI_fp_oid, "foo");
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
// create a clone
{
bufferlist bl;
bl.append("b");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 6));
}
// write
{
bufferlist bl;
bl.append("b");
ASSERT_EQ(0, ioctx.write("foo", bl, 1, 6));
}
// foo snap[1]: [HI] [HI]
// foo snap[0]: [er] [ai] [SI]
// foo head : [er] [bi] [SI]
// set-chunk (dedup)
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, bi_fp_oid, "foo");
{
ASSERT_EQ(1, cls_cas_references_chunk(ioctx, "foo", SI_fp_oid));
ASSERT_EQ(1, cls_cas_references_chunk(ioctx, "foo", er_fp_oid));
ASSERT_EQ(1, cls_cas_references_chunk(ioctx, "foo", ai_fp_oid));
ASSERT_EQ(2, cls_cas_references_chunk(ioctx, "foo", HI_fp_oid));
ASSERT_EQ(-ENOLINK, cls_cas_references_chunk(ioctx, "foo", Hi_fp_oid));
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestRollback) {
SKIP_IF_CRIMSON();
// skip test if not yet pacific
if (_get_required_osd_release(cluster) < "pacific") {
cout << "cluster is not yet pacific, skipping test" << std::endl;
return;
}
// create object
{
bufferlist bl;
bl.append("CDere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("ABere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk1", &op));
}
{
bufferlist bl;
bl.append("CDere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk2", &op));
}
{
bufferlist bl;
bl.append("EFere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk3", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("there hiHI");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("thABe hiEF");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, "chunk1", "foo");
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk3", "foo");
// foo snap[1]:
// foo snap[0]:
// foo head : [chunk1] [chunk3]
ioctx.snap_set_read(my_snaps[1]);
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 10, "chunk2", "foo");
// foo snap[1]: [ chunk2 ]
// foo snap[0]:
// foo head : [chunk1] [chunk3]
// foo snap[1]: [ chunk2 ]
// foo snap[0]:
// foo head : [chunk1] [chunk3]
ASSERT_EQ(0, ioctx.selfmanaged_snap_rollback("foo", my_snaps[0]));
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('t', bl[0]);
}
ASSERT_EQ(0, ioctx.selfmanaged_snap_rollback("foo", my_snaps[1]));
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('C', bl[0]);
}
}
TEST_F(LibRadosTwoPoolsPP, ManifestRollbackRefcount) {
SKIP_IF_CRIMSON();
// skip test if not yet pacific
if (_get_required_osd_release(cluster) < "pacific") {
cout << "cluster is not yet pacific, skipping test" << std::endl;
return;
}
// create object
{
bufferlist bl;
bl.append("CDere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("ABere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk1", &op));
}
{
bufferlist bl;
bl.append("CDere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk2", &op));
}
{
bufferlist bl;
bl.append("EFere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk3", &op));
}
{
bufferlist bl;
bl.append("DDDDD hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk4", &op));
}
{
bufferlist bl;
bl.append("EEEEE hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk5", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("there hiHI");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("thABe hiEF");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, "chunk1", "foo");
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk3", "foo");
// foo snap[1]:
// foo snap[0]:
// foo head : [chunk1] [chunk3]
ioctx.snap_set_read(my_snaps[1]);
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, "chunk4", "foo");
manifest_set_chunk(cluster, cache_ioctx, ioctx, 6, 2, "chunk5", "foo");
// foo snap[1]: [chunk4] [chunk5]
// foo snap[0]:
// foo head : [chunk1] [chunk3]
ioctx.snap_set_read(my_snaps[0]);
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 10, "chunk2", "foo");
// foo snap[1]: [chunk4] [chunk5]
// foo snap[0]: [ chunk2 ]
// foo head : [chunk1] [chunk3]
ASSERT_EQ(0, ioctx.selfmanaged_snap_rollback("foo", my_snaps[1]));
// foo snap[1]: [chunk4] [chunk5]
// foo snap[0]: [ chunk2 ]
// foo head : [chunk4] [chunk5] <-- will contain these contents
sleep(10);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk1", 0);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk3", 0);
ioctx.selfmanaged_snap_remove(my_snaps[1]);
sleep(10);
// foo snap[1]:
// foo snap[0]: [ chunk2 ]
// foo head : [chunk4] [chunk5]
ioctx.snap_set_read(librados::SNAP_HEAD);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk4", 1);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk5", 1);
{
bufferlist bl;
bl.append("thABe hiEF");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// foo snap[1]:
// foo snap[0]: [ chunk2 ]
// foo head :
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk1", 0);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk3", 0);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk4", 0);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk5", 0);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk2", 1);
}
TEST_F(LibRadosTwoPoolsPP, ManifestEvictRollback) {
SKIP_IF_CRIMSON();
// skip test if not yet pacific
if (_get_required_osd_release(cluster) < "pacific") {
cout << "cluster is not yet pacific, skipping test" << std::endl;
return;
}
// create object
{
bufferlist bl;
bl.append("CDere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("ABere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk1", &op));
}
{
bufferlist bl;
bl.append("CDere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk2", &op));
}
{
bufferlist bl;
bl.append("EFere hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("chunk3", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("there hiHI");
ASSERT_EQ(0, ioctx.write("foo", bl, bl.length(), 0));
}
// set-chunk
manifest_set_chunk(cluster, cache_ioctx, ioctx, 2, 2, "chunk1", "foo");
manifest_set_chunk(cluster, cache_ioctx, ioctx, 8, 2, "chunk3", "foo");
// foo snap[0]:
// foo head : [chunk1] [chunk3]
ioctx.snap_set_read(my_snaps[0]);
manifest_set_chunk(cluster, cache_ioctx, ioctx, 0, 10, "chunk2", "foo");
// foo snap[0]: [ chunk2 ]
// foo head : [chunk1] [chunk3]
sleep(10);
ioctx.snap_set_read(librados::SNAP_HEAD);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk1", 1);
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk3", 1);
ioctx.snap_set_read(my_snaps[0]);
// evict--this makes the chunk missing state
{
ObjectReadOperation op, stat_op;
op.tier_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
}
// rollback to my_snaps[0]
ASSERT_EQ(0, ioctx.selfmanaged_snap_rollback("foo", my_snaps[0]));
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('C', bl[0]);
}
is_intended_refcount_state(ioctx, "foo", cache_ioctx, "chunk2", 1);
}
class LibRadosTwoPoolsECPP : public RadosTestECPP
{
public:
LibRadosTwoPoolsECPP() {};
~LibRadosTwoPoolsECPP() override {};
protected:
static void SetUpTestCase() {
SKIP_IF_CRIMSON();
pool_name = get_temp_pool_name();
ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
}
static void TearDownTestCase() {
SKIP_IF_CRIMSON();
ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
}
static std::string cache_pool_name;
void SetUp() override {
SKIP_IF_CRIMSON();
cache_pool_name = get_temp_pool_name();
ASSERT_EQ(0, s_cluster.pool_create(cache_pool_name.c_str()));
RadosTestECPP::SetUp();
ASSERT_EQ(0, cluster.ioctx_create(cache_pool_name.c_str(), cache_ioctx));
cache_ioctx.application_enable("rados", true);
cache_ioctx.set_namespace(nspace);
}
void TearDown() override {
SKIP_IF_CRIMSON();
// flush + evict cache
flush_evict_all(cluster, cache_ioctx);
bufferlist inbl;
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
RadosTestECPP::TearDown();
cleanup_default_namespace(cache_ioctx);
cleanup_namespace(cache_ioctx, nspace);
cache_ioctx.close();
ASSERT_EQ(0, s_cluster.pool_delete(cache_pool_name.c_str()));
}
librados::IoCtx cache_ioctx;
};
std::string LibRadosTwoPoolsECPP::cache_pool_name;
TEST_F(LibRadosTierECPP, Dirty) {
SKIP_IF_CRIMSON();
{
ObjectWriteOperation op;
op.undirty();
ASSERT_EQ(0, ioctx.operate("foo", &op)); // still get 0 if it dne
}
{
ObjectWriteOperation op;
op.create(true);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, ioctx.operate("foo", &op, NULL));
ASSERT_TRUE(dirty);
ASSERT_EQ(0, r);
}
{
ObjectWriteOperation op;
op.undirty();
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
ObjectWriteOperation op;
op.undirty();
ASSERT_EQ(0, ioctx.operate("foo", &op)); // still 0 if already clean
}
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, ioctx.operate("foo", &op, NULL));
ASSERT_FALSE(dirty);
ASSERT_EQ(0, r);
}
//{
// ObjectWriteOperation op;
// op.truncate(0); // still a write even tho it is a no-op
// ASSERT_EQ(0, ioctx.operate("foo", &op));
//}
//{
// bool dirty = false;
// int r = -1;
// ObjectReadOperation op;
// op.is_dirty(&dirty, &r);
// ASSERT_EQ(0, ioctx.operate("foo", &op, NULL));
// ASSERT_TRUE(dirty);
// ASSERT_EQ(0, r);
//}
}
TEST_F(LibRadosTwoPoolsECPP, Overlay) {
SKIP_IF_CRIMSON();
// create objects
{
bufferlist bl;
bl.append("base");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("cache");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// by default, the overlay sends us to cache pool
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// unless we say otherwise
{
bufferlist bl;
ObjectReadOperation op;
op.read(0, 1, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
ASSERT_EQ('b', bl[0]);
}
}
TEST_F(LibRadosTwoPoolsECPP, Promote) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
}
// read, trigger a whiteout
{
bufferlist bl;
ASSERT_EQ(-ENOENT, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ(-ENOENT, ioctx.read("bar", bl, 1, 0));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
}
TEST_F(LibRadosTwoPoolsECPP, PromoteSnap) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
ObjectWriteOperation op;
op.remove();
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote on the head
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bam", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
ioctx.snap_set_read(my_snaps[0]);
// stop and scrub this pg (to make sure scrub can handle missing
// clones in the cache tier)
// This test requires cache tier and base tier to have the same pg_num/pgp_num
{
for (int tries = 0; tries < 5; ++tries) {
IoCtx cache_ioctx;
ASSERT_EQ(0, cluster.ioctx_create(cache_pool_name.c_str(), cache_ioctx));
uint32_t hash;
ASSERT_EQ(0, ioctx.get_object_pg_hash_position2("foo", &hash));
ostringstream ss;
ss << "{\"prefix\": \"pg scrub\", \"pgid\": \""
<< cache_ioctx.get_id() << "."
<< hash
<< "\"}";
int r = cluster.mon_command(ss.str(), inbl, NULL, NULL);
if (r == -EAGAIN ||
r == -ENOENT) { // in case mgr osdmap is a bit stale
sleep(5);
continue;
}
ASSERT_EQ(0, r);
break;
}
// give it a few seconds to go. this is sloppy but is usually enough time
cout << "waiting for scrub..." << std::endl;
sleep(15);
cout << "done waiting" << std::endl;
}
// read foo snap
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// read bar snap
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// read baz snap
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("baz", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
ioctx.snap_set_read(librados::SNAP_HEAD);
// read foo
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// read bar
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// read baz
{
bufferlist bl;
ASSERT_EQ(-ENOENT, ioctx.read("baz", bl, 1, 0));
}
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
TEST_F(LibRadosTwoPoolsECPP, PromoteSnapTrimRace) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// delete the snap
ASSERT_EQ(0, ioctx.selfmanaged_snap_remove(my_snaps[0]));
ioctx.snap_set_read(my_snaps[0]);
// read foo snap. the OSD may or may not realize that this snap has
// been logically deleted; either response is valid.
{
bufferlist bl;
int r = ioctx.read("foo", bl, 1, 0);
ASSERT_TRUE(r == 1 || r == -ENOENT);
}
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
TEST_F(LibRadosTwoPoolsECPP, Whiteout) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create some whiteouts, verify they behave
{
ObjectWriteOperation op;
op.assert_exists();
op.remove();
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
ObjectWriteOperation op;
op.assert_exists();
op.remove();
ASSERT_EQ(-ENOENT, ioctx.operate("bar", &op));
}
{
ObjectWriteOperation op;
op.assert_exists();
op.remove();
ASSERT_EQ(-ENOENT, ioctx.operate("bar", &op));
}
// verify the whiteouts are there in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// delete a whiteout and verify it goes away
ASSERT_EQ(-ENOENT, ioctx.remove("foo"));
{
ObjectWriteOperation op;
op.remove();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("bar", completion, &op,
librados::OPERATION_IGNORE_CACHE));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// recreate an object and verify we can read it
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
}
TEST_F(LibRadosTwoPoolsECPP, Evict) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
}
// read, trigger a whiteout, and a dirty object
{
bufferlist bl;
ASSERT_EQ(-ENOENT, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ(-ENOENT, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ(0, ioctx.write("bar", bl, bl.length(), 0));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it->get_oid() == string("foo") || it->get_oid() == string("bar"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// pin
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// evict the pinned object with -EPERM
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE,
NULL));
completion->wait_for_complete();
ASSERT_EQ(-EPERM, completion->get_return_value());
completion->release();
}
// unpin
{
ObjectWriteOperation op;
op.cache_unpin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify clean
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_FALSE(dirty);
ASSERT_EQ(0, r);
}
// evict
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE,
NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
}
TEST_F(LibRadosTwoPoolsECPP, EvictSnap) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
ObjectWriteOperation op;
op.remove();
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("ciao!");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger a promote on the head
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bam", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
// evict bam
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"bam", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"bam", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-ENOENT, completion->get_return_value());
completion->release();
}
// read foo snap
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// evict foo snap
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// snap is gone...
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-ENOENT, completion->get_return_value());
completion->release();
}
// head is still there...
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// promote head + snap of bar
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// evict bar head (fail)
ioctx.snap_set_read(librados::SNAP_HEAD);
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
// evict bar snap
ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// ...and then head
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ObjectReadOperation op;
op.read(1, 0, &bl, NULL);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"bar", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
TEST_F(LibRadosTwoPoolsECPP, TryFlush) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// verify the object is NOT present in the base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// verify dirty
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_TRUE(dirty);
ASSERT_EQ(0, r);
}
// pin
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush the pinned object with -EPERM
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EPERM, completion->get_return_value());
completion->release();
}
// unpin
{
ObjectWriteOperation op;
op.cache_unpin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify clean
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_FALSE(dirty);
ASSERT_EQ(0, r);
}
// verify in base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it != ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// evict it
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op, librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify no longer in cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
}
TEST_F(LibRadosTwoPoolsECPP, FailedFlush) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// verify the object is NOT present in the base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// set omap
{
ObjectWriteOperation op;
std::map<std::string, bufferlist> omap;
omap["somekey"] = bufferlist();
op.omap_set(omap);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_NE(0, completion->get_return_value());
completion->release();
}
// get omap
{
ObjectReadOperation op;
bufferlist bl;
int prval = 0;
std::set<std::string> keys;
keys.insert("somekey");
std::map<std::string, bufferlist> map;
op.omap_get_vals_by_keys(keys, &map, &prval);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op, &bl));
sleep(5);
bool completed = completion->is_complete();
if( !completed ) {
cache_ioctx.aio_cancel(completion);
std::cerr << "Most probably test case will hang here, please reset manually" << std::endl;
ASSERT_TRUE(completed); //in fact we are locked forever at test case shutdown unless fix for http://tracker.ceph.com/issues/14511 is applied. Seems there is no workaround for that
}
completion->release();
}
// verify still not in base tier
{
ASSERT_TRUE(ioctx.nobjects_begin() == ioctx.nobjects_end());
}
// erase it
{
ObjectWriteOperation op;
op.remove();
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush whiteout
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// evict
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op, librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify no longer in cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// or base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
}
TEST_F(LibRadosTwoPoolsECPP, Flush) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
uint64_t user_version = 0;
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// verify the object is NOT present in the base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// verify dirty
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_TRUE(dirty);
ASSERT_EQ(0, r);
user_version = cache_ioctx.get_last_version();
}
// pin
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush the pinned object with -EPERM
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EPERM, completion->get_return_value());
completion->release();
}
// unpin
{
ObjectWriteOperation op;
op.cache_unpin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify clean
{
bool dirty = false;
int r = -1;
ObjectReadOperation op;
op.is_dirty(&dirty, &r);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op, NULL));
ASSERT_FALSE(dirty);
ASSERT_EQ(0, r);
}
// verify in base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it != ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// evict it
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op, librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify no longer in cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// read it again and verify the version is consistent
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("foo", bl, 1, 0));
ASSERT_EQ(user_version, cache_ioctx.get_last_version());
}
// erase it
{
ObjectWriteOperation op;
op.remove();
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush whiteout
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// evict
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op, librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify no longer in cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// or base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
}
TEST_F(LibRadosTwoPoolsECPP, FlushSnap) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create object
{
bufferlist bl;
bl.append("a");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// create a snapshot, clone
vector<uint64_t> my_snaps(1);
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("b");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// and another
my_snaps.resize(2);
my_snaps[1] = my_snaps[0];
ASSERT_EQ(0, ioctx.selfmanaged_snap_create(&my_snaps[0]));
ASSERT_EQ(0, ioctx.selfmanaged_snap_set_write_ctx(my_snaps[0],
my_snaps));
{
bufferlist bl;
bl.append("c");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// verify the object is present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// verify the object is NOT present in the base tier
{
NObjectIterator it = ioctx.nobjects_begin();
ASSERT_TRUE(it == ioctx.nobjects_end());
}
// flush on head (should fail)
ioctx.snap_set_read(librados::SNAP_HEAD);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
// flush on recent snap (should fail)
ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EBUSY, completion->get_return_value());
completion->release();
}
// flush on oldest snap
ioctx.snap_set_read(my_snaps[1]);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush on next oldest snap
ioctx.snap_set_read(my_snaps[0]);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush on head
ioctx.snap_set_read(librados::SNAP_HEAD);
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// verify i can read the snaps from the cache pool
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('b', bl[0]);
}
ioctx.snap_set_read(my_snaps[1]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('a', bl[0]);
}
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// verify i can read the snaps from the base pool
ioctx.snap_set_read(librados::SNAP_HEAD);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('c', bl[0]);
}
ioctx.snap_set_read(my_snaps[0]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('b', bl[0]);
}
ioctx.snap_set_read(my_snaps[1]);
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('a', bl[0]);
}
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
cluster.wait_for_latest_osdmap();
// cleanup
ioctx.selfmanaged_snap_remove(my_snaps[0]);
}
TEST_F(LibRadosTierECPP, FlushWriteRaces) {
SKIP_IF_CRIMSON();
Rados cluster;
std::string pool_name = get_temp_pool_name();
std::string cache_pool_name = pool_name + "-cache";
ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
ASSERT_EQ(0, cluster.pool_create(cache_pool_name.c_str()));
IoCtx cache_ioctx;
ASSERT_EQ(0, cluster.ioctx_create(cache_pool_name.c_str(), cache_ioctx));
cache_ioctx.application_enable("rados", true);
IoCtx ioctx;
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create/dirty object
bufferlist bl;
bl.append("hi there");
{
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush + write
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
ObjectWriteOperation op2;
op2.write_full(bl);
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate(
"foo", completion2, &op2, 0));
completion->wait_for_complete();
completion2->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
}
int tries = 1000;
do {
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// try-flush + write
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
ObjectWriteOperation op2;
op2.write_full(bl);
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion2, &op2, 0));
completion->wait_for_complete();
completion2->wait_for_complete();
int r = completion->get_return_value();
ASSERT_TRUE(r == -EBUSY || r == 0);
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
if (r == -EBUSY)
break;
cout << "didn't get EBUSY, trying again" << std::endl;
}
ASSERT_TRUE(--tries);
} while (true);
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
ASSERT_EQ(0, cluster.pool_delete(cache_pool_name.c_str()));
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
}
TEST_F(LibRadosTwoPoolsECPP, FlushTryFlushRaces) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush + flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
ObjectReadOperation op2;
op2.cache_flush();
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion2, &op2,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
completion2->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
}
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush + try-flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
ObjectReadOperation op2;
op2.cache_try_flush();
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion2, &op2,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
completion2->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
}
// create/dirty object
int tries = 1000;
do {
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// try-flush + flush
// (flush will not piggyback on try-flush)
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
ObjectReadOperation op2;
op2.cache_flush();
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion2, &op2,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
completion2->wait_for_complete();
int r = completion->get_return_value();
ASSERT_TRUE(r == -EBUSY || r == 0);
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
if (r == -EBUSY)
break;
cout << "didn't get EBUSY, trying again" << std::endl;
}
ASSERT_TRUE(--tries);
} while (true);
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// try-flush + try-flush
{
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
ObjectReadOperation op2;
op2.cache_try_flush();
librados::AioCompletion *completion2 = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion2, &op2,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
completion2->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
ASSERT_EQ(0, completion2->get_return_value());
completion->release();
completion2->release();
}
}
TEST_F(LibRadosTwoPoolsECPP, TryFlushReadRace) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create/dirty object
{
bufferlist bl;
bl.append("hi there");
bufferptr bp(4000000); // make it big!
bp.zero();
bl.append(bp);
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// start a continuous stream of reads
read_ioctx = &ioctx;
test_lock.lock();
for (int i = 0; i < max_reads; ++i) {
start_flush_read();
num_reads++;
}
test_lock.unlock();
// try-flush
ObjectReadOperation op;
op.cache_try_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY |
librados::OPERATION_SKIPRWLOCKS, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
// stop reads
std::unique_lock locker{test_lock};
max_reads = 0;
cond.wait(locker, [] { return num_reads == 0;});
}
TEST_F(LibRadosTierECPP, CallForcesPromote) {
SKIP_IF_CRIMSON();
Rados cluster;
std::string pool_name = get_temp_pool_name();
std::string cache_pool_name = pool_name + "-cache";
ASSERT_EQ("", create_one_ec_pool_pp(pool_name, cluster));
ASSERT_EQ(0, cluster.pool_create(cache_pool_name.c_str()));
IoCtx cache_ioctx;
ASSERT_EQ(0, cluster.ioctx_create(cache_pool_name.c_str(), cache_ioctx));
cache_ioctx.application_enable("rados", true);
IoCtx ioctx;
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// set things up such that the op would normally be proxied
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_count", 2),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_type",
"explicit_object"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "min_read_recency_for_promote",
"4"),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// create/dirty object
bufferlist bl;
bl.append("hi there");
{
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// flush
{
ObjectReadOperation op;
op.cache_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op,
librados::OPERATION_IGNORE_OVERLAY, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// evict
{
ObjectReadOperation op;
op.cache_evict();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE,
NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// call
{
ObjectReadOperation op;
bufferlist bl;
op.exec("rbd", "get_id", bl);
bufferlist out;
// should get EIO (not an rbd object), not -EOPNOTSUPP (we didn't promote)
ASSERT_EQ(-5, ioctx.operate("foo", &op, &out));
}
// make sure foo is back in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
ASSERT_TRUE(it->get_oid() == string("foo"));
++it;
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
ASSERT_EQ(0, cluster.pool_delete(cache_pool_name.c_str()));
ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, cluster));
}
TEST_F(LibRadosTierECPP, HitSetNone) {
SKIP_IF_CRIMSON();
{
list< pair<time_t,time_t> > ls;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, ioctx.hit_set_list(123, c, &ls));
c->wait_for_complete();
ASSERT_EQ(0, c->get_return_value());
ASSERT_TRUE(ls.empty());
c->release();
}
{
bufferlist bl;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, ioctx.hit_set_get(123, c, 12345, &bl));
c->wait_for_complete();
ASSERT_EQ(-ENOENT, c->get_return_value());
c->release();
}
}
TEST_F(LibRadosTwoPoolsECPP, HitSetRead) {
SKIP_IF_CRIMSON();
// make it a tier
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
// enable hitset tracking for this pool
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_count", 2),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_type",
"explicit_object"),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
cache_ioctx.set_namespace("");
// keep reading until we see our object appear in the HitSet
utime_t start = ceph_clock_now();
utime_t hard_stop = start + utime_t(600, 0);
while (true) {
utime_t now = ceph_clock_now();
ASSERT_TRUE(now < hard_stop);
string name = "foo";
uint32_t hash;
ASSERT_EQ(0, cache_ioctx.get_object_hash_position2(name, &hash));
hobject_t oid(sobject_t(name, CEPH_NOSNAP), "", hash,
cluster.pool_lookup(cache_pool_name.c_str()), "");
bufferlist bl;
ASSERT_EQ(-ENOENT, cache_ioctx.read("foo", bl, 1, 0));
bufferlist hbl;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, cache_ioctx.hit_set_get(hash, c, now.sec(), &hbl));
c->wait_for_complete();
c->release();
if (hbl.length()) {
auto p = hbl.cbegin();
HitSet hs;
decode(hs, p);
if (hs.contains(oid)) {
cout << "ok, hit_set contains " << oid << std::endl;
break;
}
cout << "hmm, not in HitSet yet" << std::endl;
} else {
cout << "hmm, no HitSet yet" << std::endl;
}
sleep(1);
}
}
// disable this test until hitset-get reliably works on EC pools
#if 0
TEST_F(LibRadosTierECPP, HitSetWrite) {
int num_pg = _get_pg_num(cluster, pool_name);
ceph_assert(num_pg > 0);
// enable hitset tracking for this pool
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(set_pool_str(pool_name, "hit_set_count", 8),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(pool_name, "hit_set_type",
"explicit_hash"),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
ioctx.set_namespace("");
// do a bunch of writes
for (int i=0; i<1000; ++i) {
bufferlist bl;
bl.append("a");
ASSERT_EQ(0, ioctx.write(stringify(i), bl, 1, 0));
}
// get HitSets
std::map<int,HitSet> hitsets;
for (int i=0; i<num_pg; ++i) {
list< pair<time_t,time_t> > ls;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, ioctx.hit_set_list(i, c, &ls));
c->wait_for_complete();
c->release();
std::cout << "pg " << i << " ls " << ls << std::endl;
ASSERT_FALSE(ls.empty());
// get the latest
c = librados::Rados::aio_create_completion();
bufferlist bl;
ASSERT_EQ(0, ioctx.hit_set_get(i, c, ls.back().first, &bl));
c->wait_for_complete();
c->release();
//std::cout << "bl len is " << bl.length() << "\n";
//bl.hexdump(std::cout);
//std::cout << std::endl;
auto p = bl.cbegin();
decode(hitsets[i], p);
// cope with racing splits by refreshing pg_num
if (i == num_pg - 1)
num_pg = _get_pg_num(cluster, pool_name);
}
for (int i=0; i<1000; ++i) {
string n = stringify(i);
uint32_t hash = ioctx.get_object_hash_position(n);
hobject_t oid(sobject_t(n, CEPH_NOSNAP), "", hash,
cluster.pool_lookup(pool_name.c_str()), "");
std::cout << "checking for " << oid << std::endl;
bool found = false;
for (int p=0; p<num_pg; ++p) {
if (hitsets[p].contains(oid)) {
found = true;
break;
}
}
ASSERT_TRUE(found);
}
}
#endif
TEST_F(LibRadosTwoPoolsECPP, HitSetTrim) {
SKIP_IF_CRIMSON();
unsigned count = 3;
unsigned period = 3;
// make it a tier
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
// enable hitset tracking for this pool
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_count", count),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_period", period),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_type", "bloom"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(set_pool_str(cache_pool_name, "hit_set_fpp", ".01"),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
cache_ioctx.set_namespace("");
// do a bunch of writes and make sure the hitsets rotate
utime_t start = ceph_clock_now();
utime_t hard_stop = start + utime_t(count * period * 50, 0);
time_t first = 0;
int bsize = alignment;
char *buf = (char *)new char[bsize];
memset(buf, 'f', bsize);
while (true) {
string name = "foo";
uint32_t hash;
ASSERT_EQ(0, cache_ioctx.get_object_hash_position2(name, &hash));
hobject_t oid(sobject_t(name, CEPH_NOSNAP), "", hash, -1, "");
bufferlist bl;
bl.append(buf, bsize);
ASSERT_EQ(0, cache_ioctx.append("foo", bl, bsize));
list<pair<time_t, time_t> > ls;
AioCompletion *c = librados::Rados::aio_create_completion();
ASSERT_EQ(0, cache_ioctx.hit_set_list(hash, c, &ls));
c->wait_for_complete();
c->release();
cout << " got ls " << ls << std::endl;
if (!ls.empty()) {
if (!first) {
first = ls.front().first;
cout << "first is " << first << std::endl;
} else {
if (ls.front().first != first) {
cout << "first now " << ls.front().first << ", trimmed" << std::endl;
break;
}
}
}
utime_t now = ceph_clock_now();
ASSERT_TRUE(now < hard_stop);
sleep(1);
}
delete[] buf;
}
TEST_F(LibRadosTwoPoolsECPP, PromoteOn2ndRead) {
SKIP_IF_CRIMSON();
// create object
for (int i=0; i<20; ++i) {
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo" + stringify(i), &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// enable hitset tracking for this pool
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_count", 2),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_type", "bloom"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "min_read_recency_for_promote", 1),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_grade_decay_rate", 20),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_search_last_n", 1),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
int fake = 0; // set this to non-zero to test spurious promotion,
// e.g. from thrashing
int attempt = 0;
string obj;
while (true) {
// 1st read, don't trigger a promote
obj = "foo" + stringify(attempt);
cout << obj << std::endl;
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read(obj.c_str(), bl, 1, 0));
if (--fake >= 0) {
sleep(1);
ASSERT_EQ(1, ioctx.read(obj.c_str(), bl, 1, 0));
sleep(1);
}
}
// verify the object is NOT present in the cache tier
{
bool found = false;
NObjectIterator it = cache_ioctx.nobjects_begin();
while (it != cache_ioctx.nobjects_end()) {
cout << " see " << it->get_oid() << std::endl;
if (it->get_oid() == string(obj.c_str())) {
found = true;
break;
}
++it;
}
if (!found)
break;
}
++attempt;
ASSERT_LE(attempt, 20);
cout << "hrm, object is present in cache on attempt " << attempt
<< ", retrying" << std::endl;
}
// Read until the object is present in the cache tier
cout << "verifying " << obj << " is eventually promoted" << std::endl;
while (true) {
bufferlist bl;
ASSERT_EQ(1, ioctx.read(obj.c_str(), bl, 1, 0));
bool there = false;
NObjectIterator it = cache_ioctx.nobjects_begin();
while (it != cache_ioctx.nobjects_end()) {
if (it->get_oid() == string(obj.c_str())) {
there = true;
break;
}
++it;
}
if (there)
break;
sleep(1);
}
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsECPP, ProxyRead) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"readproxy\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read and verify the object
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('h', bl[0]);
}
// Verify 10 times the object is NOT present in the cache tier
uint32_t i = 0;
while (i++ < 10) {
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
sleep(1);
}
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsECPP, CachePin) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("baz", &op));
}
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bam", &op));
}
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// read, trigger promote
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ(1, ioctx.read("bar", bl, 1, 0));
ASSERT_EQ(1, ioctx.read("baz", bl, 1, 0));
ASSERT_EQ(1, ioctx.read("bam", bl, 1, 0));
}
// verify the objects are present in the cache tier
{
NObjectIterator it = cache_ioctx.nobjects_begin();
ASSERT_TRUE(it != cache_ioctx.nobjects_end());
for (uint32_t i = 0; i < 4; i++) {
ASSERT_TRUE(it->get_oid() == string("foo") ||
it->get_oid() == string("bar") ||
it->get_oid() == string("baz") ||
it->get_oid() == string("bam"));
++it;
}
ASSERT_TRUE(it == cache_ioctx.nobjects_end());
}
// pin objects
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
{
ObjectWriteOperation op;
op.cache_pin();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("baz", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// enable agent
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_count", 2),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_type", "bloom"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "min_read_recency_for_promote", 1),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "target_max_objects", 1),
inbl, NULL, NULL));
sleep(10);
// Verify the pinned object 'foo' is not flushed/evicted
uint32_t count = 0;
while (true) {
bufferlist bl;
ASSERT_EQ(1, ioctx.read("baz", bl, 1, 0));
count = 0;
NObjectIterator it = cache_ioctx.nobjects_begin();
while (it != cache_ioctx.nobjects_end()) {
ASSERT_TRUE(it->get_oid() == string("foo") ||
it->get_oid() == string("bar") ||
it->get_oid() == string("baz") ||
it->get_oid() == string("bam"));
++count;
++it;
}
if (count == 2) {
ASSERT_TRUE(it->get_oid() == string("foo") ||
it->get_oid() == string("baz"));
break;
}
sleep(1);
}
// tear down tiers
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
"\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsECPP, SetRedirectRead) {
SKIP_IF_CRIMSON();
// create object
{
bufferlist bl;
bl.append("hi there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("bar", &op));
}
// configure tier
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
{
ObjectWriteOperation op;
op.set_redirect("bar", cache_ioctx, 0);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// read and verify the object
{
bufferlist bl;
ASSERT_EQ(1, ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('t', bl[0]);
}
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsECPP, SetChunkRead) {
SKIP_IF_CRIMSON();
// note: require >= mimic
{
bufferlist bl;
bl.append("there hi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("There hi");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// set_chunk
manifest_set_chunk(cluster, ioctx, cache_ioctx, 0, 4, "bar", "foo");
// promote
{
ObjectWriteOperation op;
op.tier_promote();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// read and verify the object
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('T', bl[0]);
}
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsECPP, ManifestPromoteRead) {
SKIP_IF_CRIMSON();
// note: require >= mimic
// create object
{
bufferlist bl;
bl.append("hiaa there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("base chunk");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, cache_ioctx.operate("foo-chunk", &op));
}
{
bufferlist bl;
bl.append("HIaa there");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
{
bufferlist bl;
bl.append("BASE CHUNK");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar-chunk", &op));
}
// set-redirect
{
ObjectWriteOperation op;
op.set_redirect("bar", ioctx, 0);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// set-chunk
manifest_set_chunk(cluster, ioctx, cache_ioctx, 0, 10, "bar-chunk", "foo-chunk");
// promote
{
ObjectWriteOperation op;
op.tier_promote();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// read and verify the object (redirect)
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("foo", bl, 1, 0));
ASSERT_EQ('H', bl[0]);
}
// promote
{
ObjectWriteOperation op;
op.tier_promote();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo-chunk", completion, &op));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// read and verify the object
{
bufferlist bl;
ASSERT_EQ(1, cache_ioctx.read("foo-chunk", bl, 1, 0));
ASSERT_EQ('B', bl[0]);
}
// wait for maps to settle before next test
cluster.wait_for_latest_osdmap();
}
TEST_F(LibRadosTwoPoolsECPP, TrySetDedupTier) {
SKIP_IF_CRIMSON();
// note: require >= mimic
bufferlist inbl;
ASSERT_EQ(-EOPNOTSUPP, cluster.mon_command(
set_pool_str(pool_name, "dedup_tier", cache_pool_name),
inbl, NULL, NULL));
}
TEST_F(LibRadosTwoPoolsPP, PropagateBaseTierError) {
SKIP_IF_CRIMSON();
// write object to base tier
bufferlist omap_bl;
encode(static_cast<uint32_t>(0U), omap_bl);
ObjectWriteOperation op1;
op1.omap_set({{"somekey", omap_bl}});
ASSERT_EQ(0, ioctx.operate("propagate-base-tier-error", &op1));
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_type", "bloom"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_count", 1),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "target_max_objects", 250),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// guarded op should fail so expect error to propagate to cache tier
bufferlist test_omap_bl;
encode(static_cast<uint32_t>(1U), test_omap_bl);
ObjectWriteOperation op2;
op2.omap_cmp({{"somekey", {test_omap_bl, CEPH_OSD_CMPXATTR_OP_EQ}}}, nullptr);
op2.omap_set({{"somekey", test_omap_bl}});
ASSERT_EQ(-ECANCELED, ioctx.operate("propagate-base-tier-error", &op2));
}
TEST_F(LibRadosTwoPoolsPP, HelloWriteReturn) {
SKIP_IF_CRIMSON();
// configure cache
bufferlist inbl;
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
"\", \"tierpool\": \"" + cache_pool_name +
"\", \"force_nonempty\": \"--force-nonempty\" }",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
"\", \"overlaypool\": \"" + cache_pool_name + "\"}",
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
"{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
"\", \"mode\": \"writeback\"}",
inbl, NULL, NULL));
// set things up such that the op would normally be proxied
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_count", 2),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_period", 600),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "hit_set_type",
"explicit_object"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "min_read_recency_for_promote",
"10000"),
inbl, NULL, NULL));
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// this *will* return data due to the RETURNVEC flag
{
bufferlist in, out;
int rval;
ObjectWriteOperation o;
o.exec("hello", "write_return_data", in, &out, &rval);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &o,
librados::OPERATION_RETURNVEC));
completion->wait_for_complete();
ASSERT_EQ(42, completion->get_return_value());
ASSERT_EQ(42, rval);
out.hexdump(std::cout);
ASSERT_EQ("you might see this", std::string(out.c_str(), out.length()));
}
// this will overflow because the return data is too big
{
bufferlist in, out;
int rval;
ObjectWriteOperation o;
o.exec("hello", "write_too_much_return_data", in, &out, &rval);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_operate("foo", completion, &o,
librados::OPERATION_RETURNVEC));
completion->wait_for_complete();
ASSERT_EQ(-EOVERFLOW, completion->get_return_value());
ASSERT_EQ(-EOVERFLOW, rval);
ASSERT_EQ("", std::string(out.c_str(), out.length()));
}
}
TEST_F(LibRadosTwoPoolsPP, TierFlushDuringUnsetDedupTier) {
SKIP_IF_CRIMSON();
// skip test if not yet octopus
if (_get_required_osd_release(cluster) < "octopus") {
cout << "cluster is not yet octopus, skipping test" << std::endl;
return;
}
bufferlist inbl;
// set dedup parameters without dedup_tier
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "fingerprint_algorithm", "sha1"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_chunk_algorithm", "fastcdc"),
inbl, NULL, NULL));
ASSERT_EQ(0, cluster.mon_command(
set_pool_str(cache_pool_name, "dedup_cdc_chunk_size", 1024),
inbl, NULL, NULL));
// create object
bufferlist gbl;
{
generate_buffer(1024*8, &gbl);
ObjectWriteOperation op;
op.write_full(gbl);
ASSERT_EQ(0, cache_ioctx.operate("foo", &op));
}
{
bufferlist bl;
bl.append("there hiHI");
ObjectWriteOperation op;
op.write_full(bl);
ASSERT_EQ(0, ioctx.operate("bar", &op));
}
// wait for maps to settle
cluster.wait_for_latest_osdmap();
// set-chunk to set manifest object
{
ObjectReadOperation op;
op.set_chunk(0, 2, ioctx, "bar", 0, CEPH_OSD_OP_FLAG_WITH_REFERENCE);
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate("foo", completion, &op,
librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(0, completion->get_return_value());
completion->release();
}
// flush to check if proper error is returned
{
ObjectReadOperation op;
op.tier_flush();
librados::AioCompletion *completion = cluster.aio_create_completion();
ASSERT_EQ(0, cache_ioctx.aio_operate(
"foo", completion, &op, librados::OPERATION_IGNORE_CACHE, NULL));
completion->wait_for_complete();
ASSERT_EQ(-EINVAL, completion->get_return_value());
completion->release();
}
}
| 263,965 | 27.368189 | 185 |
cc
|
null |
ceph-main/src/test/librados/watch_notify.cc
|
#include "include/rados/librados.h"
#include "include/rados/rados_types.h"
#include "test/librados/test.h"
#include "test/librados/TestCase.h"
#include "crimson_utils.h"
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include "gtest/gtest.h"
#include "include/encoding.h"
#include <set>
#include <map>
typedef RadosTestEC LibRadosWatchNotifyEC;
int notify_sleep = 0;
// notify
static sem_t sem;
static void watch_notify_test_cb(uint8_t opcode, uint64_t ver, void *arg)
{
std::cout << __func__ << std::endl;
sem_post(&sem);
}
class LibRadosWatchNotify : public RadosTest
{
protected:
// notify 2
bufferlist notify_bl;
std::set<uint64_t> notify_cookies;
rados_ioctx_t notify_io;
const char *notify_oid = nullptr;
int notify_err = 0;
rados_completion_t notify_comp;
static void watch_notify2_test_cb(void *arg,
uint64_t notify_id,
uint64_t cookie,
uint64_t notifier_gid,
void *data,
size_t data_len);
static void watch_notify2_test_errcb(void *arg, uint64_t cookie, int err);
static void watch_notify2_test_errcb_reconnect(void *arg, uint64_t cookie, int err);
static void watch_notify2_test_errcb_aio_reconnect(void *arg, uint64_t cookie, int err);
};
void LibRadosWatchNotify::watch_notify2_test_cb(void *arg,
uint64_t notify_id,
uint64_t cookie,
uint64_t notifier_gid,
void *data,
size_t data_len)
{
std::cout << __func__ << " from " << notifier_gid << " notify_id " << notify_id
<< " cookie " << cookie << std::endl;
ceph_assert(notifier_gid > 0);
auto thiz = reinterpret_cast<LibRadosWatchNotify*>(arg);
ceph_assert(thiz);
thiz->notify_cookies.insert(cookie);
thiz->notify_bl.clear();
thiz->notify_bl.append((char*)data, data_len);
if (notify_sleep)
sleep(notify_sleep);
thiz->notify_err = 0;
rados_notify_ack(thiz->notify_io, thiz->notify_oid, notify_id, cookie,
"reply", 5);
}
void LibRadosWatchNotify::watch_notify2_test_errcb(void *arg,
uint64_t cookie,
int err)
{
std::cout << __func__ << " cookie " << cookie << " err " << err << std::endl;
ceph_assert(cookie > 1000);
auto thiz = reinterpret_cast<LibRadosWatchNotify*>(arg);
ceph_assert(thiz);
thiz->notify_err = err;
}
void LibRadosWatchNotify::watch_notify2_test_errcb_reconnect(void *arg,
uint64_t cookie,
int err)
{
std::cout << __func__ << " cookie " << cookie << " err " << err << std::endl;
ceph_assert(cookie > 1000);
auto thiz = reinterpret_cast<LibRadosWatchNotify*>(arg);
ceph_assert(thiz);
thiz->notify_err = rados_unwatch2(thiz->ioctx, cookie);
thiz->notify_cookies.erase(cookie); //delete old cookie
thiz->notify_err = rados_watch2(thiz->ioctx, thiz->notify_oid, &cookie,
watch_notify2_test_cb, watch_notify2_test_errcb_reconnect, thiz);
if (thiz->notify_err < 0) {
std::cout << __func__ << " reconnect watch failed with error " << thiz->notify_err << std::endl;
return;
}
return;
}
void LibRadosWatchNotify::watch_notify2_test_errcb_aio_reconnect(void *arg,
uint64_t cookie,
int err)
{
std::cout << __func__ << " cookie " << cookie << " err " << err << std::endl;
ceph_assert(cookie > 1000);
auto thiz = reinterpret_cast<LibRadosWatchNotify*>(arg);
ceph_assert(thiz);
thiz->notify_err = rados_aio_unwatch(thiz->ioctx, cookie, thiz->notify_comp);
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, &thiz->notify_comp));
thiz->notify_cookies.erase(cookie); //delete old cookie
thiz->notify_err = rados_aio_watch(thiz->ioctx, thiz->notify_oid, thiz->notify_comp, &cookie,
watch_notify2_test_cb, watch_notify2_test_errcb_aio_reconnect, thiz);
ASSERT_EQ(0, rados_aio_wait_for_complete(thiz->notify_comp));
ASSERT_EQ(0, rados_aio_get_return_value(thiz->notify_comp));
rados_aio_release(thiz->notify_comp);
if (thiz->notify_err < 0) {
std::cout << __func__ << " reconnect watch failed with error " << thiz->notify_err << std::endl;
return;
}
return;
}
class WatchNotifyTestCtx2;
// --
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
TEST_F(LibRadosWatchNotify, WatchNotify) {
ASSERT_EQ(0, sem_init(&sem, 0, 0));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
uint64_t handle;
ASSERT_EQ(0,
rados_watch(ioctx, "foo", 0, &handle, watch_notify_test_cb, NULL));
for (unsigned i=0; i<10; ++i) {
int r = rados_notify(ioctx, "foo", 0, NULL, 0);
if (r == 0) {
break;
}
if (!getenv("ALLOW_TIMEOUTS")) {
ASSERT_EQ(0, r);
}
}
TestAlarm alarm;
sem_wait(&sem);
rados_unwatch(ioctx, "foo", handle);
// when dne ...
ASSERT_EQ(-ENOENT,
rados_watch(ioctx, "dne", 0, &handle, watch_notify_test_cb, NULL));
sem_destroy(&sem);
}
TEST_F(LibRadosWatchNotifyEC, WatchNotify) {
SKIP_IF_CRIMSON();
ASSERT_EQ(0, sem_init(&sem, 0, 0));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
uint64_t handle;
ASSERT_EQ(0,
rados_watch(ioctx, "foo", 0, &handle, watch_notify_test_cb, NULL));
for (unsigned i=0; i<10; ++i) {
int r = rados_notify(ioctx, "foo", 0, NULL, 0);
if (r == 0) {
break;
}
if (!getenv("ALLOW_TIMEOUTS")) {
ASSERT_EQ(0, r);
}
}
TestAlarm alarm;
sem_wait(&sem);
rados_unwatch(ioctx, "foo", handle);
sem_destroy(&sem);
}
#pragma GCC diagnostic pop
#pragma GCC diagnostic warning "-Wpragmas"
// --
TEST_F(LibRadosWatchNotify, Watch2Delete) {
notify_io = ioctx;
notify_oid = "foo";
notify_err = 0;
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
uint64_t handle;
ASSERT_EQ(0,
rados_watch2(ioctx, notify_oid, &handle,
watch_notify2_test_cb,
watch_notify2_test_errcb, this));
ASSERT_EQ(0, rados_remove(ioctx, notify_oid));
int left = 300;
std::cout << "waiting up to " << left << " for disconnect notification ..."
<< std::endl;
while (notify_err == 0 && --left) {
sleep(1);
}
ASSERT_TRUE(left > 0);
ASSERT_EQ(-ENOTCONN, notify_err);
int rados_watch_check_err = rados_watch_check(ioctx, handle);
// We may hit ENOENT due to socket failure and a forced reconnect
EXPECT_TRUE(rados_watch_check_err == -ENOTCONN || rados_watch_check_err == -ENOENT)
<< "Where rados_watch_check_err = " << rados_watch_check_err;
rados_unwatch2(ioctx, handle);
rados_watch_flush(cluster);
}
TEST_F(LibRadosWatchNotify, AioWatchDelete) {
notify_io = ioctx;
notify_oid = "foo";
notify_err = 0;
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
rados_completion_t comp;
uint64_t handle;
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, &comp));
rados_aio_watch(ioctx, notify_oid, comp, &handle,
watch_notify2_test_cb, watch_notify2_test_errcb, this);
ASSERT_EQ(0, rados_aio_wait_for_complete(comp));
ASSERT_EQ(0, rados_aio_get_return_value(comp));
rados_aio_release(comp);
ASSERT_EQ(0, rados_remove(ioctx, notify_oid));
int left = 300;
std::cout << "waiting up to " << left << " for disconnect notification ..."
<< std::endl;
while (notify_err == 0 && --left) {
sleep(1);
}
ASSERT_TRUE(left > 0);
ASSERT_EQ(-ENOTCONN, notify_err);
int rados_watch_check_err = rados_watch_check(ioctx, handle);
// We may hit ENOENT due to socket failure injection and a forced reconnect
EXPECT_TRUE(rados_watch_check_err == -ENOTCONN || rados_watch_check_err == -ENOENT)
<< "Where rados_watch_check_err = " << rados_watch_check_err;
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, &comp));
rados_aio_unwatch(ioctx, handle, comp);
ASSERT_EQ(0, rados_aio_wait_for_complete(comp));
ASSERT_EQ(-ENOENT, rados_aio_get_return_value(comp));
rados_aio_release(comp);
}
// --
TEST_F(LibRadosWatchNotify, WatchNotify2) {
notify_io = ioctx;
notify_oid = "foo";
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
uint64_t handle;
ASSERT_EQ(0,
rados_watch2(ioctx, notify_oid, &handle,
watch_notify2_test_cb,
watch_notify2_test_errcb_reconnect, this));
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
char *reply_buf = 0;
size_t reply_buf_len;
ASSERT_EQ(0, rados_notify2(ioctx, notify_oid,
"notify", 6, 300000,
&reply_buf, &reply_buf_len));
bufferlist reply;
reply.append(reply_buf, reply_buf_len);
std::map<std::pair<uint64_t,uint64_t>, bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
auto reply_p = reply.cbegin();
decode(reply_map, reply_p);
decode(missed_map, reply_p);
ASSERT_EQ(1u, reply_map.size());
ASSERT_EQ(0u, missed_map.size());
ASSERT_EQ(1u, notify_cookies.size());
handle = *notify_cookies.begin();
ASSERT_EQ(1u, notify_cookies.count(handle));
ASSERT_EQ(5u, reply_map.begin()->second.length());
ASSERT_EQ(0, strncmp("reply", reply_map.begin()->second.c_str(), 5));
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
rados_buffer_free(reply_buf);
// try it on a non-existent object ... our buffer pointers
// should get zeroed.
ASSERT_EQ(-ENOENT, rados_notify2(ioctx, "doesnotexist",
"notify", 6, 300000,
&reply_buf, &reply_buf_len));
ASSERT_EQ((char*)0, reply_buf);
ASSERT_EQ(0u, reply_buf_len);
rados_unwatch2(ioctx, handle);
rados_watch_flush(cluster);
}
TEST_F(LibRadosWatchNotify, AioWatchNotify2) {
notify_io = ioctx;
notify_oid = "foo";
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
uint64_t handle;
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, ¬ify_comp));
rados_aio_watch(ioctx, notify_oid, notify_comp, &handle,
watch_notify2_test_cb, watch_notify2_test_errcb_aio_reconnect, this);
ASSERT_EQ(0, rados_aio_wait_for_complete(notify_comp));
ASSERT_EQ(0, rados_aio_get_return_value(notify_comp));
rados_aio_release(notify_comp);
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
char *reply_buf = 0;
size_t reply_buf_len;
ASSERT_EQ(0, rados_notify2(ioctx, notify_oid,
"notify", 6, 300000,
&reply_buf, &reply_buf_len));
bufferlist reply;
reply.append(reply_buf, reply_buf_len);
std::map<std::pair<uint64_t,uint64_t>, bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
auto reply_p = reply.cbegin();
decode(reply_map, reply_p);
decode(missed_map, reply_p);
ASSERT_EQ(1u, reply_map.size());
ASSERT_EQ(0u, missed_map.size());
ASSERT_EQ(1u, notify_cookies.size());
handle = *notify_cookies.begin();
ASSERT_EQ(1u, notify_cookies.count(handle));
ASSERT_EQ(5u, reply_map.begin()->second.length());
ASSERT_EQ(0, strncmp("reply", reply_map.begin()->second.c_str(), 5));
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
rados_buffer_free(reply_buf);
// try it on a non-existent object ... our buffer pointers
// should get zeroed.
ASSERT_EQ(-ENOENT, rados_notify2(ioctx, "doesnotexist",
"notify", 6, 300000,
&reply_buf, &reply_buf_len));
ASSERT_EQ((char*)0, reply_buf);
ASSERT_EQ(0u, reply_buf_len);
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, ¬ify_comp));
rados_aio_unwatch(ioctx, handle, notify_comp);
ASSERT_EQ(0, rados_aio_wait_for_complete(notify_comp));
ASSERT_EQ(0, rados_aio_get_return_value(notify_comp));
rados_aio_release(notify_comp);
}
TEST_F(LibRadosWatchNotify, AioNotify) {
notify_io = ioctx;
notify_oid = "foo";
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
uint64_t handle;
ASSERT_EQ(0,
rados_watch2(ioctx, notify_oid, &handle,
watch_notify2_test_cb,
watch_notify2_test_errcb, this));
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
char *reply_buf = 0;
size_t reply_buf_len;
rados_completion_t comp;
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, &comp));
ASSERT_EQ(0, rados_aio_notify(ioctx, "foo", comp, "notify", 6, 300000,
&reply_buf, &reply_buf_len));
ASSERT_EQ(0, rados_aio_wait_for_complete(comp));
ASSERT_EQ(0, rados_aio_get_return_value(comp));
rados_aio_release(comp);
size_t nr_acks, nr_timeouts;
notify_ack_t *acks = nullptr;
notify_timeout_t *timeouts = nullptr;
ASSERT_EQ(0, rados_decode_notify_response(reply_buf, reply_buf_len,
&acks, &nr_acks, &timeouts, &nr_timeouts));
ASSERT_EQ(1u, nr_acks);
ASSERT_EQ(0u, nr_timeouts);
ASSERT_EQ(1u, notify_cookies.size());
ASSERT_EQ(1u, notify_cookies.count(handle));
ASSERT_EQ(5u, acks[0].payload_len);
ASSERT_EQ(0, strncmp("reply", acks[0].payload, acks[0].payload_len));
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
rados_free_notify_response(acks, nr_acks, timeouts);
rados_buffer_free(reply_buf);
// try it on a non-existent object ... our buffer pointers
// should get zeroed.
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, &comp));
ASSERT_EQ(0, rados_aio_notify(ioctx, "doesnotexist", comp, "notify", 6,
300000, &reply_buf, &reply_buf_len));
ASSERT_EQ(0, rados_aio_wait_for_complete(comp));
ASSERT_EQ(-ENOENT, rados_aio_get_return_value(comp));
rados_aio_release(comp);
ASSERT_EQ((char*)0, reply_buf);
ASSERT_EQ(0u, reply_buf_len);
rados_unwatch2(ioctx, handle);
rados_watch_flush(cluster);
}
// --
TEST_F(LibRadosWatchNotify, WatchNotify2Multi) {
notify_io = ioctx;
notify_oid = "foo";
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
uint64_t handle1, handle2;
ASSERT_EQ(0,
rados_watch2(ioctx, notify_oid, &handle1,
watch_notify2_test_cb,
watch_notify2_test_errcb, this));
ASSERT_EQ(0,
rados_watch2(ioctx, notify_oid, &handle2,
watch_notify2_test_cb,
watch_notify2_test_errcb, this));
ASSERT_GT(rados_watch_check(ioctx, handle1), 0);
ASSERT_GT(rados_watch_check(ioctx, handle2), 0);
ASSERT_NE(handle1, handle2);
char *reply_buf = 0;
size_t reply_buf_len;
ASSERT_EQ(0, rados_notify2(ioctx, notify_oid,
"notify", 6, 300000,
&reply_buf, &reply_buf_len));
bufferlist reply;
reply.append(reply_buf, reply_buf_len);
std::map<std::pair<uint64_t,uint64_t>, bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
auto reply_p = reply.cbegin();
decode(reply_map, reply_p);
decode(missed_map, reply_p);
ASSERT_EQ(2u, reply_map.size());
ASSERT_EQ(5u, reply_map.begin()->second.length());
ASSERT_EQ(0u, missed_map.size());
ASSERT_EQ(2u, notify_cookies.size());
ASSERT_EQ(1u, notify_cookies.count(handle1));
ASSERT_EQ(1u, notify_cookies.count(handle2));
ASSERT_EQ(0, strncmp("reply", reply_map.begin()->second.c_str(), 5));
ASSERT_GT(rados_watch_check(ioctx, handle1), 0);
ASSERT_GT(rados_watch_check(ioctx, handle2), 0);
rados_buffer_free(reply_buf);
rados_unwatch2(ioctx, handle1);
rados_unwatch2(ioctx, handle2);
rados_watch_flush(cluster);
}
// --
TEST_F(LibRadosWatchNotify, WatchNotify2Timeout) {
notify_io = ioctx;
notify_oid = "foo";
notify_sleep = 3; // 3s
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
uint64_t handle;
ASSERT_EQ(0,
rados_watch2(ioctx, notify_oid, &handle,
watch_notify2_test_cb,
watch_notify2_test_errcb, this));
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
char *reply_buf = 0;
size_t reply_buf_len;
ASSERT_EQ(-ETIMEDOUT, rados_notify2(ioctx, notify_oid,
"notify", 6, 1000, // 1s
&reply_buf, &reply_buf_len));
ASSERT_EQ(1u, notify_cookies.size());
{
bufferlist reply;
reply.append(reply_buf, reply_buf_len);
std::map<std::pair<uint64_t,uint64_t>, bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
auto reply_p = reply.cbegin();
decode(reply_map, reply_p);
decode(missed_map, reply_p);
ASSERT_EQ(0u, reply_map.size());
ASSERT_EQ(1u, missed_map.size());
}
rados_buffer_free(reply_buf);
// we should get the next notify, though!
notify_sleep = 0;
notify_cookies.clear();
ASSERT_EQ(0, rados_notify2(ioctx, notify_oid,
"notify", 6, 300000, // 300s
&reply_buf, &reply_buf_len));
ASSERT_EQ(1u, notify_cookies.size());
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
rados_unwatch2(ioctx, handle);
rados_completion_t comp;
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, &comp));
rados_aio_watch_flush(cluster, comp);
ASSERT_EQ(0, rados_aio_wait_for_complete(comp));
ASSERT_EQ(0, rados_aio_get_return_value(comp));
rados_aio_release(comp);
rados_buffer_free(reply_buf);
}
TEST_F(LibRadosWatchNotify, Watch3Timeout) {
notify_io = ioctx;
notify_oid = "foo";
notify_cookies.clear();
notify_err = 0;
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
uint64_t handle;
time_t start = time(0);
const uint32_t timeout = 4;
{
// make sure i timeout before the messenger reconnects to the OSD,
// it will resend a watch request on behalf of the client, and the
// timer of timeout on OSD side will be reset by the new request.
char conf[128];
ASSERT_EQ(0, rados_conf_get(cluster,
"ms_connection_idle_timeout",
conf, sizeof(conf)));
auto connection_idle_timeout = std::stoll(conf);
ASSERT_LT(timeout, connection_idle_timeout);
}
ASSERT_EQ(0,
rados_watch3(ioctx, notify_oid, &handle,
watch_notify2_test_cb, watch_notify2_test_errcb,
timeout, this));
int age = rados_watch_check(ioctx, handle);
time_t age_bound = time(0) + 1 - start;
ASSERT_LT(age, age_bound * 1000);
ASSERT_GT(age, 0);
rados_conf_set(cluster, "objecter_inject_no_watch_ping", "true");
// allow a long time here since an osd peering event will renew our
// watch.
int left = 256 * timeout;
std::cout << "waiting up to " << left << " for osd to time us out ..."
<< std::endl;
while (notify_err == 0 && --left) {
sleep(1);
}
ASSERT_GT(left, 0);
rados_conf_set(cluster, "objecter_inject_no_watch_ping", "false");
ASSERT_EQ(-ENOTCONN, notify_err);
ASSERT_EQ(-ENOTCONN, rados_watch_check(ioctx, handle));
// a subsequent notify should not reach us
char *reply_buf = nullptr;
size_t reply_buf_len;
ASSERT_EQ(0, rados_notify2(ioctx, notify_oid,
"notify", 6, 300000,
&reply_buf, &reply_buf_len));
{
bufferlist reply;
reply.append(reply_buf, reply_buf_len);
std::map<std::pair<uint64_t,uint64_t>, bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
auto reply_p = reply.cbegin();
decode(reply_map, reply_p);
decode(missed_map, reply_p);
ASSERT_EQ(0u, reply_map.size());
ASSERT_EQ(0u, missed_map.size());
}
ASSERT_EQ(0u, notify_cookies.size());
ASSERT_EQ(-ENOTCONN, rados_watch_check(ioctx, handle));
rados_buffer_free(reply_buf);
// re-watch
rados_unwatch2(ioctx, handle);
rados_watch_flush(cluster);
handle = 0;
ASSERT_EQ(0,
rados_watch2(ioctx, notify_oid, &handle,
watch_notify2_test_cb,
watch_notify2_test_errcb, this));
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
// and now a notify will work.
ASSERT_EQ(0, rados_notify2(ioctx, notify_oid,
"notify", 6, 300000,
&reply_buf, &reply_buf_len));
{
bufferlist reply;
reply.append(reply_buf, reply_buf_len);
std::map<std::pair<uint64_t,uint64_t>, bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
auto reply_p = reply.cbegin();
decode(reply_map, reply_p);
decode(missed_map, reply_p);
ASSERT_EQ(1u, reply_map.size());
ASSERT_EQ(0u, missed_map.size());
ASSERT_EQ(1u, notify_cookies.count(handle));
ASSERT_EQ(5u, reply_map.begin()->second.length());
ASSERT_EQ(0, strncmp("reply", reply_map.begin()->second.c_str(), 5));
}
ASSERT_EQ(1u, notify_cookies.size());
ASSERT_GT(rados_watch_check(ioctx, handle), 0);
rados_buffer_free(reply_buf);
rados_unwatch2(ioctx, handle);
rados_watch_flush(cluster);
}
TEST_F(LibRadosWatchNotify, AioWatchDelete2) {
notify_io = ioctx;
notify_oid = "foo";
notify_err = 0;
char buf[128];
uint32_t timeout = 3;
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_write(ioctx, notify_oid, buf, sizeof(buf), 0));
rados_completion_t comp;
uint64_t handle;
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, &comp));
rados_aio_watch2(ioctx, notify_oid, comp, &handle,
watch_notify2_test_cb, watch_notify2_test_errcb, timeout, this);
ASSERT_EQ(0, rados_aio_wait_for_complete(comp));
ASSERT_EQ(0, rados_aio_get_return_value(comp));
rados_aio_release(comp);
ASSERT_EQ(0, rados_remove(ioctx, notify_oid));
int left = 30;
std::cout << "waiting up to " << left << " for disconnect notification ..."
<< std::endl;
while (notify_err == 0 && --left) {
sleep(1);
}
ASSERT_TRUE(left > 0);
ASSERT_EQ(-ENOTCONN, notify_err);
int rados_watch_check_err = rados_watch_check(ioctx, handle);
// We may hit ENOENT due to socket failure injection and a forced reconnect
EXPECT_TRUE(rados_watch_check_err == -ENOTCONN || rados_watch_check_err == -ENOENT)
<< "Where rados_watch_check_err = " << rados_watch_check_err;
ASSERT_EQ(0, rados_aio_create_completion2(nullptr, nullptr, &comp));
rados_aio_unwatch(ioctx, handle, comp);
ASSERT_EQ(0, rados_aio_wait_for_complete(comp));
ASSERT_EQ(-ENOENT, rados_aio_get_return_value(comp));
rados_aio_release(comp);
}
| 22,602 | 33.351064 | 100 |
cc
|
null |
ceph-main/src/test/librados/watch_notify_cxx.cc
|
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <set>
#include <map>
#include "gtest/gtest.h"
#include "include/encoding.h"
#include "include/rados/librados.hpp"
#include "include/rados/rados_types.h"
#include "test/librados/test_cxx.h"
#include "test/librados/testcase_cxx.h"
#include "crimson_utils.h"
using namespace librados;
typedef RadosTestECPP LibRadosWatchNotifyECPP;
int notify_sleep = 0;
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
class LibRadosWatchNotifyPP : public RadosTestParamPP
{
protected:
bufferlist notify_bl;
std::set<uint64_t> notify_cookies;
rados_ioctx_t notify_io;
const char *notify_oid = nullptr;
int notify_err = 0;
friend class WatchNotifyTestCtx2;
friend class WatchNotifyTestCtx2TimeOut;
};
IoCtx *notify_ioctx;
class WatchNotifyTestCtx2 : public WatchCtx2
{
LibRadosWatchNotifyPP *notify;
public:
WatchNotifyTestCtx2(LibRadosWatchNotifyPP *notify)
: notify(notify)
{}
void handle_notify(uint64_t notify_id, uint64_t cookie, uint64_t notifier_gid,
bufferlist& bl) override {
std::cout << __func__ << " cookie " << cookie << " notify_id " << notify_id
<< " notifier_gid " << notifier_gid << std::endl;
notify->notify_bl = bl;
notify->notify_cookies.insert(cookie);
bufferlist reply;
reply.append("reply", 5);
if (notify_sleep)
sleep(notify_sleep);
notify_ioctx->notify_ack(notify->notify_oid, notify_id, cookie, reply);
}
void handle_error(uint64_t cookie, int err) override {
std::cout << __func__ << " cookie " << cookie
<< " err " << err << std::endl;
ceph_assert(cookie > 1000);
notify_ioctx->unwatch2(cookie);
notify->notify_cookies.erase(cookie);
notify->notify_err = notify_ioctx->watch2(notify->notify_oid, &cookie, this);
if (notify->notify_err < err ) {
std::cout << "reconnect notify_err " << notify->notify_err << " err " << err << std::endl;
}
}
};
class WatchNotifyTestCtx2TimeOut : public WatchCtx2
{
LibRadosWatchNotifyPP *notify;
public:
WatchNotifyTestCtx2TimeOut(LibRadosWatchNotifyPP *notify)
: notify(notify)
{}
void handle_notify(uint64_t notify_id, uint64_t cookie, uint64_t notifier_gid,
bufferlist& bl) override {
std::cout << __func__ << " cookie " << cookie << " notify_id " << notify_id
<< " notifier_gid " << notifier_gid << std::endl;
notify->notify_bl = bl;
notify->notify_cookies.insert(cookie);
bufferlist reply;
reply.append("reply", 5);
if (notify_sleep)
sleep(notify_sleep);
notify_ioctx->notify_ack(notify->notify_oid, notify_id, cookie, reply);
}
void handle_error(uint64_t cookie, int err) override {
std::cout << __func__ << " cookie " << cookie
<< " err " << err << std::endl;
ceph_assert(cookie > 1000);
notify->notify_err = err;
}
};
// notify
static sem_t sem;
class WatchNotifyTestCtx : public WatchCtx
{
public:
void notify(uint8_t opcode, uint64_t ver, bufferlist& bl) override
{
std::cout << __func__ << std::endl;
sem_post(&sem);
}
};
TEST_P(LibRadosWatchNotifyPP, WatchNotify) {
ASSERT_EQ(0, sem_init(&sem, 0, 0));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write("foo", bl1, sizeof(buf), 0));
uint64_t handle;
WatchNotifyTestCtx ctx;
ASSERT_EQ(0, ioctx.watch("foo", 0, &handle, &ctx));
std::list<obj_watch_t> watches;
ASSERT_EQ(0, ioctx.list_watchers("foo", &watches));
ASSERT_EQ(1u, watches.size());
bufferlist bl2;
for (unsigned i=0; i<10; ++i) {
int r = ioctx.notify("foo", 0, bl2);
if (r == 0) {
break;
}
if (!getenv("ALLOW_TIMEOUTS")) {
ASSERT_EQ(0, r);
}
}
TestAlarm alarm;
sem_wait(&sem);
ioctx.unwatch("foo", handle);
sem_destroy(&sem);
}
TEST_F(LibRadosWatchNotifyECPP, WatchNotify) {
SKIP_IF_CRIMSON();
ASSERT_EQ(0, sem_init(&sem, 0, 0));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write("foo", bl1, sizeof(buf), 0));
uint64_t handle;
WatchNotifyTestCtx ctx;
ASSERT_EQ(0, ioctx.watch("foo", 0, &handle, &ctx));
std::list<obj_watch_t> watches;
ASSERT_EQ(0, ioctx.list_watchers("foo", &watches));
ASSERT_EQ(1u, watches.size());
bufferlist bl2;
for (unsigned i=0; i<10; ++i) {
int r = ioctx.notify("foo", 0, bl2);
if (r == 0) {
break;
}
if (!getenv("ALLOW_TIMEOUTS")) {
ASSERT_EQ(0, r);
}
}
TestAlarm alarm;
sem_wait(&sem);
ioctx.unwatch("foo", handle);
sem_destroy(&sem);
}
// --
TEST_P(LibRadosWatchNotifyPP, WatchNotifyTimeout) {
ASSERT_EQ(0, sem_init(&sem, 0, 0));
ioctx.set_notify_timeout(1);
uint64_t handle;
WatchNotifyTestCtx ctx;
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write("foo", bl1, sizeof(buf), 0));
ASSERT_EQ(0, ioctx.watch("foo", 0, &handle, &ctx));
sem_destroy(&sem);
ASSERT_EQ(0, ioctx.unwatch("foo", handle));
}
TEST_F(LibRadosWatchNotifyECPP, WatchNotifyTimeout) {
SKIP_IF_CRIMSON();
ASSERT_EQ(0, sem_init(&sem, 0, 0));
ioctx.set_notify_timeout(1);
uint64_t handle;
WatchNotifyTestCtx ctx;
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write("foo", bl1, sizeof(buf), 0));
ASSERT_EQ(0, ioctx.watch("foo", 0, &handle, &ctx));
sem_destroy(&sem);
ASSERT_EQ(0, ioctx.unwatch("foo", handle));
}
#pragma GCC diagnostic pop
#pragma GCC diagnostic warning "-Wpragmas"
TEST_P(LibRadosWatchNotifyPP, WatchNotify2) {
notify_oid = "foo";
notify_ioctx = &ioctx;
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write(notify_oid, bl1, sizeof(buf), 0));
uint64_t handle;
WatchNotifyTestCtx2 ctx(this);
ASSERT_EQ(0, ioctx.watch2(notify_oid, &handle, &ctx));
ASSERT_GT(ioctx.watch_check(handle), 0);
std::list<obj_watch_t> watches;
ASSERT_EQ(0, ioctx.list_watchers(notify_oid, &watches));
ASSERT_EQ(watches.size(), 1u);
bufferlist bl2, bl_reply;
ASSERT_EQ(0, ioctx.notify2(notify_oid, bl2, 300000, &bl_reply));
auto p = bl_reply.cbegin();
std::map<std::pair<uint64_t,uint64_t>,bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
decode(reply_map, p);
decode(missed_map, p);
ASSERT_EQ(1u, notify_cookies.size());
ASSERT_EQ(1u, notify_cookies.count(handle));
ASSERT_EQ(1u, reply_map.size());
ASSERT_EQ(5u, reply_map.begin()->second.length());
ASSERT_EQ(0, strncmp("reply", reply_map.begin()->second.c_str(), 5));
ASSERT_EQ(0u, missed_map.size());
ASSERT_GT(ioctx.watch_check(handle), 0);
ioctx.unwatch2(handle);
}
TEST_P(LibRadosWatchNotifyPP, AioWatchNotify2) {
notify_oid = "foo";
notify_ioctx = &ioctx;
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write(notify_oid, bl1, sizeof(buf), 0));
uint64_t handle;
WatchNotifyTestCtx2 ctx(this);
librados::AioCompletion *comp = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_watch(notify_oid, comp, &handle, &ctx));
ASSERT_EQ(0, comp->wait_for_complete());
ASSERT_EQ(0, comp->get_return_value());
comp->release();
ASSERT_GT(ioctx.watch_check(handle), 0);
std::list<obj_watch_t> watches;
ASSERT_EQ(0, ioctx.list_watchers(notify_oid, &watches));
ASSERT_EQ(watches.size(), 1u);
bufferlist bl2, bl_reply;
ASSERT_EQ(0, ioctx.notify2(notify_oid, bl2, 300000, &bl_reply));
auto p = bl_reply.cbegin();
std::map<std::pair<uint64_t,uint64_t>,bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
decode(reply_map, p);
decode(missed_map, p);
ASSERT_EQ(1u, notify_cookies.size());
ASSERT_EQ(1u, notify_cookies.count(handle));
ASSERT_EQ(1u, reply_map.size());
ASSERT_EQ(5u, reply_map.begin()->second.length());
ASSERT_EQ(0, strncmp("reply", reply_map.begin()->second.c_str(), 5));
ASSERT_EQ(0u, missed_map.size());
ASSERT_GT(ioctx.watch_check(handle), 0);
comp = cluster.aio_create_completion();
ioctx.aio_unwatch(handle, comp);
ASSERT_EQ(0, comp->wait_for_complete());
comp->release();
}
TEST_P(LibRadosWatchNotifyPP, AioNotify) {
notify_oid = "foo";
notify_ioctx = &ioctx;
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write(notify_oid, bl1, sizeof(buf), 0));
uint64_t handle;
WatchNotifyTestCtx2 ctx(this);
ASSERT_EQ(0, ioctx.watch2(notify_oid, &handle, &ctx));
ASSERT_GT(ioctx.watch_check(handle), 0);
std::list<obj_watch_t> watches;
ASSERT_EQ(0, ioctx.list_watchers(notify_oid, &watches));
ASSERT_EQ(watches.size(), 1u);
bufferlist bl2, bl_reply;
librados::AioCompletion *comp = cluster.aio_create_completion();
ASSERT_EQ(0, ioctx.aio_notify(notify_oid, comp, bl2, 300000, &bl_reply));
ASSERT_EQ(0, comp->wait_for_complete());
ASSERT_EQ(0, comp->get_return_value());
comp->release();
std::vector<librados::notify_ack_t> acks;
std::vector<librados::notify_timeout_t> timeouts;
ioctx.decode_notify_response(bl_reply, &acks, &timeouts);
ASSERT_EQ(1u, notify_cookies.size());
ASSERT_EQ(1u, notify_cookies.count(handle));
ASSERT_EQ(1u, acks.size());
ASSERT_EQ(5u, acks[0].payload_bl.length());
ASSERT_EQ(0, strncmp("reply", acks[0].payload_bl.c_str(), acks[0].payload_bl.length()));
ASSERT_EQ(0u, timeouts.size());
ASSERT_GT(ioctx.watch_check(handle), 0);
ioctx.unwatch2(handle);
cluster.watch_flush();
}
// --
TEST_P(LibRadosWatchNotifyPP, WatchNotify2Timeout) {
notify_oid = "foo";
notify_ioctx = &ioctx;
notify_sleep = 3; // 3s
notify_cookies.clear();
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write(notify_oid, bl1, sizeof(buf), 0));
uint64_t handle;
WatchNotifyTestCtx2TimeOut ctx(this);
ASSERT_EQ(0, ioctx.watch2(notify_oid, &handle, &ctx));
ASSERT_GT(ioctx.watch_check(handle), 0);
std::list<obj_watch_t> watches;
ASSERT_EQ(0, ioctx.list_watchers(notify_oid, &watches));
ASSERT_EQ(watches.size(), 1u);
ASSERT_EQ(0u, notify_cookies.size());
bufferlist bl2, bl_reply;
std::cout << " trying..." << std::endl;
ASSERT_EQ(-ETIMEDOUT, ioctx.notify2(notify_oid, bl2, 1000 /* 1s */,
&bl_reply));
std::cout << " timed out" << std::endl;
ASSERT_GT(ioctx.watch_check(handle), 0);
ioctx.unwatch2(handle);
std::cout << " flushing" << std::endl;
librados::AioCompletion *comp = cluster.aio_create_completion();
cluster.aio_watch_flush(comp);
ASSERT_EQ(0, comp->wait_for_complete());
ASSERT_EQ(0, comp->get_return_value());
std::cout << " flushed" << std::endl;
comp->release();
}
TEST_P(LibRadosWatchNotifyPP, WatchNotify3) {
notify_oid = "foo";
notify_ioctx = &ioctx;
notify_cookies.clear();
uint32_t timeout = 12; // configured timeout
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, ioctx.write(notify_oid, bl1, sizeof(buf), 0));
uint64_t handle;
WatchNotifyTestCtx2TimeOut ctx(this);
ASSERT_EQ(0, ioctx.watch3(notify_oid, &handle, &ctx, timeout));
ASSERT_GT(ioctx.watch_check(handle), 0);
std::list<obj_watch_t> watches;
ASSERT_EQ(0, ioctx.list_watchers(notify_oid, &watches));
ASSERT_EQ(watches.size(), 1u);
std::cout << "List watches" << std::endl;
for (std::list<obj_watch_t>::iterator it = watches.begin();
it != watches.end(); ++it) {
ASSERT_EQ(it->timeout_seconds, timeout);
}
bufferlist bl2, bl_reply;
std::cout << "notify2" << std::endl;
ASSERT_EQ(0, ioctx.notify2(notify_oid, bl2, 300000, &bl_reply));
std::cout << "notify2 done" << std::endl;
auto p = bl_reply.cbegin();
std::map<std::pair<uint64_t,uint64_t>,bufferlist> reply_map;
std::set<std::pair<uint64_t,uint64_t> > missed_map;
decode(reply_map, p);
decode(missed_map, p);
ASSERT_EQ(1u, notify_cookies.size());
ASSERT_EQ(1u, notify_cookies.count(handle));
ASSERT_EQ(1u, reply_map.size());
ASSERT_EQ(5u, reply_map.begin()->second.length());
ASSERT_EQ(0, strncmp("reply", reply_map.begin()->second.c_str(), 5));
ASSERT_EQ(0u, missed_map.size());
std::cout << "watch_check" << std::endl;
ASSERT_GT(ioctx.watch_check(handle), 0);
std::cout << "unwatch2" << std::endl;
ioctx.unwatch2(handle);
std::cout << " flushing" << std::endl;
cluster.watch_flush();
std::cout << "done" << std::endl;
}
// --
INSTANTIATE_TEST_SUITE_P(LibRadosWatchNotifyPPTests, LibRadosWatchNotifyPP,
::testing::Values("", "cache"));
| 12,835 | 29.781775 | 96 |
cc
|
null |
ceph-main/src/test/librados_test_stub/LibradosTestStub.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados_test_stub/LibradosTestStub.h"
#include "include/rados/librados.hpp"
#include "include/stringify.h"
#include "common/ceph_argparse.h"
#include "common/ceph_context.h"
#include "common/common_init.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/snap_types.h"
#include "librados/AioCompletionImpl.h"
#include "log/Log.h"
#include "test/librados_test_stub/TestClassHandler.h"
#include "test/librados_test_stub/TestIoCtxImpl.h"
#include "test/librados_test_stub/TestRadosClient.h"
#include "test/librados_test_stub/TestMemCluster.h"
#include "test/librados_test_stub/TestMemRadosClient.h"
#include "objclass/objclass.h"
#include "osd/osd_types.h"
#include <arpa/inet.h>
#include <boost/shared_ptr.hpp>
#include <deque>
#include <functional>
#include <list>
#include <vector>
#include "include/ceph_assert.h"
#include "include/compat.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rados
using namespace std;
namespace librados {
MockTestMemIoCtxImpl &get_mock_io_ctx(IoCtx &ioctx) {
MockTestMemIoCtxImpl **mock =
reinterpret_cast<MockTestMemIoCtxImpl **>(&ioctx);
return **mock;
}
} // namespace librados
namespace librados_test_stub {
TestClusterRef &cluster() {
static TestClusterRef s_cluster;
return s_cluster;
}
void set_cluster(TestClusterRef cluster_ref) {
cluster() = cluster_ref;
}
TestClusterRef get_cluster() {
auto &cluster_ref = cluster();
if (cluster_ref.get() == nullptr) {
cluster_ref.reset(new librados::TestMemCluster());
}
return cluster_ref;
}
librados::TestClassHandler *get_class_handler() {
static boost::shared_ptr<librados::TestClassHandler> s_class_handler;
if (!s_class_handler) {
s_class_handler.reset(new librados::TestClassHandler());
s_class_handler->open_all_classes();
}
return s_class_handler.get();
}
} // namespace librados_test_stub
namespace {
void do_out_buffer(bufferlist& outbl, char **outbuf, size_t *outbuflen) {
if (outbuf) {
if (outbl.length() > 0) {
*outbuf = (char *)malloc(outbl.length());
memcpy(*outbuf, outbl.c_str(), outbl.length());
} else {
*outbuf = NULL;
}
}
if (outbuflen) {
*outbuflen = outbl.length();
}
}
void do_out_buffer(string& outbl, char **outbuf, size_t *outbuflen) {
if (outbuf) {
if (outbl.length() > 0) {
*outbuf = (char *)malloc(outbl.length());
memcpy(*outbuf, outbl.c_str(), outbl.length());
} else {
*outbuf = NULL;
}
}
if (outbuflen) {
*outbuflen = outbl.length();
}
}
librados::TestRadosClient *create_rados_client() {
CephInitParameters iparams(CEPH_ENTITY_TYPE_CLIENT);
CephContext *cct = common_preinit(iparams, CODE_ENVIRONMENT_LIBRARY, 0);
cct->_conf.parse_env(cct->get_module_type());
cct->_conf.apply_changes(nullptr);
cct->_log->start();
auto rados_client =
librados_test_stub::get_cluster()->create_rados_client(cct);
cct->put();
return rados_client;
}
} // anonymous namespace
extern "C" int rados_aio_create_completion2(void *cb_arg,
rados_callback_t cb_complete,
rados_completion_t *pc)
{
librados::AioCompletionImpl *c = new librados::AioCompletionImpl;
if (cb_complete) {
c->set_complete_callback(cb_arg, cb_complete);
}
*pc = c;
return 0;
}
extern "C" int rados_aio_get_return_value(rados_completion_t c) {
return reinterpret_cast<librados::AioCompletionImpl*>(c)->get_return_value();
}
extern "C" rados_config_t rados_cct(rados_t cluster)
{
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
return reinterpret_cast<rados_config_t>(client->cct());
}
extern "C" int rados_conf_set(rados_t cluster, const char *option,
const char *value) {
librados::TestRadosClient *impl =
reinterpret_cast<librados::TestRadosClient*>(cluster);
CephContext *cct = impl->cct();
return cct->_conf.set_val(option, value);
}
extern "C" int rados_conf_parse_env(rados_t cluster, const char *var) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
auto& conf = client->cct()->_conf;
conf.parse_env(client->cct()->get_module_type(), var);
conf.apply_changes(NULL);
return 0;
}
extern "C" int rados_conf_read_file(rados_t cluster, const char *path) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
auto& conf = client->cct()->_conf;
int ret = conf.parse_config_files(path, NULL, 0);
if (ret == 0) {
conf.parse_env(client->cct()->get_module_type());
conf.apply_changes(NULL);
conf.complain_about_parse_error(client->cct());
} else if (ret == -ENOENT) {
// ignore missing client config
return 0;
}
return ret;
}
extern "C" int rados_connect(rados_t cluster) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
return client->connect();
}
extern "C" int rados_create(rados_t *cluster, const char * const id) {
*cluster = create_rados_client();
return 0;
}
extern "C" int rados_create_with_context(rados_t *cluster,
rados_config_t cct_) {
auto cct = reinterpret_cast<CephContext*>(cct_);
*cluster = librados_test_stub::get_cluster()->create_rados_client(cct);
return 0;
}
extern "C" rados_config_t rados_ioctx_cct(rados_ioctx_t ioctx)
{
librados::TestIoCtxImpl *ctx =
reinterpret_cast<librados::TestIoCtxImpl*>(ioctx);
return reinterpret_cast<rados_config_t>(ctx->get_rados_client()->cct());
}
extern "C" int rados_ioctx_create(rados_t cluster, const char *pool_name,
rados_ioctx_t *ioctx) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
int64_t pool_id = client->pool_lookup(pool_name);
if (pool_id < 0) {
return static_cast<int>(pool_id);
}
*ioctx = reinterpret_cast<rados_ioctx_t>(
client->create_ioctx(pool_id, pool_name));
return 0;
}
extern "C" int rados_ioctx_create2(rados_t cluster, int64_t pool_id,
rados_ioctx_t *ioctx)
{
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
std::list<std::pair<int64_t, std::string> > pools;
int r = client->pool_list(pools);
if (r < 0) {
return r;
}
for (std::list<std::pair<int64_t, std::string> >::iterator it =
pools.begin(); it != pools.end(); ++it) {
if (it->first == pool_id) {
*ioctx = reinterpret_cast<rados_ioctx_t>(
client->create_ioctx(pool_id, it->second));
return 0;
}
}
return -ENOENT;
}
extern "C" void rados_ioctx_destroy(rados_ioctx_t io) {
librados::TestIoCtxImpl *ctx =
reinterpret_cast<librados::TestIoCtxImpl*>(io);
ctx->put();
}
extern "C" rados_t rados_ioctx_get_cluster(rados_ioctx_t io) {
librados::TestIoCtxImpl *ctx =
reinterpret_cast<librados::TestIoCtxImpl*>(io);
return reinterpret_cast<rados_t>(ctx->get_rados_client());
}
extern "C" int rados_mon_command(rados_t cluster, const char **cmd,
size_t cmdlen, const char *inbuf,
size_t inbuflen, char **outbuf,
size_t *outbuflen, char **outs,
size_t *outslen) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
vector<string> cmdvec;
for (size_t i = 0; i < cmdlen; i++) {
cmdvec.push_back(cmd[i]);
}
bufferlist inbl;
inbl.append(inbuf, inbuflen);
bufferlist outbl;
string outstring;
int ret = client->mon_command(cmdvec, inbl, &outbl, &outstring);
do_out_buffer(outbl, outbuf, outbuflen);
do_out_buffer(outstring, outs, outslen);
return ret;
}
extern "C" int rados_nobjects_list_open(rados_ioctx_t io,
rados_list_ctx_t *ctx) {
librados::TestIoCtxImpl *io_ctx =
reinterpret_cast<librados::TestIoCtxImpl*>(io);
librados::TestRadosClient *client = io_ctx->get_rados_client();
std::list<librados::TestRadosClient::Object> *list =
new std::list<librados::TestRadosClient::Object>();
client->object_list(io_ctx->get_id(), list);
list->push_front(librados::TestRadosClient::Object());
*ctx = reinterpret_cast<rados_list_ctx_t>(list);
return 0;
}
extern "C" int rados_nobjects_list_next(rados_list_ctx_t ctx,
const char **entry,
const char **key,
const char **nspace) {
std::list<librados::TestRadosClient::Object> *list =
reinterpret_cast<std::list<librados::TestRadosClient::Object> *>(ctx);
if (!list->empty()) {
list->pop_front();
}
if (list->empty()) {
return -ENOENT;
}
librados::TestRadosClient::Object &obj = list->front();
if (entry != NULL) {
*entry = obj.oid.c_str();
}
if (key != NULL) {
*key = obj.locator.c_str();
}
if (nspace != NULL) {
*nspace = obj.nspace.c_str();
}
return 0;
}
extern "C" void rados_nobjects_list_close(rados_list_ctx_t ctx) {
std::list<librados::TestRadosClient::Object> *list =
reinterpret_cast<std::list<librados::TestRadosClient::Object> *>(ctx);
delete list;
}
extern "C" int rados_pool_create(rados_t cluster, const char *pool_name) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
return client->pool_create(pool_name);
}
extern "C" int rados_pool_delete(rados_t cluster, const char *pool_name) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
return client->pool_delete(pool_name);
}
extern "C" void rados_shutdown(rados_t cluster) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
client->put();
}
extern "C" int rados_wait_for_latest_osdmap(rados_t cluster) {
librados::TestRadosClient *client =
reinterpret_cast<librados::TestRadosClient*>(cluster);
return client->wait_for_latest_osdmap();
}
using namespace std::placeholders;
namespace librados {
AioCompletion::~AioCompletion()
{
auto c = reinterpret_cast<AioCompletionImpl *>(pc);
c->release();
}
void AioCompletion::release() {
delete this;
}
IoCtx::IoCtx() : io_ctx_impl(NULL) {
}
IoCtx::~IoCtx() {
close();
}
IoCtx::IoCtx(const IoCtx& rhs) {
io_ctx_impl = rhs.io_ctx_impl;
if (io_ctx_impl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->get();
}
}
IoCtx::IoCtx(IoCtx&& rhs) noexcept : io_ctx_impl(std::exchange(rhs.io_ctx_impl, nullptr))
{
}
IoCtx& IoCtx::operator=(const IoCtx& rhs) {
if (io_ctx_impl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->put();
}
io_ctx_impl = rhs.io_ctx_impl;
if (io_ctx_impl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->get();
}
return *this;
}
librados::IoCtx& librados::IoCtx::operator=(IoCtx&& rhs) noexcept
{
if (io_ctx_impl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->put();
}
io_ctx_impl = std::exchange(rhs.io_ctx_impl, nullptr);
return *this;
}
int IoCtx::aio_flush() {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->aio_flush();
return 0;
}
int IoCtx::aio_flush_async(AioCompletion *c) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->aio_flush_async(c->pc);
return 0;
}
int IoCtx::aio_notify(const std::string& oid, AioCompletion *c, bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->aio_notify(oid, c->pc, bl, timeout_ms, pbl);
return 0;
}
int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
ObjectReadOperation *op, bufferlist *pbl) {
return aio_operate(oid, c, op, 0, pbl);
}
int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
ObjectReadOperation *op, int flags,
bufferlist *pbl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
return ctx->aio_operate_read(oid, *ops, c->pc, flags, pbl,
ctx->get_snap_read(), nullptr);
}
int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
ObjectReadOperation *op, int flags,
bufferlist *pbl, const blkin_trace_info *trace_info) {
return aio_operate(oid, c, op, flags, pbl);
}
int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
ObjectWriteOperation *op) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
return ctx->aio_operate(oid, *ops, c->pc, nullptr, nullptr, 0);
}
int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
ObjectWriteOperation *op, snap_t seq,
std::vector<snap_t>& snaps, int flags,
const blkin_trace_info *trace_info) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
std::vector<snapid_t> snv;
snv.resize(snaps.size());
for (size_t i = 0; i < snaps.size(); ++i)
snv[i] = snaps[i];
SnapContext snapc(seq, snv);
return ctx->aio_operate(oid, *ops, c->pc, &snapc, nullptr, flags);
}
int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
ObjectWriteOperation *op, snap_t seq,
std::vector<snap_t>& snaps) {
return aio_operate(oid, c, op, seq, snaps, 0, nullptr);
}
int IoCtx::aio_operate(const std::string& oid, AioCompletion *c,
ObjectWriteOperation *op, snap_t seq,
std::vector<snap_t>& snaps,
const blkin_trace_info *trace_info) {
return aio_operate(oid, c, op, seq, snaps, 0, trace_info);
}
int IoCtx::aio_remove(const std::string& oid, AioCompletion *c) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->aio_remove(oid, c->pc);
}
int IoCtx::aio_remove(const std::string& oid, AioCompletion *c, int flags) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->aio_remove(oid, c->pc, flags);
}
int IoCtx::aio_watch(const std::string& o, AioCompletion *c, uint64_t *handle,
librados::WatchCtx2 *watch_ctx) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->aio_watch(o, c->pc, handle, watch_ctx);
}
int IoCtx::aio_unwatch(uint64_t handle, AioCompletion *c) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->aio_unwatch(handle, c->pc);
}
config_t IoCtx::cct() {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return reinterpret_cast<config_t>(ctx->get_rados_client()->cct());
}
void IoCtx::close() {
if (io_ctx_impl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->put();
}
io_ctx_impl = NULL;
}
int IoCtx::create(const std::string& oid, bool exclusive) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::create, _1, _2, exclusive,
ctx->get_snap_context()));
}
void IoCtx::dup(const IoCtx& rhs) {
close();
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(rhs.io_ctx_impl);
io_ctx_impl = reinterpret_cast<IoCtxImpl*>(ctx->clone());
}
int IoCtx::exec(const std::string& oid, const char *cls, const char *method,
bufferlist& inbl, bufferlist& outbl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::exec, _1, _2,
librados_test_stub::get_class_handler(), cls,
method, inbl, &outbl, ctx->get_snap_read(),
ctx->get_snap_context()));
}
void IoCtx::from_rados_ioctx_t(rados_ioctx_t p, IoCtx &io) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(p);
ctx->get();
io.close();
io.io_ctx_impl = reinterpret_cast<IoCtxImpl*>(ctx);
}
uint64_t IoCtx::get_instance_id() const {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->get_instance_id();
}
int64_t IoCtx::get_id() {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->get_id();
}
uint64_t IoCtx::get_last_version() {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->get_last_version();
}
std::string IoCtx::get_pool_name() {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->get_pool_name();
}
int IoCtx::list_snaps(const std::string& o, snap_set_t *out_snaps) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
o, std::bind(&TestIoCtxImpl::list_snaps, _1, _2, out_snaps));
}
int IoCtx::list_watchers(const std::string& o,
std::list<obj_watch_t> *out_watchers) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
o, std::bind(&TestIoCtxImpl::list_watchers, _1, _2, out_watchers));
}
int IoCtx::notify(const std::string& o, uint64_t ver, bufferlist& bl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->notify(o, bl, 0, NULL);
}
int IoCtx::notify2(const std::string& o, bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->notify(o, bl, timeout_ms, pbl);
}
void IoCtx::notify_ack(const std::string& o, uint64_t notify_id,
uint64_t handle, bufferlist& bl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->notify_ack(o, notify_id, handle, bl);
}
int IoCtx::omap_get_vals(const std::string& oid,
const std::string& start_after,
uint64_t max_return,
std::map<std::string, bufferlist> *out_vals) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::omap_get_vals, _1, _2, start_after, "",
max_return, out_vals));
}
int IoCtx::operate(const std::string& oid, ObjectWriteOperation *op) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
return ctx->operate(oid, *ops);
}
int IoCtx::operate(const std::string& oid, ObjectReadOperation *op,
bufferlist *pbl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
TestObjectOperationImpl *ops = reinterpret_cast<TestObjectOperationImpl*>(op->impl);
return ctx->operate_read(oid, *ops, pbl);
}
int IoCtx::read(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::read, _1, _2, len, off, &bl,
ctx->get_snap_read(), nullptr));
}
int IoCtx::remove(const std::string& oid) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::remove, _1, _2, ctx->get_snap_context()));
}
int IoCtx::selfmanaged_snap_create(uint64_t *snapid) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->selfmanaged_snap_create(snapid);
}
void IoCtx::aio_selfmanaged_snap_create(uint64_t *snapid, AioCompletion* c) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->aio_selfmanaged_snap_create(snapid, c->pc);
}
int IoCtx::selfmanaged_snap_remove(uint64_t snapid) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->selfmanaged_snap_remove(snapid);
}
void IoCtx::aio_selfmanaged_snap_remove(uint64_t snapid, AioCompletion* c) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->aio_selfmanaged_snap_remove(snapid, c->pc);
}
int IoCtx::selfmanaged_snap_rollback(const std::string& oid,
uint64_t snapid) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->selfmanaged_snap_rollback(oid, snapid);
}
int IoCtx::selfmanaged_snap_set_write_ctx(snap_t seq,
std::vector<snap_t>& snaps) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->selfmanaged_snap_set_write_ctx(seq, snaps);
}
void IoCtx::snap_set_read(snap_t seq) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->set_snap_read(seq);
}
int IoCtx::sparse_read(const std::string& oid, std::map<uint64_t,uint64_t>& m,
bufferlist& bl, size_t len, uint64_t off) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::sparse_read, _1, _2, off, len, &m, &bl,
ctx->get_snap_read()));
}
int IoCtx::stat(const std::string& oid, uint64_t *psize, time_t *pmtime) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::stat, _1, _2, psize, pmtime));
}
int IoCtx::tmap_update(const std::string& oid, bufferlist& cmdbl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::tmap_update, _1, _2, cmdbl));
}
int IoCtx::trunc(const std::string& oid, uint64_t off) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::truncate, _1, _2, off,
ctx->get_snap_context()));
}
int IoCtx::unwatch2(uint64_t handle) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->unwatch(handle);
}
int IoCtx::unwatch(const std::string& o, uint64_t handle) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->unwatch(handle);
}
int IoCtx::watch(const std::string& o, uint64_t ver, uint64_t *handle,
librados::WatchCtx *wctx) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->watch(o, handle, wctx, NULL);
}
int IoCtx::watch2(const std::string& o, uint64_t *handle,
librados::WatchCtx2 *wctx) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->watch(o, handle, NULL, wctx);
}
int IoCtx::write(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::write, _1, _2, bl, len, off,
ctx->get_snap_context()));
}
int IoCtx::write_full(const std::string& oid, bufferlist& bl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::write_full, _1, _2, bl,
ctx->get_snap_context()));
}
int IoCtx::writesame(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::writesame, _1, _2, bl, len, off,
ctx->get_snap_context()));
}
int IoCtx::cmpext(const std::string& oid, uint64_t off, bufferlist& cmp_bl) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->execute_operation(
oid, std::bind(&TestIoCtxImpl::cmpext, _1, _2, off, cmp_bl,
ctx->get_snap_read()));
}
int IoCtx::application_enable(const std::string& app_name, bool force) {
return 0;
}
int IoCtx::application_enable_async(const std::string& app_name,
bool force, PoolAsyncCompletion *c) {
return -EOPNOTSUPP;
}
int IoCtx::application_list(std::set<std::string> *app_names) {
return -EOPNOTSUPP;
}
int IoCtx::application_metadata_get(const std::string& app_name,
const std::string &key,
std::string *value) {
return -EOPNOTSUPP;
}
int IoCtx::application_metadata_set(const std::string& app_name,
const std::string &key,
const std::string& value) {
return -EOPNOTSUPP;
}
int IoCtx::application_metadata_remove(const std::string& app_name,
const std::string &key) {
return -EOPNOTSUPP;
}
int IoCtx::application_metadata_list(const std::string& app_name,
std::map<std::string, std::string> *values) {
return -EOPNOTSUPP;
}
void IoCtx::set_namespace(const std::string& nspace) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
ctx->set_namespace(nspace);
}
std::string IoCtx::get_namespace() const {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(io_ctx_impl);
return ctx->get_namespace();
}
void IoCtx::set_pool_full_try() {
}
bool IoCtx::get_pool_full_try() {
return false;
}
static int save_operation_result(int result, int *pval) {
if (pval != NULL) {
*pval = result;
}
return result;
}
ObjectOperation::ObjectOperation() {
TestObjectOperationImpl *o = new TestObjectOperationImpl();
o->get();
impl = reinterpret_cast<ObjectOperationImpl*>(o);
}
ObjectOperation::~ObjectOperation() {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
if (o) {
o->put();
o = NULL;
}
}
void ObjectOperation::assert_exists() {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::assert_exists, _1, _2, _4));
}
void ObjectOperation::assert_version(uint64_t ver) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::assert_version, _1, _2, ver));
}
void ObjectOperation::exec(const char *cls, const char *method,
bufferlist& inbl) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::exec, _1, _2,
librados_test_stub::get_class_handler(), cls,
method, inbl, _3, _4, _5));
}
void ObjectOperation::set_op_flags2(int flags) {
}
size_t ObjectOperation::size() {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
return o->ops.size();
}
void ObjectOperation::cmpext(uint64_t off, const bufferlist& cmp_bl,
int *prval) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
ObjectOperationTestImpl op = std::bind(&TestIoCtxImpl::cmpext, _1, _2, off,
cmp_bl, _4);
if (prval != NULL) {
op = std::bind(save_operation_result,
std::bind(op, _1, _2, _3, _4, _5, _6), prval);
}
o->ops.push_back(op);
}
void ObjectReadOperation::list_snaps(snap_set_t *out_snaps, int *prval) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
ObjectOperationTestImpl op = std::bind(&TestIoCtxImpl::list_snaps, _1, _2,
out_snaps);
if (prval != NULL) {
op = std::bind(save_operation_result,
std::bind(op, _1, _2, _3, _4, _5, _6), prval);
}
o->ops.push_back(op);
}
void ObjectReadOperation::list_watchers(std::list<obj_watch_t> *out_watchers,
int *prval) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
ObjectOperationTestImpl op = std::bind(&TestIoCtxImpl::list_watchers, _1,
_2, out_watchers);
if (prval != NULL) {
op = std::bind(save_operation_result,
std::bind(op, _1, _2, _3, _4, _5, _6), prval);
}
o->ops.push_back(op);
}
void ObjectReadOperation::read(size_t off, uint64_t len, bufferlist *pbl,
int *prval) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
ObjectOperationTestImpl op;
if (pbl != NULL) {
op = std::bind(&TestIoCtxImpl::read, _1, _2, len, off, pbl, _4, nullptr);
} else {
op = std::bind(&TestIoCtxImpl::read, _1, _2, len, off, _3, _4, nullptr);
}
if (prval != NULL) {
op = std::bind(save_operation_result,
std::bind(op, _1, _2, _3, _4, _5, _6), prval);
}
o->ops.push_back(op);
}
void ObjectReadOperation::sparse_read(uint64_t off, uint64_t len,
std::map<uint64_t,uint64_t> *m,
bufferlist *pbl, int *prval,
uint64_t truncate_size,
uint32_t truncate_seq) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
ObjectOperationTestImpl op;
if (pbl != NULL) {
op = std::bind(&TestIoCtxImpl::sparse_read, _1, _2, off, len, m, pbl, _4);
} else {
op = std::bind(&TestIoCtxImpl::sparse_read, _1, _2, off, len, m, _3, _4);
}
if (prval != NULL) {
op = std::bind(save_operation_result,
std::bind(op, _1, _2, _3, _4, _5, _6), prval);
}
o->ops.push_back(op);
}
void ObjectReadOperation::stat(uint64_t *psize, time_t *pmtime, int *prval) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
ObjectOperationTestImpl op = std::bind(&TestIoCtxImpl::stat, _1, _2,
psize, pmtime);
if (prval != NULL) {
op = std::bind(save_operation_result,
std::bind(op, _1, _2, _3, _4, _5, _6), prval);
}
o->ops.push_back(op);
}
void ObjectWriteOperation::append(const bufferlist &bl) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::append, _1, _2, bl, _5));
}
void ObjectWriteOperation::create(bool exclusive) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::create, _1, _2, exclusive, _5));
}
void ObjectWriteOperation::omap_set(const std::map<std::string, bufferlist> &map) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::omap_set, _1, _2, boost::ref(map)));
}
void ObjectWriteOperation::remove() {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::remove, _1, _2, _5));
}
void ObjectWriteOperation::selfmanaged_snap_rollback(uint64_t snapid) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::selfmanaged_snap_rollback,
_1, _2, snapid));
}
void ObjectWriteOperation::set_alloc_hint(uint64_t expected_object_size,
uint64_t expected_write_size) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::set_alloc_hint, _1, _2,
expected_object_size, expected_write_size, 0,
_5));
}
void ObjectWriteOperation::set_alloc_hint2(uint64_t expected_object_size,
uint64_t expected_write_size,
uint32_t flags) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::set_alloc_hint, _1, _2,
expected_object_size, expected_write_size, flags,
_5));
}
void ObjectWriteOperation::tmap_update(const bufferlist& cmdbl) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::tmap_update, _1, _2,
cmdbl));
}
void ObjectWriteOperation::truncate(uint64_t off) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::truncate, _1, _2, off, _5));
}
void ObjectWriteOperation::write(uint64_t off, const bufferlist& bl) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::write, _1, _2, bl, bl.length(),
off, _5));
}
void ObjectWriteOperation::write_full(const bufferlist& bl) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::write_full, _1, _2, bl, _5));
}
void ObjectWriteOperation::writesame(uint64_t off, uint64_t len,
const bufferlist& bl) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::writesame, _1, _2, bl, len,
off, _5));
}
void ObjectWriteOperation::zero(uint64_t off, uint64_t len) {
TestObjectOperationImpl *o = reinterpret_cast<TestObjectOperationImpl*>(impl);
o->ops.push_back(std::bind(&TestIoCtxImpl::zero, _1, _2, off, len, _5));
}
Rados::Rados() : client(NULL) {
}
Rados::Rados(IoCtx& ioctx) {
TestIoCtxImpl *ctx = reinterpret_cast<TestIoCtxImpl*>(ioctx.io_ctx_impl);
TestRadosClient *impl = ctx->get_rados_client();
impl->get();
client = reinterpret_cast<RadosClient*>(impl);
ceph_assert(client != NULL);
}
Rados::~Rados() {
shutdown();
}
void Rados::from_rados_t(rados_t p, Rados &rados) {
if (rados.client != nullptr) {
reinterpret_cast<TestRadosClient*>(rados.client)->put();
rados.client = nullptr;
}
auto impl = reinterpret_cast<TestRadosClient*>(p);
if (impl) {
impl->get();
rados.client = reinterpret_cast<RadosClient*>(impl);
}
}
AioCompletion *Rados::aio_create_completion(void *cb_arg,
callback_t cb_complete) {
AioCompletionImpl *c;
int r = rados_aio_create_completion2(cb_arg, cb_complete,
reinterpret_cast<void**>(&c));
ceph_assert(r == 0);
return new AioCompletion(c);
}
int Rados::aio_watch_flush(AioCompletion* c) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->aio_watch_flush(c->pc);
}
int Rados::blocklist_add(const std::string& client_address,
uint32_t expire_seconds) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->blocklist_add(client_address, expire_seconds);
}
config_t Rados::cct() {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return reinterpret_cast<config_t>(impl->cct());
}
int Rados::cluster_fsid(std::string* fsid) {
*fsid = "00000000-1111-2222-3333-444444444444";
return 0;
}
int Rados::conf_set(const char *option, const char *value) {
return rados_conf_set(reinterpret_cast<rados_t>(client), option, value);
}
int Rados::conf_get(const char *option, std::string &val) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
CephContext *cct = impl->cct();
char *str = NULL;
int ret = cct->_conf.get_val(option, &str, -1);
if (ret != 0) {
free(str);
return ret;
}
val = str;
free(str);
return 0;
}
int Rados::conf_parse_env(const char *env) const {
return rados_conf_parse_env(reinterpret_cast<rados_t>(client), env);
}
int Rados::conf_read_file(const char * const path) const {
return rados_conf_read_file(reinterpret_cast<rados_t>(client), path);
}
int Rados::connect() {
return rados_connect(reinterpret_cast<rados_t>(client));
}
uint64_t Rados::get_instance_id() {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->get_instance_id();
}
int Rados::get_min_compatible_osd(int8_t* require_osd_release) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->get_min_compatible_osd(require_osd_release);
}
int Rados::get_min_compatible_client(int8_t* min_compat_client,
int8_t* require_min_compat_client) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->get_min_compatible_client(min_compat_client,
require_min_compat_client);
}
int Rados::init(const char * const id) {
return rados_create(reinterpret_cast<rados_t *>(&client), id);
}
int Rados::init_with_context(config_t cct_) {
return rados_create_with_context(reinterpret_cast<rados_t *>(&client), cct_);
}
int Rados::ioctx_create(const char *name, IoCtx &io) {
rados_ioctx_t p;
int ret = rados_ioctx_create(reinterpret_cast<rados_t>(client), name, &p);
if (ret) {
return ret;
}
io.close();
io.io_ctx_impl = reinterpret_cast<IoCtxImpl*>(p);
return 0;
}
int Rados::ioctx_create2(int64_t pool_id, IoCtx &io)
{
rados_ioctx_t p;
int ret = rados_ioctx_create2(reinterpret_cast<rados_t>(client), pool_id, &p);
if (ret) {
return ret;
}
io.close();
io.io_ctx_impl = reinterpret_cast<IoCtxImpl*>(p);
return 0;
}
int Rados::mon_command(std::string cmd, const bufferlist& inbl,
bufferlist *outbl, std::string *outs) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
std::vector<std::string> cmds;
cmds.push_back(cmd);
return impl->mon_command(cmds, inbl, outbl, outs);
}
int Rados::service_daemon_register(const std::string& service,
const std::string& name,
const std::map<std::string,std::string>& metadata) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->service_daemon_register(service, name, metadata);
}
int Rados::service_daemon_update_status(std::map<std::string,std::string>&& status) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->service_daemon_update_status(std::move(status));
}
int Rados::pool_create(const char *name) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->pool_create(name);
}
int Rados::pool_delete(const char *name) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->pool_delete(name);
}
int Rados::pool_get_base_tier(int64_t pool, int64_t* base_tier) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->pool_get_base_tier(pool, base_tier);
}
int Rados::pool_list(std::list<std::string>& v) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
std::list<std::pair<int64_t, std::string> > pools;
int r = impl->pool_list(pools);
if (r < 0) {
return r;
}
v.clear();
for (std::list<std::pair<int64_t, std::string> >::iterator it = pools.begin();
it != pools.end(); ++it) {
v.push_back(it->second);
}
return 0;
}
int Rados::pool_list2(std::list<std::pair<int64_t, std::string> >& v)
{
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->pool_list(v);
}
int64_t Rados::pool_lookup(const char *name) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->pool_lookup(name);
}
int Rados::pool_reverse_lookup(int64_t id, std::string *name) {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->pool_reverse_lookup(id, name);
}
void Rados::shutdown() {
if (client == NULL) {
return;
}
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
impl->put();
client = NULL;
}
void Rados::test_blocklist_self(bool set) {
}
int Rados::wait_for_latest_osdmap() {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->wait_for_latest_osdmap();
}
int Rados::watch_flush() {
TestRadosClient *impl = reinterpret_cast<TestRadosClient*>(client);
return impl->watch_flush();
}
WatchCtx::~WatchCtx() {
}
WatchCtx2::~WatchCtx2() {
}
} // namespace librados
int cls_cxx_create(cls_method_context_t hctx, bool exclusive) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->create(ctx->oid, exclusive, ctx->snapc);
}
int cls_cxx_remove(cls_method_context_t hctx) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->remove(ctx->oid, ctx->io_ctx_impl->get_snap_context());
}
int cls_get_request_origin(cls_method_context_t hctx, entity_inst_t *origin) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
librados::TestRadosClient *rados_client =
ctx->io_ctx_impl->get_rados_client();
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = 0;
inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
entity_addr_t entity_addr(entity_addr_t::TYPE_DEFAULT,
rados_client->get_nonce());
entity_addr.in4_addr() = sin;
*origin = entity_inst_t(
entity_name_t::CLIENT(rados_client->get_instance_id()),
entity_addr);
return 0;
}
int cls_cxx_getxattr(cls_method_context_t hctx, const char *name,
bufferlist *outbl) {
std::map<string, bufferlist> attrs;
int r = cls_cxx_getxattrs(hctx, &attrs);
if (r < 0) {
return r;
}
std::map<string, bufferlist>::iterator it = attrs.find(name);
if (it == attrs.end()) {
return -ENODATA;
}
*outbl = it->second;
return 0;
}
int cls_cxx_getxattrs(cls_method_context_t hctx, std::map<string, bufferlist> *attrset) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->xattr_get(ctx->oid, attrset);
}
int cls_cxx_map_get_keys(cls_method_context_t hctx, const string &start_obj,
uint64_t max_to_get, std::set<string> *keys, bool *more) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
keys->clear();
std::map<string, bufferlist> vals;
int r = ctx->io_ctx_impl->omap_get_vals2(ctx->oid, start_obj, "", max_to_get,
&vals, more);
if (r < 0) {
return r;
}
for (std::map<string, bufferlist>::iterator it = vals.begin();
it != vals.end(); ++it) {
keys->insert(it->first);
}
return keys->size();
}
int cls_cxx_map_get_val(cls_method_context_t hctx, const string &key,
bufferlist *outbl) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
std::map<string, bufferlist> vals;
int r = ctx->io_ctx_impl->omap_get_vals(ctx->oid, "", key, 1024, &vals);
if (r < 0) {
return r;
}
std::map<string, bufferlist>::iterator it = vals.find(key);
if (it == vals.end()) {
return -ENOENT;
}
*outbl = it->second;
return 0;
}
int cls_cxx_map_get_vals(cls_method_context_t hctx, const string &start_obj,
const string &filter_prefix, uint64_t max_to_get,
std::map<string, bufferlist> *vals, bool *more) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
int r = ctx->io_ctx_impl->omap_get_vals2(ctx->oid, start_obj, filter_prefix,
max_to_get, vals, more);
if (r < 0) {
return r;
}
return vals->size();
}
int cls_cxx_map_remove_key(cls_method_context_t hctx, const string &key) {
std::set<std::string> keys;
keys.insert(key);
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->omap_rm_keys(ctx->oid, keys);
}
int cls_cxx_map_set_val(cls_method_context_t hctx, const string &key,
bufferlist *inbl) {
std::map<std::string, bufferlist> m;
m[key] = *inbl;
return cls_cxx_map_set_vals(hctx, &m);
}
int cls_cxx_map_set_vals(cls_method_context_t hctx,
const std::map<string, bufferlist> *map) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->omap_set(ctx->oid, *map);
}
int cls_cxx_read(cls_method_context_t hctx, int ofs, int len,
bufferlist *outbl) {
return cls_cxx_read2(hctx, ofs, len, outbl, 0);
}
int cls_cxx_read2(cls_method_context_t hctx, int ofs, int len,
bufferlist *outbl, uint32_t op_flags) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->read(
ctx->oid, len, ofs, outbl, ctx->snap_id, nullptr);
}
int cls_cxx_setxattr(cls_method_context_t hctx, const char *name,
bufferlist *inbl) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->xattr_set(ctx->oid, name, *inbl);
}
int cls_cxx_stat(cls_method_context_t hctx, uint64_t *size, time_t *mtime) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->stat(ctx->oid, size, mtime);
}
int cls_cxx_write(cls_method_context_t hctx, int ofs, int len,
bufferlist *inbl) {
return cls_cxx_write2(hctx, ofs, len, inbl, 0);
}
int cls_cxx_write2(cls_method_context_t hctx, int ofs, int len,
bufferlist *inbl, uint32_t op_flags) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->write(ctx->oid, *inbl, len, ofs, ctx->snapc);
}
int cls_cxx_write_full(cls_method_context_t hctx, bufferlist *inbl) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->write_full(ctx->oid, *inbl, ctx->snapc);
}
int cls_cxx_replace(cls_method_context_t hctx, int ofs, int len,
bufferlist *inbl) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
int r = ctx->io_ctx_impl->truncate(ctx->oid, 0, ctx->snapc);
if (r < 0) {
return r;
}
return ctx->io_ctx_impl->write(ctx->oid, *inbl, len, ofs, ctx->snapc);
}
int cls_cxx_truncate(cls_method_context_t hctx, int ofs) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->truncate(ctx->oid, ofs, ctx->snapc);
}
int cls_cxx_write_zero(cls_method_context_t hctx, int ofs, int len) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
return ctx->io_ctx_impl->zero(ctx->oid, len, ofs, ctx->snapc);
}
int cls_cxx_list_watchers(cls_method_context_t hctx,
obj_list_watch_response_t *watchers) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
std::list<obj_watch_t> obj_watchers;
int r = ctx->io_ctx_impl->list_watchers(ctx->oid, &obj_watchers);
if (r < 0) {
return r;
}
for (auto &w : obj_watchers) {
watch_item_t watcher;
watcher.name = entity_name_t::CLIENT(w.watcher_id);
watcher.cookie = w.cookie;
watcher.timeout_seconds = w.timeout_seconds;
watcher.addr.parse(w.addr);
watchers->entries.push_back(watcher);
}
return 0;
}
uint64_t cls_get_features(cls_method_context_t hctx) {
return CEPH_FEATURES_SUPPORTED_DEFAULT;
}
uint64_t cls_get_client_features(cls_method_context_t hctx) {
return CEPH_FEATURES_SUPPORTED_DEFAULT;
}
int cls_get_snapset_seq(cls_method_context_t hctx, uint64_t *snap_seq) {
librados::TestClassHandler::MethodContext *ctx =
reinterpret_cast<librados::TestClassHandler::MethodContext*>(hctx);
librados::snap_set_t snapset;
int r = ctx->io_ctx_impl->list_snaps(ctx->oid, &snapset);
if (r < 0) {
return r;
}
*snap_seq = snapset.seq;
return 0;
}
int cls_log(int level, const char *format, ...) {
int size = 256;
va_list ap;
while (1) {
char buf[size];
va_start(ap, format);
int n = vsnprintf(buf, size, format, ap);
va_end(ap);
if ((n > -1 && n < size) || size > 8196) {
dout(ceph::dout::need_dynamic(level)) << buf << dendl;
return n;
}
size *= 2;
}
return 0;
}
int cls_register(const char *name, cls_handle_t *handle) {
librados::TestClassHandler *cls = librados_test_stub::get_class_handler();
return cls->create(name, handle);
}
int cls_register_cxx_method(cls_handle_t hclass, const char *method,
int flags,
cls_method_cxx_call_t class_call,
cls_method_handle_t *handle) {
librados::TestClassHandler *cls = librados_test_stub::get_class_handler();
return cls->create_method(hclass, method, class_call, handle);
}
int cls_register_cxx_filter(cls_handle_t hclass,
const std::string &filter_name,
cls_cxx_filter_factory_t fn,
cls_filter_handle_t *)
{
librados::TestClassHandler *cls = librados_test_stub::get_class_handler();
return cls->create_filter(hclass, filter_name, fn);
}
ceph_release_t cls_get_required_osd_release(cls_handle_t hclass) {
return ceph_release_t::nautilus;
}
ceph_release_t cls_get_min_compatible_client(cls_handle_t hclass) {
return ceph_release_t::nautilus;
}
// stubs to silence TestClassHandler::open_class()
PGLSFilter::~PGLSFilter()
{}
int cls_gen_rand_base64(char *, int) {
return -ENOTSUP;
}
int cls_cxx_chunk_write_and_set(cls_method_handle_t, int,
int, bufferlist *,
uint32_t, bufferlist *, int) {
return -ENOTSUP;
}
int cls_cxx_map_read_header(cls_method_handle_t, bufferlist *) {
return -ENOTSUP;
}
uint64_t cls_get_osd_min_alloc_size(cls_method_context_t hctx) {
return 0;
}
uint64_t cls_get_pool_stripe_width(cls_method_context_t hctx) {
return 0;
}
| 51,065 | 31.546845 | 89 |
cc
|
null |
ceph-main/src/test/librados_test_stub/LibradosTestStub.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef LIBRADOS_TEST_STUB_H
#define LIBRADOS_TEST_STUB_H
#include "include/rados/librados_fwd.hpp"
#include <boost/shared_ptr.hpp>
namespace neorados {
struct IOContext;
struct RADOS;
} // namespace neorados
namespace librados {
class MockTestMemIoCtxImpl;
class MockTestMemRadosClient;
class TestCluster;
class TestClassHandler;
MockTestMemIoCtxImpl &get_mock_io_ctx(IoCtx &ioctx);
MockTestMemIoCtxImpl &get_mock_io_ctx(neorados::RADOS& rados,
neorados::IOContext& io_context);
MockTestMemRadosClient &get_mock_rados_client(neorados::RADOS& rados);
} // namespace librados
namespace librados_test_stub {
typedef boost::shared_ptr<librados::TestCluster> TestClusterRef;
void set_cluster(TestClusterRef cluster);
TestClusterRef get_cluster();
librados::TestClassHandler* get_class_handler();
} // namespace librados_test_stub
#endif // LIBRADOS_TEST_STUB_H
| 1,008 | 22.465116 | 71 |
h
|
null |
ceph-main/src/test/librados_test_stub/MockTestMemCluster.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef LIBRADOS_MOCK_TEST_MEM_CLUSTER_H
#define LIBRADOS_MOCK_TEST_MEM_CLUSTER_H
#include "include/common_fwd.h"
#include "test/librados_test_stub/TestMemCluster.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "gmock/gmock.h"
namespace librados {
class TestRadosClient;
class MockTestMemCluster : public TestMemCluster {
public:
MockTestMemCluster() {
default_to_dispatch();
}
MOCK_METHOD1(create_rados_client, TestRadosClient*(CephContext*));
MockTestMemRadosClient* do_create_rados_client(CephContext *cct) {
return new ::testing::NiceMock<MockTestMemRadosClient>(cct, this);
}
void default_to_dispatch() {
using namespace ::testing;
ON_CALL(*this, create_rados_client(_)).WillByDefault(Invoke(this, &MockTestMemCluster::do_create_rados_client));
}
};
} // namespace librados
#endif // LIBRADOS_MOCK_TEST_MEM_CLUSTER_H
| 984 | 25.621622 | 116 |
h
|
null |
ceph-main/src/test/librados_test_stub/MockTestMemIoCtxImpl.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef LIBRADOS_TEST_STUB_MOCK_TEST_MEM_IO_CTX_IMPL_H
#define LIBRADOS_TEST_STUB_MOCK_TEST_MEM_IO_CTX_IMPL_H
#include "test/librados_test_stub/TestMemIoCtxImpl.h"
#include "test/librados_test_stub/TestMemCluster.h"
#include "gmock/gmock.h"
namespace librados {
class MockTestMemRadosClient;
class MockTestMemIoCtxImpl : public TestMemIoCtxImpl {
public:
MockTestMemIoCtxImpl(MockTestMemRadosClient *mock_client,
TestMemRadosClient *client, int64_t pool_id,
const std::string& pool_name,
TestMemCluster::Pool *pool)
: TestMemIoCtxImpl(client, pool_id, pool_name, pool),
m_mock_client(mock_client), m_client(client) {
default_to_parent();
}
MockTestMemRadosClient *get_mock_rados_client() {
return m_mock_client;
}
MOCK_METHOD0(clone, TestIoCtxImpl*());
TestIoCtxImpl *do_clone() {
TestIoCtxImpl *io_ctx_impl = new ::testing::NiceMock<MockTestMemIoCtxImpl>(
m_mock_client, m_client, get_pool_id(), get_pool_name(), get_pool());
io_ctx_impl->set_snap_read(get_snap_read());
io_ctx_impl->set_snap_context(get_snap_context());
return io_ctx_impl;
}
MOCK_METHOD5(aio_notify, void(const std::string& o, AioCompletionImpl *c,
bufferlist& bl, uint64_t timeout_ms,
bufferlist *pbl));
void do_aio_notify(const std::string& o, AioCompletionImpl *c, bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl) {
return TestMemIoCtxImpl::aio_notify(o, c, bl, timeout_ms, pbl);
}
MOCK_METHOD6(aio_operate, int(const std::string&, TestObjectOperationImpl&,
AioCompletionImpl*, SnapContext*,
const ceph::real_time*, int));
int do_aio_operate(const std::string& o, TestObjectOperationImpl& ops,
AioCompletionImpl* c, SnapContext* snapc,
const ceph::real_time* pmtime, int flags) {
return TestMemIoCtxImpl::aio_operate(o, ops, c, snapc, pmtime, flags);
}
MOCK_METHOD4(aio_watch, int(const std::string& o, AioCompletionImpl *c,
uint64_t *handle, librados::WatchCtx2 *ctx));
int do_aio_watch(const std::string& o, AioCompletionImpl *c,
uint64_t *handle, librados::WatchCtx2 *ctx) {
return TestMemIoCtxImpl::aio_watch(o, c, handle, ctx);
}
MOCK_METHOD2(aio_unwatch, int(uint64_t handle, AioCompletionImpl *c));
int do_aio_unwatch(uint64_t handle, AioCompletionImpl *c) {
return TestMemIoCtxImpl::aio_unwatch(handle, c);
}
MOCK_METHOD2(assert_exists, int(const std::string &, uint64_t));
int do_assert_exists(const std::string &oid, uint64_t snap_id) {
return TestMemIoCtxImpl::assert_exists(oid, snap_id);
}
MOCK_METHOD2(assert_version, int(const std::string &, uint64_t));
int do_assert_version(const std::string &oid, uint64_t ver) {
return TestMemIoCtxImpl::assert_version(oid, ver);
}
MOCK_METHOD3(create, int(const std::string&, bool, const SnapContext &));
int do_create(const std::string& oid, bool exclusive,
const SnapContext &snapc) {
return TestMemIoCtxImpl::create(oid, exclusive, snapc);
}
MOCK_METHOD4(cmpext, int(const std::string&, uint64_t, bufferlist&,
uint64_t snap_id));
int do_cmpext(const std::string& oid, uint64_t off, bufferlist& cmp_bl,
uint64_t snap_id) {
return TestMemIoCtxImpl::cmpext(oid, off, cmp_bl, snap_id);
}
MOCK_METHOD8(exec, int(const std::string& oid,
TestClassHandler *handler,
const char *cls,
const char *method,
bufferlist& inbl,
bufferlist* outbl,
uint64_t snap_id,
const SnapContext &snapc));
int do_exec(const std::string& oid, TestClassHandler *handler,
const char *cls, const char *method, bufferlist& inbl,
bufferlist* outbl, uint64_t snap_id, const SnapContext &snapc) {
return TestMemIoCtxImpl::exec(oid, handler, cls, method, inbl, outbl,
snap_id, snapc);
}
MOCK_CONST_METHOD0(get_instance_id, uint64_t());
uint64_t do_get_instance_id() const {
return TestMemIoCtxImpl::get_instance_id();
}
MOCK_METHOD2(list_snaps, int(const std::string& o, snap_set_t *out_snaps));
int do_list_snaps(const std::string& o, snap_set_t *out_snaps) {
return TestMemIoCtxImpl::list_snaps(o, out_snaps);
}
MOCK_METHOD2(list_watchers, int(const std::string& o,
std::list<obj_watch_t> *out_watchers));
int do_list_watchers(const std::string& o,
std::list<obj_watch_t> *out_watchers) {
return TestMemIoCtxImpl::list_watchers(o, out_watchers);
}
MOCK_METHOD4(notify, int(const std::string& o, bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl));
int do_notify(const std::string& o, bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl) {
return TestMemIoCtxImpl::notify(o, bl, timeout_ms, pbl);
}
MOCK_METHOD1(set_snap_read, void(snap_t));
void do_set_snap_read(snap_t snap_id) {
return TestMemIoCtxImpl::set_snap_read(snap_id);
}
MOCK_METHOD6(sparse_read, int(const std::string& oid,
uint64_t off,
uint64_t len,
std::map<uint64_t, uint64_t> *m,
bufferlist *bl, uint64_t));
int do_sparse_read(const std::string& oid, uint64_t off, size_t len,
std::map<uint64_t, uint64_t> *m, bufferlist *bl,
uint64_t snap_id) {
return TestMemIoCtxImpl::sparse_read(oid, off, len, m, bl, snap_id);
}
MOCK_METHOD6(read, int(const std::string& oid,
size_t len,
uint64_t off,
bufferlist *bl, uint64_t snap_id, uint64_t* objver));
int do_read(const std::string& oid, size_t len, uint64_t off,
bufferlist *bl, uint64_t snap_id, uint64_t* objver) {
return TestMemIoCtxImpl::read(oid, len, off, bl, snap_id, objver);
}
MOCK_METHOD2(remove, int(const std::string& oid, const SnapContext &snapc));
int do_remove(const std::string& oid, const SnapContext &snapc) {
return TestMemIoCtxImpl::remove(oid, snapc);
}
MOCK_METHOD1(selfmanaged_snap_create, int(uint64_t *snap_id));
int do_selfmanaged_snap_create(uint64_t *snap_id) {
return TestMemIoCtxImpl::selfmanaged_snap_create(snap_id);
}
MOCK_METHOD1(selfmanaged_snap_remove, int(uint64_t snap_id));
int do_selfmanaged_snap_remove(uint64_t snap_id) {
return TestMemIoCtxImpl::selfmanaged_snap_remove(snap_id);
}
MOCK_METHOD2(selfmanaged_snap_rollback, int(const std::string& oid,
uint64_t snap_id));
int do_selfmanaged_snap_rollback(const std::string& oid, uint64_t snap_id) {
return TestMemIoCtxImpl::selfmanaged_snap_rollback(oid, snap_id);
}
MOCK_METHOD3(truncate, int(const std::string& oid,
uint64_t size,
const SnapContext &snapc));
int do_truncate(const std::string& oid, uint64_t size,
const SnapContext &snapc) {
return TestMemIoCtxImpl::truncate(oid, size, snapc);
}
MOCK_METHOD5(write, int(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off, const SnapContext &snapc));
int do_write(const std::string& oid, bufferlist& bl, size_t len, uint64_t off,
const SnapContext &snapc) {
return TestMemIoCtxImpl::write(oid, bl, len, off, snapc);
}
MOCK_METHOD3(write_full, int(const std::string& oid,
bufferlist& bl,
const SnapContext &snapc));
int do_write_full(const std::string& oid, bufferlist& bl,
const SnapContext &snapc) {
return TestMemIoCtxImpl::write_full(oid, bl, snapc);
}
MOCK_METHOD5(writesame, int(const std::string& oid, bufferlist& bl,
size_t len, uint64_t off,
const SnapContext &snapc));
int do_writesame(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off, const SnapContext &snapc) {
return TestMemIoCtxImpl::writesame(oid, bl, len, off, snapc);
}
MOCK_METHOD4(zero, int(const std::string& oid, uint64_t offset,
uint64_t length, const SnapContext &snapc));
int do_zero(const std::string& oid, uint64_t offset,
uint64_t length, const SnapContext &snapc) {
return TestMemIoCtxImpl::zero(oid, offset, length, snapc);
}
void default_to_parent() {
using namespace ::testing;
ON_CALL(*this, clone()).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_clone));
ON_CALL(*this, aio_notify(_, _, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_aio_notify));
ON_CALL(*this, aio_operate(_, _, _, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_aio_operate));
ON_CALL(*this, aio_watch(_, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_aio_watch));
ON_CALL(*this, aio_unwatch(_, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_aio_unwatch));
ON_CALL(*this, assert_exists(_, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_assert_exists));
ON_CALL(*this, assert_version(_, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_assert_version));
ON_CALL(*this, create(_, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_create));
ON_CALL(*this, cmpext(_, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_cmpext));
ON_CALL(*this, exec(_, _, _, _, _, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_exec));
ON_CALL(*this, get_instance_id()).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_get_instance_id));
ON_CALL(*this, list_snaps(_, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_list_snaps));
ON_CALL(*this, list_watchers(_, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_list_watchers));
ON_CALL(*this, notify(_, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_notify));
ON_CALL(*this, read(_, _, _, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_read));
ON_CALL(*this, set_snap_read(_)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_set_snap_read));
ON_CALL(*this, sparse_read(_, _, _, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_sparse_read));
ON_CALL(*this, remove(_, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_remove));
ON_CALL(*this, selfmanaged_snap_create(_)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_selfmanaged_snap_create));
ON_CALL(*this, selfmanaged_snap_remove(_)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_selfmanaged_snap_remove));
ON_CALL(*this, selfmanaged_snap_rollback(_, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_selfmanaged_snap_rollback));
ON_CALL(*this, truncate(_,_,_)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_truncate));
ON_CALL(*this, write(_, _, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_write));
ON_CALL(*this, write_full(_, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_write_full));
ON_CALL(*this, writesame(_, _, _, _, _)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_writesame));
ON_CALL(*this, zero(_,_,_,_)).WillByDefault(Invoke(this, &MockTestMemIoCtxImpl::do_zero));
}
private:
MockTestMemRadosClient *m_mock_client;
TestMemRadosClient *m_client;
};
} // namespace librados
#endif // LIBRADOS_TEST_STUB_MOCK_TEST_MEM_IO_CTX_IMPL_H
| 12,014 | 46.490119 | 133 |
h
|
null |
ceph-main/src/test/librados_test_stub/MockTestMemRadosClient.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef LIBRADOS_TEST_STUB_MOCK_TEST_MEM_RADOS_CLIENT_H
#define LIBRADOS_TEST_STUB_MOCK_TEST_MEM_RADOS_CLIENT_H
#include "test/librados_test_stub/TestMemRadosClient.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "gmock/gmock.h"
namespace librados {
class TestMemCluster;
class MockTestMemRadosClient : public TestMemRadosClient {
public:
MockTestMemRadosClient(CephContext *cct, TestMemCluster *test_mem_cluster)
: TestMemRadosClient(cct, test_mem_cluster) {
default_to_dispatch();
}
MOCK_METHOD0(connect, int());
int do_connect() {
return TestMemRadosClient::connect();
}
MOCK_METHOD2(create_ioctx, TestIoCtxImpl *(int64_t pool_id,
const std::string &pool_name));
MockTestMemIoCtxImpl* do_create_ioctx(int64_t pool_id,
const std::string &pool_name) {
return new ::testing::NiceMock<MockTestMemIoCtxImpl>(
this, this, pool_id, pool_name,
get_mem_cluster()->get_pool(pool_name));
}
MOCK_METHOD2(blocklist_add, int(const std::string& client_address,
uint32_t expire_seconds));
int do_blocklist_add(const std::string& client_address,
uint32_t expire_seconds) {
return TestMemRadosClient::blocklist_add(client_address, expire_seconds);
}
MOCK_METHOD1(get_min_compatible_osd, int(int8_t*));
int do_get_min_compatible_osd(int8_t* require_osd_release) {
return TestMemRadosClient::get_min_compatible_osd(require_osd_release);
}
MOCK_METHOD2(get_min_compatible_client, int(int8_t*, int8_t*));
int do_get_min_compatible_client(int8_t* min_compat_client,
int8_t* require_min_compat_client) {
return TestMemRadosClient::get_min_compatible_client(
min_compat_client, require_min_compat_client);
}
MOCK_METHOD3(service_daemon_register,
int(const std::string&,
const std::string&,
const std::map<std::string,std::string>&));
int do_service_daemon_register(const std::string& service,
const std::string& name,
const std::map<std::string,std::string>& metadata) {
return TestMemRadosClient::service_daemon_register(service, name, metadata);
}
// workaround of https://github.com/google/googletest/issues/1155
MOCK_METHOD1(service_daemon_update_status_r,
int(const std::map<std::string,std::string>&));
int do_service_daemon_update_status_r(const std::map<std::string,std::string>& status) {
auto s = status;
return TestMemRadosClient::service_daemon_update_status(std::move(s));
}
MOCK_METHOD4(mon_command, int(const std::vector<std::string>&,
const bufferlist&, bufferlist*, std::string*));
int do_mon_command(const std::vector<std::string>& cmd,
const bufferlist &inbl, bufferlist *outbl,
std::string *outs) {
return mon_command(cmd, inbl, outbl, outs);
}
MOCK_METHOD0(wait_for_latest_osd_map, int());
int do_wait_for_latest_osd_map() {
return wait_for_latest_osd_map();
}
void default_to_dispatch() {
using namespace ::testing;
ON_CALL(*this, connect()).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_connect));
ON_CALL(*this, create_ioctx(_, _)).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_create_ioctx));
ON_CALL(*this, blocklist_add(_, _)).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_blocklist_add));
ON_CALL(*this, get_min_compatible_osd(_)).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_get_min_compatible_osd));
ON_CALL(*this, get_min_compatible_client(_, _)).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_get_min_compatible_client));
ON_CALL(*this, service_daemon_register(_, _, _)).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_service_daemon_register));
ON_CALL(*this, service_daemon_update_status_r(_)).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_service_daemon_update_status_r));
ON_CALL(*this, mon_command(_, _, _, _)).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_mon_command));
ON_CALL(*this, wait_for_latest_osd_map()).WillByDefault(Invoke(this, &MockTestMemRadosClient::do_wait_for_latest_osd_map));
}
};
} // namespace librados
#endif // LIBRADOS_TEST_STUB_MOCK_TEST_MEM_RADOS_CLIENT_H
| 4,561 | 42.865385 | 142 |
h
|
null |
ceph-main/src/test/librados_test_stub/NeoradosTestStub.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/neorados/RADOS.hpp"
#include "include/rados/librados.hpp"
#include "common/ceph_mutex.h"
#include "common/hobject.h"
#include "librados/AioCompletionImpl.h"
#include "mon/error_code.h"
#include "osd/error_code.h"
#include "osd/osd_types.h"
#include "osdc/error_code.h"
#include "test/librados_test_stub/LibradosTestStub.h"
#include "test/librados_test_stub/TestClassHandler.h"
#include "test/librados_test_stub/TestIoCtxImpl.h"
#include "test/librados_test_stub/TestRadosClient.h"
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <functional>
#include <boost/system/system_error.hpp>
namespace bs = boost::system;
using namespace std::literals;
using namespace std::placeholders;
namespace neorados {
namespace detail {
class Client {
public:
ceph::mutex mutex = ceph::make_mutex("NeoradosTestStub::Client");
librados::TestRadosClient* test_rados_client;
boost::asio::io_context& io_context;
std::map<std::pair<int64_t, std::string>, librados::TestIoCtxImpl*> io_ctxs;
Client(librados::TestRadosClient* test_rados_client)
: test_rados_client(test_rados_client),
io_context(test_rados_client->get_io_context()) {
}
~Client() {
for (auto& io_ctx : io_ctxs) {
io_ctx.second->put();
}
}
librados::TestIoCtxImpl* get_io_ctx(const IOContext& ioc) {
int64_t pool_id = ioc.pool();
std::string ns = std::string{ioc.ns()};
auto lock = std::scoped_lock{mutex};
auto key = make_pair(pool_id, ns);
auto it = io_ctxs.find(key);
if (it != io_ctxs.end()) {
return it->second;
}
std::list<std::pair<int64_t, std::string>> pools;
int r = test_rados_client->pool_list(pools);
if (r < 0) {
return nullptr;
}
for (auto& pool : pools) {
if (pool.first == pool_id) {
auto io_ctx = test_rados_client->create_ioctx(pool_id, pool.second);
io_ctx->set_namespace(ns);
io_ctxs[key] = io_ctx;
return io_ctx;
}
}
return nullptr;
}
};
} // namespace detail
namespace {
struct CompletionPayload {
std::unique_ptr<Op::Completion> c;
};
void completion_callback_adapter(rados_completion_t c, void *arg) {
auto impl = reinterpret_cast<librados::AioCompletionImpl *>(c);
auto r = impl->get_return_value();
impl->release();
auto payload = reinterpret_cast<CompletionPayload*>(arg);
payload->c->defer(std::move(payload->c),
(r < 0) ? bs::error_code(-r, osd_category()) :
bs::error_code());
delete payload;
}
librados::AioCompletionImpl* create_aio_completion(
std::unique_ptr<Op::Completion>&& c) {
auto payload = new CompletionPayload{std::move(c)};
auto impl = new librados::AioCompletionImpl();
impl->set_complete_callback(payload, completion_callback_adapter);
return impl;
}
int save_operation_size(int result, size_t* pval) {
if (pval != NULL) {
*pval = result;
}
return result;
}
int save_operation_ec(int result, boost::system::error_code* ec) {
if (ec != NULL) {
*ec = {std::abs(result), bs::system_category()};
}
return result;
}
} // anonymous namespace
Object::Object() {
static_assert(impl_size >= sizeof(object_t));
new (&impl) object_t();
}
Object::Object(std::string&& s) {
static_assert(impl_size >= sizeof(object_t));
new (&impl) object_t(std::move(s));
}
Object::~Object() {
reinterpret_cast<object_t*>(&impl)->~object_t();
}
Object::operator std::string_view() const {
return std::string_view(reinterpret_cast<const object_t*>(&impl)->name);
}
struct IOContextImpl {
object_locator_t oloc;
snapid_t snap_seq = CEPH_NOSNAP;
SnapContext snapc;
};
IOContext::IOContext() {
static_assert(impl_size >= sizeof(IOContextImpl));
new (&impl) IOContextImpl();
}
IOContext::IOContext(const IOContext& rhs) {
static_assert(impl_size >= sizeof(IOContextImpl));
new (&impl) IOContextImpl(*reinterpret_cast<const IOContextImpl*>(&rhs.impl));
}
IOContext::IOContext(int64_t _pool, std::string&& _ns)
: IOContext() {
pool(_pool);
ns(std::move(_ns));
}
IOContext::~IOContext() {
reinterpret_cast<IOContextImpl*>(&impl)->~IOContextImpl();
}
std::int64_t IOContext::pool() const {
return reinterpret_cast<const IOContextImpl*>(&impl)->oloc.pool;
}
void IOContext::pool(std::int64_t _pool) {
reinterpret_cast<IOContextImpl*>(&impl)->oloc.pool = _pool;
}
std::string_view IOContext::ns() const {
return reinterpret_cast<const IOContextImpl*>(&impl)->oloc.nspace;
}
void IOContext::ns(std::string&& _ns) {
reinterpret_cast<IOContextImpl*>(&impl)->oloc.nspace = std::move(_ns);
}
std::optional<std::uint64_t> IOContext::read_snap() const {
auto& snap_seq = reinterpret_cast<const IOContextImpl*>(&impl)->snap_seq;
if (snap_seq == CEPH_NOSNAP)
return std::nullopt;
else
return snap_seq;
}
void IOContext::read_snap(std::optional<std::uint64_t> _snapid) {
auto& snap_seq = reinterpret_cast<IOContextImpl*>(&impl)->snap_seq;
snap_seq = _snapid.value_or(CEPH_NOSNAP);
}
std::optional<
std::pair<std::uint64_t,
std::vector<std::uint64_t>>> IOContext::write_snap_context() const {
auto& snapc = reinterpret_cast<const IOContextImpl*>(&impl)->snapc;
if (snapc.empty()) {
return std::nullopt;
} else {
std::vector<uint64_t> v(snapc.snaps.begin(), snapc.snaps.end());
return std::make_optional(std::make_pair(uint64_t(snapc.seq), v));
}
}
void IOContext::write_snap_context(
std::optional<std::pair<std::uint64_t, std::vector<std::uint64_t>>> _snapc) {
auto& snapc = reinterpret_cast<IOContextImpl*>(&impl)->snapc;
if (!_snapc) {
snapc.clear();
} else {
SnapContext n(_snapc->first, { _snapc->second.begin(), _snapc->second.end()});
if (!n.is_valid()) {
throw bs::system_error(EINVAL,
bs::system_category(),
"Invalid snap context.");
}
snapc = n;
}
}
void IOContext::full_try(bool _full_try) {
// no-op
}
bool operator ==(const IOContext& lhs, const IOContext& rhs) {
auto l = reinterpret_cast<const IOContextImpl*>(&lhs.impl);
auto r = reinterpret_cast<const IOContextImpl*>(&rhs.impl);
return (l->oloc == r->oloc &&
l->snap_seq == r->snap_seq &&
l->snapc.seq == r->snapc.seq &&
l->snapc.snaps == r->snapc.snaps);
}
bool operator !=(const IOContext& lhs, const IOContext& rhs) {
return !(lhs == rhs);
}
Op::Op() {
static_assert(Op::impl_size >= sizeof(librados::TestObjectOperationImpl*));
auto& o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o = new librados::TestObjectOperationImpl();
o->get();
}
Op::~Op() {
auto& o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
if (o != nullptr) {
o->put();
o = nullptr;
}
}
void Op::assert_exists() {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::assert_exists, _1, _2, _4));
}
void Op::assert_version(uint64_t ver) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::assert_version, _1, _2, ver));
}
void Op::cmpext(uint64_t off, ceph::buffer::list&& cmp_bl, std::size_t* s) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
librados::ObjectOperationTestImpl op = std::bind(
&librados::TestIoCtxImpl::cmpext, _1, _2, off, cmp_bl, _4);
if (s != nullptr) {
op = std::bind(
save_operation_size, std::bind(op, _1, _2, _3, _4, _5, _6), s);
}
o->ops.push_back(op);
}
std::size_t Op::size() const {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl* const *>(&impl);
return o->ops.size();
}
void Op::set_fadvise_random() {
// no-op
}
void Op::set_fadvise_sequential() {
// no-op
}
void Op::set_fadvise_willneed() {
// no-op
}
void Op::set_fadvise_dontneed() {
// no-op
}
void Op::set_fadvise_nocache() {
// no-op
}
void Op::balance_reads() {
// no-op
}
void Op::localize_reads() {
// no-op
}
void Op::exec(std::string_view cls, std::string_view method,
const ceph::buffer::list& inbl,
ceph::buffer::list* out,
boost::system::error_code* ec) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
auto cls_handler = librados_test_stub::get_class_handler();
librados::ObjectOperationTestImpl op =
[cls_handler, cls, method, inbl = const_cast<bufferlist&>(inbl), out]
(librados::TestIoCtxImpl* io_ctx, const std::string& oid, bufferlist* outbl,
uint64_t snap_id, const SnapContext& snapc, uint64_t*) mutable -> int {
return io_ctx->exec(
oid, cls_handler, std::string(cls).c_str(),
std::string(method).c_str(), inbl,
(out != nullptr ? out : outbl), snap_id, snapc);
};
if (ec != nullptr) {
op = std::bind(
save_operation_ec, std::bind(op, _1, _2, _3, _4, _5, _6), ec);
}
o->ops.push_back(op);
}
void Op::exec(std::string_view cls, std::string_view method,
const ceph::buffer::list& inbl,
boost::system::error_code* ec) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
auto cls_handler = librados_test_stub::get_class_handler();
librados::ObjectOperationTestImpl op =
[cls_handler, cls, method, inbl = const_cast<bufferlist&>(inbl)]
(librados::TestIoCtxImpl* io_ctx, const std::string& oid, bufferlist* outbl,
uint64_t snap_id, const SnapContext& snapc, uint64_t*) mutable -> int {
return io_ctx->exec(
oid, cls_handler, std::string(cls).c_str(),
std::string(method).c_str(), inbl, outbl, snap_id, snapc);
};
if (ec != NULL) {
op = std::bind(
save_operation_ec, std::bind(op, _1, _2, _3, _4, _5, _6), ec);
}
o->ops.push_back(op);
}
void ReadOp::read(size_t off, uint64_t len, ceph::buffer::list* out,
boost::system::error_code* ec) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
librados::ObjectOperationTestImpl op;
if (out != nullptr) {
op = std::bind(
&librados::TestIoCtxImpl::read, _1, _2, len, off, out, _4, _6);
} else {
op = std::bind(
&librados::TestIoCtxImpl::read, _1, _2, len, off, _3, _4, _6);
}
if (ec != NULL) {
op = std::bind(
save_operation_ec, std::bind(op, _1, _2, _3, _4, _5, _6), ec);
}
o->ops.push_back(op);
}
void ReadOp::sparse_read(uint64_t off, uint64_t len,
ceph::buffer::list* out,
std::vector<std::pair<std::uint64_t,
std::uint64_t>>* extents,
boost::system::error_code* ec) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
librados::ObjectOperationTestImpl op =
[off, len, out, extents]
(librados::TestIoCtxImpl* io_ctx, const std::string& oid, bufferlist* outbl,
uint64_t snap_id, const SnapContext& snapc, uint64_t*) mutable -> int {
std::map<uint64_t,uint64_t> m;
int r = io_ctx->sparse_read(
oid, off, len, &m, (out != nullptr ? out : outbl), snap_id);
if (r >= 0 && extents != nullptr) {
extents->clear();
extents->insert(extents->end(), m.begin(), m.end());
}
return r;
};
if (ec != NULL) {
op = std::bind(save_operation_ec,
std::bind(op, _1, _2, _3, _4, _5, _6), ec);
}
o->ops.push_back(op);
}
void ReadOp::list_snaps(SnapSet* snaps, bs::error_code* ec) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
librados::ObjectOperationTestImpl op =
[snaps]
(librados::TestIoCtxImpl* io_ctx, const std::string& oid, bufferlist*,
uint64_t, const SnapContext&, uint64_t*) mutable -> int {
librados::snap_set_t snap_set;
int r = io_ctx->list_snaps(oid, &snap_set);
if (r >= 0 && snaps != nullptr) {
*snaps = {};
snaps->seq = snap_set.seq;
snaps->clones.reserve(snap_set.clones.size());
for (auto& clone : snap_set.clones) {
neorados::CloneInfo clone_info;
clone_info.cloneid = clone.cloneid;
clone_info.snaps = clone.snaps;
clone_info.overlap = clone.overlap;
clone_info.size = clone.size;
snaps->clones.push_back(clone_info);
}
}
return r;
};
if (ec != NULL) {
op = std::bind(save_operation_ec,
std::bind(op, _1, _2, _3, _4, _5, _6), ec);
}
o->ops.push_back(op);
}
void WriteOp::create(bool exclusive) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::create, _1, _2, exclusive, _5));
}
void WriteOp::write(uint64_t off, ceph::buffer::list&& bl) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::write, _1, _2, bl, bl.length(), off, _5));
}
void WriteOp::write_full(ceph::buffer::list&& bl) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::write_full, _1, _2, bl, _5));
}
void WriteOp::remove() {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::remove, _1, _2, _5));
}
void WriteOp::truncate(uint64_t off) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::truncate, _1, _2, off, _5));
}
void WriteOp::zero(uint64_t off, uint64_t len) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::zero, _1, _2, off, len, _5));
}
void WriteOp::writesame(std::uint64_t off, std::uint64_t write_len,
ceph::buffer::list&& bl) {
auto o = *reinterpret_cast<librados::TestObjectOperationImpl**>(&impl);
o->ops.push_back(std::bind(
&librados::TestIoCtxImpl::writesame, _1, _2, bl, write_len, off, _5));
}
void WriteOp::set_alloc_hint(uint64_t expected_object_size,
uint64_t expected_write_size,
alloc_hint::alloc_hint_t flags) {
// no-op
}
RADOS::RADOS() = default;
RADOS::RADOS(RADOS&&) = default;
RADOS::RADOS(std::unique_ptr<detail::Client> impl)
: impl(std::move(impl)) {
}
RADOS::~RADOS() = default;
RADOS RADOS::make_with_librados(librados::Rados& rados) {
auto test_rados_client = reinterpret_cast<librados::TestRadosClient*>(
rados.client);
return RADOS{std::make_unique<detail::Client>(test_rados_client)};
}
CephContext* neorados::RADOS::cct() {
return impl->test_rados_client->cct();
}
boost::asio::io_context& neorados::RADOS::get_io_context() {
return impl->io_context;
}
boost::asio::io_context::executor_type neorados::RADOS::get_executor() const {
return impl->io_context.get_executor();
}
void RADOS::execute(const Object& o, const IOContext& ioc, ReadOp&& op,
ceph::buffer::list* bl, std::unique_ptr<Op::Completion> c,
uint64_t* objver, const blkin_trace_info* trace_info) {
auto io_ctx = impl->get_io_ctx(ioc);
if (io_ctx == nullptr) {
c->dispatch(std::move(c), osdc_errc::pool_dne);
return;
}
auto ops = *reinterpret_cast<librados::TestObjectOperationImpl**>(&op.impl);
auto snap_id = CEPH_NOSNAP;
auto opt_snap_id = ioc.read_snap();
if (opt_snap_id) {
snap_id = *opt_snap_id;
}
auto completion = create_aio_completion(std::move(c));
auto r = io_ctx->aio_operate_read(std::string{o}, *ops, completion, 0U, bl,
snap_id, objver);
ceph_assert(r == 0);
}
void RADOS::execute(const Object& o, const IOContext& ioc, WriteOp&& op,
std::unique_ptr<Op::Completion> c, uint64_t* objver,
const blkin_trace_info* trace_info) {
auto io_ctx = impl->get_io_ctx(ioc);
if (io_ctx == nullptr) {
c->dispatch(std::move(c), osdc_errc::pool_dne);
return;
}
auto ops = *reinterpret_cast<librados::TestObjectOperationImpl**>(&op.impl);
SnapContext snapc;
auto opt_snapc = ioc.write_snap_context();
if (opt_snapc) {
snapc.seq = opt_snapc->first;
snapc.snaps.assign(opt_snapc->second.begin(), opt_snapc->second.end());
}
auto completion = create_aio_completion(std::move(c));
auto r = io_ctx->aio_operate(std::string{o}, *ops, completion, &snapc, nullptr, 0U);
ceph_assert(r == 0);
}
void RADOS::mon_command(std::vector<std::string> command,
const bufferlist& bl,
std::string* outs, bufferlist* outbl,
std::unique_ptr<Op::Completion> c) {
auto r = impl->test_rados_client->mon_command(command, bl, outbl, outs);
c->post(std::move(c),
(r < 0 ? bs::error_code(-r, osd_category()) : bs::error_code()));
}
void RADOS::blocklist_add(std::string_view client_address,
std::optional<std::chrono::seconds> expire,
std::unique_ptr<SimpleOpComp> c) {
auto r = impl->test_rados_client->blocklist_add(
std::string(client_address), expire.value_or(0s).count());
c->post(std::move(c),
(r < 0 ? bs::error_code(-r, mon_category()) : bs::error_code()));
}
void RADOS::wait_for_latest_osd_map(std::unique_ptr<Op::Completion> c) {
auto r = impl->test_rados_client->wait_for_latest_osd_map();
c->dispatch(std::move(c),
(r < 0 ? bs::error_code(-r, osd_category()) :
bs::error_code()));
}
} // namespace neorados
namespace librados {
MockTestMemIoCtxImpl& get_mock_io_ctx(neorados::RADOS& rados,
neorados::IOContext& io_context) {
auto& impl = *reinterpret_cast<std::unique_ptr<neorados::detail::Client>*>(
&rados);
auto io_ctx = impl->get_io_ctx(io_context);
ceph_assert(io_ctx != nullptr);
return *reinterpret_cast<MockTestMemIoCtxImpl*>(io_ctx);
}
MockTestMemRadosClient& get_mock_rados_client(neorados::RADOS& rados) {
auto& impl = *reinterpret_cast<std::unique_ptr<neorados::detail::Client>*>(
&rados);
return *reinterpret_cast<MockTestMemRadosClient*>(impl->test_rados_client);
}
} // namespace librados
| 18,400 | 29.566445 | 86 |
cc
|
null |
ceph-main/src/test/librados_test_stub/TestClassHandler.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados_test_stub/TestClassHandler.h"
#include "test/librados_test_stub/TestIoCtxImpl.h"
#include <boost/algorithm/string/predicate.hpp>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "common/debug.h"
#include "include/ceph_assert.h"
#include "include/dlfcn_compat.h"
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rados
namespace librados {
TestClassHandler::TestClassHandler() {
}
TestClassHandler::~TestClassHandler() {
for (ClassHandles::iterator it = m_class_handles.begin();
it != m_class_handles.end(); ++it) {
dlclose(*it);
}
}
void TestClassHandler::open_class(const std::string& name,
const std::string& path) {
void *handle = dlopen(path.c_str(), RTLD_NOW);
if (handle == NULL) {
std::cerr << "Failed to load class: " << name << " (" << path << "): "
<< dlerror() << std::endl;
return;
}
// initialize
void (*cls_init)() = reinterpret_cast<void (*)()>(
dlsym(handle, "__cls_init"));
if (!cls_init) {
std::cerr << "Error locating initializer: " << dlerror() << std::endl;
} else if (cls_init) {
m_class_handles.push_back(handle);
cls_init();
return;
}
std::cerr << "Class: " << name << " (" << path << ") missing initializer"
<< std::endl;
dlclose(handle);
}
void TestClassHandler::open_all_classes() {
ceph_assert(m_class_handles.empty());
const char* env = getenv("CEPH_LIB");
std::string CEPH_LIB(env ? env : "lib");
DIR *dir = ::opendir(CEPH_LIB.c_str());
if (dir == NULL) {
ceph_abort();;
}
std::set<std::string> names;
struct dirent *pde = nullptr;
while ((pde = ::readdir(dir))) {
std::string name(pde->d_name);
if (!boost::algorithm::starts_with(name, "libcls_") ||
!boost::algorithm::ends_with(name, SHARED_LIB_SUFFIX)) {
continue;
}
names.insert(name);
}
for (auto& name : names) {
std::string class_name = name.substr(7, name.size() - 10);
open_class(class_name, CEPH_LIB + "/" + name);
}
closedir(dir);
}
int TestClassHandler::create(const std::string &name, cls_handle_t *handle) {
if (m_classes.find(name) != m_classes.end()) {
std::cerr << "Class " << name << " already exists" << std::endl;
return -EEXIST;
}
SharedClass cls(new Class());
m_classes[name] = cls;
*handle = reinterpret_cast<cls_handle_t>(cls.get());
return 0;
}
int TestClassHandler::create_method(cls_handle_t hclass,
const char *name,
cls_method_cxx_call_t class_call,
cls_method_handle_t *handle) {
Class *cls = reinterpret_cast<Class*>(hclass);
if (cls->methods.find(name) != cls->methods.end()) {
std::cerr << "Class method " << hclass << ":" << name << " already exists"
<< std::endl;
return -EEXIST;
}
SharedMethod method(new Method());
method->class_call = class_call;
cls->methods[name] = method;
return 0;
}
cls_method_cxx_call_t TestClassHandler::get_method(const std::string &cls,
const std::string &method) {
Classes::iterator c_it = m_classes.find(cls);
if (c_it == m_classes.end()) {
std::cerr << "Failed to located class " << cls << std::endl;
return NULL;
}
SharedClass scls = c_it->second;
Methods::iterator m_it = scls->methods.find(method);
if (m_it == scls->methods.end()) {
std::cerr << "Failed to located class method" << cls << "." << method
<< std::endl;
return NULL;
}
return m_it->second->class_call;
}
TestClassHandler::SharedMethodContext TestClassHandler::get_method_context(
TestIoCtxImpl *io_ctx_impl, const std::string &oid, uint64_t snap_id,
const SnapContext &snapc) {
SharedMethodContext ctx(new MethodContext());
// clone to ioctx to provide a firewall for gmock expectations
ctx->io_ctx_impl = io_ctx_impl->clone();
ctx->oid = oid;
ctx->snap_id = snap_id;
ctx->snapc = snapc;
return ctx;
}
int TestClassHandler::create_filter(cls_handle_t hclass,
const std::string& name,
cls_cxx_filter_factory_t fn)
{
Class *cls = reinterpret_cast<Class*>(hclass);
if (cls->filters.find(name) != cls->filters.end()) {
return -EEXIST;
}
cls->filters[name] = fn;
return 0;
}
TestClassHandler::MethodContext::~MethodContext() {
io_ctx_impl->put();
}
} // namespace librados
| 4,570 | 27.56875 | 79 |
cc
|
null |
ceph-main/src/test/librados_test_stub/TestClassHandler.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_CLASS_HANDLER_H
#define CEPH_TEST_CLASS_HANDLER_H
#include "objclass/objclass.h"
#include "common/snap_types.h"
#include <boost/shared_ptr.hpp>
#include <list>
#include <map>
#include <string>
namespace librados
{
class TestIoCtxImpl;
class TestClassHandler {
public:
TestClassHandler();
~TestClassHandler();
struct MethodContext {
~MethodContext();
TestIoCtxImpl *io_ctx_impl;
std::string oid;
uint64_t snap_id;
SnapContext snapc;
};
typedef boost::shared_ptr<MethodContext> SharedMethodContext;
struct Method {
cls_method_cxx_call_t class_call;
};
typedef boost::shared_ptr<Method> SharedMethod;
typedef std::map<std::string, SharedMethod> Methods;
typedef std::map<std::string, cls_cxx_filter_factory_t> Filters;
struct Class {
Methods methods;
Filters filters;
};
typedef boost::shared_ptr<Class> SharedClass;
void open_all_classes();
int create(const std::string &name, cls_handle_t *handle);
int create_method(cls_handle_t hclass, const char *method,
cls_method_cxx_call_t class_call,
cls_method_handle_t *handle);
cls_method_cxx_call_t get_method(const std::string &cls,
const std::string &method);
SharedMethodContext get_method_context(TestIoCtxImpl *io_ctx_impl,
const std::string &oid,
uint64_t snap_id,
const SnapContext &snapc);
int create_filter(cls_handle_t hclass, const std::string& filter_name,
cls_cxx_filter_factory_t fn);
private:
typedef std::map<std::string, SharedClass> Classes;
typedef std::list<void*> ClassHandles;
Classes m_classes;
ClassHandles m_class_handles;
Filters m_filters;
void open_class(const std::string& name, const std::string& path);
};
} // namespace librados
#endif // CEPH_TEST_CLASS_HANDLER_H
| 2,063 | 24.8 | 72 |
h
|
null |
ceph-main/src/test/librados_test_stub/TestCluster.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_CLUSTER_H
#define CEPH_TEST_CLUSTER_H
#include "test/librados_test_stub/TestWatchNotify.h"
#include "include/common_fwd.h"
namespace librados {
class TestRadosClient;
class TestWatchNotify;
class TestCluster {
public:
struct ObjectLocator {
std::string nspace;
std::string name;
ObjectLocator(const std::string& nspace, const std::string& name)
: nspace(nspace), name(name) {
}
bool operator<(const ObjectLocator& rhs) const {
if (nspace != rhs.nspace) {
return nspace < rhs.nspace;
}
return name < rhs.name;
}
};
struct ObjectHandler {
virtual ~ObjectHandler() {}
virtual void handle_removed(TestRadosClient* test_rados_client) = 0;
};
TestCluster() : m_watch_notify(this) {
}
virtual ~TestCluster() {
}
virtual TestRadosClient *create_rados_client(CephContext *cct) = 0;
virtual int register_object_handler(int64_t pool_id,
const ObjectLocator& locator,
ObjectHandler* object_handler) = 0;
virtual void unregister_object_handler(int64_t pool_id,
const ObjectLocator& locator,
ObjectHandler* object_handler) = 0;
TestWatchNotify *get_watch_notify() {
return &m_watch_notify;
}
protected:
TestWatchNotify m_watch_notify;
};
} // namespace librados
#endif // CEPH_TEST_CLUSTER_H
| 1,565 | 23.092308 | 76 |
h
|
null |
ceph-main/src/test/librados_test_stub/TestIoCtxImpl.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados_test_stub/TestIoCtxImpl.h"
#include "test/librados_test_stub/TestClassHandler.h"
#include "test/librados_test_stub/TestRadosClient.h"
#include "test/librados_test_stub/TestWatchNotify.h"
#include "librados/AioCompletionImpl.h"
#include "include/ceph_assert.h"
#include "common/Finisher.h"
#include "common/valgrind.h"
#include "objclass/objclass.h"
#include <functional>
#include <errno.h>
using namespace std;
namespace librados {
TestIoCtxImpl::TestIoCtxImpl() : m_client(NULL) {
get();
}
TestIoCtxImpl::TestIoCtxImpl(TestRadosClient *client, int64_t pool_id,
const std::string& pool_name)
: m_client(client), m_pool_id(pool_id), m_pool_name(pool_name),
m_snap_seq(CEPH_NOSNAP)
{
m_client->get();
get();
}
TestIoCtxImpl::TestIoCtxImpl(const TestIoCtxImpl& rhs)
: m_client(rhs.m_client),
m_pool_id(rhs.m_pool_id),
m_pool_name(rhs.m_pool_name),
m_namespace_name(rhs.m_namespace_name),
m_snap_seq(rhs.m_snap_seq)
{
m_client->get();
get();
}
TestIoCtxImpl::~TestIoCtxImpl() {
ceph_assert(m_pending_ops == 0);
}
void TestObjectOperationImpl::get() {
m_refcount++;
}
void TestObjectOperationImpl::put() {
if (--m_refcount == 0) {
ANNOTATE_HAPPENS_AFTER(&m_refcount);
ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&m_refcount);
delete this;
} else {
ANNOTATE_HAPPENS_BEFORE(&m_refcount);
}
}
void TestIoCtxImpl::get() {
m_refcount++;
}
void TestIoCtxImpl::put() {
if (--m_refcount == 0) {
m_client->put();
delete this;
}
}
uint64_t TestIoCtxImpl::get_instance_id() const {
return m_client->get_instance_id();
}
int64_t TestIoCtxImpl::get_id() {
return m_pool_id;
}
uint64_t TestIoCtxImpl::get_last_version() {
return 0;
}
std::string TestIoCtxImpl::get_pool_name() {
return m_pool_name;
}
int TestIoCtxImpl::aio_flush() {
m_client->flush_aio_operations();
return 0;
}
void TestIoCtxImpl::aio_flush_async(AioCompletionImpl *c) {
m_client->flush_aio_operations(c);
}
void TestIoCtxImpl::aio_notify(const std::string& oid, AioCompletionImpl *c,
bufferlist& bl, uint64_t timeout_ms,
bufferlist *pbl) {
m_pending_ops++;
c->get();
C_AioNotify *ctx = new C_AioNotify(this, c);
m_client->get_watch_notify()->aio_notify(m_client, m_pool_id, get_namespace(),
oid, bl, timeout_ms, pbl, ctx);
}
int TestIoCtxImpl::aio_operate(const std::string& oid, TestObjectOperationImpl &ops,
AioCompletionImpl *c, SnapContext *snap_context,
const ceph::real_time *pmtime, int flags) {
// TODO flags for now
ops.get();
m_pending_ops++;
m_client->add_aio_operation(oid, true, std::bind(
&TestIoCtxImpl::execute_aio_operations, this, oid, &ops,
reinterpret_cast<bufferlist*>(0), m_snap_seq,
snap_context != NULL ? *snap_context : m_snapc, nullptr), c);
return 0;
}
int TestIoCtxImpl::aio_operate_read(const std::string& oid,
TestObjectOperationImpl &ops,
AioCompletionImpl *c, int flags,
bufferlist *pbl, uint64_t snap_id,
uint64_t* objver) {
// TODO ignoring flags for now
ops.get();
m_pending_ops++;
m_client->add_aio_operation(oid, true, std::bind(
&TestIoCtxImpl::execute_aio_operations, this, oid, &ops, pbl, snap_id,
m_snapc, objver), c);
return 0;
}
int TestIoCtxImpl::aio_watch(const std::string& o, AioCompletionImpl *c,
uint64_t *handle, librados::WatchCtx2 *watch_ctx) {
m_pending_ops++;
c->get();
C_AioNotify *ctx = new C_AioNotify(this, c);
if (m_client->is_blocklisted()) {
m_client->get_aio_finisher()->queue(ctx, -EBLOCKLISTED);
} else {
m_client->get_watch_notify()->aio_watch(m_client, m_pool_id,
get_namespace(), o,
get_instance_id(), handle, nullptr,
watch_ctx, ctx);
}
return 0;
}
int TestIoCtxImpl::aio_unwatch(uint64_t handle, AioCompletionImpl *c) {
m_pending_ops++;
c->get();
C_AioNotify *ctx = new C_AioNotify(this, c);
if (m_client->is_blocklisted()) {
m_client->get_aio_finisher()->queue(ctx, -EBLOCKLISTED);
} else {
m_client->get_watch_notify()->aio_unwatch(m_client, handle, ctx);
}
return 0;
}
int TestIoCtxImpl::exec(const std::string& oid, TestClassHandler *handler,
const char *cls, const char *method,
bufferlist& inbl, bufferlist* outbl,
uint64_t snap_id, const SnapContext &snapc) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
cls_method_cxx_call_t call = handler->get_method(cls, method);
if (call == NULL) {
return -ENOSYS;
}
return (*call)(reinterpret_cast<cls_method_context_t>(
handler->get_method_context(this, oid, snap_id, snapc).get()), &inbl,
outbl);
}
int TestIoCtxImpl::list_watchers(const std::string& o,
std::list<obj_watch_t> *out_watchers) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
return m_client->get_watch_notify()->list_watchers(m_pool_id, get_namespace(),
o, out_watchers);
}
int TestIoCtxImpl::notify(const std::string& o, bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
return m_client->get_watch_notify()->notify(m_client, m_pool_id,
get_namespace(), o, bl,
timeout_ms, pbl);
}
void TestIoCtxImpl::notify_ack(const std::string& o, uint64_t notify_id,
uint64_t handle, bufferlist& bl) {
m_client->get_watch_notify()->notify_ack(m_client, m_pool_id, get_namespace(),
o, notify_id, handle,
m_client->get_instance_id(), bl);
}
int TestIoCtxImpl::operate(const std::string& oid,
TestObjectOperationImpl &ops) {
AioCompletionImpl *comp = new AioCompletionImpl();
ops.get();
m_pending_ops++;
m_client->add_aio_operation(oid, false, std::bind(
&TestIoCtxImpl::execute_aio_operations, this, oid, &ops,
reinterpret_cast<bufferlist*>(0), m_snap_seq, m_snapc, nullptr), comp);
comp->wait_for_complete();
int ret = comp->get_return_value();
comp->put();
return ret;
}
int TestIoCtxImpl::operate_read(const std::string& oid,
TestObjectOperationImpl &ops,
bufferlist *pbl) {
AioCompletionImpl *comp = new AioCompletionImpl();
ops.get();
m_pending_ops++;
m_client->add_aio_operation(oid, false, std::bind(
&TestIoCtxImpl::execute_aio_operations, this, oid, &ops, pbl,
m_snap_seq, m_snapc, nullptr), comp);
comp->wait_for_complete();
int ret = comp->get_return_value();
comp->put();
return ret;
}
void TestIoCtxImpl::aio_selfmanaged_snap_create(uint64_t *snapid,
AioCompletionImpl *c) {
m_client->add_aio_operation(
"", true,
std::bind(&TestIoCtxImpl::selfmanaged_snap_create, this, snapid), c);
}
void TestIoCtxImpl::aio_selfmanaged_snap_remove(uint64_t snapid,
AioCompletionImpl *c) {
m_client->add_aio_operation(
"", true,
std::bind(&TestIoCtxImpl::selfmanaged_snap_remove, this, snapid), c);
}
int TestIoCtxImpl::selfmanaged_snap_set_write_ctx(snap_t seq,
std::vector<snap_t>& snaps) {
std::vector<snapid_t> snap_ids(snaps.begin(), snaps.end());
m_snapc = SnapContext(seq, snap_ids);
return 0;
}
int TestIoCtxImpl::set_alloc_hint(const std::string& oid,
uint64_t expected_object_size,
uint64_t expected_write_size,
uint32_t flags,
const SnapContext &snapc) {
return 0;
}
void TestIoCtxImpl::set_snap_read(snap_t seq) {
if (seq == 0) {
seq = CEPH_NOSNAP;
}
m_snap_seq = seq;
}
int TestIoCtxImpl::tmap_update(const std::string& oid, bufferlist& cmdbl) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
// TODO: protect against concurrent tmap updates
bufferlist tmap_header;
std::map<string,bufferlist> tmap;
uint64_t size = 0;
int r = stat(oid, &size, NULL);
if (r == -ENOENT) {
r = create(oid, false, m_snapc);
}
if (r < 0) {
return r;
}
if (size > 0) {
bufferlist inbl;
r = read(oid, size, 0, &inbl, CEPH_NOSNAP, nullptr);
if (r < 0) {
return r;
}
auto iter = inbl.cbegin();
decode(tmap_header, iter);
decode(tmap, iter);
}
__u8 c;
std::string key;
bufferlist value;
auto iter = cmdbl.cbegin();
decode(c, iter);
decode(key, iter);
switch (c) {
case CEPH_OSD_TMAP_SET:
decode(value, iter);
tmap[key] = value;
break;
case CEPH_OSD_TMAP_RM:
r = tmap.erase(key);
if (r == 0) {
return -ENOENT;
}
break;
default:
return -EINVAL;
}
bufferlist out;
encode(tmap_header, out);
encode(tmap, out);
r = write_full(oid, out, m_snapc);
return r;
}
int TestIoCtxImpl::unwatch(uint64_t handle) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
return m_client->get_watch_notify()->unwatch(m_client, handle);
}
int TestIoCtxImpl::watch(const std::string& o, uint64_t *handle,
librados::WatchCtx *ctx, librados::WatchCtx2 *ctx2) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
return m_client->get_watch_notify()->watch(m_client, m_pool_id,
get_namespace(), o,
get_instance_id(), handle, ctx,
ctx2);
}
int TestIoCtxImpl::execute_operation(const std::string& oid,
const Operation &operation) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
TestRadosClient::Transaction transaction(m_client, get_namespace(), oid);
return operation(this, oid);
}
int TestIoCtxImpl::execute_aio_operations(const std::string& oid,
TestObjectOperationImpl *ops,
bufferlist *pbl, uint64_t snap_id,
const SnapContext &snapc,
uint64_t* objver) {
int ret = 0;
if (m_client->is_blocklisted()) {
ret = -EBLOCKLISTED;
} else {
TestRadosClient::Transaction transaction(m_client, get_namespace(), oid);
for (ObjectOperations::iterator it = ops->ops.begin();
it != ops->ops.end(); ++it) {
ret = (*it)(this, oid, pbl, snap_id, snapc, objver);
if (ret < 0) {
break;
}
}
}
m_pending_ops--;
ops->put();
return ret;
}
void TestIoCtxImpl::handle_aio_notify_complete(AioCompletionImpl *c, int r) {
m_pending_ops--;
m_client->finish_aio_completion(c, r);
}
} // namespace librados
| 11,565 | 28.281013 | 84 |
cc
|
null |
ceph-main/src/test/librados_test_stub/TestIoCtxImpl.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_IO_CTX_IMPL_H
#define CEPH_TEST_IO_CTX_IMPL_H
#include <list>
#include <atomic>
#include <boost/function.hpp>
#include "include/rados/librados.hpp"
#include "include/Context.h"
#include "common/snap_types.h"
namespace librados {
class TestClassHandler;
class TestIoCtxImpl;
class TestRadosClient;
typedef boost::function<int(TestIoCtxImpl*,
const std::string&,
bufferlist *,
uint64_t,
const SnapContext &,
uint64_t*)> ObjectOperationTestImpl;
typedef std::list<ObjectOperationTestImpl> ObjectOperations;
struct TestObjectOperationImpl {
public:
void get();
void put();
ObjectOperations ops;
private:
std::atomic<uint64_t> m_refcount = { 0 };
};
class TestIoCtxImpl {
public:
typedef boost::function<int(TestIoCtxImpl *, const std::string &)> Operation;
TestIoCtxImpl();
explicit TestIoCtxImpl(TestRadosClient *client, int64_t m_pool_id,
const std::string& pool_name);
TestRadosClient *get_rados_client() {
return m_client;
}
void get();
void put();
inline int64_t get_pool_id() const {
return m_pool_id;
}
virtual TestIoCtxImpl *clone() = 0;
virtual uint64_t get_instance_id() const;
virtual int64_t get_id();
virtual uint64_t get_last_version();
virtual std::string get_pool_name();
inline void set_namespace(const std::string& namespace_name) {
m_namespace_name = namespace_name;
}
inline std::string get_namespace() const {
return m_namespace_name;
}
snap_t get_snap_read() const {
return m_snap_seq;
}
inline void set_snap_context(const SnapContext& snapc) {
m_snapc = snapc;
}
const SnapContext &get_snap_context() const {
return m_snapc;
}
virtual int aio_flush();
virtual void aio_flush_async(AioCompletionImpl *c);
virtual void aio_notify(const std::string& oid, AioCompletionImpl *c,
bufferlist& bl, uint64_t timeout_ms, bufferlist *pbl);
virtual int aio_operate(const std::string& oid, TestObjectOperationImpl &ops,
AioCompletionImpl *c, SnapContext *snap_context,
const ceph::real_time *pmtime, int flags);
virtual int aio_operate_read(const std::string& oid, TestObjectOperationImpl &ops,
AioCompletionImpl *c, int flags,
bufferlist *pbl, uint64_t snap_id,
uint64_t* objver);
virtual int aio_remove(const std::string& oid, AioCompletionImpl *c,
int flags = 0) = 0;
virtual int aio_watch(const std::string& o, AioCompletionImpl *c,
uint64_t *handle, librados::WatchCtx2 *ctx);
virtual int aio_unwatch(uint64_t handle, AioCompletionImpl *c);
virtual int append(const std::string& oid, const bufferlist &bl,
const SnapContext &snapc) = 0;
virtual int assert_exists(const std::string &oid, uint64_t snap_id) = 0;
virtual int assert_version(const std::string &oid, uint64_t ver) = 0;
virtual int create(const std::string& oid, bool exclusive,
const SnapContext &snapc) = 0;
virtual int exec(const std::string& oid, TestClassHandler *handler,
const char *cls, const char *method,
bufferlist& inbl, bufferlist* outbl,
uint64_t snap_id, const SnapContext &snapc);
virtual int list_snaps(const std::string& o, snap_set_t *out_snaps) = 0;
virtual int list_watchers(const std::string& o,
std::list<obj_watch_t> *out_watchers);
virtual int notify(const std::string& o, bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl);
virtual void notify_ack(const std::string& o, uint64_t notify_id,
uint64_t handle, bufferlist& bl);
virtual int omap_get_vals(const std::string& oid,
const std::string& start_after,
const std::string &filter_prefix,
uint64_t max_return,
std::map<std::string, bufferlist> *out_vals) = 0;
virtual int omap_get_vals2(const std::string& oid,
const std::string& start_after,
const std::string &filter_prefix,
uint64_t max_return,
std::map<std::string, bufferlist> *out_vals,
bool *pmore) = 0;
virtual int omap_rm_keys(const std::string& oid,
const std::set<std::string>& keys) = 0;
virtual int omap_set(const std::string& oid,
const std::map<std::string, bufferlist> &map) = 0;
virtual int operate(const std::string& oid, TestObjectOperationImpl &ops);
virtual int operate_read(const std::string& oid, TestObjectOperationImpl &ops,
bufferlist *pbl);
virtual int read(const std::string& oid, size_t len, uint64_t off,
bufferlist *bl, uint64_t snap_id, uint64_t* objver) = 0;
virtual int remove(const std::string& oid, const SnapContext &snapc) = 0;
virtual int selfmanaged_snap_create(uint64_t *snapid) = 0;
virtual void aio_selfmanaged_snap_create(uint64_t *snapid,
AioCompletionImpl *c);
virtual int selfmanaged_snap_remove(uint64_t snapid) = 0;
virtual void aio_selfmanaged_snap_remove(uint64_t snapid,
AioCompletionImpl *c);
virtual int selfmanaged_snap_rollback(const std::string& oid,
uint64_t snapid) = 0;
virtual int selfmanaged_snap_set_write_ctx(snap_t seq,
std::vector<snap_t>& snaps);
virtual int set_alloc_hint(const std::string& oid,
uint64_t expected_object_size,
uint64_t expected_write_size,
uint32_t flags,
const SnapContext &snapc);
virtual void set_snap_read(snap_t seq);
virtual int sparse_read(const std::string& oid, uint64_t off, uint64_t len,
std::map<uint64_t,uint64_t> *m,
bufferlist *data_bl, uint64_t snap_id) = 0;
virtual int stat(const std::string& oid, uint64_t *psize, time_t *pmtime) = 0;
virtual int truncate(const std::string& oid, uint64_t size,
const SnapContext &snapc) = 0;
virtual int tmap_update(const std::string& oid, bufferlist& cmdbl);
virtual int unwatch(uint64_t handle);
virtual int watch(const std::string& o, uint64_t *handle,
librados::WatchCtx *ctx, librados::WatchCtx2 *ctx2);
virtual int write(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off, const SnapContext &snapc) = 0;
virtual int write_full(const std::string& oid, bufferlist& bl,
const SnapContext &snapc) = 0;
virtual int writesame(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off, const SnapContext &snapc) = 0;
virtual int cmpext(const std::string& oid, uint64_t off, bufferlist& cmp_bl,
uint64_t snap_id) = 0;
virtual int xattr_get(const std::string& oid,
std::map<std::string, bufferlist>* attrset) = 0;
virtual int xattr_set(const std::string& oid, const std::string &name,
bufferlist& bl) = 0;
virtual int zero(const std::string& oid, uint64_t off, uint64_t len,
const SnapContext &snapc) = 0;
int execute_operation(const std::string& oid,
const Operation &operation);
protected:
TestIoCtxImpl(const TestIoCtxImpl& rhs);
virtual ~TestIoCtxImpl();
int execute_aio_operations(const std::string& oid,
TestObjectOperationImpl *ops,
bufferlist *pbl, uint64_t,
const SnapContext &snapc,
uint64_t* objver);
private:
struct C_AioNotify : public Context {
TestIoCtxImpl *io_ctx;
AioCompletionImpl *aio_comp;
C_AioNotify(TestIoCtxImpl *_io_ctx, AioCompletionImpl *_aio_comp)
: io_ctx(_io_ctx), aio_comp(_aio_comp) {
}
void finish(int r) override {
io_ctx->handle_aio_notify_complete(aio_comp, r);
}
};
TestRadosClient *m_client;
int64_t m_pool_id = 0;
std::string m_pool_name;
std::string m_namespace_name;
snap_t m_snap_seq = 0;
SnapContext m_snapc;
std::atomic<uint64_t> m_refcount = { 0 };
std::atomic<uint64_t> m_pending_ops = { 0 };
void handle_aio_notify_complete(AioCompletionImpl *aio_comp, int r);
};
} // namespace librados
#endif // CEPH_TEST_IO_CTX_IMPL_H
| 8,907 | 39.126126 | 84 |
h
|
null |
ceph-main/src/test/librados_test_stub/TestMemCluster.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados_test_stub/TestMemCluster.h"
#include "test/librados_test_stub/TestMemRadosClient.h"
namespace librados {
TestMemCluster::File::File()
: objver(0), snap_id(), exists(true) {
}
TestMemCluster::File::File(const File &rhs)
: data(rhs.data),
mtime(rhs.mtime),
objver(rhs.objver),
snap_id(rhs.snap_id),
exists(rhs.exists) {
}
TestMemCluster::Pool::Pool() = default;
TestMemCluster::TestMemCluster()
: m_next_nonce(static_cast<uint32_t>(reinterpret_cast<uint64_t>(this))) {
}
TestMemCluster::~TestMemCluster() {
for (auto pool_pair : m_pools) {
pool_pair.second->put();
}
}
TestRadosClient *TestMemCluster::create_rados_client(CephContext *cct) {
return new TestMemRadosClient(cct, this);
}
int TestMemCluster::register_object_handler(int64_t pool_id,
const ObjectLocator& locator,
ObjectHandler* object_handler) {
std::lock_guard locker{m_lock};
auto pool = get_pool(m_lock, pool_id);
if (pool == nullptr) {
return -ENOENT;
}
std::unique_lock pool_locker{pool->file_lock};
auto file_it = pool->files.find(locator);
if (file_it == pool->files.end()) {
return -ENOENT;
}
auto& object_handlers = pool->file_handlers[locator];
auto it = object_handlers.find(object_handler);
ceph_assert(it == object_handlers.end());
object_handlers.insert(object_handler);
return 0;
}
void TestMemCluster::unregister_object_handler(int64_t pool_id,
const ObjectLocator& locator,
ObjectHandler* object_handler) {
std::lock_guard locker{m_lock};
auto pool = get_pool(m_lock, pool_id);
if (pool == nullptr) {
return;
}
std::unique_lock pool_locker{pool->file_lock};
auto handlers_it = pool->file_handlers.find(locator);
if (handlers_it == pool->file_handlers.end()) {
return;
}
auto& object_handlers = handlers_it->second;
object_handlers.erase(object_handler);
}
int TestMemCluster::pool_create(const std::string &pool_name) {
std::lock_guard locker{m_lock};
if (m_pools.find(pool_name) != m_pools.end()) {
return -EEXIST;
}
Pool *pool = new Pool();
pool->pool_id = ++m_pool_id;
m_pools[pool_name] = pool;
return 0;
}
int TestMemCluster::pool_delete(const std::string &pool_name) {
std::lock_guard locker{m_lock};
Pools::iterator iter = m_pools.find(pool_name);
if (iter == m_pools.end()) {
return -ENOENT;
}
iter->second->put();
m_pools.erase(iter);
return 0;
}
int TestMemCluster::pool_get_base_tier(int64_t pool_id, int64_t* base_tier) {
// TODO
*base_tier = pool_id;
return 0;
}
int TestMemCluster::pool_list(std::list<std::pair<int64_t, std::string> >& v) {
std::lock_guard locker{m_lock};
v.clear();
for (Pools::iterator iter = m_pools.begin(); iter != m_pools.end(); ++iter) {
v.push_back(std::make_pair(iter->second->pool_id, iter->first));
}
return 0;
}
int64_t TestMemCluster::pool_lookup(const std::string &pool_name) {
std::lock_guard locker{m_lock};
Pools::iterator iter = m_pools.find(pool_name);
if (iter == m_pools.end()) {
return -ENOENT;
}
return iter->second->pool_id;
}
int TestMemCluster::pool_reverse_lookup(int64_t id, std::string *name) {
std::lock_guard locker{m_lock};
for (Pools::iterator iter = m_pools.begin(); iter != m_pools.end(); ++iter) {
if (iter->second->pool_id == id) {
*name = iter->first;
return 0;
}
}
return -ENOENT;
}
TestMemCluster::Pool *TestMemCluster::get_pool(int64_t pool_id) {
std::lock_guard locker{m_lock};
return get_pool(m_lock, pool_id);
}
TestMemCluster::Pool *TestMemCluster::get_pool(const ceph::mutex& lock,
int64_t pool_id) {
for (auto &pool_pair : m_pools) {
if (pool_pair.second->pool_id == pool_id) {
return pool_pair.second;
}
}
return nullptr;
}
TestMemCluster::Pool *TestMemCluster::get_pool(const std::string &pool_name) {
std::lock_guard locker{m_lock};
Pools::iterator iter = m_pools.find(pool_name);
if (iter != m_pools.end()) {
return iter->second;
}
return nullptr;
}
void TestMemCluster::allocate_client(uint32_t *nonce, uint64_t *global_id) {
std::lock_guard locker{m_lock};
*nonce = m_next_nonce++;
*global_id = m_next_global_id++;
}
void TestMemCluster::deallocate_client(uint32_t nonce) {
std::lock_guard locker{m_lock};
m_blocklist.erase(nonce);
}
bool TestMemCluster::is_blocklisted(uint32_t nonce) const {
std::lock_guard locker{m_lock};
return (m_blocklist.find(nonce) != m_blocklist.end());
}
void TestMemCluster::blocklist(uint32_t nonce) {
{
std::lock_guard locker{m_lock};
m_blocklist.insert(nonce);
}
// after blocklisting the client, disconnect and drop its watches
m_watch_notify.blocklist(nonce);
}
void TestMemCluster::transaction_start(const ObjectLocator& locator) {
std::unique_lock locker{m_lock};
m_transaction_cond.wait(locker, [&locator, this] {
return m_transactions.count(locator) == 0;
});
auto result = m_transactions.insert(locator);
ceph_assert(result.second);
}
void TestMemCluster::transaction_finish(const ObjectLocator& locator) {
std::lock_guard locker{m_lock};
size_t count = m_transactions.erase(locator);
ceph_assert(count == 1);
m_transaction_cond.notify_all();
}
} // namespace librados
| 5,538 | 26.151961 | 79 |
cc
|
null |
ceph-main/src/test/librados_test_stub/TestMemCluster.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_MEM_CLUSTER_H
#define CEPH_TEST_MEM_CLUSTER_H
#include "test/librados_test_stub/TestCluster.h"
#include "include/buffer.h"
#include "include/interval_set.h"
#include "include/int_types.h"
#include "common/ceph_mutex.h"
#include "common/RefCountedObj.h"
#include <boost/shared_ptr.hpp>
#include <list>
#include <map>
#include <set>
#include <string>
namespace librados {
class TestMemCluster : public TestCluster {
public:
typedef std::map<std::string, bufferlist> OMap;
typedef std::map<ObjectLocator, OMap> FileOMaps;
typedef std::map<ObjectLocator, bufferlist> FileTMaps;
typedef std::map<std::string, bufferlist> XAttrs;
typedef std::map<ObjectLocator, XAttrs> FileXAttrs;
typedef std::set<ObjectHandler*> ObjectHandlers;
typedef std::map<ObjectLocator, ObjectHandlers> FileHandlers;
struct File {
File();
File(const File &rhs);
bufferlist data;
time_t mtime;
uint64_t objver;
uint64_t snap_id;
std::vector<uint64_t> snaps;
interval_set<uint64_t> snap_overlap;
bool exists;
ceph::shared_mutex lock =
ceph::make_shared_mutex("TestMemCluster::File::lock");
};
typedef boost::shared_ptr<File> SharedFile;
typedef std::list<SharedFile> FileSnapshots;
typedef std::map<ObjectLocator, FileSnapshots> Files;
typedef std::set<uint64_t> SnapSeqs;
struct Pool : public RefCountedObject {
Pool();
int64_t pool_id = 0;
SnapSeqs snap_seqs;
uint64_t snap_id = 1;
ceph::shared_mutex file_lock =
ceph::make_shared_mutex("TestMemCluster::Pool::file_lock");
Files files;
FileOMaps file_omaps;
FileTMaps file_tmaps;
FileXAttrs file_xattrs;
FileHandlers file_handlers;
};
TestMemCluster();
~TestMemCluster() override;
TestRadosClient *create_rados_client(CephContext *cct) override;
int register_object_handler(int64_t pool_id, const ObjectLocator& locator,
ObjectHandler* object_handler) override;
void unregister_object_handler(int64_t pool_id, const ObjectLocator& locator,
ObjectHandler* object_handler) override;
int pool_create(const std::string &pool_name);
int pool_delete(const std::string &pool_name);
int pool_get_base_tier(int64_t pool_id, int64_t* base_tier);
int pool_list(std::list<std::pair<int64_t, std::string> >& v);
int64_t pool_lookup(const std::string &name);
int pool_reverse_lookup(int64_t id, std::string *name);
Pool *get_pool(int64_t pool_id);
Pool *get_pool(const std::string &pool_name);
void allocate_client(uint32_t *nonce, uint64_t *global_id);
void deallocate_client(uint32_t nonce);
bool is_blocklisted(uint32_t nonce) const;
void blocklist(uint32_t nonce);
void transaction_start(const ObjectLocator& locator);
void transaction_finish(const ObjectLocator& locator);
private:
typedef std::map<std::string, Pool*> Pools;
typedef std::set<uint32_t> Blocklist;
mutable ceph::mutex m_lock =
ceph::make_mutex("TestMemCluster::m_lock");
Pools m_pools;
int64_t m_pool_id = 0;
uint32_t m_next_nonce;
uint64_t m_next_global_id = 1234;
Blocklist m_blocklist;
ceph::condition_variable m_transaction_cond;
std::set<ObjectLocator> m_transactions;
Pool *get_pool(const ceph::mutex& lock, int64_t pool_id);
};
} // namespace librados
#endif // CEPH_TEST_MEM_CLUSTER_H
| 3,468 | 26.752 | 79 |
h
|
null |
ceph-main/src/test/librados_test_stub/TestMemIoCtxImpl.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados_test_stub/TestMemIoCtxImpl.h"
#include "test/librados_test_stub/TestMemRadosClient.h"
#include "common/Clock.h"
#include "include/err.h"
#include <functional>
#include <boost/algorithm/string/predicate.hpp>
#include <errno.h>
#include <include/compat.h>
#define dout_subsys ceph_subsys_rados
#undef dout_prefix
#define dout_prefix *_dout << "TestMemIoCtxImpl: " << this << " " << __func__ \
<< ": " << oid << " "
static void to_vector(const interval_set<uint64_t> &set,
std::vector<std::pair<uint64_t, uint64_t> > *vec) {
vec->clear();
for (interval_set<uint64_t>::const_iterator it = set.begin();
it != set.end(); ++it) {
vec->push_back(*it);
}
}
// see PrimaryLogPG::finish_extent_cmp()
static int cmpext_compare(const bufferlist &bl, const bufferlist &read_bl) {
for (uint64_t idx = 0; idx < bl.length(); ++idx) {
char read_byte = (idx < read_bl.length() ? read_bl[idx] : 0);
if (bl[idx] != read_byte) {
return -MAX_ERRNO - idx;
}
}
return 0;
}
namespace librados {
TestMemIoCtxImpl::TestMemIoCtxImpl() {
}
TestMemIoCtxImpl::TestMemIoCtxImpl(const TestMemIoCtxImpl& rhs)
: TestIoCtxImpl(rhs), m_client(rhs.m_client), m_pool(rhs.m_pool) {
m_pool->get();
}
TestMemIoCtxImpl::TestMemIoCtxImpl(TestMemRadosClient *client, int64_t pool_id,
const std::string& pool_name,
TestMemCluster::Pool *pool)
: TestIoCtxImpl(client, pool_id, pool_name), m_client(client),
m_pool(pool) {
m_pool->get();
}
TestMemIoCtxImpl::~TestMemIoCtxImpl() {
m_pool->put();
}
TestIoCtxImpl *TestMemIoCtxImpl::clone() {
return new TestMemIoCtxImpl(*this);
}
int TestMemIoCtxImpl::aio_remove(const std::string& oid, AioCompletionImpl *c, int flags) {
m_client->add_aio_operation(oid, true,
std::bind(&TestMemIoCtxImpl::remove, this, oid,
get_snap_context()),
c);
return 0;
}
int TestMemIoCtxImpl::append(const std::string& oid, const bufferlist &bl,
const SnapContext &snapc) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
auto cct = m_client->cct();
ldout(cct, 20) << "length=" << bl.length() << ", snapc=" << snapc << dendl;
TestMemCluster::SharedFile file;
{
std::unique_lock l{m_pool->file_lock};
file = get_file(oid, true, CEPH_NOSNAP, snapc);
}
std::unique_lock l{file->lock};
auto off = file->data.length();
ensure_minimum_length(off + bl.length(), &file->data);
file->data.begin(off).copy_in(bl.length(), bl);
return 0;
}
int TestMemIoCtxImpl::assert_exists(const std::string &oid, uint64_t snap_id) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
std::shared_lock l{m_pool->file_lock};
TestMemCluster::SharedFile file = get_file(oid, false, snap_id, {});
if (file == NULL) {
return -ENOENT;
}
return 0;
}
int TestMemIoCtxImpl::assert_version(const std::string &oid, uint64_t ver) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
std::shared_lock l{m_pool->file_lock};
TestMemCluster::SharedFile file = get_file(oid, false, CEPH_NOSNAP, {});
if (file == NULL || !file->exists) {
return -ENOENT;
}
if (ver < file->objver) {
return -ERANGE;
}
if (ver > file->objver) {
return -EOVERFLOW;
}
return 0;
}
int TestMemIoCtxImpl::create(const std::string& oid, bool exclusive,
const SnapContext &snapc) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
auto cct = m_client->cct();
ldout(cct, 20) << "snapc=" << snapc << dendl;
std::unique_lock l{m_pool->file_lock};
if (exclusive) {
TestMemCluster::SharedFile file = get_file(oid, false, CEPH_NOSNAP, {});
if (file != NULL && file->exists) {
return -EEXIST;
}
}
get_file(oid, true, CEPH_NOSNAP, snapc);
return 0;
}
int TestMemIoCtxImpl::list_snaps(const std::string& oid, snap_set_t *out_snaps) {
auto cct = m_client->cct();
ldout(cct, 20) << dendl;
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
out_snaps->seq = 0;
out_snaps->clones.clear();
std::shared_lock l{m_pool->file_lock};
TestMemCluster::Files::iterator it = m_pool->files.find(
{get_namespace(), oid});
if (it == m_pool->files.end()) {
return -ENOENT;
}
bool include_head = false;
TestMemCluster::FileSnapshots &file_snaps = it->second;
for (TestMemCluster::FileSnapshots::iterator s_it = file_snaps.begin();
s_it != file_snaps.end(); ++s_it) {
TestMemCluster::File &file = *s_it->get();
if (file_snaps.size() > 1) {
out_snaps->seq = file.snap_id;
TestMemCluster::FileSnapshots::iterator next_it(s_it);
++next_it;
if (next_it == file_snaps.end()) {
include_head = true;
break;
}
++out_snaps->seq;
if (!file.exists) {
continue;
}
// update the overlap with the next version's overlap metadata
TestMemCluster::File &next_file = *next_it->get();
interval_set<uint64_t> overlap;
if (next_file.exists) {
overlap = next_file.snap_overlap;
}
clone_info_t clone;
clone.cloneid = file.snap_id;
clone.snaps = file.snaps;
to_vector(overlap, &clone.overlap);
clone.size = file.data.length();
out_snaps->clones.push_back(clone);
}
}
if ((file_snaps.size() == 1 && file_snaps.back()->data.length() > 0) ||
include_head)
{
// Include the SNAP_HEAD
TestMemCluster::File &file = *file_snaps.back();
if (file.exists) {
std::shared_lock l2{file.lock};
if (out_snaps->seq == 0 && !include_head) {
out_snaps->seq = file.snap_id;
}
clone_info_t head_clone;
head_clone.cloneid = librados::SNAP_HEAD;
head_clone.size = file.data.length();
out_snaps->clones.push_back(head_clone);
}
}
ldout(cct, 20) << "seq=" << out_snaps->seq << ", "
<< "clones=[";
bool first_clone = true;
for (auto& clone : out_snaps->clones) {
*_dout << "{"
<< "cloneid=" << clone.cloneid << ", "
<< "snaps=" << clone.snaps << ", "
<< "overlap=" << clone.overlap << ", "
<< "size=" << clone.size << "}";
if (!first_clone) {
*_dout << ", ";
} else {
first_clone = false;
}
}
*_dout << "]" << dendl;
return 0;
}
int TestMemIoCtxImpl::omap_get_vals2(const std::string& oid,
const std::string& start_after,
const std::string &filter_prefix,
uint64_t max_return,
std::map<std::string, bufferlist> *out_vals,
bool *pmore) {
if (out_vals == NULL) {
return -EINVAL;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
TestMemCluster::SharedFile file;
{
std::shared_lock l{m_pool->file_lock};
file = get_file(oid, false, CEPH_NOSNAP, {});
if (file == NULL) {
return -ENOENT;
}
}
out_vals->clear();
std::shared_lock l{file->lock};
TestMemCluster::FileOMaps::iterator o_it = m_pool->file_omaps.find(
{get_namespace(), oid});
if (o_it == m_pool->file_omaps.end()) {
if (pmore) {
*pmore = false;
}
return 0;
}
TestMemCluster::OMap &omap = o_it->second;
TestMemCluster::OMap::iterator it = omap.begin();
if (!start_after.empty()) {
it = omap.upper_bound(start_after);
}
while (it != omap.end() && max_return > 0) {
if (filter_prefix.empty() ||
boost::algorithm::starts_with(it->first, filter_prefix)) {
(*out_vals)[it->first] = it->second;
--max_return;
}
++it;
}
if (pmore) {
*pmore = (it != omap.end());
}
return 0;
}
int TestMemIoCtxImpl::omap_get_vals(const std::string& oid,
const std::string& start_after,
const std::string &filter_prefix,
uint64_t max_return,
std::map<std::string, bufferlist> *out_vals) {
return omap_get_vals2(oid, start_after, filter_prefix, max_return, out_vals, nullptr);
}
int TestMemIoCtxImpl::omap_rm_keys(const std::string& oid,
const std::set<std::string>& keys) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
TestMemCluster::SharedFile file;
{
std::unique_lock l{m_pool->file_lock};
file = get_file(oid, true, CEPH_NOSNAP, get_snap_context());
if (file == NULL) {
return -ENOENT;
}
}
std::unique_lock l{file->lock};
for (std::set<std::string>::iterator it = keys.begin();
it != keys.end(); ++it) {
m_pool->file_omaps[{get_namespace(), oid}].erase(*it);
}
return 0;
}
int TestMemIoCtxImpl::omap_set(const std::string& oid,
const std::map<std::string, bufferlist> &map) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
TestMemCluster::SharedFile file;
{
std::unique_lock l{m_pool->file_lock};
file = get_file(oid, true, CEPH_NOSNAP, get_snap_context());
if (file == NULL) {
return -ENOENT;
}
}
std::unique_lock l{file->lock};
for (std::map<std::string, bufferlist>::const_iterator it = map.begin();
it != map.end(); ++it) {
bufferlist bl;
bl.append(it->second);
m_pool->file_omaps[{get_namespace(), oid}][it->first] = bl;
}
return 0;
}
int TestMemIoCtxImpl::read(const std::string& oid, size_t len, uint64_t off,
bufferlist *bl, uint64_t snap_id,
uint64_t* objver) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
TestMemCluster::SharedFile file;
{
std::shared_lock l{m_pool->file_lock};
file = get_file(oid, false, snap_id, {});
if (file == NULL) {
return -ENOENT;
}
}
std::shared_lock l{file->lock};
if (len == 0) {
len = file->data.length();
}
len = clip_io(off, len, file->data.length());
if (bl != NULL && len > 0) {
bufferlist bit;
bit.substr_of(file->data, off, len);
append_clone(bit, bl);
}
if (objver != nullptr) {
*objver = file->objver;
}
return len;
}
int TestMemIoCtxImpl::remove(const std::string& oid, const SnapContext &snapc) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
auto cct = m_client->cct();
ldout(cct, 20) << "snapc=" << snapc << dendl;
std::unique_lock l{m_pool->file_lock};
TestMemCluster::SharedFile file = get_file(oid, false, CEPH_NOSNAP, snapc);
if (file == NULL) {
return -ENOENT;
}
file = get_file(oid, true, CEPH_NOSNAP, snapc);
{
std::unique_lock l2{file->lock};
file->exists = false;
}
TestCluster::ObjectLocator locator(get_namespace(), oid);
TestMemCluster::Files::iterator it = m_pool->files.find(locator);
ceph_assert(it != m_pool->files.end());
if (*it->second.rbegin() == file) {
TestMemCluster::ObjectHandlers object_handlers;
std::swap(object_handlers, m_pool->file_handlers[locator]);
m_pool->file_handlers.erase(locator);
for (auto object_handler : object_handlers) {
object_handler->handle_removed(m_client);
}
}
if (it->second.size() == 1) {
m_pool->files.erase(it);
m_pool->file_omaps.erase(locator);
}
return 0;
}
int TestMemIoCtxImpl::selfmanaged_snap_create(uint64_t *snapid) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
std::unique_lock l{m_pool->file_lock};
*snapid = ++m_pool->snap_id;
m_pool->snap_seqs.insert(*snapid);
return 0;
}
int TestMemIoCtxImpl::selfmanaged_snap_remove(uint64_t snapid) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
std::unique_lock l{m_pool->file_lock};
TestMemCluster::SnapSeqs::iterator it =
m_pool->snap_seqs.find(snapid);
if (it == m_pool->snap_seqs.end()) {
return -ENOENT;
}
// TODO clean up all file snapshots
m_pool->snap_seqs.erase(it);
return 0;
}
int TestMemIoCtxImpl::selfmanaged_snap_rollback(const std::string& oid,
uint64_t snapid) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
std::unique_lock l{m_pool->file_lock};
TestMemCluster::SharedFile file;
TestMemCluster::Files::iterator f_it = m_pool->files.find(
{get_namespace(), oid});
if (f_it == m_pool->files.end()) {
return 0;
}
TestMemCluster::FileSnapshots &snaps = f_it->second;
file = snaps.back();
size_t versions = 0;
for (TestMemCluster::FileSnapshots::reverse_iterator it = snaps.rbegin();
it != snaps.rend(); ++it) {
TestMemCluster::SharedFile file = *it;
if (file->snap_id < get_snap_read()) {
if (versions == 0) {
// already at the snapshot version
return 0;
} else if (file->snap_id == CEPH_NOSNAP) {
if (versions == 1) {
// delete it current HEAD, next one is correct version
snaps.erase(it.base());
} else {
// overwrite contents of current HEAD
file = TestMemCluster::SharedFile (new TestMemCluster::File(**it));
file->snap_id = CEPH_NOSNAP;
*it = file;
}
} else {
// create new head version
file = TestMemCluster::SharedFile (new TestMemCluster::File(**it));
file->snap_id = m_pool->snap_id;
snaps.push_back(file);
}
return 0;
}
++versions;
}
return 0;
}
int TestMemIoCtxImpl::set_alloc_hint(const std::string& oid,
uint64_t expected_object_size,
uint64_t expected_write_size,
uint32_t flags,
const SnapContext &snapc) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
{
std::unique_lock l{m_pool->file_lock};
get_file(oid, true, CEPH_NOSNAP, snapc);
}
return 0;
}
int TestMemIoCtxImpl::sparse_read(const std::string& oid, uint64_t off,
uint64_t len,
std::map<uint64_t,uint64_t> *m,
bufferlist *data_bl, uint64_t snap_id) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
// TODO verify correctness
TestMemCluster::SharedFile file;
{
std::shared_lock l{m_pool->file_lock};
file = get_file(oid, false, snap_id, {});
if (file == NULL) {
return -ENOENT;
}
}
std::shared_lock l{file->lock};
len = clip_io(off, len, file->data.length());
// TODO support sparse read
if (m != NULL) {
m->clear();
if (len > 0) {
(*m)[off] = len;
}
}
if (data_bl != NULL && len > 0) {
bufferlist bit;
bit.substr_of(file->data, off, len);
append_clone(bit, data_bl);
}
return len > 0 ? 1 : 0;
}
int TestMemIoCtxImpl::stat(const std::string& oid, uint64_t *psize,
time_t *pmtime) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
TestMemCluster::SharedFile file;
{
std::shared_lock l{m_pool->file_lock};
file = get_file(oid, false, CEPH_NOSNAP, {});
if (file == NULL) {
return -ENOENT;
}
}
std::shared_lock l{file->lock};
if (psize != NULL) {
*psize = file->data.length();
}
if (pmtime != NULL) {
*pmtime = file->mtime;
}
return 0;
}
int TestMemIoCtxImpl::truncate(const std::string& oid, uint64_t size,
const SnapContext &snapc) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
auto cct = m_client->cct();
ldout(cct, 20) << "size=" << size << ", snapc=" << snapc << dendl;
TestMemCluster::SharedFile file;
{
std::unique_lock l{m_pool->file_lock};
file = get_file(oid, true, CEPH_NOSNAP, snapc);
}
std::unique_lock l{file->lock};
bufferlist bl(size);
interval_set<uint64_t> is;
if (file->data.length() > size) {
is.insert(size, file->data.length() - size);
bl.substr_of(file->data, 0, size);
file->data.swap(bl);
} else if (file->data.length() != size) {
if (size == 0) {
bl.clear();
} else {
is.insert(0, size);
bl.append_zero(size - file->data.length());
file->data.append(bl);
}
}
is.intersection_of(file->snap_overlap);
file->snap_overlap.subtract(is);
return 0;
}
int TestMemIoCtxImpl::write(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off, const SnapContext &snapc) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
auto cct = m_client->cct();
ldout(cct, 20) << "extent=" << off << "~" << len << ", snapc=" << snapc
<< dendl;
TestMemCluster::SharedFile file;
{
std::unique_lock l{m_pool->file_lock};
file = get_file(oid, true, CEPH_NOSNAP, snapc);
}
std::unique_lock l{file->lock};
if (len > 0) {
interval_set<uint64_t> is;
is.insert(off, len);
is.intersection_of(file->snap_overlap);
file->snap_overlap.subtract(is);
}
ensure_minimum_length(off + len, &file->data);
file->data.begin(off).copy_in(len, bl);
return 0;
}
int TestMemIoCtxImpl::write_full(const std::string& oid, bufferlist& bl,
const SnapContext &snapc) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
auto cct = m_client->cct();
ldout(cct, 20) << "length=" << bl.length() << ", snapc=" << snapc << dendl;
TestMemCluster::SharedFile file;
{
std::unique_lock l{m_pool->file_lock};
file = get_file(oid, true, CEPH_NOSNAP, snapc);
if (file == NULL) {
return -ENOENT;
}
}
std::unique_lock l{file->lock};
if (bl.length() > 0) {
interval_set<uint64_t> is;
is.insert(0, bl.length());
is.intersection_of(file->snap_overlap);
file->snap_overlap.subtract(is);
}
file->data.clear();
ensure_minimum_length(bl.length(), &file->data);
file->data.begin().copy_in(bl.length(), bl);
return 0;
}
int TestMemIoCtxImpl::writesame(const std::string& oid, bufferlist& bl,
size_t len, uint64_t off,
const SnapContext &snapc) {
if (get_snap_read() != CEPH_NOSNAP) {
return -EROFS;
} else if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
if (len == 0 || (len % bl.length())) {
return -EINVAL;
}
TestMemCluster::SharedFile file;
{
std::unique_lock l{m_pool->file_lock};
file = get_file(oid, true, CEPH_NOSNAP, snapc);
}
std::unique_lock l{file->lock};
if (len > 0) {
interval_set<uint64_t> is;
is.insert(off, len);
is.intersection_of(file->snap_overlap);
file->snap_overlap.subtract(is);
}
ensure_minimum_length(off + len, &file->data);
while (len > 0) {
file->data.begin(off).copy_in(bl.length(), bl);
off += bl.length();
len -= bl.length();
}
return 0;
}
int TestMemIoCtxImpl::cmpext(const std::string& oid, uint64_t off,
bufferlist& cmp_bl, uint64_t snap_id) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
bufferlist read_bl;
uint64_t len = cmp_bl.length();
TestMemCluster::SharedFile file;
{
std::shared_lock l{m_pool->file_lock};
file = get_file(oid, false, snap_id, {});
if (file == NULL) {
return cmpext_compare(cmp_bl, read_bl);
}
}
std::shared_lock l{file->lock};
if (off >= file->data.length()) {
len = 0;
} else if (off + len > file->data.length()) {
len = file->data.length() - off;
}
read_bl.substr_of(file->data, off, len);
return cmpext_compare(cmp_bl, read_bl);
}
int TestMemIoCtxImpl::xattr_get(const std::string& oid,
std::map<std::string, bufferlist>* attrset) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
TestMemCluster::SharedFile file;
std::shared_lock l{m_pool->file_lock};
TestMemCluster::FileXAttrs::iterator it = m_pool->file_xattrs.find(
{get_namespace(), oid});
if (it == m_pool->file_xattrs.end()) {
return -ENODATA;
}
*attrset = it->second;
return 0;
}
int TestMemIoCtxImpl::xattr_set(const std::string& oid, const std::string &name,
bufferlist& bl) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
std::unique_lock l{m_pool->file_lock};
m_pool->file_xattrs[{get_namespace(), oid}][name] = bl;
return 0;
}
int TestMemIoCtxImpl::zero(const std::string& oid, uint64_t off, uint64_t len,
const SnapContext &snapc) {
if (m_client->is_blocklisted()) {
return -EBLOCKLISTED;
}
auto cct = m_client->cct();
ldout(cct, 20) << "extent=" << off << "~" << len << ", snapc=" << snapc
<< dendl;
bool truncate_redirect = false;
TestMemCluster::SharedFile file;
{
std::unique_lock l{m_pool->file_lock};
file = get_file(oid, false, CEPH_NOSNAP, snapc);
if (!file) {
return 0;
}
file = get_file(oid, true, CEPH_NOSNAP, snapc);
std::shared_lock l2{file->lock};
if (len > 0 && off + len >= file->data.length()) {
// Zero -> Truncate logic embedded in OSD
truncate_redirect = true;
}
}
if (truncate_redirect) {
return truncate(oid, off, snapc);
}
bufferlist bl;
bl.append_zero(len);
return write(oid, bl, len, off, snapc);
}
void TestMemIoCtxImpl::append_clone(bufferlist& src, bufferlist* dest) {
// deep-copy the src to ensure our memory-based mock RADOS data cannot
// be modified by callers
if (src.length() > 0) {
bufferlist::iterator iter = src.begin();
buffer::ptr ptr;
iter.copy_deep(src.length(), ptr);
dest->append(ptr);
}
}
size_t TestMemIoCtxImpl::clip_io(size_t off, size_t len, size_t bl_len) {
if (off >= bl_len) {
len = 0;
} else if (off + len > bl_len) {
len = bl_len - off;
}
return len;
}
void TestMemIoCtxImpl::ensure_minimum_length(size_t len, bufferlist *bl) {
if (len > bl->length()) {
bufferptr ptr(buffer::create(len - bl->length()));
ptr.zero();
bl->append(ptr);
}
}
TestMemCluster::SharedFile TestMemIoCtxImpl::get_file(
const std::string &oid, bool write, uint64_t snap_id,
const SnapContext &snapc) {
ceph_assert(ceph_mutex_is_locked(m_pool->file_lock) ||
ceph_mutex_is_wlocked(m_pool->file_lock));
ceph_assert(!write || ceph_mutex_is_wlocked(m_pool->file_lock));
TestMemCluster::SharedFile file;
TestMemCluster::Files::iterator it = m_pool->files.find(
{get_namespace(), oid});
if (it != m_pool->files.end()) {
file = it->second.back();
} else if (!write) {
return TestMemCluster::SharedFile();
}
if (write) {
bool new_version = false;
if (!file || !file->exists) {
file = TestMemCluster::SharedFile(new TestMemCluster::File());
new_version = true;
} else {
if (!snapc.snaps.empty() && file->snap_id < snapc.seq) {
for (std::vector<snapid_t>::const_reverse_iterator seq_it =
snapc.snaps.rbegin();
seq_it != snapc.snaps.rend(); ++seq_it) {
if (*seq_it > file->snap_id && *seq_it <= snapc.seq) {
file->snaps.push_back(*seq_it);
}
}
bufferlist prev_data = file->data;
file = TestMemCluster::SharedFile(
new TestMemCluster::File(*file));
file->data.clear();
append_clone(prev_data, &file->data);
if (prev_data.length() > 0) {
file->snap_overlap.insert(0, prev_data.length());
}
new_version = true;
}
}
if (new_version) {
file->snap_id = snapc.seq;
file->mtime = ceph_clock_now().sec();
m_pool->files[{get_namespace(), oid}].push_back(file);
}
file->objver++;
return file;
}
if (snap_id == CEPH_NOSNAP) {
if (!file->exists) {
ceph_assert(it->second.size() > 1);
return TestMemCluster::SharedFile();
}
return file;
}
TestMemCluster::FileSnapshots &snaps = it->second;
for (TestMemCluster::FileSnapshots::reverse_iterator it = snaps.rbegin();
it != snaps.rend(); ++it) {
TestMemCluster::SharedFile file = *it;
if (file->snap_id < snap_id) {
if (!file->exists) {
return TestMemCluster::SharedFile();
}
return file;
}
}
return TestMemCluster::SharedFile();
}
} // namespace librados
| 25,213 | 26.258378 | 91 |
cc
|
null |
ceph-main/src/test/librados_test_stub/TestMemIoCtxImpl.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_MEM_IO_CTX_IMPL_H
#define CEPH_TEST_MEM_IO_CTX_IMPL_H
#include "test/librados_test_stub/TestIoCtxImpl.h"
#include "test/librados_test_stub/TestMemCluster.h"
namespace librados {
class TestMemRadosClient;
class TestMemIoCtxImpl : public TestIoCtxImpl {
public:
TestMemIoCtxImpl();
TestMemIoCtxImpl(TestMemRadosClient *client, int64_t m_pool_id,
const std::string& pool_name,
TestMemCluster::Pool *pool);
~TestMemIoCtxImpl() override;
TestIoCtxImpl *clone() override;
int aio_remove(const std::string& oid, AioCompletionImpl *c, int flags = 0) override;
int append(const std::string& oid, const bufferlist &bl,
const SnapContext &snapc) override;
int assert_exists(const std::string &oid, uint64_t snap_id) override;
int assert_version(const std::string &oid, uint64_t ver) override;
int create(const std::string& oid, bool exclusive,
const SnapContext &snapc) override;
int list_snaps(const std::string& o, snap_set_t *out_snaps) override;
int omap_get_vals(const std::string& oid,
const std::string& start_after,
const std::string &filter_prefix,
uint64_t max_return,
std::map<std::string, bufferlist> *out_vals) override;
int omap_get_vals2(const std::string& oid,
const std::string& start_after,
const std::string &filter_prefix,
uint64_t max_return,
std::map<std::string, bufferlist> *out_vals,
bool *pmore) override;
int omap_rm_keys(const std::string& oid,
const std::set<std::string>& keys) override;
int omap_set(const std::string& oid, const std::map<std::string,
bufferlist> &map) override;
int read(const std::string& oid, size_t len, uint64_t off,
bufferlist *bl, uint64_t snap_id, uint64_t* objver) override;
int remove(const std::string& oid, const SnapContext &snapc) override;
int selfmanaged_snap_create(uint64_t *snapid) override;
int selfmanaged_snap_remove(uint64_t snapid) override;
int selfmanaged_snap_rollback(const std::string& oid,
uint64_t snapid) override;
int set_alloc_hint(const std::string& oid, uint64_t expected_object_size,
uint64_t expected_write_size, uint32_t flags,
const SnapContext &snapc) override;
int sparse_read(const std::string& oid, uint64_t off, uint64_t len,
std::map<uint64_t,uint64_t> *m, bufferlist *data_bl,
uint64_t snap_id) override;
int stat(const std::string& oid, uint64_t *psize, time_t *pmtime) override;
int truncate(const std::string& oid, uint64_t size,
const SnapContext &snapc) override;
int write(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off, const SnapContext &snapc) override;
int write_full(const std::string& oid, bufferlist& bl,
const SnapContext &snapc) override;
int writesame(const std::string& oid, bufferlist& bl, size_t len,
uint64_t off, const SnapContext &snapc) override;
int cmpext(const std::string& oid, uint64_t off, bufferlist& cmp_bl,
uint64_t snap_id) override;
int xattr_get(const std::string& oid,
std::map<std::string, bufferlist>* attrset) override;
int xattr_set(const std::string& oid, const std::string &name,
bufferlist& bl) override;
int zero(const std::string& oid, uint64_t off, uint64_t len,
const SnapContext &snapc) override;
protected:
TestMemCluster::Pool *get_pool() {
return m_pool;
}
private:
TestMemIoCtxImpl(const TestMemIoCtxImpl&);
TestMemRadosClient *m_client = nullptr;
TestMemCluster::Pool *m_pool = nullptr;
void append_clone(bufferlist& src, bufferlist* dest);
size_t clip_io(size_t off, size_t len, size_t bl_len);
void ensure_minimum_length(size_t len, bufferlist *bl);
TestMemCluster::SharedFile get_file(const std::string &oid, bool write,
uint64_t snap_id,
const SnapContext &snapc);
};
} // namespace librados
#endif // CEPH_TEST_MEM_IO_CTX_IMPL_H
| 4,396 | 40.87619 | 87 |
h
|
null |
ceph-main/src/test/librados_test_stub/TestMemRadosClient.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados_test_stub/TestMemRadosClient.h"
#include "test/librados_test_stub/TestMemCluster.h"
#include "test/librados_test_stub/TestMemIoCtxImpl.h"
#include <errno.h>
#include <sstream>
namespace librados {
TestMemRadosClient::TestMemRadosClient(CephContext *cct,
TestMemCluster *test_mem_cluster)
: TestRadosClient(cct, test_mem_cluster->get_watch_notify()),
m_mem_cluster(test_mem_cluster) {
m_mem_cluster->allocate_client(&m_nonce, &m_global_id);
}
TestMemRadosClient::~TestMemRadosClient() {
m_mem_cluster->deallocate_client(m_nonce);
}
TestIoCtxImpl *TestMemRadosClient::create_ioctx(int64_t pool_id,
const std::string &pool_name) {
return new TestMemIoCtxImpl(this, pool_id, pool_name,
m_mem_cluster->get_pool(pool_name));
}
void TestMemRadosClient::object_list(int64_t pool_id,
std::list<librados::TestRadosClient::Object> *list) {
list->clear();
auto pool = m_mem_cluster->get_pool(pool_id);
if (pool != nullptr) {
std::shared_lock file_locker{pool->file_lock};
for (auto &file_pair : pool->files) {
Object obj;
obj.oid = file_pair.first.name;
list->push_back(obj);
}
}
}
int TestMemRadosClient::pool_create(const std::string &pool_name) {
if (is_blocklisted()) {
return -EBLOCKLISTED;
}
return m_mem_cluster->pool_create(pool_name);
}
int TestMemRadosClient::pool_delete(const std::string &pool_name) {
if (is_blocklisted()) {
return -EBLOCKLISTED;
}
return m_mem_cluster->pool_delete(pool_name);
}
int TestMemRadosClient::pool_get_base_tier(int64_t pool_id, int64_t* base_tier) {
// TODO
*base_tier = pool_id;
return 0;
}
int TestMemRadosClient::pool_list(std::list<std::pair<int64_t, std::string> >& v) {
return m_mem_cluster->pool_list(v);
}
int64_t TestMemRadosClient::pool_lookup(const std::string &pool_name) {
return m_mem_cluster->pool_lookup(pool_name);
}
int TestMemRadosClient::pool_reverse_lookup(int64_t id, std::string *name) {
return m_mem_cluster->pool_reverse_lookup(id, name);
}
int TestMemRadosClient::watch_flush() {
get_watch_notify()->flush(this);
return 0;
}
bool TestMemRadosClient::is_blocklisted() const {
return m_mem_cluster->is_blocklisted(m_nonce);
}
int TestMemRadosClient::blocklist_add(const std::string& client_address,
uint32_t expire_seconds) {
if (is_blocklisted()) {
return -EBLOCKLISTED;
}
// extract the nonce to use as a unique key to the client
auto idx = client_address.find("/");
if (idx == std::string::npos || idx + 1 >= client_address.size()) {
return -EINVAL;
}
std::stringstream nonce_ss(client_address.substr(idx + 1));
uint32_t nonce;
nonce_ss >> nonce;
if (!nonce_ss) {
return -EINVAL;
}
m_mem_cluster->blocklist(nonce);
return 0;
}
void TestMemRadosClient::transaction_start(const std::string& nspace,
const std::string &oid) {
m_mem_cluster->transaction_start({nspace, oid});
}
void TestMemRadosClient::transaction_finish(const std::string& nspace,
const std::string &oid) {
m_mem_cluster->transaction_finish({nspace, oid});
}
} // namespace librados
| 3,360 | 27.243697 | 83 |
cc
|
null |
ceph-main/src/test/librados_test_stub/TestMemRadosClient.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_MEM_RADOS_CLIENT_H
#define CEPH_TEST_MEM_RADOS_CLIENT_H
#include "test/librados_test_stub/TestRadosClient.h"
#include "include/ceph_assert.h"
#include <list>
#include <string>
namespace librados {
class AioCompletionImpl;
class TestMemCluster;
class TestMemRadosClient : public TestRadosClient {
public:
TestMemRadosClient(CephContext *cct, TestMemCluster *test_mem_cluster);
~TestMemRadosClient() override;
TestIoCtxImpl *create_ioctx(int64_t pool_id,
const std::string &pool_name) override;
uint32_t get_nonce() override {
return m_nonce;
}
uint64_t get_instance_id() override {
return m_global_id;
}
int get_min_compatible_osd(int8_t* require_osd_release) override {
*require_osd_release = CEPH_RELEASE_OCTOPUS;
return 0;
}
int get_min_compatible_client(int8_t* min_compat_client,
int8_t* require_min_compat_client) override {
*min_compat_client = CEPH_RELEASE_MIMIC;
*require_min_compat_client = CEPH_RELEASE_MIMIC;
return 0;
}
void object_list(int64_t pool_id,
std::list<librados::TestRadosClient::Object> *list) override;
int service_daemon_register(const std::string& service,
const std::string& name,
const std::map<std::string,std::string>& metadata) override {
return 0;
}
int service_daemon_update_status(std::map<std::string,std::string>&& status) override {
return 0;
}
int pool_create(const std::string &pool_name) override;
int pool_delete(const std::string &pool_name) override;
int pool_get_base_tier(int64_t pool_id, int64_t* base_tier) override;
int pool_list(std::list<std::pair<int64_t, std::string> >& v) override;
int64_t pool_lookup(const std::string &name) override;
int pool_reverse_lookup(int64_t id, std::string *name) override;
int watch_flush() override;
bool is_blocklisted() const override;
int blocklist_add(const std::string& client_address,
uint32_t expire_seconds) override;
protected:
TestMemCluster *get_mem_cluster() {
return m_mem_cluster;
}
protected:
void transaction_start(const std::string& nspace,
const std::string &oid) override;
void transaction_finish(const std::string& nspace,
const std::string &oid) override;
private:
TestMemCluster *m_mem_cluster;
uint32_t m_nonce;
uint64_t m_global_id;
};
} // namespace librados
#endif // CEPH_TEST_MEM_RADOS_CLIENT_H
| 2,667 | 28.977528 | 91 |
h
|
null |
ceph-main/src/test/librados_test_stub/TestRadosClient.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados_test_stub/TestRadosClient.h"
#include "test/librados_test_stub/TestIoCtxImpl.h"
#include "librados/AioCompletionImpl.h"
#include "include/ceph_assert.h"
#include "common/ceph_json.h"
#include "common/Finisher.h"
#include "common/async/context_pool.h"
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <errno.h>
#include <atomic>
#include <functional>
#include <sstream>
static int get_concurrency() {
int concurrency = 0;
char *env = getenv("LIBRADOS_CONCURRENCY");
if (env != NULL) {
concurrency = atoi(env);
}
if (concurrency == 0) {
concurrency = boost::thread::thread::hardware_concurrency();
}
if (concurrency == 0) {
concurrency = 1;
}
return concurrency;
}
using namespace std::placeholders;
namespace librados {
namespace {
const char *config_keys[] = {
"librados_thread_count",
NULL
};
} // anonymous namespace
static void finish_aio_completion(AioCompletionImpl *c, int r) {
c->lock.lock();
c->complete = true;
c->rval = r;
c->lock.unlock();
rados_callback_t cb_complete = c->callback_complete;
void *cb_complete_arg = c->callback_complete_arg;
if (cb_complete) {
cb_complete(c, cb_complete_arg);
}
rados_callback_t cb_safe = c->callback_safe;
void *cb_safe_arg = c->callback_safe_arg;
if (cb_safe) {
cb_safe(c, cb_safe_arg);
}
c->lock.lock();
c->callback_complete = NULL;
c->callback_safe = NULL;
c->cond.notify_all();
c->put_unlock();
}
class AioFunctionContext : public Context {
public:
AioFunctionContext(const TestRadosClient::AioFunction &callback,
Finisher *finisher, AioCompletionImpl *c)
: m_callback(callback), m_finisher(finisher), m_comp(c)
{
if (m_comp != NULL) {
m_comp->get();
}
}
void finish(int r) override {
int ret = m_callback();
if (m_comp != NULL) {
if (m_finisher != NULL) {
m_finisher->queue(new LambdaContext(std::bind(
&finish_aio_completion, m_comp, ret)));
} else {
finish_aio_completion(m_comp, ret);
}
}
}
private:
TestRadosClient::AioFunction m_callback;
Finisher *m_finisher;
AioCompletionImpl *m_comp;
};
TestRadosClient::TestRadosClient(CephContext *cct,
TestWatchNotify *watch_notify)
: m_cct(cct->get()), m_watch_notify(watch_notify),
m_aio_finisher(new Finisher(m_cct)),
m_io_context_pool(std::make_unique<ceph::async::io_context_pool>())
{
get();
// simulate multiple OSDs
int concurrency = get_concurrency();
for (int i = 0; i < concurrency; ++i) {
m_finishers.push_back(new Finisher(m_cct));
m_finishers.back()->start();
}
// replicate AIO callback processing
m_aio_finisher->start();
// replicate neorados callback processing
m_cct->_conf.add_observer(this);
m_io_context_pool->start(m_cct->_conf.get_val<uint64_t>(
"librados_thread_count"));
}
TestRadosClient::~TestRadosClient() {
flush_aio_operations();
for (size_t i = 0; i < m_finishers.size(); ++i) {
m_finishers[i]->stop();
delete m_finishers[i];
}
m_aio_finisher->stop();
delete m_aio_finisher;
m_cct->_conf.remove_observer(this);
m_io_context_pool->stop();
m_cct->put();
m_cct = NULL;
}
boost::asio::io_context& TestRadosClient::get_io_context() {
return m_io_context_pool->get_io_context();
}
const char** TestRadosClient::get_tracked_conf_keys() const {
return config_keys;
}
void TestRadosClient::handle_conf_change(
const ConfigProxy& conf, const std::set<std::string> &changed) {
if (changed.count("librados_thread_count")) {
m_io_context_pool->stop();
m_io_context_pool->start(conf.get_val<std::uint64_t>(
"librados_thread_count"));
}
}
void TestRadosClient::get() {
m_refcount++;
}
void TestRadosClient::put() {
if (--m_refcount == 0) {
shutdown();
delete this;
}
}
CephContext *TestRadosClient::cct() {
return m_cct;
}
int TestRadosClient::connect() {
return 0;
}
void TestRadosClient::shutdown() {
}
int TestRadosClient::wait_for_latest_osdmap() {
return 0;
}
int TestRadosClient::mon_command(const std::vector<std::string>& cmd,
const bufferlist &inbl,
bufferlist *outbl, std::string *outs) {
for (std::vector<std::string>::const_iterator it = cmd.begin();
it != cmd.end(); ++it) {
JSONParser parser;
if (!parser.parse(it->c_str(), it->length())) {
return -EINVAL;
}
JSONObjIter j_it = parser.find("prefix");
if (j_it.end()) {
return -EINVAL;
}
if ((*j_it)->get_data() == "osd tier add") {
return 0;
} else if ((*j_it)->get_data() == "osd tier cache-mode") {
return 0;
} else if ((*j_it)->get_data() == "osd tier set-overlay") {
return 0;
} else if ((*j_it)->get_data() == "osd tier remove-overlay") {
return 0;
} else if ((*j_it)->get_data() == "osd tier remove") {
return 0;
} else if ((*j_it)->get_data() == "config-key rm") {
return 0;
} else if ((*j_it)->get_data() == "config set") {
return 0;
} else if ((*j_it)->get_data() == "df") {
std::stringstream str;
str << R"({"pools": [)";
std::list<std::pair<int64_t, std::string>> pools;
pool_list(pools);
for (auto& pool : pools) {
if (pools.begin()->first != pool.first) {
str << ",";
}
str << R"({"name": ")" << pool.second << R"(", "stats": )"
<< R"({"percent_used": 1.0, "bytes_used": 0, "max_avail": 0}})";
}
str << "]}";
outbl->append(str.str());
return 0;
} else if ((*j_it)->get_data() == "osd blocklist") {
auto op_it = parser.find("blocklistop");
if (!op_it.end() && (*op_it)->get_data() == "add") {
uint32_t expire = 0;
auto expire_it = parser.find("expire");
if (!expire_it.end()) {
expire = boost::lexical_cast<uint32_t>((*expire_it)->get_data());
}
auto addr_it = parser.find("addr");
return blocklist_add((*addr_it)->get_data(), expire);
}
}
}
return -ENOSYS;
}
void TestRadosClient::add_aio_operation(const std::string& oid,
bool queue_callback,
const AioFunction &aio_function,
AioCompletionImpl *c) {
AioFunctionContext *ctx = new AioFunctionContext(
aio_function, queue_callback ? m_aio_finisher : NULL, c);
get_finisher(oid)->queue(ctx);
}
struct WaitForFlush {
int flushed() {
if (--count == 0) {
aio_finisher->queue(new LambdaContext(std::bind(
&finish_aio_completion, c, 0)));
delete this;
}
return 0;
}
std::atomic<int64_t> count = { 0 };
Finisher *aio_finisher;
AioCompletionImpl *c;
};
void TestRadosClient::flush_aio_operations() {
AioCompletionImpl *comp = new AioCompletionImpl();
flush_aio_operations(comp);
comp->wait_for_complete();
comp->put();
}
void TestRadosClient::flush_aio_operations(AioCompletionImpl *c) {
c->get();
WaitForFlush *wait_for_flush = new WaitForFlush();
wait_for_flush->count = m_finishers.size();
wait_for_flush->aio_finisher = m_aio_finisher;
wait_for_flush->c = c;
for (size_t i = 0; i < m_finishers.size(); ++i) {
AioFunctionContext *ctx = new AioFunctionContext(
std::bind(&WaitForFlush::flushed, wait_for_flush),
nullptr, nullptr);
m_finishers[i]->queue(ctx);
}
}
int TestRadosClient::aio_watch_flush(AioCompletionImpl *c) {
c->get();
Context *ctx = new LambdaContext(std::bind(
&TestRadosClient::finish_aio_completion, this, c, std::placeholders::_1));
get_watch_notify()->aio_flush(this, ctx);
return 0;
}
void TestRadosClient::finish_aio_completion(AioCompletionImpl *c, int r) {
librados::finish_aio_completion(c, r);
}
Finisher *TestRadosClient::get_finisher(const std::string &oid) {
std::size_t h = m_hash(oid);
return m_finishers[h % m_finishers.size()];
}
} // namespace librados
| 8,123 | 25.038462 | 78 |
cc
|
null |
ceph-main/src/test/librados_test_stub/TestRadosClient.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_RADOS_CLIENT_H
#define CEPH_TEST_RADOS_CLIENT_H
#include <map>
#include <memory>
#include <list>
#include <string>
#include <vector>
#include <atomic>
#include <boost/function.hpp>
#include <boost/functional/hash.hpp>
#include "include/rados/librados.hpp"
#include "common/config.h"
#include "common/config_obs.h"
#include "include/buffer_fwd.h"
#include "test/librados_test_stub/TestWatchNotify.h"
class Finisher;
namespace boost { namespace asio { struct io_context; }}
namespace ceph { namespace async { struct io_context_pool; }}
namespace librados {
class TestIoCtxImpl;
class TestRadosClient : public md_config_obs_t {
public:
static void Deallocate(librados::TestRadosClient* client)
{
client->put();
}
typedef boost::function<int()> AioFunction;
struct Object {
std::string oid;
std::string locator;
std::string nspace;
};
class Transaction {
public:
Transaction(TestRadosClient *rados_client, const std::string& nspace,
const std::string &oid)
: rados_client(rados_client), nspace(nspace), oid(oid) {
rados_client->transaction_start(nspace, oid);
}
~Transaction() {
rados_client->transaction_finish(nspace, oid);
}
private:
TestRadosClient *rados_client;
std::string nspace;
std::string oid;
};
TestRadosClient(CephContext *cct, TestWatchNotify *watch_notify);
void get();
void put();
virtual CephContext *cct();
virtual uint32_t get_nonce() = 0;
virtual uint64_t get_instance_id() = 0;
virtual int get_min_compatible_osd(int8_t* require_osd_release) = 0;
virtual int get_min_compatible_client(int8_t* min_compat_client,
int8_t* require_min_compat_client) = 0;
virtual int connect();
virtual void shutdown();
virtual int wait_for_latest_osdmap();
virtual TestIoCtxImpl *create_ioctx(int64_t pool_id,
const std::string &pool_name) = 0;
virtual int mon_command(const std::vector<std::string>& cmd,
const bufferlist &inbl,
bufferlist *outbl, std::string *outs);
virtual void object_list(int64_t pool_id,
std::list<librados::TestRadosClient::Object> *list) = 0;
virtual int service_daemon_register(const std::string& service,
const std::string& name,
const std::map<std::string,std::string>& metadata) = 0;
virtual int service_daemon_update_status(std::map<std::string,std::string>&& status) = 0;
virtual int pool_create(const std::string &pool_name) = 0;
virtual int pool_delete(const std::string &pool_name) = 0;
virtual int pool_get_base_tier(int64_t pool_id, int64_t* base_tier) = 0;
virtual int pool_list(std::list<std::pair<int64_t, std::string> >& v) = 0;
virtual int64_t pool_lookup(const std::string &name) = 0;
virtual int pool_reverse_lookup(int64_t id, std::string *name) = 0;
virtual int aio_watch_flush(AioCompletionImpl *c);
virtual int watch_flush() = 0;
virtual bool is_blocklisted() const = 0;
virtual int blocklist_add(const std::string& client_address,
uint32_t expire_seconds) = 0;
virtual int wait_for_latest_osd_map() {
return 0;
}
Finisher *get_aio_finisher() {
return m_aio_finisher;
}
TestWatchNotify *get_watch_notify() {
return m_watch_notify;
}
void add_aio_operation(const std::string& oid, bool queue_callback,
const AioFunction &aio_function, AioCompletionImpl *c);
void flush_aio_operations();
void flush_aio_operations(AioCompletionImpl *c);
void finish_aio_completion(AioCompletionImpl *c, int r);
boost::asio::io_context& get_io_context();
protected:
virtual ~TestRadosClient();
virtual void transaction_start(const std::string& nspace,
const std::string &oid) = 0;
virtual void transaction_finish(const std::string& nspace,
const std::string &oid) = 0;
const char** get_tracked_conf_keys() const override;
void handle_conf_change(const ConfigProxy& conf,
const std::set<std::string> &changed) override;
private:
struct IOContextPool;
CephContext *m_cct;
std::atomic<uint64_t> m_refcount = { 0 };
TestWatchNotify *m_watch_notify;
Finisher *get_finisher(const std::string& oid);
Finisher *m_aio_finisher;
std::vector<Finisher *> m_finishers;
boost::hash<std::string> m_hash;
std::unique_ptr<ceph::async::io_context_pool> m_io_context_pool;
};
} // namespace librados
#endif // CEPH_TEST_RADOS_CLIENT_H
| 4,728 | 28.01227 | 93 |
h
|
null |
ceph-main/src/test/librados_test_stub/TestWatchNotify.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados_test_stub/TestWatchNotify.h"
#include "include/Context.h"
#include "common/Cond.h"
#include "include/stringify.h"
#include "common/Finisher.h"
#include "test/librados_test_stub/TestCluster.h"
#include "test/librados_test_stub/TestRadosClient.h"
#include <boost/bind/bind.hpp>
#include <boost/function.hpp>
#include "include/ceph_assert.h"
#define dout_subsys ceph_subsys_rados
#undef dout_prefix
#define dout_prefix *_dout << "TestWatchNotify::" << __func__ << ": "
namespace librados {
std::ostream& operator<<(std::ostream& out,
const TestWatchNotify::WatcherID &watcher_id) {
out << "(" << watcher_id.first << "," << watcher_id.second << ")";
return out;
}
struct TestWatchNotify::ObjectHandler : public TestCluster::ObjectHandler {
TestWatchNotify* test_watch_notify;
int64_t pool_id;
std::string nspace;
std::string oid;
ObjectHandler(TestWatchNotify* test_watch_notify, int64_t pool_id,
const std::string& nspace, const std::string& oid)
: test_watch_notify(test_watch_notify), pool_id(pool_id),
nspace(nspace), oid(oid) {
}
void handle_removed(TestRadosClient* test_rados_client) override {
// copy member variables since this object might be deleted
auto _test_watch_notify = test_watch_notify;
auto _pool_id = pool_id;
auto _nspace = nspace;
auto _oid = oid;
auto ctx = new LambdaContext([_test_watch_notify, _pool_id, _nspace, _oid](int r) {
_test_watch_notify->handle_object_removed(_pool_id, _nspace, _oid);
});
test_rados_client->get_aio_finisher()->queue(ctx);
}
};
TestWatchNotify::TestWatchNotify(TestCluster* test_cluster)
: m_test_cluster(test_cluster) {
}
void TestWatchNotify::flush(TestRadosClient *rados_client) {
CephContext *cct = rados_client->cct();
ldout(cct, 20) << "enter" << dendl;
// block until we know no additional async notify callbacks will occur
C_SaferCond ctx;
m_async_op_tracker.wait_for_ops(&ctx);
ctx.wait();
}
int TestWatchNotify::list_watchers(int64_t pool_id, const std::string& nspace,
const std::string& o,
std::list<obj_watch_t> *out_watchers) {
std::lock_guard lock{m_lock};
SharedWatcher watcher = get_watcher(pool_id, nspace, o);
if (!watcher) {
return -ENOENT;
}
out_watchers->clear();
for (TestWatchNotify::WatchHandles::iterator it =
watcher->watch_handles.begin();
it != watcher->watch_handles.end(); ++it) {
obj_watch_t obj;
strncpy(obj.addr, it->second.addr.c_str(), sizeof(obj.addr) - 1);
obj.addr[sizeof(obj.addr) - 1] = '\0';
obj.watcher_id = static_cast<int64_t>(it->second.gid);
obj.cookie = it->second.handle;
obj.timeout_seconds = 30;
out_watchers->push_back(obj);
}
return 0;
}
void TestWatchNotify::aio_flush(TestRadosClient *rados_client,
Context *on_finish) {
rados_client->get_aio_finisher()->queue(on_finish);
}
int TestWatchNotify::watch(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& o,
uint64_t gid, uint64_t *handle,
librados::WatchCtx *ctx, librados::WatchCtx2 *ctx2) {
C_SaferCond cond;
aio_watch(rados_client, pool_id, nspace, o, gid, handle, ctx, ctx2, &cond);
return cond.wait();
}
void TestWatchNotify::aio_watch(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& o,
uint64_t gid, uint64_t *handle,
librados::WatchCtx *watch_ctx,
librados::WatchCtx2 *watch_ctx2,
Context *on_finish) {
auto ctx = new LambdaContext([=, this](int) {
execute_watch(rados_client, pool_id, nspace, o, gid, handle, watch_ctx,
watch_ctx2, on_finish);
});
rados_client->get_aio_finisher()->queue(ctx);
}
int TestWatchNotify::unwatch(TestRadosClient *rados_client,
uint64_t handle) {
C_SaferCond ctx;
aio_unwatch(rados_client, handle, &ctx);
return ctx.wait();
}
void TestWatchNotify::aio_unwatch(TestRadosClient *rados_client,
uint64_t handle, Context *on_finish) {
auto ctx = new LambdaContext([this, rados_client, handle, on_finish](int) {
execute_unwatch(rados_client, handle, on_finish);
});
rados_client->get_aio_finisher()->queue(ctx);
}
void TestWatchNotify::aio_notify(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace,
const std::string& oid, const bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl,
Context *on_notify) {
auto ctx = new LambdaContext([=, this](int) {
execute_notify(rados_client, pool_id, nspace, oid, bl, pbl, on_notify);
});
rados_client->get_aio_finisher()->queue(ctx);
}
int TestWatchNotify::notify(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& oid,
bufferlist& bl, uint64_t timeout_ms,
bufferlist *pbl) {
C_SaferCond cond;
aio_notify(rados_client, pool_id, nspace, oid, bl, timeout_ms, pbl, &cond);
return cond.wait();
}
void TestWatchNotify::notify_ack(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace,
const std::string& o, uint64_t notify_id,
uint64_t handle, uint64_t gid,
bufferlist& bl) {
CephContext *cct = rados_client->cct();
ldout(cct, 20) << "notify_id=" << notify_id << ", handle=" << handle
<< ", gid=" << gid << dendl;
std::lock_guard lock{m_lock};
WatcherID watcher_id = std::make_pair(gid, handle);
ack_notify(rados_client, pool_id, nspace, o, notify_id, watcher_id, bl);
finish_notify(rados_client, pool_id, nspace, o, notify_id);
}
void TestWatchNotify::execute_watch(TestRadosClient *rados_client,
int64_t pool_id, const std::string& nspace,
const std::string& o, uint64_t gid,
uint64_t *handle, librados::WatchCtx *ctx,
librados::WatchCtx2 *ctx2,
Context* on_finish) {
CephContext *cct = rados_client->cct();
m_lock.lock();
SharedWatcher watcher = get_watcher(pool_id, nspace, o);
if (!watcher) {
m_lock.unlock();
on_finish->complete(-ENOENT);
return;
}
WatchHandle watch_handle;
watch_handle.rados_client = rados_client;
watch_handle.addr = "127.0.0.1:0/" + stringify(rados_client->get_nonce());
watch_handle.nonce = rados_client->get_nonce();
watch_handle.gid = gid;
watch_handle.handle = ++m_handle;
watch_handle.watch_ctx = ctx;
watch_handle.watch_ctx2 = ctx2;
watcher->watch_handles[watch_handle.handle] = watch_handle;
*handle = watch_handle.handle;
ldout(cct, 20) << "oid=" << o << ", gid=" << gid << ": handle=" << *handle
<< dendl;
m_lock.unlock();
on_finish->complete(0);
}
void TestWatchNotify::execute_unwatch(TestRadosClient *rados_client,
uint64_t handle, Context* on_finish) {
CephContext *cct = rados_client->cct();
ldout(cct, 20) << "handle=" << handle << dendl;
{
std::lock_guard locker{m_lock};
for (FileWatchers::iterator it = m_file_watchers.begin();
it != m_file_watchers.end(); ++it) {
SharedWatcher watcher = it->second;
WatchHandles::iterator w_it = watcher->watch_handles.find(handle);
if (w_it != watcher->watch_handles.end()) {
watcher->watch_handles.erase(w_it);
maybe_remove_watcher(watcher);
break;
}
}
}
on_finish->complete(0);
}
TestWatchNotify::SharedWatcher TestWatchNotify::get_watcher(
int64_t pool_id, const std::string& nspace, const std::string& oid) {
ceph_assert(ceph_mutex_is_locked(m_lock));
auto it = m_file_watchers.find({pool_id, nspace, oid});
if (it == m_file_watchers.end()) {
SharedWatcher watcher(new Watcher(pool_id, nspace, oid));
watcher->object_handler.reset(new ObjectHandler(
this, pool_id, nspace, oid));
int r = m_test_cluster->register_object_handler(
pool_id, {nspace, oid}, watcher->object_handler.get());
if (r < 0) {
// object doesn't exist
return SharedWatcher();
}
m_file_watchers[{pool_id, nspace, oid}] = watcher;
return watcher;
}
return it->second;
}
void TestWatchNotify::maybe_remove_watcher(SharedWatcher watcher) {
ceph_assert(ceph_mutex_is_locked(m_lock));
// TODO
if (watcher->watch_handles.empty() && watcher->notify_handles.empty()) {
auto pool_id = watcher->pool_id;
auto& nspace = watcher->nspace;
auto& oid = watcher->oid;
if (watcher->object_handler) {
m_test_cluster->unregister_object_handler(pool_id, {nspace, oid},
watcher->object_handler.get());
watcher->object_handler.reset();
}
m_file_watchers.erase({pool_id, nspace, oid});
}
}
void TestWatchNotify::execute_notify(TestRadosClient *rados_client,
int64_t pool_id, const std::string& nspace,
const std::string &oid,
const bufferlist &bl, bufferlist *pbl,
Context *on_notify) {
CephContext *cct = rados_client->cct();
m_lock.lock();
uint64_t notify_id = ++m_notify_id;
SharedWatcher watcher = get_watcher(pool_id, nspace, oid);
if (!watcher) {
ldout(cct, 1) << "oid=" << oid << ": not found" << dendl;
m_lock.unlock();
on_notify->complete(-ENOENT);
return;
}
ldout(cct, 20) << "oid=" << oid << ": notify_id=" << notify_id << dendl;
SharedNotifyHandle notify_handle(new NotifyHandle());
notify_handle->rados_client = rados_client;
notify_handle->pbl = pbl;
notify_handle->on_notify = on_notify;
WatchHandles &watch_handles = watcher->watch_handles;
for (auto &watch_handle_pair : watch_handles) {
WatchHandle &watch_handle = watch_handle_pair.second;
notify_handle->pending_watcher_ids.insert(std::make_pair(
watch_handle.gid, watch_handle.handle));
m_async_op_tracker.start_op();
uint64_t notifier_id = rados_client->get_instance_id();
watch_handle.rados_client->get_aio_finisher()->queue(new LambdaContext(
[this, pool_id, nspace, oid, bl, notify_id, watch_handle, notifier_id](int r) {
bufferlist notify_bl;
notify_bl.append(bl);
if (watch_handle.watch_ctx2 != NULL) {
watch_handle.watch_ctx2->handle_notify(notify_id,
watch_handle.handle,
notifier_id, notify_bl);
} else if (watch_handle.watch_ctx != NULL) {
watch_handle.watch_ctx->notify(0, 0, notify_bl);
// auto ack old-style watch/notify clients
ack_notify(watch_handle.rados_client, pool_id, nspace, oid, notify_id,
{watch_handle.gid, watch_handle.handle}, bufferlist());
}
m_async_op_tracker.finish_op();
}));
}
watcher->notify_handles[notify_id] = notify_handle;
finish_notify(rados_client, pool_id, nspace, oid, notify_id);
m_lock.unlock();
}
void TestWatchNotify::ack_notify(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace,
const std::string &oid, uint64_t notify_id,
const WatcherID &watcher_id,
const bufferlist &bl) {
CephContext *cct = rados_client->cct();
ceph_assert(ceph_mutex_is_locked(m_lock));
SharedWatcher watcher = get_watcher(pool_id, nspace, oid);
if (!watcher) {
ldout(cct, 1) << "oid=" << oid << ": not found" << dendl;
return;
}
NotifyHandles::iterator it = watcher->notify_handles.find(notify_id);
if (it == watcher->notify_handles.end()) {
ldout(cct, 1) << "oid=" << oid << ", notify_id=" << notify_id
<< ", WatcherID=" << watcher_id << ": not found" << dendl;
return;
}
ldout(cct, 20) << "oid=" << oid << ", notify_id=" << notify_id
<< ", WatcherID=" << watcher_id << dendl;
bufferlist response;
response.append(bl);
SharedNotifyHandle notify_handle = it->second;
notify_handle->notify_responses[watcher_id] = response;
notify_handle->pending_watcher_ids.erase(watcher_id);
}
void TestWatchNotify::finish_notify(TestRadosClient *rados_client,
int64_t pool_id, const std::string& nspace,
const std::string &oid,
uint64_t notify_id) {
CephContext *cct = rados_client->cct();
ldout(cct, 20) << "oid=" << oid << ", notify_id=" << notify_id << dendl;
ceph_assert(ceph_mutex_is_locked(m_lock));
SharedWatcher watcher = get_watcher(pool_id, nspace, oid);
if (!watcher) {
ldout(cct, 1) << "oid=" << oid << ": not found" << dendl;
return;
}
NotifyHandles::iterator it = watcher->notify_handles.find(notify_id);
if (it == watcher->notify_handles.end()) {
ldout(cct, 1) << "oid=" << oid << ", notify_id=" << notify_id
<< ": not found" << dendl;
return;
}
SharedNotifyHandle notify_handle = it->second;
if (!notify_handle->pending_watcher_ids.empty()) {
ldout(cct, 10) << "oid=" << oid << ", notify_id=" << notify_id
<< ": pending watchers, returning" << dendl;
return;
}
ldout(cct, 20) << "oid=" << oid << ", notify_id=" << notify_id
<< ": completing" << dendl;
if (notify_handle->pbl != NULL) {
encode(notify_handle->notify_responses, *notify_handle->pbl);
encode(notify_handle->pending_watcher_ids, *notify_handle->pbl);
}
notify_handle->rados_client->get_aio_finisher()->queue(
notify_handle->on_notify, 0);
watcher->notify_handles.erase(notify_id);
maybe_remove_watcher(watcher);
}
void TestWatchNotify::blocklist(uint32_t nonce) {
std::lock_guard locker{m_lock};
for (auto file_it = m_file_watchers.begin();
file_it != m_file_watchers.end(); ) {
auto &watcher = file_it->second;
for (auto w_it = watcher->watch_handles.begin();
w_it != watcher->watch_handles.end();) {
auto& watch_handle = w_it->second;
if (watch_handle.nonce == nonce) {
auto handle = watch_handle.handle;
auto watch_ctx2 = watch_handle.watch_ctx2;
if (watch_ctx2 != nullptr) {
auto ctx = new LambdaContext([handle, watch_ctx2](int) {
watch_ctx2->handle_error(handle, -ENOTCONN);
});
watch_handle.rados_client->get_aio_finisher()->queue(ctx);
}
w_it = watcher->watch_handles.erase(w_it);
} else {
++w_it;
}
}
++file_it;
maybe_remove_watcher(watcher);
}
}
void TestWatchNotify::handle_object_removed(int64_t pool_id,
const std::string& nspace,
const std::string& oid) {
std::lock_guard locker{m_lock};
auto it = m_file_watchers.find({pool_id, nspace, oid});
if (it == m_file_watchers.end()) {
return;
}
auto watcher = it->second;
// cancel all in-flight notifications
for (auto& notify_handle_pair : watcher->notify_handles) {
auto notify_handle = notify_handle_pair.second;
notify_handle->rados_client->get_aio_finisher()->queue(
notify_handle->on_notify, -ENOENT);
}
// alert all watchers of the loss of connection
for (auto& watch_handle_pair : watcher->watch_handles) {
auto& watch_handle = watch_handle_pair.second;
auto handle = watch_handle.handle;
auto watch_ctx2 = watch_handle.watch_ctx2;
if (watch_ctx2 != nullptr) {
auto ctx = new LambdaContext([handle, watch_ctx2](int) {
watch_ctx2->handle_error(handle, -ENOTCONN);
});
watch_handle.rados_client->get_aio_finisher()->queue(ctx);
}
}
m_file_watchers.erase(it);
}
} // namespace librados
| 16,552 | 34.984783 | 87 |
cc
|
null |
ceph-main/src/test/librados_test_stub/TestWatchNotify.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_WATCH_NOTIFY_H
#define CEPH_TEST_WATCH_NOTIFY_H
#include "include/rados/librados.hpp"
#include "common/AsyncOpTracker.h"
#include "common/ceph_mutex.h"
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <list>
#include <map>
class Finisher;
namespace librados {
class TestCluster;
class TestRadosClient;
class TestWatchNotify : boost::noncopyable {
public:
typedef std::pair<uint64_t, uint64_t> WatcherID;
typedef std::set<WatcherID> WatcherIDs;
typedef std::map<std::pair<uint64_t, uint64_t>, bufferlist> NotifyResponses;
struct NotifyHandle {
TestRadosClient *rados_client = nullptr;
WatcherIDs pending_watcher_ids;
NotifyResponses notify_responses;
bufferlist *pbl = nullptr;
Context *on_notify = nullptr;
};
typedef boost::shared_ptr<NotifyHandle> SharedNotifyHandle;
typedef std::map<uint64_t, SharedNotifyHandle> NotifyHandles;
struct WatchHandle {
TestRadosClient *rados_client = nullptr;
std::string addr;
uint32_t nonce;
uint64_t gid;
uint64_t handle;
librados::WatchCtx* watch_ctx;
librados::WatchCtx2* watch_ctx2;
};
typedef std::map<uint64_t, WatchHandle> WatchHandles;
struct ObjectHandler;
typedef boost::shared_ptr<ObjectHandler> SharedObjectHandler;
struct Watcher {
Watcher(int64_t pool_id, const std::string& nspace, const std::string& oid)
: pool_id(pool_id), nspace(nspace), oid(oid) {
}
int64_t pool_id;
std::string nspace;
std::string oid;
SharedObjectHandler object_handler;
WatchHandles watch_handles;
NotifyHandles notify_handles;
};
typedef boost::shared_ptr<Watcher> SharedWatcher;
TestWatchNotify(TestCluster* test_cluster);
int list_watchers(int64_t pool_id, const std::string& nspace,
const std::string& o, std::list<obj_watch_t> *out_watchers);
void aio_flush(TestRadosClient *rados_client, Context *on_finish);
void aio_watch(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& o, uint64_t gid,
uint64_t *handle, librados::WatchCtx *watch_ctx,
librados::WatchCtx2 *watch_ctx2, Context *on_finish);
void aio_unwatch(TestRadosClient *rados_client, uint64_t handle,
Context *on_finish);
void aio_notify(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& oid,
const bufferlist& bl, uint64_t timeout_ms, bufferlist *pbl,
Context *on_notify);
void flush(TestRadosClient *rados_client);
int notify(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& o, bufferlist& bl,
uint64_t timeout_ms, bufferlist *pbl);
void notify_ack(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& o,
uint64_t notify_id, uint64_t handle, uint64_t gid,
bufferlist& bl);
int watch(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& o, uint64_t gid,
uint64_t *handle, librados::WatchCtx *ctx,
librados::WatchCtx2 *ctx2);
int unwatch(TestRadosClient *rados_client, uint64_t handle);
void blocklist(uint32_t nonce);
private:
typedef std::tuple<int64_t, std::string, std::string> PoolFile;
typedef std::map<PoolFile, SharedWatcher> FileWatchers;
TestCluster *m_test_cluster;
uint64_t m_handle = 0;
uint64_t m_notify_id = 0;
ceph::mutex m_lock =
ceph::make_mutex("librados::TestWatchNotify::m_lock");
AsyncOpTracker m_async_op_tracker;
FileWatchers m_file_watchers;
SharedWatcher get_watcher(int64_t pool_id, const std::string& nspace,
const std::string& oid);
void maybe_remove_watcher(SharedWatcher shared_watcher);
void execute_watch(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string& o,
uint64_t gid, uint64_t *handle,
librados::WatchCtx *watch_ctx,
librados::WatchCtx2 *watch_ctx2,
Context *on_finish);
void execute_unwatch(TestRadosClient *rados_client, uint64_t handle,
Context *on_finish);
void execute_notify(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string &oid,
const bufferlist &bl, bufferlist *pbl,
Context *on_notify);
void ack_notify(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string &oid,
uint64_t notify_id, const WatcherID &watcher_id,
const bufferlist &bl);
void finish_notify(TestRadosClient *rados_client, int64_t pool_id,
const std::string& nspace, const std::string &oid,
uint64_t notify_id);
void handle_object_removed(int64_t pool_id, const std::string& nspace,
const std::string& oid);
};
} // namespace librados
#endif // CEPH_TEST_WATCH_NOTIFY_H
| 5,364 | 35.006711 | 80 |
h
|
null |
ceph-main/src/test/libradosstriper/TestCase.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <errno.h>
#include "test/librados/test.h"
#include "test/librados/test_cxx.h"
#include "test/libradosstriper/TestCase.h"
using namespace libradosstriper;
std::string StriperTest::pool_name;
rados_t StriperTest::s_cluster = NULL;
void StriperTest::SetUpTestCase()
{
pool_name = get_temp_pool_name();
ASSERT_EQ("", create_one_pool(pool_name, &s_cluster));
}
void StriperTest::TearDownTestCase()
{
ASSERT_EQ(0, destroy_one_pool(pool_name, &s_cluster));
}
void StriperTest::SetUp()
{
cluster = StriperTest::s_cluster;
ASSERT_EQ(0, rados_ioctx_create(cluster, pool_name.c_str(), &ioctx));
ASSERT_EQ(0, rados_striper_create(ioctx, &striper));
}
void StriperTest::TearDown()
{
rados_striper_destroy(striper);
rados_ioctx_destroy(ioctx);
}
std::string StriperTestPP::pool_name;
librados::Rados StriperTestPP::s_cluster;
void StriperTestPP::SetUpTestCase()
{
pool_name = get_temp_pool_name();
ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
}
void StriperTestPP::TearDownTestCase()
{
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
}
void StriperTestPP::SetUp()
{
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
ASSERT_EQ(0, RadosStriper::striper_create(ioctx, &striper));
}
// this is pure copy and paste from previous class
// but for the inheritance from TestWithParam
// with gtest >= 1.6, we couldd avoid this by using
// inheritance from WithParamInterface
std::string StriperTestParam::pool_name;
librados::Rados StriperTestParam::s_cluster;
void StriperTestParam::SetUpTestCase()
{
pool_name = get_temp_pool_name();
ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
}
void StriperTestParam::TearDownTestCase()
{
ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
}
void StriperTestParam::SetUp()
{
ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
ASSERT_EQ(0, RadosStriper::striper_create(ioctx, &striper));
}
| 2,025 | 24.012346 | 71 |
cc
|
null |
ceph-main/src/test/libradosstriper/TestCase.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_RADOS_TESTCASE_H
#define CEPH_TEST_RADOS_TESTCASE_H
#include "include/rados/librados.h"
#include "include/rados/librados.hpp"
#include "include/radosstriper/libradosstriper.h"
#include "include/radosstriper/libradosstriper.hpp"
#include "gtest/gtest.h"
#include <string>
/**
* These test cases create a temporary pool that lives as long as the
* test case. Each test within a test case gets a new ioctx and striper
* set to a unique namespace within the pool.
*
* Since pool creation and deletion is slow, this allows many tests to
* run faster.
*/
class StriperTest : public ::testing::Test {
public:
StriperTest() {}
~StriperTest() override {}
protected:
static void SetUpTestCase();
static void TearDownTestCase();
static rados_t s_cluster;
static std::string pool_name;
void SetUp() override;
void TearDown() override;
rados_t cluster = NULL;
rados_ioctx_t ioctx = NULL;
rados_striper_t striper = NULL;
};
class StriperTestPP : public ::testing::Test {
public:
StriperTestPP() : cluster(s_cluster) {}
~StriperTestPP() override {}
static void SetUpTestCase();
static void TearDownTestCase();
protected:
static librados::Rados s_cluster;
static std::string pool_name;
void SetUp() override;
librados::Rados &cluster;
librados::IoCtx ioctx;
libradosstriper::RadosStriper striper;
};
struct TestData {
uint32_t stripe_unit;
uint32_t stripe_count;
uint32_t object_size;
size_t size;
};
// this is pure copy and paste from previous class
// but for the inheritance from TestWithParam
// with gtest >= 1.6, we couldd avoid this by using
// inheritance from WithParamInterface
class StriperTestParam : public ::testing::TestWithParam<TestData> {
public:
StriperTestParam() : cluster(s_cluster) {}
~StriperTestParam() override {}
static void SetUpTestCase();
static void TearDownTestCase();
protected:
static librados::Rados s_cluster;
static std::string pool_name;
void SetUp() override;
librados::Rados &cluster;
librados::IoCtx ioctx;
libradosstriper::RadosStriper striper;
};
#endif
| 2,186 | 25.349398 | 72 |
h
|
null |
ceph-main/src/test/libradosstriper/aio.cc
|
#include "include/rados/librados.h"
#include "include/rados/librados.hpp"
#include "include/radosstriper/libradosstriper.h"
#include "include/radosstriper/libradosstriper.hpp"
#include "test/librados/test.h"
#include "test/libradosstriper/TestCase.h"
#include <boost/scoped_ptr.hpp>
#include <fcntl.h>
#include <semaphore.h>
#include <errno.h>
using namespace librados;
using namespace libradosstriper;
using std::pair;
class AioTestData
{
public:
AioTestData() : m_complete(false) {
sem_init(&m_sem, 0, 0);
}
~AioTestData() {
sem_destroy(&m_sem);
}
void notify() {
sem_post(&m_sem);
}
void wait() {
sem_wait(&m_sem);
}
bool m_complete;
private:
sem_t m_sem;
};
void set_completion_complete(rados_completion_t cb, void *arg)
{
AioTestData *test = static_cast<AioTestData*>(arg);
test->m_complete = true;
test->notify();
}
TEST_F(StriperTest, SimpleWrite) {
AioTestData test_data;
rados_completion_t my_completion;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_write(striper, "StriperTest", my_completion, buf, sizeof(buf), 0));
TestAlarm alarm;
test_data.wait();
rados_aio_release(my_completion);
}
TEST_F(StriperTestPP, SimpleWritePP) {
AioTestData test_data;
AioCompletion *my_completion = librados::Rados::aio_create_completion
((void*)&test_data, set_completion_complete);
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.aio_write("SimpleWritePP", my_completion, bl1, sizeof(buf), 0));
TestAlarm alarm;
test_data.wait();
my_completion->release();
}
TEST_F(StriperTest, WaitForSafe) {
AioTestData test_data;
rados_completion_t my_completion;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_write(striper, "WaitForSafe", my_completion, buf, sizeof(buf), 0));
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion);
test_data.wait();
rados_aio_release(my_completion);
}
TEST_F(StriperTestPP, WaitForSafePP) {
AioTestData test_data;
AioCompletion *my_completion =
librados::Rados::aio_create_completion(&test_data,
set_completion_complete);
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.aio_write("WaitForSafePP", my_completion, bl1, sizeof(buf), 0));
TestAlarm alarm;
my_completion->wait_for_complete();
test_data.wait();
my_completion->release();
}
TEST_F(StriperTest, RoundTrip) {
AioTestData test_data;
rados_completion_t my_completion;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_write(striper, "RoundTrip", my_completion, buf, sizeof(buf), 0));
{
TestAlarm alarm;
test_data.wait();
}
char buf2[128];
memset(buf2, 0, sizeof(buf2));
rados_completion_t my_completion2;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion2));
ASSERT_EQ(0, rados_striper_aio_read(striper, "RoundTrip", my_completion2, buf2, sizeof(buf2), 0));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion2);
}
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
test_data.wait();
rados_aio_release(my_completion);
rados_aio_release(my_completion2);
}
TEST_F(StriperTest, RoundTrip2) {
AioTestData test_data;
rados_completion_t my_completion;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_write(striper, "RoundTrip2", my_completion, buf, sizeof(buf), 0));
{
TestAlarm alarm;
test_data.wait();
}
char buf2[128];
memset(buf2, 0, sizeof(buf2));
rados_completion_t my_completion2;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion2));
ASSERT_EQ(0, rados_striper_aio_read(striper, "RoundTrip2", my_completion2, buf2, sizeof(buf2), 0));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion2);
}
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
test_data.wait();
rados_aio_release(my_completion);
rados_aio_release(my_completion2);
}
TEST_F(StriperTestPP, RoundTripPP) {
AioTestData test_data;
AioCompletion *my_completion =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.aio_write("RoundTripPP", my_completion, bl1, sizeof(buf), 0));
{
TestAlarm alarm;
test_data.wait();
}
bufferlist bl2;
AioCompletion *my_completion2 =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
ASSERT_EQ(0, striper.aio_read("RoundTripPP", my_completion2, &bl2, sizeof(buf), 0));
{
TestAlarm alarm;
my_completion2->wait_for_complete();
}
ASSERT_EQ(0, memcmp(buf, bl2.c_str(), sizeof(buf)));
test_data.wait();
my_completion->release();
my_completion2->release();
}
TEST_F(StriperTestPP, RoundTripPP2) {
AioTestData test_data;
AioCompletion *my_completion =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.aio_write("RoundTripPP2", my_completion, bl1, sizeof(buf), 0));
{
TestAlarm alarm;
test_data.wait();
}
bufferlist bl2;
AioCompletion *my_completion2 =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
ASSERT_EQ(0, striper.aio_read("RoundTripPP2", my_completion2, &bl2, sizeof(buf), 0));
{
TestAlarm alarm;
my_completion2->wait_for_complete();
}
ASSERT_EQ(0, memcmp(buf, bl2.c_str(), sizeof(buf)));
test_data.wait();
my_completion->release();
my_completion2->release();
}
TEST_F(StriperTest, IsComplete) {
AioTestData test_data;
rados_completion_t my_completion;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_write(striper, "IsComplete", my_completion, buf, sizeof(buf), 0));
{
TestAlarm alarm;
test_data.wait();
}
char buf2[128];
memset(buf2, 0, sizeof(buf2));
rados_completion_t my_completion2;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion2));
ASSERT_EQ(0, rados_striper_aio_read(striper, "IsComplete", my_completion2, buf2, sizeof(buf2), 0));
{
TestAlarm alarm;
// Busy-wait until the AIO completes.
// Normally we wouldn't do this, but we want to test rados_aio_is_complete.
while (true) {
int is_complete = rados_aio_is_complete(my_completion2);
if (is_complete)
break;
}
}
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
test_data.wait();
rados_aio_release(my_completion);
rados_aio_release(my_completion2);
}
TEST_F(StriperTestPP, IsCompletePP) {
AioTestData test_data;
AioCompletion *my_completion =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.aio_write("IsCompletePP", my_completion, bl1, sizeof(buf), 0));
{
TestAlarm alarm;
test_data.wait();
}
bufferlist bl2;
AioCompletion *my_completion2 =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
ASSERT_EQ(0, striper.aio_read("IsCompletePP", my_completion2, &bl2, sizeof(buf), 0));
{
TestAlarm alarm;
// Busy-wait until the AIO completes.
// Normally we wouldn't do this, but we want to test rados_aio_is_complete.
while (true) {
int is_complete = my_completion2->is_complete();
if (is_complete)
break;
}
}
ASSERT_EQ(0, memcmp(buf, bl2.c_str(), sizeof(buf)));
test_data.wait();
my_completion->release();
my_completion2->release();
}
TEST_F(StriperTest, IsSafe) {
AioTestData test_data;
rados_completion_t my_completion;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_write(striper, "IsSafe", my_completion, buf, sizeof(buf), 0));
{
TestAlarm alarm;
// Busy-wait until the AIO completes.
// Normally we wouldn't do this, but we want to test rados_aio_is_safe.
while (true) {
int is_safe = rados_aio_is_safe(my_completion);
if (is_safe)
break;
}
}
test_data.wait();
char buf2[128];
memset(buf2, 0, sizeof(buf2));
rados_completion_t my_completion2;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion2));
ASSERT_EQ(0, rados_striper_aio_read(striper, "IsSafe", my_completion2, buf2, sizeof(buf2), 0));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion2);
}
test_data.wait();
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
rados_aio_release(my_completion);
rados_aio_release(my_completion2);
}
TEST_F(StriperTest, RoundTripAppend) {
AioTestData test_data;
rados_completion_t my_completion, my_completion2, my_completion3;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_append(striper, "RoundTripAppend", my_completion, buf, sizeof(buf)));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion);
}
test_data.wait();
char buf2[128];
memset(buf2, 0xdd, sizeof(buf2));
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion2));
ASSERT_EQ(0, rados_striper_aio_append(striper, "RoundTripAppend", my_completion2, buf2, sizeof(buf)));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion2);
}
test_data.wait();
char buf3[sizeof(buf) + sizeof(buf2)];
memset(buf3, 0, sizeof(buf3));
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion3));
ASSERT_EQ(0, rados_striper_aio_read(striper, "RoundTripAppend", my_completion3, buf3, sizeof(buf3), 0));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion3);
}
ASSERT_EQ((int)(sizeof(buf) + sizeof(buf2)), rados_aio_get_return_value(my_completion3));
ASSERT_EQ(0, memcmp(buf3, buf, sizeof(buf)));
ASSERT_EQ(0, memcmp(buf3 + sizeof(buf), buf2, sizeof(buf2)));
test_data.wait();
rados_aio_release(my_completion);
rados_aio_release(my_completion2);
rados_aio_release(my_completion3);
}
TEST_F(StriperTestPP, RoundTripAppendPP) {
AioTestData test_data;
AioCompletion *my_completion =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.aio_append("RoundTripAppendPP", my_completion, bl1, sizeof(buf)));
{
TestAlarm alarm;
my_completion->wait_for_complete();
}
test_data.wait();
char buf2[128];
memset(buf2, 0xdd, sizeof(buf2));
bufferlist bl2;
bl2.append(buf2, sizeof(buf2));
AioCompletion *my_completion2 =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
ASSERT_EQ(0, striper.aio_append("RoundTripAppendPP", my_completion2, bl2, sizeof(buf2)));
{
TestAlarm alarm;
my_completion2->wait_for_complete();
}
test_data.wait();
bufferlist bl3;
AioCompletion *my_completion3 =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
ASSERT_EQ(0, striper.aio_read("RoundTripAppendPP", my_completion3, &bl3, 2 * sizeof(buf), 0));
{
TestAlarm alarm;
my_completion3->wait_for_complete();
}
test_data.wait();
ASSERT_EQ(sizeof(buf) + sizeof(buf2), (unsigned)my_completion3->get_return_value());
ASSERT_EQ(0, memcmp(bl3.c_str(), buf, sizeof(buf)));
ASSERT_EQ(0, memcmp(bl3.c_str() + sizeof(buf), buf2, sizeof(buf2)));
my_completion->release();
my_completion2->release();
my_completion3->release();
}
TEST_F(StriperTest, Flush) {
AioTestData test_data;
rados_completion_t my_completion;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xee, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_write(striper, "Flush", my_completion, buf, sizeof(buf), 0));
rados_striper_aio_flush(striper);
test_data.wait();
char buf2[128];
memset(buf2, 0, sizeof(buf2));
rados_completion_t my_completion2;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion2));
ASSERT_EQ(0, rados_striper_aio_read(striper, "Flush", my_completion2, buf2, sizeof(buf2), 0));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion2);
}
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
test_data.wait();
rados_aio_release(my_completion);
rados_aio_release(my_completion2);
}
TEST_F(StriperTestPP, FlushPP) {
AioTestData test_data;
AioCompletion *my_completion =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
char buf[128];
memset(buf, 0xee, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.aio_write("FlushPP", my_completion, bl1, sizeof(buf), 0));
striper.aio_flush();
test_data.wait();
bufferlist bl2;
AioCompletion *my_completion2 =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
ASSERT_EQ(0, striper.aio_read("FlushPP", my_completion2, &bl2, sizeof(buf), 0));
{
TestAlarm alarm;
my_completion2->wait_for_complete();
}
ASSERT_EQ(0, memcmp(buf, bl2.c_str(), sizeof(buf)));
test_data.wait();
my_completion->release();
my_completion2->release();
}
TEST_F(StriperTest, RoundTripWriteFull) {
AioTestData test_data;
rados_completion_t my_completion, my_completion2, my_completion3;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_aio_write(striper, "RoundTripWriteFull", my_completion, buf, sizeof(buf), 0));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion);
}
test_data.wait();
char buf2[64];
memset(buf2, 0xdd, sizeof(buf2));
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion2));
ASSERT_EQ(0, rados_striper_aio_write_full(striper, "RoundTripWriteFull", my_completion2, buf2, sizeof(buf2)));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion2);
}
test_data.wait();
char buf3[sizeof(buf) + sizeof(buf2)];
memset(buf3, 0, sizeof(buf3));
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion3));
ASSERT_EQ(0, rados_striper_aio_read(striper, "RoundTripWriteFull", my_completion3, buf3, sizeof(buf3), 0));
{
TestAlarm alarm;
rados_aio_wait_for_complete(my_completion3);
}
ASSERT_EQ(sizeof(buf2), (unsigned)rados_aio_get_return_value(my_completion3));
ASSERT_EQ(0, memcmp(buf3, buf2, sizeof(buf2)));
test_data.wait();
rados_aio_release(my_completion);
rados_aio_release(my_completion2);
rados_aio_release(my_completion3);
}
TEST_F(StriperTestPP, RoundTripWriteFullPP) {
AioTestData test_data;
AioCompletion *my_completion =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.aio_write("RoundTripWriteFullPP", my_completion, bl1, sizeof(buf), 0));
{
TestAlarm alarm;
my_completion->wait_for_complete();
}
test_data.wait();
char buf2[64];
memset(buf2, 0xdd, sizeof(buf2));
bufferlist bl2;
bl2.append(buf2, sizeof(buf2));
AioCompletion *my_completion2 =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
ASSERT_EQ(0, striper.aio_write_full("RoundTripWriteFullPP", my_completion2, bl2));
{
TestAlarm alarm;
my_completion2->wait_for_complete();
}
test_data.wait();
bufferlist bl3;
AioCompletion *my_completion3 =
librados::Rados::aio_create_completion(&test_data, set_completion_complete);
ASSERT_EQ(0, striper.aio_read("RoundTripWriteFullPP", my_completion3, &bl3, sizeof(buf), 0));
{
TestAlarm alarm;
my_completion3->wait_for_complete();
}
ASSERT_EQ(sizeof(buf2), (unsigned)my_completion3->get_return_value());
ASSERT_EQ(0, memcmp(bl3.c_str(), buf2, sizeof(buf2)));
test_data.wait();
my_completion->release();
my_completion2->release();
my_completion3->release();
}
TEST_F(StriperTest, RemoveTest) {
char buf[128];
char buf2[sizeof(buf)];
// create oabject
memset(buf, 0xaa, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "RemoveTest", buf, sizeof(buf), 0));
// async remove it
AioTestData test_data;
rados_completion_t my_completion;
ASSERT_EQ(0, rados_aio_create_completion2(&test_data,
set_completion_complete,
&my_completion));
ASSERT_EQ(0, rados_striper_aio_remove(striper, "RemoveTest", my_completion));
{
TestAlarm alarm;
ASSERT_EQ(0, rados_aio_wait_for_complete(my_completion));
}
test_data.wait();
ASSERT_EQ(0, rados_aio_get_return_value(my_completion));
rados_aio_release(my_completion);
// check we get ENOENT on reading
ASSERT_EQ(-ENOENT, rados_striper_read(striper, "RemoveTest", buf2, sizeof(buf2), 0));
}
TEST_F(StriperTestPP, RemoveTestPP) {
char buf[128];
memset(buf, 0xaa, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("RemoveTestPP", bl, sizeof(buf), 0));
AioCompletion *my_completion = cluster.aio_create_completion(nullptr, nullptr);
ASSERT_EQ(0, striper.aio_remove("RemoveTestPP", my_completion));
{
TestAlarm alarm;
ASSERT_EQ(0, my_completion->wait_for_complete());
}
ASSERT_EQ(0, my_completion->get_return_value());
bufferlist bl2;
ASSERT_EQ(-ENOENT, striper.read("RemoveTestPP", &bl2, sizeof(buf), 0));
my_completion->release();
}
| 20,187 | 32.929412 | 112 |
cc
|
null |
ceph-main/src/test/libradosstriper/io.cc
|
#include "include/rados/librados.h"
#include "include/rados/librados.hpp"
#include "include/radosstriper/libradosstriper.h"
#include "include/radosstriper/libradosstriper.hpp"
#include "test/librados/test.h"
#include "test/libradosstriper/TestCase.h"
#include <fcntl.h>
#include <errno.h>
#include "gtest/gtest.h"
using namespace librados;
using namespace libradosstriper;
using std::string;
TEST_F(StriperTest, SimpleWrite) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "SimpleWrite", buf, sizeof(buf), 0));
}
TEST_F(StriperTestPP, SimpleWritePP) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("SimpleWritePP", bl, sizeof(buf), 0));
}
TEST_F(StriperTest, SimpleWriteFull) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_write_full(striper, "SimpleWrite", buf, sizeof(buf)));
}
TEST_F(StriperTestPP, SimpleWriteFullPP) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write_full("SimpleWritePP", bl));
}
TEST_F(StriperTest, Stat) {
uint64_t size = 0;
time_t mtime = 0;
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "Stat", buf, sizeof(buf), 0));
ASSERT_EQ(0, rados_striper_stat(striper, "Stat", &size, &mtime));
ASSERT_EQ(size, sizeof(buf));
ASSERT_EQ(-ENOENT, rados_striper_stat(striper, "nonexistent", &size, &mtime));
}
TEST_F(StriperTest, Stat2) {
uint64_t size = 0;
struct timespec mtime = {};
char buf[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "Stat2", buf, sizeof(buf), 0));
ASSERT_EQ(0, rados_striper_stat2(striper, "Stat2", &size, &mtime));
ASSERT_EQ(size, sizeof(buf));
ASSERT_EQ(-ENOENT, rados_striper_stat2(striper, "nonexistent", &size, &mtime));
}
TEST_F(StriperTestPP, StatPP) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("Statpp", bl, sizeof(buf), 0));
uint64_t size = 0;
time_t mtime = 0;
ASSERT_EQ(0, striper.stat("Statpp", &size, &mtime));
ASSERT_EQ(size, sizeof(buf));
ASSERT_EQ(-ENOENT, striper.stat("nonexistent", &size, &mtime));
}
TEST_F(StriperTestPP, Stat2PP) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("Stat2pp", bl, sizeof(buf), 0));
uint64_t size = 0;
struct timespec mtime = {};
ASSERT_EQ(0, striper.stat2("Stat2pp", &size, &mtime));
ASSERT_EQ(size, sizeof(buf));
ASSERT_EQ(-ENOENT, striper.stat2("nonexistent", &size, &mtime));
}
TEST_F(StriperTest, RoundTrip) {
char buf[128];
char buf2[sizeof(buf)];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "RoundTrip", buf, sizeof(buf), 0));
memset(buf2, 0, sizeof(buf2));
ASSERT_EQ((int)sizeof(buf2), rados_striper_read(striper, "RoundTrip", buf2, sizeof(buf2), 0));
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
}
TEST_F(StriperTestPP, RoundTripPP) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("RoundTripPP", bl, sizeof(buf), 0));
bufferlist cl;
ASSERT_EQ((int)sizeof(buf), striper.read("RoundTripPP", &cl, sizeof(buf), 0));
ASSERT_EQ(0, memcmp(buf, cl.c_str(), sizeof(buf)));
}
TEST_F(StriperTest, OverlappingWriteRoundTrip) {
char buf[128];
char buf2[64];
char buf3[sizeof(buf)];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "OverlappingWriteRoundTrip", buf, sizeof(buf), 0));
memset(buf2, 0xdd, sizeof(buf2));
ASSERT_EQ(0, rados_striper_write(striper, "OverlappingWriteRoundTrip", buf2, sizeof(buf2), 0));
memset(buf3, 0, sizeof(buf3));
ASSERT_EQ((int)sizeof(buf3), rados_striper_read(striper, "OverlappingWriteRoundTrip", buf3, sizeof(buf3), 0));
ASSERT_EQ(0, memcmp(buf3, buf2, sizeof(buf2)));
ASSERT_EQ(0, memcmp(buf3 + sizeof(buf2), buf, sizeof(buf) - sizeof(buf2)));
}
TEST_F(StriperTestPP, OverlappingWriteRoundTripPP) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("OverlappingWriteRoundTripPP", bl1, sizeof(buf), 0));
char buf2[64];
memset(buf2, 0xdd, sizeof(buf2));
bufferlist bl2;
bl2.append(buf2, sizeof(buf2));
ASSERT_EQ(0, striper.write("OverlappingWriteRoundTripPP", bl2, sizeof(buf2), 0));
bufferlist bl3;
ASSERT_EQ((int)sizeof(buf), striper.read("OverlappingWriteRoundTripPP", &bl3, sizeof(buf), 0));
ASSERT_EQ(0, memcmp(bl3.c_str(), buf2, sizeof(buf2)));
ASSERT_EQ(0, memcmp(bl3.c_str() + sizeof(buf2), buf, sizeof(buf) - sizeof(buf2)));
}
TEST_F(StriperTest, SparseWriteRoundTrip) {
char buf[128];
char buf2[2*sizeof(buf)];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "SparseWriteRoundTrip", buf, sizeof(buf), 0));
ASSERT_EQ(0, rados_striper_write(striper, "SparseWriteRoundTrip", buf, sizeof(buf), 1000000000));
memset(buf2, 0xaa, sizeof(buf2));
ASSERT_EQ((int)sizeof(buf2), rados_striper_read(striper, "SparseWriteRoundTrip", buf2, sizeof(buf2), 0));
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
memset(buf, 0, sizeof(buf));
ASSERT_EQ(0, memcmp(buf, buf2+sizeof(buf), sizeof(buf)));
memset(buf2, 0xaa, sizeof(buf2));
ASSERT_EQ((int)sizeof(buf), rados_striper_read(striper, "SparseWriteRoundTrip", buf2, sizeof(buf), 500000000));
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
}
TEST_F(StriperTestPP, SparseWriteRoundTripPP) {
char buf[128];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("SparseWriteRoundTripPP", bl1, sizeof(buf), 0));
ASSERT_EQ(0, striper.write("SparseWriteRoundTripPP", bl1, sizeof(buf), 1000000000));
bufferlist bl2;
ASSERT_EQ((int)(2*sizeof(buf)), striper.read("SparseWriteRoundTripPP", &bl2, 2*sizeof(buf), 0));
ASSERT_EQ(0, memcmp(bl2.c_str(), buf, sizeof(buf)));
memset(buf, 0, sizeof(buf));
ASSERT_EQ(0, memcmp(bl2.c_str()+sizeof(buf), buf, sizeof(buf)));
ASSERT_EQ((int)sizeof(buf), striper.read("SparseWriteRoundTripPP", &bl2, sizeof(buf), 500000000));
ASSERT_EQ(0, memcmp(bl2.c_str(), buf, sizeof(buf)));
}
TEST_F(StriperTest, WriteFullRoundTrip) {
char buf[128];
char buf2[64];
char buf3[128];
memset(buf, 0xcc, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "WriteFullRoundTrip", buf, sizeof(buf), 0));
memset(buf2, 0xdd, sizeof(buf2));
ASSERT_EQ(0, rados_striper_write_full(striper, "WriteFullRoundTrip", buf2, sizeof(buf2)));
memset(buf3, 0x00, sizeof(buf3));
ASSERT_EQ((int)sizeof(buf2), rados_striper_read(striper, "WriteFullRoundTrip", buf3, sizeof(buf3), 0));
ASSERT_EQ(0, memcmp(buf2, buf3, sizeof(buf2)));
}
TEST_F(StriperTestPP, WriteFullRoundTripPP) {
char buf[128];
char buf2[64];
memset(buf, 0xcc, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("WriteFullRoundTripPP", bl1, sizeof(buf), 0));
memset(buf2, 0xdd, sizeof(buf2));
bufferlist bl2;
bl2.append(buf2, sizeof(buf2));
ASSERT_EQ(0, striper.write_full("WriteFullRoundTripPP", bl2));
bufferlist bl3;
ASSERT_EQ((int)sizeof(buf2), striper.read("WriteFullRoundTripPP", &bl3, sizeof(buf), 0));
ASSERT_EQ(0, memcmp(bl3.c_str(), buf2, sizeof(buf2)));
}
TEST_F(StriperTest, AppendRoundTrip) {
char buf[64];
char buf2[64];
char buf3[sizeof(buf) + sizeof(buf2)];
memset(buf, 0xde, sizeof(buf));
ASSERT_EQ(0, rados_striper_append(striper, "AppendRoundTrip", buf, sizeof(buf)));
memset(buf2, 0xad, sizeof(buf2));
ASSERT_EQ(0, rados_striper_append(striper, "AppendRoundTrip", buf2, sizeof(buf2)));
memset(buf3, 0, sizeof(buf3));
ASSERT_EQ((int)sizeof(buf3), rados_striper_read(striper, "AppendRoundTrip", buf3, sizeof(buf3), 0));
ASSERT_EQ(0, memcmp(buf3, buf, sizeof(buf)));
ASSERT_EQ(0, memcmp(buf3 + sizeof(buf), buf2, sizeof(buf2)));
}
TEST_F(StriperTestPP, AppendRoundTripPP) {
char buf[64];
char buf2[64];
memset(buf, 0xde, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.append("AppendRoundTripPP", bl1, sizeof(buf)));
memset(buf2, 0xad, sizeof(buf2));
bufferlist bl2;
bl2.append(buf2, sizeof(buf2));
ASSERT_EQ(0, striper.append("AppendRoundTripPP", bl2, sizeof(buf2)));
bufferlist bl3;
ASSERT_EQ((int)(sizeof(buf) + sizeof(buf2)),
striper.read("AppendRoundTripPP", &bl3, (sizeof(buf) + sizeof(buf2)), 0));
const char *bl3_str = bl3.c_str();
ASSERT_EQ(0, memcmp(bl3_str, buf, sizeof(buf)));
ASSERT_EQ(0, memcmp(bl3_str + sizeof(buf), buf2, sizeof(buf2)));
}
TEST_F(StriperTest, TruncTest) {
char buf[128];
char buf2[sizeof(buf)];
memset(buf, 0xaa, sizeof(buf));
ASSERT_EQ(0, rados_striper_append(striper, "TruncTest", buf, sizeof(buf)));
ASSERT_EQ(0, rados_striper_trunc(striper, "TruncTest", sizeof(buf) / 2));
memset(buf2, 0, sizeof(buf2));
ASSERT_EQ((int)(sizeof(buf)/2), rados_striper_read(striper, "TruncTest", buf2, sizeof(buf2), 0));
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)/2));
}
TEST_F(StriperTestPP, TruncTestPP) {
char buf[128];
memset(buf, 0xaa, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.append("TruncTestPP", bl, sizeof(buf)));
ASSERT_EQ(0, striper.trunc("TruncTestPP", sizeof(buf) / 2));
bufferlist bl2;
ASSERT_EQ((int)(sizeof(buf)/2), striper.read("TruncTestPP", &bl2, sizeof(buf), 0));
ASSERT_EQ(0, memcmp(bl2.c_str(), buf, sizeof(buf)/2));
}
TEST_F(StriperTest, TruncTestGrow) {
char buf[128];
char buf2[sizeof(buf)*2];
memset(buf, 0xaa, sizeof(buf));
ASSERT_EQ(0, rados_striper_append(striper, "TruncTestGrow", buf, sizeof(buf)));
ASSERT_EQ(0, rados_striper_trunc(striper, "TruncTestGrow", sizeof(buf2)));
memset(buf2, 0xbb, sizeof(buf2));
ASSERT_EQ((int)sizeof(buf2), rados_striper_read(striper, "TruncTestGrow", buf2, sizeof(buf2), 0));
ASSERT_EQ(0, memcmp(buf, buf2, sizeof(buf)));
memset(buf, 0x00, sizeof(buf));
ASSERT_EQ(0, memcmp(buf, buf2+sizeof(buf), sizeof(buf)));
}
TEST_F(StriperTestPP, TruncTestGrowPP) {
char buf[128];
memset(buf, 0xaa, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.append("TruncTestGrowPP", bl, sizeof(buf)));
ASSERT_EQ(0, striper.trunc("TruncTestGrowPP", sizeof(buf) * 2));
bufferlist bl2;
ASSERT_EQ(sizeof(buf)*2, (unsigned)striper.read("TruncTestGrowPP", &bl2, sizeof(buf)*2, 0));
ASSERT_EQ(0, memcmp(bl2.c_str(), buf, sizeof(buf)));
memset(buf, 0x00, sizeof(buf));
ASSERT_EQ(0, memcmp(bl2.c_str()+sizeof(buf), buf, sizeof(buf)));
}
TEST_F(StriperTest, RemoveTest) {
char buf[128];
char buf2[sizeof(buf)];
memset(buf, 0xaa, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "RemoveTest", buf, sizeof(buf), 0));
ASSERT_EQ(0, rados_striper_remove(striper, "RemoveTest"));
ASSERT_EQ(-ENOENT, rados_striper_read(striper, "RemoveTest", buf2, sizeof(buf2), 0));
}
TEST_F(StriperTestPP, RemoveTestPP) {
char buf[128];
memset(buf, 0xaa, sizeof(buf));
bufferlist bl;
bl.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("RemoveTestPP", bl, sizeof(buf), 0));
ASSERT_EQ(0, striper.remove("RemoveTestPP"));
bufferlist bl2;
ASSERT_EQ(-ENOENT, striper.read("RemoveTestPP", &bl2, sizeof(buf), 0));
}
TEST_F(StriperTest, XattrsRoundTrip) {
char buf[128];
char attr1_buf[] = "foo bar baz";
memset(buf, 0xaa, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "XattrsRoundTrip", buf, sizeof(buf), 0));
ASSERT_EQ(-ENODATA, rados_striper_getxattr(striper, "XattrsRoundTrip", "attr1", buf, sizeof(buf)));
ASSERT_EQ(0, rados_striper_setxattr(striper, "XattrsRoundTrip", "attr1", attr1_buf, sizeof(attr1_buf)));
ASSERT_EQ((int)sizeof(attr1_buf), rados_striper_getxattr(striper, "XattrsRoundTrip", "attr1", buf, sizeof(buf)));
ASSERT_EQ(0, memcmp(attr1_buf, buf, sizeof(attr1_buf)));
}
TEST_F(StriperTestPP, XattrsRoundTripPP) {
char buf[128];
memset(buf, 0xaa, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("XattrsRoundTripPP", bl1, sizeof(buf), 0));
char attr1_buf[] = "foo bar baz";
bufferlist bl2;
ASSERT_EQ(-ENODATA, striper.getxattr("XattrsRoundTripPP", "attr1", bl2));
bufferlist bl3;
bl3.append(attr1_buf, sizeof(attr1_buf));
ASSERT_EQ(0, striper.setxattr("XattrsRoundTripPP", "attr1", bl3));
bufferlist bl4;
ASSERT_EQ((int)sizeof(attr1_buf), striper.getxattr("XattrsRoundTripPP", "attr1", bl4));
ASSERT_EQ(0, memcmp(bl4.c_str(), attr1_buf, sizeof(attr1_buf)));
}
TEST_F(StriperTest, RmXattr) {
char buf[128];
char attr1_buf[] = "foo bar baz";
memset(buf, 0xaa, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "RmXattr", buf, sizeof(buf), 0));
ASSERT_EQ(0, rados_striper_setxattr(striper, "RmXattr", "attr1", attr1_buf, sizeof(attr1_buf)));
ASSERT_EQ(0, rados_striper_rmxattr(striper, "RmXattr", "attr1"));
ASSERT_EQ(-ENODATA, rados_striper_getxattr(striper, "RmXattr", "attr1", buf, sizeof(buf)));
}
TEST_F(StriperTestPP, RmXattrPP) {
char buf[128];
memset(buf, 0xaa, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("RmXattrPP", bl1, sizeof(buf), 0));
char attr1_buf[] = "foo bar baz";
bufferlist bl2;
bl2.append(attr1_buf, sizeof(attr1_buf));
ASSERT_EQ(0, striper.setxattr("RmXattrPP", "attr1", bl2));
ASSERT_EQ(0, striper.rmxattr("RmXattrPP", "attr1"));
bufferlist bl3;
ASSERT_EQ(-ENODATA, striper.getxattr("RmXattrPP", "attr1", bl3));
}
TEST_F(StriperTest, XattrIter) {
char buf[128];
char attr1_buf[] = "foo bar baz";
char attr2_buf[256];
for (size_t j = 0; j < sizeof(attr2_buf); ++j) {
attr2_buf[j] = j % 0xff;
}
memset(buf, 0xaa, sizeof(buf));
ASSERT_EQ(0, rados_striper_write(striper, "RmXattr", buf, sizeof(buf), 0));
ASSERT_EQ(0, rados_striper_setxattr(striper, "RmXattr", "attr1", attr1_buf, sizeof(attr1_buf)));
ASSERT_EQ(0, rados_striper_setxattr(striper, "RmXattr", "attr2", attr2_buf, sizeof(attr2_buf)));
rados_xattrs_iter_t iter;
ASSERT_EQ(0, rados_striper_getxattrs(striper, "RmXattr", &iter));
int num_seen = 0;
while (true) {
const char *name;
const char *val;
size_t len;
ASSERT_EQ(0, rados_striper_getxattrs_next(iter, &name, &val, &len));
if (name == NULL) {
break;
}
ASSERT_LT(num_seen, 2) << "Extra attribute : " << name;
if ((strcmp(name, "attr1") == 0) && (val != NULL) && (memcmp(val, attr1_buf, len) == 0)) {
num_seen++;
continue;
}
else if ((strcmp(name, "attr2") == 0) && (val != NULL) && (memcmp(val, attr2_buf, len) == 0)) {
num_seen++;
continue;
}
else {
ASSERT_EQ(0, 1) << "Unexpected attribute : " << name;;
}
}
rados_striper_getxattrs_end(iter);
}
TEST_F(StriperTestPP, XattrListPP) {
char buf[128];
memset(buf, 0xaa, sizeof(buf));
bufferlist bl1;
bl1.append(buf, sizeof(buf));
ASSERT_EQ(0, striper.write("RmXattrPP", bl1, sizeof(buf), 0));
char attr1_buf[] = "foo bar baz";
bufferlist bl2;
bl2.append(attr1_buf, sizeof(attr1_buf));
ASSERT_EQ(0, striper.setxattr("RmXattrPP", "attr1", bl2));
char attr2_buf[256];
for (size_t j = 0; j < sizeof(attr2_buf); ++j) {
attr2_buf[j] = j % 0xff;
}
bufferlist bl3;
bl3.append(attr2_buf, sizeof(attr2_buf));
ASSERT_EQ(0, striper.setxattr("RmXattrPP", "attr2", bl3));
std::map<std::string, bufferlist> attrset;
ASSERT_EQ(0, striper.getxattrs("RmXattrPP", attrset));
for (std::map<std::string, bufferlist>::iterator i = attrset.begin();
i != attrset.end(); ++i) {
if (i->first == string("attr1")) {
ASSERT_EQ(0, memcmp(i->second.c_str(), attr1_buf, sizeof(attr1_buf)));
}
else if (i->first == string("attr2")) {
ASSERT_EQ(0, memcmp(i->second.c_str(), attr2_buf, sizeof(attr2_buf)));
}
else {
ASSERT_EQ(0, 1) << "Unexpected attribute : " << i->first;
}
}
}
| 16,023 | 36.178654 | 115 |
cc
|
null |
ceph-main/src/test/libradosstriper/striping.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/compat.h"
#include "include/types.h"
#include "include/rados/librados.h"
#include "include/rados/librados.hpp"
#include "include/radosstriper/libradosstriper.h"
#include "include/radosstriper/libradosstriper.hpp"
#include "include/ceph_fs.h"
#include "test/librados/test.h"
#include "test/libradosstriper/TestCase.h"
#include <string>
#include <errno.h>
using namespace librados;
using namespace libradosstriper;
class StriperTestRT : public StriperTestParam {
public:
StriperTestRT() : StriperTestParam() {}
protected:
char* getObjName(const std::string& soid, uint64_t nb)
{
char name[soid.size()+18];
sprintf(name, "%s.%016llx", soid.c_str(), (long long unsigned int)nb);
return strdup(name);
}
void checkObjectFromRados(const std::string& soid, bufferlist &bl,
uint64_t exp_stripe_unit, uint64_t exp_stripe_count,
uint64_t exp_object_size, size_t size)
{
checkObjectFromRados(soid, bl, exp_stripe_unit, exp_stripe_count, exp_object_size, size, size);
}
void checkObjectFromRados(const std::string& soid, bufferlist &bl,
uint64_t exp_stripe_unit, uint64_t exp_stripe_count,
uint64_t exp_object_size, size_t size,
size_t actual_size_if_sparse)
{
// checking first object's rados xattrs
bufferlist xattrbl;
char* firstOid = getObjName(soid, 0);
ASSERT_LT(0, ioctx.getxattr(firstOid, "striper.layout.stripe_unit", xattrbl));
std::string s_xattr(xattrbl.c_str(), xattrbl.length()); // adds 0 byte at the end
uint64_t stripe_unit = strtoll(s_xattr.c_str(), NULL, 10);
ASSERT_LT((unsigned)0, stripe_unit);
ASSERT_EQ(stripe_unit, exp_stripe_unit);
xattrbl.clear();
ASSERT_LT(0, ioctx.getxattr(firstOid, "striper.layout.stripe_count", xattrbl));
s_xattr = std::string(xattrbl.c_str(), xattrbl.length()); // adds 0 byte at the end
uint64_t stripe_count = strtoll(s_xattr.c_str(), NULL, 10);
ASSERT_LT(0U, stripe_count);
ASSERT_EQ(stripe_count, exp_stripe_count);
xattrbl.clear();
ASSERT_LT(0, ioctx.getxattr(firstOid, "striper.layout.object_size", xattrbl));
s_xattr = std::string(xattrbl.c_str(), xattrbl.length()); // adds 0 byte at the end
uint64_t object_size = strtoll(s_xattr.c_str(), NULL, 10);
ASSERT_EQ(object_size, exp_object_size);
xattrbl.clear();
ASSERT_LT(0, ioctx.getxattr(firstOid, "striper.size", xattrbl));
s_xattr = std::string(xattrbl.c_str(), xattrbl.length()); // adds 0 byte at the end
uint64_t xa_size = strtoll(s_xattr.c_str(), NULL, 10);
ASSERT_EQ(xa_size, size);
// checking object content from rados point of view
// we will go stripe by stripe, read the content of each of them and
// check with expectations
uint64_t stripe_per_object = object_size / stripe_unit;
uint64_t stripe_per_objectset = stripe_per_object * stripe_count;
uint64_t nb_stripes_in_object = (size+stripe_unit-1)/stripe_unit;
for (uint64_t stripe_nb = 0;
stripe_nb < nb_stripes_in_object;
stripe_nb++) {
// find out where this stripe is stored
uint64_t objectset = stripe_nb / stripe_per_objectset;
uint64_t stripe_in_object_set = stripe_nb % stripe_per_objectset;
uint64_t object_in_set = stripe_in_object_set % stripe_count;
uint64_t stripe_in_object = stripe_in_object_set / stripe_count;
uint64_t object_nb = objectset * stripe_count + object_in_set;
uint64_t start = stripe_in_object * stripe_unit;
uint64_t len = stripe_unit;
if (stripe_nb == nb_stripes_in_object-1 and size % stripe_unit != 0) {
len = size % stripe_unit;
}
// handle case of sparse object (can only be sparse at the end in our tests)
if (actual_size_if_sparse < size and
((actual_size_if_sparse+stripe_unit-1)/stripe_unit)-1 == stripe_nb) {
len = actual_size_if_sparse % stripe_unit;
if (0 == len) len = stripe_unit;
}
bufferlist stripe_data;
// check object content
char* oid = getObjName(soid, object_nb);
int rc = ioctx.read(oid, stripe_data, len, start);
if (actual_size_if_sparse < size and
(actual_size_if_sparse+stripe_unit-1)/stripe_unit <= stripe_nb) {
// sparse object case : the stripe does not exist, but the rados object may
uint64_t object_start = (object_in_set + objectset*stripe_per_objectset) * stripe_unit;
if (actual_size_if_sparse <= object_start) {
ASSERT_EQ(rc, -ENOENT);
} else {
ASSERT_EQ(rc, 0);
}
} else {
ASSERT_EQ((uint64_t)rc, len);
bufferlist original_data;
original_data.substr_of(bl, stripe_nb*stripe_unit, len);
ASSERT_EQ(0, memcmp(original_data.c_str(), stripe_data.c_str(), len));
}
free(oid);
}
// checking rados object sizes; we go object by object
uint64_t nb_full_object_sets = nb_stripes_in_object / stripe_per_objectset;
uint64_t nb_extra_objects = nb_stripes_in_object % stripe_per_objectset;
if (nb_extra_objects > stripe_count) nb_extra_objects = stripe_count;
uint64_t nb_objects = nb_full_object_sets * stripe_count + nb_extra_objects;
for (uint64_t object_nb = 0; object_nb < nb_objects; object_nb++) {
uint64_t rados_size;
time_t mtime;
char* oid = getObjName(soid, object_nb);
uint64_t nb_full_object_set = object_nb / stripe_count;
uint64_t object_index_in_set = object_nb % stripe_count;
uint64_t object_start_stripe = nb_full_object_set * stripe_per_objectset + object_index_in_set;
uint64_t object_start_off = object_start_stripe * stripe_unit;
if (actual_size_if_sparse < size and actual_size_if_sparse <= object_start_off) {
ASSERT_EQ(-ENOENT, ioctx.stat(oid, &rados_size, &mtime));
} else {
ASSERT_EQ(0, ioctx.stat(oid, &rados_size, &mtime));
uint64_t offset;
uint64_t stripe_size = stripe_count * stripe_unit;
uint64_t set_size = stripe_count * object_size;
uint64_t len = 0;
for (offset = object_start_off;
(offset < (object_start_off) + set_size) && (offset < actual_size_if_sparse);
offset += stripe_size) {
if (offset + stripe_unit > actual_size_if_sparse) {
len += actual_size_if_sparse-offset;
} else {
len += stripe_unit;
}
}
ASSERT_EQ(len, rados_size);
}
free(oid);
}
// check we do not have an extra object behind
uint64_t rados_size;
time_t mtime;
char* oid = getObjName(soid, nb_objects);
ASSERT_EQ(-ENOENT, ioctx.stat(oid, &rados_size, &mtime));
free(oid);
free(firstOid);
}
};
TEST_P(StriperTestRT, StripedRoundtrip) {
// get striping parameters and apply them
TestData testData = GetParam();
ASSERT_EQ(0, striper.set_object_layout_stripe_unit(testData.stripe_unit));
ASSERT_EQ(0, striper.set_object_layout_stripe_count(testData.stripe_count));
ASSERT_EQ(0, striper.set_object_layout_object_size(testData.object_size));
std::ostringstream oss;
oss << "StripedRoundtrip_" << testData.stripe_unit << "_"
<< testData.stripe_count << "_" << testData.object_size
<< "_" << testData.size;
std::string soid = oss.str();
// writing striped data
std::unique_ptr<char[]> buf1;
bufferlist bl1;
{
SCOPED_TRACE("Writing initial object");
buf1 = std::make_unique<char[]>(testData.size);
for (unsigned int i = 0; i < testData.size; i++) buf1[i] = 13*((unsigned char)i);
bl1.append(buf1.get(), testData.size);
ASSERT_EQ(0, striper.write(soid, bl1, testData.size, 0));
// checking object state from Rados point of view
ASSERT_NO_FATAL_FAILURE(checkObjectFromRados(soid, bl1, testData.stripe_unit,
testData.stripe_count, testData.object_size,
testData.size));
}
// adding more data to object and checking again
std::unique_ptr<char[]> buf2;
bufferlist bl2;
{
SCOPED_TRACE("Testing append");
buf2 = std::make_unique<char[]>(testData.size);
for (unsigned int i = 0; i < testData.size; i++) buf2[i] = 17*((unsigned char)i);
bl2.append(buf2.get(), testData.size);
ASSERT_EQ(0, striper.append(soid, bl2, testData.size));
bl1.append(buf2.get(), testData.size);
ASSERT_NO_FATAL_FAILURE(checkObjectFromRados(soid, bl1, testData.stripe_unit,
testData.stripe_count, testData.object_size,
testData.size*2));
}
// truncating to half original size and checking again
{
SCOPED_TRACE("Testing trunc to truncate object");
ASSERT_EQ(0, striper.trunc(soid, testData.size/2));
ASSERT_NO_FATAL_FAILURE(checkObjectFromRados(soid, bl1, testData.stripe_unit,
testData.stripe_count, testData.object_size,
testData.size/2));
}
// truncating back to original size and checking again (especially for 0s)
{
SCOPED_TRACE("Testing trunc to extend object with 0s");
ASSERT_EQ(0, striper.trunc(soid, testData.size));
bufferlist bl3;
bl3.substr_of(bl1, 0, testData.size/2);
bl3.append_zero(testData.size - testData.size/2);
ASSERT_NO_FATAL_FAILURE(checkObjectFromRados(soid, bl3, testData.stripe_unit,
testData.stripe_count, testData.object_size,
testData.size, testData.size/2));
}
{
SCOPED_TRACE("Testing write_full");
// using write_full and checking again
ASSERT_EQ(0, striper.write_full(soid, bl2));
checkObjectFromRados(soid, bl2, testData.stripe_unit,
testData.stripe_count, testData.object_size,
testData.size);
}
{
SCOPED_TRACE("Testing standard remove");
// call remove
ASSERT_EQ(0, striper.remove(soid));
// check that the removal was successful
uint64_t size;
time_t mtime;
for (uint64_t object_nb = 0;
object_nb < testData.size*2/testData.object_size + testData.stripe_count;
object_nb++) {
char* oid = getObjName(soid, object_nb);
ASSERT_EQ(-ENOENT, ioctx.stat(oid, &size, &mtime));
free(oid);
}
}
{
SCOPED_TRACE("Testing remove when no object size");
// recreate object
ASSERT_EQ(0, striper.write(soid, bl1, testData.size*2, 0));
// remove the object size attribute from the striped object
char* firstOid = getObjName(soid, 0);
ASSERT_EQ(0, ioctx.rmxattr(firstOid, "striper.size"));
free(firstOid);
// check that stat fails
uint64_t size;
time_t mtime;
ASSERT_EQ(-ENODATA, striper.stat(soid, &size, &mtime));
// call remove
ASSERT_EQ(0, striper.remove(soid));
// check that the removal was successful
for (uint64_t object_nb = 0;
object_nb < testData.size*2/testData.object_size + testData.stripe_count;
object_nb++) {
char* oid = getObjName(soid, object_nb);
ASSERT_EQ(-ENOENT, ioctx.stat(oid, &size, &mtime));
free(oid);
}
}
}
const TestData simple_stripe_schemes[] = {
// stripe_unit, stripe_count, object_size, size
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, 2},
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 5, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 5, 3*CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 5, 3*CEPH_MIN_STRIPE_UNIT, 8*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 5, 3*CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 5, 3*CEPH_MIN_STRIPE_UNIT, 15*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 5, 3*CEPH_MIN_STRIPE_UNIT, 25*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 5, 3*CEPH_MIN_STRIPE_UNIT, 45*CEPH_MIN_STRIPE_UNIT+100},
{262144, 5, 262144, 2},
{262144, 5, 262144, 262144},
{262144, 5, 262144, 262144-1},
{262144, 5, 262144, 2*262144},
{262144, 5, 262144, 12*262144},
{262144, 5, 262144, 2*262144-1},
{262144, 5, 262144, 12*262144-1},
{262144, 5, 262144, 2*262144+100},
{262144, 5, 262144, 12*262144+100},
{262144, 5, 3*262144, 2*262144+100},
{262144, 5, 3*262144, 8*262144+100},
{262144, 5, 3*262144, 12*262144+100},
{262144, 5, 3*262144, 15*262144+100},
{262144, 5, 3*262144, 25*262144+100},
{262144, 5, 3*262144, 45*262144+100},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, 2},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 1, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 1, 3*CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 1, 3*CEPH_MIN_STRIPE_UNIT, 8*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 1, 3*CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 1, 3*CEPH_MIN_STRIPE_UNIT, 15*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 1, 3*CEPH_MIN_STRIPE_UNIT, 25*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 1, 3*CEPH_MIN_STRIPE_UNIT, 45*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, 2},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT-1},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 50, CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 50, 3*CEPH_MIN_STRIPE_UNIT, 2*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 50, 3*CEPH_MIN_STRIPE_UNIT, 8*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 50, 3*CEPH_MIN_STRIPE_UNIT, 12*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 50, 3*CEPH_MIN_STRIPE_UNIT, 15*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 50, 3*CEPH_MIN_STRIPE_UNIT, 25*CEPH_MIN_STRIPE_UNIT+100},
{CEPH_MIN_STRIPE_UNIT, 50, 3*CEPH_MIN_STRIPE_UNIT, 45*CEPH_MIN_STRIPE_UNIT+100}
};
INSTANTIATE_TEST_SUITE_P(SimpleStriping,
StriperTestRT,
::testing::ValuesIn(simple_stripe_schemes));
| 16,848 | 50.057576 | 101 |
cc
|
null |
ceph-main/src/test/librbd/fsx.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:8; indent-tabs-mode:t -*-
// vim: ts=8 sw=8 smarttab
/*
* Copyright (C) 1991, NeXT Computer, Inc. All Rights Reserved.
*
* File: fsx.cc
* Author: Avadis Tevanian, Jr.
*
* File system exerciser.
*
* Rewritten 8/98 by Conrad Minshall.
*
* Small changes to work under Linux -- davej.
*
* Checks for mmap last-page zero fill.
*/
#include <sys/types.h>
#include <unistd.h>
#include <getopt.h>
#include <limits.h>
#include <strings.h>
#if defined(__FreeBSD__)
#include <sys/disk.h>
#endif
#include <sys/file.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <sys/mman.h>
#include <sys/ioctl.h>
#endif
#if defined(__linux__)
#include <linux/fs.h>
#endif
#ifdef HAVE_ERR_H
#include <err.h>
#endif
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <fcntl.h>
#include <random>
#include "include/compat.h"
#include "include/intarith.h"
#if defined(WITH_KRBD)
#include "include/krbd.h"
#endif
#include "include/rados/librados.h"
#include "include/rados/librados.hpp"
#include "include/rbd/librbd.h"
#include "include/rbd/librbd.hpp"
#include "common/Cond.h"
#include "common/SubProcess.h"
#include "common/safe_io.h"
#include "journal/Journaler.h"
#include "journal/ReplayEntry.h"
#include "journal/ReplayHandler.h"
#include "journal/Settings.h"
#include <boost/scope_exit.hpp>
#define NUMPRINTCOLUMNS 32 /* # columns of data to print on each line */
/*
* A log entry is an operation and a bunch of arguments.
*/
struct log_entry {
int operation;
int args[3];
};
#define LOGSIZE 1000
struct log_entry oplog[LOGSIZE]; /* the log */
int logptr = 0; /* current position in log */
int logcount = 0; /* total ops */
/*
* The operation matrix is complex due to conditional execution of different
* features. Hence when we come to deciding what operation to run, we need to
* be careful in how we select the different operations. The active operations
* are mapped to numbers as follows:
*
* lite !lite
* READ: 0 0
* WRITE: 1 1
* MAPREAD: 2 2
* MAPWRITE: 3 3
* TRUNCATE: - 4
* FALLOCATE: - 5
* PUNCH HOLE: - 6
* WRITESAME: - 7
* COMPAREANDWRITE: - 8
*
* When mapped read/writes are disabled, they are simply converted to normal
* reads and writes. When fallocate/fpunch calls are disabled, they are
* converted to OP_SKIPPED. Hence OP_SKIPPED needs to have a number higher than
* the operation selection matrix, as does the OP_CLOSEOPEN which is an
* operation modifier rather than an operation in itself.
*
* Because of the "lite" version, we also need to have different "maximum
* operation" defines to allow the ops to be selected correctly based on the
* mode being run.
*/
/* common operations */
#define OP_READ 0
#define OP_WRITE 1
#define OP_MAPREAD 2
#define OP_MAPWRITE 3
#define OP_MAX_LITE 4
/* !lite operations */
#define OP_TRUNCATE 4
#define OP_FALLOCATE 5
#define OP_PUNCH_HOLE 6
#define OP_WRITESAME 7
#define OP_COMPARE_AND_WRITE 8
/* rbd-specific operations */
#define OP_CLONE 9
#define OP_FLATTEN 10
#define OP_MAX_FULL 11
/* operation modifiers */
#define OP_CLOSEOPEN 100
#define OP_SKIPPED 101
#undef PAGE_SIZE
#define PAGE_SIZE get_page_size()
#undef PAGE_MASK
#define PAGE_MASK (PAGE_SIZE - 1)
char *original_buf; /* a pointer to the original data */
char *good_buf; /* a pointer to the correct data */
char *temp_buf; /* a pointer to the current data */
char dirpath[1024];
off_t file_size = 0;
off_t biggest = 0;
unsigned long testcalls = 0; /* calls to function "test" */
const char* cluster_name = "ceph"; /* --cluster optional */
const char* client_id = "admin"; /* --id optional */
unsigned long simulatedopcount = 0; /* -b flag */
int closeprob = 0; /* -c flag */
int debug = 0; /* -d flag */
unsigned long debugstart = 0; /* -D flag */
int flush_enabled = 0; /* -f flag */
int deep_copy = 0; /* -g flag */
int holebdy = 1; /* -h flag */
bool journal_replay = false; /* -j flag */
int keep_on_success = 0; /* -k flag */
int do_fsync = 0; /* -y flag */
unsigned long maxfilelen = 256 * 1024; /* -l flag */
int sizechecks = 1; /* -n flag disables them */
int maxoplen = 64 * 1024; /* -o flag */
int quiet = 0; /* -q flag */
unsigned long progressinterval = 0; /* -p flag */
int readbdy = 1; /* -r flag */
int style = 0; /* -s flag */
int prealloc = 0; /* -x flag */
int truncbdy = 1; /* -t flag */
int writebdy = 1; /* -w flag */
long monitorstart = -1; /* -m flag */
long monitorend = -1; /* -m flag */
int lite = 0; /* -L flag */
long numops = -1; /* -N flag */
int randomoplen = 1; /* -O flag disables it */
int seed = 1; /* -S flag */
int mapped_writes = 0; /* -W flag disables */
int fallocate_calls = 0; /* -F flag disables */
int punch_hole_calls = 1; /* -H flag disables */
int clone_calls = 1; /* -C flag disables */
int randomize_striping = 1; /* -U flag disables */
int randomize_parent_overlap = 1;
int mapped_reads = 0; /* -R flag disables it */
int fsxgoodfd = 0;
int o_direct = 0; /* -Z flag */
int num_clones = 0;
int page_size;
int page_mask;
int mmap_mask;
FILE * fsxlogf = NULL;
int badoff = -1;
int closeopen = 0;
void
vwarnc(int code, const char *fmt, va_list ap) {
fprintf(stderr, "fsx: ");
if (fmt != NULL) {
vfprintf(stderr, fmt, ap);
fprintf(stderr, ": ");
}
fprintf(stderr, "%s\n", strerror(code));
}
void
warn(const char * fmt, ...) {
va_list ap;
va_start(ap, fmt);
vwarnc(errno, fmt, ap);
va_end(ap);
}
#define BUF_SIZE 1024
void
prt(const char *fmt, ...)
{
va_list args;
char buffer[BUF_SIZE];
va_start(args, fmt);
vsnprintf(buffer, BUF_SIZE, fmt, args);
va_end(args);
fprintf(stdout, "%s", buffer);
if (fsxlogf)
fprintf(fsxlogf, "%s", buffer);
}
void
prterr(const char *prefix)
{
prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(errno));
}
void
prterrcode(const char *prefix, int code)
{
prt("%s%s%s\n", prefix, prefix ? ": " : "", strerror(-code));
}
void
simple_err(const char *msg, int err)
{
fprintf(stderr, "%s: %s\n", msg, strerror(-err));
}
/*
* random
*/
std::mt19937 random_generator;
uint_fast32_t
get_random(void)
{
return random_generator();
}
int get_features(uint64_t* features);
void replay_imagename(char *buf, size_t len, int clones);
namespace {
static const std::string JOURNAL_CLIENT_ID("fsx");
struct ReplayHandler : public journal::ReplayHandler {
journal::Journaler *journaler;
journal::Journaler *replay_journaler;
Context *on_finish;
ReplayHandler(journal::Journaler *journaler,
journal::Journaler *replay_journaler, Context *on_finish)
: journaler(journaler), replay_journaler(replay_journaler),
on_finish(on_finish) {
}
void handle_entries_available() override {
while (true) {
journal::ReplayEntry replay_entry;
if (!journaler->try_pop_front(&replay_entry)) {
return;
}
replay_journaler->append(0, replay_entry.get_data());
}
}
void handle_complete(int r) override {
on_finish->complete(r);
}
};
int get_image_id(librados::IoCtx &io_ctx, const char *image_name,
std::string *image_id) {
librbd::RBD rbd;
librbd::Image image;
int r = rbd.open(io_ctx, image, image_name);
if (r < 0) {
simple_err("failed to open image", r);
return r;
}
rbd_image_info_t info;
r = image.stat(info, sizeof(info));
if (r < 0) {
simple_err("failed to stat image", r);
return r;
}
*image_id = std::string(&info.block_name_prefix[strlen(RBD_DATA_PREFIX)]);
return 0;
}
int register_journal(rados_ioctx_t ioctx, const char *image_name) {
librados::IoCtx io_ctx;
librados::IoCtx::from_rados_ioctx_t(ioctx, io_ctx);
std::string image_id;
int r = get_image_id(io_ctx, image_name, &image_id);
if (r < 0) {
return r;
}
journal::Journaler journaler(io_ctx, image_id, JOURNAL_CLIENT_ID, {},
nullptr);
r = journaler.register_client(bufferlist());
if (r < 0) {
simple_err("failed to register journal client", r);
return r;
}
return 0;
}
int unregister_journal(rados_ioctx_t ioctx, const char *image_name) {
librados::IoCtx io_ctx;
librados::IoCtx::from_rados_ioctx_t(ioctx, io_ctx);
std::string image_id;
int r = get_image_id(io_ctx, image_name, &image_id);
if (r < 0) {
return r;
}
journal::Journaler journaler(io_ctx, image_id, JOURNAL_CLIENT_ID, {},
nullptr);
r = journaler.unregister_client();
if (r < 0) {
simple_err("failed to unregister journal client", r);
return r;
}
return 0;
}
int create_replay_image(rados_ioctx_t ioctx, int order,
uint64_t stripe_unit, int stripe_count,
const char *replay_image_name,
const char *last_replay_image_name) {
librados::IoCtx io_ctx;
librados::IoCtx::from_rados_ioctx_t(ioctx, io_ctx);
uint64_t features;
int r = get_features(&features);
if (r < 0) {
return r;
}
librbd::RBD rbd;
if (last_replay_image_name == nullptr) {
r = rbd.create2(io_ctx, replay_image_name, 0, features, &order);
} else {
r = rbd.clone2(io_ctx, last_replay_image_name, "snap",
io_ctx, replay_image_name, features, &order,
stripe_unit, stripe_count);
}
if (r < 0) {
simple_err("failed to create replay image", r);
return r;
}
return 0;
}
int replay_journal(rados_ioctx_t ioctx, const char *image_name,
const char *replay_image_name) {
librados::IoCtx io_ctx;
librados::IoCtx::from_rados_ioctx_t(ioctx, io_ctx);
std::string image_id;
int r = get_image_id(io_ctx, image_name, &image_id);
if (r < 0) {
return r;
}
std::string replay_image_id;
r = get_image_id(io_ctx, replay_image_name, &replay_image_id);
if (r < 0) {
return r;
}
journal::Journaler journaler(io_ctx, image_id, JOURNAL_CLIENT_ID, {},
nullptr);
C_SaferCond init_ctx;
journaler.init(&init_ctx);
BOOST_SCOPE_EXIT_ALL( (&journaler) ) {
journaler.shut_down();
};
r = init_ctx.wait();
if (r < 0) {
simple_err("failed to initialize journal", r);
return r;
}
journal::Journaler replay_journaler(io_ctx, replay_image_id, "", {},
nullptr);
C_SaferCond replay_init_ctx;
replay_journaler.init(&replay_init_ctx);
BOOST_SCOPE_EXIT_ALL( (&replay_journaler) ) {
replay_journaler.shut_down();
};
r = replay_init_ctx.wait();
if (r < 0) {
simple_err("failed to initialize replay journal", r);
return r;
}
replay_journaler.start_append(0);
C_SaferCond replay_ctx;
ReplayHandler replay_handler(&journaler, &replay_journaler,
&replay_ctx);
// copy journal events from source image to replay image
journaler.start_replay(&replay_handler);
r = replay_ctx.wait();
journaler.stop_replay();
C_SaferCond stop_ctx;
replay_journaler.stop_append(&stop_ctx);
int stop_r = stop_ctx.wait();
if (r == 0 && stop_r < 0) {
r = stop_r;
}
if (r < 0) {
simple_err("failed to replay journal", r);
return r;
}
librbd::RBD rbd;
librbd::Image image;
r = rbd.open(io_ctx, image, replay_image_name);
if (r < 0) {
simple_err("failed to open replay image", r);
return r;
}
// perform an IO op to initiate the journal replay
bufferlist bl;
r = static_cast<ssize_t>(image.write(0, 0, bl));
if (r < 0) {
simple_err("failed to write to replay image", r);
return r;
}
return 0;
}
int finalize_journal(rados_ioctx_t ioctx, const char *imagename, int clones,
int order, uint64_t stripe_unit, int stripe_count) {
char replayimagename[1024];
replay_imagename(replayimagename, sizeof(replayimagename), clones);
char lastreplayimagename[1024];
if (clones > 0) {
replay_imagename(lastreplayimagename,
sizeof(lastreplayimagename), clones - 1);
}
int ret = create_replay_image(ioctx, order, stripe_unit,
stripe_count, replayimagename,
clones > 0 ? lastreplayimagename :
nullptr);
if (ret < 0) {
exit(EXIT_FAILURE);
}
ret = replay_journal(ioctx, imagename, replayimagename);
if (ret < 0) {
exit(EXIT_FAILURE);
}
return 0;
}
} // anonymous namespace
/*
* rbd
*/
struct rbd_ctx {
const char *name; /* image name */
rbd_image_t image; /* image handle */
const char *krbd_name; /* image /dev/rbd<id> name */ /* reused for nbd test */
int krbd_fd; /* image /dev/rbd<id> fd */ /* reused for nbd test */
};
#define RBD_CTX_INIT (struct rbd_ctx) { NULL, NULL, NULL, -1}
struct rbd_operations {
int (*open)(const char *name, struct rbd_ctx *ctx);
int (*close)(struct rbd_ctx *ctx);
ssize_t (*read)(struct rbd_ctx *ctx, uint64_t off, size_t len, char *buf);
ssize_t (*write)(struct rbd_ctx *ctx, uint64_t off, size_t len, const char *buf);
int (*flush)(struct rbd_ctx *ctx);
int (*discard)(struct rbd_ctx *ctx, uint64_t off, uint64_t len);
int (*get_size)(struct rbd_ctx *ctx, uint64_t *size);
int (*resize)(struct rbd_ctx *ctx, uint64_t size);
int (*clone)(struct rbd_ctx *ctx, const char *src_snapname,
const char *dst_imagename, int *order, int stripe_unit,
int stripe_count);
int (*flatten)(struct rbd_ctx *ctx);
ssize_t (*writesame)(struct rbd_ctx *ctx, uint64_t off, size_t len,
const char *buf, size_t data_len);
ssize_t (*compare_and_write)(struct rbd_ctx *ctx, uint64_t off, size_t len,
const char *cmp_buf, const char *buf);
};
char *pool; /* name of the pool our test image is in */
char *iname; /* name of our test image */
rados_t cluster; /* handle for our test cluster */
rados_ioctx_t ioctx; /* handle for our test pool */
#if defined(WITH_KRBD)
struct krbd_ctx *krbd; /* handle for libkrbd */
#endif
bool skip_partial_discard; /* rbd_skip_partial_discard config value*/
int get_features(uint64_t* features) {
char buf[1024];
int r = rados_conf_get(cluster, "rbd_default_features", buf,
sizeof(buf));
if (r < 0) {
simple_err("Could not get rbd_default_features value", r);
return r;
}
*features = strtol(buf, NULL, 0);
if (clone_calls) {
*features |= RBD_FEATURE_LAYERING;
}
if (journal_replay) {
*features |= (RBD_FEATURE_EXCLUSIVE_LOCK |
RBD_FEATURE_JOURNALING);
}
return 0;
}
/*
* librbd/krbd rbd_operations handlers. Given the rest of fsx.c, no
* attempt to do error handling is made in these handlers.
*/
int
__librbd_open(const char *name, struct rbd_ctx *ctx)
{
rbd_image_t image;
int ret;
ceph_assert(!ctx->name && !ctx->image &&
!ctx->krbd_name && ctx->krbd_fd < 0);
ret = rbd_open(ioctx, name, &image, NULL);
if (ret < 0) {
prt("rbd_open(%s) failed\n", name);
return ret;
}
ctx->name = strdup(name);
ctx->image = image;
ctx->krbd_name = NULL;
ctx->krbd_fd = -1;
return 0;
}
int
librbd_open(const char *name, struct rbd_ctx *ctx)
{
return __librbd_open(name, ctx);
}
int
__librbd_close(struct rbd_ctx *ctx)
{
int ret;
ceph_assert(ctx->name && ctx->image);
ret = rbd_close(ctx->image);
if (ret < 0) {
prt("rbd_close(%s) failed\n", ctx->name);
return ret;
}
free((void *)ctx->name);
ctx->name = NULL;
ctx->image = NULL;
return 0;
}
int
librbd_close(struct rbd_ctx *ctx)
{
return __librbd_close(ctx);
}
int
librbd_verify_object_map(struct rbd_ctx *ctx)
{
int n;
uint64_t flags;
n = rbd_get_flags(ctx->image, &flags);
if (n < 0) {
prt("rbd_get_flags() failed\n");
return n;
}
if ((flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0) {
prt("rbd_get_flags() indicates object map is invalid\n");
return -EINVAL;
}
return 0;
}
ssize_t
librbd_read(struct rbd_ctx *ctx, uint64_t off, size_t len, char *buf)
{
ssize_t n;
n = rbd_read(ctx->image, off, len, buf);
if (n < 0)
prt("rbd_read(%llu, %zu) failed\n", off, len);
return n;
}
ssize_t
librbd_write(struct rbd_ctx *ctx, uint64_t off, size_t len, const char *buf)
{
ssize_t n;
int ret;
n = rbd_write(ctx->image, off, len, buf);
if (n < 0) {
prt("rbd_write(%llu, %zu) failed\n", off, len);
return n;
}
ret = librbd_verify_object_map(ctx);
if (ret < 0) {
return ret;
}
return n;
}
int
librbd_flush(struct rbd_ctx *ctx)
{
int ret;
ret = rbd_flush(ctx->image);
if (ret < 0) {
prt("rbd_flush failed\n");
return ret;
}
return librbd_verify_object_map(ctx);
}
int
librbd_discard(struct rbd_ctx *ctx, uint64_t off, uint64_t len)
{
int ret;
ret = rbd_discard(ctx->image, off, len);
if (ret < 0) {
prt("rbd_discard(%llu, %llu) failed\n", off, len);
return ret;
}
return librbd_verify_object_map(ctx);
}
ssize_t
librbd_writesame(struct rbd_ctx *ctx, uint64_t off, size_t len,
const char *buf, size_t data_len)
{
ssize_t n;
int ret;
n = rbd_writesame(ctx->image, off, len, buf, data_len, 0);
if (n < 0) {
prt("rbd_writesame(%llu, %zu) failed\n", off, len);
return n;
}
ret = librbd_verify_object_map(ctx);
if (ret < 0) {
return ret;
}
return n;
}
ssize_t
librbd_compare_and_write(struct rbd_ctx *ctx, uint64_t off, size_t len,
const char *cmp_buf, const char *buf)
{
ssize_t n;
int ret;
uint64_t mismatch_off = 0;
n = rbd_compare_and_write(ctx->image, off, len, cmp_buf, buf, &mismatch_off, 0);
if (n == -EINVAL) {
return n;
} else if (n < 0) {
prt("rbd_compare_and_write mismatch(%llu, %zu, %llu) failed\n",
off, len, mismatch_off);
return n;
}
ret = librbd_verify_object_map(ctx);
if (ret < 0) {
return ret;
}
return n;
}
int
librbd_get_size(struct rbd_ctx *ctx, uint64_t *size)
{
int ret;
ret = rbd_get_size(ctx->image, size);
if (ret < 0) {
prt("rbd_get_size failed\n");
return ret;
}
return 0;
}
int
__librbd_resize(struct rbd_ctx *ctx, uint64_t size)
{
int ret;
ret = rbd_resize(ctx->image, size);
if (ret < 0) {
prt("rbd_resize(%llu) failed\n", size);
return ret;
}
return librbd_verify_object_map(ctx);
}
int
librbd_resize(struct rbd_ctx *ctx, uint64_t size)
{
return __librbd_resize(ctx, size);
}
int
__librbd_deep_copy(struct rbd_ctx *ctx, const char *src_snapname,
const char *dst_imagename, uint64_t features, int *order,
int stripe_unit, int stripe_count) {
int ret;
rbd_image_options_t opts;
rbd_image_options_create(&opts);
BOOST_SCOPE_EXIT_ALL( (&opts) ) {
rbd_image_options_destroy(opts);
};
ret = rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FEATURES,
features);
ceph_assert(ret == 0);
ret = rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER,
*order);
ceph_assert(ret == 0);
ret = rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT,
stripe_unit);
ceph_assert(ret == 0);
ret = rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_COUNT,
stripe_count);
ceph_assert(ret == 0);
ret = rbd_snap_set(ctx->image, src_snapname);
if (ret < 0) {
prt("rbd_snap_set(%s@%s) failed\n", ctx->name, src_snapname);
return ret;
}
ret = rbd_deep_copy(ctx->image, ioctx, dst_imagename, opts);
if (ret < 0) {
prt("rbd_deep_copy(%s@%s -> %s) failed\n",
ctx->name, src_snapname, dst_imagename);
return ret;
}
ret = rbd_snap_set(ctx->image, "");
if (ret < 0) {
prt("rbd_snap_set(%s@) failed\n", ctx->name);
return ret;
}
rbd_image_t image;
ret = rbd_open(ioctx, dst_imagename, &image, nullptr);
if (ret < 0) {
prt("rbd_open(%s) failed\n", dst_imagename);
return ret;
}
ret = rbd_snap_unprotect(image, src_snapname);
if (ret < 0) {
prt("rbd_snap_unprotect(%s@%s) failed\n", dst_imagename,
src_snapname);
return ret;
}
ret = rbd_snap_remove(image, src_snapname);
if (ret < 0) {
prt("rbd_snap_remove(%s@%s) failed\n", dst_imagename,
src_snapname);
return ret;
}
ret = rbd_close(image);
if (ret < 0) {
prt("rbd_close(%s) failed\n", dst_imagename);
return ret;
}
return 0;
}
int
__librbd_clone(struct rbd_ctx *ctx, const char *src_snapname,
const char *dst_imagename, int *order, int stripe_unit,
int stripe_count)
{
int ret;
ret = rbd_snap_create(ctx->image, src_snapname);
if (ret < 0) {
prt("rbd_snap_create(%s@%s) failed\n", ctx->name,
src_snapname);
return ret;
}
ret = rbd_snap_protect(ctx->image, src_snapname);
if (ret < 0) {
prt("rbd_snap_protect(%s@%s) failed\n", ctx->name,
src_snapname);
return ret;
}
uint64_t features;
ret = get_features(&features);
if (ret < 0) {
return ret;
}
if (deep_copy) {
ret = __librbd_deep_copy(ctx, src_snapname, dst_imagename, features,
order, stripe_unit, stripe_count);
if (ret < 0) {
prt("deep_copy(%s@%s -> %s) failed\n", ctx->name,
src_snapname, dst_imagename);
return ret;
}
} else {
ret = rbd_clone2(ioctx, ctx->name, src_snapname, ioctx,
dst_imagename, features, order,
stripe_unit, stripe_count);
if (ret < 0) {
prt("rbd_clone2(%s@%s -> %s) failed\n", ctx->name,
src_snapname, dst_imagename);
return ret;
}
}
return 0;
}
int
librbd_clone(struct rbd_ctx *ctx, const char *src_snapname,
const char *dst_imagename, int *order, int stripe_unit,
int stripe_count)
{
return __librbd_clone(ctx, src_snapname, dst_imagename, order,
stripe_unit, stripe_count);
}
int
__librbd_flatten(struct rbd_ctx *ctx)
{
int ret;
ret = rbd_flatten(ctx->image);
if (ret < 0) {
prt("rbd_flatten failed\n");
return ret;
}
return librbd_verify_object_map(ctx);
}
int
librbd_flatten(struct rbd_ctx *ctx)
{
return __librbd_flatten(ctx);
}
const struct rbd_operations librbd_operations = {
librbd_open,
librbd_close,
librbd_read,
librbd_write,
librbd_flush,
librbd_discard,
librbd_get_size,
librbd_resize,
librbd_clone,
librbd_flatten,
librbd_writesame,
librbd_compare_and_write,
};
#if defined(WITH_KRBD)
int
krbd_open(const char *name, struct rbd_ctx *ctx)
{
char buf[1024];
char *devnode;
int fd;
int ret;
ret = __librbd_open(name, ctx);
if (ret < 0)
return ret;
ret = rados_conf_get(cluster, "rbd_default_map_options", buf,
sizeof(buf));
if (ret < 0) {
simple_err("Could not get rbd_default_map_options value", ret);
return ret;
}
ret = krbd_map(krbd, pool, "", name, "", buf, &devnode);
if (ret < 0) {
prt("krbd_map(%s) failed\n", name);
return ret;
}
fd = open(devnode, O_RDWR | o_direct);
if (fd < 0) {
ret = -errno;
prt("open(%s) failed\n", devnode);
return ret;
}
ctx->krbd_name = devnode;
ctx->krbd_fd = fd;
return 0;
}
int
krbd_close(struct rbd_ctx *ctx)
{
int ret;
ceph_assert(ctx->krbd_name && ctx->krbd_fd >= 0);
if (close(ctx->krbd_fd) < 0) {
ret = -errno;
prt("close(%s) failed\n", ctx->krbd_name);
return ret;
}
ret = krbd_unmap(krbd, ctx->krbd_name, "");
if (ret < 0) {
prt("krbd_unmap(%s) failed\n", ctx->krbd_name);
return ret;
}
free((void *)ctx->krbd_name);
ctx->krbd_name = NULL;
ctx->krbd_fd = -1;
return __librbd_close(ctx);
}
#endif // WITH_KRBD
#if defined(__linux__)
ssize_t
krbd_read(struct rbd_ctx *ctx, uint64_t off, size_t len, char *buf)
{
ssize_t n;
n = pread(ctx->krbd_fd, buf, len, off);
if (n < 0) {
n = -errno;
prt("pread(%llu, %zu) failed\n", off, len);
return n;
}
return n;
}
ssize_t
krbd_write(struct rbd_ctx *ctx, uint64_t off, size_t len, const char *buf)
{
ssize_t n;
n = pwrite(ctx->krbd_fd, buf, len, off);
if (n < 0) {
n = -errno;
prt("pwrite(%llu, %zu) failed\n", off, len);
return n;
}
return n;
}
int
__krbd_flush(struct rbd_ctx *ctx, bool invalidate)
{
int ret;
if (o_direct)
return 0;
/*
* BLKFLSBUF will sync the filesystem on top of the device (we
* don't care about that here, since we write directly to it),
* write out any dirty buffers and invalidate the buffer cache.
* It won't do a hardware cache flush.
*
* fsync() will write out any dirty buffers and do a hardware
* cache flush (which we don't care about either, because for
* krbd it's a noop). It won't try to empty the buffer cache
* nor poke the filesystem before writing out.
*
* Given that, for our purposes, fsync is a flush, while
* BLKFLSBUF is a flush+invalidate.
*/
if (invalidate)
ret = ioctl(ctx->krbd_fd, BLKFLSBUF, NULL);
else
ret = fsync(ctx->krbd_fd);
if (ret < 0) {
ret = -errno;
prt("%s failed\n", invalidate ? "BLKFLSBUF" : "fsync");
return ret;
}
return 0;
}
int
krbd_flush(struct rbd_ctx *ctx)
{
return __krbd_flush(ctx, false);
}
int
krbd_discard(struct rbd_ctx *ctx, uint64_t off, uint64_t len)
{
uint64_t range[2] = { off, len };
int ret;
/*
* BLKZEROOUT goes straight to disk and doesn't do anything
* about dirty buffers. This means we need to flush so that
*
* write 0..3M
* discard 1..2M
*
* results in "data 0000 data" rather than "data data data" on
* disk and invalidate so that
*
* discard 1..2M
* read 0..3M
*
* returns "data 0000 data" rather than "data data data" in
* case 1..2M was cached.
*
* Note: These cache coherency issues are supposed to be fixed
* in recent kernels.
*/
ret = __krbd_flush(ctx, true);
if (ret < 0)
return ret;
/*
* off and len must be 512-byte aligned, otherwise BLKZEROOUT
* will fail with -EINVAL. This means that -K (enable krbd
* mode) requires -h 512 or similar.
*/
if (ioctl(ctx->krbd_fd, BLKZEROOUT, &range) < 0) {
ret = -errno;
prt("BLKZEROOUT(%llu, %llu) failed\n", off, len);
return ret;
}
return 0;
}
int
krbd_get_size(struct rbd_ctx *ctx, uint64_t *size)
{
uint64_t bytes;
if (ioctl(ctx->krbd_fd, BLKGETSIZE64, &bytes) < 0) {
int ret = -errno;
prt("BLKGETSIZE64 failed\n");
return ret;
}
*size = bytes;
return 0;
}
int
krbd_resize(struct rbd_ctx *ctx, uint64_t size)
{
int ret;
ceph_assert(size % truncbdy == 0);
/*
* When krbd detects a size change, it calls revalidate_disk(),
* which ends up calling invalidate_bdev(), which invalidates
* clean pages and does nothing about dirty pages beyond the
* new size. The preceding cache flush makes sure those pages
* are invalidated, which is what we need on shrink so that
*
* write 0..1M
* resize 0
* resize 2M
* read 0..2M
*
* returns "0000 0000" rather than "data 0000".
*/
ret = __krbd_flush(ctx, false);
if (ret < 0)
return ret;
return __librbd_resize(ctx, size);
}
int
krbd_clone(struct rbd_ctx *ctx, const char *src_snapname,
const char *dst_imagename, int *order, int stripe_unit,
int stripe_count)
{
int ret;
ret = __krbd_flush(ctx, false);
if (ret < 0)
return ret;
return __librbd_clone(ctx, src_snapname, dst_imagename, order,
stripe_unit, stripe_count);
}
int
krbd_flatten(struct rbd_ctx *ctx)
{
int ret;
ret = __krbd_flush(ctx, false);
if (ret < 0)
return ret;
return __librbd_flatten(ctx);
}
#endif // __linux__
#if defined(WITH_KRBD)
const struct rbd_operations krbd_operations = {
krbd_open,
krbd_close,
krbd_read,
krbd_write,
krbd_flush,
krbd_discard,
krbd_get_size,
krbd_resize,
krbd_clone,
krbd_flatten,
NULL,
};
#endif // WITH_KRBD
#if defined(__linux__)
int
nbd_open(const char *name, struct rbd_ctx *ctx)
{
int r;
int fd;
char dev[4096];
char *devnode;
SubProcess process("rbd-nbd", SubProcess::KEEP, SubProcess::PIPE,
SubProcess::KEEP);
process.add_cmd_arg("map");
process.add_cmd_arg("--io-timeout=600");
std::string img;
img.append(pool);
img.append("/");
img.append(name);
process.add_cmd_arg(img.c_str());
r = __librbd_open(name, ctx);
if (r < 0)
return r;
r = process.spawn();
if (r < 0) {
prt("nbd_open failed to run rbd-nbd error: %s\n", process.err().c_str());
return r;
}
r = safe_read(process.get_stdout(), dev, sizeof(dev));
if (r < 0) {
prt("nbd_open failed to get nbd device path\n");
return r;
}
for (int i = 0; i < r; ++i)
if (dev[i] == 10 || dev[i] == 13)
dev[i] = 0;
dev[r] = 0;
r = process.join();
if (r) {
prt("rbd-nbd failed with error: %s", process.err().c_str());
return -EINVAL;
}
devnode = strdup(dev);
if (!devnode)
return -ENOMEM;
fd = open(devnode, O_RDWR | o_direct);
if (fd < 0) {
r = -errno;
prt("open(%s) failed\n", devnode);
return r;
}
ctx->krbd_name = devnode;
ctx->krbd_fd = fd;
return 0;
}
int
nbd_close(struct rbd_ctx *ctx)
{
int r;
ceph_assert(ctx->krbd_name && ctx->krbd_fd >= 0);
if (close(ctx->krbd_fd) < 0) {
r = -errno;
prt("close(%s) failed\n", ctx->krbd_name);
return r;
}
SubProcess process("rbd-nbd");
process.add_cmd_arg("unmap");
process.add_cmd_arg(ctx->krbd_name);
r = process.spawn();
if (r < 0) {
prt("nbd_close failed to run rbd-nbd error: %s\n", process.err().c_str());
return r;
}
r = process.join();
if (r) {
prt("rbd-nbd failed with error: %d", process.err().c_str());
return -EINVAL;
}
free((void *)ctx->krbd_name);
ctx->krbd_name = NULL;
ctx->krbd_fd = -1;
return __librbd_close(ctx);
}
int
nbd_clone(struct rbd_ctx *ctx, const char *src_snapname,
const char *dst_imagename, int *order, int stripe_unit,
int stripe_count)
{
int ret;
ret = __krbd_flush(ctx, false);
if (ret < 0)
return ret;
return __librbd_clone(ctx, src_snapname, dst_imagename, order,
stripe_unit, stripe_count);
}
const struct rbd_operations nbd_operations = {
nbd_open,
nbd_close,
krbd_read,
krbd_write,
krbd_flush,
krbd_discard,
krbd_get_size,
krbd_resize,
nbd_clone,
krbd_flatten,
NULL,
};
#endif // __linux__
#if defined(__FreeBSD__)
int
ggate_open(const char *name, struct rbd_ctx *ctx)
{
int r;
int fd;
char dev[4096];
char *devnode;
SubProcess process("rbd-ggate", SubProcess::KEEP, SubProcess::PIPE,
SubProcess::KEEP);
process.add_cmd_arg("map");
std::string img;
img.append(pool);
img.append("/");
img.append(name);
process.add_cmd_arg(img.c_str());
r = __librbd_open(name, ctx);
if (r < 0) {
return r;
}
r = process.spawn();
if (r < 0) {
prt("ggate_open failed to run rbd-ggate: %s\n",
process.err().c_str());
return r;
}
r = safe_read(process.get_stdout(), dev, sizeof(dev));
if (r < 0) {
prt("ggate_open failed to get ggate device path\n");
return r;
}
for (int i = 0; i < r; ++i) {
if (dev[i] == '\r' || dev[i] == '\n') {
dev[i] = 0;
}
}
dev[r] = 0;
r = process.join();
if (r) {
prt("rbd-ggate failed with error: %s", process.err().c_str());
return -EINVAL;
}
devnode = strdup(dev);
if (!devnode) {
return -ENOMEM;
}
for (int i = 0; i < 100; i++) {
fd = open(devnode, O_RDWR | o_direct);
if (fd >= 0 || errno != ENOENT) {
break;
}
usleep(100000);
}
if (fd < 0) {
r = -errno;
prt("open(%s) failed\n", devnode);
return r;
}
ctx->krbd_name = devnode;
ctx->krbd_fd = fd;
return 0;
}
int
ggate_close(struct rbd_ctx *ctx)
{
int r;
ceph_assert(ctx->krbd_name && ctx->krbd_fd >= 0);
if (close(ctx->krbd_fd) < 0) {
r = -errno;
prt("close(%s) failed\n", ctx->krbd_name);
return r;
}
SubProcess process("rbd-ggate");
process.add_cmd_arg("unmap");
process.add_cmd_arg(ctx->krbd_name);
r = process.spawn();
if (r < 0) {
prt("ggate_close failed to run rbd-nbd: %s\n",
process.err().c_str());
return r;
}
r = process.join();
if (r) {
prt("rbd-ggate failed with error: %d", process.err().c_str());
return -EINVAL;
}
free((void *)ctx->krbd_name);
ctx->krbd_name = NULL;
ctx->krbd_fd = -1;
return __librbd_close(ctx);
}
ssize_t
ggate_read(struct rbd_ctx *ctx, uint64_t off, size_t len, char *buf)
{
ssize_t n;
n = pread(ctx->krbd_fd, buf, len, off);
if (n < 0) {
n = -errno;
prt("pread(%llu, %zu) failed\n", off, len);
return n;
}
return n;
}
ssize_t
ggate_write(struct rbd_ctx *ctx, uint64_t off, size_t len, const char *buf)
{
ssize_t n;
n = pwrite(ctx->krbd_fd, buf, len, off);
if (n < 0) {
n = -errno;
prt("pwrite(%llu, %zu) failed\n", off, len);
return n;
}
return n;
}
int
__ggate_flush(struct rbd_ctx *ctx, bool invalidate)
{
int ret;
if (o_direct) {
return 0;
}
if (invalidate) {
ret = ioctl(ctx->krbd_fd, DIOCGFLUSH, NULL);
} else {
ret = fsync(ctx->krbd_fd);
}
if (ret < 0) {
ret = -errno;
prt("%s failed\n", invalidate ? "DIOCGFLUSH" : "fsync");
return ret;
}
return 0;
}
int
ggate_flush(struct rbd_ctx *ctx)
{
return __ggate_flush(ctx, false);
}
int
ggate_discard(struct rbd_ctx *ctx, uint64_t off, uint64_t len)
{
off_t range[2] = {static_cast<off_t>(off), static_cast<off_t>(len)};
int ret;
ret = __ggate_flush(ctx, true);
if (ret < 0) {
return ret;
}
if (ioctl(ctx->krbd_fd, DIOCGDELETE, &range) < 0) {
ret = -errno;
prt("DIOCGDELETE(%llu, %llu) failed\n", off, len);
return ret;
}
return 0;
}
int
ggate_get_size(struct rbd_ctx *ctx, uint64_t *size)
{
off_t bytes;
if (ioctl(ctx->krbd_fd, DIOCGMEDIASIZE, &bytes) < 0) {
int ret = -errno;
prt("DIOCGMEDIASIZE failed\n");
return ret;
}
*size = bytes;
return 0;
}
int
ggate_resize(struct rbd_ctx *ctx, uint64_t size)
{
int ret;
ceph_assert(size % truncbdy == 0);
ret = __ggate_flush(ctx, false);
if (ret < 0) {
return ret;
}
return __librbd_resize(ctx, size);
}
int
ggate_clone(struct rbd_ctx *ctx, const char *src_snapname,
const char *dst_imagename, int *order, int stripe_unit,
int stripe_count)
{
int ret;
ret = __ggate_flush(ctx, false);
if (ret < 0) {
return ret;
}
return __librbd_clone(ctx, src_snapname, dst_imagename, order,
stripe_unit, stripe_count);
}
int
ggate_flatten(struct rbd_ctx *ctx)
{
int ret;
ret = __ggate_flush(ctx, false);
if (ret < 0) {
return ret;
}
return __librbd_flatten(ctx);
}
const struct rbd_operations ggate_operations = {
ggate_open,
ggate_close,
ggate_read,
ggate_write,
ggate_flush,
ggate_discard,
ggate_get_size,
ggate_resize,
ggate_clone,
ggate_flatten,
NULL,
};
#endif // __FreeBSD__
struct rbd_ctx ctx = RBD_CTX_INIT;
const struct rbd_operations *ops = &librbd_operations;
static bool rbd_image_has_parent(struct rbd_ctx *ctx)
{
int ret;
rbd_linked_image_spec_t parent_image;
rbd_snap_spec_t parent_snap;
ret = rbd_get_parent(ctx->image, &parent_image, &parent_snap);
if (ret < 0 && ret != -ENOENT) {
prterrcode("rbd_get_parent_info", ret);
exit(1);
}
rbd_linked_image_spec_cleanup(&parent_image);
rbd_snap_spec_cleanup(&parent_snap);
return !ret;
}
/*
* fsx
*/
void
log4(int operation, int arg0, int arg1, int arg2)
{
struct log_entry *le;
le = &oplog[logptr];
le->operation = operation;
if (closeopen)
le->operation = ~ le->operation;
le->args[0] = arg0;
le->args[1] = arg1;
le->args[2] = arg2;
logptr++;
logcount++;
if (logptr >= LOGSIZE)
logptr = 0;
}
void
logdump(void)
{
int i, count, down;
struct log_entry *lp;
const char *falloc_type[3] = {"PAST_EOF", "EXTENDING", "INTERIOR"};
prt("LOG DUMP (%d total operations):\n", logcount);
if (logcount < LOGSIZE) {
i = 0;
count = logcount;
} else {
i = logptr;
count = LOGSIZE;
}
for ( ; count > 0; count--) {
int opnum;
opnum = i+1 + (logcount/LOGSIZE)*LOGSIZE;
prt("%d(%3d mod 256): ", opnum, opnum%256);
lp = &oplog[i];
if ((closeopen = lp->operation < 0))
lp->operation = ~ lp->operation;
switch (lp->operation) {
case OP_MAPREAD:
prt("MAPREAD 0x%x thru 0x%x\t(0x%x bytes)",
lp->args[0], lp->args[0] + lp->args[1] - 1,
lp->args[1]);
if (badoff >= lp->args[0] && badoff <
lp->args[0] + lp->args[1])
prt("\t***RRRR***");
break;
case OP_MAPWRITE:
prt("MAPWRITE 0x%x thru 0x%x\t(0x%x bytes)",
lp->args[0], lp->args[0] + lp->args[1] - 1,
lp->args[1]);
if (badoff >= lp->args[0] && badoff <
lp->args[0] + lp->args[1])
prt("\t******WWWW");
break;
case OP_READ:
prt("READ 0x%x thru 0x%x\t(0x%x bytes)",
lp->args[0], lp->args[0] + lp->args[1] - 1,
lp->args[1]);
if (badoff >= lp->args[0] &&
badoff < lp->args[0] + lp->args[1])
prt("\t***RRRR***");
break;
case OP_WRITE:
prt("WRITE 0x%x thru 0x%x\t(0x%x bytes)",
lp->args[0], lp->args[0] + lp->args[1] - 1,
lp->args[1]);
if (lp->args[0] > lp->args[2])
prt(" HOLE");
else if (lp->args[0] + lp->args[1] > lp->args[2])
prt(" EXTEND");
if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
badoff < lp->args[0] + lp->args[1])
prt("\t***WWWW");
break;
case OP_TRUNCATE:
down = lp->args[0] < lp->args[1];
prt("TRUNCATE %s\tfrom 0x%x to 0x%x",
down ? "DOWN" : "UP", lp->args[1], lp->args[0]);
if (badoff >= lp->args[!down] &&
badoff < lp->args[!!down])
prt("\t******WWWW");
break;
case OP_FALLOCATE:
/* 0: offset 1: length 2: where alloced */
prt("FALLOC 0x%x thru 0x%x\t(0x%x bytes) %s",
lp->args[0], lp->args[0] + lp->args[1],
lp->args[1], falloc_type[lp->args[2]]);
if (badoff >= lp->args[0] &&
badoff < lp->args[0] + lp->args[1])
prt("\t******FFFF");
break;
case OP_PUNCH_HOLE:
prt("PUNCH 0x%x thru 0x%x\t(0x%x bytes)",
lp->args[0], lp->args[0] + lp->args[1] - 1,
lp->args[1]);
if (badoff >= lp->args[0] && badoff <
lp->args[0] + lp->args[1])
prt("\t******PPPP");
break;
case OP_WRITESAME:
prt("WRITESAME 0x%x thru 0x%x\t(0x%x bytes) data_size 0x%x",
lp->args[0], lp->args[0] + lp->args[1] - 1,
lp->args[1], lp->args[2]);
if (badoff >= lp->args[0] &&
badoff < lp->args[0] + lp->args[1])
prt("\t***WSWSWSWS");
break;
case OP_COMPARE_AND_WRITE:
prt("COMPARE_AND_WRITE 0x%x thru 0x%x\t(0x%x bytes)",
lp->args[0], lp->args[0] + lp->args[1] - 1,
lp->args[1]);
if (lp->args[0] > lp->args[2])
prt(" HOLE");
else if (lp->args[0] + lp->args[1] > lp->args[2])
prt(" EXTEND");
if ((badoff >= lp->args[0] || badoff >=lp->args[2]) &&
badoff < lp->args[0] + lp->args[1])
prt("\t***WWWW");
break;
case OP_CLONE:
prt("CLONE");
break;
case OP_FLATTEN:
prt("FLATTEN");
break;
case OP_SKIPPED:
prt("SKIPPED (no operation)");
break;
default:
prt("BOGUS LOG ENTRY (operation code = %d)!",
lp->operation);
}
if (closeopen)
prt("\n\t\tCLOSE/OPEN");
prt("\n");
i++;
if (i == LOGSIZE)
i = 0;
}
}
void
save_buffer(char *buffer, off_t bufferlength, int fd)
{
off_t ret;
ssize_t byteswritten;
if (fd <= 0 || bufferlength == 0)
return;
if (bufferlength > SSIZE_MAX) {
prt("fsx flaw: overflow in save_buffer\n");
exit(67);
}
ret = lseek(fd, (off_t)0, SEEK_SET);
if (ret == (off_t)-1)
prterr("save_buffer: lseek 0");
byteswritten = write(fd, buffer, (size_t)bufferlength);
if (byteswritten != bufferlength) {
if (byteswritten == -1)
prterr("save_buffer write");
else
warn("save_buffer: short write, 0x%x bytes instead of 0x%llx\n",
(unsigned)byteswritten,
(unsigned long long)bufferlength);
}
}
void
report_failure(int status)
{
logdump();
if (fsxgoodfd) {
if (good_buf) {
save_buffer(good_buf, file_size, fsxgoodfd);
prt("Correct content saved for comparison\n");
prt("(maybe hexdump \"%s\" vs \"%s.fsxgood\")\n",
iname, iname);
}
close(fsxgoodfd);
}
sleep(3); // so the log can flush to disk. KLUDGEY!
exit(status);
}
#define short_at(cp) ((unsigned short)((*((unsigned char *)(cp)) << 8) | \
*(((unsigned char *)(cp)) + 1)))
int
fsxcmp(char *good_buf, char *temp_buf, unsigned size)
{
if (!skip_partial_discard) {
return memcmp(good_buf, temp_buf, size);
}
for (unsigned i = 0; i < size; i++) {
if (good_buf[i] != temp_buf[i] && good_buf[i] != 0) {
return good_buf[i] - temp_buf[i];
}
}
return 0;
}
void
check_buffers(char *good_buf, char *temp_buf, unsigned offset, unsigned size)
{
if (fsxcmp(good_buf + offset, temp_buf, size) != 0) {
unsigned i = 0;
unsigned n = 0;
prt("READ BAD DATA: offset = 0x%x, size = 0x%x, fname = %s\n",
offset, size, iname);
prt("OFFSET\tGOOD\tBAD\tRANGE\n");
while (size > 0) {
unsigned char c = good_buf[offset];
unsigned char t = temp_buf[i];
if (c != t) {
if (n < 16) {
unsigned bad = short_at(&temp_buf[i]);
prt("0x%5x\t0x%04x\t0x%04x", offset,
short_at(&good_buf[offset]), bad);
unsigned op = temp_buf[(offset & 1) ? i+1 : i];
prt("\t0x%5x\n", n);
if (op)
prt("operation# (mod 256) for "
"the bad data may be %u\n",
((unsigned)op & 0xff));
else
prt("operation# (mod 256) for "
"the bad data unknown, check"
" HOLE and EXTEND ops\n");
}
n++;
badoff = offset;
}
offset++;
i++;
size--;
}
report_failure(110);
}
}
void
check_size(void)
{
uint64_t size;
int ret;
ret = ops->get_size(&ctx, &size);
if (ret < 0)
prterrcode("check_size: ops->get_size", ret);
if ((uint64_t)file_size != size) {
prt("Size error: expected 0x%llx stat 0x%llx\n",
(unsigned long long)file_size,
(unsigned long long)size);
report_failure(120);
}
}
#define TRUNC_HACK_SIZE (200ULL << 9) /* 512-byte aligned for krbd */
void
check_trunc_hack(void)
{
uint64_t size;
int ret;
ret = ops->resize(&ctx, 0ULL);
if (ret < 0)
prterrcode("check_trunc_hack: ops->resize pre", ret);
ret = ops->resize(&ctx, TRUNC_HACK_SIZE);
if (ret < 0)
prterrcode("check_trunc_hack: ops->resize actual", ret);
ret = ops->get_size(&ctx, &size);
if (ret < 0)
prterrcode("check_trunc_hack: ops->get_size", ret);
if (size != TRUNC_HACK_SIZE) {
prt("no extend on truncate! not posix!\n");
exit(130);
}
ret = ops->resize(&ctx, 0ULL);
if (ret < 0)
prterrcode("check_trunc_hack: ops->resize post", ret);
}
int
create_image()
{
int r;
int order = 0;
char buf[32];
char client_name[256];
sprintf(client_name, "client.%s", client_id);
r = rados_create2(&cluster, cluster_name, client_name, 0);
if (r < 0) {
simple_err("Could not create cluster handle", r);
return r;
}
rados_conf_parse_env(cluster, NULL);
r = rados_conf_read_file(cluster, NULL);
if (r < 0) {
simple_err("Error reading ceph config file", r);
goto failed_shutdown;
}
r = rados_connect(cluster);
if (r < 0) {
simple_err("Error connecting to cluster", r);
goto failed_shutdown;
}
#if defined(WITH_KRBD)
r = krbd_create_from_context(rados_cct(cluster), 0, &krbd);
if (r < 0) {
simple_err("Could not create libkrbd handle", r);
goto failed_shutdown;
}
#endif
r = rados_pool_create(cluster, pool);
if (r < 0 && r != -EEXIST) {
simple_err("Error creating pool", r);
goto failed_krbd;
}
r = rados_ioctx_create(cluster, pool, &ioctx);
if (r < 0) {
simple_err("Error creating ioctx", r);
goto failed_krbd;
}
rados_application_enable(ioctx, "rbd", 1);
if (clone_calls || journal_replay) {
uint64_t features;
r = get_features(&features);
if (r < 0) {
goto failed_open;
}
r = rbd_create2(ioctx, iname, file_size, features, &order);
} else {
r = rbd_create(ioctx, iname, file_size, &order);
}
if (r < 0) {
simple_err("Error creating image", r);
goto failed_open;
}
if (journal_replay) {
r = register_journal(ioctx, iname);
if (r < 0) {
goto failed_open;
}
}
r = rados_conf_get(cluster, "rbd_skip_partial_discard", buf,
sizeof(buf));
if (r < 0) {
simple_err("Could not get rbd_skip_partial_discard value", r);
goto failed_open;
}
skip_partial_discard = (strcmp(buf, "true") == 0);
return 0;
failed_open:
rados_ioctx_destroy(ioctx);
failed_krbd:
#if defined(WITH_KRBD)
krbd_destroy(krbd);
#endif
failed_shutdown:
rados_shutdown(cluster);
return r;
}
void
doflush(unsigned offset, unsigned size)
{
int ret;
if (o_direct)
return;
ret = ops->flush(&ctx);
if (ret < 0)
prterrcode("doflush: ops->flush", ret);
}
void
doread(unsigned offset, unsigned size)
{
int ret;
offset -= offset % readbdy;
if (o_direct)
size -= size % readbdy;
if (size == 0) {
if (!quiet && testcalls > simulatedopcount && !o_direct)
prt("skipping zero size read\n");
log4(OP_SKIPPED, OP_READ, offset, size);
return;
}
if (size + offset > file_size) {
if (!quiet && testcalls > simulatedopcount)
prt("skipping seek/read past end of file\n");
log4(OP_SKIPPED, OP_READ, offset, size);
return;
}
log4(OP_READ, offset, size, 0);
if (testcalls <= simulatedopcount)
return;
if (!quiet &&
((progressinterval && testcalls % progressinterval == 0) ||
(debug &&
(monitorstart == -1 ||
(static_cast<long>(offset + size) > monitorstart &&
(monitorend == -1 ||
static_cast<long>(offset) <= monitorend))))))
prt("%lu read\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
offset, offset + size - 1, size);
ret = ops->read(&ctx, offset, size, temp_buf);
if (ret != (int)size) {
if (ret < 0)
prterrcode("doread: ops->read", ret);
else
prt("short read: 0x%x bytes instead of 0x%x\n",
ret, size);
report_failure(141);
}
check_buffers(good_buf, temp_buf, offset, size);
}
void
check_eofpage(char *s, unsigned offset, char *p, int size)
{
unsigned long last_page, should_be_zero;
if (offset + size <= (file_size & ~page_mask))
return;
/*
* we landed in the last page of the file
* test to make sure the VM system provided 0's
* beyond the true end of the file mapping
* (as required by mmap def in 1996 posix 1003.1)
*/
last_page = ((unsigned long)p + (offset & page_mask) + size) & ~page_mask;
for (should_be_zero = last_page + (file_size & page_mask);
should_be_zero < last_page + page_size;
should_be_zero++)
if (*(char *)should_be_zero) {
prt("Mapped %s: non-zero data past EOF (0x%llx) page offset 0x%x is 0x%04x\n",
s, file_size - 1, should_be_zero & page_mask,
short_at(should_be_zero));
report_failure(205);
}
}
void
gendata(char *original_buf, char *good_buf, unsigned offset, unsigned size)
{
while (size--) {
good_buf[offset] = testcalls % 256;
if (offset % 2)
good_buf[offset] += original_buf[offset];
offset++;
}
}
void
dowrite(unsigned offset, unsigned size)
{
ssize_t ret;
off_t newsize;
offset -= offset % writebdy;
if (o_direct)
size -= size % writebdy;
if (size == 0) {
if (!quiet && testcalls > simulatedopcount && !o_direct)
prt("skipping zero size write\n");
log4(OP_SKIPPED, OP_WRITE, offset, size);
return;
}
log4(OP_WRITE, offset, size, file_size);
gendata(original_buf, good_buf, offset, size);
if (file_size < offset + size) {
newsize = ceil(((double)offset + size) / truncbdy) * truncbdy;
if (file_size < newsize)
memset(good_buf + file_size, '\0', newsize - file_size);
file_size = newsize;
if (lite) {
warn("Lite file size bug in fsx!");
report_failure(149);
}
ret = ops->resize(&ctx, newsize);
if (ret < 0) {
prterrcode("dowrite: ops->resize", ret);
report_failure(150);
}
}
if (testcalls <= simulatedopcount)
return;
if (!quiet &&
((progressinterval && testcalls % progressinterval == 0) ||
(debug &&
(monitorstart == -1 ||
(static_cast<long>(offset + size) > monitorstart &&
(monitorend == -1 ||
static_cast<long>(offset) <= monitorend))))))
prt("%lu write\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
offset, offset + size - 1, size);
ret = ops->write(&ctx, offset, size, good_buf + offset);
if (ret != (ssize_t)size) {
if (ret < 0)
prterrcode("dowrite: ops->write", ret);
else
prt("short write: 0x%x bytes instead of 0x%x\n",
ret, size);
report_failure(151);
}
if (flush_enabled)
doflush(offset, size);
}
void
dotruncate(unsigned size)
{
int oldsize = file_size;
int ret;
size -= size % truncbdy;
if (size > biggest) {
biggest = size;
if (!quiet && testcalls > simulatedopcount)
prt("truncating to largest ever: 0x%x\n", size);
}
log4(OP_TRUNCATE, size, (unsigned)file_size, 0);
if (size > file_size)
memset(good_buf + file_size, '\0', size - file_size);
else if (size < file_size)
memset(good_buf + size, '\0', file_size - size);
file_size = size;
if (testcalls <= simulatedopcount)
return;
if ((progressinterval && testcalls % progressinterval == 0) ||
(debug && (monitorstart == -1 || monitorend == -1 ||
(long)size <= monitorend)))
prt("%lu trunc\tfrom 0x%x to 0x%x\n", testcalls, oldsize, size);
ret = ops->resize(&ctx, size);
if (ret < 0) {
prterrcode("dotruncate: ops->resize", ret);
report_failure(160);
}
}
void
do_punch_hole(unsigned offset, unsigned length)
{
unsigned end_offset;
int max_offset = 0;
int max_len = 0;
int ret;
offset -= offset % holebdy;
length -= length % holebdy;
if (length == 0) {
if (!quiet && testcalls > simulatedopcount)
prt("skipping zero length punch hole\n");
log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
return;
}
if (file_size <= (loff_t)offset) {
if (!quiet && testcalls > simulatedopcount)
prt("skipping hole punch off the end of the file\n");
log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, length);
return;
}
end_offset = offset + length;
log4(OP_PUNCH_HOLE, offset, length, 0);
if (testcalls <= simulatedopcount)
return;
if ((progressinterval && testcalls % progressinterval == 0) ||
(debug && (monitorstart == -1 || monitorend == -1 ||
(long)end_offset <= monitorend))) {
prt("%lu punch\tfrom 0x%x to 0x%x, (0x%x bytes)\n", testcalls,
offset, offset+length, length);
}
ret = ops->discard(&ctx, (unsigned long long)offset,
(unsigned long long)length);
if (ret < 0) {
prterrcode("do_punch_hole: ops->discard", ret);
report_failure(161);
}
max_offset = offset < file_size ? offset : file_size;
max_len = max_offset + length <= file_size ? length :
file_size - max_offset;
memset(good_buf + max_offset, '\0', max_len);
}
unsigned get_data_size(unsigned size)
{
unsigned i;
unsigned hint;
unsigned max = sqrt((double)size) + 1;
unsigned good = 1;
unsigned curr = good;
hint = get_random() % max;
for (i = 1; i < max && curr < hint; i++) {
if (size % i == 0) {
good = curr;
curr = i;
}
}
if (curr == hint)
good = curr;
return good;
}
void
dowritesame(unsigned offset, unsigned size)
{
ssize_t ret;
off_t newsize;
unsigned buf_off;
unsigned data_size;
int n;
offset -= offset % writebdy;
if (o_direct)
size -= size % writebdy;
if (size == 0) {
if (!quiet && testcalls > simulatedopcount && !o_direct)
prt("skipping zero size writesame\n");
log4(OP_SKIPPED, OP_WRITESAME, offset, size);
return;
}
data_size = get_data_size(size);
log4(OP_WRITESAME, offset, size, data_size);
gendata(original_buf, good_buf, offset, data_size);
if (file_size < offset + size) {
newsize = ceil(((double)offset + size) / truncbdy) * truncbdy;
if (file_size < newsize)
memset(good_buf + file_size, '\0', newsize - file_size);
file_size = newsize;
if (lite) {
warn("Lite file size bug in fsx!");
report_failure(162);
}
ret = ops->resize(&ctx, newsize);
if (ret < 0) {
prterrcode("dowritesame: ops->resize", ret);
report_failure(163);
}
}
for (n = size / data_size, buf_off = data_size; n > 1; n--) {
memcpy(good_buf + offset + buf_off, good_buf + offset, data_size);
buf_off += data_size;
}
if (testcalls <= simulatedopcount)
return;
if (!quiet &&
((progressinterval && testcalls % progressinterval == 0) ||
(debug &&
(monitorstart == -1 ||
(static_cast<long>(offset + size) > monitorstart &&
(monitorend == -1 ||
static_cast<long>(offset) <= monitorend))))))
prt("%lu writesame\t0x%x thru\t0x%x\tdata_size\t0x%x(0x%x bytes)\n", testcalls,
offset, offset + size - 1, data_size, size);
ret = ops->writesame(&ctx, offset, size, good_buf + offset, data_size);
if (ret != (ssize_t)size) {
if (ret < 0)
prterrcode("dowritesame: ops->writesame", ret);
else
prt("short writesame: 0x%x bytes instead of 0x%x\n",
ret, size);
report_failure(164);
}
if (flush_enabled)
doflush(offset, size);
}
void
docompareandwrite(unsigned offset, unsigned size)
{
int ret;
if (skip_partial_discard) {
if (!quiet && testcalls > simulatedopcount)
prt("compare and write disabled\n");
log4(OP_SKIPPED, OP_COMPARE_AND_WRITE, offset, size);
return;
}
offset -= offset % writebdy;
if (o_direct)
size -= size % writebdy;
if (size == 0) {
if (!quiet && testcalls > simulatedopcount && !o_direct)
prt("skipping zero size read\n");
log4(OP_SKIPPED, OP_READ, offset, size);
return;
}
if (size + offset > file_size) {
if (!quiet && testcalls > simulatedopcount)
prt("skipping seek/compare past end of file\n");
log4(OP_SKIPPED, OP_COMPARE_AND_WRITE, offset, size);
return;
}
memcpy(temp_buf + offset, good_buf + offset, size);
gendata(original_buf, good_buf, offset, size);
log4(OP_COMPARE_AND_WRITE, offset, size, 0);
if (testcalls <= simulatedopcount)
return;
if (!quiet &&
((progressinterval && testcalls % progressinterval == 0) ||
(debug &&
(monitorstart == -1 ||
(static_cast<long>(offset + size) > monitorstart &&
(monitorend == -1 ||
static_cast<long>(offset) <= monitorend))))))
prt("%lu compareandwrite\t0x%x thru\t0x%x\t(0x%x bytes)\n", testcalls,
offset, offset + size - 1, size);
ret = ops->compare_and_write(&ctx, offset, size, temp_buf + offset,
good_buf + offset);
if (ret != (ssize_t)size) {
if (ret == -EINVAL) {
memcpy(good_buf + offset, temp_buf + offset, size);
return;
}
if (ret < 0)
prterrcode("docompareandwrite: ops->compare_and_write", ret);
else
prt("short write: 0x%x bytes instead of 0x%x\n", ret, size);
report_failure(151);
return;
}
if (flush_enabled)
doflush(offset, size);
}
void clone_filename(char *buf, size_t len, int clones)
{
#if __GNUC__ && __GNUC__ >= 8
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"
#endif
snprintf(buf, len, "%s/fsx-%s-parent%d",
dirpath, iname, clones);
#if __GNUC__ && __GNUC__ >= 8
#pragma GCC diagnostic pop
#endif
}
void clone_imagename(char *buf, size_t len, int clones)
{
if (clones > 0) {
snprintf(buf, len, "%s-clone%d", iname, clones);
} else {
strncpy(buf, iname, len - 1);
buf[len - 1] = '\0';
}
}
void replay_imagename(char *buf, size_t len, int clones)
{
clone_imagename(buf, len, clones);
strncat(buf, "-replay", len - strlen(buf));
buf[len - 1] = '\0';
}
void check_clone(int clonenum, bool replay_image);
void
do_clone()
{
char filename[1024];
char imagename[1024];
char lastimagename[1024];
int ret, fd;
int order = 0, stripe_unit = 0, stripe_count = 0;
uint64_t newsize = file_size;
log4(OP_CLONE, 0, 0, 0);
++num_clones;
if (randomize_striping) {
order = 18 + get_random() % 8;
stripe_unit = 1ull << (order - 1 - (get_random() % 8));
stripe_count = 2 + get_random() % 14;
}
prt("%lu clone\t%d order %d su %d sc %d\n", testcalls, num_clones,
order, stripe_unit, stripe_count);
clone_imagename(imagename, sizeof(imagename), num_clones);
clone_imagename(lastimagename, sizeof(lastimagename),
num_clones - 1);
ceph_assert(strcmp(lastimagename, ctx.name) == 0);
ret = ops->clone(&ctx, "snap", imagename, &order, stripe_unit,
stripe_count);
if (ret < 0) {
prterrcode("do_clone: ops->clone", ret);
exit(165);
}
if (randomize_parent_overlap && rbd_image_has_parent(&ctx)) {
int rand = get_random() % 16 + 1; // [1..16]
if (rand < 13) {
uint64_t overlap;
ret = rbd_get_overlap(ctx.image, &overlap);
if (ret < 0) {
prterrcode("do_clone: rbd_get_overlap", ret);
exit(1);
}
if (rand < 10) { // 9/16
newsize = overlap * ((double)rand / 10);
newsize -= newsize % truncbdy;
} else { // 3/16
newsize = 0;
}
ceph_assert(newsize != (uint64_t)file_size);
prt("truncating image %s from 0x%llx (overlap 0x%llx) to 0x%llx\n",
ctx.name, file_size, overlap, newsize);
ret = ops->resize(&ctx, newsize);
if (ret < 0) {
prterrcode("do_clone: ops->resize", ret);
exit(1);
}
} else if (rand < 15) { // 2/16
prt("flattening image %s\n", ctx.name);
ret = ops->flatten(&ctx);
if (ret < 0) {
prterrcode("do_clone: ops->flatten", ret);
exit(1);
}
} else { // 2/16
prt("leaving image %s intact\n", ctx.name);
}
}
clone_filename(filename, sizeof(filename), num_clones);
if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
simple_err("do_clone: open", -errno);
exit(162);
}
save_buffer(good_buf, newsize, fd);
if ((ret = close(fd)) < 0) {
simple_err("do_clone: close", -errno);
exit(163);
}
/*
* Close parent.
*/
if ((ret = ops->close(&ctx)) < 0) {
prterrcode("do_clone: ops->close", ret);
exit(174);
}
if (journal_replay) {
ret = finalize_journal(ioctx, lastimagename, num_clones - 1,
order, stripe_unit, stripe_count);
if (ret < 0) {
exit(EXIT_FAILURE);
}
ret = register_journal(ioctx, imagename);
if (ret < 0) {
exit(EXIT_FAILURE);
}
}
/*
* Open freshly made clone.
*/
if ((ret = ops->open(imagename, &ctx)) < 0) {
prterrcode("do_clone: ops->open", ret);
exit(166);
}
if (num_clones > 1) {
if (journal_replay) {
check_clone(num_clones - 2, true);
}
check_clone(num_clones - 2, false);
}
}
void
check_clone(int clonenum, bool replay_image)
{
char filename[128];
char imagename[128];
int ret, fd;
struct rbd_ctx cur_ctx = RBD_CTX_INIT;
struct stat file_info;
char *good_buf, *temp_buf;
if (replay_image) {
replay_imagename(imagename, sizeof(imagename), clonenum);
} else {
clone_imagename(imagename, sizeof(imagename), clonenum);
}
if ((ret = ops->open(imagename, &cur_ctx)) < 0) {
prterrcode("check_clone: ops->open", ret);
exit(167);
}
clone_filename(filename, sizeof(filename), clonenum + 1);
if ((fd = open(filename, O_RDONLY)) < 0) {
simple_err("check_clone: open", -errno);
exit(168);
}
prt("checking clone #%d, image %s against file %s\n",
clonenum, imagename, filename);
if ((ret = fstat(fd, &file_info)) < 0) {
simple_err("check_clone: fstat", -errno);
exit(169);
}
good_buf = NULL;
ret = posix_memalign((void **)&good_buf,
std::max(writebdy, (int)sizeof(void *)),
file_info.st_size);
if (ret > 0) {
prterrcode("check_clone: posix_memalign(good_buf)", -ret);
exit(96);
}
temp_buf = NULL;
ret = posix_memalign((void **)&temp_buf,
std::max(readbdy, (int)sizeof(void *)),
file_info.st_size);
if (ret > 0) {
prterrcode("check_clone: posix_memalign(temp_buf)", -ret);
exit(97);
}
if ((ret = pread(fd, good_buf, file_info.st_size, 0)) < 0) {
simple_err("check_clone: pread", -errno);
exit(170);
}
if ((ret = ops->read(&cur_ctx, 0, file_info.st_size, temp_buf)) < 0) {
prterrcode("check_clone: ops->read", ret);
exit(171);
}
close(fd);
if ((ret = ops->close(&cur_ctx)) < 0) {
prterrcode("check_clone: ops->close", ret);
exit(174);
}
check_buffers(good_buf, temp_buf, 0, file_info.st_size);
if (!replay_image) {
unlink(filename);
}
free(good_buf);
free(temp_buf);
}
void
writefileimage()
{
ssize_t ret;
ret = ops->write(&ctx, 0, file_size, good_buf);
if (ret != file_size) {
if (ret < 0)
prterrcode("writefileimage: ops->write", ret);
else
prt("short write: 0x%x bytes instead of 0x%llx\n",
ret, (unsigned long long)file_size);
report_failure(172);
}
if (!lite) {
ret = ops->resize(&ctx, file_size);
if (ret < 0) {
prterrcode("writefileimage: ops->resize", ret);
report_failure(173);
}
}
}
void
do_flatten()
{
int ret;
if (!rbd_image_has_parent(&ctx)) {
log4(OP_SKIPPED, OP_FLATTEN, 0, 0);
return;
}
log4(OP_FLATTEN, 0, 0, 0);
prt("%lu flatten\n", testcalls);
ret = ops->flatten(&ctx);
if (ret < 0) {
prterrcode("writefileimage: ops->flatten", ret);
exit(177);
}
}
void
docloseopen(void)
{
char *name;
int ret;
if (testcalls <= simulatedopcount)
return;
name = strdup(ctx.name);
if (debug)
prt("%lu close/open\n", testcalls);
ret = ops->close(&ctx);
if (ret < 0) {
prterrcode("docloseopen: ops->close", ret);
report_failure(180);
}
ret = ops->open(name, &ctx);
if (ret < 0) {
prterrcode("docloseopen: ops->open", ret);
report_failure(181);
}
free(name);
}
#define TRIM_OFF_LEN(off, len, size) \
do { \
if (size) \
(off) %= (size); \
else \
(off) = 0; \
if ((unsigned)(off) + (unsigned)(len) > (unsigned)(size)) \
(len) = (size) - (off); \
} while (0)
void
test(void)
{
unsigned long offset;
unsigned long size = maxoplen;
unsigned long rv = get_random();
unsigned long op;
if (simulatedopcount > 0 && testcalls == simulatedopcount)
writefileimage();
testcalls++;
if (closeprob)
closeopen = (rv >> 3) < (1u << 28) / (unsigned)closeprob;
if (debugstart > 0 && testcalls >= debugstart)
debug = 1;
if (!quiet && testcalls < simulatedopcount && testcalls % 100000 == 0)
prt("%lu...\n", testcalls);
offset = get_random();
if (randomoplen)
size = get_random() % (maxoplen + 1);
/* calculate appropriate op to run */
if (lite)
op = rv % OP_MAX_LITE;
else
op = rv % OP_MAX_FULL;
switch (op) {
case OP_MAPREAD:
if (!mapped_reads)
op = OP_READ;
break;
case OP_MAPWRITE:
if (!mapped_writes)
op = OP_WRITE;
break;
case OP_FALLOCATE:
if (!fallocate_calls) {
log4(OP_SKIPPED, OP_FALLOCATE, offset, size);
goto out;
}
break;
case OP_PUNCH_HOLE:
if (!punch_hole_calls) {
log4(OP_SKIPPED, OP_PUNCH_HOLE, offset, size);
goto out;
}
break;
case OP_CLONE:
/* clone, 8% chance */
if (!clone_calls || file_size == 0 || get_random() % 100 >= 8) {
log4(OP_SKIPPED, OP_CLONE, 0, 0);
goto out;
}
break;
case OP_FLATTEN:
/* flatten four times as rarely as clone, 2% chance */
if (get_random() % 100 >= 2) {
log4(OP_SKIPPED, OP_FLATTEN, 0, 0);
goto out;
}
break;
case OP_WRITESAME:
/* writesame not implemented */
if (!ops->writesame) {
log4(OP_SKIPPED, OP_WRITESAME, offset, size);
goto out;
}
break;
case OP_COMPARE_AND_WRITE:
/* compare_and_write not implemented */
if (!ops->compare_and_write) {
log4(OP_SKIPPED, OP_COMPARE_AND_WRITE, offset, size);
goto out;
}
break;
}
switch (op) {
case OP_READ:
TRIM_OFF_LEN(offset, size, file_size);
doread(offset, size);
break;
case OP_WRITE:
TRIM_OFF_LEN(offset, size, maxfilelen);
dowrite(offset, size);
break;
case OP_MAPREAD:
TRIM_OFF_LEN(offset, size, file_size);
exit(183);
break;
case OP_MAPWRITE:
TRIM_OFF_LEN(offset, size, maxfilelen);
exit(182);
break;
case OP_TRUNCATE:
if (!style)
size = get_random() % maxfilelen;
dotruncate(size);
break;
case OP_PUNCH_HOLE:
TRIM_OFF_LEN(offset, size, file_size);
do_punch_hole(offset, size);
break;
case OP_WRITESAME:
TRIM_OFF_LEN(offset, size, maxfilelen);
dowritesame(offset, size);
break;
case OP_COMPARE_AND_WRITE:
TRIM_OFF_LEN(offset, size, file_size);
docompareandwrite(offset, size);
break;
case OP_CLONE:
do_clone();
break;
case OP_FLATTEN:
do_flatten();
break;
default:
prterr("test: unknown operation");
report_failure(42);
break;
}
out:
if (sizechecks && testcalls > simulatedopcount)
check_size();
if (closeopen)
docloseopen();
}
void
cleanup(int sig)
{
if (sig)
prt("signal %d\n", sig);
prt("testcalls = %lu\n", testcalls);
exit(sig);
}
void
usage(void)
{
fprintf(stdout, "usage: %s",
"fsx [-dfjknqxyACFHKLORUWZ] [-b opnum] [-c Prob] [-h holebdy] [-l flen] [-m start:end] [-o oplen] [-p progressinterval] [-r readbdy] [-s style] [-t truncbdy] [-w writebdy] [-D startingop] [-N numops] [-P dirpath] [-S seed] pname iname\n\
-b opnum: beginning operation number (default 1)\n\
-c P: 1 in P chance of file close+open at each op (default infinity)\n\
-d: debug output for all operations\n\
-f: flush and invalidate cache after I/O\n\
-g: deep copy instead of clone\n\
-h holebdy: 4096 would make discards page aligned (default 1)\n\
-j: journal replay stress test\n\
-k: keep data on success (default 0)\n\
-l flen: the upper bound on file size (default 262144)\n\
-m startop:endop: monitor (print debug output) specified byte range (default 0:infinity)\n\
-n: no verifications of file size\n\
-o oplen: the upper bound on operation size (default 65536)\n\
-p progressinterval: debug output at specified operation interval\n\
-q: quieter operation\n\
-r readbdy: 4096 would make reads page aligned (default 1)\n\
-s style: 1 gives smaller truncates (default 0)\n\
-t truncbdy: 4096 would make truncates page aligned (default 1)\n\
-w writebdy: 4096 would make writes page aligned (default 1)\n\
-x: preallocate file space before starting, XFS only (default 0)\n\
-y: synchronize changes to a file\n"
" -C: do not use clone calls\n\
-D startingop: debug output starting at specified operation\n"
#ifdef FALLOCATE
" -F: Do not use fallocate (preallocation) calls\n"
#endif
#if defined(__FreeBSD__)
" -G: enable rbd-ggate mode (use -L, -r and -w too)\n"
#endif
" -H: do not use punch hole calls\n"
#if defined(WITH_KRBD)
" -K: enable krbd mode (use -t and -h too)\n"
#endif
#if defined(__linux__)
" -M: enable rbd-nbd mode (use -t and -h too)\n"
#endif
" -L: fsxLite - no file creations & no file size changes\n\
-N numops: total # operations to do (default infinity)\n\
-O: use oplen (see -o flag) for every op (default random)\n\
-P dirpath: save .fsxlog and .fsxgood files in dirpath (default ./)\n\
-R: read() system calls only (mapped reads disabled)\n\
-S seed: for random # generator (default 1) 0 gets timestamp\n\
-U: disable randomized striping\n\
-W: mapped write operations DISabled\n\
-Z: O_DIRECT (use -R, -W, -r and -w too)\n\
poolname: this is REQUIRED (no default)\n\
imagename: this is REQUIRED (no default)\n");
exit(89);
}
int
getnum(char *s, char **e)
{
int ret;
*e = (char *) 0;
ret = strtol(s, e, 0);
if (*e)
switch (**e) {
case 'b':
case 'B':
ret *= 512;
*e = *e + 1;
break;
case 'k':
case 'K':
ret *= 1024;
*e = *e + 1;
break;
case 'm':
case 'M':
ret *= 1024*1024;
*e = *e + 1;
break;
case 'w':
case 'W':
ret *= 4;
*e = *e + 1;
break;
}
return (ret);
}
void
test_fallocate()
{
#ifdef FALLOCATE
if (!lite && fallocate_calls) {
if (fallocate(fd, 0, 0, 1) && errno == EOPNOTSUPP) {
if(!quiet)
warn("main: filesystem does not support fallocate, disabling\n");
fallocate_calls = 0;
} else {
ftruncate(fd, 0);
}
}
#else /* ! FALLOCATE */
fallocate_calls = 0;
#endif
}
void remove_image(rados_ioctx_t ioctx, char *imagename, bool remove_snap,
bool unregister) {
rbd_image_t image;
char errmsg[128];
int ret;
if ((ret = rbd_open(ioctx, imagename, &image, NULL)) < 0) {
sprintf(errmsg, "rbd_open %s", imagename);
prterrcode(errmsg, ret);
report_failure(101);
}
if (remove_snap) {
if ((ret = rbd_snap_unprotect(image, "snap")) < 0) {
sprintf(errmsg, "rbd_snap_unprotect %s@snap",
imagename);
prterrcode(errmsg, ret);
report_failure(102);
}
if ((ret = rbd_snap_remove(image, "snap")) < 0) {
sprintf(errmsg, "rbd_snap_remove %s@snap",
imagename);
prterrcode(errmsg, ret);
report_failure(103);
}
}
if ((ret = rbd_close(image)) < 0) {
sprintf(errmsg, "rbd_close %s", imagename);
prterrcode(errmsg, ret);
report_failure(104);
}
if (unregister &&
(ret = unregister_journal(ioctx, imagename)) < 0) {
report_failure(105);
}
if ((ret = rbd_remove(ioctx, imagename)) < 0) {
sprintf(errmsg, "rbd_remove %s", imagename);
prterrcode(errmsg, ret);
report_failure(106);
}
}
int
main(int argc, char **argv)
{
enum {
LONG_OPT_CLUSTER = 1000,
LONG_OPT_ID = 1001
};
int i, style, ch, ret;
char *endp;
char goodfile[1024];
char logfile[1024];
const char* optstring = "b:c:dfgh:jkl:m:no:p:qr:s:t:w:xyCD:FGHKMLN:OP:RS:UWZ";
const struct option longopts[] = {
{"cluster", 1, NULL, LONG_OPT_CLUSTER},
{"id", 1, NULL, LONG_OPT_ID}};
goodfile[0] = 0;
logfile[0] = 0;
page_size = PAGE_SIZE;
page_mask = page_size - 1;
mmap_mask = page_mask;
setvbuf(stdout, (char *)0, _IOLBF, 0); /* line buffered stdout */
while ((ch = getopt_long(argc, argv, optstring, longopts, NULL)) != EOF) {
switch (ch) {
case LONG_OPT_CLUSTER:
cluster_name = optarg;
break;
case LONG_OPT_ID:
client_id = optarg;
break;
case 'b':
simulatedopcount = getnum(optarg, &endp);
if (!quiet)
fprintf(stdout, "Will begin at operation %lu\n",
simulatedopcount);
if (simulatedopcount == 0)
usage();
simulatedopcount -= 1;
break;
case 'c':
closeprob = getnum(optarg, &endp);
if (!quiet)
fprintf(stdout,
"Chance of close/open is 1 in %d\n",
closeprob);
if (closeprob <= 0)
usage();
break;
case 'd':
debug = 1;
break;
case 'f':
flush_enabled = 1;
break;
case 'g':
deep_copy = 1;
break;
case 'h':
holebdy = getnum(optarg, &endp);
if (holebdy <= 0)
usage();
break;
case 'j':
journal_replay = true;
break;
case 'k':
keep_on_success = 1;
break;
case 'l':
{
int _num = getnum(optarg, &endp);
if (_num <= 0)
usage();
maxfilelen = _num;
}
break;
case 'm':
monitorstart = getnum(optarg, &endp);
if (monitorstart < 0)
usage();
if (!endp || *endp++ != ':')
usage();
monitorend = getnum(endp, &endp);
if (monitorend < 0)
usage();
if (monitorend == 0)
monitorend = -1; /* aka infinity */
debug = 1;
break;
case 'n':
sizechecks = 0;
break;
case 'o':
maxoplen = getnum(optarg, &endp);
if (maxoplen <= 0)
usage();
break;
case 'p':
progressinterval = getnum(optarg, &endp);
if (progressinterval == 0)
usage();
break;
case 'q':
quiet = 1;
break;
case 'r':
readbdy = getnum(optarg, &endp);
if (readbdy <= 0)
usage();
break;
case 's':
style = getnum(optarg, &endp);
if (style < 0 || style > 1)
usage();
break;
case 't':
truncbdy = getnum(optarg, &endp);
if (truncbdy <= 0)
usage();
break;
case 'w':
writebdy = getnum(optarg, &endp);
if (writebdy <= 0)
usage();
break;
case 'x':
prealloc = 1;
break;
case 'y':
do_fsync = 1;
break;
case 'C':
clone_calls = 0;
break;
case 'D':
debugstart = getnum(optarg, &endp);
if (debugstart < 1)
usage();
break;
case 'F':
fallocate_calls = 0;
break;
#if defined(__FreeBSD__)
case 'G':
prt("rbd-ggate mode enabled\n");
ops = &ggate_operations;
break;
#endif
case 'H':
punch_hole_calls = 0;
break;
#if defined(WITH_KRBD)
case 'K':
prt("krbd mode enabled\n");
ops = &krbd_operations;
break;
#endif
#if defined(__linux__)
case 'M':
prt("rbd-nbd mode enabled\n");
ops = &nbd_operations;
break;
#endif
case 'L':
lite = 1;
break;
case 'N':
numops = getnum(optarg, &endp);
if (numops < 0)
usage();
break;
case 'O':
randomoplen = 0;
break;
case 'P':
strncpy(dirpath, optarg, sizeof(dirpath)-1);
dirpath[sizeof(dirpath)-1] = '\0';
strncpy(goodfile, dirpath, sizeof(goodfile)-1);
goodfile[sizeof(goodfile)-1] = '\0';
if (strlen(goodfile) < sizeof(goodfile)-2) {
strcat(goodfile, "/");
} else {
prt("file name to long\n");
exit(1);
}
strncpy(logfile, dirpath, sizeof(logfile)-1);
logfile[sizeof(logfile)-1] = '\0';
if (strlen(logfile) < sizeof(logfile)-2) {
strcat(logfile, "/");
} else {
prt("file path to long\n");
exit(1);
}
break;
case 'R':
mapped_reads = 0;
if (!quiet)
fprintf(stdout, "mapped reads DISABLED\n");
break;
case 'S':
seed = getnum(optarg, &endp);
if (seed == 0)
seed = std::random_device()() % 10000;
if (!quiet)
fprintf(stdout, "Seed set to %d\n", seed);
if (seed < 0)
usage();
break;
case 'U':
randomize_striping = 0;
break;
case 'W':
mapped_writes = 0;
if (!quiet)
fprintf(stdout, "mapped writes DISABLED\n");
break;
case 'Z':
#ifdef O_DIRECT
o_direct = O_DIRECT;
#endif
break;
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc != 2)
usage();
pool = argv[0];
iname = argv[1];
#ifndef _WIN32
signal(SIGHUP, cleanup);
signal(SIGINT, cleanup);
signal(SIGPIPE, cleanup);
signal(SIGALRM, cleanup);
signal(SIGTERM, cleanup);
signal(SIGXCPU, cleanup);
signal(SIGXFSZ, cleanup);
signal(SIGVTALRM, cleanup);
signal(SIGUSR1, cleanup);
signal(SIGUSR2, cleanup);
#endif
random_generator.seed(seed);
if (lite) {
file_size = maxfilelen;
}
ret = create_image();
if (ret < 0) {
prterrcode(iname, ret);
exit(90);
}
ret = ops->open(iname, &ctx);
if (ret < 0) {
simple_err("Error opening image", ret);
exit(91);
}
if (!dirpath[0])
strcat(dirpath, ".");
strncat(goodfile, iname, 256);
strcat (goodfile, ".fsxgood");
fsxgoodfd = open(goodfile, O_RDWR|O_CREAT|O_TRUNC, 0666);
if (fsxgoodfd < 0) {
prterr(goodfile);
exit(92);
}
strncat(logfile, iname, 256);
strcat (logfile, ".fsxlog");
fsxlogf = fopen(logfile, "w");
if (fsxlogf == NULL) {
prterr(logfile);
exit(93);
}
original_buf = (char *) malloc(maxfilelen);
for (i = 0; i < (int)maxfilelen; i++)
original_buf[i] = get_random() % 256;
ret = posix_memalign((void **)&good_buf,
std::max(writebdy, (int)sizeof(void *)), maxfilelen);
if (ret > 0) {
if (ret == EINVAL)
prt("writebdy is not a suitable power of two\n");
else
prterrcode("main: posix_memalign(good_buf)", -ret);
exit(94);
}
memset(good_buf, '\0', maxfilelen);
ret = posix_memalign((void **)&temp_buf,
std::max(readbdy, (int)sizeof(void *)), maxfilelen);
if (ret > 0) {
if (ret == EINVAL)
prt("readbdy is not a suitable power of two\n");
else
prterrcode("main: posix_memalign(temp_buf)", -ret);
exit(95);
}
memset(temp_buf, '\0', maxfilelen);
if (lite) { /* zero entire existing file */
ssize_t written;
written = ops->write(&ctx, 0, (size_t)maxfilelen, good_buf);
if (written != (ssize_t)maxfilelen) {
if (written < 0) {
prterrcode(iname, written);
warn("main: error on write");
} else
warn("main: short write, 0x%x bytes instead "
"of 0x%lx\n",
(unsigned)written,
maxfilelen);
exit(98);
}
} else
check_trunc_hack();
//test_fallocate();
while (numops == -1 || numops--)
test();
ret = ops->close(&ctx);
if (ret < 0) {
prterrcode("ops->close", ret);
report_failure(99);
}
if (journal_replay) {
char imagename[1024];
clone_imagename(imagename, sizeof(imagename), num_clones);
ret = finalize_journal(ioctx, imagename, num_clones, 0, 0, 0);
if (ret < 0) {
report_failure(100);
}
}
if (num_clones > 0) {
if (journal_replay) {
check_clone(num_clones - 1, true);
}
check_clone(num_clones - 1, false);
}
if (!keep_on_success) {
while (num_clones >= 0) {
static bool remove_snap = false;
if (journal_replay) {
char replayimagename[1024];
replay_imagename(replayimagename,
sizeof(replayimagename),
num_clones);
remove_image(ioctx, replayimagename,
remove_snap,
false);
}
char clonename[128];
clone_imagename(clonename, 128, num_clones);
remove_image(ioctx, clonename, remove_snap,
journal_replay);
remove_snap = true;
num_clones--;
}
}
prt("All operations completed A-OK!\n");
fclose(fsxlogf);
rados_ioctx_destroy(ioctx);
#if defined(WITH_KRBD)
krbd_destroy(krbd);
#endif
rados_shutdown(cluster);
free(original_buf);
free(good_buf);
free(temp_buf);
exit(0);
return 0;
}
| 79,455 | 22.037402 | 239 |
cc
|
null |
ceph-main/src/test/librbd/test_BlockGuard.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/BlockGuard.h"
namespace librbd {
class TestIOBlockGuard : public TestFixture {
public:
static uint32_t s_index;
struct Operation {
uint32_t index;
Operation() : index(++s_index) {
}
Operation(Operation &&rhs) : index(rhs.index) {
}
Operation(const Operation &) = delete;
Operation& operator=(Operation &&rhs) {
index = rhs.index;
return *this;
}
bool operator==(const Operation &rhs) const {
return index == rhs.index;
}
};
typedef std::list<Operation> Operations;
typedef BlockGuard<Operation> OpBlockGuard;
void SetUp() override {
TestFixture::SetUp();
m_cct = reinterpret_cast<CephContext*>(m_ioctx.cct());
}
CephContext *m_cct;
};
TEST_F(TestIOBlockGuard, NonDetainedOps) {
OpBlockGuard op_block_guard(m_cct);
Operation op1;
BlockGuardCell *cell1;
ASSERT_EQ(0, op_block_guard.detain({1, 3}, &op1, &cell1));
Operation op2;
BlockGuardCell *cell2;
ASSERT_EQ(0, op_block_guard.detain({0, 1}, &op2, &cell2));
Operation op3;
BlockGuardCell *cell3;
ASSERT_EQ(0, op_block_guard.detain({3, 6}, &op3, &cell3));
Operations released_ops;
op_block_guard.release(cell1, &released_ops);
ASSERT_TRUE(released_ops.empty());
op_block_guard.release(cell2, &released_ops);
ASSERT_TRUE(released_ops.empty());
op_block_guard.release(cell3, &released_ops);
ASSERT_TRUE(released_ops.empty());
}
TEST_F(TestIOBlockGuard, DetainedOps) {
OpBlockGuard op_block_guard(m_cct);
Operation op1;
BlockGuardCell *cell1;
ASSERT_EQ(0, op_block_guard.detain({1, 3}, &op1, &cell1));
Operation op2;
BlockGuardCell *cell2;
ASSERT_EQ(1, op_block_guard.detain({2, 6}, &op2, &cell2));
ASSERT_EQ(nullptr, cell2);
Operation op3;
BlockGuardCell *cell3;
ASSERT_EQ(2, op_block_guard.detain({0, 2}, &op3, &cell3));
ASSERT_EQ(nullptr, cell3);
Operations expected_ops;
expected_ops.push_back(std::move(op2));
expected_ops.push_back(std::move(op3));
Operations released_ops;
op_block_guard.release(cell1, &released_ops);
ASSERT_EQ(expected_ops, released_ops);
}
uint32_t TestIOBlockGuard::s_index = 0;
} // namespace librbd
| 2,343 | 22.676768 | 70 |
cc
|
null |
ceph-main/src/test/librbd/test_DeepCopy.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/Operations.h"
#include "librbd/api/Io.h"
#include "librbd/api/Image.h"
#include "librbd/api/Snapshot.h"
#include "librbd/internal.h"
#include "librbd/io/ReadResult.h"
#include "test/librados/crimson_utils.h"
void register_test_deep_copy() {
}
namespace librbd {
struct TestDeepCopy : public TestFixture {
void SetUp() override {
TestFixture::SetUp();
std::string image_name = get_temp_image_name();
int order = 22;
uint64_t size = (1 << order) * 20;
uint64_t features = 0;
bool old_format = !::get_features(&features);
EXPECT_EQ(0, create_image_full_pp(m_rbd, m_ioctx, image_name, size,
features, old_format, &order));
ASSERT_EQ(0, open_image(image_name, &m_src_ictx));
if (old_format) {
// The destination should always be in the new format.
uint64_t format = 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_FORMAT, format));
}
}
void TearDown() override {
if (m_src_ictx != nullptr) {
deep_copy();
if (m_dst_ictx != nullptr) {
compare();
close_image(m_dst_ictx);
}
close_image(m_src_ictx);
}
TestFixture::TearDown();
}
void deep_copy() {
std::string dst_name = get_temp_image_name();
librbd::NoOpProgressContext no_op;
EXPECT_EQ(0, api::Io<>::flush(*m_src_ictx));
EXPECT_EQ(0, librbd::api::Image<>::deep_copy(m_src_ictx, m_src_ictx->md_ctx,
dst_name.c_str(), m_opts,
no_op));
EXPECT_EQ(0, open_image(dst_name, &m_dst_ictx));
}
void compare() {
std::vector<librbd::snap_info_t> src_snaps, dst_snaps;
EXPECT_EQ(m_src_ictx->size, m_dst_ictx->size);
EXPECT_EQ(0, librbd::api::Snapshot<>::list(m_src_ictx, src_snaps));
EXPECT_EQ(0, librbd::api::Snapshot<>::list(m_dst_ictx, dst_snaps));
EXPECT_EQ(src_snaps.size(), dst_snaps.size());
for (size_t i = 0; i <= src_snaps.size(); i++) {
const char *src_snap_name = nullptr;
const char *dst_snap_name = nullptr;
if (i < src_snaps.size()) {
EXPECT_EQ(src_snaps[i].name, dst_snaps[i].name);
src_snap_name = src_snaps[i].name.c_str();
dst_snap_name = dst_snaps[i].name.c_str();
}
EXPECT_EQ(0, librbd::api::Image<>::snap_set(
m_src_ictx, cls::rbd::UserSnapshotNamespace(),
src_snap_name));
EXPECT_EQ(0, librbd::api::Image<>::snap_set(
m_dst_ictx, cls::rbd::UserSnapshotNamespace(),
dst_snap_name));
uint64_t src_size, dst_size;
{
std::shared_lock src_locker{m_src_ictx->image_lock};
std::shared_lock dst_locker{m_dst_ictx->image_lock};
src_size = m_src_ictx->get_image_size(m_src_ictx->snap_id);
dst_size = m_dst_ictx->get_image_size(m_dst_ictx->snap_id);
}
EXPECT_EQ(src_size, dst_size);
if (m_dst_ictx->test_features(RBD_FEATURE_LAYERING)) {
bool flags_set;
std::shared_lock dst_locker{m_dst_ictx->image_lock};
EXPECT_EQ(0, m_dst_ictx->test_flags(m_dst_ictx->snap_id,
RBD_FLAG_OBJECT_MAP_INVALID,
m_dst_ictx->image_lock, &flags_set));
EXPECT_FALSE(flags_set);
}
ssize_t read_size = 1 << m_src_ictx->order;
uint64_t offset = 0;
while (offset < src_size) {
read_size = std::min(read_size, static_cast<ssize_t>(src_size - offset));
bufferptr src_ptr(read_size);
bufferlist src_bl;
src_bl.push_back(src_ptr);
librbd::io::ReadResult src_result{&src_bl};
EXPECT_EQ(read_size, api::Io<>::read(
*m_src_ictx, offset, read_size,
librbd::io::ReadResult{src_result}, 0));
bufferptr dst_ptr(read_size);
bufferlist dst_bl;
dst_bl.push_back(dst_ptr);
librbd::io::ReadResult dst_result{&dst_bl};
EXPECT_EQ(read_size, api::Io<>::read(
*m_dst_ictx, offset, read_size,
librbd::io::ReadResult{dst_result}, 0));
if (!src_bl.contents_equal(dst_bl)) {
std::cout << "snap: " << (src_snap_name ? src_snap_name : "null")
<< ", block " << offset << "~" << read_size << " differs"
<< std::endl;
std::cout << "src block: " << std::endl; src_bl.hexdump(std::cout);
std::cout << "dst block: " << std::endl; dst_bl.hexdump(std::cout);
}
EXPECT_TRUE(src_bl.contents_equal(dst_bl));
offset += read_size;
}
}
}
void test_no_snaps() {
bufferlist bl;
bl.append(std::string(((1 << m_src_ictx->order) * 2) + 1, '1'));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 0 * bl.length(), bl.length(),
bufferlist{bl}, 0));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 2 * bl.length(), bl.length(),
bufferlist{bl}, 0));
}
void test_snaps() {
bufferlist bl;
bl.append(std::string(((1 << m_src_ictx->order) * 2) + 1, '1'));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 0 * bl.length(), bl.length(),
bufferlist{bl}, 0));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap1"));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 1 * bl.length(), bl.length(),
bufferlist{bl}, 0));
bufferlist bl1;
bl1.append(std::string(1000, 'X'));
ASSERT_EQ(static_cast<ssize_t>(bl1.length()),
api::Io<>::write(*m_src_ictx, 0 * bl.length(), bl1.length(),
bufferlist{bl1}, 0));
ASSERT_EQ(static_cast<ssize_t>(bl1.length()),
api::Io<>::discard(*m_src_ictx, bl1.length() + 10,
bl1.length(), false));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap2"));
ASSERT_EQ(static_cast<ssize_t>(bl1.length()),
api::Io<>::write(*m_src_ictx, 1 * bl.length(), bl1.length(),
bufferlist{bl1}, 0));
ASSERT_EQ(static_cast<ssize_t>(bl1.length()),
api::Io<>::discard(*m_src_ictx, 2 * bl1.length() + 10,
bl1.length(), false));
}
void test_snap_discard() {
bufferlist bl;
bl.append(std::string(100, '1'));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 0, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap"));
size_t len = (1 << m_src_ictx->order) * 2;
ASSERT_EQ(static_cast<ssize_t>(len),
api::Io<>::discard(*m_src_ictx, 0, len, false));
}
void test_clone_discard() {
bufferlist bl;
bl.append(std::string(100, '1'));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 0, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap"));
ASSERT_EQ(0, snap_protect(*m_src_ictx, "snap"));
std::string clone_name = get_temp_image_name();
int order = m_src_ictx->order;
uint64_t features;
ASSERT_EQ(0, librbd::get_features(m_src_ictx, &features));
ASSERT_EQ(0, librbd::clone(m_ioctx, m_src_ictx->name.c_str(), "snap",
m_ioctx, clone_name.c_str(), features, &order, 0,
0));
close_image(m_src_ictx);
ASSERT_EQ(0, open_image(clone_name, &m_src_ictx));
size_t len = (1 << m_src_ictx->order) * 2;
ASSERT_EQ(static_cast<ssize_t>(len),
api::Io<>::discard(*m_src_ictx, 0, len, false));
}
void test_clone_shrink() {
bufferlist bl;
bl.append(std::string(100, '1'));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 0, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap"));
ASSERT_EQ(0, snap_protect(*m_src_ictx, "snap"));
std::string clone_name = get_temp_image_name();
int order = m_src_ictx->order;
uint64_t features;
ASSERT_EQ(0, librbd::get_features(m_src_ictx, &features));
ASSERT_EQ(0, librbd::clone(m_ioctx, m_src_ictx->name.c_str(), "snap",
m_ioctx, clone_name.c_str(), features, &order, 0,
0));
close_image(m_src_ictx);
ASSERT_EQ(0, open_image(clone_name, &m_src_ictx));
librbd::NoOpProgressContext no_op;
auto new_size = m_src_ictx->size >> 1;
ASSERT_EQ(0, m_src_ictx->operations->resize(new_size, true, no_op));
}
void test_clone_expand() {
bufferlist bl;
bl.append(std::string(100, '1'));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 0, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap"));
ASSERT_EQ(0, snap_protect(*m_src_ictx, "snap"));
std::string clone_name = get_temp_image_name();
int order = m_src_ictx->order;
uint64_t features;
ASSERT_EQ(0, librbd::get_features(m_src_ictx, &features));
ASSERT_EQ(0, librbd::clone(m_ioctx, m_src_ictx->name.c_str(), "snap",
m_ioctx, clone_name.c_str(), features, &order, 0,
0));
close_image(m_src_ictx);
ASSERT_EQ(0, open_image(clone_name, &m_src_ictx));
librbd::NoOpProgressContext no_op;
auto new_size = m_src_ictx->size << 1;
ASSERT_EQ(0, m_src_ictx->operations->resize(new_size, true, no_op));
}
void test_clone_hide_parent() {
uint64_t object_size = 1 << m_src_ictx->order;
bufferlist bl;
bl.append(std::string(100, '1'));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, object_size, bl.length(),
bufferlist{bl}, 0));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap"));
ASSERT_EQ(0, snap_protect(*m_src_ictx, "snap"));
std::string clone_name = get_temp_image_name();
int order = m_src_ictx->order;
uint64_t features;
ASSERT_EQ(0, librbd::get_features(m_src_ictx, &features));
ASSERT_EQ(0, librbd::clone(m_ioctx, m_src_ictx->name.c_str(), "snap",
m_ioctx, clone_name.c_str(), features, &order, 0,
0));
close_image(m_src_ictx);
ASSERT_EQ(0, open_image(clone_name, &m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap1"));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::discard(*m_src_ictx, object_size, bl.length(), false));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap2"));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, m_src_ictx->operations->resize(object_size, true, no_op));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap3"));
ASSERT_EQ(0, m_src_ictx->operations->resize(2 * object_size, true, no_op));
}
void test_clone() {
bufferlist bl;
bl.append(std::string(((1 << m_src_ictx->order) * 2) + 1, '1'));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 0 * bl.length(), bl.length(),
bufferlist{bl}, 0));
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, 2 * bl.length(), bl.length(),
bufferlist{bl}, 0));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap"));
ASSERT_EQ(0, snap_protect(*m_src_ictx, "snap"));
std::string clone_name = get_temp_image_name();
int order = m_src_ictx->order;
uint64_t features;
ASSERT_EQ(0, librbd::get_features(m_src_ictx, &features));
ASSERT_EQ(0, librbd::clone(m_ioctx, m_src_ictx->name.c_str(), "snap",
m_ioctx, clone_name.c_str(), features, &order, 0,
0));
close_image(m_src_ictx);
ASSERT_EQ(0, open_image(clone_name, &m_src_ictx));
bufferlist bl1;
bl1.append(std::string(1000, 'X'));
ASSERT_EQ(static_cast<ssize_t>(bl1.length()),
api::Io<>::write(*m_src_ictx, 0 * bl.length(), bl1.length(),
bufferlist{bl1}, 0));
ASSERT_EQ(static_cast<ssize_t>(bl1.length()),
api::Io<>::discard(*m_src_ictx, bl1.length() + 10,
bl1.length(), false));
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
ASSERT_EQ(0, snap_create(*m_src_ictx, "snap"));
ASSERT_EQ(0, snap_protect(*m_src_ictx, "snap"));
clone_name = get_temp_image_name();
ASSERT_EQ(0, librbd::clone(m_ioctx, m_src_ictx->name.c_str(), "snap",
m_ioctx, clone_name.c_str(), features, &order, 0,
0));
close_image(m_src_ictx);
ASSERT_EQ(0, open_image(clone_name, &m_src_ictx));
ASSERT_EQ(static_cast<ssize_t>(bl1.length()),
api::Io<>::write(*m_src_ictx, 1 * bl.length(), bl1.length(),
bufferlist{bl1}, 0));
ASSERT_EQ(static_cast<ssize_t>(bl1.length()),
api::Io<>::discard(*m_src_ictx, 2 * bl1.length() + 10,
bl1.length(), false));
}
void test_stress() {
uint64_t initial_size, size;
{
std::shared_lock src_locker{m_src_ictx->image_lock};
size = initial_size = m_src_ictx->get_image_size(CEPH_NOSNAP);
}
int nsnaps = 4;
const char *c = getenv("TEST_RBD_DEEPCOPY_STRESS_NSNAPS");
if (c != NULL) {
std::stringstream ss(c);
ASSERT_TRUE(ss >> nsnaps);
}
int nwrites = 4;
c = getenv("TEST_RBD_DEEPCOPY_STRESS_NWRITES");
if (c != NULL) {
std::stringstream ss(c);
ASSERT_TRUE(ss >> nwrites);
}
for (int i = 0; i < nsnaps; i++) {
for (int j = 0; j < nwrites; j++) {
size_t len = rand() % ((1 << m_src_ictx->order) * 2);
ASSERT_GT(size, len);
bufferlist bl;
bl.append(std::string(len, static_cast<char>('A' + i)));
uint64_t off = std::min(static_cast<uint64_t>(rand() % size),
static_cast<uint64_t>(size - len));
std::cout << "write: " << static_cast<char>('A' + i) << " " << off
<< "~" << len << std::endl;
ASSERT_EQ(static_cast<ssize_t>(bl.length()),
api::Io<>::write(*m_src_ictx, off, bl.length(),
bufferlist{bl}, 0));
len = rand() % ((1 << m_src_ictx->order) * 2);
ASSERT_GT(size, len);
off = std::min(static_cast<uint64_t>(rand() % size),
static_cast<uint64_t>(size - len));
std::cout << "discard: " << off << "~" << len << std::endl;
ASSERT_EQ(static_cast<ssize_t>(len),
api::Io<>::discard(*m_src_ictx, off, len, false));
}
ASSERT_EQ(0, api::Io<>::flush(*m_src_ictx));
std::string snap_name = "snap" + stringify(i);
std::cout << "snap: " << snap_name << std::endl;
ASSERT_EQ(0, snap_create(*m_src_ictx, snap_name.c_str()));
if (m_src_ictx->test_features(RBD_FEATURE_LAYERING) && rand() % 4) {
ASSERT_EQ(0, snap_protect(*m_src_ictx, snap_name.c_str()));
std::string clone_name = get_temp_image_name();
int order = m_src_ictx->order;
uint64_t features;
ASSERT_EQ(0, librbd::get_features(m_src_ictx, &features));
std::cout << "clone " << m_src_ictx->name << " -> " << clone_name
<< std::endl;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_src_ictx->name.c_str(),
snap_name.c_str(), m_ioctx,
clone_name.c_str(), features, &order,
m_src_ictx->stripe_unit,
m_src_ictx->stripe_count));
close_image(m_src_ictx);
ASSERT_EQ(0, open_image(clone_name, &m_src_ictx));
}
if (rand() % 2) {
librbd::NoOpProgressContext no_op;
uint64_t new_size = initial_size + rand() % size;
std::cout << "resize: " << new_size << std::endl;
ASSERT_EQ(0, m_src_ictx->operations->resize(new_size, true, no_op));
{
std::shared_lock src_locker{m_src_ictx->image_lock};
size = m_src_ictx->get_image_size(CEPH_NOSNAP);
}
ASSERT_EQ(new_size, size);
}
}
}
librbd::ImageCtx *m_src_ictx = nullptr;
librbd::ImageCtx *m_dst_ictx = nullptr;
librbd::ImageOptions m_opts;
};
TEST_F(TestDeepCopy, Empty)
{
}
TEST_F(TestDeepCopy, NoSnaps)
{
test_no_snaps();
}
TEST_F(TestDeepCopy, Snaps)
{
test_snaps();
}
TEST_F(TestDeepCopy, SnapDiscard)
{
test_snap_discard();
}
TEST_F(TestDeepCopy, CloneDiscard)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
test_clone_discard();
}
TEST_F(TestDeepCopy, CloneShrink)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
test_clone_shrink();
}
TEST_F(TestDeepCopy, CloneExpand)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
test_clone_expand();
}
TEST_F(TestDeepCopy, CloneHideParent)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
test_clone_hide_parent();
}
TEST_F(TestDeepCopy, Clone)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
test_clone();
}
TEST_F(TestDeepCopy, CloneFlatten)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
uint64_t flatten = 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_FLATTEN, flatten));
test_clone();
}
TEST_F(TestDeepCopy, Stress)
{
test_stress();
}
TEST_F(TestDeepCopy, NoSnaps_LargerDstObjSize)
{
SKIP_IF_CRIMSON();
uint64_t order = m_src_ictx->order + 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
test_no_snaps();
}
TEST_F(TestDeepCopy, Snaps_LargerDstObjSize)
{
SKIP_IF_CRIMSON();
uint64_t order = m_src_ictx->order + 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
test_snaps();
}
TEST_F(TestDeepCopy, Clone_LargerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
uint64_t order = m_src_ictx->order + 1 + rand() % 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
test_clone();
}
TEST_F(TestDeepCopy, CloneFlatten_LargerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
uint64_t order = m_src_ictx->order + 1 + rand() % 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
uint64_t flatten = 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_FLATTEN, flatten));
test_clone();
}
TEST_F(TestDeepCopy, Stress_LargerDstObjSize)
{
SKIP_IF_CRIMSON();
uint64_t order = m_src_ictx->order + 1 + rand() % 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
test_stress();
}
TEST_F(TestDeepCopy, NoSnaps_SmallerDstObjSize)
{
uint64_t order = m_src_ictx->order - 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
uint64_t stripe_unit = m_src_ictx->stripe_unit >> 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
test_no_snaps();
}
TEST_F(TestDeepCopy, Snaps_SmallerDstObjSize)
{
uint64_t order = m_src_ictx->order - 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
uint64_t stripe_unit = m_src_ictx->stripe_unit >> 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
test_snaps();
}
TEST_F(TestDeepCopy, Clone_SmallerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
uint64_t order = m_src_ictx->order - 1 - rand() % 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
uint64_t stripe_unit = m_src_ictx->stripe_unit >> 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
test_clone();
}
TEST_F(TestDeepCopy, CloneFlatten_SmallerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
uint64_t order = m_src_ictx->order - 1 - rand() % 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
uint64_t stripe_unit = m_src_ictx->stripe_unit >> 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
uint64_t flatten = 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_FLATTEN, flatten));
test_clone();
}
TEST_F(TestDeepCopy, Stress_SmallerDstObjSize)
{
uint64_t order = m_src_ictx->order - 1 - rand() % 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
uint64_t stripe_unit = m_src_ictx->stripe_unit >> 2;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
test_stress();
}
TEST_F(TestDeepCopy, NoSnaps_StrippingLargerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order + 1;
uint64_t stripe_unit = 1 << (order - 2);
uint64_t stripe_count = 4;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
test_no_snaps();
}
TEST_F(TestDeepCopy, Snaps_StrippingLargerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order + 1;
uint64_t stripe_unit = 1 << (order - 2);
uint64_t stripe_count = 4;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
test_snaps();
}
TEST_F(TestDeepCopy, Clone_StrippingLargerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order + 1;
uint64_t stripe_unit = 1 << (order - 2);
uint64_t stripe_count = 4;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
test_clone();
}
TEST_F(TestDeepCopy, CloneFlatten_StrippingLargerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order + 1;
uint64_t stripe_unit = 1 << (order - 2);
uint64_t stripe_count = 4;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
uint64_t flatten = 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_FLATTEN, flatten));
test_clone();
}
TEST_F(TestDeepCopy, Stress_StrippingLargerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order + 1 + rand() % 2;
uint64_t stripe_unit = 1 << (order - rand() % 4);
uint64_t stripe_count = 2 + rand() % 14;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
test_stress();
}
TEST_F(TestDeepCopy, NoSnaps_StrippingSmallerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order - 1;
uint64_t stripe_unit = 1 << (order - 2);
uint64_t stripe_count = 4;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
test_no_snaps();
}
TEST_F(TestDeepCopy, Snaps_StrippingSmallerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order - 1;
uint64_t stripe_unit = 1 << (order - 2);
uint64_t stripe_count = 4;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
test_snaps();
}
TEST_F(TestDeepCopy, Clone_StrippingSmallerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order - 1 - rand() % 2;
uint64_t stripe_unit = 1 << (order - rand() % 4);
uint64_t stripe_count = 2 + rand() % 14;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
test_clone();
}
TEST_F(TestDeepCopy, CloneFlatten_StrippingSmallerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order - 1 - rand() % 2;
uint64_t stripe_unit = 1 << (order - rand() % 4);
uint64_t stripe_count = 2 + rand() % 14;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
uint64_t flatten = 1;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_FLATTEN, flatten));
test_clone();
}
TEST_F(TestDeepCopy, Stress_StrippingSmallerDstObjSize)
{
REQUIRE_FEATURE(RBD_FEATURE_STRIPINGV2);
uint64_t order = m_src_ictx->order - 1 - rand() % 2;
uint64_t stripe_unit = 1 << (order - rand() % 4);
uint64_t stripe_count = 2 + rand() % 14;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
test_stress();
}
} // namespace librbd
| 25,853 | 32.840314 | 81 |
cc
|
null |
ceph-main/src/test/librbd/test_Groups.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "include/rbd/librbd.h"
#include "include/rbd/librbd.hpp"
#include "test/librados/test.h"
#include "gtest/gtest.h"
#include <boost/scope_exit.hpp>
#include <chrono>
#include <vector>
void register_test_groups() {
}
class TestGroup : public TestFixture {
};
TEST_F(TestGroup, group_create)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, _pool_name.c_str(), &ioctx);
BOOST_SCOPE_EXIT(ioctx) {
rados_ioctx_destroy(ioctx);
} BOOST_SCOPE_EXIT_END;
librbd::RBD rbd;
ASSERT_EQ(0, rbd_group_create(ioctx, "mygroup"));
size_t size = 0;
ASSERT_EQ(-ERANGE, rbd_group_list(ioctx, NULL, &size));
ASSERT_EQ(strlen("mygroup") + 1, size);
char groups[80];
ASSERT_EQ(static_cast<int>(strlen("mygroup") + 1),
rbd_group_list(ioctx, groups, &size));
ASSERT_STREQ("mygroup", groups);
ASSERT_EQ(0, rbd_group_remove(ioctx, "mygroup"));
ASSERT_EQ(0, rbd_group_list(ioctx, groups, &size));
}
TEST_F(TestGroup, group_createPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), ioctx));
librbd::RBD rbd;
ASSERT_EQ(0, rbd.group_create(ioctx, "mygroup"));
std::vector<std::string> groups;
ASSERT_EQ(0, rbd.group_list(ioctx, &groups));
ASSERT_EQ(1U, groups.size());
ASSERT_EQ("mygroup", groups[0]);
groups.clear();
ASSERT_EQ(0, rbd.group_rename(ioctx, "mygroup", "newgroup"));
ASSERT_EQ(0, rbd.group_list(ioctx, &groups));
ASSERT_EQ(1U, groups.size());
ASSERT_EQ("newgroup", groups[0]);
ASSERT_EQ(0, rbd.group_remove(ioctx, "newgroup"));
groups.clear();
ASSERT_EQ(0, rbd.group_list(ioctx, &groups));
ASSERT_EQ(0U, groups.size());
}
TEST_F(TestGroup, add_image)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, _pool_name.c_str(), &ioctx);
BOOST_SCOPE_EXIT(ioctx) {
rados_ioctx_destroy(ioctx);
} BOOST_SCOPE_EXIT_END;
const char *group_name = "mycg";
ASSERT_EQ(0, rbd_group_create(ioctx, group_name));
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, m_image_name.c_str(), &image, NULL));
BOOST_SCOPE_EXIT(image) {
EXPECT_EQ(0, rbd_close(image));
} BOOST_SCOPE_EXIT_END;
uint64_t features;
ASSERT_EQ(0, rbd_get_features(image, &features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) == 0ULL);
uint64_t op_features;
ASSERT_EQ(0, rbd_get_op_features(image, &op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) == 0ULL);
rbd_group_info_t group_info;
ASSERT_EQ(0, rbd_get_group(image, &group_info, sizeof(group_info)));
ASSERT_EQ(0, strcmp("", group_info.name));
ASSERT_EQ(RBD_GROUP_INVALID_POOL, group_info.pool);
rbd_group_info_cleanup(&group_info, sizeof(group_info));
ASSERT_EQ(0, rbd_group_image_add(ioctx, group_name, ioctx,
m_image_name.c_str()));
ASSERT_EQ(-ERANGE, rbd_get_group(image, &group_info, 0));
ASSERT_EQ(0, rbd_get_group(image, &group_info, sizeof(group_info)));
ASSERT_EQ(0, strcmp(group_name, group_info.name));
ASSERT_EQ(rados_ioctx_get_id(ioctx), group_info.pool);
rbd_group_info_cleanup(&group_info, sizeof(group_info));
ASSERT_EQ(0, rbd_get_features(image, &features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) ==
RBD_FEATURE_OPERATIONS);
ASSERT_EQ(0, rbd_get_op_features(image, &op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) ==
RBD_OPERATION_FEATURE_GROUP);
size_t num_images = 0;
ASSERT_EQ(-ERANGE, rbd_group_image_list(ioctx, group_name, NULL,
sizeof(rbd_group_image_info_t),
&num_images));
ASSERT_EQ(1U, num_images);
rbd_group_image_info_t images[1];
ASSERT_EQ(1, rbd_group_image_list(ioctx, group_name, images,
sizeof(rbd_group_image_info_t),
&num_images));
ASSERT_EQ(m_image_name, images[0].name);
ASSERT_EQ(rados_ioctx_get_id(ioctx), images[0].pool);
ASSERT_EQ(0, rbd_group_image_list_cleanup(images,
sizeof(rbd_group_image_info_t),
num_images));
ASSERT_EQ(0, rbd_group_image_remove(ioctx, group_name, ioctx,
m_image_name.c_str()));
ASSERT_EQ(0, rbd_get_features(image, &features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) == 0ULL);
ASSERT_EQ(0, rbd_get_op_features(image, &op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) == 0ULL);
ASSERT_EQ(0, rbd_group_image_list(ioctx, group_name, images,
sizeof(rbd_group_image_info_t),
&num_images));
ASSERT_EQ(0U, num_images);
ASSERT_EQ(0, rbd_group_remove(ioctx, group_name));
}
TEST_F(TestGroup, add_imagePP)
{
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), ioctx));
const char *group_name = "mycg";
librbd::RBD rbd;
ASSERT_EQ(0, rbd.group_create(ioctx, group_name));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, m_image_name.c_str(), NULL));
uint64_t features;
ASSERT_EQ(0, image.features(&features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) == 0ULL);
uint64_t op_features;
ASSERT_EQ(0, image.get_op_features(&op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) == 0ULL);
librbd::group_info_t group_info;
ASSERT_EQ(0, image.get_group(&group_info, sizeof(group_info)));
ASSERT_EQ(std::string(""), group_info.name);
ASSERT_EQ(RBD_GROUP_INVALID_POOL, group_info.pool);
ASSERT_EQ(0, rbd.group_image_add(ioctx, group_name, ioctx,
m_image_name.c_str()));
ASSERT_EQ(-ERANGE, image.get_group(&group_info, 0));
ASSERT_EQ(0, image.get_group(&group_info, sizeof(group_info)));
ASSERT_EQ(std::string(group_name), group_info.name);
ASSERT_EQ(ioctx.get_id(), group_info.pool);
ASSERT_EQ(0, image.features(&features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) ==
RBD_FEATURE_OPERATIONS);
ASSERT_EQ(0, image.get_op_features(&op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) ==
RBD_OPERATION_FEATURE_GROUP);
std::vector<librbd::group_image_info_t> images;
ASSERT_EQ(0, rbd.group_image_list(ioctx, group_name, &images,
sizeof(librbd::group_image_info_t)));
ASSERT_EQ(1U, images.size());
ASSERT_EQ(m_image_name, images[0].name);
ASSERT_EQ(ioctx.get_id(), images[0].pool);
ASSERT_EQ(0, rbd.group_image_remove(ioctx, group_name, ioctx,
m_image_name.c_str()));
ASSERT_EQ(0, image.features(&features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) == 0ULL);
ASSERT_EQ(0, image.get_op_features(&op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) == 0ULL);
images.clear();
ASSERT_EQ(0, rbd.group_image_list(ioctx, group_name, &images,
sizeof(librbd::group_image_info_t)));
ASSERT_EQ(0U, images.size());
ASSERT_EQ(0, rbd.group_remove(ioctx, group_name));
}
TEST_F(TestGroup, add_snapshot)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, _pool_name.c_str(), &ioctx);
BOOST_SCOPE_EXIT(ioctx) {
rados_ioctx_destroy(ioctx);
} BOOST_SCOPE_EXIT_END;
const char *group_name = "snap_group";
const char *snap_name = "snap_snapshot";
const char orig_data[] = "orig data";
const char test_data[] = "test data";
char read_data[10];
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, m_image_name.c_str(), &image, NULL));
BOOST_SCOPE_EXIT(image) {
EXPECT_EQ(0, rbd_close(image));
} BOOST_SCOPE_EXIT_END;
ASSERT_EQ(10, rbd_write(image, 0, 10, orig_data));
ASSERT_EQ(10, rbd_read(image, 0, 10, read_data));
ASSERT_EQ(0, memcmp(orig_data, read_data, 10));
ASSERT_EQ(0, rbd_group_create(ioctx, group_name));
ASSERT_EQ(0, rbd_group_image_add(ioctx, group_name, ioctx,
m_image_name.c_str()));
struct Watcher {
static void quiesce_cb(void *arg) {
Watcher *watcher = static_cast<Watcher *>(arg);
watcher->handle_quiesce();
}
static void unquiesce_cb(void *arg) {
Watcher *watcher = static_cast<Watcher *>(arg);
watcher->handle_unquiesce();
}
rbd_image_t ℑ
uint64_t handle = 0;
size_t quiesce_count = 0;
size_t unquiesce_count = 0;
int r = 0;
ceph::mutex lock = ceph::make_mutex("lock");
ceph::condition_variable cv;
Watcher(rbd_image_t &image) : image(image) {
}
void handle_quiesce() {
ASSERT_EQ(quiesce_count, unquiesce_count);
quiesce_count++;
rbd_quiesce_complete(image, handle, r);
}
void handle_unquiesce() {
std::unique_lock locker(lock);
unquiesce_count++;
cv.notify_one();
}
bool wait_for_unquiesce(size_t c) {
std::unique_lock locker(lock);
return cv.wait_for(locker, std::chrono::seconds(60),
[this, c]() { return unquiesce_count >= c; });
}
} watcher(image);
ASSERT_EQ(0, rbd_quiesce_watch(image, Watcher::quiesce_cb,
Watcher::unquiesce_cb, &watcher,
&watcher.handle));
ASSERT_EQ(0, rbd_group_snap_create(ioctx, group_name, snap_name));
ASSERT_TRUE(watcher.wait_for_unquiesce(1U));
ASSERT_EQ(1U, watcher.quiesce_count);
size_t num_snaps = 0;
ASSERT_EQ(-ERANGE, rbd_group_snap_list(ioctx, group_name, NULL,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_EQ(1U, num_snaps);
rbd_group_snap_info_t snaps[1];
ASSERT_EQ(1, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_STREQ(snap_name, snaps[0].name);
ASSERT_EQ(10, rbd_write(image, 11, 10, test_data));
ASSERT_EQ(10, rbd_read(image, 11, 10, read_data));
ASSERT_EQ(0, memcmp(test_data, read_data, 10));
ASSERT_EQ(0, rbd_group_snap_rollback(ioctx, group_name, snap_name));
ASSERT_EQ(10, rbd_read(image, 0, 10, read_data));
ASSERT_EQ(0, memcmp(orig_data, read_data, 10));
ASSERT_EQ(0, rbd_group_snap_list_cleanup(snaps, sizeof(rbd_group_snap_info_t),
num_snaps));
ASSERT_EQ(0, rbd_group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_EQ(0U, num_snaps);
ASSERT_EQ(-EINVAL, rbd_group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_SKIP_QUIESCE |
RBD_SNAP_CREATE_IGNORE_QUIESCE_ERROR));
watcher.r = -EINVAL;
ASSERT_EQ(-EINVAL, rbd_group_snap_create2(ioctx, group_name, snap_name, 0));
num_snaps = 1;
ASSERT_EQ(0, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
watcher.quiesce_count = 0;
watcher.unquiesce_count = 0;
ASSERT_EQ(0, rbd_group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_SKIP_QUIESCE));
ASSERT_EQ(0U, watcher.quiesce_count);
num_snaps = 1;
ASSERT_EQ(1, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_EQ(0, rbd_group_snap_list_cleanup(snaps, sizeof(rbd_group_snap_info_t),
num_snaps));
ASSERT_EQ(0, rbd_group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd_group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_IGNORE_QUIESCE_ERROR));
ASSERT_EQ(1, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_EQ(0, rbd_group_snap_list_cleanup(snaps, sizeof(rbd_group_snap_info_t),
num_snaps));
ASSERT_EQ(0, rbd_group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd_quiesce_unwatch(image, watcher.handle));
ASSERT_EQ(0, rbd_group_remove(ioctx, group_name));
}
TEST_F(TestGroup, add_snapshotPP)
{
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), ioctx));
const char *group_name = "snap_group";
const char *snap_name = "snap_snapshot";
librbd::RBD rbd;
ASSERT_EQ(0, rbd.group_create(ioctx, group_name));
ASSERT_EQ(0, rbd.group_image_add(ioctx, group_name, ioctx,
m_image_name.c_str()));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, m_image_name.c_str(), NULL));
bufferlist expect_bl;
bufferlist read_bl;
expect_bl.append(std::string(512, '1'));
ASSERT_EQ((ssize_t)expect_bl.length(), image.write(0, expect_bl.length(), expect_bl));
ASSERT_EQ(512, image.read(0, 512, read_bl));
ASSERT_TRUE(expect_bl.contents_equal(read_bl));
ASSERT_EQ(0, rbd.group_snap_create(ioctx, group_name, snap_name));
std::vector<librbd::group_snap_info_t> snaps;
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(1U, snaps.size());
ASSERT_EQ(snap_name, snaps[0].name);
bufferlist write_bl;
write_bl.append(std::string(1024, '2'));
ASSERT_EQ(1024, image.write(513, write_bl.length(), write_bl));
read_bl.clear();
ASSERT_EQ(1024, image.read(513, 1024, read_bl));
ASSERT_TRUE(write_bl.contents_equal(read_bl));
ASSERT_EQ(0, rbd.group_snap_rollback(ioctx, group_name, snap_name));
ASSERT_EQ(512, image.read(0, 512, read_bl));
ASSERT_TRUE(expect_bl.contents_equal(read_bl));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, rbd.group_snap_remove(ioctx, group_name, snap_name));
snaps.clear();
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(0U, snaps.size());
ASSERT_EQ(0, rbd.group_snap_create(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(1U, snaps.size());
ASSERT_EQ(0, rbd.group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(-EINVAL, rbd.group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_SKIP_QUIESCE |
RBD_SNAP_CREATE_IGNORE_QUIESCE_ERROR));
snaps.clear();
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(0U, snaps.size());
ASSERT_EQ(0, rbd.group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_SKIP_QUIESCE));
snaps.clear();
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(1U, snaps.size());
ASSERT_EQ(0, rbd.group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd.group_remove(ioctx, group_name));
}
| 15,721 | 34.251121 | 88 |
cc
|
null |
ceph-main/src/test/librbd/test_ImageWatcher.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "include/int_types.h"
#include "include/stringify.h"
#include "include/rados/librados.h"
#include "include/rbd/librbd.hpp"
#include "common/Cond.h"
#include "common/ceph_mutex.h"
#include "common/errno.h"
#include "cls/lock/cls_lock_client.h"
#include "cls/lock/cls_lock_types.h"
#include "librbd/internal.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageWatcher.h"
#include "librbd/WatchNotifyTypes.h"
#include "librbd/io/AioCompletion.h"
#include "test/librados/test.h"
#include "gtest/gtest.h"
#include <boost/assign/std/set.hpp>
#include <boost/assign/std/map.hpp>
#include <boost/scope_exit.hpp>
#include <boost/thread/thread.hpp>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <vector>
using namespace std::chrono_literals;
using namespace ceph;
using namespace boost::assign;
using namespace librbd::watch_notify;
void register_test_image_watcher() {
}
class TestImageWatcher : public TestFixture {
public:
TestImageWatcher() : m_watch_ctx(NULL)
{
}
class WatchCtx : public librados::WatchCtx2 {
public:
explicit WatchCtx(TestImageWatcher &parent) : m_parent(parent), m_handle(0) {}
int watch(const librbd::ImageCtx &ictx) {
m_header_oid = ictx.header_oid;
return m_parent.m_ioctx.watch2(m_header_oid, &m_handle, this);
}
int unwatch() {
return m_parent.m_ioctx.unwatch2(m_handle);
}
void handle_notify(uint64_t notify_id,
uint64_t cookie,
uint64_t notifier_id,
bufferlist& bl) override {
try {
int op;
bufferlist payload;
auto iter = bl.cbegin();
DECODE_START(1, iter);
decode(op, iter);
iter.copy_all(payload);
DECODE_FINISH(iter);
NotifyOp notify_op = static_cast<NotifyOp>(op);
/*
std::cout << "NOTIFY: " << notify_op << ", " << notify_id
<< ", " << cookie << ", " << notifier_id << std::endl;
*/
std::lock_guard l{m_parent.m_callback_lock};
m_parent.m_notify_payloads[notify_op] = payload;
bufferlist reply;
if (m_parent.m_notify_acks.count(notify_op) > 0) {
reply = m_parent.m_notify_acks[notify_op];
m_parent.m_notifies += notify_op;
m_parent.m_callback_cond.notify_all();
}
m_parent.m_ioctx.notify_ack(m_header_oid, notify_id, cookie, reply);
} catch (...) {
FAIL();
}
}
void handle_error(uint64_t cookie, int err) override {
std::cerr << "ERROR: " << cookie << ", " << cpp_strerror(err)
<< std::endl;
}
uint64_t get_handle() const {
return m_handle;
}
private:
TestImageWatcher &m_parent;
std::string m_header_oid;
uint64_t m_handle;
};
void TearDown() override {
deregister_image_watch();
TestFixture::TearDown();
}
int deregister_image_watch() {
if (m_watch_ctx != NULL) {
int r = m_watch_ctx->unwatch();
librados::Rados rados(m_ioctx);
rados.watch_flush();
delete m_watch_ctx;
m_watch_ctx = NULL;
return r;
}
return 0;
}
int register_image_watch(librbd::ImageCtx &ictx) {
m_watch_ctx = new WatchCtx(*this);
return m_watch_ctx->watch(ictx);
}
bool wait_for_notifies(librbd::ImageCtx &ictx) {
std::unique_lock l{m_callback_lock};
while (m_notifies.size() < m_notify_acks.size()) {
if (m_callback_cond.wait_for(l, 10s) == std::cv_status::timeout) {
break;
}
}
return (m_notifies.size() == m_notify_acks.size());
}
bufferlist create_response_message(int r) {
bufferlist bl;
encode(ResponseMessage(r), bl);
return bl;
}
bool extract_async_request_id(NotifyOp op, AsyncRequestId *id) {
if (m_notify_payloads.count(op) == 0) {
return false;
}
bufferlist payload = m_notify_payloads[op];
auto iter = payload.cbegin();
switch (op) {
case NOTIFY_OP_FLATTEN:
{
FlattenPayload payload;
payload.decode(2, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_RESIZE:
{
ResizePayload payload;
payload.decode(2, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_SNAP_CREATE:
{
SnapCreatePayload payload;
payload.decode(7, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_SNAP_RENAME:
{
SnapRenamePayload payload;
payload.decode(7, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_SNAP_REMOVE:
{
SnapRemovePayload payload;
payload.decode(7, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_SNAP_PROTECT:
{
SnapProtectPayload payload;
payload.decode(7, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_SNAP_UNPROTECT:
{
SnapUnprotectPayload payload;
payload.decode(7, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_RENAME:
{
RenamePayload payload;
payload.decode(7, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_REBUILD_OBJECT_MAP:
{
RebuildObjectMapPayload payload;
payload.decode(2, iter);
*id = payload.async_request_id;
}
return true;
case NOTIFY_OP_UPDATE_FEATURES:
{
UpdateFeaturesPayload payload;
payload.decode(7, iter);
*id = payload.async_request_id;
}
return true;
default:
break;
}
return false;
}
int notify_async_progress(librbd::ImageCtx *ictx, const AsyncRequestId &id,
uint64_t offset, uint64_t total) {
bufferlist bl;
encode(NotifyMessage(new AsyncProgressPayload(id, offset, total)), bl);
return m_ioctx.notify2(ictx->header_oid, bl, 5000, NULL);
}
int notify_async_complete(librbd::ImageCtx *ictx, const AsyncRequestId &id,
int r) {
bufferlist bl;
encode(NotifyMessage(new AsyncCompletePayload(id, r)), bl);
return m_ioctx.notify2(ictx->header_oid, bl, 5000, NULL);
}
typedef std::map<NotifyOp, bufferlist> NotifyOpPayloads;
typedef std::set<NotifyOp> NotifyOps;
WatchCtx *m_watch_ctx;
NotifyOps m_notifies;
NotifyOpPayloads m_notify_payloads;
NotifyOpPayloads m_notify_acks;
AsyncRequestId m_async_request_id;
ceph::mutex m_callback_lock = ceph::make_mutex("m_callback_lock");
ceph::condition_variable m_callback_cond;
};
struct ProgressContext : public librbd::ProgressContext {
ceph::mutex mutex = ceph::make_mutex("ProgressContext::mutex");
ceph::condition_variable cond;
bool received;
uint64_t offset;
uint64_t total;
ProgressContext() : received(false),
offset(0), total(0) {}
int update_progress(uint64_t offset_, uint64_t total_) override {
std::lock_guard l{mutex};
offset = offset_;
total = total_;
received = true;
cond.notify_all();
return 0;
}
bool wait(librbd::ImageCtx *ictx, uint64_t offset_, uint64_t total_) {
std::unique_lock l{mutex};
while (!received) {
if (cond.wait_for(l, 10s) == std::cv_status::timeout) {
break;
}
}
return (received && offset == offset_ && total == total_);
}
};
struct FlattenTask {
librbd::ImageCtx *ictx;
ProgressContext *progress_context;
int result;
FlattenTask(librbd::ImageCtx *ictx_, ProgressContext *ctx)
: ictx(ictx_), progress_context(ctx), result(0) {}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_flatten(0, *progress_context, &ctx);
result = ctx.wait();
}
};
struct ResizeTask {
librbd::ImageCtx *ictx;
ProgressContext *progress_context;
int result;
ResizeTask(librbd::ImageCtx *ictx_, ProgressContext *ctx)
: ictx(ictx_), progress_context(ctx), result(0) {}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_resize(0, 0, true, *progress_context, &ctx);
result = ctx.wait();
}
};
struct SnapCreateTask {
librbd::ImageCtx *ictx;
ProgressContext *progress_context;
int result;
SnapCreateTask(librbd::ImageCtx *ictx_, ProgressContext *ctx)
: ictx(ictx_), progress_context(ctx), result(0) {}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_snap_create(0, cls::rbd::UserSnapshotNamespace(),
"snap", 0, *progress_context, &ctx);
result = ctx.wait();
}
};
struct SnapRenameTask {
librbd::ImageCtx *ictx;
int result = 0;
SnapRenameTask(librbd::ImageCtx *ictx)
: ictx(ictx) {
}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_snap_rename(0, 1, "snap-rename", &ctx);
result = ctx.wait();
}
};
struct SnapRemoveTask {
librbd::ImageCtx *ictx;
int result = 0;
SnapRemoveTask(librbd::ImageCtx *ictx)
: ictx(ictx) {
}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_snap_remove(
0, cls::rbd::UserSnapshotNamespace(), "snap", &ctx);
result = ctx.wait();
}
};
struct SnapProtectTask {
librbd::ImageCtx *ictx;
int result = 0;
SnapProtectTask(librbd::ImageCtx *ictx)
: ictx(ictx) {
}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_snap_protect(
0, cls::rbd::UserSnapshotNamespace(), "snap", &ctx);
result = ctx.wait();
}
};
struct SnapUnprotectTask {
librbd::ImageCtx *ictx;
int result = 0;
SnapUnprotectTask(librbd::ImageCtx *ictx)
: ictx(ictx) {
}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_snap_unprotect(
0, cls::rbd::UserSnapshotNamespace(), "snap", &ctx);
result = ctx.wait();
}
};
struct RenameTask {
librbd::ImageCtx *ictx;
int result = 0;
RenameTask(librbd::ImageCtx *ictx)
: ictx(ictx) {
}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_rename(0, "new_name", &ctx);
result = ctx.wait();
}
};
struct RebuildObjectMapTask {
librbd::ImageCtx *ictx;
ProgressContext *progress_context;
int result;
RebuildObjectMapTask(librbd::ImageCtx *ictx_, ProgressContext *ctx)
: ictx(ictx_), progress_context(ctx), result(0) {}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
ictx->image_watcher->notify_rebuild_object_map(0, *progress_context, &ctx);
result = ctx.wait();
}
};
struct UpdateFeaturesTask {
librbd::ImageCtx *ictx;
int result;
UpdateFeaturesTask(librbd::ImageCtx *ictx)
: ictx(ictx), result(0) {}
void operator()() {
std::shared_lock l{ictx->owner_lock};
C_SaferCond ctx;
uint64_t features = 24;
bool enabled = 0;
ictx->image_watcher->notify_update_features(0, features, enabled, &ctx);
result = ctx.wait();
}
};
TEST_F(TestImageWatcher, NotifyHeaderUpdate) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
m_notify_acks = {{NOTIFY_OP_HEADER_UPDATE, {}}};
ictx->notify_update();
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_HEADER_UPDATE;
ASSERT_EQ(expected_notify_ops, m_notifies);
}
TEST_F(TestImageWatcher, NotifyFlatten) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_FLATTEN, create_response_message(0)}};
ProgressContext progress_context;
FlattenTask flatten_task(ictx, &progress_context);
boost::thread thread(boost::ref(flatten_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_FLATTEN;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_FLATTEN, &async_request_id));
ASSERT_EQ(0, notify_async_progress(ictx, async_request_id, 10, 20));
ASSERT_TRUE(progress_context.wait(ictx, 10, 20));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, flatten_task.result);
}
TEST_F(TestImageWatcher, NotifyResize) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_RESIZE, create_response_message(0)}};
ProgressContext progress_context;
ResizeTask resize_task(ictx, &progress_context);
boost::thread thread(boost::ref(resize_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_RESIZE;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_RESIZE, &async_request_id));
ASSERT_EQ(0, notify_async_progress(ictx, async_request_id, 10, 20));
ASSERT_TRUE(progress_context.wait(ictx, 10, 20));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, resize_task.result);
}
TEST_F(TestImageWatcher, NotifyRebuildObjectMap) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_REBUILD_OBJECT_MAP, create_response_message(0)}};
ProgressContext progress_context;
RebuildObjectMapTask rebuild_task(ictx, &progress_context);
boost::thread thread(boost::ref(rebuild_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_REBUILD_OBJECT_MAP;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_REBUILD_OBJECT_MAP,
&async_request_id));
ASSERT_EQ(0, notify_async_progress(ictx, async_request_id, 10, 20));
ASSERT_TRUE(progress_context.wait(ictx, 10, 20));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, rebuild_task.result);
}
TEST_F(TestImageWatcher, NotifyUpdateFeatures) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_UPDATE_FEATURES, create_response_message(0)}};
UpdateFeaturesTask update_features_task(ictx);
boost::thread thread(boost::ref(update_features_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_UPDATE_FEATURES;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_UPDATE_FEATURES,
&async_request_id));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, update_features_task.result);
}
TEST_F(TestImageWatcher, NotifySnapCreate) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_SNAP_CREATE, create_response_message(0)}};
ProgressContext progress_context;
SnapCreateTask snap_create_task(ictx, &progress_context);
boost::thread thread(boost::ref(snap_create_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_SNAP_CREATE;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_SNAP_CREATE,
&async_request_id));
ASSERT_EQ(0, notify_async_progress(ictx, async_request_id, 1, 10));
ASSERT_TRUE(progress_context.wait(ictx, 1, 10));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, snap_create_task.result);
}
TEST_F(TestImageWatcher, NotifySnapCreateError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_SNAP_CREATE, create_response_message(-EEXIST)}};
std::shared_lock l{ictx->owner_lock};
C_SaferCond notify_ctx;
librbd::NoOpProgressContext prog_ctx;
ictx->image_watcher->notify_snap_create(0, cls::rbd::UserSnapshotNamespace(),
"snap", 0, prog_ctx, ¬ify_ctx);
ASSERT_EQ(-EEXIST, notify_ctx.wait());
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_SNAP_CREATE;
ASSERT_EQ(expected_notify_ops, m_notifies);
}
TEST_F(TestImageWatcher, NotifySnapRename) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_SNAP_RENAME, create_response_message(0)}};
SnapRenameTask snap_rename_task(ictx);
boost::thread thread(boost::ref(snap_rename_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_SNAP_RENAME;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_SNAP_RENAME,
&async_request_id));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, snap_rename_task.result);
}
TEST_F(TestImageWatcher, NotifySnapRenameError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_SNAP_RENAME, create_response_message(-EEXIST)}};
std::shared_lock l{ictx->owner_lock};
C_SaferCond notify_ctx;
ictx->image_watcher->notify_snap_rename(0, 1, "snap-rename", ¬ify_ctx);
ASSERT_EQ(-EEXIST, notify_ctx.wait());
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_SNAP_RENAME;
ASSERT_EQ(expected_notify_ops, m_notifies);
}
TEST_F(TestImageWatcher, NotifySnapRemove) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_SNAP_REMOVE, create_response_message(0)}};
SnapRemoveTask snap_remove_task(ictx);
boost::thread thread(boost::ref(snap_remove_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_SNAP_REMOVE;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_SNAP_REMOVE,
&async_request_id));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, snap_remove_task.result);
}
TEST_F(TestImageWatcher, NotifySnapProtect) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_SNAP_PROTECT, create_response_message(0)}};
SnapProtectTask snap_protect_task(ictx);
boost::thread thread(boost::ref(snap_protect_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_SNAP_PROTECT;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_SNAP_PROTECT,
&async_request_id));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, snap_protect_task.result);
}
TEST_F(TestImageWatcher, NotifySnapUnprotect) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_SNAP_UNPROTECT, create_response_message(0)}};
SnapUnprotectTask snap_unprotect_task(ictx);
boost::thread thread(boost::ref(snap_unprotect_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_SNAP_UNPROTECT;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_SNAP_UNPROTECT,
&async_request_id));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, snap_unprotect_task.result);
}
TEST_F(TestImageWatcher, NotifyRename) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_RENAME, create_response_message(0)}};
RenameTask rename_task(ictx);
boost::thread thread(boost::ref(rename_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_RENAME;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_RENAME,
&async_request_id));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, 0));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(0, rename_task.result);
}
TEST_F(TestImageWatcher, NotifyAsyncTimedOut) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_FLATTEN, {}}};
ProgressContext progress_context;
FlattenTask flatten_task(ictx, &progress_context);
boost::thread thread(boost::ref(flatten_task));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(-ETIMEDOUT, flatten_task.result);
}
TEST_F(TestImageWatcher, NotifyAsyncError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_FLATTEN, create_response_message(-EIO)}};
ProgressContext progress_context;
FlattenTask flatten_task(ictx, &progress_context);
boost::thread thread(boost::ref(flatten_task));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(-EIO, flatten_task.result);
}
TEST_F(TestImageWatcher, NotifyAsyncCompleteError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_FLATTEN, create_response_message(0)}};
ProgressContext progress_context;
FlattenTask flatten_task(ictx, &progress_context);
boost::thread thread(boost::ref(flatten_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
NotifyOps expected_notify_ops;
expected_notify_ops += NOTIFY_OP_FLATTEN;
ASSERT_EQ(expected_notify_ops, m_notifies);
AsyncRequestId async_request_id;
ASSERT_TRUE(extract_async_request_id(NOTIFY_OP_FLATTEN, &async_request_id));
ASSERT_EQ(0, notify_async_complete(ictx, async_request_id, -ESHUTDOWN));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(-ESHUTDOWN, flatten_task.result);
}
TEST_F(TestImageWatcher, NotifyAsyncRequestTimedOut) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->config.set_val("rbd_request_timed_out_seconds", "0");
ASSERT_EQ(0, register_image_watch(*ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE,
"auto " + stringify(m_watch_ctx->get_handle())));
m_notify_acks = {{NOTIFY_OP_FLATTEN, create_response_message(0)}};
ProgressContext progress_context;
FlattenTask flatten_task(ictx, &progress_context);
boost::thread thread(boost::ref(flatten_task));
ASSERT_TRUE(wait_for_notifies(*ictx));
ASSERT_TRUE(thread.timed_join(boost::posix_time::seconds(10)));
ASSERT_EQ(-ETIMEDOUT, flatten_task.result);
}
| 27,184 | 27.889479 | 82 |
cc
|
null |
ceph-main/src/test/librbd/test_Migration.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librados/test.h"
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "librbd/api/Group.h"
#include "librbd/api/Image.h"
#include "librbd/api/Io.h"
#include "librbd/api/Migration.h"
#include "librbd/api/Mirror.h"
#include "librbd/api/Namespace.h"
#include "librbd/api/Snapshot.h"
#include "librbd/image/AttachChildRequest.h"
#include "librbd/image/AttachParentRequest.h"
#include "librbd/internal.h"
#include "librbd/io/ReadResult.h"
#include "common/Cond.h"
#include <boost/scope_exit.hpp>
void register_test_migration() {
}
namespace librbd {
struct TestMigration : public TestFixture {
static void SetUpTestCase() {
TestFixture::SetUpTestCase();
_other_pool_name = get_temp_pool_name("test-librbd-");
ASSERT_EQ(0, _rados.pool_create(_other_pool_name.c_str()));
}
static void TearDownTestCase() {
ASSERT_EQ(0, _rados.pool_delete(_other_pool_name.c_str()));
TestFixture::TearDownTestCase();
}
void SetUp() override {
TestFixture::SetUp();
ASSERT_EQ(0, _rados.ioctx_create(_other_pool_name.c_str(),
_other_pool_ioctx));
open_image(m_ioctx, m_image_name, &m_ictx);
m_image_id = m_ictx->id;
std::string ref_image_name = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(m_rbd, m_ioctx, ref_image_name, m_ictx->size));
EXPECT_EQ(0, _rados.ioctx_create2(m_ioctx.get_id(), m_ref_ioctx));
open_image(m_ref_ioctx, ref_image_name, &m_ref_ictx);
resize(20 * (1 << 22));
}
void TearDown() override {
if (m_ref_ictx != nullptr) {
close_image(m_ref_ictx);
}
if (m_ictx != nullptr) {
close_image(m_ictx);
}
_other_pool_ioctx.close();
TestFixture::TearDown();
}
void compare(const std::string &description = "") {
std::vector<librbd::snap_info_t> src_snaps, dst_snaps;
EXPECT_EQ(m_ref_ictx->size, m_ictx->size);
EXPECT_EQ(0, librbd::api::Snapshot<>::list(m_ref_ictx, src_snaps));
EXPECT_EQ(0, librbd::api::Snapshot<>::list(m_ictx, dst_snaps));
EXPECT_EQ(src_snaps.size(), dst_snaps.size());
for (size_t i = 0; i <= src_snaps.size(); i++) {
const char *src_snap_name = nullptr;
const char *dst_snap_name = nullptr;
if (i < src_snaps.size()) {
EXPECT_EQ(src_snaps[i].name, dst_snaps[i].name);
src_snap_name = src_snaps[i].name.c_str();
dst_snap_name = dst_snaps[i].name.c_str();
}
EXPECT_EQ(0, librbd::api::Image<>::snap_set(
m_ref_ictx, cls::rbd::UserSnapshotNamespace(),
src_snap_name));
EXPECT_EQ(0, librbd::api::Image<>::snap_set(
m_ictx, cls::rbd::UserSnapshotNamespace(),
dst_snap_name));
compare_snaps(
description + " snap: " + (src_snap_name ? src_snap_name : "null"),
m_ref_ictx, m_ictx);
}
}
void compare_snaps(const std::string &description, librbd::ImageCtx *src_ictx,
librbd::ImageCtx *dst_ictx) {
uint64_t src_size, dst_size;
{
std::shared_lock src_locker{src_ictx->image_lock};
std::shared_lock dst_locker{dst_ictx->image_lock};
src_size = src_ictx->get_image_size(src_ictx->snap_id);
dst_size = dst_ictx->get_image_size(dst_ictx->snap_id);
}
if (src_size != dst_size) {
std::cout << description << ": size differs" << std::endl;
EXPECT_EQ(src_size, dst_size);
}
if (dst_ictx->test_features(RBD_FEATURE_LAYERING)) {
bool flags_set;
std::shared_lock dst_locker{dst_ictx->image_lock};
EXPECT_EQ(0, dst_ictx->test_flags(dst_ictx->snap_id,
RBD_FLAG_OBJECT_MAP_INVALID,
dst_ictx->image_lock, &flags_set));
EXPECT_FALSE(flags_set);
}
ssize_t read_size = 1 << src_ictx->order;
uint64_t offset = 0;
while (offset < src_size) {
read_size = std::min(read_size, static_cast<ssize_t>(src_size - offset));
bufferptr src_ptr(read_size);
bufferlist src_bl;
src_bl.push_back(src_ptr);
librbd::io::ReadResult src_result{&src_bl};
EXPECT_EQ(read_size, api::Io<>::read(
*src_ictx, offset, read_size,
librbd::io::ReadResult{src_result}, 0));
bufferptr dst_ptr(read_size);
bufferlist dst_bl;
dst_bl.push_back(dst_ptr);
librbd::io::ReadResult dst_result{&dst_bl};
EXPECT_EQ(read_size, api::Io<>::read(
*dst_ictx, offset, read_size,
librbd::io::ReadResult{dst_result}, 0));
if (!src_bl.contents_equal(dst_bl)) {
std::cout << description
<< ", block " << offset << "~" << read_size << " differs"
<< std::endl;
std::cout << "src block: " << src_ictx->id << ": " << std::endl; src_bl.hexdump(std::cout);
std::cout << "dst block: " << dst_ictx->id << ": " << std::endl; dst_bl.hexdump(std::cout);
}
EXPECT_TRUE(src_bl.contents_equal(dst_bl));
offset += read_size;
}
}
void open_image(librados::IoCtx& io_ctx, const std::string &name,
const std::string &id, bool read_only, int flags,
librbd::ImageCtx **ictx) {
*ictx = new librbd::ImageCtx(name, id, nullptr, io_ctx, read_only);
m_ictxs.insert(*ictx);
ASSERT_EQ(0, (*ictx)->state->open(flags));
(*ictx)->discard_granularity_bytes = 0;
}
void open_image(librados::IoCtx& io_ctx, const std::string &name,
librbd::ImageCtx **ictx) {
open_image(io_ctx, name, "", false, 0, ictx);
}
void migration_prepare(librados::IoCtx& dst_io_ctx,
const std::string &dst_name, int r = 0) {
std::cout << __func__ << std::endl;
close_image(m_ictx);
m_ictx = nullptr;
EXPECT_EQ(r, librbd::api::Migration<>::prepare(m_ioctx, m_image_name,
dst_io_ctx, dst_name,
m_opts));
if (r == 0) {
open_image(dst_io_ctx, dst_name, &m_ictx);
} else {
open_image(m_ioctx, m_image_name, &m_ictx);
}
compare("after prepare");
}
void migration_execute(librados::IoCtx& io_ctx, const std::string &name,
int r = 0) {
std::cout << __func__ << std::endl;
librbd::NoOpProgressContext no_op;
EXPECT_EQ(r, librbd::api::Migration<>::execute(io_ctx, name, no_op));
}
void migration_abort(librados::IoCtx& io_ctx, const std::string &name,
int r = 0) {
std::cout << __func__ << std::endl;
std::string dst_name = m_ictx->name;
close_image(m_ictx);
m_ictx = nullptr;
librbd::NoOpProgressContext no_op;
EXPECT_EQ(r, librbd::api::Migration<>::abort(io_ctx, name, no_op));
if (r == 0) {
open_image(m_ioctx, m_image_name, &m_ictx);
} else {
open_image(m_ioctx, dst_name, &m_ictx);
}
compare("after abort");
}
void migration_commit(librados::IoCtx& io_ctx, const std::string &name) {
std::cout << __func__ << std::endl;
librbd::NoOpProgressContext no_op;
EXPECT_EQ(0, librbd::api::Migration<>::commit(io_ctx, name, no_op));
compare("after commit");
}
void migration_status(librbd::image_migration_state_t state) {
librbd::image_migration_status_t status;
EXPECT_EQ(0, librbd::api::Migration<>::status(m_ioctx, m_image_name,
&status));
EXPECT_EQ(status.source_pool_id, m_ioctx.get_id());
EXPECT_EQ(status.source_pool_namespace, m_ioctx.get_namespace());
EXPECT_EQ(status.source_image_name, m_image_name);
EXPECT_EQ(status.source_image_id, m_image_id);
EXPECT_EQ(status.dest_pool_id, m_ictx->md_ctx.get_id());
EXPECT_EQ(status.dest_pool_namespace, m_ictx->md_ctx.get_namespace());
EXPECT_EQ(status.dest_image_name, m_ictx->name);
EXPECT_EQ(status.dest_image_id, m_ictx->id);
EXPECT_EQ(status.state, state);
}
void migrate(librados::IoCtx& dst_io_ctx, const std::string &dst_name) {
migration_prepare(dst_io_ctx, dst_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
migration_execute(dst_io_ctx, dst_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(dst_io_ctx, dst_name);
}
void write(uint64_t off, uint64_t len, char c) {
std::cout << "write: " << c << " " << off << "~" << len << std::endl;
bufferlist ref_bl;
ref_bl.append(std::string(len, c));
ASSERT_EQ(static_cast<ssize_t>(len),
api::Io<>::write(*m_ref_ictx, off, len, std::move(ref_bl), 0));
bufferlist bl;
bl.append(std::string(len, c));
ASSERT_EQ(static_cast<ssize_t>(len),
api::Io<>::write(*m_ictx, off, len, std::move(bl), 0));
}
void discard(uint64_t off, uint64_t len) {
std::cout << "discard: " << off << "~" << len << std::endl;
ASSERT_EQ(static_cast<ssize_t>(len),
api::Io<>::discard(*m_ref_ictx, off, len, false));
ASSERT_EQ(static_cast<ssize_t>(len),
api::Io<>::discard(*m_ictx, off, len, false));
}
void flush() {
ASSERT_EQ(0, TestFixture::flush_writeback_cache(m_ref_ictx));
ASSERT_EQ(0, TestFixture::flush_writeback_cache(m_ictx));
}
void snap_create(const std::string &snap_name) {
std::cout << "snap_create: " << snap_name << std::endl;
flush();
ASSERT_EQ(0, TestFixture::snap_create(*m_ref_ictx, snap_name));
ASSERT_EQ(0, TestFixture::snap_create(*m_ictx, snap_name));
}
void snap_protect(const std::string &snap_name) {
std::cout << "snap_protect: " << snap_name << std::endl;
ASSERT_EQ(0, TestFixture::snap_protect(*m_ref_ictx, snap_name));
ASSERT_EQ(0, TestFixture::snap_protect(*m_ictx, snap_name));
}
void clone(const std::string &snap_name) {
snap_protect(snap_name);
int order = m_ref_ictx->order;
uint64_t features;
ASSERT_EQ(0, librbd::get_features(m_ref_ictx, &features));
std::string ref_clone_name = get_temp_image_name();
std::string clone_name = get_temp_image_name();
std::cout << "clone " << m_ictx->name << " -> " << clone_name
<< std::endl;
ASSERT_EQ(0, librbd::clone(m_ref_ictx->md_ctx, m_ref_ictx->name.c_str(),
snap_name.c_str(), m_ref_ioctx,
ref_clone_name.c_str(), features, &order,
m_ref_ictx->stripe_unit,
m_ref_ictx->stripe_count));
ASSERT_EQ(0, librbd::clone(m_ictx->md_ctx, m_ictx->name.c_str(),
snap_name.c_str(), m_ioctx,
clone_name.c_str(), features, &order,
m_ictx->stripe_unit,
m_ictx->stripe_count));
close_image(m_ref_ictx);
open_image(m_ref_ioctx, ref_clone_name, &m_ref_ictx);
close_image(m_ictx);
open_image(m_ioctx, clone_name, &m_ictx);
m_image_name = m_ictx->name;
m_image_id = m_ictx->id;
}
void resize(uint64_t size) {
std::cout << "resize: " << size << std::endl;
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, m_ref_ictx->operations->resize(size, true, no_op));
ASSERT_EQ(0, m_ictx->operations->resize(size, true, no_op));
}
void test_no_snaps() {
uint64_t len = (1 << m_ictx->order) * 2 + 1;
write(0 * len, len, '1');
write(2 * len, len, '1');
flush();
}
void test_snaps() {
uint64_t len = (1 << m_ictx->order) * 2 + 1;
write(0 * len, len, '1');
snap_create("snap1");
write(1 * len, len, '1');
write(0 * len, 1000, 'X');
discard(1000 + 10, 1000);
snap_create("snap2");
write(1 * len, 1000, 'X');
discard(2 * len + 10, 1000);
uint64_t size = m_ictx->size;
resize(size << 1);
write(size - 1, len, '2');
snap_create("snap3");
resize(size);
discard(size - 1, 1);
flush();
}
void test_clone() {
uint64_t len = (1 << m_ictx->order) * 2 + 1;
write(0 * len, len, 'X');
write(2 * len, len, 'X');
snap_create("snap");
clone("snap");
write(0, 1000, 'X');
discard(1010, 1000);
snap_create("snap");
clone("snap");
write(1000, 1000, 'X');
discard(2010, 1000);
flush();
}
template <typename L>
void test_migrate_parent(uint32_t clone_format, L&& test) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
std::string prev_clone_format;
ASSERT_EQ(0, _rados.conf_get("rbd_default_clone_format",
prev_clone_format));
ASSERT_EQ(0, _rados.conf_set("rbd_default_clone_format",
stringify(clone_format).c_str()));
BOOST_SCOPE_EXIT_TPL(&prev_clone_format) {
_rados.conf_set("rbd_default_clone_format", prev_clone_format.c_str());
} BOOST_SCOPE_EXIT_END;
write(0, 10, 'A');
snap_create("snap1");
snap_protect("snap1");
int order = m_ictx->order;
uint64_t features;
ASSERT_EQ(0, librbd::get_features(m_ictx, &features));
std::string clone_name = get_temp_image_name();
ASSERT_EQ(0, librbd::clone(m_ictx->md_ctx, m_ictx->name.c_str(), "snap1",
m_ioctx, clone_name.c_str(), features, &order,
m_ictx->stripe_unit, m_ictx->stripe_count));
librbd::ImageCtx *child_ictx;
open_image(m_ioctx, clone_name, &child_ictx);
test(child_ictx);
ASSERT_EQ(0, child_ictx->state->refresh());
bufferlist bl;
bufferptr ptr(10);
bl.push_back(ptr);
librbd::io::ReadResult result{&bl};
ASSERT_EQ(10, api::Io<>::read(
*child_ictx, 0, 10, librbd::io::ReadResult{result}, 0));
bufferlist ref_bl;
ref_bl.append(std::string(10, 'A'));
ASSERT_TRUE(ref_bl.contents_equal(bl));
close_image(child_ictx);
}
void test_stress(const std::string &snap_name_prefix = "snap",
char start_char = 'A') {
uint64_t initial_size = m_ictx->size;
int nsnaps = 4;
const char *c = getenv("TEST_RBD_MIGRATION_STRESS_NSNAPS");
if (c != NULL) {
std::stringstream ss(c);
ASSERT_TRUE(ss >> nsnaps);
}
int nwrites = 4;
c = getenv("TEST_RBD_MIGRATION_STRESS_NWRITES");
if (c != NULL) {
std::stringstream ss(c);
ASSERT_TRUE(ss >> nwrites);
}
for (int i = 0; i < nsnaps; i++) {
for (int j = 0; j < nwrites; j++) {
size_t len = rand() % ((1 << m_ictx->order) * 2);
ASSERT_GT(m_ictx->size, len);
uint64_t off = std::min(static_cast<uint64_t>(rand() % m_ictx->size),
static_cast<uint64_t>(m_ictx->size - len));
write(off, len, start_char + i);
len = rand() % ((1 << m_ictx->order) * 2);
ASSERT_GT(m_ictx->size, len);
off = std::min(static_cast<uint64_t>(rand() % m_ictx->size),
static_cast<uint64_t>(m_ictx->size - len));
discard(off, len);
}
std::string snap_name = snap_name_prefix + stringify(i);
snap_create(snap_name);
if (m_ictx->test_features(RBD_FEATURE_LAYERING) &&
!m_ictx->test_features(RBD_FEATURE_MIGRATING) &&
rand() % 4) {
clone(snap_name);
}
if (rand() % 2) {
librbd::NoOpProgressContext no_op;
uint64_t new_size = initial_size + rand() % m_ictx->size;
resize(new_size);
ASSERT_EQ(new_size, m_ictx->size);
}
}
flush();
}
void test_stress2(bool concurrent) {
test_stress();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
std::thread user([this]() {
test_stress("user", 'a');
for (int i = 0; i < 5; i++) {
uint64_t off = (i + 1) * m_ictx->size / 10;
uint64_t len = m_ictx->size / 40;
write(off, len, '1' + i);
off += len / 4;
len /= 2;
discard(off, len);
}
flush();
});
if (concurrent) {
librados::IoCtx io_ctx;
EXPECT_EQ(0, _rados.ioctx_create2(m_ioctx.get_id(), io_ctx));
migration_execute(io_ctx, m_image_name);
io_ctx.close();
user.join();
} else {
user.join();
compare("before execute");
migration_execute(m_ioctx, m_image_name);
}
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
static std::string _other_pool_name;
static librados::IoCtx _other_pool_ioctx;
std::string m_image_id;
librbd::ImageCtx *m_ictx = nullptr;
librados::IoCtx m_ref_ioctx;
librbd::ImageCtx *m_ref_ictx = nullptr;
librbd::ImageOptions m_opts;
};
std::string TestMigration::_other_pool_name;
librados::IoCtx TestMigration::_other_pool_ioctx;
TEST_F(TestMigration, Empty)
{
uint64_t features = m_ictx->features ^ RBD_FEATURE_LAYERING;
features &= ~RBD_FEATURE_DIRTY_CACHE;
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_FEATURES, features));
migrate(m_ioctx, m_image_name);
ASSERT_EQ(features, m_ictx->features);
}
TEST_F(TestMigration, OtherName)
{
std::string name = get_temp_image_name();
migrate(m_ioctx, name);
ASSERT_EQ(name, m_ictx->name);
}
TEST_F(TestMigration, OtherPool)
{
migrate(_other_pool_ioctx, m_image_name);
ASSERT_EQ(_other_pool_ioctx.get_id(), m_ictx->md_ctx.get_id());
}
TEST_F(TestMigration, OtherNamespace)
{
ASSERT_EQ(0, librbd::api::Namespace<>::create(_other_pool_ioctx, "ns1"));
_other_pool_ioctx.set_namespace("ns1");
migrate(_other_pool_ioctx, m_image_name);
ASSERT_EQ(_other_pool_ioctx.get_id(), m_ictx->md_ctx.get_id());
ASSERT_EQ(_other_pool_ioctx.get_namespace(), m_ictx->md_ctx.get_namespace());
_other_pool_ioctx.set_namespace("");
}
TEST_F(TestMigration, DataPool)
{
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_DATA_POOL,
_other_pool_ioctx.get_pool_name().c_str()));
migrate(m_ioctx, m_image_name);
ASSERT_EQ(_other_pool_ioctx.get_id(), m_ictx->data_ctx.get_id());
}
TEST_F(TestMigration, AbortAfterPrepare)
{
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
migration_abort(m_ioctx, m_image_name);
}
TEST_F(TestMigration, AbortAfterFailedPrepare)
{
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_DATA_POOL, "INVALID_POOL"));
migration_prepare(m_ioctx, m_image_name, -ENOENT);
// Migration is automatically aborted if prepare failed
}
TEST_F(TestMigration, AbortAfterExecute)
{
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_abort(m_ioctx, m_image_name);
}
TEST_F(TestMigration, OtherPoolAbortAfterExecute)
{
migration_prepare(_other_pool_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
migration_execute(_other_pool_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_abort(_other_pool_ioctx, m_image_name);
}
TEST_F(TestMigration, OtherNamespaceAbortAfterExecute)
{
ASSERT_EQ(0, librbd::api::Namespace<>::create(_other_pool_ioctx, "ns2"));
_other_pool_ioctx.set_namespace("ns2");
migration_prepare(_other_pool_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
migration_execute(_other_pool_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_abort(_other_pool_ioctx, m_image_name);
_other_pool_ioctx.set_namespace("");
ASSERT_EQ(0, librbd::api::Namespace<>::remove(_other_pool_ioctx, "ns2"));
}
TEST_F(TestMigration, MirroringSamePool)
{
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
ASSERT_EQ(0, librbd::api::Mirror<>::mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(0, librbd::api::Mirror<>::image_enable(
m_ictx, RBD_MIRROR_IMAGE_MODE_JOURNAL, false));
librbd::mirror_image_info_t info;
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
migrate(m_ioctx, m_image_name);
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
}
TEST_F(TestMigration, MirroringAbort)
{
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
ASSERT_EQ(0, librbd::api::Mirror<>::mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(0, librbd::api::Mirror<>::image_enable(
m_ictx, RBD_MIRROR_IMAGE_MODE_JOURNAL, false));
librbd::mirror_image_info_t info;
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_DISABLED, info.state);
migration_abort(m_ioctx, m_image_name);
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
}
TEST_F(TestMigration, MirroringOtherPoolDisabled)
{
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
ASSERT_EQ(0, librbd::api::Mirror<>::mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(0, librbd::api::Mirror<>::image_enable(
m_ictx, RBD_MIRROR_IMAGE_MODE_JOURNAL, false));
librbd::mirror_image_info_t info;
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
migrate(_other_pool_ioctx, m_image_name);
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_DISABLED, info.state);
}
TEST_F(TestMigration, MirroringOtherPoolEnabled)
{
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
ASSERT_EQ(0, librbd::api::Mirror<>::mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(0, librbd::api::Mirror<>::mode_set(_other_pool_ioctx,
RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(0, librbd::api::Mirror<>::image_enable(
m_ictx, RBD_MIRROR_IMAGE_MODE_JOURNAL, false));
librbd::mirror_image_info_t info;
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
migrate(_other_pool_ioctx, m_image_name);
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
}
TEST_F(TestMigration, MirroringPool)
{
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
ASSERT_EQ(0, librbd::api::Mirror<>::mode_set(_other_pool_ioctx,
RBD_MIRROR_MODE_POOL));
librbd::mirror_image_info_t info;
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_DISABLED, info.state);
migrate(_other_pool_ioctx, m_image_name);
ASSERT_EQ(0, librbd::api::Mirror<>::image_get_info(m_ictx, &info));
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
}
TEST_F(TestMigration, Group)
{
REQUIRE_FORMAT_V2();
ASSERT_EQ(0, librbd::api::Group<>::create(m_ioctx, "123"));
ASSERT_EQ(0, librbd::api::Group<>::image_add(m_ioctx, "123", m_ioctx,
m_image_name.c_str()));
librbd::group_info_t info;
ASSERT_EQ(0, librbd::api::Group<>::image_get_group(m_ictx, &info));
std::string name = get_temp_image_name();
migrate(m_ioctx, name);
ASSERT_EQ(0, librbd::api::Group<>::image_get_group(m_ictx, &info));
ASSERT_EQ(info.name, "123");
ASSERT_EQ(0, librbd::api::Group<>::image_remove(m_ioctx, "123", m_ioctx,
name.c_str()));
ASSERT_EQ(0, librbd::api::Group<>::remove(m_ioctx, "123"));
}
TEST_F(TestMigration, GroupAbort)
{
REQUIRE_FORMAT_V2();
ASSERT_EQ(0, librbd::api::Group<>::create(m_ioctx, "123"));
ASSERT_EQ(0, librbd::api::Group<>::image_add(m_ioctx, "123", m_ioctx,
m_image_name.c_str()));
librbd::group_info_t info;
ASSERT_EQ(0, librbd::api::Group<>::image_get_group(m_ictx, &info));
std::string name = get_temp_image_name();
migration_prepare(m_ioctx, name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
ASSERT_EQ(0, librbd::api::Group<>::image_get_group(m_ictx, &info));
ASSERT_EQ(info.name, "123");
migration_abort(m_ioctx, m_image_name);
ASSERT_EQ(0, librbd::api::Group<>::image_get_group(m_ictx, &info));
ASSERT_EQ(info.name, "123");
ASSERT_EQ(0, librbd::api::Group<>::image_remove(m_ioctx, "123", m_ioctx,
m_image_name.c_str()));
ASSERT_EQ(0, librbd::api::Group<>::remove(m_ioctx, "123"));
}
TEST_F(TestMigration, NoSnaps)
{
test_no_snaps();
migrate(m_ioctx, m_image_name);
}
TEST_F(TestMigration, NoSnapsOtherPool)
{
test_no_snaps();
test_no_snaps();
migrate(_other_pool_ioctx, m_image_name);
}
TEST_F(TestMigration, NoSnapsDataPool)
{
test_no_snaps();
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_DATA_POOL,
_other_pool_ioctx.get_pool_name().c_str()));
migrate(m_ioctx, m_image_name);
EXPECT_EQ(_other_pool_ioctx.get_id(), m_ictx->data_ctx.get_id());
}
TEST_F(TestMigration, NoSnapsShrinkAfterPrepare)
{
test_no_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
resize(m_ictx->size >> 1);
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, NoSnapsShrinkToZeroBeforePrepare)
{
test_no_snaps();
resize(0);
migrate(m_ioctx, m_image_name);
}
TEST_F(TestMigration, NoSnapsShrinkToZeroAfterPrepare)
{
test_no_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
resize(0);
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, NoSnapsExpandAfterPrepare)
{
test_no_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
resize(m_ictx->size << 1);
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, NoSnapsSnapAfterPrepare)
{
test_no_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
snap_create("after_prepare_snap");
resize(m_ictx->size >> 1);
write(0, 1000, '*');
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, Snaps)
{
test_snaps();
migrate(m_ioctx, m_image_name);
}
TEST_F(TestMigration, SnapsOtherPool)
{
test_snaps();
test_no_snaps();
migrate(_other_pool_ioctx, m_image_name);
EXPECT_EQ(_other_pool_ioctx.get_id(), m_ictx->md_ctx.get_id());
}
TEST_F(TestMigration, SnapsDataPool)
{
test_snaps();
ASSERT_EQ(0, m_opts.set(RBD_IMAGE_OPTION_DATA_POOL,
_other_pool_ioctx.get_pool_name().c_str()));
migrate(m_ioctx, m_image_name);
EXPECT_EQ(_other_pool_ioctx.get_id(), m_ictx->data_ctx.get_id());
}
TEST_F(TestMigration, SnapsShrinkAfterPrepare)
{
test_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
resize(m_ictx->size >> 1);
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, SnapsShrinkToZeroBeforePrepare)
{
test_snaps();
resize(0);
migrate(m_ioctx, m_image_name);
}
TEST_F(TestMigration, SnapsShrinkToZeroAfterPrepare)
{
test_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
resize(0);
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, SnapsExpandAfterPrepare)
{
test_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
auto size = m_ictx->size;
resize(size << 1);
write(size, 1000, '*');
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, SnapsExpandAfterPrepare2)
{
auto size = m_ictx->size;
write(size >> 1, 10, 'X');
snap_create("snap1");
resize(size >> 1);
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
resize(size);
write(size >> 1, 5, 'Y');
compare("before execute");
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, SnapsSnapAfterPrepare)
{
test_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
auto ictx = new librbd::ImageCtx(m_ictx->name.c_str(), "", "snap3", m_ioctx,
false);
ASSERT_EQ(0, ictx->state->open(0));
EXPECT_EQ(0, librbd::api::Image<>::snap_set(
m_ref_ictx, cls::rbd::UserSnapshotNamespace(), "snap3"));
compare_snaps("opened after prepare snap3", m_ref_ictx, ictx);
EXPECT_EQ(0, librbd::api::Image<>::snap_set(
m_ref_ictx, cls::rbd::UserSnapshotNamespace(), nullptr));
EXPECT_EQ(0, ictx->state->close());
snap_create("after_prepare_snap");
resize(m_ictx->size >> 1);
write(0, 1000, '*');
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, SnapsSnapExpandAfterPrepare)
{
test_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
snap_create("after_prepare_snap");
auto size = m_ictx->size;
resize(size << 1);
write(size, 1000, '*');
migration_execute(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_EXECUTED);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, Clone)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
test_clone();
migrate(m_ioctx, m_image_name);
}
TEST_F(TestMigration, CloneParent) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
snap_create("snap");
librbd::linked_image_spec_t expected_parent_image;
expected_parent_image.image_id = m_ictx->id;
expected_parent_image.image_name = m_ictx->name;
auto it = m_ictx->snap_ids.find({cls::rbd::UserSnapshotNamespace{}, "snap"});
ASSERT_TRUE(it != m_ictx->snap_ids.end());
librbd::snap_spec_t expected_parent_snap;
expected_parent_snap.id = it->second;
clone("snap");
migration_prepare(m_ioctx, m_image_name);
librbd::linked_image_spec_t parent_image;
librbd::snap_spec_t parent_snap;
ASSERT_EQ(0, librbd::api::Image<>::get_parent(m_ictx, &parent_image,
&parent_snap));
ASSERT_EQ(expected_parent_image.image_id, parent_image.image_id);
ASSERT_EQ(expected_parent_image.image_name, parent_image.image_name);
ASSERT_EQ(expected_parent_snap.id, parent_snap.id);
migration_abort(m_ioctx, m_image_name);
}
TEST_F(TestMigration, CloneUpdateAfterPrepare)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
write(0, 10, 'X');
snap_create("snap");
clone("snap");
migration_prepare(m_ioctx, m_image_name);
write(0, 1, 'Y');
migration_execute(m_ioctx, m_image_name);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, TriggerAssertSnapcSeq)
{
auto size = m_ictx->size;
write((size >> 1) + 0, 10, 'A');
snap_create("snap1");
write((size >> 1) + 1, 10, 'B');
migration_prepare(m_ioctx, m_image_name);
// copyup => deep copy (first time)
write((size >> 1) + 2, 10, 'C');
// preserve data before resizing
snap_create("snap2");
// decrease head overlap
resize(size >> 1);
// migrate object => deep copy (second time) => assert_snapc_seq => -ERANGE
migration_execute(m_ioctx, m_image_name);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, SnapTrimBeforePrepare)
{
auto size = m_ictx->size;
write(size >> 1, 10, 'A');
snap_create("snap1");
resize(size >> 1);
migration_prepare(m_ioctx, m_image_name);
resize(size);
snap_create("snap3");
write(size >> 1, 10, 'B');
snap_create("snap4");
resize(size >> 1);
migration_execute(m_ioctx, m_image_name);
migration_commit(m_ioctx, m_image_name);
}
TEST_F(TestMigration, AbortInUseImage) {
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
librbd::NoOpProgressContext no_op;
EXPECT_EQ(-EBUSY, librbd::api::Migration<>::abort(m_ioctx, m_ictx->name,
no_op));
}
TEST_F(TestMigration, AbortWithoutSnapshots) {
test_no_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
test_no_snaps();
migration_abort(m_ioctx, m_image_name);
}
TEST_F(TestMigration, AbortWithSnapshots) {
test_snaps();
migration_prepare(m_ioctx, m_image_name);
migration_status(RBD_IMAGE_MIGRATION_STATE_PREPARED);
test_no_snaps();
flush();
ASSERT_EQ(0, TestFixture::snap_create(*m_ictx, "dst-only-snap"));
test_no_snaps();
migration_abort(m_ioctx, m_image_name);
}
TEST_F(TestMigration, CloneV1Parent)
{
const uint32_t CLONE_FORMAT = 1;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *) {
migrate(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV2Parent)
{
const uint32_t CLONE_FORMAT = 2;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *) {
migrate(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV1ParentAbort)
{
const uint32_t CLONE_FORMAT = 1;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *) {
migration_prepare(m_ioctx, m_image_name);
migration_abort(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV2ParentAbort)
{
const uint32_t CLONE_FORMAT = 2;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *) {
migration_prepare(m_ioctx, m_image_name);
migration_abort(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV1ParentAbortFixIncompleteChildReattach)
{
const uint32_t CLONE_FORMAT = 1;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *child_ictx) {
auto src_image_id = m_ictx->id;
migration_prepare(m_ioctx, m_image_name);
// Attach the child to both source and destination
// to emulate a crash when re-attaching the child
librbd::ImageCtx *src_ictx;
open_image(m_ioctx, "", src_image_id, false,
librbd::OPEN_FLAG_IGNORE_MIGRATING, &src_ictx);
C_SaferCond cond;
auto req = librbd::image::AttachChildRequest<>::create(
child_ictx, src_ictx, src_ictx->snaps[0], nullptr, 0,
CLONE_FORMAT, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
close_image(src_ictx);
migration_abort(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV1ParentAbortFixParentReattach)
{
const uint32_t CLONE_FORMAT = 1;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *child_ictx) {
auto src_image_id = m_ictx->id;
migration_prepare(m_ioctx, m_image_name);
// Re-attach the child back to the source to emulate a crash
// after the parent reattach but before the child reattach
librbd::ImageCtx *src_ictx;
open_image(m_ioctx, "", src_image_id, false,
librbd::OPEN_FLAG_IGNORE_MIGRATING, &src_ictx);
C_SaferCond cond;
auto req = librbd::image::AttachChildRequest<>::create(
child_ictx, src_ictx, src_ictx->snaps[0], m_ictx,
m_ictx->snaps[0], CLONE_FORMAT, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
close_image(src_ictx);
migration_abort(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV1ParentAbortRelinkNotNeeded)
{
const uint32_t CLONE_FORMAT = 1;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *child_ictx) {
auto src_image_id = m_ictx->id;
auto parent_spec = child_ictx->parent_md.spec;
parent_spec.image_id = m_ictx->id;
parent_spec.snap_id = m_ictx->snaps[0];
auto parent_overlap = child_ictx->parent_md.overlap;
migration_prepare(m_ioctx, m_image_name);
// Relink the child back to emulate a crash
// before relinking the child
C_SaferCond cond;
auto req = librbd::image::AttachParentRequest<>::create(
*child_ictx, parent_spec, parent_overlap, true, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
librbd::ImageCtx *src_ictx;
open_image(m_ioctx, "", src_image_id, false,
librbd::OPEN_FLAG_IGNORE_MIGRATING, &src_ictx);
C_SaferCond cond1;
auto req1 = librbd::image::AttachChildRequest<>::create(
child_ictx, src_ictx, src_ictx->snaps[0], m_ictx,
m_ictx->snaps[0], CLONE_FORMAT, &cond1);
req1->send();
ASSERT_EQ(0, cond1.wait());
close_image(src_ictx);
migration_abort(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV2ParentAbortFixIncompleteChildReattach)
{
const uint32_t CLONE_FORMAT = 2;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *child_ictx) {
auto src_image_id = m_ictx->id;
migration_prepare(m_ioctx, m_image_name);
// Attach the child to both source and destination
// to emulate a crash when re-attaching the child
librbd::ImageCtx *src_ictx;
open_image(m_ioctx, "", src_image_id, false,
librbd::OPEN_FLAG_IGNORE_MIGRATING, &src_ictx);
C_SaferCond cond;
auto req = librbd::image::AttachChildRequest<>::create(
child_ictx, src_ictx, src_ictx->snaps[0], nullptr, 0,
CLONE_FORMAT, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
close_image(src_ictx);
migration_abort(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV2ParentAbortFixParentReattach)
{
const uint32_t CLONE_FORMAT = 2;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *child_ictx) {
auto src_image_id = m_ictx->id;
migration_prepare(m_ioctx, m_image_name);
// Re-attach the child back to the source to emulate a crash
// after the parent reattach but before the child reattach
librbd::ImageCtx *src_ictx;
open_image(m_ioctx, "", src_image_id, false,
librbd::OPEN_FLAG_IGNORE_MIGRATING, &src_ictx);
C_SaferCond cond;
auto req = librbd::image::AttachChildRequest<>::create(
child_ictx, src_ictx, src_ictx->snaps[0], m_ictx,
m_ictx->snaps[0], CLONE_FORMAT, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
close_image(src_ictx);
migration_abort(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, CloneV2ParentAbortRelinkNotNeeded)
{
const uint32_t CLONE_FORMAT = 2;
test_migrate_parent(
CLONE_FORMAT, [this](librbd::ImageCtx *child_ictx) {
auto src_image_id = m_ictx->id;
auto parent_spec = child_ictx->parent_md.spec;
parent_spec.image_id = m_ictx->id;
parent_spec.snap_id = m_ictx->snaps[0];
auto parent_overlap = child_ictx->parent_md.overlap;
migration_prepare(m_ioctx, m_image_name);
// Relink the child back to emulate a crash
// before relinking the child
C_SaferCond cond;
auto req = librbd::image::AttachParentRequest<>::create(
*child_ictx, parent_spec, parent_overlap, true, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
librbd::ImageCtx *src_ictx;
open_image(m_ioctx, "", src_image_id, false,
librbd::OPEN_FLAG_IGNORE_MIGRATING, &src_ictx);
C_SaferCond cond1;
auto req1 = librbd::image::AttachChildRequest<>::create(
child_ictx, src_ictx, src_ictx->snaps[0], m_ictx,
m_ictx->snaps[0], CLONE_FORMAT, &cond1);
req1->send();
ASSERT_EQ(0, cond1.wait());
close_image(src_ictx);
migration_abort(m_ioctx, m_image_name);
});
}
TEST_F(TestMigration, StressNoMigrate)
{
test_stress();
compare();
}
TEST_F(TestMigration, Stress)
{
test_stress();
migrate(m_ioctx, m_image_name);
}
TEST_F(TestMigration, Stress2)
{
test_stress2(false);
}
TEST_F(TestMigration, StressLive)
{
test_stress2(true);
}
} // namespace librbd
| 41,554 | 29.555147 | 99 |
cc
|
null |
ceph-main/src/test/librbd/test_MirroringWatcher.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "include/rbd_types.h"
#include "librbd/MirroringWatcher.h"
#include "common/Cond.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <list>
void register_test_mirroring_watcher() {
}
namespace librbd {
namespace {
struct MockMirroringWatcher : public MirroringWatcher<> {
std::string oid;
MockMirroringWatcher(ImageCtx &image_ctx)
: MirroringWatcher<>(image_ctx.md_ctx, image_ctx.op_work_queue) {
}
MOCK_METHOD1(handle_mode_updated, void(cls::rbd::MirrorMode));
MOCK_METHOD3(handle_image_updated, void(cls::rbd::MirrorImageState,
const std::string &,
const std::string &));
};
} // anonymous namespace
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Invoke;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMirroringWatcher : public TestFixture {
public:
void SetUp() override {
TestFixture::SetUp();
bufferlist bl;
ASSERT_EQ(0, m_ioctx.write_full(RBD_MIRRORING, bl));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
m_image_watcher = new MockMirroringWatcher(*ictx);
C_SaferCond ctx;
m_image_watcher->register_watch(&ctx);
if (ctx.wait() != 0) {
delete m_image_watcher;
m_image_watcher = nullptr;
FAIL();
}
}
void TearDown() override {
if (m_image_watcher != nullptr) {
C_SaferCond ctx;
m_image_watcher->unregister_watch(&ctx);
ASSERT_EQ(0, ctx.wait());
delete m_image_watcher;
}
TestFixture::TearDown();
}
MockMirroringWatcher *m_image_watcher = nullptr;
};
TEST_F(TestMirroringWatcher, ModeUpdated) {
EXPECT_CALL(*m_image_watcher,
handle_mode_updated(cls::rbd::MIRROR_MODE_DISABLED))
.Times(AtLeast(1));
C_SaferCond ctx;
MockMirroringWatcher::notify_mode_updated(
m_ioctx, cls::rbd::MIRROR_MODE_DISABLED, &ctx);
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMirroringWatcher, ImageStatusUpdated) {
EXPECT_CALL(*m_image_watcher,
handle_image_updated(cls::rbd::MIRROR_IMAGE_STATE_ENABLED,
StrEq("image id"),
StrEq("global image id")))
.Times(AtLeast(1));
C_SaferCond ctx;
MockMirroringWatcher::notify_image_updated(
m_ioctx, cls::rbd::MIRROR_IMAGE_STATE_ENABLED, "image id",
"global image id", &ctx);
ASSERT_EQ(0, ctx.wait());
}
} // namespace librbd
| 2,644 | 24.931373 | 72 |
cc
|
null |
ceph-main/src/test/librbd/test_ObjectMap.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/ImageWatcher.h"
#include "librbd/internal.h"
#include "librbd/ObjectMap.h"
#include "common/Cond.h"
#include "common/Throttle.h"
#include "cls/rbd/cls_rbd_client.h"
#include "cls/rbd/cls_rbd_types.h"
#include <list>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/rolling_sum.hpp>
void register_test_object_map() {
}
class TestObjectMap : public TestFixture {
public:
int when_open_object_map(librbd::ImageCtx *ictx) {
C_SaferCond ctx;
librbd::ObjectMap<> *object_map = new librbd::ObjectMap<>(*ictx, ictx->snap_id);
object_map->open(&ctx);
int r = ctx.wait();
object_map->put();
return r;
}
};
TEST_F(TestObjectMap, RefreshInvalidatesWhenCorrupt) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool flags_set;
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_FALSE(flags_set);
C_SaferCond lock_ctx;
{
std::unique_lock owner_locker{ictx->owner_lock};
ictx->exclusive_lock->try_acquire_lock(&lock_ctx);
}
ASSERT_EQ(0, lock_ctx.wait());
std::string oid = librbd::ObjectMap<>::object_map_name(ictx->id, CEPH_NOSNAP);
bufferlist bl;
bl.append("corrupt");
ASSERT_EQ(0, ictx->md_ctx.write_full(oid, bl));
ASSERT_EQ(0, when_open_object_map(ictx));
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_TRUE(flags_set);
}
TEST_F(TestObjectMap, RefreshInvalidatesWhenTooSmall) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool flags_set;
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_FALSE(flags_set);
C_SaferCond lock_ctx;
{
std::unique_lock owner_locker{ictx->owner_lock};
ictx->exclusive_lock->try_acquire_lock(&lock_ctx);
}
ASSERT_EQ(0, lock_ctx.wait());
librados::ObjectWriteOperation op;
librbd::cls_client::object_map_resize(&op, 0, OBJECT_NONEXISTENT);
std::string oid = librbd::ObjectMap<>::object_map_name(ictx->id, CEPH_NOSNAP);
ASSERT_EQ(0, ictx->md_ctx.operate(oid, &op));
ASSERT_EQ(0, when_open_object_map(ictx));
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_TRUE(flags_set);
}
TEST_F(TestObjectMap, InvalidateFlagOnDisk) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool flags_set;
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_FALSE(flags_set);
C_SaferCond lock_ctx;
{
std::unique_lock owner_locker{ictx->owner_lock};
ictx->exclusive_lock->try_acquire_lock(&lock_ctx);
}
ASSERT_EQ(0, lock_ctx.wait());
std::string oid = librbd::ObjectMap<>::object_map_name(ictx->id, CEPH_NOSNAP);
bufferlist bl;
bl.append("corrupt");
ASSERT_EQ(0, ictx->md_ctx.write_full(oid, bl));
ASSERT_EQ(0, when_open_object_map(ictx));
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_TRUE(flags_set);
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_TRUE(flags_set);
}
TEST_F(TestObjectMap, AcquireLockInvalidatesWhenTooSmall) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool flags_set;
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_FALSE(flags_set);
librados::ObjectWriteOperation op;
librbd::cls_client::object_map_resize(&op, 0, OBJECT_NONEXISTENT);
std::string oid = librbd::ObjectMap<>::object_map_name(ictx->id, CEPH_NOSNAP);
ASSERT_EQ(0, ictx->md_ctx.operate(oid, &op));
C_SaferCond lock_ctx;
{
std::unique_lock owner_locker{ictx->owner_lock};
ictx->exclusive_lock->try_acquire_lock(&lock_ctx);
}
ASSERT_EQ(0, lock_ctx.wait());
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_TRUE(flags_set);
// Test the flag is stored on disk
ASSERT_EQ(0, ictx->state->refresh());
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_TRUE(flags_set);
}
namespace chrono = std::chrono;
TEST_F(TestObjectMap, DISABLED_StressTest) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
uint64_t object_count = cls::rbd::MAX_OBJECT_MAP_OBJECT_COUNT;
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, resize(ictx, ictx->layout.object_size * object_count));
bool flags_set;
ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP, RBD_FLAG_OBJECT_MAP_INVALID,
&flags_set));
ASSERT_FALSE(flags_set);
srand(time(NULL) % (unsigned long) -1);
coarse_mono_time start = coarse_mono_clock::now();
chrono::duration<double> last = chrono::duration<double>::zero();
const int WINDOW_SIZE = 5;
typedef boost::accumulators::accumulator_set<
double, boost::accumulators::stats<
boost::accumulators::tag::rolling_sum> > RollingSum;
RollingSum time_acc(
boost::accumulators::tag::rolling_window::window_size = WINDOW_SIZE);
RollingSum ios_acc(
boost::accumulators::tag::rolling_window::window_size = WINDOW_SIZE);
uint32_t io_threads = 16;
uint64_t cur_ios = 0;
SimpleThrottle throttle(io_threads, false);
for (uint64_t ios = 0; ios < 100000;) {
if (throttle.pending_error()) {
break;
}
throttle.start_op();
uint64_t object_no = (rand() % object_count);
auto ctx = new LambdaContext([&throttle, object_no](int r) {
ASSERT_EQ(0, r) << "object_no=" << object_no;
throttle.end_op(r);
});
std::shared_lock owner_locker{ictx->owner_lock};
std::shared_lock image_locker{ictx->image_lock};
ASSERT_TRUE(ictx->object_map != nullptr);
if (!ictx->object_map->aio_update<
Context, &Context::complete>(CEPH_NOSNAP, object_no,
OBJECT_EXISTS, {}, {}, true,
ctx)) {
ctx->complete(0);
} else {
++cur_ios;
++ios;
}
coarse_mono_time now = coarse_mono_clock::now();
chrono::duration<double> elapsed = now - start;
if (last == chrono::duration<double>::zero()) {
last = elapsed;
} else if ((int)elapsed.count() != (int)last.count()) {
time_acc((elapsed - last).count());
ios_acc(static_cast<double>(cur_ios));
cur_ios = 0;
double time_sum = boost::accumulators::rolling_sum(time_acc);
std::cerr << std::setw(5) << (int)elapsed.count() << "\t"
<< std::setw(8) << (int)ios << "\t"
<< std::fixed << std::setw(8) << std::setprecision(2)
<< boost::accumulators::rolling_sum(ios_acc) / time_sum
<< std::endl;
last = elapsed;
}
}
ASSERT_EQ(0, throttle.wait_for_ret());
}
| 7,711 | 31 | 84 |
cc
|
null |
ceph-main/src/test/librbd/test_Operations.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/ImageCtx.h"
#include "librbd/Operations.h"
void register_test_operations() {
}
class TestOperations : public TestFixture {
public:
};
TEST_F(TestOperations, DisableJournalingCorrupt) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, m_ioctx.remove("journal." + ictx->id));
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
}
| 689 | 24.555556 | 72 |
cc
|
null |
ceph-main/src/test/librbd/test_Trash.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "cls/rbd/cls_rbd_client.h"
#include "cls/rbd/cls_rbd_types.h"
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/api/Trash.h"
#include <set>
#include <vector>
void register_test_trash() {
}
namespace librbd {
static bool operator==(const trash_image_info_t& lhs,
const trash_image_info_t& rhs) {
return (lhs.id == rhs.id &&
lhs.name == rhs.name &&
lhs.source == rhs.source);
}
static bool operator==(const image_spec_t& lhs,
const image_spec_t& rhs) {
return (lhs.id == rhs.id && lhs.name == rhs.name);
}
class TestTrash : public TestFixture {
public:
TestTrash() {}
};
TEST_F(TestTrash, UserRemovingSource) {
REQUIRE_FORMAT_V2();
auto compare_lambda = [](const trash_image_info_t& lhs,
const trash_image_info_t& rhs) {
if (lhs.id != rhs.id) {
return lhs.id < rhs.id;
} else if (lhs.name != rhs.name) {
return lhs.name < rhs.name;
}
return lhs.source < rhs.source;
};
typedef std::set<trash_image_info_t, decltype(compare_lambda)> TrashEntries;
librbd::RBD rbd;
librbd::Image image;
auto image_name1 = m_image_name;
std::string image_id1;
ASSERT_EQ(0, rbd.open(m_ioctx, image, image_name1.c_str()));
ASSERT_EQ(0, image.get_id(&image_id1));
ASSERT_EQ(0, image.close());
auto image_name2 = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(m_rbd, m_ioctx, image_name2, m_image_size));
std::string image_id2;
ASSERT_EQ(0, rbd.open(m_ioctx, image, image_name2.c_str()));
ASSERT_EQ(0, image.get_id(&image_id2));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, api::Trash<>::move(m_ioctx, RBD_TRASH_IMAGE_SOURCE_USER,
image_name1, image_id1, 0));
ASSERT_EQ(0, api::Trash<>::move(m_ioctx, RBD_TRASH_IMAGE_SOURCE_REMOVING,
image_name2, image_id2, 0));
TrashEntries trash_entries{compare_lambda};
TrashEntries expected_trash_entries{compare_lambda};
std::vector<trash_image_info_t> entries;
ASSERT_EQ(0, api::Trash<>::list(m_ioctx, entries, true));
trash_entries.insert(entries.begin(), entries.end());
expected_trash_entries = {
{.id = image_id1,
.name = image_name1,
.source = RBD_TRASH_IMAGE_SOURCE_USER},
};
ASSERT_EQ(expected_trash_entries, trash_entries);
std::vector<image_spec_t> expected_images = {
{.id = image_id2, .name = image_name2}
};
std::vector<image_spec_t> images;
ASSERT_EQ(0, rbd.list2(m_ioctx, &images));
ASSERT_EQ(expected_images, images);
}
TEST_F(TestTrash, RestoreMirroringSource) {
REQUIRE_FORMAT_V2();
librbd::RBD rbd;
librbd::Image image;
std::string image_id;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str()));
ASSERT_EQ(0, image.get_id(&image_id));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, api::Trash<>::move(m_ioctx, RBD_TRASH_IMAGE_SOURCE_MIRRORING,
m_image_name, 0));
ASSERT_EQ(0, rbd.trash_restore(m_ioctx, image_id.c_str(),
m_image_name.c_str()));
}
} // namespace librbd
| 3,253 | 28.853211 | 78 |
cc
|
null |
ceph-main/src/test/librbd/test_fixture.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "common/Cond.h"
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "include/stringify.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageState.h"
#include "librbd/ImageWatcher.h"
#include "librbd/io/ImageDispatchSpec.h"
#include "librbd/Operations.h"
#include "librbd/api/Io.h"
#include "cls/lock/cls_lock_client.h"
#include "cls/lock/cls_lock_types.h"
#include "cls/rbd/cls_rbd_types.h"
#include "librbd/internal.h"
#include "test/librados/test.h"
#include "test/librados/test_cxx.h"
#include <iostream>
#include <sstream>
#include <stdlib.h>
std::string TestFixture::_pool_name;
librados::Rados TestFixture::_rados;
rados_t TestFixture::_cluster;
uint64_t TestFixture::_image_number = 0;
std::string TestFixture::_data_pool;
TestFixture::TestFixture() : m_image_size(0) {
}
void TestFixture::SetUpTestCase() {
ASSERT_EQ("", connect_cluster(&_cluster));
_pool_name = get_temp_pool_name("test-librbd-");
ASSERT_EQ("", create_one_pool_pp(_pool_name, _rados));
bool created = false;
ASSERT_EQ(0, create_image_data_pool(_rados, _data_pool, &created));
if (!_data_pool.empty()) {
printf("using image data pool: %s\n", _data_pool.c_str());
if (!created) {
_data_pool.clear();
}
}
}
void TestFixture::TearDownTestCase() {
rados_shutdown(_cluster);
if (!_data_pool.empty()) {
ASSERT_EQ(0, _rados.pool_delete(_data_pool.c_str()));
}
ASSERT_EQ(0, destroy_one_pool_pp(_pool_name, _rados));
}
std::string TestFixture::get_temp_image_name() {
++_image_number;
return "image" + stringify(_image_number);
}
void TestFixture::SetUp() {
ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), m_ioctx));
m_cct = reinterpret_cast<CephContext*>(m_ioctx.cct());
librados::Rados rados(m_ioctx);
rados.conf_set("rbd_persistent_cache_path", ".");
m_image_name = get_temp_image_name();
m_image_size = 2 << 20;
ASSERT_EQ(0, create_image_pp(m_rbd, m_ioctx, m_image_name, m_image_size));
}
void TestFixture::TearDown() {
unlock_image();
for (std::set<librbd::ImageCtx *>::iterator iter = m_ictxs.begin();
iter != m_ictxs.end(); ++iter) {
(*iter)->state->close();
}
m_ioctx.close();
}
int TestFixture::open_image(const std::string &image_name,
librbd::ImageCtx **ictx) {
*ictx = new librbd::ImageCtx(image_name.c_str(), "", nullptr, m_ioctx, false);
m_ictxs.insert(*ictx);
return (*ictx)->state->open(0);
}
int TestFixture::snap_create(librbd::ImageCtx &ictx,
const std::string &snap_name) {
librbd::NoOpProgressContext prog_ctx;
return ictx.operations->snap_create(cls::rbd::UserSnapshotNamespace(),
snap_name.c_str(), 0, prog_ctx);
}
int TestFixture::snap_protect(librbd::ImageCtx &ictx,
const std::string &snap_name) {
return ictx.operations->snap_protect(cls::rbd::UserSnapshotNamespace(),
snap_name.c_str());
}
int TestFixture::flatten(librbd::ImageCtx &ictx,
librbd::ProgressContext &prog_ctx) {
return ictx.operations->flatten(prog_ctx);
}
int TestFixture::resize(librbd::ImageCtx *ictx, uint64_t size){
librbd::NoOpProgressContext prog_ctx;
return ictx->operations->resize(size, true, prog_ctx);
}
void TestFixture::close_image(librbd::ImageCtx *ictx) {
m_ictxs.erase(ictx);
ictx->state->close();
}
int TestFixture::lock_image(librbd::ImageCtx &ictx, ClsLockType lock_type,
const std::string &cookie) {
int r = rados::cls::lock::lock(&ictx.md_ctx, ictx.header_oid, RBD_LOCK_NAME,
lock_type, cookie, "internal", "", utime_t(),
0);
if (r == 0) {
m_lock_object = ictx.header_oid;
m_lock_cookie = cookie;
}
return r;
}
int TestFixture::unlock_image() {
int r = 0;
if (!m_lock_cookie.empty()) {
r = rados::cls::lock::unlock(&m_ioctx, m_lock_object, RBD_LOCK_NAME,
m_lock_cookie);
m_lock_cookie = "";
}
return r;
}
int TestFixture::acquire_exclusive_lock(librbd::ImageCtx &ictx) {
int r = librbd::api::Io<>::write(ictx, 0, 0, {}, 0);
if (r != 0) {
return r;
}
std::shared_lock owner_locker{ictx.owner_lock};
ceph_assert(ictx.exclusive_lock != nullptr);
return ictx.exclusive_lock->is_lock_owner() ? 0 : -EINVAL;
}
int TestFixture::flush_writeback_cache(librbd::ImageCtx *image_ctx) {
if (image_ctx->test_features(RBD_FEATURE_DIRTY_CACHE)) {
// cache exists. Close to flush data
C_SaferCond ctx;
auto aio_comp = librbd::io::AioCompletion::create_and_start(
&ctx, image_ctx, librbd::io::AIO_TYPE_FLUSH);
auto req = librbd::io::ImageDispatchSpec::create_flush(
*image_ctx, librbd::io::IMAGE_DISPATCH_LAYER_INTERNAL_START, aio_comp,
librbd::io::FLUSH_SOURCE_INTERNAL, {});
req->send();
return ctx.wait();
} else {
return librbd::api::Io<>::flush(*image_ctx);
}
}
| 4,989 | 29.060241 | 80 |
cc
|
null |
ceph-main/src/test/librbd/test_fixture.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/int_types.h"
#include "include/rados/librados.h"
#include "include/rbd/librbd.hpp"
#include "librbd/ImageCtx.h"
#include "gtest/gtest.h"
#include <set>
#include <string>
using namespace ceph;
class TestFixture : public ::testing::Test {
public:
TestFixture();
static void SetUpTestCase();
static void TearDownTestCase();
static std::string get_temp_image_name();
void SetUp() override;
void TearDown() override;
int open_image(const std::string &image_name, librbd::ImageCtx **ictx);
void close_image(librbd::ImageCtx *ictx);
int snap_create(librbd::ImageCtx &ictx, const std::string &snap_name);
int snap_protect(librbd::ImageCtx &ictx, const std::string &snap_name);
int flatten(librbd::ImageCtx &ictx, librbd::ProgressContext &prog_ctx);
int resize(librbd::ImageCtx *ictx, uint64_t size);
int lock_image(librbd::ImageCtx &ictx, ClsLockType lock_type,
const std::string &cookie);
int unlock_image();
int flush_writeback_cache(librbd::ImageCtx *image_ctx);
int acquire_exclusive_lock(librbd::ImageCtx &ictx);
static std::string _pool_name;
static librados::Rados _rados;
static rados_t _cluster;
static uint64_t _image_number;
static std::string _data_pool;
CephContext* m_cct = nullptr;
librados::IoCtx m_ioctx;
librbd::RBD m_rbd;
std::string m_image_name;
uint64_t m_image_size;
std::set<librbd::ImageCtx *> m_ictxs;
std::string m_lock_object;
std::string m_lock_cookie;
};
| 1,585 | 25 | 73 |
h
|
null |
ceph-main/src/test/librbd/test_internal.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "cls/journal/cls_journal_client.h"
#include "cls/rbd/cls_rbd_client.h"
#include "cls/rbd/cls_rbd_types.h"
#include "test/librados/test_cxx.h"
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "include/rbd/librbd.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageState.h"
#include "librbd/ImageWatcher.h"
#include "librbd/internal.h"
#include "librbd/ObjectMap.h"
#include "librbd/Operations.h"
#include "librbd/api/DiffIterate.h"
#include "librbd/api/Image.h"
#include "librbd/api/Io.h"
#include "librbd/api/Migration.h"
#include "librbd/api/PoolMetadata.h"
#include "librbd/api/Snapshot.h"
#include "librbd/io/AioCompletion.h"
#include "librbd/io/ImageRequest.h"
#include "osdc/Striper.h"
#include "common/Cond.h"
#include <boost/scope_exit.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/assign/list_of.hpp>
#include <utility>
#include <vector>
#include "test/librados/crimson_utils.h"
using namespace std;
void register_test_internal() {
}
namespace librbd {
class TestInternal : public TestFixture {
public:
TestInternal() {}
typedef std::vector<std::pair<std::string, bool> > Snaps;
void TearDown() override {
unlock_image();
for (Snaps::iterator iter = m_snaps.begin(); iter != m_snaps.end(); ++iter) {
librbd::ImageCtx *ictx;
EXPECT_EQ(0, open_image(m_image_name, &ictx));
if (iter->second) {
EXPECT_EQ(0,
ictx->operations->snap_unprotect(cls::rbd::UserSnapshotNamespace(),
iter->first.c_str()));
}
EXPECT_EQ(0,
ictx->operations->snap_remove(cls::rbd::UserSnapshotNamespace(),
iter->first.c_str()));
}
TestFixture::TearDown();
}
int create_snapshot(const char *snap_name, bool snap_protect) {
librbd::ImageCtx *ictx;
int r = open_image(m_image_name, &ictx);
if (r < 0) {
return r;
}
r = snap_create(*ictx, snap_name);
if (r < 0) {
return r;
}
m_snaps.push_back(std::make_pair(snap_name, snap_protect));
if (snap_protect) {
r = ictx->operations->snap_protect(cls::rbd::UserSnapshotNamespace(), snap_name);
if (r < 0) {
return r;
}
}
close_image(ictx);
return 0;
}
Snaps m_snaps;
};
class DummyContext : public Context {
public:
void finish(int r) override {
}
};
void generate_random_iomap(librbd::Image &image, int num_objects, int object_size,
int max_count, map<uint64_t, uint64_t> &iomap)
{
uint64_t stripe_unit, stripe_count;
stripe_unit = image.get_stripe_unit();
stripe_count = image.get_stripe_count();
while (max_count-- > 0) {
// generate random image offset based on base random object
// number and object offset and then map that back to an
// object number based on stripe unit and count.
uint64_t ono = rand() % num_objects;
uint64_t offset = rand() % (object_size - TEST_IO_SIZE);
uint64_t imageoff = (ono * object_size) + offset;
file_layout_t layout;
layout.object_size = object_size;
layout.stripe_unit = stripe_unit;
layout.stripe_count = stripe_count;
vector<ObjectExtent> ex;
Striper::file_to_extents(g_ceph_context, 1, &layout, imageoff, TEST_IO_SIZE, 0, ex);
// lets not worry if IO spans multiple extents (>1 object). in such
// as case we would perform the write multiple times to the same
// offset, but we record all objects that would be generated with
// this IO. TODO: fix this if such a need is required by your
// test.
vector<ObjectExtent>::iterator it;
map<uint64_t, uint64_t> curr_iomap;
for (it = ex.begin(); it != ex.end(); ++it) {
if (iomap.find((*it).objectno) != iomap.end()) {
break;
}
curr_iomap.insert(make_pair((*it).objectno, imageoff));
}
if (it == ex.end()) {
iomap.insert(curr_iomap.begin(), curr_iomap.end());
}
}
}
static bool is_sparsify_supported(librados::IoCtx &ioctx,
const std::string &oid) {
EXPECT_EQ(0, ioctx.create(oid, true));
int r = librbd::cls_client::sparsify(&ioctx, oid, 16, true);
EXPECT_TRUE(r == 0 || r == -EOPNOTSUPP);
ioctx.remove(oid);
return (r == 0);
}
static bool is_sparse_read_supported(librados::IoCtx &ioctx,
const std::string &oid) {
EXPECT_EQ(0, ioctx.create(oid, true));
bufferlist inbl;
inbl.append(std::string(1, 'X'));
EXPECT_EQ(0, ioctx.write(oid, inbl, inbl.length(), 1));
EXPECT_EQ(0, ioctx.write(oid, inbl, inbl.length(), 3));
std::map<uint64_t, uint64_t> m;
bufferlist outbl;
int r = ioctx.sparse_read(oid, m, outbl, 4, 0);
ioctx.remove(oid);
int expected_r = 2;
std::map<uint64_t, uint64_t> expected_m = {{1, 1}, {3, 1}};
bufferlist expected_outbl;
expected_outbl.append(std::string(2, 'X'));
return (r == expected_r && m == expected_m &&
outbl.contents_equal(expected_outbl));
}
TEST_F(TestInternal, OpenByID) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
std::string id = ictx->id;
close_image(ictx);
ictx = new librbd::ImageCtx("", id, nullptr, m_ioctx, true);
ASSERT_EQ(0, ictx->state->open(0));
ASSERT_EQ(ictx->name, m_image_name);
close_image(ictx);
}
TEST_F(TestInternal, OpenSnapDNE) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx = new librbd::ImageCtx(m_image_name, "", "unknown_snap", m_ioctx, true);
ASSERT_EQ(-ENOENT, ictx->state->open(librbd::OPEN_FLAG_SKIP_OPEN_PARENT));
}
TEST_F(TestInternal, IsExclusiveLockOwner) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool is_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &is_owner));
ASSERT_FALSE(is_owner);
C_SaferCond ctx;
{
std::unique_lock l{ictx->owner_lock};
ictx->exclusive_lock->try_acquire_lock(&ctx);
}
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &is_owner));
ASSERT_TRUE(is_owner);
}
TEST_F(TestInternal, ResizeLocksImage) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, ictx->operations->resize(m_image_size >> 1, true, no_op));
bool is_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &is_owner));
ASSERT_TRUE(is_owner);
}
TEST_F(TestInternal, ResizeFailsToLockImage) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE, "manually locked"));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(-EROFS, ictx->operations->resize(m_image_size >> 1, true, no_op));
}
TEST_F(TestInternal, SnapCreateLocksImage) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap1"));
BOOST_SCOPE_EXIT( (ictx) ) {
ASSERT_EQ(0,
ictx->operations->snap_remove(cls::rbd::UserSnapshotNamespace(),
"snap1"));
} BOOST_SCOPE_EXIT_END;
bool is_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &is_owner));
ASSERT_TRUE(is_owner);
}
TEST_F(TestInternal, SnapCreateFailsToLockImage) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE, "manually locked"));
ASSERT_EQ(-EROFS, snap_create(*ictx, "snap1"));
}
TEST_F(TestInternal, SnapRollbackLocksImage) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
ASSERT_EQ(0, create_snapshot("snap1", false));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, ictx->operations->snap_rollback(cls::rbd::UserSnapshotNamespace(),
"snap1",
no_op));
bool is_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &is_owner));
ASSERT_TRUE(is_owner);
}
TEST_F(TestInternal, SnapRollbackFailsToLockImage) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
ASSERT_EQ(0, create_snapshot("snap1", false));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE, "manually locked"));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(-EROFS,
ictx->operations->snap_rollback(cls::rbd::UserSnapshotNamespace(),
"snap1",
no_op));
}
TEST_F(TestInternal, SnapSetReleasesLock) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
ASSERT_EQ(0, create_snapshot("snap1", false));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, librbd::api::Image<>::snap_set(
ictx, cls::rbd::UserSnapshotNamespace(), "snap1"));
bool is_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &is_owner));
ASSERT_FALSE(is_owner);
}
TEST_F(TestInternal, FlattenLocksImage) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snapshot("snap1", true));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
uint64_t features;
ASSERT_EQ(0, librbd::get_features(ictx, &features));
std::string clone_name = get_temp_image_name();
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap1", m_ioctx,
clone_name.c_str(), features, &order, 0, 0));
librbd::ImageCtx *ictx2;
ASSERT_EQ(0, open_image(clone_name, &ictx2));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, ictx2->operations->flatten(no_op));
bool is_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx2, &is_owner));
ASSERT_TRUE(is_owner);
}
TEST_F(TestInternal, FlattenFailsToLockImage) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snapshot("snap1", true));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
uint64_t features;
ASSERT_EQ(0, librbd::get_features(ictx, &features));
std::string clone_name = get_temp_image_name();
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap1", m_ioctx,
clone_name.c_str(), features, &order, 0, 0));
TestInternal *parent = this;
librbd::ImageCtx *ictx2 = NULL;
BOOST_SCOPE_EXIT( (&m_ioctx) (clone_name) (parent) (&ictx2) ) {
if (ictx2 != NULL) {
parent->close_image(ictx2);
parent->unlock_image();
}
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, librbd::api::Image<>::remove(m_ioctx, clone_name, no_op));
} BOOST_SCOPE_EXIT_END;
ASSERT_EQ(0, open_image(clone_name, &ictx2));
ASSERT_EQ(0, lock_image(*ictx2, ClsLockType::EXCLUSIVE, "manually locked"));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(-EROFS, ictx2->operations->flatten(no_op));
}
TEST_F(TestInternal, WriteFailsToLockImageBlocklisted) {
SKIP_IF_CRIMSON();
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::Rados blocklist_rados;
ASSERT_EQ("", connect_cluster_pp(blocklist_rados));
librados::IoCtx blocklist_ioctx;
ASSERT_EQ(0, blocklist_rados.ioctx_create(_pool_name.c_str(),
blocklist_ioctx));
auto ictx = new librbd::ImageCtx(m_image_name, "", nullptr, blocklist_ioctx,
false);
ASSERT_EQ(0, ictx->state->open(0));
std::list<librbd::image_watcher_t> watchers;
ASSERT_EQ(0, librbd::list_watchers(ictx, watchers));
ASSERT_EQ(1U, watchers.size());
bool lock_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, blocklist_rados.blocklist_add(watchers.front().addr, 0));
ceph::bufferlist bl;
bl.append(std::string(256, '1'));
ASSERT_EQ(-EBLOCKLISTED, api::Io<>::write(*ictx, 0, bl.length(),
std::move(bl), 0));
ASSERT_EQ(-EBLOCKLISTED, librbd::is_exclusive_lock_owner(ictx, &lock_owner));
close_image(ictx);
}
TEST_F(TestInternal, WriteFailsToLockImageBlocklistedWatch) {
SKIP_IF_CRIMSON();
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::Rados blocklist_rados;
ASSERT_EQ("", connect_cluster_pp(blocklist_rados));
librados::IoCtx blocklist_ioctx;
ASSERT_EQ(0, blocklist_rados.ioctx_create(_pool_name.c_str(),
blocklist_ioctx));
auto ictx = new librbd::ImageCtx(m_image_name, "", nullptr, blocklist_ioctx,
false);
ASSERT_EQ(0, ictx->state->open(0));
std::list<librbd::image_watcher_t> watchers;
ASSERT_EQ(0, librbd::list_watchers(ictx, watchers));
ASSERT_EQ(1U, watchers.size());
bool lock_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, blocklist_rados.blocklist_add(watchers.front().addr, 0));
// let ImageWatcher discover that the watch can't be re-registered to
// eliminate the (intended) race in WriteFailsToLockImageBlocklisted
while (!ictx->image_watcher->is_blocklisted()) {
sleep(1);
}
ceph::bufferlist bl;
bl.append(std::string(256, '1'));
ASSERT_EQ(-EBLOCKLISTED, api::Io<>::write(*ictx, 0, bl.length(),
std::move(bl), 0));
ASSERT_EQ(-EBLOCKLISTED, librbd::is_exclusive_lock_owner(ictx, &lock_owner));
close_image(ictx);
}
TEST_F(TestInternal, AioWriteRequestsLock) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE, "manually locked"));
std::string buffer(256, '1');
Context *ctx = new DummyContext();
auto c = librbd::io::AioCompletion::create(ctx);
c->get();
bufferlist bl;
bl.append(buffer);
api::Io<>::aio_write(*ictx, c, 0, buffer.size(), std::move(bl), 0, true);
bool is_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &is_owner));
ASSERT_FALSE(is_owner);
ASSERT_FALSE(c->is_complete());
unlock_image();
ASSERT_EQ(0, c->wait_for_complete());
c->put();
}
TEST_F(TestInternal, AioDiscardRequestsLock) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, lock_image(*ictx, ClsLockType::EXCLUSIVE, "manually locked"));
Context *ctx = new DummyContext();
auto c = librbd::io::AioCompletion::create(ctx);
c->get();
api::Io<>::aio_discard(*ictx, c, 0, 256, false, true);
bool is_owner;
ASSERT_EQ(0, librbd::is_exclusive_lock_owner(ictx, &is_owner));
ASSERT_FALSE(is_owner);
ASSERT_FALSE(c->is_complete());
unlock_image();
ASSERT_EQ(0, c->wait_for_complete());
c->put();
}
TEST_F(TestInternal, CancelAsyncResize) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
C_SaferCond ctx;
{
std::unique_lock l{ictx->owner_lock};
ictx->exclusive_lock->try_acquire_lock(&ctx);
}
ASSERT_EQ(0, ctx.wait());
{
std::shared_lock owner_locker{ictx->owner_lock};
ASSERT_TRUE(ictx->exclusive_lock->is_lock_owner());
}
uint64_t size;
ASSERT_EQ(0, librbd::get_size(ictx, &size));
uint32_t attempts = 0;
while (attempts++ < 20 && size > 0) {
C_SaferCond ctx;
librbd::NoOpProgressContext prog_ctx;
size -= std::min<uint64_t>(size, 1 << 18);
{
std::shared_lock l{ictx->owner_lock};
ictx->operations->execute_resize(size, true, prog_ctx, &ctx, 0);
}
// try to interrupt the in-progress resize
ictx->cancel_async_requests();
int r = ctx.wait();
if (r == -ERESTART) {
std::cout << "detected canceled async request" << std::endl;
break;
}
ASSERT_EQ(0, r);
}
}
TEST_F(TestInternal, MultipleResize) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
if (ictx->exclusive_lock != nullptr) {
C_SaferCond ctx;
{
std::unique_lock l{ictx->owner_lock};
ictx->exclusive_lock->try_acquire_lock(&ctx);
}
std::shared_lock owner_locker{ictx->owner_lock};
ASSERT_EQ(0, ctx.wait());
ASSERT_TRUE(ictx->exclusive_lock->is_lock_owner());
}
uint64_t size;
ASSERT_EQ(0, librbd::get_size(ictx, &size));
uint64_t original_size = size;
std::vector<C_SaferCond*> contexts;
uint32_t attempts = 0;
librbd::NoOpProgressContext prog_ctx;
while (size > 0) {
uint64_t new_size = original_size;
if (attempts++ % 2 == 0) {
size -= std::min<uint64_t>(size, 1 << 18);
new_size = size;
}
std::shared_lock l{ictx->owner_lock};
contexts.push_back(new C_SaferCond());
ictx->operations->execute_resize(new_size, true, prog_ctx, contexts.back(), 0);
}
for (uint32_t i = 0; i < contexts.size(); ++i) {
ASSERT_EQ(0, contexts[i]->wait());
delete contexts[i];
}
ASSERT_EQ(0, librbd::get_size(ictx, &size));
ASSERT_EQ(0U, size);
}
TEST_F(TestInternal, Metadata) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
map<string, bool> test_confs = boost::assign::map_list_of(
"aaaaaaa", false)(
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", false)(
"cccccccccccccc", false);
map<string, bool>::iterator it = test_confs.begin();
int r;
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
r = ictx->operations->metadata_set(it->first, "value1");
ASSERT_EQ(0, r);
++it;
r = ictx->operations->metadata_set(it->first, "value2");
ASSERT_EQ(0, r);
++it;
r = ictx->operations->metadata_set(it->first, "value3");
ASSERT_EQ(0, r);
r = ictx->operations->metadata_set("abcd", "value4");
ASSERT_EQ(0, r);
r = ictx->operations->metadata_set("xyz", "value5");
ASSERT_EQ(0, r);
map<string, bufferlist> pairs;
r = librbd::metadata_list(ictx, "", 0, &pairs);
ASSERT_EQ(0, r);
uint8_t original_pairs_num = 0;
if (ictx->test_features(RBD_FEATURE_DIRTY_CACHE)) {
original_pairs_num = 1;
}
ASSERT_EQ(original_pairs_num + 5, pairs.size());
r = ictx->operations->metadata_remove("abcd");
ASSERT_EQ(0, r);
r = ictx->operations->metadata_remove("xyz");
ASSERT_EQ(0, r);
pairs.clear();
r = librbd::metadata_list(ictx, "", 0, &pairs);
ASSERT_EQ(0, r);
ASSERT_EQ(original_pairs_num + 3, pairs.size());
string val;
r = librbd::metadata_get(ictx, it->first, &val);
ASSERT_EQ(0, r);
ASSERT_STREQ(val.c_str(), "value3");
}
TEST_F(TestInternal, MetadataConfApply) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(-ENOENT, ictx->operations->metadata_remove("conf_rbd_cache"));
bool cache = ictx->cache;
std::string rbd_conf_cache = cache ? "true" : "false";
std::string new_rbd_conf_cache = !cache ? "true" : "false";
ASSERT_EQ(0, ictx->operations->metadata_set("conf_rbd_cache",
new_rbd_conf_cache));
ASSERT_EQ(!cache, ictx->cache);
ASSERT_EQ(0, ictx->operations->metadata_remove("conf_rbd_cache"));
ASSERT_EQ(cache, ictx->cache);
}
TEST_F(TestInternal, SnapshotCopyup)
{
//https://tracker.ceph.com/issues/58263
// Clone overlap is WIP
SKIP_IF_CRIMSON();
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool sparse_read_supported = is_sparse_read_supported(
ictx->data_ctx, ictx->get_object_name(10));
bufferlist bl;
bl.append(std::string(256, '1'));
ASSERT_EQ(256, api::Io<>::write(*ictx, 0, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ(256, api::Io<>::write(*ictx, 1024, bl.length(), bufferlist{bl},
0));
ASSERT_EQ(0, snap_create(*ictx, "snap1"));
ASSERT_EQ(0,
ictx->operations->snap_protect(cls::rbd::UserSnapshotNamespace(),
"snap1"));
uint64_t features;
ASSERT_EQ(0, librbd::get_features(ictx, &features));
std::string clone_name = get_temp_image_name();
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap1", m_ioctx,
clone_name.c_str(), features, &order, 0, 0));
librbd::ImageCtx *ictx2;
ASSERT_EQ(0, open_image(clone_name, &ictx2));
ASSERT_EQ(0, snap_create(*ictx2, "snap1"));
ASSERT_EQ(0, snap_create(*ictx2, "snap2"));
ASSERT_EQ(256, api::Io<>::write(*ictx2, 256, bl.length(), bufferlist{bl},
0));
ASSERT_EQ(0, flush_writeback_cache(ictx2));
librados::IoCtx snap_ctx;
snap_ctx.dup(ictx2->data_ctx);
snap_ctx.snap_set_read(CEPH_SNAPDIR);
librados::snap_set_t snap_set;
ASSERT_EQ(0, snap_ctx.list_snaps(ictx2->get_object_name(0), &snap_set));
uint64_t copyup_end = ictx2->enable_sparse_copyup ? 1024 + 256 : 1 << order;
std::vector< std::pair<uint64_t,uint64_t> > expected_overlap =
boost::assign::list_of(
std::make_pair(0, 256))(
std::make_pair(512, copyup_end - 512));
ASSERT_EQ(2U, snap_set.clones.size());
ASSERT_NE(CEPH_NOSNAP, snap_set.clones[0].cloneid);
ASSERT_EQ(2U, snap_set.clones[0].snaps.size());
ASSERT_EQ(expected_overlap, snap_set.clones[0].overlap);
ASSERT_EQ(CEPH_NOSNAP, snap_set.clones[1].cloneid);
bufferptr read_ptr(256);
bufferlist read_bl;
read_bl.push_back(read_ptr);
std::list<std::string> snaps = {"snap1", "snap2", ""};
librbd::io::ReadResult read_result{&read_bl};
for (std::list<std::string>::iterator it = snaps.begin();
it != snaps.end(); ++it) {
const char *snap_name = it->empty() ? NULL : it->c_str();
ASSERT_EQ(0, librbd::api::Image<>::snap_set(
ictx2, cls::rbd::UserSnapshotNamespace(), snap_name));
ASSERT_EQ(256,
api::Io<>::read(*ictx2, 0, 256,
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(bl.contents_equal(read_bl));
ASSERT_EQ(256,
api::Io<>::read(*ictx2, 1024, 256,
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(bl.contents_equal(read_bl));
ASSERT_EQ(256,
api::Io<>::read(*ictx2, 256, 256,
librbd::io::ReadResult{read_result}, 0));
if (snap_name == NULL) {
ASSERT_TRUE(bl.contents_equal(read_bl));
} else {
ASSERT_TRUE(read_bl.is_zero());
}
// verify sparseness was preserved
{
librados::IoCtx io_ctx;
io_ctx.dup(m_ioctx);
librados::Rados rados(io_ctx);
EXPECT_EQ(0, rados.conf_set("rbd_cache", "false"));
EXPECT_EQ(0, rados.conf_set("rbd_sparse_read_threshold_bytes", "256"));
auto ictx3 = new librbd::ImageCtx(clone_name, "", snap_name, io_ctx,
true);
ASSERT_EQ(0, ictx3->state->open(0));
BOOST_SCOPE_EXIT(ictx3) {
ictx3->state->close();
} BOOST_SCOPE_EXIT_END;
std::vector<std::pair<uint64_t, uint64_t>> expected_m;
bufferlist expected_bl;
if (ictx3->enable_sparse_copyup && sparse_read_supported) {
if (snap_name == NULL) {
expected_m = {{0, 512}, {1024, 256}};
expected_bl.append(std::string(256 * 3, '1'));
} else {
expected_m = {{0, 256}, {1024, 256}};
expected_bl.append(std::string(256 * 2, '1'));
}
} else {
expected_m = {{0, 1024 + 256}};
if (snap_name == NULL) {
expected_bl.append(std::string(256 * 2, '1'));
expected_bl.append(std::string(256 * 2, '\0'));
expected_bl.append(std::string(256 * 1, '1'));
} else {
expected_bl.append(std::string(256 * 1, '1'));
expected_bl.append(std::string(256 * 3, '\0'));
expected_bl.append(std::string(256 * 1, '1'));
}
}
std::vector<std::pair<uint64_t, uint64_t>> read_m;
librbd::io::ReadResult sparse_read_result{&read_m, &read_bl};
EXPECT_EQ(1024 + 256,
api::Io<>::read(*ictx3, 0, 1024 + 256,
librbd::io::ReadResult{sparse_read_result}, 0));
EXPECT_EQ(expected_m, read_m);
EXPECT_TRUE(expected_bl.contents_equal(read_bl));
}
// verify the object map was properly updated
if ((ictx2->features & RBD_FEATURE_OBJECT_MAP) != 0) {
uint8_t state = OBJECT_EXISTS;
if ((ictx2->features & RBD_FEATURE_FAST_DIFF) != 0 &&
it != snaps.begin() && snap_name != NULL) {
state = OBJECT_EXISTS_CLEAN;
}
librbd::ObjectMap<> *object_map = new librbd::ObjectMap<>(*ictx2, ictx2->snap_id);
C_SaferCond ctx;
object_map->open(&ctx);
ASSERT_EQ(0, ctx.wait());
std::shared_lock image_locker{ictx2->image_lock};
ASSERT_EQ(state, (*object_map)[0]);
object_map->put();
}
}
}
TEST_F(TestInternal, SnapshotCopyupZeros)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
// create an empty clone
ASSERT_EQ(0, snap_create(*ictx, "snap1"));
ASSERT_EQ(0,
ictx->operations->snap_protect(cls::rbd::UserSnapshotNamespace(),
"snap1"));
uint64_t features;
ASSERT_EQ(0, librbd::get_features(ictx, &features));
std::string clone_name = get_temp_image_name();
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap1", m_ioctx,
clone_name.c_str(), features, &order, 0, 0));
librbd::ImageCtx *ictx2;
ASSERT_EQ(0, open_image(clone_name, &ictx2));
ASSERT_EQ(0, snap_create(*ictx2, "snap1"));
bufferlist bl;
bl.append(std::string(256, '1'));
ASSERT_EQ(256, api::Io<>::write(*ictx2, 256, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ(0, flush_writeback_cache(ictx2));
librados::IoCtx snap_ctx;
snap_ctx.dup(ictx2->data_ctx);
snap_ctx.snap_set_read(CEPH_SNAPDIR);
librados::snap_set_t snap_set;
ASSERT_EQ(0, snap_ctx.list_snaps(ictx2->get_object_name(0), &snap_set));
// verify that snapshot wasn't affected
ASSERT_EQ(1U, snap_set.clones.size());
ASSERT_EQ(CEPH_NOSNAP, snap_set.clones[0].cloneid);
bufferptr read_ptr(256);
bufferlist read_bl;
read_bl.push_back(read_ptr);
std::list<std::string> snaps = {"snap1", ""};
librbd::io::ReadResult read_result{&read_bl};
for (std::list<std::string>::iterator it = snaps.begin();
it != snaps.end(); ++it) {
const char *snap_name = it->empty() ? NULL : it->c_str();
ASSERT_EQ(0, librbd::api::Image<>::snap_set(
ictx2, cls::rbd::UserSnapshotNamespace(), snap_name));
ASSERT_EQ(256,
api::Io<>::read(*ictx2, 0, 256,
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(read_bl.is_zero());
ASSERT_EQ(256,
api::Io<>::read(*ictx2, 256, 256,
librbd::io::ReadResult{read_result}, 0));
if (snap_name == NULL) {
ASSERT_TRUE(bl.contents_equal(read_bl));
} else {
ASSERT_TRUE(read_bl.is_zero());
}
// verify that only HEAD object map was updated
if ((ictx2->features & RBD_FEATURE_OBJECT_MAP) != 0) {
uint8_t state = OBJECT_EXISTS;
if (snap_name != NULL) {
state = OBJECT_NONEXISTENT;
}
librbd::ObjectMap<> *object_map = new librbd::ObjectMap<>(*ictx2, ictx2->snap_id);
C_SaferCond ctx;
object_map->open(&ctx);
ASSERT_EQ(0, ctx.wait());
std::shared_lock image_locker{ictx2->image_lock};
ASSERT_EQ(state, (*object_map)[0]);
object_map->put();
}
}
}
TEST_F(TestInternal, SnapshotCopyupZerosMigration)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
uint64_t features;
ASSERT_EQ(0, librbd::get_features(ictx, &features));
close_image(ictx);
// migrate an empty image
std::string dst_name = get_temp_image_name();
librbd::ImageOptions dst_opts;
dst_opts.set(RBD_IMAGE_OPTION_FEATURES, features);
ASSERT_EQ(0, librbd::api::Migration<>::prepare(m_ioctx, m_image_name,
m_ioctx, dst_name,
dst_opts));
librbd::ImageCtx *ictx2;
ASSERT_EQ(0, open_image(dst_name, &ictx2));
ASSERT_EQ(0, snap_create(*ictx2, "snap1"));
bufferlist bl;
bl.append(std::string(256, '1'));
ASSERT_EQ(256, api::Io<>::write(*ictx2, 256, bl.length(), bufferlist{bl}, 0));
librados::IoCtx snap_ctx;
snap_ctx.dup(ictx2->data_ctx);
snap_ctx.snap_set_read(CEPH_SNAPDIR);
librados::snap_set_t snap_set;
ASSERT_EQ(0, snap_ctx.list_snaps(ictx2->get_object_name(0), &snap_set));
// verify that snapshot wasn't affected
ASSERT_EQ(1U, snap_set.clones.size());
ASSERT_EQ(CEPH_NOSNAP, snap_set.clones[0].cloneid);
bufferptr read_ptr(256);
bufferlist read_bl;
read_bl.push_back(read_ptr);
std::list<std::string> snaps = {"snap1", ""};
librbd::io::ReadResult read_result{&read_bl};
for (std::list<std::string>::iterator it = snaps.begin();
it != snaps.end(); ++it) {
const char *snap_name = it->empty() ? NULL : it->c_str();
ASSERT_EQ(0, librbd::api::Image<>::snap_set(
ictx2, cls::rbd::UserSnapshotNamespace(), snap_name));
ASSERT_EQ(256,
api::Io<>::read(*ictx2, 0, 256,
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(read_bl.is_zero());
ASSERT_EQ(256,
api::Io<>::read(*ictx2, 256, 256,
librbd::io::ReadResult{read_result}, 0));
if (snap_name == NULL) {
ASSERT_TRUE(bl.contents_equal(read_bl));
} else {
ASSERT_TRUE(read_bl.is_zero());
}
// verify that only HEAD object map was updated
if ((ictx2->features & RBD_FEATURE_OBJECT_MAP) != 0) {
uint8_t state = OBJECT_EXISTS;
if (snap_name != NULL) {
state = OBJECT_NONEXISTENT;
}
librbd::ObjectMap<> *object_map = new librbd::ObjectMap<>(*ictx2, ictx2->snap_id);
C_SaferCond ctx;
object_map->open(&ctx);
ASSERT_EQ(0, ctx.wait());
std::shared_lock image_locker{ictx2->image_lock};
ASSERT_EQ(state, (*object_map)[0]);
object_map->put();
}
}
}
TEST_F(TestInternal, ResizeCopyup)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
m_image_name = get_temp_image_name();
m_image_size = 1 << 14;
uint64_t features = 0;
::get_features(&features);
int order = 12;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, m_image_name.c_str(), m_image_size,
features, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bufferlist bl;
bl.append(std::string(4096, '1'));
for (size_t i = 0; i < m_image_size; i += bl.length()) {
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::write(*ictx, i, bl.length(), bufferlist{bl}, 0));
}
ASSERT_EQ(0, snap_create(*ictx, "snap1"));
ASSERT_EQ(0,
ictx->operations->snap_protect(cls::rbd::UserSnapshotNamespace(),
"snap1"));
std::string clone_name = get_temp_image_name();
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap1", m_ioctx,
clone_name.c_str(), features, &order, 0, 0));
librbd::ImageCtx *ictx2;
ASSERT_EQ(0, open_image(clone_name, &ictx2));
ASSERT_EQ(0, snap_create(*ictx2, "snap1"));
bufferptr read_ptr(bl.length());
bufferlist read_bl;
read_bl.push_back(read_ptr);
// verify full / partial object removal properly copyup
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, ictx2->operations->resize(m_image_size - (1 << order) - 32,
true, no_op));
ASSERT_EQ(0, ictx2->operations->resize(m_image_size - (2 << order) - 32,
true, no_op));
ASSERT_EQ(0, librbd::api::Image<>::snap_set(ictx2,
cls::rbd::UserSnapshotNamespace(),
"snap1"));
{
// hide the parent from the snapshot
std::unique_lock image_locker{ictx2->image_lock};
ictx2->snap_info.begin()->second.parent = librbd::ParentImageInfo();
}
librbd::io::ReadResult read_result{&read_bl};
for (size_t i = 2 << order; i < m_image_size; i += bl.length()) {
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::read(*ictx2, i, bl.length(),
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(bl.contents_equal(read_bl));
}
}
TEST_F(TestInternal, DiscardCopyup)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
CephContext* cct = reinterpret_cast<CephContext*>(_rados.cct());
REQUIRE(!cct->_conf.get_val<bool>("rbd_skip_partial_discard"));
m_image_name = get_temp_image_name();
m_image_size = 1 << 14;
uint64_t features = 0;
::get_features(&features);
int order = 12;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, m_image_name.c_str(), m_image_size,
features, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bufferlist bl;
bl.append(std::string(4096, '1'));
for (size_t i = 0; i < m_image_size; i += bl.length()) {
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::write(*ictx, i, bl.length(), bufferlist{bl}, 0));
}
ASSERT_EQ(0, snap_create(*ictx, "snap1"));
ASSERT_EQ(0,
ictx->operations->snap_protect(cls::rbd::UserSnapshotNamespace(),
"snap1"));
std::string clone_name = get_temp_image_name();
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap1", m_ioctx,
clone_name.c_str(), features, &order, 0, 0));
librbd::ImageCtx *ictx2;
ASSERT_EQ(0, open_image(clone_name, &ictx2));
ASSERT_EQ(0, snap_create(*ictx2, "snap1"));
bufferptr read_ptr(bl.length());
bufferlist read_bl;
read_bl.push_back(read_ptr);
ASSERT_EQ(static_cast<int>(m_image_size - 64),
api::Io<>::discard(*ictx2, 32, m_image_size - 64, false));
ASSERT_EQ(0, librbd::api::Image<>::snap_set(ictx2,
cls::rbd::UserSnapshotNamespace(),
"snap1"));
{
// hide the parent from the snapshot
std::unique_lock image_locker{ictx2->image_lock};
ictx2->snap_info.begin()->second.parent = librbd::ParentImageInfo();
}
librbd::io::ReadResult read_result{&read_bl};
for (size_t i = 0; i < m_image_size; i += bl.length()) {
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::read(*ictx2, i, bl.length(),
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(bl.contents_equal(read_bl));
}
}
TEST_F(TestInternal, ImageOptions) {
rbd_image_options_t opts1 = NULL, opts2 = NULL;
uint64_t uint64_val1 = 10, uint64_val2 = 0;
std::string string_val1;
librbd::image_options_create(&opts1);
ASSERT_NE((rbd_image_options_t)NULL, opts1);
ASSERT_TRUE(librbd::image_options_is_empty(opts1));
ASSERT_EQ(-EINVAL, librbd::image_options_get(opts1, RBD_IMAGE_OPTION_FEATURES,
&string_val1));
ASSERT_EQ(-ENOENT, librbd::image_options_get(opts1, RBD_IMAGE_OPTION_FEATURES,
&uint64_val1));
ASSERT_EQ(-EINVAL, librbd::image_options_set(opts1, RBD_IMAGE_OPTION_FEATURES,
string_val1));
ASSERT_EQ(0, librbd::image_options_set(opts1, RBD_IMAGE_OPTION_FEATURES,
uint64_val1));
ASSERT_FALSE(librbd::image_options_is_empty(opts1));
ASSERT_EQ(0, librbd::image_options_get(opts1, RBD_IMAGE_OPTION_FEATURES,
&uint64_val2));
ASSERT_EQ(uint64_val1, uint64_val2);
librbd::image_options_create_ref(&opts2, opts1);
ASSERT_NE((rbd_image_options_t)NULL, opts2);
ASSERT_FALSE(librbd::image_options_is_empty(opts2));
uint64_val2 = 0;
ASSERT_NE(uint64_val1, uint64_val2);
ASSERT_EQ(0, librbd::image_options_get(opts2, RBD_IMAGE_OPTION_FEATURES,
&uint64_val2));
ASSERT_EQ(uint64_val1, uint64_val2);
uint64_val2++;
ASSERT_NE(uint64_val1, uint64_val2);
ASSERT_EQ(-ENOENT, librbd::image_options_get(opts1, RBD_IMAGE_OPTION_ORDER,
&uint64_val1));
ASSERT_EQ(-ENOENT, librbd::image_options_get(opts2, RBD_IMAGE_OPTION_ORDER,
&uint64_val2));
ASSERT_EQ(0, librbd::image_options_set(opts2, RBD_IMAGE_OPTION_ORDER,
uint64_val2));
ASSERT_EQ(0, librbd::image_options_get(opts1, RBD_IMAGE_OPTION_ORDER,
&uint64_val1));
ASSERT_EQ(0, librbd::image_options_get(opts2, RBD_IMAGE_OPTION_ORDER,
&uint64_val2));
ASSERT_EQ(uint64_val1, uint64_val2);
librbd::image_options_destroy(opts1);
uint64_val2++;
ASSERT_NE(uint64_val1, uint64_val2);
ASSERT_EQ(0, librbd::image_options_get(opts2, RBD_IMAGE_OPTION_ORDER,
&uint64_val2));
ASSERT_EQ(uint64_val1, uint64_val2);
ASSERT_EQ(0, librbd::image_options_unset(opts2, RBD_IMAGE_OPTION_ORDER));
ASSERT_EQ(-ENOENT, librbd::image_options_unset(opts2, RBD_IMAGE_OPTION_ORDER));
librbd::image_options_clear(opts2);
ASSERT_EQ(-ENOENT, librbd::image_options_get(opts2, RBD_IMAGE_OPTION_FEATURES,
&uint64_val2));
ASSERT_TRUE(librbd::image_options_is_empty(opts2));
librbd::image_options_destroy(opts2);
}
TEST_F(TestInternal, WriteFullCopyup) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, ictx->operations->resize(1 << ictx->order, true, no_op));
bufferlist bl;
bl.append(std::string(1 << ictx->order, '1'));
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::write(*ictx, 0, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ(0, flush_writeback_cache(ictx));
ASSERT_EQ(0, create_snapshot("snap1", true));
std::string clone_name = get_temp_image_name();
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap1", m_ioctx,
clone_name.c_str(), ictx->features, &order, 0, 0));
TestInternal *parent = this;
librbd::ImageCtx *ictx2 = NULL;
BOOST_SCOPE_EXIT( (&m_ioctx) (clone_name) (parent) (&ictx2) ) {
if (ictx2 != NULL) {
ictx2->operations->snap_remove(cls::rbd::UserSnapshotNamespace(),
"snap1");
parent->close_image(ictx2);
}
librbd::NoOpProgressContext remove_no_op;
ASSERT_EQ(0, librbd::api::Image<>::remove(m_ioctx, clone_name,
remove_no_op));
} BOOST_SCOPE_EXIT_END;
ASSERT_EQ(0, open_image(clone_name, &ictx2));
ASSERT_EQ(0, ictx2->operations->snap_create(cls::rbd::UserSnapshotNamespace(),
"snap1", 0, no_op));
bufferlist write_full_bl;
write_full_bl.append(std::string(1 << ictx2->order, '2'));
ASSERT_EQ((ssize_t)write_full_bl.length(),
api::Io<>::write(*ictx2, 0, write_full_bl.length(),
bufferlist{write_full_bl}, 0));
ASSERT_EQ(0, ictx2->operations->flatten(no_op));
bufferptr read_ptr(bl.length());
bufferlist read_bl;
read_bl.push_back(read_ptr);
librbd::io::ReadResult read_result{&read_bl};
ASSERT_EQ((ssize_t)read_bl.length(),
api::Io<>::read(*ictx2, 0, read_bl.length(),
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(write_full_bl.contents_equal(read_bl));
ASSERT_EQ(0, librbd::api::Image<>::snap_set(ictx2,
cls::rbd::UserSnapshotNamespace(),
"snap1"));
ASSERT_EQ((ssize_t)read_bl.length(),
api::Io<>::read(*ictx2, 0, read_bl.length(),
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(bl.contents_equal(read_bl));
}
static int iterate_cb(uint64_t off, size_t len, int exists, void *arg)
{
interval_set<uint64_t> *diff = static_cast<interval_set<uint64_t> *>(arg);
diff->insert(off, len);
return 0;
}
TEST_F(TestInternal, DiffIterateCloneOverwrite) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::RBD rbd;
librbd::Image image;
uint64_t size = 20 << 20;
int order = 0;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
bufferlist bl;
bl.append(std::string(4096, '1'));
ASSERT_EQ(4096, image.write(0, 4096, bl));
interval_set<uint64_t> one;
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, false, false, iterate_cb,
(void *)&one));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
std::string clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "one"));
ASSERT_EQ(0,
ictx->operations->snap_protect(cls::rbd::UserSnapshotNamespace(),
"one"));
// Simulate a client that doesn't support deep flatten (old librbd / krbd)
// which will copy up the full object from the parent
std::string oid = ictx->object_prefix + ".0000000000000000";
librados::IoCtx io_ctx;
io_ctx.dup(m_ioctx);
io_ctx.selfmanaged_snap_set_write_ctx(ictx->snapc.seq, ictx->snaps);
ASSERT_EQ(0, io_ctx.write(oid, bl, 4096, 4096));
interval_set<uint64_t> diff;
ASSERT_EQ(0, librbd::api::Image<>::snap_set(ictx,
cls::rbd::UserSnapshotNamespace(),
"one"));
ASSERT_EQ(0, librbd::api::DiffIterate<>::diff_iterate(
ictx, cls::rbd::UserSnapshotNamespace(), nullptr, 0, size, true, false,
iterate_cb, (void *)&diff));
ASSERT_EQ(one, diff);
}
TEST_F(TestInternal, TestCoR)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
std::string config_value;
ASSERT_EQ(0, _rados.conf_get("rbd_clone_copy_on_read", config_value));
if (config_value == "false") {
GTEST_SKIP() << "Skipping due to disabled copy-on-read";
}
m_image_name = get_temp_image_name();
m_image_size = 4 << 20;
int order = 12; // smallest object size is 4K
uint64_t features;
ASSERT_TRUE(::get_features(&features));
ASSERT_EQ(0, create_image_full_pp(m_rbd, m_ioctx, m_image_name, m_image_size,
features, false, &order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
librbd::image_info_t info;
ASSERT_EQ(0, image.stat(info, sizeof(info)));
const int object_num = info.size / info.obj_size;
printf("made parent image \"%s\": %ldK (%d * %" PRIu64 "K)\n", m_image_name.c_str(),
(unsigned long)m_image_size, object_num, info.obj_size/1024);
// write something into parent
char test_data[TEST_IO_SIZE + 1];
for (int i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
// generate a random map which covers every objects with random
// offset
map<uint64_t, uint64_t> write_tracker;
generate_random_iomap(image, object_num, info.obj_size, 100, write_tracker);
printf("generated random write map:\n");
for (map<uint64_t, uint64_t>::iterator itr = write_tracker.begin();
itr != write_tracker.end(); ++itr)
printf("\t [%-8lu, %-8lu]\n",
(unsigned long)itr->first, (unsigned long)itr->second);
bufferlist bl;
bl.append(test_data, TEST_IO_SIZE);
printf("write data based on random map\n");
for (map<uint64_t, uint64_t>::iterator itr = write_tracker.begin();
itr != write_tracker.end(); ++itr) {
printf("\twrite object-%-4lu\t\n", (unsigned long)itr->first);
ASSERT_EQ(TEST_IO_SIZE, image.write(itr->second, TEST_IO_SIZE, bl));
}
ASSERT_EQ(0, image.flush());
bufferlist readbl;
printf("verify written data by reading\n");
{
map<uint64_t, uint64_t>::iterator itr = write_tracker.begin();
printf("\tread object-%-4lu\n", (unsigned long)itr->first);
ASSERT_EQ(TEST_IO_SIZE, image.read(itr->second, TEST_IO_SIZE, readbl));
ASSERT_TRUE(readbl.contents_equal(bl));
}
int64_t data_pool_id = image.get_data_pool_id();
rados_ioctx_t d_ioctx;
ASSERT_EQ(0, rados_wait_for_latest_osdmap(_cluster));
ASSERT_EQ(0, rados_ioctx_create2(_cluster, data_pool_id, &d_ioctx));
std::string block_name_prefix = image.get_block_name_prefix() + ".";
const char *entry;
rados_list_ctx_t list_ctx;
set<string> obj_checker;
ASSERT_EQ(0, rados_nobjects_list_open(d_ioctx, &list_ctx));
while (rados_nobjects_list_next(list_ctx, &entry, NULL, NULL) != -ENOENT) {
if (boost::starts_with(entry, block_name_prefix)) {
const char *block_name_suffix = entry + block_name_prefix.length();
obj_checker.insert(block_name_suffix);
}
}
rados_nobjects_list_close(list_ctx);
std::string snapname = "snap";
std::string clonename = get_temp_image_name();
ASSERT_EQ(0, image.snap_create(snapname.c_str()));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, m_image_name.c_str(), snapname.c_str()));
ASSERT_EQ(0, image.snap_protect(snapname.c_str()));
printf("made snapshot \"%s@parent_snap\" and protect it\n", m_image_name.c_str());
ASSERT_EQ(0, clone_image_pp(m_rbd, image, m_ioctx, m_image_name.c_str(), snapname.c_str(),
m_ioctx, clonename.c_str(), features));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, clonename.c_str(), NULL));
printf("made and opened clone \"%s\"\n", clonename.c_str());
printf("read from \"child\"\n");
{
map<uint64_t, uint64_t>::iterator itr = write_tracker.begin();
printf("\tread object-%-4lu\n", (unsigned long)itr->first);
ASSERT_EQ(TEST_IO_SIZE, image.read(itr->second, TEST_IO_SIZE, readbl));
ASSERT_TRUE(readbl.contents_equal(bl));
}
for (map<uint64_t, uint64_t>::iterator itr = write_tracker.begin();
itr != write_tracker.end(); ++itr) {
printf("\tread object-%-4lu\n", (unsigned long)itr->first);
ASSERT_EQ(TEST_IO_SIZE, image.read(itr->second, TEST_IO_SIZE, readbl));
ASSERT_TRUE(readbl.contents_equal(bl));
}
printf("read again reversely\n");
for (map<uint64_t, uint64_t>::iterator itr = --write_tracker.end();
itr != write_tracker.begin(); --itr) {
printf("\tread object-%-4lu\n", (unsigned long)itr->first);
ASSERT_EQ(TEST_IO_SIZE, image.read(itr->second, TEST_IO_SIZE, readbl));
ASSERT_TRUE(readbl.contents_equal(bl));
}
// close child to flush all copy-on-read
ASSERT_EQ(0, image.close());
printf("check whether child image has the same set of objects as parent\n");
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, clonename.c_str(), NULL));
block_name_prefix = image.get_block_name_prefix() + ".";
ASSERT_EQ(0, rados_nobjects_list_open(d_ioctx, &list_ctx));
while (rados_nobjects_list_next(list_ctx, &entry, NULL, NULL) != -ENOENT) {
if (boost::starts_with(entry, block_name_prefix)) {
const char *block_name_suffix = entry + block_name_prefix.length();
set<string>::iterator it = obj_checker.find(block_name_suffix);
ASSERT_TRUE(it != obj_checker.end());
obj_checker.erase(it);
}
}
rados_nobjects_list_close(list_ctx);
ASSERT_TRUE(obj_checker.empty());
ASSERT_EQ(0, image.close());
rados_ioctx_destroy(d_ioctx);
}
TEST_F(TestInternal, FlattenNoEmptyObjects)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
m_image_name = get_temp_image_name();
m_image_size = 4 << 20;
int order = 12; // smallest object size is 4K
uint64_t features;
ASSERT_TRUE(::get_features(&features));
ASSERT_EQ(0, create_image_full_pp(m_rbd, m_ioctx, m_image_name, m_image_size,
features, false, &order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
librbd::image_info_t info;
ASSERT_EQ(0, image.stat(info, sizeof(info)));
const int object_num = info.size / info.obj_size;
printf("made parent image \"%s\": %" PRIu64 "K (%d * %" PRIu64 "K)\n",
m_image_name.c_str(), m_image_size, object_num, info.obj_size/1024);
// write something into parent
char test_data[TEST_IO_SIZE + 1];
for (int i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
// generate a random map which covers every objects with random
// offset
map<uint64_t, uint64_t> write_tracker;
generate_random_iomap(image, object_num, info.obj_size, 100, write_tracker);
printf("generated random write map:\n");
for (map<uint64_t, uint64_t>::iterator itr = write_tracker.begin();
itr != write_tracker.end(); ++itr)
printf("\t [%-8lu, %-8lu]\n",
(unsigned long)itr->first, (unsigned long)itr->second);
bufferlist bl;
bl.append(test_data, TEST_IO_SIZE);
printf("write data based on random map\n");
for (map<uint64_t, uint64_t>::iterator itr = write_tracker.begin();
itr != write_tracker.end(); ++itr) {
printf("\twrite object-%-4lu\t\n", (unsigned long)itr->first);
ASSERT_EQ(TEST_IO_SIZE, image.write(itr->second, TEST_IO_SIZE, bl));
}
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
bufferlist readbl;
printf("verify written data by reading\n");
{
map<uint64_t, uint64_t>::iterator itr = write_tracker.begin();
printf("\tread object-%-4lu\n", (unsigned long)itr->first);
ASSERT_EQ(TEST_IO_SIZE, image.read(itr->second, TEST_IO_SIZE, readbl));
ASSERT_TRUE(readbl.contents_equal(bl));
}
int64_t data_pool_id = image.get_data_pool_id();
rados_ioctx_t d_ioctx;
ASSERT_EQ(0, rados_wait_for_latest_osdmap(_cluster));
ASSERT_EQ(0, rados_ioctx_create2(_cluster, data_pool_id, &d_ioctx));
std::string block_name_prefix = image.get_block_name_prefix() + ".";
const char *entry;
rados_list_ctx_t list_ctx;
set<string> obj_checker;
ASSERT_EQ(0, rados_nobjects_list_open(d_ioctx, &list_ctx));
while (rados_nobjects_list_next(list_ctx, &entry, NULL, NULL) != -ENOENT) {
if (boost::starts_with(entry, block_name_prefix)) {
const char *block_name_suffix = entry + block_name_prefix.length();
obj_checker.insert(block_name_suffix);
}
}
rados_nobjects_list_close(list_ctx);
std::string snapname = "snap";
std::string clonename = get_temp_image_name();
ASSERT_EQ(0, image.snap_create(snapname.c_str()));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, m_image_name.c_str(), snapname.c_str()));
ASSERT_EQ(0, image.snap_protect(snapname.c_str()));
printf("made snapshot \"%s@parent_snap\" and protect it\n", m_image_name.c_str());
ASSERT_EQ(0, clone_image_pp(m_rbd, image, m_ioctx, m_image_name.c_str(), snapname.c_str(),
m_ioctx, clonename.c_str(), features));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, clonename.c_str(), NULL));
printf("made and opened clone \"%s\"\n", clonename.c_str());
printf("flattening clone: \"%s\"\n", clonename.c_str());
ASSERT_EQ(0, image.flatten());
printf("check whether child image has the same set of objects as parent\n");
block_name_prefix = image.get_block_name_prefix() + ".";
ASSERT_EQ(0, rados_nobjects_list_open(d_ioctx, &list_ctx));
while (rados_nobjects_list_next(list_ctx, &entry, NULL, NULL) != -ENOENT) {
if (boost::starts_with(entry, block_name_prefix)) {
const char *block_name_suffix = entry + block_name_prefix.length();
set<string>::iterator it = obj_checker.find(block_name_suffix);
ASSERT_TRUE(it != obj_checker.end());
obj_checker.erase(it);
}
}
rados_nobjects_list_close(list_ctx);
ASSERT_TRUE(obj_checker.empty());
ASSERT_EQ(0, image.close());
rados_ioctx_destroy(d_ioctx);
}
TEST_F(TestInternal, PoolMetadataConfApply) {
REQUIRE_FORMAT_V2();
librbd::api::PoolMetadata<>::remove(m_ioctx, "conf_rbd_cache");
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool cache = ictx->cache;
std::string rbd_conf_cache = cache ? "true" : "false";
std::string new_rbd_conf_cache = !cache ? "true" : "false";
ASSERT_EQ(0, librbd::api::PoolMetadata<>::set(m_ioctx, "conf_rbd_cache",
new_rbd_conf_cache));
ASSERT_EQ(0, ictx->state->refresh());
ASSERT_EQ(!cache, ictx->cache);
ASSERT_EQ(0, ictx->operations->metadata_set("conf_rbd_cache",
rbd_conf_cache));
ASSERT_EQ(cache, ictx->cache);
ASSERT_EQ(0, ictx->operations->metadata_remove("conf_rbd_cache"));
ASSERT_EQ(!cache, ictx->cache);
ASSERT_EQ(0, librbd::api::PoolMetadata<>::remove(m_ioctx, "conf_rbd_cache"));
ASSERT_EQ(0, ictx->state->refresh());
ASSERT_EQ(cache, ictx->cache);
close_image(ictx);
ASSERT_EQ(0, librbd::api::PoolMetadata<>::set(m_ioctx,
"conf_rbd_default_order",
"17"));
ASSERT_EQ(0, librbd::api::PoolMetadata<>::set(m_ioctx,
"conf_rbd_journal_order",
"13"));
std::string image_name = get_temp_image_name();
int order = 0;
uint64_t features;
ASSERT_TRUE(::get_features(&features));
ASSERT_EQ(0, create_image_full_pp(m_rbd, m_ioctx, image_name, m_image_size,
features, false, &order));
ASSERT_EQ(0, open_image(image_name, &ictx));
ASSERT_EQ(ictx->order, 17);
ASSERT_EQ(ictx->config.get_val<uint64_t>("rbd_journal_order"), 13U);
if (is_feature_enabled(RBD_FEATURE_JOURNALING)) {
uint8_t order;
uint8_t splay_width;
int64_t pool_id;
C_SaferCond cond;
cls::journal::client::get_immutable_metadata(m_ioctx, "journal." + ictx->id,
&order, &splay_width, &pool_id,
&cond);
ASSERT_EQ(0, cond.wait());
ASSERT_EQ(order, 13);
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
ASSERT_EQ(0, librbd::api::PoolMetadata<>::set(m_ioctx,
"conf_rbd_journal_order",
"14"));
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
true));
ASSERT_EQ(ictx->config.get_val<uint64_t>("rbd_journal_order"), 14U);
C_SaferCond cond1;
cls::journal::client::get_immutable_metadata(m_ioctx, "journal." + ictx->id,
&order, &splay_width, &pool_id,
&cond1);
ASSERT_EQ(0, cond1.wait());
ASSERT_EQ(order, 14);
}
ASSERT_EQ(0, librbd::api::PoolMetadata<>::remove(m_ioctx,
"conf_rbd_default_order"));
ASSERT_EQ(0, librbd::api::PoolMetadata<>::remove(m_ioctx,
"conf_rbd_journal_order"));
}
TEST_F(TestInternal, Sparsify) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool sparsify_supported = is_sparsify_supported(ictx->data_ctx,
ictx->get_object_name(10));
bool sparse_read_supported = is_sparse_read_supported(
ictx->data_ctx, ictx->get_object_name(10));
std::cout << "sparsify_supported=" << sparsify_supported << std::endl;
std::cout << "sparse_read_supported=" << sparse_read_supported << std::endl;
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, ictx->operations->resize((1 << ictx->order) * 20, true, no_op));
bufferlist bl;
bl.append(std::string(4096, '\0'));
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::write(*ictx, 0, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::write(*ictx, (1 << ictx->order) * 1 + 512,
bl.length(), bufferlist{bl}, 0));
bl.append(std::string(4096, '1'));
bl.append(std::string(4096, '\0'));
bl.append(std::string(4096, '2'));
bl.append(std::string(4096 - 1, '\0'));
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::write(*ictx, (1 << ictx->order) * 10, bl.length(),
bufferlist{bl}, 0));
bufferlist bl2;
bl2.append(std::string(4096 - 1, '\0'));
ASSERT_EQ((ssize_t)bl2.length(),
api::Io<>::write(*ictx, (1 << ictx->order) * 10 + 4096 * 10,
bl2.length(), bufferlist{bl2}, 0));
ASSERT_EQ(0, flush_writeback_cache(ictx));
ASSERT_EQ(0, ictx->operations->sparsify(4096, no_op));
bufferptr read_ptr(bl.length());
bufferlist read_bl;
read_bl.push_back(read_ptr);
librbd::io::ReadResult read_result{&read_bl};
ASSERT_EQ((ssize_t)read_bl.length(),
api::Io<>::read(*ictx, (1 << ictx->order) * 10, read_bl.length(),
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(bl.contents_equal(read_bl));
std::string oid = ictx->get_object_name(0);
uint64_t size;
ASSERT_EQ(-ENOENT, ictx->data_ctx.stat(oid, &size, NULL));
oid = ictx->get_object_name(1);
ASSERT_EQ(-ENOENT, ictx->data_ctx.stat(oid, &size, NULL));
oid = ictx->get_object_name(10);
std::map<uint64_t, uint64_t> m;
std::map<uint64_t, uint64_t> expected_m;
auto read_len = bl.length();
bl.clear();
if (sparsify_supported && sparse_read_supported) {
expected_m = {{4096 * 1, 4096}, {4096 * 3, 4096}};
bl.append(std::string(4096, '1'));
bl.append(std::string(4096, '2'));
} else {
expected_m = {{0, 4096 * 4}};
bl.append(std::string(4096, '\0'));
bl.append(std::string(4096, '1'));
bl.append(std::string(4096, '\0'));
bl.append(std::string(4096, '2'));
}
read_bl.clear();
EXPECT_EQ(static_cast<int>(expected_m.size()),
ictx->data_ctx.sparse_read(oid, m, read_bl, read_len, 0));
EXPECT_EQ(m, expected_m);
EXPECT_TRUE(bl.contents_equal(read_bl));
}
TEST_F(TestInternal, SparsifyClone) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
bool sparsify_supported = is_sparsify_supported(ictx->data_ctx,
ictx->get_object_name(10));
std::cout << "sparsify_supported=" << sparsify_supported << std::endl;
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, ictx->operations->resize((1 << ictx->order) * 10, true, no_op));
ASSERT_EQ(0, create_snapshot("snap", true));
std::string clone_name = get_temp_image_name();
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap", m_ioctx,
clone_name.c_str(), ictx->features, &order, 0, 0));
close_image(ictx);
ASSERT_EQ(0, open_image(clone_name, &ictx));
BOOST_SCOPE_EXIT_ALL(this, &ictx, clone_name) {
close_image(ictx);
librbd::NoOpProgressContext no_op;
EXPECT_EQ(0, librbd::api::Image<>::remove(m_ioctx, clone_name, no_op));
};
ASSERT_EQ(0, ictx->operations->resize((1 << ictx->order) * 20, true, no_op));
bufferlist bl;
bl.append(std::string(4096, '\0'));
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::write(*ictx, 0, bl.length(), bufferlist{bl}, 0));
bl.append(std::string(4096, '1'));
bl.append(std::string(4096, '\0'));
bl.append(std::string(4096, '2'));
bl.append(std::string(4096, '\0'));
ASSERT_EQ((ssize_t)bl.length(),
api::Io<>::write(*ictx, (1 << ictx->order) * 10, bl.length(),
bufferlist{bl}, 0));
ASSERT_EQ(0, flush_writeback_cache(ictx));
ASSERT_EQ(0, ictx->operations->sparsify(4096, no_op));
bufferptr read_ptr(bl.length());
bufferlist read_bl;
read_bl.push_back(read_ptr);
librbd::io::ReadResult read_result{&read_bl};
ASSERT_EQ((ssize_t)read_bl.length(),
api::Io<>::read(*ictx, (1 << ictx->order) * 10, read_bl.length(),
librbd::io::ReadResult{read_result}, 0));
ASSERT_TRUE(bl.contents_equal(read_bl));
std::string oid = ictx->get_object_name(0);
uint64_t size;
ASSERT_EQ(0, ictx->data_ctx.stat(oid, &size, NULL));
ASSERT_EQ(0, ictx->data_ctx.read(oid, read_bl, 4096, 0));
}
TEST_F(TestInternal, MissingDataPool) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap1"));
std::string header_oid = ictx->header_oid;
close_image(ictx);
// emulate non-existent data pool
int64_t pool_id = 1234;
std::string pool_name;
int r;
while ((r = _rados.pool_reverse_lookup(pool_id, &pool_name)) == 0) {
pool_id++;
}
ASSERT_EQ(r, -ENOENT);
bufferlist bl;
using ceph::encode;
encode(pool_id, bl);
ASSERT_EQ(0, m_ioctx.omap_set(header_oid, {{"data_pool_id", bl}}));
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_FALSE(ictx->data_ctx.is_valid());
ASSERT_EQ(pool_id, librbd::api::Image<>::get_data_pool_id(ictx));
librbd::image_info_t info;
ASSERT_EQ(0, librbd::info(ictx, info, sizeof(info)));
vector<librbd::snap_info_t> snaps;
EXPECT_EQ(0, librbd::api::Snapshot<>::list(ictx, snaps));
EXPECT_EQ(1U, snaps.size());
EXPECT_EQ("snap1", snaps[0].name);
bufferptr read_ptr(256);
bufferlist read_bl;
read_bl.push_back(read_ptr);
librbd::io::ReadResult read_result{&read_bl};
ASSERT_EQ(-ENODEV,
api::Io<>::read(*ictx, 0, 256,
librbd::io::ReadResult{read_result}, 0));
ASSERT_EQ(-ENODEV,
api::Io<>::write(*ictx, 0, bl.length(), bufferlist{bl}, 0));
ASSERT_EQ(-ENODEV, api::Io<>::discard(*ictx, 0, 1, 256));
ASSERT_EQ(-ENODEV,
api::Io<>::write_same(*ictx, 0, bl.length(), bufferlist{bl}, 0));
uint64_t mismatch_off;
ASSERT_EQ(-ENODEV,
api::Io<>::compare_and_write(*ictx, 0, bl.length(),
bufferlist{bl}, bufferlist{bl},
&mismatch_off, 0));
ASSERT_EQ(-ENODEV, api::Io<>::flush(*ictx));
ASSERT_EQ(-ENODEV, snap_create(*ictx, "snap2"));
ASSERT_EQ(0, ictx->operations->snap_remove(cls::rbd::UserSnapshotNamespace(),
"snap1"));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(-ENODEV, ictx->operations->resize(0, true, no_op));
close_image(ictx);
ASSERT_EQ(0, librbd::api::Image<>::remove(m_ioctx, m_image_name, no_op));
ASSERT_EQ(0, create_image_pp(m_rbd, m_ioctx, m_image_name, m_image_size));
}
} // namespace librbd
| 62,358 | 32.364901 | 92 |
cc
|
null |
ceph-main/src/test/librbd/test_librbd.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2011 New Dream Network
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "include/int_types.h"
#include "include/rados/librados.h"
#include "include/rbd_types.h"
#include "include/rbd/librbd.h"
#include "include/rbd/librbd.hpp"
#include "include/event_type.h"
#include "include/err.h"
#include "common/ceph_mutex.h"
#include "json_spirit/json_spirit.h"
#include "test/librados/crimson_utils.h"
#include "gtest/gtest.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <poll.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <sstream>
#include <list>
#include <set>
#include <thread>
#include <vector>
#include <limits>
#include "test/librados/test.h"
#include "test/librados/test_cxx.h"
#include "test/librbd/test_support.h"
#include "common/event_socket.h"
#include "include/interval_set.h"
#include "include/stringify.h"
#include <boost/assign/list_of.hpp>
#include <boost/scope_exit.hpp>
#ifdef HAVE_EVENTFD
#include <sys/eventfd.h>
#endif
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
using namespace std;
using std::chrono::seconds;
#define ASSERT_PASSED0(x) \
do { \
bool passed = false; \
x(&passed); \
ASSERT_TRUE(passed); \
} while(0)
#define ASSERT_PASSED(x, args...) \
do { \
bool passed = false; \
x(args, &passed); \
ASSERT_TRUE(passed); \
} while(0)
void register_test_librbd() {
}
static int get_features(bool *old_format, uint64_t *features)
{
const char *c = getenv("RBD_FEATURES");
if (c && strlen(c) > 0) {
stringstream ss;
ss << c;
ss >> *features;
if (ss.fail())
return -EINVAL;
*old_format = false;
cout << "using new format!" << std::endl;
} else {
*old_format = true;
*features = 0;
cout << "using old format" << std::endl;
}
return 0;
}
static int create_image_full(rados_ioctx_t ioctx, const char *name,
uint64_t size, int *order, int old_format,
uint64_t features)
{
if (old_format) {
// ensure old-format tests actually use the old format
int r = rados_conf_set(rados_ioctx_get_cluster(ioctx),
"rbd_default_format", "1");
if (r < 0) {
return r;
}
return rbd_create(ioctx, name, size, order);
} else if ((features & RBD_FEATURE_STRIPINGV2) != 0) {
uint64_t stripe_unit = IMAGE_STRIPE_UNIT;
if (*order) {
// use a conservative stripe_unit for non default order
stripe_unit = (1ull << (*order-1));
}
printf("creating image with stripe unit: %" PRIu64 ", "
"stripe count: %" PRIu64 "\n",
stripe_unit, IMAGE_STRIPE_COUNT);
return rbd_create3(ioctx, name, size, features, order,
stripe_unit, IMAGE_STRIPE_COUNT);
} else {
return rbd_create2(ioctx, name, size, features, order);
}
}
static int clone_image(rados_ioctx_t p_ioctx,
rbd_image_t p_image, const char *p_name,
const char *p_snap_name, rados_ioctx_t c_ioctx,
const char *c_name, uint64_t features, int *c_order)
{
uint64_t stripe_unit, stripe_count;
int r;
r = rbd_get_stripe_unit(p_image, &stripe_unit);
if (r != 0) {
return r;
}
r = rbd_get_stripe_count(p_image, &stripe_count);
if (r != 0) {
return r;
}
return rbd_clone2(p_ioctx, p_name, p_snap_name, c_ioctx,
c_name, features, c_order, stripe_unit, stripe_count);
}
static int create_image(rados_ioctx_t ioctx, const char *name,
uint64_t size, int *order)
{
bool old_format;
uint64_t features;
int r = get_features(&old_format, &features);
if (r < 0)
return r;
return create_image_full(ioctx, name, size, order, old_format, features);
}
static int create_image_pp(librbd::RBD &rbd,
librados::IoCtx &ioctx,
const char *name,
uint64_t size, int *order) {
bool old_format;
uint64_t features;
int r = get_features(&old_format, &features);
if (r < 0)
return r;
if (old_format) {
librados::Rados rados(ioctx);
int r = rados.conf_set("rbd_default_format", "1");
if (r < 0) {
return r;
}
return rbd.create(ioctx, name, size, order);
} else {
return rbd.create2(ioctx, name, size, features, order);
}
}
void simple_write_cb(rbd_completion_t cb, void *arg)
{
printf("write completion cb called!\n");
}
void simple_read_cb(rbd_completion_t cb, void *arg)
{
printf("read completion cb called!\n");
}
void aio_write_test_data_and_poll(rbd_image_t image, int fd, const char *test_data,
uint64_t off, size_t len, uint32_t iohint, bool *passed)
{
rbd_completion_t comp;
uint64_t data = 0x123;
rbd_aio_create_completion((void*)&data, (rbd_callback_t) simple_write_cb, &comp);
printf("created completion\n");
printf("started write\n");
if (iohint)
rbd_aio_write2(image, off, len, test_data, comp, iohint);
else
rbd_aio_write(image, off, len, test_data, comp);
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
ASSERT_EQ(1, poll(&pfd, 1, -1));
ASSERT_TRUE(pfd.revents & POLLIN);
rbd_completion_t comps[1];
ASSERT_EQ(1, rbd_poll_io_events(image, comps, 1));
uint64_t count;
ASSERT_EQ(static_cast<ssize_t>(sizeof(count)),
read(fd, &count, sizeof(count)));
int r = rbd_aio_get_return_value(comps[0]);
ASSERT_TRUE(rbd_aio_is_complete(comps[0]));
ASSERT_TRUE(*(uint64_t*)rbd_aio_get_arg(comps[0]) == data);
printf("return value is: %d\n", r);
ASSERT_EQ(0, r);
printf("finished write\n");
rbd_aio_release(comps[0]);
*passed = true;
}
void aio_write_test_data(rbd_image_t image, const char *test_data, uint64_t off, size_t len, uint32_t iohint, bool *passed)
{
rbd_completion_t comp;
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp);
printf("created completion\n");
if (iohint)
rbd_aio_write2(image, off, len, test_data, comp, iohint);
else
rbd_aio_write(image, off, len, test_data, comp);
printf("started write\n");
rbd_aio_wait_for_complete(comp);
int r = rbd_aio_get_return_value(comp);
printf("return value is: %d\n", r);
ASSERT_EQ(0, r);
printf("finished write\n");
rbd_aio_release(comp);
*passed = true;
}
void write_test_data(rbd_image_t image, const char *test_data, uint64_t off, size_t len, uint32_t iohint, bool *passed)
{
ssize_t written;
if (iohint)
written = rbd_write2(image, off, len, test_data, iohint);
else
written = rbd_write(image, off, len, test_data);
printf("wrote: %d\n", (int) written);
ASSERT_EQ(len, static_cast<size_t>(written));
*passed = true;
}
void aio_discard_test_data(rbd_image_t image, uint64_t off, uint64_t len, bool *passed)
{
rbd_completion_t comp;
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp);
rbd_aio_discard(image, off, len, comp);
rbd_aio_wait_for_complete(comp);
int r = rbd_aio_get_return_value(comp);
ASSERT_EQ(0, r);
printf("aio discard: %d~%d = %d\n", (int)off, (int)len, (int)r);
rbd_aio_release(comp);
*passed = true;
}
void discard_test_data(rbd_image_t image, uint64_t off, size_t len, bool *passed)
{
ssize_t written;
written = rbd_discard(image, off, len);
printf("discard: %d~%d = %d\n", (int)off, (int)len, (int)written);
ASSERT_EQ(len, static_cast<size_t>(written));
*passed = true;
}
void aio_read_test_data_and_poll(rbd_image_t image, int fd, const char *expected,
uint64_t off, size_t len, uint32_t iohint, bool *passed)
{
rbd_completion_t comp;
char *result = (char *)malloc(len + 1);
ASSERT_NE(static_cast<char *>(NULL), result);
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
printf("created completion\n");
printf("started read\n");
if (iohint)
rbd_aio_read2(image, off, len, result, comp, iohint);
else
rbd_aio_read(image, off, len, result, comp);
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
ASSERT_EQ(1, poll(&pfd, 1, -1));
ASSERT_TRUE(pfd.revents & POLLIN);
rbd_completion_t comps[1];
ASSERT_EQ(1, rbd_poll_io_events(image, comps, 1));
uint64_t count;
ASSERT_EQ(static_cast<ssize_t>(sizeof(count)),
read(fd, &count, sizeof(count)));
int r = rbd_aio_get_return_value(comps[0]);
ASSERT_TRUE(rbd_aio_is_complete(comps[0]));
printf("return value is: %d\n", r);
ASSERT_EQ(len, static_cast<size_t>(r));
rbd_aio_release(comps[0]);
if (memcmp(result, expected, len)) {
printf("read: %s\nexpected: %s\n", result, expected);
ASSERT_EQ(0, memcmp(result, expected, len));
}
free(result);
*passed = true;
}
void aio_read_test_data(rbd_image_t image, const char *expected, uint64_t off, size_t len, uint32_t iohint, bool *passed)
{
rbd_completion_t comp;
char *result = (char *)malloc(len + 1);
ASSERT_NE(static_cast<char *>(NULL), result);
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
printf("created completion\n");
if (iohint)
rbd_aio_read2(image, off, len, result, comp, iohint);
else
rbd_aio_read(image, off, len, result, comp);
printf("started read\n");
rbd_aio_wait_for_complete(comp);
int r = rbd_aio_get_return_value(comp);
printf("return value is: %d\n", r);
ASSERT_EQ(len, static_cast<size_t>(r));
rbd_aio_release(comp);
if (memcmp(result, expected, len)) {
printf("read: %s\nexpected: %s\n", result, expected);
ASSERT_EQ(0, memcmp(result, expected, len));
}
free(result);
*passed = true;
}
void read_test_data(rbd_image_t image, const char *expected, uint64_t off, size_t len, uint32_t iohint, bool *passed)
{
ssize_t read;
char *result = (char *)malloc(len + 1);
ASSERT_NE(static_cast<char *>(NULL), result);
if (iohint)
read = rbd_read2(image, off, len, result, iohint);
else
read = rbd_read(image, off, len, result);
printf("read: %d\n", (int) read);
ASSERT_EQ(len, static_cast<size_t>(read));
result[len] = '\0';
if (memcmp(result, expected, len)) {
printf("read: %s\nexpected: %s\n", result, expected);
ASSERT_EQ(0, memcmp(result, expected, len));
}
free(result);
*passed = true;
}
void aio_writesame_test_data(rbd_image_t image, const char *test_data, uint64_t off, uint64_t len,
uint64_t data_len, uint32_t iohint, bool *passed)
{
rbd_completion_t comp;
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp);
printf("created completion\n");
int r;
r = rbd_aio_writesame(image, off, len, test_data, data_len, comp, iohint);
printf("started writesame\n");
if (len % data_len) {
ASSERT_EQ(-EINVAL, r);
printf("expected fail, finished writesame\n");
rbd_aio_release(comp);
*passed = true;
return;
}
rbd_aio_wait_for_complete(comp);
r = rbd_aio_get_return_value(comp);
printf("return value is: %d\n", r);
ASSERT_EQ(0, r);
printf("finished writesame\n");
rbd_aio_release(comp);
//verify data
printf("to verify the data\n");
ssize_t read;
char *result = (char *)malloc(data_len+ 1);
ASSERT_NE(static_cast<char *>(NULL), result);
uint64_t left = len;
while (left > 0) {
read = rbd_read(image, off, data_len, result);
ASSERT_EQ(data_len, static_cast<size_t>(read));
result[data_len] = '\0';
if (memcmp(result, test_data, data_len)) {
printf("read: %d ~ %d\n", (int) off, (int) read);
printf("read: %s\nexpected: %s\n", result, test_data);
ASSERT_EQ(0, memcmp(result, test_data, data_len));
}
off += data_len;
left -= data_len;
}
ASSERT_EQ(0U, left);
free(result);
printf("verified\n");
*passed = true;
}
void writesame_test_data(rbd_image_t image, const char *test_data, uint64_t off, uint64_t len,
uint64_t data_len, uint32_t iohint, bool *passed)
{
ssize_t written;
written = rbd_writesame(image, off, len, test_data, data_len, iohint);
if (len % data_len) {
ASSERT_EQ(-EINVAL, written);
printf("expected fail, finished writesame\n");
*passed = true;
return;
}
ASSERT_EQ(len, static_cast<size_t>(written));
printf("wrote: %d\n", (int) written);
//verify data
printf("to verify the data\n");
ssize_t read;
char *result = (char *)malloc(data_len+ 1);
ASSERT_NE(static_cast<char *>(NULL), result);
uint64_t left = len;
while (left > 0) {
read = rbd_read(image, off, data_len, result);
ASSERT_EQ(data_len, static_cast<size_t>(read));
result[data_len] = '\0';
if (memcmp(result, test_data, data_len)) {
printf("read: %d ~ %d\n", (int) off, (int) read);
printf("read: %s\nexpected: %s\n", result, test_data);
ASSERT_EQ(0, memcmp(result, test_data, data_len));
}
off += data_len;
left -= data_len;
}
ASSERT_EQ(0U, left);
free(result);
printf("verified\n");
*passed = true;
}
void aio_compare_and_write_test_data(rbd_image_t image, const char *cmp_data,
const char *test_data, uint64_t off,
size_t len, uint32_t iohint, bool *passed)
{
rbd_completion_t comp;
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_write_cb, &comp);
printf("created completion\n");
uint64_t mismatch_offset;
rbd_aio_compare_and_write(image, off, len, cmp_data, test_data, comp, &mismatch_offset, iohint);
printf("started aio compare and write\n");
rbd_aio_wait_for_complete(comp);
int r = rbd_aio_get_return_value(comp);
printf("return value is: %d\n", r);
ASSERT_EQ(0, r);
printf("finished aio compare and write\n");
rbd_aio_release(comp);
*passed = true;
}
void compare_and_write_test_data(rbd_image_t image, const char *cmp_data,
const char *test_data, uint64_t off, size_t len,
uint64_t *mismatch_off, uint32_t iohint, bool *passed)
{
printf("start compare and write\n");
ssize_t written;
written = rbd_compare_and_write(image, off, len, cmp_data, test_data, mismatch_off, iohint);
printf("compare and wrote: %d\n", (int) written);
ASSERT_EQ(len, static_cast<size_t>(written));
*passed = true;
}
class TestLibRBD : public ::testing::Test {
public:
TestLibRBD() : m_pool_number() {
}
static void SetUpTestCase() {
_pool_names.clear();
_unique_pool_names.clear();
_image_number = 0;
ASSERT_EQ("", connect_cluster(&_cluster));
ASSERT_EQ("", connect_cluster_pp(_rados));
create_optional_data_pool();
}
static void TearDownTestCase() {
rados_shutdown(_cluster);
_rados.wait_for_latest_osdmap();
_pool_names.insert(_pool_names.end(), _unique_pool_names.begin(),
_unique_pool_names.end());
for (size_t i = 1; i < _pool_names.size(); ++i) {
ASSERT_EQ(0, _rados.pool_delete(_pool_names[i].c_str()));
}
if (!_pool_names.empty()) {
ASSERT_EQ(0, destroy_one_pool_pp(_pool_names[0], _rados));
}
}
void SetUp() override {
ASSERT_NE("", m_pool_name = create_pool());
}
bool is_skip_partial_discard_enabled() {
std::string value;
EXPECT_EQ(0, _rados.conf_get("rbd_skip_partial_discard", value));
return value == "true";
}
bool is_skip_partial_discard_enabled(rbd_image_t image) {
if (is_skip_partial_discard_enabled()) {
rbd_flush(image);
uint64_t features;
EXPECT_EQ(0, rbd_get_features(image, &features));
return !(features & RBD_FEATURE_DIRTY_CACHE);
}
return false;
}
bool is_skip_partial_discard_enabled(librbd::Image& image) {
if (is_skip_partial_discard_enabled()) {
image.flush();
uint64_t features;
EXPECT_EQ(0, image.features(&features));
return !(features & RBD_FEATURE_DIRTY_CACHE);
}
return false;
}
void validate_object_map(rbd_image_t image, bool *passed) {
uint64_t flags;
ASSERT_EQ(0, rbd_get_flags(image, &flags));
*passed = ((flags & RBD_FLAG_OBJECT_MAP_INVALID) == 0);
}
void validate_object_map(librbd::Image &image, bool *passed) {
uint64_t flags;
ASSERT_EQ(0, image.get_flags(&flags));
*passed = ((flags & RBD_FLAG_OBJECT_MAP_INVALID) == 0);
}
static std::string get_temp_image_name() {
++_image_number;
return "image" + stringify(_image_number);
}
static void create_optional_data_pool() {
bool created = false;
std::string data_pool;
ASSERT_EQ(0, create_image_data_pool(_rados, data_pool, &created));
if (!data_pool.empty()) {
printf("using image data pool: %s\n", data_pool.c_str());
if (created) {
_unique_pool_names.push_back(data_pool);
}
}
}
std::string create_pool(bool unique = false) {
librados::Rados rados;
std::string pool_name;
if (unique) {
pool_name = get_temp_pool_name("test-librbd-");
EXPECT_EQ("", create_one_pool_pp(pool_name, rados));
_unique_pool_names.push_back(pool_name);
} else if (m_pool_number < _pool_names.size()) {
pool_name = _pool_names[m_pool_number];
} else {
pool_name = get_temp_pool_name("test-librbd-");
EXPECT_EQ("", create_one_pool_pp(pool_name, rados));
_pool_names.push_back(pool_name);
}
++m_pool_number;
return pool_name;
}
void test_io(rbd_image_t image) {
bool skip_discard = is_skip_partial_discard_enabled(image);
char test_data[TEST_IO_SIZE + 1];
char zero_data[TEST_IO_SIZE + 1];
char mismatch_data[TEST_IO_SIZE + 1];
int i;
uint64_t mismatch_offset;
for (i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
memset(zero_data, 0, sizeof(zero_data));
memset(mismatch_data, 9, sizeof(mismatch_data));
for (i = 0; i < 5; ++i)
ASSERT_PASSED(write_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_write_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, 0);
for (i = 0; i < 5; ++i)
ASSERT_PASSED(compare_and_write_test_data, image, test_data, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE, &mismatch_offset, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_compare_and_write_test_data, image, test_data, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
for (i = 0; i < 5; ++i)
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_read_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, 0);
// discard 2nd, 4th sections.
ASSERT_PASSED(discard_test_data, image, TEST_IO_SIZE, TEST_IO_SIZE);
ASSERT_PASSED(aio_discard_test_data, image, TEST_IO_SIZE*3, TEST_IO_SIZE);
ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, skip_discard ? test_data : zero_data,
TEST_IO_SIZE, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE*2,
TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, skip_discard ? test_data : zero_data,
TEST_IO_SIZE*3, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE*4,
TEST_IO_SIZE, 0);
for (i = 0; i < 15; ++i) {
if (i % 3 == 2) {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, 0);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, 0);
} else if (i % 3 == 1) {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE + i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE + i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
} else {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
}
}
for (i = 0; i < 15; ++i) {
if (i % 3 == 2) {
ASSERT_PASSED(aio_writesame_test_data, image, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE,
0);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data,
TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE,
0);
} else if (i % 3 == 1) {
ASSERT_PASSED(aio_writesame_test_data, image, test_data,
TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data,
TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
} else {
ASSERT_PASSED(aio_writesame_test_data, image, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data,
TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
}
}
rbd_image_info_t info;
rbd_completion_t comp;
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
// can't read or write starting past end
ASSERT_EQ(-EINVAL, rbd_write(image, info.size, 1, test_data));
ASSERT_EQ(-EINVAL, rbd_read(image, info.size, 1, test_data));
// reading through end returns amount up to end
ASSERT_EQ(10, rbd_read(image, info.size - 10, 100, test_data));
// writing through end returns amount up to end
ASSERT_EQ(10, rbd_write(image, info.size - 10, 100, test_data));
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
ASSERT_EQ(0, rbd_aio_write(image, info.size, 1, test_data, comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EINVAL, rbd_aio_get_return_value(comp));
rbd_aio_release(comp);
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
ASSERT_EQ(0, rbd_aio_read(image, info.size, 1, test_data, comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EINVAL, rbd_aio_get_return_value(comp));
rbd_aio_release(comp);
ASSERT_PASSED(write_test_data, image, zero_data, 0, TEST_IO_SIZE,
LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
mismatch_offset = 123;
ASSERT_EQ(-EILSEQ, rbd_compare_and_write(image, 0, TEST_IO_SIZE,
mismatch_data, mismatch_data, &mismatch_offset, 0));
ASSERT_EQ(0U, mismatch_offset);
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
mismatch_offset = 123;
ASSERT_EQ(0, rbd_aio_compare_and_write(image, 0, TEST_IO_SIZE, mismatch_data,
mismatch_data, comp, &mismatch_offset, 0));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EILSEQ, rbd_aio_get_return_value(comp));
ASSERT_EQ(0U, mismatch_offset);
rbd_aio_release(comp);
ASSERT_PASSED(validate_object_map, image);
}
static std::vector<std::string> _pool_names;
static std::vector<std::string> _unique_pool_names;
static rados_t _cluster;
static librados::Rados _rados;
static uint64_t _image_number;
std::string m_pool_name;
uint32_t m_pool_number;
};
std::vector<std::string> TestLibRBD::_pool_names;
std::vector<std::string> TestLibRBD::_unique_pool_names;
rados_t TestLibRBD::_cluster;
librados::Rados TestLibRBD::_rados;
uint64_t TestLibRBD::_image_number = 0;
TEST_F(TestLibRBD, CreateAndStat)
{
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
rbd_image_info_t info;
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
printf("image has size %llu and order %d\n", (unsigned long long) info.size, info.order);
ASSERT_EQ(info.size, size);
ASSERT_EQ(info.order, order);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, CreateWithSameDataPool)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
rbd_image_t image;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
bool old_format;
uint64_t features;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
rbd_image_options_t image_options;
rbd_image_options_create(&image_options);
BOOST_SCOPE_EXIT( (&image_options) ) {
rbd_image_options_destroy(image_options);
} BOOST_SCOPE_EXIT_END;
ASSERT_EQ(0, rbd_image_options_set_uint64(image_options,
RBD_IMAGE_OPTION_FEATURES,
features));
ASSERT_EQ(0, rbd_image_options_set_string(image_options,
RBD_IMAGE_OPTION_DATA_POOL,
m_pool_name.c_str()));
ASSERT_EQ(0, rbd_create4(ioctx, name.c_str(), size, image_options));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, CreateAndStatPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::image_info_t info;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ASSERT_EQ(0, image.stat(info, sizeof(info)));
ASSERT_EQ(info.size, size);
ASSERT_EQ(info.order, order);
}
ioctx.close();
}
TEST_F(TestLibRBD, GetId)
{
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
ASSERT_EQ(0, create_image(ioctx, name.c_str(), 0, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
char id[4096];
if (!is_feature_enabled(0)) {
// V1 image
ASSERT_EQ(-EINVAL, rbd_get_id(image, id, sizeof(id)));
} else {
ASSERT_EQ(-ERANGE, rbd_get_id(image, id, 0));
ASSERT_EQ(0, rbd_get_id(image, id, sizeof(id)));
ASSERT_LT(0U, strlen(id));
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_open_by_id(ioctx, id, &image, NULL));
size_t name_len = 0;
ASSERT_EQ(-ERANGE, rbd_get_name(image, NULL, &name_len));
ASSERT_EQ(name_len, name.size() + 1);
char image_name[name_len];
ASSERT_EQ(0, rbd_get_name(image, image_name, &name_len));
ASSERT_STREQ(name.c_str(), image_name);
}
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, GetIdPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
std::string id;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), 0, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
if (!is_feature_enabled(0)) {
// V1 image
ASSERT_EQ(-EINVAL, image.get_id(&id));
} else {
ASSERT_EQ(0, image.get_id(&id));
ASSERT_LT(0U, id.size());
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, rbd.open_by_id(ioctx, image, id.c_str(), NULL));
std::string image_name;
ASSERT_EQ(0, image.get_name(&image_name));
ASSERT_EQ(name, image_name);
}
}
TEST_F(TestLibRBD, GetBlockNamePrefix)
{
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
ASSERT_EQ(0, create_image(ioctx, name.c_str(), 0, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
char prefix[4096];
ASSERT_EQ(-ERANGE, rbd_get_block_name_prefix(image, prefix, 0));
ASSERT_EQ(0, rbd_get_block_name_prefix(image, prefix, sizeof(prefix)));
ASSERT_LT(0U, strlen(prefix));
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, GetBlockNamePrefixPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), 0, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ASSERT_LT(0U, image.get_block_name_prefix().size());
}
TEST_F(TestLibRBD, TestGetCreateTimestamp)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
ASSERT_EQ(0, create_image(ioctx, name.c_str(), 0, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
struct timespec timestamp;
ASSERT_EQ(0, rbd_get_create_timestamp(image, ×tamp));
ASSERT_LT(0, timestamp.tv_sec);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, GetCreateTimestampPP)
{
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), 0, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
struct timespec timestamp;
ASSERT_EQ(0, image.get_create_timestamp(×tamp));
ASSERT_LT(0, timestamp.tv_sec);
}
TEST_F(TestLibRBD, OpenAio)
{
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
rbd_image_info_t info;
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
rbd_completion_t open_comp;
ASSERT_EQ(0, rbd_aio_create_completion(NULL, NULL, &open_comp));
ASSERT_EQ(0, rbd_aio_open(ioctx, name.c_str(), &image, NULL, open_comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(open_comp));
ASSERT_EQ(1, rbd_aio_is_complete(open_comp));
ASSERT_EQ(0, rbd_aio_get_return_value(open_comp));
rbd_aio_release(open_comp);
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
printf("image has size %llu and order %d\n", (unsigned long long) info.size, info.order);
ASSERT_EQ(info.size, size);
ASSERT_EQ(info.order, order);
rbd_completion_t close_comp;
ASSERT_EQ(0, rbd_aio_create_completion(NULL, NULL, &close_comp));
ASSERT_EQ(0, rbd_aio_close(image, close_comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(close_comp));
ASSERT_EQ(1, rbd_aio_is_complete(close_comp));
ASSERT_EQ(0, rbd_aio_get_return_value(close_comp));
rbd_aio_release(close_comp);
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, OpenAioFail)
{
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
std::string name = get_temp_image_name();
rbd_image_t image;
rbd_completion_t open_comp;
ASSERT_EQ(0, rbd_aio_create_completion(NULL, NULL, &open_comp));
ASSERT_EQ(0, rbd_aio_open(ioctx, name.c_str(), &image, NULL, open_comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(open_comp));
ASSERT_EQ(1, rbd_aio_is_complete(open_comp));
ASSERT_EQ(-ENOENT, rbd_aio_get_return_value(open_comp));
rbd_aio_release(open_comp);
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, OpenAioPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::image_info_t info;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::RBD::AioCompletion *open_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, rbd.aio_open(ioctx, image, name.c_str(), NULL, open_comp));
ASSERT_EQ(0, open_comp->wait_for_complete());
ASSERT_EQ(1, open_comp->is_complete());
ASSERT_EQ(0, open_comp->get_return_value());
open_comp->release();
ASSERT_EQ(0, image.stat(info, sizeof(info)));
ASSERT_EQ(info.size, size);
ASSERT_EQ(info.order, order);
// reopen
open_comp = new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, rbd.aio_open(ioctx, image, name.c_str(), NULL, open_comp));
ASSERT_EQ(0, open_comp->wait_for_complete());
ASSERT_EQ(1, open_comp->is_complete());
ASSERT_EQ(0, open_comp->get_return_value());
open_comp->release();
// close
librbd::RBD::AioCompletion *close_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, image.aio_close(close_comp));
ASSERT_EQ(0, close_comp->wait_for_complete());
ASSERT_EQ(1, close_comp->is_complete());
ASSERT_EQ(0, close_comp->get_return_value());
close_comp->release();
// close closed image
close_comp = new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(-EINVAL, image.aio_close(close_comp));
close_comp->release();
ioctx.close();
}
TEST_F(TestLibRBD, OpenAioFailPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
std::string name = get_temp_image_name();
librbd::RBD::AioCompletion *open_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, rbd.aio_open(ioctx, image, name.c_str(), NULL, open_comp));
ASSERT_EQ(0, open_comp->wait_for_complete());
ASSERT_EQ(1, open_comp->is_complete());
ASSERT_EQ(-ENOENT, open_comp->get_return_value());
open_comp->release();
}
ioctx.close();
}
TEST_F(TestLibRBD, ResizeAndStat)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_info_t info;
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_resize(image, size * 4));
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
ASSERT_EQ(info.size, size * 4);
ASSERT_EQ(0, rbd_resize(image, size / 2));
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
ASSERT_EQ(info.size, size / 2);
// downsizing without allowing shrink should fail
// and image size should not change
ASSERT_EQ(-EINVAL, rbd_resize2(image, size / 4, false, NULL, NULL));
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
ASSERT_EQ(info.size, size / 2);
ASSERT_EQ(0, rbd_resize2(image, size / 4, true, NULL, NULL));
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
ASSERT_EQ(info.size, size / 4);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, ResizeAndStatPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::image_info_t info;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ASSERT_EQ(0, image.resize(size * 4));
ASSERT_EQ(0, image.stat(info, sizeof(info)));
ASSERT_EQ(info.size, size * 4);
ASSERT_EQ(0, image.resize(size / 2));
ASSERT_EQ(0, image.stat(info, sizeof(info)));
ASSERT_EQ(info.size, size / 2);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, UpdateWatchAndResize)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
struct Watcher {
rbd_image_t &m_image;
std::mutex m_lock;
std::condition_variable m_cond;
size_t m_size = 0;
static void cb(void *arg) {
Watcher *watcher = static_cast<Watcher *>(arg);
watcher->handle_notify();
}
explicit Watcher(rbd_image_t &image) : m_image(image) {}
void handle_notify() {
rbd_image_info_t info;
ASSERT_EQ(0, rbd_stat(m_image, &info, sizeof(info)));
std::lock_guard<std::mutex> locker(m_lock);
m_size = info.size;
m_cond.notify_one();
}
void wait_for_size(size_t size) {
std::unique_lock<std::mutex> locker(m_lock);
ASSERT_TRUE(m_cond.wait_for(locker, seconds(5),
[size, this] {
return this->m_size == size;}));
}
} watcher(image);
uint64_t handle;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_update_watch(image, &handle, Watcher::cb, &watcher));
ASSERT_EQ(0, rbd_resize(image, size * 4));
watcher.wait_for_size(size * 4);
ASSERT_EQ(0, rbd_resize(image, size / 2));
watcher.wait_for_size(size / 2);
ASSERT_EQ(0, rbd_update_unwatch(image, handle));
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, UpdateWatchAndResizePP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
struct Watcher : public librbd::UpdateWatchCtx {
explicit Watcher(librbd::Image &image) : m_image(image) {
}
void handle_notify() override {
librbd::image_info_t info;
ASSERT_EQ(0, m_image.stat(info, sizeof(info)));
std::lock_guard<std::mutex> locker(m_lock);
m_size = info.size;
m_cond.notify_one();
}
void wait_for_size(size_t size) {
std::unique_lock<std::mutex> locker(m_lock);
ASSERT_TRUE(m_cond.wait_for(locker, seconds(5),
[size, this] {
return this->m_size == size;}));
}
librbd::Image &m_image;
std::mutex m_lock;
std::condition_variable m_cond;
size_t m_size = 0;
} watcher(image);
uint64_t handle;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ASSERT_EQ(0, image.update_watch(&watcher, &handle));
ASSERT_EQ(0, image.resize(size * 4));
watcher.wait_for_size(size * 4);
ASSERT_EQ(0, image.resize(size / 2));
watcher.wait_for_size(size / 2);
ASSERT_EQ(0, image.update_unwatch(handle));
}
ioctx.close();
}
int test_ls(rados_ioctx_t io_ctx, size_t num_expected, ...)
{
int num_images, i;
char *names, *cur_name;
va_list ap;
size_t max_size = 1024;
names = (char *) malloc(sizeof(char) * 1024);
int len = rbd_list(io_ctx, names, &max_size);
std::set<std::string> image_names;
for (i = 0, num_images = 0, cur_name = names; cur_name < names + len; i++) {
printf("image: %s\n", cur_name);
image_names.insert(cur_name);
cur_name += strlen(cur_name) + 1;
num_images++;
}
free(names);
va_start(ap, num_expected);
for (i = num_expected; i > 0; i--) {
char *expected = va_arg(ap, char *);
printf("expected = %s\n", expected);
std::set<std::string>::iterator it = image_names.find(expected);
if (it != image_names.end()) {
printf("found %s\n", expected);
image_names.erase(it);
printf("erased %s\n", expected);
} else {
ADD_FAILURE() << "Unable to find image " << expected;
va_end(ap);
return -ENOENT;
}
}
va_end(ap);
if (!image_names.empty()) {
ADD_FAILURE() << "Unexpected images discovered";
return -EINVAL;
}
return num_images;
}
TEST_F(TestLibRBD, TestCreateLsDelete)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, create_pool(true).c_str(), &ioctx);
int order = 0;
std::string name = get_temp_image_name();
std::string name2 = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, test_ls(ioctx, 0));
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(1, test_ls(ioctx, 1, name.c_str()));
ASSERT_EQ(0, create_image(ioctx, name2.c_str(), size, &order));
ASSERT_EQ(2, test_ls(ioctx, 2, name.c_str(), name2.c_str()));
ASSERT_EQ(0, rbd_remove(ioctx, name.c_str()));
ASSERT_EQ(1, test_ls(ioctx, 1, name2.c_str()));
ASSERT_EQ(-ENOENT, rbd_remove(ioctx, name.c_str()));
rados_ioctx_destroy(ioctx);
}
int test_ls_pp(librbd::RBD& rbd, librados::IoCtx& io_ctx, size_t num_expected, ...)
{
int r;
size_t i;
va_list ap;
vector<string> names;
r = rbd.list(io_ctx, names);
if (r == -ENOENT)
r = 0;
EXPECT_TRUE(r >= 0);
cout << "num images is: " << names.size() << std::endl
<< "expected: " << num_expected << std::endl;
int num = names.size();
for (i = 0; i < names.size(); i++) {
cout << "image: " << names[i] << std::endl;
}
va_start(ap, num_expected);
for (i = num_expected; i > 0; i--) {
char *expected = va_arg(ap, char *);
cout << "expected = " << expected << std::endl;
vector<string>::iterator listed_name = find(names.begin(), names.end(), string(expected));
if (listed_name == names.end()) {
ADD_FAILURE() << "Unable to find image " << expected;
va_end(ap);
return -ENOENT;
}
names.erase(listed_name);
}
va_end(ap);
if (!names.empty()) {
ADD_FAILURE() << "Unexpected images discovered";
return -EINVAL;
}
return num;
}
TEST_F(TestLibRBD, TestCreateLsDeletePP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(create_pool(true).c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
std::string name2 = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(1, test_ls_pp(rbd, ioctx, 1, name.c_str()));
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name2.c_str(), size, &order));
ASSERT_EQ(2, test_ls_pp(rbd, ioctx, 2, name.c_str(), name2.c_str()));
ASSERT_EQ(0, rbd.remove(ioctx, name.c_str()));
ASSERT_EQ(1, test_ls_pp(rbd, ioctx, 1, name2.c_str()));
}
ioctx.close();
}
static int print_progress_percent(uint64_t offset, uint64_t src_size,
void *data)
{
float percent = ((float)offset * 100) / src_size;
printf("%3.2f%% done\n", percent);
return 0;
}
TEST_F(TestLibRBD, TestCopy)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, create_pool(true).c_str(), &ioctx);
rbd_image_t image;
rbd_image_t image2;
rbd_image_t image3;
int order = 0;
std::string name = get_temp_image_name();
std::string name2 = get_temp_image_name();
std::string name3 = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(1, test_ls(ioctx, 1, name.c_str()));
size_t sum_key_len = 0;
size_t sum_value_len = 0;
std::string key;
std::string val;
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_set(image, key.c_str(), val.c_str()));
sum_key_len += (key.size() + 1);
sum_value_len += (val.size() + 1);
}
char keys[1024];
char vals[1024];
size_t keys_len = sizeof(keys);
size_t vals_len = sizeof(vals);
char value[1024];
size_t value_len = sizeof(value);
ASSERT_EQ(0, rbd_copy(image, ioctx, name2.c_str()));
ASSERT_EQ(2, test_ls(ioctx, 2, name.c_str(), name2.c_str()));
ASSERT_EQ(0, rbd_open(ioctx, name2.c_str(), &image2, NULL));
ASSERT_EQ(0, rbd_metadata_list(image2, "key", 70, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, sum_key_len);
ASSERT_EQ(vals_len, sum_value_len);
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_get(image2, key.c_str(), value, &value_len));
ASSERT_STREQ(val.c_str(), value);
value_len = sizeof(value);
}
ASSERT_EQ(0, rbd_copy_with_progress(image, ioctx, name3.c_str(),
print_progress_percent, NULL));
ASSERT_EQ(3, test_ls(ioctx, 3, name.c_str(), name2.c_str(), name3.c_str()));
keys_len = sizeof(keys);
vals_len = sizeof(vals);
ASSERT_EQ(0, rbd_open(ioctx, name3.c_str(), &image3, NULL));
ASSERT_EQ(0, rbd_metadata_list(image3, "key", 70, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, sum_key_len);
ASSERT_EQ(vals_len, sum_value_len);
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_get(image3, key.c_str(), value, &value_len));
ASSERT_STREQ(val.c_str(), value);
value_len = sizeof(value);
}
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_close(image2));
ASSERT_EQ(0, rbd_close(image3));
rados_ioctx_destroy(ioctx);
}
class PrintProgress : public librbd::ProgressContext
{
public:
int update_progress(uint64_t offset, uint64_t src_size) override
{
float percent = ((float)offset * 100) / src_size;
printf("%3.2f%% done\n", percent);
return 0;
}
};
TEST_F(TestLibRBD, TestCopyPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(create_pool(true).c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
librbd::Image image2;
librbd::Image image3;
int order = 0;
std::string name = get_temp_image_name();
std::string name2 = get_temp_image_name();
std::string name3 = get_temp_image_name();
uint64_t size = 2 << 20;
PrintProgress pp;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string key;
std::string val;
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, image.metadata_set(key, val));
}
ASSERT_EQ(1, test_ls_pp(rbd, ioctx, 1, name.c_str()));
ASSERT_EQ(0, image.copy(ioctx, name2.c_str()));
ASSERT_EQ(2, test_ls_pp(rbd, ioctx, 2, name.c_str(), name2.c_str()));
ASSERT_EQ(0, rbd.open(ioctx, image2, name2.c_str(), NULL));
map<string, bufferlist> pairs;
std::string value;
ASSERT_EQ(0, image2.metadata_list("", 70, &pairs));
ASSERT_EQ(70U, pairs.size());
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, image2.metadata_get(key.c_str(), &value));
ASSERT_STREQ(val.c_str(), value.c_str());
}
ASSERT_EQ(0, image.copy_with_progress(ioctx, name3.c_str(), pp));
ASSERT_EQ(3, test_ls_pp(rbd, ioctx, 3, name.c_str(), name2.c_str(),
name3.c_str()));
ASSERT_EQ(0, rbd.open(ioctx, image3, name3.c_str(), NULL));
pairs.clear();
ASSERT_EQ(0, image3.metadata_list("", 70, &pairs));
ASSERT_EQ(70U, pairs.size());
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, image3.metadata_get(key.c_str(), &value));
ASSERT_STREQ(val.c_str(), value.c_str());
}
}
ioctx.close();
}
TEST_F(TestLibRBD, TestDeepCopy)
{
REQUIRE_FORMAT_V2();
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, create_pool(true).c_str(), &ioctx);
BOOST_SCOPE_EXIT_ALL( (&ioctx) ) {
rados_ioctx_destroy(ioctx);
};
rbd_image_t image;
rbd_image_t image2;
rbd_image_t image3;
rbd_image_t image4;
rbd_image_t image5;
rbd_image_t image6;
int order = 0;
std::string name = get_temp_image_name();
std::string name2 = get_temp_image_name();
std::string name3 = get_temp_image_name();
std::string name4 = get_temp_image_name();
std::string name5 = get_temp_image_name();
std::string name6 = get_temp_image_name();
uint64_t size = 2 << 20;
rbd_image_options_t opts;
rbd_image_options_create(&opts);
BOOST_SCOPE_EXIT_ALL( (&opts) ) {
rbd_image_options_destroy(opts);
};
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
BOOST_SCOPE_EXIT_ALL( (&image) ) {
ASSERT_EQ(0, rbd_close(image));
};
ASSERT_EQ(1, test_ls(ioctx, 1, name.c_str()));
size_t sum_key_len = 0;
size_t sum_value_len = 0;
std::string key;
std::string val;
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_set(image, key.c_str(), val.c_str()));
sum_key_len += (key.size() + 1);
sum_value_len += (val.size() + 1);
}
char keys[1024];
char vals[1024];
size_t keys_len = sizeof(keys);
size_t vals_len = sizeof(vals);
char value[1024];
size_t value_len = sizeof(value);
ASSERT_EQ(0, rbd_deep_copy(image, ioctx, name2.c_str(), opts));
ASSERT_EQ(2, test_ls(ioctx, 2, name.c_str(), name2.c_str()));
ASSERT_EQ(0, rbd_open(ioctx, name2.c_str(), &image2, NULL));
BOOST_SCOPE_EXIT_ALL( (&image2) ) {
ASSERT_EQ(0, rbd_close(image2));
};
ASSERT_EQ(0, rbd_metadata_list(image2, "key", 70, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, sum_key_len);
ASSERT_EQ(vals_len, sum_value_len);
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_get(image2, key.c_str(), value, &value_len));
ASSERT_STREQ(val.c_str(), value);
value_len = sizeof(value);
}
ASSERT_EQ(0, rbd_deep_copy_with_progress(image, ioctx, name3.c_str(), opts,
print_progress_percent, NULL));
ASSERT_EQ(3, test_ls(ioctx, 3, name.c_str(), name2.c_str(), name3.c_str()));
keys_len = sizeof(keys);
vals_len = sizeof(vals);
ASSERT_EQ(0, rbd_open(ioctx, name3.c_str(), &image3, NULL));
BOOST_SCOPE_EXIT_ALL( (&image3) ) {
ASSERT_EQ(0, rbd_close(image3));
};
ASSERT_EQ(0, rbd_metadata_list(image3, "key", 70, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, sum_key_len);
ASSERT_EQ(vals_len, sum_value_len);
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_get(image3, key.c_str(), value, &value_len));
ASSERT_STREQ(val.c_str(), value);
value_len = sizeof(value);
}
ASSERT_EQ(0, rbd_snap_create(image, "deep_snap"));
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, "deep_snap"));
ASSERT_EQ(0, rbd_snap_protect(image, "deep_snap"));
ASSERT_EQ(0, rbd_clone3(ioctx, name.c_str(), "deep_snap", ioctx,
name4.c_str(), opts));
ASSERT_EQ(4, test_ls(ioctx, 4, name.c_str(), name2.c_str(), name3.c_str(),
name4.c_str()));
ASSERT_EQ(0, rbd_open(ioctx, name4.c_str(), &image4, NULL));
BOOST_SCOPE_EXIT_ALL( (&image4) ) {
ASSERT_EQ(0, rbd_close(image4));
};
ASSERT_EQ(0, rbd_snap_create(image4, "deep_snap"));
ASSERT_EQ(0, rbd_deep_copy(image4, ioctx, name5.c_str(), opts));
ASSERT_EQ(5, test_ls(ioctx, 5, name.c_str(), name2.c_str(), name3.c_str(),
name4.c_str(), name5.c_str()));
ASSERT_EQ(0, rbd_open(ioctx, name5.c_str(), &image5, NULL));
BOOST_SCOPE_EXIT_ALL( (&image5) ) {
ASSERT_EQ(0, rbd_close(image5));
};
ASSERT_EQ(0, rbd_metadata_list(image5, "key", 70, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, sum_key_len);
ASSERT_EQ(vals_len, sum_value_len);
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_get(image5, key.c_str(), value, &value_len));
ASSERT_STREQ(val.c_str(), value);
value_len = sizeof(value);
}
ASSERT_EQ(0, rbd_deep_copy_with_progress(image4, ioctx, name6.c_str(), opts,
print_progress_percent, NULL));
ASSERT_EQ(6, test_ls(ioctx, 6, name.c_str(), name2.c_str(), name3.c_str(),
name4.c_str(), name5.c_str(), name6.c_str()));
keys_len = sizeof(keys);
vals_len = sizeof(vals);
ASSERT_EQ(0, rbd_open(ioctx, name6.c_str(), &image6, NULL));
BOOST_SCOPE_EXIT_ALL( (&image6) ) {
ASSERT_EQ(0, rbd_close(image6));
};
ASSERT_EQ(0, rbd_metadata_list(image6, "key", 70, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, sum_key_len);
ASSERT_EQ(vals_len, sum_value_len);
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_get(image6, key.c_str(), value, &value_len));
ASSERT_STREQ(val.c_str(), value);
value_len = sizeof(value);
}
}
TEST_F(TestLibRBD, TestDeepCopyPP)
{
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(create_pool(true).c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
librbd::Image image2;
librbd::Image image3;
int order = 0;
std::string name = get_temp_image_name();
std::string name2 = get_temp_image_name();
std::string name3 = get_temp_image_name();
uint64_t size = 2 << 20;
librbd::ImageOptions opts;
PrintProgress pp;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string key;
std::string val;
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, image.metadata_set(key, val));
}
ASSERT_EQ(1, test_ls_pp(rbd, ioctx, 1, name.c_str()));
ASSERT_EQ(0, image.deep_copy(ioctx, name2.c_str(), opts));
ASSERT_EQ(2, test_ls_pp(rbd, ioctx, 2, name.c_str(), name2.c_str()));
ASSERT_EQ(0, rbd.open(ioctx, image2, name2.c_str(), NULL));
map<string, bufferlist> pairs;
std::string value;
ASSERT_EQ(0, image2.metadata_list("", 70, &pairs));
ASSERT_EQ(70U, pairs.size());
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, image2.metadata_get(key.c_str(), &value));
ASSERT_STREQ(val.c_str(), value.c_str());
}
ASSERT_EQ(0, image.deep_copy_with_progress(ioctx, name3.c_str(), opts, pp));
ASSERT_EQ(3, test_ls_pp(rbd, ioctx, 3, name.c_str(), name2.c_str(),
name3.c_str()));
ASSERT_EQ(0, rbd.open(ioctx, image3, name3.c_str(), NULL));
pairs.clear();
ASSERT_EQ(0, image3.metadata_list("", 70, &pairs));
ASSERT_EQ(70U, pairs.size());
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, image3.metadata_get(key.c_str(), &value));
ASSERT_STREQ(val.c_str(), value.c_str());
}
}
ioctx.close();
}
int test_ls_snaps(rbd_image_t image, int num_expected, ...)
{
int num_snaps, i, j, max_size = 10;
va_list ap;
rbd_snap_info_t snaps[max_size];
num_snaps = rbd_snap_list(image, snaps, &max_size);
printf("num snaps is: %d\nexpected: %d\n", num_snaps, num_expected);
for (i = 0; i < num_snaps; i++) {
printf("snap: %s\n", snaps[i].name);
}
va_start(ap, num_expected);
for (i = num_expected; i > 0; i--) {
char *expected = va_arg(ap, char *);
uint64_t expected_size = va_arg(ap, uint64_t);
bool found = false;
for (j = 0; j < num_snaps; j++) {
if (snaps[j].name == NULL)
continue;
if (strcmp(snaps[j].name, expected) == 0) {
printf("found %s with size %llu\n", snaps[j].name, (unsigned long long) snaps[j].size);
EXPECT_EQ(expected_size, snaps[j].size);
free((void *) snaps[j].name);
snaps[j].name = NULL;
found = true;
break;
}
}
EXPECT_TRUE(found);
}
va_end(ap);
for (i = 0; i < num_snaps; i++) {
EXPECT_EQ((const char *)0, snaps[i].name);
}
return num_snaps;
}
TEST_F(TestLibRBD, TestCreateLsDeleteSnap)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
uint64_t size2 = 4 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_snap_create(image, "snap1"));
ASSERT_EQ(1, test_ls_snaps(image, 1, "snap1", size));
ASSERT_EQ(0, rbd_resize(image, size2));
ASSERT_EQ(0, rbd_snap_create(image, "snap2"));
ASSERT_EQ(2, test_ls_snaps(image, 2, "snap1", size, "snap2", size2));
ASSERT_EQ(0, rbd_snap_remove(image, "snap1"));
ASSERT_EQ(1, test_ls_snaps(image, 1, "snap2", size2));
ASSERT_EQ(0, rbd_snap_remove(image, "snap2"));
ASSERT_EQ(0, test_ls_snaps(image, 0));
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
int test_get_snapshot_timestamp(rbd_image_t image, uint64_t snap_id)
{
struct timespec timestamp;
EXPECT_EQ(0, rbd_snap_get_timestamp(image, snap_id, ×tamp));
EXPECT_LT(0, timestamp.tv_sec);
return 0;
}
TEST_F(TestLibRBD, TestGetSnapShotTimeStamp)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int num_snaps, max_size = 10;
rbd_snap_info_t snaps[max_size];
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_snap_create(image, "snap1"));
num_snaps = rbd_snap_list(image, snaps, &max_size);
ASSERT_EQ(1, num_snaps);
ASSERT_EQ(0, test_get_snapshot_timestamp(image, snaps[0].id));
free((void *)snaps[0].name);
ASSERT_EQ(0, rbd_snap_create(image, "snap2"));
num_snaps = rbd_snap_list(image, snaps, &max_size);
ASSERT_EQ(2, num_snaps);
ASSERT_EQ(0, test_get_snapshot_timestamp(image, snaps[0].id));
ASSERT_EQ(0, test_get_snapshot_timestamp(image, snaps[1].id));
free((void *)snaps[0].name);
free((void *)snaps[1].name);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
int test_ls_snaps(librbd::Image& image, size_t num_expected, ...)
{
int r;
size_t i, j;
va_list ap;
vector<librbd::snap_info_t> snaps;
r = image.snap_list(snaps);
EXPECT_TRUE(r >= 0);
cout << "num snaps is: " << snaps.size() << std::endl
<< "expected: " << num_expected << std::endl;
for (i = 0; i < snaps.size(); i++) {
cout << "snap: " << snaps[i].name << std::endl;
}
va_start(ap, num_expected);
for (i = num_expected; i > 0; i--) {
char *expected = va_arg(ap, char *);
uint64_t expected_size = va_arg(ap, uint64_t);
int found = 0;
for (j = 0; j < snaps.size(); j++) {
if (snaps[j].name == "")
continue;
if (strcmp(snaps[j].name.c_str(), expected) == 0) {
cout << "found " << snaps[j].name << " with size " << snaps[j].size
<< std::endl;
EXPECT_EQ(expected_size, snaps[j].size);
snaps[j].name = "";
found = 1;
break;
}
}
EXPECT_TRUE(found);
}
va_end(ap);
for (i = 0; i < snaps.size(); i++) {
EXPECT_EQ("", snaps[i].name);
}
return snaps.size();
}
TEST_F(TestLibRBD, TestCreateLsDeleteSnapPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
uint64_t size2 = 4 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool exists;
ASSERT_EQ(0, image.snap_exists2("snap1", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_EQ(0, image.snap_exists2("snap1", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(1, test_ls_snaps(image, 1, "snap1", size));
ASSERT_EQ(0, image.resize(size2));
ASSERT_EQ(0, image.snap_exists2("snap2", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, image.snap_create("snap2"));
ASSERT_EQ(0, image.snap_exists2("snap2", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(2, test_ls_snaps(image, 2, "snap1", size, "snap2", size2));
ASSERT_EQ(0, image.snap_remove("snap1"));
ASSERT_EQ(0, image.snap_exists2("snap1", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(1, test_ls_snaps(image, 1, "snap2", size2));
ASSERT_EQ(0, image.snap_remove("snap2"));
ASSERT_EQ(0, image.snap_exists2("snap2", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, test_ls_snaps(image, 0));
}
ioctx.close();
}
TEST_F(TestLibRBD, TestGetNameIdSnapPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_EQ(0, image.snap_create("snap2"));
ASSERT_EQ(0, image.snap_create("snap3"));
vector<librbd::snap_info_t> snaps;
int r = image.snap_list(snaps);
EXPECT_TRUE(r >= 0);
for (size_t i = 0; i < snaps.size(); ++i) {
std::string expected_snap_name;
image.snap_get_name(snaps[i].id, &expected_snap_name);
ASSERT_EQ(expected_snap_name, snaps[i].name);
}
for (size_t i = 0; i < snaps.size(); ++i) {
uint64_t expected_snap_id;
image.snap_get_id(snaps[i].name, &expected_snap_id);
ASSERT_EQ(expected_snap_id, snaps[i].id);
}
ASSERT_EQ(0, image.snap_remove("snap1"));
ASSERT_EQ(0, image.snap_remove("snap2"));
ASSERT_EQ(0, image.snap_remove("snap3"));
ASSERT_EQ(0, test_ls_snaps(image, 0));
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCreateLsRenameSnapPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
uint64_t size2 = 4 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool exists;
ASSERT_EQ(0, image.snap_exists2("snap1", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_EQ(0, image.snap_exists2("snap1", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(1, test_ls_snaps(image, 1, "snap1", size));
ASSERT_EQ(0, image.resize(size2));
ASSERT_EQ(0, image.snap_exists2("snap2", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, image.snap_create("snap2"));
ASSERT_EQ(0, image.snap_exists2("snap2", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(2, test_ls_snaps(image, 2, "snap1", size, "snap2", size2));
ASSERT_EQ(0, image.snap_rename("snap1","snap1-rename"));
ASSERT_EQ(2, test_ls_snaps(image, 2, "snap1-rename", size, "snap2", size2));
ASSERT_EQ(0, image.snap_exists2("snap1", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, image.snap_exists2("snap1-rename", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(0, image.snap_remove("snap1-rename"));
ASSERT_EQ(0, image.snap_rename("snap2","snap2-rename"));
ASSERT_EQ(1, test_ls_snaps(image, 1, "snap2-rename", size2));
ASSERT_EQ(0, image.snap_exists2("snap2", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, image.snap_exists2("snap2-rename", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(0, image.snap_remove("snap2-rename"));
ASSERT_EQ(0, test_ls_snaps(image, 0));
}
ioctx.close();
}
TEST_F(TestLibRBD, ConcurrentCreatesUnvalidatedPool)
{
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, create_pool(true).c_str(),
&ioctx));
std::vector<std::string> names;
for (int i = 0; i < 4; i++) {
names.push_back(get_temp_image_name());
}
uint64_t size = 2 << 20;
std::vector<std::thread> threads;
for (const auto& name : names) {
threads.emplace_back([ioctx, &name, size]() {
int order = 0;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
});
}
for (auto& thread : threads) {
thread.join();
}
for (const auto& name : names) {
ASSERT_EQ(0, rbd_remove(ioctx, name.c_str()));
}
rados_ioctx_destroy(ioctx);
}
static void remove_full_try(rados_ioctx_t ioctx, const std::string& image_name,
const std::string& data_pool_name)
{
int order = 0;
uint64_t quota = 10 << 20;
uint64_t size = 5 * quota;
ASSERT_EQ(0, create_image(ioctx, image_name.c_str(), size, &order));
std::string cmdstr = "{\"prefix\": \"osd pool set-quota\", \"pool\": \"" +
data_pool_name + "\", \"field\": \"max_bytes\", \"val\": \"" +
std::to_string(quota) + "\"}";
char *cmd[1];
cmd[0] = (char *)cmdstr.c_str();
ASSERT_EQ(0, rados_mon_command(rados_ioctx_get_cluster(ioctx),
(const char **)cmd, 1, "", 0, nullptr, 0,
nullptr, 0));
rados_set_pool_full_try(ioctx);
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, image_name.c_str(), &image, nullptr));
uint64_t off;
size_t len = 1 << 20;
ssize_t ret;
for (off = 0; off < size; off += len) {
ret = rbd_write_zeroes(image, off, len,
RBD_WRITE_ZEROES_FLAG_THICK_PROVISION,
LIBRADOS_OP_FLAG_FADVISE_FUA);
if (ret < 0) {
break;
}
ASSERT_EQ(ret, len);
sleep(1);
}
ASSERT_TRUE(off >= quota && off < size);
ASSERT_EQ(ret, -EDQUOT);
ASSERT_EQ(0, rbd_close(image));
// make sure we have latest map that marked the pool full
ASSERT_EQ(0, rados_wait_for_latest_osdmap(rados_ioctx_get_cluster(ioctx)));
ASSERT_EQ(0, rbd_remove(ioctx, image_name.c_str()));
}
TEST_F(TestLibRBD, RemoveFullTry)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
REQUIRE(!is_librados_test_stub(_rados));
rados_ioctx_t ioctx;
auto pool_name = create_pool(true);
ASSERT_EQ(0, rados_ioctx_create(_cluster, pool_name.c_str(), &ioctx));
// cancel out rbd_default_data_pool -- we need an image without
// a separate data pool
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "conf_rbd_default_data_pool",
pool_name.c_str()));
int order = 0;
auto image_name = get_temp_image_name();
// FIXME: this is a workaround for rbd_trash object being created
// on the first remove -- pre-create it to avoid bumping into quota
ASSERT_EQ(0, create_image(ioctx, image_name.c_str(), 0, &order));
ASSERT_EQ(0, rbd_remove(ioctx, image_name.c_str()));
remove_full_try(ioctx, image_name, pool_name);
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, RemoveFullTryDataPool)
{
REQUIRE_FORMAT_V2();
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
REQUIRE(!is_librados_test_stub(_rados));
rados_ioctx_t ioctx;
auto pool_name = create_pool(true);
auto data_pool_name = create_pool(true);
ASSERT_EQ(0, rados_ioctx_create(_cluster, pool_name.c_str(), &ioctx));
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "conf_rbd_default_data_pool",
data_pool_name.c_str()));
auto image_name = get_temp_image_name();
remove_full_try(ioctx, image_name, data_pool_name);
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestIO)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rados_conf_set(_cluster, "rbd_read_from_replica_policy", "balance"));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
test_io(image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestEncryptionLUKS1)
{
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 32 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rados_conf_set(
_cluster, "rbd_read_from_replica_policy", "balance"));
rbd_image_t image;
rbd_encryption_luks1_format_options_t luks1_opts = {
.alg = RBD_ENCRYPTION_ALGORITHM_AES256,
.passphrase = "password",
.passphrase_size = 8,
};
rbd_encryption_luks2_format_options_t luks2_opts = {
.alg = RBD_ENCRYPTION_ALGORITHM_AES256,
.passphrase = "password",
.passphrase_size = 8,
};
rbd_encryption_luks_format_options_t luks_opts = {
.passphrase = "password",
.passphrase_size = 8,
};
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
#ifndef HAVE_LIBCRYPTSETUP
ASSERT_EQ(-ENOTSUP, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)));
#else
ASSERT_EQ(-EINVAL, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)));
ASSERT_EQ(0, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
ASSERT_EQ(-EEXIST, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
test_io(image);
ASSERT_PASSED(write_test_data, image, "test", 0, 4, 0);
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(-EINVAL, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
ASSERT_EQ(0, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
ASSERT_PASSED(read_test_data, image, "test", 0, 4, 0);
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(-EINVAL, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
ASSERT_EQ(0, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)));
ASSERT_PASSED(read_test_data, image, "test", 0, 4, 0);
#endif
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestEncryptionLUKS2)
{
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 32 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rados_conf_set(
_cluster, "rbd_read_from_replica_policy", "balance"));
rbd_image_t image;
rbd_encryption_luks1_format_options_t luks1_opts = {
.alg = RBD_ENCRYPTION_ALGORITHM_AES256,
.passphrase = "password",
.passphrase_size = 8,
};
rbd_encryption_luks2_format_options_t luks2_opts = {
.alg = RBD_ENCRYPTION_ALGORITHM_AES256,
.passphrase = "password",
.passphrase_size = 8,
};
rbd_encryption_luks_format_options_t luks_opts = {
.passphrase = "password",
.passphrase_size = 8,
};
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
#ifndef HAVE_LIBCRYPTSETUP
ASSERT_EQ(-ENOTSUP, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)));
ASSERT_EQ(-ENOTSUP, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)));
#else
ASSERT_EQ(-EINVAL, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)));
ASSERT_EQ(0, rbd_encryption_format(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
ASSERT_EQ(-EEXIST, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
test_io(image);
ASSERT_PASSED(write_test_data, image, "test", 0, 4, 0);
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(-EINVAL, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
ASSERT_EQ(0, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)));
ASSERT_PASSED(read_test_data, image, "test", 0, 4, 0);
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(-EINVAL, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)));
ASSERT_EQ(0, rbd_encryption_load(
image, RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)));
ASSERT_PASSED(read_test_data, image, "test", 0, 4, 0);
#endif
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
#ifdef HAVE_LIBCRYPTSETUP
TEST_F(TestLibRBD, TestCloneEncryption)
{
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
ASSERT_EQ(0, rados_conf_set(
_cluster, "rbd_read_from_replica_policy", "balance"));
// create base image, write 'a's
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 256 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_PASSED(write_test_data, image, "aaaa", 0, 4, 0);
ASSERT_EQ(0, rbd_flush(image));
// clone, encrypt with LUKS1, write 'b's
ASSERT_EQ(0, rbd_snap_create(image, "snap"));
ASSERT_EQ(0, rbd_snap_protect(image, "snap"));
rbd_image_options_t image_opts;
rbd_image_options_create(&image_opts);
BOOST_SCOPE_EXIT_ALL( (&image_opts) ) {
rbd_image_options_destroy(image_opts);
};
std::string child1_name = get_temp_image_name();
ASSERT_EQ(0, rbd_clone3(ioctx, name.c_str(), "snap", ioctx,
child1_name.c_str(), image_opts));
rbd_image_t child1;
ASSERT_EQ(0, rbd_open(ioctx, child1_name.c_str(), &child1, NULL));
rbd_encryption_luks1_format_options_t child1_opts = {
.alg = RBD_ENCRYPTION_ALGORITHM_AES256,
.passphrase = "password",
.passphrase_size = 8,
};
ASSERT_EQ(-EINVAL, rbd_encryption_load(
child1, RBD_ENCRYPTION_FORMAT_LUKS1, &child1_opts,
sizeof(child1_opts)));
ASSERT_EQ(0, rbd_encryption_format(
child1, RBD_ENCRYPTION_FORMAT_LUKS1, &child1_opts,
sizeof(child1_opts)));
ASSERT_EQ(0, rbd_encryption_load(
child1, RBD_ENCRYPTION_FORMAT_LUKS1, &child1_opts,
sizeof(child1_opts)));
ASSERT_PASSED(write_test_data, child1, "bbbb", 64 << 20, 4, 0);
ASSERT_EQ(0, rbd_flush(child1));
// clone, encrypt with LUKS2 (same passphrase), write 'c's
ASSERT_EQ(0, rbd_snap_create(child1, "snap"));
ASSERT_EQ(0, rbd_snap_protect(child1, "snap"));
std::string child2_name = get_temp_image_name();
ASSERT_EQ(0, rbd_clone3(ioctx, child1_name.c_str(), "snap", ioctx,
child2_name.c_str(), image_opts));
rbd_image_t child2;
ASSERT_EQ(0, rbd_open(ioctx, child2_name.c_str(), &child2, NULL));
rbd_encryption_luks2_format_options_t child2_opts = {
.alg = RBD_ENCRYPTION_ALGORITHM_AES256,
.passphrase = "password",
.passphrase_size = 8,
};
ASSERT_EQ(0, rbd_encryption_format(
child2, RBD_ENCRYPTION_FORMAT_LUKS2, &child2_opts,
sizeof(child2_opts)));
rbd_encryption_luks_format_options_t child2_lopts = {
.passphrase = "password",
.passphrase_size = 8,
};
ASSERT_EQ(0, rbd_encryption_load(
child2, RBD_ENCRYPTION_FORMAT_LUKS, &child2_lopts,
sizeof(child2_lopts)));
ASSERT_PASSED(write_test_data, child2, "cccc", 128 << 20, 4, 0);
ASSERT_EQ(0, rbd_flush(child2));
// clone, encrypt with LUKS2 (different passphrase)
ASSERT_EQ(0, rbd_snap_create(child2, "snap"));
ASSERT_EQ(0, rbd_snap_protect(child2, "snap"));
std::string child3_name = get_temp_image_name();
ASSERT_EQ(0, rbd_clone3(ioctx, child2_name.c_str(), "snap", ioctx,
child3_name.c_str(), image_opts));
rbd_image_t child3;
ASSERT_EQ(0, rbd_open(ioctx, child3_name.c_str(), &child3, NULL));
rbd_encryption_luks2_format_options_t child3_opts = {
.alg = RBD_ENCRYPTION_ALGORITHM_AES256,
.passphrase = "12345678",
.passphrase_size = 8,
};
ASSERT_EQ(0, rbd_encryption_format(
child3, RBD_ENCRYPTION_FORMAT_LUKS2, &child3_opts,
sizeof(child3_opts)));
ASSERT_EQ(-EPERM, rbd_encryption_load(
child3, RBD_ENCRYPTION_FORMAT_LUKS2, &child3_opts,
sizeof(child3_opts)));
// verify child3 data
rbd_encryption_spec_t specs[] = {
{ .format = RBD_ENCRYPTION_FORMAT_LUKS2,
.opts = &child3_opts,
.opts_size = sizeof(child3_opts)},
{ .format = RBD_ENCRYPTION_FORMAT_LUKS2,
.opts = &child2_opts,
.opts_size = sizeof(child2_opts)},
{ .format = RBD_ENCRYPTION_FORMAT_LUKS1,
.opts = &child1_opts,
.opts_size = sizeof(child1_opts)}
};
ASSERT_EQ(0, rbd_encryption_load2(child3, specs, 3));
ASSERT_PASSED(read_test_data, child3, "aaaa", 0, 4, 0);
ASSERT_PASSED(read_test_data, child3, "bbbb", 64 << 20, 4, 0);
ASSERT_PASSED(read_test_data, child3, "cccc", 128 << 20, 4, 0);
// clone without formatting
ASSERT_EQ(0, rbd_snap_create(child3, "snap"));
ASSERT_EQ(0, rbd_snap_protect(child3, "snap"));
std::string child4_name = get_temp_image_name();
ASSERT_EQ(0, rbd_clone3(ioctx, child3_name.c_str(), "snap", ioctx,
child4_name.c_str(), image_opts));
rbd_image_t child4;
ASSERT_EQ(0, rbd_open(ioctx, child4_name.c_str(), &child4, NULL));
rbd_encryption_spec_t child4_specs[] = {
{ .format = RBD_ENCRYPTION_FORMAT_LUKS2,
.opts = &child3_opts,
.opts_size = sizeof(child3_opts)},
{ .format = RBD_ENCRYPTION_FORMAT_LUKS2,
.opts = &child3_opts,
.opts_size = sizeof(child3_opts)},
{ .format = RBD_ENCRYPTION_FORMAT_LUKS2,
.opts = &child2_opts,
.opts_size = sizeof(child2_opts)},
{ .format = RBD_ENCRYPTION_FORMAT_LUKS1,
.opts = &child1_opts,
.opts_size = sizeof(child1_opts)}
};
ASSERT_EQ(0, rbd_encryption_load2(child4, child4_specs, 4));
// flatten child4
ASSERT_EQ(0, rbd_flatten(child4));
// reopen child4 and load encryption
ASSERT_EQ(0, rbd_close(child4));
ASSERT_EQ(0, rbd_open(ioctx, child4_name.c_str(), &child4, NULL));
ASSERT_EQ(0, rbd_encryption_load(
child4, RBD_ENCRYPTION_FORMAT_LUKS2, &child3_opts,
sizeof(child3_opts)));
// verify flattend image
ASSERT_PASSED(read_test_data, child4, "aaaa", 0, 4, 0);
ASSERT_PASSED(read_test_data, child4, "bbbb", 64 << 20, 4, 0);
ASSERT_PASSED(read_test_data, child4, "cccc", 128 << 20, 4, 0);
ASSERT_EQ(0, rbd_close(child4));
ASSERT_EQ(0, rbd_remove(ioctx, child4_name.c_str()));
ASSERT_EQ(0, rbd_snap_unprotect(child3, "snap"));
ASSERT_EQ(0, rbd_snap_remove(child3, "snap"));
ASSERT_EQ(0, rbd_close(child3));
ASSERT_EQ(0, rbd_remove(ioctx, child3_name.c_str()));
ASSERT_EQ(0, rbd_snap_unprotect(child2, "snap"));
ASSERT_EQ(0, rbd_snap_remove(child2, "snap"));
ASSERT_EQ(0, rbd_close(child2));
ASSERT_EQ(0, rbd_remove(ioctx, child2_name.c_str()));
ASSERT_EQ(0, rbd_snap_unprotect(child1, "snap"));
ASSERT_EQ(0, rbd_snap_remove(child1, "snap"));
ASSERT_EQ(0, rbd_close(child1));
ASSERT_EQ(0, rbd_remove(ioctx, child1_name.c_str()));
ASSERT_EQ(0, rbd_snap_unprotect(image, "snap"));
ASSERT_EQ(0, rbd_snap_remove(image, "snap"));
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_remove(ioctx, name.c_str()));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, LUKS1UnderLUKS2WithoutResize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string parent_name = get_temp_image_name();
std::string clone_name = get_temp_image_name();
uint64_t data_size = 25 << 20;
uint64_t luks1_meta_size = 4 << 20;
uint64_t luks2_meta_size = 16 << 20;
std::string parent_passphrase = "parent passphrase";
std::string clone_passphrase = "clone passphrase";
{
int order = 22;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(),
luks1_meta_size + data_size, &order));
librbd::Image parent;
ASSERT_EQ(0, rbd.open(ioctx, parent, parent_name.c_str(), nullptr));
librbd::encryption_luks1_format_options_t fopts = {
RBD_ENCRYPTION_ALGORITHM_AES256, parent_passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &fopts,
sizeof(fopts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
ASSERT_EQ(0, parent.snap_create("snap"));
ASSERT_EQ(0, parent.snap_protect("snap"));
uint64_t features;
ASSERT_EQ(0, parent.features(&features));
ASSERT_EQ(0, rbd.clone(ioctx, parent_name.c_str(), "snap", ioctx,
clone_name.c_str(), features, &order));
}
{
librbd::Image clone;
ASSERT_EQ(0, rbd.open(ioctx, clone, clone_name.c_str(), nullptr));
librbd::encryption_luks2_format_options_t fopts =
{RBD_ENCRYPTION_ALGORITHM_AES256, clone_passphrase};
ASSERT_EQ(0, clone.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &fopts,
sizeof(fopts)));
librbd::encryption_luks_format_options_t opts1 = {parent_passphrase};
librbd::encryption_luks_format_options_t opts2 = {clone_passphrase};
librbd::encryption_spec_t specs[] = {
{RBD_ENCRYPTION_FORMAT_LUKS, &opts2, sizeof(opts2)},
{RBD_ENCRYPTION_FORMAT_LUKS, &opts1, sizeof(opts1)}};
ASSERT_EQ(0, clone.encryption_load2(specs, std::size(specs)));
uint64_t size;
ASSERT_EQ(0, clone.size(&size));
EXPECT_EQ(data_size + luks1_meta_size - luks2_meta_size, size);
uint64_t overlap;
ASSERT_EQ(0, clone.overlap(&overlap));
EXPECT_EQ(data_size + luks1_meta_size - luks2_meta_size, overlap);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(
data_size + luks1_meta_size - luks2_meta_size, 'a'));
ceph::bufferlist read_bl;
ASSERT_EQ(expected_bl.length(),
clone.read(0, expected_bl.length(), read_bl));
EXPECT_TRUE(expected_bl.contents_equal(read_bl));
}
}
TEST_F(TestLibRBD, LUKS2UnderLUKS1WithoutResize)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string parent_name = get_temp_image_name();
std::string clone_name = get_temp_image_name();
uint64_t data_size = 25 << 20;
uint64_t luks1_meta_size = 4 << 20;
uint64_t luks2_meta_size = 16 << 20;
std::string parent_passphrase = "parent passphrase";
std::string clone_passphrase = "clone passphrase";
{
int order = 22;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(),
luks2_meta_size + data_size, &order));
librbd::Image parent;
ASSERT_EQ(0, rbd.open(ioctx, parent, parent_name.c_str(), nullptr));
librbd::encryption_luks2_format_options_t fopts = {
RBD_ENCRYPTION_ALGORITHM_AES256, parent_passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &fopts,
sizeof(fopts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
ASSERT_EQ(0, parent.snap_create("snap"));
ASSERT_EQ(0, parent.snap_protect("snap"));
uint64_t features;
ASSERT_EQ(0, parent.features(&features));
ASSERT_EQ(0, rbd.clone(ioctx, parent_name.c_str(), "snap", ioctx,
clone_name.c_str(), features, &order));
}
{
librbd::Image clone;
ASSERT_EQ(0, rbd.open(ioctx, clone, clone_name.c_str(), nullptr));
librbd::encryption_luks1_format_options_t fopts =
{RBD_ENCRYPTION_ALGORITHM_AES256, clone_passphrase};
ASSERT_EQ(0, clone.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &fopts,
sizeof(fopts)));
librbd::encryption_luks_format_options_t opts1 = {parent_passphrase};
librbd::encryption_luks_format_options_t opts2 = {clone_passphrase};
librbd::encryption_spec_t specs[] = {
{RBD_ENCRYPTION_FORMAT_LUKS, &opts2, sizeof(opts2)},
{RBD_ENCRYPTION_FORMAT_LUKS, &opts1, sizeof(opts1)}};
ASSERT_EQ(0, clone.encryption_load2(specs, std::size(specs)));
uint64_t size;
ASSERT_EQ(0, clone.size(&size));
EXPECT_EQ(data_size + luks2_meta_size - luks1_meta_size, size);
uint64_t overlap;
ASSERT_EQ(0, clone.overlap(&overlap));
EXPECT_EQ(data_size, overlap);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(data_size, 'a'));
expected_bl.append_zero(luks2_meta_size - luks1_meta_size);
ceph::bufferlist read_bl;
ASSERT_EQ(expected_bl.length(),
clone.read(0, expected_bl.length(), read_bl));
EXPECT_TRUE(expected_bl.contents_equal(read_bl));
}
}
TEST_F(TestLibRBD, EncryptionFormatNoData)
{
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
auto name = get_temp_image_name();
uint64_t luks1_meta_size = 4 << 20;
std::string passphrase = "some passphrase";
{
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), luks1_meta_size - 1,
&order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
librbd::encryption_luks1_format_options_t opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
ASSERT_EQ(-ENOSPC, image.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1,
&opts, sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(luks1_meta_size - 1, size);
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
ASSERT_EQ(0, image.resize(luks1_meta_size));
librbd::encryption_luks1_format_options_t opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
ASSERT_EQ(0, image.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(0, size);
}
}
TEST_F(TestLibRBD, EncryptionLoadBadSize)
{
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
auto name = get_temp_image_name();
uint64_t luks1_meta_size = 4 << 20;
std::string passphrase = "some passphrase";
{
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), luks1_meta_size,
&order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
librbd::encryption_luks1_format_options_t opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
ASSERT_EQ(0, image.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &opts,
sizeof(opts)));
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(0, image.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(0, size);
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
ASSERT_EQ(0, image.resize(luks1_meta_size - 1));
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(-EINVAL, image.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(luks1_meta_size - 1, size);
}
}
TEST_F(TestLibRBD, EncryptionLoadBadStripePattern)
{
REQUIRE_FEATURE(RBD_FEATURE_STRIPINGV2);
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
bool old_format;
uint64_t features;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
librbd::RBD rbd;
auto name1 = get_temp_image_name();
auto name2 = get_temp_image_name();
auto name3 = get_temp_image_name();
std::string passphrase = "some passphrase";
{
int order = 22;
ASSERT_EQ(0, rbd.create3(ioctx, name1.c_str(), 20 << 20, features, &order,
2 << 20, 2));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name1.c_str(), nullptr));
librbd::encryption_luks1_format_options_t opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
ASSERT_EQ(0, image.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(12 << 20, size);
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name1.c_str(), nullptr));
// different but compatible striping pattern
librbd::ImageOptions image_opts;
ASSERT_EQ(0, image_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, 1 << 20));
ASSERT_EQ(0, image_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, 2));
ASSERT_EQ(0, image.deep_copy(ioctx, name2.c_str(), image_opts));
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name2.c_str(), nullptr));
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(0, image.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(12 << 20, size);
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name1.c_str(), nullptr));
// incompatible striping pattern
librbd::ImageOptions image_opts;
ASSERT_EQ(0, image_opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, 1 << 20));
ASSERT_EQ(0, image_opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, 3));
ASSERT_EQ(0, image.deep_copy(ioctx, name3.c_str(), image_opts));
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name3.c_str(), nullptr));
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(-EINVAL, image.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(20 << 20, size);
}
}
TEST_F(TestLibRBD, EncryptionLoadFormatMismatch)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name1 = get_temp_image_name();
std::string name2 = get_temp_image_name();
std::string name3 = get_temp_image_name();
std::string passphrase = "some passphrase";
librbd::encryption_luks1_format_options_t luks1_opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
librbd::encryption_luks2_format_options_t luks2_opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
librbd::encryption_luks_format_options_t luks_opts = {passphrase};
#define LUKS_ONE {RBD_ENCRYPTION_FORMAT_LUKS1, &luks1_opts, sizeof(luks1_opts)}
#define LUKS_TWO {RBD_ENCRYPTION_FORMAT_LUKS2, &luks2_opts, sizeof(luks2_opts)}
#define LUKS_ANY {RBD_ENCRYPTION_FORMAT_LUKS, &luks_opts, sizeof(luks_opts)}
const std::vector<librbd::encryption_spec_t> bad_specs[] = {
{},
{LUKS_ONE},
{LUKS_TWO},
{LUKS_ONE, LUKS_ONE},
{LUKS_ONE, LUKS_TWO},
{LUKS_ONE, LUKS_ANY},
{LUKS_TWO, LUKS_TWO},
{LUKS_ANY, LUKS_TWO},
{LUKS_ONE, LUKS_ONE, LUKS_ONE},
{LUKS_ONE, LUKS_ONE, LUKS_TWO},
{LUKS_ONE, LUKS_ONE, LUKS_ANY},
{LUKS_ONE, LUKS_TWO, LUKS_ONE},
{LUKS_ONE, LUKS_TWO, LUKS_TWO},
{LUKS_ONE, LUKS_TWO, LUKS_ANY},
{LUKS_ONE, LUKS_ANY, LUKS_ONE},
{LUKS_ONE, LUKS_ANY, LUKS_TWO},
{LUKS_ONE, LUKS_ANY, LUKS_ANY},
{LUKS_TWO, LUKS_ONE, LUKS_TWO},
{LUKS_TWO, LUKS_TWO, LUKS_ONE},
{LUKS_TWO, LUKS_TWO, LUKS_TWO},
{LUKS_TWO, LUKS_TWO, LUKS_ANY},
{LUKS_TWO, LUKS_ANY, LUKS_TWO},
{LUKS_ANY, LUKS_ONE, LUKS_TWO},
{LUKS_ANY, LUKS_TWO, LUKS_ONE},
{LUKS_ANY, LUKS_TWO, LUKS_TWO},
{LUKS_ANY, LUKS_TWO, LUKS_ANY},
{LUKS_ANY, LUKS_ANY, LUKS_TWO},
{LUKS_ANY, LUKS_ANY, LUKS_ANY, LUKS_ANY}};
const std::vector<librbd::encryption_spec_t> good_specs[] = {
{LUKS_ANY},
{LUKS_TWO, LUKS_ONE},
{LUKS_TWO, LUKS_ANY},
{LUKS_ANY, LUKS_ONE},
{LUKS_ANY, LUKS_ANY},
{LUKS_TWO, LUKS_ONE, LUKS_ONE},
{LUKS_TWO, LUKS_ONE, LUKS_ANY},
{LUKS_TWO, LUKS_ANY, LUKS_ONE},
{LUKS_TWO, LUKS_ANY, LUKS_ANY},
{LUKS_ANY, LUKS_ONE, LUKS_ONE},
{LUKS_ANY, LUKS_ONE, LUKS_ANY},
{LUKS_ANY, LUKS_ANY, LUKS_ONE},
{LUKS_ANY, LUKS_ANY, LUKS_ANY}};
static_assert(std::size(bad_specs) + std::size(good_specs) == 1 + 3 + 9 + 27 + 1);
#undef LUKS_ONE
#undef LUKS_TWO
#undef LUKS_ANY
{
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name1.c_str(), 20 << 20, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name1.c_str(), nullptr));
ASSERT_EQ(0, image1.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1,
&luks1_opts, sizeof(luks1_opts)));
ASSERT_EQ(0, image1.snap_create("snap"));
ASSERT_EQ(0, image1.snap_protect("snap"));
uint64_t features;
ASSERT_EQ(0, image1.features(&features));
ASSERT_EQ(0, rbd.clone(ioctx, name1.c_str(), "snap", ioctx, name2.c_str(),
features, &order));
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name2.c_str(), nullptr));
ASSERT_EQ(0, image2.snap_create("snap"));
ASSERT_EQ(0, image2.snap_protect("snap"));
ASSERT_EQ(0, rbd.clone(ioctx, name2.c_str(), "snap", ioctx, name3.c_str(),
features, &order));
librbd::Image image3;
ASSERT_EQ(0, rbd.open(ioctx, image3, name3.c_str(), nullptr));
ASSERT_EQ(0, image3.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2,
&luks2_opts, sizeof(luks2_opts)));
}
{
librbd::Image image3;
ASSERT_EQ(0, rbd.open(ioctx, image3, name3.c_str(), nullptr));
for (auto& specs : bad_specs) {
ASSERT_EQ(-EINVAL, image3.encryption_load2(specs.data(), specs.size()));
}
}
for (auto& specs : good_specs) {
librbd::Image image3;
ASSERT_EQ(0, rbd.open(ioctx, image3, name3.c_str(), nullptr));
ASSERT_EQ(0, image3.encryption_load2(specs.data(), specs.size()));
}
}
TEST_F(TestLibRBD, EncryptedResize)
{
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
auto name = get_temp_image_name();
uint64_t luks2_meta_size = 16 << 20;
uint64_t data_size = 10 << 20;
std::string passphrase = "some passphrase";
{
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(),
luks2_meta_size + data_size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(luks2_meta_size + data_size, size);
librbd::encryption_luks2_format_options_t opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
ASSERT_EQ(0, image.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &opts,
sizeof(opts)));
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(data_size, size);
ASSERT_EQ(0, image.resize(data_size * 3));
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(data_size * 3, size);
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(luks2_meta_size + data_size * 3, size);
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(0, image.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(data_size * 3, size);
ASSERT_EQ(0, image.resize(data_size / 2));
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(data_size / 2, size);
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(luks2_meta_size + data_size / 2, size);
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(0, image.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(data_size / 2, size);
ASSERT_EQ(0, image.resize(0));
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(0, size);
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(luks2_meta_size, size);
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(0, image.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(0, size);
ASSERT_EQ(0, image.resize(data_size));
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(data_size, size);
}
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
uint64_t size;
ASSERT_EQ(0, image.size(&size));
ASSERT_EQ(luks2_meta_size + data_size, size);
}
}
TEST_F(TestLibRBD, EncryptedFlattenSmallData)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string parent_name = get_temp_image_name();
std::string clone_name = get_temp_image_name();
uint64_t data_size = 5000;
uint64_t luks2_meta_size = 16 << 20;
std::string passphrase = "some passphrase";
{
int order = 22;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(),
luks2_meta_size + data_size, &order));
librbd::Image parent;
ASSERT_EQ(0, rbd.open(ioctx, parent, parent_name.c_str(), nullptr));
librbd::encryption_luks2_format_options_t opts = {
RBD_ENCRYPTION_ALGORITHM_AES256, passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &opts,
sizeof(opts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
ASSERT_EQ(0, parent.snap_create("snap"));
ASSERT_EQ(0, parent.snap_protect("snap"));
uint64_t features;
ASSERT_EQ(0, parent.features(&features));
ASSERT_EQ(0, rbd.clone(ioctx, parent_name.c_str(), "snap", ioctx,
clone_name.c_str(), features, &order));
}
{
librbd::Image clone;
ASSERT_EQ(0, rbd.open(ioctx, clone, clone_name.c_str(), nullptr));
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(0, clone.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, clone.size(&size));
ASSERT_EQ(data_size, size);
uint64_t overlap;
ASSERT_EQ(0, clone.overlap(&overlap));
ASSERT_EQ(data_size, overlap);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(data_size, 'a'));
ceph::bufferlist read_bl1;
ASSERT_EQ(data_size, clone.read(0, data_size, read_bl1));
ASSERT_TRUE(expected_bl.contents_equal(read_bl1));
ASSERT_EQ(0, clone.flatten());
ceph::bufferlist read_bl2;
ASSERT_EQ(data_size, clone.read(0, data_size, read_bl2));
ASSERT_TRUE(expected_bl.contents_equal(read_bl2));
}
{
librbd::Image clone;
ASSERT_EQ(0, rbd.open(ioctx, clone, clone_name.c_str(), nullptr));
librbd::encryption_luks_format_options_t opts = {passphrase};
ASSERT_EQ(0, clone.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts)));
uint64_t size;
ASSERT_EQ(0, clone.size(&size));
ASSERT_EQ(data_size, size);
uint64_t overlap;
ASSERT_EQ(0, clone.overlap(&overlap));
ASSERT_EQ(0, overlap);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(data_size, 'a'));
ceph::bufferlist read_bl;
ASSERT_EQ(data_size, clone.read(0, data_size, read_bl));
ASSERT_TRUE(expected_bl.contents_equal(read_bl));
}
}
struct LUKSOnePassphrase {
int load(librbd::Image& clone) {
librbd::encryption_luks_format_options_t opts = {m_passphrase};
return clone.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts));
}
int load_flattened(librbd::Image& clone) {
return load(clone);
}
std::string m_passphrase = "some passphrase";
};
struct LUKSTwoPassphrases {
int load(librbd::Image& clone) {
librbd::encryption_luks_format_options_t opts1 = {m_parent_passphrase};
librbd::encryption_luks_format_options_t opts2 = {m_clone_passphrase};
librbd::encryption_spec_t specs[] = {
{RBD_ENCRYPTION_FORMAT_LUKS, &opts2, sizeof(opts2)},
{RBD_ENCRYPTION_FORMAT_LUKS, &opts1, sizeof(opts1)}};
return clone.encryption_load2(specs, std::size(specs));
}
int load_flattened(librbd::Image& clone) {
librbd::encryption_luks_format_options_t opts = {m_clone_passphrase};
return clone.encryption_load(RBD_ENCRYPTION_FORMAT_LUKS, &opts,
sizeof(opts));
}
std::string m_parent_passphrase = "parent passphrase";
std::string m_clone_passphrase = "clone passphrase";
};
struct PlaintextUnderLUKS1 : LUKSOnePassphrase {
protected:
void setup_parent(librbd::Image& parent, uint64_t data_size, bool* passed) {
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
// before taking a parent snapshot, (temporarily) add extra space
// to the parent to account for upcoming LUKS1 header in the clone,
// making the clone able to reach all parent data
uint64_t luks1_meta_size = 4 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks1_meta_size));
*passed = true;
}
void setup_clone(librbd::Image& clone, uint64_t data_size, bool* passed) {
librbd::encryption_luks1_format_options_t fopts =
{RBD_ENCRYPTION_ALGORITHM_AES256, m_passphrase};
ASSERT_EQ(0, clone.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &fopts,
sizeof(fopts)));
*passed = true;
}
};
struct PlaintextUnderLUKS2 : LUKSOnePassphrase {
void setup_parent(librbd::Image& parent, uint64_t data_size, bool* passed) {
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
// before taking a parent snapshot, (temporarily) add extra space
// to the parent to account for upcoming LUKS2 header in the clone,
// making the clone able to reach all parent data
uint64_t luks2_meta_size = 16 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks2_meta_size));
*passed = true;
}
void setup_clone(librbd::Image& clone, uint64_t data_size, bool* passed) {
librbd::encryption_luks2_format_options_t fopts =
{RBD_ENCRYPTION_ALGORITHM_AES256, m_passphrase};
ASSERT_EQ(0, clone.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &fopts,
sizeof(fopts)));
*passed = true;
}
};
struct UnformattedLUKS1 : LUKSOnePassphrase {
void setup_parent(librbd::Image& parent, uint64_t data_size, bool* passed) {
uint64_t luks1_meta_size = 4 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks1_meta_size));
librbd::encryption_luks1_format_options_t fopts = {
RBD_ENCRYPTION_ALGORITHM_AES256, m_passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &fopts,
sizeof(fopts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
*passed = true;
}
void setup_clone(librbd::Image& clone, uint64_t data_size, bool* passed) {
*passed = true;
}
};
struct LUKS1UnderLUKS1 : LUKSTwoPassphrases {
void setup_parent(librbd::Image& parent, uint64_t data_size, bool* passed) {
uint64_t luks1_meta_size = 4 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks1_meta_size));
librbd::encryption_luks1_format_options_t fopts = {
RBD_ENCRYPTION_ALGORITHM_AES256, m_parent_passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &fopts,
sizeof(fopts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
*passed = true;
}
void setup_clone(librbd::Image& clone, uint64_t data_size, bool* passed) {
librbd::encryption_luks1_format_options_t fopts =
{RBD_ENCRYPTION_ALGORITHM_AES256, m_clone_passphrase};
ASSERT_EQ(0, clone.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &fopts,
sizeof(fopts)));
*passed = true;
}
};
struct LUKS1UnderLUKS2 : LUKSTwoPassphrases {
void setup_parent(librbd::Image& parent, uint64_t data_size, bool* passed) {
uint64_t luks1_meta_size = 4 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks1_meta_size));
librbd::encryption_luks1_format_options_t fopts = {
RBD_ENCRYPTION_ALGORITHM_AES256, m_parent_passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &fopts,
sizeof(fopts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
// before taking a parent snapshot, (temporarily) add extra space
// to the parent to account for upcoming LUKS2 header in the clone,
// making the clone able to reach all parent data
// space taken by LUKS1 header in the parent would be reused
uint64_t luks2_meta_size = 16 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks2_meta_size - luks1_meta_size));
*passed = true;
}
void setup_clone(librbd::Image& clone, uint64_t data_size, bool* passed) {
librbd::encryption_luks2_format_options_t fopts =
{RBD_ENCRYPTION_ALGORITHM_AES256, m_clone_passphrase};
ASSERT_EQ(0, clone.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &fopts,
sizeof(fopts)));
*passed = true;
}
};
struct UnformattedLUKS2 : LUKSOnePassphrase {
void setup_parent(librbd::Image& parent, uint64_t data_size, bool* passed) {
uint64_t luks2_meta_size = 16 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks2_meta_size));
librbd::encryption_luks2_format_options_t fopts = {
RBD_ENCRYPTION_ALGORITHM_AES256, m_passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &fopts,
sizeof(fopts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
*passed = true;
}
void setup_clone(librbd::Image& clone, uint64_t data_size, bool* passed) {
*passed = true;
}
};
struct LUKS2UnderLUKS2 : LUKSTwoPassphrases {
void setup_parent(librbd::Image& parent, uint64_t data_size, bool* passed) {
uint64_t luks2_meta_size = 16 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks2_meta_size));
librbd::encryption_luks2_format_options_t fopts = {
RBD_ENCRYPTION_ALGORITHM_AES256, m_parent_passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &fopts,
sizeof(fopts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
*passed = true;
}
void setup_clone(librbd::Image& clone, uint64_t data_size, bool* passed) {
librbd::encryption_luks2_format_options_t fopts =
{RBD_ENCRYPTION_ALGORITHM_AES256, m_clone_passphrase};
ASSERT_EQ(0, clone.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &fopts,
sizeof(fopts)));
*passed = true;
}
};
struct LUKS2UnderLUKS1 : LUKSTwoPassphrases {
void setup_parent(librbd::Image& parent, uint64_t data_size, bool* passed) {
uint64_t luks2_meta_size = 16 << 20;
ASSERT_EQ(0, parent.resize(data_size + luks2_meta_size));
librbd::encryption_luks2_format_options_t fopts = {
RBD_ENCRYPTION_ALGORITHM_AES256, m_parent_passphrase};
ASSERT_EQ(0, parent.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS2, &fopts,
sizeof(fopts)));
ceph::bufferlist bl;
bl.append(std::string(data_size, 'a'));
ASSERT_EQ(data_size, parent.write(0, data_size, bl));
*passed = true;
}
void setup_clone(librbd::Image& clone, uint64_t data_size, bool* passed) {
librbd::encryption_luks1_format_options_t fopts =
{RBD_ENCRYPTION_ALGORITHM_AES256, m_clone_passphrase};
ASSERT_EQ(0, clone.encryption_format(RBD_ENCRYPTION_FORMAT_LUKS1, &fopts,
sizeof(fopts)));
// after loading encryption on the clone, one can get rid of
// unneeded space allowance in the clone arising from LUKS2 header
// in the parent being bigger than LUKS1 header in the clone
ASSERT_EQ(0, load(clone));
ASSERT_EQ(0, clone.resize(data_size));
*passed = true;
}
};
template <typename FormatPolicy>
class EncryptedFlattenTest : public TestLibRBD, FormatPolicy {
protected:
void create_and_setup(bool* passed) {
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), m_ioctx));
int order = 22;
ASSERT_EQ(0, create_image_pp(m_rbd, m_ioctx, m_parent_name.c_str(),
m_data_size, &order));
librbd::Image parent;
ASSERT_EQ(0, m_rbd.open(m_ioctx, parent, m_parent_name.c_str(), nullptr));
ASSERT_PASSED(FormatPolicy::setup_parent, parent, m_data_size);
ASSERT_EQ(0, parent.snap_create("snap"));
ASSERT_EQ(0, parent.snap_protect("snap"));
uint64_t features;
ASSERT_EQ(0, parent.features(&features));
ASSERT_EQ(0, m_rbd.clone(m_ioctx, m_parent_name.c_str(), "snap", m_ioctx,
m_clone_name.c_str(), features, &order));
librbd::Image clone;
ASSERT_EQ(0, m_rbd.open(m_ioctx, clone, m_clone_name.c_str(), nullptr));
ASSERT_PASSED(FormatPolicy::setup_clone, clone, m_data_size);
*passed = true;
}
void open_and_load(librbd::Image& clone, bool* passed) {
ASSERT_EQ(0, m_rbd.open(m_ioctx, clone, m_clone_name.c_str(), nullptr));
ASSERT_EQ(0, FormatPolicy::load(clone));
*passed = true;
}
void open_and_load_flattened(librbd::Image& clone, bool* passed) {
ASSERT_EQ(0, m_rbd.open(m_ioctx, clone, m_clone_name.c_str(), nullptr));
ASSERT_EQ(0, FormatPolicy::load_flattened(clone));
*passed = true;
}
void verify_size_and_overlap(librbd::Image& image, uint64_t expected_size,
uint64_t expected_overlap) {
uint64_t size;
ASSERT_EQ(0, image.size(&size));
EXPECT_EQ(expected_size, size);
uint64_t overlap;
ASSERT_EQ(0, image.overlap(&overlap));
EXPECT_EQ(expected_overlap, overlap);
}
void verify_data(librbd::Image& image, const ceph::bufferlist& expected_bl) {
ceph::bufferlist read_bl;
ASSERT_EQ(expected_bl.length(),
image.read(0, expected_bl.length(), read_bl));
EXPECT_TRUE(expected_bl.contents_equal(read_bl));
}
librados::IoCtx m_ioctx;
librbd::RBD m_rbd;
std::string m_parent_name = get_temp_image_name();
std::string m_clone_name = get_temp_image_name();
uint64_t m_data_size = 25 << 20;
};
using EncryptedFlattenTestTypes =
::testing::Types<PlaintextUnderLUKS1, PlaintextUnderLUKS2,
UnformattedLUKS1, LUKS1UnderLUKS1, LUKS1UnderLUKS2,
UnformattedLUKS2, LUKS2UnderLUKS2, LUKS2UnderLUKS1>;
TYPED_TEST_SUITE(EncryptedFlattenTest, EncryptedFlattenTestTypes);
TYPED_TEST(EncryptedFlattenTest, Simple)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
ASSERT_PASSED0(this->create_and_setup);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(this->m_data_size, 'a'));
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load, clone);
this->verify_size_and_overlap(clone, this->m_data_size, this->m_data_size);
this->verify_data(clone, expected_bl);
ASSERT_EQ(0, clone.flatten());
this->verify_data(clone, expected_bl);
}
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load_flattened, clone);
this->verify_size_and_overlap(clone, this->m_data_size, 0);
this->verify_data(clone, expected_bl);
}
}
TYPED_TEST(EncryptedFlattenTest, Grow)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
ASSERT_PASSED0(this->create_and_setup);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(this->m_data_size, 'a'));
expected_bl.append_zero(1);
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load, clone);
ASSERT_EQ(0, clone.resize(this->m_data_size + 1));
this->verify_size_and_overlap(clone, this->m_data_size + 1,
this->m_data_size);
this->verify_data(clone, expected_bl);
ASSERT_EQ(0, clone.flatten());
this->verify_data(clone, expected_bl);
}
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load_flattened, clone);
this->verify_size_and_overlap(clone, this->m_data_size + 1, 0);
this->verify_data(clone, expected_bl);
}
}
TYPED_TEST(EncryptedFlattenTest, Shrink)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
ASSERT_PASSED0(this->create_and_setup);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(this->m_data_size - 1, 'a'));
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load, clone);
ASSERT_EQ(0, clone.resize(this->m_data_size - 1));
this->verify_size_and_overlap(clone, this->m_data_size - 1,
this->m_data_size - 1);
this->verify_data(clone, expected_bl);
ASSERT_EQ(0, clone.flatten());
this->verify_data(clone, expected_bl);
}
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load_flattened, clone);
this->verify_size_and_overlap(clone, this->m_data_size - 1, 0);
this->verify_data(clone, expected_bl);
}
}
TYPED_TEST(EncryptedFlattenTest, ShrinkToOne)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
ASSERT_PASSED0(this->create_and_setup);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(1, 'a'));
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load, clone);
ASSERT_EQ(0, clone.resize(1));
this->verify_size_and_overlap(clone, 1, 1);
this->verify_data(clone, expected_bl);
ASSERT_EQ(0, clone.flatten());
this->verify_data(clone, expected_bl);
}
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load_flattened, clone);
this->verify_size_and_overlap(clone, 1, 0);
this->verify_data(clone, expected_bl);
}
}
TYPED_TEST(EncryptedFlattenTest, ShrinkToOneAfterSnapshot)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
ASSERT_PASSED0(this->create_and_setup);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(1, 'a'));
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load, clone);
ASSERT_EQ(0, clone.snap_create("snap"));
ASSERT_EQ(0, clone.resize(1));
this->verify_size_and_overlap(clone, 1, 1);
this->verify_data(clone, expected_bl);
ASSERT_EQ(0, clone.flatten());
this->verify_data(clone, expected_bl);
}
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load_flattened, clone);
this->verify_size_and_overlap(clone, 1, 0);
this->verify_data(clone, expected_bl);
}
}
TYPED_TEST(EncryptedFlattenTest, MinOverlap)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
ASSERT_PASSED0(this->create_and_setup);
ceph::bufferlist expected_bl;
expected_bl.append(std::string(1, 'a'));
expected_bl.append_zero(this->m_data_size - 1);
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load, clone);
ASSERT_EQ(0, clone.resize(1));
ASSERT_EQ(0, clone.resize(this->m_data_size));
this->verify_size_and_overlap(clone, this->m_data_size, 1);
this->verify_data(clone, expected_bl);
ASSERT_EQ(0, clone.flatten());
this->verify_data(clone, expected_bl);
}
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load_flattened, clone);
this->verify_size_and_overlap(clone, this->m_data_size, 0);
this->verify_data(clone, expected_bl);
}
}
TYPED_TEST(EncryptedFlattenTest, ZeroOverlap)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
REQUIRE(!is_feature_enabled(RBD_FEATURE_STRIPINGV2));
REQUIRE(!is_feature_enabled(RBD_FEATURE_JOURNALING));
ASSERT_PASSED0(this->create_and_setup);
ceph::bufferlist expected_bl;
expected_bl.append_zero(this->m_data_size);
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load, clone);
ASSERT_EQ(0, clone.resize(0));
ASSERT_EQ(0, clone.resize(this->m_data_size));
this->verify_size_and_overlap(clone, this->m_data_size, 0);
this->verify_data(clone, expected_bl);
ASSERT_EQ(0, clone.flatten());
this->verify_data(clone, expected_bl);
}
{
librbd::Image clone;
ASSERT_PASSED(this->open_and_load_flattened, clone);
this->verify_size_and_overlap(clone, this->m_data_size, 0);
this->verify_data(clone, expected_bl);
}
}
#endif
TEST_F(TestLibRBD, TestIOWithIOHint)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
bool skip_discard = is_skip_partial_discard_enabled(image);
char test_data[TEST_IO_SIZE + 1];
char zero_data[TEST_IO_SIZE + 1];
char mismatch_data[TEST_IO_SIZE + 1];
int i;
uint64_t mismatch_offset;
for (i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
memset(zero_data, 0, sizeof(zero_data));
memset(mismatch_data, 9, sizeof(mismatch_data));
for (i = 0; i < 5; ++i)
ASSERT_PASSED(write_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_write_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
for (i = 0; i < 5; ++i)
ASSERT_PASSED(compare_and_write_test_data, image, test_data, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE, &mismatch_offset, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_compare_and_write_test_data, image, test_data, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
for (i = 0; i < 5; ++i)
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE,
LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_read_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL|LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
// discard 2nd, 4th sections.
ASSERT_PASSED(discard_test_data, image, TEST_IO_SIZE, TEST_IO_SIZE);
ASSERT_PASSED(aio_discard_test_data, image, TEST_IO_SIZE*3, TEST_IO_SIZE);
ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_SIZE,
LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL);
ASSERT_PASSED(read_test_data, image, skip_discard ? test_data : zero_data,
TEST_IO_SIZE, TEST_IO_SIZE,
LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL);
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE*2, TEST_IO_SIZE,
LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL);
ASSERT_PASSED(read_test_data, image, skip_discard ? test_data : zero_data,
TEST_IO_SIZE*3, TEST_IO_SIZE,
LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL);
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE*4, TEST_IO_SIZE, 0);
for (i = 0; i < 15; ++i) {
if (i % 3 == 2) {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
} else if (i % 3 == 1) {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
} else {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
}
}
for (i = 0; i < 15; ++i) {
if (i % 3 == 2) {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
} else if (i % 3 == 1) {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
} else {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
}
}
rbd_image_info_t info;
rbd_completion_t comp;
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
// can't read or write starting past end
ASSERT_EQ(-EINVAL, rbd_write(image, info.size, 1, test_data));
ASSERT_EQ(-EINVAL, rbd_read(image, info.size, 1, test_data));
// reading through end returns amount up to end
ASSERT_EQ(10, rbd_read2(image, info.size - 10, 100, test_data,
LIBRADOS_OP_FLAG_FADVISE_NOCACHE));
// writing through end returns amount up to end
ASSERT_EQ(10, rbd_write2(image, info.size - 10, 100, test_data,
LIBRADOS_OP_FLAG_FADVISE_DONTNEED));
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
ASSERT_EQ(0, rbd_aio_read2(image, info.size, 1, test_data, comp,
LIBRADOS_OP_FLAG_FADVISE_DONTNEED));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EINVAL, rbd_aio_get_return_value(comp));
rbd_aio_release(comp);
ASSERT_PASSED(write_test_data, image, zero_data, 0, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
mismatch_offset = 123;
ASSERT_EQ(-EILSEQ, rbd_compare_and_write(image, 0, TEST_IO_SIZE, mismatch_data, mismatch_data,
&mismatch_offset, LIBRADOS_OP_FLAG_FADVISE_DONTNEED));
ASSERT_EQ(0U, mismatch_offset);
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
mismatch_offset = 123;
ASSERT_EQ(0, rbd_aio_compare_and_write(image, 0, TEST_IO_SIZE, mismatch_data, mismatch_data,
comp, &mismatch_offset, LIBRADOS_OP_FLAG_FADVISE_DONTNEED));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EILSEQ, rbd_aio_get_return_value(comp));
ASSERT_EQ(0U, mismatch_offset);
rbd_aio_release(comp);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestDataPoolIO)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
std::string data_pool_name = create_pool(true);
rbd_image_t image;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
bool old_format;
uint64_t features;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
rbd_image_options_t image_options;
rbd_image_options_create(&image_options);
BOOST_SCOPE_EXIT( (&image_options) ) {
rbd_image_options_destroy(image_options);
} BOOST_SCOPE_EXIT_END;
ASSERT_EQ(0, rbd_image_options_set_uint64(image_options,
RBD_IMAGE_OPTION_FEATURES,
features));
ASSERT_EQ(0, rbd_image_options_set_string(image_options,
RBD_IMAGE_OPTION_DATA_POOL,
data_pool_name.c_str()));
ASSERT_EQ(0, rbd_create4(ioctx, name.c_str(), size, image_options));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_NE(-1, rbd_get_data_pool_id(image));
bool skip_discard = is_skip_partial_discard_enabled(image);
char test_data[TEST_IO_SIZE + 1];
char zero_data[TEST_IO_SIZE + 1];
int i;
for (i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
memset(zero_data, 0, sizeof(zero_data));
for (i = 0; i < 5; ++i)
ASSERT_PASSED(write_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_write_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
for (i = 0; i < 5; ++i)
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_read_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
// discard 2nd, 4th sections.
ASSERT_PASSED(discard_test_data, image, TEST_IO_SIZE, TEST_IO_SIZE);
ASSERT_PASSED(aio_discard_test_data, image, TEST_IO_SIZE*3, TEST_IO_SIZE);
ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, skip_discard ? test_data : zero_data,
TEST_IO_SIZE, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE*2, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, skip_discard ? test_data : zero_data,
TEST_IO_SIZE*3, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE*4, TEST_IO_SIZE, 0);
rbd_image_info_t info;
rbd_completion_t comp;
ASSERT_EQ(0, rbd_stat(image, &info, sizeof(info)));
// can't read or write starting past end
ASSERT_EQ(-EINVAL, rbd_write(image, info.size, 1, test_data));
ASSERT_EQ(-EINVAL, rbd_read(image, info.size, 1, test_data));
// reading through end returns amount up to end
ASSERT_EQ(10, rbd_read(image, info.size - 10, 100, test_data));
// writing through end returns amount up to end
ASSERT_EQ(10, rbd_write(image, info.size - 10, 100, test_data));
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
ASSERT_EQ(0, rbd_aio_write(image, info.size, 1, test_data, comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EINVAL, rbd_aio_get_return_value(comp));
rbd_aio_release(comp);
rbd_aio_create_completion(NULL, (rbd_callback_t) simple_read_cb, &comp);
ASSERT_EQ(0, rbd_aio_read(image, info.size, 1, test_data, comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EINVAL, rbd_aio_get_return_value(comp));
rbd_aio_release(comp);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestCompareAndWriteMismatch)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// We only support to compare and write the same amount of (len) bytes
std::string cmp_buffer("This is a test");
std::string write_buffer("Write this !!!");
std::string mismatch_buffer("This will fail");
std::string read_buffer(cmp_buffer.length(), '1');
ssize_t written = rbd_write(image, off, cmp_buffer.length(),
cmp_buffer.data());
ASSERT_EQ(cmp_buffer.length(), written);
// Compare should fail because of mismatch
uint64_t mismatch_off = 0;
written = rbd_compare_and_write(image, off, write_buffer.length(),
mismatch_buffer.data(), write_buffer.data(),
&mismatch_off, 0);
ASSERT_EQ(-EILSEQ, written);
ASSERT_EQ(5U, mismatch_off);
// check nothing was written
ssize_t read = rbd_read(image, off, read_buffer.length(), read_buffer.data());
ASSERT_EQ(read_buffer.length(), read);
ASSERT_EQ(cmp_buffer, read_buffer);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestAioCompareAndWriteMismatch)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// We only support to compare and write the same amount of (len) bytes
std::string cmp_buffer("This is a test");
std::string write_buffer("Write this !!!");
std::string mismatch_buffer("This will fail");
std::string read_buffer(cmp_buffer.length(), '1');
ssize_t written = rbd_write(image, off, cmp_buffer.length(),
cmp_buffer.data());
ASSERT_EQ(cmp_buffer.length(), written);
// Compare should fail because of mismatch
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
uint64_t mismatch_off = 0;
int ret = rbd_aio_compare_and_write(image, off, write_buffer.length(),
mismatch_buffer.data(),
write_buffer.data(), comp,
&mismatch_off, 0);
ASSERT_EQ(0, ret);
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EILSEQ, rbd_aio_get_return_value(comp));
ASSERT_EQ(5U, mismatch_off);
rbd_aio_release(comp);
// check nothing was written
ssize_t read = rbd_read(image, off, read_buffer.length(), read_buffer.data());
ASSERT_EQ(read_buffer.length(), read);
ASSERT_EQ(cmp_buffer, read_buffer);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestCompareAndWriteSuccess)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// We only support to compare and write the same amount of (len) bytes
std::string cmp_buffer("This is a test");
std::string write_buffer("Write this !!!");
std::string read_buffer(cmp_buffer.length(), '1');
ssize_t written = rbd_write(image, off, cmp_buffer.length(),
cmp_buffer.data());
ASSERT_EQ(cmp_buffer.length(), written);
/*
* we compare against the written buffer (cmp_buffer) and write the buffer
* We expect: len bytes written
*/
uint64_t mismatch_off = 0;
written = rbd_compare_and_write(image, off, write_buffer.length(),
cmp_buffer.data(), write_buffer.data(),
&mismatch_off, 0);
ASSERT_EQ(write_buffer.length(), written);
ASSERT_EQ(0U, mismatch_off);
ssize_t read = rbd_read(image, off, read_buffer.length(), read_buffer.data());
ASSERT_EQ(read_buffer.length(), read);
ASSERT_EQ(write_buffer, read_buffer);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestAioCompareAndWriteSuccess)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// We only support to compare and write the same amount of (len) bytes
std::string cmp_buffer("This is a test");
std::string write_buffer("Write this !!!");
std::string read_buffer(cmp_buffer.length(), '1');
ssize_t written = rbd_write(image, off, cmp_buffer.length(),
cmp_buffer.data());
ASSERT_EQ(cmp_buffer.length(), written);
/*
* we compare against the written buffer (cmp_buffer) and write the buffer
* We expect: len bytes written
*/
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
uint64_t mismatch_off = 0;
int ret = rbd_aio_compare_and_write(image, off, write_buffer.length(),
cmp_buffer.data(), write_buffer.data(),
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(0, rbd_aio_get_return_value(comp));
ASSERT_EQ(0U, mismatch_off);
rbd_aio_release(comp);
ssize_t read = rbd_read(image, off, read_buffer.length(), read_buffer.data());
ASSERT_EQ(read_buffer.length(), read);
ASSERT_EQ(write_buffer, read_buffer);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestCompareAndWriteStripeUnitUnaligned)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit;
rbd_get_stripe_unit(image, &stripe_unit);
std::string large_write_buffer(stripe_unit, '2');
std::string large_cmp_buffer(stripe_unit * 2, '4');
ssize_t written = rbd_write(image, stripe_unit, large_cmp_buffer.length(),
large_cmp_buffer.data());
ASSERT_EQ(large_cmp_buffer.length(), written);
/*
* compare and write at offset stripe_unit + 1 and stripe unit size
* Expect fail because access exceeds stripe (unaligned)
*/
uint64_t mismatch_off = 0;
written = rbd_compare_and_write(image, stripe_unit + 1, stripe_unit,
large_cmp_buffer.data(),
large_write_buffer.data(),
&mismatch_off, 0);
ASSERT_EQ(-EINVAL, written);
ASSERT_EQ(0U, mismatch_off);
// check nothing has been written
std::string large_read_buffer(large_cmp_buffer.length(), '5');
ssize_t read = rbd_read(image, stripe_unit, large_read_buffer.length(),
large_read_buffer.data());
ASSERT_EQ(large_read_buffer.length(), read);
auto buffer_mismatch = std::mismatch(large_cmp_buffer.begin(),
large_cmp_buffer.end(),
large_read_buffer.begin());
ASSERT_EQ(large_read_buffer.end(), buffer_mismatch.second);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestAioCompareAndWriteStripeUnitUnaligned)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit;
rbd_get_stripe_unit(image, &stripe_unit);
std::string large_write_buffer(stripe_unit, '2');
std::string large_cmp_buffer(stripe_unit * 2, '4');
ssize_t written = rbd_write(image, stripe_unit, large_cmp_buffer.length(),
large_cmp_buffer.data());
ASSERT_EQ(large_cmp_buffer.length(), written);
/*
* compare and write at offset stripe_unit + 1 and stripe unit size
* Expect fail because access spans stripe unit boundary (unaligned)
*/
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
uint64_t mismatch_off = 0;
int ret = rbd_aio_compare_and_write(image, stripe_unit + 1, stripe_unit,
large_cmp_buffer.data(),
large_write_buffer.data(),
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EINVAL, rbd_aio_get_return_value(comp));
ASSERT_EQ(0U, mismatch_off);
rbd_aio_release(comp);
// check nothing has been written
std::string large_read_buffer(large_cmp_buffer.length(), '5');
ssize_t read = rbd_read(image, stripe_unit, large_read_buffer.length(),
large_read_buffer.data());
ASSERT_EQ(large_read_buffer.length(), read);
auto buffer_mismatch = std::mismatch(large_cmp_buffer.begin(),
large_cmp_buffer.end(),
large_read_buffer.begin());
ASSERT_EQ(large_read_buffer.end(), buffer_mismatch.second);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestCompareAndWriteTooLarge)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit;
rbd_get_stripe_unit(image, &stripe_unit);
std::string large_write_buffer(stripe_unit * 2, '2');
std::string large_cmp_buffer(large_write_buffer.length(), '4');
ssize_t written = rbd_write(image, stripe_unit, large_cmp_buffer.length(),
large_cmp_buffer.data());
ASSERT_EQ(large_cmp_buffer.length(), written);
/*
* compare and write at offset stripe_unit and stripe unit size + 1
* Expect fail because access is larger than stripe unit size
*/
uint64_t mismatch_off = 0;
written = rbd_compare_and_write(image, stripe_unit, stripe_unit + 1,
large_cmp_buffer.data(),
large_write_buffer.data(),
&mismatch_off, 0);
ASSERT_EQ(-EINVAL, written);
ASSERT_EQ(0U, mismatch_off);
// check nothing has been written
std::string large_read_buffer(large_cmp_buffer.length(), '5');
ssize_t read = rbd_read(image, stripe_unit, large_read_buffer.length(),
large_read_buffer.data());
ASSERT_EQ(large_read_buffer.length(), read);
auto buffer_mismatch = std::mismatch(large_cmp_buffer.begin(),
large_cmp_buffer.end(),
large_read_buffer.begin());
ASSERT_EQ(large_read_buffer.end(), buffer_mismatch.second);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestAioCompareAndWriteTooLarge)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit;
rbd_get_stripe_unit(image, &stripe_unit);
std::string large_write_buffer(stripe_unit * 2, '2');
std::string large_cmp_buffer(large_write_buffer.length(), '4');
ssize_t written = rbd_write(image, stripe_unit, large_cmp_buffer.length(),
large_cmp_buffer.data());
ASSERT_EQ(large_cmp_buffer.length(), written);
/*
* compare and write at offset stripe_unit and stripe unit size + 1
* Expect fail because access is larger than stripe unit size
*/
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
uint64_t mismatch_off = 0;
int ret = rbd_aio_compare_and_write(image, stripe_unit, stripe_unit + 1,
large_cmp_buffer.data(),
large_write_buffer.data(),
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EINVAL, rbd_aio_get_return_value(comp));
ASSERT_EQ(0U, mismatch_off);
rbd_aio_release(comp);
// check nothing has been written
std::string large_read_buffer(large_cmp_buffer.length(), '5');
ssize_t read = rbd_read(image, stripe_unit, large_read_buffer.length(),
large_read_buffer.data());
ASSERT_EQ(large_read_buffer.length(), read);
auto buffer_mismatch = std::mismatch(large_cmp_buffer.begin(),
large_cmp_buffer.end(),
large_read_buffer.begin());
ASSERT_EQ(large_read_buffer.end(), buffer_mismatch.second);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestCompareAndWriteStripeUnitSuccess)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit;
rbd_get_stripe_unit(image, &stripe_unit);
std::string large_write_buffer(stripe_unit, '2');
std::string large_cmp_buffer(stripe_unit * 2, '4');
ssize_t written = rbd_write(image, stripe_unit, large_cmp_buffer.length(),
large_cmp_buffer.data());
ASSERT_EQ(large_cmp_buffer.length(), written);
// aligned stripe unit size access => expect success
uint64_t mismatch_off = 0;
written = rbd_compare_and_write(image, stripe_unit, stripe_unit,
large_cmp_buffer.data(),
large_write_buffer.data(),
&mismatch_off, 0);
ASSERT_EQ(stripe_unit, written);
ASSERT_EQ(0U, mismatch_off);
// check stripe_unit bytes of large_write_buffer were written
std::string large_read_buffer(large_cmp_buffer.length(), '5');
ssize_t read = rbd_read(image, stripe_unit, large_read_buffer.length(),
large_read_buffer.data());
ASSERT_EQ(large_read_buffer.length(), read);
auto buffer_mismatch = std::mismatch(large_read_buffer.begin(),
large_read_buffer.begin() + stripe_unit,
large_write_buffer.begin());
ASSERT_EQ(large_write_buffer.end(), buffer_mismatch.second);
// check data beyond stripe_unit size was not overwritten
buffer_mismatch = std::mismatch(large_read_buffer.begin() + stripe_unit,
large_read_buffer.end(),
large_cmp_buffer.begin());
ASSERT_EQ(large_cmp_buffer.begin() + stripe_unit, buffer_mismatch.second);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestAioCompareAndWriteStripeUnitSuccess)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit;
rbd_get_stripe_unit(image, &stripe_unit);
std::string large_write_buffer(stripe_unit, '2');
std::string large_cmp_buffer(stripe_unit * 2, '4');
ssize_t written = rbd_write(image, stripe_unit, large_cmp_buffer.length(),
large_cmp_buffer.data());
ASSERT_EQ(large_cmp_buffer.length(), written);
// aligned stripe unit size access => expect success
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
uint64_t mismatch_off = 0;
int ret = rbd_aio_compare_and_write(image, stripe_unit, stripe_unit,
large_cmp_buffer.data(),
large_write_buffer.data(),
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(0, rbd_aio_get_return_value(comp));
ASSERT_EQ(0U, mismatch_off);
rbd_aio_release(comp);
// check stripe_unit bytes of large_write_buffer were written
std::string large_read_buffer(large_cmp_buffer.length(), '5');
ssize_t read = rbd_read(image, stripe_unit, large_read_buffer.length(),
large_read_buffer.data());
ASSERT_EQ(large_read_buffer.length(), read);
auto buffer_mismatch = std::mismatch(large_read_buffer.begin(),
large_read_buffer.begin() + stripe_unit,
large_write_buffer.begin());
ASSERT_EQ(large_write_buffer.end(), buffer_mismatch.second);
// check data beyond stripe_unit size was not overwritten
buffer_mismatch = std::mismatch(large_read_buffer.begin() + stripe_unit,
large_read_buffer.end(),
large_cmp_buffer.begin());
ASSERT_EQ(large_cmp_buffer.begin() + stripe_unit, buffer_mismatch.second);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestAioCompareAndWriteVIovecLenDiffers)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
std::string cmp_buffer("This is a test");
size_t cmp_len = cmp_buffer.length();
std::string write_buffer("Write this !!!");
struct iovec write_iovs[] = {
{.iov_base = &write_buffer[0], .iov_len = 6},
{.iov_base = &write_buffer[6], .iov_len = 5},
{.iov_base = &write_buffer[11], .iov_len = 3}
};
ASSERT_EQ(cmp_len, rbd_write(image, off, cmp_len, cmp_buffer.data()));
// should fail because compare iovec len cannot be different to write iovec len
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
uint64_t mismatch_off = 0;
int ret = rbd_aio_compare_and_writev(image, off,
write_iovs /* cmp_iovs */, 1,
write_iovs, std::size(write_iovs),
comp, &mismatch_off, 0);
ASSERT_EQ(-EINVAL, ret);
ASSERT_EQ(0U, mismatch_off);
rbd_aio_release(comp);
// check nothing was written
std::string read_buffer(cmp_buffer.length(), '1');
ssize_t read = rbd_read(image, off, read_buffer.length(), read_buffer.data());
ASSERT_EQ(read_buffer.length(), read);
ASSERT_EQ(cmp_buffer, read_buffer);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestAioCompareAndWriteVMismatch)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
std::string cmp_buffer("This is a test");
int cmp_len = cmp_buffer.length();
std::string write_buffer("Write this !!!");
struct iovec write_iovs[] = {
{.iov_base = &write_buffer[0], .iov_len = 6},
{.iov_base = &write_buffer[6], .iov_len = 5},
{.iov_base = &write_buffer[11], .iov_len = 3}
};
std::string mismatch_buffer("This will fail");
struct iovec mismatch_iovs[] = {
{.iov_base = &mismatch_buffer[0], .iov_len = 5},
{.iov_base = &mismatch_buffer[5], .iov_len = 5},
{.iov_base = &mismatch_buffer[10], .iov_len = 4}
};
ASSERT_EQ(cmp_len, rbd_write(image, off, cmp_len, cmp_buffer.data()));
// this should execute the compare but fail because of mismatch
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
uint64_t mismatch_off = 0;
int ret = rbd_aio_compare_and_writev(image, off,
mismatch_iovs /* cmp_iovs */,
std::size(mismatch_iovs),
write_iovs, std::size(write_iovs),
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(-EILSEQ, rbd_aio_get_return_value(comp));
ASSERT_EQ(5U, mismatch_off);
rbd_aio_release(comp);
// check nothing was written
std::string read_buffer(cmp_buffer.length(), '1');
ssize_t read = rbd_read(image, off, read_buffer.length(), read_buffer.data());
ASSERT_EQ(read_buffer.length(), read);
ASSERT_EQ(cmp_buffer, read_buffer);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestAioCompareAndWriteVSuccess)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
std::string cmp_buffer("This is a test");
struct iovec cmp_iovs[] = {
{.iov_base = &cmp_buffer[0], .iov_len = 5},
{.iov_base = &cmp_buffer[5], .iov_len = 3},
{.iov_base = &cmp_buffer[8], .iov_len = 2},
{.iov_base = &cmp_buffer[10], .iov_len = 4}
};
size_t cmp_len = cmp_buffer.length();
std::string write_buffer("Write this !!!");
struct iovec write_iovs[] = {
{.iov_base = &write_buffer[0], .iov_len = 6},
{.iov_base = &write_buffer[6], .iov_len = 5},
{.iov_base = &write_buffer[11], .iov_len = 3}
};
ASSERT_EQ(cmp_len, rbd_write(image, off, cmp_len, cmp_buffer.data()));
// compare against the buffer written before => should succeed
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
uint64_t mismatch_off = 0;
int ret = rbd_aio_compare_and_writev(image, off,
cmp_iovs, std::size(cmp_iovs),
write_iovs, std::size(write_iovs),
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(0, rbd_aio_get_return_value(comp));
ASSERT_EQ(0U, mismatch_off);
rbd_aio_release(comp);
// check data was successfully written
std::string read_buffer(cmp_buffer.length(), '1');
ssize_t read = rbd_read(image, off, read_buffer.length(), read_buffer.data());
ASSERT_EQ(read_buffer.length(), read);
ASSERT_EQ(write_buffer, read_buffer);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestScatterGatherIO)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
std::string write_buffer("This is a test");
// These iovecs should produce a length overflow
struct iovec bad_iovs[] = {
{.iov_base = &write_buffer[0], .iov_len = 5},
{.iov_base = NULL, .iov_len = std::numeric_limits<size_t>::max()}
};
struct iovec write_iovs[] = {
{.iov_base = &write_buffer[0], .iov_len = 5},
{.iov_base = &write_buffer[5], .iov_len = 3},
{.iov_base = &write_buffer[8], .iov_len = 2},
{.iov_base = &write_buffer[10], .iov_len = 4}
};
rbd_completion_t comp;
rbd_aio_create_completion(NULL, NULL, &comp);
ASSERT_EQ(-EINVAL, rbd_aio_writev(image, write_iovs, 0, 0, comp));
ASSERT_EQ(-EINVAL, rbd_aio_writev(image, bad_iovs, 2, 0, comp));
ASSERT_EQ(0, rbd_aio_writev(image, write_iovs,
sizeof(write_iovs) / sizeof(struct iovec),
1<<order, comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(0, rbd_aio_get_return_value(comp));
rbd_aio_release(comp);
std::string read_buffer(write_buffer.size(), '1');
struct iovec read_iovs[] = {
{.iov_base = &read_buffer[0], .iov_len = 4},
{.iov_base = &read_buffer[8], .iov_len = 4},
{.iov_base = &read_buffer[12], .iov_len = 2}
};
rbd_aio_create_completion(NULL, NULL, &comp);
ASSERT_EQ(-EINVAL, rbd_aio_readv(image, read_iovs, 0, 0, comp));
ASSERT_EQ(-EINVAL, rbd_aio_readv(image, bad_iovs, 2, 0, comp));
ASSERT_EQ(0, rbd_aio_readv(image, read_iovs,
sizeof(read_iovs) / sizeof(struct iovec),
1<<order, comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(10, rbd_aio_get_return_value(comp));
rbd_aio_release(comp);
ASSERT_EQ("This1111 is a ", read_buffer);
std::string linear_buffer(write_buffer.size(), '1');
struct iovec linear_iovs[] = {
{.iov_base = &linear_buffer[4], .iov_len = 4}
};
rbd_aio_create_completion(NULL, NULL, &comp);
ASSERT_EQ(0, rbd_aio_readv(image, linear_iovs,
sizeof(linear_iovs) / sizeof(struct iovec),
1<<order, comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(comp));
ASSERT_EQ(4, rbd_aio_get_return_value(comp));
rbd_aio_release(comp);
ASSERT_EQ("1111This111111", linear_buffer);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestEmptyDiscard)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_PASSED(aio_discard_test_data, image, 0, 1*1024*1024);
ASSERT_PASSED(aio_discard_test_data, image, 0, 4*1024*1024);
ASSERT_PASSED(aio_discard_test_data, image, 3*1024*1024, 1*1024*1024);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestFUA)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image_write;
rbd_image_t image_read;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image_write, NULL));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image_read, NULL));
// enable writeback cache
rbd_flush(image_write);
char test_data[TEST_IO_SIZE + 1];
int i;
for (i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
for (i = 0; i < 5; ++i)
ASSERT_PASSED(write_test_data, image_write, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_FUA);
for (i = 0; i < 5; ++i)
ASSERT_PASSED(read_test_data, image_read, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_write_test_data, image_write, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_FUA);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_read_test_data, image_read, test_data,
TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
ASSERT_PASSED(validate_object_map, image_write);
ASSERT_PASSED(validate_object_map, image_read);
ASSERT_EQ(0, rbd_close(image_write));
ASSERT_EQ(0, rbd_close(image_read));
ASSERT_EQ(0, rbd_remove(ioctx, name.c_str()));
rados_ioctx_destroy(ioctx);
}
void simple_write_cb_pp(librbd::completion_t cb, void *arg)
{
cout << "write completion cb called!" << std::endl;
}
void simple_read_cb_pp(librbd::completion_t cb, void *arg)
{
cout << "read completion cb called!" << std::endl;
}
void aio_write_test_data(librbd::Image& image, const char *test_data,
off_t off, uint32_t iohint, bool *passed)
{
ceph::bufferlist bl;
bl.append(test_data, strlen(test_data));
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL, (librbd::callback_t) simple_write_cb_pp);
printf("created completion\n");
if (iohint)
image.aio_write2(off, strlen(test_data), bl, comp, iohint);
else
image.aio_write(off, strlen(test_data), bl, comp);
printf("started write\n");
comp->wait_for_complete();
int r = comp->get_return_value();
printf("return value is: %d\n", r);
ASSERT_EQ(0, r);
printf("finished write\n");
comp->release();
*passed = true;
}
void aio_discard_test_data(librbd::Image& image, off_t off, size_t len, bool *passed)
{
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL, (librbd::callback_t) simple_write_cb_pp);
image.aio_discard(off, len, comp);
comp->wait_for_complete();
int r = comp->get_return_value();
ASSERT_EQ(0, r);
comp->release();
*passed = true;
}
void write_test_data(librbd::Image& image, const char *test_data, off_t off, uint32_t iohint, bool *passed)
{
size_t written;
size_t len = strlen(test_data);
ceph::bufferlist bl;
bl.append(test_data, len);
if (iohint)
written = image.write2(off, len, bl, iohint);
else
written = image.write(off, len, bl);
printf("wrote: %u\n", (unsigned int) written);
ASSERT_EQ(bl.length(), written);
*passed = true;
}
void discard_test_data(librbd::Image& image, off_t off, size_t len, bool *passed)
{
size_t written;
written = image.discard(off, len);
printf("discard: %u~%u\n", (unsigned)off, (unsigned)len);
ASSERT_EQ(len, written);
*passed = true;
}
void aio_read_test_data(librbd::Image& image, const char *expected, off_t off, size_t expected_len, uint32_t iohint, bool *passed)
{
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL, (librbd::callback_t) simple_read_cb_pp);
ceph::bufferlist bl;
printf("created completion\n");
if (iohint)
image.aio_read2(off, expected_len, bl, comp, iohint);
else
image.aio_read(off, expected_len, bl, comp);
printf("started read\n");
comp->wait_for_complete();
int r = comp->get_return_value();
printf("return value is: %d\n", r);
ASSERT_EQ(TEST_IO_SIZE, r);
ASSERT_EQ(0, memcmp(expected, bl.c_str(), TEST_IO_SIZE));
printf("finished read\n");
comp->release();
*passed = true;
}
void read_test_data(librbd::Image& image, const char *expected, off_t off, size_t expected_len, uint32_t iohint, bool *passed)
{
int read;
size_t len = expected_len;
ceph::bufferlist bl;
if (iohint)
read = image.read2(off, len, bl, iohint);
else
read = image.read(off, len, bl);
ASSERT_TRUE(read >= 0);
std::string bl_str(bl.c_str(), read);
printf("read: %u\n", (unsigned int) read);
int result = memcmp(bl_str.c_str(), expected, expected_len);
if (result != 0) {
printf("read: %s\nexpected: %s\n", bl_str.c_str(), expected);
ASSERT_EQ(0, result);
}
*passed = true;
}
void aio_writesame_test_data(librbd::Image& image, const char *test_data, off_t off,
size_t len, size_t data_len, uint32_t iohint, bool *passed)
{
ceph::bufferlist bl;
bl.append(test_data, data_len);
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL, (librbd::callback_t) simple_write_cb_pp);
printf("created completion\n");
int r;
r = image.aio_writesame(off, len, bl, comp, iohint);
printf("started writesame\n");
if (len % data_len) {
ASSERT_EQ(-EINVAL, r);
printf("expected fail, finished writesame\n");
comp->release();
*passed = true;
return;
}
comp->wait_for_complete();
r = comp->get_return_value();
printf("return value is: %d\n", r);
ASSERT_EQ(0, r);
printf("finished writesame\n");
comp->release();
//verify data
printf("to verify the data\n");
int read;
uint64_t left = len;
while (left > 0) {
ceph::bufferlist bl;
read = image.read(off, data_len, bl);
ASSERT_EQ(data_len, static_cast<size_t>(read));
std::string bl_str(bl.c_str(), read);
int result = memcmp(bl_str.c_str(), test_data, data_len);
if (result !=0 ) {
printf("read: %u ~ %u\n", (unsigned int) off, (unsigned int) read);
printf("read: %s\nexpected: %s\n", bl_str.c_str(), test_data);
ASSERT_EQ(0, result);
}
off += data_len;
left -= data_len;
}
ASSERT_EQ(0U, left);
printf("verified\n");
*passed = true;
}
void writesame_test_data(librbd::Image& image, const char *test_data, off_t off,
ssize_t len, size_t data_len, uint32_t iohint,
bool *passed)
{
ssize_t written;
ceph::bufferlist bl;
bl.append(test_data, data_len);
written = image.writesame(off, len, bl, iohint);
if (len % data_len) {
ASSERT_EQ(-EINVAL, written);
printf("expected fail, finished writesame\n");
*passed = true;
return;
}
ASSERT_EQ(len, written);
printf("wrote: %u\n", (unsigned int) written);
*passed = true;
//verify data
printf("to verify the data\n");
int read;
uint64_t left = len;
while (left > 0) {
ceph::bufferlist bl;
read = image.read(off, data_len, bl);
ASSERT_EQ(data_len, static_cast<size_t>(read));
std::string bl_str(bl.c_str(), read);
int result = memcmp(bl_str.c_str(), test_data, data_len);
if (result !=0 ) {
printf("read: %u ~ %u\n", (unsigned int) off, (unsigned int) read);
printf("read: %s\nexpected: %s\n", bl_str.c_str(), test_data);
ASSERT_EQ(0, result);
}
off += data_len;
left -= data_len;
}
ASSERT_EQ(0U, left);
printf("verified\n");
*passed = true;
}
void aio_compare_and_write_test_data(librbd::Image& image, const char *cmp_data,
const char *test_data, off_t off, ssize_t len,
uint32_t iohint, bool *passed)
{
ceph::bufferlist cmp_bl;
cmp_bl.append(cmp_data, strlen(cmp_data));
ceph::bufferlist test_bl;
test_bl.append(test_data, strlen(test_data));
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL, (librbd::callback_t) simple_write_cb_pp);
printf("created completion\n");
uint64_t mismatch_offset;
image.aio_compare_and_write(off, len, cmp_bl, test_bl, comp, &mismatch_offset, iohint);
printf("started aio compare and write\n");
comp->wait_for_complete();
int r = comp->get_return_value();
printf("return value is: %d\n", r);
ASSERT_EQ(0, r);
printf("finished aio compare and write\n");
comp->release();
*passed = true;
}
void compare_and_write_test_data(librbd::Image& image, const char *cmp_data, const char *test_data,
off_t off, ssize_t len, uint64_t *mismatch_off, uint32_t iohint, bool *passed)
{
size_t written;
ceph::bufferlist cmp_bl;
cmp_bl.append(cmp_data, strlen(cmp_data));
ceph::bufferlist test_bl;
test_bl.append(test_data, strlen(test_data));
printf("start compare and write\n");
written = image.compare_and_write(off, len, cmp_bl, test_bl, mismatch_off, iohint);
printf("compare and wrote: %d\n", (int) written);
ASSERT_EQ(len, static_cast<ssize_t>(written));
*passed = true;
}
TEST_F(TestLibRBD, TestIOPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool skip_discard = this->is_skip_partial_discard_enabled(image);
char test_data[TEST_IO_SIZE + 1];
char zero_data[TEST_IO_SIZE + 1];
int i;
uint64_t mismatch_offset;
for (i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
memset(zero_data, 0, sizeof(zero_data));
for (i = 0; i < 5; ++i)
ASSERT_PASSED(write_test_data, image, test_data, strlen(test_data) * i, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_write_test_data, image, test_data, strlen(test_data) * i, 0);
for (i = 0; i < 5; ++i)
ASSERT_PASSED(compare_and_write_test_data, image, test_data, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, &mismatch_offset, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_compare_and_write_test_data, image, test_data, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE, 0);
for (i = 0; i < 5; ++i)
ASSERT_PASSED(read_test_data, image, test_data, strlen(test_data) * i, TEST_IO_SIZE, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_read_test_data, image, test_data, strlen(test_data) * i, TEST_IO_SIZE, 0);
// discard 2nd, 4th sections.
ASSERT_PASSED(discard_test_data, image, TEST_IO_SIZE, TEST_IO_SIZE);
ASSERT_PASSED(aio_discard_test_data, image, TEST_IO_SIZE*3, TEST_IO_SIZE);
ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, skip_discard ? test_data : zero_data,
TEST_IO_SIZE, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE*2, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, skip_discard ? test_data : zero_data,
TEST_IO_SIZE*3, TEST_IO_SIZE, 0);
ASSERT_PASSED(read_test_data, image, test_data, TEST_IO_SIZE*4, TEST_IO_SIZE, 0);
for (i = 0; i < 15; ++i) {
if (i % 3 == 2) {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, 0);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, 0);
} else if (i % 3 == 1) {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
} else {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
}
}
for (i = 0; i < 15; ++i) {
if (i % 3 == 2) {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, 0);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, 0);
} else if (i % 3 == 1) {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE + i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
} else {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE * i, TEST_IO_SIZE * i * 32, TEST_IO_SIZE, 0);
}
}
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
static void compare_written(librbd::Image& image, off_t off, size_t len,
const std::string& buffer, bool *passed)
{
bufferlist read_bl;
ssize_t read = image.read(off, len, read_bl);
ASSERT_EQ(len, read);
std::string read_buffer(read_bl.c_str(), read);
ASSERT_EQ(buffer.substr(0, len), read_buffer);
*passed = true;
}
TEST_F(TestLibRBD, TestCompareAndWriteCompareTooSmallPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
std::string small_buffer("Too small");
ceph::bufferlist small_bl;
small_bl.append(&small_buffer[0], 4);
small_bl.append(&small_buffer[4], 4);
// should fail because compare bufferlist cannot be smaller than len
uint64_t mismatch_off = 0;
written = image.compare_and_write(off, cmp_bl.length(),
small_bl, /* cmp_bl */
write_bl,
&mismatch_off, 0);
ASSERT_EQ(-EINVAL, written);
ASSERT_EQ(0U, mismatch_off);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteCompareTooSmallPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
std::string small_buffer("Too small");
ceph::bufferlist small_bl;
small_bl.append(&small_buffer[0], 4);
small_bl.append(&small_buffer[4], 4);
// should fail because compare bufferlist cannot be smaller than len
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(off, cmp_bl.length(),
small_bl, /* cmp_bl */
write_bl,
comp, &mismatch_off, 0);
ASSERT_EQ(-EINVAL, ret);
ASSERT_EQ(0U, mismatch_off);
comp->release();
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCompareAndWriteWriteTooSmallPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
std::string small_buffer("Too small");
ceph::bufferlist small_bl;
small_bl.append(&small_buffer[0], 4);
small_bl.append(&small_buffer[4], 4);
// should fail because write bufferlist cannot be smaller than len
uint64_t mismatch_off = 0;
written = image.compare_and_write(off, cmp_bl.length(),
cmp_bl,
small_bl, /* write_bl */
&mismatch_off, 0);
ASSERT_EQ(-EINVAL, written);
ASSERT_EQ(0U, mismatch_off);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteWriteTooSmallPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
std::string small_buffer("Too small");
ceph::bufferlist small_bl;
small_bl.append(&small_buffer[0], 4);
small_bl.append(&small_buffer[4], 4);
// should fail because write bufferlist cannot be smaller than len
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(off, cmp_bl.length(),
cmp_bl,
small_bl, /* write_bl */
comp, &mismatch_off, 0);
ASSERT_EQ(-EINVAL, ret);
ASSERT_EQ(0U, mismatch_off);
comp->release();
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCompareAndWriteMismatchPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
std::string mismatch_buffer("This will fail");
ceph::bufferlist mismatch_bl;
mismatch_bl.append(&mismatch_buffer[0], 5);
mismatch_bl.append(&mismatch_buffer[5], 5);
mismatch_bl.append(&mismatch_buffer[10], 4);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
// this should execute the compare but fail because of mismatch
uint64_t mismatch_off = 0;
written = image.compare_and_write(off, write_bl.length(),
mismatch_bl, /* cmp_bl */
write_bl,
&mismatch_off, 0);
ASSERT_EQ(-EILSEQ, written);
ASSERT_EQ(5U, mismatch_off);
// check that nothing was written
ASSERT_PASSED(compare_written, image, off, cmp_bl.length(), cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteMismatchPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
std::string mismatch_buffer("This will fail");
ceph::bufferlist mismatch_bl;
mismatch_bl.append(&mismatch_buffer[0], 5);
mismatch_bl.append(&mismatch_buffer[5], 5);
mismatch_bl.append(&mismatch_buffer[10], 4);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
// this should execute the compare but fail because of mismatch
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(off, write_bl.length(),
mismatch_bl, /* cmp_bl */
write_bl,
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
comp->wait_for_complete();
ssize_t aio_ret = comp->get_return_value();
ASSERT_EQ(-EILSEQ, aio_ret);
ASSERT_EQ(5U, mismatch_off);
comp->release();
// check that nothing was written
ASSERT_PASSED(compare_written, image, off, cmp_bl.length(), cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCompareAndWriteMismatchBufferlistGreaterLenPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
std::string mismatch_buffer("This will fail");
ceph::bufferlist mismatch_bl;
mismatch_bl.append(&mismatch_buffer[0], 5);
mismatch_bl.append(&mismatch_buffer[5], 5);
mismatch_bl.append(&mismatch_buffer[10], 4);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
/*
* we allow cmp_bl and write_bl to be greater than len so this
* should execute the compare but fail because of mismatch
*/
uint64_t mismatch_off = 0;
written = image.compare_and_write(off, cmp_bl.length() - 1,
mismatch_bl, /* cmp_bl */
write_bl,
&mismatch_off, 0);
ASSERT_EQ(-EILSEQ, written);
ASSERT_EQ(5U, mismatch_off);
// check that nothing was written
ASSERT_PASSED(compare_written, image, off, cmp_bl.length(), cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteMismatchBufferlistGreaterLenPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
std::string mismatch_buffer("This will fail");
ceph::bufferlist mismatch_bl;
mismatch_bl.append(&mismatch_buffer[0], 5);
mismatch_bl.append(&mismatch_buffer[5], 5);
mismatch_bl.append(&mismatch_buffer[10], 4);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
/*
* we allow cmp_bl and write_bl to be greater than len so this
* should execute the compare but fail because of mismatch
*/
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(off, cmp_bl.length() - 1,
mismatch_bl, /* cmp_bl */
write_bl,
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
comp->wait_for_complete();
ssize_t aio_ret = comp->get_return_value();
ASSERT_EQ(-EILSEQ, aio_ret);
ASSERT_EQ(5U, mismatch_off);
comp->release();
// check that nothing was written
ASSERT_PASSED(compare_written, image, off, cmp_bl.length(), cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCompareAndWriteSuccessPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
// compare against the buffer written before => should succeed
uint64_t mismatch_off = 0;
written = image.compare_and_write(off, cmp_bl.length(),
cmp_bl,
write_bl,
&mismatch_off, 0);
ASSERT_EQ(write_bl.length(), written);
ASSERT_EQ(0U, mismatch_off);
// check write_bl was written
ASSERT_PASSED(compare_written, image, off, write_bl.length(), write_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteSuccessPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
ssize_t written = image.write(off, cmp_bl.length(), cmp_bl);
ASSERT_EQ(cmp_bl.length(), written);
// compare against the buffer written before => should succeed
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(off, write_bl.length(),
cmp_bl,
write_bl,
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
comp->wait_for_complete();
ssize_t aio_ret = comp->get_return_value();
ASSERT_EQ(0, aio_ret);
ASSERT_EQ(0U, mismatch_off);
comp->release();
// check write_bl was written
ASSERT_PASSED(compare_written, image, off, write_bl.length(), write_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCompareAndWriteSuccessBufferlistGreaterLenPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
std::string mismatch_buffer("This will fail");
ceph::bufferlist mismatch_bl;
mismatch_bl.append(&mismatch_buffer[0], 5);
mismatch_bl.append(&mismatch_buffer[5], 5);
mismatch_bl.append(&mismatch_buffer[10], 4);
/*
* Test len < cmp_bl & write_bl => should succeed but only compare
* len bytes resp. only write len bytes
*/
ssize_t written = image.write(off, mismatch_bl.length(), mismatch_bl);
ASSERT_EQ(mismatch_bl.length(), written);
size_t len_m1 = cmp_bl.length() - 1;
written = image.write(off, len_m1, cmp_bl);
ASSERT_EQ(len_m1, written);
// the content of the image at off should now be "This is a tesl"
uint64_t mismatch_off = 0;
written = image.compare_and_write(off, len_m1,
cmp_bl,
write_bl,
&mismatch_off, 0);
ASSERT_EQ(len_m1, written);
ASSERT_EQ(0U, mismatch_off);
// check that only write_bl.length() - 1 bytes were written
ASSERT_PASSED(compare_written, image, off, len_m1, write_buffer);
ASSERT_PASSED(compare_written, image, off + len_m1, 1, std::string("l"));
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteSuccessBufferlistGreaterLenPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
off_t off = 512;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string cmp_buffer("This is a test");
ceph::bufferlist cmp_bl;
cmp_bl.append(&cmp_buffer[0], 5);
cmp_bl.append(&cmp_buffer[5], 3);
cmp_bl.append(&cmp_buffer[8], 2);
cmp_bl.append(&cmp_buffer[10], 4);
std::string write_buffer("Write this !!!");
ceph::bufferlist write_bl;
write_bl.append(&write_buffer[0], 6);
write_bl.append(&write_buffer[6], 5);
write_bl.append(&write_buffer[11], 3);
std::string mismatch_buffer("This will fail");
ceph::bufferlist mismatch_bl;
mismatch_bl.append(&mismatch_buffer[0], 5);
mismatch_bl.append(&mismatch_buffer[5], 5);
mismatch_bl.append(&mismatch_buffer[10], 4);
/*
* Test len < cmp_bl & write_bl => should succeed but only compare
* len bytes resp. only write len bytes
*/
ssize_t written = image.write(off, mismatch_bl.length(), mismatch_bl);
ASSERT_EQ(mismatch_bl.length(), written);
size_t len_m1 = cmp_bl.length() - 1;
written = image.write(off, len_m1, cmp_bl);
ASSERT_EQ(len_m1, written);
// the content of the image at off should now be "This is a tesl"
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(off, len_m1,
cmp_bl,
write_bl,
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
comp->wait_for_complete();
ssize_t aio_ret = comp->get_return_value();
ASSERT_EQ(0, aio_ret);
ASSERT_EQ(0U, mismatch_off);
comp->release();
// check that only write_bl.length() - 1 bytes were written
ASSERT_PASSED(compare_written, image, off, len_m1, write_buffer);
ASSERT_PASSED(compare_written, image, off + len_m1, 1, std::string("l"));
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCompareAndWriteStripeUnitUnalignedPP)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit = image.get_stripe_unit();
std::string large_write_buffer(stripe_unit, '2');
ceph::bufferlist large_write_bl;
large_write_bl.append(large_write_buffer.data(),
large_write_buffer.length());
std::string large_cmp_buffer(stripe_unit * 2, '3');
ceph::bufferlist large_cmp_bl;
large_cmp_bl.append(large_cmp_buffer.data(), large_cmp_buffer.length());
ssize_t written = image.write(stripe_unit, large_cmp_bl.length(),
large_cmp_bl);
ASSERT_EQ(large_cmp_bl.length(), written);
/*
* compare and write at offset stripe_unit + 1 and stripe unit size
* Expect fail because access exceeds stripe
*/
uint64_t mismatch_off = 0;
written = image.compare_and_write(stripe_unit + 1, stripe_unit,
large_cmp_bl,
large_write_bl,
&mismatch_off, 0);
ASSERT_EQ(-EINVAL, written);
ASSERT_EQ(0U, mismatch_off);
// check nothing has been written
ASSERT_PASSED(compare_written, image, stripe_unit, large_cmp_bl.length(),
large_cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteStripeUnitUnalignedPP)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit = image.get_stripe_unit();
std::string large_write_buffer(stripe_unit, '2');
ceph::bufferlist large_write_bl;
large_write_bl.append(large_write_buffer.data(),
large_write_buffer.length());
std::string large_cmp_buffer(stripe_unit * 2, '3');
ceph::bufferlist large_cmp_bl;
large_cmp_bl.append(large_cmp_buffer.data(), large_cmp_buffer.length());
ssize_t written = image.write(stripe_unit, large_cmp_bl.length(),
large_cmp_bl);
ASSERT_EQ(large_cmp_bl.length(), written);
/*
* compare and write at offset stripe_unit + 1 and stripe unit size
* Expect fail because access exceeds stripe
*/
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(stripe_unit + 1, stripe_unit,
large_cmp_bl,
large_write_bl,
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
comp->wait_for_complete();
ssize_t aio_ret = comp->get_return_value();
ASSERT_EQ(-EINVAL, aio_ret);
ASSERT_EQ(0U, mismatch_off);
comp->release();
// check nothing has been written
ASSERT_PASSED(compare_written, image, stripe_unit, large_cmp_bl.length(),
large_cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCompareAndWriteTooLargePP)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit = image.get_stripe_unit();
std::string large_write_buffer(stripe_unit * 2, '2');
ceph::bufferlist large_write_bl;
large_write_bl.append(large_write_buffer.data(),
large_write_buffer.length());
std::string large_cmp_buffer(stripe_unit * 2, '3');
ceph::bufferlist large_cmp_bl;
large_cmp_bl.append(large_cmp_buffer.data(), large_cmp_buffer.length());
ssize_t written = image.write(stripe_unit, large_cmp_bl.length(),
large_cmp_bl);
ASSERT_EQ(large_cmp_bl.length(), written);
/*
* compare and write at offset stripe_unit and stripe unit size + 1
* Expect fail because access is larger than stripe unit size
*/
uint64_t mismatch_off = 0;
written = image.compare_and_write(stripe_unit, stripe_unit + 1,
large_cmp_bl,
large_write_bl,
&mismatch_off, 0);
ASSERT_EQ(-EINVAL, written);
ASSERT_EQ(0U, mismatch_off);
// check nothing has been written
ASSERT_PASSED(compare_written, image, stripe_unit, large_cmp_bl.length(),
large_cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteTooLargePP)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit = image.get_stripe_unit();
std::string large_write_buffer(stripe_unit * 2, '2');
ceph::bufferlist large_write_bl;
large_write_bl.append(large_write_buffer.data(),
large_write_buffer.length());
std::string large_cmp_buffer(stripe_unit * 2, '3');
ceph::bufferlist large_cmp_bl;
large_cmp_bl.append(large_cmp_buffer.data(), large_cmp_buffer.length());
ssize_t written = image.write(stripe_unit, large_cmp_bl.length(),
large_cmp_bl);
ASSERT_EQ(large_cmp_bl.length(), written);
/*
* compare and write at offset stripe_unit and stripe unit size + 1
* Expect fail because access is larger than stripe unit size
*/
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(stripe_unit, stripe_unit + 1,
large_cmp_bl,
large_write_bl,
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
comp->wait_for_complete();
ssize_t aio_ret = comp->get_return_value();
ASSERT_EQ(-EINVAL, aio_ret);
ASSERT_EQ(0U, mismatch_off);
comp->release();
// check nothing has been written
ASSERT_PASSED(compare_written, image, stripe_unit, large_cmp_bl.length(),
large_cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestCompareAndWriteStripeUnitSuccessPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit = image.get_stripe_unit();
std::string large_write_buffer(stripe_unit * 2, '2');
ceph::bufferlist large_write_bl;
large_write_bl.append(large_write_buffer.data(),
large_write_buffer.length());
std::string large_cmp_buffer(stripe_unit * 2, '3');
ceph::bufferlist large_cmp_bl;
large_cmp_bl.append(large_cmp_buffer.data(), large_cmp_buffer.length());
ssize_t written = image.write(stripe_unit, large_cmp_bl.length(),
large_cmp_bl);
ASSERT_EQ(large_cmp_bl.length(), written);
// aligned stripe unit size access => expect success
uint64_t mismatch_off = 0;
written = image.compare_and_write(stripe_unit, stripe_unit,
large_cmp_bl,
large_write_bl,
&mismatch_off, 0);
ASSERT_EQ(stripe_unit, written);
ASSERT_EQ(0U, mismatch_off);
// check large_write_bl was written and nothing beyond
ASSERT_PASSED(compare_written, image, stripe_unit, stripe_unit,
large_write_buffer);
ASSERT_PASSED(compare_written, image, stripe_unit * 2, stripe_unit,
large_cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestAioCompareAndWriteStripeUnitSuccessPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 20 << 20; /* 20MiB */
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
// large write test => we allow stripe unit size writes (aligned)
uint64_t stripe_unit = image.get_stripe_unit();
std::string large_write_buffer(stripe_unit * 2, '2');
ceph::bufferlist large_write_bl;
large_write_bl.append(large_write_buffer.data(),
large_write_buffer.length());
std::string large_cmp_buffer(stripe_unit * 2, '3');
ceph::bufferlist large_cmp_bl;
large_cmp_bl.append(large_cmp_buffer.data(),
large_cmp_buffer.length());
ssize_t written = image.write(stripe_unit, large_cmp_bl.length(),
large_cmp_bl);
ASSERT_EQ(large_cmp_bl.length(), written);
// aligned stripe unit size access => expect success
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) simple_write_cb_pp);
uint64_t mismatch_off = 0;
int ret = image.aio_compare_and_write(stripe_unit, stripe_unit,
large_cmp_bl,
large_write_bl,
comp, &mismatch_off, 0);
ASSERT_EQ(0, ret);
comp->wait_for_complete();
ssize_t aio_ret = comp->get_return_value();
ASSERT_EQ(0, aio_ret);
ASSERT_EQ(0U, mismatch_off);
comp->release();
// check large_write_bl was written and nothing beyond
ASSERT_PASSED(compare_written, image, stripe_unit, stripe_unit,
large_write_buffer);
ASSERT_PASSED(compare_written, image, stripe_unit * 2, stripe_unit,
large_cmp_buffer);
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestIOPPWithIOHint)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
char test_data[TEST_IO_SIZE + 1];
char zero_data[TEST_IO_SIZE + 1];
test_data[TEST_IO_SIZE] = '\0';
int i;
for (i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
memset(zero_data, 0, sizeof(zero_data));
for (i = 0; i < 5; ++i)
ASSERT_PASSED(write_test_data, image, test_data, strlen(test_data) * i,
LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_write_test_data, image, test_data, strlen(test_data) * i,
LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
ASSERT_PASSED(read_test_data, image, test_data, strlen(test_data),
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_RANDOM);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_read_test_data, image, test_data, strlen(test_data) * i,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL|LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
for (i = 0; i < 15; ++i) {
if (i % 3 == 2) {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
} else if (i % 3 == 1) {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE + i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE + i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
} else {
ASSERT_PASSED(writesame_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
ASSERT_PASSED(writesame_test_data, image, zero_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
}
}
for (i = 0; i < 15; ++i) {
if (i % 3 == 2) {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32 + i, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
} else if (i % 3 == 1) {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE + i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE + i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
} else {
ASSERT_PASSED(aio_writesame_test_data, image, test_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
ASSERT_PASSED(aio_writesame_test_data, image, zero_data, TEST_IO_SIZE * i,
TEST_IO_SIZE * i * 32, TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_DONTNEED);
}
}
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, TestIOToSnapshot)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t isize = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), isize, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
int i, r;
rbd_image_t image_at_snap;
char orig_data[TEST_IO_TO_SNAP_SIZE + 1];
char test_data[TEST_IO_TO_SNAP_SIZE + 1];
for (i = 0; i < TEST_IO_TO_SNAP_SIZE; ++i)
test_data[i] = (char) (i + 48);
test_data[TEST_IO_TO_SNAP_SIZE] = '\0';
orig_data[TEST_IO_TO_SNAP_SIZE] = '\0';
r = rbd_read(image, 0, TEST_IO_TO_SNAP_SIZE, orig_data);
ASSERT_EQ(r, TEST_IO_TO_SNAP_SIZE);
ASSERT_EQ(0, test_ls_snaps(image, 0));
ASSERT_EQ(0, rbd_snap_create(image, "orig"));
ASSERT_EQ(1, test_ls_snaps(image, 1, "orig", isize));
ASSERT_PASSED(read_test_data, image, orig_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
printf("write test data!\n");
ASSERT_PASSED(write_test_data, image, test_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
ASSERT_EQ(0, rbd_snap_create(image, "written"));
ASSERT_EQ(2, test_ls_snaps(image, 2, "orig", isize, "written", isize));
ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
rbd_snap_set(image, "orig");
ASSERT_PASSED(read_test_data, image, orig_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
rbd_snap_set(image, "written");
ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
rbd_snap_set(image, "orig");
r = rbd_write(image, 0, TEST_IO_TO_SNAP_SIZE, test_data);
printf("write to snapshot returned %d\n", r);
ASSERT_LT(r, 0);
cout << strerror(-r) << std::endl;
ASSERT_PASSED(read_test_data, image, orig_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
rbd_snap_set(image, "written");
ASSERT_PASSED(read_test_data, image, test_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
r = rbd_snap_rollback(image, "orig");
ASSERT_EQ(r, -EROFS);
r = rbd_snap_set(image, NULL);
ASSERT_EQ(r, 0);
r = rbd_snap_rollback(image, "orig");
ASSERT_EQ(r, 0);
ASSERT_PASSED(write_test_data, image, test_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
rbd_flush(image);
printf("opening testimg@orig\n");
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image_at_snap, "orig"));
ASSERT_PASSED(read_test_data, image_at_snap, orig_data, 0, TEST_IO_TO_SNAP_SIZE, 0);
r = rbd_write(image_at_snap, 0, TEST_IO_TO_SNAP_SIZE, test_data);
printf("write to snapshot returned %d\n", r);
ASSERT_LT(r, 0);
cout << strerror(-r) << std::endl;
ASSERT_EQ(0, rbd_close(image_at_snap));
ASSERT_EQ(2, test_ls_snaps(image, 2, "orig", isize, "written", isize));
ASSERT_EQ(0, rbd_snap_remove(image, "written"));
ASSERT_EQ(1, test_ls_snaps(image, 1, "orig", isize));
ASSERT_EQ(0, rbd_snap_remove(image, "orig"));
ASSERT_EQ(0, test_ls_snaps(image, 0));
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestSnapshotDeletedIo)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t isize = 2 << 20;
int r;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), isize, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_snap_create(image, "orig"));
r = rbd_snap_set(image, "orig");
ASSERT_EQ(r, 0);
ASSERT_EQ(0, rbd_snap_remove(image, "orig"));
char test[20];
ASSERT_EQ(-ENOENT, rbd_read(image, 20, 20, test));
r = rbd_snap_set(image, NULL);
ASSERT_EQ(r, 0);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestClone)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, rados_conf_set(_cluster, "rbd_default_clone_format", "1"));
BOOST_SCOPE_EXIT_ALL(&) {
ASSERT_EQ(0, rados_conf_set(_cluster, "rbd_default_clone_format", "auto"));
};
rados_ioctx_t ioctx;
rbd_image_info_t pinfo, cinfo;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
bool old_format;
uint64_t features;
rbd_image_t parent, child;
int order = 0;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
std::string parent_name = get_temp_image_name();
std::string child_name = get_temp_image_name();
// make a parent to clone from
ASSERT_EQ(0, create_image_full(ioctx, parent_name.c_str(), 4<<20, &order,
false, features));
ASSERT_EQ(0, rbd_open(ioctx, parent_name.c_str(), &parent, NULL));
printf("made parent image \"parent\"\n");
char *data = (char *)"testdata";
ASSERT_EQ((ssize_t)strlen(data), rbd_write(parent, 0, strlen(data), data));
// can't clone a non-snapshot, expect failure
EXPECT_NE(0, clone_image(ioctx, parent, parent_name.c_str(), NULL, ioctx,
child_name.c_str(), features, &order));
// verify that there is no parent info on "parent"
ASSERT_EQ(-ENOENT, rbd_get_parent_info(parent, NULL, 0, NULL, 0, NULL, 0));
printf("parent has no parent info\n");
// create 70 metadatas to verify we can clone all key/value pairs
std::string key;
std::string val;
size_t sum_key_len = 0;
size_t sum_value_len = 0;
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_set(parent, key.c_str(), val.c_str()));
sum_key_len += (key.size() + 1);
sum_value_len += (val.size() + 1);
}
char keys[1024];
char vals[1024];
size_t keys_len = sizeof(keys);
size_t vals_len = sizeof(vals);
char value[1024];
size_t value_len = sizeof(value);
// create a snapshot, reopen as the parent we're interested in
ASSERT_EQ(0, rbd_snap_create(parent, "parent_snap"));
printf("made snapshot \"parent@parent_snap\"\n");
ASSERT_EQ(0, rbd_close(parent));
ASSERT_EQ(0, rbd_open(ioctx, parent_name.c_str(), &parent, "parent_snap"));
ASSERT_EQ(-EINVAL, clone_image(ioctx, parent, parent_name.c_str(), "parent_snap",
ioctx, child_name.c_str(), features, &order));
// unprotected image should fail unprotect
ASSERT_EQ(-EINVAL, rbd_snap_unprotect(parent, "parent_snap"));
printf("can't unprotect an unprotected snap\n");
ASSERT_EQ(0, rbd_snap_protect(parent, "parent_snap"));
// protecting again should fail
ASSERT_EQ(-EBUSY, rbd_snap_protect(parent, "parent_snap"));
printf("can't protect a protected snap\n");
// This clone and open should work
ASSERT_EQ(0, clone_image(ioctx, parent, parent_name.c_str(), "parent_snap",
ioctx, child_name.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx, child_name.c_str(), &child, NULL));
printf("made and opened clone \"child\"\n");
// check read
ASSERT_PASSED(read_test_data, child, data, 0, strlen(data), 0);
// check write
ASSERT_EQ((ssize_t)strlen(data), rbd_write(child, 20, strlen(data), data));
ASSERT_PASSED(read_test_data, child, data, 20, strlen(data), 0);
ASSERT_PASSED(read_test_data, child, data, 0, strlen(data), 0);
// check attributes
ASSERT_EQ(0, rbd_stat(parent, &pinfo, sizeof(pinfo)));
ASSERT_EQ(0, rbd_stat(child, &cinfo, sizeof(cinfo)));
EXPECT_EQ(cinfo.size, pinfo.size);
uint64_t overlap;
rbd_get_overlap(child, &overlap);
EXPECT_EQ(overlap, pinfo.size);
EXPECT_EQ(cinfo.obj_size, pinfo.obj_size);
EXPECT_EQ(cinfo.order, pinfo.order);
printf("sizes and overlaps are good between parent and child\n");
// check key/value pairs in child image
ASSERT_EQ(0, rbd_metadata_list(child, "key", 70, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(sum_key_len, keys_len);
ASSERT_EQ(sum_value_len, vals_len);
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_get(child, key.c_str(), value, &value_len));
ASSERT_STREQ(val.c_str(), value);
value_len = sizeof(value);
}
printf("child image successfully cloned all image-meta pairs\n");
// sizing down child results in changing overlap and size, not parent size
ASSERT_EQ(0, rbd_resize(child, 2UL<<20));
ASSERT_EQ(0, rbd_stat(child, &cinfo, sizeof(cinfo)));
rbd_get_overlap(child, &overlap);
ASSERT_EQ(overlap, 2UL<<20);
ASSERT_EQ(cinfo.size, 2UL<<20);
ASSERT_EQ(0, rbd_resize(child, 4UL<<20));
ASSERT_EQ(0, rbd_stat(child, &cinfo, sizeof(cinfo)));
rbd_get_overlap(child, &overlap);
ASSERT_EQ(overlap, 2UL<<20);
ASSERT_EQ(cinfo.size, 4UL<<20);
printf("sized down clone, changed overlap\n");
// sizing back up doesn't change that
ASSERT_EQ(0, rbd_resize(child, 5UL<<20));
ASSERT_EQ(0, rbd_stat(child, &cinfo, sizeof(cinfo)));
rbd_get_overlap(child, &overlap);
ASSERT_EQ(overlap, 2UL<<20);
ASSERT_EQ(cinfo.size, 5UL<<20);
ASSERT_EQ(0, rbd_stat(parent, &pinfo, sizeof(pinfo)));
printf("parent info: size %llu obj_size %llu parent_pool %llu\n",
(unsigned long long)pinfo.size, (unsigned long long)pinfo.obj_size,
(unsigned long long)pinfo.parent_pool);
ASSERT_EQ(pinfo.size, 4UL<<20);
printf("sized up clone, changed size but not overlap or parent's size\n");
ASSERT_PASSED(validate_object_map, child);
ASSERT_EQ(0, rbd_close(child));
ASSERT_PASSED(validate_object_map, parent);
ASSERT_EQ(-EBUSY, rbd_snap_remove(parent, "parent_snap"));
printf("can't remove parent while child still exists\n");
ASSERT_EQ(0, rbd_remove(ioctx, child_name.c_str()));
ASSERT_EQ(-EBUSY, rbd_snap_remove(parent, "parent_snap"));
printf("can't remove parent while still protected\n");
ASSERT_EQ(0, rbd_snap_unprotect(parent, "parent_snap"));
ASSERT_EQ(0, rbd_snap_remove(parent, "parent_snap"));
printf("removed parent snap after unprotecting\n");
ASSERT_EQ(0, rbd_close(parent));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestClone2)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, rados_conf_set(_cluster, "rbd_default_clone_format", "2"));
BOOST_SCOPE_EXIT_ALL(&) {
ASSERT_EQ(0, rados_conf_set(_cluster, "rbd_default_clone_format", "auto"));
};
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
bool old_format;
uint64_t features;
rbd_image_t parent, child;
int order = 0;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
std::string parent_name = get_temp_image_name();
std::string child_name = get_temp_image_name();
// make a parent to clone from
ASSERT_EQ(0, create_image_full(ioctx, parent_name.c_str(), 4<<20, &order,
false, features));
ASSERT_EQ(0, rbd_open(ioctx, parent_name.c_str(), &parent, NULL));
printf("made parent image \"parent\"\n");
char *data = (char *)"testdata";
char *childata = (char *)"childata";
ASSERT_EQ((ssize_t)strlen(data), rbd_write(parent, 0, strlen(data), data));
ASSERT_EQ((ssize_t)strlen(data), rbd_write(parent, 12, strlen(data), data));
// can't clone a non-snapshot, expect failure
EXPECT_NE(0, clone_image(ioctx, parent, parent_name.c_str(), NULL, ioctx,
child_name.c_str(), features, &order));
// verify that there is no parent info on "parent"
ASSERT_EQ(-ENOENT, rbd_get_parent_info(parent, NULL, 0, NULL, 0, NULL, 0));
printf("parent has no parent info\n");
// create 70 metadatas to verify we can clone all key/value pairs
std::string key;
std::string val;
size_t sum_key_len = 0;
size_t sum_value_len = 0;
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_set(parent, key.c_str(), val.c_str()));
sum_key_len += (key.size() + 1);
sum_value_len += (val.size() + 1);
}
char keys[1024];
char vals[1024];
size_t keys_len = sizeof(keys);
size_t vals_len = sizeof(vals);
char value[1024];
size_t value_len = sizeof(value);
// create a snapshot, reopen as the parent we're interested in
ASSERT_EQ(0, rbd_snap_create(parent, "parent_snap"));
printf("made snapshot \"parent@parent_snap\"\n");
ASSERT_EQ(0, rbd_close(parent));
ASSERT_EQ(0, rbd_open(ioctx, parent_name.c_str(), &parent, "parent_snap"));
// This clone and open should work
ASSERT_EQ(0, clone_image(ioctx, parent, parent_name.c_str(), "parent_snap",
ioctx, child_name.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx, child_name.c_str(), &child, NULL));
printf("made and opened clone \"child\"\n");
// check key/value pairs in child image
ASSERT_EQ(0, rbd_metadata_list(child, "key", 70, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(sum_key_len, keys_len);
ASSERT_EQ(sum_value_len, vals_len);
for (int i = 1; i <= 70; i++) {
key = "key" + stringify(i);
val = "value" + stringify(i);
ASSERT_EQ(0, rbd_metadata_get(child, key.c_str(), value, &value_len));
ASSERT_STREQ(val.c_str(), value);
value_len = sizeof(value);
}
printf("child image successfully cloned all image-meta pairs\n");
// write something in
ASSERT_EQ((ssize_t)strlen(childata), rbd_write(child, 20, strlen(childata), childata));
char test[strlen(data) * 2];
ASSERT_EQ((ssize_t)strlen(data), rbd_read(child, 20, strlen(data), test));
ASSERT_EQ(0, memcmp(test, childata, strlen(childata)));
// overlap
ASSERT_EQ((ssize_t)sizeof(test), rbd_read(child, 20 - strlen(data), sizeof(test), test));
ASSERT_EQ(0, memcmp(test, data, strlen(data)));
ASSERT_EQ(0, memcmp(test + strlen(data), childata, strlen(childata)));
// all parent
ASSERT_EQ((ssize_t)sizeof(test), rbd_read(child, 0, sizeof(test), test));
ASSERT_EQ(0, memcmp(test, data, strlen(data)));
ASSERT_PASSED(validate_object_map, child);
ASSERT_PASSED(validate_object_map, parent);
rbd_snap_info_t snaps[2];
int max_snaps = 2;
ASSERT_EQ(1, rbd_snap_list(parent, snaps, &max_snaps));
rbd_snap_list_end(snaps);
ASSERT_EQ(0, rbd_snap_remove_by_id(parent, snaps[0].id));
rbd_snap_namespace_type_t snap_namespace_type;
ASSERT_EQ(0, rbd_snap_get_namespace_type(parent, snaps[0].id,
&snap_namespace_type));
ASSERT_EQ(RBD_SNAP_NAMESPACE_TYPE_TRASH, snap_namespace_type);
char original_name[32];
ASSERT_EQ(0, rbd_snap_get_trash_namespace(parent, snaps[0].id,
original_name,
sizeof(original_name)));
ASSERT_EQ(0, strcmp("parent_snap", original_name));
ASSERT_EQ(0, rbd_close(child));
ASSERT_EQ(0, rbd_close(parent));
rados_ioctx_destroy(ioctx);
}
static void test_list_children(rbd_image_t image, ssize_t num_expected, ...)
{
va_list ap;
va_start(ap, num_expected);
size_t pools_len = 100;
size_t children_len = 100;
char *pools = NULL;
char *children = NULL;
ssize_t num_children;
do {
free(pools);
free(children);
pools = (char *) malloc(pools_len);
children = (char *) malloc(children_len);
num_children = rbd_list_children(image, pools, &pools_len,
children, &children_len);
} while (num_children == -ERANGE);
ASSERT_EQ(num_expected, num_children);
for (ssize_t i = num_expected; i > 0; --i) {
char *expected_pool = va_arg(ap, char *);
char *expected_image = va_arg(ap, char *);
char *pool = pools;
char *image = children;
bool found = 0;
printf("\ntrying to find %s/%s\n", expected_pool, expected_image);
for (ssize_t j = 0; j < num_children; ++j) {
printf("checking %s/%s\n", pool, image);
if (strcmp(expected_pool, pool) == 0 &&
strcmp(expected_image, image) == 0) {
printf("found child %s/%s\n\n", pool, image);
found = 1;
break;
}
pool += strlen(pool) + 1;
image += strlen(image) + 1;
if (j == num_children - 1) {
ASSERT_EQ(pool - pools - 1, (ssize_t) pools_len);
ASSERT_EQ(image - children - 1, (ssize_t) children_len);
}
}
ASSERT_TRUE(found);
}
va_end(ap);
if (pools)
free(pools);
if (children)
free(children);
}
static void test_list_children2(rbd_image_t image, int num_expected, ...)
{
int num_children, i, j, max_size = 10;
va_list ap;
rbd_child_info_t children[max_size];
num_children = rbd_list_children2(image, children, &max_size);
printf("num children is: %d\nexpected: %d\n", num_children, num_expected);
for (i = 0; i < num_children; i++) {
printf("child: %s\n", children[i].image_name);
}
va_start(ap, num_expected);
for (i = num_expected; i > 0; i--) {
char *expected_id = va_arg(ap, char *);
char *expected_pool = va_arg(ap, char *);
char *expected_image = va_arg(ap, char *);
bool expected_trash = va_arg(ap, int);
bool found = false;
for (j = 0; j < num_children; j++) {
if (children[j].pool_name == NULL ||
children[j].image_name == NULL ||
children[j].image_id == NULL)
continue;
if (strcmp(children[j].image_id, expected_id) == 0 &&
strcmp(children[j].pool_name, expected_pool) == 0 &&
strcmp(children[j].image_name, expected_image) == 0 &&
children[j].trash == expected_trash) {
printf("found child %s/%s/%s\n\n", children[j].pool_name, children[j].image_name, children[j].image_id);
rbd_list_child_cleanup(&children[j]);
children[j].pool_name = NULL;
children[j].image_name = NULL;
children[j].image_id = NULL;
found = true;
break;
}
}
EXPECT_TRUE(found);
}
va_end(ap);
for (i = 0; i < num_children; i++) {
EXPECT_EQ((const char *)0, children[i].pool_name);
EXPECT_EQ((const char *)0, children[i].image_name);
EXPECT_EQ((const char *)0, children[i].image_id);
}
}
TEST_F(TestLibRBD, ListChildren)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::RBD rbd;
rados_ioctx_t ioctx1, ioctx2;
string pool_name1 = create_pool(true);
string pool_name2 = create_pool(true);
ASSERT_NE("", pool_name2);
rados_ioctx_create(_cluster, pool_name1.c_str(), &ioctx1);
rados_ioctx_create(_cluster, pool_name2.c_str(), &ioctx2);
rbd_image_t image1;
rbd_image_t image2;
rbd_image_t image3;
rbd_image_t image4;
bool old_format;
uint64_t features;
rbd_image_t parent;
int order = 0;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
std::string parent_name = get_temp_image_name();
std::string child_name1 = get_temp_image_name();
std::string child_name2 = get_temp_image_name();
std::string child_name3 = get_temp_image_name();
std::string child_name4 = get_temp_image_name();
char child_id1[4096];
char child_id2[4096];
char child_id3[4096];
char child_id4[4096];
// make a parent to clone from
ASSERT_EQ(0, create_image_full(ioctx1, parent_name.c_str(), 4<<20, &order,
false, features));
ASSERT_EQ(0, rbd_open(ioctx1, parent_name.c_str(), &parent, NULL));
// create a snapshot, reopen as the parent we're interested in
ASSERT_EQ(0, rbd_snap_create(parent, "parent_snap"));
ASSERT_EQ(0, rbd_snap_set(parent, "parent_snap"));
ASSERT_EQ(0, rbd_snap_protect(parent, "parent_snap"));
ASSERT_EQ(0, rbd_close(parent));
ASSERT_EQ(0, rbd_open(ioctx1, parent_name.c_str(), &parent, "parent_snap"));
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "parent_snap",
ioctx2, child_name1.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx2, child_name1.c_str(), &image1, NULL));
ASSERT_EQ(0, rbd_get_id(image1, child_id1, sizeof(child_id1)));
test_list_children(parent, 1, pool_name2.c_str(), child_name1.c_str());
test_list_children2(parent, 1,
child_id1, pool_name2.c_str(), child_name1.c_str(), false);
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "parent_snap",
ioctx1, child_name2.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx1, child_name2.c_str(), &image2, NULL));
ASSERT_EQ(0, rbd_get_id(image2, child_id2, sizeof(child_id2)));
test_list_children(parent, 2, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str());
test_list_children2(parent, 2,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false);
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "parent_snap",
ioctx2, child_name3.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx2, child_name3.c_str(), &image3, NULL));
ASSERT_EQ(0, rbd_get_id(image3, child_id3, sizeof(child_id3)));
test_list_children(parent, 3, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name3.c_str());
test_list_children2(parent, 3,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), false);
librados::IoCtx ioctx3;
ASSERT_EQ(0, _rados.ioctx_create(pool_name2.c_str(), ioctx3));
ASSERT_EQ(0, rbd_close(image3));
ASSERT_EQ(0, rbd.trash_move(ioctx3, child_name3.c_str(), 0));
test_list_children(parent, 2, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str());
test_list_children2(parent, 3,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), true);
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "parent_snap",
ioctx2, child_name4.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx2, child_name4.c_str(), &image4, NULL));
ASSERT_EQ(0, rbd_get_id(image4, child_id4, sizeof(child_id4)));
test_list_children(parent, 3, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name4.c_str());
test_list_children2(parent, 4,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), true,
child_id4, pool_name2.c_str(), child_name4.c_str(), false);
ASSERT_EQ(0, rbd.trash_restore(ioctx3, child_id3, ""));
test_list_children(parent, 4, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name3.c_str(),
pool_name2.c_str(), child_name4.c_str());
test_list_children2(parent, 4,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), false,
child_id4, pool_name2.c_str(), child_name4.c_str(), false);
ASSERT_EQ(0, rbd_close(image1));
ASSERT_EQ(0, rbd_remove(ioctx2, child_name1.c_str()));
test_list_children(parent, 3,
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name3.c_str(),
pool_name2.c_str(), child_name4.c_str());
test_list_children2(parent, 3,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), false,
child_id4, pool_name2.c_str(), child_name4.c_str(), false);
ASSERT_EQ(0, rbd_remove(ioctx2, child_name3.c_str()));
test_list_children(parent, 2,
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name4.c_str());
test_list_children2(parent, 2,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id4, pool_name2.c_str(), child_name4.c_str(), false);
ASSERT_EQ(0, rbd_close(image4));
ASSERT_EQ(0, rbd_remove(ioctx2, child_name4.c_str()));
test_list_children(parent, 1,
pool_name1.c_str(), child_name2.c_str());
test_list_children2(parent, 1,
child_id2, pool_name1.c_str(), child_name2.c_str(), false);
ASSERT_EQ(0, rbd_close(image2));
ASSERT_EQ(0, rbd_remove(ioctx1, child_name2.c_str()));
test_list_children(parent, 0);
test_list_children2(parent, 0);
ASSERT_EQ(0, rbd_snap_unprotect(parent, "parent_snap"));
ASSERT_EQ(0, rbd_snap_remove(parent, "parent_snap"));
ASSERT_EQ(0, rbd_close(parent));
ASSERT_EQ(0, rbd_remove(ioctx1, parent_name.c_str()));
rados_ioctx_destroy(ioctx1);
rados_ioctx_destroy(ioctx2);
}
TEST_F(TestLibRBD, ListChildrenTiered)
{
SKIP_IF_CRIMSON();
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::RBD rbd;
string pool_name1 = create_pool(true);
string pool_name2 = create_pool(true);
string pool_name3 = create_pool(true);
ASSERT_NE("", pool_name1);
ASSERT_NE("", pool_name2);
ASSERT_NE("", pool_name3);
std::string cmdstr = "{\"prefix\": \"osd tier add\", \"pool\": \"" +
pool_name1 + "\", \"tierpool\":\"" + pool_name3 + "\", \"force_nonempty\":\"\"}";
char *cmd[1];
cmd[0] = (char *)cmdstr.c_str();
ASSERT_EQ(0, rados_mon_command(_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
cmdstr = "{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" +
pool_name3 + "\", \"mode\":\"writeback\"}";
cmd[0] = (char *)cmdstr.c_str();
ASSERT_EQ(0, rados_mon_command(_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
cmdstr = "{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" +
pool_name1 + "\", \"overlaypool\":\"" + pool_name3 + "\"}";
cmd[0] = (char *)cmdstr.c_str();
ASSERT_EQ(0, rados_mon_command(_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
EXPECT_EQ(0, rados_wait_for_latest_osdmap(_cluster));
string parent_name = get_temp_image_name();
string child_name1 = get_temp_image_name();
string child_name2 = get_temp_image_name();
string child_name3 = get_temp_image_name();
string child_name4 = get_temp_image_name();
char child_id1[4096];
char child_id2[4096];
char child_id3[4096];
char child_id4[4096];
rbd_image_t image1;
rbd_image_t image2;
rbd_image_t image3;
rbd_image_t image4;
rados_ioctx_t ioctx1, ioctx2;
rados_ioctx_create(_cluster, pool_name1.c_str(), &ioctx1);
rados_ioctx_create(_cluster, pool_name2.c_str(), &ioctx2);
bool old_format;
uint64_t features;
rbd_image_t parent;
int order = 0;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
// make a parent to clone from
ASSERT_EQ(0, create_image_full(ioctx1, parent_name.c_str(), 4<<20, &order,
false, features));
ASSERT_EQ(0, rbd_open(ioctx1, parent_name.c_str(), &parent, NULL));
// create a snapshot, reopen as the parent we're interested in
ASSERT_EQ(0, rbd_snap_create(parent, "parent_snap"));
ASSERT_EQ(0, rbd_snap_set(parent, "parent_snap"));
ASSERT_EQ(0, rbd_snap_protect(parent, "parent_snap"));
ASSERT_EQ(0, rbd_close(parent));
ASSERT_EQ(0, rbd_open(ioctx1, parent_name.c_str(), &parent, "parent_snap"));
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "parent_snap",
ioctx2, child_name1.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx2, child_name1.c_str(), &image1, NULL));
ASSERT_EQ(0, rbd_get_id(image1, child_id1, sizeof(child_id1)));
test_list_children(parent, 1, pool_name2.c_str(), child_name1.c_str());
test_list_children2(parent, 1,
child_id1, pool_name2.c_str(), child_name1.c_str(), false);
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "parent_snap",
ioctx1, child_name2.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx1, child_name2.c_str(), &image2, NULL));
ASSERT_EQ(0, rbd_get_id(image2, child_id2, sizeof(child_id2)));
test_list_children(parent, 2, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str());
test_list_children2(parent, 2,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false);
// read from the cache to populate it
rbd_image_t tier_image;
ASSERT_EQ(0, rbd_open(ioctx1, child_name2.c_str(), &tier_image, NULL));
size_t len = 4 * 1024 * 1024;
char* buf = (char*)malloc(len);
ssize_t size = rbd_read(tier_image, 0, len, buf);
ASSERT_GT(size, 0);
free(buf);
ASSERT_EQ(0, rbd_close(tier_image));
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "parent_snap",
ioctx2, child_name3.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx2, child_name3.c_str(), &image3, NULL));
ASSERT_EQ(0, rbd_get_id(image3, child_id3, sizeof(child_id3)));
test_list_children(parent, 3, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name3.c_str());
test_list_children2(parent, 3,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), false);
librados::IoCtx ioctx3;
ASSERT_EQ(0, _rados.ioctx_create(pool_name2.c_str(), ioctx3));
ASSERT_EQ(0, rbd_close(image3));
ASSERT_EQ(0, rbd.trash_move(ioctx3, child_name3.c_str(), 0));
test_list_children(parent, 2, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str());
test_list_children2(parent, 3,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), true);
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "parent_snap",
ioctx2, child_name4.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx2, child_name4.c_str(), &image4, NULL));
ASSERT_EQ(0, rbd_get_id(image4, child_id4, sizeof(child_id4)));
test_list_children(parent, 3, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name4.c_str());
test_list_children2(parent, 4,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), true,
child_id4, pool_name2.c_str(), child_name4.c_str(), false);
ASSERT_EQ(0, rbd.trash_restore(ioctx3, child_id3, ""));
test_list_children(parent, 4, pool_name2.c_str(), child_name1.c_str(),
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name3.c_str(),
pool_name2.c_str(), child_name4.c_str());
test_list_children2(parent, 4,
child_id1, pool_name2.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), false,
child_id4, pool_name2.c_str(), child_name4.c_str(), false);
ASSERT_EQ(0, rbd_close(image1));
ASSERT_EQ(0, rbd_remove(ioctx2, child_name1.c_str()));
test_list_children(parent, 3,
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name3.c_str(),
pool_name2.c_str(), child_name4.c_str());
test_list_children2(parent, 3,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, pool_name2.c_str(), child_name3.c_str(), false,
child_id4, pool_name2.c_str(), child_name4.c_str(), false);
ASSERT_EQ(0, rbd_remove(ioctx2, child_name3.c_str()));
test_list_children(parent, 2,
pool_name1.c_str(), child_name2.c_str(),
pool_name2.c_str(), child_name4.c_str());
test_list_children2(parent, 2,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id4, pool_name2.c_str(), child_name4.c_str(), false);
ASSERT_EQ(0, rbd_close(image4));
ASSERT_EQ(0, rbd_remove(ioctx2, child_name4.c_str()));
test_list_children(parent, 1,
pool_name1.c_str(), child_name2.c_str());
test_list_children2(parent, 1,
child_id2, pool_name1.c_str(), child_name2.c_str(), false);
ASSERT_EQ(0, rbd_close(image2));
ASSERT_EQ(0, rbd_remove(ioctx1, child_name2.c_str()));
test_list_children(parent, 0);
test_list_children2(parent, 0);
ASSERT_EQ(0, rbd_snap_unprotect(parent, "parent_snap"));
ASSERT_EQ(0, rbd_snap_remove(parent, "parent_snap"));
ASSERT_EQ(0, rbd_close(parent));
ASSERT_EQ(0, rbd_remove(ioctx1, parent_name.c_str()));
rados_ioctx_destroy(ioctx1);
rados_ioctx_destroy(ioctx2);
cmdstr = "{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" +
pool_name1 + "\"}";
cmd[0] = (char *)cmdstr.c_str();
ASSERT_EQ(0, rados_mon_command(_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
cmdstr = "{\"prefix\": \"osd tier remove\", \"pool\": \"" +
pool_name1 + "\", \"tierpool\":\"" + pool_name3 + "\"}";
cmd[0] = (char *)cmdstr.c_str();
ASSERT_EQ(0, rados_mon_command(_cluster, (const char **)cmd, 1, "", 0, NULL, 0, NULL, 0));
}
TEST_F(TestLibRBD, LockingPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
std::string cookie1 = "foo";
std::string cookie2 = "bar";
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
// no lockers initially
std::list<librbd::locker_t> lockers;
std::string tag;
bool exclusive;
ASSERT_EQ(0, image.list_lockers(&lockers, &exclusive, &tag));
ASSERT_EQ(0u, lockers.size());
ASSERT_EQ("", tag);
// exclusive lock is exclusive
ASSERT_EQ(0, image.lock_exclusive(cookie1));
ASSERT_EQ(-EEXIST, image.lock_exclusive(cookie1));
ASSERT_EQ(-EBUSY, image.lock_exclusive(""));
ASSERT_EQ(-EEXIST, image.lock_shared(cookie1, ""));
ASSERT_EQ(-EBUSY, image.lock_shared(cookie1, "test"));
ASSERT_EQ(-EBUSY, image.lock_shared("", "test"));
ASSERT_EQ(-EBUSY, image.lock_shared("", ""));
// list exclusive
ASSERT_EQ(0, image.list_lockers(&lockers, &exclusive, &tag));
ASSERT_TRUE(exclusive);
ASSERT_EQ("", tag);
ASSERT_EQ(1u, lockers.size());
ASSERT_EQ(cookie1, lockers.front().cookie);
// unlock
ASSERT_EQ(-ENOENT, image.unlock(""));
ASSERT_EQ(-ENOENT, image.unlock(cookie2));
ASSERT_EQ(0, image.unlock(cookie1));
ASSERT_EQ(-ENOENT, image.unlock(cookie1));
ASSERT_EQ(0, image.list_lockers(&lockers, &exclusive, &tag));
ASSERT_EQ(0u, lockers.size());
ASSERT_EQ(0, image.lock_shared(cookie1, ""));
ASSERT_EQ(-EEXIST, image.lock_shared(cookie1, ""));
ASSERT_EQ(0, image.lock_shared(cookie2, ""));
ASSERT_EQ(-EEXIST, image.lock_shared(cookie2, ""));
ASSERT_EQ(-EEXIST, image.lock_exclusive(cookie1));
ASSERT_EQ(-EEXIST, image.lock_exclusive(cookie2));
ASSERT_EQ(-EBUSY, image.lock_exclusive(""));
ASSERT_EQ(-EBUSY, image.lock_exclusive("test"));
// list shared
ASSERT_EQ(0, image.list_lockers(&lockers, &exclusive, &tag));
ASSERT_EQ(2u, lockers.size());
}
ioctx.close();
}
TEST_F(TestLibRBD, FlushAio)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
size_t num_aios = 256;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
char test_data[TEST_IO_SIZE + 1];
size_t i;
for (i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
rbd_completion_t write_comps[num_aios];
for (i = 0; i < num_aios; ++i) {
ASSERT_EQ(0, rbd_aio_create_completion(NULL, NULL, &write_comps[i]));
uint64_t offset = rand() % (size - TEST_IO_SIZE);
ASSERT_EQ(0, rbd_aio_write(image, offset, TEST_IO_SIZE, test_data,
write_comps[i]));
}
rbd_completion_t flush_comp;
ASSERT_EQ(0, rbd_aio_create_completion(NULL, NULL, &flush_comp));
ASSERT_EQ(0, rbd_aio_flush(image, flush_comp));
ASSERT_EQ(0, rbd_aio_wait_for_complete(flush_comp));
ASSERT_EQ(1, rbd_aio_is_complete(flush_comp));
rbd_aio_release(flush_comp);
for (i = 0; i < num_aios; ++i) {
ASSERT_EQ(1, rbd_aio_is_complete(write_comps[i]));
rbd_aio_release(write_comps[i]);
}
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_remove(ioctx, name.c_str()));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, FlushAioPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
const size_t num_aios = 256;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
char test_data[TEST_IO_SIZE + 1];
size_t i;
for (i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
librbd::RBD::AioCompletion *write_comps[num_aios];
ceph::bufferlist bls[num_aios];
for (i = 0; i < num_aios; ++i) {
bls[i].append(test_data, strlen(test_data));
write_comps[i] = new librbd::RBD::AioCompletion(NULL, NULL);
uint64_t offset = rand() % (size - TEST_IO_SIZE);
ASSERT_EQ(0, image.aio_write(offset, TEST_IO_SIZE, bls[i],
write_comps[i]));
}
librbd::RBD::AioCompletion *flush_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, image.aio_flush(flush_comp));
ASSERT_EQ(0, flush_comp->wait_for_complete());
ASSERT_EQ(1, flush_comp->is_complete());
flush_comp->release();
for (i = 0; i < num_aios; ++i) {
librbd::RBD::AioCompletion *comp = write_comps[i];
ASSERT_EQ(1, comp->is_complete());
comp->release();
}
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
int iterate_cb(uint64_t off, size_t len, int exists, void *arg)
{
//cout << "iterate_cb " << off << "~" << len << std::endl;
interval_set<uint64_t> *diff = static_cast<interval_set<uint64_t> *>(arg);
diff->insert(off, len);
return 0;
}
static int iterate_error_cb(uint64_t off, size_t len, int exists, void *arg)
{
return -EINVAL;
}
void scribble(librbd::Image& image, int n, int max, bool skip_discard,
interval_set<uint64_t> *exists,
interval_set<uint64_t> *what)
{
uint64_t size;
image.size(&size);
interval_set<uint64_t> exists_at_start = *exists;
for (int i=0; i<n; i++) {
uint64_t off = rand() % (size - max + 1);
uint64_t len = 1 + rand() % max;
if (!skip_discard && rand() % 4 == 0) {
ASSERT_EQ((int)len, image.discard(off, len));
interval_set<uint64_t> w;
w.insert(off, len);
// the zeroed bit no longer exists...
w.intersection_of(*exists);
exists->subtract(w);
// the bits we discarded are no long written...
interval_set<uint64_t> w2 = w;
w2.intersection_of(*what);
what->subtract(w2);
// except for the extents that existed at the start that we overwrote.
interval_set<uint64_t> w3;
w3.insert(off, len);
w3.intersection_of(exists_at_start);
what->union_of(w3);
} else {
bufferlist bl;
bl.append(buffer::create(len));
bl.zero();
ASSERT_EQ((int)len, image.write(off, len, bl));
interval_set<uint64_t> w;
w.insert(off, len);
what->union_of(w);
exists->union_of(w);
}
}
}
interval_set<uint64_t> round_diff_interval(const interval_set<uint64_t>& diff,
uint64_t object_size)
{
if (object_size == 0) {
return diff;
}
interval_set<uint64_t> rounded_diff;
for (interval_set<uint64_t>::const_iterator it = diff.begin();
it != diff.end(); ++it) {
uint64_t off = it.get_start();
uint64_t len = it.get_len();
off -= off % object_size;
len += (object_size - (len % object_size));
interval_set<uint64_t> interval;
interval.insert(off, len);
rounded_diff.union_of(interval);
}
return rounded_diff;
}
TEST_F(TestLibRBD, SnapDiff)
{
REQUIRE_FEATURE(RBD_FEATURE_FAST_DIFF);
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string image_name = get_temp_image_name();
uint64_t size = 100 << 20;
ASSERT_EQ(0, create_image(ioctx, image_name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, image_name.c_str(), &image, nullptr));
char test_data[TEST_IO_SIZE + 1];
for (size_t i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
test_data[TEST_IO_SIZE] = '\0';
ASSERT_PASSED(write_test_data, image, test_data, 0,
TEST_IO_SIZE, LIBRADOS_OP_FLAG_FADVISE_NOCACHE);
interval_set<uint64_t> diff;
ASSERT_EQ(0, rbd_diff_iterate2(image, nullptr, 0, size, true, true,
iterate_cb, &diff));
EXPECT_EQ(1 << order, diff.size());
ASSERT_EQ(0, rbd_snap_create(image, "snap1"));
ASSERT_EQ(0, rbd_snap_create(image, "snap2"));
diff.clear();
ASSERT_EQ(0, rbd_diff_iterate2(image, nullptr, 0, size, true, true,
iterate_cb, &diff));
EXPECT_EQ(1 << order, diff.size());
diff.clear();
ASSERT_EQ(0, rbd_diff_iterate2(image, "snap1", 0, size, true, true,
iterate_cb, &diff));
EXPECT_EQ(0, diff.size());
diff.clear();
ASSERT_EQ(0, rbd_diff_iterate2(image, "snap2", 0, size, true, true,
iterate_cb, &diff));
EXPECT_EQ(0, diff.size());
ASSERT_EQ(0, rbd_snap_remove(image, "snap1"));
ASSERT_EQ(0, rbd_snap_remove(image, "snap2"));
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_remove(ioctx, image_name.c_str()));
rados_ioctx_destroy(ioctx);
}
template <typename T>
class DiffIterateTest : public TestLibRBD {
public:
static const uint8_t whole_object = T::whole_object;
};
template <bool _whole_object>
class DiffIterateParams {
public:
static const uint8_t whole_object = _whole_object;
};
typedef ::testing::Types<DiffIterateParams<false>,
DiffIterateParams<true> > DiffIterateTypes;
TYPED_TEST_SUITE(DiffIterateTest, DiffIterateTypes);
TYPED_TEST(DiffIterateTest, DiffIterate)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = this->get_temp_image_name();
uint64_t size = 20 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool skip_discard = this->is_skip_partial_discard_enabled(image);
uint64_t object_size = 0;
if (this->whole_object) {
object_size = 1 << order;
}
interval_set<uint64_t> exists;
interval_set<uint64_t> one, two;
scribble(image, 10, 102400, skip_discard, &exists, &one);
cout << " wrote " << one << std::endl;
ASSERT_EQ(0, image.snap_create("one"));
scribble(image, 10, 102400, skip_discard, &exists, &two);
two = round_diff_interval(two, object_size);
cout << " wrote " << two << std::endl;
interval_set<uint64_t> diff;
ASSERT_EQ(0, image.diff_iterate2("one", 0, size, true, this->whole_object,
iterate_cb, (void *)&diff));
cout << " diff was " << diff << std::endl;
if (!two.subset_of(diff)) {
interval_set<uint64_t> i;
i.intersection_of(two, diff);
interval_set<uint64_t> l = two;
l.subtract(i);
cout << " ... two - (two*diff) = " << l << std::endl;
}
ASSERT_TRUE(two.subset_of(diff));
}
ioctx.close();
}
struct diff_extent {
diff_extent(uint64_t _offset, uint64_t _length, bool _exists,
uint64_t object_size) :
offset(_offset), length(_length), exists(_exists)
{
if (object_size != 0) {
offset -= offset % object_size;
length = object_size;
}
}
uint64_t offset;
uint64_t length;
bool exists;
bool operator==(const diff_extent& o) const {
return offset == o.offset && length == o.length && exists == o.exists;
}
};
ostream& operator<<(ostream & o, const diff_extent& e) {
return o << '(' << e.offset << '~' << e.length << ' ' << (e.exists ? "true" : "false") << ')';
}
int vector_iterate_cb(uint64_t off, size_t len, int exists, void *arg)
{
cout << "iterate_cb " << off << "~" << len << std::endl;
vector<diff_extent> *diff = static_cast<vector<diff_extent> *>(arg);
diff->push_back(diff_extent(off, len, exists, 0));
return 0;
}
TYPED_TEST(DiffIterateTest, DiffIterateDiscard)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = this->get_temp_image_name();
uint64_t size = 20 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
uint64_t object_size = 0;
if (this->whole_object) {
object_size = 1 << order;
}
vector<diff_extent> extents;
ceph::bufferlist bl;
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(0u, extents.size());
char data[256];
memset(data, 1, sizeof(data));
bl.append(data, 256);
ASSERT_EQ(256, image.write(0, 256, bl));
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(1u, extents.size());
ASSERT_EQ(diff_extent(0, 256, true, object_size), extents[0]);
int obj_ofs = 256;
ASSERT_EQ(1 << order, image.discard(0, 1 << order));
extents.clear();
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(0u, extents.size());
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_EQ(256, image.write(0, 256, bl));
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(1u, extents.size());
ASSERT_EQ(diff_extent(0, 256, true, object_size), extents[0]);
ASSERT_EQ(0, image.snap_create("snap2"));
ASSERT_EQ(obj_ofs, image.discard(0, obj_ofs));
extents.clear();
ASSERT_EQ(0, image.snap_set("snap2"));
ASSERT_EQ(0, image.diff_iterate2("snap1", 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(1u, extents.size());
ASSERT_EQ(diff_extent(0, 256, true, object_size), extents[0]);
ASSERT_EQ(0, image.snap_set(NULL));
ASSERT_EQ(1 << order, image.discard(0, 1 << order));
ASSERT_EQ(0, image.snap_create("snap3"));
ASSERT_EQ(0, image.snap_set("snap3"));
extents.clear();
ASSERT_EQ(0, image.diff_iterate2("snap1", 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(1u, extents.size());
ASSERT_EQ(diff_extent(0, 256, false, object_size), extents[0]);
ASSERT_PASSED(this->validate_object_map, image);
}
TYPED_TEST(DiffIterateTest, DiffIterateStress)
{
REQUIRE(!is_rbd_pwl_enabled((CephContext *)this->_rados.cct()));
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = this->get_temp_image_name();
uint64_t size = 400 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool skip_discard = this->is_skip_partial_discard_enabled(image);
uint64_t object_size = 0;
if (this->whole_object) {
object_size = 1 << order;
}
interval_set<uint64_t> curexists;
vector<interval_set<uint64_t> > wrote;
vector<interval_set<uint64_t> > exists;
vector<string> snap;
int n = 20;
for (int i=0; i<n; i++) {
interval_set<uint64_t> w;
scribble(image, 10, 8192000, skip_discard, &curexists, &w);
cout << " i=" << i << " exists " << curexists << " wrote " << w << std::endl;
string s = "snap" + stringify(i);
ASSERT_EQ(0, image.snap_create(s.c_str()));
wrote.push_back(w);
exists.push_back(curexists);
snap.push_back(s);
}
for (int h=0; h<n-1; h++) {
for (int i=0; i<n-h-1; i++) {
for (int j=(h==0 ? i+1 : n-1); j<n; j++) {
interval_set<uint64_t> diff, actual, uex;
for (int k=i+1; k<=j; k++)
diff.union_of(wrote[k]);
cout << "from " << i << " to "
<< (h != 0 ? string("HEAD") : stringify(j)) << " diff "
<< round_diff_interval(diff, object_size) << std::endl;
// limit to extents that exists both at the beginning and at the end
uex.union_of(exists[i], exists[j]);
diff.intersection_of(uex);
diff = round_diff_interval(diff, object_size);
cout << " limited diff " << diff << std::endl;
ASSERT_EQ(0, image.snap_set(h==0 ? snap[j].c_str() : NULL));
ASSERT_EQ(0, image.diff_iterate2(snap[i].c_str(), 0, size, true,
this->whole_object, iterate_cb,
(void *)&actual));
cout << " actual was " << actual << std::endl;
if (!diff.subset_of(actual)) {
interval_set<uint64_t> i;
i.intersection_of(diff, actual);
interval_set<uint64_t> l = diff;
l.subtract(i);
cout << " ... diff - (actual*diff) = " << l << std::endl;
}
ASSERT_TRUE(diff.subset_of(actual));
}
}
ASSERT_EQ(0, image.snap_set(NULL));
ASSERT_EQ(0, image.snap_remove(snap[n-h-1].c_str()));
}
ASSERT_PASSED(this->validate_object_map, image);
}
TYPED_TEST(DiffIterateTest, DiffIterateRegression6926)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = this->get_temp_image_name();
uint64_t size = 20 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
uint64_t object_size = 0;
if (this->whole_object) {
object_size = 1 << order;
}
vector<diff_extent> extents;
ceph::bufferlist bl;
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(0u, extents.size());
ASSERT_EQ(0, image.snap_create("snap1"));
char data[256];
memset(data, 1, sizeof(data));
bl.append(data, 256);
ASSERT_EQ(256, image.write(0, 256, bl));
extents.clear();
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(1u, extents.size());
ASSERT_EQ(diff_extent(0, 256, true, object_size), extents[0]);
ASSERT_EQ(0, image.snap_set("snap1"));
extents.clear();
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, (void *) &extents));
ASSERT_EQ(static_cast<size_t>(0), extents.size());
}
TYPED_TEST(DiffIterateTest, DiffIterateParent)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 22;
std::string name = this->get_temp_image_name();
ssize_t size = 20 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
uint64_t features;
ASSERT_EQ(0, image.features(&features));
uint64_t object_size = 0;
if (this->whole_object) {
object_size = 1 << order;
}
ceph::bufferlist bl;
bl.append(std::string(size, '1'));
ASSERT_EQ(size, image.write(0, size, bl));
ASSERT_EQ(0, image.snap_create("snap"));
ASSERT_EQ(0, image.snap_protect("snap"));
std::string clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(ioctx, name.c_str(), "snap", ioctx,
clone_name.c_str(), features, &order));
librbd::Image clone;
ASSERT_EQ(0, rbd.open(ioctx, clone, clone_name.c_str(), NULL));
std::vector<diff_extent> extents;
ASSERT_EQ(0, clone.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, &extents));
ASSERT_EQ(5u, extents.size());
ASSERT_EQ(diff_extent(0, 4194304, true, object_size), extents[0]);
ASSERT_EQ(diff_extent(4194304, 4194304, true, object_size), extents[1]);
ASSERT_EQ(diff_extent(8388608, 4194304, true, object_size), extents[2]);
ASSERT_EQ(diff_extent(12582912, 4194304, true, object_size), extents[3]);
ASSERT_EQ(diff_extent(16777216, 4194304, true, object_size), extents[4]);
extents.clear();
ASSERT_EQ(0, clone.resize(size / 2));
ASSERT_EQ(0, clone.resize(size));
ASSERT_EQ(1, clone.write(size - 1, 1, bl));
ASSERT_EQ(0, clone.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, &extents));
ASSERT_EQ(4u, extents.size());
ASSERT_EQ(diff_extent(0, 4194304, true, object_size), extents[0]);
ASSERT_EQ(diff_extent(4194304, 4194304, true, object_size), extents[1]);
ASSERT_EQ(diff_extent(8388608, 2097152, true, object_size), extents[2]);
// hole (parent overlap = 10M) followed by copyup'ed object
ASSERT_EQ(diff_extent(16777216, 4194304, true, object_size), extents[3]);
ASSERT_PASSED(this->validate_object_map, image);
ASSERT_PASSED(this->validate_object_map, clone);
}
ioctx.close();
}
TYPED_TEST(DiffIterateTest, DiffIterateIgnoreParent)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
std::string name = this->get_temp_image_name();
uint64_t size = 20 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool skip_discard = this->is_skip_partial_discard_enabled(image);
uint64_t features;
ASSERT_EQ(0, image.features(&features));
uint64_t object_size = 0;
if (this->whole_object) {
object_size = 1 << order;
}
bufferlist bl;
bl.append(buffer::create(size));
bl.zero();
interval_set<uint64_t> one;
one.insert(0, size);
ASSERT_EQ((int)size, image.write(0, size, bl));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
std::string clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(ioctx, name.c_str(), "one", ioctx, clone_name.c_str(),
features, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, clone_name.c_str(), NULL));
interval_set<uint64_t> exists;
interval_set<uint64_t> two;
scribble(image, 10, 102400, skip_discard, &exists, &two);
two = round_diff_interval(two, object_size);
cout << " wrote " << two << " to clone" << std::endl;
interval_set<uint64_t> diff;
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, false, this->whole_object,
iterate_cb, (void *)&diff));
cout << " diff was " << diff << std::endl;
if (!this->whole_object) {
ASSERT_FALSE(one.subset_of(diff));
}
ASSERT_TRUE(two.subset_of(diff));
}
TYPED_TEST(DiffIterateTest, DiffIterateCallbackError)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = this->get_temp_image_name();
uint64_t size = 20 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool skip_discard = this->is_skip_partial_discard_enabled(image);
interval_set<uint64_t> exists;
interval_set<uint64_t> one;
scribble(image, 10, 102400, skip_discard, &exists, &one);
cout << " wrote " << one << std::endl;
interval_set<uint64_t> diff;
ASSERT_EQ(-EINVAL, image.diff_iterate2(NULL, 0, size, true,
this->whole_object,
iterate_error_cb, NULL));
}
ioctx.close();
}
TYPED_TEST(DiffIterateTest, DiffIterateParentDiscard)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
std::string name = this->get_temp_image_name();
uint64_t size = 20 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool skip_discard = this->is_skip_partial_discard_enabled(image);
uint64_t features;
ASSERT_EQ(0, image.features(&features));
uint64_t object_size = 0;
if (this->whole_object) {
object_size = 1 << order;
}
interval_set<uint64_t> exists;
interval_set<uint64_t> one;
scribble(image, 10, 102400, skip_discard, &exists, &one);
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(1 << order, image.discard(0, 1 << order));
ASSERT_EQ(0, image.snap_create("two"));
ASSERT_EQ(0, image.snap_protect("two"));
exists.clear();
one.clear();
std::string clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(ioctx, name.c_str(), "two", ioctx,
clone_name.c_str(), features, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, clone_name.c_str(), NULL));
interval_set<uint64_t> two;
scribble(image, 10, 102400, skip_discard, &exists, &two);
two = round_diff_interval(two, object_size);
interval_set<uint64_t> diff;
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
iterate_cb, (void *)&diff));
ASSERT_TRUE(two.subset_of(diff));
}
TYPED_TEST(DiffIterateTest, DiffIterateUnalignedSmall)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 0;
std::string name = this->get_temp_image_name();
ssize_t size = 10 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ceph::bufferlist bl;
bl.append(std::string(size, '1'));
ASSERT_EQ(size, image.write(0, size, bl));
std::vector<diff_extent> extents;
ASSERT_EQ(0, image.diff_iterate2(NULL, 5000005, 1234, true,
this->whole_object, vector_iterate_cb,
&extents));
ASSERT_EQ(1u, extents.size());
ASSERT_EQ(diff_extent(5000005, 1234, true, 0), extents[0]);
ASSERT_PASSED(this->validate_object_map, image);
}
ioctx.close();
}
TYPED_TEST(DiffIterateTest, DiffIterateUnaligned)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 22;
std::string name = this->get_temp_image_name();
ssize_t size = 20 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ceph::bufferlist bl;
bl.append(std::string(size, '1'));
ASSERT_EQ(size, image.write(0, size, bl));
std::vector<diff_extent> extents;
ASSERT_EQ(0, image.diff_iterate2(NULL, 8376263, 4260970, true,
this->whole_object, vector_iterate_cb,
&extents));
ASSERT_EQ(3u, extents.size());
ASSERT_EQ(diff_extent(8376263, 12345, true, 0), extents[0]);
ASSERT_EQ(diff_extent(8388608, 4194304, true, 0), extents[1]);
ASSERT_EQ(diff_extent(12582912, 54321, true, 0), extents[2]);
ASSERT_PASSED(this->validate_object_map, image);
}
ioctx.close();
}
TYPED_TEST(DiffIterateTest, DiffIterateStriping)
{
REQUIRE_FEATURE(RBD_FEATURE_STRIPINGV2);
librados::IoCtx ioctx;
ASSERT_EQ(0, this->_rados.ioctx_create(this->m_pool_name.c_str(), ioctx));
bool old_format;
uint64_t features;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
{
librbd::RBD rbd;
librbd::Image image;
int order = 22;
std::string name = this->get_temp_image_name();
ssize_t size = 24 << 20;
ASSERT_EQ(0, rbd.create3(ioctx, name.c_str(), size, features, &order,
1 << 20, 3));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ceph::bufferlist bl;
bl.append(std::string(size, '1'));
ASSERT_EQ(size, image.write(0, size, bl));
std::vector<diff_extent> extents;
ASSERT_EQ(0, image.diff_iterate2(NULL, 0, size, true, this->whole_object,
vector_iterate_cb, &extents));
ASSERT_EQ(2u, extents.size());
ASSERT_EQ(diff_extent(0, 12 << 20, true, 0), extents[0]);
ASSERT_EQ(diff_extent(12 << 20, 12 << 20, true, 0), extents[1]);
extents.clear();
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(size, image.discard(0, size));
ASSERT_EQ(0, image.diff_iterate2("one", 0, size, true, this->whole_object,
vector_iterate_cb, &extents));
ASSERT_EQ(2u, extents.size());
ASSERT_EQ(diff_extent(0, 12 << 20, false, 0), extents[0]);
ASSERT_EQ(diff_extent(12 << 20, 12 << 20, false, 0), extents[1]);
extents.clear();
ASSERT_EQ(1 << 20, image.write(0, 1 << 20, bl));
ASSERT_EQ(2 << 20, image.write(2 << 20, 2 << 20, bl));
ASSERT_EQ(2 << 20, image.write(5 << 20, 2 << 20, bl));
ASSERT_EQ(2 << 20, image.write(8 << 20, 2 << 20, bl));
ASSERT_EQ(13 << 20, image.write(11 << 20, 13 << 20, bl));
ASSERT_EQ(0, image.diff_iterate2("one", 0, size, true, this->whole_object,
vector_iterate_cb, &extents));
ASSERT_EQ(10u, extents.size());
ASSERT_EQ(diff_extent(0, 1 << 20, true, 0), extents[0]);
ASSERT_EQ(diff_extent(1 << 20, 1 << 20, false, 0), extents[1]);
ASSERT_EQ(diff_extent(2 << 20, 2 << 20, true, 0), extents[2]);
ASSERT_EQ(diff_extent(4 << 20, 1 << 20, false, 0), extents[3]);
ASSERT_EQ(diff_extent(5 << 20, 2 << 20, true, 0), extents[4]);
ASSERT_EQ(diff_extent(7 << 20, 1 << 20, false, 0), extents[5]);
ASSERT_EQ(diff_extent(8 << 20, 2 << 20, true, 0), extents[6]);
ASSERT_EQ(diff_extent(10 << 20, 1 << 20, false, 0), extents[7]);
ASSERT_EQ(diff_extent(11 << 20, 1 << 20, true, 0), extents[8]);
ASSERT_EQ(diff_extent(12 << 20, 12 << 20, true, 0), extents[9]);
ASSERT_PASSED(this->validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, ZeroLengthWrite)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
char read_data[1];
ASSERT_EQ(0, rbd_write(image, 0, 0, NULL));
ASSERT_EQ(1, rbd_read(image, 0, 1, read_data));
ASSERT_EQ('\0', read_data[0]);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, ZeroLengthDiscard)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
const char data[] = "blah";
char read_data[sizeof(data)];
ASSERT_EQ((int)strlen(data), rbd_write(image, 0, strlen(data), data));
ASSERT_EQ(0, rbd_discard(image, 0, 0));
ASSERT_EQ((int)strlen(data), rbd_read(image, 0, strlen(data), read_data));
ASSERT_EQ(0, memcmp(data, read_data, strlen(data)));
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, ZeroLengthRead)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
char read_data[1];
ASSERT_EQ(0, rbd_read(image, 0, 0, read_data));
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, LargeCacheRead)
{
std::string config_value;
ASSERT_EQ(0, _rados.conf_get("rbd_cache", config_value));
if (config_value == "false") {
GTEST_SKIP() << "Skipping due to disabled cache";
}
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
uint32_t new_cache_size = 1 << 20;
std::string orig_cache_size;
ASSERT_EQ(0, _rados.conf_get("rbd_cache_size", orig_cache_size));
ASSERT_EQ(0, _rados.conf_set("rbd_cache_size",
stringify(new_cache_size).c_str()));
ASSERT_EQ(0, _rados.conf_get("rbd_cache_size", config_value));
ASSERT_EQ(stringify(new_cache_size), config_value);
BOOST_SCOPE_EXIT( (orig_cache_size) ) {
ASSERT_EQ(0, _rados.conf_set("rbd_cache_size", orig_cache_size.c_str()));
} BOOST_SCOPE_EXIT_END;
rbd_image_t image;
int order = 21;
std::string name = get_temp_image_name();
uint64_t size = 1 << order;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
std::string buffer(1 << order, '1');
ASSERT_EQ(static_cast<ssize_t>(buffer.size()),
rbd_write(image, 0, buffer.size(), buffer.c_str()));
ASSERT_EQ(0, rbd_invalidate_cache(image));
ASSERT_EQ(static_cast<ssize_t>(buffer.size()),
rbd_read(image, 0, buffer.size(), &buffer[0]));
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestPendingAio)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
bool old_format;
uint64_t features;
rbd_image_t image;
int order = 0;
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
std::string name = get_temp_image_name();
uint64_t size = 4 << 20;
ASSERT_EQ(0, create_image_full(ioctx, name.c_str(), size, &order,
false, features));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_invalidate_cache(image));
char test_data[TEST_IO_SIZE];
for (size_t i = 0; i < TEST_IO_SIZE; ++i) {
test_data[i] = (char) (rand() % (126 - 33) + 33);
}
size_t num_aios = 256;
rbd_completion_t comps[num_aios];
for (size_t i = 0; i < num_aios; ++i) {
ASSERT_EQ(0, rbd_aio_create_completion(NULL, NULL, &comps[i]));
uint64_t offset = rand() % (size - TEST_IO_SIZE);
ASSERT_EQ(0, rbd_aio_write(image, offset, TEST_IO_SIZE, test_data,
comps[i]));
}
for (size_t i = 0; i < num_aios; ++i) {
ASSERT_EQ(0, rbd_aio_wait_for_complete(comps[i]));
rbd_aio_release(comps[i]);
}
ASSERT_EQ(0, rbd_invalidate_cache(image));
for (size_t i = 0; i < num_aios; ++i) {
ASSERT_EQ(0, rbd_aio_create_completion(NULL, NULL, &comps[i]));
uint64_t offset = rand() % (size - TEST_IO_SIZE);
ASSERT_LE(0, rbd_aio_read(image, offset, TEST_IO_SIZE, test_data,
comps[i]));
}
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
for (size_t i = 0; i < num_aios; ++i) {
ASSERT_EQ(1, rbd_aio_is_complete(comps[i]));
rbd_aio_release(comps[i]);
}
rados_ioctx_destroy(ioctx);
}
void compare_and_write_copyup(librados::IoCtx &ioctx, bool deep_copyup,
bool *passed)
{
librbd::RBD rbd;
std::string parent_name = TestLibRBD::get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(), size, &order));
librbd::Image parent_image;
ASSERT_EQ(0, rbd.open(ioctx, parent_image, parent_name.c_str(), NULL));
bufferlist bl;
bl.append(std::string(4096, '1'));
ASSERT_EQ((ssize_t)bl.length(), parent_image.write(0, bl.length(), bl));
ASSERT_EQ(0, parent_image.snap_create("snap1"));
ASSERT_EQ(0, parent_image.snap_protect("snap1"));
uint64_t features;
ASSERT_EQ(0, parent_image.features(&features));
std::string clone_name = TestLibRBD::get_temp_image_name();
EXPECT_EQ(0, rbd.clone(ioctx, parent_name.c_str(), "snap1", ioctx,
clone_name.c_str(), features, &order));
librbd::Image clone_image;
ASSERT_EQ(0, rbd.open(ioctx, clone_image, clone_name.c_str(), NULL));
if (deep_copyup) {
ASSERT_EQ(0, clone_image.snap_create("snap1"));
}
bufferlist cmp_bl;
cmp_bl.append(std::string(512, '1'));
bufferlist write_bl;
write_bl.append(std::string(512, '2'));
uint64_t mismatch_off = 0;
ASSERT_EQ((ssize_t)write_bl.length(),
clone_image.compare_and_write(512, write_bl.length(), cmp_bl,
write_bl, &mismatch_off, 0));
ASSERT_EQ(0U, mismatch_off);
bufferlist read_bl;
ASSERT_EQ(4096, clone_image.read(0, 4096, read_bl));
bufferlist expected_bl;
expected_bl.append(std::string(512, '1'));
expected_bl.append(std::string(512, '2'));
expected_bl.append(std::string(3072, '1'));
ASSERT_TRUE(expected_bl.contents_equal(read_bl));
*passed = true;
}
TEST_F(TestLibRBD, CompareAndWriteCopyup)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
ASSERT_PASSED(compare_and_write_copyup, ioctx, false);
ASSERT_PASSED(compare_and_write_copyup, ioctx, true);
}
void compare_and_write_copyup_mismatch(librados::IoCtx &ioctx,
bool deep_copyup, bool *passed)
{
librbd::RBD rbd;
std::string parent_name = TestLibRBD::get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(), size, &order));
librbd::Image parent_image;
ASSERT_EQ(0, rbd.open(ioctx, parent_image, parent_name.c_str(), NULL));
bufferlist bl;
bl.append(std::string(4096, '1'));
ASSERT_EQ((ssize_t)bl.length(), parent_image.write(0, bl.length(), bl));
ASSERT_EQ(0, parent_image.snap_create("snap1"));
ASSERT_EQ(0, parent_image.snap_protect("snap1"));
uint64_t features;
ASSERT_EQ(0, parent_image.features(&features));
std::string clone_name = TestLibRBD::get_temp_image_name();
EXPECT_EQ(0, rbd.clone(ioctx, parent_name.c_str(), "snap1", ioctx,
clone_name.c_str(), features, &order));
librbd::Image clone_image;
ASSERT_EQ(0, rbd.open(ioctx, clone_image, clone_name.c_str(), NULL));
if (deep_copyup) {
ASSERT_EQ(0, clone_image.snap_create("snap1"));
}
bufferlist cmp_bl;
cmp_bl.append(std::string(48, '1'));
cmp_bl.append(std::string(464, '3'));
bufferlist write_bl;
write_bl.append(std::string(512, '2'));
uint64_t mismatch_off = 0;
ASSERT_EQ(-EILSEQ,
clone_image.compare_and_write(512, write_bl.length(), cmp_bl,
write_bl, &mismatch_off, 0));
ASSERT_EQ(48U, mismatch_off);
bufferlist read_bl;
ASSERT_EQ(4096, clone_image.read(0, 4096, read_bl));
ASSERT_TRUE(bl.contents_equal(read_bl));
*passed = true;
}
TEST_F(TestLibRBD, CompareAndWriteCopyupMismatch)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
ASSERT_PASSED(compare_and_write_copyup_mismatch, ioctx, false);
ASSERT_PASSED(compare_and_write_copyup_mismatch, ioctx, true);
}
TEST_F(TestLibRBD, Flatten)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string parent_name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(), size, &order));
librbd::Image parent_image;
ASSERT_EQ(0, rbd.open(ioctx, parent_image, parent_name.c_str(), NULL));
bufferlist bl;
bl.append(std::string(4096, '1'));
ASSERT_EQ((ssize_t)bl.length(), parent_image.write(0, bl.length(), bl));
ASSERT_EQ(0, parent_image.snap_create("snap1"));
ASSERT_EQ(0, parent_image.snap_protect("snap1"));
uint64_t features;
ASSERT_EQ(0, parent_image.features(&features));
std::string clone_name = get_temp_image_name();
EXPECT_EQ(0, rbd.clone(ioctx, parent_name.c_str(), "snap1", ioctx,
clone_name.c_str(), features, &order));
librbd::Image clone_image;
ASSERT_EQ(0, rbd.open(ioctx, clone_image, clone_name.c_str(), NULL));
ASSERT_EQ(0, clone_image.flatten());
librbd::RBD::AioCompletion *read_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
bufferlist read_bl;
clone_image.aio_read(0, bl.length(), read_bl, read_comp);
ASSERT_EQ(0, read_comp->wait_for_complete());
ASSERT_EQ((ssize_t)bl.length(), read_comp->get_return_value());
read_comp->release();
ASSERT_TRUE(bl.contents_equal(read_bl));
ASSERT_PASSED(validate_object_map, clone_image);
}
TEST_F(TestLibRBD, Sparsify)
{
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
BOOST_SCOPE_EXIT_ALL(&ioctx) {
rados_ioctx_destroy(ioctx);
};
const size_t CHUNK_SIZE = 4096 * 2;
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = CHUNK_SIZE * 1024;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
BOOST_SCOPE_EXIT_ALL(&image) {
rbd_close(image);
};
char test_data[4 * CHUNK_SIZE + 1];
for (size_t i = 0; i < 4 ; ++i) {
for (size_t j = 0; j < CHUNK_SIZE; j++) {
if (i % 2) {
test_data[i * CHUNK_SIZE + j] = (char)(rand() % (126 - 33) + 33);
} else {
test_data[i * CHUNK_SIZE + j] = '\0';
}
}
}
test_data[4 * CHUNK_SIZE] = '\0';
ASSERT_PASSED(write_test_data, image, test_data, 0, 4 * CHUNK_SIZE, 0);
ASSERT_EQ(0, rbd_flush(image));
ASSERT_EQ(-EINVAL, rbd_sparsify(image, 16));
ASSERT_EQ(-EINVAL, rbd_sparsify(image, 1 << (order + 1)));
ASSERT_EQ(-EINVAL, rbd_sparsify(image, 4096 + 1));
ASSERT_EQ(0, rbd_sparsify(image, 4096));
ASSERT_PASSED(read_test_data, image, test_data, 0, 4 * CHUNK_SIZE, 0);
}
TEST_F(TestLibRBD, SparsifyPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 12 * 1024 * 1024;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
bufferlist bl;
bl.append(std::string(4096, '\0'));
bl.append(std::string(4096, '1'));
bl.append(std::string(4096, '\0'));
ASSERT_EQ((ssize_t)bl.length(), image.write(0, bl.length(), bl));
ASSERT_EQ(0, image.flush());
ASSERT_EQ(-EINVAL, image.sparsify(16));
ASSERT_EQ(-EINVAL, image.sparsify(1 << (order + 1)));
ASSERT_EQ(-EINVAL, image.sparsify(4096 + 1));
ASSERT_EQ(0, image.sparsify(4096));
bufferlist read_bl;
ASSERT_EQ((ssize_t)bl.length(), image.read(0, bl.length(), read_bl));
ASSERT_TRUE(bl.contents_equal(read_bl));
ASSERT_PASSED(validate_object_map, image);
}
TEST_F(TestLibRBD, SnapshotLimit)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
uint64_t limit;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_snap_get_limit(image, &limit));
ASSERT_EQ(UINT64_MAX, limit);
ASSERT_EQ(0, rbd_snap_set_limit(image, 2));
ASSERT_EQ(0, rbd_snap_get_limit(image, &limit));
ASSERT_EQ(2U, limit);
ASSERT_EQ(0, rbd_snap_create(image, "snap1"));
ASSERT_EQ(-ERANGE, rbd_snap_set_limit(image, 0));
ASSERT_EQ(0, rbd_snap_create(image, "snap2"));
ASSERT_EQ(-EDQUOT, rbd_snap_create(image, "snap3"));
ASSERT_EQ(0, rbd_snap_set_limit(image, UINT64_MAX));
ASSERT_EQ(0, rbd_snap_create(image, "snap3"));
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, SnapshotLimitPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
uint64_t limit;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ASSERT_EQ(0, image.snap_get_limit(&limit));
ASSERT_EQ(UINT64_MAX, limit);
ASSERT_EQ(0, image.snap_set_limit(2));
ASSERT_EQ(0, image.snap_get_limit(&limit));
ASSERT_EQ(2U, limit);
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_EQ(-ERANGE, image.snap_set_limit(0));
ASSERT_EQ(0, image.snap_create("snap2"));
ASSERT_EQ(-EDQUOT, image.snap_create("snap3"));
ASSERT_EQ(0, image.snap_set_limit(UINT64_MAX));
ASSERT_EQ(0, image.snap_create("snap3"));
}
ioctx.close();
}
TEST_F(TestLibRBD, RebuildObjectMapViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_OBJECT_MAP);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
std::string object_map_oid;
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string image_id;
ASSERT_EQ(0, get_image_id(image, &image_id));
object_map_oid = RBD_OBJECT_MAP_PREFIX + image_id;
}
// corrupt the object map
bufferlist bl;
bl.append("foo");
ASSERT_EQ(0, ioctx.write(object_map_oid, bl, bl.length(), 0));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bool lock_owner;
bl.clear();
ASSERT_EQ(0, image1.write(0, 0, bl));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
uint64_t flags;
ASSERT_EQ(0, image1.get_flags(&flags));
ASSERT_TRUE((flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
PrintProgress prog_ctx;
ASSERT_EQ(0, image2.rebuild_object_map(prog_ctx));
ASSERT_PASSED(validate_object_map, image1);
ASSERT_PASSED(validate_object_map, image2);
}
TEST_F(TestLibRBD, RenameViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
std::string new_name = get_temp_image_name();
ASSERT_EQ(0, rbd.rename(ioctx, name.c_str(), new_name.c_str()));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
name = new_name;
new_name = get_temp_image_name();
ASSERT_EQ(0, rbd.rename(ioctx, name.c_str(), new_name.c_str()));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, new_name.c_str(), NULL));
}
TEST_F(TestLibRBD, SnapCreateViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
// switch to writeback cache
ASSERT_EQ(0, image1.flush());
bufferlist bl;
bl.append(std::string(4096, '1'));
ASSERT_EQ((ssize_t)bl.length(), image1.write(0, bl.length(), bl));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.snap_create("snap1"));
bool exists;
ASSERT_EQ(0, image1.snap_exists2("snap1", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(0, image2.snap_exists2("snap1", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
}
TEST_F(TestLibRBD, SnapRemoveViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_FAST_DIFF);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
ASSERT_EQ(0, image1.snap_create("snap1"));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.snap_remove("snap1"));
bool exists;
ASSERT_EQ(0, image1.snap_exists2("snap1", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, image2.snap_exists2("snap1", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
}
TEST_F(TestLibRBD, UpdateFeaturesViaLockOwner) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
librbd::RBD rbd;
int order = 0;
//creates full with rbd default features
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
bool lock_owner;
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.update_features(RBD_FEATURE_OBJECT_MAP, false));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.update_features(RBD_FEATURE_OBJECT_MAP, true));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
}
TEST_F(TestLibRBD, EnableJournalingViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.update_features(RBD_FEATURE_JOURNALING, false));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.update_features(RBD_FEATURE_JOURNALING, true));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
}
TEST_F(TestLibRBD, SnapRemove2)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
ASSERT_EQ(0, image1.snap_create("snap1"));
bool exists;
ASSERT_EQ(0, image1.snap_exists2("snap1", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(0, image1.snap_protect("snap1"));
bool is_protected;
ASSERT_EQ(0, image1.snap_is_protected("snap1", &is_protected));
ASSERT_TRUE(is_protected);
uint64_t features;
ASSERT_EQ(0, image1.features(&features));
std::string child_name = get_temp_image_name();
EXPECT_EQ(0, rbd.clone(ioctx, name.c_str(), "snap1", ioctx,
child_name.c_str(), features, &order));
ASSERT_EQ(0, image1.snap_exists2("snap1", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(0, image1.snap_is_protected("snap1", &is_protected));
ASSERT_TRUE(is_protected);
ASSERT_EQ(-EBUSY, image1.snap_remove("snap1"));
PrintProgress pp;
ASSERT_EQ(0, image1.snap_remove2("snap1", RBD_SNAP_REMOVE_FORCE, pp));
ASSERT_EQ(0, image1.snap_exists2("snap1", &exists));
ASSERT_FALSE(exists);
}
TEST_F(TestLibRBD, SnapRenameViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
ASSERT_EQ(0, image1.snap_create("snap1"));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.snap_rename("snap1", "snap1-rename"));
bool exists;
ASSERT_EQ(0, image1.snap_exists2("snap1-rename", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(0, image2.snap_exists2("snap1-rename", &exists));
ASSERT_TRUE(exists);
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
}
TEST_F(TestLibRBD, SnapProtectViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_EQ(0, image1.snap_create("snap1"));
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.snap_protect("snap1"));
bool is_protected;
ASSERT_EQ(0, image2.snap_is_protected("snap1", &is_protected));
ASSERT_TRUE(is_protected);
ASSERT_EQ(0, image1.snap_is_protected("snap1", &is_protected));
ASSERT_TRUE(is_protected);
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
}
TEST_F(TestLibRBD, SnapUnprotectViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_EQ(0, image1.snap_create("snap1"));
ASSERT_EQ(0, image1.snap_protect("snap1"));
bool is_protected;
ASSERT_EQ(0, image1.snap_is_protected("snap1", &is_protected));
ASSERT_TRUE(is_protected);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.snap_unprotect("snap1"));
ASSERT_EQ(0, image2.snap_is_protected("snap1", &is_protected));
ASSERT_FALSE(is_protected);
ASSERT_EQ(0, image1.snap_is_protected("snap1", &is_protected));
ASSERT_FALSE(is_protected);
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
}
TEST_F(TestLibRBD, FlattenViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string parent_name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(), size, &order));
librbd::Image parent_image;
ASSERT_EQ(0, rbd.open(ioctx, parent_image, parent_name.c_str(), NULL));
ASSERT_EQ(0, parent_image.snap_create("snap1"));
ASSERT_EQ(0, parent_image.snap_protect("snap1"));
uint64_t features;
ASSERT_EQ(0, parent_image.features(&features));
std::string name = get_temp_image_name();
EXPECT_EQ(0, rbd.clone(ioctx, parent_name.c_str(), "snap1", ioctx,
name.c_str(), features, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.flatten());
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_PASSED(validate_object_map, image1);
}
TEST_F(TestLibRBD, ResizeViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.resize(0));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_PASSED(validate_object_map, image1);
}
TEST_F(TestLibRBD, SparsifyViaLockOwner)
{
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bufferlist bl;
ASSERT_EQ(0, image1.write(0, 0, bl));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image2.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, image2.sparsify(4096));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_PASSED(validate_object_map, image1);
}
TEST_F(TestLibRBD, ObjectMapConsistentSnap)
{
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 20;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
int num_snaps = 10;
for (int i = 0; i < num_snaps; ++i) {
std::string snap_name = "snap" + stringify(i);
ASSERT_EQ(0, image1.snap_create(snap_name.c_str()));
}
thread writer([&image1](){
librbd::image_info_t info;
int r = image1.stat(info, sizeof(info));
ceph_assert(r == 0);
bufferlist bl;
bl.append("foo");
for (unsigned i = 0; i < info.num_objs; ++i) {
r = image1.write((1 << info.order) * i, bl.length(), bl);
ceph_assert(r == (int) bl.length());
}
});
writer.join();
for (int i = 0; i < num_snaps; ++i) {
std::string snap_name = "snap" + stringify(i);
ASSERT_EQ(0, image1.snap_set(snap_name.c_str()));
ASSERT_PASSED(validate_object_map, image1);
}
ASSERT_EQ(0, image1.snap_set(NULL));
ASSERT_PASSED(validate_object_map, image1);
}
void memset_rand(char *buf, size_t len) {
for (size_t i = 0; i < len; ++i) {
buf[i] = (char) (rand() % (126 - 33) + 33);
}
}
TEST_F(TestLibRBD, Metadata)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
rbd_image_t image1;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image1, NULL));
char keys[1024];
char vals[1024];
size_t keys_len = sizeof(keys);
size_t vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_metadata_list(image, "key", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(0U, keys_len);
ASSERT_EQ(0U, vals_len);
char value[1024];
size_t value_len = sizeof(value);
memset_rand(value, value_len);
ASSERT_EQ(0, rbd_metadata_set(image1, "key1", "value1"));
ASSERT_EQ(0, rbd_metadata_set(image1, "key2", "value2"));
ASSERT_EQ(0, rbd_metadata_get(image1, "key1", value, &value_len));
ASSERT_STREQ(value, "value1");
value_len = 1;
ASSERT_EQ(-ERANGE, rbd_metadata_get(image1, "key1", value, &value_len));
ASSERT_EQ(value_len, strlen("value1") + 1);
ASSERT_EQ(-ERANGE, rbd_metadata_list(image1, "key", 0, keys, &keys_len, vals,
&vals_len));
keys_len = sizeof(keys);
vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_metadata_list(image1, "key", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1 + strlen("key2") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1 + strlen("value2") + 1);
ASSERT_STREQ(keys, "key1");
ASSERT_STREQ(keys + strlen(keys) + 1, "key2");
ASSERT_STREQ(vals, "value1");
ASSERT_STREQ(vals + strlen(vals) + 1, "value2");
ASSERT_EQ(0, rbd_metadata_remove(image1, "key1"));
ASSERT_EQ(-ENOENT, rbd_metadata_remove(image1, "key3"));
value_len = sizeof(value);
ASSERT_EQ(-ENOENT, rbd_metadata_get(image1, "key3", value, &value_len));
ASSERT_EQ(0, rbd_metadata_list(image1, "key", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key2") + 1);
ASSERT_EQ(vals_len, strlen("value2") + 1);
ASSERT_STREQ(keys, "key2");
ASSERT_STREQ(vals, "value2");
// test config setting
ASSERT_EQ(0, rbd_metadata_set(image1, "conf_rbd_cache", "false"));
ASSERT_EQ(-EINVAL, rbd_metadata_set(image1, "conf_rbd_cache", "INVALID_VAL"));
ASSERT_EQ(0, rbd_metadata_remove(image1, "conf_rbd_cache"));
// test metadata with snapshot adding
ASSERT_EQ(0, rbd_snap_create(image1, "snap1"));
ASSERT_EQ(0, rbd_snap_protect(image1, "snap1"));
ASSERT_EQ(0, rbd_snap_set(image1, "snap1"));
ASSERT_EQ(-EROFS, rbd_metadata_set(image1, "key1", "value1"));
ASSERT_EQ(-EROFS, rbd_metadata_remove(image1, "key2"));
keys_len = sizeof(keys);
vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_metadata_list(image1, "key", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key2") + 1);
ASSERT_EQ(vals_len, strlen("value2") + 1);
ASSERT_STREQ(keys, "key2");
ASSERT_STREQ(vals, "value2");
ASSERT_EQ(0, rbd_snap_set(image1, NULL));
ASSERT_EQ(0, rbd_metadata_set(image1, "key1", "value1"));
ASSERT_EQ(0, rbd_metadata_set(image1, "key3", "value3"));
keys_len = sizeof(keys);
vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_metadata_list(image1, "key", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len,
strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") + 1);
ASSERT_EQ(vals_len,
strlen("value1") + 1 + strlen("value2") + 1 + strlen("value3") + 1);
ASSERT_STREQ(keys, "key1");
ASSERT_STREQ(keys + strlen("key1") + 1, "key2");
ASSERT_STREQ(keys + strlen("key1") + 1 + strlen("key2") + 1, "key3");
ASSERT_STREQ(vals, "value1");
ASSERT_STREQ(vals + strlen("value1") + 1, "value2");
ASSERT_STREQ(vals + strlen("value1") + 1 + strlen("value2") + 1, "value3");
// test metadata with cloning
uint64_t features;
ASSERT_EQ(0, rbd_get_features(image1, &features));
string cname = get_temp_image_name();
EXPECT_EQ(0, rbd_clone(ioctx, name.c_str(), "snap1", ioctx,
cname.c_str(), features, &order));
rbd_image_t image2;
ASSERT_EQ(0, rbd_open(ioctx, cname.c_str(), &image2, NULL));
ASSERT_EQ(0, rbd_metadata_set(image2, "key4", "value4"));
keys_len = sizeof(keys);
vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_metadata_list(image2, "key", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") +
1 + strlen("key4") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1 + strlen("value2") + 1 +
strlen("value3") + 1 + strlen("value4") + 1);
ASSERT_STREQ(keys + strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") +
1, "key4");
ASSERT_STREQ(vals + strlen("value1") + 1 + strlen("value2") + 1 +
strlen("value3") + 1, "value4");
ASSERT_EQ(0, rbd_metadata_list(image1, "key", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len,
strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") + 1);
ASSERT_EQ(vals_len,
strlen("value1") + 1 + strlen("value2") + 1 + strlen("value3") + 1);
ASSERT_EQ(-ENOENT, rbd_metadata_get(image1, "key4", value, &value_len));
// test short buffer cases
keys_len = strlen("key1") + 1;
vals_len = strlen("value1") + 1;
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_metadata_list(image2, "key", 1, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1);
ASSERT_STREQ(keys, "key1");
ASSERT_STREQ(vals, "value1");
ASSERT_EQ(-ERANGE, rbd_metadata_list(image2, "key", 2, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1 + strlen("key2") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1 + strlen("value2") + 1);
ASSERT_EQ(-ERANGE, rbd_metadata_list(image2, "key", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") +
1 + strlen("key4") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1 + strlen("value2") + 1 +
strlen("value3") + 1 + strlen("value4") + 1);
// test `start` param
keys_len = sizeof(keys);
vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_metadata_list(image2, "key2", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key3") + 1 + strlen("key4") + 1);
ASSERT_EQ(vals_len, strlen("value3") + 1 + strlen("value4") + 1);
ASSERT_STREQ(keys, "key3");
ASSERT_STREQ(vals, "value3");
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_close(image1));
ASSERT_EQ(0, rbd_close(image2));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, MetadataPP)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
uint64_t features;
string value;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
map<string, bufferlist> pairs;
ASSERT_EQ(0, image1.metadata_list("key", 0, &pairs));
ASSERT_TRUE(pairs.empty());
ASSERT_EQ(0, image1.metadata_set("key1", "value1"));
ASSERT_EQ(0, image1.metadata_set("key2", "value2"));
ASSERT_EQ(0, image1.metadata_get("key1", &value));
ASSERT_EQ(0, strcmp("value1", value.c_str()));
ASSERT_EQ(0, image1.metadata_list("key", 0, &pairs));
ASSERT_EQ(2U, pairs.size());
ASSERT_EQ(0, strncmp("value1", pairs["key1"].c_str(), 6));
ASSERT_EQ(0, strncmp("value2", pairs["key2"].c_str(), 6));
pairs.clear();
ASSERT_EQ(0, image1.metadata_remove("key1"));
ASSERT_EQ(-ENOENT, image1.metadata_remove("key3"));
ASSERT_TRUE(image1.metadata_get("key3", &value) < 0);
ASSERT_EQ(0, image1.metadata_list("key", 0, &pairs));
ASSERT_EQ(1U, pairs.size());
ASSERT_EQ(0, strncmp("value2", pairs["key2"].c_str(), 6));
// test config setting
ASSERT_EQ(0, image1.metadata_set("conf_rbd_cache", "false"));
ASSERT_EQ(-EINVAL, image1.metadata_set("conf_rbd_cache", "INVALID_VALUE"));
ASSERT_EQ(0, image1.metadata_remove("conf_rbd_cache"));
// test metadata with snapshot adding
ASSERT_EQ(0, image1.snap_create("snap1"));
ASSERT_EQ(0, image1.snap_protect("snap1"));
ASSERT_EQ(0, image1.snap_set("snap1"));
pairs.clear();
ASSERT_EQ(-EROFS, image1.metadata_set("key1", "value1"));
ASSERT_EQ(-EROFS, image1.metadata_remove("key2"));
ASSERT_EQ(0, image1.metadata_list("key", 0, &pairs));
ASSERT_EQ(1U, pairs.size());
ASSERT_EQ(0, strncmp("value2", pairs["key2"].c_str(), 6));
ASSERT_EQ(0, image1.snap_set(NULL));
ASSERT_EQ(0, image1.metadata_set("key1", "value1"));
ASSERT_EQ(0, image1.metadata_set("key3", "value3"));
ASSERT_EQ(0, image1.metadata_list("key", 0, &pairs));
ASSERT_EQ(3U, pairs.size());
ASSERT_EQ(0, strncmp("value1", pairs["key1"].c_str(), 6));
ASSERT_EQ(0, strncmp("value2", pairs["key2"].c_str(), 6));
ASSERT_EQ(0, strncmp("value3", pairs["key3"].c_str(), 6));
// test metadata with cloning
string cname = get_temp_image_name();
librbd::Image image2;
ASSERT_EQ(0, image1.features(&features));
EXPECT_EQ(0, rbd.clone(ioctx, name.c_str(), "snap1", ioctx,
cname.c_str(), features, &order));
ASSERT_EQ(0, rbd.open(ioctx, image2, cname.c_str(), NULL));
ASSERT_EQ(0, image2.metadata_set("key4", "value4"));
pairs.clear();
ASSERT_EQ(0, image2.metadata_list("key", 0, &pairs));
ASSERT_EQ(4U, pairs.size());
pairs.clear();
ASSERT_EQ(0, image1.metadata_list("key", 0, &pairs));
ASSERT_EQ(3U, pairs.size());
ASSERT_EQ(-ENOENT, image1.metadata_get("key4", &value));
}
TEST_F(TestLibRBD, UpdateFeatures)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 20;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
uint8_t old_format;
ASSERT_EQ(0, image.old_format(&old_format));
if (old_format) {
ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_EXCLUSIVE_LOCK, true));
return;
}
uint64_t features;
ASSERT_EQ(0, image.features(&features));
// must provide a single feature
ASSERT_EQ(-EINVAL, image.update_features(0, true));
uint64_t disable_features;
disable_features = features & (RBD_FEATURE_EXCLUSIVE_LOCK |
RBD_FEATURE_OBJECT_MAP |
RBD_FEATURE_FAST_DIFF |
RBD_FEATURE_JOURNALING);
if (disable_features != 0) {
ASSERT_EQ(0, image.update_features(disable_features, false));
}
ASSERT_EQ(0, image.features(&features));
ASSERT_EQ(0U, features & disable_features);
// cannot enable object map nor journaling w/o exclusive lock
ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_OBJECT_MAP, true));
ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_JOURNALING, true));
ASSERT_EQ(0, image.update_features(RBD_FEATURE_EXCLUSIVE_LOCK, true));
ASSERT_EQ(0, image.features(&features));
ASSERT_NE(0U, features & RBD_FEATURE_EXCLUSIVE_LOCK);
// can enable fast diff w/o object map
ASSERT_EQ(0, image.update_features(RBD_FEATURE_FAST_DIFF, true));
ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_OBJECT_MAP, true));
ASSERT_EQ(0, image.features(&features));
ASSERT_NE(0U, features & RBD_FEATURE_OBJECT_MAP);
uint64_t expected_flags = RBD_FLAG_OBJECT_MAP_INVALID |
RBD_FLAG_FAST_DIFF_INVALID;
uint64_t flags;
ASSERT_EQ(0, image.get_flags(&flags));
ASSERT_EQ(expected_flags, flags);
ASSERT_EQ(0, image.update_features(RBD_FEATURE_OBJECT_MAP, false));
ASSERT_EQ(0, image.features(&features));
ASSERT_EQ(0U, features & RBD_FEATURE_OBJECT_MAP);
// can disable object map w/ fast diff
ASSERT_EQ(0, image.update_features(RBD_FEATURE_OBJECT_MAP, true));
ASSERT_EQ(0, image.update_features(RBD_FEATURE_OBJECT_MAP, false));
ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_FAST_DIFF, false));
ASSERT_EQ(0, image.features(&features));
ASSERT_EQ(0U, features & RBD_FEATURE_FAST_DIFF);
ASSERT_EQ(0, image.get_flags(&flags));
ASSERT_EQ(0U, flags);
// cannot disable exclusive lock w/ object map
ASSERT_EQ(0, image.update_features(RBD_FEATURE_OBJECT_MAP, true));
ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_EXCLUSIVE_LOCK, false));
ASSERT_EQ(0, image.update_features(RBD_FEATURE_OBJECT_MAP, false));
// cannot disable exclusive lock w/ journaling
ASSERT_EQ(0, image.update_features(RBD_FEATURE_JOURNALING, true));
ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_EXCLUSIVE_LOCK, false));
ASSERT_EQ(0, image.update_features(RBD_FEATURE_JOURNALING, false));
ASSERT_EQ(0, image.get_flags(&flags));
ASSERT_EQ(0U, flags);
ASSERT_EQ(0, image.update_features(RBD_FEATURE_EXCLUSIVE_LOCK, false));
ASSERT_EQ(0, image.features(&features));
if ((features & RBD_FEATURE_DEEP_FLATTEN) != 0) {
ASSERT_EQ(0, image.update_features(RBD_FEATURE_DEEP_FLATTEN, false));
}
ASSERT_EQ(-EINVAL, image.update_features(RBD_FEATURE_DEEP_FLATTEN, true));
}
TEST_F(TestLibRBD, FeaturesBitmaskString)
{
librbd::RBD rbd;
uint64_t features = RBD_FEATURES_DEFAULT;
std::string features_str;
std::string expected_str = "deep-flatten,exclusive-lock,fast-diff,layering,object-map";
rbd.features_to_string(features, &features_str);
ASSERT_EQ(expected_str, features_str);
features = RBD_FEATURE_LAYERING;
features_str = "";
expected_str = "layering";
rbd.features_to_string(features, &features_str);
ASSERT_EQ(expected_str, features_str);
uint64_t features_bitmask;
features_str = "deep-flatten,exclusive-lock,fast-diff,layering,object-map";
rbd.features_from_string(features_str, &features_bitmask);
ASSERT_EQ(features_bitmask, RBD_FEATURES_DEFAULT);
features_str = "layering";
features_bitmask = 0;
rbd.features_from_string(features_str, &features_bitmask);
ASSERT_EQ(features_bitmask, RBD_FEATURE_LAYERING);
}
TEST_F(TestLibRBD, RebuildObjectMap)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 20;
int order = 18;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
PrintProgress prog_ctx;
std::string object_map_oid;
bufferlist bl;
bl.append("foo");
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
uint64_t features;
ASSERT_EQ(0, image.features(&features));
if ((features & RBD_FEATURE_OBJECT_MAP) == 0) {
ASSERT_EQ(-EINVAL, image.rebuild_object_map(prog_ctx));
return;
}
ASSERT_EQ((ssize_t)bl.length(), image.write(0, bl.length(), bl));
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_EQ((ssize_t)bl.length(), image.write(1<<order, bl.length(), bl));
std::string image_id;
ASSERT_EQ(0, get_image_id(image, &image_id));
object_map_oid = RBD_OBJECT_MAP_PREFIX + image_id;
}
// corrupt the object map
ASSERT_EQ(0, ioctx.write(object_map_oid, bl, bl.length(), 0));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bool lock_owner;
bl.clear();
ASSERT_EQ(0, image1.write(0, 0, bl));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
uint64_t flags;
ASSERT_EQ(0, image1.get_flags(&flags));
ASSERT_TRUE((flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0);
ASSERT_EQ(0, image1.rebuild_object_map(prog_ctx));
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
bufferlist read_bl;
ASSERT_EQ((ssize_t)bl.length(), image2.read(0, bl.length(), read_bl));
ASSERT_TRUE(bl.contents_equal(read_bl));
read_bl.clear();
ASSERT_EQ((ssize_t)bl.length(), image2.read(1<<order, bl.length(), read_bl));
ASSERT_TRUE(bl.contents_equal(read_bl));
ASSERT_PASSED(validate_object_map, image1);
ASSERT_PASSED(validate_object_map, image2);
}
TEST_F(TestLibRBD, RebuildNewObjectMap)
{
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
std::string name = get_temp_image_name();
uint64_t size = 1 << 20;
int order = 18;
uint64_t features = RBD_FEATURE_EXCLUSIVE_LOCK;
ASSERT_EQ(0, create_image_full(ioctx, name.c_str(), size, &order,
false, features));
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_update_features(image, RBD_FEATURE_OBJECT_MAP, true));
ASSERT_EQ(0, rbd_rebuild_object_map(image, print_progress_percent, NULL));
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, CheckObjectMap)
{
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 20;
int order = 18;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
PrintProgress prog_ctx;
bufferlist bl1;
bufferlist bl2;
bl1.append("foo");
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
uint64_t features;
ASSERT_EQ(0, image.features(&features));
ASSERT_EQ((ssize_t)bl1.length(), image.write(0, bl1.length(), bl1));
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_EQ((ssize_t)bl1.length(), image.write(1<<order, bl1.length(), bl1));
}
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
std::string image_id;
ASSERT_EQ(0, get_image_id(image1, &image_id));
std::string object_map_oid = RBD_OBJECT_MAP_PREFIX + image_id;
ASSERT_LT(0, ioctx.read(object_map_oid, bl2, 1024, 0));
bool lock_owner;
ASSERT_EQ((ssize_t)bl1.length(), image1.write(3 * (1 << 18), bl1.length(), bl1));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
//reopen image to reread now corrupt object map from disk
image1.close();
bl1.clear();
ASSERT_LT(0, ioctx.read(object_map_oid, bl1, 1024, 0));
ASSERT_FALSE(bl1.contents_equal(bl2));
ASSERT_EQ(0, ioctx.write_full(object_map_oid, bl2));
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
uint64_t flags;
ASSERT_EQ(0, image1.get_flags(&flags));
ASSERT_TRUE((flags & RBD_FLAG_OBJECT_MAP_INVALID) == 0);
ASSERT_EQ(0, image1.check_object_map(prog_ctx));
ASSERT_EQ(0, image1.get_flags(&flags));
ASSERT_TRUE((flags & RBD_FLAG_OBJECT_MAP_INVALID) != 0);
}
TEST_F(TestLibRBD, BlockingAIO)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 20;
int order = 18;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
std::string non_blocking_aio;
ASSERT_EQ(0, _rados.conf_get("rbd_non_blocking_aio", non_blocking_aio));
ASSERT_EQ(0, _rados.conf_set("rbd_non_blocking_aio", "0"));
BOOST_SCOPE_EXIT( (non_blocking_aio) ) {
ASSERT_EQ(0, _rados.conf_set("rbd_non_blocking_aio",
non_blocking_aio.c_str()));
} BOOST_SCOPE_EXIT_END;
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bool skip_discard = this->is_skip_partial_discard_enabled(image);
bufferlist bl;
ASSERT_EQ(0, image.write(0, bl.length(), bl));
bl.append(std::string(256, '1'));
librbd::RBD::AioCompletion *write_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, image.aio_write(0, bl.length(), bl, write_comp));
librbd::RBD::AioCompletion *flush_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, image.aio_flush(flush_comp));
ASSERT_EQ(0, flush_comp->wait_for_complete());
ASSERT_EQ(0, flush_comp->get_return_value());
flush_comp->release();
ASSERT_EQ(1, write_comp->is_complete());
ASSERT_EQ(0, write_comp->get_return_value());
write_comp->release();
librbd::RBD::AioCompletion *discard_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, image.aio_discard(128, 128, discard_comp));
ASSERT_EQ(0, discard_comp->wait_for_complete());
discard_comp->release();
librbd::RBD::AioCompletion *read_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
bufferlist read_bl;
image.aio_read(0, bl.length(), read_bl, read_comp);
ASSERT_EQ(0, read_comp->wait_for_complete());
ASSERT_EQ((ssize_t)bl.length(), read_comp->get_return_value());
read_comp->release();
bufferlist expected_bl;
expected_bl.append(std::string(128, '1'));
expected_bl.append(std::string(128, skip_discard ? '1' : '\0'));
ASSERT_TRUE(expected_bl.contents_equal(read_bl));
}
TEST_F(TestLibRBD, ExclusiveLockTransition)
{
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
std::list<librbd::RBD::AioCompletion *> comps;
ceph::bufferlist bl;
bl.append(std::string(1 << order, '1'));
for (size_t object_no = 0; object_no < (size >> 12); ++object_no) {
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL,
NULL);
comps.push_back(comp);
if (object_no % 2 == 0) {
ASSERT_EQ(0, image1.aio_write(object_no << order, bl.length(), bl, comp));
} else {
ASSERT_EQ(0, image2.aio_write(object_no << order, bl.length(), bl, comp));
}
}
while (!comps.empty()) {
librbd::RBD::AioCompletion *comp = comps.front();
comps.pop_front();
ASSERT_EQ(0, comp->wait_for_complete());
ASSERT_EQ(1, comp->is_complete());
comp->release();
}
librbd::Image image3;
ASSERT_EQ(0, rbd.open(ioctx, image3, name.c_str(), NULL));
for (size_t object_no = 0; object_no < (size >> 12); ++object_no) {
bufferlist read_bl;
ASSERT_EQ((ssize_t)bl.length(), image3.read(object_no << order, bl.length(),
read_bl));
ASSERT_TRUE(bl.contents_equal(read_bl));
}
ASSERT_PASSED(validate_object_map, image1);
ASSERT_PASSED(validate_object_map, image2);
ASSERT_PASSED(validate_object_map, image3);
}
TEST_F(TestLibRBD, ExclusiveLockReadTransition)
{
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
bool lock_owner;
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_FALSE(lock_owner);
// journaling should force read ops to acquire the lock
bufferlist read_bl;
ASSERT_EQ(0, image1.read(0, 0, read_bl));
ASSERT_EQ(0, image1.is_exclusive_lock_owner(&lock_owner));
ASSERT_TRUE(lock_owner);
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
std::list<librbd::RBD::AioCompletion *> comps;
std::list<bufferlist> read_bls;
for (size_t object_no = 0; object_no < (size >> 12); ++object_no) {
librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL,
NULL);
comps.push_back(comp);
read_bls.emplace_back();
if (object_no % 2 == 0) {
ASSERT_EQ(0, image1.aio_read(object_no << order, 1 << order, read_bls.back(), comp));
} else {
ASSERT_EQ(0, image2.aio_read(object_no << order, 1 << order, read_bls.back(), comp));
}
}
while (!comps.empty()) {
librbd::RBD::AioCompletion *comp = comps.front();
comps.pop_front();
ASSERT_EQ(0, comp->wait_for_complete());
ASSERT_EQ(1, comp->is_complete());
comp->release();
}
}
TEST_F(TestLibRBD, CacheMayCopyOnWrite) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
std::string clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(ioctx, name.c_str(), "one", ioctx, clone_name.c_str(),
RBD_FEATURE_LAYERING, &order));
librbd::Image clone;
ASSERT_EQ(0, rbd.open(ioctx, clone, clone_name.c_str(), NULL));
ASSERT_EQ(0, clone.flush());
bufferlist expect_bl;
expect_bl.append(std::string(1024, '\0'));
// test double read path
bufferlist read_bl;
uint64_t offset = 0;
ASSERT_EQ(1024, clone.read(offset + 2048, 1024, read_bl));
ASSERT_TRUE(expect_bl.contents_equal(read_bl));
bufferlist write_bl;
write_bl.append(std::string(1024, '1'));
ASSERT_EQ(1024, clone.write(offset, write_bl.length(), write_bl));
read_bl.clear();
ASSERT_EQ(1024, clone.read(offset + 2048, 1024, read_bl));
ASSERT_TRUE(expect_bl.contents_equal(read_bl));
// test read retry path
offset = 1 << order;
ASSERT_EQ(1024, clone.write(offset, write_bl.length(), write_bl));
read_bl.clear();
ASSERT_EQ(1024, clone.read(offset + 2048, 1024, read_bl));
ASSERT_TRUE(expect_bl.contents_equal(read_bl));
}
TEST_F(TestLibRBD, FlushEmptyOpsOnExternalSnapshot) {
std::string cache_enabled;
ASSERT_EQ(0, _rados.conf_get("rbd_cache", cache_enabled));
ASSERT_EQ(0, _rados.conf_set("rbd_cache", "false"));
BOOST_SCOPE_EXIT( (cache_enabled) ) {
ASSERT_EQ(0, _rados.conf_set("rbd_cache", cache_enabled.c_str()));
} BOOST_SCOPE_EXIT_END;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image1;
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, image1.snap_create("snap1"));
librbd::RBD::AioCompletion *read_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
bufferlist read_bl;
image2.aio_read(0, 1024, read_bl, read_comp);
ASSERT_EQ(0, read_comp->wait_for_complete());
read_comp->release();
}
TEST_F(TestLibRBD, TestImageOptions)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
//make create image options
uint64_t features = RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2 ;
uint64_t order = 0;
uint64_t stripe_unit = IMAGE_STRIPE_UNIT;
uint64_t stripe_count = IMAGE_STRIPE_COUNT;
rbd_image_options_t opts;
rbd_image_options_create(&opts);
bool is_set;
ASSERT_EQ(-EINVAL, rbd_image_options_is_set(opts, 12345, &is_set));
ASSERT_EQ(0, rbd_image_options_is_set(opts, RBD_IMAGE_OPTION_FORMAT,
&is_set));
ASSERT_FALSE(is_set);
ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FORMAT,
2));
ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FEATURES,
features));
ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER,
order));
ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT,
stripe_unit));
ASSERT_EQ(0, rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_COUNT,
stripe_count));
ASSERT_EQ(0, rbd_image_options_is_set(opts, RBD_IMAGE_OPTION_FORMAT,
&is_set));
ASSERT_TRUE(is_set);
std::string parent_name = get_temp_image_name();
// make parent
ASSERT_EQ(0, rbd_create4(ioctx, parent_name.c_str(), 4<<20, opts));
// check order is returned in opts
ASSERT_EQ(0, rbd_image_options_get_uint64(opts, RBD_IMAGE_OPTION_ORDER,
&order));
ASSERT_NE((uint64_t)0, order);
// write some data to parent
rbd_image_t parent;
ASSERT_EQ(0, rbd_open(ioctx, parent_name.c_str(), &parent, NULL));
char *data = (char *)"testdata";
ASSERT_EQ((ssize_t)strlen(data), rbd_write(parent, 0, strlen(data), data));
ASSERT_EQ((ssize_t)strlen(data), rbd_write(parent, 12, strlen(data), data));
// create a snapshot, reopen as the parent we're interested in
ASSERT_EQ(0, rbd_snap_create(parent, "parent_snap"));
ASSERT_EQ(0, rbd_close(parent));
ASSERT_EQ(0, rbd_open(ioctx, parent_name.c_str(), &parent, "parent_snap"));
// clone
std::string child_name = get_temp_image_name();
ASSERT_EQ(0, rbd_snap_protect(parent, "parent_snap"));
ASSERT_EQ(0, rbd_clone3(ioctx, parent_name.c_str(), "parent_snap", ioctx,
child_name.c_str(), opts));
// copy
std::string copy1_name = get_temp_image_name();
ASSERT_EQ(0, rbd_copy3(parent, ioctx, copy1_name.c_str(), opts));
std::string copy2_name = get_temp_image_name();
ASSERT_EQ(0, rbd_copy_with_progress3(parent, ioctx, copy2_name.c_str(), opts,
print_progress_percent, NULL));
ASSERT_EQ(0, rbd_close(parent));
rbd_image_options_destroy(opts);
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestImageOptionsPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
//make create image options
uint64_t features = RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2 ;
uint64_t order = 0;
uint64_t stripe_unit = IMAGE_STRIPE_UNIT;
uint64_t stripe_count = IMAGE_STRIPE_COUNT;
librbd::ImageOptions opts;
ASSERT_EQ(0, opts.set(RBD_IMAGE_OPTION_FORMAT, static_cast<uint64_t>(2)));
ASSERT_EQ(0, opts.set(RBD_IMAGE_OPTION_FEATURES, features));
ASSERT_EQ(0, opts.set(RBD_IMAGE_OPTION_ORDER, order));
ASSERT_EQ(0, opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, stripe_unit));
ASSERT_EQ(0, opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count));
librbd::RBD rbd;
std::string parent_name = get_temp_image_name();
// make parent
ASSERT_EQ(0, rbd.create4(ioctx, parent_name.c_str(), 4<<20, opts));
// check order is returned in opts
ASSERT_EQ(0, opts.get(RBD_IMAGE_OPTION_ORDER, &order));
ASSERT_NE((uint64_t)0, order);
// write some data to parent
librbd::Image parent;
ASSERT_EQ(0, rbd.open(ioctx, parent, parent_name.c_str(), NULL));
ssize_t len = 1024;
bufferlist bl;
bl.append(buffer::create(len));
bl.zero();
ASSERT_EQ(len, parent.write(0, len, bl));
ASSERT_EQ(len, parent.write(len, len, bl));
// create a snapshot, reopen as the parent we're interested in
ASSERT_EQ(0, parent.snap_create("parent_snap"));
ASSERT_EQ(0, parent.close());
ASSERT_EQ(0, rbd.open(ioctx, parent, parent_name.c_str(), "parent_snap"));
// clone
std::string child_name = get_temp_image_name();
ASSERT_EQ(0, parent.snap_protect("parent_snap"));
ASSERT_EQ(0, rbd.clone3(ioctx, parent_name.c_str(), "parent_snap", ioctx,
child_name.c_str(), opts));
// copy
std::string copy1_name = get_temp_image_name();
ASSERT_EQ(0, parent.copy3(ioctx, copy1_name.c_str(), opts));
std::string copy2_name = get_temp_image_name();
PrintProgress pp;
ASSERT_EQ(0, parent.copy_with_progress3(ioctx, copy2_name.c_str(), opts, pp));
ASSERT_EQ(0, parent.close());
}
TEST_F(TestLibRBD, EventSocketPipe)
{
EventSocket event_sock;
int pipe_fd[2]; // read and write fd
char buf[32];
ASSERT_EQ(0, pipe(pipe_fd));
ASSERT_FALSE(event_sock.is_valid());
ASSERT_EQ(-EINVAL, event_sock.init(pipe_fd[1], EVENT_SOCKET_TYPE_NONE));
ASSERT_FALSE(event_sock.is_valid());
ASSERT_EQ(-EINVAL, event_sock.init(pipe_fd[1], 44));
ASSERT_FALSE(event_sock.is_valid());
#ifndef HAVE_EVENTFD
ASSERT_EQ(-EINVAL, event_sock.init(pipe_fd[1], EVENT_SOCKET_TYPE_EVENTFD));
ASSERT_FALSE(event_sock.is_valid());
#endif
ASSERT_EQ(0, event_sock.init(pipe_fd[1], EVENT_SOCKET_TYPE_PIPE));
ASSERT_TRUE(event_sock.is_valid());
ASSERT_EQ(0, event_sock.notify());
ASSERT_EQ(1, read(pipe_fd[0], buf, 32));
ASSERT_EQ('i', buf[0]);
close(pipe_fd[0]);
close(pipe_fd[1]);
}
TEST_F(TestLibRBD, EventSocketEventfd)
{
#ifdef HAVE_EVENTFD
EventSocket event_sock;
int event_fd;
struct pollfd poll_fd;
char buf[32];
event_fd = eventfd(0, EFD_NONBLOCK);
ASSERT_NE(-1, event_fd);
ASSERT_FALSE(event_sock.is_valid());
ASSERT_EQ(-EINVAL, event_sock.init(event_fd, EVENT_SOCKET_TYPE_NONE));
ASSERT_FALSE(event_sock.is_valid());
ASSERT_EQ(-EINVAL, event_sock.init(event_fd, 44));
ASSERT_FALSE(event_sock.is_valid());
ASSERT_EQ(0, event_sock.init(event_fd, EVENT_SOCKET_TYPE_EVENTFD));
ASSERT_TRUE(event_sock.is_valid());
ASSERT_EQ(0, event_sock.notify());
poll_fd.fd = event_fd;
poll_fd.events = POLLIN;
ASSERT_EQ(1, poll(&poll_fd, 1, -1));
ASSERT_TRUE(poll_fd.revents & POLLIN);
ASSERT_EQ(static_cast<ssize_t>(sizeof(uint64_t)), read(event_fd, buf, 32));
ASSERT_EQ(1U, *reinterpret_cast<uint64_t *>(buf));
close(event_fd);
#endif
}
TEST_F(TestLibRBD, ImagePollIO)
{
#ifdef HAVE_EVENTFD
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int fd = eventfd(0, EFD_NONBLOCK);
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_set_image_notification(image, fd, EVENT_SOCKET_TYPE_EVENTFD));
char test_data[TEST_IO_SIZE + 1];
char zero_data[TEST_IO_SIZE + 1];
int i;
for (i = 0; i < TEST_IO_SIZE; ++i)
test_data[i] = (char) (rand() % (126 - 33) + 33);
test_data[TEST_IO_SIZE] = '\0';
memset(zero_data, 0, sizeof(zero_data));
for (i = 0; i < 5; ++i)
ASSERT_PASSED(write_test_data, image, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_write_test_data_and_poll, image, fd, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
for (i = 5; i < 10; ++i)
ASSERT_PASSED(aio_read_test_data_and_poll, image, fd, test_data, TEST_IO_SIZE * i, TEST_IO_SIZE, 0);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
#endif
}
namespace librbd {
static bool operator==(const image_spec_t &lhs, const image_spec_t &rhs) {
return (lhs.id == rhs.id && lhs.name == rhs.name);
}
static bool operator==(const linked_image_spec_t &lhs,
const linked_image_spec_t &rhs) {
return (lhs.pool_id == rhs.pool_id &&
lhs.pool_name == rhs.pool_name &&
lhs.pool_namespace == rhs.pool_namespace &&
lhs.image_id == rhs.image_id &&
lhs.image_name == rhs.image_name &&
lhs.trash == rhs.trash);
}
static bool operator==(const mirror_peer_t &lhs, const mirror_peer_t &rhs) {
return (lhs.uuid == rhs.uuid &&
lhs.cluster_name == rhs.cluster_name &&
lhs.client_name == rhs.client_name);
}
static std::ostream& operator<<(std::ostream &os, const mirror_peer_t &peer) {
os << "uuid=" << peer.uuid << ", "
<< "cluster=" << peer.cluster_name << ", "
<< "client=" << peer.client_name;
return os;
}
} // namespace librbd
TEST_F(TestLibRBD, Mirror) {
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::vector<librbd::mirror_peer_t> expected_peers;
std::vector<librbd::mirror_peer_t> peers;
ASSERT_EQ(0, rbd.mirror_peer_list(ioctx, &peers));
ASSERT_EQ(expected_peers, peers);
std::string uuid1;
ASSERT_EQ(-EINVAL, rbd.mirror_peer_add(ioctx, &uuid1, "cluster1", "client"));
rbd_mirror_mode_t mirror_mode;
ASSERT_EQ(0, rbd.mirror_mode_get(ioctx, &mirror_mode));
ASSERT_EQ(RBD_MIRROR_MODE_DISABLED, mirror_mode);
ASSERT_EQ(0, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(0, rbd.mirror_mode_get(ioctx, &mirror_mode));
// Add some images to the pool
int order = 0;
std::string parent_name = get_temp_image_name();
std::string child_name = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(rbd, ioctx, parent_name.c_str(), 2 << 20,
&order));
bool old_format;
uint64_t features;
ASSERT_EQ(0, get_features(&old_format, &features));
if ((features & RBD_FEATURE_LAYERING) != 0) {
librbd::Image parent;
ASSERT_EQ(0, rbd.open(ioctx, parent, parent_name.c_str(), NULL));
ASSERT_EQ(0, parent.snap_create("parent_snap"));
ASSERT_EQ(0, parent.close());
ASSERT_EQ(0, rbd.open(ioctx, parent, parent_name.c_str(), "parent_snap"));
ASSERT_EQ(0, parent.snap_protect("parent_snap"));
ASSERT_EQ(0, parent.close());
ASSERT_EQ(0, rbd.clone(ioctx, parent_name.c_str(), "parent_snap", ioctx,
child_name.c_str(), features, &order));
}
ASSERT_EQ(RBD_MIRROR_MODE_IMAGE, mirror_mode);
ASSERT_EQ(0, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_POOL));
ASSERT_EQ(0, rbd.mirror_mode_get(ioctx, &mirror_mode));
ASSERT_EQ(RBD_MIRROR_MODE_POOL, mirror_mode);
std::string uuid2;
std::string uuid3;
ASSERT_EQ(0, rbd.mirror_peer_add(ioctx, &uuid1, "cluster1", "client"));
ASSERT_EQ(0, rbd.mirror_peer_add(ioctx, &uuid2, "cluster2", "admin"));
ASSERT_EQ(-EEXIST, rbd.mirror_peer_add(ioctx, &uuid3, "cluster1", "foo"));
ASSERT_EQ(0, rbd.mirror_peer_add(ioctx, &uuid3, "cluster3", "admin"));
ASSERT_EQ(0, rbd.mirror_peer_list(ioctx, &peers));
auto sort_peers = [](const librbd::mirror_peer_t &lhs,
const librbd::mirror_peer_t &rhs) {
return lhs.uuid < rhs.uuid;
};
expected_peers = {
{uuid1, "cluster1", "client"},
{uuid2, "cluster2", "admin"},
{uuid3, "cluster3", "admin"}};
std::sort(expected_peers.begin(), expected_peers.end(), sort_peers);
ASSERT_EQ(expected_peers, peers);
ASSERT_EQ(0, rbd.mirror_peer_remove(ioctx, "uuid4"));
ASSERT_EQ(0, rbd.mirror_peer_remove(ioctx, uuid2));
ASSERT_EQ(-ENOENT, rbd.mirror_peer_set_client(ioctx, "uuid4", "new client"));
ASSERT_EQ(0, rbd.mirror_peer_set_client(ioctx, uuid1, "new client"));
ASSERT_EQ(-ENOENT, rbd.mirror_peer_set_cluster(ioctx, "uuid4",
"new cluster"));
ASSERT_EQ(0, rbd.mirror_peer_set_cluster(ioctx, uuid3, "new cluster"));
ASSERT_EQ(0, rbd.mirror_peer_list(ioctx, &peers));
expected_peers = {
{uuid1, "cluster1", "new client"},
{uuid3, "new cluster", "admin"}};
std::sort(expected_peers.begin(), expected_peers.end(), sort_peers);
ASSERT_EQ(expected_peers, peers);
ASSERT_EQ(-EBUSY, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_DISABLED));
ASSERT_EQ(0, rbd.mirror_peer_remove(ioctx, uuid1));
ASSERT_EQ(0, rbd.mirror_peer_remove(ioctx, uuid3));
ASSERT_EQ(0, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestLibRBD, MirrorPeerAttributes) {
REQUIRE(!is_librados_test_stub(_rados));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
ASSERT_EQ(0, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_IMAGE));
std::string uuid;
ASSERT_EQ(0, rbd.mirror_peer_add(ioctx, &uuid, "remote_cluster", "client"));
std::map<std::string, std::string> attributes;
ASSERT_EQ(-ENOENT, rbd.mirror_peer_get_attributes(ioctx, uuid, &attributes));
ASSERT_EQ(-ENOENT, rbd.mirror_peer_set_attributes(ioctx, "missing uuid",
attributes));
std::map<std::string, std::string> expected_attributes{
{"mon_host", "1.2.3.4"},
{"key", "ABC"}};
ASSERT_EQ(0, rbd.mirror_peer_set_attributes(ioctx, uuid,
expected_attributes));
ASSERT_EQ(0, rbd.mirror_peer_get_attributes(ioctx, uuid,
&attributes));
ASSERT_EQ(expected_attributes, attributes);
ASSERT_EQ(0, rbd.mirror_peer_remove(ioctx, uuid));
ASSERT_EQ(0, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestLibRBD, CreateWithMirrorEnabled) {
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
ASSERT_EQ(0, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_IMAGE));
librbd::ImageOptions image_options;
ASSERT_EQ(0, image_options.set(
RBD_IMAGE_OPTION_MIRROR_IMAGE_MODE,
static_cast<uint64_t>(RBD_MIRROR_IMAGE_MODE_SNAPSHOT)));
std::string parent_name = get_temp_image_name();
ASSERT_EQ(0, rbd.create4(ioctx, parent_name.c_str(), 2<<20, image_options));
librbd::Image parent_image;
ASSERT_EQ(0, rbd.open(ioctx, parent_image, parent_name.c_str(), NULL));
librbd::mirror_image_mode_t mirror_image_mode;
ASSERT_EQ(0, parent_image.mirror_image_get_mode(&mirror_image_mode));
ASSERT_EQ(RBD_MIRROR_IMAGE_MODE_SNAPSHOT, mirror_image_mode);
ASSERT_EQ(0, parent_image.snap_create("parent_snap"));
ASSERT_EQ(0, parent_image.snap_protect("parent_snap"));
std::string child_name = get_temp_image_name();
ASSERT_EQ(0, rbd.clone3(ioctx, parent_name.c_str(), "parent_snap", ioctx,
child_name.c_str(), image_options));
librbd::Image child_image;
ASSERT_EQ(0, rbd.open(ioctx, child_image, child_name.c_str(), NULL));
ASSERT_EQ(0, child_image.mirror_image_get_mode(&mirror_image_mode));
ASSERT_EQ(RBD_MIRROR_IMAGE_MODE_SNAPSHOT, mirror_image_mode);
ASSERT_EQ(0, child_image.mirror_image_disable(true));
ASSERT_EQ(0, parent_image.mirror_image_disable(true));
ASSERT_EQ(0, rbd.mirror_mode_set(ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestLibRBD, FlushCacheWithCopyupOnExternalSnapshot) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image image;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
bufferlist bl;
bl.append(std::string(size, '1'));
ASSERT_EQ((int)size, image.write(0, size, bl));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
std::string clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(ioctx, name.c_str(), "one", ioctx, clone_name.c_str(),
RBD_FEATURE_LAYERING, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, clone_name.c_str(), NULL));
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, clone_name.c_str(), NULL));
// prepare CoW writeback that will be flushed on next op
bl.clear();
bl.append(std::string(1, '1'));
ASSERT_EQ(0, image.flush());
ASSERT_EQ(1, image.write(0, 1, bl));
ASSERT_EQ(0, image2.snap_create("snap1"));
librbd::RBD::AioCompletion *read_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
bufferlist read_bl;
image.aio_read(0, 1024, read_bl, read_comp);
ASSERT_EQ(0, read_comp->wait_for_complete());
read_comp->release();
}
TEST_F(TestLibRBD, ExclusiveLock)
{
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
static char buf[10];
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
rbd_image_t image1;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image1, NULL));
int lock_owner;
ASSERT_EQ(0, rbd_lock_acquire(image1, RBD_LOCK_MODE_EXCLUSIVE));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1, &lock_owner));
ASSERT_TRUE(lock_owner);
rbd_lock_mode_t lock_mode;
char *lock_owners[1];
size_t max_lock_owners = 0;
ASSERT_EQ(-ERANGE, rbd_lock_get_owners(image1, &lock_mode, lock_owners,
&max_lock_owners));
ASSERT_EQ(1U, max_lock_owners);
ASSERT_EQ(0, rbd_lock_get_owners(image1, &lock_mode, lock_owners,
&max_lock_owners));
ASSERT_EQ(RBD_LOCK_MODE_EXCLUSIVE, lock_mode);
ASSERT_STRNE("", lock_owners[0]);
ASSERT_EQ(1U, max_lock_owners);
rbd_image_t image2;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image2, NULL));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2, &lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(-EOPNOTSUPP, rbd_lock_break(image1, RBD_LOCK_MODE_SHARED, ""));
ASSERT_EQ(-EBUSY, rbd_lock_break(image1, RBD_LOCK_MODE_EXCLUSIVE,
"not the owner"));
ASSERT_EQ(0, rbd_lock_release(image1));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1, &lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(-ENOENT, rbd_lock_break(image1, RBD_LOCK_MODE_EXCLUSIVE,
lock_owners[0]));
rbd_lock_get_owners_cleanup(lock_owners, max_lock_owners);
ASSERT_EQ(-EROFS, rbd_write(image1, 0, sizeof(buf), buf));
ASSERT_EQ((ssize_t)sizeof(buf), rbd_write(image2, 0, sizeof(buf), buf));
ASSERT_EQ(0, rbd_lock_acquire(image2, RBD_LOCK_MODE_EXCLUSIVE));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2, &lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_EQ(0, rbd_lock_release(image2));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2, &lock_owner));
ASSERT_FALSE(lock_owner);
ASSERT_EQ(0, rbd_lock_acquire(image1, RBD_LOCK_MODE_EXCLUSIVE));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1, &lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_EQ((ssize_t)sizeof(buf), rbd_write(image1, 0, sizeof(buf), buf));
ASSERT_EQ(-EROFS, rbd_write(image2, 0, sizeof(buf), buf));
ASSERT_EQ(0, rbd_lock_release(image1));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1, &lock_owner));
ASSERT_FALSE(lock_owner);
int owner_id = -1;
std::mutex lock;
const auto pingpong = [&](int m_id, rbd_image_t &m_image) {
for (int i = 0; i < 10; i++) {
{
std::lock_guard<std::mutex> locker(lock);
if (owner_id == m_id) {
std::cout << m_id << ": releasing exclusive lock" << std::endl;
EXPECT_EQ(0, rbd_lock_release(m_image));
int lock_owner;
EXPECT_EQ(0, rbd_is_exclusive_lock_owner(m_image, &lock_owner));
EXPECT_FALSE(lock_owner);
owner_id = -1;
std::cout << m_id << ": exclusive lock released" << std::endl;
continue;
}
}
std::cout << m_id << ": acquiring exclusive lock" << std::endl;
int r;
do {
r = rbd_lock_acquire(m_image, RBD_LOCK_MODE_EXCLUSIVE);
if (r == -EROFS) {
usleep(1000);
}
} while (r == -EROFS);
EXPECT_EQ(0, r);
int lock_owner;
EXPECT_EQ(0, rbd_is_exclusive_lock_owner(m_image, &lock_owner));
EXPECT_TRUE(lock_owner);
std::cout << m_id << ": exclusive lock acquired" << std::endl;
{
std::lock_guard<std::mutex> locker(lock);
owner_id = m_id;
}
usleep(rand() % 50000);
}
std::lock_guard<std::mutex> locker(lock);
if (owner_id == m_id) {
EXPECT_EQ(0, rbd_lock_release(m_image));
int lock_owner;
EXPECT_EQ(0, rbd_is_exclusive_lock_owner(m_image, &lock_owner));
EXPECT_FALSE(lock_owner);
owner_id = -1;
}
};
thread ping(bind(pingpong, 1, ref(image1)));
thread pong(bind(pingpong, 2, ref(image2)));
ping.join();
pong.join();
ASSERT_EQ(0, rbd_lock_acquire(image2, RBD_LOCK_MODE_EXCLUSIVE));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image2, &lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_EQ(0, rbd_close(image2));
ASSERT_EQ(0, rbd_lock_acquire(image1, RBD_LOCK_MODE_EXCLUSIVE));
ASSERT_EQ(0, rbd_is_exclusive_lock_owner(image1, &lock_owner));
ASSERT_TRUE(lock_owner);
ASSERT_EQ(0, rbd_close(image1));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, BreakLock)
{
SKIP_IF_CRIMSON();
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
REQUIRE(!is_rbd_pwl_enabled((CephContext *)_rados.cct()));
static char buf[10];
rados_t blocklist_cluster;
ASSERT_EQ("", connect_cluster(&blocklist_cluster));
rados_ioctx_t ioctx, blocklist_ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
ASSERT_EQ(0, rados_ioctx_create(blocklist_cluster, m_pool_name.c_str(),
&blocklist_ioctx));
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
int order = 0;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
rbd_image_t image, blocklist_image;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_open(blocklist_ioctx, name.c_str(), &blocklist_image, NULL));
ASSERT_EQ(0, rbd_metadata_set(image, "conf_rbd_blocklist_on_break_lock", "true"));
ASSERT_EQ(0, rbd_lock_acquire(blocklist_image, RBD_LOCK_MODE_EXCLUSIVE));
rbd_lock_mode_t lock_mode;
char *lock_owners[1];
size_t max_lock_owners = 1;
ASSERT_EQ(0, rbd_lock_get_owners(image, &lock_mode, lock_owners,
&max_lock_owners));
ASSERT_EQ(RBD_LOCK_MODE_EXCLUSIVE, lock_mode);
ASSERT_STRNE("", lock_owners[0]);
ASSERT_EQ(1U, max_lock_owners);
ASSERT_EQ(0, rbd_lock_break(image, RBD_LOCK_MODE_EXCLUSIVE, lock_owners[0]));
ASSERT_EQ(0, rbd_lock_acquire(image, RBD_LOCK_MODE_EXCLUSIVE));
EXPECT_EQ(0, rados_wait_for_latest_osdmap(blocklist_cluster));
ASSERT_EQ((ssize_t)sizeof(buf), rbd_write(image, 0, sizeof(buf), buf));
ASSERT_EQ(-EBLOCKLISTED, rbd_write(blocklist_image, 0, sizeof(buf), buf));
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_close(blocklist_image));
rbd_lock_get_owners_cleanup(lock_owners, max_lock_owners);
rados_ioctx_destroy(ioctx);
rados_ioctx_destroy(blocklist_ioctx);
rados_shutdown(blocklist_cluster);
}
TEST_F(TestLibRBD, DiscardAfterWrite)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 20;
int order = 18;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
if (this->is_skip_partial_discard_enabled(image)) {
return;
}
// enable writeback cache
ASSERT_EQ(0, image.flush());
bufferlist bl;
bl.append(std::string(256, '1'));
librbd::RBD::AioCompletion *write_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, image.aio_write(0, bl.length(), bl, write_comp));
ASSERT_EQ(0, write_comp->wait_for_complete());
write_comp->release();
librbd::RBD::AioCompletion *discard_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, image.aio_discard(0, 256, discard_comp));
ASSERT_EQ(0, discard_comp->wait_for_complete());
discard_comp->release();
librbd::RBD::AioCompletion *read_comp =
new librbd::RBD::AioCompletion(NULL, NULL);
bufferlist read_bl;
image.aio_read(0, bl.length(), read_bl, read_comp);
ASSERT_EQ(0, read_comp->wait_for_complete());
ASSERT_EQ(bl.length(), read_comp->get_return_value());
ASSERT_TRUE(read_bl.is_zero());
read_comp->release();
}
TEST_F(TestLibRBD, DefaultFeatures) {
std::string orig_default_features;
ASSERT_EQ(0, _rados.conf_get("rbd_default_features", orig_default_features));
BOOST_SCOPE_EXIT_ALL(orig_default_features) {
ASSERT_EQ(0, _rados.conf_set("rbd_default_features",
orig_default_features.c_str()));
};
std::list<std::pair<std::string, std::string> > feature_names_to_bitmask = {
{"", orig_default_features},
{"layering", "1"},
{"layering, exclusive-lock", "5"},
{"exclusive-lock,journaling", "68"},
{"125", "125"}
};
for (auto &pair : feature_names_to_bitmask) {
ASSERT_EQ(0, _rados.conf_set("rbd_default_features", pair.first.c_str()));
std::string features;
ASSERT_EQ(0, _rados.conf_get("rbd_default_features", features));
ASSERT_EQ(pair.second, features);
}
}
TEST_F(TestLibRBD, TestTrashMoveAndPurge) {
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
std::string image_id;
ASSERT_EQ(0, image.get_id(&image_id));
image.close();
ASSERT_EQ(0, rbd.trash_move(ioctx, name.c_str(), 0));
std::vector<std::string> images;
ASSERT_EQ(0, rbd.list(ioctx, images));
for (const auto& image : images) {
ASSERT_TRUE(image != name);
}
librbd::trash_image_info_t info;
ASSERT_EQ(-ENOENT, rbd.trash_get(ioctx, "dummy image id", &info));
ASSERT_EQ(0, rbd.trash_get(ioctx, image_id.c_str(), &info));
ASSERT_EQ(image_id, info.id);
std::vector<librbd::trash_image_info_t> entries;
ASSERT_EQ(0, rbd.trash_list(ioctx, entries));
ASSERT_FALSE(entries.empty());
ASSERT_EQ(entries.begin()->id, image_id);
entries.clear();
PrintProgress pp;
ASSERT_EQ(0, rbd.trash_remove_with_progress(ioctx, image_id.c_str(),
false, pp));
ASSERT_EQ(0, rbd.trash_list(ioctx, entries));
ASSERT_TRUE(entries.empty());
}
TEST_F(TestLibRBD, TestTrashMoveAndPurgeNonExpiredDelay) {
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
std::string image_id;
ASSERT_EQ(0, image.get_id(&image_id));
image.close();
ASSERT_EQ(0, rbd.trash_move(ioctx, name.c_str(), 100));
PrintProgress pp;
ASSERT_EQ(-EPERM, rbd.trash_remove_with_progress(ioctx, image_id.c_str(),
false, pp));
PrintProgress pp2;
ASSERT_EQ(0, rbd.trash_remove_with_progress(ioctx, image_id.c_str(),
true, pp2));
}
TEST_F(TestLibRBD, TestTrashPurge) {
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name1 = get_temp_image_name();
std::string name2 = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name1.c_str(), size, &order));
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name2.c_str(), size, &order));
librbd::Image image1;
ASSERT_EQ(0, rbd.open(ioctx, image1, name1.c_str(), nullptr));
std::string image_id1;
ASSERT_EQ(0, image1.get_id(&image_id1));
image1.close();
ASSERT_EQ(0, rbd.trash_move(ioctx, name1.c_str(), 0));
librbd::Image image2;
ASSERT_EQ(0, rbd.open(ioctx, image2, name2.c_str(), nullptr));
ceph::bufferlist bl;
bl.append(std::string(1024, '0'));
ASSERT_EQ(1024, image2.write(0, 1024, bl));
std::string image_id2;
ASSERT_EQ(0, image2.get_id(&image_id2));
image2.close();
ASSERT_EQ(0, rbd.trash_move(ioctx, name2.c_str(), 100));
ASSERT_EQ(0, rbd.trash_purge(ioctx, 0, -1));
std::vector<librbd::trash_image_info_t> entries;
ASSERT_EQ(0, rbd.trash_list(ioctx, entries));
ASSERT_EQ(1U, entries.size());
ASSERT_EQ(image_id2, entries[0].id);
ASSERT_EQ(name2, entries[0].name);
entries.clear();
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
float threshold = 0.0;
if (!is_librados_test_stub(_rados)) {
// real cluster usage reports have a long latency to update
threshold = -1.0;
}
ASSERT_EQ(0, rbd.trash_purge(ioctx, now.tv_sec+1000, threshold));
ASSERT_EQ(0, rbd.trash_list(ioctx, entries));
ASSERT_EQ(0U, entries.size());
}
TEST_F(TestLibRBD, TestTrashMoveAndRestore) {
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
std::string image_id;
ASSERT_EQ(0, image.get_id(&image_id));
image.close();
ASSERT_EQ(0, rbd.trash_move(ioctx, name.c_str(), 10));
std::vector<std::string> images;
ASSERT_EQ(0, rbd.list(ioctx, images));
for (const auto& image : images) {
ASSERT_TRUE(image != name);
}
std::vector<librbd::trash_image_info_t> entries;
ASSERT_EQ(0, rbd.trash_list(ioctx, entries));
ASSERT_FALSE(entries.empty());
ASSERT_EQ(entries.begin()->id, image_id);
images.clear();
ASSERT_EQ(0, rbd.trash_restore(ioctx, image_id.c_str(), ""));
ASSERT_EQ(0, rbd.list(ioctx, images));
ASSERT_FALSE(images.empty());
bool found = false;
for (const auto& image : images) {
if (image == name) {
found = true;
break;
}
}
ASSERT_TRUE(found);
}
TEST_F(TestLibRBD, TestListWatchers) {
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
std::list<librbd::image_watcher_t> watchers;
// No watchers
ASSERT_EQ(0, rbd.open_read_only(ioctx, image, name.c_str(), nullptr));
ASSERT_EQ(0, image.list_watchers(watchers));
ASSERT_EQ(0U, watchers.size());
ASSERT_EQ(0, image.close());
// One watcher
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
ASSERT_EQ(0, image.list_watchers(watchers));
ASSERT_EQ(1U, watchers.size());
auto watcher1 = watchers.front();
ASSERT_EQ(0, image.close());
// (Still) one watcher
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
ASSERT_EQ(0, image.list_watchers(watchers));
ASSERT_EQ(1U, watchers.size());
auto watcher2 = watchers.front();
ASSERT_EQ(0, image.close());
EXPECT_EQ(watcher1.addr, watcher2.addr);
EXPECT_EQ(watcher1.id, watcher2.id);
}
TEST_F(TestLibRBD, TestSetSnapById) {
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
std::string name = get_temp_image_name();
uint64_t size = 1 << 18;
int order = 12;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
ASSERT_EQ(0, image.snap_create("snap"));
vector<librbd::snap_info_t> snaps;
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_EQ(1U, snaps.size());
ASSERT_EQ(0, image.snap_set_by_id(snaps[0].id));
ASSERT_EQ(0, image.snap_set_by_id(CEPH_NOSNAP));
}
TEST_F(TestLibRBD, Namespaces) {
rados_ioctx_t ioctx;
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx));
rados_remove(ioctx, RBD_NAMESPACE);
ASSERT_EQ(0, rbd_namespace_create(ioctx, "name1"));
ASSERT_EQ(0, rbd_namespace_create(ioctx, "name2"));
ASSERT_EQ(0, rbd_namespace_create(ioctx, "name3"));
ASSERT_EQ(0, rbd_namespace_remove(ioctx, "name2"));
char names[1024];
size_t max_size = sizeof(names);
int len = rbd_namespace_list(ioctx, names, &max_size);
std::vector<std::string> cpp_names;
for (char* cur_name = names; cur_name < names + len; ) {
cpp_names.push_back(cur_name);
cur_name += strlen(cur_name) + 1;
}
ASSERT_EQ(2U, cpp_names.size());
ASSERT_EQ("name1", cpp_names[0]);
ASSERT_EQ("name3", cpp_names[1]);
bool exists;
ASSERT_EQ(0, rbd_namespace_exists(ioctx, "name2", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, rbd_namespace_exists(ioctx, "name3", &exists));
ASSERT_TRUE(exists);
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, NamespacesPP) {
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
ioctx.remove(RBD_NAMESPACE);
librbd::RBD rbd;
ASSERT_EQ(-EINVAL, rbd.namespace_create(ioctx, ""));
ASSERT_EQ(-EINVAL, rbd.namespace_remove(ioctx, ""));
ASSERT_EQ(0, rbd.namespace_create(ioctx, "name1"));
ASSERT_EQ(-EEXIST, rbd.namespace_create(ioctx, "name1"));
ASSERT_EQ(0, rbd.namespace_create(ioctx, "name2"));
ASSERT_EQ(0, rbd.namespace_create(ioctx, "name3"));
ASSERT_EQ(0, rbd.namespace_remove(ioctx, "name2"));
ASSERT_EQ(-ENOENT, rbd.namespace_remove(ioctx, "name2"));
std::vector<std::string> names;
ASSERT_EQ(0, rbd.namespace_list(ioctx, &names));
ASSERT_EQ(2U, names.size());
ASSERT_EQ("name1", names[0]);
ASSERT_EQ("name3", names[1]);
bool exists;
ASSERT_EQ(0, rbd.namespace_exists(ioctx, "name2", &exists));
ASSERT_FALSE(exists);
ASSERT_EQ(0, rbd.namespace_exists(ioctx, "name3", &exists));
ASSERT_TRUE(exists);
librados::IoCtx ns_io_ctx;
ns_io_ctx.dup(ioctx);
std::string name = get_temp_image_name();
int order = 0;
uint64_t features = 0;
if (!get_features(&features)) {
// old format doesn't support namespaces
ns_io_ctx.set_namespace("name1");
ASSERT_EQ(-EINVAL, create_image_pp(rbd, ns_io_ctx, name.c_str(), 0,
&order));
return;
}
ns_io_ctx.set_namespace("missing");
ASSERT_EQ(-ENOENT, create_image_pp(rbd, ns_io_ctx, name.c_str(), 0, &order));
ns_io_ctx.set_namespace("name1");
ASSERT_EQ(0, create_image_pp(rbd, ns_io_ctx, name.c_str(), 0, &order));
ASSERT_EQ(-EBUSY, rbd.namespace_remove(ns_io_ctx, "name1"));
std::string image_id;
{
librbd::Image image;
ASSERT_EQ(-ENOENT, rbd.open(ioctx, image, name.c_str(), NULL));
ASSERT_EQ(0, rbd.open(ns_io_ctx, image, name.c_str(), NULL));
ASSERT_EQ(0, get_image_id(image, &image_id));
}
ASSERT_EQ(-ENOENT, rbd.trash_move(ioctx, name.c_str(), 0));
ASSERT_EQ(0, rbd.trash_move(ns_io_ctx, name.c_str(), 0));
ASSERT_EQ(-EBUSY, rbd.namespace_remove(ns_io_ctx, "name1"));
PrintProgress pp;
ASSERT_EQ(-ENOENT, rbd.trash_remove_with_progress(ioctx, image_id.c_str(),
false, pp));
ASSERT_EQ(0, rbd.trash_remove_with_progress(ns_io_ctx, image_id.c_str(),
false, pp));
ASSERT_EQ(0, rbd.namespace_remove(ns_io_ctx, "name1"));
names.clear();
ASSERT_EQ(0, rbd.namespace_list(ioctx, &names));
ASSERT_EQ(1U, names.size());
ASSERT_EQ("name3", names[0]);
}
TEST_F(TestLibRBD, Migration) {
bool old_format;
uint64_t features;
ASSERT_EQ(0, get_features(&old_format, &features));
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
BOOST_SCOPE_EXIT(&ioctx) {
rados_ioctx_destroy(ioctx);
} BOOST_SCOPE_EXIT_END;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
rbd_image_options_t image_options;
rbd_image_options_create(&image_options);
BOOST_SCOPE_EXIT(&image_options) {
rbd_image_options_destroy(image_options);
} BOOST_SCOPE_EXIT_END;
ASSERT_EQ(0, rbd_migration_prepare(ioctx, name.c_str(), ioctx, name.c_str(),
image_options));
rbd_image_migration_status_t status;
ASSERT_EQ(0, rbd_migration_status(ioctx, name.c_str(), &status,
sizeof(status)));
ASSERT_EQ(status.source_pool_id, rados_ioctx_get_id(ioctx));
ASSERT_EQ(status.source_image_name, name);
if (old_format) {
ASSERT_EQ(status.source_image_id, string());
} else {
ASSERT_NE(status.source_image_id, string());
ASSERT_EQ(-EROFS, rbd_trash_remove(ioctx, status.source_image_id, false));
ASSERT_EQ(-EINVAL, rbd_trash_restore(ioctx, status.source_image_id, name.c_str()));
}
ASSERT_EQ(status.dest_pool_id, rados_ioctx_get_id(ioctx));
ASSERT_EQ(status.dest_image_name, name);
ASSERT_NE(status.dest_image_id, string());
ASSERT_EQ(status.state, RBD_IMAGE_MIGRATION_STATE_PREPARED);
rbd_migration_status_cleanup(&status);
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
char source_spec[2048];
size_t source_spec_length = sizeof(source_spec);
ASSERT_EQ(0, rbd_get_migration_source_spec(image, source_spec,
&source_spec_length));
json_spirit::mValue json_source_spec;
json_spirit::read(source_spec, json_source_spec);
EXPECT_EQ(0, rbd_close(image));
ASSERT_EQ(-EBUSY, rbd_remove(ioctx, name.c_str()));
ASSERT_EQ(-EBUSY, rbd_trash_move(ioctx, name.c_str(), 0));
ASSERT_EQ(0, rbd_migration_execute(ioctx, name.c_str()));
ASSERT_EQ(0, rbd_migration_status(ioctx, name.c_str(), &status,
sizeof(status)));
ASSERT_EQ(status.state, RBD_IMAGE_MIGRATION_STATE_EXECUTED);
rbd_migration_status_cleanup(&status);
ASSERT_EQ(0, rbd_migration_commit(ioctx, name.c_str()));
std::string new_name = get_temp_image_name();
ASSERT_EQ(0, rbd_migration_prepare(ioctx, name.c_str(), ioctx,
new_name.c_str(), image_options));
ASSERT_EQ(-EBUSY, rbd_remove(ioctx, new_name.c_str()));
ASSERT_EQ(-EBUSY, rbd_trash_move(ioctx, new_name.c_str(), 0));
ASSERT_EQ(0, rbd_migration_abort(ioctx, name.c_str()));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
EXPECT_EQ(0, rbd_close(image));
}
TEST_F(TestLibRBD, MigrationPP) {
bool old_format;
uint64_t features;
ASSERT_EQ(0, get_features(&old_format, &features));
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
librbd::RBD rbd;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::ImageOptions image_options;
ASSERT_EQ(0, rbd.migration_prepare(ioctx, name.c_str(), ioctx, name.c_str(),
image_options));
librbd::image_migration_status_t status;
ASSERT_EQ(0, rbd.migration_status(ioctx, name.c_str(), &status,
sizeof(status)));
ASSERT_EQ(status.source_pool_id, ioctx.get_id());
ASSERT_EQ(status.source_image_name, name);
if (old_format) {
ASSERT_EQ(status.source_image_id, "");
} else {
ASSERT_NE(status.source_image_id, "");
ASSERT_EQ(-EROFS, rbd.trash_remove(ioctx, status.source_image_id.c_str(), false));
ASSERT_EQ(-EINVAL, rbd.trash_restore(ioctx, status.source_image_id.c_str(), name.c_str()));
}
ASSERT_EQ(status.dest_pool_id, ioctx.get_id());
ASSERT_EQ(status.dest_image_name, name);
ASSERT_NE(status.dest_image_id, "");
ASSERT_EQ(status.state, RBD_IMAGE_MIGRATION_STATE_PREPARED);
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
std::string source_spec;
ASSERT_EQ(0, image.get_migration_source_spec(&source_spec));
json_spirit::mValue json_source_spec;
json_spirit::read(source_spec, json_source_spec);
json_spirit::mObject json_source_spec_obj = json_source_spec.get_obj();
ASSERT_EQ("native", json_source_spec_obj["type"].get_str());
ASSERT_EQ(ioctx.get_id(), json_source_spec_obj["pool_id"].get_int64());
ASSERT_EQ("", json_source_spec_obj["pool_namespace"].get_str());
ASSERT_EQ(1, json_source_spec_obj.count("image_name"));
if (!old_format) {
ASSERT_EQ(1, json_source_spec_obj.count("image_id"));
}
ASSERT_EQ(0, image.close());
ASSERT_EQ(-EBUSY, rbd.remove(ioctx, name.c_str()));
ASSERT_EQ(-EBUSY, rbd.trash_move(ioctx, name.c_str(), 0));
ASSERT_EQ(0, rbd.migration_execute(ioctx, name.c_str()));
ASSERT_EQ(0, rbd.migration_status(ioctx, name.c_str(), &status,
sizeof(status)));
ASSERT_EQ(status.state, RBD_IMAGE_MIGRATION_STATE_EXECUTED);
ASSERT_EQ(0, rbd.migration_commit(ioctx, name.c_str()));
std::string new_name = get_temp_image_name();
ASSERT_EQ(0, rbd.migration_prepare(ioctx, name.c_str(), ioctx,
new_name.c_str(), image_options));
ASSERT_EQ(-EBUSY, rbd.remove(ioctx, new_name.c_str()));
ASSERT_EQ(-EBUSY, rbd.trash_move(ioctx, new_name.c_str(), 0));
ASSERT_EQ(0, rbd.migration_abort(ioctx, name.c_str()));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
}
TEST_F(TestLibRBD, TestGetAccessTimestamp)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
struct timespec timestamp;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_get_access_timestamp(image, ×tamp));
ASSERT_LT(0, timestamp.tv_sec);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, TestGetModifyTimestamp)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
struct timespec timestamp;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_get_modify_timestamp(image, ×tamp));
ASSERT_LT(0, timestamp.tv_sec);
ASSERT_PASSED(validate_object_map, image);
ASSERT_EQ(0, rbd_close(image));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, ZeroOverlapFlatten) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
librbd::RBD rbd;
librbd::Image parent_image;
std::string name = get_temp_image_name();
uint64_t size = 1;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, parent_image, name.c_str(), NULL));
uint64_t features;
ASSERT_EQ(0, parent_image.features(&features));
ASSERT_EQ(0, parent_image.snap_create("snap"));
ASSERT_EQ(0, parent_image.snap_protect("snap"));
std::string clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(ioctx, name.c_str(), "snap", ioctx, clone_name.c_str(),
features, &order));
librbd::Image clone_image;
ASSERT_EQ(0, rbd.open(ioctx, clone_image, clone_name.c_str(), NULL));
ASSERT_EQ(0, clone_image.resize(0));
ASSERT_EQ(0, clone_image.flatten());
}
TEST_F(TestLibRBD, PoolMetadata)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
char keys[1024];
char vals[1024];
size_t keys_len = sizeof(keys);
size_t vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_pool_metadata_list(ioctx, "", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(0U, keys_len);
ASSERT_EQ(0U, vals_len);
char value[1024];
size_t value_len = sizeof(value);
memset_rand(value, value_len);
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "key1", "value1"));
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "key2", "value2"));
ASSERT_EQ(0, rbd_pool_metadata_get(ioctx, "key1", value, &value_len));
ASSERT_STREQ(value, "value1");
value_len = 1;
ASSERT_EQ(-ERANGE, rbd_pool_metadata_get(ioctx, "key1", value, &value_len));
ASSERT_EQ(value_len, strlen("value1") + 1);
ASSERT_EQ(-ERANGE, rbd_pool_metadata_list(ioctx, "", 0, keys, &keys_len, vals,
&vals_len));
keys_len = sizeof(keys);
vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_pool_metadata_list(ioctx, "", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1 + strlen("key2") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1 + strlen("value2") + 1);
ASSERT_STREQ(keys, "key1");
ASSERT_STREQ(keys + strlen(keys) + 1, "key2");
ASSERT_STREQ(vals, "value1");
ASSERT_STREQ(vals + strlen(vals) + 1, "value2");
ASSERT_EQ(0, rbd_pool_metadata_remove(ioctx, "key1"));
ASSERT_EQ(-ENOENT, rbd_pool_metadata_remove(ioctx, "key3"));
value_len = sizeof(value);
ASSERT_EQ(-ENOENT, rbd_pool_metadata_get(ioctx, "key3", value, &value_len));
ASSERT_EQ(0, rbd_pool_metadata_list(ioctx, "", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key2") + 1);
ASSERT_EQ(vals_len, strlen("value2") + 1);
ASSERT_STREQ(keys, "key2");
ASSERT_STREQ(vals, "value2");
// test config setting
ASSERT_EQ(-EINVAL, rbd_pool_metadata_set(ioctx, "conf_UNKNOWN", "false"));
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "conf_rbd_cache", "false"));
ASSERT_EQ(-EINVAL, rbd_pool_metadata_set(ioctx, "conf_rbd_cache", "INVALID"));
ASSERT_EQ(0, rbd_pool_metadata_remove(ioctx, "conf_rbd_cache"));
// test short buffer cases
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "key1", "value1"));
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "key3", "value3"));
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "key4", "value4"));
keys_len = strlen("key1") + 1;
vals_len = strlen("value1") + 1;
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_pool_metadata_list(ioctx, "", 1, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1);
ASSERT_STREQ(keys, "key1");
ASSERT_STREQ(vals, "value1");
ASSERT_EQ(-ERANGE, rbd_pool_metadata_list(ioctx, "", 2, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1 + strlen("key2") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1 + strlen("value2") + 1);
ASSERT_EQ(-ERANGE, rbd_pool_metadata_list(ioctx, "", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key1") + 1 + strlen("key2") + 1 + strlen("key3") +
1 + strlen("key4") + 1);
ASSERT_EQ(vals_len, strlen("value1") + 1 + strlen("value2") + 1 +
strlen("value3") + 1 + strlen("value4") + 1);
// test `start` param
keys_len = sizeof(keys);
vals_len = sizeof(vals);
memset_rand(keys, keys_len);
memset_rand(vals, vals_len);
ASSERT_EQ(0, rbd_pool_metadata_list(ioctx, "key2", 0, keys, &keys_len, vals,
&vals_len));
ASSERT_EQ(keys_len, strlen("key3") + 1 + strlen("key4") + 1);
ASSERT_EQ(vals_len, strlen("value3") + 1 + strlen("value4") + 1);
ASSERT_STREQ(keys, "key3");
ASSERT_STREQ(vals, "value3");
//cleanup
ASSERT_EQ(0, rbd_pool_metadata_remove(ioctx, "key1"));
ASSERT_EQ(0, rbd_pool_metadata_remove(ioctx, "key2"));
ASSERT_EQ(0, rbd_pool_metadata_remove(ioctx, "key3"));
ASSERT_EQ(0, rbd_pool_metadata_remove(ioctx, "key4"));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, PoolMetadataPP)
{
REQUIRE_FORMAT_V2();
librbd::RBD rbd;
string value;
map<string, bufferlist> pairs;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
ASSERT_EQ(0, rbd.pool_metadata_list(ioctx, "", 0, &pairs));
ASSERT_TRUE(pairs.empty());
ASSERT_EQ(0, rbd.pool_metadata_set(ioctx, "key1", "value1"));
ASSERT_EQ(0, rbd.pool_metadata_set(ioctx, "key2", "value2"));
ASSERT_EQ(0, rbd.pool_metadata_get(ioctx, "key1", &value));
ASSERT_EQ(value, "value1");
ASSERT_EQ(0, rbd.pool_metadata_list(ioctx, "", 0, &pairs));
ASSERT_EQ(2U, pairs.size());
ASSERT_EQ(0, strncmp("value1", pairs["key1"].c_str(), 6));
ASSERT_EQ(0, strncmp("value2", pairs["key2"].c_str(), 6));
ASSERT_EQ(0, rbd.pool_metadata_remove(ioctx, "key1"));
ASSERT_EQ(-ENOENT, rbd.pool_metadata_remove(ioctx, "key3"));
ASSERT_EQ(-ENOENT, rbd.pool_metadata_get(ioctx, "key3", &value));
pairs.clear();
ASSERT_EQ(0, rbd.pool_metadata_list(ioctx, "", 0, &pairs));
ASSERT_EQ(1U, pairs.size());
ASSERT_EQ(0, strncmp("value2", pairs["key2"].c_str(), 6));
// test `start` param
ASSERT_EQ(0, rbd.pool_metadata_set(ioctx, "key1", "value1"));
ASSERT_EQ(0, rbd.pool_metadata_set(ioctx, "key3", "value3"));
pairs.clear();
ASSERT_EQ(0, rbd.pool_metadata_list(ioctx, "key2", 0, &pairs));
ASSERT_EQ(1U, pairs.size());
ASSERT_EQ(0, strncmp("value3", pairs["key3"].c_str(), 6));
// test config setting
ASSERT_EQ(-EINVAL, rbd.pool_metadata_set(ioctx, "conf_UNKNOWN", "false"));
ASSERT_EQ(0, rbd.pool_metadata_set(ioctx, "conf_rbd_cache", "false"));
ASSERT_EQ(-EINVAL, rbd.pool_metadata_set(ioctx, "conf_rbd_cache", "INVALID"));
ASSERT_EQ(0, rbd.pool_metadata_remove(ioctx, "conf_rbd_cache"));
// cleanup
ASSERT_EQ(0, rbd.pool_metadata_remove(ioctx, "key1"));
ASSERT_EQ(0, rbd.pool_metadata_remove(ioctx, "key2"));
ASSERT_EQ(0, rbd.pool_metadata_remove(ioctx, "key3"));
}
TEST_F(TestLibRBD, Config)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
ASSERT_EQ(0, rbd_pool_metadata_set(ioctx, "conf_rbd_cache", "false"));
rbd_config_option_t options[1024];
int max_options = 0;
ASSERT_EQ(-ERANGE, rbd_config_pool_list(ioctx, options, &max_options));
ASSERT_EQ(0, rbd_config_pool_list(ioctx, options, &max_options));
ASSERT_GT(max_options, 0);
ASSERT_LT(max_options, 1024);
for (int i = 0; i < max_options; i++) {
if (options[i].name == std::string("rbd_cache")) {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_POOL);
ASSERT_STREQ("false", options[i].value);
} else {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_CONFIG);
}
}
rbd_config_pool_list_cleanup(options, max_options);
rbd_image_t image;
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image, NULL));
ASSERT_EQ(0, rbd_config_image_list(image, options, &max_options));
for (int i = 0; i < max_options; i++) {
if (options[i].name == std::string("rbd_cache")) {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_POOL);
ASSERT_STREQ("false", options[i].value);
} else {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_CONFIG);
}
}
rbd_config_image_list_cleanup(options, max_options);
ASSERT_EQ(0, rbd_metadata_set(image, "conf_rbd_cache", "true"));
ASSERT_EQ(0, rbd_config_image_list(image, options, &max_options));
for (int i = 0; i < max_options; i++) {
if (options[i].name == std::string("rbd_cache")) {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_IMAGE);
ASSERT_STREQ("true", options[i].value);
} else {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_CONFIG);
}
}
rbd_config_image_list_cleanup(options, max_options);
ASSERT_EQ(0, rbd_metadata_remove(image, "conf_rbd_cache"));
ASSERT_EQ(0, rbd_config_image_list(image, options, &max_options));
for (int i = 0; i < max_options; i++) {
if (options[i].name == std::string("rbd_cache")) {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_POOL);
ASSERT_STREQ("false", options[i].value);
} else {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_CONFIG);
}
}
rbd_config_image_list_cleanup(options, max_options);
ASSERT_EQ(0, rbd_close(image));
ASSERT_EQ(0, rbd_pool_metadata_remove(ioctx, "conf_rbd_cache"));
ASSERT_EQ(-ERANGE, rbd_config_pool_list(ioctx, options, &max_options));
ASSERT_EQ(0, rbd_config_pool_list(ioctx, options, &max_options));
for (int i = 0; i < max_options; i++) {
ASSERT_EQ(options[i].source, RBD_CONFIG_SOURCE_CONFIG);
}
rbd_config_pool_list_cleanup(options, max_options);
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, ConfigPP)
{
REQUIRE_FORMAT_V2();
librbd::RBD rbd;
string value;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
ASSERT_EQ(0, rbd.pool_metadata_set(ioctx, "conf_rbd_cache", "false"));
std::vector<librbd::config_option_t> options;
ASSERT_EQ(0, rbd.config_list(ioctx, &options));
for (auto &option : options) {
if (option.name == std::string("rbd_cache")) {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_POOL);
ASSERT_EQ("false", option.value);
} else {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_CONFIG);
}
}
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), nullptr));
options.clear();
ASSERT_EQ(0, image.config_list(&options));
for (auto &option : options) {
if (option.name == std::string("rbd_cache")) {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_POOL);
ASSERT_EQ("false", option.value);
} else {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_CONFIG);
}
}
ASSERT_EQ(0, image.metadata_set("conf_rbd_cache", "true"));
options.clear();
ASSERT_EQ(0, image.config_list(&options));
for (auto &option : options) {
if (option.name == std::string("rbd_cache")) {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_IMAGE);
ASSERT_EQ("true", option.value);
} else {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_CONFIG);
}
}
ASSERT_EQ(0, image.metadata_remove("conf_rbd_cache"));
options.clear();
ASSERT_EQ(0, image.config_list(&options));
for (auto &option : options) {
if (option.name == std::string("rbd_cache")) {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_POOL);
ASSERT_EQ("false", option.value);
} else {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_CONFIG);
}
}
ASSERT_EQ(0, rbd.pool_metadata_remove(ioctx, "conf_rbd_cache"));
options.clear();
ASSERT_EQ(0, rbd.config_list(ioctx, &options));
for (auto &option : options) {
ASSERT_EQ(option.source, RBD_CONFIG_SOURCE_CONFIG);
}
}
TEST_F(TestLibRBD, PoolStatsPP)
{
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(create_pool(true).c_str(), ioctx));
librbd::RBD rbd;
std::string image_name;
uint64_t size = 2 << 20;
uint64_t expected_size = 0;
for (size_t idx = 0; idx < 4; ++idx) {
image_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, image_name.c_str(), size, &order));
expected_size += size;
}
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("snap1"));
ASSERT_EQ(0, image.resize(0));
ASSERT_EQ(0, image.close());
uint64_t expect_head_size = (expected_size - size);
uint64_t image_count;
uint64_t provisioned_bytes;
uint64_t max_provisioned_bytes;
uint64_t snap_count;
uint64_t trash_image_count;
uint64_t trash_provisioned_bytes;
uint64_t trash_max_provisioned_bytes;
uint64_t trash_snap_count;
librbd::PoolStats pool_stats1;
pool_stats1.add(RBD_POOL_STAT_OPTION_IMAGES, &image_count);
pool_stats1.add(RBD_POOL_STAT_OPTION_IMAGE_PROVISIONED_BYTES,
&provisioned_bytes);
ASSERT_EQ(0, rbd.pool_stats_get(ioctx, &pool_stats1));
ASSERT_EQ(4U, image_count);
ASSERT_EQ(expect_head_size, provisioned_bytes);
pool_stats1.add(RBD_POOL_STAT_OPTION_IMAGE_MAX_PROVISIONED_BYTES,
&max_provisioned_bytes);
ASSERT_EQ(0, rbd.pool_stats_get(ioctx, &pool_stats1));
ASSERT_EQ(4U, image_count);
ASSERT_EQ(expect_head_size, provisioned_bytes);
ASSERT_EQ(expected_size, max_provisioned_bytes);
librbd::PoolStats pool_stats2;
pool_stats2.add(RBD_POOL_STAT_OPTION_IMAGE_SNAPSHOTS, &snap_count);
pool_stats2.add(RBD_POOL_STAT_OPTION_TRASH_IMAGES, &trash_image_count);
pool_stats2.add(RBD_POOL_STAT_OPTION_TRASH_SNAPSHOTS, &trash_snap_count);
ASSERT_EQ(0, rbd.pool_stats_get(ioctx, &pool_stats2));
ASSERT_EQ(1U, snap_count);
ASSERT_EQ(0U, trash_image_count);
ASSERT_EQ(0U, trash_snap_count);
ASSERT_EQ(0, rbd.trash_move(ioctx, image_name.c_str(), 0));
librbd::PoolStats pool_stats3;
pool_stats3.add(RBD_POOL_STAT_OPTION_TRASH_IMAGES, &trash_image_count);
pool_stats3.add(RBD_POOL_STAT_OPTION_TRASH_PROVISIONED_BYTES,
&trash_provisioned_bytes);
pool_stats3.add(RBD_POOL_STAT_OPTION_TRASH_MAX_PROVISIONED_BYTES,
&trash_max_provisioned_bytes);
pool_stats3.add(RBD_POOL_STAT_OPTION_TRASH_SNAPSHOTS, &trash_snap_count);
ASSERT_EQ(0, rbd.pool_stats_get(ioctx, &pool_stats3));
ASSERT_EQ(1U, trash_image_count);
ASSERT_EQ(0U, trash_provisioned_bytes);
ASSERT_EQ(size, trash_max_provisioned_bytes);
ASSERT_EQ(1U, trash_snap_count);
}
TEST_F(TestLibRBD, ImageSpec) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(create_pool(true).c_str(), ioctx));
librbd::RBD rbd;
librbd::Image parent_image;
std::string name = get_temp_image_name();
uint64_t size = 1;
int order = 0;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, parent_image, name.c_str(), NULL));
std::string parent_id;
ASSERT_EQ(0, parent_image.get_id(&parent_id));
uint64_t features;
ASSERT_EQ(0, parent_image.features(&features));
ASSERT_EQ(0, parent_image.snap_create("snap"));
ASSERT_EQ(0, parent_image.snap_protect("snap"));
std::string clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(ioctx, name.c_str(), "snap", ioctx, clone_name.c_str(),
features, &order));
librbd::Image clone_image;
ASSERT_EQ(0, rbd.open(ioctx, clone_image, clone_name.c_str(), NULL));
std::string clone_id;
ASSERT_EQ(0, clone_image.get_id(&clone_id));
std::vector<librbd::image_spec_t> images;
ASSERT_EQ(0, rbd.list2(ioctx, &images));
std::vector<librbd::image_spec_t> expected_images{
{.id = parent_id, .name = name},
{.id = clone_id, .name = clone_name}
};
std::sort(expected_images.begin(), expected_images.end(),
[](const librbd::image_spec_t& lhs, const librbd::image_spec_t &rhs) {
return lhs.name < rhs.name;
});
ASSERT_EQ(expected_images, images);
librbd::linked_image_spec_t parent_image_spec;
librbd::snap_spec_t parent_snap_spec;
ASSERT_EQ(0, clone_image.get_parent(&parent_image_spec, &parent_snap_spec));
librbd::linked_image_spec_t expected_parent_image_spec{
.pool_id = ioctx.get_id(),
.pool_name = ioctx.get_pool_name(),
.pool_namespace = ioctx.get_namespace(),
.image_id = parent_id,
.image_name = name,
.trash = false
};
ASSERT_EQ(expected_parent_image_spec, parent_image_spec);
ASSERT_EQ(RBD_SNAP_NAMESPACE_TYPE_USER, parent_snap_spec.namespace_type);
ASSERT_EQ("snap", parent_snap_spec.name);
std::vector<librbd::linked_image_spec_t> children;
ASSERT_EQ(0, parent_image.list_children3(&children));
std::vector<librbd::linked_image_spec_t> expected_children{
{
.pool_id = ioctx.get_id(),
.pool_name = ioctx.get_pool_name(),
.pool_namespace = ioctx.get_namespace(),
.image_id = clone_id,
.image_name = clone_name,
.trash = false
}
};
ASSERT_EQ(expected_children, children);
children.clear();
ASSERT_EQ(0, parent_image.list_descendants(&children));
ASSERT_EQ(expected_children, children);
ASSERT_EQ(0, clone_image.snap_create("snap"));
ASSERT_EQ(0, clone_image.snap_protect("snap"));
auto grand_clone_name = this->get_temp_image_name();
ASSERT_EQ(0, rbd.clone(ioctx, clone_name.c_str(), "snap", ioctx,
grand_clone_name.c_str(), features, &order));
librbd::Image grand_clone_image;
ASSERT_EQ(0, rbd.open(ioctx, grand_clone_image, grand_clone_name.c_str(),
nullptr));
std::string grand_clone_id;
ASSERT_EQ(0, grand_clone_image.get_id(&grand_clone_id));
children.clear();
ASSERT_EQ(0, parent_image.list_children3(&children));
ASSERT_EQ(expected_children, children);
children.clear();
ASSERT_EQ(0, parent_image.list_descendants(&children));
expected_children.push_back(
{
.pool_id = ioctx.get_id(),
.pool_name = ioctx.get_pool_name(),
.pool_namespace = ioctx.get_namespace(),
.image_id = grand_clone_id,
.image_name = grand_clone_name,
.trash = false
}
);
ASSERT_EQ(expected_children, children);
}
void super_simple_write_cb_pp(librbd::completion_t cb, void *arg)
{
}
TEST_F(TestLibRBD, DISABLED_TestSeqWriteAIOPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
{
librbd::RBD rbd;
librbd::Image image;
int order = 21;
std::string name = get_temp_image_name();
uint64_t size = 5 * (1 << order);
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
char test_data[(TEST_IO_SIZE + 1) * 10];
for (int i = 0; i < 10; i++) {
for (uint64_t j = 0; j < TEST_IO_SIZE; j++) {
test_data[(TEST_IO_SIZE + 1) * i + j] = (char)(rand() % (126 - 33) + 33);
}
test_data[(TEST_IO_SIZE + 1) * i + TEST_IO_SIZE] = '\0';
}
struct timespec start_time;
clock_gettime(CLOCK_REALTIME, &start_time);
std::list<librbd::RBD::AioCompletion *> comps;
for (uint64_t i = 0; i < size / TEST_IO_SIZE; ++i) {
char *p = test_data + (TEST_IO_SIZE + 1) * (i % 10);
ceph::bufferlist bl;
bl.append(p, strlen(p));
auto comp = new librbd::RBD::AioCompletion(
NULL, (librbd::callback_t) super_simple_write_cb_pp);
image.aio_write(strlen(p) * i, strlen(p), bl, comp);
comps.push_back(comp);
if (i % 1000 == 0) {
cout << i << " reqs sent" << std::endl;
image.flush();
for (auto comp : comps) {
comp->wait_for_complete();
ASSERT_EQ(0, comp->get_return_value());
comp->release();
}
comps.clear();
}
}
int i = 0;
for (auto comp : comps) {
comp->wait_for_complete();
ASSERT_EQ(0, comp->get_return_value());
comp->release();
if (i % 1000 == 0) {
std::cout << i << " reqs completed" << std::endl;
}
i++;
}
comps.clear();
struct timespec end_time;
clock_gettime(CLOCK_REALTIME, &end_time);
int duration = end_time.tv_sec * 1000 + end_time.tv_nsec / 1000000 -
start_time.tv_sec * 1000 - start_time.tv_nsec / 1000000;
std::cout << "duration: " << duration << " msec" << std::endl;
for (uint64_t i = 0; i < size / TEST_IO_SIZE; ++i) {
char *p = test_data + (TEST_IO_SIZE + 1) * (i % 10);
ASSERT_PASSED(read_test_data, image, p, strlen(p) * i, TEST_IO_SIZE, 0);
}
ASSERT_PASSED(validate_object_map, image);
}
ioctx.close();
}
TEST_F(TestLibRBD, SnapRemoveWithChildMissing)
{
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, rados_conf_set(_cluster, "rbd_default_clone_format", "2"));
BOOST_SCOPE_EXIT_ALL(&) {
ASSERT_EQ(0, rados_conf_set(_cluster, "rbd_default_clone_format", "auto"));
};
librbd::RBD rbd;
rados_ioctx_t ioctx1, ioctx2;
string pool_name1 = create_pool(true);
rados_ioctx_create(_cluster, pool_name1.c_str(), &ioctx1);
ASSERT_EQ(0, rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx2));
bool old_format;
uint64_t features;
rbd_image_t parent, child1, child2, child3;
int order = 0;
char child_id1[4096];
char child_id2[4096];
char child_id3[4096];
ASSERT_EQ(0, get_features(&old_format, &features));
ASSERT_FALSE(old_format);
std::string parent_name = get_temp_image_name();
std::string child_name1 = get_temp_image_name();
std::string child_name2 = get_temp_image_name();
std::string child_name3 = get_temp_image_name();
ASSERT_EQ(0, create_image_full(ioctx1, parent_name.c_str(), 4<<20, &order,
false, features));
ASSERT_EQ(0, rbd_open(ioctx1, parent_name.c_str(), &parent, NULL));
ASSERT_EQ(0, rbd_snap_create(parent, "snap1"));
ASSERT_EQ(0, rbd_snap_create(parent, "snap2"));
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "snap1",
ioctx2, child_name1.c_str(), features, &order));
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "snap2",
ioctx1, child_name2.c_str(), features, &order));
ASSERT_EQ(0, clone_image(ioctx1, parent, parent_name.c_str(), "snap2",
ioctx2, child_name3.c_str(), features, &order));
ASSERT_EQ(0, rbd_open(ioctx2, child_name1.c_str(), &child1, NULL));
ASSERT_EQ(0, rbd_open(ioctx1, child_name2.c_str(), &child2, NULL));
ASSERT_EQ(0, rbd_open(ioctx2, child_name3.c_str(), &child3, NULL));
ASSERT_EQ(0, rbd_get_id(child1, child_id1, sizeof(child_id1)));
ASSERT_EQ(0, rbd_get_id(child2, child_id2, sizeof(child_id2)));
ASSERT_EQ(0, rbd_get_id(child3, child_id3, sizeof(child_id3)));
test_list_children2(parent, 3,
child_id1, m_pool_name.c_str(), child_name1.c_str(), false,
child_id2, pool_name1.c_str(), child_name2.c_str(), false,
child_id3, m_pool_name.c_str(), child_name3.c_str(), false);
size_t max_size = 10;
rbd_linked_image_spec_t children[max_size];
ASSERT_EQ(0, rbd_list_children3(parent, children, &max_size));
ASSERT_EQ(3, static_cast<int>(max_size));
rbd_linked_image_spec_list_cleanup(children, max_size);
ASSERT_EQ(0, rbd_close(child1));
ASSERT_EQ(0, rbd_close(child2));
ASSERT_EQ(0, rbd_close(child3));
rados_ioctx_destroy(ioctx2);
ASSERT_EQ(0, rados_pool_delete(_cluster, m_pool_name.c_str()));
_pool_names.erase(std::remove(_pool_names.begin(),
_pool_names.end(), m_pool_name),
_pool_names.end());
EXPECT_EQ(0, rados_wait_for_latest_osdmap(_cluster));
ASSERT_EQ(0, rbd_list_children3(parent, children, &max_size));
ASSERT_EQ(3, static_cast<int>(max_size));
rbd_linked_image_spec_list_cleanup(children, max_size);
ASSERT_EQ(0, rbd_snap_remove(parent, "snap1"));
ASSERT_EQ(0, rbd_list_children3(parent, children, &max_size));
ASSERT_EQ(2, static_cast<int>(max_size));
rbd_linked_image_spec_list_cleanup(children, max_size);
ASSERT_EQ(0, rbd_remove(ioctx1, child_name2.c_str()));
ASSERT_EQ(0, rbd_list_children3(parent, children, &max_size));
ASSERT_EQ(1, static_cast<int>(max_size));
rbd_linked_image_spec_list_cleanup(children, max_size);
ASSERT_EQ(0, rbd_snap_remove(parent, "snap2"));
ASSERT_EQ(0, rbd_list_children3(parent, children, &max_size));
ASSERT_EQ(0, static_cast<int>(max_size));
rbd_linked_image_spec_list_cleanup(children, max_size);
test_list_children2(parent, 0);
ASSERT_EQ(0, test_ls_snaps(parent, 0));
ASSERT_EQ(0, rbd_close(parent));
rados_ioctx_destroy(ioctx1);
}
TEST_F(TestLibRBD, QuiesceWatch)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, m_pool_name.c_str(), &ioctx);
int order = 0;
std::string name = get_temp_image_name();
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image(ioctx, name.c_str(), size, &order));
rbd_image_t image1, image2;
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image1, NULL));
ASSERT_EQ(0, rbd_open(ioctx, name.c_str(), &image2, NULL));
struct Watcher {
static void quiesce_cb(void *arg) {
Watcher *watcher = static_cast<Watcher *>(arg);
watcher->handle_quiesce();
}
static void unquiesce_cb(void *arg) {
Watcher *watcher = static_cast<Watcher *>(arg);
watcher->handle_unquiesce();
}
rbd_image_t ℑ
uint64_t handle = 0;
size_t quiesce_count = 0;
size_t unquiesce_count = 0;
ceph::mutex lock = ceph::make_mutex("lock");
ceph::condition_variable cv;
Watcher(rbd_image_t &image) : image(image) {
}
void handle_quiesce() {
ASSERT_EQ(quiesce_count, unquiesce_count);
quiesce_count++;
rbd_quiesce_complete(image, handle, 0);
}
void handle_unquiesce() {
std::unique_lock locker(lock);
unquiesce_count++;
ASSERT_EQ(quiesce_count, unquiesce_count);
cv.notify_one();
}
bool wait_for_unquiesce(size_t c) {
std::unique_lock locker(lock);
return cv.wait_for(locker, seconds(60),
[this, c]() { return unquiesce_count >= c; });
}
} watcher1(image1), watcher2(image2);
ASSERT_EQ(0, rbd_quiesce_watch(image1, Watcher::quiesce_cb,
Watcher::unquiesce_cb, &watcher1,
&watcher1.handle));
ASSERT_EQ(0, rbd_quiesce_watch(image2, Watcher::quiesce_cb,
Watcher::unquiesce_cb, &watcher2,
&watcher2.handle));
ASSERT_EQ(0, rbd_snap_create(image1, "snap1"));
ASSERT_EQ(1U, watcher1.quiesce_count);
ASSERT_TRUE(watcher1.wait_for_unquiesce(1U));
ASSERT_EQ(1U, watcher2.quiesce_count);
ASSERT_TRUE(watcher2.wait_for_unquiesce(1U));
ASSERT_EQ(0, rbd_snap_create(image2, "snap2"));
ASSERT_EQ(2U, watcher1.quiesce_count);
ASSERT_TRUE(watcher1.wait_for_unquiesce(2U));
ASSERT_EQ(2U, watcher2.quiesce_count);
ASSERT_TRUE(watcher2.wait_for_unquiesce(2U));
ASSERT_EQ(0, rbd_quiesce_unwatch(image1, watcher1.handle));
ASSERT_EQ(0, rbd_snap_create(image1, "snap3"));
ASSERT_EQ(2U, watcher1.quiesce_count);
ASSERT_EQ(2U, watcher1.unquiesce_count);
ASSERT_EQ(3U, watcher2.quiesce_count);
ASSERT_TRUE(watcher2.wait_for_unquiesce(3U));
ASSERT_EQ(0, rbd_quiesce_unwatch(image2, watcher2.handle));
ASSERT_EQ(0, rbd_snap_remove(image1, "snap1"));
ASSERT_EQ(0, rbd_snap_remove(image1, "snap2"));
ASSERT_EQ(0, rbd_snap_remove(image1, "snap3"));
ASSERT_EQ(0, rbd_close(image1));
ASSERT_EQ(0, rbd_close(image2));
ASSERT_EQ(0, rbd_remove(ioctx, name.c_str()));
rados_ioctx_destroy(ioctx);
}
TEST_F(TestLibRBD, QuiesceWatchPP)
{
librbd::RBD rbd;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
std::string name = get_temp_image_name();
int order = 0;
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
{
librbd::Image image1, image2;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
struct Watcher : public librbd::QuiesceWatchCtx {
librbd::Image ℑ
uint64_t handle = 0;
size_t quiesce_count = 0;
size_t unquiesce_count = 0;
ceph::mutex lock = ceph::make_mutex("lock");
ceph::condition_variable cv;
Watcher(librbd::Image &image) : image(image) {
}
void handle_quiesce() override {
ASSERT_EQ(quiesce_count, unquiesce_count);
quiesce_count++;
image.quiesce_complete(handle, 0);
}
void handle_unquiesce() override {
std::unique_lock locker(lock);
unquiesce_count++;
ASSERT_EQ(quiesce_count, unquiesce_count);
cv.notify_one();
}
bool wait_for_unquiesce(size_t c) {
std::unique_lock locker(lock);
return cv.wait_for(locker, seconds(60),
[this, c]() { return unquiesce_count >= c; });
}
} watcher1(image1), watcher2(image2);
ASSERT_EQ(0, image1.quiesce_watch(&watcher1, &watcher1.handle));
ASSERT_EQ(0, image2.quiesce_watch(&watcher2, &watcher2.handle));
ASSERT_EQ(0, image1.snap_create("snap1"));
ASSERT_EQ(1U, watcher1.quiesce_count);
ASSERT_TRUE(watcher1.wait_for_unquiesce(1U));
ASSERT_EQ(1U, watcher2.quiesce_count);
ASSERT_TRUE(watcher2.wait_for_unquiesce(1U));
ASSERT_EQ(0, image2.snap_create("snap2"));
ASSERT_EQ(2U, watcher1.quiesce_count);
ASSERT_TRUE(watcher1.wait_for_unquiesce(2U));
ASSERT_EQ(2U, watcher2.quiesce_count);
ASSERT_TRUE(watcher2.wait_for_unquiesce(2U));
ASSERT_EQ(0, image1.quiesce_unwatch(watcher1.handle));
ASSERT_EQ(0, image1.snap_create("snap3"));
ASSERT_EQ(2U, watcher1.quiesce_count);
ASSERT_EQ(2U, watcher1.unquiesce_count);
ASSERT_EQ(3U, watcher2.quiesce_count);
ASSERT_TRUE(watcher2.wait_for_unquiesce(3U));
ASSERT_EQ(0, image2.quiesce_unwatch(watcher2.handle));
ASSERT_EQ(0, image1.snap_remove("snap1"));
ASSERT_EQ(0, image1.snap_remove("snap2"));
ASSERT_EQ(0, image1.snap_remove("snap3"));
}
ASSERT_EQ(0, rbd.remove(ioctx, name.c_str()));
ioctx.close();
}
TEST_F(TestLibRBD, QuiesceWatchError)
{
SKIP_IF_CRIMSON();
librbd::RBD rbd;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
std::string name = get_temp_image_name();
int order = 0;
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
{
librbd::Image image1, image2;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
struct Watcher : public librbd::QuiesceWatchCtx {
librbd::Image ℑ
int r;
uint64_t handle;
size_t quiesce_count = 0;
size_t unquiesce_count = 0;
ceph::mutex lock = ceph::make_mutex("lock");
ceph::condition_variable cv;
Watcher(librbd::Image &image, int r) : image(image), r(r) {
}
void reset_counters() {
quiesce_count = 0;
unquiesce_count = 0;
}
void handle_quiesce() override {
quiesce_count++;
image.quiesce_complete(handle, r);
}
void handle_unquiesce() override {
std::unique_lock locker(lock);
unquiesce_count++;
cv.notify_one();
}
bool wait_for_unquiesce() {
std::unique_lock locker(lock);
return cv.wait_for(locker, seconds(60),
[this]() {
return quiesce_count == unquiesce_count;
});
}
} watcher10(image1, -EINVAL), watcher11(image1, 0), watcher20(image2, 0);
ASSERT_EQ(0, image1.quiesce_watch(&watcher10, &watcher10.handle));
ASSERT_EQ(0, image1.quiesce_watch(&watcher11, &watcher11.handle));
ASSERT_EQ(0, image2.quiesce_watch(&watcher20, &watcher20.handle));
ASSERT_EQ(-EINVAL, image1.snap_create("snap1"));
ASSERT_GT(watcher10.quiesce_count, 0U);
ASSERT_EQ(watcher10.unquiesce_count, 0U);
ASSERT_GT(watcher11.quiesce_count, 0U);
ASSERT_TRUE(watcher11.wait_for_unquiesce());
ASSERT_GT(watcher20.quiesce_count, 0U);
ASSERT_TRUE(watcher20.wait_for_unquiesce());
PrintProgress prog_ctx;
watcher10.reset_counters();
watcher11.reset_counters();
watcher20.reset_counters();
ASSERT_EQ(0, image2.snap_create2("snap2",
RBD_SNAP_CREATE_IGNORE_QUIESCE_ERROR,
prog_ctx));
ASSERT_GT(watcher10.quiesce_count, 0U);
ASSERT_EQ(watcher10.unquiesce_count, 0U);
ASSERT_GT(watcher11.quiesce_count, 0U);
ASSERT_TRUE(watcher11.wait_for_unquiesce());
ASSERT_GT(watcher20.quiesce_count, 0U);
ASSERT_TRUE(watcher20.wait_for_unquiesce());
ASSERT_EQ(0, image1.quiesce_unwatch(watcher10.handle));
watcher11.reset_counters();
watcher20.reset_counters();
ASSERT_EQ(0, image1.snap_create("snap3"));
ASSERT_GT(watcher11.quiesce_count, 0U);
ASSERT_TRUE(watcher11.wait_for_unquiesce());
ASSERT_GT(watcher20.quiesce_count, 0U);
ASSERT_TRUE(watcher20.wait_for_unquiesce());
ASSERT_EQ(0, image1.quiesce_unwatch(watcher11.handle));
watcher20.reset_counters();
ASSERT_EQ(0, image2.snap_create2("snap4", RBD_SNAP_CREATE_SKIP_QUIESCE,
prog_ctx));
ASSERT_EQ(watcher20.quiesce_count, 0U);
ASSERT_EQ(watcher20.unquiesce_count, 0U);
ASSERT_EQ(0, image2.quiesce_unwatch(watcher20.handle));
ASSERT_EQ(0, image1.snap_remove("snap2"));
ASSERT_EQ(0, image1.snap_remove("snap3"));
ASSERT_EQ(0, image1.snap_remove("snap4"));
}
ASSERT_EQ(0, rbd.remove(ioctx, name.c_str()));
ioctx.close();
}
TEST_F(TestLibRBD, QuiesceWatchTimeout)
{
REQUIRE(!is_librados_test_stub(_rados));
ASSERT_EQ(0, _rados.conf_set("rbd_quiesce_notification_attempts", "2"));
librbd::RBD rbd;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
std::string name = get_temp_image_name();
int order = 0;
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
{
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
struct Watcher : public librbd::QuiesceWatchCtx {
librbd::Image ℑ
std::mutex m_lock;
std::condition_variable m_cond;
size_t quiesce_count = 0;
size_t unquiesce_count = 0;
Watcher(librbd::Image &image) : image(image) {
}
void handle_quiesce() override {
std::lock_guard<std::mutex> locker(m_lock);
quiesce_count++;
m_cond.notify_one();
}
void handle_unquiesce() override {
std::lock_guard<std::mutex> locker(m_lock);
unquiesce_count++;
m_cond.notify_one();
}
void wait_for_quiesce() {
std::unique_lock<std::mutex> locker(m_lock);
ASSERT_TRUE(m_cond.wait_for(locker, seconds(60),
[this] {
return quiesce_count >= 1;
}));
}
void wait_for_unquiesce() {
std::unique_lock<std::mutex> locker(m_lock);
ASSERT_TRUE(m_cond.wait_for(locker, seconds(60),
[this] {
return quiesce_count == unquiesce_count;
}));
quiesce_count = unquiesce_count = 0;
}
} watcher(image);
uint64_t handle;
ASSERT_EQ(0, image.quiesce_watch(&watcher, &handle));
std::cerr << "test quiesce is not long enough to time out" << std::endl;
thread quiesce1([&image, &watcher, handle]() {
watcher.wait_for_quiesce();
sleep(8);
image.quiesce_complete(handle, 0);
});
ASSERT_EQ(0, image.snap_create("snap1"));
quiesce1.join();
ASSERT_GE(watcher.quiesce_count, 1U);
watcher.wait_for_unquiesce();
std::cerr << "test quiesce is timed out" << std::endl;
bool timed_out = false;
thread quiesce2([&image, &watcher, handle, &timed_out]() {
watcher.wait_for_quiesce();
for (int i = 0; !timed_out && i < 60; i++) {
std::cerr << "waiting for timed out ... " << i << std::endl;
sleep(1);
}
image.quiesce_complete(handle, 0);
});
ASSERT_EQ(-ETIMEDOUT, image.snap_create("snap2"));
timed_out = true;
quiesce2.join();
ASSERT_GE(watcher.quiesce_count, 1U);
watcher.wait_for_unquiesce();
thread quiesce3([&image, handle, &watcher]() {
watcher.wait_for_quiesce();
image.quiesce_complete(handle, 0);
});
std::cerr << "test retry succeeds" << std::endl;
ASSERT_EQ(0, image.snap_create("snap2"));
quiesce3.join();
ASSERT_GE(watcher.quiesce_count, 1U);
watcher.wait_for_unquiesce();
ASSERT_EQ(0, image.snap_remove("snap1"));
ASSERT_EQ(0, image.snap_remove("snap2"));
}
ASSERT_EQ(0, rbd.remove(ioctx, name.c_str()));
ioctx.close();
}
TEST_F(TestLibRBD, WriteZeroes) {
librbd::RBD rbd;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
std::string name = get_temp_image_name();
int order = 0;
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
// 1s from [0, 256) / length 256
char data[256];
memset(data, 1, sizeof(data));
bufferlist bl;
bl.append(data, 256);
ASSERT_EQ(256, image.write(0, 256, bl));
interval_set<uint64_t> diff;
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
auto expected_diff = interval_set<uint64_t>{{{0, 256}}};
ASSERT_EQ(expected_diff, diff);
// writes zero passed the current end extents.
// Now 1s from [0, 192) / length 192
ASSERT_EQ(size - 192,
image.write_zeroes(192, size - 192, 0U, 0));
diff.clear();
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
expected_diff = interval_set<uint64_t>{{{0, 192}}};
ASSERT_EQ(expected_diff, diff);
// zero an existing extent and truncate some off the end
// Now 1s from [64, 192) / length 192
ASSERT_EQ(64, image.write_zeroes(0, 64, 0U, 0));
diff.clear();
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
expected_diff = interval_set<uint64_t>{{{0, 192}}};
ASSERT_EQ(expected_diff, diff);
bufferlist expected_bl;
expected_bl.append_zero(64);
bufferlist sub_bl;
sub_bl.substr_of(bl, 0, 128);
expected_bl.claim_append(sub_bl);
expected_bl.append_zero(size - 192);
bufferlist read_bl;
EXPECT_EQ(size, image.read(0, size, read_bl));
EXPECT_EQ(expected_bl, read_bl);
ASSERT_EQ(0, image.close());
}
TEST_F(TestLibRBD, WriteZeroesThickProvision) {
librbd::RBD rbd;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
std::string name = get_temp_image_name();
int order = 0;
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, name.c_str(), NULL));
interval_set<uint64_t> diff;
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
auto expected_diff = interval_set<uint64_t>{{}};
ASSERT_EQ(expected_diff, diff);
// writes unaligned zeroes as a prepend
ASSERT_EQ(128, image.write_zeroes(
0, 128, RBD_WRITE_ZEROES_FLAG_THICK_PROVISION, 0));
diff.clear();
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
expected_diff = interval_set<uint64_t>{{{0, 128}}};
ASSERT_EQ(expected_diff, diff);
ASSERT_EQ(512, image.write_zeroes(
384, 512, RBD_WRITE_ZEROES_FLAG_THICK_PROVISION, 0));
diff.clear();
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
expected_diff = interval_set<uint64_t>{{{0, 896}}};
ASSERT_EQ(expected_diff, diff);
// prepend with write-same
ASSERT_EQ(640, image.write_zeroes(
896, 640, RBD_WRITE_ZEROES_FLAG_THICK_PROVISION, 0));
diff.clear();
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
expected_diff = interval_set<uint64_t>{{{0, 1536}}};
ASSERT_EQ(expected_diff, diff);
// write-same with append
ASSERT_EQ(640, image.write_zeroes(
1536, 640, RBD_WRITE_ZEROES_FLAG_THICK_PROVISION, 0));
diff.clear();
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
expected_diff = interval_set<uint64_t>{{{0, 2176}}};
ASSERT_EQ(expected_diff, diff);
// prepend + write-same + append
ASSERT_EQ(768, image.write_zeroes(
2176, 768, RBD_WRITE_ZEROES_FLAG_THICK_PROVISION, 0));
diff.clear();
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
expected_diff = interval_set<uint64_t>{{{0, 2944}}};
// write-same
ASSERT_EQ(1024, image.write_zeroes(
3072, 1024, RBD_WRITE_ZEROES_FLAG_THICK_PROVISION, 0));
diff.clear();
ASSERT_EQ(0, image.diff_iterate2(nullptr, 0, size, false, false,
iterate_cb, (void *)&diff));
expected_diff = interval_set<uint64_t>{{{0, 4096}}};
bufferlist expected_bl;
expected_bl.append_zero(size);
bufferlist read_bl;
EXPECT_EQ(size, image.read(0, size, read_bl));
EXPECT_EQ(expected_bl, read_bl);
ASSERT_EQ(0, image.close());
}
TEST_F(TestLibRBD, ConcurrentOperations)
{
SKIP_IF_CRIMSON();
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::RBD rbd;
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(m_pool_name.c_str(), ioctx));
std::string name = get_temp_image_name();
int order = 0;
uint64_t size = 2 << 20;
ASSERT_EQ(0, create_image_pp(rbd, ioctx, name.c_str(), size, &order));
// Test creating/removing many snapshots simultaneously
std::vector<librbd::Image> images(10);
std::vector<librbd::RBD::AioCompletion *> comps;
for (auto &image : images) {
auto comp = new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, rbd.aio_open(ioctx, image, name.c_str(), NULL, comp));
comps.push_back(comp);
}
for (auto &comp : comps) {
ASSERT_EQ(0, comp->wait_for_complete());
ASSERT_EQ(1, comp->is_complete());
ASSERT_EQ(0, comp->get_return_value());
comp->release();
}
comps.clear();
std::vector<std::thread> threads;
int i = 0;
for (auto &image : images) {
std::string snap_name = "snap" + stringify(i++);
threads.emplace_back([&image, snap_name]() {
int r = image.snap_create(snap_name.c_str());
ceph_assert(r == 0);
});
}
for (auto &t : threads) {
t.join();
}
threads.clear();
i = 0;
for (auto &image : images) {
std::string snap_name = "snap" + stringify(i++);
threads.emplace_back([&image, snap_name](){
int r = image.snap_remove(snap_name.c_str());
ceph_assert(r == 0);
});
}
for (auto &t : threads) {
t.join();
}
threads.clear();
for (auto &image : images) {
auto comp = new librbd::RBD::AioCompletion(NULL, NULL);
ASSERT_EQ(0, image.aio_close(comp));
comps.push_back(comp);
}
for (auto &comp : comps) {
ASSERT_EQ(0, comp->wait_for_complete());
ASSERT_EQ(1, comp->is_complete());
ASSERT_EQ(0, comp->get_return_value());
comp->release();
}
comps.clear();
// Test shutdown
{
librbd::Image image1, image2, image3;
ASSERT_EQ(0, rbd.open(ioctx, image1, name.c_str(), NULL));
ASSERT_EQ(0, rbd.open(ioctx, image2, name.c_str(), NULL));
ASSERT_EQ(0, rbd.open(ioctx, image3, name.c_str(), NULL));
ASSERT_EQ(0, image1.lock_acquire(RBD_LOCK_MODE_EXCLUSIVE));
struct Watcher : public librbd::QuiesceWatchCtx {
size_t count = 0;
ceph::mutex lock = ceph::make_mutex("lock");
ceph::condition_variable cv;
void handle_quiesce() override {
std::unique_lock locker(lock);
count++;
cv.notify_one();
}
void handle_unquiesce() override {
}
bool wait_for_quiesce(size_t c) {
std::unique_lock locker(lock);
return cv.wait_for(locker, seconds(60),
[this, c]() { return count >= c; });
}
} watcher;
uint64_t handle;
ASSERT_EQ(0, image2.quiesce_watch(&watcher, &handle));
auto close1_comp = new librbd::RBD::AioCompletion(NULL, NULL);
std::thread create_snap1([&image1, close1_comp]() {
int r = image1.snap_create("snap1");
ceph_assert(r == 0);
r = image1.aio_close(close1_comp);
ceph_assert(r == 0);
});
ASSERT_TRUE(watcher.wait_for_quiesce(1));
std::thread create_snap2([&image2]() {
int r = image2.snap_create("snap2");
ceph_assert(r == 0);
});
std::thread create_snap3([&image3]() {
int r = image3.snap_create("snap3");
ceph_assert(r == 0);
});
image2.quiesce_complete(handle, 0);
create_snap1.join();
ASSERT_TRUE(watcher.wait_for_quiesce(2));
image2.quiesce_complete(handle, 0);
ASSERT_TRUE(watcher.wait_for_quiesce(3));
image2.quiesce_complete(handle, 0);
ASSERT_EQ(0, close1_comp->wait_for_complete());
ASSERT_EQ(1, close1_comp->is_complete());
ASSERT_EQ(0, close1_comp->get_return_value());
close1_comp->release();
create_snap2.join();
create_snap3.join();
ASSERT_EQ(0, image2.quiesce_unwatch(handle));
ASSERT_EQ(0, image2.snap_remove("snap1"));
ASSERT_EQ(0, image2.snap_remove("snap2"));
ASSERT_EQ(0, image2.snap_remove("snap3"));
}
ASSERT_EQ(0, rbd.remove(ioctx, name.c_str()));
ioctx.close();
}
// poorman's ceph_assert()
namespace ceph {
void __ceph_assert_fail(const char *assertion, const char *file, int line,
const char *func) {
ceph_abort();
}
}
#pragma GCC diagnostic pop
#pragma GCC diagnostic warning "-Wpragmas"
| 427,422 | 32.793722 | 130 |
cc
|
null |
ceph-main/src/test/librbd/test_main.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/rados/librados.hpp"
#include "global/global_context.h"
#include "test/librados/test.h"
#include "test/librados/test_cxx.h"
#include "gtest/gtest.h"
#include <iostream>
#include <string>
extern void register_test_librbd();
#ifdef TEST_LIBRBD_INTERNALS
extern void register_test_deep_copy();
extern void register_test_groups();
extern void register_test_image_watcher();
extern void register_test_internal();
extern void register_test_journal_entries();
extern void register_test_journal_replay();
extern void register_test_migration();
extern void register_test_mirroring();
extern void register_test_mirroring_watcher();
extern void register_test_object_map();
extern void register_test_operations();
extern void register_test_trash();
#endif // TEST_LIBRBD_INTERNALS
int main(int argc, char **argv)
{
setenv("RBD_FORCE_ALLOW_V1","1",1);
register_test_librbd();
#ifdef TEST_LIBRBD_INTERNALS
register_test_deep_copy();
register_test_groups();
register_test_image_watcher();
register_test_internal();
register_test_journal_entries();
register_test_journal_replay();
register_test_migration();
register_test_mirroring();
register_test_mirroring_watcher();
register_test_object_map();
register_test_operations();
register_test_trash();
#endif // TEST_LIBRBD_INTERNALS
::testing::InitGoogleTest(&argc, argv);
librados::Rados rados;
std::string result = connect_cluster_pp(rados);
if (result != "" ) {
std::cerr << result << std::endl;
return 1;
}
#ifdef TEST_LIBRBD_INTERNALS
g_ceph_context = reinterpret_cast<CephContext*>(rados.cct());
#endif // TEST_LIBRBD_INTERNALS
int r = rados.conf_set("lockdep", "true");
if (r < 0) {
std::cerr << "warning: failed to enable lockdep" << std::endl;
}
int seed = getpid();
std::cout << "seed " << seed << std::endl;
srand(seed);
return RUN_ALL_TESTS();
}
| 1,981 | 26.527778 | 70 |
cc
|
null |
ceph-main/src/test/librbd/test_mirroring.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2016 SUSE LINUX GmbH
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ImageState.h"
#include "librbd/ImageWatcher.h"
#include "librbd/internal.h"
#include "librbd/ObjectMap.h"
#include "librbd/Operations.h"
#include "librbd/api/Image.h"
#include "librbd/api/Namespace.h"
#include "librbd/io/AioCompletion.h"
#include "librbd/io/ImageRequest.h"
#include "librbd/journal/Types.h"
#include "librbd/mirror/snapshot/GetImageStateRequest.h"
#include "librbd/mirror/snapshot/RemoveImageStateRequest.h"
#include "librbd/mirror/snapshot/SetImageStateRequest.h"
#include "librbd/mirror/snapshot/UnlinkPeerRequest.h"
#include "journal/Journaler.h"
#include "journal/Settings.h"
#include "common/Cond.h"
#include <boost/scope_exit.hpp>
#include <boost/assign/list_of.hpp>
#include <utility>
#include <vector>
using namespace std;
void register_test_mirroring() {
}
namespace librbd {
static bool operator==(const mirror_peer_site_t& lhs,
const mirror_peer_site_t& rhs) {
return (lhs.uuid == rhs.uuid &&
lhs.direction == rhs.direction &&
lhs.site_name == rhs.site_name &&
lhs.client_name == rhs.client_name &&
lhs.last_seen == rhs.last_seen);
}
static std::ostream& operator<<(std::ostream& os,
const mirror_peer_site_t& rhs) {
os << "uuid=" << rhs.uuid << ", "
<< "direction=" << rhs.direction << ", "
<< "site_name=" << rhs.site_name << ", "
<< "client_name=" << rhs.client_name << ", "
<< "last_seen=" << rhs.last_seen;
return os;
}
};
class TestMirroring : public TestFixture {
public:
TestMirroring() {}
void TearDown() override {
unlock_image();
TestFixture::TearDown();
}
void SetUp() override {
ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), m_ioctx));
}
std::string image_name = "mirrorimg1";
int get_local_mirror_image_site_status(
const librbd::mirror_image_global_status_t& status,
librbd::mirror_image_site_status_t* local_status) {
auto it = std::find_if(status.site_statuses.begin(),
status.site_statuses.end(),
[](auto& site_status) {
return (site_status.mirror_uuid ==
RBD_MIRROR_IMAGE_STATUS_LOCAL_MIRROR_UUID);
});
if (it == status.site_statuses.end()) {
return -ENOENT;
}
*local_status = *it;
return 0;
}
void check_mirror_image_enable(
rbd_mirror_mode_t mirror_mode, uint64_t features, int expected_r,
rbd_mirror_image_state_t mirror_state,
rbd_mirror_image_mode_t mirror_image_mode = RBD_MIRROR_IMAGE_MODE_JOURNAL) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features, &order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, mirror_mode));
ASSERT_EQ(expected_r, image.mirror_image_enable2(mirror_image_mode));
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image, sizeof(mirror_image)));
ASSERT_EQ(mirror_state, mirror_image.state);
if (mirror_image.state == RBD_MIRROR_IMAGE_ENABLED) {
librbd::mirror_image_mode_t mode;
ASSERT_EQ(0, image.mirror_image_get_mode(&mode));
ASSERT_EQ(mirror_image_mode, mode);
}
librbd::mirror_image_global_status_t status;
ASSERT_EQ(0, image.mirror_image_get_global_status(&status, sizeof(status)));
librbd::mirror_image_site_status_t local_status;
ASSERT_EQ(0, get_local_mirror_image_site_status(status, &local_status));
ASSERT_EQ(MIRROR_IMAGE_STATUS_STATE_UNKNOWN, local_status.state);
std::string instance_id;
ASSERT_EQ(mirror_state == RBD_MIRROR_IMAGE_ENABLED ? -ENOENT : -EINVAL,
image.mirror_image_get_instance_id(&instance_id));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
void check_mirror_image_disable(rbd_mirror_mode_t mirror_mode,
uint64_t features,
int expected_r,
rbd_mirror_image_state_t mirror_state) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_POOL));
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features, &order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, mirror_mode));
ASSERT_EQ(expected_r, image.mirror_image_disable(false));
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image, sizeof(mirror_image)));
ASSERT_EQ(mirror_state, mirror_image.state);
librbd::mirror_image_global_status_t status;
ASSERT_EQ(0, image.mirror_image_get_global_status(&status, sizeof(status)));
librbd::mirror_image_site_status_t local_status;
ASSERT_EQ(0, get_local_mirror_image_site_status(status, &local_status));
ASSERT_EQ(MIRROR_IMAGE_STATUS_STATE_UNKNOWN, local_status.state);
std::string instance_id;
ASSERT_EQ(mirror_state == RBD_MIRROR_IMAGE_ENABLED ? -ENOENT : -EINVAL,
image.mirror_image_get_instance_id(&instance_id));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
void check_mirroring_status(size_t *images_count) {
std::map<std::string, librbd::mirror_image_global_status_t> images;
ASSERT_EQ(0, m_rbd.mirror_image_global_status_list(m_ioctx, "", 4096,
&images));
std::map<librbd::mirror_image_status_state_t, int> states;
ASSERT_EQ(0, m_rbd.mirror_image_status_summary(m_ioctx, &states));
size_t states_count = 0;
for (auto &s : states) {
states_count += s.second;
}
ASSERT_EQ(images.size(), states_count);
*images_count = images.size();
std::map<std::string, std::string> instance_ids;
ASSERT_EQ(0, m_rbd.mirror_image_instance_id_list(m_ioctx, "", 4096,
&instance_ids));
ASSERT_TRUE(instance_ids.empty());
}
void check_mirroring_on_create(uint64_t features,
rbd_mirror_mode_t mirror_mode,
rbd_mirror_image_state_t mirror_state) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, mirror_mode));
size_t mirror_images_count = 0;
check_mirroring_status(&mirror_images_count);
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features, &order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image, sizeof(mirror_image)));
ASSERT_EQ(mirror_state, mirror_image.state);
librbd::mirror_image_global_status_t status;
ASSERT_EQ(0, image.mirror_image_get_global_status(&status, sizeof(status)));
librbd::mirror_image_site_status_t local_status;
ASSERT_EQ(0, get_local_mirror_image_site_status(status, &local_status));
ASSERT_EQ(MIRROR_IMAGE_STATUS_STATE_UNKNOWN, local_status.state);
size_t mirror_images_new_count = 0;
check_mirroring_status(&mirror_images_new_count);
if (mirror_mode == RBD_MIRROR_MODE_POOL &&
mirror_state == RBD_MIRROR_IMAGE_ENABLED) {
ASSERT_EQ(mirror_images_new_count, mirror_images_count + 1);
} else {
ASSERT_EQ(mirror_images_new_count, mirror_images_count);
}
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
check_mirroring_status(&mirror_images_new_count);
ASSERT_EQ(mirror_images_new_count, mirror_images_count);
}
void check_mirroring_on_update_features(
uint64_t init_features, bool enable, bool enable_mirroring,
uint64_t features, int expected_r, rbd_mirror_mode_t mirror_mode,
rbd_mirror_image_state_t mirror_state,
rbd_mirror_image_mode_t mirror_image_mode = RBD_MIRROR_IMAGE_MODE_JOURNAL) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, mirror_mode));
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, init_features, &order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
if (enable_mirroring) {
ASSERT_EQ(0, image.mirror_image_enable2(mirror_image_mode));
}
ASSERT_EQ(expected_r, image.update_features(features, enable));
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image, sizeof(mirror_image)));
ASSERT_EQ(mirror_state, mirror_image.state);
if (mirror_image.state == RBD_MIRROR_IMAGE_ENABLED) {
librbd::mirror_image_mode_t mode;
ASSERT_EQ(0, image.mirror_image_get_mode(&mode));
ASSERT_EQ(mirror_image_mode, mode);
}
librbd::mirror_image_global_status_t status;
ASSERT_EQ(0, image.mirror_image_get_global_status(&status, sizeof(status)));
librbd::mirror_image_site_status_t local_status;
ASSERT_EQ(0, get_local_mirror_image_site_status(status, &local_status));
ASSERT_EQ(MIRROR_IMAGE_STATUS_STATE_UNKNOWN, local_status.state);
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
void setup_images_with_mirror_mode(rbd_mirror_mode_t mirror_mode,
std::vector<uint64_t>& features_vec) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, mirror_mode));
int id = 1;
int order = 20;
for (const auto& features : features_vec) {
std::stringstream img_name("img_");
img_name << id++;
std::string img_name_str = img_name.str();
ASSERT_EQ(0, m_rbd.create2(m_ioctx, img_name_str.c_str(), 2048, features, &order));
}
}
void check_mirroring_on_mirror_mode_set(rbd_mirror_mode_t mirror_mode,
std::vector<rbd_mirror_image_state_t>& states_vec) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, mirror_mode));
std::vector< std::tuple<std::string, rbd_mirror_image_state_t> > images;
int id = 1;
for (const auto& mirror_state : states_vec) {
std::stringstream img_name("img_");
img_name << id++;
std::string img_name_str = img_name.str();
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, img_name_str.c_str()));
images.push_back(std::make_tuple(img_name_str, mirror_state));
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image, sizeof(mirror_image)));
ASSERT_EQ(mirror_state, mirror_image.state);
librbd::mirror_image_global_status_t status;
ASSERT_EQ(0, image.mirror_image_get_global_status(&status,
sizeof(status)));
librbd::mirror_image_site_status_t local_status;
ASSERT_EQ(0, get_local_mirror_image_site_status(status, &local_status));
ASSERT_EQ(MIRROR_IMAGE_STATUS_STATE_UNKNOWN, local_status.state);
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, img_name_str.c_str()));
}
}
void check_remove_image(rbd_mirror_mode_t mirror_mode, uint64_t features,
bool enable_mirroring, bool demote = false) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, mirror_mode));
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
if (enable_mirroring) {
ASSERT_EQ(0, image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_JOURNAL));
}
if (demote) {
ASSERT_EQ(0, image.mirror_image_demote());
ASSERT_EQ(0, image.mirror_image_disable(true));
}
image.close();
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
void check_trash_move_restore(rbd_mirror_mode_t mirror_mode,
bool enable_mirroring) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, mirror_mode));
int order = 20;
uint64_t features = RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
if (enable_mirroring) {
ASSERT_EQ(0, image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_JOURNAL));
}
std::string image_id;
ASSERT_EQ(0, image.get_id(&image_id));
image.close();
ASSERT_EQ(0, m_rbd.trash_move(m_ioctx, image_name.c_str(), 100));
ASSERT_EQ(0, m_rbd.open_by_id(m_ioctx, image, image_id.c_str(), NULL));
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image, sizeof(mirror_image)));
ASSERT_EQ(mirror_image.state, RBD_MIRROR_IMAGE_DISABLED);
ASSERT_EQ(0, m_rbd.trash_restore(m_ioctx, image_id.c_str(), ""));
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image, sizeof(mirror_image)));
if (mirror_mode == RBD_MIRROR_MODE_POOL) {
ASSERT_EQ(mirror_image.state, RBD_MIRROR_IMAGE_ENABLED);
} else {
ASSERT_EQ(mirror_image.state, RBD_MIRROR_IMAGE_DISABLED);
}
image.close();
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
void setup_mirror_peer(librados::IoCtx &io_ctx, librbd::Image &image) {
ASSERT_EQ(0, image.snap_create("sync-point-snap"));
std::string image_id;
ASSERT_EQ(0, get_image_id(image, &image_id));
librbd::journal::MirrorPeerClientMeta peer_client_meta(
"remote-image-id", {{{}, "sync-point-snap", boost::none}}, {});
librbd::journal::ClientData client_data(peer_client_meta);
journal::Journaler journaler(io_ctx, image_id, "peer-client", {}, nullptr);
C_SaferCond init_ctx;
journaler.init(&init_ctx);
ASSERT_EQ(-ENOENT, init_ctx.wait());
bufferlist client_data_bl;
encode(client_data, client_data_bl);
ASSERT_EQ(0, journaler.register_client(client_data_bl));
C_SaferCond shut_down_ctx;
journaler.shut_down(&shut_down_ctx);
ASSERT_EQ(0, shut_down_ctx.wait());
}
};
TEST_F(TestMirroring, EnableImageMirror_In_MirrorModeImage) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirror_image_enable(RBD_MIRROR_MODE_IMAGE, features, 0,
RBD_MIRROR_IMAGE_ENABLED, RBD_MIRROR_IMAGE_MODE_JOURNAL);
}
TEST_F(TestMirroring, EnableImageMirror_In_MirrorModePool) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirror_image_enable(RBD_MIRROR_MODE_POOL, features, -EINVAL,
RBD_MIRROR_IMAGE_ENABLED, RBD_MIRROR_IMAGE_MODE_JOURNAL);
}
TEST_F(TestMirroring, EnableImageMirror_In_MirrorModeDisabled) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirror_image_enable(RBD_MIRROR_MODE_DISABLED, features, -EINVAL,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, DisableImageMirror_In_MirrorModeImage) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirror_image_disable(RBD_MIRROR_MODE_IMAGE, features, 0,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, DisableImageMirror_In_MirrorModeImage_NoObjectMap) {
uint64_t features = 0;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirror_image_disable(RBD_MIRROR_MODE_IMAGE, features, 0,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, DisableImageMirror_In_MirrorModePool) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirror_image_disable(RBD_MIRROR_MODE_POOL, features, -EINVAL,
RBD_MIRROR_IMAGE_ENABLED);
}
TEST_F(TestMirroring, DisableImageMirror_In_MirrorModeDisabled) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirror_image_disable(RBD_MIRROR_MODE_DISABLED, features, -EINVAL,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, DisableImageMirrorWithPeer) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
uint64_t features = RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING;
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
ASSERT_EQ(0, image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_JOURNAL));
setup_mirror_peer(m_ioctx, image);
ASSERT_EQ(0, image.mirror_image_disable(false));
std::vector<librbd::snap_info_t> snaps;
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_TRUE(snaps.empty());
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image,
sizeof(mirror_image)));
ASSERT_EQ(RBD_MIRROR_IMAGE_DISABLED, mirror_image.state);
librbd::mirror_image_global_status_t status;
ASSERT_EQ(0, image.mirror_image_get_global_status(&status, sizeof(status)));
librbd::mirror_image_site_status_t local_status;
ASSERT_EQ(0, get_local_mirror_image_site_status(status, &local_status));
ASSERT_EQ(MIRROR_IMAGE_STATUS_STATE_UNKNOWN, local_status.state);
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestMirroring, DisableJournalingWithPeer) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_POOL));
uint64_t features = RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING;
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
setup_mirror_peer(m_ioctx, image);
ASSERT_EQ(0, image.update_features(RBD_FEATURE_JOURNALING, false));
std::vector<librbd::snap_info_t> snaps;
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_TRUE(snaps.empty());
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image,
sizeof(mirror_image)));
ASSERT_EQ(RBD_MIRROR_IMAGE_DISABLED, mirror_image.state);
librbd::mirror_image_global_status_t status;
ASSERT_EQ(0, image.mirror_image_get_global_status(&status, sizeof(status)));
librbd::mirror_image_site_status_t local_status;
ASSERT_EQ(0, get_local_mirror_image_site_status(status, &local_status));
ASSERT_EQ(MIRROR_IMAGE_STATUS_STATE_UNKNOWN, local_status.state);
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestMirroring, EnableImageMirror_In_MirrorModeDisabled_WithoutJournaling) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
check_mirror_image_enable(RBD_MIRROR_MODE_DISABLED, features, -EINVAL,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, EnableImageMirror_In_MirrorModePool_WithoutJournaling) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
check_mirror_image_enable(RBD_MIRROR_MODE_POOL, features, -EINVAL,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, EnableImageMirror_In_MirrorModeImage_WithoutJournaling) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
check_mirror_image_enable(RBD_MIRROR_MODE_IMAGE, features, 0,
RBD_MIRROR_IMAGE_ENABLED, RBD_MIRROR_IMAGE_MODE_SNAPSHOT);
}
TEST_F(TestMirroring, EnableImageMirror_In_MirrorModeImage_WithoutExclusiveLock) {
uint64_t features = 0;
check_mirror_image_enable(RBD_MIRROR_MODE_IMAGE, features, 0,
RBD_MIRROR_IMAGE_ENABLED, RBD_MIRROR_IMAGE_MODE_SNAPSHOT);
}
TEST_F(TestMirroring, CreateImage_In_MirrorModeDisabled) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirroring_on_create(features, RBD_MIRROR_MODE_DISABLED,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, CreateImage_In_MirrorModeImage) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirroring_on_create(features, RBD_MIRROR_MODE_IMAGE,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, CreateImage_In_MirrorModePool) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
features |= RBD_FEATURE_JOURNALING;
check_mirroring_on_create(features, RBD_MIRROR_MODE_POOL,
RBD_MIRROR_IMAGE_ENABLED);
}
TEST_F(TestMirroring, CreateImage_In_MirrorModePool_WithoutJournaling) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
check_mirroring_on_create(features, RBD_MIRROR_MODE_POOL,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, CreateImage_In_MirrorModeImage_WithoutJournaling) {
uint64_t features = 0;
features |= RBD_FEATURE_OBJECT_MAP;
features |= RBD_FEATURE_EXCLUSIVE_LOCK;
check_mirroring_on_create(features, RBD_MIRROR_MODE_IMAGE,
RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, EnableJournaling_In_MirrorModeDisabled) {
uint64_t init_features = 0;
init_features |= RBD_FEATURE_OBJECT_MAP;
init_features |= RBD_FEATURE_EXCLUSIVE_LOCK;
uint64_t features = RBD_FEATURE_JOURNALING;
check_mirroring_on_update_features(init_features, true, false, features, 0,
RBD_MIRROR_MODE_DISABLED, RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, EnableJournaling_In_MirrorModeImage) {
uint64_t init_features = 0;
init_features |= RBD_FEATURE_OBJECT_MAP;
init_features |= RBD_FEATURE_EXCLUSIVE_LOCK;
uint64_t features = RBD_FEATURE_JOURNALING;
check_mirroring_on_update_features(init_features, true, false, features, 0,
RBD_MIRROR_MODE_IMAGE, RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, EnableJournaling_In_MirrorModeImage_SnapshotMirroringEnabled) {
uint64_t init_features = 0;
init_features |= RBD_FEATURE_OBJECT_MAP;
init_features |= RBD_FEATURE_EXCLUSIVE_LOCK;
uint64_t features = RBD_FEATURE_JOURNALING;
check_mirroring_on_update_features(init_features, true, true, features,
0, RBD_MIRROR_MODE_IMAGE, RBD_MIRROR_IMAGE_ENABLED,
RBD_MIRROR_IMAGE_MODE_SNAPSHOT);
}
TEST_F(TestMirroring, EnableJournaling_In_MirrorModePool) {
uint64_t init_features = 0;
init_features |= RBD_FEATURE_OBJECT_MAP;
init_features |= RBD_FEATURE_EXCLUSIVE_LOCK;
uint64_t features = RBD_FEATURE_JOURNALING;
check_mirroring_on_update_features(init_features, true, false, features, 0,
RBD_MIRROR_MODE_POOL, RBD_MIRROR_IMAGE_ENABLED,
RBD_MIRROR_IMAGE_MODE_JOURNAL);
}
TEST_F(TestMirroring, DisableJournaling_In_MirrorModePool) {
uint64_t init_features = 0;
init_features |= RBD_FEATURE_OBJECT_MAP;
init_features |= RBD_FEATURE_EXCLUSIVE_LOCK;
init_features |= RBD_FEATURE_JOURNALING;
uint64_t features = RBD_FEATURE_JOURNALING;
check_mirroring_on_update_features(init_features, false, false, features, 0,
RBD_MIRROR_MODE_POOL, RBD_MIRROR_IMAGE_DISABLED);
}
TEST_F(TestMirroring, DisableJournaling_In_MirrorModeImage) {
uint64_t init_features = 0;
init_features |= RBD_FEATURE_OBJECT_MAP;
init_features |= RBD_FEATURE_EXCLUSIVE_LOCK;
init_features |= RBD_FEATURE_JOURNALING;
uint64_t features = RBD_FEATURE_JOURNALING;
check_mirroring_on_update_features(init_features, false, true, features,
-EINVAL, RBD_MIRROR_MODE_IMAGE, RBD_MIRROR_IMAGE_ENABLED,
RBD_MIRROR_IMAGE_MODE_JOURNAL);
}
TEST_F(TestMirroring, MirrorModeSet_DisabledMode_To_PoolMode) {
std::vector<uint64_t> features_vec;
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK);
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING);
setup_images_with_mirror_mode(RBD_MIRROR_MODE_DISABLED, features_vec);
std::vector<rbd_mirror_image_state_t> states_vec;
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
states_vec.push_back(RBD_MIRROR_IMAGE_ENABLED);
check_mirroring_on_mirror_mode_set(RBD_MIRROR_MODE_POOL, states_vec);
}
TEST_F(TestMirroring, MirrorModeSet_PoolMode_To_DisabledMode) {
std::vector<uint64_t> features_vec;
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK);
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING);
setup_images_with_mirror_mode(RBD_MIRROR_MODE_POOL, features_vec);
std::vector<rbd_mirror_image_state_t> states_vec;
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
check_mirroring_on_mirror_mode_set(RBD_MIRROR_MODE_DISABLED, states_vec);
}
TEST_F(TestMirroring, MirrorModeSet_DisabledMode_To_ImageMode) {
std::vector<uint64_t> features_vec;
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK);
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING);
setup_images_with_mirror_mode(RBD_MIRROR_MODE_DISABLED, features_vec);
std::vector<rbd_mirror_image_state_t> states_vec;
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
check_mirroring_on_mirror_mode_set(RBD_MIRROR_MODE_IMAGE, states_vec);
}
TEST_F(TestMirroring, MirrorModeSet_PoolMode_To_ImageMode) {
std::vector<uint64_t> features_vec;
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK);
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING);
setup_images_with_mirror_mode(RBD_MIRROR_MODE_POOL, features_vec);
std::vector<rbd_mirror_image_state_t> states_vec;
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
states_vec.push_back(RBD_MIRROR_IMAGE_ENABLED);
check_mirroring_on_mirror_mode_set(RBD_MIRROR_MODE_IMAGE, states_vec);
}
TEST_F(TestMirroring, MirrorModeSet_ImageMode_To_PoolMode) {
std::vector<uint64_t> features_vec;
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK);
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING);
setup_images_with_mirror_mode(RBD_MIRROR_MODE_IMAGE, features_vec);
std::vector<rbd_mirror_image_state_t> states_vec;
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
states_vec.push_back(RBD_MIRROR_IMAGE_ENABLED);
check_mirroring_on_mirror_mode_set(RBD_MIRROR_MODE_POOL, states_vec);
}
TEST_F(TestMirroring, MirrorModeSet_ImageMode_To_DisabledMode) {
std::vector<uint64_t> features_vec;
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK);
features_vec.push_back(RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING);
setup_images_with_mirror_mode(RBD_MIRROR_MODE_POOL, features_vec);
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(-EINVAL, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_POOL));
std::vector<rbd_mirror_image_state_t> states_vec;
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
states_vec.push_back(RBD_MIRROR_IMAGE_DISABLED);
check_mirroring_on_mirror_mode_set(RBD_MIRROR_MODE_DISABLED, states_vec);
}
TEST_F(TestMirroring, RemoveImage_With_MirrorImageEnabled) {
check_remove_image(RBD_MIRROR_MODE_IMAGE,
RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING,
true);
}
TEST_F(TestMirroring, RemoveImage_With_MirrorImageDisabled) {
check_remove_image(RBD_MIRROR_MODE_IMAGE,
RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING,
false);
}
TEST_F(TestMirroring, RemoveImage_With_ImageWithoutJournal) {
check_remove_image(RBD_MIRROR_MODE_IMAGE,
RBD_FEATURE_EXCLUSIVE_LOCK,
false);
}
TEST_F(TestMirroring, RemoveImage_With_MirrorImageDemoted) {
check_remove_image(RBD_MIRROR_MODE_IMAGE,
RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING,
true, true);
}
TEST_F(TestMirroring, TrashMoveRestore_PoolMode) {
check_trash_move_restore(RBD_MIRROR_MODE_POOL, false);
}
TEST_F(TestMirroring, TrashMoveRestore_ImageMode_MirroringDisabled) {
check_trash_move_restore(RBD_MIRROR_MODE_IMAGE, false);
}
TEST_F(TestMirroring, TrashMoveRestore_ImageMode_MirroringEnabled) {
check_trash_move_restore(RBD_MIRROR_MODE_IMAGE, true);
}
TEST_F(TestMirroring, MirrorStatusList) {
std::vector<uint64_t>
features_vec(5, RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING);
setup_images_with_mirror_mode(RBD_MIRROR_MODE_POOL, features_vec);
std::string last_read = "";
std::map<std::string, librbd::mirror_image_global_status_t> images;
ASSERT_EQ(0, m_rbd.mirror_image_global_status_list(m_ioctx, last_read, 2,
&images));
ASSERT_EQ(2U, images.size());
last_read = images.rbegin()->first;
images.clear();
ASSERT_EQ(0, m_rbd.mirror_image_global_status_list(m_ioctx, last_read, 2,
&images));
ASSERT_EQ(2U, images.size());
last_read = images.rbegin()->first;
images.clear();
ASSERT_EQ(0, m_rbd.mirror_image_global_status_list(m_ioctx, last_read, 4096,
&images));
ASSERT_EQ(1U, images.size());
last_read = images.rbegin()->first;
images.clear();
ASSERT_EQ(0, m_rbd.mirror_image_global_status_list(m_ioctx, last_read, 4096,
&images));
ASSERT_EQ(0U, images.size());
}
TEST_F(TestMirroring, RemoveBootstrapped)
{
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_POOL));
uint64_t features = RBD_FEATURE_EXCLUSIVE_LOCK | RBD_FEATURE_JOURNALING;
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
librbd::NoOpProgressContext no_op;
ASSERT_EQ(-EBUSY, librbd::api::Image<>::remove(m_ioctx, image_name, no_op));
// simulate the image is open by rbd-mirror bootstrap
uint64_t handle;
struct MirrorWatcher : public librados::WatchCtx2 {
explicit MirrorWatcher(librados::IoCtx &ioctx) : m_ioctx(ioctx) {
}
void handle_notify(uint64_t notify_id, uint64_t cookie,
uint64_t notifier_id, bufferlist& bl) override {
// received IMAGE_UPDATED notification from remove
m_notified = true;
m_ioctx.notify_ack(RBD_MIRRORING, notify_id, cookie, bl);
}
void handle_error(uint64_t cookie, int err) override {
}
librados::IoCtx &m_ioctx;
bool m_notified = false;
} watcher(m_ioctx);
ASSERT_EQ(0, m_ioctx.create(RBD_MIRRORING, false));
ASSERT_EQ(0, m_ioctx.watch2(RBD_MIRRORING, &handle, &watcher));
// now remove should succeed
ASSERT_EQ(0, librbd::api::Image<>::remove(m_ioctx, image_name, no_op));
ASSERT_EQ(0, m_ioctx.unwatch2(handle));
ASSERT_TRUE(watcher.m_notified);
ASSERT_EQ(0, image.close());
}
TEST_F(TestMirroring, AioPromoteDemote) {
std::list<std::string> image_names;
for (size_t idx = 0; idx < 10; ++idx) {
image_names.push_back(get_temp_image_name());
}
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
// create mirror images
int order = 20;
std::list<librbd::Image> images;
for (auto &image_name : image_names) {
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 2048,
RBD_FEATURE_EXCLUSIVE_LOCK |
RBD_FEATURE_JOURNALING,
&order));
images.emplace_back();
ASSERT_EQ(0, m_rbd.open(m_ioctx, images.back(), image_name.c_str()));
ASSERT_EQ(0, images.back().mirror_image_enable2(
RBD_MIRROR_IMAGE_MODE_JOURNAL));
}
// demote all images
std::list<librbd::RBD::AioCompletion *> aio_comps;
for (auto &image : images) {
aio_comps.push_back(new librbd::RBD::AioCompletion(nullptr, nullptr));
ASSERT_EQ(0, image.aio_mirror_image_demote(aio_comps.back()));
}
for (auto aio_comp : aio_comps) {
ASSERT_EQ(0, aio_comp->wait_for_complete());
ASSERT_EQ(1, aio_comp->is_complete());
ASSERT_EQ(0, aio_comp->get_return_value());
aio_comp->release();
}
aio_comps.clear();
// verify demotions
for (auto &image : images) {
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image,
sizeof(mirror_image)));
ASSERT_FALSE(mirror_image.primary);
}
// promote all images
for (auto &image : images) {
aio_comps.push_back(new librbd::RBD::AioCompletion(nullptr, nullptr));
ASSERT_EQ(0, image.aio_mirror_image_promote(false, aio_comps.back()));
}
for (auto aio_comp : aio_comps) {
ASSERT_EQ(0, aio_comp->wait_for_complete());
ASSERT_EQ(1, aio_comp->is_complete());
ASSERT_EQ(0, aio_comp->get_return_value());
aio_comp->release();
}
// verify promotions
for (auto &image : images) {
librbd::mirror_image_info_t mirror_image;
ASSERT_EQ(0, image.mirror_image_get_info(&mirror_image,
sizeof(mirror_image)));
ASSERT_TRUE(mirror_image.primary);
}
}
TEST_F(TestMirroring, AioGetInfo) {
std::list<std::string> image_names;
for (size_t idx = 0; idx < 10; ++idx) {
image_names.push_back(get_temp_image_name());
}
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_POOL));
// create mirror images
int order = 20;
std::list<librbd::Image> images;
for (auto &image_name : image_names) {
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 2048,
RBD_FEATURE_EXCLUSIVE_LOCK |
RBD_FEATURE_JOURNALING,
&order));
images.emplace_back();
ASSERT_EQ(0, m_rbd.open(m_ioctx, images.back(), image_name.c_str()));
}
std::list<librbd::RBD::AioCompletion *> aio_comps;
std::list<librbd::mirror_image_info_t> infos;
for (auto &image : images) {
aio_comps.push_back(new librbd::RBD::AioCompletion(nullptr, nullptr));
infos.emplace_back();
ASSERT_EQ(0, image.aio_mirror_image_get_info(&infos.back(),
sizeof(infos.back()),
aio_comps.back()));
}
for (auto aio_comp : aio_comps) {
ASSERT_EQ(0, aio_comp->wait_for_complete());
ASSERT_EQ(1, aio_comp->is_complete());
ASSERT_EQ(0, aio_comp->get_return_value());
aio_comp->release();
}
aio_comps.clear();
for (auto &info : infos) {
ASSERT_NE("", info.global_id);
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, info.state);
ASSERT_TRUE(info.primary);
}
}
TEST_F(TestMirroring, AioGetStatus) {
std::list<std::string> image_names;
for (size_t idx = 0; idx < 10; ++idx) {
image_names.push_back(get_temp_image_name());
}
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_POOL));
// create mirror images
int order = 20;
std::list<librbd::Image> images;
for (auto &image_name : image_names) {
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 2048,
RBD_FEATURE_EXCLUSIVE_LOCK |
RBD_FEATURE_JOURNALING,
&order));
images.emplace_back();
ASSERT_EQ(0, m_rbd.open(m_ioctx, images.back(), image_name.c_str()));
}
std::list<librbd::RBD::AioCompletion *> aio_comps;
std::list<librbd::mirror_image_global_status_t> statuses;
for (auto &image : images) {
aio_comps.push_back(new librbd::RBD::AioCompletion(nullptr, nullptr));
statuses.emplace_back();
ASSERT_EQ(0, image.aio_mirror_image_get_global_status(
&statuses.back(), sizeof(statuses.back()),
aio_comps.back()));
}
for (auto aio_comp : aio_comps) {
ASSERT_EQ(0, aio_comp->wait_for_complete());
ASSERT_EQ(1, aio_comp->is_complete());
ASSERT_EQ(0, aio_comp->get_return_value());
aio_comp->release();
}
aio_comps.clear();
for (auto &status : statuses) {
ASSERT_NE("", status.name);
ASSERT_NE("", status.info.global_id);
ASSERT_EQ(RBD_MIRROR_IMAGE_ENABLED, status.info.state);
ASSERT_TRUE(status.info.primary);
librbd::mirror_image_site_status_t local_status;
ASSERT_EQ(0, get_local_mirror_image_site_status(status, &local_status));
ASSERT_EQ(MIRROR_IMAGE_STATUS_STATE_UNKNOWN, local_status.state);
ASSERT_EQ("status not found", local_status.description);
ASSERT_FALSE(local_status.up);
ASSERT_EQ(0, local_status.last_update);
}
}
TEST_F(TestMirroring, SiteName) {
REQUIRE(!is_librados_test_stub(_rados));
const std::string expected_site_name("us-east-1a");
ASSERT_EQ(0, m_rbd.mirror_site_name_set(_rados, expected_site_name));
std::string site_name;
ASSERT_EQ(0, m_rbd.mirror_site_name_get(_rados, &site_name));
ASSERT_EQ(expected_site_name, site_name);
ASSERT_EQ(0, m_rbd.mirror_site_name_set(_rados, ""));
std::string fsid;
ASSERT_EQ(0, _rados.cluster_fsid(&fsid));
ASSERT_EQ(0, m_rbd.mirror_site_name_get(_rados, &site_name));
ASSERT_EQ(fsid, site_name);
}
TEST_F(TestMirroring, Bootstrap) {
REQUIRE(!is_librados_test_stub(_rados));
std::string token_b64;
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
ASSERT_EQ(-EINVAL, m_rbd.mirror_peer_bootstrap_create(m_ioctx, &token_b64));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_POOL));
ASSERT_EQ(0, m_rbd.mirror_peer_bootstrap_create(m_ioctx, &token_b64));
bufferlist token_b64_bl;
token_b64_bl.append(token_b64);
bufferlist token_bl;
token_bl.decode_base64(token_b64_bl);
// cannot import token into same cluster
ASSERT_EQ(-EINVAL,
m_rbd.mirror_peer_bootstrap_import(
m_ioctx, RBD_MIRROR_PEER_DIRECTION_RX, token_b64));
}
TEST_F(TestMirroring, PeerDirection) {
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_POOL));
std::string uuid;
ASSERT_EQ(-EINVAL, m_rbd.mirror_peer_site_add(
m_ioctx, &uuid, RBD_MIRROR_PEER_DIRECTION_TX, "siteA",
"client.admin"));
ASSERT_EQ(0, m_rbd.mirror_peer_site_add(m_ioctx, &uuid,
RBD_MIRROR_PEER_DIRECTION_RX_TX,
"siteA", "client.admin"));
std::vector<librbd::mirror_peer_site_t> peers;
ASSERT_EQ(0, m_rbd.mirror_peer_site_list(m_ioctx, &peers));
std::vector<librbd::mirror_peer_site_t> expected_peers = {
{uuid, RBD_MIRROR_PEER_DIRECTION_RX_TX, "siteA", "", "client.admin", 0}};
ASSERT_EQ(expected_peers, peers);
ASSERT_EQ(0, m_rbd.mirror_peer_site_set_direction(
m_ioctx, uuid, RBD_MIRROR_PEER_DIRECTION_RX));
ASSERT_EQ(0, m_rbd.mirror_peer_site_list(m_ioctx, &peers));
expected_peers = {
{uuid, RBD_MIRROR_PEER_DIRECTION_RX, "siteA", "", "client.admin", 0}};
ASSERT_EQ(expected_peers, peers);
ASSERT_EQ(0, m_rbd.mirror_peer_site_remove(m_ioctx, uuid));
}
TEST_F(TestMirroring, Snapshot)
{
REQUIRE_FORMAT_V2();
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
uint64_t features;
ASSERT_TRUE(get_features(&features));
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
ASSERT_EQ(0, image.metadata_set(
"conf_rbd_mirroring_max_mirroring_snapshots", "5"));
uint64_t snap_id;
ASSERT_EQ(-EINVAL, image.mirror_image_create_snapshot(&snap_id));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(-EINVAL, image.mirror_image_create_snapshot(&snap_id));
ASSERT_EQ(0, image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_SNAPSHOT));
librbd::mirror_image_mode_t mode;
ASSERT_EQ(0, image.mirror_image_get_mode(&mode));
ASSERT_EQ(RBD_MIRROR_IMAGE_MODE_SNAPSHOT, mode);
ASSERT_EQ(-EINVAL, image.mirror_image_create_snapshot(&snap_id));
std::string peer_uuid;
ASSERT_EQ(0, m_rbd.mirror_peer_site_add(m_ioctx, &peer_uuid,
RBD_MIRROR_PEER_DIRECTION_RX_TX,
"cluster", "client"));
// The mirroring was enabled when no peer was configured. Therefore, the
// initial snapshot has no peers linked and will be removed after the
// creation of a new mirror snapshot.
ASSERT_EQ(0, image.mirror_image_create_snapshot(&snap_id));
vector<librbd::snap_info_t> snaps;
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_EQ(1U, snaps.size());
ASSERT_EQ(snaps[0].id, snap_id);
for (int i = 0; i < 5; i++) {
ASSERT_EQ(0, image.mirror_image_create_snapshot(&snap_id));
}
snaps.clear();
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_EQ(5U, snaps.size());
ASSERT_EQ(snaps[4].id, snap_id);
// automatic peer unlink on max_mirroring_snapshots reached
ASSERT_EQ(0, image.mirror_image_create_snapshot(&snap_id));
vector<librbd::snap_info_t> snaps1;
ASSERT_EQ(0, image.snap_list(snaps1));
ASSERT_EQ(5U, snaps1.size());
ASSERT_EQ(snaps1[0].id, snaps[0].id);
ASSERT_EQ(snaps1[1].id, snaps[1].id);
ASSERT_EQ(snaps1[2].id, snaps[2].id);
ASSERT_EQ(snaps1[3].id, snaps[3].id);
ASSERT_EQ(snaps1[4].id, snap_id);
librbd::snap_namespace_type_t snap_ns_type;
ASSERT_EQ(0, image.snap_get_namespace_type(snap_id, &snap_ns_type));
ASSERT_EQ(RBD_SNAP_NAMESPACE_TYPE_MIRROR, snap_ns_type);
librbd::snap_mirror_namespace_t mirror_snap;
ASSERT_EQ(0, image.snap_get_mirror_namespace(snap_id, &mirror_snap,
sizeof(mirror_snap)));
ASSERT_EQ(1U, mirror_snap.mirror_peer_uuids.size());
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer_uuid));
for (auto &snap : snaps1) {
ASSERT_EQ(0, image.snap_remove_by_id(snap.id));
}
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_peer_site_remove(m_ioctx, peer_uuid));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestMirroring, SnapshotRemoveOnDisable)
{
REQUIRE_FORMAT_V2();
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
std::string peer_uuid;
ASSERT_EQ(0, m_rbd.mirror_peer_site_add(m_ioctx, &peer_uuid,
RBD_MIRROR_PEER_DIRECTION_RX_TX,
"cluster", "client"));
uint64_t features;
ASSERT_TRUE(get_features(&features));
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
ASSERT_EQ(0, image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_SNAPSHOT));
uint64_t snap_id;
ASSERT_EQ(0, image.mirror_image_create_snapshot(&snap_id));
vector<librbd::snap_info_t> snaps;
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_EQ(2U, snaps.size());
ASSERT_EQ(snaps[1].id, snap_id);
ASSERT_EQ(0, image.mirror_image_disable(false));
snaps.clear();
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_TRUE(snaps.empty());
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_peer_site_remove(m_ioctx, peer_uuid));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestMirroring, SnapshotUnlinkPeer)
{
REQUIRE_FORMAT_V2();
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
std::string peer1_uuid;
ASSERT_EQ(0, m_rbd.mirror_peer_site_add(m_ioctx, &peer1_uuid,
RBD_MIRROR_PEER_DIRECTION_RX_TX,
"cluster1", "client"));
std::string peer2_uuid;
ASSERT_EQ(0, m_rbd.mirror_peer_site_add(m_ioctx, &peer2_uuid,
RBD_MIRROR_PEER_DIRECTION_RX_TX,
"cluster2", "client"));
std::string peer3_uuid;
ASSERT_EQ(0, m_rbd.mirror_peer_site_add(m_ioctx, &peer3_uuid,
RBD_MIRROR_PEER_DIRECTION_RX_TX,
"cluster3", "client"));
uint64_t features;
ASSERT_TRUE(get_features(&features));
features &= ~RBD_FEATURE_JOURNALING;
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
ASSERT_EQ(0, image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_SNAPSHOT));
uint64_t snap_id;
ASSERT_EQ(0, image.mirror_image_create_snapshot(&snap_id));
uint64_t snap_id2;
ASSERT_EQ(0, image.mirror_image_create_snapshot(&snap_id2));
librbd::snap_mirror_namespace_t mirror_snap;
ASSERT_EQ(0, image.snap_get_mirror_namespace(snap_id, &mirror_snap,
sizeof(mirror_snap)));
ASSERT_EQ(3U, mirror_snap.mirror_peer_uuids.size());
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer1_uuid));
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer2_uuid));
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer3_uuid));
auto ictx = new librbd::ImageCtx(image_name, "", nullptr, m_ioctx, false);
ASSERT_EQ(0, ictx->state->open(0));
BOOST_SCOPE_EXIT(&ictx) {
if (ictx != nullptr) {
ictx->state->close();
}
} BOOST_SCOPE_EXIT_END;
C_SaferCond cond1;
auto req = librbd::mirror::snapshot::UnlinkPeerRequest<>::create(
ictx, snap_id, peer1_uuid, true, &cond1);
req->send();
ASSERT_EQ(0, cond1.wait());
ASSERT_EQ(0, image.snap_get_mirror_namespace(snap_id, &mirror_snap,
sizeof(mirror_snap)));
ASSERT_EQ(2U, mirror_snap.mirror_peer_uuids.size());
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer2_uuid));
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer3_uuid));
ASSERT_EQ(0, librbd::api::Namespace<>::create(m_ioctx, "ns1"));
librados::IoCtx ns_ioctx;
ns_ioctx.dup(m_ioctx);
ns_ioctx.set_namespace("ns1");
ASSERT_EQ(0, m_rbd.mirror_mode_set(ns_ioctx, RBD_MIRROR_MODE_IMAGE));
ASSERT_EQ(0, m_rbd.create2(ns_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image ns_image;
ASSERT_EQ(0, m_rbd.open(ns_ioctx, ns_image, image_name.c_str()));
ASSERT_EQ(0, ns_image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_SNAPSHOT));
uint64_t ns_snap_id;
ASSERT_EQ(0, ns_image.mirror_image_create_snapshot(&ns_snap_id));
ASSERT_EQ(0, ns_image.snap_get_mirror_namespace(ns_snap_id, &mirror_snap,
sizeof(mirror_snap)));
ASSERT_EQ(3U, mirror_snap.mirror_peer_uuids.size());
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer1_uuid));
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer2_uuid));
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer3_uuid));
ASSERT_EQ(0, m_rbd.mirror_peer_site_remove(m_ioctx, peer3_uuid));
ASSERT_EQ(0, image.snap_get_mirror_namespace(snap_id, &mirror_snap,
sizeof(mirror_snap)));
ASSERT_EQ(1U, mirror_snap.mirror_peer_uuids.size());
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer2_uuid));
ASSERT_EQ(0, ns_image.snap_get_mirror_namespace(ns_snap_id, &mirror_snap,
sizeof(mirror_snap)));
ASSERT_EQ(2U, mirror_snap.mirror_peer_uuids.size());
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer1_uuid));
ASSERT_EQ(1, mirror_snap.mirror_peer_uuids.count(peer2_uuid));
C_SaferCond cond2;
req = librbd::mirror::snapshot::UnlinkPeerRequest<>::create(
ictx, snap_id, peer2_uuid, true, &cond2);
req->send();
ASSERT_EQ(0, cond2.wait());
ASSERT_EQ(-ENOENT, image.snap_get_mirror_namespace(snap_id, &mirror_snap,
sizeof(mirror_snap)));
ictx->state->close();
ictx = nullptr;
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, ns_image.close());
ASSERT_EQ(0, m_rbd.remove(ns_ioctx, image_name.c_str()));
ASSERT_EQ(0, librbd::api::Namespace<>::remove(m_ioctx, "ns1"));
ASSERT_EQ(0, m_rbd.mirror_peer_site_remove(m_ioctx, peer1_uuid));
ASSERT_EQ(0, m_rbd.mirror_peer_site_remove(m_ioctx, peer2_uuid));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestMirroring, SnapshotImageState)
{
REQUIRE_FORMAT_V2();
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
uint64_t features;
ASSERT_TRUE(get_features(&features));
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
ASSERT_EQ(0, image.snap_create("snap"));
ASSERT_EQ(0, image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_SNAPSHOT));
std::vector<librbd::snap_info_t> snaps;
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_EQ(2U, snaps.size());
auto snap_id = snaps[1].id;
auto ictx = new librbd::ImageCtx(image_name, "", nullptr, m_ioctx, false);
ASSERT_EQ(0, ictx->state->open(0));
BOOST_SCOPE_EXIT(&ictx) {
if (ictx != nullptr) {
ictx->state->close();
}
} BOOST_SCOPE_EXIT_END;
{
C_SaferCond cond;
auto req = librbd::mirror::snapshot::SetImageStateRequest<>::create(
ictx, snap_id, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
}
librbd::mirror::snapshot::ImageState image_state;
{
C_SaferCond cond;
auto req = librbd::mirror::snapshot::GetImageStateRequest<>::create(
ictx, snap_id, &image_state, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
}
ASSERT_EQ(image_name, image_state.name);
ASSERT_EQ(0, image.features(&features));
ASSERT_EQ(features & ~RBD_FEATURES_IMPLICIT_ENABLE, image_state.features);
ASSERT_EQ(1U, image_state.snapshots.size());
ASSERT_EQ("snap", image_state.snapshots.begin()->second.name);
uint8_t original_pairs_num = image_state.metadata.size();
{
C_SaferCond cond;
auto req = librbd::mirror::snapshot::RemoveImageStateRequest<>::create(
ictx, snap_id, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
}
// test storing "large" image state in multiple objects
ASSERT_EQ(0, ictx->config.set_val("rbd_default_order", "8"));
for (int i = 0; i < 10; i++) {
ASSERT_EQ(0, image.metadata_set(stringify(i), std::string(1024, 'A' + i)));
}
{
C_SaferCond cond;
auto req = librbd::mirror::snapshot::SetImageStateRequest<>::create(
ictx, snap_id, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
}
{
C_SaferCond cond;
auto req = librbd::mirror::snapshot::GetImageStateRequest<>::create(
ictx, snap_id, &image_state, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
}
ASSERT_EQ(image_name, image_state.name);
ASSERT_EQ(features & ~RBD_FEATURES_IMPLICIT_ENABLE, image_state.features);
ASSERT_EQ(original_pairs_num + 10, image_state.metadata.size());
for (int i = 0; i < 10; i++) {
auto &bl = image_state.metadata[stringify(i)];
ASSERT_EQ(0, strncmp(std::string(1024, 'A' + i).c_str(), bl.c_str(),
bl.length()));
}
{
C_SaferCond cond;
auto req = librbd::mirror::snapshot::RemoveImageStateRequest<>::create(
ictx, snap_id, &cond);
req->send();
ASSERT_EQ(0, cond.wait());
}
ASSERT_EQ(0, ictx->state->close());
ictx = nullptr;
ASSERT_EQ(0, image.snap_remove("snap"));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
}
TEST_F(TestMirroring, SnapshotPromoteDemote)
{
REQUIRE_FORMAT_V2();
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
std::string peer_uuid;
ASSERT_EQ(0, m_rbd.mirror_peer_site_add(m_ioctx, &peer_uuid,
RBD_MIRROR_PEER_DIRECTION_RX_TX,
"cluster", "client"));
uint64_t features;
ASSERT_TRUE(get_features(&features));
int order = 20;
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 4096, features,
&order));
librbd::Image image;
ASSERT_EQ(0, m_rbd.open(m_ioctx, image, image_name.c_str()));
ASSERT_EQ(0, image.mirror_image_enable2(RBD_MIRROR_IMAGE_MODE_SNAPSHOT));
librbd::mirror_image_mode_t mode;
ASSERT_EQ(0, image.mirror_image_get_mode(&mode));
ASSERT_EQ(RBD_MIRROR_IMAGE_MODE_SNAPSHOT, mode);
ASSERT_EQ(-EINVAL, image.mirror_image_promote(false));
ASSERT_EQ(0, image.mirror_image_demote());
ASSERT_EQ(0, image.mirror_image_promote(false));
ASSERT_EQ(0, image.mirror_image_demote());
ASSERT_EQ(0, image.mirror_image_promote(false));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
ASSERT_EQ(0, m_rbd.mirror_peer_site_remove(m_ioctx, peer_uuid));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
TEST_F(TestMirroring, AioSnapshotCreate)
{
REQUIRE_FORMAT_V2();
std::list<std::string> image_names;
for (size_t idx = 0; idx < 10; ++idx) {
image_names.push_back(get_temp_image_name());
}
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_IMAGE));
std::string peer_uuid;
ASSERT_EQ(0, m_rbd.mirror_peer_site_add(m_ioctx, &peer_uuid,
RBD_MIRROR_PEER_DIRECTION_RX_TX,
"cluster", "client"));
// create mirror images
uint64_t features;
ASSERT_TRUE(get_features(&features));
int order = 20;
std::list<librbd::Image> images;
for (auto &image_name : image_names) {
ASSERT_EQ(0, m_rbd.create2(m_ioctx, image_name.c_str(), 2048, features,
&order));
images.emplace_back();
ASSERT_EQ(0, m_rbd.open(m_ioctx, images.back(), image_name.c_str()));
ASSERT_EQ(0, images.back().mirror_image_enable2(
RBD_MIRROR_IMAGE_MODE_SNAPSHOT));
}
// create snapshots
std::list<uint64_t> snap_ids;
std::list<librbd::RBD::AioCompletion *> aio_comps;
for (auto &image : images) {
snap_ids.emplace_back();
aio_comps.push_back(new librbd::RBD::AioCompletion(nullptr, nullptr));
ASSERT_EQ(0, image.aio_mirror_image_create_snapshot(0, &snap_ids.back(),
aio_comps.back()));
}
for (auto aio_comp : aio_comps) {
ASSERT_EQ(0, aio_comp->wait_for_complete());
ASSERT_EQ(1, aio_comp->is_complete());
ASSERT_EQ(0, aio_comp->get_return_value());
aio_comp->release();
}
aio_comps.clear();
// verify
for (auto &image : images) {
vector<librbd::snap_info_t> snaps;
ASSERT_EQ(0, image.snap_list(snaps));
ASSERT_EQ(2U, snaps.size());
ASSERT_EQ(snaps[1].id, snap_ids.front());
std::string image_name;
ASSERT_EQ(0, image.get_name(&image_name));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, m_rbd.remove(m_ioctx, image_name.c_str()));
snap_ids.pop_front();
}
ASSERT_EQ(0, m_rbd.mirror_peer_site_remove(m_ioctx, peer_uuid));
ASSERT_EQ(0, m_rbd.mirror_mode_set(m_ioctx, RBD_MIRROR_MODE_DISABLED));
}
| 57,302 | 36.113342 | 90 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_ConfigWatcher.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "include/rbd_types.h"
#include "common/ceph_mutex.h"
#include "librbd/ConfigWatcher.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <list>
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
} // namespace librbd
#include "librbd/ConfigWatcher.cc"
namespace librbd {
using ::testing::Invoke;
class TestMockConfigWatcher : public TestMockFixture {
public:
typedef ConfigWatcher<MockTestImageCtx> MockConfigWatcher;
librbd::ImageCtx *m_image_ctx;
ceph::mutex m_lock = ceph::make_mutex("m_lock");
ceph::condition_variable m_cv;
bool m_refreshed = false;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
}
void expect_update_notification(MockTestImageCtx& mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, handle_update_notification())
.WillOnce(Invoke([this]() {
std::unique_lock locker{m_lock};
m_refreshed = true;
m_cv.notify_all();
}));
}
void wait_for_update_notification() {
std::unique_lock locker{m_lock};
m_cv.wait(locker, [this] {
if (m_refreshed) {
m_refreshed = false;
return true;
}
return false;
});
}
};
TEST_F(TestMockConfigWatcher, GlobalConfig) {
MockTestImageCtx mock_image_ctx(*m_image_ctx);
MockConfigWatcher mock_config_watcher(mock_image_ctx);
mock_config_watcher.init();
expect_update_notification(mock_image_ctx);
mock_image_ctx.cct->_conf.set_val("rbd_cache", "false");
mock_image_ctx.cct->_conf.set_val("rbd_cache", "true");
mock_image_ctx.cct->_conf.apply_changes(nullptr);
wait_for_update_notification();
mock_config_watcher.shut_down();
}
TEST_F(TestMockConfigWatcher, IgnoreOverriddenGlobalConfig) {
MockTestImageCtx mock_image_ctx(*m_image_ctx);
MockConfigWatcher mock_config_watcher(mock_image_ctx);
mock_config_watcher.init();
EXPECT_CALL(*mock_image_ctx.state, handle_update_notification())
.Times(0);
mock_image_ctx.config_overrides.insert("rbd_cache");
mock_image_ctx.cct->_conf.set_val("rbd_cache", "false");
mock_image_ctx.cct->_conf.set_val("rbd_cache", "true");
mock_image_ctx.cct->_conf.apply_changes(nullptr);
mock_config_watcher.shut_down();
ASSERT_FALSE(m_refreshed);
}
} // namespace librbd
| 2,622 | 24.970297 | 70 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_DeepCopyRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "include/rbd/librbd.hpp"
#include "librbd/AsioEngine.h"
#include "librbd/DeepCopyRequest.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "librbd/internal.h"
#include "librbd/api/Image.h"
#include "librbd/deep_copy/Handler.h"
#include "librbd/deep_copy/ImageCopyRequest.h"
#include "librbd/deep_copy/MetadataCopyRequest.h"
#include "librbd/deep_copy/SnapshotCopyRequest.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockObjectMap.h"
#include "test/librbd/test_support.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <boost/scope_exit.hpp>
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace deep_copy {
template <>
class ImageCopyRequest<librbd::MockTestImageCtx> {
public:
static ImageCopyRequest* s_instance;
Context *on_finish;
static ImageCopyRequest* create(
librbd::MockTestImageCtx *src_image_ctx,
librbd::MockTestImageCtx *dst_image_ctx,
librados::snap_t src_snap_id_start,
librados::snap_t src_snap_id_end,
librados::snap_t dst_snap_id_start,
bool flatten, const ObjectNumber &object_number,
const SnapSeqs &snap_seqs, Handler *handler,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
ImageCopyRequest() {
s_instance = this;
}
void put() {
}
void get() {
}
MOCK_METHOD0(cancel, void());
MOCK_METHOD0(send, void());
};
template <>
class MetadataCopyRequest<librbd::MockTestImageCtx> {
public:
static MetadataCopyRequest* s_instance;
Context *on_finish;
static MetadataCopyRequest* create(librbd::MockTestImageCtx *src_image_ctx,
librbd::MockTestImageCtx *dst_image_ctx,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MetadataCopyRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
template <>
class SnapshotCopyRequest<librbd::MockTestImageCtx> {
public:
static SnapshotCopyRequest* s_instance;
Context *on_finish;
static SnapshotCopyRequest* create(librbd::MockTestImageCtx *src_image_ctx,
librbd::MockTestImageCtx *dst_image_ctx,
librados::snap_t src_snap_id_start,
librados::snap_t src_snap_id_end,
librados::snap_t dst_snap_id_start,
bool flatten,
librbd::asio::ContextWQ *work_queue,
SnapSeqs *snap_seqs, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
SnapshotCopyRequest() {
s_instance = this;
}
void put() {
}
void get() {
}
MOCK_METHOD0(cancel, void());
MOCK_METHOD0(send, void());
};
ImageCopyRequest<librbd::MockTestImageCtx>* ImageCopyRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
MetadataCopyRequest<librbd::MockTestImageCtx>* MetadataCopyRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
SnapshotCopyRequest<librbd::MockTestImageCtx>* SnapshotCopyRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
} // namespace deep_copy
} // namespace librbd
// template definitions
#include "librbd/DeepCopyRequest.cc"
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::SetArgPointee;
using ::testing::WithArg;
class TestMockDeepCopyRequest : public TestMockFixture {
public:
typedef librbd::DeepCopyRequest<librbd::MockTestImageCtx> MockDeepCopyRequest;
typedef librbd::deep_copy::ImageCopyRequest<librbd::MockTestImageCtx> MockImageCopyRequest;
typedef librbd::deep_copy::MetadataCopyRequest<librbd::MockTestImageCtx> MockMetadataCopyRequest;
typedef librbd::deep_copy::SnapshotCopyRequest<librbd::MockTestImageCtx> MockSnapshotCopyRequest;
librbd::ImageCtx *m_src_image_ctx;
librbd::ImageCtx *m_dst_image_ctx;
std::shared_ptr<librbd::AsioEngine> m_asio_engine;
librbd::asio::ContextWQ *m_work_queue;
void SetUp() override {
TestMockFixture::SetUp();
librbd::RBD rbd;
ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
ASSERT_EQ(0, open_image(m_image_name, &m_dst_image_ctx));
m_asio_engine = std::make_shared<librbd::AsioEngine>(
m_src_image_ctx->md_ctx);
m_work_queue = m_asio_engine->get_work_queue();
}
void TearDown() override {
TestMockFixture::TearDown();
}
void expect_test_features(librbd::MockImageCtx &mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, test_features(_, _))
.WillRepeatedly(WithArg<0>(Invoke([&mock_image_ctx](uint64_t features) {
return (mock_image_ctx.features & features) != 0;
})));
}
void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
}
void expect_rollback_object_map(librbd::MockObjectMap &mock_object_map, int r) {
EXPECT_CALL(mock_object_map, rollback(_, _))
.WillOnce(WithArg<1>(Invoke([this, r](Context *ctx) {
m_work_queue->queue(ctx, r);
})));
}
void expect_create_object_map(librbd::MockTestImageCtx &mock_image_ctx,
librbd::MockObjectMap *mock_object_map) {
EXPECT_CALL(mock_image_ctx, create_object_map(CEPH_NOSNAP))
.WillOnce(Return(mock_object_map));
}
void expect_open_object_map(librbd::MockTestImageCtx &mock_image_ctx,
librbd::MockObjectMap &mock_object_map, int r) {
EXPECT_CALL(mock_object_map, open(_))
.WillOnce(Invoke([this, r](Context *ctx) {
m_work_queue->queue(ctx, r);
}));
}
void expect_copy_snapshots(
MockSnapshotCopyRequest &mock_snapshot_copy_request, int r) {
EXPECT_CALL(mock_snapshot_copy_request, send())
.WillOnce(Invoke([this, &mock_snapshot_copy_request, r]() {
m_work_queue->queue(mock_snapshot_copy_request.on_finish, r);
}));
}
void expect_copy_image(MockImageCopyRequest &mock_image_copy_request, int r) {
EXPECT_CALL(mock_image_copy_request, send())
.WillOnce(Invoke([this, &mock_image_copy_request, r]() {
m_work_queue->queue(mock_image_copy_request.on_finish, r);
}));
}
void expect_copy_object_map(librbd::MockExclusiveLock &mock_exclusive_lock,
librbd::MockObjectMap *mock_object_map, int r) {
expect_start_op(mock_exclusive_lock);
expect_rollback_object_map(*mock_object_map, r);
}
void expect_refresh_object_map(librbd::MockTestImageCtx &mock_image_ctx,
librbd::MockObjectMap *mock_object_map,
int r) {
expect_start_op(*mock_image_ctx.exclusive_lock);
expect_create_object_map(mock_image_ctx, mock_object_map);
expect_open_object_map(mock_image_ctx, *mock_object_map, r);
}
void expect_copy_metadata(MockMetadataCopyRequest &mock_metadata_copy_request,
int r) {
EXPECT_CALL(mock_metadata_copy_request, send())
.WillOnce(Invoke([this, &mock_metadata_copy_request, r]() {
m_work_queue->queue(mock_metadata_copy_request.on_finish, r);
}));
}
};
TEST_F(TestMockDeepCopyRequest, SimpleCopy) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockImageCopyRequest mock_image_copy_request;
MockMetadataCopyRequest mock_metadata_copy_request;
MockSnapshotCopyRequest mock_snapshot_copy_request;
librbd::MockExclusiveLock mock_exclusive_lock;
mock_dst_image_ctx.exclusive_lock = &mock_exclusive_lock;
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = nullptr;
if (is_feature_enabled(RBD_FEATURE_OBJECT_MAP)) {
mock_dst_image_ctx.object_map = &mock_object_map;
}
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_copy_snapshots(mock_snapshot_copy_request, 0);
expect_copy_image(mock_image_copy_request, 0);
if (mock_dst_image_ctx.object_map != nullptr) {
expect_refresh_object_map(mock_dst_image_ctx, &mock_object_map, 0);
}
expect_copy_metadata(mock_metadata_copy_request, 0);
C_SaferCond ctx;
librbd::SnapSeqs snap_seqs;
librbd::deep_copy::NoOpHandler no_op;
auto request = librbd::DeepCopyRequest<librbd::MockTestImageCtx>::create(
&mock_src_image_ctx, &mock_dst_image_ctx, 0, CEPH_NOSNAP, 0, false,
boost::none, m_work_queue, &snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyRequest, ErrorOnCopySnapshots) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCopyRequest mock_snapshot_copy_request;
InSequence seq;
expect_copy_snapshots(mock_snapshot_copy_request, -EINVAL);
C_SaferCond ctx;
librbd::SnapSeqs snap_seqs;
librbd::deep_copy::NoOpHandler no_op;
auto request = librbd::DeepCopyRequest<librbd::MockTestImageCtx>::create(
&mock_src_image_ctx, &mock_dst_image_ctx, 0, CEPH_NOSNAP, 0, false,
boost::none, m_work_queue, &snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyRequest, ErrorOnRefreshObjectMap) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockImageCopyRequest mock_image_copy_request;
MockSnapshotCopyRequest mock_snapshot_copy_request;
librbd::MockExclusiveLock mock_exclusive_lock;
mock_dst_image_ctx.exclusive_lock = &mock_exclusive_lock;
librbd::MockObjectMap *mock_object_map = new librbd::MockObjectMap();
mock_dst_image_ctx.object_map = mock_object_map;
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_copy_snapshots(mock_snapshot_copy_request, 0);
expect_copy_image(mock_image_copy_request, 0);
expect_start_op(*mock_dst_image_ctx.exclusive_lock);
expect_create_object_map(mock_dst_image_ctx, mock_object_map);
expect_open_object_map(mock_dst_image_ctx, *mock_object_map, -EINVAL);
C_SaferCond ctx;
librbd::SnapSeqs snap_seqs;
librbd::deep_copy::NoOpHandler no_op;
auto request = librbd::DeepCopyRequest<librbd::MockTestImageCtx>::create(
&mock_src_image_ctx, &mock_dst_image_ctx, 0, CEPH_NOSNAP, 0, false,
boost::none, m_work_queue, &snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyRequest, ErrorOnCopyImage) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockImageCopyRequest mock_image_copy_request;
MockSnapshotCopyRequest mock_snapshot_copy_request;
InSequence seq;
expect_copy_snapshots(mock_snapshot_copy_request, 0);
expect_copy_image(mock_image_copy_request, -EINVAL);
C_SaferCond ctx;
librbd::SnapSeqs snap_seqs;
librbd::deep_copy::NoOpHandler no_op;
auto request = librbd::DeepCopyRequest<librbd::MockTestImageCtx>::create(
&mock_src_image_ctx, &mock_dst_image_ctx, 0, CEPH_NOSNAP, 0, false,
boost::none, m_work_queue, &snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyRequest, ErrorOnCopyMetadata) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockImageCopyRequest mock_image_copy_request;
MockMetadataCopyRequest mock_metadata_copy_request;
MockSnapshotCopyRequest mock_snapshot_copy_request;
librbd::MockExclusiveLock mock_exclusive_lock;
mock_dst_image_ctx.exclusive_lock = &mock_exclusive_lock;
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = nullptr;
if (is_feature_enabled(RBD_FEATURE_OBJECT_MAP)) {
mock_dst_image_ctx.object_map = &mock_object_map;
}
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_copy_snapshots(mock_snapshot_copy_request, 0);
expect_copy_image(mock_image_copy_request, 0);
if (mock_dst_image_ctx.object_map != nullptr) {
expect_refresh_object_map(mock_dst_image_ctx, &mock_object_map, 0);
}
expect_copy_metadata(mock_metadata_copy_request, -EINVAL);
C_SaferCond ctx;
librbd::SnapSeqs snap_seqs;
librbd::deep_copy::NoOpHandler no_op;
auto request = librbd::DeepCopyRequest<librbd::MockTestImageCtx>::create(
&mock_src_image_ctx, &mock_dst_image_ctx, 0, CEPH_NOSNAP, 0, false,
boost::none, m_work_queue, &snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyRequest, Snap) {
EXPECT_EQ(0, snap_create(*m_src_image_ctx, "copy"));
EXPECT_EQ(0, librbd::api::Image<>::snap_set(m_src_image_ctx,
cls::rbd::UserSnapshotNamespace(),
"copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockImageCopyRequest mock_image_copy_request;
MockMetadataCopyRequest mock_metadata_copy_request;
MockSnapshotCopyRequest mock_snapshot_copy_request;
librbd::MockExclusiveLock mock_exclusive_lock;
mock_dst_image_ctx.exclusive_lock = &mock_exclusive_lock;
librbd::MockObjectMap mock_object_map;
if (is_feature_enabled(RBD_FEATURE_OBJECT_MAP)) {
mock_dst_image_ctx.object_map = &mock_object_map;
}
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_copy_snapshots(mock_snapshot_copy_request, 0);
expect_copy_image(mock_image_copy_request, 0);
if (mock_dst_image_ctx.object_map != nullptr) {
expect_copy_object_map(mock_exclusive_lock, &mock_object_map, 0);
expect_refresh_object_map(mock_dst_image_ctx, &mock_object_map, 0);
}
expect_copy_metadata(mock_metadata_copy_request, 0);
C_SaferCond ctx;
librbd::SnapSeqs snap_seqs = {{m_src_image_ctx->snap_id, 123}};
librbd::deep_copy::NoOpHandler no_op;
auto request = librbd::DeepCopyRequest<librbd::MockTestImageCtx>::create(
&mock_src_image_ctx, &mock_dst_image_ctx, 0, m_src_image_ctx->snap_id,
0, false, boost::none, m_work_queue, &snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyRequest, ErrorOnRollbackObjectMap) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
EXPECT_EQ(0, snap_create(*m_src_image_ctx, "copy"));
EXPECT_EQ(0, librbd::api::Image<>::snap_set(m_src_image_ctx,
cls::rbd::UserSnapshotNamespace(),
"copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockImageCopyRequest mock_image_copy_request;
MockMetadataCopyRequest mock_metadata_copy_request;
MockSnapshotCopyRequest mock_snapshot_copy_request;
librbd::MockExclusiveLock mock_exclusive_lock;
mock_dst_image_ctx.exclusive_lock = &mock_exclusive_lock;
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_copy_snapshots(mock_snapshot_copy_request, 0);
expect_copy_image(mock_image_copy_request, 0);
expect_copy_object_map(mock_exclusive_lock, &mock_object_map, -EINVAL);
C_SaferCond ctx;
librbd::SnapSeqs snap_seqs = {{m_src_image_ctx->snap_id, 123}};
librbd::deep_copy::NoOpHandler no_op;
auto request = librbd::DeepCopyRequest<librbd::MockTestImageCtx>::create(
&mock_src_image_ctx, &mock_dst_image_ctx, 0, m_src_image_ctx->snap_id,
0, false, boost::none, m_work_queue, &snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
| 16,440 | 34.205567 | 115 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_ExclusiveLock.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/exclusive_lock/MockPolicy.h"
#include "test/librbd/mock/io/MockImageDispatch.h"
#include "librbd/ExclusiveLock.h"
#include "librbd/ManagedLock.h"
#include "librbd/exclusive_lock/ImageDispatch.h"
#include "librbd/exclusive_lock/PreAcquireRequest.h"
#include "librbd/exclusive_lock/PostAcquireRequest.h"
#include "librbd/exclusive_lock/PreReleaseRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <list>
#include <boost/scope_exit.hpp>
namespace librbd {
namespace {
struct MockExclusiveLockImageCtx : public MockImageCtx {
asio::ContextWQ *op_work_queue;
MockExclusiveLockImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
op_work_queue = image_ctx.op_work_queue;
}
};
} // anonymous namespace
namespace watcher {
template <>
struct Traits<MockExclusiveLockImageCtx> {
typedef librbd::MockImageWatcher Watcher;
};
}
template <>
struct ManagedLock<MockExclusiveLockImageCtx> {
ManagedLock(librados::IoCtx& ioctx, AsioEngine& asio_engine,
const std::string& oid, librbd::MockImageWatcher *watcher,
managed_lock::Mode mode, bool blocklist_on_break_lock,
uint32_t blocklist_expire_seconds)
{}
virtual ~ManagedLock() = default;
mutable ceph::mutex m_lock = ceph::make_mutex("ManagedLock::m_lock");
virtual void shutdown_handler(int r, Context *) = 0;
virtual void pre_acquire_lock_handler(Context *) = 0;
virtual void post_acquire_lock_handler(int, Context *) = 0;
virtual void pre_release_lock_handler(bool, Context *) = 0;
virtual void post_release_lock_handler(bool, int, Context *) = 0;
virtual void post_reacquire_lock_handler(int, Context *) = 0;
MOCK_CONST_METHOD0(is_lock_owner, bool());
MOCK_METHOD1(shut_down, void(Context*));
MOCK_METHOD1(acquire_lock, void(Context*));
void set_state_uninitialized() {
}
MOCK_METHOD0(set_state_initializing, void());
MOCK_METHOD0(set_state_unlocked, void());
MOCK_METHOD0(set_state_waiting_for_lock, void());
MOCK_METHOD0(set_state_post_acquiring, void());
MOCK_CONST_METHOD0(is_state_shutdown, bool());
MOCK_CONST_METHOD0(is_state_acquiring, bool());
MOCK_CONST_METHOD0(is_state_post_acquiring, bool());
MOCK_CONST_METHOD0(is_state_releasing, bool());
MOCK_CONST_METHOD0(is_state_pre_releasing, bool());
MOCK_CONST_METHOD0(is_state_locked, bool());
MOCK_CONST_METHOD0(is_state_waiting_for_lock, bool());
MOCK_CONST_METHOD0(is_action_acquire_lock, bool());
MOCK_METHOD0(execute_next_action, void());
};
namespace exclusive_lock {
using librbd::ImageWatcher;
template<typename T>
struct BaseRequest {
static std::list<T *> s_requests;
Context *on_lock_unlock = nullptr;
Context *on_finish = nullptr;
static T* create(MockExclusiveLockImageCtx &image_ctx,
Context *on_lock_unlock, Context *on_finish) {
ceph_assert(!s_requests.empty());
T* req = s_requests.front();
req->on_lock_unlock = on_lock_unlock;
req->on_finish = on_finish;
s_requests.pop_front();
return req;
}
BaseRequest() {
s_requests.push_back(reinterpret_cast<T*>(this));
}
};
template<typename T>
std::list<T *> BaseRequest<T>::s_requests;
template<>
struct ImageDispatch<MockExclusiveLockImageCtx>
: public librbd::io::MockImageDispatch {
static ImageDispatch* s_instance;
static ImageDispatch* create(MockExclusiveLockImageCtx*) {
ceph_assert(s_instance != nullptr);
return s_instance;
}
void destroy() {
}
ImageDispatch() {
s_instance = this;
}
io::ImageDispatchLayer get_dispatch_layer() const override {
return io::IMAGE_DISPATCH_LAYER_EXCLUSIVE_LOCK;
}
MOCK_METHOD3(set_require_lock, void(bool, io::Direction, Context*));
MOCK_METHOD1(unset_require_lock, void(io::Direction));
};
ImageDispatch<MockExclusiveLockImageCtx>* ImageDispatch<MockExclusiveLockImageCtx>::s_instance = nullptr;
template <>
struct PreAcquireRequest<MockExclusiveLockImageCtx> : public BaseRequest<PreAcquireRequest<MockExclusiveLockImageCtx> > {
static PreAcquireRequest<MockExclusiveLockImageCtx> *create(
MockExclusiveLockImageCtx &image_ctx, Context *on_finish) {
return BaseRequest::create(image_ctx, nullptr, on_finish);
}
MOCK_METHOD0(send, void());
};
template <>
struct PostAcquireRequest<MockExclusiveLockImageCtx> : public BaseRequest<PostAcquireRequest<MockExclusiveLockImageCtx> > {
MOCK_METHOD0(send, void());
};
template <>
struct PreReleaseRequest<MockExclusiveLockImageCtx> : public BaseRequest<PreReleaseRequest<MockExclusiveLockImageCtx> > {
static PreReleaseRequest<MockExclusiveLockImageCtx> *create(
MockExclusiveLockImageCtx &image_ctx,
ImageDispatch<MockExclusiveLockImageCtx>* ImageDispatch,
bool shutting_down, AsyncOpTracker &async_op_tracker,
Context *on_finish) {
return BaseRequest::create(image_ctx, nullptr, on_finish);
}
MOCK_METHOD0(send, void());
};
} // namespace exclusive_lock
} // namespace librbd
// template definitions
#include "librbd/ExclusiveLock.cc"
ACTION_P(FinishLockUnlock, request) {
if (request->on_lock_unlock != nullptr) {
request->on_lock_unlock->complete(0);
}
}
ACTION_P2(CompleteRequest, request, ret) {
request->on_finish->complete(ret);
}
namespace librbd {
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::WithArg;
class TestMockExclusiveLock : public TestMockFixture {
public:
typedef ManagedLock<MockExclusiveLockImageCtx> MockManagedLock;
typedef ExclusiveLock<MockExclusiveLockImageCtx> MockExclusiveLock;
typedef exclusive_lock::ImageDispatch<MockExclusiveLockImageCtx> MockImageDispatch;
typedef exclusive_lock::PreAcquireRequest<MockExclusiveLockImageCtx> MockPreAcquireRequest;
typedef exclusive_lock::PostAcquireRequest<MockExclusiveLockImageCtx> MockPostAcquireRequest;
typedef exclusive_lock::PreReleaseRequest<MockExclusiveLockImageCtx> MockPreReleaseRequest;
void expect_set_state_initializing(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, set_state_initializing());
}
void expect_set_state_unlocked(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, set_state_unlocked());
}
void expect_set_state_waiting_for_lock(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, set_state_waiting_for_lock());
}
void expect_set_state_post_acquiring(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, set_state_post_acquiring());
}
void expect_is_state_acquiring(MockManagedLock *managed_lock, bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_acquiring())
.WillOnce(Return(ret_val));
}
void expect_is_state_waiting_for_lock(MockManagedLock *managed_lock,
bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_waiting_for_lock())
.WillOnce(Return(ret_val));
}
void expect_is_state_pre_releasing(MockManagedLock *managed_lock,
bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_pre_releasing())
.WillOnce(Return(ret_val));
}
void expect_is_state_releasing(MockManagedLock *managed_lock, bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_releasing())
.WillOnce(Return(ret_val));
}
void expect_is_state_locked(MockManagedLock *managed_lock, bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_locked())
.WillOnce(Return(ret_val));
}
void expect_is_state_shutdown(MockManagedLock *managed_lock, bool ret_val) {
EXPECT_CALL(*managed_lock, is_state_shutdown())
.WillOnce(Return(ret_val));
}
void expect_is_action_acquire_lock(MockManagedLock *managed_lock,
bool ret_val) {
EXPECT_CALL(*managed_lock, is_action_acquire_lock())
.WillOnce(Return(ret_val));
}
void expect_set_require_lock(MockImageDispatch &mock_image_dispatch,
bool init_shutdown, io::Direction direction) {
EXPECT_CALL(mock_image_dispatch, set_require_lock(init_shutdown,
direction, _))
.WillOnce(WithArg<2>(Invoke([](Context* ctx) { ctx->complete(0); })));
}
void expect_set_require_lock(MockExclusiveLockImageCtx &mock_image_ctx,
MockImageDispatch &mock_image_dispatch,
bool init_shutdown) {
if (mock_image_ctx.clone_copy_on_read ||
(mock_image_ctx.features & RBD_FEATURE_JOURNALING) != 0 ||
is_rbd_pwl_enabled(mock_image_ctx.cct)) {
expect_set_require_lock(mock_image_dispatch, init_shutdown,
io::DIRECTION_BOTH);
} else {
expect_set_require_lock(mock_image_dispatch, init_shutdown,
io::DIRECTION_WRITE);
}
}
void expect_unset_require_lock(MockImageDispatch &mock_image_dispatch) {
EXPECT_CALL(mock_image_dispatch, unset_require_lock(io::DIRECTION_BOTH));
}
void expect_register_dispatch(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, register_dispatch(_));
}
void expect_shut_down_dispatch(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, shut_down_dispatch(_, _))
.WillOnce(WithArg<1>(Invoke([](Context* ctx) {
ctx->complete(0);
})));
}
void expect_prepare_lock_complete(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete());
}
void expect_pre_acquire_request(MockPreAcquireRequest &pre_acquire_request,
int r) {
EXPECT_CALL(pre_acquire_request, send())
.WillOnce(CompleteRequest(&pre_acquire_request, r));
}
void expect_post_acquire_request(MockExclusiveLock *mock_exclusive_lock,
MockPostAcquireRequest &post_acquire_request,
int r) {
EXPECT_CALL(post_acquire_request, send())
.WillOnce(DoAll(FinishLockUnlock(&post_acquire_request),
CompleteRequest(&post_acquire_request, r)));
expect_set_state_post_acquiring(mock_exclusive_lock);
}
void expect_pre_release_request(MockPreReleaseRequest &pre_release_request,
int r) {
EXPECT_CALL(pre_release_request, send())
.WillOnce(CompleteRequest(&pre_release_request, r));
}
void expect_notify_request_lock(MockExclusiveLockImageCtx &mock_image_ctx,
MockExclusiveLock *mock_exclusive_lock) {
EXPECT_CALL(*mock_image_ctx.image_watcher, notify_request_lock());
}
void expect_notify_acquired_lock(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.image_watcher, notify_acquired_lock())
.Times(1);
}
void expect_notify_released_lock(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.image_watcher, notify_released_lock())
.Times(1);
}
void expect_flush_notifies(MockExclusiveLockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.image_watcher, flush(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_shut_down(MockManagedLock *managed_lock) {
EXPECT_CALL(*managed_lock, shut_down(_))
.WillOnce(CompleteContext(0, static_cast<asio::ContextWQ*>(nullptr)));
}
void expect_accept_blocked_request(
MockExclusiveLockImageCtx &mock_image_ctx,
exclusive_lock::MockPolicy &policy,
exclusive_lock::OperationRequestType request_type, bool value) {
EXPECT_CALL(mock_image_ctx, get_exclusive_lock_policy())
.WillOnce(Return(&policy));
EXPECT_CALL(policy, accept_blocked_request(request_type))
.WillOnce(Return(value));
}
int when_init(MockExclusiveLockImageCtx &mock_image_ctx,
MockExclusiveLock *exclusive_lock) {
C_SaferCond ctx;
{
std::unique_lock owner_locker{mock_image_ctx.owner_lock};
exclusive_lock->init(mock_image_ctx.features, &ctx);
}
return ctx.wait();
}
int when_pre_acquire_lock_handler(MockManagedLock *managed_lock) {
C_SaferCond ctx;
managed_lock->pre_acquire_lock_handler(&ctx);
return ctx.wait();
}
int when_post_acquire_lock_handler(MockManagedLock *managed_lock, int r) {
C_SaferCond ctx;
managed_lock->post_acquire_lock_handler(r, &ctx);
return ctx.wait();
}
int when_pre_release_lock_handler(MockManagedLock *managed_lock,
bool shutting_down) {
C_SaferCond ctx;
managed_lock->pre_release_lock_handler(shutting_down, &ctx);
return ctx.wait();
}
int when_post_release_lock_handler(MockManagedLock *managed_lock,
bool shutting_down, int r) {
C_SaferCond ctx;
managed_lock->post_release_lock_handler(shutting_down, r, &ctx);
return ctx.wait();
}
int when_post_reacquire_lock_handler(MockManagedLock *managed_lock, int r) {
C_SaferCond ctx;
managed_lock->post_reacquire_lock_handler(r, &ctx);
return ctx.wait();
}
int when_shut_down(MockExclusiveLockImageCtx &mock_image_ctx,
MockExclusiveLock *exclusive_lock) {
C_SaferCond ctx;
{
std::unique_lock owner_locker{mock_image_ctx.owner_lock};
exclusive_lock->shut_down(&ctx);
}
return ctx.wait();
}
bool is_lock_owner(MockExclusiveLockImageCtx &mock_image_ctx,
MockExclusiveLock *exclusive_lock) {
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
return exclusive_lock->is_lock_owner();
}
};
TEST_F(TestMockExclusiveLock, StateTransitions) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// (try) acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
MockPostAcquireRequest try_lock_post_acquire;
expect_post_acquire_request(exclusive_lock, try_lock_post_acquire, 0);
expect_is_state_acquiring(exclusive_lock, true);
expect_notify_acquired_lock(mock_image_ctx);
expect_unset_require_lock(mock_image_dispatch);
ASSERT_EQ(0, when_post_acquire_lock_handler(exclusive_lock, 0));
// release lock
MockPreReleaseRequest pre_request_release;
expect_pre_release_request(pre_request_release, 0);
ASSERT_EQ(0, when_pre_release_lock_handler(exclusive_lock, false));
expect_is_state_pre_releasing(exclusive_lock, false);
expect_is_state_releasing(exclusive_lock, true);
expect_notify_released_lock(mock_image_ctx);
ASSERT_EQ(0, when_post_release_lock_handler(exclusive_lock, false, 0));
// (try) acquire lock
MockPreAcquireRequest request_lock_pre_acquire;
expect_pre_acquire_request(request_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
MockPostAcquireRequest request_lock_post_acquire;
expect_post_acquire_request(exclusive_lock, request_lock_post_acquire, 0);
expect_is_state_acquiring(exclusive_lock, true);
expect_notify_acquired_lock(mock_image_ctx);
expect_unset_require_lock(mock_image_dispatch);
ASSERT_EQ(0, when_post_acquire_lock_handler(exclusive_lock, 0));
// shut down (and release)
expect_shut_down(exclusive_lock);
expect_is_state_waiting_for_lock(exclusive_lock, false);
ASSERT_EQ(0, when_shut_down(mock_image_ctx, exclusive_lock));
MockPreReleaseRequest shutdown_pre_release;
expect_pre_release_request(shutdown_pre_release, 0);
ASSERT_EQ(0, when_pre_release_lock_handler(exclusive_lock, true));
expect_shut_down_dispatch(mock_image_ctx);
expect_notify_released_lock(mock_image_ctx);
ASSERT_EQ(0, when_post_release_lock_handler(exclusive_lock, true, 0));
}
TEST_F(TestMockExclusiveLock, TryLockAlreadyLocked) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// try acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, false);
ASSERT_EQ(0, when_post_acquire_lock_handler(exclusive_lock, -EAGAIN));
}
TEST_F(TestMockExclusiveLock, TryLockError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// try acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, false);
ASSERT_EQ(-EBUSY, when_post_acquire_lock_handler(exclusive_lock, -EBUSY));
}
TEST_F(TestMockExclusiveLock, AcquireLockAlreadyLocked) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, true);
expect_set_state_waiting_for_lock(exclusive_lock);
expect_notify_request_lock(mock_image_ctx, exclusive_lock);
ASSERT_EQ(-ECANCELED, when_post_acquire_lock_handler(exclusive_lock,
-EAGAIN));
}
TEST_F(TestMockExclusiveLock, AcquireLockBusy) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, true);
expect_set_state_waiting_for_lock(exclusive_lock);
expect_notify_request_lock(mock_image_ctx, exclusive_lock);
ASSERT_EQ(-ECANCELED, when_post_acquire_lock_handler(exclusive_lock,
-EBUSY));
}
TEST_F(TestMockExclusiveLock, AcquireLockError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
expect_is_state_acquiring(exclusive_lock, true);
expect_prepare_lock_complete(mock_image_ctx);
expect_is_action_acquire_lock(exclusive_lock, true);
ASSERT_EQ(-EBLOCKLISTED, when_post_acquire_lock_handler(exclusive_lock,
-EBLOCKLISTED));
}
TEST_F(TestMockExclusiveLock, PostAcquireLockError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// (try) acquire lock
MockPreAcquireRequest request_lock_pre_acquire;
expect_pre_acquire_request(request_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
MockPostAcquireRequest request_lock_post_acquire;
expect_post_acquire_request(exclusive_lock, request_lock_post_acquire,
-EPERM);
expect_is_state_acquiring(exclusive_lock, true);
ASSERT_EQ(-EPERM, when_post_acquire_lock_handler(exclusive_lock, 0));
}
TEST_F(TestMockExclusiveLock, PreReleaseLockError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// release lock
MockPreReleaseRequest pre_request_release;
expect_pre_release_request(pre_request_release, -EINVAL);
ASSERT_EQ(-EINVAL, when_pre_release_lock_handler(exclusive_lock, false));
expect_is_state_pre_releasing(exclusive_lock, true);
ASSERT_EQ(-EINVAL, when_post_release_lock_handler(exclusive_lock, false,
-EINVAL));
}
TEST_F(TestMockExclusiveLock, ReacquireLock) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
// (try) acquire lock
MockPreAcquireRequest try_lock_pre_acquire;
expect_pre_acquire_request(try_lock_pre_acquire, 0);
ASSERT_EQ(0, when_pre_acquire_lock_handler(exclusive_lock));
MockPostAcquireRequest try_lock_post_acquire;
expect_post_acquire_request(exclusive_lock, try_lock_post_acquire, 0);
expect_is_state_acquiring(exclusive_lock, true);
expect_notify_acquired_lock(mock_image_ctx);
expect_unset_require_lock(mock_image_dispatch);
ASSERT_EQ(0, when_post_acquire_lock_handler(exclusive_lock, 0));
// reacquire lock
expect_notify_acquired_lock(mock_image_ctx);
ASSERT_EQ(0, when_post_reacquire_lock_handler(exclusive_lock, 0));
// shut down (and release)
expect_shut_down(exclusive_lock);
expect_is_state_waiting_for_lock(exclusive_lock, false);
ASSERT_EQ(0, when_shut_down(mock_image_ctx, exclusive_lock));
MockPreReleaseRequest shutdown_pre_release;
expect_pre_release_request(shutdown_pre_release, 0);
ASSERT_EQ(0, when_pre_release_lock_handler(exclusive_lock, true));
expect_shut_down_dispatch(mock_image_ctx);
expect_notify_released_lock(mock_image_ctx);
ASSERT_EQ(0, when_post_release_lock_handler(exclusive_lock, true, 0));
}
TEST_F(TestMockExclusiveLock, BlockRequests) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockExclusiveLockImageCtx mock_image_ctx(*ictx);
MockExclusiveLock *exclusive_lock = new MockExclusiveLock(mock_image_ctx);
exclusive_lock::MockPolicy mock_exclusive_lock_policy;
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&exclusive_lock) {
exclusive_lock->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
expect_set_state_initializing(exclusive_lock);
MockImageDispatch mock_image_dispatch;
expect_register_dispatch(mock_image_ctx);
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true);
expect_set_state_unlocked(exclusive_lock);
ASSERT_EQ(0, when_init(mock_image_ctx, exclusive_lock));
int ret_val;
expect_is_state_shutdown(exclusive_lock, false);
expect_is_state_locked(exclusive_lock, true);
ASSERT_TRUE(exclusive_lock->accept_request(
exclusive_lock::OPERATION_REQUEST_TYPE_GENERAL, &ret_val));
ASSERT_EQ(0, ret_val);
exclusive_lock->block_requests(-EROFS);
expect_is_state_shutdown(exclusive_lock, false);
expect_is_state_locked(exclusive_lock, true);
expect_accept_blocked_request(mock_image_ctx, mock_exclusive_lock_policy,
exclusive_lock::OPERATION_REQUEST_TYPE_GENERAL,
false);
ASSERT_FALSE(exclusive_lock->accept_request(
exclusive_lock::OPERATION_REQUEST_TYPE_GENERAL, &ret_val));
ASSERT_EQ(-EROFS, ret_val);
expect_is_state_shutdown(exclusive_lock, false);
expect_is_state_locked(exclusive_lock, true);
expect_accept_blocked_request(
mock_image_ctx, mock_exclusive_lock_policy,
exclusive_lock::OPERATION_REQUEST_TYPE_TRASH_SNAP_REMOVE, true);
ASSERT_TRUE(exclusive_lock->accept_request(
exclusive_lock::OPERATION_REQUEST_TYPE_TRASH_SNAP_REMOVE,
&ret_val));
ASSERT_EQ(0, ret_val);
exclusive_lock->unblock_requests();
expect_is_state_shutdown(exclusive_lock, false);
expect_is_state_locked(exclusive_lock, true);
ASSERT_TRUE(exclusive_lock->accept_request(
exclusive_lock::OPERATION_REQUEST_TYPE_GENERAL, &ret_val));
ASSERT_EQ(0, ret_val);
}
} // namespace librbd
| 29,663 | 34.653846 | 123 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_Journal.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/journal/mock/MockJournaler.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockJournalPolicy.h"
#include "test/librbd/mock/io/MockObjectDispatch.h"
#include "common/Cond.h"
#include "common/ceph_mutex.h"
#include "common/WorkQueue.h"
#include "cls/journal/cls_journal_types.h"
#include "journal/Journaler.h"
#include "librbd/Journal.h"
#include "librbd/Utils.h"
#include "librbd/io/AioCompletion.h"
#include "librbd/io/ObjectDispatchSpec.h"
#include "librbd/journal/Replay.h"
#include "librbd/journal/RemoveRequest.h"
#include "librbd/journal/CreateRequest.h"
#include "librbd/journal/ObjectDispatch.h"
#include "librbd/journal/OpenRequest.h"
#include "librbd/journal/Types.h"
#include "librbd/journal/TypeTraits.h"
#include "librbd/journal/PromoteRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <functional>
#include <list>
#include <boost/scope_exit.hpp>
#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_rbd
namespace librbd {
namespace {
struct MockJournalImageCtx : public MockImageCtx {
MockJournalImageCtx(librbd::ImageCtx& image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace journal {
template <>
struct TypeTraits<MockJournalImageCtx> {
typedef ::journal::MockJournalerProxy Journaler;
typedef ::journal::MockFutureProxy Future;
typedef ::journal::MockReplayEntryProxy ReplayEntry;
};
struct MockReplay {
static MockReplay *s_instance;
static MockReplay &get_instance() {
ceph_assert(s_instance != nullptr);
return *s_instance;
}
MockReplay() {
s_instance = this;
}
MOCK_METHOD2(shut_down, void(bool cancel_ops, Context *));
MOCK_METHOD2(decode, int(bufferlist::const_iterator*, EventEntry *));
MOCK_METHOD3(process, void(const EventEntry&, Context *, Context *));
MOCK_METHOD2(replay_op_ready, void(uint64_t, Context *));
};
template <>
class Replay<MockJournalImageCtx> {
public:
static Replay *create(MockJournalImageCtx &image_ctx) {
return new Replay();
}
void shut_down(bool cancel_ops, Context *on_finish) {
MockReplay::get_instance().shut_down(cancel_ops, on_finish);
}
int decode(bufferlist::const_iterator *it, EventEntry *event_entry) {
return MockReplay::get_instance().decode(it, event_entry);
}
void process(const EventEntry& event_entry, Context *on_ready,
Context *on_commit) {
MockReplay::get_instance().process(event_entry, on_ready, on_commit);
}
void replay_op_ready(uint64_t op_tid, Context *on_resume) {
MockReplay::get_instance().replay_op_ready(op_tid, on_resume);
}
};
MockReplay *MockReplay::s_instance = nullptr;
struct MockRemove {
static MockRemove *s_instance;
static MockRemove &get_instance() {
ceph_assert(s_instance != nullptr);
return *s_instance;
}
MockRemove() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
template <>
class RemoveRequest<MockJournalImageCtx> {
public:
static RemoveRequest *create(IoCtx &ioctx, const std::string &imageid,
const std::string &client_id,
ContextWQ *op_work_queue, Context *on_finish) {
return new RemoveRequest();
}
void send() {
MockRemove::get_instance().send();
}
};
MockRemove *MockRemove::s_instance = nullptr;
struct MockCreate {
static MockCreate *s_instance;
static MockCreate &get_instance() {
ceph_assert(s_instance != nullptr);
return *s_instance;
}
MockCreate() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
template<>
class CreateRequest<MockJournalImageCtx> {
public:
static CreateRequest *create(IoCtx &ioctx, const std::string &imageid,
uint8_t order, uint8_t splay_width,
const std::string &object_pool,
uint64_t tag_class, TagData &tag_data,
const std::string &client_id,
ContextWQ *op_work_queue, Context *on_finish) {
return new CreateRequest();
}
void send() {
MockCreate::get_instance().send();
}
};
MockCreate *MockCreate::s_instance = nullptr;
template<>
class OpenRequest<MockJournalImageCtx> {
public:
TagData *tag_data;
Context *on_finish;
static OpenRequest *s_instance;
static OpenRequest *create(MockJournalImageCtx *image_ctx,
::journal::MockJournalerProxy *journaler,
ceph::mutex *lock, journal::ImageClientMeta *client_meta,
uint64_t *tag_tid, journal::TagData *tag_data,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->tag_data = tag_data;
s_instance->on_finish = on_finish;
return s_instance;
}
OpenRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
OpenRequest<MockJournalImageCtx> *OpenRequest<MockJournalImageCtx>::s_instance = nullptr;
template <>
class PromoteRequest<MockJournalImageCtx> {
public:
static PromoteRequest s_instance;
static PromoteRequest *create(MockJournalImageCtx *image_ctx, bool force,
Context *on_finish) {
return &s_instance;
}
MOCK_METHOD0(send, void());
};
PromoteRequest<MockJournalImageCtx> PromoteRequest<MockJournalImageCtx>::s_instance;
template <>
struct ObjectDispatch<MockJournalImageCtx> : public io::MockObjectDispatch {
static ObjectDispatch* s_instance;
static ObjectDispatch* create(MockJournalImageCtx* image_ctx,
Journal<MockJournalImageCtx>* journal) {
ceph_assert(s_instance != nullptr);
return s_instance;
}
ObjectDispatch() {
s_instance = this;
}
};
ObjectDispatch<MockJournalImageCtx>* ObjectDispatch<MockJournalImageCtx>::s_instance = nullptr;
} // namespace journal
} // namespace librbd
// template definitions
#include "librbd/Journal.cc"
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::MatcherCast;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::SetArgPointee;
using ::testing::WithArg;
using namespace std::placeholders;
ACTION_P2(StartReplay, wq, ctx) {
wq->queue(ctx, 0);
}
namespace librbd {
class TestMockJournal : public TestMockFixture {
public:
typedef journal::MockReplay MockJournalReplay;
typedef Journal<MockJournalImageCtx> MockJournal;
typedef journal::OpenRequest<MockJournalImageCtx> MockJournalOpenRequest;
typedef journal::ObjectDispatch<MockJournalImageCtx> MockObjectDispatch;
typedef std::function<void(::journal::ReplayHandler*)> ReplayAction;
typedef std::list<Context *> Contexts;
TestMockJournal() = default;
~TestMockJournal() override {
ceph_assert(m_commit_contexts.empty());
}
ceph::mutex m_lock = ceph::make_mutex("lock");
ceph::condition_variable m_cond;
Contexts m_commit_contexts;
struct C_ReplayAction : public Context {
::journal::ReplayHandler **replay_handler;
ReplayAction replay_action;
C_ReplayAction(::journal::ReplayHandler **replay_handler,
const ReplayAction &replay_action)
: replay_handler(replay_handler), replay_action(replay_action) {
}
void finish(int r) override {
if (replay_action) {
replay_action(*replay_handler);
}
}
};
void expect_construct_journaler(::journal::MockJournaler &mock_journaler) {
EXPECT_CALL(mock_journaler, construct());
}
void expect_open_journaler(MockImageCtx &mock_image_ctx,
::journal::MockJournaler &mock_journaler,
MockJournalOpenRequest &mock_open_request,
bool primary, int r) {
EXPECT_CALL(mock_journaler, add_listener(_))
.WillOnce(SaveArg<0>(&m_listener));
EXPECT_CALL(mock_open_request, send())
.WillOnce(DoAll(Invoke([&mock_open_request, primary]() {
if (!primary) {
mock_open_request.tag_data->mirror_uuid = "remote mirror uuid";
}
}),
FinishRequest(&mock_open_request, r,
&mock_image_ctx)));
}
void expect_shut_down_journaler(::journal::MockJournaler &mock_journaler) {
EXPECT_CALL(mock_journaler, remove_listener(_));
EXPECT_CALL(mock_journaler, shut_down(_))
.WillOnce(CompleteContext(0, static_cast<asio::ContextWQ*>(NULL)));
}
void expect_register_dispatch(MockImageCtx& mock_image_ctx,
MockObjectDispatch& mock_object_dispatch) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher,
register_dispatch(&mock_object_dispatch));
}
void expect_shut_down_dispatch(MockImageCtx& mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher,
shut_down_dispatch(io::OBJECT_DISPATCH_LAYER_JOURNAL, _))
.WillOnce(WithArg<1>(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue)));
}
void expect_get_max_append_size(::journal::MockJournaler &mock_journaler,
uint32_t max_size) {
EXPECT_CALL(mock_journaler, get_max_append_size())
.WillOnce(Return(max_size));
}
void expect_get_journaler_cached_client(::journal::MockJournaler &mock_journaler,
const journal::ImageClientMeta &client_meta,
int r) {
journal::ClientData client_data;
client_data.client_meta = client_meta;
cls::journal::Client client;
encode(client_data, client.data);
EXPECT_CALL(mock_journaler, get_cached_client("", _))
.WillOnce(DoAll(SetArgPointee<1>(client),
Return(r)));
}
void expect_get_journaler_tags(MockImageCtx &mock_image_ctx,
::journal::MockJournaler &mock_journaler,
uint64_t start_after_tag_tid,
::journal::Journaler::Tags &&tags, int r) {
EXPECT_CALL(mock_journaler, get_tags(start_after_tag_tid, 0, _, _))
.WillOnce(DoAll(SetArgPointee<2>(tags),
WithArg<3>(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue))));
}
void expect_start_replay(MockJournalImageCtx &mock_image_ctx,
::journal::MockJournaler &mock_journaler,
const ReplayAction &action) {
EXPECT_CALL(mock_journaler, start_replay(_))
.WillOnce(DoAll(SaveArg<0>(&m_replay_handler),
StartReplay(mock_image_ctx.image_ctx->op_work_queue,
new C_ReplayAction(&m_replay_handler,
action))));
}
void expect_stop_replay(::journal::MockJournaler &mock_journaler) {
EXPECT_CALL(mock_journaler, stop_replay(_))
.WillOnce(CompleteContext(0, static_cast<asio::ContextWQ*>(NULL)));
}
void expect_shut_down_replay(MockJournalImageCtx &mock_image_ctx,
MockJournalReplay &mock_journal_replay, int r,
bool cancel_ops = false) {
EXPECT_CALL(mock_journal_replay, shut_down(cancel_ops, _))
.WillOnce(WithArg<1>(Invoke([this, &mock_image_ctx, r](Context *on_flush) {
this->commit_replay(mock_image_ctx, on_flush, r);})));
}
void expect_get_data(::journal::MockReplayEntry &mock_replay_entry) {
EXPECT_CALL(mock_replay_entry, get_data())
.WillOnce(Return(bufferlist()));
}
void expect_try_pop_front(MockJournalImageCtx &mock_image_ctx,
::journal::MockJournaler &mock_journaler,
bool entries_available,
::journal::MockReplayEntry &mock_replay_entry,
const ReplayAction &action = {}) {
EXPECT_CALL(mock_journaler, try_pop_front(_))
.WillOnce(DoAll(SetArgPointee<0>(::journal::MockReplayEntryProxy()),
StartReplay(mock_image_ctx.image_ctx->op_work_queue,
new C_ReplayAction(&m_replay_handler,
action)),
Return(entries_available)));
if (entries_available) {
expect_get_data(mock_replay_entry);
}
}
void expect_replay_process(MockJournalReplay &mock_journal_replay) {
EXPECT_CALL(mock_journal_replay, decode(_, _))
.WillOnce(Return(0));
EXPECT_CALL(mock_journal_replay, process(_, _, _))
.WillOnce(DoAll(WithArg<1>(CompleteContext(0, static_cast<asio::ContextWQ*>(NULL))),
WithArg<2>(Invoke(this, &TestMockJournal::save_commit_context))));
}
void expect_start_append(::journal::MockJournaler &mock_journaler) {
EXPECT_CALL(mock_journaler, start_append(_));
}
void expect_set_append_batch_options(MockJournalImageCtx &mock_image_ctx,
::journal::MockJournaler &mock_journaler,
bool user_flushed) {
if (mock_image_ctx.image_ctx->config.get_val<bool>("rbd_journal_object_writethrough_until_flush") ==
user_flushed) {
EXPECT_CALL(mock_journaler, set_append_batch_options(_, _, _));
}
}
void expect_stop_append(::journal::MockJournaler &mock_journaler, int r) {
EXPECT_CALL(mock_journaler, stop_append(_))
.WillOnce(CompleteContext(r, static_cast<asio::ContextWQ*>(NULL)));
}
void expect_committed(::journal::MockJournaler &mock_journaler,
size_t events) {
EXPECT_CALL(mock_journaler, committed(MatcherCast<const ::journal::MockReplayEntryProxy&>(_)))
.Times(events);
}
void expect_append_journaler(::journal::MockJournaler &mock_journaler) {
EXPECT_CALL(mock_journaler, append(_, _))
.WillOnce(Return(::journal::MockFutureProxy()));
}
void expect_wait_future(::journal::MockFuture &mock_future,
Context **on_safe) {
EXPECT_CALL(mock_future, wait(_))
.WillOnce(SaveArg<0>(on_safe));
}
void expect_future_committed(::journal::MockJournaler &mock_journaler) {
EXPECT_CALL(mock_journaler, committed(MatcherCast<const ::journal::MockFutureProxy&>(_)));
}
void expect_future_is_valid(::journal::MockFuture &mock_future) {
EXPECT_CALL(mock_future, is_valid()).WillOnce(Return(false));
}
void expect_flush_commit_position(::journal::MockJournaler &mock_journaler) {
EXPECT_CALL(mock_journaler, flush_commit_position(_))
.WillOnce(CompleteContext(0, static_cast<asio::ContextWQ*>(NULL)));
}
int when_open(MockJournal *mock_journal) {
C_SaferCond ctx;
mock_journal->open(&ctx);
return ctx.wait();
}
int when_close(MockJournal *mock_journal) {
C_SaferCond ctx;
mock_journal->close(&ctx);
return ctx.wait();
}
uint64_t when_append_write_event(MockJournalImageCtx &mock_image_ctx,
MockJournal *mock_journal, uint64_t length) {
bufferlist bl;
bl.append_zero(length);
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
return mock_journal->append_write_event(0, length, bl, false);
}
uint64_t when_append_compare_and_write_event(
MockJournalImageCtx &mock_image_ctx, MockJournal *mock_journal,
uint64_t length) {
bufferlist cmp_bl;
cmp_bl.append_zero(length);
bufferlist write_bl;
write_bl.append_zero(length);
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
return mock_journal->append_compare_and_write_event(0, length, cmp_bl,
write_bl, false);
}
uint64_t when_append_io_event(MockJournalImageCtx &mock_image_ctx,
MockJournal *mock_journal,
int filter_ret_val) {
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
return mock_journal->append_io_event(
journal::EventEntry{journal::AioFlushEvent{}}, 0, 0, false,
filter_ret_val);
}
void save_commit_context(Context *ctx) {
std::lock_guard locker{m_lock};
m_commit_contexts.push_back(ctx);
m_cond.notify_all();
}
void wake_up() {
std::lock_guard locker{m_lock};
m_cond.notify_all();
}
void commit_replay(MockJournalImageCtx &mock_image_ctx, Context *on_flush,
int r) {
Contexts commit_contexts;
std::swap(commit_contexts, m_commit_contexts);
derr << "SHUT DOWN REPLAY START" << dendl;
for (auto ctx : commit_contexts) {
mock_image_ctx.image_ctx->op_work_queue->queue(ctx, r);
}
on_flush = new LambdaContext([on_flush](int r) {
derr << "FLUSH START" << dendl;
on_flush->complete(r);
derr << "FLUSH FINISH" << dendl;
});
mock_image_ctx.image_ctx->op_work_queue->queue(on_flush, 0);
derr << "SHUT DOWN REPLAY FINISH" << dendl;
}
void open_journal(MockJournalImageCtx &mock_image_ctx,
MockJournal *mock_journal,
MockObjectDispatch& mock_object_dispatch,
::journal::MockJournaler &mock_journaler,
MockJournalOpenRequest &mock_open_request,
bool primary = true) {
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
primary, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_complete, _1, 0));
MockJournalReplay mock_journal_replay;
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0);
expect_committed(mock_journaler, 0);
expect_flush_commit_position(mock_journaler);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
ASSERT_EQ(0, when_open(mock_journal));
}
void close_journal(MockJournalImageCtx& mock_image_ctx,
MockJournal *mock_journal,
::journal::MockJournaler &mock_journaler) {
expect_stop_append(mock_journaler, 0);
expect_shut_down_dispatch(mock_image_ctx);
ASSERT_EQ(0, when_close(mock_journal));
}
static void invoke_replay_ready(::journal::ReplayHandler *handler) {
handler->handle_entries_available();
}
static void invoke_replay_complete(::journal::ReplayHandler *handler, int r) {
handler->handle_complete(r);
}
::journal::ReplayHandler *m_replay_handler = nullptr;
::journal::JournalMetadataListener *m_listener = nullptr;
};
TEST_F(TestMockJournal, StateTransitions) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
MockObjectDispatch mock_object_dispatch;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_ready, _1));
::journal::MockReplayEntry mock_replay_entry;
MockJournalReplay mock_journal_replay;
expect_try_pop_front(mock_image_ctx, mock_journaler, true, mock_replay_entry);
expect_replay_process(mock_journal_replay);
expect_try_pop_front(mock_image_ctx, mock_journaler, true, mock_replay_entry);
expect_replay_process(mock_journal_replay);
expect_try_pop_front(mock_image_ctx, mock_journaler, false, mock_replay_entry,
std::bind(&invoke_replay_ready, _1));
expect_try_pop_front(mock_image_ctx, mock_journaler, true, mock_replay_entry);
expect_replay_process(mock_journal_replay);
expect_try_pop_front(mock_image_ctx, mock_journaler, false, mock_replay_entry,
std::bind(&invoke_replay_complete, _1, 0));
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0);
expect_committed(mock_journaler, 3);
expect_flush_commit_position(mock_journaler);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
ASSERT_EQ(0, when_open(mock_journal));
expect_stop_append(mock_journaler, 0);
expect_shut_down_journaler(mock_journaler);
expect_shut_down_dispatch(mock_image_ctx);
ASSERT_EQ(0, when_close(mock_journal));
}
TEST_F(TestMockJournal, InitError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
MockObjectDispatch mock_object_dispatch;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, -EINVAL);
expect_shut_down_journaler(mock_journaler);
ASSERT_EQ(-EINVAL, when_open(mock_journal));
}
TEST_F(TestMockJournal, ReplayCompleteError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
MockObjectDispatch mock_object_dispatch;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_complete, _1, -EINVAL));
MockJournalReplay mock_journal_replay;
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0, true);
expect_flush_commit_position(mock_journaler);
expect_shut_down_journaler(mock_journaler);
// replay failure should result in replay-restart
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_complete, _1, 0));
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0);
expect_flush_commit_position(mock_journaler);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
ASSERT_EQ(0, when_open(mock_journal));
expect_stop_append(mock_journaler, 0);
expect_shut_down_journaler(mock_journaler);
expect_shut_down_dispatch(mock_image_ctx);
ASSERT_EQ(0, when_close(mock_journal));
}
TEST_F(TestMockJournal, FlushReplayError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
MockObjectDispatch mock_object_dispatch;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_ready, _1));
::journal::MockReplayEntry mock_replay_entry;
MockJournalReplay mock_journal_replay;
expect_try_pop_front(mock_image_ctx, mock_journaler, true, mock_replay_entry);
expect_replay_process(mock_journal_replay);
expect_try_pop_front(mock_image_ctx, mock_journaler, false, mock_replay_entry,
std::bind(&invoke_replay_complete, _1, 0));
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, -EINVAL);
expect_flush_commit_position(mock_journaler);
expect_shut_down_journaler(mock_journaler);
// replay flush failure should result in replay-restart
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_complete, _1, 0));
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0);
expect_flush_commit_position(mock_journaler);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
ASSERT_EQ(0, when_open(mock_journal));
expect_stop_append(mock_journaler, 0);
expect_shut_down_journaler(mock_journaler);
expect_shut_down_dispatch(mock_image_ctx);
ASSERT_EQ(0, when_close(mock_journal));
}
TEST_F(TestMockJournal, CorruptEntry) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
MockObjectDispatch mock_object_dispatch;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_ready, _1));
::journal::MockReplayEntry mock_replay_entry;
MockJournalReplay mock_journal_replay;
expect_try_pop_front(mock_image_ctx, mock_journaler, true, mock_replay_entry);
EXPECT_CALL(mock_journal_replay, decode(_, _)).WillOnce(Return(-EBADMSG));
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0, true);
expect_flush_commit_position(mock_journaler);
expect_shut_down_journaler(mock_journaler);
// replay failure should result in replay-restart
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_complete, _1, 0));
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0);
expect_flush_commit_position(mock_journaler);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
ASSERT_EQ(0, when_open(mock_journal));
expect_stop_append(mock_journaler, -EINVAL);
expect_shut_down_journaler(mock_journaler);
expect_shut_down_dispatch(mock_image_ctx);
ASSERT_EQ(-EINVAL, when_close(mock_journal));
}
TEST_F(TestMockJournal, StopError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
MockObjectDispatch mock_object_dispatch;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_complete, _1, 0));
MockJournalReplay mock_journal_replay;
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0);
expect_flush_commit_position(mock_journaler);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
ASSERT_EQ(0, when_open(mock_journal));
expect_stop_append(mock_journaler, -EINVAL);
expect_shut_down_journaler(mock_journaler);
expect_shut_down_dispatch(mock_image_ctx);
ASSERT_EQ(-EINVAL, when_close(mock_journal));
}
TEST_F(TestMockJournal, ReplayOnDiskPreFlushError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
MockObjectDispatch mock_object_dispatch;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_ready, _1));
::journal::MockReplayEntry mock_replay_entry;
MockJournalReplay mock_journal_replay;
expect_try_pop_front(mock_image_ctx, mock_journaler, true, mock_replay_entry);
EXPECT_CALL(mock_journal_replay, decode(_, _))
.WillOnce(Return(0));
Context *on_ready;
EXPECT_CALL(mock_journal_replay, process(_, _, _))
.WillOnce(DoAll(SaveArg<1>(&on_ready),
WithArg<2>(Invoke(this, &TestMockJournal::save_commit_context))));
expect_try_pop_front(mock_image_ctx, mock_journaler, false,
mock_replay_entry);
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0, true);
expect_flush_commit_position(mock_journaler);
expect_shut_down_journaler(mock_journaler);
// replay write-to-disk failure should result in replay-restart
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler, {
std::bind(&invoke_replay_complete, _1, 0)
});
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0);
expect_flush_commit_position(mock_journaler);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
C_SaferCond ctx;
mock_journal->open(&ctx);
// wait for the process callback
{
std::unique_lock locker{m_lock};
m_cond.wait(locker, [this] { return !m_commit_contexts.empty(); });
}
on_ready->complete(0);
// inject RADOS error in the middle of replay
Context *on_safe = m_commit_contexts.front();
m_commit_contexts.clear();
on_safe->complete(-EINVAL);
// flag the replay as complete
m_replay_handler->handle_complete(0);
ASSERT_EQ(0, ctx.wait());
expect_stop_append(mock_journaler, 0);
expect_shut_down_journaler(mock_journaler);
expect_shut_down_dispatch(mock_image_ctx);
ASSERT_EQ(0, when_close(mock_journal));
}
TEST_F(TestMockJournal, ReplayOnDiskPostFlushError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
InSequence seq;
MockObjectDispatch mock_object_dispatch;
expect_register_dispatch(mock_image_ctx, mock_object_dispatch);
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_ready, _1));
::journal::MockReplayEntry mock_replay_entry;
MockJournalReplay mock_journal_replay;
expect_try_pop_front(mock_image_ctx, mock_journaler, true, mock_replay_entry);
expect_replay_process(mock_journal_replay);
expect_try_pop_front(mock_image_ctx, mock_journaler, false, mock_replay_entry,
std::bind(&invoke_replay_complete, _1, 0));
expect_stop_replay(mock_journaler);
Context *on_flush = nullptr;
EXPECT_CALL(mock_journal_replay, shut_down(false, _))
.WillOnce(DoAll(SaveArg<1>(&on_flush),
InvokeWithoutArgs(this, &TestMockJournal::wake_up)));
expect_flush_commit_position(mock_journaler);
// replay write-to-disk failure should result in replay-restart
expect_shut_down_journaler(mock_journaler);
expect_construct_journaler(mock_journaler);
expect_open_journaler(mock_image_ctx, mock_journaler, mock_open_request,
true, 0);
expect_get_max_append_size(mock_journaler, 1 << 16);
expect_start_replay(
mock_image_ctx, mock_journaler,
std::bind(&invoke_replay_complete, _1, 0));
expect_stop_replay(mock_journaler);
expect_shut_down_replay(mock_image_ctx, mock_journal_replay, 0);
expect_flush_commit_position(mock_journaler);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
C_SaferCond ctx;
mock_journal->open(&ctx);
// proceed with the flush
{
// wait for on_flush callback
std::unique_lock locker{m_lock};
m_cond.wait(locker, [&] {return on_flush != nullptr;});
}
{
// wait for the on_safe process callback
std::unique_lock locker{m_lock};
m_cond.wait(locker, [this] {return !m_commit_contexts.empty();});
}
m_commit_contexts.front()->complete(-EINVAL);
m_commit_contexts.clear();
on_flush->complete(0);
ASSERT_EQ(0, ctx.wait());
expect_stop_append(mock_journaler, 0);
expect_shut_down_journaler(mock_journaler);
expect_shut_down_dispatch(mock_image_ctx);
ASSERT_EQ(0, when_close(mock_journal));
}
TEST_F(TestMockJournal, EventAndIOCommitOrder) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
::journal::MockFuture mock_future;
Context *on_journal_safe1;
expect_append_journaler(mock_journaler);
expect_wait_future(mock_future, &on_journal_safe1);
ASSERT_EQ(1U, when_append_io_event(mock_image_ctx, mock_journal, 0));
mock_journal->get_work_queue()->drain();
Context *on_journal_safe2;
expect_append_journaler(mock_journaler);
expect_wait_future(mock_future, &on_journal_safe2);
ASSERT_EQ(2U, when_append_io_event(mock_image_ctx, mock_journal, 0));
mock_journal->get_work_queue()->drain();
// commit journal event followed by IO event (standard)
on_journal_safe1->complete(0);
ictx->op_work_queue->drain();
expect_future_committed(mock_journaler);
mock_journal->commit_io_event(1U, 0);
// commit IO event followed by journal event (cache overwrite)
mock_journal->commit_io_event(2U, 0);
expect_future_committed(mock_journaler);
C_SaferCond event_ctx;
mock_journal->wait_event(2U, &event_ctx);
on_journal_safe2->complete(0);
ictx->op_work_queue->drain();
ASSERT_EQ(0, event_ctx.wait());
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, AppendWriteEvent) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
InSequence seq;
::journal::MockFuture mock_future;
Context *on_journal_safe = nullptr;
expect_append_journaler(mock_journaler);
expect_append_journaler(mock_journaler);
expect_append_journaler(mock_journaler);
expect_wait_future(mock_future, &on_journal_safe);
ASSERT_EQ(1U, when_append_write_event(mock_image_ctx, mock_journal, 1 << 17));
mock_journal->get_work_queue()->drain();
on_journal_safe->complete(0);
C_SaferCond event_ctx;
mock_journal->wait_event(1U, &event_ctx);
ASSERT_EQ(0, event_ctx.wait());
expect_future_committed(mock_journaler);
expect_future_committed(mock_journaler);
expect_future_committed(mock_journaler);
mock_journal->commit_io_event(1U, 0);
ictx->op_work_queue->drain();
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, AppendCompareAndWriteEvent) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
InSequence seq;
::journal::MockFuture mock_future;
Context *on_journal_safe = nullptr;
expect_append_journaler(mock_journaler);
expect_append_journaler(mock_journaler);
expect_append_journaler(mock_journaler);
expect_wait_future(mock_future, &on_journal_safe);
ASSERT_EQ(1U, when_append_compare_and_write_event(mock_image_ctx,
mock_journal,
1 << 16));
mock_journal->get_work_queue()->drain();
on_journal_safe->complete(0);
C_SaferCond event_ctx;
mock_journal->wait_event(1U, &event_ctx);
ASSERT_EQ(0, event_ctx.wait());
expect_future_committed(mock_journaler);
expect_future_committed(mock_journaler);
expect_future_committed(mock_journaler);
mock_journal->commit_io_event(1U, 0);
ictx->op_work_queue->drain();
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, EventCommitError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
::journal::MockFuture mock_future;
Context *on_journal_safe;
expect_append_journaler(mock_journaler);
expect_wait_future(mock_future, &on_journal_safe);
ASSERT_EQ(1U, when_append_io_event(mock_image_ctx, mock_journal, 0));
mock_journal->get_work_queue()->drain();
// commit the event in the journal w/o waiting writeback
expect_future_committed(mock_journaler);
C_SaferCond object_request_ctx;
mock_journal->wait_event(1U, &object_request_ctx);
on_journal_safe->complete(-EINVAL);
ASSERT_EQ(-EINVAL, object_request_ctx.wait());
// cache should receive the error after attempting writeback
expect_future_is_valid(mock_future);
C_SaferCond flush_ctx;
mock_journal->flush_event(1U, &flush_ctx);
ASSERT_EQ(-EINVAL, flush_ctx.wait());
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, EventCommitErrorWithPendingWriteback) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
::journal::MockFuture mock_future;
Context *on_journal_safe;
expect_append_journaler(mock_journaler);
expect_wait_future(mock_future, &on_journal_safe);
ASSERT_EQ(1U, when_append_io_event(mock_image_ctx, mock_journal, 0));
mock_journal->get_work_queue()->drain();
expect_future_is_valid(mock_future);
C_SaferCond flush_ctx;
mock_journal->flush_event(1U, &flush_ctx);
// commit the event in the journal w/ waiting cache writeback
expect_future_committed(mock_journaler);
C_SaferCond object_request_ctx;
mock_journal->wait_event(1U, &object_request_ctx);
on_journal_safe->complete(-EINVAL);
ASSERT_EQ(-EINVAL, object_request_ctx.wait());
// cache should receive the error if waiting
ASSERT_EQ(-EINVAL, flush_ctx.wait());
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, IOCommitError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
::journal::MockFuture mock_future;
Context *on_journal_safe;
expect_append_journaler(mock_journaler);
expect_wait_future(mock_future, &on_journal_safe);
ASSERT_EQ(1U, when_append_io_event(mock_image_ctx, mock_journal, 0));
mock_journal->get_work_queue()->drain();
// failed IO remains uncommitted in journal
on_journal_safe->complete(0);
ictx->op_work_queue->drain();
mock_journal->commit_io_event(1U, -EINVAL);
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, IOCommitErrorFiltered) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
::journal::MockFuture mock_future;
Context *on_journal_safe;
expect_append_journaler(mock_journaler);
expect_wait_future(mock_future, &on_journal_safe);
ASSERT_EQ(1U, when_append_io_event(mock_image_ctx, mock_journal, -EILSEQ));
mock_journal->get_work_queue()->drain();
// filter failed IO committed in journal
on_journal_safe->complete(0);
ictx->op_work_queue->drain();
expect_future_committed(mock_journaler);
mock_journal->commit_io_event(1U, -EILSEQ);
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, FlushCommitPosition) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
expect_flush_commit_position(mock_journaler);
C_SaferCond ctx;
mock_journal->flush_commit_position(&ctx);
ASSERT_EQ(0, ctx.wait());
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, ExternalReplay) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
InSequence seq;
expect_stop_append(mock_journaler, 0);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
expect_shut_down_journaler(mock_journaler);
C_SaferCond start_ctx;
journal::Replay<MockJournalImageCtx> *journal_replay = nullptr;
mock_journal->start_external_replay(&journal_replay, &start_ctx);
ASSERT_EQ(0, start_ctx.wait());
mock_journal->stop_external_replay();
}
TEST_F(TestMockJournal, ExternalReplayFailure) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
InSequence seq;
expect_stop_append(mock_journaler, -EINVAL);
expect_start_append(mock_journaler);
expect_set_append_batch_options(mock_image_ctx, mock_journaler, false);
expect_shut_down_journaler(mock_journaler);
C_SaferCond start_ctx;
journal::Replay<MockJournalImageCtx> *journal_replay = nullptr;
mock_journal->start_external_replay(&journal_replay, &start_ctx);
ASSERT_EQ(-EINVAL, start_ctx.wait());
}
TEST_F(TestMockJournal, AppendDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
MockJournalPolicy mock_journal_policy;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
InSequence seq;
std::shared_lock image_locker{mock_image_ctx.image_lock};
EXPECT_CALL(mock_image_ctx, get_journal_policy()).WillOnce(
Return(ictx->get_journal_policy()));
ASSERT_TRUE(mock_journal->is_journal_appending());
EXPECT_CALL(mock_image_ctx, get_journal_policy()).WillOnce(
Return(&mock_journal_policy));
EXPECT_CALL(mock_journal_policy, append_disabled()).WillOnce(Return(true));
ASSERT_FALSE(mock_journal->is_journal_appending());
expect_shut_down_journaler(mock_journaler);
}
TEST_F(TestMockJournal, CloseListenerEvent) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
BOOST_SCOPE_EXIT(&mock_journal) {
mock_journal->put();
} BOOST_SCOPE_EXIT_END
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
struct Listener : public journal::Listener {
C_SaferCond ctx;
void handle_close() override {
ctx.complete(0);
}
void handle_resync() override {
ADD_FAILURE() << "unexpected resync request";
}
void handle_promoted() override {
ADD_FAILURE() << "unexpected promotion event";
}
} listener;
mock_journal->add_listener(&listener);
expect_shut_down_journaler(mock_journaler);
close_journal(mock_image_ctx, mock_journal, mock_journaler);
ASSERT_EQ(0, listener.ctx.wait());
mock_journal->remove_listener(&listener);
}
TEST_F(TestMockJournal, ResyncRequested) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request,
false);
struct Listener : public journal::Listener {
C_SaferCond ctx;
void handle_close() override {
ADD_FAILURE() << "unexpected close action";
}
void handle_resync() override {
ctx.complete(0);
}
void handle_promoted() override {
ADD_FAILURE() << "unexpected promotion event";
}
} listener;
mock_journal->add_listener(&listener);
BOOST_SCOPE_EXIT_ALL(&) {
mock_journal->remove_listener(&listener);
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
InSequence seq;
journal::TagData tag_data;
tag_data.mirror_uuid = Journal<>::LOCAL_MIRROR_UUID;
bufferlist tag_data_bl;
encode(tag_data, tag_data_bl);
expect_get_journaler_tags(mock_image_ctx, mock_journaler, 0,
{{0, 0, tag_data_bl}}, 0);
journal::ImageClientMeta image_client_meta;
image_client_meta.tag_class = 0;
image_client_meta.resync_requested = true;
expect_get_journaler_cached_client(mock_journaler, image_client_meta, 0);
expect_shut_down_journaler(mock_journaler);
m_listener->handle_update(nullptr);
ASSERT_EQ(0, listener.ctx.wait());
}
TEST_F(TestMockJournal, ForcePromoted) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request, false);
struct Listener : public journal::Listener {
C_SaferCond ctx;
void handle_close() override {
ADD_FAILURE() << "unexpected close action";
}
void handle_resync() override {
ADD_FAILURE() << "unexpected resync event";
}
void handle_promoted() override {
ctx.complete(0);
}
} listener;
mock_journal->add_listener(&listener);
BOOST_SCOPE_EXIT_ALL(&) {
mock_journal->remove_listener(&listener);
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
InSequence seq;
journal::TagData tag_data;
tag_data.mirror_uuid = Journal<>::LOCAL_MIRROR_UUID;
bufferlist tag_data_bl;
encode(tag_data, tag_data_bl);
expect_get_journaler_tags(mock_image_ctx, mock_journaler, 0,
{{100, 0, tag_data_bl}}, 0);
journal::ImageClientMeta image_client_meta;
image_client_meta.tag_class = 0;
expect_get_journaler_cached_client(mock_journaler, image_client_meta, 0);
expect_shut_down_journaler(mock_journaler);
m_listener->handle_update(nullptr);
ASSERT_EQ(0, listener.ctx.wait());
}
TEST_F(TestMockJournal, UserFlushed) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockJournalImageCtx mock_image_ctx(*ictx);
MockJournal *mock_journal = new MockJournal(mock_image_ctx);
MockObjectDispatch mock_object_dispatch;
::journal::MockJournaler mock_journaler;
MockJournalOpenRequest mock_open_request;
open_journal(mock_image_ctx, mock_journal, mock_object_dispatch,
mock_journaler, mock_open_request);
BOOST_SCOPE_EXIT_ALL(&) {
close_journal(mock_image_ctx, mock_journal, mock_journaler);
mock_journal->put();
};
expect_set_append_batch_options(mock_image_ctx, mock_journaler, true);
mock_journal->user_flushed();
expect_shut_down_journaler(mock_journaler);
}
} // namespace librbd
| 56,280 | 33.464789 | 108 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_ManagedLock.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/ManagedLock.h"
#include "librbd/managed_lock/AcquireRequest.h"
#include "librbd/managed_lock/BreakRequest.h"
#include "librbd/managed_lock/GetLockerRequest.h"
#include "librbd/managed_lock/ReacquireRequest.h"
#include "librbd/managed_lock/ReleaseRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <list>
using namespace std;
namespace librbd {
struct MockManagedLockImageCtx : public MockImageCtx {
explicit MockManagedLockImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {}
};
namespace watcher {
template <>
struct Traits<MockManagedLockImageCtx> {
typedef librbd::MockImageWatcher Watcher;
};
}
struct MockMockManagedLock : public ManagedLock<MockManagedLockImageCtx> {
MockMockManagedLock(librados::IoCtx& ioctx, AsioEngine& asio_engine,
const std::string& oid, librbd::MockImageWatcher *watcher,
managed_lock::Mode mode, bool blocklist_on_break_lock,
uint32_t blocklist_expire_seconds)
: ManagedLock<MockManagedLockImageCtx>(ioctx, asio_engine, oid, watcher,
librbd::managed_lock::EXCLUSIVE, true, 0) {
};
virtual ~MockMockManagedLock() = default;
MOCK_METHOD2(post_reacquire_lock_handler, void(int, Context*));
MOCK_METHOD2(pre_release_lock_handler, void(bool, Context*));
MOCK_METHOD3(post_release_lock_handler, void(bool, int, Context*));
};
namespace managed_lock {
template<typename T>
struct BaseRequest {
static std::list<T *> s_requests;
Context *on_finish = nullptr;
static T* create(librados::IoCtx& ioctx, MockImageWatcher *watcher,
const std::string& oid, const std::string& cookie,
Context *on_finish) {
ceph_assert(!s_requests.empty());
T* req = s_requests.front();
req->on_finish = on_finish;
s_requests.pop_front();
return req;
}
BaseRequest() {
s_requests.push_back(reinterpret_cast<T*>(this));
}
};
template<typename T>
std::list<T *> BaseRequest<T>::s_requests;
template <>
struct AcquireRequest<MockManagedLockImageCtx>
: public BaseRequest<AcquireRequest<MockManagedLockImageCtx> > {
static AcquireRequest* create(librados::IoCtx& ioctx,
MockImageWatcher *watcher,
AsioEngine& asio_engine,
const std::string& oid,
const std::string& cookie,
bool exclusive, bool blocklist_on_break_lock,
uint32_t blocklist_expire_seconds,
Context *on_finish) {
return BaseRequest::create(ioctx, watcher, oid, cookie, on_finish);
}
MOCK_METHOD0(send, void());
};
template <>
struct ReacquireRequest<MockManagedLockImageCtx> : public BaseRequest<ReacquireRequest<MockManagedLockImageCtx> > {
static ReacquireRequest* create(librados::IoCtx &ioctx, const std::string& oid,
const string& old_cookie, const std::string& new_cookie,
bool exclusive, Context *on_finish) {
return BaseRequest::create(ioctx, nullptr, oid, new_cookie,
on_finish);
}
MOCK_METHOD0(send, void());
};
template <>
struct ReleaseRequest<MockManagedLockImageCtx> : public BaseRequest<ReleaseRequest<MockManagedLockImageCtx> > {
static ReleaseRequest* create(librados::IoCtx& ioctx, MockImageWatcher *watcher,
asio::ContextWQ *work_queue,
const std::string& oid,
const std::string& cookie, Context *on_finish) {
return BaseRequest::create(ioctx, watcher, oid, cookie, on_finish);
}
MOCK_METHOD0(send, void());
};
template <>
struct GetLockerRequest<MockManagedLockImageCtx> {
static GetLockerRequest* create(librados::IoCtx& ioctx,
const std::string& oid, bool exclusive,
Locker *locker, Context *on_finish) {
ceph_abort_msg("unexpected call");
}
void send() {
ceph_abort_msg("unexpected call");
}
};
template <>
struct BreakRequest<MockManagedLockImageCtx> {
static BreakRequest* create(librados::IoCtx& ioctx,
AsioEngine& asio_engine,
const std::string& oid, const Locker &locker,
bool exclusive, bool blocklist_locker,
uint32_t blocklist_expire_seconds,
bool force_break_lock, Context *on_finish) {
ceph_abort_msg("unexpected call");
}
void send() {
ceph_abort_msg("unexpected call");
}
};
} // namespace managed_lock
} // namespace librbd
// template definitions
#include "librbd/ManagedLock.cc"
template class librbd::ManagedLock<librbd::MockManagedLockImageCtx>;
ACTION_P3(QueueRequest, request, r, wq) {
if (request->on_finish != nullptr) {
if (wq != nullptr) {
wq->queue(request->on_finish, r);
} else {
request->on_finish->complete(r);
}
}
}
ACTION_P2(QueueContext, r, wq) {
wq->queue(arg0, r);
}
ACTION_P(Notify, ctx) {
ctx->complete(0);
}
namespace librbd {
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::WithArg;
class TestMockManagedLock : public TestMockFixture {
public:
typedef ManagedLock<MockManagedLockImageCtx> MockManagedLock;
typedef managed_lock::AcquireRequest<MockManagedLockImageCtx> MockAcquireRequest;
typedef managed_lock::ReacquireRequest<MockManagedLockImageCtx> MockReacquireRequest;
typedef managed_lock::ReleaseRequest<MockManagedLockImageCtx> MockReleaseRequest;
void expect_get_watch_handle(MockImageWatcher &mock_watcher,
uint64_t watch_handle = 1234567890) {
EXPECT_CALL(mock_watcher, get_watch_handle())
.WillOnce(Return(watch_handle));
}
void expect_acquire_lock(MockImageWatcher &watcher,
asio::ContextWQ *work_queue,
MockAcquireRequest &acquire_request, int r) {
expect_get_watch_handle(watcher);
EXPECT_CALL(acquire_request, send())
.WillOnce(QueueRequest(&acquire_request, r, work_queue));
}
void expect_is_blocklisted(MockImageWatcher &watcher,
bool blocklisted) {
EXPECT_CALL(watcher, is_blocklisted()).WillOnce(Return(blocklisted));
}
void expect_release_lock(asio::ContextWQ *work_queue,
MockReleaseRequest &release_request, int r) {
EXPECT_CALL(release_request, send())
.WillOnce(QueueRequest(&release_request, r, work_queue));
}
void expect_reacquire_lock(MockImageWatcher& watcher,
asio::ContextWQ *work_queue,
MockReacquireRequest &mock_reacquire_request,
int r) {
EXPECT_CALL(mock_reacquire_request, send())
.WillOnce(QueueRequest(&mock_reacquire_request, r, work_queue));
}
void expect_flush_notifies(MockImageWatcher *mock_watcher) {
EXPECT_CALL(*mock_watcher, flush(_))
.WillOnce(CompleteContext(0, (asio::ContextWQ *)nullptr));
}
void expect_post_reacquired_lock_handler(MockImageWatcher& watcher,
MockMockManagedLock &managed_lock, uint64_t &client_id) {
expect_get_watch_handle(watcher);
EXPECT_CALL(managed_lock, post_reacquire_lock_handler(_, _))
.WillOnce(Invoke([&client_id](int r, Context *on_finish){
if (r >= 0) {
client_id = 98765;
}
on_finish->complete(r);}));
}
void expect_pre_release_lock_handler(MockMockManagedLock &managed_lock,
bool shutting_down, int r) {
EXPECT_CALL(managed_lock, pre_release_lock_handler(shutting_down, _))
.WillOnce(WithArg<1>(Invoke([r](Context *on_finish){
on_finish->complete(r);
})));
}
void expect_post_release_lock_handler(MockMockManagedLock &managed_lock,
bool shutting_down, int expect_r,
int r) {
EXPECT_CALL(managed_lock, post_release_lock_handler(shutting_down, expect_r, _))
.WillOnce(WithArg<2>(Invoke([r](Context *on_finish){
on_finish->complete(r);
})));
}
int when_acquire_lock(MockManagedLock &managed_lock) {
C_SaferCond ctx;
{
managed_lock.acquire_lock(&ctx);
}
return ctx.wait();
}
int when_release_lock(MockManagedLock &managed_lock) {
C_SaferCond ctx;
{
managed_lock.release_lock(&ctx);
}
return ctx.wait();
}
int when_shut_down(MockManagedLock &managed_lock) {
C_SaferCond ctx;
{
managed_lock.shut_down(&ctx);
}
return ctx.wait();
}
bool is_lock_owner(MockManagedLock &managed_lock) {
return managed_lock.is_lock_owner();
}
};
TEST_F(TestMockManagedLock, StateTransitions) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire1;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire1, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReleaseRequest request_release;
expect_release_lock(ictx->op_work_queue, request_release, 0);
ASSERT_EQ(0, when_release_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
MockAcquireRequest request_lock_acquire2;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire2, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockLockedState) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_EQ(0, when_acquire_lock(managed_lock));
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockAlreadyLocked) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, -EAGAIN);
ASSERT_EQ(-EAGAIN, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockBusy) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, -EBUSY);
ASSERT_EQ(-EBUSY, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, -EINVAL);
ASSERT_EQ(-EINVAL, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockBlocklist) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
// will abort after seeing blocklist error (avoid infinite request loop)
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire, -EBLOCKLISTED);
ASSERT_EQ(-EBLOCKLISTED, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, AcquireLockBlocklistedWatch) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
expect_is_blocklisted(*mock_image_ctx.image_watcher, true);
ASSERT_EQ(-EBLOCKLISTED, when_acquire_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ReleaseLockUnlockedState) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
ASSERT_EQ(0, when_release_lock(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ReleaseLockBlocklist) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockMockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
expect_pre_release_lock_handler(managed_lock, false, -EBLOCKLISTED);
expect_post_release_lock_handler(managed_lock, false, -EBLOCKLISTED, -EBLOCKLISTED);
ASSERT_EQ(-EBLOCKLISTED, when_release_lock(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ReleaseLockError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest try_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, try_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
MockReleaseRequest release;
expect_release_lock(ictx->op_work_queue, release, -EINVAL);
ASSERT_EQ(-EINVAL, when_release_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, ConcurrentRequests) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
expect_get_watch_handle(*mock_image_ctx.image_watcher);
C_SaferCond wait_for_send_ctx1;
MockAcquireRequest acquire_error;
EXPECT_CALL(acquire_error, send())
.WillOnce(Notify(&wait_for_send_ctx1));
MockAcquireRequest request_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_acquire, 0);
MockReleaseRequest release;
C_SaferCond wait_for_send_ctx2;
EXPECT_CALL(release, send())
.WillOnce(Notify(&wait_for_send_ctx2));
C_SaferCond acquire_request_ctx1;
managed_lock.acquire_lock(&acquire_request_ctx1);
C_SaferCond acquire_lock_ctx1;
C_SaferCond acquire_lock_ctx2;
managed_lock.acquire_lock(&acquire_lock_ctx1);
managed_lock.acquire_lock(&acquire_lock_ctx2);
// fail the try_lock
ASSERT_EQ(0, wait_for_send_ctx1.wait());
acquire_error.on_finish->complete(-EINVAL);
ASSERT_EQ(-EINVAL, acquire_request_ctx1.wait());
C_SaferCond acquire_lock_ctx3;
managed_lock.acquire_lock(&acquire_lock_ctx3);
C_SaferCond release_lock_ctx1;
managed_lock.release_lock(&release_lock_ctx1);
// all three pending request locks should complete
ASSERT_EQ(-EINVAL, acquire_lock_ctx1.wait());
ASSERT_EQ(-EINVAL, acquire_lock_ctx2.wait());
ASSERT_EQ(0, acquire_lock_ctx3.wait());
// proceed with the release
ASSERT_EQ(0, wait_for_send_ctx2.wait());
release.on_finish->complete(0);
ASSERT_EQ(0, release_lock_ctx1.wait());
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ReacquireLock) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReacquireRequest mock_reacquire_request;
C_SaferCond reacquire_ctx;
expect_get_watch_handle(*mock_image_ctx.image_watcher, 98765);
expect_reacquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, mock_reacquire_request, 0);
managed_lock.reacquire_lock(&reacquire_ctx);
ASSERT_EQ(0, reacquire_ctx.wait());
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, AttemptReacquireBlocklistedLock) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue,
request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
MockReleaseRequest request_release;
expect_release_lock(ictx->op_work_queue, request_release, 0);
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
expect_is_blocklisted(*mock_image_ctx.image_watcher, false);
managed_lock.reacquire_lock(nullptr);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, ReacquireBlocklistedLock) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue,
request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
MockReleaseRequest request_release;
expect_release_lock(ictx->op_work_queue, request_release, 0);
MockAcquireRequest request_lock_reacquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue,
request_lock_reacquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
C_SaferCond reacquire_ctx;
managed_lock.reacquire_lock(&reacquire_ctx);
ASSERT_EQ(0, reacquire_ctx.wait());
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, ReacquireLockError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReacquireRequest mock_reacquire_request;
C_SaferCond reacquire_ctx;
expect_get_watch_handle(*mock_image_ctx.image_watcher, 98765);
expect_reacquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, mock_reacquire_request, -EOPNOTSUPP);
MockReleaseRequest reacquire_lock_release;
expect_release_lock(ictx->op_work_queue, reacquire_lock_release, 0);
MockAcquireRequest reacquire_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, reacquire_lock_acquire, 0);
managed_lock.reacquire_lock(&reacquire_ctx);
ASSERT_EQ(0, reacquire_ctx.wait());
MockReleaseRequest shutdown_release;
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_FALSE(is_lock_owner(managed_lock));
}
TEST_F(TestMockManagedLock, ReacquireWithSameCookie) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockMockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
MockAcquireRequest request_lock_acquire;
expect_acquire_lock(*mock_image_ctx.image_watcher, ictx->op_work_queue, request_lock_acquire, 0);
ASSERT_EQ(0, when_acquire_lock(managed_lock));
ASSERT_TRUE(is_lock_owner(managed_lock));
// watcher with same cookie after rewatch
uint64_t client_id = 0;
C_SaferCond reacquire_ctx;
expect_post_reacquired_lock_handler(*mock_image_ctx.image_watcher, managed_lock, client_id);
managed_lock.reacquire_lock(&reacquire_ctx);
ASSERT_LT(0U, client_id);
ASSERT_TRUE(is_lock_owner(managed_lock));
MockReleaseRequest shutdown_release;
expect_pre_release_lock_handler(managed_lock, true, 0);
expect_release_lock(ictx->op_work_queue, shutdown_release, 0);
expect_post_release_lock_handler(managed_lock, true, 0, 0);
ASSERT_EQ(0, when_shut_down(managed_lock));
}
TEST_F(TestMockManagedLock, ShutDownWhileWaiting) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockManagedLockImageCtx mock_image_ctx(*ictx);
MockMockManagedLock managed_lock(ictx->md_ctx, *ictx->asio_engine,
ictx->header_oid, mock_image_ctx.image_watcher,
librbd::managed_lock::EXCLUSIVE, true, 0);
InSequence seq;
expect_get_watch_handle(*mock_image_ctx.image_watcher, 0);
expect_is_blocklisted(*mock_image_ctx.image_watcher, false);
C_SaferCond acquire_ctx;
managed_lock.acquire_lock(&acquire_ctx);
ASSERT_EQ(0, when_shut_down(managed_lock));
ASSERT_EQ(-ERESTART, acquire_ctx.wait());
ASSERT_FALSE(is_lock_owner(managed_lock));
}
} // namespace librbd
| 26,414 | 35.484807 | 115 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_ObjectMap.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "librbd/ObjectMap.h"
#include "librbd/object_map/RefreshRequest.h"
#include "librbd/object_map/UnlockRequest.h"
#include "librbd/object_map/UpdateRequest.h"
#include <boost/scope_exit.hpp>
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace object_map {
template <>
struct RefreshRequest<MockTestImageCtx> {
Context *on_finish = nullptr;
ceph::BitVector<2u> *object_map = nullptr;
static RefreshRequest *s_instance;
static RefreshRequest *create(MockTestImageCtx &image_ctx, ceph::shared_mutex*,
ceph::BitVector<2u> *object_map,
uint64_t snap_id, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
s_instance->object_map = object_map;
return s_instance;
}
MOCK_METHOD0(send, void());
RefreshRequest() {
s_instance = this;
}
};
template <>
struct UnlockRequest<MockTestImageCtx> {
Context *on_finish = nullptr;
static UnlockRequest *s_instance;
static UnlockRequest *create(MockTestImageCtx &image_ctx,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
UnlockRequest() {
s_instance = this;
}
};
template <>
struct UpdateRequest<MockTestImageCtx> {
Context *on_finish = nullptr;
static UpdateRequest *s_instance;
static UpdateRequest *create(MockTestImageCtx &image_ctx, ceph::shared_mutex*,
ceph::BitVector<2u> *object_map,
uint64_t snap_id,
uint64_t start_object_no, uint64_t end_object_no,
uint8_t new_state,
const boost::optional<uint8_t> ¤t_state,
const ZTracer::Trace &parent_trace,
bool ignore_enoent, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
s_instance->construct(snap_id, start_object_no, end_object_no, new_state,
current_state, ignore_enoent);
return s_instance;
}
MOCK_METHOD6(construct, void(uint64_t snap_id, uint64_t start_object_no,
uint64_t end_object_no, uint8_t new_state,
const boost::optional<uint8_t> ¤t_state,
bool ignore_enoent));
MOCK_METHOD0(send, void());
UpdateRequest() {
s_instance = this;
}
};
RefreshRequest<MockTestImageCtx> *RefreshRequest<MockTestImageCtx>::s_instance = nullptr;
UnlockRequest<MockTestImageCtx> *UnlockRequest<MockTestImageCtx>::s_instance = nullptr;
UpdateRequest<MockTestImageCtx> *UpdateRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace object_map
} // namespace librbd
#include "librbd/ObjectMap.cc"
namespace librbd {
using testing::_;
using testing::InSequence;
using testing::Invoke;
class TestMockObjectMap : public TestMockFixture {
public:
typedef ObjectMap<MockTestImageCtx> MockObjectMap;
typedef object_map::RefreshRequest<MockTestImageCtx> MockRefreshRequest;
typedef object_map::UnlockRequest<MockTestImageCtx> MockUnlockRequest;
typedef object_map::UpdateRequest<MockTestImageCtx> MockUpdateRequest;
void expect_refresh(MockTestImageCtx &mock_image_ctx,
MockRefreshRequest &mock_refresh_request,
const ceph::BitVector<2u> &object_map, int r) {
EXPECT_CALL(mock_refresh_request, send())
.WillOnce(Invoke([&mock_image_ctx, &mock_refresh_request, &object_map, r]() {
*mock_refresh_request.object_map = object_map;
mock_image_ctx.image_ctx->op_work_queue->queue(mock_refresh_request.on_finish, r);
}));
}
void expect_unlock(MockTestImageCtx &mock_image_ctx,
MockUnlockRequest &mock_unlock_request, int r) {
EXPECT_CALL(mock_unlock_request, send())
.WillOnce(Invoke([&mock_image_ctx, &mock_unlock_request, r]() {
mock_image_ctx.image_ctx->op_work_queue->queue(mock_unlock_request.on_finish, r);
}));
}
void expect_update(MockTestImageCtx &mock_image_ctx,
MockUpdateRequest &mock_update_request,
uint64_t snap_id, uint64_t start_object_no,
uint64_t end_object_no, uint8_t new_state,
const boost::optional<uint8_t> ¤t_state,
bool ignore_enoent, Context **on_finish) {
EXPECT_CALL(mock_update_request, construct(snap_id, start_object_no,
end_object_no, new_state,
current_state, ignore_enoent))
.Times(1);
EXPECT_CALL(mock_update_request, send())
.WillOnce(Invoke([&mock_update_request, on_finish]() {
*on_finish = mock_update_request.on_finish;
}));
}
};
TEST_F(TestMockObjectMap, NonDetainedUpdate) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
InSequence seq;
ceph::BitVector<2u> object_map;
object_map.resize(4);
MockRefreshRequest mock_refresh_request;
expect_refresh(mock_image_ctx, mock_refresh_request, object_map, 0);
MockUpdateRequest mock_update_request;
Context *finish_update_1;
expect_update(mock_image_ctx, mock_update_request, CEPH_NOSNAP,
0, 1, 1, {}, false, &finish_update_1);
Context *finish_update_2;
expect_update(mock_image_ctx, mock_update_request, CEPH_NOSNAP,
1, 2, 1, {}, false, &finish_update_2);
MockUnlockRequest mock_unlock_request;
expect_unlock(mock_image_ctx, mock_unlock_request, 0);
MockObjectMap *mock_object_map = new MockObjectMap(mock_image_ctx, CEPH_NOSNAP);
BOOST_SCOPE_EXIT(&mock_object_map) {
mock_object_map->put();
} BOOST_SCOPE_EXIT_END
C_SaferCond open_ctx;
mock_object_map->open(&open_ctx);
ASSERT_EQ(0, open_ctx.wait());
C_SaferCond update_ctx1;
C_SaferCond update_ctx2;
{
std::shared_lock image_locker{mock_image_ctx.image_lock};
mock_object_map->aio_update(CEPH_NOSNAP, 0, 1, {}, {}, false, &update_ctx1);
mock_object_map->aio_update(CEPH_NOSNAP, 1, 1, {}, {}, false, &update_ctx2);
}
finish_update_2->complete(0);
ASSERT_EQ(0, update_ctx2.wait());
finish_update_1->complete(0);
ASSERT_EQ(0, update_ctx1.wait());
C_SaferCond close_ctx;
mock_object_map->close(&close_ctx);
ASSERT_EQ(0, close_ctx.wait());
}
TEST_F(TestMockObjectMap, DetainedUpdate) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
InSequence seq;
ceph::BitVector<2u> object_map;
object_map.resize(4);
MockRefreshRequest mock_refresh_request;
expect_refresh(mock_image_ctx, mock_refresh_request, object_map, 0);
MockUpdateRequest mock_update_request;
Context *finish_update_1;
expect_update(mock_image_ctx, mock_update_request, CEPH_NOSNAP,
1, 4, 1, {}, false, &finish_update_1);
Context *finish_update_2 = nullptr;
expect_update(mock_image_ctx, mock_update_request, CEPH_NOSNAP,
1, 3, 1, {}, false, &finish_update_2);
Context *finish_update_3 = nullptr;
expect_update(mock_image_ctx, mock_update_request, CEPH_NOSNAP,
2, 3, 1, {}, false, &finish_update_3);
Context *finish_update_4 = nullptr;
expect_update(mock_image_ctx, mock_update_request, CEPH_NOSNAP,
0, 2, 1, {}, false, &finish_update_4);
MockUnlockRequest mock_unlock_request;
expect_unlock(mock_image_ctx, mock_unlock_request, 0);
MockObjectMap *mock_object_map = new MockObjectMap(mock_image_ctx, CEPH_NOSNAP);
BOOST_SCOPE_EXIT(&mock_object_map) {
mock_object_map->put();
} BOOST_SCOPE_EXIT_END
C_SaferCond open_ctx;
mock_object_map->open(&open_ctx);
ASSERT_EQ(0, open_ctx.wait());
C_SaferCond update_ctx1;
C_SaferCond update_ctx2;
C_SaferCond update_ctx3;
C_SaferCond update_ctx4;
{
std::shared_lock image_locker{mock_image_ctx.image_lock};
mock_object_map->aio_update(CEPH_NOSNAP, 1, 4, 1, {}, {}, false,
&update_ctx1);
mock_object_map->aio_update(CEPH_NOSNAP, 1, 3, 1, {}, {}, false,
&update_ctx2);
mock_object_map->aio_update(CEPH_NOSNAP, 2, 3, 1, {}, {}, false,
&update_ctx3);
mock_object_map->aio_update(CEPH_NOSNAP, 0, 2, 1, {}, {}, false,
&update_ctx4);
}
// updates 2, 3, and 4 are blocked on update 1
ASSERT_EQ(nullptr, finish_update_2);
finish_update_1->complete(0);
ASSERT_EQ(0, update_ctx1.wait());
// updates 3 and 4 are blocked on update 2
ASSERT_NE(nullptr, finish_update_2);
ASSERT_EQ(nullptr, finish_update_3);
ASSERT_EQ(nullptr, finish_update_4);
finish_update_2->complete(0);
ASSERT_EQ(0, update_ctx2.wait());
ASSERT_NE(nullptr, finish_update_3);
ASSERT_NE(nullptr, finish_update_4);
finish_update_3->complete(0);
finish_update_4->complete(0);
ASSERT_EQ(0, update_ctx3.wait());
ASSERT_EQ(0, update_ctx4.wait());
C_SaferCond close_ctx;
mock_object_map->close(&close_ctx);
ASSERT_EQ(0, close_ctx.wait());
}
} // namespace librbd
| 9,780 | 33.199301 | 92 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_TrashWatcher.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "include/rbd_types.h"
#include "librbd/TrashWatcher.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <list>
namespace librbd {
namespace {
struct MockTrashWatcher : public TrashWatcher<> {
MockTrashWatcher(ImageCtx &image_ctx)
: TrashWatcher<>(image_ctx.md_ctx, image_ctx.op_work_queue) {
}
MOCK_METHOD2(handle_image_added, void(const std::string&,
const cls::rbd::TrashImageSpec&));
MOCK_METHOD1(handle_image_removed, void(const std::string&));
};
} // anonymous namespace
using ::testing::_;
using ::testing::AtLeast;
using ::testing::StrEq;
class TestTrashWatcher : public TestMockFixture {
public:
void SetUp() override {
TestFixture::SetUp();
bufferlist bl;
ASSERT_EQ(0, m_ioctx.write_full(RBD_TRASH, bl));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
m_trash_watcher = new MockTrashWatcher(*ictx);
C_SaferCond ctx;
m_trash_watcher->register_watch(&ctx);
if (ctx.wait() != 0) {
delete m_trash_watcher;
m_trash_watcher = nullptr;
FAIL();
}
}
void TearDown() override {
if (m_trash_watcher != nullptr) {
C_SaferCond ctx;
m_trash_watcher->unregister_watch(&ctx);
ASSERT_EQ(0, ctx.wait());
delete m_trash_watcher;
}
TestFixture::TearDown();
}
MockTrashWatcher *m_trash_watcher = nullptr;
};
TEST_F(TestTrashWatcher, ImageAdded) {
REQUIRE_FORMAT_V2();
cls::rbd::TrashImageSpec trash_image_spec{
cls::rbd::TRASH_IMAGE_SOURCE_USER, "image name",
ceph_clock_now(), ceph_clock_now()};
EXPECT_CALL(*m_trash_watcher, handle_image_added(StrEq("image id"),
trash_image_spec))
.Times(AtLeast(1));
C_SaferCond ctx;
MockTrashWatcher::notify_image_added(m_ioctx, "image id", trash_image_spec,
&ctx);
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestTrashWatcher, ImageRemoved) {
REQUIRE_FORMAT_V2();
EXPECT_CALL(*m_trash_watcher, handle_image_removed(StrEq("image id")))
.Times(AtLeast(1));
C_SaferCond ctx;
MockTrashWatcher::notify_image_removed(m_ioctx, "image id", &ctx);
ASSERT_EQ(0, ctx.wait());
}
} // namespace librbd
| 2,436 | 24.385417 | 77 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_Watcher.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "common/Cond.h"
#include "common/ceph_mutex.h"
#include "librados/AioCompletionImpl.h"
#include "librbd/Watcher.h"
#include "librbd/watcher/RewatchRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <list>
namespace librbd {
namespace {
struct MockWatcher : public Watcher {
std::string oid;
MockWatcher(librados::IoCtx& ioctx, asio::ContextWQ *work_queue,
const std::string& oid)
: Watcher(ioctx, work_queue, oid) {
}
virtual void handle_notify(uint64_t notify_id, uint64_t handle,
uint64_t notifier_id, bufferlist &bl) {
}
};
} // anonymous namespace
} // namespace librbd
namespace librbd {
using ::testing::_;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::WithArg;
using ::testing::WithArgs;
class TestMockWatcher : public TestMockFixture {
public:
TestMockWatcher() = default;
virtual void SetUp() {
TestMockFixture::SetUp();
m_oid = get_temp_image_name();
bufferlist bl;
ASSERT_EQ(0, m_ioctx.write_full(m_oid, bl));
}
void expect_aio_watch(MockImageCtx &mock_image_ctx, int r,
const std::function<void()> &action = std::function<void()>()) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
librados::MockTestMemRadosClient *mock_rados_client(
mock_io_ctx.get_mock_rados_client());
EXPECT_CALL(mock_io_ctx, aio_watch(m_oid, _, _, _))
.WillOnce(DoAll(WithArgs<1, 2, 3>(Invoke([this, &mock_image_ctx, mock_rados_client, r, action](
librados::AioCompletionImpl *c, uint64_t *cookie,
librados::WatchCtx2 *watch_ctx) {
if (r == 0) {
*cookie = 234U;
m_watch_ctx = watch_ctx;
}
c->get();
mock_image_ctx.image_ctx->op_work_queue->queue(new LambdaContext([mock_rados_client, action, c](int r) {
if (action) {
action();
}
mock_rados_client->finish_aio_completion(c, r);
}), r);
notify_watch();
})), Return(0)));
}
void expect_aio_unwatch(MockImageCtx &mock_image_ctx, int r,
const std::function<void()> &action = std::function<void()>()) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
librados::MockTestMemRadosClient *mock_rados_client(
mock_io_ctx.get_mock_rados_client());
EXPECT_CALL(mock_io_ctx, aio_unwatch(_, _))
.WillOnce(DoAll(Invoke([this, &mock_image_ctx, mock_rados_client, r, action](
uint64_t handle, librados::AioCompletionImpl *c) {
c->get();
mock_image_ctx.image_ctx->op_work_queue->queue(new LambdaContext([mock_rados_client, action, c](int r) {
if (action) {
action();
}
mock_rados_client->finish_aio_completion(c, r);
}), r);
notify_watch();
}), Return(0)));
}
std::string m_oid;
librados::WatchCtx2 *m_watch_ctx = nullptr;
void notify_watch() {
std::lock_guard locker{m_lock};
++m_watch_count;
m_cond.notify_all();
}
bool wait_for_watch(MockImageCtx &mock_image_ctx, size_t count) {
using namespace std::chrono_literals;
std::unique_lock locker{m_lock};
while (m_watch_count < count) {
if (m_cond.wait_for(locker, 10s) == std::cv_status::timeout) {
return false;
}
}
m_watch_count -= count;
return true;
}
ceph::mutex m_lock = ceph::make_mutex("TestMockWatcher::m_lock");
ceph::condition_variable m_cond;
size_t m_watch_count = 0;
};
TEST_F(TestMockWatcher, Success) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(0, register_ctx.wait());
C_SaferCond unregister_ctx;
mock_image_watcher.unregister_watch(&unregister_ctx);
ASSERT_EQ(0, unregister_ctx.wait());
}
TEST_F(TestMockWatcher, RegisterError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
InSequence seq;
expect_aio_watch(mock_image_ctx, -EINVAL);
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(-EINVAL, register_ctx.wait());
}
TEST_F(TestMockWatcher, UnregisterError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, -EINVAL);
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(0, register_ctx.wait());
C_SaferCond unregister_ctx;
mock_image_watcher.unregister_watch(&unregister_ctx);
ASSERT_EQ(-EINVAL, unregister_ctx.wait());
}
TEST_F(TestMockWatcher, Reregister) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(0, register_ctx.wait());
ceph_assert(m_watch_ctx != nullptr);
m_watch_ctx->handle_error(0, -ESHUTDOWN);
// wait for recovery unwatch/watch
ASSERT_TRUE(wait_for_watch(mock_image_ctx, 3));
C_SaferCond unregister_ctx;
mock_image_watcher.unregister_watch(&unregister_ctx);
ASSERT_EQ(0, unregister_ctx.wait());
}
TEST_F(TestMockWatcher, ReregisterUnwatchError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, -EINVAL);
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(0, register_ctx.wait());
ceph_assert(m_watch_ctx != nullptr);
m_watch_ctx->handle_error(0, -ESHUTDOWN);
// wait for recovery unwatch/watch
ASSERT_TRUE(wait_for_watch(mock_image_ctx, 3));
C_SaferCond unregister_ctx;
mock_image_watcher.unregister_watch(&unregister_ctx);
ASSERT_EQ(0, unregister_ctx.wait());
}
TEST_F(TestMockWatcher, ReregisterWatchError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
expect_aio_watch(mock_image_ctx, -EPERM);
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(0, register_ctx.wait());
ceph_assert(m_watch_ctx != nullptr);
m_watch_ctx->handle_error(0, -ESHUTDOWN);
// wait for recovery unwatch/watch
ASSERT_TRUE(wait_for_watch(mock_image_ctx, 4));
C_SaferCond unregister_ctx;
mock_image_watcher.unregister_watch(&unregister_ctx);
ASSERT_EQ(0, unregister_ctx.wait());
}
TEST_F(TestMockWatcher, ReregisterWatchBlocklist) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
expect_aio_watch(mock_image_ctx, -EBLOCKLISTED);
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_TRUE(wait_for_watch(mock_image_ctx, 1));
ASSERT_EQ(0, register_ctx.wait());
ceph_assert(m_watch_ctx != nullptr);
m_watch_ctx->handle_error(0, -EBLOCKLISTED);
// wait for recovery unwatch/watch
ASSERT_TRUE(wait_for_watch(mock_image_ctx, 2));
ASSERT_TRUE(mock_image_watcher.is_blocklisted());
C_SaferCond unregister_ctx;
mock_image_watcher.unregister_watch(&unregister_ctx);
ASSERT_EQ(0, unregister_ctx.wait());
}
TEST_F(TestMockWatcher, ReregisterUnwatchPendingUnregister) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
// inject an unregister
C_SaferCond unregister_ctx;
expect_aio_unwatch(mock_image_ctx, -EBLOCKLISTED,
[&mock_image_watcher, &unregister_ctx]() {
mock_image_watcher.unregister_watch(&unregister_ctx);
});
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(0, register_ctx.wait());
ceph_assert(m_watch_ctx != nullptr);
m_watch_ctx->handle_error(0, -EBLOCKLISTED);
ASSERT_EQ(0, unregister_ctx.wait());
}
TEST_F(TestMockWatcher, ReregisterWatchPendingUnregister) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
// inject an unregister
C_SaferCond unregister_ctx;
expect_aio_watch(mock_image_ctx, -ESHUTDOWN,
[&mock_image_watcher, &unregister_ctx]() {
mock_image_watcher.unregister_watch(&unregister_ctx);
});
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(0, register_ctx.wait());
ceph_assert(m_watch_ctx != nullptr);
m_watch_ctx->handle_error(0, -ESHUTDOWN);
ASSERT_EQ(0, unregister_ctx.wait());
}
TEST_F(TestMockWatcher, ReregisterPendingUnregister) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockWatcher mock_image_watcher(m_ioctx, ictx->op_work_queue, m_oid);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_aio_watch(mock_image_ctx, 0);
expect_aio_unwatch(mock_image_ctx, 0);
// inject an unregister
C_SaferCond unregister_ctx;
expect_aio_watch(mock_image_ctx, 0,
[&mock_image_watcher, &unregister_ctx]() {
mock_image_watcher.unregister_watch(&unregister_ctx);
});
expect_aio_unwatch(mock_image_ctx, 0);
C_SaferCond register_ctx;
mock_image_watcher.register_watch(®ister_ctx);
ASSERT_EQ(0, register_ctx.wait());
ceph_assert(m_watch_ctx != nullptr);
m_watch_ctx->handle_error(0, -ESHUTDOWN);
ASSERT_EQ(0, unregister_ctx.wait());
}
} // namespace librbd
| 11,873 | 28.246305 | 114 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_fixture.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librados_test_stub/LibradosTestStub.h"
#include "test/librados_test_stub/MockTestMemCluster.h"
// template definitions
#include "librbd/AsyncRequest.cc"
#include "librbd/AsyncObjectThrottle.cc"
#include "librbd/operation/Request.cc"
template class librbd::AsyncRequest<librbd::MockImageCtx>;
template class librbd::AsyncObjectThrottle<librbd::MockImageCtx>;
template class librbd::operation::Request<librbd::MockImageCtx>;
using ::testing::_;
using ::testing::DoDefault;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;
TestMockFixture::TestClusterRef TestMockFixture::s_test_cluster;
void TestMockFixture::SetUpTestCase() {
s_test_cluster = librados_test_stub::get_cluster();
// use a mock version of the in-memory cluster
librados_test_stub::set_cluster(boost::shared_ptr<librados::TestCluster>(
new ::testing::NiceMock<librados::MockTestMemCluster>()));
TestFixture::SetUpTestCase();
}
void TestMockFixture::TearDownTestCase() {
TestFixture::TearDownTestCase();
librados_test_stub::set_cluster(s_test_cluster);
}
void TestMockFixture::TearDown() {
// Mock rados client lives across tests -- reset it to initial state
librados::MockTestMemRadosClient *mock_rados_client =
get_mock_io_ctx(m_ioctx).get_mock_rados_client();
ASSERT_TRUE(mock_rados_client != nullptr);
::testing::Mock::VerifyAndClear(mock_rados_client);
mock_rados_client->default_to_dispatch();
dynamic_cast<librados::MockTestMemCluster*>(
librados_test_stub::get_cluster().get())->default_to_dispatch();
TestFixture::TearDown();
}
void TestMockFixture::expect_unlock_exclusive_lock(librbd::ImageCtx &ictx) {
EXPECT_CALL(get_mock_io_ctx(ictx.md_ctx),
exec(_, _, StrEq("lock"), StrEq("unlock"), _, _, _, _))
.WillRepeatedly(DoDefault());
if (ictx.test_features(RBD_FEATURE_DIRTY_CACHE)) {
EXPECT_CALL(get_mock_io_ctx(ictx.md_ctx),
exec(ictx.header_oid, _, StrEq("rbd"), StrEq("set_features"), _, _, _, _))
.WillOnce(DoDefault());
EXPECT_CALL(get_mock_io_ctx(ictx.md_ctx),
exec(ictx.header_oid, _, StrEq("rbd"), StrEq("metadata_set"), _, _, _, _))
.WillOnce(DoDefault());
EXPECT_CALL(get_mock_io_ctx(ictx.md_ctx),
exec(ictx.header_oid, _, StrEq("rbd"), StrEq("metadata_remove"), _, _, _, _))
.WillOnce(DoDefault());
}
}
void TestMockFixture::expect_op_work_queue(librbd::MockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.op_work_queue, queue(_, _))
.WillRepeatedly(DispatchContext(
mock_image_ctx.image_ctx->op_work_queue));
}
void TestMockFixture::initialize_features(librbd::ImageCtx *ictx,
librbd::MockImageCtx &mock_image_ctx,
librbd::MockExclusiveLock &mock_exclusive_lock,
librbd::MockJournal &mock_journal,
librbd::MockObjectMap &mock_object_map) {
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
}
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
mock_image_ctx.journal = &mock_journal;
}
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
}
void TestMockFixture::expect_is_journal_appending(librbd::MockJournal &mock_journal,
bool appending) {
EXPECT_CALL(mock_journal, is_journal_appending()).WillOnce(Return(appending));
}
void TestMockFixture::expect_is_journal_replaying(librbd::MockJournal &mock_journal) {
EXPECT_CALL(mock_journal, is_journal_replaying()).WillOnce(Return(false));
}
void TestMockFixture::expect_is_journal_ready(librbd::MockJournal &mock_journal) {
EXPECT_CALL(mock_journal, is_journal_ready()).WillOnce(Return(true));
}
void TestMockFixture::expect_allocate_op_tid(librbd::MockImageCtx &mock_image_ctx) {
if (mock_image_ctx.journal != nullptr) {
EXPECT_CALL(*mock_image_ctx.journal, allocate_op_tid())
.WillOnce(Return(1U));
}
}
void TestMockFixture::expect_append_op_event(librbd::MockImageCtx &mock_image_ctx,
bool can_affect_io, int r) {
if (mock_image_ctx.journal != nullptr) {
if (can_affect_io) {
expect_is_journal_replaying(*mock_image_ctx.journal);
}
expect_is_journal_appending(*mock_image_ctx.journal, true);
expect_allocate_op_tid(mock_image_ctx);
EXPECT_CALL(*mock_image_ctx.journal, append_op_event_mock(_, _, _))
.WillOnce(WithArg<2>(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue)));
}
}
void TestMockFixture::expect_commit_op_event(librbd::MockImageCtx &mock_image_ctx, int r) {
if (mock_image_ctx.journal != nullptr) {
expect_is_journal_appending(*mock_image_ctx.journal, true);
expect_is_journal_ready(*mock_image_ctx.journal);
EXPECT_CALL(*mock_image_ctx.journal, commit_op_event(1U, r, _))
.WillOnce(WithArg<2>(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue)));
}
}
| 5,399 | 39 | 101 |
cc
|
null |
ceph-main/src/test/librbd/test_mock_fixture.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#ifndef CEPH_TEST_LIBRBD_TEST_MOCK_FIXTURE_H
#define CEPH_TEST_LIBRBD_TEST_MOCK_FIXTURE_H
#include "test/librbd/test_fixture.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librados_test_stub/LibradosTestStub.h"
#include "librbd/asio/ContextWQ.h"
#include <boost/shared_ptr.hpp>
#include <gmock/gmock.h>
namespace librados {
class TestCluster;
class MockTestMemCluster;
class MockTestMemIoCtxImpl;
class MockTestMemRadosClient;
}
namespace librbd {
class MockImageCtx;
}
ACTION_P(CopyInBufferlist, str) {
arg0->append(str);
}
ACTION_P2(CompleteContext, r, wq) {
librbd::asio::ContextWQ *context_wq = reinterpret_cast<
librbd::asio::ContextWQ *>(wq);
if (context_wq != NULL) {
context_wq->queue(arg0, r);
} else {
arg0->complete(r);
}
}
ACTION_P(DispatchContext, wq) {
wq->queue(arg0, arg1);
}
ACTION_P3(FinishRequest, request, r, mock) {
librbd::MockImageCtx *mock_image_ctx =
reinterpret_cast<librbd::MockImageCtx *>(mock);
mock_image_ctx->image_ctx->op_work_queue->queue(request->on_finish, r);
}
ACTION_P(GetReference, ref_object) {
ref_object->get();
}
MATCHER_P(ContentsEqual, bl, "") {
// TODO fix const-correctness of bufferlist
return const_cast<bufferlist &>(arg).contents_equal(
const_cast<bufferlist &>(bl));
}
class TestMockFixture : public TestFixture {
public:
typedef boost::shared_ptr<librados::TestCluster> TestClusterRef;
static void SetUpTestCase();
static void TearDownTestCase();
void TearDown() override;
void expect_op_work_queue(librbd::MockImageCtx &mock_image_ctx);
void expect_unlock_exclusive_lock(librbd::ImageCtx &ictx);
void initialize_features(librbd::ImageCtx *ictx,
librbd::MockImageCtx &mock_image_ctx,
librbd::MockExclusiveLock &mock_exclusive_lock,
librbd::MockJournal &mock_journal,
librbd::MockObjectMap &mock_object_map);
void expect_is_journal_appending(librbd::MockJournal &mock_journal,
bool appending);
void expect_is_journal_replaying(librbd::MockJournal &mock_journal);
void expect_is_journal_ready(librbd::MockJournal &mock_journal);
void expect_allocate_op_tid(librbd::MockImageCtx &mock_image_ctx);
void expect_append_op_event(librbd::MockImageCtx &mock_image_ctx,
bool can_affect_io, int r);
void expect_commit_op_event(librbd::MockImageCtx &mock_image_ctx, int r);
private:
static TestClusterRef s_test_cluster;
};
#endif // CEPH_TEST_LIBRBD_TEST_MOCK_FIXTURE_H
| 2,684 | 28.833333 | 75 |
h
|
null |
ceph-main/src/test/librbd/test_support.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_support.h"
#include "include/rbd_types.h"
#include "gtest/gtest.h"
#include "common/ceph_context.h"
#include <sstream>
bool get_features(uint64_t *features) {
const char *c = getenv("RBD_FEATURES");
if (c == NULL) {
return false;
}
std::stringstream ss(c);
if (!(ss >> *features)) {
return false;
}
return true;
}
bool is_feature_enabled(uint64_t feature) {
uint64_t features;
return (get_features(&features) && (features & feature) == feature);
}
int create_image_full_pp(librbd::RBD &rbd, librados::IoCtx &ioctx,
const std::string &name, uint64_t size,
uint64_t features, bool old_format, int *order)
{
if (old_format) {
librados::Rados rados(ioctx);
int r = rados.conf_set("rbd_default_format", "1");
if (r < 0) {
return r;
}
return rbd.create(ioctx, name.c_str(), size, order);
} else if ((features & RBD_FEATURE_STRIPINGV2) != 0) {
uint64_t stripe_unit = IMAGE_STRIPE_UNIT;
if (*order) {
// use a conservative stripe_unit for non default order
stripe_unit = (1ull << (*order-1));
}
printf("creating image with stripe unit: %" PRIu64 ", stripe count: %" PRIu64 "\n",
stripe_unit, IMAGE_STRIPE_COUNT);
return rbd.create3(ioctx, name.c_str(), size, features, order, stripe_unit,
IMAGE_STRIPE_COUNT);
} else {
return rbd.create2(ioctx, name.c_str(), size, features, order);
}
}
int create_image_pp(librbd::RBD &rbd, librados::IoCtx &ioctx,
const std::string &name, uint64_t size) {
int order = 0;
uint64_t features = 0;
if (!get_features(&features)) {
// ensure old-format tests actually use the old format
librados::Rados rados(ioctx);
int r = rados.conf_set("rbd_default_format", "1");
if (r < 0) {
return r;
}
return rbd.create(ioctx, name.c_str(), size, &order);
} else {
return rbd.create2(ioctx, name.c_str(), size, features, &order);
}
}
int clone_image_pp(librbd::RBD &rbd, librbd::Image &p_image, librados::IoCtx &p_ioctx,
const char *p_name, const char *p_snap_name, librados::IoCtx &c_ioctx,
const char *c_name, uint64_t features)
{
uint64_t stripe_unit = p_image.get_stripe_unit();
uint64_t stripe_count = p_image.get_stripe_count();
librbd::image_info_t p_info;
int r = p_image.stat(p_info, sizeof(p_info));
if (r < 0) {
return r;
}
int c_order = p_info.order;
return rbd.clone2(p_ioctx, p_name, p_snap_name, c_ioctx, c_name,
features, &c_order, stripe_unit, stripe_count);
}
int get_image_id(librbd::Image &image, std::string *image_id)
{
int r = image.get_id(image_id);
if (r < 0) {
return r;
}
return 0;
}
int create_image_data_pool(librados::Rados &rados, std::string &data_pool, bool *created) {
std::string pool;
int r = rados.conf_get("rbd_default_data_pool", pool);
if (r != 0) {
return r;
} else if (pool.empty()) {
return 0;
}
r = rados.pool_create(pool.c_str());
if ((r == 0) || (r == -EEXIST)) {
data_pool = pool;
*created = (r == 0);
return 0;
}
librados::IoCtx ioctx;
r = rados.ioctx_create(pool.c_str(), ioctx);
if (r < 0) {
return r;
}
librbd::RBD rbd;
return rbd.pool_init(ioctx, true);
}
bool is_librados_test_stub(librados::Rados &rados) {
std::string fsid;
EXPECT_EQ(0, rados.cluster_fsid(&fsid));
return fsid == "00000000-1111-2222-3333-444444444444";
}
bool is_rbd_pwl_enabled(ceph::common::CephContext *cct) {
#if defined(WITH_RBD_RWL) || defined(WITH_RBD_SSD_CACHE)
auto value = cct->_conf.get_val<std::string>("rbd_persistent_cache_mode");
return value == "disabled" ? false : true;
#else
return false;
#endif
}
| 3,890 | 27.195652 | 91 |
cc
|
null |
ceph-main/src/test/librbd/test_support.h
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "include/int_types.h"
#include "include/rados/librados.h"
#include "include/rbd/librbd.hpp"
#include <string>
static const uint64_t IMAGE_STRIPE_UNIT = 65536;
static const uint64_t IMAGE_STRIPE_COUNT = 16;
#define TEST_IO_SIZE 512
#define TEST_IO_TO_SNAP_SIZE 80
bool get_features(uint64_t *features);
bool is_feature_enabled(uint64_t feature);
int create_image_pp(librbd::RBD &rbd, librados::IoCtx &ioctx,
const std::string &name, uint64_t size);
int create_image_full_pp(librbd::RBD &rbd, librados::IoCtx &ioctx,
const std::string &name, uint64_t size,
uint64_t features, bool old_format, int *order);
int clone_image_pp(librbd::RBD &rbd, librbd::Image &p_image, librados::IoCtx &p_ioctx,
const char *p_name, const char *p_snap_name, librados::IoCtx &c_ioctx,
const char *c_name, uint64_t features);
int get_image_id(librbd::Image &image, std::string *image_id);
int create_image_data_pool(librados::Rados &rados, std::string &data_pool, bool *created);
bool is_librados_test_stub(librados::Rados &rados);
bool is_rbd_pwl_enabled(ceph::common::CephContext *ctx);
#define REQUIRE(x) { \
if (!(x)) { \
GTEST_SKIP() << "Skipping due to unmet REQUIRE"; \
} \
}
#define REQUIRE_FEATURE(feature) REQUIRE(is_feature_enabled(feature))
#define REQUIRE_FORMAT_V1() REQUIRE(!is_feature_enabled(0))
#define REQUIRE_FORMAT_V2() REQUIRE_FEATURE(0)
| 1,582 | 38.575 | 90 |
h
|
null |
ceph-main/src/test/librbd/cache/test_mock_ParentCacheObjectDispatch.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "include/Context.h"
#include "tools/immutable_object_cache/CacheClient.h"
#include "test/immutable_object_cache/MockCacheDaemon.h"
#include "librbd/cache/ParentCacheObjectDispatch.h"
#include "librbd/plugin/Api.h"
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/mock/MockImageCtx.h"
using namespace ceph::immutable_obj_cache;
namespace librbd {
namespace {
struct MockParentImageCacheImageCtx : public MockImageCtx {
MockParentImageCacheImageCtx(ImageCtx& image_ctx)
: MockImageCtx(image_ctx) {
}
~MockParentImageCacheImageCtx() {}
};
} // anonymous namespace
namespace cache {
template<>
struct TypeTraits<MockParentImageCacheImageCtx> {
typedef ceph::immutable_obj_cache::MockCacheClient CacheClient;
};
} // namespace cache
namespace plugin {
template <>
struct Api<MockParentImageCacheImageCtx> {
MOCK_METHOD6(read_parent, void(MockParentImageCacheImageCtx*, uint64_t,
librbd::io::ReadExtents*, librados::snap_t,
const ZTracer::Trace &, Context*));
};
} // namespace plugin
} // namespace librbd
#include "librbd/cache/ParentCacheObjectDispatch.cc"
template class librbd::cache::ParentCacheObjectDispatch<librbd::MockParentImageCacheImageCtx>;
namespace librbd {
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::WithArg;
using ::testing::WithArgs;
class TestMockParentCacheObjectDispatch : public TestMockFixture {
public :
typedef cache::ParentCacheObjectDispatch<librbd::MockParentImageCacheImageCtx> MockParentImageCache;
typedef plugin::Api<MockParentImageCacheImageCtx> MockPluginApi;
// ====== mock cache client ====
void expect_cache_run(MockParentImageCache& mparent_image_cache, bool ret_val) {
auto& expect = EXPECT_CALL(*(mparent_image_cache.get_cache_client()), run());
expect.WillOnce((Invoke([]() {
})));
}
void expect_cache_session_state(MockParentImageCache& mparent_image_cache, bool ret_val) {
auto & expect = EXPECT_CALL(*(mparent_image_cache.get_cache_client()), is_session_work());
expect.WillOnce((Invoke([ret_val]() {
return ret_val;
})));
}
void expect_cache_connect(MockParentImageCache& mparent_image_cache, int ret_val) {
auto& expect = EXPECT_CALL(*(mparent_image_cache.get_cache_client()), connect());
expect.WillOnce((Invoke([ret_val]() {
return ret_val;
})));
}
void expect_cache_async_connect(MockParentImageCache& mparent_image_cache, int ret_val,
Context* on_finish) {
auto& expect = EXPECT_CALL(*(mparent_image_cache.get_cache_client()), connect(_));
expect.WillOnce(WithArg<0>(Invoke([on_finish, ret_val](Context* ctx) {
ctx->complete(ret_val);
on_finish->complete(ret_val);
})));
}
void expect_cache_lookup_object(MockParentImageCache& mparent_image_cache,
const std::string &cache_path) {
EXPECT_CALL(*(mparent_image_cache.get_cache_client()),
lookup_object(_, _, _, _, _, _))
.WillOnce(WithArg<5>(Invoke([cache_path](CacheGenContextURef on_finish) {
ObjectCacheReadReplyData ack(RBDSC_READ_REPLY, 0, cache_path);
on_finish.release()->complete(&ack);
})));
}
void expect_read_parent(MockPluginApi &mock_plugin_api, uint64_t object_no,
io::ReadExtents* extents, librados::snap_t snap_id,
int r) {
EXPECT_CALL(mock_plugin_api,
read_parent(_, object_no, extents, snap_id, _, _))
.WillOnce(WithArg<5>(CompleteContext(r, static_cast<asio::ContextWQ*>(nullptr))));
}
void expect_cache_close(MockParentImageCache& mparent_image_cache, int ret_val) {
auto& expect = EXPECT_CALL(*(mparent_image_cache.get_cache_client()), close());
expect.WillOnce((Invoke([]() {
})));
}
void expect_cache_stop(MockParentImageCache& mparent_image_cache, int ret_val) {
auto& expect = EXPECT_CALL(*(mparent_image_cache.get_cache_client()), stop());
expect.WillOnce((Invoke([]() {
})));
}
void expect_cache_register(MockParentImageCache& mparent_image_cache, Context* mock_handle_register, int ret_val) {
auto& expect = EXPECT_CALL(*(mparent_image_cache.get_cache_client()), register_client(_));
expect.WillOnce(WithArg<0>(Invoke([mock_handle_register, ret_val](Context* ctx) {
if(ret_val == 0) {
mock_handle_register->complete(true);
} else {
mock_handle_register->complete(false);
}
ctx->complete(true);
return ret_val;
})));
}
void expect_io_object_dispatcher_register_state(MockParentImageCache& mparent_image_cache,
int ret_val) {
auto& expect = EXPECT_CALL((*(mparent_image_cache.get_image_ctx()->io_object_dispatcher)),
register_dispatch(_));
expect.WillOnce(WithArg<0>(Invoke([&mparent_image_cache]
(io::ObjectDispatchInterface* object_dispatch) {
ASSERT_EQ(object_dispatch, &mparent_image_cache);
})));
}
};
TEST_F(TestMockParentCacheObjectDispatch, test_initialization_success) {
librbd::ImageCtx* ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockParentImageCacheImageCtx mock_image_ctx(*ictx);
mock_image_ctx.child = &mock_image_ctx;
MockPluginApi mock_plugin_api;
auto mock_parent_image_cache = MockParentImageCache::create(&mock_image_ctx,
mock_plugin_api);
expect_cache_run(*mock_parent_image_cache, 0);
C_SaferCond cond;
Context* handle_connect = new LambdaContext([&cond](int ret) {
ASSERT_EQ(ret, 0);
cond.complete(0);
});
expect_cache_async_connect(*mock_parent_image_cache, 0, handle_connect);
Context* ctx = new LambdaContext([](bool reg) {
ASSERT_EQ(reg, true);
});
expect_cache_register(*mock_parent_image_cache, ctx, 0);
expect_io_object_dispatcher_register_state(*mock_parent_image_cache, 0);
expect_cache_close(*mock_parent_image_cache, 0);
expect_cache_stop(*mock_parent_image_cache, 0);
mock_parent_image_cache->init();
cond.wait();
ASSERT_EQ(mock_parent_image_cache->get_dispatch_layer(),
io::OBJECT_DISPATCH_LAYER_PARENT_CACHE);
expect_cache_session_state(*mock_parent_image_cache, true);
ASSERT_EQ(mock_parent_image_cache->get_cache_client()->is_session_work(), true);
mock_parent_image_cache->get_cache_client()->close();
mock_parent_image_cache->get_cache_client()->stop();
delete mock_parent_image_cache;
}
TEST_F(TestMockParentCacheObjectDispatch, test_initialization_fail_at_connect) {
librbd::ImageCtx* ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockParentImageCacheImageCtx mock_image_ctx(*ictx);
mock_image_ctx.child = &mock_image_ctx;
MockPluginApi mock_plugin_api;
auto mock_parent_image_cache = MockParentImageCache::create(&mock_image_ctx,
mock_plugin_api);
expect_cache_run(*mock_parent_image_cache, 0);
C_SaferCond cond;
Context* handle_connect = new LambdaContext([&cond](int ret) {
ASSERT_EQ(ret, -1);
cond.complete(0);
});
expect_cache_async_connect(*mock_parent_image_cache, -1, handle_connect);
expect_io_object_dispatcher_register_state(*mock_parent_image_cache, 0);
expect_cache_session_state(*mock_parent_image_cache, false);
expect_cache_close(*mock_parent_image_cache, 0);
expect_cache_stop(*mock_parent_image_cache, 0);
mock_parent_image_cache->init();
// initialization fails.
ASSERT_EQ(mock_parent_image_cache->get_dispatch_layer(),
io::OBJECT_DISPATCH_LAYER_PARENT_CACHE);
ASSERT_EQ(mock_parent_image_cache->get_cache_client()->is_session_work(), false);
mock_parent_image_cache->get_cache_client()->close();
mock_parent_image_cache->get_cache_client()->stop();
delete mock_parent_image_cache;
}
TEST_F(TestMockParentCacheObjectDispatch, test_initialization_fail_at_register) {
librbd::ImageCtx* ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockParentImageCacheImageCtx mock_image_ctx(*ictx);
mock_image_ctx.child = &mock_image_ctx;
MockPluginApi mock_plugin_api;
auto mock_parent_image_cache = MockParentImageCache::create(&mock_image_ctx,
mock_plugin_api);
expect_cache_run(*mock_parent_image_cache, 0);
C_SaferCond cond;
Context* handle_connect = new LambdaContext([&cond](int ret) {
ASSERT_EQ(ret, 0);
cond.complete(0);
});
expect_cache_async_connect(*mock_parent_image_cache, 0, handle_connect);
Context* ctx = new LambdaContext([](bool reg) {
ASSERT_EQ(reg, false);
});
expect_cache_register(*mock_parent_image_cache, ctx, -1);
expect_io_object_dispatcher_register_state(*mock_parent_image_cache, 0);
expect_cache_close(*mock_parent_image_cache, 0);
expect_cache_stop(*mock_parent_image_cache, 0);
mock_parent_image_cache->init();
cond.wait();
ASSERT_EQ(mock_parent_image_cache->get_dispatch_layer(),
io::OBJECT_DISPATCH_LAYER_PARENT_CACHE);
expect_cache_session_state(*mock_parent_image_cache, true);
ASSERT_EQ(mock_parent_image_cache->get_cache_client()->is_session_work(), true);
mock_parent_image_cache->get_cache_client()->close();
mock_parent_image_cache->get_cache_client()->stop();
delete mock_parent_image_cache;
}
TEST_F(TestMockParentCacheObjectDispatch, test_disable_interface) {
librbd::ImageCtx* ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockParentImageCacheImageCtx mock_image_ctx(*ictx);
mock_image_ctx.child = &mock_image_ctx;
MockPluginApi mock_plugin_api;
auto mock_parent_image_cache = MockParentImageCache::create(&mock_image_ctx,
mock_plugin_api);
std::string temp_oid("12345");
ceph::bufferlist temp_bl;
IOContext io_context = mock_image_ctx.get_data_io_context();
io::DispatchResult* temp_dispatch_result = nullptr;
io::Extents temp_buffer_extents;
int* temp_op_flags = nullptr;
uint64_t* temp_journal_tid = nullptr;
Context** temp_on_finish = nullptr;
Context* temp_on_dispatched = nullptr;
ZTracer::Trace* temp_trace = nullptr;
io::LightweightBufferExtents buffer_extents;
ASSERT_EQ(mock_parent_image_cache->discard(0, 0, 0, io_context, 0,
*temp_trace, temp_op_flags, temp_journal_tid, temp_dispatch_result,
temp_on_finish, temp_on_dispatched), false);
ASSERT_EQ(mock_parent_image_cache->write(0, 0, std::move(temp_bl),
io_context, 0, 0, std::nullopt, *temp_trace, temp_op_flags,
temp_journal_tid, temp_dispatch_result, temp_on_finish,
temp_on_dispatched), false);
ASSERT_EQ(mock_parent_image_cache->write_same(0, 0, 0, std::move(buffer_extents),
std::move(temp_bl), io_context, 0, *temp_trace, temp_op_flags,
temp_journal_tid, temp_dispatch_result, temp_on_finish, temp_on_dispatched), false );
ASSERT_EQ(mock_parent_image_cache->compare_and_write(0, 0, std::move(temp_bl), std::move(temp_bl),
io_context, 0, *temp_trace, temp_journal_tid, temp_op_flags,
temp_journal_tid, temp_dispatch_result, temp_on_finish,
temp_on_dispatched), false);
ASSERT_EQ(mock_parent_image_cache->flush(io::FLUSH_SOURCE_USER, *temp_trace, temp_journal_tid,
temp_dispatch_result, temp_on_finish, temp_on_dispatched), false);
ASSERT_EQ(mock_parent_image_cache->invalidate_cache(nullptr), false);
ASSERT_EQ(mock_parent_image_cache->reset_existence_cache(nullptr), false);
delete mock_parent_image_cache;
}
TEST_F(TestMockParentCacheObjectDispatch, test_read) {
librbd::ImageCtx* ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockParentImageCacheImageCtx mock_image_ctx(*ictx);
mock_image_ctx.child = &mock_image_ctx;
MockPluginApi mock_plugin_api;
auto mock_parent_image_cache = MockParentImageCache::create(&mock_image_ctx,
mock_plugin_api);
expect_cache_run(*mock_parent_image_cache, 0);
C_SaferCond conn_cond;
Context* handle_connect = new LambdaContext([&conn_cond](int ret) {
ASSERT_EQ(ret, 0);
conn_cond.complete(0);
});
expect_cache_async_connect(*mock_parent_image_cache, 0, handle_connect);
Context* ctx = new LambdaContext([](bool reg) {
ASSERT_EQ(reg, true);
});
expect_cache_register(*mock_parent_image_cache, ctx, 0);
expect_io_object_dispatcher_register_state(*mock_parent_image_cache, 0);
expect_cache_close(*mock_parent_image_cache, 0);
expect_cache_stop(*mock_parent_image_cache, 0);
mock_parent_image_cache->init();
conn_cond.wait();
ASSERT_EQ(mock_parent_image_cache->get_dispatch_layer(),
io::OBJECT_DISPATCH_LAYER_PARENT_CACHE);
expect_cache_session_state(*mock_parent_image_cache, true);
ASSERT_EQ(mock_parent_image_cache->get_cache_client()->is_session_work(), true);
auto& expect = EXPECT_CALL(*(mock_parent_image_cache->get_cache_client()), is_session_work());
expect.WillOnce(Return(true));
expect_cache_lookup_object(*mock_parent_image_cache, "/dev/null");
C_SaferCond on_dispatched;
io::DispatchResult dispatch_result;
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
mock_parent_image_cache->read(
0, &extents, mock_image_ctx.get_data_io_context(), 0, 0, {}, nullptr,
nullptr, &dispatch_result, nullptr, &on_dispatched);
ASSERT_EQ(0, on_dispatched.wait());
mock_parent_image_cache->get_cache_client()->close();
mock_parent_image_cache->get_cache_client()->stop();
delete mock_parent_image_cache;
}
TEST_F(TestMockParentCacheObjectDispatch, test_read_dne) {
librbd::ImageCtx* ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockParentImageCacheImageCtx mock_image_ctx(*ictx);
mock_image_ctx.child = &mock_image_ctx;
MockPluginApi mock_plugin_api;
auto mock_parent_image_cache = MockParentImageCache::create(&mock_image_ctx,
mock_plugin_api);
expect_cache_run(*mock_parent_image_cache, 0);
C_SaferCond conn_cond;
Context* handle_connect = new LambdaContext([&conn_cond](int ret) {
ASSERT_EQ(ret, 0);
conn_cond.complete(0);
});
expect_cache_async_connect(*mock_parent_image_cache, 0, handle_connect);
Context* ctx = new LambdaContext([](bool reg) {
ASSERT_EQ(reg, true);
});
expect_cache_register(*mock_parent_image_cache, ctx, 0);
expect_io_object_dispatcher_register_state(*mock_parent_image_cache, 0);
expect_cache_close(*mock_parent_image_cache, 0);
expect_cache_stop(*mock_parent_image_cache, 0);
mock_parent_image_cache->init();
conn_cond.wait();
ASSERT_EQ(mock_parent_image_cache->get_dispatch_layer(),
io::OBJECT_DISPATCH_LAYER_PARENT_CACHE);
expect_cache_session_state(*mock_parent_image_cache, true);
ASSERT_EQ(mock_parent_image_cache->get_cache_client()->is_session_work(),
true);
EXPECT_CALL(*(mock_parent_image_cache->get_cache_client()), is_session_work())
.WillOnce(Return(true));
expect_cache_lookup_object(*mock_parent_image_cache, "");
io::ReadExtents extents = {{0, 4096}};
expect_read_parent(mock_plugin_api, 0, &extents, CEPH_NOSNAP, 0);
C_SaferCond on_dispatched;
io::DispatchResult dispatch_result;
mock_parent_image_cache->read(
0, &extents, mock_image_ctx.get_data_io_context(), 0, 0, {}, nullptr,
nullptr, &dispatch_result, nullptr, &on_dispatched);
ASSERT_EQ(0, on_dispatched.wait());
mock_parent_image_cache->get_cache_client()->close();
mock_parent_image_cache->get_cache_client()->stop();
delete mock_parent_image_cache;
}
} // namespace librbd
| 16,108 | 36.63785 | 117 |
cc
|
null |
ceph-main/src/test/librbd/cache/test_mock_WriteAroundObjectDispatch.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "include/rbd/librbd.hpp"
#include "librbd/cache/WriteAroundObjectDispatch.h"
#include "librbd/io/ObjectDispatchSpec.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
struct MockContext : public C_SaferCond {
MOCK_METHOD1(complete, void(int));
MOCK_METHOD1(finish, void(int));
void do_complete(int r) {
C_SaferCond::complete(r);
}
};
} // anonymous namespace
} // namespace librbd
#include "librbd/cache/WriteAroundObjectDispatch.cc"
namespace librbd {
namespace cache {
using ::testing::_;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
struct TestMockCacheWriteAroundObjectDispatch : public TestMockFixture {
typedef WriteAroundObjectDispatch<librbd::MockTestImageCtx> MockWriteAroundObjectDispatch;
void expect_op_work_queue(MockTestImageCtx& mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.op_work_queue, queue(_, _))
.WillRepeatedly(Invoke([](Context* ctx, int r) {
ctx->complete(r);
}));
}
void expect_context_complete(MockContext& mock_context, int r) {
EXPECT_CALL(mock_context, complete(r))
.WillOnce(Invoke([&mock_context](int r) {
mock_context.do_complete(r);
}));
}
};
TEST_F(TestMockCacheWriteAroundObjectDispatch, WriteThrough) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 0, false);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx;
MockContext dispatch_ctx;
Context* finish_ctx_ptr = &finish_ctx;
ASSERT_FALSE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr,
&dispatch_ctx));
ASSERT_EQ(finish_ctx_ptr, &finish_ctx);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, WriteThroughUntilFlushed) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 4096, true);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx;
MockContext dispatch_ctx;
Context* finish_ctx_ptr = &finish_ctx;
ASSERT_FALSE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr,
&dispatch_ctx));
ASSERT_EQ(finish_ctx_ptr, &finish_ctx);
ASSERT_FALSE(object_dispatch.flush(io::FLUSH_SOURCE_USER, {}, nullptr,
&dispatch_result, &finish_ctx_ptr,
&dispatch_ctx));
expect_context_complete(dispatch_ctx, 0);
expect_context_complete(finish_ctx, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr,
&dispatch_ctx));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr, &finish_ctx);
ASSERT_EQ(0, dispatch_ctx.wait());
ASSERT_EQ(0, finish_ctx.wait());
finish_ctx_ptr->complete(0);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, DispatchIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 4096, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx;
MockContext dispatch_ctx;
Context* finish_ctx_ptr = &finish_ctx;
expect_context_complete(dispatch_ctx, 0);
expect_context_complete(finish_ctx, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr,
&dispatch_ctx));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr, &finish_ctx);
ASSERT_EQ(0, dispatch_ctx.wait());
ASSERT_EQ(0, finish_ctx.wait());
finish_ctx_ptr->complete(0);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, BlockedIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 16384, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx1;
MockContext dispatch_ctx1;
Context* finish_ctx_ptr1 = &finish_ctx1;
expect_context_complete(dispatch_ctx1, 0);
expect_context_complete(finish_ctx1, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt,{}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr1,
&dispatch_ctx1));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr1, &finish_ctx1);
MockContext finish_ctx2;
MockContext dispatch_ctx2;
Context* finish_ctx_ptr2 = &finish_ctx2;
expect_context_complete(dispatch_ctx2, 0);
expect_context_complete(finish_ctx2, 0);
ASSERT_TRUE(object_dispatch.write(0, 4096, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr2,
&dispatch_ctx2));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr2, &finish_ctx2);
MockContext finish_ctx3;
MockContext dispatch_ctx3;
Context* finish_ctx_ptr3 = &finish_ctx3;
ASSERT_TRUE(object_dispatch.write(0, 1024, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr3,
&dispatch_ctx3));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr3, &finish_ctx3);
ASSERT_EQ(0, dispatch_ctx1.wait());
ASSERT_EQ(0, dispatch_ctx2.wait());
ASSERT_EQ(0, finish_ctx1.wait());
ASSERT_EQ(0, finish_ctx2.wait());
finish_ctx_ptr2->complete(0);
expect_context_complete(dispatch_ctx3, 0);
expect_context_complete(finish_ctx3, 0);
finish_ctx_ptr1->complete(0);
ASSERT_EQ(0, dispatch_ctx3.wait());
finish_ctx_ptr3->complete(0);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, QueuedIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 4095, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx1;
MockContext dispatch_ctx1;
Context* finish_ctx_ptr1 = &finish_ctx1;
expect_context_complete(dispatch_ctx1, 0);
expect_context_complete(finish_ctx1, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr1,
&dispatch_ctx1));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr1, &finish_ctx1);
MockContext finish_ctx2;
MockContext dispatch_ctx2;
Context* finish_ctx_ptr2 = &finish_ctx2;
ASSERT_TRUE(object_dispatch.write(0, 8192, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr2,
&dispatch_ctx2));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr2, &finish_ctx2);
ASSERT_EQ(0, dispatch_ctx1.wait());
expect_context_complete(dispatch_ctx2, 0);
expect_context_complete(finish_ctx2, 0);
finish_ctx_ptr1->complete(0);
ASSERT_EQ(0, finish_ctx1.wait());
ASSERT_EQ(0, dispatch_ctx2.wait());
ASSERT_EQ(0, finish_ctx2.wait());
finish_ctx_ptr2->complete(0);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, BlockedAndQueuedIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 8196, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx1;
MockContext dispatch_ctx1;
Context* finish_ctx_ptr1 = &finish_ctx1;
expect_context_complete(dispatch_ctx1, 0);
expect_context_complete(finish_ctx1, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr1,
&dispatch_ctx1));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr1, &finish_ctx1);
MockContext finish_ctx2;
MockContext dispatch_ctx2;
Context* finish_ctx_ptr2 = &finish_ctx2;
expect_context_complete(dispatch_ctx2, 0);
expect_context_complete(finish_ctx2, 0);
ASSERT_TRUE(object_dispatch.write(0, 4096, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr2,
&dispatch_ctx2));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr2, &finish_ctx2);
MockContext finish_ctx3;
MockContext dispatch_ctx3;
Context* finish_ctx_ptr3 = &finish_ctx3;
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr3,
&dispatch_ctx3));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr3, &finish_ctx3);
ASSERT_EQ(0, dispatch_ctx1.wait());
ASSERT_EQ(0, dispatch_ctx2.wait());
ASSERT_EQ(0, finish_ctx1.wait());
ASSERT_EQ(0, finish_ctx2.wait());
finish_ctx_ptr2->complete(0);
expect_context_complete(dispatch_ctx3, 0);
expect_context_complete(finish_ctx3, 0);
finish_ctx_ptr1->complete(0);
ASSERT_EQ(0, dispatch_ctx3.wait());
ASSERT_EQ(0, finish_ctx3.wait());
finish_ctx_ptr3->complete(0);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, Flush) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 4096, true);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
io::DispatchResult dispatch_result;
MockContext finish_ctx;
MockContext dispatch_ctx;
Context* finish_ctx_ptr = &finish_ctx;
ASSERT_FALSE(object_dispatch.flush(io::FLUSH_SOURCE_USER, {}, nullptr,
&dispatch_result, &finish_ctx_ptr,
&dispatch_ctx));
ASSERT_EQ(finish_ctx_ptr, &finish_ctx);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, FlushQueuedOnInFlightIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 4096, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx1;
MockContext dispatch_ctx1;
Context* finish_ctx_ptr1 = &finish_ctx1;
expect_context_complete(dispatch_ctx1, 0);
expect_context_complete(finish_ctx1, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr1,
&dispatch_ctx1));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr1, &finish_ctx1);
ASSERT_EQ(0, dispatch_ctx1.wait());
MockContext finish_ctx2;
MockContext dispatch_ctx2;
Context* finish_ctx_ptr2 = &finish_ctx2;
ASSERT_FALSE(object_dispatch.flush(io::FLUSH_SOURCE_USER, {}, nullptr,
&dispatch_result, &finish_ctx_ptr2,
&dispatch_ctx2));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr2, &finish_ctx2);
expect_context_complete(finish_ctx2, 0);
finish_ctx_ptr1->complete(0);
ASSERT_EQ(0, finish_ctx1.wait());
finish_ctx_ptr2->complete(0);
ASSERT_EQ(0, finish_ctx2.wait());
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, FlushQueuedOnQueuedIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 4096, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx1;
MockContext dispatch_ctx1;
Context* finish_ctx_ptr1 = &finish_ctx1;
expect_context_complete(dispatch_ctx1, 0);
expect_context_complete(finish_ctx1, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr1,
&dispatch_ctx1));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr1, &finish_ctx1);
ASSERT_EQ(0, dispatch_ctx1.wait());
MockContext finish_ctx2;
MockContext dispatch_ctx2;
Context* finish_ctx_ptr2 = &finish_ctx2;
ASSERT_TRUE(object_dispatch.write(0, 8192, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr2,
&dispatch_ctx2));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr2, &finish_ctx2);
ASSERT_EQ(0, dispatch_ctx1.wait());
MockContext finish_ctx3;
MockContext dispatch_ctx3;
Context* finish_ctx_ptr3 = &finish_ctx3;
ASSERT_TRUE(object_dispatch.flush(io::FLUSH_SOURCE_USER, {}, nullptr,
&dispatch_result, &finish_ctx_ptr3,
&dispatch_ctx3));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr3, &finish_ctx3);
expect_context_complete(dispatch_ctx2, 0);
expect_context_complete(finish_ctx2, 0);
expect_context_complete(dispatch_ctx3, 0);
finish_ctx_ptr1->complete(0);
ASSERT_EQ(0, finish_ctx1.wait());
ASSERT_EQ(0, dispatch_ctx2.wait());
ASSERT_EQ(0, finish_ctx2.wait());
expect_context_complete(finish_ctx3, 0);
finish_ctx_ptr2->complete(0);
finish_ctx_ptr3->complete(0);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, FlushError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 4096, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx1;
MockContext dispatch_ctx1;
Context* finish_ctx_ptr1 = &finish_ctx1;
expect_context_complete(dispatch_ctx1, 0);
expect_context_complete(finish_ctx1, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr1,
&dispatch_ctx1));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr1, &finish_ctx1);
ASSERT_EQ(0, dispatch_ctx1.wait());
ASSERT_EQ(0, finish_ctx1.wait());
MockContext finish_ctx2;
MockContext dispatch_ctx2;
Context* finish_ctx_ptr2 = &finish_ctx2;
ASSERT_FALSE(object_dispatch.flush(io::FLUSH_SOURCE_USER, {}, nullptr,
&dispatch_result, &finish_ctx_ptr2,
&dispatch_ctx2));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr2, &finish_ctx2);
expect_context_complete(finish_ctx2, -EPERM);
finish_ctx_ptr1->complete(-EPERM);
finish_ctx_ptr2->complete(0);
ASSERT_EQ(-EPERM, finish_ctx2.wait());
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, UnoptimizedIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 16384, false);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx;
MockContext dispatch_ctx;
Context* finish_ctx_ptr = &finish_ctx;
ASSERT_FALSE(object_dispatch.compare_and_write(0, 0, std::move(data),
std::move(data), {}, 0, {},
nullptr, nullptr, nullptr,
&dispatch_result,
&finish_ctx_ptr,
&dispatch_ctx));
ASSERT_EQ(finish_ctx_ptr, &finish_ctx);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, UnoptimizedIOInFlightIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 16384, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx1;
MockContext dispatch_ctx1;
Context* finish_ctx_ptr1 = &finish_ctx1;
expect_context_complete(dispatch_ctx1, 0);
expect_context_complete(finish_ctx1, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr1,
&dispatch_ctx1));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr1, &finish_ctx1);
ASSERT_EQ(0, dispatch_ctx1.wait());
ASSERT_EQ(0, finish_ctx1.wait());
MockContext finish_ctx2;
MockContext dispatch_ctx2;
Context* finish_ctx_ptr2 = &finish_ctx2;
ASSERT_TRUE(object_dispatch.compare_and_write(0, 0, std::move(data),
std::move(data), {}, 0, {},
nullptr, nullptr, nullptr,
&dispatch_result,
&finish_ctx_ptr2,
&dispatch_ctx2));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_EQ(finish_ctx_ptr2, &finish_ctx2);
expect_context_complete(dispatch_ctx2, 0);
finish_ctx_ptr1->complete(0);
ASSERT_EQ(0, dispatch_ctx2.wait());
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, UnoptimizedIOBlockedIO) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 4096, false);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx1;
MockContext dispatch_ctx1;
Context* finish_ctx_ptr1 = &finish_ctx1;
expect_context_complete(dispatch_ctx1, 0);
expect_context_complete(finish_ctx1, 0);
ASSERT_TRUE(object_dispatch.write(0, 0, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr1,
&dispatch_ctx1));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr1, &finish_ctx1);
ASSERT_EQ(0, dispatch_ctx1.wait());
ASSERT_EQ(0, finish_ctx1.wait());
MockContext finish_ctx2;
MockContext dispatch_ctx2;
Context* finish_ctx_ptr2 = &finish_ctx2;
ASSERT_TRUE(object_dispatch.write(0, 4096, std::move(data), {}, 0, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr2,
&dispatch_ctx2));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_NE(finish_ctx_ptr2, &finish_ctx2);
MockContext finish_ctx3;
MockContext dispatch_ctx3;
Context* finish_ctx_ptr3 = &finish_ctx3;
ASSERT_TRUE(object_dispatch.compare_and_write(0, 0, std::move(data),
std::move(data), {}, 0, {},
nullptr, nullptr, nullptr,
&dispatch_result,
&finish_ctx_ptr3,
&dispatch_ctx3));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_EQ(finish_ctx_ptr3, &finish_ctx3);
expect_context_complete(dispatch_ctx3, 0);
expect_context_complete(dispatch_ctx2, 0);
expect_context_complete(finish_ctx2, 0);
finish_ctx_ptr1->complete(0);
ASSERT_EQ(0, dispatch_ctx3.wait());
ASSERT_EQ(0, dispatch_ctx2.wait());
ASSERT_EQ(0, finish_ctx2.wait());
finish_ctx_ptr2->complete(0);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, WriteFUA) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 16384, false);
InSequence seq;
bufferlist data;
data.append(std::string(4096, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx;
MockContext dispatch_ctx;
Context* finish_ctx_ptr = &finish_ctx;
ASSERT_FALSE(object_dispatch.write(0, 0, std::move(data), {},
LIBRADOS_OP_FLAG_FADVISE_FUA, 0,
std::nullopt, {}, nullptr, nullptr,
&dispatch_result, &finish_ctx_ptr,
&dispatch_ctx));
ASSERT_EQ(finish_ctx_ptr, &finish_ctx);
}
TEST_F(TestMockCacheWriteAroundObjectDispatch, WriteSameFUA) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockWriteAroundObjectDispatch object_dispatch(&mock_image_ctx, 16384, false);
InSequence seq;
bufferlist data;
data.append(std::string(512, '1'));
io::DispatchResult dispatch_result;
MockContext finish_ctx;
MockContext dispatch_ctx;
Context* finish_ctx_ptr = &finish_ctx;
ASSERT_FALSE(object_dispatch.write_same(0, 0, 8192, {{0, 8192}},
std::move(data), {},
LIBRADOS_OP_FLAG_FADVISE_FUA, {},
nullptr, nullptr, &dispatch_result,
&finish_ctx_ptr, &dispatch_ctx));
ASSERT_EQ(finish_ctx_ptr, &finish_ctx);
}
} // namespace cache
} // namespace librbd
| 24,920 | 34.399148 | 92 |
cc
|
null |
ceph-main/src/test/librbd/cache/pwl/test_WriteLogMap.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/cache/pwl/LogMap.cc"
void register_test_write_log_map() {
}
namespace librbd {
namespace cache {
namespace pwl {
using namespace std;
struct TestLogEntry {
uint64_t image_offset_bytes;
uint64_t write_bytes;
uint32_t referring_map_entries = 0;
TestLogEntry(const uint64_t image_offset_bytes, const uint64_t write_bytes)
: image_offset_bytes(image_offset_bytes), write_bytes(write_bytes) {
}
uint64_t get_offset_bytes() {
return image_offset_bytes;
}
uint64_t get_write_bytes() {
return write_bytes;
}
BlockExtent block_extent() {
return BlockExtent(image_offset_bytes, image_offset_bytes + write_bytes);
}
uint32_t get_map_ref() {
return referring_map_entries;
}
void inc_map_ref() {
referring_map_entries++;
}
void dec_map_ref() {
referring_map_entries--;
}
friend std::ostream &operator<<(std::ostream &os,
const TestLogEntry &entry) {
os << "referring_map_entries=" << entry.referring_map_entries << ", "
<< "image_offset_bytes=" << entry.image_offset_bytes << ", "
<< "write_bytes=" << entry.write_bytes;
return os;
};
};
typedef std::list<std::shared_ptr<TestLogEntry>> TestLogEntries;
typedef LogMapEntry<TestLogEntry> TestMapEntry;
typedef LogMapEntries<TestLogEntry> TestLogMapEntries;
typedef LogMap<TestLogEntry> TestLogMap;
class TestWriteLogMap : public TestFixture {
public:
void SetUp() override {
TestFixture::SetUp();
m_cct = reinterpret_cast<CephContext*>(m_ioctx.cct());
}
CephContext *m_cct;
};
TEST_F(TestWriteLogMap, Simple) {
TestLogEntries es;
TestLogMapEntries lme;
TestLogMap map(m_cct);
/* LogEntry takes offset, length, in bytes */
auto e1 = make_shared<TestLogEntry>(4, 8);
TestLogEntry *e1_ptr = e1.get();
ASSERT_EQ(4, e1_ptr->get_offset_bytes());
ASSERT_EQ(8, e1_ptr->get_write_bytes());
map.add_log_entry(e1);
/* BlockExtent takes first, last, in blocks */
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(0, 100));
int numfound = found0.size();
/* Written range includes the single write above */
ASSERT_EQ(1, numfound);
ASSERT_EQ(e1, found0.front().log_entry);
/* Nothing before that */
found0 = map.find_map_entries(BlockExtent(0, 3));
numfound = found0.size();
ASSERT_EQ(0, numfound);
/* Nothing after that */
found0 = map.find_map_entries(BlockExtent(12, 99));
numfound = found0.size();
ASSERT_EQ(0, numfound);
/* 4-11 will be e1 */
for (int i=4; i<12; i++) {
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(i, i + 1));
int numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e1, found0.front().log_entry);
}
map.remove_log_entry(e1);
/* Nothing should be found */
for (int i=4; i<12; i++) {
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(i, i + 1));
int numfound = found0.size();
ASSERT_EQ(0, numfound);
}
}
TEST_F(TestWriteLogMap, OverlapFront) {
TestLogMap map(m_cct);
auto e0 = make_shared<TestLogEntry>(4, 8);
map.add_log_entry(e0);
/* replaces block 4-7 of e0 */
auto e1 = make_shared<TestLogEntry>(0, 8);
map.add_log_entry(e1);
/* Written range includes the two writes above */
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(0, 100));
int numfound = found0.size();
ASSERT_EQ(2, numfound);
ASSERT_EQ(e1, found0.front().log_entry);
ASSERT_EQ(0, found0.front().block_extent.block_start);
ASSERT_EQ(8, found0.front().block_extent.block_end);
found0.pop_front();
ASSERT_EQ(e0, found0.front().log_entry);
ASSERT_EQ(8, found0.front().block_extent.block_start);
ASSERT_EQ(12, found0.front().block_extent.block_end);
/* 0-7 will be e1 */
for (int i=0; i<8; i++) {
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(i, i + 1));
int numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e1, found0.front().log_entry);
}
/* 8-11 will be e0 */
for (int i=8; i<12; i++) {
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(i, i + 1));
int numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e0, found0.front().log_entry);
}
}
TEST_F(TestWriteLogMap, OverlapBack) {
TestLogMap map(m_cct);
auto e0 = make_shared<TestLogEntry>(0, 8);
map.add_log_entry(e0);
/* replaces block 4-7 of e0 */
auto e1 = make_shared<TestLogEntry>(4, 8);
map.add_log_entry(e1);
/* Written range includes the two writes above */
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(0, 100));
int numfound = found0.size();
ASSERT_EQ(2, numfound);
ASSERT_EQ(e0, found0.front().log_entry);
ASSERT_EQ(0, found0.front().block_extent.block_start);
ASSERT_EQ(4, found0.front().block_extent.block_end);
found0.pop_front();
ASSERT_EQ(e1, found0.front().log_entry);
ASSERT_EQ(4, found0.front().block_extent.block_start);
ASSERT_EQ(12, found0.front().block_extent.block_end);
/* 0-3 will be e0 */
for (int i=0; i<4; i++) {
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(i, i + 1));
int numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e0, found0.front().log_entry);
}
/* 4-11 will be e1 */
for (int i=4; i<12; i++) {
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(i, i + 1));
int numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e1, found0.front().log_entry);
}
map.remove_log_entry(e0);
/* 0-3 will find nothing */
for (int i=0; i<4; i++) {
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(i, i + 1));
int numfound = found0.size();
ASSERT_EQ(0, numfound);
}
/* 4-11 will still be e1 */
for (int i=4; i<12; i++) {
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(i, i + 1));
int numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e1, found0.front().log_entry);
}
}
TEST_F(TestWriteLogMap, OverlapMiddle) {
TestLogMap map(m_cct);
auto e0 = make_shared<TestLogEntry>(0, 1);
map.add_log_entry(e0);
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(0, 1));
int numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e0, found0.front().log_entry);
TestLogEntries entries = map.find_log_entries(BlockExtent(0, 1));
int entriesfound = entries.size();
ASSERT_EQ(1, entriesfound);
ASSERT_EQ(e0, entries.front());
auto e1 = make_shared<TestLogEntry>(1, 1);
map.add_log_entry(e1);
found0 = map.find_map_entries(BlockExtent(1, 2));
numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e1, found0.front().log_entry);
entries = map.find_log_entries(BlockExtent(1, 2));
entriesfound = entries.size();
ASSERT_EQ(1, entriesfound);
ASSERT_EQ(e1, entries.front());
auto e2 = make_shared<TestLogEntry>(2, 1);
map.add_log_entry(e2);
found0 = map.find_map_entries(BlockExtent(2, 3));
numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e2, found0.front().log_entry);
entries = map.find_log_entries(BlockExtent(2, 3));
entriesfound = entries.size();
ASSERT_EQ(1, entriesfound);
ASSERT_EQ(e2, entries.front());
/* replaces e1 */
auto e3 = make_shared<TestLogEntry>(1, 1);
map.add_log_entry(e3);
found0 = map.find_map_entries(BlockExtent(1, 2));
numfound = found0.size();
ASSERT_EQ(1, numfound);
ASSERT_EQ(e3, found0.front().log_entry);
entries = map.find_log_entries(BlockExtent(1, 2));
entriesfound = entries.size();
ASSERT_EQ(1, entriesfound);
ASSERT_EQ(e3, entries.front());
found0 = map.find_map_entries(BlockExtent(0, 100));
numfound = found0.size();
ASSERT_EQ(3, numfound);
ASSERT_EQ(e0, found0.front().log_entry);
found0.pop_front();
ASSERT_EQ(e3, found0.front().log_entry);
found0.pop_front();
ASSERT_EQ(e2, found0.front().log_entry);
entries = map.find_log_entries(BlockExtent(0, 100));
entriesfound = entries.size();
ASSERT_EQ(3, entriesfound);
ASSERT_EQ(e0, entries.front());
entries.pop_front();
ASSERT_EQ(e3, entries.front());
entries.pop_front();
ASSERT_EQ(e2, entries.front());
entries.clear();
entries.emplace_back(e0);
entries.emplace_back(e1);
map.remove_log_entries(entries);
found0 = map.find_map_entries(BlockExtent(0, 100));
numfound = found0.size();
ASSERT_EQ(2, numfound);
ASSERT_EQ(e3, found0.front().log_entry);
found0.pop_front();
ASSERT_EQ(e2, found0.front().log_entry);
}
TEST_F(TestWriteLogMap, OverlapSplit) {
TestLogMap map(m_cct);
auto e0 = make_shared<TestLogEntry>(0, 8);
map.add_log_entry(e0);
/* Splits e0 at 1 */
auto e1 = make_shared<TestLogEntry>(1, 1);
map.add_log_entry(e1);
/* Splits e0 again at 4 */
auto e2 = make_shared<TestLogEntry>(4, 2);
map.add_log_entry(e2);
/* Replaces one block of e2, and one of e0 */
auto e3 = make_shared<TestLogEntry>(5, 2);
map.add_log_entry(e3);
/* Expecting: 0:e0, 1:e1, 2..3:e0, 4:e2, 5..6:e3, 7:e0 */
TestLogMapEntries found0 = map.find_map_entries(BlockExtent(0, 100));
int numfound = found0.size();
ASSERT_EQ(6, numfound);
ASSERT_EQ(e0, found0.front().log_entry);
ASSERT_EQ(0, found0.front().block_extent.block_start);
ASSERT_EQ(1, found0.front().block_extent.block_end);
found0.pop_front();
ASSERT_EQ(e1, found0.front().log_entry);
ASSERT_EQ(1, found0.front().block_extent.block_start);
ASSERT_EQ(2, found0.front().block_extent.block_end);
found0.pop_front();
ASSERT_EQ(e0, found0.front().log_entry);
ASSERT_EQ(2, found0.front().block_extent.block_start);
ASSERT_EQ(4, found0.front().block_extent.block_end);
found0.pop_front();
ASSERT_EQ(e2, found0.front().log_entry);
ASSERT_EQ(4, found0.front().block_extent.block_start);
ASSERT_EQ(5, found0.front().block_extent.block_end);
found0.pop_front();
ASSERT_EQ(e3, found0.front().log_entry);
ASSERT_EQ(5, found0.front().block_extent.block_start);
ASSERT_EQ(7, found0.front().block_extent.block_end);
found0.pop_front();
ASSERT_EQ(e0, found0.front().log_entry);
ASSERT_EQ(7, found0.front().block_extent.block_start);
ASSERT_EQ(8, found0.front().block_extent.block_end);
}
} // namespace pwl
} // namespace cache
} // namespace librbd
| 10,309 | 29.412979 | 77 |
cc
|
null |
ceph-main/src/test/librbd/cache/pwl/test_mock_ReplicatedWriteLog.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <iostream>
#include "common/hostname.h"
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "include/rbd/librbd.hpp"
#include "librbd/cache/pwl/ImageCacheState.h"
#include "librbd/cache/pwl/Types.h"
#include "librbd/cache/ImageWriteback.h"
#include "librbd/plugin/Api.h"
namespace librbd {
namespace {
struct MockContextRWL : public C_SaferCond {
MOCK_METHOD1(complete, void(int));
MOCK_METHOD1(finish, void(int));
void do_complete(int r) {
C_SaferCond::complete(r);
}
};
} // anonymous namespace
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
#include "librbd/cache/pwl/AbstractWriteLog.cc"
#include "librbd/cache/pwl/rwl/WriteLog.cc"
template class librbd::cache::pwl::rwl::WriteLog<librbd::MockImageCtx>;
// template definitions
#include "librbd/cache/ImageWriteback.cc"
#include "librbd/cache/pwl/ImageCacheState.cc"
#include "librbd/cache/pwl/Request.cc"
#include "librbd/cache/pwl/rwl/Request.cc"
#include "librbd/plugin/Api.cc"
namespace librbd {
namespace cache {
namespace pwl {
using ::testing::_;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
typedef io::Extent Extent;
typedef io::Extents Extents;
struct TestMockCacheReplicatedWriteLog : public TestMockFixture {
typedef librbd::cache::pwl::rwl::WriteLog<librbd::MockImageCtx> MockReplicatedWriteLog;
typedef librbd::cache::pwl::ImageCacheState<librbd::MockImageCtx> MockImageCacheStateRWL;
typedef librbd::cache::ImageWriteback<librbd::MockImageCtx> MockImageWriteback;
typedef librbd::plugin::Api<librbd::MockImageCtx> MockApi;
MockImageCacheStateRWL *get_cache_state(
MockImageCtx& mock_image_ctx, MockApi& mock_api) {
MockImageCacheStateRWL *rwl_state = new MockImageCacheStateRWL(&mock_image_ctx, mock_api);
return rwl_state;
}
void validate_cache_state(librbd::ImageCtx *image_ctx,
MockImageCacheStateRWL &state,
bool present, bool empty, bool clean,
string host, string path,
uint64_t size) {
ASSERT_EQ(present, state.present);
ASSERT_EQ(empty, state.empty);
ASSERT_EQ(clean, state.clean);
ASSERT_EQ(host, state.host);
ASSERT_EQ(path, state.path);
ASSERT_EQ(size, state.size);
}
void expect_context_complete(MockContextRWL& mock_context, int r) {
EXPECT_CALL(mock_context, complete(r))
.WillRepeatedly(Invoke([&mock_context](int r) {
mock_context.do_complete(r);
}));
}
void expect_metadata_set(MockImageCtx& mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.operations, execute_metadata_set(_, _, _))
.WillRepeatedly(Invoke([](std::string key, std::string val, Context* ctx) {
ctx->complete(0);
}));
}
void expect_metadata_remove(MockImageCtx& mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.operations, execute_metadata_remove(_, _))
.WillRepeatedly(Invoke([](std::string key, Context* ctx) {
ctx->complete(0);
}));
}
};
TEST_F(TestMockCacheReplicatedWriteLog, init_state_write) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockApi mock_api;
MockImageCacheStateRWL image_cache_state(&mock_image_ctx, mock_api);
validate_cache_state(ictx, image_cache_state, false, true, true, "", "", 0);
image_cache_state.empty = false;
image_cache_state.clean = false;
ceph::mutex lock = ceph::make_mutex("MockImageCacheStateRWL lock");
MockContextRWL finish_ctx;
expect_metadata_set(mock_image_ctx);
expect_context_complete(finish_ctx, 0);
std::unique_lock locker(lock);
image_cache_state.write_image_cache_state(locker, &finish_ctx);
ASSERT_FALSE(locker.owns_lock());
ASSERT_EQ(0, finish_ctx.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, init_state_json_write) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockApi mock_api;
MockImageCacheStateRWL image_cache_state(&mock_image_ctx, mock_api);
string strf = "{ \"present\": true, \"empty\": false, \"clean\": false, \
\"host\": \"testhost\", \
\"path\": \"/tmp\", \
\"mode\": \"rwl\", \
\"size\": 1024 }";
json_spirit::mValue json_root;
ASSERT_TRUE(json_spirit::read(strf.c_str(), json_root));
ASSERT_TRUE(image_cache_state.init_from_metadata(json_root));
validate_cache_state(ictx, image_cache_state, true, false, false,
"testhost", "/tmp", 1024);
MockContextRWL finish_ctx;
expect_metadata_remove(mock_image_ctx);
expect_context_complete(finish_ctx, 0);
image_cache_state.clear_image_cache_state(&finish_ctx);
ASSERT_EQ(0, finish_ctx.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, init_shutdown) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
MockContextRWL finish_ctx1;
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
rwl.shut_down(&finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, write) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
MockContextRWL finish_ctx1;
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, flush) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_flush;
expect_context_complete(finish_ctx_flush, 0);
rwl.flush(&finish_ctx_flush);
ASSERT_EQ(0, finish_ctx_flush.wait());
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, flush_source_shutdown) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_flush;
expect_context_complete(finish_ctx_flush, 0);
rwl.flush(io::FLUSH_SOURCE_SHUTDOWN, &finish_ctx_flush);
ASSERT_EQ(0, finish_ctx_flush.wait());
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, flush_source_internal) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_flush;
expect_context_complete(finish_ctx_flush, 0);
rwl.flush(io::FLUSH_SOURCE_INTERNAL, &finish_ctx_flush);
ASSERT_EQ(0, finish_ctx_flush.wait());
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, flush_source_user) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
usleep(10000);
MockContextRWL finish_ctx_flush;
expect_context_complete(finish_ctx_flush, 0);
rwl.flush(io::FLUSH_SOURCE_USER, &finish_ctx_flush);
ASSERT_EQ(0, finish_ctx_flush.wait());
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, read_hit_rwl_cache) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_read;
expect_context_complete(finish_ctx_read, 0);
Extents image_extents_read{{0, 4096}};
bufferlist read_bl;
rwl.read(std::move(image_extents_read), &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(bl_copy.contents_equal(read_bl));
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, read_hit_part_rwl_cache) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_read;
Extents image_extents_read{{512, 4096}};
bufferlist hit_bl;
bl_copy.begin(511).copy(4096-512, hit_bl);
expect_context_complete(finish_ctx_read, 512);
bufferlist read_bl;
rwl.read(std::move(image_extents_read), &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(512, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
bufferlist read_bl_hit;
read_bl.begin(0).copy(4096-512, read_bl_hit);
ASSERT_TRUE(hit_bl.contents_equal(read_bl_hit));
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, read_miss_rwl_cache) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_read;
Extents image_extents_read{{4096, 4096}};
expect_context_complete(finish_ctx_read, 4096);
bufferlist read_bl;
ASSERT_EQ(0, read_bl.length());
rwl.read(std::move(image_extents_read), &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(4096, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, discard) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_discard;
expect_context_complete(finish_ctx_discard, 0);
rwl.discard(0, 4096, 1, &finish_ctx_discard);
ASSERT_EQ(0, finish_ctx_discard.wait());
MockContextRWL finish_ctx_read;
bufferlist read_bl;
expect_context_complete(finish_ctx_read, 0);
rwl.read({{0, 4096}}, &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(read_bl.is_zero());
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, writesame) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
bufferlist bl, test_bl;
bl.append(std::string(512, '1'));
test_bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
rwl.writesame(0, 4096, std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_read;
bufferlist read_bl;
expect_context_complete(finish_ctx_read, 0);
rwl.read({{0, 4096}}, &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(test_bl.contents_equal(read_bl));
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, invalidate) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_invalidate;
expect_context_complete(finish_ctx_invalidate, 0);
rwl.invalidate(&finish_ctx_invalidate);
ASSERT_EQ(0, finish_ctx_invalidate.wait());
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, compare_and_write_compare_matched) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl1;
bl1.append(std::string(4096, '1'));
bufferlist com_bl = bl1;
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl1), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_cw;
bufferlist bl2;
bl2.append(std::string(4096, '2'));
bufferlist bl2_copy = bl2;
uint64_t mismatch_offset = -1;
expect_context_complete(finish_ctx_cw, 0);
rwl.compare_and_write({{0, 4096}}, std::move(com_bl), std::move(bl2),
&mismatch_offset, fadvise_flags, &finish_ctx_cw);
ASSERT_EQ(0, finish_ctx_cw.wait());
ASSERT_EQ(0, mismatch_offset);
MockContextRWL finish_ctx_read;
bufferlist read_bl;
expect_context_complete(finish_ctx_read, 0);
rwl.read({{0, 4096}}, &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(bl2_copy.contents_equal(read_bl));
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheReplicatedWriteLog, compare_and_write_compare_failed) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockReplicatedWriteLog rwl(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextRWL finish_ctx1;
expect_context_complete(finish_ctx1, 0);
rwl.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextRWL finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl1;
bl1.append(std::string(4096, '1'));
bufferlist bl1_copy = bl1;
int fadvise_flags = 0;
rwl.write(std::move(image_extents), std::move(bl1), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextRWL finish_ctx_cw;
bufferlist bl2;
bl2.append(std::string(4096, '2'));
bufferlist com_bl = bl2;
uint64_t mismatch_offset = -1;
expect_context_complete(finish_ctx_cw, -EILSEQ);
rwl.compare_and_write({{0, 4096}}, std::move(com_bl), std::move(bl2),
&mismatch_offset, fadvise_flags, &finish_ctx_cw);
ASSERT_EQ(-EILSEQ, finish_ctx_cw.wait());
ASSERT_EQ(0, mismatch_offset);
MockContextRWL finish_ctx_read;
bufferlist read_bl;
expect_context_complete(finish_ctx_read, 0);
rwl.read({{0, 4096}}, &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(bl1_copy.contents_equal(read_bl));
MockContextRWL finish_ctx3;
expect_context_complete(finish_ctx3, 0);
rwl.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
} // namespace pwl
} // namespace cache
} // namespace librbd
| 24,205 | 31.534946 | 94 |
cc
|
null |
ceph-main/src/test/librbd/cache/pwl/test_mock_SSDWriteLog.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <iostream>
#include "common/hostname.h"
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "include/rbd/librbd.hpp"
#include "librbd/cache/pwl/AbstractWriteLog.h"
#include "librbd/cache/pwl/ImageCacheState.h"
#include "librbd/cache/pwl/Types.h"
#include "librbd/cache/ImageWriteback.h"
#include "librbd/plugin/Api.h"
namespace librbd {
namespace {
struct MockContextSSD : public C_SaferCond {
MOCK_METHOD1(complete, void(int));
MOCK_METHOD1(finish, void(int));
void do_complete(int r) {
C_SaferCond::complete(r);
}
};
} // anonymous namespace
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
#include "librbd/cache/pwl/AbstractWriteLog.cc"
#include "librbd/cache/pwl/ssd/WriteLog.cc"
template class librbd::cache::pwl::ssd::WriteLog<librbd::MockImageCtx>;
// template definitions
#include "librbd/cache/ImageWriteback.cc"
#include "librbd/cache/pwl/ImageCacheState.cc"
#include "librbd/cache/pwl/Request.cc"
#include "librbd/plugin/Api.cc"
#include "librbd/cache/pwl/ssd/Request.cc"
namespace librbd {
namespace cache {
namespace pwl {
using ::testing::_;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
typedef io::Extent Extent;
typedef io::Extents Extents;
struct TestMockCacheSSDWriteLog : public TestMockFixture {
typedef librbd::cache::pwl::ssd::WriteLog<librbd::MockImageCtx> MockSSDWriteLog;
typedef librbd::cache::pwl::ImageCacheState<librbd::MockImageCtx> MockImageCacheStateSSD;
typedef librbd::cache::ImageWriteback<librbd::MockImageCtx> MockImageWriteback;
typedef librbd::plugin::Api<librbd::MockImageCtx> MockApi;
MockImageCacheStateSSD *get_cache_state(
MockImageCtx& mock_image_ctx, MockApi& mock_api) {
MockImageCacheStateSSD *ssd_state = new MockImageCacheStateSSD(
&mock_image_ctx, mock_api);
return ssd_state;
}
void validate_cache_state(librbd::ImageCtx *image_ctx,
MockImageCacheStateSSD &state,
bool present, bool empty, bool clean,
string host, string path,
uint64_t size) {
ASSERT_EQ(present, state.present);
ASSERT_EQ(empty, state.empty);
ASSERT_EQ(clean, state.clean);
ASSERT_EQ(host, state.host);
ASSERT_EQ(path, state.path);
ASSERT_EQ(size, state.size);
}
void expect_context_complete(MockContextSSD& mock_context, int r) {
EXPECT_CALL(mock_context, complete(r))
.WillRepeatedly(Invoke([&mock_context](int r) {
mock_context.do_complete(r);
}));
}
void expect_metadata_set(MockImageCtx& mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.operations, execute_metadata_set(_, _, _))
.WillRepeatedly(Invoke([](std::string key, std::string val, Context* ctx) {
ctx->complete(0);
}));
}
void expect_metadata_remove(MockImageCtx& mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.operations, execute_metadata_remove(_, _))
.WillRepeatedly(Invoke([](std::string key, Context* ctx) {
ctx->complete(0);
}));
}
};
TEST_F(TestMockCacheSSDWriteLog, init_state_write) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockApi mock_api;
MockImageCacheStateSSD image_cache_state(&mock_image_ctx, mock_api);
validate_cache_state(ictx, image_cache_state, false, true, true, "", "", 0);
image_cache_state.empty = false;
image_cache_state.clean = false;
ceph::mutex lock = ceph::make_mutex("MockImageCacheStateSSD lock");
MockContextSSD finish_ctx;
expect_metadata_set(mock_image_ctx);
expect_context_complete(finish_ctx, 0);
std::unique_lock locker(lock);
image_cache_state.write_image_cache_state(locker, &finish_ctx);
ASSERT_FALSE(locker.owns_lock());
ASSERT_EQ(0, finish_ctx.wait());
}
TEST_F(TestMockCacheSSDWriteLog, init_state_json_write) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockApi mock_api;
MockImageCacheStateSSD image_cache_state(&mock_image_ctx, mock_api);
string strf = "{ \"present\": true, \"empty\": false, \"clean\": false, \
\"host\": \"testhost\", \
\"path\": \"/tmp\", \
\"mode\": \"ssd\", \
\"size\": 1024 }";
json_spirit::mValue json_root;
ASSERT_TRUE(json_spirit::read(strf.c_str(), json_root));
ASSERT_TRUE(image_cache_state.init_from_metadata(json_root));
validate_cache_state(ictx, image_cache_state, true, false, false,
"testhost", "/tmp", 1024);
MockContextSSD finish_ctx;
expect_metadata_remove(mock_image_ctx);
expect_context_complete(finish_ctx, 0);
image_cache_state.clear_image_cache_state(&finish_ctx);
ASSERT_EQ(0, finish_ctx.wait());
}
TEST_F(TestMockCacheSSDWriteLog, init_shutdown) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
MockContextSSD finish_ctx1;
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
ssd.shut_down(&finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
}
TEST_F(TestMockCacheSSDWriteLog, write) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
MockContextSSD finish_ctx1;
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, read_hit_ssd_cache) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl),
fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_read;
expect_context_complete(finish_ctx_read, 0);
Extents image_extents_read{{0, 4096}};
bufferlist read_bl;
ssd.read(std::move(image_extents_read), &read_bl,
fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(bl_copy.contents_equal(read_bl));
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, read_hit_part_ssd_cache) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 8192}};
bufferlist bl;
bl.append(std::string(8192, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl),
fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_read;
Extents image_extents_read{{4096, 4096}};
bufferlist hit_bl;
bl_copy.begin(4095).copy(4096, hit_bl);
expect_context_complete(finish_ctx_read, 0);
bufferlist read_bl;
ssd.read(std::move(image_extents_read), &read_bl,
fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
bufferlist read_bl_hit;
read_bl.begin(0).copy(4096, read_bl_hit);
ASSERT_TRUE(hit_bl.contents_equal(read_bl_hit));
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, read_miss_ssd_cache) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl),
fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_read;
Extents image_extents_read{{4096, 4096}};
expect_context_complete(finish_ctx_read, 4096);
bufferlist read_bl;
ASSERT_EQ(0, read_bl.length());
ssd.read(std::move(image_extents_read), &read_bl,
fadvise_flags, &finish_ctx_read);
ASSERT_EQ(4096, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, compare_and_write_compare_matched) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl1;
bl1.append(std::string(4096, '1'));
bufferlist com_bl = bl1;
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl1), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_cw;
bufferlist bl2;
bl2.append(std::string(4096, '2'));
bufferlist bl2_copy = bl2;
uint64_t mismatch_offset = -1;
expect_context_complete(finish_ctx_cw, 0);
ssd.compare_and_write({{0, 4096}}, std::move(com_bl), std::move(bl2),
&mismatch_offset, fadvise_flags, &finish_ctx_cw);
ASSERT_EQ(0, finish_ctx_cw.wait());
ASSERT_EQ(0, mismatch_offset);
MockContextSSD finish_ctx_read;
bufferlist read_bl;
expect_context_complete(finish_ctx_read, 0);
ssd.read({{0, 4096}}, &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(bl2_copy.contents_equal(read_bl));
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, compare_and_write_compare_failed) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl1;
bl1.append(std::string(4096, '1'));
bufferlist bl1_copy = bl1;
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl1), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_cw;
bufferlist bl2;
bl2.append(std::string(4096, '2'));
bufferlist com_bl = bl2;
uint64_t mismatch_offset = -1;
expect_context_complete(finish_ctx_cw, -EILSEQ);
ssd.compare_and_write({{0, 4096}}, std::move(com_bl), std::move(bl2),
&mismatch_offset, fadvise_flags, &finish_ctx_cw);
ASSERT_EQ(-EILSEQ, finish_ctx_cw.wait());
ASSERT_EQ(0, mismatch_offset);
MockContextSSD finish_ctx_read;
bufferlist read_bl;
expect_context_complete(finish_ctx_read, 0);
ssd.read({{0, 4096}}, &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(bl1_copy.contents_equal(read_bl));
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, writesame) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
bufferlist bl, test_bl;
bl.append(std::string(512, '1'));
test_bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
ssd.writesame(0, 4096, std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_read;
bufferlist read_bl;
expect_context_complete(finish_ctx_read, 0);
ssd.read({{0, 4096}}, &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(test_bl.contents_equal(read_bl));
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, discard) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_discard;
expect_context_complete(finish_ctx_discard, 0);
ssd.discard(0, 4096, 1, &finish_ctx_discard);
ASSERT_EQ(0, finish_ctx_discard.wait());
MockContextSSD finish_ctx_read;
bufferlist read_bl;
expect_context_complete(finish_ctx_read, 0);
ssd.read({{0, 4096}}, &read_bl, fadvise_flags, &finish_ctx_read);
ASSERT_EQ(0, finish_ctx_read.wait());
ASSERT_EQ(4096, read_bl.length());
ASSERT_TRUE(read_bl.is_zero());
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, invalidate) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_invalidate;
expect_context_complete(finish_ctx_invalidate, 0);
ssd.invalidate(&finish_ctx_invalidate);
ASSERT_EQ(0, finish_ctx_invalidate.wait());
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, flush) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
bufferlist bl_copy = bl;
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_flush;
expect_context_complete(finish_ctx_flush, 0);
ssd.flush(&finish_ctx_flush);
ASSERT_EQ(0, finish_ctx_flush.wait());
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, flush_source_shutdown) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_flush;
expect_context_complete(finish_ctx_flush, 0);
ssd.flush(io::FLUSH_SOURCE_SHUTDOWN, &finish_ctx_flush);
ASSERT_EQ(0, finish_ctx_flush.wait());
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, flush_source_internal) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
MockContextSSD finish_ctx_flush;
expect_context_complete(finish_ctx_flush, 0);
ssd.flush(io::FLUSH_SOURCE_INTERNAL, &finish_ctx_flush);
ASSERT_EQ(0, finish_ctx_flush.wait());
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
ssd.shut_down(&finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
}
TEST_F(TestMockCacheSSDWriteLog, flush_source_user) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockImageCtx mock_image_ctx(*ictx);
MockImageWriteback mock_image_writeback(mock_image_ctx);
MockApi mock_api;
MockSSDWriteLog ssd(
mock_image_ctx, get_cache_state(mock_image_ctx, mock_api),
mock_image_writeback, mock_api);
expect_op_work_queue(mock_image_ctx);
expect_metadata_set(mock_image_ctx);
MockContextSSD finish_ctx1;
expect_context_complete(finish_ctx1, 0);
ssd.init(&finish_ctx1);
ASSERT_EQ(0, finish_ctx1.wait());
MockContextSSD finish_ctx2;
expect_context_complete(finish_ctx2, 0);
Extents image_extents{{0, 4096}};
bufferlist bl;
bl.append(std::string(4096, '1'));
int fadvise_flags = 0;
ssd.write(std::move(image_extents), std::move(bl), fadvise_flags, &finish_ctx2);
ASSERT_EQ(0, finish_ctx2.wait());
usleep(10000);
MockContextSSD finish_ctx_flush;
expect_context_complete(finish_ctx_flush, 0);
ssd.flush(io::FLUSH_SOURCE_USER, &finish_ctx_flush);
ASSERT_EQ(0, finish_ctx_flush.wait());
MockContextSSD finish_ctx3;
expect_context_complete(finish_ctx3, 0);
Extents image_extents2{{0, 4096}};
bufferlist bl2;
bl2.append(std::string(4096, '1'));
int fadvise_flags2 = 0;
ssd.write(std::move(image_extents2), std::move(bl2), fadvise_flags2, &finish_ctx3);
ASSERT_EQ(0, finish_ctx3.wait());
MockContextSSD finish_ctx4;
expect_context_complete(finish_ctx4, 0);
ssd.shut_down(&finish_ctx4);
ASSERT_EQ(0, finish_ctx4.wait());
}
} // namespace pwl
} // namespace cache
} // namespace librbd
| 24,435 | 31.068241 | 91 |
cc
|
null |
ceph-main/src/test/librbd/crypto/test_mock_BlockCrypto.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "librbd/crypto/BlockCrypto.h"
#include "test/librbd/mock/crypto/MockDataCryptor.h"
#include "librbd/crypto/BlockCrypto.cc"
template class librbd::crypto::BlockCrypto<
librbd::crypto::MockCryptoContext>;
using ::testing::ExpectationSet;
using ::testing::internal::ExpectationBase;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArg;
using ::testing::_;
namespace librbd {
namespace crypto {
MATCHER_P(CompareArrayToString, s, "") {
return (memcmp(arg, s.c_str(), s.length()) == 0);
}
struct TestMockCryptoBlockCrypto : public TestFixture {
MockDataCryptor* cryptor;
BlockCrypto<MockCryptoContext>* bc;
int cryptor_block_size = 16;
int cryptor_iv_size = 16;
int block_size = 4096;
int data_offset = 0;
ExpectationSet* expectation_set;
void SetUp() override {
TestFixture::SetUp();
cryptor = new MockDataCryptor();
cryptor->block_size = cryptor_block_size;
bc = new BlockCrypto<MockCryptoContext>(
reinterpret_cast<CephContext*>(m_ioctx.cct()), cryptor,
block_size, data_offset);
expectation_set = new ExpectationSet();
}
void TearDown() override {
delete expectation_set;
delete bc;
TestFixture::TearDown();
}
void expect_get_context(CipherMode mode) {
_set_last_expectation(
EXPECT_CALL(*cryptor, get_context(mode))
.After(*expectation_set).WillOnce(Return(
new MockCryptoContext())));
}
void expect_return_context(CipherMode mode) {
_set_last_expectation(
EXPECT_CALL(*cryptor, return_context(_, mode))
.After(*expectation_set).WillOnce(WithArg<0>(
Invoke([](MockCryptoContext* ctx) {
delete ctx;
}))));
}
void expect_init_context(const std::string& iv) {
_set_last_expectation(
EXPECT_CALL(*cryptor, init_context(_, CompareArrayToString(iv),
cryptor_iv_size))
.After(*expectation_set));
}
void expect_update_context(const std::string& in_str, int out_ret) {
_set_last_expectation(
EXPECT_CALL(*cryptor, update_context(_,
CompareArrayToString(in_str),
_, in_str.length()))
.After(*expectation_set).WillOnce(Return(out_ret)));
}
void _set_last_expectation(ExpectationBase& expectation) {
delete expectation_set;
expectation_set = new ExpectationSet(expectation);
}
};
TEST_F(TestMockCryptoBlockCrypto, Encrypt) {
uint32_t image_offset = 0x1230 * 512;
ceph::bufferlist data1;
data1.append(std::string(2048, '1'));
ceph::bufferlist data2;
data2.append(std::string(4096, '2'));
ceph::bufferlist data3;
data3.append(std::string(2048, '3'));
ceph::bufferlist data;
data.claim_append(data1);
data.claim_append(data2);
data.claim_append(data3);
expect_get_context(CipherMode::CIPHER_MODE_ENC);
expect_init_context(std::string("\x30\x12\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16));
expect_update_context(std::string(2048, '1') + std::string(2048, '2'), 4096);
expect_init_context(std::string("\x38\x12\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16));
expect_update_context(std::string(2048, '2') + std::string(2048, '3'), 4096);
expect_return_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_EQ(0, bc->encrypt(&data, image_offset));
ASSERT_EQ(data.length(), 8192);
}
TEST_F(TestMockCryptoBlockCrypto, UnalignedImageOffset) {
ceph::bufferlist data;
data.append(std::string(4096, '1'));
ASSERT_EQ(-EINVAL, bc->encrypt(&data, 2));
}
TEST_F(TestMockCryptoBlockCrypto, UnalignedDataLength) {
ceph::bufferlist data;
data.append(std::string(512, '1'));
ASSERT_EQ(-EINVAL, bc->encrypt(&data, 0));
}
TEST_F(TestMockCryptoBlockCrypto, GetContextError) {
ceph::bufferlist data;
data.append(std::string(4096, '1'));
EXPECT_CALL(*cryptor, get_context(CipherMode::CIPHER_MODE_ENC)).WillOnce(
Return(nullptr));
ASSERT_EQ(-EIO, bc->encrypt(&data, 0));
}
TEST_F(TestMockCryptoBlockCrypto, InitContextError) {
ceph::bufferlist data;
data.append(std::string(4096, '1'));
expect_get_context(CipherMode::CIPHER_MODE_ENC);
EXPECT_CALL(*cryptor, init_context(_, _, _)).WillOnce(Return(-123));
expect_return_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_EQ(-123, bc->encrypt(&data, 0));
}
TEST_F(TestMockCryptoBlockCrypto, UpdateContextError) {
ceph::bufferlist data;
data.append(std::string(4096, '1'));
expect_get_context(CipherMode::CIPHER_MODE_ENC);
EXPECT_CALL(*cryptor, init_context(_, _, _));
EXPECT_CALL(*cryptor, update_context(_, _, _, _)).WillOnce(Return(-123));
expect_return_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_EQ(-123, bc->encrypt(&data, 0));
}
} // namespace crypto
} // namespace librbd
| 5,097 | 31.471338 | 79 |
cc
|
null |
ceph-main/src/test/librbd/crypto/test_mock_CryptoContextPool.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "gtest/gtest.h"
#include "librbd/crypto/CryptoContextPool.h"
#include "test/librbd/mock/crypto/MockDataCryptor.h"
#include "librbd/crypto/CryptoContextPool.cc"
template class librbd::crypto::CryptoContextPool<
librbd::crypto::MockCryptoContext>;
using ::testing::Return;
namespace librbd {
namespace crypto {
struct TestMockCryptoCryptoContextPool : public ::testing::Test {
MockDataCryptor cryptor;
void expect_get_context(CipherMode mode) {
EXPECT_CALL(cryptor, get_context(mode)).WillOnce(Return(
new MockCryptoContext()));
}
void expect_return_context(MockCryptoContext* ctx, CipherMode mode) {
delete ctx;
EXPECT_CALL(cryptor, return_context(ctx, mode));
}
};
TEST_F(TestMockCryptoCryptoContextPool, Test) {
CryptoContextPool<MockCryptoContext> pool(&cryptor, 1);
expect_get_context(CipherMode::CIPHER_MODE_ENC);
auto enc_ctx = pool.get_context(CipherMode::CIPHER_MODE_ENC);
expect_get_context(CipherMode::CIPHER_MODE_DEC);
auto dec_ctx1 = pool.get_context(CipherMode::CIPHER_MODE_DEC);
expect_get_context(CipherMode::CIPHER_MODE_DEC);
auto dec_ctx2 = pool.get_context(CipherMode::CIPHER_MODE_DEC);
pool.return_context(dec_ctx1, CipherMode::CIPHER_MODE_DEC);
expect_return_context(dec_ctx2, CipherMode::CIPHER_MODE_DEC);
pool.return_context(dec_ctx2, CipherMode::CIPHER_MODE_DEC);
pool.return_context(enc_ctx, CipherMode::CIPHER_MODE_ENC);
ASSERT_EQ(enc_ctx, pool.get_context(CipherMode::CIPHER_MODE_ENC));
pool.return_context(enc_ctx, CipherMode::CIPHER_MODE_ENC);
expect_return_context(enc_ctx, CipherMode::CIPHER_MODE_ENC);
expect_return_context(dec_ctx1, CipherMode::CIPHER_MODE_DEC);
}
} // namespace crypto
} // namespace librbd
| 1,850 | 32.654545 | 73 |
cc
|
null |
ceph-main/src/test/librbd/crypto/test_mock_CryptoObjectDispatch.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockObjectMap.h"
#include "test/librbd/mock/crypto/MockCryptoInterface.h"
#include "librbd/crypto/CryptoObjectDispatch.h"
#include "librbd/io/ObjectDispatchSpec.h"
#include "librbd/io/Utils.h"
#include "librbd/io/Utils.cc"
template bool librbd::io::util::trigger_copyup(
MockImageCtx *image_ctx, uint64_t object_no, IOContext io_context,
Context* on_finish);
template class librbd::io::ObjectWriteRequest<librbd::MockImageCtx>;
template class librbd::io::AbstractObjectWriteRequest<librbd::MockImageCtx>;
#include "librbd/io/ObjectRequest.cc"
namespace librbd {
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
namespace io {
template <>
struct CopyupRequest<librbd::MockImageCtx> {
MOCK_METHOD0(send, void());
MOCK_METHOD2(append_request, void(
AbstractObjectWriteRequest<librbd::MockImageCtx>*,
const Extents&));
static CopyupRequest* s_instance;
static CopyupRequest* create(librbd::MockImageCtx* ictx, uint64_t objectno,
Extents&& image_extents, ImageArea area,
const ZTracer::Trace& parent_trace) {
return s_instance;
}
CopyupRequest() {
s_instance = this;
}
};
CopyupRequest<librbd::MockImageCtx>* CopyupRequest<
librbd::MockImageCtx>::s_instance = nullptr;
namespace util {
namespace {
struct Mock {
static Mock* s_instance;
Mock() {
s_instance = this;
}
MOCK_METHOD6(read_parent,
void(MockImageCtx*, uint64_t, io::ReadExtents*,
librados::snap_t, const ZTracer::Trace &, Context*));
};
Mock *Mock::s_instance = nullptr;
} // anonymous namespace
template <> void read_parent(
MockImageCtx *image_ctx, uint64_t object_no,
io::ReadExtents* extents, librados::snap_t snap_id,
const ZTracer::Trace &trace, Context* on_finish) {
Mock::s_instance->read_parent(image_ctx, object_no, extents, snap_id, trace,
on_finish);
}
} // namespace util
} // namespace io
} // namespace librbd
#include "librbd/crypto/CryptoObjectDispatch.cc"
namespace librbd {
namespace crypto {
template <>
uint64_t get_file_offset(MockImageCtx *image_ctx, uint64_t object_no,
uint64_t offset) {
return Striper::get_file_offset(image_ctx->cct, &image_ctx->layout,
object_no, offset);
}
using ::testing::_;
using ::testing::ElementsAre;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Pair;
using ::testing::Return;
using ::testing::WithArg;
struct TestMockCryptoCryptoObjectDispatch : public TestMockFixture {
typedef CryptoObjectDispatch<librbd::MockImageCtx> MockCryptoObjectDispatch;
typedef io::AbstractObjectWriteRequest<librbd::MockImageCtx>
MockAbstractObjectWriteRequest;
typedef io::CopyupRequest<librbd::MockImageCtx> MockCopyupRequest;
typedef io::util::Mock MockUtils;
MockCryptoInterface crypto;
MockImageCtx* mock_image_ctx;
MockCryptoObjectDispatch* mock_crypto_object_dispatch;
C_SaferCond finished_cond;
Context *on_finish = &finished_cond;
C_SaferCond dispatched_cond;
Context *on_dispatched = &dispatched_cond;
Context *dispatcher_ctx;
ceph::bufferlist data;
io::DispatchResult dispatch_result;
io::Extents extent_map;
int object_dispatch_flags = 0;
MockUtils mock_utils;
MockCopyupRequest copyup_request;
void SetUp() override {
TestMockFixture::SetUp();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
mock_image_ctx = new MockImageCtx(*ictx);
mock_crypto_object_dispatch = new MockCryptoObjectDispatch(
mock_image_ctx, &crypto);
data.append(std::string(4096, '1'));
}
void TearDown() override {
C_SaferCond cond;
Context *on_finish = &cond;
mock_crypto_object_dispatch->shut_down(on_finish);
ASSERT_EQ(0, cond.wait());
delete mock_crypto_object_dispatch;
delete mock_image_ctx;
TestMockFixture::TearDown();
}
void expect_object_read(io::ReadExtents* extents, uint64_t version = 0) {
EXPECT_CALL(*mock_image_ctx->io_object_dispatcher, send(_))
.WillOnce(Invoke([this, extents,
version](io::ObjectDispatchSpec* spec) {
auto* read = boost::get<io::ObjectDispatchSpec::ReadRequest>(
&spec->request);
ASSERT_TRUE(read != nullptr);
ASSERT_EQ(extents->size(), read->extents->size());
for (uint64_t i = 0; i < extents->size(); ++i) {
ASSERT_EQ((*extents)[i].offset, (*read->extents)[i].offset);
ASSERT_EQ((*extents)[i].length, (*read->extents)[i].length);
(*read->extents)[i].bl = (*extents)[i].bl;
(*read->extents)[i].extent_map = (*extents)[i].extent_map;
}
if (read->version != nullptr) {
*(read->version) = version;
}
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
dispatcher_ctx = &spec->dispatcher_ctx;
}));
}
void expect_read_parent(MockUtils &mock_utils, uint64_t object_no,
io::ReadExtents* extents, librados::snap_t snap_id,
int r) {
EXPECT_CALL(mock_utils,
read_parent(_, object_no, extents, snap_id, _, _))
.WillOnce(WithArg<5>(CompleteContext(
r, static_cast<asio::ContextWQ*>(nullptr))));
}
void expect_object_write(uint64_t object_off, const std::string& data,
int write_flags,
std::optional<uint64_t> assert_version) {
EXPECT_CALL(*mock_image_ctx->io_object_dispatcher, send(_))
.WillOnce(Invoke([this, object_off, data, write_flags,
assert_version](io::ObjectDispatchSpec* spec) {
auto* write = boost::get<io::ObjectDispatchSpec::WriteRequest>(
&spec->request);
ASSERT_TRUE(write != nullptr);
ASSERT_EQ(object_off, write->object_off);
ASSERT_TRUE(data == write->data.to_str());
ASSERT_EQ(write_flags, write->write_flags);
ASSERT_EQ(assert_version, write->assert_version);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
dispatcher_ctx = &spec->dispatcher_ctx;
}));
}
void expect_object_write_same() {
EXPECT_CALL(*mock_image_ctx->io_object_dispatcher, send(_))
.WillOnce(Invoke([this](io::ObjectDispatchSpec* spec) {
auto* write_same = boost::get<
io::ObjectDispatchSpec::WriteSameRequest>(
&spec->request);
ASSERT_TRUE(write_same != nullptr);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
dispatcher_ctx = &spec->dispatcher_ctx;
}));
}
void expect_get_object_size() {
EXPECT_CALL(*mock_image_ctx, get_object_size()).WillOnce(Return(
mock_image_ctx->layout.object_size));
}
void expect_remap_to_logical(uint64_t offset, uint64_t length) {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, remap_to_logical(
ElementsAre(Pair(offset, length))));
}
void expect_get_parent_overlap(uint64_t overlap) {
EXPECT_CALL(*mock_image_ctx, get_parent_overlap(_, _))
.WillOnce(WithArg<1>(Invoke([overlap](uint64_t *o) {
*o = overlap;
return 0;
})));
}
void expect_prune_parent_extents(uint64_t object_overlap) {
EXPECT_CALL(*mock_image_ctx, prune_parent_extents(_, _, _, _))
.WillOnce(Return(object_overlap));
}
void expect_copyup(MockAbstractObjectWriteRequest** write_request, int r) {
EXPECT_CALL(copyup_request, append_request(_, _))
.WillOnce(WithArg<0>(
Invoke([write_request](
MockAbstractObjectWriteRequest *req) {
*write_request = req;
})));
EXPECT_CALL(copyup_request, send())
.WillOnce(Invoke([write_request, r]() {
(*write_request)->handle_copyup(r);
}));
}
void expect_encrypt(int count = 1) {
EXPECT_CALL(crypto, encrypt(_, _)).Times(count);
}
void expect_decrypt(int count = 1) {
EXPECT_CALL(crypto, decrypt(_, _)).Times(count);
}
};
TEST_F(TestMockCryptoCryptoObjectDispatch, Flush) {
ASSERT_FALSE(mock_crypto_object_dispatch->flush(
io::FLUSH_SOURCE_USER, {}, nullptr, nullptr, &on_finish, nullptr));
ASSERT_EQ(on_finish, &finished_cond); // not modified
on_finish->complete(0);
ASSERT_EQ(0, finished_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, Discard) {
expect_object_write_same();
ASSERT_TRUE(mock_crypto_object_dispatch->discard(
11, 0, 4096, mock_image_ctx->get_data_io_context(), 0, {},
&object_dispatch_flags, nullptr, &dispatch_result, &on_finish,
on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0);
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, AlignedReadFail) {
io::ReadExtents extents = {{0, 4096}};
expect_object_read(&extents);
ASSERT_TRUE(mock_crypto_object_dispatch->read(
11, &extents, mock_image_ctx->get_data_io_context(), 0, 0, {},
nullptr, &object_dispatch_flags, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(-EIO);
ASSERT_EQ(-EIO, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, AlignedRead) {
io::ReadExtents extents = {{0, 16384}, {32768, 4096}};
extents[0].bl.append(std::string(1024, '1') + std::string(1024, '2') +
std::string(1024, '3') + std::string(1024, '4'));
extents[0].extent_map = {{1024, 1024}, {3072, 2048}, {16384 - 1024, 1024}};
extents[1].bl.append(std::string(4096, '0'));
expect_object_read(&extents);
ASSERT_TRUE(mock_crypto_object_dispatch->read(
11, &extents, mock_image_ctx->get_data_io_context(), 0, 0, {},
nullptr, &object_dispatch_flags, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
expect_decrypt(3);
dispatcher_ctx->complete(0);
ASSERT_EQ(16384 + 4096, dispatched_cond.wait());
auto expected_bl_data = (
std::string(1024, '\0') + std::string(1024, '1') +
std::string(1024, '\0') + std::string(1024, '2') +
std::string(1024, '3') + std::string(3072, '\0') +
std::string(3072, '\0') + std::string(1024, '4'));
ASSERT_TRUE(extents[0].bl.to_str() == expected_bl_data);
ASSERT_THAT(extents[0].extent_map,
ElementsAre(Pair(0, 8192), Pair(16384 - 4096, 4096)));
}
TEST_F(TestMockCryptoCryptoObjectDispatch, ReadFromParent) {
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
expect_object_read(&extents);
expect_read_parent(mock_utils, 11, &extents, CEPH_NOSNAP, 8192);
ASSERT_TRUE(mock_crypto_object_dispatch->read(
11, &extents, mock_image_ctx->get_data_io_context(), 0, 0, {},
nullptr, &object_dispatch_flags, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
// no decrypt
dispatcher_ctx->complete(-ENOENT);
ASSERT_EQ(8192, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, ReadFromParentDisabled) {
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
expect_object_read(&extents);
ASSERT_TRUE(mock_crypto_object_dispatch->read(
11, &extents, mock_image_ctx->get_data_io_context(), 0,
io::READ_FLAG_DISABLE_READ_FROM_PARENT, {},
nullptr, &object_dispatch_flags, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
// no decrypt
dispatcher_ctx->complete(-ENOENT);
ASSERT_EQ(-ENOENT, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedRead) {
io::ReadExtents extents = {{0, 1}, {8191, 1}, {8193, 1},
{16384 + 1, 4096 * 5 - 2}};
io::ReadExtents aligned_extents = {{0, 4096}, {4096, 4096}, {8192, 4096},
{16384, 4096 * 5}};
aligned_extents[0].bl.append(std::string("1") + std::string(4096, '0'));
aligned_extents[1].bl.append(std::string(4095, '0') + std::string("2"));
aligned_extents[2].bl.append(std::string("03") + std::string(4094, '0'));
aligned_extents[3].bl.append(std::string("0") + std::string(4095, '4') +
std::string(4096, '5') +
std::string(4095, '6') + std::string("0"));
aligned_extents[3].extent_map = {{16384, 4096}, {16384 + 2 * 4096, 4096},
{16384 + 4 * 4096, 4096}};
expect_object_read(&aligned_extents);
ASSERT_TRUE(mock_crypto_object_dispatch->read(
11, &extents, mock_image_ctx->get_data_io_context(), 0, 0, {},
nullptr, &object_dispatch_flags, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(4096*8);
ASSERT_EQ(3 + 4096 * 5 - 2, dispatched_cond.wait());
ASSERT_TRUE(extents[0].bl.to_str() == std::string("1"));
ASSERT_TRUE(extents[1].bl.to_str() == std::string("2"));
ASSERT_TRUE(extents[2].bl.to_str() == std::string("3"));
auto expected_bl_data = (std::string(4095, '4') + std::string(4096, '5') +
std::string(4095, '6'));
ASSERT_TRUE(extents[3].bl.to_str() == expected_bl_data);
ASSERT_THAT(extents[3].extent_map,
ElementsAre(Pair(16384 + 1, 4095), Pair(16384 + 2 * 4096, 4096),
Pair(16384 + 4 * 4096, 4095)));
}
TEST_F(TestMockCryptoCryptoObjectDispatch, AlignedWrite) {
expect_encrypt();
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 0, std::move(data), mock_image_ctx->get_data_io_context(), 0, 0,
std::nullopt, {}, nullptr, nullptr, &dispatch_result, &on_finish,
on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_CONTINUE);
ASSERT_EQ(on_finish, &finished_cond); // not modified
on_finish->complete(0);
ASSERT_EQ(0, finished_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedWrite) {
ceph::bufferlist write_data;
uint64_t version = 1234;
write_data.append(std::string(8192, '1'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
expect_object_read(&extents, version);
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 1, std::move(write_data), mock_image_ctx->get_data_io_context(),
0, 0, std::nullopt, {}, nullptr, nullptr, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
auto expected_data =
std::string("2") + std::string(8192, '1') + std::string(4095, '3');
expect_object_write(0, expected_data, 0, std::make_optional(version));
dispatcher_ctx->complete(8192); // complete read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0); // complete write
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedWriteWithNoObject) {
ceph::bufferlist write_data;
write_data.append(std::string(8192, '1'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
expect_object_read(&extents);
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 1, std::move(write_data), mock_image_ctx->get_data_io_context(),
0, 0, std::nullopt, {}, nullptr, nullptr, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
expect_get_object_size();
expect_get_parent_overlap(0);
auto expected_data = (std::string(1, '\0') + std::string(8192, '1') +
std::string(4095, '\0'));
expect_object_write(0, expected_data, io::OBJECT_WRITE_FLAG_CREATE_EXCLUSIVE,
std::nullopt);
dispatcher_ctx->complete(-ENOENT); // complete read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0); // complete write
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedWriteFailCreate) {
ceph::bufferlist write_data;
write_data.append(std::string(8192, '1'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
expect_object_read(&extents);
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 1, std::move(write_data), mock_image_ctx->get_data_io_context(),
0, 0, std::nullopt, {}, nullptr, nullptr, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
expect_get_object_size();
expect_get_parent_overlap(0);
auto expected_data = (std::string(1, '\0') + std::string(8192, '1') +
std::string(4095, '\0'));
expect_object_write(0, expected_data, io::OBJECT_WRITE_FLAG_CREATE_EXCLUSIVE,
std::nullopt);
dispatcher_ctx->complete(-ENOENT); // complete read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
uint64_t version = 1234;
expect_object_read(&extents, version);
dispatcher_ctx->complete(-EEXIST); // complete write, request will restart
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
auto expected_data2 =
std::string("2") + std::string(8192, '1') + std::string(4095, '3');
expect_object_write(0, expected_data2, 0, std::make_optional(version));
dispatcher_ctx->complete(8192); // complete read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0); // complete write
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedWriteCopyup) {
MockObjectMap mock_object_map;
mock_image_ctx->object_map = &mock_object_map;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx->exclusive_lock = &mock_exclusive_lock;
ceph::bufferlist write_data;
write_data.append(std::string(8192, '1'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
expect_object_read(&extents);
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 1, std::move(write_data), mock_image_ctx->get_data_io_context(),
0, 0, std::nullopt, {}, nullptr, nullptr, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
expect_get_object_size();
expect_get_parent_overlap(100 << 20);
expect_remap_to_logical(11 * mock_image_ctx->layout.object_size,
mock_image_ctx->layout.object_size);
expect_prune_parent_extents(mock_image_ctx->layout.object_size);
EXPECT_CALL(mock_exclusive_lock, is_lock_owner()).WillRepeatedly(
Return(true));
EXPECT_CALL(*mock_image_ctx->object_map, object_may_exist(11)).WillOnce(
Return(false));
MockAbstractObjectWriteRequest *write_request = nullptr;
expect_copyup(&write_request, 0);
// unaligned write restarted
uint64_t version = 1234;
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
expect_object_read(&extents, version);
dispatcher_ctx->complete(-ENOENT); // complete first read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
auto expected_data =
std::string("2") + std::string(8192, '1') + std::string(4095, '3');
expect_object_write(0, expected_data, 0, std::make_optional(version));
dispatcher_ctx->complete(8192); // complete second read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0); // complete write
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedWriteEmptyCopyup) {
MockObjectMap mock_object_map;
mock_image_ctx->object_map = &mock_object_map;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx->exclusive_lock = &mock_exclusive_lock;
ceph::bufferlist write_data;
write_data.append(std::string(8192, '1'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
expect_object_read(&extents);
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 1, std::move(write_data), mock_image_ctx->get_data_io_context(),
0, 0, std::nullopt, {}, nullptr, nullptr, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
expect_get_object_size();
expect_get_parent_overlap(100 << 20);
expect_remap_to_logical(11 * mock_image_ctx->layout.object_size,
mock_image_ctx->layout.object_size);
expect_prune_parent_extents(mock_image_ctx->layout.object_size);
EXPECT_CALL(mock_exclusive_lock, is_lock_owner()).WillRepeatedly(
Return(true));
EXPECT_CALL(*mock_image_ctx->object_map, object_may_exist(11)).WillOnce(
Return(false));
MockAbstractObjectWriteRequest *write_request = nullptr;
expect_copyup(&write_request, 0);
// unaligned write restarted
expect_object_read(&extents);
dispatcher_ctx->complete(-ENOENT); // complete first read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
auto expected_data =
std::string(1, '\0') + std::string(8192, '1') +
std::string(4095, '\0');
expect_object_write(0, expected_data, io::OBJECT_WRITE_FLAG_CREATE_EXCLUSIVE,
std::nullopt);
dispatcher_ctx->complete(-ENOENT); // complete second read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0); // complete write
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedWriteFailVersionCheck) {
ceph::bufferlist write_data;
uint64_t version = 1234;
write_data.append(std::string(8192, '1'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
expect_object_read(&extents, version);
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 1, std::move(write_data), mock_image_ctx->get_data_io_context(),
0, 0, std::nullopt, {}, nullptr, nullptr, &dispatch_result,
&on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
auto expected_data =
std::string("2") + std::string(8192, '1') + std::string(4095, '3');
expect_object_write(0, expected_data, 0, std::make_optional(version));
dispatcher_ctx->complete(8192); // complete read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
version = 1235;
expect_object_read(&extents, version);
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
dispatcher_ctx->complete(-ERANGE); // complete write, request will restart
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
expect_object_write(0, expected_data, 0, std::make_optional(version));
dispatcher_ctx->complete(8192); // complete read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0); // complete write
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedWriteWithAssertVersion) {
ceph::bufferlist write_data;
uint64_t version = 1234;
uint64_t assert_version = 1233;
write_data.append(std::string(8192, '1'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
expect_object_read(&extents, version);
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 1, std::move(write_data), mock_image_ctx->get_data_io_context(),
0, 0, std::make_optional(assert_version), {}, nullptr, nullptr,
&dispatch_result, &on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
dispatcher_ctx->complete(8192); // complete read
ASSERT_EQ(-ERANGE, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, UnalignedWriteWithExclusiveCreate) {
ceph::bufferlist write_data;
write_data.append(std::string(8192, '1'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
expect_object_read(&extents);
ASSERT_TRUE(mock_crypto_object_dispatch->write(
11, 1, std::move(write_data), mock_image_ctx->get_data_io_context(),
0, io::OBJECT_WRITE_FLAG_CREATE_EXCLUSIVE, std::nullopt, {}, nullptr,
nullptr, &dispatch_result, &on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
dispatcher_ctx->complete(8192); // complete read
ASSERT_EQ(-EEXIST, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, CompareAndWrite) {
ceph::bufferlist write_data;
uint64_t version = 1234;
write_data.append(std::string(8192, '1'));
ceph::bufferlist cmp_data;
cmp_data.append(std::string(4096, '2'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}, {0, 8192}};
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
extents[2].bl.append(std::string(8192, '2'));
expect_object_read(&extents, version);
ASSERT_TRUE(mock_crypto_object_dispatch->compare_and_write(
11, 1, std::move(cmp_data), std::move(write_data),
mock_image_ctx->get_data_io_context(), 0, {}, nullptr, nullptr,
nullptr, &dispatch_result, &on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
auto expected_data =
std::string("2") + std::string(8192, '1') + std::string(4095, '3');
expect_object_write(0, expected_data, 0, std::make_optional(version));
dispatcher_ctx->complete(4096*4); // complete read
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0); // complete write
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, CompareAndWriteFail) {
ceph::bufferlist write_data;
uint64_t version = 1234;
write_data.append(std::string(8192, '1'));
ceph::bufferlist cmp_data;
cmp_data.append(std::string(4094, '2') + std::string(2, '4'));
io::ReadExtents extents = {{0, 4096}, {8192, 4096}, {0, 8192}};
extents[0].bl.append(std::string(4096, '2'));
extents[1].bl.append(std::string(4096, '3'));
extents[2].bl.append(std::string(8192, '2'));
expect_object_read(&extents, version);
uint64_t mismatch_offset;
ASSERT_TRUE(mock_crypto_object_dispatch->compare_and_write(
11, 1, std::move(cmp_data), std::move(write_data),
mock_image_ctx->get_data_io_context(), 0, {}, &mismatch_offset,
nullptr, nullptr, &dispatch_result, &on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(on_finish, &finished_cond);
dispatcher_ctx->complete(4096*4); // complete read
ASSERT_EQ(-EILSEQ, dispatched_cond.wait());
ASSERT_EQ(mismatch_offset, 4094);
}
TEST_F(TestMockCryptoCryptoObjectDispatch, WriteSame) {
io::LightweightBufferExtents buffer_extents;
ceph::bufferlist write_data;
write_data.append(std::string("12"));
expect_object_write(0, std::string("12121") , 0, std::nullopt);
ASSERT_TRUE(mock_crypto_object_dispatch->write_same(
11, 0, 5, {{0, 5}}, std::move(write_data),
mock_image_ctx->get_data_io_context(), 0, {}, nullptr, nullptr,
&dispatch_result, &on_finish, on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_EQ(ETIMEDOUT, dispatched_cond.wait_for(0));
dispatcher_ctx->complete(0);
ASSERT_EQ(0, dispatched_cond.wait());
}
TEST_F(TestMockCryptoCryptoObjectDispatch, PrepareCopyup) {
char* data = (char*)"0123456789";
io::SnapshotSparseBufferlist snapshot_sparse_bufferlist;
auto& snap1 = snapshot_sparse_bufferlist[0];
auto& snap2 = snapshot_sparse_bufferlist[1];
snap1.insert(0, 1, {io::SPARSE_EXTENT_STATE_DATA, 1,
ceph::bufferlist::static_from_mem(data + 1, 1)});
snap1.insert(8191, 1, {io::SPARSE_EXTENT_STATE_DATA, 1,
ceph::bufferlist::static_from_mem(data + 2, 1)});
snap1.insert(8193, 3, {io::SPARSE_EXTENT_STATE_DATA, 3,
ceph::bufferlist::static_from_mem(data + 3, 3)});
snap2.insert(0, 2, {io::SPARSE_EXTENT_STATE_ZEROED, 2});
snap2.insert(8191, 3, {io::SPARSE_EXTENT_STATE_DATA, 3,
ceph::bufferlist::static_from_mem(data + 6, 3)});
snap2.insert(16384, 1, {io::SPARSE_EXTENT_STATE_DATA, 1,
ceph::bufferlist::static_from_mem(data + 9, 1)});
expect_get_object_size();
expect_encrypt(6);
InSequence seq;
uint64_t base = 11 * mock_image_ctx->layout.object_size;
expect_remap_to_logical(base, 4096);
expect_remap_to_logical(base + 4096, 4096);
expect_remap_to_logical(base + 8192, 4096);
expect_remap_to_logical(base, 4096);
expect_remap_to_logical(base + 4096, 8192);
expect_remap_to_logical(base + 16384, 4096);
ASSERT_EQ(0, mock_crypto_object_dispatch->prepare_copyup(
11, &snapshot_sparse_bufferlist));
ASSERT_EQ(2, snapshot_sparse_bufferlist.size());
auto& snap1_result = snapshot_sparse_bufferlist[0];
auto& snap2_result = snapshot_sparse_bufferlist[1];
auto it = snap1_result.begin();
ASSERT_NE(it, snap1_result.end());
ASSERT_EQ(0, it.get_off());
ASSERT_EQ(4096 * 3, it.get_len());
ASSERT_TRUE(it.get_val().bl.to_str() ==
std::string("1") + std::string(4095, '\0') +
std::string(4095, '\0') + std::string("2") +
std::string(1, '\0') + std::string("345") + std::string(4092, '\0'));
ASSERT_EQ(++it, snap1_result.end());
it = snap2_result.begin();
ASSERT_NE(it, snap2_result.end());
ASSERT_EQ(0, it.get_off());
ASSERT_EQ(4096 * 3, it.get_len());
ASSERT_TRUE(it.get_val().bl.to_str() ==
std::string(4096, '\0') +
std::string(4095, '\0') + std::string("6") +
std::string("7845") + std::string(4092, '\0'));
ASSERT_NE(++it, snap2_result.end());
ASSERT_EQ(16384, it.get_off());
ASSERT_EQ(4096, it.get_len());
ASSERT_TRUE(it.get_val().bl.to_str() ==
std::string("9") + std::string(4095, '\0'));
ASSERT_EQ(++it, snap2_result.end());
}
} // namespace crypto
} // namespace librbd
| 31,839 | 38.750312 | 79 |
cc
|
null |
ceph-main/src/test/librbd/crypto/test_mock_FormatRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/crypto/MockCryptoInterface.h"
#include "test/librbd/mock/crypto/MockEncryptionFormat.h"
#include "librbd/crypto/Utils.h"
namespace librbd {
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
#include "librbd/crypto/FormatRequest.cc"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace crypto {
namespace {
} // anonymous namespace
namespace util {
template <>
void set_crypto(MockTestImageCtx *image_ctx,
std::unique_ptr<MockEncryptionFormat> encryption_format) {
image_ctx->encryption_format = std::move(encryption_format);
}
} // namespace util
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArg;
template <>
struct ShutDownCryptoRequest<MockTestImageCtx> {
Context *on_finish = nullptr;
static ShutDownCryptoRequest *s_instance;
static ShutDownCryptoRequest *create(MockTestImageCtx *image_ctx,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
ShutDownCryptoRequest() {
s_instance = this;
}
};
ShutDownCryptoRequest<MockTestImageCtx> *ShutDownCryptoRequest<
MockTestImageCtx>::s_instance = nullptr;
struct TestMockCryptoFormatRequest : public TestMockFixture {
typedef FormatRequest<librbd::MockTestImageCtx> MockFormatRequest;
typedef ShutDownCryptoRequest<MockTestImageCtx> MockShutDownCryptoRequest;
MockTestImageCtx* mock_image_ctx;
MockTestImageCtx* mock_parent_image_ctx;
C_SaferCond finished_cond;
Context *on_finish = &finished_cond;
MockShutDownCryptoRequest mock_shutdown_crypto_request;
MockEncryptionFormat* old_encryption_format;
MockEncryptionFormat* new_encryption_format;
Context* format_context;
MockFormatRequest* mock_format_request;
std::string key = std::string(64, '0');
void SetUp() override {
TestMockFixture::SetUp();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
mock_image_ctx = new MockTestImageCtx(*ictx);
mock_parent_image_ctx = new MockTestImageCtx(*ictx);
old_encryption_format = new MockEncryptionFormat();
new_encryption_format = new MockEncryptionFormat();
mock_image_ctx->encryption_format.reset(old_encryption_format);
mock_format_request = MockFormatRequest::create(
mock_image_ctx,
std::unique_ptr<MockEncryptionFormat>(new_encryption_format),
on_finish);
}
void TearDown() override {
delete mock_parent_image_ctx;
delete mock_image_ctx;
TestMockFixture::TearDown();
}
void expect_test_journal_feature(bool has_journal=false) {
EXPECT_CALL(*mock_image_ctx, test_features(
RBD_FEATURE_JOURNALING)).WillOnce(Return(has_journal));
}
void expect_shutdown_crypto(int r = 0) {
EXPECT_CALL(mock_shutdown_crypto_request, send()).WillOnce(
Invoke([this, r]() {
if (r == 0) {
mock_image_ctx->encryption_format.reset();
}
mock_shutdown_crypto_request.on_finish->complete(r);
}));
}
void expect_encryption_format() {
EXPECT_CALL(*new_encryption_format, format(
mock_image_ctx, _)).WillOnce(
WithArg<1>(Invoke([this](Context* ctx) {
format_context = ctx;
})));
}
void expect_image_flush(int r) {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_)).WillOnce(
Invoke([r](io::ImageDispatchSpec* spec) {
ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
&spec->request) != nullptr);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
spec->aio_comp->set_request_count(1);
spec->aio_comp->add_request();
spec->aio_comp->complete_request(r);
}));
}
};
TEST_F(TestMockCryptoFormatRequest, JournalEnabled) {
expect_test_journal_feature(true);
mock_format_request->send();
ASSERT_EQ(-ENOTSUP, finished_cond.wait());
ASSERT_EQ(old_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoFormatRequest, FailShutDownCrypto) {
expect_test_journal_feature(false);
expect_shutdown_crypto(-EIO);
mock_format_request->send();
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(old_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoFormatRequest, FormatFail) {
mock_image_ctx->encryption_format = nullptr;
expect_test_journal_feature(false);
expect_encryption_format();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
format_context->complete(-EIO);
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format);
}
TEST_F(TestMockCryptoFormatRequest, Success) {
mock_image_ctx->encryption_format = nullptr;
expect_test_journal_feature(false);
expect_encryption_format();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_flush(0);
format_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(new_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoFormatRequest, FailFlush) {
expect_test_journal_feature(false);
expect_shutdown_crypto();
expect_encryption_format();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_flush(-EIO);
format_context->complete(0);
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoFormatRequest, CryptoAlreadyLoaded) {
expect_test_journal_feature(false);
expect_shutdown_crypto();
expect_encryption_format();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_flush(0);
format_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(new_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoFormatRequest, ThinFormat) {
mock_image_ctx->encryption_format = nullptr;
mock_image_ctx->parent = mock_parent_image_ctx;
expect_test_journal_feature(false);
expect_encryption_format();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_flush(0);
format_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoFormatRequest, ThinFormatEncryptionLoaded) {
mock_image_ctx->parent = mock_parent_image_ctx;
expect_test_journal_feature(false);
mock_format_request->send();
ASSERT_EQ(-EINVAL, finished_cond.wait());
}
} // namespace crypto
} // namespace librbd
| 7,266 | 30.323276 | 76 |
cc
|
null |
ceph-main/src/test/librbd/crypto/test_mock_LoadRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "librbd/crypto/CryptoObjectDispatch.h"
#include "librbd/crypto/Utils.h"
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/crypto/MockCryptoInterface.h"
#include "test/librbd/mock/crypto/MockEncryptionFormat.h"
#include "test/librbd/mock/io/MockObjectDispatch.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
MockTestImageCtx *parent = nullptr;
};
} // anonymous namespace
namespace util {
inline ImageCtx *get_image_ctx(MockTestImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
namespace crypto {
namespace util {
template <>
void set_crypto(MockTestImageCtx *image_ctx,
std::unique_ptr<MockEncryptionFormat> encryption_format) {
image_ctx->encryption_format = std::move(encryption_format);
}
} // namespace util
} // namespace crypto
} // namespace librbd
#include "librbd/crypto/LoadRequest.cc"
namespace librbd {
namespace crypto {
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArgs;
struct TestMockCryptoLoadRequest : public TestMockFixture {
typedef LoadRequest<librbd::MockTestImageCtx> MockLoadRequest;
MockTestImageCtx* mock_image_ctx;
MockTestImageCtx* mock_parent_image_ctx;
C_SaferCond finished_cond;
Context *on_finish = &finished_cond;
MockEncryptionFormat* mock_encryption_format;
MockEncryptionFormat* cloned_encryption_format;
Context* load_context;
MockLoadRequest* mock_load_request;
void SetUp() override {
TestMockFixture::SetUp();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
mock_image_ctx = new MockTestImageCtx(*ictx);
mock_parent_image_ctx = new MockTestImageCtx(*ictx);
mock_image_ctx->parent = mock_parent_image_ctx;
mock_encryption_format = new MockEncryptionFormat();
std::vector<std::unique_ptr<MockEncryptionFormat>> formats;
formats.emplace_back(mock_encryption_format);
mock_load_request = MockLoadRequest::create(
mock_image_ctx, std::move(formats), on_finish);
}
void TearDown() override {
delete mock_image_ctx;
delete mock_parent_image_ctx;
TestMockFixture::TearDown();
}
void expect_test_journal_feature(MockTestImageCtx* ictx,
bool has_journal = false) {
EXPECT_CALL(*ictx, test_features(
RBD_FEATURE_JOURNALING)).WillOnce(Return(has_journal));
}
void expect_encryption_load(MockEncryptionFormat* encryption_format,
MockTestImageCtx* ictx,
std::string detected_format = "SOMEFORMAT") {
EXPECT_CALL(*encryption_format, load(
ictx, _, _)).WillOnce(
WithArgs<1, 2>(Invoke([this, detected_format](
std::string* detected_format_name, Context* ctx) {
if (!detected_format.empty()) {
*detected_format_name = detected_format;
}
load_context = ctx;
})));
}
void expect_encryption_format_clone(MockEncryptionFormat* encryption_format) {
cloned_encryption_format = new MockEncryptionFormat();
EXPECT_CALL(*encryption_format, clone()).WillOnce(
Invoke([this]() {
return std::unique_ptr<MockEncryptionFormat>(
cloned_encryption_format);
}));
}
void expect_image_flush(int r = 0) {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_)).WillOnce(
Invoke([r](io::ImageDispatchSpec* spec) {
ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
&spec->request) != nullptr);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
spec->aio_comp->set_request_count(1);
spec->aio_comp->add_request();
spec->aio_comp->complete_request(r);
}));
}
void expect_invalidate_cache(int r = 0) {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher,
invalidate_cache(_)).WillOnce(
Invoke([r](Context* ctx) {
ctx->complete(r);
}));
}
};
TEST_F(TestMockCryptoLoadRequest, NoFormats) {
delete mock_load_request;
std::vector<std::unique_ptr<MockEncryptionFormat>> formats;
mock_load_request = MockLoadRequest::create(
mock_image_ctx, std::move(formats), on_finish);
mock_load_request->send();
ASSERT_EQ(-EINVAL, finished_cond.wait());
}
TEST_F(TestMockCryptoLoadRequest, CryptoAlreadyLoaded) {
mock_image_ctx->encryption_format.reset(new MockEncryptionFormat());
mock_load_request->send();
ASSERT_EQ(-EEXIST, finished_cond.wait());
}
TEST_F(TestMockCryptoLoadRequest, JournalEnabled) {
expect_test_journal_feature(mock_image_ctx, true);
mock_load_request->send();
ASSERT_EQ(-ENOTSUP, finished_cond.wait());
}
TEST_F(TestMockCryptoLoadRequest, JournalEnabledOnParent) {
expect_test_journal_feature(mock_image_ctx);
expect_test_journal_feature(mock_parent_image_ctx, true);
mock_load_request->send();
ASSERT_EQ(-ENOTSUP, finished_cond.wait());
}
TEST_F(TestMockCryptoLoadRequest, LoadFail) {
expect_test_journal_feature(mock_image_ctx);
expect_test_journal_feature(mock_parent_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
load_context->complete(-EINVAL);
ASSERT_EQ(-EINVAL, finished_cond.wait());
}
TEST_F(TestMockCryptoLoadRequest, Success) {
delete mock_load_request;
mock_image_ctx->parent = nullptr;
mock_encryption_format = new MockEncryptionFormat();
std::vector<std::unique_ptr<MockEncryptionFormat>> formats;
formats.emplace_back(mock_encryption_format);
mock_load_request = MockLoadRequest::create(
mock_image_ctx, std::move(formats), on_finish);
expect_test_journal_feature(mock_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_invalidate_cache();
load_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLoadRequest, FlushFail) {
delete mock_load_request;
mock_image_ctx->parent = nullptr;
mock_encryption_format = new MockEncryptionFormat();
std::vector<std::unique_ptr<MockEncryptionFormat>> formats;
formats.emplace_back(mock_encryption_format);
mock_load_request = MockLoadRequest::create(
mock_image_ctx, std::move(formats), on_finish);
expect_test_journal_feature(mock_image_ctx);
expect_image_flush(-EIO);
mock_load_request->send();
ASSERT_EQ(-EIO, finished_cond.wait());
}
TEST_F(TestMockCryptoLoadRequest, InvalidateCacheFail) {
delete mock_load_request;
mock_image_ctx->parent = nullptr;
mock_encryption_format = new MockEncryptionFormat();
std::vector<std::unique_ptr<MockEncryptionFormat>> formats;
formats.emplace_back(mock_encryption_format);
mock_load_request = MockLoadRequest::create(
mock_image_ctx, std::move(formats), on_finish);
expect_test_journal_feature(mock_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_invalidate_cache(-EIO);
load_context->complete(0);
ASSERT_EQ(-EIO, finished_cond.wait());
}
TEST_F(TestMockCryptoLoadRequest, LoadClonedEncryptedParent) {
expect_test_journal_feature(mock_image_ctx);
expect_test_journal_feature(mock_parent_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_encryption_format_clone(mock_encryption_format);
expect_encryption_load(cloned_encryption_format, mock_parent_image_ctx);
load_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_invalidate_cache();
load_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
ASSERT_EQ(cloned_encryption_format,
mock_parent_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLoadRequest, LoadClonedParentFail) {
expect_test_journal_feature(mock_image_ctx);
expect_test_journal_feature(mock_parent_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_encryption_format_clone(mock_encryption_format);
expect_encryption_load(cloned_encryption_format, mock_parent_image_ctx);
load_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
load_context->complete(-EIO);
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
ASSERT_EQ(nullptr, mock_parent_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLoadRequest, LoadClonedPlaintextParent) {
expect_test_journal_feature(mock_image_ctx);
expect_test_journal_feature(mock_parent_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_encryption_format_clone(mock_encryption_format);
expect_encryption_load(
cloned_encryption_format, mock_parent_image_ctx,
LoadRequest<MockImageCtx>::UNKNOWN_FORMAT);
load_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_invalidate_cache();
load_context->complete(-EINVAL);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
ASSERT_EQ(nullptr, mock_parent_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLoadRequest, LoadClonedParentDetectionError) {
expect_test_journal_feature(mock_image_ctx);
expect_test_journal_feature(mock_parent_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_encryption_format_clone(mock_encryption_format);
expect_encryption_load(
cloned_encryption_format, mock_parent_image_ctx, "");
load_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
load_context->complete(-EINVAL);
ASSERT_EQ(-EINVAL, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
ASSERT_EQ(nullptr, mock_parent_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLoadRequest, LoadParentFail) {
delete mock_load_request;
mock_encryption_format = new MockEncryptionFormat();
auto mock_parent_encryption_format = new MockEncryptionFormat();
std::vector<std::unique_ptr<MockEncryptionFormat>> formats;
formats.emplace_back(mock_encryption_format);
formats.emplace_back(mock_parent_encryption_format);
mock_load_request = MockLoadRequest::create(
mock_image_ctx,
std::move(formats),
on_finish);
expect_test_journal_feature(mock_image_ctx);
expect_test_journal_feature(mock_parent_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_encryption_load(mock_parent_encryption_format, mock_parent_image_ctx);
load_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
load_context->complete(-EINVAL);
ASSERT_EQ(-EINVAL, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
ASSERT_EQ(nullptr, mock_parent_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLoadRequest, EncryptedParent) {
delete mock_load_request;
mock_encryption_format = new MockEncryptionFormat();
auto mock_parent_encryption_format = new MockEncryptionFormat();
std::vector<std::unique_ptr<MockEncryptionFormat>> formats;
formats.emplace_back(mock_encryption_format);
formats.emplace_back(mock_parent_encryption_format);
mock_load_request = MockLoadRequest::create(
mock_image_ctx,
std::move(formats),
on_finish);
expect_test_journal_feature(mock_image_ctx);
expect_test_journal_feature(mock_parent_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_encryption_load(mock_parent_encryption_format, mock_parent_image_ctx);
load_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_invalidate_cache();
load_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
ASSERT_EQ(mock_parent_encryption_format,
mock_parent_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLoadRequest, TooManyFormats) {
delete mock_load_request;
mock_encryption_format = new MockEncryptionFormat();
auto mock_parent_encryption_format = new MockEncryptionFormat();
std::vector<std::unique_ptr<MockEncryptionFormat>> formats;
formats.emplace_back(mock_encryption_format);
formats.emplace_back(mock_parent_encryption_format);
mock_image_ctx->parent = nullptr;
mock_load_request = MockLoadRequest::create(
mock_image_ctx,
std::move(formats),
on_finish);
expect_test_journal_feature(mock_image_ctx);
expect_image_flush();
expect_encryption_load(mock_encryption_format, mock_image_ctx);
mock_load_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
load_context->complete(0);
ASSERT_EQ(-EINVAL, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
}
} // namespace crypto
} // namespace librbd
| 14,294 | 36.32376 | 80 |
cc
|
null |
ceph-main/src/test/librbd/crypto/test_mock_ShutDownCryptoRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "librbd/crypto/Utils.h"
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/crypto/MockEncryptionFormat.h"
#include "librbd/crypto/ShutDownCryptoRequest.cc"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
MockTestImageCtx *parent = nullptr;
};
} // anonymous namespace
namespace crypto {
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArgs;
struct TestMockShutDownCryptoRequest : public TestMockFixture {
typedef ShutDownCryptoRequest<MockTestImageCtx> MockShutDownCryptoRequest;
MockTestImageCtx* mock_image_ctx;
C_SaferCond finished_cond;
Context *on_finish = &finished_cond;
MockShutDownCryptoRequest* mock_shutdown_crypto_request;
MockEncryptionFormat* mock_encryption_format;
Context* shutdown_object_dispatch_context;
Context* shutdown_image_dispatch_context;
void SetUp() override {
TestMockFixture::SetUp();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
mock_image_ctx = new MockTestImageCtx(*ictx);
mock_encryption_format = new MockEncryptionFormat();
mock_image_ctx->encryption_format.reset(mock_encryption_format);
mock_shutdown_crypto_request = MockShutDownCryptoRequest::create(
mock_image_ctx, on_finish);
}
void TearDown() override {
delete mock_image_ctx;
TestMockFixture::TearDown();
}
void expect_crypto_object_layer_exists_check(
MockTestImageCtx* image_ctx, bool exists) {
EXPECT_CALL(*image_ctx->io_object_dispatcher, exists(
io::OBJECT_DISPATCH_LAYER_CRYPTO)).WillOnce(Return(exists));
}
void expect_crypto_image_layer_exists_check(
MockTestImageCtx* image_ctx, bool exists) {
EXPECT_CALL(*image_ctx->io_image_dispatcher, exists(
io::IMAGE_DISPATCH_LAYER_CRYPTO)).WillOnce(Return(exists));
}
void expect_shutdown_crypto_object_dispatch(MockTestImageCtx* image_ctx) {
EXPECT_CALL(*image_ctx->io_object_dispatcher, shut_down_dispatch(
io::OBJECT_DISPATCH_LAYER_CRYPTO, _)).WillOnce(
WithArgs<1>(Invoke([this](Context* ctx) {
shutdown_object_dispatch_context = ctx;
})));
}
void expect_shutdown_crypto_image_dispatch(MockTestImageCtx* image_ctx) {
EXPECT_CALL(*image_ctx->io_image_dispatcher, shut_down_dispatch(
io::IMAGE_DISPATCH_LAYER_CRYPTO, _)).WillOnce(
WithArgs<1>(Invoke([this](Context* ctx) {
shutdown_image_dispatch_context = ctx;
})));
}
};
TEST_F(TestMockShutDownCryptoRequest, NoCryptoObjectDispatch) {
expect_crypto_object_layer_exists_check(mock_image_ctx, false);
mock_shutdown_crypto_request->send();
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockShutDownCryptoRequest, FailShutdownObjectDispatch) {
expect_crypto_object_layer_exists_check(mock_image_ctx, true);
expect_shutdown_crypto_object_dispatch(mock_image_ctx);
mock_shutdown_crypto_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
shutdown_object_dispatch_context->complete(-EIO);
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockShutDownCryptoRequest, NoCryptoImageDispatch) {
expect_crypto_object_layer_exists_check(mock_image_ctx, true);
expect_shutdown_crypto_object_dispatch(mock_image_ctx);
mock_shutdown_crypto_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_crypto_image_layer_exists_check(mock_image_ctx, false);
shutdown_object_dispatch_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockShutDownCryptoRequest, FailShutdownImageDispatch) {
expect_crypto_object_layer_exists_check(mock_image_ctx, true);
expect_shutdown_crypto_object_dispatch(mock_image_ctx);
mock_shutdown_crypto_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_crypto_image_layer_exists_check(mock_image_ctx, true);
expect_shutdown_crypto_image_dispatch(mock_image_ctx);
shutdown_object_dispatch_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
shutdown_image_dispatch_context->complete(-EIO);
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockShutDownCryptoRequest, Success) {
expect_crypto_object_layer_exists_check(mock_image_ctx, true);
expect_shutdown_crypto_object_dispatch(mock_image_ctx);
mock_shutdown_crypto_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_crypto_image_layer_exists_check(mock_image_ctx, true);
expect_shutdown_crypto_image_dispatch(mock_image_ctx);
shutdown_object_dispatch_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
shutdown_image_dispatch_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockShutDownCryptoRequest, ShutdownParent) {
auto parent_image_ctx = new MockTestImageCtx(*mock_image_ctx->image_ctx);
mock_image_ctx->parent = parent_image_ctx;
expect_crypto_object_layer_exists_check(mock_image_ctx, true);
expect_shutdown_crypto_object_dispatch(mock_image_ctx);
mock_shutdown_crypto_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_crypto_image_layer_exists_check(mock_image_ctx, true);
expect_shutdown_crypto_image_dispatch(mock_image_ctx);
shutdown_object_dispatch_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_crypto_object_layer_exists_check(parent_image_ctx, true);
expect_shutdown_crypto_object_dispatch(parent_image_ctx);
shutdown_image_dispatch_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_crypto_image_layer_exists_check(parent_image_ctx, true);
expect_shutdown_crypto_image_dispatch(parent_image_ctx);
shutdown_object_dispatch_context->complete(0);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
mock_image_ctx->parent = nullptr;
shutdown_image_dispatch_context->complete(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_EQ(nullptr, mock_image_ctx->encryption_format.get());
ASSERT_EQ(nullptr, parent_image_ctx->encryption_format.get());
delete parent_image_ctx;
}
} // namespace crypto
} // namespace librbd
| 6,773 | 37.708571 | 77 |
cc
|
null |
ceph-main/src/test/librbd/crypto/luks/test_mock_FlattenRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/crypto/MockCryptoInterface.h"
#include "test/librbd/mock/crypto/MockEncryptionFormat.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
#include "librbd/crypto/luks/FlattenRequest.cc"
namespace librbd {
namespace crypto {
namespace luks {
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
struct TestMockCryptoLuksFlattenRequest : public TestMockFixture {
typedef FlattenRequest<MockTestImageCtx> MockFlattenRequest;
const size_t OBJECT_SIZE = 4 * 1024 * 1024;
const uint64_t DATA_OFFSET = MockCryptoInterface::DATA_OFFSET;
const char* passphrase_cstr = "password";
std::string passphrase = passphrase_cstr;
MockTestImageCtx* mock_image_ctx;
MockFlattenRequest* mock_flatten_request;
MockEncryptionFormat* mock_encryption_format;
MockCryptoInterface mock_crypto;
C_SaferCond finished_cond;
Context *on_finish = &finished_cond;
Context* image_read_request;
io::AioCompletion* aio_comp;
ceph::bufferlist header_bl;
void SetUp() override {
TestMockFixture::SetUp();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
mock_image_ctx = new MockTestImageCtx(*ictx);
mock_encryption_format = new MockEncryptionFormat();
mock_image_ctx->encryption_format.reset(mock_encryption_format);
mock_flatten_request = MockFlattenRequest::create(
mock_image_ctx, on_finish);
}
void TearDown() override {
delete mock_image_ctx;
TestMockFixture::TearDown();
}
void generate_header(const char* type, const char* alg, size_t key_size,
const char* cipher_mode, uint32_t sector_size,
bool magic_switched) {
Header header(mock_image_ctx->cct);
ASSERT_EQ(0, header.init());
ASSERT_EQ(0, header.format(type, alg, nullptr, key_size, cipher_mode,
sector_size, OBJECT_SIZE, true));
ASSERT_EQ(0, header.add_keyslot(passphrase_cstr, strlen(passphrase_cstr)));
ASSERT_LT(0, header.read(&header_bl));
if (magic_switched) {
ASSERT_EQ(0, Magic::replace_magic(mock_image_ctx->cct, header_bl));
}
}
void expect_get_crypto() {
EXPECT_CALL(*mock_encryption_format, get_crypto()).WillOnce(
Return(&mock_crypto));
}
void expect_image_read(uint64_t offset, uint64_t length) {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_))
.WillOnce(Invoke([this, offset,
length](io::ImageDispatchSpec* spec) {
auto* read = boost::get<io::ImageDispatchSpec::Read>(
&spec->request);
ASSERT_TRUE(read != nullptr);
ASSERT_EQ(1, spec->image_extents.size());
ASSERT_EQ(offset, spec->image_extents[0].first);
ASSERT_EQ(length, spec->image_extents[0].second);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
auto aio_comp = spec->aio_comp;
aio_comp->set_request_count(1);
aio_comp->read_result = std::move(read->read_result);
aio_comp->read_result.set_image_extents(spec->image_extents);
auto ctx = new io::ReadResult::C_ImageReadRequest(
aio_comp, 0, spec->image_extents);
if (header_bl.length() < offset + length) {
header_bl.append_zero(offset + length - header_bl.length());
}
ctx->bl.substr_of(header_bl, offset, length);
image_read_request = ctx;
}));
}
void expect_image_write() {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_))
.WillOnce(Invoke([this](io::ImageDispatchSpec* spec) {
auto* write = boost::get<io::ImageDispatchSpec::Write>(
&spec->request);
ASSERT_TRUE(write != nullptr);
ASSERT_EQ(1, spec->image_extents.size());
ASSERT_EQ(0, spec->image_extents[0].first);
ASSERT_GT(spec->image_extents[0].second, 0);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
aio_comp = spec->aio_comp;
// patch header_bl with write
bufferlist bl;
bl.substr_of(header_bl, write->bl.length(),
header_bl.length() - write->bl.length());
header_bl = write->bl;
header_bl.claim_append(bl);
}));
}
void expect_image_flush(int r) {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_)).WillOnce(
Invoke([r](io::ImageDispatchSpec* spec) {
ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
&spec->request) != nullptr);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
spec->aio_comp->set_request_count(1);
spec->aio_comp->add_request();
spec->aio_comp->complete_request(r);
}));
}
void complete_aio(int r) {
if (r < 0) {
aio_comp->fail(r);
} else {
aio_comp->set_request_count(1);
aio_comp->add_request();
aio_comp->complete_request(r);
}
}
void verify_header(const char* expected_format) {
Header header(mock_image_ctx->cct);
ASSERT_EQ(0, header.init());
ASSERT_EQ(0, header.write(header_bl));
ASSERT_EQ(0, header.load(expected_format));
}
};
TEST_F(TestMockCryptoLuksFlattenRequest, LUKS1) {
generate_header(CRYPT_LUKS1, "aes", 32, "xts-plain64", 512, true);
expect_get_crypto();
expect_image_read(0, DATA_OFFSET);
mock_flatten_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_write();
image_read_request->complete(DATA_OFFSET);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_flush(0);
complete_aio(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NO_FATAL_FAILURE(verify_header(CRYPT_LUKS1));
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLuksFlattenRequest, LUKS2) {
generate_header(CRYPT_LUKS2, "aes", 32, "xts-plain64", 4096, true);
expect_get_crypto();
expect_image_read(0, DATA_OFFSET);
mock_flatten_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_write();
image_read_request->complete(DATA_OFFSET);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_flush(0);
complete_aio(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NO_FATAL_FAILURE(verify_header(CRYPT_LUKS2));
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLuksFlattenRequest, FailedRead) {
generate_header(CRYPT_LUKS2, "aes", 32, "xts-plain64", 4096, true);
expect_get_crypto();
expect_image_read(0, DATA_OFFSET);
mock_flatten_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
image_read_request->complete(-EIO);
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLuksFlattenRequest, AlreadyFlattened) {
generate_header(CRYPT_LUKS2, "aes", 32, "xts-plain64", 4096, false);
expect_get_crypto();
expect_image_read(0, DATA_OFFSET);
mock_flatten_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_write();
image_read_request->complete(DATA_OFFSET);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_flush(0);
complete_aio(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NO_FATAL_FAILURE(verify_header(CRYPT_LUKS2));
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLuksFlattenRequest, FailedWrite) {
generate_header(CRYPT_LUKS2, "aes", 32, "xts-plain64", 4096, true);
expect_get_crypto();
expect_image_read(0, DATA_OFFSET);
mock_flatten_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_write();
image_read_request->complete(DATA_OFFSET);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
complete_aio(-EIO);
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
TEST_F(TestMockCryptoLuksFlattenRequest, FailedFlush) {
generate_header(CRYPT_LUKS2, "aes", 32, "xts-plain64", 4096, true);
expect_get_crypto();
expect_image_read(0, DATA_OFFSET);
mock_flatten_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_write();
image_read_request->complete(DATA_OFFSET);
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
expect_image_flush(-EIO);
complete_aio(0);
ASSERT_EQ(-EIO, finished_cond.wait());
ASSERT_EQ(mock_encryption_format, mock_image_ctx->encryption_format.get());
}
} // namespace luks
} // namespace crypto
} // namespace librbd
| 9,362 | 34.067416 | 79 |
cc
|
null |
ceph-main/src/test/librbd/crypto/luks/test_mock_FormatRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
namespace librbd {
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
#include "librbd/crypto/luks/FormatRequest.cc"
namespace librbd {
namespace crypto {
namespace luks {
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
struct TestMockCryptoLuksFormatRequest : public TestMockFixture {
typedef FormatRequest<librbd::MockImageCtx> MockFormatRequest;
const size_t OBJECT_SIZE = 4 * 1024 * 1024;
const size_t IMAGE_SIZE = 1024 * 1024 * 1024;
const char* passphrase_cstr = "password";
std::string passphrase = passphrase_cstr;
MockImageCtx* mock_image_ctx;
C_SaferCond finished_cond;
Context *on_finish = &finished_cond;
io::AioCompletion* aio_comp;
ceph::bufferlist header_bl;
std::unique_ptr<CryptoInterface> crypto;
void SetUp() override {
TestMockFixture::SetUp();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
mock_image_ctx = new MockImageCtx(*ictx);
}
void TearDown() override {
delete mock_image_ctx;
TestMockFixture::TearDown();
}
void expect_get_stripe_period() {
EXPECT_CALL(*mock_image_ctx, get_stripe_period()).WillOnce(Return(
OBJECT_SIZE));
}
void expect_get_image_size(uint64_t image_size) {
EXPECT_CALL(*mock_image_ctx, get_image_size(CEPH_NOSNAP)).WillOnce(Return(
image_size));
}
void expect_image_write() {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_))
.WillOnce(Invoke([this](io::ImageDispatchSpec* spec) {
auto* write = boost::get<io::ImageDispatchSpec::Write>(
&spec->request);
ASSERT_TRUE(write != nullptr);
ASSERT_EQ(1, spec->image_extents.size());
ASSERT_EQ(0, spec->image_extents[0].first);
ASSERT_GT(spec->image_extents[0].second, 0);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
aio_comp = spec->aio_comp;
header_bl = write->bl;
}));
}
void complete_aio(int r) {
if (r < 0) {
aio_comp->fail(r);
} else {
aio_comp->set_request_count(1);
aio_comp->add_request();
aio_comp->complete_request(r);
}
}
void verify_header(const char* expected_format, size_t expected_key_length,
uint64_t expected_sector_size, bool magic_switched) {
Header header(mock_image_ctx->cct);
ASSERT_EQ(0, header.init());
if (magic_switched) {
Header non_switched_header(mock_image_ctx->cct);
ASSERT_EQ(0, non_switched_header.init());
ASSERT_EQ(0, non_switched_header.write(header_bl));
ASSERT_EQ(-EINVAL, non_switched_header.load(expected_format));
ASSERT_EQ(0, Magic::replace_magic(mock_image_ctx->cct, header_bl));
}
ASSERT_EQ(0, header.write(header_bl));
ASSERT_EQ(0, header.load(expected_format));
ASSERT_EQ(expected_sector_size, header.get_sector_size());
ASSERT_EQ(0, header.get_data_offset() % OBJECT_SIZE);
char volume_key[64];
size_t volume_key_size = sizeof(volume_key);
ASSERT_EQ(0, header.read_volume_key(
passphrase_cstr, strlen(passphrase_cstr),
reinterpret_cast<char*>(volume_key), &volume_key_size));
ASSERT_EQ(expected_key_length, crypto->get_key_length());
ASSERT_EQ(0, std::memcmp(
volume_key, crypto->get_key(), expected_key_length));
ASSERT_EQ(expected_sector_size, crypto->get_block_size());
ASSERT_EQ(header.get_data_offset(), crypto->get_data_offset());
}
};
TEST_F(TestMockCryptoLuksFormatRequest, LUKS1) {
auto mock_format_request = MockFormatRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS1,
RBD_ENCRYPTION_ALGORITHM_AES128, std::move(passphrase), &crypto,
on_finish, true);
expect_get_stripe_period();
expect_get_image_size(IMAGE_SIZE);
expect_image_write();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
complete_aio(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NO_FATAL_FAILURE(verify_header(CRYPT_LUKS1, 32, 512, false));
}
TEST_F(TestMockCryptoLuksFormatRequest, AES128) {
auto mock_format_request = MockFormatRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS2,
RBD_ENCRYPTION_ALGORITHM_AES128, std::move(passphrase), &crypto,
on_finish, true);
expect_get_stripe_period();
expect_get_image_size(IMAGE_SIZE);
expect_image_write();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
complete_aio(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NO_FATAL_FAILURE(verify_header(CRYPT_LUKS2, 32, 4096, false));
}
TEST_F(TestMockCryptoLuksFormatRequest, AES256) {
auto mock_format_request = MockFormatRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS2,
RBD_ENCRYPTION_ALGORITHM_AES256, std::move(passphrase), &crypto,
on_finish, true);
expect_get_stripe_period();
expect_get_image_size(IMAGE_SIZE);
expect_image_write();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
complete_aio(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NO_FATAL_FAILURE(verify_header(CRYPT_LUKS2, 64, 4096, false));
}
TEST_F(TestMockCryptoLuksFormatRequest, LUKS1OnCloned) {
mock_image_ctx->parent = mock_image_ctx;
auto mock_format_request = MockFormatRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS1,
RBD_ENCRYPTION_ALGORITHM_AES256, std::move(passphrase), &crypto,
on_finish, true);
expect_get_stripe_period();
expect_get_image_size(IMAGE_SIZE);
expect_image_write();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
complete_aio(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NO_FATAL_FAILURE(verify_header(CRYPT_LUKS1, 64, 512, true));
}
TEST_F(TestMockCryptoLuksFormatRequest, LUKS2OnCloned) {
mock_image_ctx->parent = mock_image_ctx;
auto mock_format_request = MockFormatRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS2,
RBD_ENCRYPTION_ALGORITHM_AES256, std::move(passphrase), &crypto,
on_finish, true);
expect_get_stripe_period();
expect_get_image_size(IMAGE_SIZE);
expect_image_write();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
complete_aio(0);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NO_FATAL_FAILURE(verify_header(CRYPT_LUKS2, 64, 4096, true));
}
TEST_F(TestMockCryptoLuksFormatRequest, ImageTooSmall) {
auto mock_format_request = MockFormatRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS2,
RBD_ENCRYPTION_ALGORITHM_AES256, std::move(passphrase), &crypto,
on_finish, true);
expect_get_stripe_period();
expect_get_image_size(1024*1024);
mock_format_request->send();
ASSERT_EQ(-ENOSPC, finished_cond.wait());
}
TEST_F(TestMockCryptoLuksFormatRequest, WriteFail) {
auto mock_format_request = MockFormatRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS2,
RBD_ENCRYPTION_ALGORITHM_AES256, std::move(passphrase), &crypto,
on_finish, true);
expect_get_stripe_period();
expect_get_image_size(IMAGE_SIZE);
expect_image_write();
mock_format_request->send();
ASSERT_EQ(ETIMEDOUT, finished_cond.wait_for(0));
complete_aio(-123);
ASSERT_EQ(-123, finished_cond.wait());
}
} // namespace luks
} // namespace crypto
} // namespace librbd
| 7,788 | 32.718615 | 78 |
cc
|
null |
ceph-main/src/test/librbd/crypto/luks/test_mock_LoadRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
namespace librbd {
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
#include "librbd/crypto/luks/LoadRequest.cc"
namespace librbd {
namespace crypto {
namespace luks {
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
struct TestMockCryptoLuksLoadRequest : public TestMockFixture {
typedef LoadRequest<librbd::MockImageCtx> MockLoadRequest;
const size_t OBJECT_SIZE = 4 * 1024 * 1024;
const char* passphrase_cstr = "password";
std::string passphrase = passphrase_cstr;
MockImageCtx* mock_image_ctx;
std::unique_ptr<CryptoInterface> crypto;
MockLoadRequest* mock_load_request;
C_SaferCond finished_cond;
Context *on_finish = &finished_cond;
Context* image_read_request;
ceph::bufferlist header_bl;
uint64_t data_offset;
std::string detected_format_name;
void SetUp() override {
TestMockFixture::SetUp();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
mock_image_ctx = new MockImageCtx(*ictx);
mock_load_request = MockLoadRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS2, std::move(passphrase),
&crypto, &detected_format_name, on_finish);
detected_format_name = "";
}
void TearDown() override {
delete mock_image_ctx;
TestMockFixture::TearDown();
}
// returns data offset in bytes
void generate_header(const char* type, const char* alg, size_t key_size,
const char* cipher_mode, uint32_t sector_size,
bool magic_switched) {
Header header(mock_image_ctx->cct);
ASSERT_EQ(0, header.init());
ASSERT_EQ(0, header.format(type, alg, nullptr, key_size, cipher_mode,
sector_size, OBJECT_SIZE, true));
ASSERT_EQ(0, header.add_keyslot(passphrase_cstr, strlen(passphrase_cstr)));
ASSERT_LT(0, header.read(&header_bl));
if (magic_switched) {
ASSERT_EQ(0, Magic::replace_magic(mock_image_ctx->cct, header_bl));
}
data_offset = header.get_data_offset();
}
void expect_image_read(uint64_t offset, uint64_t length) {
EXPECT_CALL(*mock_image_ctx->io_image_dispatcher, send(_))
.WillOnce(Invoke([this, offset,
length](io::ImageDispatchSpec* spec) {
auto* read = boost::get<io::ImageDispatchSpec::Read>(
&spec->request);
ASSERT_TRUE(read != nullptr);
ASSERT_EQ(1, spec->image_extents.size());
ASSERT_EQ(offset, spec->image_extents[0].first);
ASSERT_EQ(length, spec->image_extents[0].second);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
auto aio_comp = spec->aio_comp;
aio_comp->set_request_count(1);
aio_comp->read_result = std::move(read->read_result);
aio_comp->read_result.set_image_extents(spec->image_extents);
auto ctx = new io::ReadResult::C_ImageReadRequest(
aio_comp, 0, spec->image_extents);
if (header_bl.length() < offset + length) {
header_bl.append_zero(offset + length - header_bl.length());
}
ctx->bl.substr_of(header_bl, offset, length);
image_read_request = ctx;
}));
}
void expect_get_image_size(uint64_t size) {
EXPECT_CALL(*mock_image_ctx, get_image_size(_)).WillOnce(
Return(size));
}
void expect_get_stripe_period(uint64_t period) {
EXPECT_CALL(*mock_image_ctx, get_stripe_period()).WillOnce(
Return(period));
}
};
TEST_F(TestMockCryptoLuksLoadRequest, AES128) {
generate_header(CRYPT_LUKS2, "aes", 32, "xts-plain64", 4096, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NE(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, AES256) {
generate_header(CRYPT_LUKS2, "aes", 64, "xts-plain64", 4096, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NE(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, LUKS1) {
delete mock_load_request;
mock_load_request = MockLoadRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS1, {passphrase_cstr}, &crypto,
&detected_format_name, on_finish);
generate_header(CRYPT_LUKS1, "aes", 32, "xts-plain64", 512, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NE(crypto.get(), nullptr);
ASSERT_EQ("LUKS1", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, LUKS1ViaLUKS) {
delete mock_load_request;
mock_load_request = MockLoadRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS, {passphrase_cstr}, &crypto,
&detected_format_name, on_finish);
generate_header(CRYPT_LUKS1, "aes", 32, "xts-plain64", 512, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NE(crypto.get(), nullptr);
ASSERT_EQ("LUKS1", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, UnknownFormat) {
header_bl.append_zero(MAXIMUM_HEADER_SIZE);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(-EINVAL, finished_cond.wait());
ASSERT_EQ(crypto.get(), nullptr);
ASSERT_EQ("<unknown>", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, WrongFormat) {
generate_header(CRYPT_LUKS1, "aes", 32, "xts-plain64", 512, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
mock_load_request->send();
expect_image_read(DEFAULT_INITIAL_READ_SIZE,
MAXIMUM_HEADER_SIZE - DEFAULT_INITIAL_READ_SIZE);
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
image_read_request->complete(MAXIMUM_HEADER_SIZE - DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(-EINVAL, finished_cond.wait());
ASSERT_EQ(crypto.get(), nullptr);
ASSERT_EQ("LUKS", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, UnsupportedAlgorithm) {
generate_header(CRYPT_LUKS2, "twofish", 32, "xts-plain64", 4096, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(-ENOTSUP, finished_cond.wait());
ASSERT_EQ(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, UnsupportedCipherMode) {
generate_header(CRYPT_LUKS2, "aes", 32, "cbc-essiv:sha256", 4096, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(-ENOTSUP, finished_cond.wait());
ASSERT_EQ(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, BadSize) {
generate_header(CRYPT_LUKS2, "aes", 64, "xts-plain64", 4096, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE - 1);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(-EINVAL, finished_cond.wait());
ASSERT_EQ(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, BadStripePattern) {
generate_header(CRYPT_LUKS2, "aes", 64, "xts-plain64", 4096, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE * 3);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(-EINVAL, finished_cond.wait());
ASSERT_EQ(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, HeaderBiggerThanInitialRead) {
generate_header(CRYPT_LUKS2, "aes", 64, "xts-plain64", 4096, false);
mock_load_request->set_initial_read_size(4096);
expect_image_read(0, 4096);
mock_load_request->send();
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
expect_image_read(4096, MAXIMUM_HEADER_SIZE - 4096);
image_read_request->complete(4096); // complete initial read
image_read_request->complete(MAXIMUM_HEADER_SIZE - 4096);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NE(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, LUKS1FormattedClone) {
mock_image_ctx->parent = mock_image_ctx;
delete mock_load_request;
mock_load_request = MockLoadRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS1, {passphrase_cstr}, &crypto,
&detected_format_name, on_finish);
generate_header(CRYPT_LUKS1, "aes", 64, "xts-plain64", 512, true);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NE(crypto.get(), nullptr);
ASSERT_EQ("LUKS1", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, LUKS2FormattedClone) {
mock_image_ctx->parent = mock_image_ctx;
generate_header(CRYPT_LUKS2, "aes", 64, "xts-plain64", 4096, true);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
mock_load_request->send();
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NE(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, KeyslotsBiggerThanInitialRead) {
generate_header(CRYPT_LUKS2, "aes", 64, "xts-plain64", 4096, false);
mock_load_request->set_initial_read_size(16384);
expect_image_read(0, 16384);
mock_load_request->send();
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
expect_image_read(16384, data_offset - 16384);
image_read_request->complete(16384); // complete initial read
image_read_request->complete(data_offset - 16384);
ASSERT_EQ(0, finished_cond.wait());
ASSERT_NE(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
TEST_F(TestMockCryptoLuksLoadRequest, WrongPassphrase) {
delete mock_load_request;
mock_load_request = MockLoadRequest::create(
mock_image_ctx, RBD_ENCRYPTION_FORMAT_LUKS2, "wrong", &crypto,
&detected_format_name, on_finish);
generate_header(CRYPT_LUKS2, "aes", 64, "xts-plain64", 4096, false);
expect_image_read(0, DEFAULT_INITIAL_READ_SIZE);
expect_get_image_size(OBJECT_SIZE << 5);
expect_get_stripe_period(OBJECT_SIZE);
mock_load_request->send();
// crypt_volume_key_get will fail, we will retry reading more
expect_image_read(DEFAULT_INITIAL_READ_SIZE,
data_offset - DEFAULT_INITIAL_READ_SIZE);
image_read_request->complete(DEFAULT_INITIAL_READ_SIZE);
image_read_request->complete(data_offset - DEFAULT_INITIAL_READ_SIZE);
ASSERT_EQ(-EPERM, finished_cond.wait());
ASSERT_EQ(crypto.get(), nullptr);
ASSERT_EQ("LUKS2", detected_format_name);
}
} // namespace luks
} // namespace crypto
} // namespace librbd
| 12,322 | 35.89521 | 80 |
cc
|
null |
ceph-main/src/test/librbd/crypto/openssl/test_DataCryptor.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "librbd/crypto/openssl/DataCryptor.h"
namespace librbd {
namespace crypto {
namespace openssl {
const char* TEST_CIPHER_NAME = "aes-256-xts";
const unsigned char TEST_KEY[64] = {1};
const unsigned char TEST_IV[16] = {2};
const unsigned char TEST_IV_2[16] = {3};
const unsigned char TEST_DATA[4096] = {4};
struct TestCryptoOpensslDataCryptor : public TestFixture {
DataCryptor *cryptor;
void SetUp() override {
TestFixture::SetUp();
cryptor = new DataCryptor(reinterpret_cast<CephContext*>(m_ioctx.cct()));
ASSERT_EQ(0,
cryptor->init(TEST_CIPHER_NAME, TEST_KEY, sizeof(TEST_KEY)));
}
void TearDown() override {
delete cryptor;
TestFixture::TearDown();
}
};
TEST_F(TestCryptoOpensslDataCryptor, InvalidCipherName) {
EXPECT_EQ(-EINVAL, cryptor->init(nullptr, TEST_KEY, sizeof(TEST_KEY)));
EXPECT_EQ(-EINVAL, cryptor->init("", TEST_KEY, sizeof(TEST_KEY)));
EXPECT_EQ(-EINVAL, cryptor->init("Invalid", TEST_KEY, sizeof(TEST_KEY)));
}
TEST_F(TestCryptoOpensslDataCryptor, InvalidKey) {
EXPECT_EQ(-EINVAL, cryptor->init(TEST_CIPHER_NAME, nullptr, 0));
EXPECT_EQ(-EINVAL, cryptor->init(TEST_CIPHER_NAME, nullptr,
sizeof(TEST_KEY)));
EXPECT_EQ(-EINVAL, cryptor->init(TEST_CIPHER_NAME, TEST_KEY, 1));
}
TEST_F(TestCryptoOpensslDataCryptor, GetContextInvalidMode) {
EXPECT_EQ(nullptr, cryptor->get_context(static_cast<CipherMode>(-1)));
}
TEST_F(TestCryptoOpensslDataCryptor, ReturnNullContext) {
cryptor->return_context(nullptr, static_cast<CipherMode>(-1));
}
TEST_F(TestCryptoOpensslDataCryptor, ReturnContextInvalidMode) {
auto ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_NE(ctx, nullptr);
cryptor->return_context(ctx, CipherMode::CIPHER_MODE_DEC);
ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_NE(ctx, nullptr);
cryptor->return_context(ctx, static_cast<CipherMode>(-1));
}
TEST_F(TestCryptoOpensslDataCryptor, EncryptDecrypt) {
auto ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_NE(ctx, nullptr);
cryptor->init_context(ctx, TEST_IV, sizeof(TEST_IV));
unsigned char out[sizeof(TEST_DATA)];
ASSERT_EQ(sizeof(TEST_DATA),
cryptor->update_context(ctx, TEST_DATA, out, sizeof(TEST_DATA)));
cryptor->return_context(ctx, CipherMode::CIPHER_MODE_ENC);
ctx = cryptor->get_context(CipherMode::CIPHER_MODE_DEC);
ASSERT_NE(ctx, nullptr);
ASSERT_EQ(0, cryptor->init_context(ctx, TEST_IV, sizeof(TEST_IV)));
ASSERT_EQ(sizeof(TEST_DATA),
cryptor->update_context(ctx, out, out, sizeof(TEST_DATA)));
ASSERT_EQ(0, memcmp(out, TEST_DATA, sizeof(TEST_DATA)));
cryptor->return_context(ctx, CipherMode::CIPHER_MODE_DEC);
}
TEST_F(TestCryptoOpensslDataCryptor, ReuseContext) {
auto ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_NE(ctx, nullptr);
ASSERT_EQ(0, cryptor->init_context(ctx, TEST_IV, sizeof(TEST_IV)));
unsigned char out[sizeof(TEST_DATA)];
ASSERT_EQ(sizeof(TEST_DATA),
cryptor->update_context(ctx, TEST_DATA, out, sizeof(TEST_DATA)));
ASSERT_EQ(0, cryptor->init_context(ctx, TEST_IV_2, sizeof(TEST_IV_2)));
ASSERT_EQ(sizeof(TEST_DATA),
cryptor->update_context(ctx, TEST_DATA, out, sizeof(TEST_DATA)));
auto ctx2 = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_NE(ctx2, nullptr);
ASSERT_EQ(0, cryptor->init_context(ctx2, TEST_IV_2, sizeof(TEST_IV_2)));
unsigned char out2[sizeof(TEST_DATA)];
ASSERT_EQ(sizeof(TEST_DATA),
cryptor->update_context(ctx2, TEST_DATA, out2, sizeof(TEST_DATA)));
ASSERT_EQ(0, memcmp(out, out2, sizeof(TEST_DATA)));
cryptor->return_context(ctx, CipherMode::CIPHER_MODE_ENC);
cryptor->return_context(ctx2, CipherMode::CIPHER_MODE_ENC);
}
TEST_F(TestCryptoOpensslDataCryptor, InvalidIVLength) {
auto ctx = cryptor->get_context(CipherMode::CIPHER_MODE_ENC);
ASSERT_NE(ctx, nullptr);
ASSERT_EQ(-EINVAL, cryptor->init_context(ctx, TEST_IV, 1));
cryptor->return_context(ctx, CipherMode::CIPHER_MODE_ENC);
}
} // namespace openssl
} // namespace crypto
} // namespace librbd
| 4,279 | 34.966387 | 79 |
cc
|
null |
ceph-main/src/test/librbd/deep_copy/test_mock_ImageCopyRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "include/rbd/librbd.hpp"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/Operations.h"
#include "librbd/deep_copy/Handler.h"
#include "librbd/deep_copy/ImageCopyRequest.h"
#include "librbd/deep_copy/ObjectCopyRequest.h"
#include "librbd/object_map/DiffRequest.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/test_support.h"
#include <boost/scope_exit.hpp>
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
static MockTestImageCtx* s_instance;
static MockTestImageCtx* create(const std::string &image_name,
const std::string &image_id,
librados::snap_t snap_id, librados::IoCtx& p,
bool read_only) {
ceph_assert(s_instance != nullptr);
return s_instance;
}
explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
s_instance = this;
}
MOCK_METHOD0(destroy, void());
};
MockTestImageCtx* MockTestImageCtx::s_instance = nullptr;
} // anonymous namespace
namespace deep_copy {
template <>
struct ObjectCopyRequest<librbd::MockTestImageCtx> {
static ObjectCopyRequest* s_instance;
static ObjectCopyRequest* create(
librbd::MockTestImageCtx *src_image_ctx,
librbd::MockTestImageCtx *dst_image_ctx,
librados::snap_t src_snap_id_start,
librados::snap_t dst_snap_id_start,
const SnapMap &snap_map,
uint64_t object_number, uint32_t flags, Handler* handler,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
std::lock_guard locker{s_instance->lock};
s_instance->snap_map = &snap_map;
s_instance->flags = flags;
s_instance->object_contexts[object_number] = on_finish;
s_instance->cond.notify_all();
return s_instance;
}
MOCK_METHOD0(send, void());
ceph::mutex lock = ceph::make_mutex("lock");
ceph::condition_variable cond;
const SnapMap *snap_map = nullptr;
std::map<uint64_t, Context *> object_contexts;
uint32_t flags = 0;
ObjectCopyRequest() {
s_instance = this;
}
};
ObjectCopyRequest<librbd::MockTestImageCtx>* ObjectCopyRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
} // namespace deep_copy
namespace object_map {
template <>
struct DiffRequest<MockTestImageCtx> {
BitVector<2>* object_diff_state = nullptr;
Context* on_finish = nullptr;
static DiffRequest* s_instance;
static DiffRequest* create(MockTestImageCtx *image_ctx,
uint64_t snap_id_start, uint64_t snap_id_end,
BitVector<2>* object_diff_state,
Context* on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->object_diff_state = object_diff_state;
s_instance->on_finish = on_finish;
return s_instance;
}
DiffRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
DiffRequest<MockTestImageCtx>* DiffRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace object_map
} // namespace librbd
// template definitions
#include "librbd/deep_copy/ImageCopyRequest.cc"
template class librbd::deep_copy::ImageCopyRequest<librbd::MockTestImageCtx>;
using namespace std::chrono_literals;
namespace librbd {
namespace deep_copy {
using ::testing::_;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
class TestMockDeepCopyImageCopyRequest : public TestMockFixture {
public:
typedef ImageCopyRequest<librbd::MockTestImageCtx> MockImageCopyRequest;
typedef ObjectCopyRequest<librbd::MockTestImageCtx> MockObjectCopyRequest;
typedef object_map::DiffRequest<librbd::MockTestImageCtx> MockDiffRequest;
librbd::ImageCtx *m_src_image_ctx;
librbd::ImageCtx *m_dst_image_ctx;
std::shared_ptr<librbd::AsioEngine> m_asio_engine;
asio::ContextWQ *m_work_queue;
librbd::SnapSeqs m_snap_seqs;
SnapMap m_snap_map;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
librbd::RBD rbd;
std::string dst_image_name = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(rbd, m_ioctx, dst_image_name, m_image_size));
ASSERT_EQ(0, open_image(dst_image_name, &m_dst_image_ctx));
m_asio_engine = std::make_shared<librbd::AsioEngine>(
m_src_image_ctx->md_ctx);
m_work_queue = m_asio_engine->get_work_queue();
}
void expect_get_image_size(librbd::MockTestImageCtx &mock_image_ctx,
uint64_t size) {
EXPECT_CALL(mock_image_ctx, get_image_size(_))
.WillOnce(Return(size)).RetiresOnSaturation();
}
void expect_diff_send(MockDiffRequest& mock_request,
const BitVector<2>& diff_state, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(Invoke([this, &mock_request, diff_state, r]() {
if (r >= 0) {
*mock_request.object_diff_state = diff_state;
}
m_work_queue->queue(mock_request.on_finish, r);
}));
}
void expect_object_copy_send(MockObjectCopyRequest &mock_object_copy_request,
uint32_t flags) {
EXPECT_CALL(mock_object_copy_request, send())
.WillOnce(Invoke([&mock_object_copy_request, flags]() {
ASSERT_EQ(flags, mock_object_copy_request.flags);
}));
}
bool complete_object_copy(MockObjectCopyRequest &mock_object_copy_request,
uint64_t object_num, Context **object_ctx, int r) {
std::unique_lock locker{mock_object_copy_request.lock};
while (mock_object_copy_request.object_contexts.count(object_num) == 0) {
if (mock_object_copy_request.cond.wait_for(locker, 10s) ==
std::cv_status::timeout) {
return false;
}
}
if (object_ctx != nullptr) {
*object_ctx = mock_object_copy_request.object_contexts[object_num];
} else {
m_work_queue->queue(mock_object_copy_request.object_contexts[object_num],
r);
}
return true;
}
SnapMap wait_for_snap_map(MockObjectCopyRequest &mock_object_copy_request) {
std::unique_lock locker{mock_object_copy_request.lock};
while (mock_object_copy_request.snap_map == nullptr) {
if (mock_object_copy_request.cond.wait_for(locker, 10s) ==
std::cv_status::timeout) {
return SnapMap();
}
}
return *mock_object_copy_request.snap_map;
}
int create_snap(librbd::ImageCtx *image_ctx, const char* snap_name,
librados::snap_t *snap_id) {
NoOpProgressContext prog_ctx;
int r = image_ctx->operations->snap_create(
cls::rbd::UserSnapshotNamespace(), snap_name, 0, prog_ctx);
if (r < 0) {
return r;
}
r = image_ctx->state->refresh();
if (r < 0) {
return r;
}
if (image_ctx->snap_ids.count({cls::rbd::UserSnapshotNamespace(),
snap_name}) == 0) {
return -ENOENT;
}
if (snap_id != nullptr) {
*snap_id = image_ctx->snap_ids[{cls::rbd::UserSnapshotNamespace(),
snap_name}];
}
return 0;
}
int create_snap(const char* snap_name,
librados::snap_t *src_snap_id_ = nullptr) {
librados::snap_t src_snap_id;
int r = create_snap(m_src_image_ctx, snap_name, &src_snap_id);
if (r < 0) {
return r;
}
if (src_snap_id_ != nullptr) {
*src_snap_id_ = src_snap_id;
}
librados::snap_t dst_snap_id;
r = create_snap(m_dst_image_ctx, snap_name, &dst_snap_id);
if (r < 0) {
return r;
}
// collection of all existing snaps in dst image
SnapIds dst_snap_ids({dst_snap_id});
if (!m_snap_map.empty()) {
dst_snap_ids.insert(dst_snap_ids.end(),
m_snap_map.rbegin()->second.begin(),
m_snap_map.rbegin()->second.end());
}
m_snap_map[src_snap_id] = dst_snap_ids;
m_snap_seqs[src_snap_id] = dst_snap_id;
return 0;
}
};
TEST_F(TestMockDeepCopyImageCopyRequest, SimpleImage) {
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockObjectCopyRequest mock_object_copy_request;
InSequence seq;
MockDiffRequest mock_diff_request;
expect_diff_send(mock_diff_request, {}, -EINVAL);
expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
expect_get_image_size(mock_src_image_ctx, 0);
expect_object_copy_send(mock_object_copy_request, 0);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, FastDiffNonExistent) {
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
InSequence seq;
MockDiffRequest mock_diff_request;
BitVector<2> diff_state;
diff_state.resize(1);
expect_diff_send(mock_diff_request, diff_state, 0);
expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
expect_get_image_size(mock_src_image_ctx, 0);
expect_op_work_queue(mock_src_image_ctx);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, FastDiffExistsDirty) {
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
InSequence seq;
MockDiffRequest mock_diff_request;
BitVector<2> diff_state;
diff_state.resize(1);
diff_state[0] = object_map::DIFF_STATE_DATA_UPDATED;
expect_diff_send(mock_diff_request, diff_state, 0);
expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
expect_get_image_size(mock_src_image_ctx, 0);
MockObjectCopyRequest mock_object_copy_request;
expect_object_copy_send(mock_object_copy_request, 0);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, FastDiffExistsClean) {
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
InSequence seq;
MockDiffRequest mock_diff_request;
BitVector<2> diff_state;
diff_state.resize(1);
diff_state[0] = object_map::DIFF_STATE_DATA;
expect_diff_send(mock_diff_request, diff_state, 0);
expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
expect_get_image_size(mock_src_image_ctx, 0);
MockObjectCopyRequest mock_object_copy_request;
expect_object_copy_send(mock_object_copy_request,
OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, FastDiffMix) {
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
uint64_t object_count = 12;
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockObjectCopyRequest mock_object_copy_request;
InSequence seq;
MockDiffRequest mock_diff_request;
BitVector<2> diff_state;
diff_state.resize(object_count);
diff_state[1] = object_map::DIFF_STATE_DATA_UPDATED;
diff_state[2] = object_map::DIFF_STATE_DATA_UPDATED;
diff_state[3] = object_map::DIFF_STATE_DATA;
diff_state[5] = object_map::DIFF_STATE_DATA_UPDATED;
diff_state[8] = object_map::DIFF_STATE_DATA;
diff_state[9] = object_map::DIFF_STATE_DATA;
diff_state[10] = object_map::DIFF_STATE_DATA_UPDATED;
expect_diff_send(mock_diff_request, diff_state, 0);
expect_get_image_size(mock_src_image_ctx,
object_count * (1 << m_src_image_ctx->order));
expect_get_image_size(mock_src_image_ctx, 0);
expect_op_work_queue(mock_src_image_ctx);
expect_object_copy_send(mock_object_copy_request, 0);
expect_object_copy_send(mock_object_copy_request, 0);
expect_object_copy_send(mock_object_copy_request,
OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN);
expect_op_work_queue(mock_src_image_ctx);
expect_object_copy_send(mock_object_copy_request, 0);
expect_op_work_queue(mock_src_image_ctx);
expect_object_copy_send(mock_object_copy_request,
OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN);
expect_object_copy_send(mock_object_copy_request,
OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN);
expect_object_copy_send(mock_object_copy_request, 0);
expect_op_work_queue(mock_src_image_ctx);
std::vector<bool> seen(object_count);
struct Handler : public librbd::deep_copy::NoOpHandler {
Handler(std::vector<bool>* seen) : m_seen(seen) {}
int update_progress(uint64_t object_no, uint64_t end_object_no) override {
EXPECT_THAT(object_no, ::testing::AllOf(::testing::Ge(1),
::testing::Le(m_seen->size())));
EXPECT_EQ(end_object_no, m_seen->size());
EXPECT_FALSE((*m_seen)[object_no - 1]);
(*m_seen)[object_no - 1] = true;
return 0;
}
std::vector<bool>* m_seen;
} handler(&seen);
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &handler, &ctx);
request->send();
ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 1, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 2, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 3, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 5, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 8, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 9, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 10, nullptr, 0));
ASSERT_EQ(0, ctx.wait());
EXPECT_THAT(seen, ::testing::Each(::testing::IsTrue()));
}
TEST_F(TestMockDeepCopyImageCopyRequest, OutOfOrder) {
std::string max_ops_str;
ASSERT_EQ(0, _rados.conf_get("rbd_concurrent_management_ops", max_ops_str));
ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops", "10"));
BOOST_SCOPE_EXIT( (max_ops_str) ) {
ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops",
max_ops_str.c_str()));
} BOOST_SCOPE_EXIT_END;
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
uint64_t object_count = 55;
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockObjectCopyRequest mock_object_copy_request;
MockDiffRequest mock_diff_request;
expect_diff_send(mock_diff_request, {}, -EINVAL);
expect_get_image_size(mock_src_image_ctx,
object_count * (1 << m_src_image_ctx->order));
expect_get_image_size(mock_src_image_ctx, 0);
EXPECT_CALL(mock_object_copy_request, send()).Times(object_count);
class Handler : public librbd::deep_copy::NoOpHandler {
public:
uint64_t object_count;
librbd::deep_copy::ObjectNumber expected_object_number;
Handler(uint64_t object_count)
: object_count(object_count) {
}
int update_progress(uint64_t object_no, uint64_t end_object_no) override {
EXPECT_LE(object_no, object_count);
EXPECT_EQ(end_object_no, object_count);
if (!expected_object_number) {
expected_object_number = 0;
} else {
expected_object_number = *expected_object_number + 1;
}
EXPECT_EQ(*expected_object_number, object_no - 1);
return 0;
}
} handler(object_count);
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &handler, &ctx);
request->send();
std::map<uint64_t, Context*> copy_contexts;
ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
for (uint64_t i = 0; i < object_count; ++i) {
if (i % 10 == 0) {
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, i,
©_contexts[i], 0));
} else {
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, i, nullptr,
0));
}
}
for (auto& pair : copy_contexts) {
pair.second->complete(0);
}
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, SnapshotSubset) {
librados::snap_t snap_id_start;
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("snap1"));
ASSERT_EQ(0, create_snap("snap2", &snap_id_start));
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockObjectCopyRequest mock_object_copy_request;
InSequence seq;
MockDiffRequest mock_diff_request;
expect_diff_send(mock_diff_request, {}, -EINVAL);
expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
expect_get_image_size(mock_src_image_ctx, 0);
expect_get_image_size(mock_src_image_ctx, 0);
expect_get_image_size(mock_src_image_ctx, 0);
expect_object_copy_send(mock_object_copy_request, 0);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
snap_id_start, snap_id_end, 0, false,
boost::none, m_snap_seqs, &no_op,
&ctx);
request->send();
SnapMap snap_map(m_snap_map);
snap_map.erase(snap_map.begin());
ASSERT_EQ(snap_map, wait_for_snap_map(mock_object_copy_request));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, RestartPartialSync) {
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockObjectCopyRequest mock_object_copy_request;
InSequence seq;
MockDiffRequest mock_diff_request;
expect_diff_send(mock_diff_request, {}, -EINVAL);
expect_get_image_size(mock_src_image_ctx, 2 * (1 << m_src_image_ctx->order));
expect_get_image_size(mock_src_image_ctx, 0);
expect_object_copy_send(mock_object_copy_request, 0);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false,
librbd::deep_copy::ObjectNumber{0U},
m_snap_seqs, &no_op, &ctx);
request->send();
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 1, nullptr, 0));
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, Cancel) {
std::string max_ops_str;
ASSERT_EQ(0, _rados.conf_get("rbd_concurrent_management_ops", max_ops_str));
ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops", "1"));
BOOST_SCOPE_EXIT( (max_ops_str) ) {
ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops",
max_ops_str.c_str()));
} BOOST_SCOPE_EXIT_END;
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockObjectCopyRequest mock_object_copy_request;
InSequence seq;
MockDiffRequest mock_diff_request;
expect_diff_send(mock_diff_request, {}, -EINVAL);
expect_get_image_size(mock_src_image_ctx, 1 << m_src_image_ctx->order);
expect_get_image_size(mock_src_image_ctx, 0);
expect_object_copy_send(mock_object_copy_request, 0);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
request->cancel();
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
ASSERT_EQ(-ECANCELED, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, Cancel_Inflight_Sync) {
std::string max_ops_str;
ASSERT_EQ(0, _rados.conf_get("rbd_concurrent_management_ops", max_ops_str));
ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops", "3"));
BOOST_SCOPE_EXIT( (max_ops_str) ) {
ASSERT_EQ(0, _rados.conf_set("rbd_concurrent_management_ops",
max_ops_str.c_str()));
} BOOST_SCOPE_EXIT_END;
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockObjectCopyRequest mock_object_copy_request;
InSequence seq;
MockDiffRequest mock_diff_request;
expect_diff_send(mock_diff_request, {}, -EINVAL);
expect_get_image_size(mock_src_image_ctx, 6 * (1 << m_src_image_ctx->order));
expect_get_image_size(mock_src_image_ctx, m_image_size);
EXPECT_CALL(mock_object_copy_request, send()).Times(6);
struct Handler : public librbd::deep_copy::NoOpHandler {
librbd::deep_copy::ObjectNumber object_number;
int update_progress(uint64_t object_no, uint64_t end_object_no) override {
object_number = object_number ? *object_number + 1 : 0;
return 0;
}
} handler;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &handler, &ctx);
request->send();
ASSERT_EQ(m_snap_map, wait_for_snap_map(mock_object_copy_request));
Context *cancel_ctx = nullptr;
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 0, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 1, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 2, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 3, &cancel_ctx,
0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 4, nullptr, 0));
ASSERT_TRUE(complete_object_copy(mock_object_copy_request, 5, nullptr, 0));
request->cancel();
cancel_ctx->complete(0);
ASSERT_EQ(-ECANCELED, ctx.wait());
ASSERT_EQ(5u, handler.object_number.get());
}
TEST_F(TestMockDeepCopyImageCopyRequest, CancelBeforeSend) {
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
InSequence seq;
MockDiffRequest mock_diff_request;
expect_diff_send(mock_diff_request, {}, -EINVAL);
expect_get_image_size(mock_src_image_ctx, 2 * (1 << m_src_image_ctx->order));
expect_get_image_size(mock_src_image_ctx, 0);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, snap_id_end, 0, false, boost::none,
m_snap_seqs, &no_op, &ctx);
request->cancel();
request->send();
ASSERT_EQ(-ECANCELED, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, MissingSnap) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
0, 123, 0, false, boost::none,
m_snap_seqs, &no_op, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, MissingFromSnap) {
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
123, snap_id_end, 0, false,
boost::none, m_snap_seqs, &no_op,
&ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, EmptySnapMap) {
librados::snap_t snap_id_start;
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("snap1", &snap_id_start));
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
snap_id_start, snap_id_end, 0, false,
boost::none, {{0, 0}}, &no_op, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyImageCopyRequest, EmptySnapSeqs) {
librados::snap_t snap_id_start;
librados::snap_t snap_id_end;
ASSERT_EQ(0, create_snap("snap1", &snap_id_start));
ASSERT_EQ(0, create_snap("copy", &snap_id_end));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::deep_copy::NoOpHandler no_op;
C_SaferCond ctx;
auto request = new MockImageCopyRequest(&mock_src_image_ctx,
&mock_dst_image_ctx,
snap_id_start, snap_id_end, 0, false,
boost::none, {}, &no_op, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
} // namespace deep_copy
} // namespace librbd
| 29,313 | 35.279703 | 111 |
cc
|
null |
ceph-main/src/test/librbd/deep_copy/test_mock_MetadataCopyRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "include/rbd/librbd.hpp"
#include "include/stringify.h"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/deep_copy/MetadataCopyRequest.h"
#include "librbd/image/GetMetadataRequest.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/test_support.h"
#include <map>
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace image {
template <>
struct GetMetadataRequest<MockTestImageCtx> {
std::map<std::string, bufferlist>* pairs = nullptr;
Context* on_finish = nullptr;
static GetMetadataRequest* s_instance;
static GetMetadataRequest* create(librados::IoCtx&,
const std::string& oid,
bool filter_internal,
const std::string& filter_key_prefix,
const std::string& last_key,
uint32_t max_results,
std::map<std::string, bufferlist>* pairs,
Context* on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->pairs = pairs;
s_instance->on_finish = on_finish;
return s_instance;
}
GetMetadataRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
GetMetadataRequest<MockTestImageCtx>* GetMetadataRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace image
} // namespace librbd
// template definitions
#include "librbd/deep_copy/MetadataCopyRequest.cc"
namespace librbd {
namespace deep_copy {
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMockDeepCopyMetadataCopyRequest : public TestMockFixture {
public:
typedef MetadataCopyRequest<librbd::MockTestImageCtx> MockMetadataCopyRequest;
typedef image::GetMetadataRequest<MockTestImageCtx> MockGetMetadataRequest;
typedef std::map<std::string, bufferlist> Metadata;
librbd::ImageCtx *m_src_image_ctx;
librbd::ImageCtx *m_dst_image_ctx;
std::shared_ptr<librbd::AsioEngine> m_asio_engine;
asio::ContextWQ *m_work_queue;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
librbd::RBD rbd;
std::string dst_image_name = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(rbd, m_ioctx, dst_image_name, m_image_size));
ASSERT_EQ(0, open_image(dst_image_name, &m_dst_image_ctx));
m_asio_engine = std::make_shared<librbd::AsioEngine>(
m_src_image_ctx->md_ctx);
m_work_queue = m_asio_engine->get_work_queue();
}
void expect_get_metadata(MockGetMetadataRequest& mock_request,
const Metadata& metadata, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(Invoke([this, &mock_request, metadata, r]() {
*mock_request.pairs = metadata;
m_work_queue->queue(mock_request.on_finish, r);
}));
}
void expect_metadata_set(librbd::MockTestImageCtx &mock_image_ctx,
const Metadata& metadata, int r) {
bufferlist in_bl;
encode(metadata, in_bl);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("metadata_set"), ContentsEqual(in_bl), _, _, _))
.WillOnce(Return(r));
}
};
TEST_F(TestMockDeepCopyMetadataCopyRequest, Success) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
size_t idx = 1;
Metadata key_values_1;
for (; idx <= 128; ++idx) {
bufferlist bl;
bl.append("value" + stringify(idx));
key_values_1.emplace("key" + stringify(idx), bl);
}
Metadata key_values_2;
for (; idx <= 255; ++idx) {
bufferlist bl;
bl.append("value" + stringify(idx));
key_values_2.emplace("key" + stringify(idx), bl);
}
InSequence seq;
MockGetMetadataRequest mock_request;
expect_get_metadata(mock_request, key_values_1, 0);
expect_metadata_set(mock_dst_image_ctx, key_values_1, 0);
expect_get_metadata(mock_request, key_values_2, 0);
expect_metadata_set(mock_dst_image_ctx, key_values_2, 0);
C_SaferCond ctx;
auto request = MockMetadataCopyRequest::create(&mock_src_image_ctx,
&mock_dst_image_ctx,
&ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyMetadataCopyRequest, Empty) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
Metadata key_values;
InSequence seq;
MockGetMetadataRequest mock_request;
expect_get_metadata(mock_request, key_values, 0);
C_SaferCond ctx;
auto request = MockMetadataCopyRequest::create(&mock_src_image_ctx,
&mock_dst_image_ctx,
&ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopyMetadataCopyRequest, MetadataListError) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
Metadata key_values;
InSequence seq;
MockGetMetadataRequest mock_request;
expect_get_metadata(mock_request, key_values, -EINVAL);
C_SaferCond ctx;
auto request = MockMetadataCopyRequest::create(&mock_src_image_ctx,
&mock_dst_image_ctx,
&ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyMetadataCopyRequest, MetadataSetError) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
Metadata key_values;
bufferlist bl;
bl.append("value");
key_values.emplace("key", bl);
InSequence seq;
MockGetMetadataRequest mock_request;
expect_get_metadata(mock_request, key_values, 0);
expect_metadata_set(mock_dst_image_ctx, key_values, -EINVAL);
C_SaferCond ctx;
auto request = MockMetadataCopyRequest::create(&mock_src_image_ctx,
&mock_dst_image_ctx,
&ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
} // namespace deep_sync
} // namespace librbd
| 6,885 | 30.158371 | 97 |
cc
|
null |
ceph-main/src/test/librbd/deep_copy/test_mock_ObjectCopyRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "include/interval_set.h"
#include "include/neorados/RADOS.hpp"
#include "include/rbd/librbd.hpp"
#include "include/rbd/object_map_types.h"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/Operations.h"
#include "librbd/api/Image.h"
#include "librbd/api/Io.h"
#include "librbd/deep_copy/ObjectCopyRequest.h"
#include "librbd/deep_copy/Utils.h"
#include "librbd/io/ReadResult.h"
#include "librbd/io/Utils.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/test_support.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
MockTestImageCtx *parent = nullptr;
};
} // anonymous namespace
namespace util {
inline ImageCtx* get_image_ctx(MockTestImageCtx* image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
namespace io {
namespace util {
template <>
void area_to_object_extents(MockTestImageCtx* image_ctx, uint64_t offset,
uint64_t length, ImageArea area,
uint64_t buffer_offset,
striper::LightweightObjectExtents* object_extents) {
Striper::file_to_extents(image_ctx->cct, &image_ctx->layout, offset, length,
0, buffer_offset, object_extents);
}
template <>
std::pair<Extents, ImageArea> object_to_area_extents(
MockTestImageCtx* image_ctx, uint64_t object_no,
const Extents& object_extents) {
Extents extents;
for (auto [off, len] : object_extents) {
Striper::extent_to_file(image_ctx->cct, &image_ctx->layout, object_no, off,
len, extents);
}
return {std::move(extents), ImageArea::DATA};
}
} // namespace util
} // namespace io
} // namespace librbd
// template definitions
#include "librbd/deep_copy/ObjectCopyRequest.cc"
template class librbd::deep_copy::ObjectCopyRequest<librbd::MockTestImageCtx>;
static bool operator==(const SnapContext& rhs, const SnapContext& lhs) {
return (rhs.seq == lhs.seq && rhs.snaps == lhs.snaps);
}
namespace librbd {
namespace deep_copy {
using ::testing::_;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::WithArg;
namespace {
void scribble(librbd::ImageCtx *image_ctx, int num_ops, size_t max_size,
interval_set<uint64_t> *what)
{
uint64_t object_size = 1 << image_ctx->order;
for (int i = 0; i < num_ops; i++) {
uint64_t off = rand() % (object_size - max_size + 1);
uint64_t len = 1 + rand() % max_size;
std::cout << __func__ << ": off=" << off << ", len=" << len << std::endl;
bufferlist bl;
bl.append(std::string(len, '1'));
int r = api::Io<>::write(*image_ctx, off, len, std::move(bl), 0);
ASSERT_EQ(static_cast<int>(len), r);
interval_set<uint64_t> w;
w.insert(off, len);
what->union_of(w);
}
std::cout << " wrote " << *what << std::endl;
}
} // anonymous namespace
MATCHER(IsListSnaps, "") {
auto req = boost::get<io::ImageDispatchSpec::ListSnaps>(&arg->request);
return (req != nullptr);
}
MATCHER_P2(IsRead, snap_id, image_interval, "") {
auto req = boost::get<io::ImageDispatchSpec::Read>(&arg->request);
if (req == nullptr ||
arg->io_context->read_snap().value_or(CEPH_NOSNAP) != snap_id) {
return false;
}
// ensure the read request encloses the full snapshot delta
interval_set<uint64_t> expected_interval(image_interval);
interval_set<uint64_t> read_interval;
for (auto &image_extent : arg->image_extents) {
read_interval.insert(image_extent.first, image_extent.second);
}
interval_set<uint64_t> intersection;
intersection.intersection_of(expected_interval, read_interval);
expected_interval.subtract(intersection);
return expected_interval.empty();
}
class TestMockDeepCopyObjectCopyRequest : public TestMockFixture {
public:
typedef ObjectCopyRequest<librbd::MockTestImageCtx> MockObjectCopyRequest;
librbd::ImageCtx *m_src_image_ctx;
librbd::ImageCtx *m_dst_image_ctx;
std::shared_ptr<librbd::AsioEngine> m_asio_engine;
asio::ContextWQ *m_work_queue;
SnapMap m_snap_map;
SnapSeqs m_snap_seqs;
std::vector<librados::snap_t> m_src_snap_ids;
std::vector<librados::snap_t> m_dst_snap_ids;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
librbd::NoOpProgressContext no_op;
m_image_size = 1 << m_src_image_ctx->order;
ASSERT_EQ(0, m_src_image_ctx->operations->resize(m_image_size, true, no_op));
librbd::RBD rbd;
std::string dst_image_name = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(rbd, m_ioctx, dst_image_name, m_image_size));
ASSERT_EQ(0, open_image(dst_image_name, &m_dst_image_ctx));
m_asio_engine = std::make_shared<librbd::AsioEngine>(
m_src_image_ctx->md_ctx);
m_work_queue = m_asio_engine->get_work_queue();
}
bool is_fast_diff(librbd::MockImageCtx &mock_image_ctx) {
return (mock_image_ctx.features & RBD_FEATURE_FAST_DIFF) != 0;
}
void prepare_exclusive_lock(librbd::MockImageCtx &mock_image_ctx,
librbd::MockExclusiveLock &mock_exclusive_lock) {
if ((mock_image_ctx.features & RBD_FEATURE_EXCLUSIVE_LOCK) == 0) {
return;
}
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
}
void expect_get_object_count(librbd::MockImageCtx& mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, get_object_count(_))
.WillRepeatedly(Invoke([&mock_image_ctx](librados::snap_t snap_id) {
return mock_image_ctx.image_ctx->get_object_count(snap_id);
}));
}
void expect_test_features(librbd::MockImageCtx &mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, test_features(_))
.WillRepeatedly(WithArg<0>(Invoke([&mock_image_ctx](uint64_t features) {
return (mock_image_ctx.features & features) != 0;
})));
}
void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
if ((m_src_image_ctx->features & RBD_FEATURE_EXCLUSIVE_LOCK) == 0) {
return;
}
EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
}
void expect_list_snaps(librbd::MockTestImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, send(IsListSnaps()))
.WillOnce(Invoke(
[&mock_image_ctx, r](io::ImageDispatchSpec* spec) {
if (r < 0) {
spec->fail(r);
return;
}
spec->image_dispatcher =
mock_image_ctx.image_ctx->io_image_dispatcher;
mock_image_ctx.image_ctx->io_image_dispatcher->send(spec);
}));
}
void expect_get_object_name(librbd::MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, get_object_name(0))
.WillOnce(Return(mock_image_ctx.image_ctx->get_object_name(0)));
}
MockObjectCopyRequest *create_request(
librbd::MockTestImageCtx &mock_src_image_ctx,
librbd::MockTestImageCtx &mock_dst_image_ctx,
librados::snap_t src_snap_id_start,
librados::snap_t src_snap_id_end,
librados::snap_t dst_snap_id_start,
uint32_t flags, Context *on_finish) {
SnapMap snap_map;
util::compute_snap_map(mock_dst_image_ctx.cct, src_snap_id_start,
src_snap_id_end, m_dst_snap_ids, m_snap_seqs,
&snap_map);
expect_get_object_name(mock_dst_image_ctx);
return new MockObjectCopyRequest(&mock_src_image_ctx, &mock_dst_image_ctx,
src_snap_id_start, dst_snap_id_start,
snap_map, 0, flags, nullptr, on_finish);
}
void expect_read(librbd::MockTestImageCtx& mock_image_ctx,
uint64_t snap_id, uint64_t offset, uint64_t length, int r) {
interval_set<uint64_t> extents;
extents.insert(offset, length);
expect_read(mock_image_ctx, snap_id, extents, r);
}
void expect_read(librbd::MockTestImageCtx& mock_image_ctx, uint64_t snap_id,
const interval_set<uint64_t> &extents, int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher,
send(IsRead(snap_id, extents)))
.WillOnce(Invoke(
[&mock_image_ctx, r](io::ImageDispatchSpec* spec) {
if (r < 0) {
spec->fail(r);
return;
}
spec->image_dispatcher =
mock_image_ctx.image_ctx->io_image_dispatcher;
mock_image_ctx.image_ctx->io_image_dispatcher->send(spec);
}));
}
void expect_write(librados::MockTestMemIoCtxImpl &mock_io_ctx,
uint64_t offset, uint64_t length,
const SnapContext &snapc, int r) {
auto &expect = EXPECT_CALL(mock_io_ctx, write(_, _, length, offset, snapc));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_write(librados::MockTestMemIoCtxImpl &mock_io_ctx,
const interval_set<uint64_t> &extents,
const SnapContext &snapc, int r) {
for (auto extent : extents) {
expect_write(mock_io_ctx, extent.first, extent.second, snapc, r);
if (r < 0) {
break;
}
}
}
void expect_truncate(librados::MockTestMemIoCtxImpl &mock_io_ctx,
uint64_t offset, int r) {
auto &expect = EXPECT_CALL(mock_io_ctx, truncate(_, offset, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_remove(librados::MockTestMemIoCtxImpl &mock_io_ctx, int r) {
auto &expect = EXPECT_CALL(mock_io_ctx, remove(_, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_update_object_map(librbd::MockTestImageCtx &mock_image_ctx,
librbd::MockObjectMap &mock_object_map,
librados::snap_t snap_id, uint8_t state,
int r) {
if (mock_image_ctx.image_ctx->object_map != nullptr) {
auto &expect = EXPECT_CALL(mock_object_map, aio_update(snap_id, 0, 1, state, _, _, false, _));
if (r < 0) {
expect.WillOnce(DoAll(WithArg<7>(Invoke([this, r](Context *ctx) {
m_work_queue->queue(ctx, r);
})),
Return(true)));
} else {
expect.WillOnce(DoAll(WithArg<7>(Invoke([&mock_image_ctx, snap_id, state](Context *ctx) {
ceph_assert(ceph_mutex_is_locked(mock_image_ctx.image_ctx->image_lock));
mock_image_ctx.image_ctx->object_map->aio_update<Context>(
snap_id, 0, 1, state, boost::none, {}, false, ctx);
})),
Return(true)));
}
}
}
void expect_prepare_copyup(MockTestImageCtx& mock_image_ctx, int r = 0) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher,
prepare_copyup(_, _)).WillOnce(Return(r));
}
int create_snap(librbd::ImageCtx *image_ctx, const char* snap_name,
librados::snap_t *snap_id) {
NoOpProgressContext prog_ctx;
int r = image_ctx->operations->snap_create(
cls::rbd::UserSnapshotNamespace(), snap_name, 0, prog_ctx);
if (r < 0) {
return r;
}
r = image_ctx->state->refresh();
if (r < 0) {
return r;
}
if (image_ctx->snap_ids.count({cls::rbd::UserSnapshotNamespace(),
snap_name}) == 0) {
return -ENOENT;
}
if (snap_id != nullptr) {
*snap_id = image_ctx->snap_ids[{cls::rbd::UserSnapshotNamespace(),
snap_name}];
}
return 0;
}
int create_snap(const char* snap_name) {
librados::snap_t src_snap_id;
int r = create_snap(m_src_image_ctx, snap_name, &src_snap_id);
if (r < 0) {
return r;
}
librados::snap_t dst_snap_id;
r = create_snap(m_dst_image_ctx, snap_name, &dst_snap_id);
if (r < 0) {
return r;
}
// collection of all existing snaps in dst image
SnapIds dst_snap_ids({dst_snap_id});
if (!m_snap_map.empty()) {
dst_snap_ids.insert(dst_snap_ids.end(),
m_snap_map.rbegin()->second.begin(),
m_snap_map.rbegin()->second.end());
}
m_snap_map[src_snap_id] = dst_snap_ids;
m_snap_seqs[src_snap_id] = dst_snap_id;
m_src_snap_ids.push_back(src_snap_id);
m_dst_snap_ids.push_back(dst_snap_id);
return 0;
}
std::string get_snap_name(librbd::ImageCtx *image_ctx,
librados::snap_t snap_id) {
auto it = std::find_if(image_ctx->snap_ids.begin(),
image_ctx->snap_ids.end(),
[snap_id](const std::pair<std::pair<cls::rbd::SnapshotNamespace,
std::string>,
librados::snap_t> &pair) {
return (pair.second == snap_id);
});
if (it == image_ctx->snap_ids.end()) {
return "";
}
return it->first.second;
}
int copy_objects() {
int r;
uint64_t object_size = 1 << m_src_image_ctx->order;
bufferlist bl;
bl.append(std::string(object_size, '1'));
r = api::Io<>::read(*m_src_image_ctx, 0, object_size,
librbd::io::ReadResult{&bl}, 0);
if (r < 0) {
return r;
}
r = api::Io<>::write(*m_dst_image_ctx, 0, object_size, std::move(bl), 0);
if (r < 0) {
return r;
}
return 0;
}
int compare_objects() {
SnapMap snap_map(m_snap_map);
if (snap_map.empty()) {
return -ENOENT;
}
int r;
uint64_t object_size = 1 << m_src_image_ctx->order;
while (!snap_map.empty()) {
librados::snap_t src_snap_id = snap_map.begin()->first;
librados::snap_t dst_snap_id = *snap_map.begin()->second.begin();
snap_map.erase(snap_map.begin());
std::string snap_name = get_snap_name(m_src_image_ctx, src_snap_id);
if (snap_name.empty()) {
return -ENOENT;
}
std::cout << "comparing '" << snap_name << " (" << src_snap_id
<< " to " << dst_snap_id << ")" << std::endl;
r = librbd::api::Image<>::snap_set(m_src_image_ctx,
cls::rbd::UserSnapshotNamespace(),
snap_name.c_str());
if (r < 0) {
return r;
}
r = librbd::api::Image<>::snap_set(m_dst_image_ctx,
cls::rbd::UserSnapshotNamespace(),
snap_name.c_str());
if (r < 0) {
return r;
}
bufferlist src_bl;
src_bl.append(std::string(object_size, '1'));
r = api::Io<>::read(
*m_src_image_ctx, 0, object_size, librbd::io::ReadResult{&src_bl}, 0);
if (r < 0) {
return r;
}
bufferlist dst_bl;
dst_bl.append(std::string(object_size, '1'));
r = api::Io<>::read(
*m_dst_image_ctx, 0, object_size, librbd::io::ReadResult{&dst_bl}, 0);
if (r < 0) {
return r;
}
if (!src_bl.contents_equal(dst_bl)) {
std::cout << "src block: " << std::endl; src_bl.hexdump(std::cout);
std::cout << "dst block: " << std::endl; dst_bl.hexdump(std::cout);
return -EBADMSG;
}
}
r = librbd::api::Image<>::snap_set(m_src_image_ctx,
cls::rbd::UserSnapshotNamespace(),
nullptr);
if (r < 0) {
return r;
}
r = librbd::api::Image<>::snap_set(m_dst_image_ctx,
cls::rbd::UserSnapshotNamespace(),
nullptr);
if (r < 0) {
return r;
}
return 0;
}
};
TEST_F(TestMockDeepCopyObjectCopyRequest, DNE) {
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
InSequence seq;
expect_list_snaps(mock_src_image_ctx, -ENOENT);
request->send();
ASSERT_EQ(-ENOENT, ctx.wait());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, Write) {
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
request->get_dst_io_ctx()));
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[0], OBJECT_EXISTS, 0);
expect_prepare_copyup(mock_dst_image_ctx);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
request->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(0, compare_objects());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, ReadError) {
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(),
-EINVAL);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, WriteError) {
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
request->get_dst_io_ctx()));
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[0], OBJECT_EXISTS, 0);
expect_prepare_copyup(mock_dst_image_ctx);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, -EINVAL);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, WriteSnaps) {
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("one"));
interval_set<uint64_t> two;
scribble(m_src_image_ctx, 10, 102400, &two);
ASSERT_EQ(0, create_snap("two"));
if (one.range_end() < two.range_end()) {
interval_set<uint64_t> resize_diff;
resize_diff.insert(one.range_end(), two.range_end() - one.range_end());
two.union_of(resize_diff);
}
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
request->get_dst_io_ctx()));
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[2], two, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[0], OBJECT_EXISTS, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[1], OBJECT_EXISTS, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[2], is_fast_diff(mock_dst_image_ctx) ?
OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
expect_prepare_copyup(mock_dst_image_ctx);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, two,
{m_dst_snap_ids[0], {m_dst_snap_ids[0]}}, 0);
request->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(0, compare_objects());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, Trim) {
ASSERT_EQ(0, m_src_image_ctx->operations->metadata_set(
"conf_rbd_skip_partial_discard", "false"));
m_src_image_ctx->discard_granularity_bytes = 0;
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("one"));
// trim the object
uint64_t trim_offset = rand() % one.range_end();
ASSERT_LE(0, api::Io<>::discard(
*m_src_image_ctx, trim_offset, one.range_end() - trim_offset,
m_src_image_ctx->discard_granularity_bytes));
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
request->get_dst_io_ctx()));
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[0], OBJECT_EXISTS, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[1], OBJECT_EXISTS, 0);
expect_prepare_copyup(mock_dst_image_ctx);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
expect_start_op(mock_exclusive_lock);
expect_truncate(mock_dst_io_ctx, trim_offset, 0);
request->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(0, compare_objects());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, Remove) {
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("one"));
ASSERT_EQ(0, create_snap("two"));
// remove the object
uint64_t object_size = 1 << m_src_image_ctx->order;
ASSERT_LE(0, api::Io<>::discard(
*m_src_image_ctx, 0, object_size,
m_src_image_ctx->discard_granularity_bytes));
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
request->get_dst_io_ctx()));
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[1], 0, one.range_end(), 0);
expect_start_op(mock_exclusive_lock);
uint8_t state = OBJECT_EXISTS;
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[0], state, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[1], is_fast_diff(mock_dst_image_ctx) ?
OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
expect_prepare_copyup(mock_dst_image_ctx);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
expect_start_op(mock_exclusive_lock);
expect_remove(mock_dst_io_ctx, 0);
request->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(0, compare_objects());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, ObjectMapUpdateError) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[0], OBJECT_EXISTS, -EBLOCKLISTED);
request->send();
ASSERT_EQ(-EBLOCKLISTED, ctx.wait());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, PrepareCopyupError) {
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("copy"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, 0, &ctx);
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[0], OBJECT_EXISTS, 0);
expect_prepare_copyup(mock_dst_image_ctx, -EIO);
request->send();
ASSERT_EQ(-EIO, ctx.wait());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, WriteSnapsStart) {
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, copy_objects());
ASSERT_EQ(0, create_snap("one"));
auto src_snap_id_start = m_src_image_ctx->snaps[0];
auto dst_snap_id_start = m_dst_image_ctx->snaps[0];
interval_set<uint64_t> two;
scribble(m_src_image_ctx, 10, 102400, &two);
ASSERT_EQ(0, create_snap("two"));
interval_set<uint64_t> three;
scribble(m_src_image_ctx, 10, 102400, &three);
ASSERT_EQ(0, create_snap("three"));
auto max_extent = one.range_end();
if (max_extent < two.range_end()) {
interval_set<uint64_t> resize_diff;
resize_diff.insert(max_extent, two.range_end() - max_extent);
two.union_of(resize_diff);
}
max_extent = std::max(max_extent, two.range_end());
if (max_extent < three.range_end()) {
interval_set<uint64_t> resize_diff;
resize_diff.insert(max_extent, three.range_end() - max_extent);
three.union_of(resize_diff);
}
interval_set<uint64_t> four;
scribble(m_src_image_ctx, 10, 102400, &four);
// map should begin after src start and src end's dst snap seqs should
// point to HEAD revision
m_snap_seqs.rbegin()->second = CEPH_NOSNAP;
m_dst_snap_ids.pop_back();
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
C_SaferCond ctx;
MockObjectCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx,
src_snap_id_start,
CEPH_NOSNAP,
dst_snap_id_start,
0, &ctx);
librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
request->get_dst_io_ctx()));
InSequence seq;
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[1], two, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[2], three, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[1], OBJECT_EXISTS, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
CEPH_NOSNAP, OBJECT_EXISTS, 0);
expect_prepare_copyup(mock_dst_image_ctx);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, two,
{m_dst_snap_ids[0], {m_dst_snap_ids[0]}}, 0);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, three,
{m_dst_snap_ids[1], {m_dst_snap_ids[1], m_dst_snap_ids[0]}}, 0);
request->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(0, compare_objects());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, Incremental) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
// scribble some data
interval_set<uint64_t> one;
scribble(m_src_image_ctx, 10, 102400, &one);
ASSERT_EQ(0, create_snap("snap1"));
mock_dst_image_ctx.snaps = m_dst_image_ctx->snaps;
InSequence seq;
C_SaferCond ctx1;
auto request1 = create_request(mock_src_image_ctx, mock_dst_image_ctx,
0, m_src_snap_ids[0], 0, 0, &ctx1);
expect_list_snaps(mock_src_image_ctx, 0);
expect_read(mock_src_image_ctx, m_src_snap_ids[0], 0, one.range_end(), 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[0], OBJECT_EXISTS, 0);
librados::MockTestMemIoCtxImpl &mock_dst_io_ctx(get_mock_io_ctx(
request1->get_dst_io_ctx()));
expect_prepare_copyup(mock_dst_image_ctx);
expect_start_op(mock_exclusive_lock);
expect_write(mock_dst_io_ctx, 0, one.range_end(), {0, {}}, 0);
request1->send();
ASSERT_EQ(0, ctx1.wait());
// clean (no-updates) snapshots
ASSERT_EQ(0, create_snap("snap2"));
ASSERT_EQ(0, create_snap("snap3"));
mock_dst_image_ctx.snaps = m_dst_image_ctx->snaps;
C_SaferCond ctx2;
auto request2 = create_request(mock_src_image_ctx, mock_dst_image_ctx,
m_src_snap_ids[0], m_src_snap_ids[2],
m_dst_snap_ids[0], 0, &ctx2);
expect_list_snaps(mock_src_image_ctx, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[1],
is_fast_diff(mock_dst_image_ctx) ?
OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[2],
is_fast_diff(mock_dst_image_ctx) ?
OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
request2->send();
ASSERT_EQ(0, ctx2.wait());
ASSERT_EQ(0, compare_objects());
}
TEST_F(TestMockDeepCopyObjectCopyRequest, SkipSnapList) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
librbd::MockObjectMap mock_object_map;
mock_dst_image_ctx.object_map = &mock_object_map;
expect_op_work_queue(mock_src_image_ctx);
expect_test_features(mock_dst_image_ctx);
expect_get_object_count(mock_dst_image_ctx);
ASSERT_EQ(0, create_snap("snap1"));
mock_dst_image_ctx.snaps = m_dst_image_ctx->snaps;
InSequence seq;
// clean (no-updates) snapshots
ASSERT_EQ(0, create_snap("snap2"));
mock_dst_image_ctx.snaps = m_dst_image_ctx->snaps;
C_SaferCond ctx;
auto request = create_request(mock_src_image_ctx, mock_dst_image_ctx,
m_src_snap_ids[0], m_src_snap_ids[1],
m_dst_snap_ids[0],
OBJECT_COPY_REQUEST_FLAG_EXISTS_CLEAN, &ctx);
expect_start_op(mock_exclusive_lock);
expect_update_object_map(mock_dst_image_ctx, mock_object_map,
m_dst_snap_ids[1],
is_fast_diff(mock_dst_image_ctx) ?
OBJECT_EXISTS_CLEAN : OBJECT_EXISTS, 0);
request->send();
ASSERT_EQ(0, ctx.wait());
}
} // namespace deep_copy
} // namespace librbd
| 38,242 | 33.48422 | 106 |
cc
|
null |
ceph-main/src/test/librbd/deep_copy/test_mock_SetHeadRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librados_test_stub/LibradosTestStub.h"
#include "include/rbd/librbd.hpp"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "osdc/Striper.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "librbd/deep_copy/SetHeadRequest.h"
#include "librbd/image/AttachParentRequest.h"
#include "librbd/image/DetachParentRequest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace image {
template <>
struct AttachParentRequest<MockTestImageCtx> {
Context* on_finish = nullptr;
static AttachParentRequest* s_instance;
static AttachParentRequest* create(MockTestImageCtx&,
const cls::rbd::ParentImageSpec& pspec,
uint64_t parent_overlap, bool reattach,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
AttachParentRequest() {
s_instance = this;
}
};
AttachParentRequest<MockTestImageCtx>* AttachParentRequest<MockTestImageCtx>::s_instance = nullptr;
template <>
class DetachParentRequest<MockTestImageCtx> {
public:
static DetachParentRequest *s_instance;
static DetachParentRequest *create(MockTestImageCtx &image_ctx,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
Context *on_finish = nullptr;
DetachParentRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
DetachParentRequest<MockTestImageCtx> *DetachParentRequest<MockTestImageCtx>::s_instance;
} // namespace image
} // namespace librbd
// template definitions
#include "librbd/deep_copy/SetHeadRequest.cc"
template class librbd::deep_copy::SetHeadRequest<librbd::MockTestImageCtx>;
namespace librbd {
namespace deep_copy {
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMockDeepCopySetHeadRequest : public TestMockFixture {
public:
typedef SetHeadRequest<librbd::MockTestImageCtx> MockSetHeadRequest;
typedef image::AttachParentRequest<MockTestImageCtx> MockAttachParentRequest;
typedef image::DetachParentRequest<MockTestImageCtx> MockDetachParentRequest;
librbd::ImageCtx *m_image_ctx;
std::shared_ptr<librbd::AsioEngine> m_asio_engine;
asio::ContextWQ *m_work_queue;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
m_asio_engine = std::make_shared<librbd::AsioEngine>(
m_image_ctx->md_ctx);
m_work_queue = m_asio_engine->get_work_queue();
}
void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
}
void expect_test_features(librbd::MockTestImageCtx &mock_image_ctx,
uint64_t features, bool enabled) {
EXPECT_CALL(mock_image_ctx, test_features(features))
.WillOnce(Return(enabled));
}
void expect_set_size(librbd::MockTestImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("set_size"), _, _, _, _))
.WillOnce(Return(r));
}
void expect_detach_parent(MockImageCtx &mock_image_ctx,
MockDetachParentRequest& mock_request, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(FinishRequest(&mock_request, r, &mock_image_ctx));
}
void expect_attach_parent(MockImageCtx &mock_image_ctx,
MockAttachParentRequest& mock_request, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(FinishRequest(&mock_request, r, &mock_image_ctx));
}
MockSetHeadRequest *create_request(
librbd::MockTestImageCtx &mock_local_image_ctx, uint64_t size,
const cls::rbd::ParentImageSpec &parent_spec, uint64_t parent_overlap,
Context *on_finish) {
return new MockSetHeadRequest(&mock_local_image_ctx, size, parent_spec,
parent_overlap, on_finish);
}
};
TEST_F(TestMockDeepCopySetHeadRequest, Resize) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
InSequence seq;
expect_start_op(mock_exclusive_lock);
expect_set_size(mock_image_ctx, 0);
C_SaferCond ctx;
auto request = create_request(mock_image_ctx, 123, {}, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopySetHeadRequest, ResizeError) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
InSequence seq;
expect_start_op(mock_exclusive_lock);
expect_set_size(mock_image_ctx, -EINVAL);
C_SaferCond ctx;
auto request = create_request(mock_image_ctx, 123, {}, 0, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopySetHeadRequest, RemoveParent) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
mock_image_ctx.parent_md.spec.pool_id = 213;
InSequence seq;
expect_start_op(mock_exclusive_lock);
MockDetachParentRequest mock_detach_parent;
expect_detach_parent(mock_image_ctx, mock_detach_parent, 0);
C_SaferCond ctx;
auto request = create_request(mock_image_ctx, m_image_ctx->size, {}, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopySetHeadRequest, RemoveParentError) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
mock_image_ctx.parent_md.spec.pool_id = 213;
InSequence seq;
expect_start_op(mock_exclusive_lock);
MockDetachParentRequest mock_detach_parent;
expect_detach_parent(mock_image_ctx, mock_detach_parent, -EINVAL);
C_SaferCond ctx;
auto request = create_request(mock_image_ctx, m_image_ctx->size, {}, 0, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopySetHeadRequest, RemoveSetParent) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
mock_image_ctx.parent_md.spec.pool_id = 213;
InSequence seq;
expect_start_op(mock_exclusive_lock);
MockDetachParentRequest mock_detach_parent;
expect_detach_parent(mock_image_ctx, mock_detach_parent, 0);
expect_start_op(mock_exclusive_lock);
MockAttachParentRequest mock_attach_parent;
expect_attach_parent(mock_image_ctx, mock_attach_parent, 0);
C_SaferCond ctx;
auto request = create_request(mock_image_ctx, m_image_ctx->size,
{123, "", "test", 0}, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopySetHeadRequest, SetParentSpec) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
InSequence seq;
expect_start_op(mock_exclusive_lock);
MockAttachParentRequest mock_attach_parent;
expect_attach_parent(mock_image_ctx, mock_attach_parent, 0);
C_SaferCond ctx;
auto request = create_request(mock_image_ctx, m_image_ctx->size,
{123, "", "test", 0}, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopySetHeadRequest, SetParentOverlap) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
mock_image_ctx.parent_md.spec = {123, "", "test", 0};
mock_image_ctx.parent_md.overlap = m_image_ctx->size;
InSequence seq;
expect_start_op(mock_exclusive_lock);
expect_set_size(mock_image_ctx, 0);
C_SaferCond ctx;
auto request = create_request(mock_image_ctx, 123,
mock_image_ctx.parent_md.spec, 123, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(123U, mock_image_ctx.parent_md.overlap);
}
TEST_F(TestMockDeepCopySetHeadRequest, SetParentError) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
InSequence seq;
expect_start_op(mock_exclusive_lock);
MockAttachParentRequest mock_attach_parent;
expect_attach_parent(mock_image_ctx, mock_attach_parent, -ESTALE);
C_SaferCond ctx;
auto request = create_request(mock_image_ctx, m_image_ctx->size,
{123, "", "test", 0}, 0, &ctx);
request->send();
ASSERT_EQ(-ESTALE, ctx.wait());
}
} // namespace deep_copy
} // namespace librbd
| 9,579 | 31.255892 | 99 |
cc
|
null |
ceph-main/src/test/librbd/deep_copy/test_mock_SnapshotCopyRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "include/rbd/librbd.hpp"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "librbd/deep_copy/SetHeadRequest.h"
#include "librbd/deep_copy/SnapshotCopyRequest.h"
#include "librbd/deep_copy/SnapshotCreateRequest.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/test_support.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace deep_copy {
template <>
class SetHeadRequest<librbd::MockTestImageCtx> {
public:
static SetHeadRequest* s_instance;
Context *on_finish;
static SetHeadRequest* create(librbd::MockTestImageCtx *image_ctx,
uint64_t size,
const cls::rbd::ParentImageSpec &parent_spec,
uint64_t parent_overlap, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
SetHeadRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
template <>
struct SnapshotCreateRequest<librbd::MockTestImageCtx> {
static SnapshotCreateRequest* s_instance;
static SnapshotCreateRequest* create(librbd::MockTestImageCtx* image_ctx,
const std::string &snap_name,
const cls::rbd::SnapshotNamespace &snap_namespace,
uint64_t size,
const cls::rbd::ParentImageSpec &parent_spec,
uint64_t parent_overlap,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
Context *on_finish = nullptr;
SnapshotCreateRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
SetHeadRequest<librbd::MockTestImageCtx>* SetHeadRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
SnapshotCreateRequest<librbd::MockTestImageCtx>* SnapshotCreateRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
} // namespace deep_copy
} // namespace librbd
// template definitions
#include "librbd/deep_copy/SnapshotCopyRequest.cc"
template class librbd::deep_copy::SnapshotCopyRequest<librbd::MockTestImageCtx>;
namespace librbd {
namespace deep_copy {
using ::testing::_;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::SetArgPointee;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMockDeepCopySnapshotCopyRequest : public TestMockFixture {
public:
typedef SetHeadRequest<librbd::MockTestImageCtx> MockSetHeadRequest;
typedef SnapshotCopyRequest<librbd::MockTestImageCtx> MockSnapshotCopyRequest;
typedef SnapshotCreateRequest<librbd::MockTestImageCtx> MockSnapshotCreateRequest;
librbd::ImageCtx *m_src_image_ctx;
librbd::ImageCtx *m_dst_image_ctx;
std::shared_ptr<librbd::AsioEngine> m_asio_engine;
asio::ContextWQ *m_work_queue;
librbd::SnapSeqs m_snap_seqs;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_src_image_ctx));
librbd::RBD rbd;
std::string dst_image_name = get_temp_image_name();
ASSERT_EQ(0, create_image_pp(rbd, m_ioctx, dst_image_name, m_image_size));
ASSERT_EQ(0, open_image(dst_image_name, &m_dst_image_ctx));
m_asio_engine = std::make_shared<librbd::AsioEngine>(
m_src_image_ctx->md_ctx);
m_work_queue = m_asio_engine->get_work_queue();
}
void prepare_exclusive_lock(librbd::MockImageCtx &mock_image_ctx,
librbd::MockExclusiveLock &mock_exclusive_lock) {
if ((mock_image_ctx.features & RBD_FEATURE_EXCLUSIVE_LOCK) == 0) {
return;
}
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
}
void expect_test_features(librbd::MockImageCtx &mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, test_features(_, _))
.WillRepeatedly(WithArg<0>(Invoke([&mock_image_ctx](uint64_t features) {
return (mock_image_ctx.features & features) != 0;
})));
EXPECT_CALL(mock_image_ctx, test_features(_))
.WillRepeatedly(WithArg<0>(Invoke([&mock_image_ctx](uint64_t features) {
return (mock_image_ctx.features & features) != 0;
})));
}
void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
if ((m_src_image_ctx->features & RBD_FEATURE_EXCLUSIVE_LOCK) == 0) {
return;
}
EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
}
void expect_get_snap_namespace(librbd::MockTestImageCtx &mock_image_ctx,
uint64_t snap_id) {
EXPECT_CALL(mock_image_ctx, get_snap_namespace(snap_id, _))
.WillOnce(Invoke([&mock_image_ctx](uint64_t snap_id,
cls::rbd::SnapshotNamespace* snap_ns) {
auto it = mock_image_ctx.snap_info.find(snap_id);
*snap_ns = it->second.snap_namespace;
return 0;
}));
}
void expect_snap_create(librbd::MockTestImageCtx &mock_image_ctx,
MockSnapshotCreateRequest &mock_snapshot_create_request,
const std::string &snap_name, uint64_t snap_id, int r) {
EXPECT_CALL(mock_snapshot_create_request, send())
.WillOnce(DoAll(Invoke([&mock_image_ctx, snap_id, snap_name]() {
inject_snap(mock_image_ctx, snap_id, snap_name);
}),
Invoke([this, &mock_snapshot_create_request, r]() {
m_work_queue->queue(mock_snapshot_create_request.on_finish, r);
})));
}
void expect_snap_remove(librbd::MockTestImageCtx &mock_image_ctx,
const std::string &snap_name, int r) {
EXPECT_CALL(*mock_image_ctx.operations, execute_snap_remove(_, StrEq(snap_name), _))
.WillOnce(WithArg<2>(Invoke([this, r](Context *ctx) {
m_work_queue->queue(ctx, r);
})));
}
void expect_snap_protect(librbd::MockTestImageCtx &mock_image_ctx,
const std::string &snap_name, int r) {
EXPECT_CALL(*mock_image_ctx.operations, execute_snap_protect(_, StrEq(snap_name), _))
.WillOnce(WithArg<2>(Invoke([this, r](Context *ctx) {
m_work_queue->queue(ctx, r);
})));
}
void expect_snap_unprotect(librbd::MockTestImageCtx &mock_image_ctx,
const std::string &snap_name, int r) {
EXPECT_CALL(*mock_image_ctx.operations, execute_snap_unprotect(_, StrEq(snap_name), _))
.WillOnce(WithArg<2>(Invoke([this, r](Context *ctx) {
m_work_queue->queue(ctx, r);
})));
}
void expect_snap_is_protected(librbd::MockTestImageCtx &mock_image_ctx,
uint64_t snap_id, bool is_protected, int r) {
EXPECT_CALL(mock_image_ctx, is_snap_protected(snap_id, _))
.WillOnce(DoAll(SetArgPointee<1>(is_protected),
Return(r)));
}
void expect_snap_is_unprotected(librbd::MockTestImageCtx &mock_image_ctx,
uint64_t snap_id, bool is_unprotected, int r) {
EXPECT_CALL(mock_image_ctx, is_snap_unprotected(snap_id, _))
.WillOnce(DoAll(SetArgPointee<1>(is_unprotected),
Return(r)));
}
void expect_set_head(MockSetHeadRequest &mock_set_head_request, int r) {
EXPECT_CALL(mock_set_head_request, send())
.WillOnce(Invoke([&mock_set_head_request, r]() {
mock_set_head_request.on_finish->complete(r);
}));
}
static void inject_snap(librbd::MockTestImageCtx &mock_image_ctx,
uint64_t snap_id, const std::string &snap_name) {
mock_image_ctx.snap_ids[{cls::rbd::UserSnapshotNamespace(),
snap_name}] = snap_id;
}
MockSnapshotCopyRequest *create_request(
librbd::MockTestImageCtx &mock_src_image_ctx,
librbd::MockTestImageCtx &mock_dst_image_ctx,
librados::snap_t src_snap_id_start,
librados::snap_t src_snap_id_end,
librados::snap_t dst_snap_id_start,
Context *on_finish) {
return new MockSnapshotCopyRequest(&mock_src_image_ctx, &mock_dst_image_ctx,
src_snap_id_start, src_snap_id_end,
dst_snap_id_start, false, m_work_queue,
&m_snap_seqs, on_finish);
}
int create_snap(librbd::ImageCtx *image_ctx,
const cls::rbd::SnapshotNamespace& snap_ns,
const std::string &snap_name, bool protect) {
NoOpProgressContext prog_ctx;
int r = image_ctx->operations->snap_create(snap_ns, snap_name.c_str(), 0,
prog_ctx);
if (r < 0) {
return r;
}
if (protect) {
EXPECT_TRUE(std::holds_alternative<cls::rbd::UserSnapshotNamespace>(snap_ns));
r = image_ctx->operations->snap_protect(snap_ns, snap_name.c_str());
if (r < 0) {
return r;
}
}
r = image_ctx->state->refresh();
if (r < 0) {
return r;
}
return 0;
}
int create_snap(librbd::ImageCtx *image_ctx, const std::string &snap_name,
bool protect = false) {
return create_snap(image_ctx, cls::rbd::UserSnapshotNamespace{}, snap_name,
protect);
}
void validate_snap_seqs(const librbd::SnapSeqs &snap_seqs) {
ASSERT_EQ(snap_seqs, m_snap_seqs);
}
};
TEST_F(TestMockDeepCopySnapshotCopyRequest, Empty) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSetHeadRequest mock_set_head_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_set_head(mock_set_head_request, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({});
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapCreate) {
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1"));
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap2"));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t src_snap_id2 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap2"}];
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCreateRequest mock_snapshot_create_request;
MockSetHeadRequest mock_set_head_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1", 12, 0);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id2);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap2", 14, 0);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id2, false, 0);
expect_set_head(mock_set_head_request, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({{src_snap_id1, 12}, {src_snap_id2, 14}});
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapCreateError) {
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCreateRequest mock_snapshot_create_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
uint64_t src_snap_id1 = mock_src_image_ctx.snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1",
12, -EINVAL);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapCreateCancel) {
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCreateRequest mock_snapshot_create_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_start_op(mock_exclusive_lock);
EXPECT_CALL(mock_snapshot_create_request, send())
.WillOnce(DoAll(InvokeWithoutArgs([request]() {
request->cancel();
}),
Invoke([this, &mock_snapshot_create_request]() {
m_work_queue->queue(mock_snapshot_create_request.on_finish, 0);
})));
request->send();
ASSERT_EQ(-ECANCELED, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapRemoveAndCreate) {
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1"));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1"));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCreateRequest mock_snapshot_create_request;
MockSetHeadRequest mock_set_head_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx,
m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}],
true, 0);
expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_remove(mock_dst_image_ctx, "snap1", 0);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1", 12, 0);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
expect_set_head(mock_set_head_request, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({{src_snap_id1, 12}});
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapRemoveError) {
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1"));
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx,
m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}],
true, 0);
expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_remove(mock_dst_image_ctx, "snap1", -EINVAL);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapUnprotect) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
m_snap_seqs[src_snap_id1] = dst_snap_id1;
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSetHeadRequest mock_set_head_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, false, 0);
expect_snap_is_unprotected(mock_src_image_ctx, src_snap_id1, true, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_unprotect(mock_dst_image_ctx, "snap1", 0);
expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
expect_set_head(mock_set_head_request, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({{src_snap_id1, dst_snap_id1}});
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapUnprotectError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
m_snap_seqs[src_snap_id1] = dst_snap_id1;
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, false, 0);
expect_snap_is_unprotected(mock_src_image_ctx, src_snap_id1, true, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_unprotect(mock_dst_image_ctx, "snap1", -EBUSY);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(-EBUSY, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapUnprotectCancel) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
m_snap_seqs[src_snap_id1] = dst_snap_id1;
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, false, 0);
expect_snap_is_unprotected(mock_src_image_ctx, src_snap_id1, true, 0);
expect_start_op(mock_exclusive_lock);
EXPECT_CALL(*mock_dst_image_ctx.operations,
execute_snap_unprotect(_, StrEq("snap1"), _))
.WillOnce(DoAll(InvokeWithoutArgs([request]() {
request->cancel();
}),
WithArg<2>(Invoke([this](Context *ctx) {
m_work_queue->queue(ctx, 0);
}))));
request->send();
ASSERT_EQ(-ECANCELED, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapUnprotectRemove) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCreateRequest mock_snapshot_create_request;
MockSetHeadRequest mock_set_head_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx,
m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}],
false, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_unprotect(mock_dst_image_ctx, "snap1", 0);
expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_remove(mock_dst_image_ctx, "snap1", 0);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1",
12, 0);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
expect_set_head(mock_set_head_request, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({{src_snap_id1, 12}});
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapCreateProtect) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCreateRequest mock_snapshot_create_request;
MockSetHeadRequest mock_set_head_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1",
12, 0);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, true, 0);
expect_snap_is_protected(mock_dst_image_ctx, 12, false, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_protect(mock_dst_image_ctx, "snap1", 0);
expect_set_head(mock_set_head_request, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({{src_snap_id1, 12}});
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapProtect) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
m_snap_seqs[src_snap_id1] = dst_snap_id1;
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSetHeadRequest mock_set_head_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, true, 0);
expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, true, 0);
expect_snap_is_protected(mock_dst_image_ctx, dst_snap_id1, false, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_protect(mock_dst_image_ctx, "snap1", 0);
expect_set_head(mock_set_head_request, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({{src_snap_id1, dst_snap_id1}});
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapProtectError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
m_snap_seqs[src_snap_id1] = dst_snap_id1;
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, true, 0);
expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, true, 0);
expect_snap_is_protected(mock_dst_image_ctx, dst_snap_id1, false, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_protect(mock_dst_image_ctx, "snap1", -EINVAL);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SnapProtectCancel) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
uint64_t dst_snap_id1 = m_dst_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
m_snap_seqs[src_snap_id1] = dst_snap_id1;
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id1, true, 0);
expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id1);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, true, 0);
expect_snap_is_protected(mock_dst_image_ctx, dst_snap_id1, false, 0);
expect_start_op(mock_exclusive_lock);
EXPECT_CALL(*mock_dst_image_ctx.operations,
execute_snap_protect(_, StrEq("snap1"), _))
.WillOnce(DoAll(InvokeWithoutArgs([request]() {
request->cancel();
}),
WithArg<2>(Invoke([this](Context *ctx) {
m_work_queue->queue(ctx, 0);
}))));
request->send();
ASSERT_EQ(-ECANCELED, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, SetHeadError) {
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSetHeadRequest mock_set_head_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_set_head(mock_set_head_request, -EINVAL);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx, 0,
CEPH_NOSNAP, 0, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, NoSetHead) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", true));
uint64_t src_snap_id1 = m_src_image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace(), "snap1"}];
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCreateRequest mock_snapshot_create_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id1);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap1",
12, 0);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id1, false, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx,0,
src_snap_id1, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({{src_snap_id1, 12}});
}
TEST_F(TestMockDeepCopySnapshotCopyRequest, StartEndLimit) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap1", false));
ASSERT_EQ(0, create_snap(m_src_image_ctx, "snap2", false));
ASSERT_EQ(0, create_snap(m_src_image_ctx,
{cls::rbd::MirrorSnapshotNamespace{
cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY,
{"peer uuid1"}, "", CEPH_NOSNAP}},
"snap3", false));
auto src_snap_id1 = m_src_image_ctx->snaps[2];
auto src_snap_id2 = m_src_image_ctx->snaps[1];
auto src_snap_id3 = m_src_image_ctx->snaps[0];
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap0", true));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap1", false));
ASSERT_EQ(0, create_snap(m_dst_image_ctx, "snap3", false));
auto dst_snap_id1 = m_dst_image_ctx->snaps[1];
auto dst_snap_id3 = m_dst_image_ctx->snaps[0];
librbd::MockTestImageCtx mock_src_image_ctx(*m_src_image_ctx);
librbd::MockTestImageCtx mock_dst_image_ctx(*m_dst_image_ctx);
MockSnapshotCreateRequest mock_snapshot_create_request;
librbd::MockExclusiveLock mock_exclusive_lock;
prepare_exclusive_lock(mock_dst_image_ctx, mock_exclusive_lock);
expect_test_features(mock_dst_image_ctx);
InSequence seq;
expect_snap_is_unprotected(mock_dst_image_ctx, dst_snap_id3,
true, 0);
expect_get_snap_namespace(mock_dst_image_ctx, dst_snap_id3);
expect_start_op(mock_exclusive_lock);
expect_snap_remove(mock_dst_image_ctx, "snap3", 0);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id2);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_dst_image_ctx, mock_snapshot_create_request, "snap2",
12, 0);
expect_get_snap_namespace(mock_src_image_ctx, src_snap_id3);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id2, false, 0);
expect_snap_is_protected(mock_src_image_ctx, src_snap_id3, false, 0);
MockSetHeadRequest mock_set_head_request;
expect_set_head(mock_set_head_request, 0);
C_SaferCond ctx;
MockSnapshotCopyRequest *request = create_request(mock_src_image_ctx,
mock_dst_image_ctx,
src_snap_id1,
src_snap_id3,
dst_snap_id1, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
validate_snap_seqs({{src_snap_id2, 12}, {src_snap_id3, CEPH_NOSNAP}});
}
} // namespace deep_copy
} // namespace librbd
| 35,850 | 37.84182 | 119 |
cc
|
null |
ceph-main/src/test/librbd/deep_copy/test_mock_SnapshotCreateRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librados_test_stub/LibradosTestStub.h"
#include "include/rbd/librbd.hpp"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "osdc/Striper.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "librbd/deep_copy/SetHeadRequest.h"
#include "librbd/deep_copy/SnapshotCreateRequest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace deep_copy {
template <>
class SetHeadRequest<librbd::MockTestImageCtx> {
public:
static SetHeadRequest* s_instance;
Context *on_finish;
static SetHeadRequest* create(librbd::MockTestImageCtx *image_ctx,
uint64_t size,
const cls::rbd::ParentImageSpec &parent_spec,
uint64_t parent_overlap, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
SetHeadRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
SetHeadRequest<librbd::MockTestImageCtx>* SetHeadRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
} // namespace deep_copy
} // namespace librbd
// template definitions
#include "librbd/deep_copy/SnapshotCreateRequest.cc"
template class librbd::deep_copy::SnapshotCreateRequest<librbd::MockTestImageCtx>;
namespace librbd {
namespace deep_copy {
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMockDeepCopySnapshotCreateRequest : public TestMockFixture {
public:
typedef SetHeadRequest<librbd::MockTestImageCtx> MockSetHeadRequest;
typedef SnapshotCreateRequest<librbd::MockTestImageCtx> MockSnapshotCreateRequest;
librbd::ImageCtx *m_image_ctx;
std::shared_ptr<librbd::AsioEngine> m_asio_engine;
asio::ContextWQ *m_work_queue;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
m_asio_engine = std::make_shared<librbd::AsioEngine>(
m_image_ctx->md_ctx);
m_work_queue = m_asio_engine->get_work_queue();
}
void expect_start_op(librbd::MockExclusiveLock &mock_exclusive_lock) {
EXPECT_CALL(mock_exclusive_lock, start_op(_)).WillOnce(Return(new LambdaContext([](int){})));
}
void expect_test_features(librbd::MockTestImageCtx &mock_image_ctx,
uint64_t features, bool enabled) {
EXPECT_CALL(mock_image_ctx, test_features(features))
.WillOnce(Return(enabled));
}
void expect_set_head(MockSetHeadRequest &mock_set_head_request, int r) {
EXPECT_CALL(mock_set_head_request, send())
.WillOnce(Invoke([&mock_set_head_request, r]() {
mock_set_head_request.on_finish->complete(r);
}));
}
void expect_snap_create(librbd::MockTestImageCtx &mock_image_ctx,
const std::string &snap_name, uint64_t snap_id, int r) {
uint64_t flags = SNAP_CREATE_FLAG_SKIP_OBJECT_MAP |
SNAP_CREATE_FLAG_SKIP_NOTIFY_QUIESCE;
EXPECT_CALL(*mock_image_ctx.operations,
execute_snap_create(_, StrEq(snap_name), _, 0, flags, _))
.WillOnce(DoAll(InvokeWithoutArgs([&mock_image_ctx, snap_id, snap_name]() {
inject_snap(mock_image_ctx, snap_id, snap_name);
}),
WithArg<2>(Invoke([this, r](Context *ctx) {
m_work_queue->queue(ctx, r);
}))));
}
void expect_object_map_resize(librbd::MockTestImageCtx &mock_image_ctx,
librados::snap_t snap_id, int r) {
std::string oid(librbd::ObjectMap<>::object_map_name(mock_image_ctx.id,
snap_id));
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(oid, _, StrEq("rbd"), StrEq("object_map_resize"), _, _, _,
_))
.WillOnce(Return(r));
}
static void inject_snap(librbd::MockTestImageCtx &mock_image_ctx,
uint64_t snap_id, const std::string &snap_name) {
mock_image_ctx.snap_ids[{cls::rbd::UserSnapshotNamespace(),
snap_name}] = snap_id;
}
MockSnapshotCreateRequest *create_request(librbd::MockTestImageCtx &mock_local_image_ctx,
const std::string &snap_name,
const cls::rbd::SnapshotNamespace &snap_namespace,
uint64_t size,
const cls::rbd::ParentImageSpec &spec,
uint64_t parent_overlap,
Context *on_finish) {
return new MockSnapshotCreateRequest(&mock_local_image_ctx, snap_name, snap_namespace, size,
spec, parent_overlap, on_finish);
}
};
TEST_F(TestMockDeepCopySnapshotCreateRequest, SnapCreate) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockSetHeadRequest mock_set_head_request;
InSequence seq;
expect_set_head(mock_set_head_request, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_image_ctx, "snap1", 10, 0);
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, false);
C_SaferCond ctx;
MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
"snap1",
cls::rbd::UserSnapshotNamespace(),
m_image_ctx->size,
{}, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCreateRequest, SetHeadError) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
MockSetHeadRequest mock_set_head_request;
InSequence seq;
expect_set_head(mock_set_head_request, -EINVAL);
C_SaferCond ctx;
MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
"snap1",
cls::rbd::UserSnapshotNamespace(),
123, {}, 0,
&ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCreateRequest, SnapCreateError) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockSetHeadRequest mock_set_head_request;
InSequence seq;
expect_set_head(mock_set_head_request, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_image_ctx, "snap1", 10, -EINVAL);
C_SaferCond ctx;
MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
"snap1",
cls::rbd::UserSnapshotNamespace(),
m_image_ctx->size,
{}, 0, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCreateRequest, ResizeObjectMap) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockSetHeadRequest mock_set_head_request;
InSequence seq;
expect_set_head(mock_set_head_request, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_image_ctx, "snap1", 10, 0);
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_start_op(mock_exclusive_lock);
expect_object_map_resize(mock_image_ctx, 10, 0);
C_SaferCond ctx;
MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
"snap1",
cls::rbd::UserSnapshotNamespace(),
m_image_ctx->size,
{}, 0, &ctx);
request->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockDeepCopySnapshotCreateRequest, ResizeObjectMapError) {
librbd::MockTestImageCtx mock_image_ctx(*m_image_ctx);
librbd::MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockSetHeadRequest mock_set_head_request;
InSequence seq;
expect_set_head(mock_set_head_request, 0);
expect_start_op(mock_exclusive_lock);
expect_snap_create(mock_image_ctx, "snap1", 10, 0);
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_start_op(mock_exclusive_lock);
expect_object_map_resize(mock_image_ctx, 10, -EINVAL);
C_SaferCond ctx;
MockSnapshotCreateRequest *request = create_request(mock_image_ctx,
"snap1",
cls::rbd::UserSnapshotNamespace(),
m_image_ctx->size,
{}, 0, &ctx);
request->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
} // namespace deep_copy
} // namespace librbd
| 9,814 | 36.319392 | 105 |
cc
|
null |
ceph-main/src/test/librbd/exclusive_lock/test_mock_PostAcquireRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/cache/MockImageCache.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockImageState.h"
#include "test/librbd/mock/MockJournal.h"
#include "test/librbd/mock/MockJournalPolicy.h"
#include "test/librbd/mock/MockObjectMap.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "librbd/exclusive_lock/PostAcquireRequest.h"
#include "librbd/image/RefreshRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <arpa/inet.h>
#include <list>
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
};
inline ImageCtx &get_image_ctx(MockTestImageCtx &image_ctx) {
return *(image_ctx.image_ctx);
}
} // anonymous namespace
namespace image {
template<>
struct RefreshRequest<librbd::MockTestImageCtx> {
static RefreshRequest *s_instance;
Context *on_finish = nullptr;
static RefreshRequest *create(librbd::MockTestImageCtx &image_ctx,
bool acquire_lock_refresh,
bool skip_open_parent, Context *on_finish) {
EXPECT_TRUE(acquire_lock_refresh);
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
RefreshRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
RefreshRequest<librbd::MockTestImageCtx> *RefreshRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
} // namespace image
} // namespace librbd
// template definitions
#include "librbd/Journal.cc"
#include "librbd/exclusive_lock/PostAcquireRequest.cc"
template class librbd::exclusive_lock::PostAcquireRequest<librbd::MockTestImageCtx>;
ACTION_P3(FinishRequest2, request, r, mock) {
mock->image_ctx->op_work_queue->queue(request->on_finish, r);
}
namespace librbd {
namespace exclusive_lock {
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::SetArgPointee;
using ::testing::StrEq;
using ::testing::WithArg;
static const std::string TEST_COOKIE("auto 123");
class TestMockExclusiveLockPostAcquireRequest : public TestMockFixture {
public:
typedef PostAcquireRequest<MockTestImageCtx> MockPostAcquireRequest;
typedef librbd::image::RefreshRequest<MockTestImageCtx> MockRefreshRequest;
void expect_test_features(MockTestImageCtx &mock_image_ctx, uint64_t features,
bool enabled) {
EXPECT_CALL(mock_image_ctx, test_features(features))
.WillOnce(Return(enabled));
}
void expect_test_features(MockTestImageCtx &mock_image_ctx, uint64_t features,
ceph::shared_mutex &lock, bool enabled) {
EXPECT_CALL(mock_image_ctx, test_features(features, _))
.WillOnce(Return(enabled));
}
void expect_is_refresh_required(MockTestImageCtx &mock_image_ctx, bool required) {
EXPECT_CALL(*mock_image_ctx.state, is_refresh_required())
.WillOnce(Return(required));
}
void expect_refresh(MockTestImageCtx &mock_image_ctx,
MockRefreshRequest &mock_refresh_request, int r) {
EXPECT_CALL(mock_refresh_request, send())
.WillOnce(FinishRequest2(&mock_refresh_request, r,
&mock_image_ctx));
}
void expect_create_object_map(MockTestImageCtx &mock_image_ctx,
MockObjectMap *mock_object_map) {
EXPECT_CALL(mock_image_ctx, create_object_map(_))
.WillOnce(Return(mock_object_map));
}
void expect_open_object_map(MockTestImageCtx &mock_image_ctx,
MockObjectMap &mock_object_map, int r) {
EXPECT_CALL(mock_object_map, open(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_close_object_map(MockTestImageCtx &mock_image_ctx,
MockObjectMap &mock_object_map) {
EXPECT_CALL(mock_object_map, close(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_create_journal(MockTestImageCtx &mock_image_ctx,
MockJournal *mock_journal) {
EXPECT_CALL(mock_image_ctx, create_journal())
.WillOnce(Return(mock_journal));
}
void expect_open_journal(MockTestImageCtx &mock_image_ctx,
MockJournal &mock_journal, int r) {
EXPECT_CALL(mock_journal, open(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_close_journal(MockTestImageCtx &mock_image_ctx,
MockJournal &mock_journal) {
EXPECT_CALL(mock_journal, close(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_get_journal_policy(MockTestImageCtx &mock_image_ctx,
MockJournalPolicy &mock_journal_policy) {
EXPECT_CALL(mock_image_ctx, get_journal_policy())
.WillOnce(Return(&mock_journal_policy));
}
void expect_journal_disabled(MockJournalPolicy &mock_journal_policy,
bool disabled) {
EXPECT_CALL(mock_journal_policy, journal_disabled())
.WillOnce(Return(disabled));
}
void expect_allocate_journal_tag(MockTestImageCtx &mock_image_ctx,
MockJournalPolicy &mock_journal_policy,
int r) {
EXPECT_CALL(mock_journal_policy, allocate_tag_on_lock(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_handle_prepare_lock_complete(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete());
}
void expect_acquired_exclusive_lock(MockTestImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.plugin_registry, acquired_exclusive_lock(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_prerelease_exclusive_lock(MockTestImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.plugin_registry, prerelease_exclusive_lock(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
};
TEST_F(TestMockExclusiveLockPostAcquireRequest, Success) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, false);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_create_object_map(mock_image_ctx, &mock_object_map);
expect_open_object_map(mock_image_ctx, mock_object_map, 0);
MockJournal mock_journal;
MockJournalPolicy mock_journal_policy;
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, true);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_journal_disabled(mock_journal_policy, false);
expect_create_journal(mock_image_ctx, &mock_journal);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_open_journal(mock_image_ctx, mock_journal, 0);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_allocate_journal_tag(mock_image_ctx, mock_journal_policy, 0);
expect_acquired_exclusive_lock(mock_image_ctx, 0);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(0, acquire_ctx.wait());
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, SuccessRefresh) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockRefreshRequest mock_refresh_request;
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, true);
expect_refresh(mock_image_ctx, mock_refresh_request, 0);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, false);
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, false);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_acquired_exclusive_lock(mock_image_ctx, 0);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(0, acquire_ctx.wait());
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, SuccessJournalDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, false);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_create_object_map(mock_image_ctx, &mock_object_map);
expect_open_object_map(mock_image_ctx, mock_object_map, 0);
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, false);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_acquired_exclusive_lock(mock_image_ctx, 0);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(0, acquire_ctx.wait());
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, SuccessObjectMapDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, false);
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, false);
MockJournal mock_journal;
MockJournalPolicy mock_journal_policy;
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, true);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_journal_disabled(mock_journal_policy, false);
expect_create_journal(mock_image_ctx, &mock_journal);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_open_journal(mock_image_ctx, mock_journal, 0);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_allocate_journal_tag(mock_image_ctx, mock_journal_policy, 0);
expect_acquired_exclusive_lock(mock_image_ctx, 0);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(0, acquire_ctx.wait());
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, RefreshError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockRefreshRequest mock_refresh_request;
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, true);
expect_refresh(mock_image_ctx, mock_refresh_request, -EINVAL);
expect_handle_prepare_lock_complete(mock_image_ctx);
C_SaferCond *acquire_ctx = new C_SaferCond();
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, RefreshLockDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockRefreshRequest mock_refresh_request;
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, true);
expect_refresh(mock_image_ctx, mock_refresh_request, -ERESTART);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, false);
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, false);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_acquired_exclusive_lock(mock_image_ctx, 0);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(0, acquire_ctx.wait());
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, JournalError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, false);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_create_object_map(mock_image_ctx, &mock_object_map);
expect_open_object_map(mock_image_ctx, mock_object_map, 0);
MockJournal mock_journal;
MockJournalPolicy mock_journal_policy;
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, true);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_journal_disabled(mock_journal_policy, false);
expect_create_journal(mock_image_ctx, &mock_journal);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_open_journal(mock_image_ctx, mock_journal, -EINVAL);
expect_close_journal(mock_image_ctx, mock_journal);
expect_close_object_map(mock_image_ctx, mock_object_map);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, AllocateJournalTagError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, false);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_create_object_map(mock_image_ctx, &mock_object_map);
expect_open_object_map(mock_image_ctx, mock_object_map, 0);
MockJournal mock_journal;
MockJournalPolicy mock_journal_policy;
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, true);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_journal_disabled(mock_journal_policy, false);
expect_create_journal(mock_image_ctx, &mock_journal);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_open_journal(mock_image_ctx, mock_journal, 0);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_allocate_journal_tag(mock_image_ctx, mock_journal_policy, -EPERM);
expect_close_journal(mock_image_ctx, mock_journal);
expect_close_object_map(mock_image_ctx, mock_object_map);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, InitImageCacheError) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, false);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_create_object_map(mock_image_ctx, &mock_object_map);
expect_open_object_map(mock_image_ctx, mock_object_map, 0);
MockJournal mock_journal;
MockJournalPolicy mock_journal_policy;
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, true);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_journal_disabled(mock_journal_policy, false);
expect_create_journal(mock_image_ctx, &mock_journal);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_open_journal(mock_image_ctx, mock_journal, 0);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_allocate_journal_tag(mock_image_ctx, mock_journal_policy, 0);
expect_acquired_exclusive_lock(mock_image_ctx, -ENOENT);
expect_prerelease_exclusive_lock(mock_image_ctx, 0);
expect_close_journal(mock_image_ctx, mock_journal);
expect_close_object_map(mock_image_ctx, mock_object_map);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(-ENOENT, ctx.wait());
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, OpenObjectMapError) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, false);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_create_object_map(mock_image_ctx, &mock_object_map);
expect_open_object_map(mock_image_ctx, mock_object_map, -EINVAL);
expect_handle_prepare_lock_complete(mock_image_ctx);
C_SaferCond *acquire_ctx = new C_SaferCond();
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
ASSERT_EQ(nullptr, mock_image_ctx.object_map);
}
TEST_F(TestMockExclusiveLockPostAcquireRequest, OpenObjectMapTooBig) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_refresh_required(mock_image_ctx, false);
MockObjectMap mock_object_map;
expect_test_features(mock_image_ctx, RBD_FEATURE_OBJECT_MAP, true);
expect_create_object_map(mock_image_ctx, &mock_object_map);
expect_open_object_map(mock_image_ctx, mock_object_map, -EFBIG);
MockJournal mock_journal;
MockJournalPolicy mock_journal_policy;
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
mock_image_ctx.image_lock, true);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_journal_disabled(mock_journal_policy, false);
expect_create_journal(mock_image_ctx, &mock_journal);
expect_handle_prepare_lock_complete(mock_image_ctx);
expect_open_journal(mock_image_ctx, mock_journal, 0);
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_allocate_journal_tag(mock_image_ctx, mock_journal_policy, 0);
expect_acquired_exclusive_lock(mock_image_ctx, 0);
C_SaferCond acquire_ctx;
C_SaferCond ctx;
MockPostAcquireRequest *req = MockPostAcquireRequest::create(mock_image_ctx,
&acquire_ctx,
&ctx);
req->send();
ASSERT_EQ(0, acquire_ctx.wait());
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(nullptr, mock_image_ctx.object_map);
}
} // namespace exclusive_lock
} // namespace librbd
| 21,606 | 36.06175 | 105 |
cc
|
null |
ceph-main/src/test/librbd/exclusive_lock/test_mock_PreAcquireRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "librbd/exclusive_lock/PreAcquireRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <arpa/inet.h>
#include <list>
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
};
inline ImageCtx &get_image_ctx(MockTestImageCtx &image_ctx) {
return *(image_ctx.image_ctx);
}
} // anonymous namespace
} // namespace librbd
// template definitions
#include "librbd/exclusive_lock/PreAcquireRequest.cc"
template class librbd::exclusive_lock::PreAcquireRequest<librbd::MockTestImageCtx>;
namespace librbd {
namespace exclusive_lock {
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::SetArgPointee;
using ::testing::StrEq;
using ::testing::WithArg;
static const std::string TEST_COOKIE("auto 123");
class TestMockExclusiveLockPreAcquireRequest : public TestMockFixture {
public:
typedef PreAcquireRequest<MockTestImageCtx> MockPreAcquireRequest;
void expect_flush_notifies(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.image_watcher, flush(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_prepare_lock(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, prepare_lock(_))
.WillOnce(Invoke([](Context *on_ready) {
on_ready->complete(0);
}));
}
void expect_handle_prepare_lock_complete(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete());
}
};
TEST_F(TestMockExclusiveLockPreAcquireRequest, Success) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_prepare_lock(mock_image_ctx);
expect_flush_notifies(mock_image_ctx);
C_SaferCond ctx;
MockPreAcquireRequest *req = MockPreAcquireRequest::create(mock_image_ctx,
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
} // namespace exclusive_lock
} // namespace librbd
| 2,677 | 27.795699 | 89 |
cc
|
null |
ceph-main/src/test/librbd/exclusive_lock/test_mock_PreReleaseRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/cache/MockImageCache.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockJournal.h"
#include "test/librbd/mock/MockObjectMap.h"
#include "test/librbd/mock/io/MockObjectDispatch.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "common/AsyncOpTracker.h"
#include "librbd/exclusive_lock/ImageDispatch.h"
#include "librbd/exclusive_lock/PreReleaseRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <list>
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx& image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace exclusive_lock {
template <>
struct ImageDispatch<MockTestImageCtx> {
MOCK_METHOD3(set_require_lock, void(bool init_shutdown, io::Direction,
Context*));
MOCK_METHOD1(unset_require_lock, void(io::Direction));
};
} // namespace exclusive_lock
namespace util {
inline ImageCtx* get_image_ctx(MockTestImageCtx* image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
// template definitions
#include "librbd/exclusive_lock/PreReleaseRequest.cc"
namespace librbd {
namespace exclusive_lock {
namespace {
struct MockContext : public Context {
MOCK_METHOD1(complete, void(int));
MOCK_METHOD1(finish, void(int));
};
} // anonymous namespace
using ::testing::_;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;
static const std::string TEST_COOKIE("auto 123");
class TestMockExclusiveLockPreReleaseRequest : public TestMockFixture {
public:
typedef ImageDispatch<MockTestImageCtx> MockImageDispatch;
typedef PreReleaseRequest<MockTestImageCtx> MockPreReleaseRequest;
void expect_complete_context(MockContext &mock_context, int r) {
EXPECT_CALL(mock_context, complete(r));
}
void expect_test_features(MockTestImageCtx &mock_image_ctx, uint64_t features,
bool enabled) {
EXPECT_CALL(mock_image_ctx, test_features(features))
.WillOnce(Return(enabled));
}
void expect_set_require_lock(MockImageDispatch &mock_image_dispatch,
bool init_shutdown,
librbd::io::Direction direction, int r) {
EXPECT_CALL(mock_image_dispatch, set_require_lock(init_shutdown,
direction, _))
.WillOnce(WithArg<2>(Invoke([r](Context* ctx) { ctx->complete(r); })));
}
void expect_set_require_lock(MockTestImageCtx &mock_image_ctx,
MockImageDispatch &mock_image_dispatch,
bool init_shutdown, int r) {
expect_test_features(mock_image_ctx, RBD_FEATURE_EXCLUSIVE_LOCK, true);
if (!mock_image_ctx.clone_copy_on_read) {
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING,
((mock_image_ctx.features & RBD_FEATURE_JOURNALING) != 0));
if ((mock_image_ctx.features & RBD_FEATURE_JOURNALING) == 0) {
expect_test_features(mock_image_ctx, RBD_FEATURE_DIRTY_CACHE,
((mock_image_ctx.features & RBD_FEATURE_DIRTY_CACHE) != 0));
}
}
if (mock_image_ctx.clone_copy_on_read ||
(mock_image_ctx.features & RBD_FEATURE_JOURNALING) != 0 ||
(mock_image_ctx.features & RBD_FEATURE_DIRTY_CACHE) != 0) {
expect_set_require_lock(mock_image_dispatch, init_shutdown,
librbd::io::DIRECTION_BOTH, r);
} else {
expect_set_require_lock(mock_image_dispatch, init_shutdown,
librbd::io::DIRECTION_WRITE, r);
}
}
void expect_unset_require_lock(MockImageDispatch &mock_image_dispatch) {
EXPECT_CALL(mock_image_dispatch, unset_require_lock(
io::DIRECTION_BOTH));
}
void expect_cancel_op_requests(MockTestImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(mock_image_ctx, cancel_async_requests(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_close_journal(MockTestImageCtx &mock_image_ctx,
MockJournal &mock_journal, int r) {
EXPECT_CALL(mock_journal, close(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_close_object_map(MockTestImageCtx &mock_image_ctx,
MockObjectMap &mock_object_map) {
EXPECT_CALL(mock_object_map, close(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_prerelease_exclusive_lock(MockTestImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.plugin_registry, prerelease_exclusive_lock(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_invalidate_cache(MockTestImageCtx &mock_image_ctx,
int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, invalidate_cache(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_flush_notifies(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.image_watcher, flush(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_prepare_lock(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, prepare_lock(_))
.WillOnce(Invoke([](Context *on_ready) {
on_ready->complete(0);
}));
}
void expect_handle_prepare_lock_complete(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete());
}
void expect_flush_io(MockTestImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, send(_))
.WillOnce(Invoke([&mock_image_ctx, r](io::ImageDispatchSpec* spec) {
ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
&spec->request) != nullptr);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
auto aio_comp = spec->aio_comp;
auto ctx = new LambdaContext([aio_comp](int r) {
if (r < 0) {
aio_comp->fail(r);
} else {
aio_comp->set_request_count(1);
aio_comp->add_request();
aio_comp->complete_request(r);
}
});
mock_image_ctx.image_ctx->op_work_queue->queue(ctx, r);
}));
}
AsyncOpTracker m_async_op_tracker;
};
TEST_F(TestMockExclusiveLockPreReleaseRequest, Success) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_cancel_op_requests(mock_image_ctx, 0);
MockImageDispatch mock_image_dispatch;
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, false, 0);
expect_prepare_lock(mock_image_ctx);
expect_prerelease_exclusive_lock(mock_image_ctx, 0);
expect_invalidate_cache(mock_image_ctx, 0);
expect_flush_io(mock_image_ctx, 0);
expect_flush_notifies(mock_image_ctx);
MockJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
expect_close_journal(mock_image_ctx, mock_journal, -EINVAL);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
expect_close_object_map(mock_image_ctx, mock_object_map);
expect_handle_prepare_lock_complete(mock_image_ctx);
C_SaferCond ctx;
MockPreReleaseRequest *req = MockPreReleaseRequest::create(
mock_image_ctx, &mock_image_dispatch, false, m_async_op_tracker, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPreReleaseRequest, SuccessJournalDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageDispatch mock_image_dispatch;
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, false, 0);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_cancel_op_requests(mock_image_ctx, 0);
expect_prepare_lock(mock_image_ctx);
expect_prerelease_exclusive_lock(mock_image_ctx, 0);
expect_invalidate_cache(mock_image_ctx, 0);
expect_flush_io(mock_image_ctx, 0);
expect_flush_notifies(mock_image_ctx);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
expect_close_object_map(mock_image_ctx, mock_object_map);
expect_handle_prepare_lock_complete(mock_image_ctx);
C_SaferCond ctx;
MockPreReleaseRequest *req = MockPreReleaseRequest::create(
mock_image_ctx, &mock_image_dispatch, false, m_async_op_tracker, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPreReleaseRequest, SuccessObjectMapDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageDispatch mock_image_dispatch;
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, true, 0);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_cancel_op_requests(mock_image_ctx, 0);
expect_prerelease_exclusive_lock(mock_image_ctx, 0);
expect_invalidate_cache(mock_image_ctx, 0);
expect_flush_io(mock_image_ctx, 0);
expect_flush_notifies(mock_image_ctx);
C_SaferCond release_ctx;
C_SaferCond ctx;
MockPreReleaseRequest *req = MockPreReleaseRequest::create(
mock_image_ctx, &mock_image_dispatch, true, m_async_op_tracker, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPreReleaseRequest, Blocklisted) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_cancel_op_requests(mock_image_ctx, 0);
MockImageDispatch mock_image_dispatch;
expect_set_require_lock(mock_image_ctx, mock_image_dispatch, false,
-EBLOCKLISTED);
expect_prepare_lock(mock_image_ctx);
expect_prerelease_exclusive_lock(mock_image_ctx, 0);
expect_invalidate_cache(mock_image_ctx, -EBLOCKLISTED);
expect_flush_io(mock_image_ctx, -EBLOCKLISTED);
expect_flush_notifies(mock_image_ctx);
MockJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
expect_close_journal(mock_image_ctx, mock_journal, -EBLOCKLISTED);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
expect_close_object_map(mock_image_ctx, mock_object_map);
expect_handle_prepare_lock_complete(mock_image_ctx);
C_SaferCond ctx;
MockPreReleaseRequest *req = MockPreReleaseRequest::create(
mock_image_ctx, &mock_image_dispatch, false, m_async_op_tracker, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockExclusiveLockPreReleaseRequest, Disabled) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_cancel_op_requests(mock_image_ctx, 0);
MockImageDispatch mock_image_dispatch;
expect_test_features(mock_image_ctx, RBD_FEATURE_EXCLUSIVE_LOCK, false);
expect_prepare_lock(mock_image_ctx);
expect_prerelease_exclusive_lock(mock_image_ctx, 0);
expect_invalidate_cache(mock_image_ctx, 0);
expect_flush_io(mock_image_ctx, 0);
expect_flush_notifies(mock_image_ctx);
MockJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
expect_close_journal(mock_image_ctx, mock_journal, -EINVAL);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
expect_close_object_map(mock_image_ctx, mock_object_map);
expect_handle_prepare_lock_complete(mock_image_ctx);
C_SaferCond ctx;
MockPreReleaseRequest *req = MockPreReleaseRequest::create(
mock_image_ctx, &mock_image_dispatch, false, m_async_op_tracker, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
} // namespace exclusive_lock
} // namespace librbd
| 12,655 | 31.534704 | 89 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_AttachChildRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockContextWQ.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "librbd/image/AttachChildRequest.h"
#include "librbd/image/RefreshRequest.h"
#include "librbd/internal.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace image {
template <>
struct RefreshRequest<MockTestImageCtx> {
Context* on_finish = nullptr;
static RefreshRequest* s_instance;
static RefreshRequest* create(MockTestImageCtx &image_ctx,
bool acquiring_lock, bool skip_open_parent,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
RefreshRequest() {
s_instance = this;
}
};
RefreshRequest<MockTestImageCtx>* RefreshRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace image
} // namespace librbd
// template definitions
#include "librbd/image/AttachChildRequest.cc"
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMockImageAttachChildRequest : public TestMockFixture {
public:
typedef AttachChildRequest<MockTestImageCtx> MockAttachChildRequest;
typedef RefreshRequest<MockTestImageCtx> MockRefreshRequest;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
NoOpProgressContext prog_ctx;
ASSERT_EQ(0, image_ctx->operations->snap_create(
cls::rbd::UserSnapshotNamespace{}, "snap", 0, prog_ctx));
if (is_feature_enabled(RBD_FEATURE_LAYERING)) {
ASSERT_EQ(0, image_ctx->operations->snap_protect(
cls::rbd::UserSnapshotNamespace{}, "snap"));
uint64_t snap_id = image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace{}, "snap"}];
ASSERT_NE(CEPH_NOSNAP, snap_id);
C_SaferCond ctx;
image_ctx->state->snap_set(snap_id, &ctx);
ASSERT_EQ(0, ctx.wait());
}
}
void expect_add_child(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(RBD_CHILDREN, _, StrEq("rbd"), StrEq("add_child"), _, _, _,
_))
.WillOnce(Return(r));
}
void expect_refresh(MockRefreshRequest& mock_refresh_request, int r) {
EXPECT_CALL(mock_refresh_request, send())
.WillOnce(Invoke([this, &mock_refresh_request, r]() {
image_ctx->op_work_queue->queue(mock_refresh_request.on_finish, r);
}));
}
void expect_is_snap_protected(MockImageCtx &mock_image_ctx, bool is_protected,
int r) {
EXPECT_CALL(mock_image_ctx, is_snap_protected(_, _))
.WillOnce(WithArg<1>(Invoke([is_protected, r](bool* is_prot) {
*is_prot = is_protected;
return r;
})));
}
void expect_op_features_set(MockImageCtx &mock_image_ctx, int r) {
bufferlist bl;
encode(static_cast<uint64_t>(RBD_OPERATION_FEATURE_CLONE_CHILD), bl);
encode(static_cast<uint64_t>(RBD_OPERATION_FEATURE_CLONE_CHILD), bl);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(util::header_name(mock_image_ctx.id), _, StrEq("rbd"),
StrEq("op_features_set"), ContentsEqual(bl), _, _, _))
.WillOnce(Return(r));
}
void expect_child_attach(MockImageCtx &mock_image_ctx, int r) {
bufferlist bl;
encode(mock_image_ctx.snap_id, bl);
encode(cls::rbd::ChildImageSpec{m_ioctx.get_id(), "", mock_image_ctx.id},
bl);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("child_attach"), ContentsEqual(bl), _, _, _))
.WillOnce(Return(r));
}
librbd::ImageCtx *image_ctx;
};
TEST_F(TestMockImageAttachChildRequest, SuccessV1) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_add_child(mock_image_ctx, 0);
MockRefreshRequest mock_refresh_request;
expect_refresh(mock_refresh_request, 0);
expect_is_snap_protected(mock_image_ctx, true, 0);
C_SaferCond ctx;
auto req = MockAttachChildRequest::create(&mock_image_ctx, &mock_image_ctx,
image_ctx->snap_id, nullptr, 0, 1,
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageAttachChildRequest, SuccessV2) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_op_features_set(mock_image_ctx, 0);
expect_child_attach(mock_image_ctx, 0);
C_SaferCond ctx;
auto req = MockAttachChildRequest::create(&mock_image_ctx, &mock_image_ctx,
image_ctx->snap_id, nullptr, 0, 2,
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageAttachChildRequest, AddChildError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_add_child(mock_image_ctx, -EINVAL);
C_SaferCond ctx;
auto req = MockAttachChildRequest::create(&mock_image_ctx, &mock_image_ctx,
image_ctx->snap_id, nullptr, 0, 1,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageAttachChildRequest, RefreshError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_add_child(mock_image_ctx, 0);
MockRefreshRequest mock_refresh_request;
expect_refresh(mock_refresh_request, -EINVAL);
C_SaferCond ctx;
auto req = MockAttachChildRequest::create(&mock_image_ctx, &mock_image_ctx,
image_ctx->snap_id, nullptr, 0, 1,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageAttachChildRequest, ValidateProtectedFailed) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_add_child(mock_image_ctx, 0);
MockRefreshRequest mock_refresh_request;
expect_refresh(mock_refresh_request, 0);
expect_is_snap_protected(mock_image_ctx, false, 0);
C_SaferCond ctx;
auto req = MockAttachChildRequest::create(&mock_image_ctx, &mock_image_ctx,
image_ctx->snap_id, nullptr, 0, 1,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageAttachChildRequest, SetCloneError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_op_features_set(mock_image_ctx, -EINVAL);
C_SaferCond ctx;
auto req = MockAttachChildRequest::create(&mock_image_ctx, &mock_image_ctx,
image_ctx->snap_id, nullptr, 0, 2,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageAttachChildRequest, AttachChildError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_op_features_set(mock_image_ctx, 0);
expect_child_attach(mock_image_ctx, -EINVAL);
C_SaferCond ctx;
auto req = MockAttachChildRequest::create(&mock_image_ctx, &mock_image_ctx,
image_ctx->snap_id, nullptr, 0, 2,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
} // namespace image
} // namespace librbd
| 8,380 | 29.365942 | 89 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_AttachParentRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockContextWQ.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "librbd/image/AttachParentRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
} // namespace librbd
// template definitions
#include "librbd/image/AttachParentRequest.cc"
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrEq;
class TestMockImageAttachParentRequest : public TestMockFixture {
public:
typedef AttachParentRequest<MockTestImageCtx> MockAttachParentRequest;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
}
void expect_parent_attach(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("parent_attach"), _, _, _, _))
.WillOnce(Return(r));
}
void expect_set_parent(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("set_parent"), _, _, _, _))
.WillOnce(Return(r));
}
librbd::ImageCtx *image_ctx;
};
TEST_F(TestMockImageAttachParentRequest, ParentAttachSuccess) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_attach(mock_image_ctx, 0);
cls::rbd::ParentImageSpec parent_image_spec{
1, "ns", "image id", 123};
C_SaferCond ctx;
auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
234, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageAttachParentRequest, SetParentSuccess) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);
expect_set_parent(mock_image_ctx, 0);
cls::rbd::ParentImageSpec parent_image_spec{
1, "", "image id", 123};
C_SaferCond ctx;
auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
234, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageAttachParentRequest, ParentAttachError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_attach(mock_image_ctx, -EPERM);
cls::rbd::ParentImageSpec parent_image_spec{
1, "", "image id", 123};
C_SaferCond ctx;
auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
234, false, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockImageAttachParentRequest, SetParentError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);
expect_set_parent(mock_image_ctx, -EINVAL);
cls::rbd::ParentImageSpec parent_image_spec{
1, "", "image id", 123};
C_SaferCond ctx;
auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
234, false, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageAttachParentRequest, NamespaceUnsupported) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);
cls::rbd::ParentImageSpec parent_image_spec{
1, "ns", "image id", 123};
C_SaferCond ctx;
auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
234, false, &ctx);
req->send();
ASSERT_EQ(-EXDEV, ctx.wait());
}
} // namespace image
} // namespace librbd
| 4,373 | 27.038462 | 79 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_CloneRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockContextWQ.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "librbd/deep_copy/MetadataCopyRequest.h"
#include "librbd/image/TypeTraits.h"
#include "librbd/image/AttachChildRequest.h"
#include "librbd/image/AttachParentRequest.h"
#include "librbd/image/CreateRequest.h"
#include "librbd/image/RemoveRequest.h"
#include "librbd/mirror/EnableRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
static MockTestImageCtx* s_instance;
static MockTestImageCtx* create(const std::string &image_name,
const std::string &image_id,
const char *snap, librados::IoCtx& p,
bool read_only) {
ceph_assert(s_instance != nullptr);
return s_instance;
}
static MockTestImageCtx* create(const std::string &image_name,
const std::string &image_id,
librados::snap_t snap_id, IoCtx& p,
bool read_only) {
ceph_assert(s_instance != nullptr);
return s_instance;
}
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
s_instance = this;
}
};
MockTestImageCtx* MockTestImageCtx::s_instance = nullptr;
} // anonymous namespace
namespace deep_copy {
template <>
struct MetadataCopyRequest<MockTestImageCtx> {
Context* on_finish = nullptr;
static MetadataCopyRequest* s_instance;
static MetadataCopyRequest* create(MockTestImageCtx* src_image_ctx,
MockTestImageCtx* dst_image_ctx,
Context* on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MetadataCopyRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
MetadataCopyRequest<MockTestImageCtx>* MetadataCopyRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace deep_copy
namespace image {
template <>
struct AttachChildRequest<MockTestImageCtx> {
uint32_t clone_format;
Context* on_finish = nullptr;
static AttachChildRequest* s_instance;
static AttachChildRequest* create(MockTestImageCtx *,
MockTestImageCtx *,
const librados::snap_t &,
MockTestImageCtx *,
const librados::snap_t &,
uint32_t clone_format,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->clone_format = clone_format;
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
AttachChildRequest() {
s_instance = this;
}
};
AttachChildRequest<MockTestImageCtx>* AttachChildRequest<MockTestImageCtx>::s_instance = nullptr;
template <>
struct AttachParentRequest<MockTestImageCtx> {
Context* on_finish = nullptr;
static AttachParentRequest* s_instance;
static AttachParentRequest* create(MockTestImageCtx&,
const cls::rbd::ParentImageSpec& pspec,
uint64_t parent_overlap, bool reattach,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
AttachParentRequest() {
s_instance = this;
}
};
AttachParentRequest<MockTestImageCtx>* AttachParentRequest<MockTestImageCtx>::s_instance = nullptr;
template <>
struct CreateRequest<MockTestImageCtx> {
Context* on_finish = nullptr;
static CreateRequest* s_instance;
static CreateRequest* create(const ConfigProxy& config, IoCtx &ioctx,
const std::string &image_name,
const std::string &image_id, uint64_t size,
const ImageOptions &image_options,
bool skip_mirror_enable,
cls::rbd::MirrorImageMode mode,
const std::string &non_primary_global_image_id,
const std::string &primary_mirror_uuid,
asio::ContextWQ *op_work_queue,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
CreateRequest() {
s_instance = this;
}
};
CreateRequest<MockTestImageCtx>* CreateRequest<MockTestImageCtx>::s_instance = nullptr;
template <>
struct RemoveRequest<MockTestImageCtx> {
Context* on_finish = nullptr;
static RemoveRequest* s_instance;
static RemoveRequest* create(librados::IoCtx &ioctx,
const std::string &image_name,
const std::string &image_id,
bool force, bool from_trash_remove,
ProgressContext &prog_ctx,
asio::ContextWQ *op_work_queue,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
RemoveRequest() {
s_instance = this;
}
};
RemoveRequest<MockTestImageCtx>* RemoveRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace image
namespace mirror {
template <>
struct EnableRequest<MockTestImageCtx> {
Context* on_finish = nullptr;
static EnableRequest* s_instance;
static EnableRequest* create(MockTestImageCtx* image_ctx,
cls::rbd::MirrorImageMode mode,
const std::string &non_primary_global_image_id,
bool image_clean, Context *on_finish) {
ceph_assert(s_instance != nullptr);
EXPECT_TRUE(image_clean);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
EnableRequest() {
s_instance = this;
}
};
EnableRequest<MockTestImageCtx>* EnableRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace mirror
} // namespace librbd
// template definitions
#include "librbd/image/CloneRequest.cc"
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMockImageCloneRequest : public TestMockFixture {
public:
typedef CloneRequest<MockTestImageCtx> MockCloneRequest;
typedef AttachChildRequest<MockTestImageCtx> MockAttachChildRequest;
typedef AttachParentRequest<MockTestImageCtx> MockAttachParentRequest;
typedef CreateRequest<MockTestImageCtx> MockCreateRequest;
typedef RemoveRequest<MockTestImageCtx> MockRemoveRequest;
typedef deep_copy::MetadataCopyRequest<MockTestImageCtx> MockMetadataCopyRequest;
typedef mirror::EnableRequest<MockTestImageCtx> MockMirrorEnableRequest;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, _rados.conf_set("rbd_default_clone_format", "2"));
ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
NoOpProgressContext prog_ctx;
ASSERT_EQ(0, image_ctx->operations->snap_create(
cls::rbd::UserSnapshotNamespace{}, "snap", 0, prog_ctx));
if (is_feature_enabled(RBD_FEATURE_LAYERING)) {
ASSERT_EQ(0, image_ctx->operations->snap_protect(
cls::rbd::UserSnapshotNamespace{}, "snap"));
uint64_t snap_id = image_ctx->snap_ids[
{cls::rbd::UserSnapshotNamespace{}, "snap"}];
ASSERT_NE(CEPH_NOSNAP, snap_id);
C_SaferCond ctx;
image_ctx->state->snap_set(snap_id, &ctx);
ASSERT_EQ(0, ctx.wait());
}
}
void expect_get_min_compat_client(int8_t min_compat_client, int r) {
auto mock_rados_client = get_mock_io_ctx(m_ioctx).get_mock_rados_client();
EXPECT_CALL(*mock_rados_client, get_min_compatible_client(_, _))
.WillOnce(Invoke([min_compat_client, r](int8_t* min, int8_t* required_min) {
*min = min_compat_client;
*required_min = min_compat_client;
return r;
}));
}
void expect_get_image_size(MockTestImageCtx &mock_image_ctx, uint64_t snap_id,
uint64_t size) {
EXPECT_CALL(mock_image_ctx, get_image_size(snap_id))
.WillOnce(Return(size));
}
void expect_is_snap_protected(MockImageCtx &mock_image_ctx, bool is_protected,
int r) {
EXPECT_CALL(mock_image_ctx, is_snap_protected(_, _))
.WillOnce(WithArg<1>(Invoke([is_protected, r](bool* is_prot) {
*is_prot = is_protected;
return r;
})));
}
void expect_create(MockCreateRequest& mock_create_request, int r) {
EXPECT_CALL(mock_create_request, send())
.WillOnce(Invoke([this, &mock_create_request, r]() {
image_ctx->op_work_queue->queue(mock_create_request.on_finish, r);
}));
}
void expect_open(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.state, open(true, _))
.WillOnce(WithArg<1>(Invoke([this, r](Context* ctx) {
image_ctx->op_work_queue->queue(ctx, r);
})));
}
void expect_attach_parent(MockAttachParentRequest& mock_request, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(Invoke([this, &mock_request, r]() {
image_ctx->op_work_queue->queue(mock_request.on_finish, r);
}));
}
void expect_attach_child(MockAttachChildRequest& mock_request,
uint32_t clone_format, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(Invoke([this, &mock_request, clone_format, r]() {
EXPECT_EQ(mock_request.clone_format, clone_format);
image_ctx->op_work_queue->queue(mock_request.on_finish, r);
}));
}
void expect_metadata_copy(MockMetadataCopyRequest& mock_request, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(Invoke([this, &mock_request, r]() {
image_ctx->op_work_queue->queue(mock_request.on_finish, r);
}));
}
void expect_test_features(MockTestImageCtx &mock_image_ctx,
uint64_t features, bool enabled) {
EXPECT_CALL(mock_image_ctx, test_features(features))
.WillOnce(Return(enabled));
}
void expect_mirror_mode_get(MockTestImageCtx &mock_image_ctx,
cls::rbd::MirrorMode mirror_mode, int r) {
bufferlist out_bl;
encode(static_cast<uint32_t>(mirror_mode), out_bl);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(RBD_MIRRORING, _, StrEq("rbd"), StrEq("mirror_mode_get"),
_, _, _, _))
.WillOnce(WithArg<5>(Invoke([out_bl, r](bufferlist* out) {
*out = out_bl;
return r;
})));
}
void expect_mirror_enable(MockMirrorEnableRequest& mock_mirror_enable_request,
int r) {
EXPECT_CALL(mock_mirror_enable_request, send())
.WillOnce(Invoke([this, &mock_mirror_enable_request, r]() {
image_ctx->op_work_queue->queue(mock_mirror_enable_request.on_finish, r);
}));
}
void expect_close(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.state, close(_))
.WillOnce(Invoke([this, r](Context* ctx) {
image_ctx->op_work_queue->queue(ctx, r);
}));
}
void expect_remove(MockRemoveRequest& mock_remove_request, int r) {
EXPECT_CALL(mock_remove_request, send())
.WillOnce(Invoke([this, &mock_remove_request, r]() {
image_ctx->op_work_queue->queue(mock_remove_request.on_finish, r);
}));
}
librbd::ImageCtx *image_ctx;
};
TEST_F(TestMockImageCloneRequest, SuccessV1) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, _rados.conf_set("rbd_default_clone_format", "1"));
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 1, 0);
MockMetadataCopyRequest mock_request;
expect_metadata_copy(mock_request, 0);
MockMirrorEnableRequest mock_mirror_enable_request;
if (is_feature_enabled(RBD_FEATURE_JOURNALING)) {
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, true);
expect_mirror_mode_get(mock_image_ctx, cls::rbd::MIRROR_MODE_POOL, 0);
expect_mirror_enable(mock_mirror_enable_request, 0);
} else {
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, false);
}
expect_close(mock_image_ctx, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, SuccessV2) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, _rados.conf_set("rbd_default_clone_format", "2"));
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 2, 0);
MockMetadataCopyRequest mock_request;
expect_metadata_copy(mock_request, 0);
MockMirrorEnableRequest mock_mirror_enable_request;
if (is_feature_enabled(RBD_FEATURE_JOURNALING)) {
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, true);
expect_mirror_mode_get(mock_image_ctx, cls::rbd::MIRROR_MODE_POOL, 0);
expect_mirror_enable(mock_mirror_enable_request, 0);
} else {
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, false);
}
expect_close(mock_image_ctx, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, SuccessAuto) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
ASSERT_EQ(0, _rados.conf_set("rbd_default_clone_format", "auto"));
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 2, 0);
MockMetadataCopyRequest mock_request;
expect_metadata_copy(mock_request, 0);
MockMirrorEnableRequest mock_mirror_enable_request;
if (is_feature_enabled(RBD_FEATURE_JOURNALING)) {
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, true);
expect_mirror_mode_get(mock_image_ctx, cls::rbd::MIRROR_MODE_POOL, 0);
expect_mirror_enable(mock_mirror_enable_request, 0);
} else {
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, false);
}
expect_close(mock_image_ctx, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, OpenParentError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, -EINVAL);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, CreateError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, -EINVAL);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, OpenError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, -EINVAL);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, AttachParentError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, -EINVAL);
expect_close(mock_image_ctx, 0);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, AttachChildError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 2, -EINVAL);
expect_close(mock_image_ctx, 0);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, MetadataCopyError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 2, 0);
MockMetadataCopyRequest mock_request;
expect_metadata_copy(mock_request, -EINVAL);
expect_close(mock_image_ctx, 0);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, GetMirrorModeError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_JOURNALING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 2, 0);
MockMetadataCopyRequest mock_request;
expect_metadata_copy(mock_request, 0);
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, true);
expect_mirror_mode_get(mock_image_ctx, cls::rbd::MIRROR_MODE_POOL, -EINVAL);
expect_close(mock_image_ctx, 0);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, MirrorEnableError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_JOURNALING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 2, 0);
MockMetadataCopyRequest mock_request;
expect_metadata_copy(mock_request, 0);
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, true);
expect_mirror_mode_get(mock_image_ctx, cls::rbd::MIRROR_MODE_POOL, 0);
MockMirrorEnableRequest mock_mirror_enable_request;
expect_mirror_enable(mock_mirror_enable_request, -EINVAL);
expect_close(mock_image_ctx, 0);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, CloseError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 2, 0);
MockMetadataCopyRequest mock_request;
expect_metadata_copy(mock_request, 0);
expect_test_features(mock_image_ctx, RBD_FEATURE_JOURNALING, false);
expect_close(mock_image_ctx, -EINVAL);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, RemoveError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, -EINVAL);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, -EPERM);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, CloseParentError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, -EINVAL);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
expect_close(mock_image_ctx, -EPERM);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL, "", "",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImageCloneRequest, SnapshotMirrorEnableNonPrimary) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_open(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, mock_image_ctx.snaps.front(), 123);
expect_is_snap_protected(mock_image_ctx, true, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
expect_open(mock_image_ctx, 0);
MockAttachParentRequest mock_attach_parent_request;
expect_attach_parent(mock_attach_parent_request, 0);
MockAttachChildRequest mock_attach_child_request;
expect_attach_child(mock_attach_child_request, 2, 0);
MockMetadataCopyRequest mock_request;
expect_metadata_copy(mock_request, 0);
MockMirrorEnableRequest mock_mirror_enable_request;
expect_mirror_enable(mock_mirror_enable_request, 0);
expect_close(mock_image_ctx, 0);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
ImageOptions clone_opts;
auto req = new MockCloneRequest(m_cct->_conf, m_ioctx, "parent id", "", {}, 123,
m_ioctx, "clone name", "clone id", clone_opts,
cls::rbd::MIRROR_IMAGE_MODE_JOURNAL,
"global image id", "primary mirror uuid",
image_ctx->op_work_queue, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
} // namespace image
} // namespace librbd
| 32,156 | 32.462019 | 99 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_DetachChildRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockContextWQ.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "librbd/image/TypeTraits.h"
#include "librbd/image/DetachChildRequest.h"
#include "librbd/trash/RemoveRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
static MockTestImageCtx* s_instance;
static MockTestImageCtx* create(const std::string &image_name,
const std::string &image_id,
const char *snap, librados::IoCtx& p,
bool read_only) {
ceph_assert(s_instance != nullptr);
return s_instance;
}
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
s_instance = this;
}
};
MockTestImageCtx* MockTestImageCtx::s_instance = nullptr;
} // anonymous namespace
namespace image {
template <>
struct TypeTraits<MockTestImageCtx> {
typedef librbd::MockContextWQ ContextWQ;
};
} // namespace image
namespace trash {
template <>
class RemoveRequest<MockTestImageCtx> {
private:
typedef ::librbd::image::TypeTraits<MockTestImageCtx> TypeTraits;
typedef typename TypeTraits::ContextWQ ContextWQ;
public:
static RemoveRequest *s_instance;
static RemoveRequest *create(librados::IoCtx &ioctx,
MockTestImageCtx *image_ctx,
ContextWQ *op_work_queue, bool force,
ProgressContext &prog_ctx, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
Context *on_finish = nullptr;
RemoveRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
RemoveRequest<MockTestImageCtx> *RemoveRequest<MockTestImageCtx>::s_instance;
} // namespace trash
} // namespace librbd
// template definitions
#include "librbd/image/DetachChildRequest.cc"
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMockImageDetachChildRequest : public TestMockFixture {
public:
typedef DetachChildRequest<MockTestImageCtx> MockDetachChildRequest;
typedef trash::RemoveRequest<MockTestImageCtx> MockTrashRemoveRequest;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
}
void expect_test_op_features(MockTestImageCtx& mock_image_ctx, bool enabled) {
EXPECT_CALL(mock_image_ctx,
test_op_features(RBD_OPERATION_FEATURE_CLONE_CHILD))
.WillOnce(Return(enabled));
}
void expect_create_ioctx(MockImageCtx &mock_image_ctx,
librados::MockTestMemIoCtxImpl **io_ctx_impl) {
*io_ctx_impl = &get_mock_io_ctx(mock_image_ctx.md_ctx);
auto rados_client = (*io_ctx_impl)->get_mock_rados_client();
EXPECT_CALL(*rados_client, create_ioctx(_, _))
.WillOnce(DoAll(GetReference(*io_ctx_impl), Return(*io_ctx_impl)));
}
void expect_child_detach(MockImageCtx &mock_image_ctx,
librados::MockTestMemIoCtxImpl &mock_io_ctx_impl,
int r) {
auto& parent_spec = mock_image_ctx.parent_md.spec;
bufferlist bl;
encode(parent_spec.snap_id, bl);
encode(cls::rbd::ChildImageSpec{mock_image_ctx.md_ctx.get_id(), "",
mock_image_ctx.id}, bl);
EXPECT_CALL(mock_io_ctx_impl,
exec(util::header_name(parent_spec.image_id),
_, StrEq("rbd"), StrEq("child_detach"), ContentsEqual(bl),
_, _, _))
.WillOnce(Return(r));
}
void expect_remove_child(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(RBD_CHILDREN, _, StrEq("rbd"), StrEq("remove_child"), _,
_, _, _))
.WillOnce(Return(r));
}
void expect_snapshot_get(MockImageCtx &mock_image_ctx,
librados::MockTestMemIoCtxImpl &mock_io_ctx_impl,
const std::string& parent_header_name,
const cls::rbd::SnapshotInfo& snap_info, int r) {
using ceph::encode;
EXPECT_CALL(mock_io_ctx_impl,
exec(parent_header_name, _, StrEq("rbd"),
StrEq("snapshot_get"), _, _, _, _))
.WillOnce(WithArg<5>(Invoke([snap_info, r](bufferlist* bl) {
encode(snap_info, *bl);
return r;
})));
}
void expect_open(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.state, open(true, _))
.WillOnce(WithArg<1>(Invoke([this, &mock_image_ctx, r](Context* ctx) {
EXPECT_EQ(0U, mock_image_ctx.read_only_mask &
IMAGE_READ_ONLY_FLAG_NON_PRIMARY);
image_ctx->op_work_queue->queue(ctx, r);
})));
if (r == 0) {
EXPECT_CALL(mock_image_ctx, test_features(_))
.WillOnce(Return(false));
}
}
void expect_close(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.state, close(_))
.WillOnce(Invoke([this, r](Context* ctx) {
image_ctx->op_work_queue->queue(ctx, r);
}));
}
void expect_snap_remove(MockImageCtx &mock_image_ctx,
const std::string &snap_name, int r) {
EXPECT_CALL(*mock_image_ctx.operations,
snap_remove({cls::rbd::TrashSnapshotNamespace{}},
StrEq(snap_name), _))
.WillOnce(WithArg<2>(Invoke([this, r](Context *ctx) {
image_ctx->op_work_queue->queue(ctx, r);
})));
}
void expect_trash_get(MockImageCtx &mock_image_ctx,
librados::MockTestMemIoCtxImpl &mock_io_ctx_impl,
const cls::rbd::TrashImageSpec& trash_spec,
int r) {
using ceph::encode;
EXPECT_CALL(mock_io_ctx_impl,
exec(RBD_TRASH, _, StrEq("rbd"),
StrEq("trash_get"), _, _, _, _))
.WillOnce(WithArg<5>(Invoke([trash_spec, r](bufferlist* bl) {
encode(trash_spec, *bl);
return r;
})));
}
void expect_trash_remove(MockTrashRemoveRequest& mock_trash_remove_request,
int r) {
EXPECT_CALL(mock_trash_remove_request, send())
.WillOnce(Invoke([&mock_trash_remove_request, r]() {
mock_trash_remove_request.on_finish->complete(r);
}));
}
librbd::ImageCtx *image_ctx;
};
TEST_F(TestMockImageDetachChildRequest, SuccessV1) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, false);
expect_remove_child(mock_image_ctx, 0);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, SuccessV2) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, true);
librados::MockTestMemIoCtxImpl *mock_io_ctx_impl;
expect_create_ioctx(mock_image_ctx, &mock_io_ctx_impl);
expect_child_detach(mock_image_ctx, *mock_io_ctx_impl, 0);
expect_snapshot_get(mock_image_ctx, *mock_io_ctx_impl,
"rbd_header.parent id",
{234, {cls::rbd::UserSnapshotNamespace{}},
"snap1", 123, {}, 0}, 0);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, TrashedSnapshotSuccess) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, true);
librados::MockTestMemIoCtxImpl *mock_io_ctx_impl;
expect_create_ioctx(mock_image_ctx, &mock_io_ctx_impl);
expect_child_detach(mock_image_ctx, *mock_io_ctx_impl, 0);
expect_snapshot_get(mock_image_ctx, *mock_io_ctx_impl,
"rbd_header.parent id",
{234, {cls::rbd::TrashSnapshotNamespace{}},
"snap1", 123, {}, 0}, 0);
expect_open(mock_image_ctx, 0);
expect_snap_remove(mock_image_ctx, "snap1", 0);
const cls::rbd::TrashImageSpec trash_spec;
expect_trash_get(mock_image_ctx, *mock_io_ctx_impl, trash_spec, -ENOENT);
expect_close(mock_image_ctx, 0);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, ParentAutoRemove) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, true);
librados::MockTestMemIoCtxImpl *mock_io_ctx_impl;
expect_create_ioctx(mock_image_ctx, &mock_io_ctx_impl);
expect_child_detach(mock_image_ctx, *mock_io_ctx_impl, 0);
expect_snapshot_get(mock_image_ctx, *mock_io_ctx_impl,
"rbd_header.parent id",
{234, {cls::rbd::TrashSnapshotNamespace{}},
"snap1", 123, {}, 0}, 0);
expect_open(mock_image_ctx, 0);
expect_snap_remove(mock_image_ctx, "snap1", 0);
const cls::rbd::TrashImageSpec trash_spec =
{cls::rbd::TRASH_IMAGE_SOURCE_USER_PARENT, "parent", {}, {}};
expect_trash_get(mock_image_ctx, *mock_io_ctx_impl, trash_spec, 0);
MockTrashRemoveRequest mock_trash_remove_request;
expect_trash_remove(mock_trash_remove_request, 0);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, TrashedSnapshotInUse) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, true);
librados::MockTestMemIoCtxImpl *mock_io_ctx_impl;
expect_create_ioctx(mock_image_ctx, &mock_io_ctx_impl);
expect_child_detach(mock_image_ctx, *mock_io_ctx_impl, 0);
expect_snapshot_get(mock_image_ctx, *mock_io_ctx_impl,
"rbd_header.parent id",
{234, {cls::rbd::TrashSnapshotNamespace{}},
"snap1", 123, {}, 1}, 0);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, TrashedSnapshotSnapshotGetError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, true);
librados::MockTestMemIoCtxImpl *mock_io_ctx_impl;
expect_create_ioctx(mock_image_ctx, &mock_io_ctx_impl);
expect_child_detach(mock_image_ctx, *mock_io_ctx_impl, 0);
expect_snapshot_get(mock_image_ctx, *mock_io_ctx_impl,
"rbd_header.parent id",
{234, {cls::rbd::TrashSnapshotNamespace{}},
"snap1", 123, {}, 0}, -EINVAL);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, TrashedSnapshotOpenParentError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, true);
librados::MockTestMemIoCtxImpl *mock_io_ctx_impl;
expect_create_ioctx(mock_image_ctx, &mock_io_ctx_impl);
expect_child_detach(mock_image_ctx, *mock_io_ctx_impl, 0);
expect_snapshot_get(mock_image_ctx, *mock_io_ctx_impl,
"rbd_header.parent id",
{234, {cls::rbd::TrashSnapshotNamespace{}},
"snap1", 123, {}, 0}, 0);
expect_open(mock_image_ctx, -EPERM);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, TrashedSnapshotRemoveError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, true);
librados::MockTestMemIoCtxImpl *mock_io_ctx_impl;
expect_create_ioctx(mock_image_ctx, &mock_io_ctx_impl);
expect_child_detach(mock_image_ctx, *mock_io_ctx_impl, 0);
expect_snapshot_get(mock_image_ctx, *mock_io_ctx_impl,
"rbd_header.parent id",
{234, {cls::rbd::TrashSnapshotNamespace{}},
"snap1", 123, {}, 0}, 0);
expect_open(mock_image_ctx, 0);
expect_snap_remove(mock_image_ctx, "snap1", -EPERM);
expect_close(mock_image_ctx, -EPERM);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, ParentDNE) {
MockTestImageCtx mock_image_ctx(*image_ctx);
expect_op_work_queue(mock_image_ctx);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, ChildDetachError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, true);
librados::MockTestMemIoCtxImpl *mock_io_ctx_impl;
expect_create_ioctx(mock_image_ctx, &mock_io_ctx_impl);
expect_child_detach(mock_image_ctx, *mock_io_ctx_impl, -EPERM);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockImageDetachChildRequest, RemoveChildError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
mock_image_ctx.parent_md.spec = {m_ioctx.get_id(), "", "parent id", 234};
InSequence seq;
expect_test_op_features(mock_image_ctx, false);
expect_remove_child(mock_image_ctx, -EINVAL);
C_SaferCond ctx;
auto req = MockDetachChildRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
} // namespace image
} // namespace librbd
| 15,583 | 33.250549 | 80 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_DetachParentRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockContextWQ.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "librbd/image/DetachParentRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
} // namespace librbd
// template definitions
#include "librbd/image/DetachParentRequest.cc"
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrEq;
class TestMockImageDetachParentRequest : public TestMockFixture {
public:
typedef DetachParentRequest<MockTestImageCtx> MockDetachParentRequest;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
}
void expect_parent_detach(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("parent_detach"), _, _, _, _))
.WillOnce(Return(r));
}
void expect_remove_parent(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("remove_parent"), _, _, _, _))
.WillOnce(Return(r));
}
librbd::ImageCtx *image_ctx;
};
TEST_F(TestMockImageDetachParentRequest, ParentDetachSuccess) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_detach(mock_image_ctx, 0);
C_SaferCond ctx;
auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachParentRequest, RemoveParentSuccess) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_detach(mock_image_ctx, -EOPNOTSUPP);
expect_remove_parent(mock_image_ctx, 0);
C_SaferCond ctx;
auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachParentRequest, ParentDNE) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_detach(mock_image_ctx, -ENOENT);
C_SaferCond ctx;
auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageDetachParentRequest, ParentDetachError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_detach(mock_image_ctx, -EPERM);
C_SaferCond ctx;
auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockImageDetachParentRequest, RemoveParentError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
MockTestImageCtx mock_image_ctx(*image_ctx);
InSequence seq;
expect_parent_detach(mock_image_ctx, -EOPNOTSUPP);
expect_remove_parent(mock_image_ctx, -EINVAL);
C_SaferCond ctx;
auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
} // namespace image
} // namespace librbd
| 3,602 | 25.492647 | 72 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_ListWatchersRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "librbd/image/ListWatchersRequest.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "test/librbd/mock/MockContextWQ.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/test_support.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public librbd::MockImageCtx {
MockTestImageCtx(librbd::ImageCtx &image_ctx)
: librbd::MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
} // namespace librbd
// template definitions
#include "librbd/image/ListWatchersRequest.cc"
template class librbd::image::ListWatchersRequest<librbd::MockImageCtx>;
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::SetArgPointee;
using ::testing::StrEq;
class TestMockListWatchersRequest : public TestMockFixture {
public:
typedef ListWatchersRequest<MockImageCtx> MockListWatchersRequest;
obj_watch_t watcher(const std::string &address, uint64_t watch_handle) {
obj_watch_t w;
strcpy(w.addr, address.c_str());
w.watcher_id = 0;
w.cookie = watch_handle;
w.timeout_seconds = 0;
return w;
}
void expect_list_watchers(MockTestImageCtx &mock_image_ctx,
const std::string oid,
const std::list<obj_watch_t> &watchers, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
list_watchers(oid, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoAll(SetArgPointee<1>(watchers), Return(0)));
}
}
void expect_list_image_watchers(MockTestImageCtx &mock_image_ctx,
const std::list<obj_watch_t> &watchers,
int r) {
expect_list_watchers(mock_image_ctx, mock_image_ctx.header_oid,
watchers, r);
}
void expect_list_mirror_watchers(MockTestImageCtx &mock_image_ctx,
const std::list<obj_watch_t> &watchers,
int r) {
expect_list_watchers(mock_image_ctx, RBD_MIRRORING, watchers, r);
}
void expect_get_watch_handle(MockImageWatcher &mock_watcher,
uint64_t watch_handle) {
EXPECT_CALL(mock_watcher, get_watch_handle())
.WillOnce(Return(watch_handle));
}
};
TEST_F(TestMockListWatchersRequest, NoImageWatchers) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageWatcher mock_watcher;
InSequence seq;
expect_list_image_watchers(mock_image_ctx, {}, 0);
std::list<obj_watch_t> watchers;
C_SaferCond ctx;
auto req = MockListWatchersRequest::create(mock_image_ctx, 0, &watchers,
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_TRUE(watchers.empty());
}
TEST_F(TestMockListWatchersRequest, Error) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageWatcher mock_watcher;
InSequence seq;
expect_list_image_watchers(mock_image_ctx, {}, -EINVAL);
std::list<obj_watch_t> watchers;
C_SaferCond ctx;
auto req = MockListWatchersRequest::create(mock_image_ctx, 0, &watchers,
&ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockListWatchersRequest, Success) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageWatcher mock_watcher;
InSequence seq;
expect_list_image_watchers(mock_image_ctx,
{watcher("a", 123), watcher("b", 456)}, 0);
expect_get_watch_handle(*mock_image_ctx.image_watcher, 123);
std::list<obj_watch_t> watchers;
C_SaferCond ctx;
auto req = MockListWatchersRequest::create(mock_image_ctx, 0, &watchers,
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(2U, watchers.size());
auto w = watchers.begin();
ASSERT_STREQ("a", w->addr);
ASSERT_EQ(123U, w->cookie);
w++;
ASSERT_STREQ("b", w->addr);
ASSERT_EQ(456U, w->cookie);
}
TEST_F(TestMockListWatchersRequest, FilterOutMyInstance) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageWatcher mock_watcher;
InSequence seq;
expect_list_image_watchers(mock_image_ctx,
{watcher("a", 123), watcher("b", 456)}, 0);
expect_get_watch_handle(*mock_image_ctx.image_watcher, 123);
std::list<obj_watch_t> watchers;
C_SaferCond ctx;
auto req = MockListWatchersRequest::create(
mock_image_ctx, LIST_WATCHERS_FILTER_OUT_MY_INSTANCE, &watchers, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(1U, watchers.size());
ASSERT_STREQ("b", watchers.begin()->addr);
ASSERT_EQ(456U, watchers.begin()->cookie);
}
TEST_F(TestMockListWatchersRequest, FilterOutMirrorInstance) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageWatcher mock_watcher;
InSequence seq;
expect_list_image_watchers(mock_image_ctx,
{watcher("a", 123), watcher("b", 456)}, 0);
expect_list_mirror_watchers(mock_image_ctx, {watcher("b", 789)}, 0);
expect_get_watch_handle(*mock_image_ctx.image_watcher, 123);
std::list<obj_watch_t> watchers;
C_SaferCond ctx;
auto req = MockListWatchersRequest::create(
mock_image_ctx, LIST_WATCHERS_FILTER_OUT_MIRROR_INSTANCES, &watchers,
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(1U, watchers.size());
ASSERT_STREQ("a", watchers.begin()->addr);
ASSERT_EQ(123U, watchers.begin()->cookie);
}
} // namespace image
} // namespace librbd
| 6,221 | 28.211268 | 77 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_PreRemoveRequest.cc
|
// -*- mode:c++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "librbd/ImageState.h"
#include "librbd/Operations.h"
#include "librbd/image/ListWatchersRequest.h"
#include "librbd/image/PreRemoveRequest.h"
#include "librbd/image/RefreshParentRequest.h"
#include "librbd/operation/SnapshotRemoveRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <arpa/inet.h>
#include <list>
#include <boost/scope_exit.hpp>
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace operation {
template <>
class SnapshotRemoveRequest<MockTestImageCtx> {
public:
static SnapshotRemoveRequest *s_instance;
static SnapshotRemoveRequest *create(MockTestImageCtx &image_ctx,
cls::rbd::SnapshotNamespace sn,
std::string name,
uint64_t id, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
Context *on_finish = nullptr;
SnapshotRemoveRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
SnapshotRemoveRequest<MockTestImageCtx> *SnapshotRemoveRequest<MockTestImageCtx>::s_instance;
} // namespace operation
namespace image {
template<>
class ListWatchersRequest<MockTestImageCtx> {
public:
static ListWatchersRequest *s_instance;
Context *on_finish = nullptr;
static ListWatchersRequest *create(MockTestImageCtx &image_ctx, int flags,
std::list<obj_watch_t> *watchers,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
ListWatchersRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
ListWatchersRequest<MockTestImageCtx> *ListWatchersRequest<MockTestImageCtx>::s_instance;
} // namespace image
} // namespace librbd
// template definitions
#include "librbd/exclusive_lock/StandardPolicy.cc"
#include "librbd/image/PreRemoveRequest.cc"
ACTION_P(TestFeatures, image_ctx) {
return ((image_ctx->features & arg0) != 0);
}
ACTION_P(ShutDownExclusiveLock, image_ctx) {
// shutting down exclusive lock will close object map and journal
image_ctx->exclusive_lock = nullptr;
image_ctx->object_map = nullptr;
image_ctx->journal = nullptr;
}
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
class TestMockImagePreRemoveRequest : public TestMockFixture {
public:
typedef PreRemoveRequest<MockTestImageCtx> MockPreRemoveRequest;
typedef ListWatchersRequest<MockTestImageCtx> MockListWatchersRequest;
typedef librbd::operation::SnapshotRemoveRequest<MockTestImageCtx> MockSnapshotRemoveRequest;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_test_imctx));
m_mock_imctx = new MockTestImageCtx(*m_test_imctx);
}
void TearDown() override {
delete m_mock_imctx;
TestMockFixture::TearDown();
}
void expect_test_features(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, test_features(_))
.WillRepeatedly(TestFeatures(&mock_image_ctx));
}
void expect_set_exclusive_lock_policy(MockTestImageCtx& mock_image_ctx) {
if (m_mock_imctx->exclusive_lock != nullptr) {
EXPECT_CALL(mock_image_ctx, set_exclusive_lock_policy(_))
.WillOnce(Invoke([](exclusive_lock::Policy* policy) {
ASSERT_FALSE(policy->may_auto_request_lock());
delete policy;
}));
}
}
void expect_set_journal_policy(MockTestImageCtx &mock_image_ctx) {
if (m_test_imctx->test_features(RBD_FEATURE_JOURNALING)) {
EXPECT_CALL(mock_image_ctx, set_journal_policy(_))
.WillOnce(Invoke([](journal::Policy* policy) {
ASSERT_TRUE(policy->journal_disabled());
delete policy;
}));
}
}
void expect_acquire_exclusive_lock(MockTestImageCtx &mock_image_ctx,
MockExclusiveLock &mock_exclusive_lock,
int r) {
if (m_mock_imctx->exclusive_lock != nullptr) {
EXPECT_CALL(mock_exclusive_lock, acquire_lock(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
}
void expect_shut_down_exclusive_lock(MockTestImageCtx &mock_image_ctx,
MockExclusiveLock &mock_exclusive_lock,
int r) {
if (m_mock_imctx->exclusive_lock != nullptr) {
EXPECT_CALL(mock_exclusive_lock, shut_down(_))
.WillOnce(DoAll(ShutDownExclusiveLock(&mock_image_ctx),
CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue)));
}
}
void expect_is_exclusive_lock_owner(MockTestImageCtx &mock_image_ctx,
MockExclusiveLock &mock_exclusive_lock,
bool is_owner) {
if (m_mock_imctx->exclusive_lock != nullptr) {
EXPECT_CALL(mock_exclusive_lock, is_lock_owner()).WillOnce(Return(is_owner));
}
}
void expect_list_image_watchers(
MockTestImageCtx &mock_image_ctx,
MockListWatchersRequest &mock_list_watchers_request, int r) {
EXPECT_CALL(mock_list_watchers_request, send())
.WillOnce(FinishRequest(&mock_list_watchers_request, r, &mock_image_ctx));
}
void expect_get_group(MockTestImageCtx &mock_image_ctx, int r) {
if (mock_image_ctx.old_format) {
return;
}
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("image_group_get"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_remove_snap(MockTestImageCtx &mock_image_ctx,
MockSnapshotRemoveRequest& mock_snap_remove_request,
int r) {
EXPECT_CALL(mock_snap_remove_request, send())
.WillOnce(FinishRequest(&mock_snap_remove_request, r, &mock_image_ctx));
}
librbd::ImageCtx *m_test_imctx = nullptr;
MockTestImageCtx *m_mock_imctx = nullptr;
};
TEST_F(TestMockImagePreRemoveRequest, Success) {
MockExclusiveLock mock_exclusive_lock;
if (m_test_imctx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
m_mock_imctx->exclusive_lock = &mock_exclusive_lock;
}
expect_op_work_queue(*m_mock_imctx);
expect_test_features(*m_mock_imctx);
InSequence seq;
expect_set_exclusive_lock_policy(*m_mock_imctx);
expect_set_journal_policy(*m_mock_imctx);
expect_acquire_exclusive_lock(*m_mock_imctx, mock_exclusive_lock, 0);
expect_is_exclusive_lock_owner(*m_mock_imctx, mock_exclusive_lock, true);
MockListWatchersRequest mock_list_watchers_request;
expect_list_image_watchers(*m_mock_imctx, mock_list_watchers_request, 0);
expect_get_group(*m_mock_imctx, 0);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, OperationsDisabled) {
REQUIRE_FORMAT_V2();
m_mock_imctx->operations_disabled = true;
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(-EROFS, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, ExclusiveLockTryAcquireFailed) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
MockExclusiveLock mock_exclusive_lock;
m_mock_imctx->exclusive_lock = &mock_exclusive_lock;
expect_op_work_queue(*m_mock_imctx);
expect_test_features(*m_mock_imctx);
InSequence seq;
expect_set_exclusive_lock_policy(*m_mock_imctx);
expect_set_journal_policy(*m_mock_imctx);
expect_acquire_exclusive_lock(*m_mock_imctx, mock_exclusive_lock,
-EINVAL);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(-EBUSY, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, ExclusiveLockTryAcquireNotLockOwner) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
MockExclusiveLock mock_exclusive_lock;
m_mock_imctx->exclusive_lock = &mock_exclusive_lock;
expect_op_work_queue(*m_mock_imctx);
expect_test_features(*m_mock_imctx);
InSequence seq;
expect_set_exclusive_lock_policy(*m_mock_imctx);
expect_set_journal_policy(*m_mock_imctx);
expect_acquire_exclusive_lock(*m_mock_imctx, mock_exclusive_lock, 0);
expect_is_exclusive_lock_owner(*m_mock_imctx, mock_exclusive_lock, false);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(-EBUSY, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, Force) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
MockExclusiveLock mock_exclusive_lock;
m_mock_imctx->exclusive_lock = &mock_exclusive_lock;
expect_op_work_queue(*m_mock_imctx);
expect_test_features(*m_mock_imctx);
InSequence seq;
expect_set_exclusive_lock_policy(*m_mock_imctx);
expect_set_journal_policy(*m_mock_imctx);
expect_acquire_exclusive_lock(*m_mock_imctx, mock_exclusive_lock,
-EINVAL);
expect_shut_down_exclusive_lock(*m_mock_imctx, mock_exclusive_lock, 0);
MockListWatchersRequest mock_list_watchers_request;
expect_list_image_watchers(*m_mock_imctx, mock_list_watchers_request, 0);
expect_get_group(*m_mock_imctx, 0);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, true, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, ExclusiveLockShutDownFailed) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
MockExclusiveLock mock_exclusive_lock;
m_mock_imctx->exclusive_lock = &mock_exclusive_lock;
expect_op_work_queue(*m_mock_imctx);
expect_test_features(*m_mock_imctx);
InSequence seq;
expect_set_exclusive_lock_policy(*m_mock_imctx);
expect_set_journal_policy(*m_mock_imctx);
expect_acquire_exclusive_lock(*m_mock_imctx, mock_exclusive_lock, -EINVAL);
expect_shut_down_exclusive_lock(*m_mock_imctx, mock_exclusive_lock, -EINVAL);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, true, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, Migration) {
m_mock_imctx->features |= RBD_FEATURE_MIGRATING;
expect_test_features(*m_mock_imctx);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(-EBUSY, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, Snapshots) {
m_mock_imctx->snap_info = {
{123, {"snap1", {cls::rbd::UserSnapshotNamespace{}}, {}, {}, {}, {}, {}}}};
expect_test_features(*m_mock_imctx);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(-ENOTEMPTY, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, Watchers) {
MockExclusiveLock mock_exclusive_lock;
if (m_test_imctx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
m_mock_imctx->exclusive_lock = &mock_exclusive_lock;
}
expect_op_work_queue(*m_mock_imctx);
expect_test_features(*m_mock_imctx);
InSequence seq;
expect_set_exclusive_lock_policy(*m_mock_imctx);
expect_set_journal_policy(*m_mock_imctx);
expect_acquire_exclusive_lock(*m_mock_imctx, mock_exclusive_lock, 0);
expect_is_exclusive_lock_owner(*m_mock_imctx, mock_exclusive_lock, true);
MockListWatchersRequest mock_list_watchers_request;
expect_list_image_watchers(*m_mock_imctx, mock_list_watchers_request,
-EINVAL);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, GroupError) {
REQUIRE_FORMAT_V2();
MockExclusiveLock mock_exclusive_lock;
if (m_test_imctx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
m_mock_imctx->exclusive_lock = &mock_exclusive_lock;
}
expect_op_work_queue(*m_mock_imctx);
expect_test_features(*m_mock_imctx);
InSequence seq;
expect_set_exclusive_lock_policy(*m_mock_imctx);
expect_set_journal_policy(*m_mock_imctx);
expect_acquire_exclusive_lock(*m_mock_imctx, mock_exclusive_lock, 0);
expect_is_exclusive_lock_owner(*m_mock_imctx, mock_exclusive_lock, true);
MockListWatchersRequest mock_list_watchers_request;
expect_list_image_watchers(*m_mock_imctx, mock_list_watchers_request, 0);
expect_get_group(*m_mock_imctx, -EINVAL);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockImagePreRemoveRequest, AutoDeleteSnapshots) {
REQUIRE_FORMAT_V2();
MockExclusiveLock mock_exclusive_lock;
if (m_test_imctx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
m_mock_imctx->exclusive_lock = &mock_exclusive_lock;
}
expect_op_work_queue(*m_mock_imctx);
expect_test_features(*m_mock_imctx);
m_mock_imctx->snap_info = {
{123, {"snap1", {cls::rbd::TrashSnapshotNamespace{}}, {}, {}, {}, {}, {}}}};
InSequence seq;
expect_set_exclusive_lock_policy(*m_mock_imctx);
expect_set_journal_policy(*m_mock_imctx);
expect_acquire_exclusive_lock(*m_mock_imctx, mock_exclusive_lock, 0);
expect_is_exclusive_lock_owner(*m_mock_imctx, mock_exclusive_lock, true);
MockListWatchersRequest mock_list_watchers_request;
expect_list_image_watchers(*m_mock_imctx, mock_list_watchers_request, 0);
expect_get_group(*m_mock_imctx, 0);
MockSnapshotRemoveRequest mock_snap_remove_request;
expect_remove_snap(*m_mock_imctx, mock_snap_remove_request, 0);
C_SaferCond ctx;
auto req = MockPreRemoveRequest::create(m_mock_imctx, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
} // namespace image
} // namespace librbd
| 14,514 | 30.148069 | 95 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_RefreshRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockImageWatcher.h"
#include "test/librbd/mock/MockJournal.h"
#include "test/librbd/mock/MockJournalPolicy.h"
#include "test/librbd/mock/MockObjectMap.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/Operations.h"
#include "librbd/api/Image.h"
#include "librbd/image/GetMetadataRequest.h"
#include "librbd/image/RefreshRequest.h"
#include "librbd/image/RefreshParentRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <arpa/inet.h>
#include <queue>
#include <boost/scope_exit.hpp>
namespace librbd {
namespace {
struct MockRefreshImageCtx : public MockImageCtx {
MockRefreshImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace image {
template <>
struct GetMetadataRequest<MockRefreshImageCtx> {
std::string oid;
std::map<std::string, bufferlist>* pairs = nullptr;
Context* on_finish = nullptr;
static GetMetadataRequest* s_instance;
static GetMetadataRequest* create(librados::IoCtx&,
const std::string& oid,
bool filter_internal,
const std::string& filter_key_prefix,
const std::string& last_key,
uint32_t max_results,
std::map<std::string, bufferlist>* pairs,
Context* on_finish) {
ceph_assert(s_instance != nullptr);
EXPECT_EQ("conf_", filter_key_prefix);
EXPECT_EQ("conf_", last_key);
s_instance->oid = oid;
s_instance->pairs = pairs;
s_instance->on_finish = on_finish;
return s_instance;
}
GetMetadataRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
template <>
struct RefreshParentRequest<MockRefreshImageCtx> {
static std::queue<RefreshParentRequest*> s_instances;
static RefreshParentRequest* create(MockRefreshImageCtx &mock_image_ctx,
const ParentImageInfo &parent_md,
const MigrationInfo &migration_info,
Context *on_finish) {
ceph_assert(!s_instances.empty());
auto instance = s_instances.front();
instance->on_finish = on_finish;
return instance;
}
static bool is_refresh_required(MockRefreshImageCtx &mock_image_ctx,
const ParentImageInfo& parent_md,
const MigrationInfo &migration_info) {
ceph_assert(!s_instances.empty());
return s_instances.front()->is_refresh_required();
}
Context *on_finish = nullptr;
RefreshParentRequest() {
s_instances.push(this);
}
~RefreshParentRequest() {
ceph_assert(this == s_instances.front());
s_instances.pop();
}
MOCK_CONST_METHOD0(is_refresh_required, bool());
MOCK_METHOD0(send, void());
MOCK_METHOD0(apply, void());
MOCK_METHOD1(finalize, void(Context *));
};
GetMetadataRequest<MockRefreshImageCtx>* GetMetadataRequest<MockRefreshImageCtx>::s_instance = nullptr;
std::queue<RefreshParentRequest<MockRefreshImageCtx>*> RefreshParentRequest<MockRefreshImageCtx>::s_instances;
} // namespace image
namespace util {
inline ImageCtx *get_image_ctx(librbd::MockRefreshImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
// template definitions
#include "librbd/image/RefreshRequest.cc"
ACTION_P(TestFeatures, image_ctx) {
return ((image_ctx->features & arg0) != 0);
}
ACTION_P(ShutDownExclusiveLock, image_ctx) {
// shutting down exclusive lock will close object map and journal
image_ctx->exclusive_lock = nullptr;
image_ctx->object_map = nullptr;
image_ctx->journal = nullptr;
}
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArg;
using ::testing::StrEq;
class TestMockImageRefreshRequest : public TestMockFixture {
public:
typedef GetMetadataRequest<MockRefreshImageCtx> MockGetMetadataRequest;
typedef RefreshRequest<MockRefreshImageCtx> MockRefreshRequest;
typedef RefreshParentRequest<MockRefreshImageCtx> MockRefreshParentRequest;
typedef std::map<std::string, bufferlist> Metadata;
void set_v1_migration_header(ImageCtx *ictx) {
bufferlist hdr;
ASSERT_EQ(0, read_header_bl(ictx->md_ctx, ictx->header_oid, hdr, nullptr));
ASSERT_TRUE(hdr.length() >= sizeof(rbd_obj_header_ondisk));
ASSERT_EQ(0, memcmp(RBD_HEADER_TEXT, hdr.c_str(), sizeof(RBD_HEADER_TEXT)));
bufferlist::iterator it = hdr.begin();
it.copy_in(sizeof(RBD_MIGRATE_HEADER_TEXT), RBD_MIGRATE_HEADER_TEXT);
ASSERT_EQ(0, ictx->md_ctx.write(ictx->header_oid, hdr, hdr.length(), 0));
}
void expect_set_require_lock(MockExclusiveLock &mock_exclusive_lock,
librbd::io::Direction direction) {
EXPECT_CALL(mock_exclusive_lock, set_require_lock(true, direction, _))
.WillOnce(WithArg<2>(Invoke([](Context* ctx) { ctx->complete(0); })));
}
void expect_unset_require_lock(MockExclusiveLock &mock_exclusive_lock,
librbd::io::Direction direction) {
EXPECT_CALL(mock_exclusive_lock, unset_require_lock(direction));
}
void expect_v1_read_header(MockRefreshImageCtx &mock_image_ctx, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
read(mock_image_ctx.header_oid, _, _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_v1_get_snapshots(MockRefreshImageCtx &mock_image_ctx, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("snap_list"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_v1_get_locks(MockRefreshImageCtx &mock_image_ctx, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("lock"),
StrEq("get_info"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_get_mutable_metadata(MockRefreshImageCtx &mock_image_ctx,
uint64_t features, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_size"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
uint64_t incompatible = (
mock_image_ctx.read_only ? features & RBD_FEATURES_INCOMPATIBLE :
features & RBD_FEATURES_RW_INCOMPATIBLE);
expect.WillOnce(DoDefault());
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_features"), _, _, _, _))
.WillOnce(WithArg<5>(Invoke([features, incompatible](bufferlist* out_bl) {
encode(features, *out_bl);
encode(incompatible, *out_bl);
return 0;
})));
expect_get_flags(mock_image_ctx, 0);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_snapcontext"), _, _, _, _))
.WillOnce(DoDefault());
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("lock"),
StrEq("get_info"), _, _, _, _))
.WillOnce(DoDefault());
}
}
void expect_parent_overlap_get(MockRefreshImageCtx &mock_image_ctx, int r) {
auto& expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("parent_overlap_get"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_get_parent(MockRefreshImageCtx &mock_image_ctx, int r) {
auto& expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("parent_get"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
expect_parent_overlap_get(mock_image_ctx, 0);
}
}
void expect_get_parent_legacy(MockRefreshImageCtx &mock_image_ctx, int r) {
auto& expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_parent"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_get_migration_header(MockRefreshImageCtx &mock_image_ctx, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("migration_get"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_get_metadata(MockRefreshImageCtx& mock_image_ctx,
MockGetMetadataRequest& mock_request,
const std::string& oid,
const Metadata& metadata, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(Invoke([&mock_image_ctx, &mock_request, oid, metadata, r]() {
ASSERT_EQ(oid, mock_request.oid);
*mock_request.pairs = metadata;
mock_image_ctx.image_ctx->op_work_queue->queue(
mock_request.on_finish, r);
}));
}
void expect_get_flags(MockRefreshImageCtx &mock_image_ctx, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_flags"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_get_op_features(MockRefreshImageCtx &mock_image_ctx,
uint64_t op_features, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("op_features_get"), _, _, _, _))
.WillOnce(WithArg<5>(Invoke([op_features, r](bufferlist* out_bl) {
encode(op_features, *out_bl);
return r;
})));
}
void expect_get_group(MockRefreshImageCtx &mock_image_ctx, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("image_group_get"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_get_snapshots(MockRefreshImageCtx &mock_image_ctx,
bool legacy_parent, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("snapshot_get"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
if (legacy_parent) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_parent"), _, _, _, _))
.WillOnce(DoDefault());
} else {
expect_parent_overlap_get(mock_image_ctx, 0);
}
expect_get_flags(mock_image_ctx, 0);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_protection_status"), _, _, _, _))
.WillOnce(DoDefault());
}
}
void expect_get_snapshots_legacy(MockRefreshImageCtx &mock_image_ctx,
bool include_timestamp, int r) {
auto &expect = EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_snapshot_name"), _, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_size"), _, _, _, _))
.WillOnce(DoDefault());
if (include_timestamp) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_snapshot_timestamp"), _, _, _, _))
.WillOnce(DoDefault());
}
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_parent"), _, _, _, _))
.WillOnce(DoDefault());
expect_get_flags(mock_image_ctx, 0);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
StrEq("get_protection_status"), _, _, _, _))
.WillOnce(DoDefault());
}
}
void expect_apply_metadata(MockRefreshImageCtx &mock_image_ctx,
int r) {
EXPECT_CALL(*mock_image_ctx.image_watcher, is_unregistered())
.WillOnce(Return(false));
EXPECT_CALL(mock_image_ctx, apply_metadata(_, false))
.WillOnce(Return(r));
}
void expect_add_snap(MockRefreshImageCtx &mock_image_ctx,
const std::string &snap_name, uint64_t snap_id) {
EXPECT_CALL(mock_image_ctx, add_snap(_, snap_name, snap_id, _, _, _, _, _));
}
void expect_init_exclusive_lock(MockRefreshImageCtx &mock_image_ctx,
MockExclusiveLock &mock_exclusive_lock,
int r) {
EXPECT_CALL(mock_image_ctx, create_exclusive_lock())
.WillOnce(Return(&mock_exclusive_lock));
EXPECT_CALL(mock_exclusive_lock, init(mock_image_ctx.features, _))
.WillOnce(WithArg<1>(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue)));
}
void expect_shut_down_exclusive_lock(MockRefreshImageCtx &mock_image_ctx,
MockExclusiveLock &mock_exclusive_lock,
int r) {
EXPECT_CALL(mock_exclusive_lock, shut_down(_))
.WillOnce(DoAll(ShutDownExclusiveLock(&mock_image_ctx),
CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue)));
}
void expect_init_layout(MockRefreshImageCtx &mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, init_layout(_));
}
void expect_test_features(MockRefreshImageCtx &mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, test_features(_, _))
.WillRepeatedly(TestFeatures(&mock_image_ctx));
}
void expect_refresh_parent_is_required(MockRefreshParentRequest &mock_refresh_parent_request,
bool required) {
EXPECT_CALL(mock_refresh_parent_request, is_refresh_required())
.WillRepeatedly(Return(required));
}
void expect_refresh_parent_send(MockRefreshImageCtx &mock_image_ctx,
MockRefreshParentRequest &mock_refresh_parent_request,
int r) {
EXPECT_CALL(mock_refresh_parent_request, send())
.WillOnce(FinishRequest(&mock_refresh_parent_request, r,
&mock_image_ctx));
}
void expect_refresh_parent_apply(MockRefreshParentRequest &mock_refresh_parent_request) {
EXPECT_CALL(mock_refresh_parent_request, apply());
}
void expect_refresh_parent_finalize(MockRefreshImageCtx &mock_image_ctx,
MockRefreshParentRequest &mock_refresh_parent_request,
int r) {
EXPECT_CALL(mock_refresh_parent_request, finalize(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_is_exclusive_lock_owner(MockExclusiveLock &mock_exclusive_lock,
bool is_owner) {
EXPECT_CALL(mock_exclusive_lock, is_lock_owner()).WillOnce(Return(is_owner));
}
void expect_get_journal_policy(MockImageCtx &mock_image_ctx,
MockJournalPolicy &mock_journal_policy) {
EXPECT_CALL(mock_image_ctx, get_journal_policy())
.WillOnce(Return(&mock_journal_policy));
}
void expect_journal_disabled(MockJournalPolicy &mock_journal_policy,
bool disabled) {
EXPECT_CALL(mock_journal_policy, journal_disabled())
.WillOnce(Return(disabled));
}
void expect_open_journal(MockRefreshImageCtx &mock_image_ctx,
MockJournal &mock_journal, int r) {
EXPECT_CALL(mock_image_ctx, create_journal())
.WillOnce(Return(&mock_journal));
EXPECT_CALL(mock_journal, open(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_close_journal(MockRefreshImageCtx &mock_image_ctx,
MockJournal &mock_journal, int r) {
EXPECT_CALL(mock_journal, close(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_open_object_map(MockRefreshImageCtx &mock_image_ctx,
MockObjectMap *mock_object_map, int r) {
EXPECT_CALL(mock_image_ctx, create_object_map(_))
.WillOnce(Return(mock_object_map));
EXPECT_CALL(*mock_object_map, open(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_close_object_map(MockRefreshImageCtx &mock_image_ctx,
MockObjectMap &mock_object_map, int r) {
EXPECT_CALL(mock_object_map, close(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_get_snap_id(MockRefreshImageCtx &mock_image_ctx,
const std::string &snap_name,
uint64_t snap_id) {
EXPECT_CALL(mock_image_ctx,
get_snap_id(_, snap_name)).WillOnce(Return(snap_id));
}
void expect_block_writes(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, block_writes(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_unblock_writes(MockImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, unblock_writes())
.Times(1);
}
void expect_image_flush(MockImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, send(_))
.WillOnce(Invoke([r](io::ImageDispatchSpec* spec) {
ASSERT_TRUE(boost::get<io::ImageDispatchSpec::Flush>(
&spec->request) != nullptr);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
spec->aio_comp->set_request_count(1);
spec->aio_comp->add_request();
spec->aio_comp->complete_request(r);
}));
}
};
TEST_F(TestMockImageRefreshRequest, SuccessV1) {
REQUIRE_FORMAT_V1();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
expect_v1_read_header(mock_image_ctx, 0);
expect_v1_get_snapshots(mock_image_ctx, 0);
expect_v1_get_locks(mock_image_ctx, 0);
expect_init_layout(mock_image_ctx);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessSnapshotV1) {
REQUIRE_FORMAT_V1();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
ASSERT_EQ(0, ictx->state->refresh());
MockRefreshImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
expect_v1_read_header(mock_image_ctx, 0);
expect_v1_get_snapshots(mock_image_ctx, 0);
expect_v1_get_locks(mock_image_ctx, 0);
expect_init_layout(mock_image_ctx);
expect_add_snap(mock_image_ctx, "snap", ictx->snap_ids.begin()->second);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessV2) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, -EOPNOTSUPP);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessSnapshotV2) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_get_snapshots(mock_image_ctx, false, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
expect_add_snap(mock_image_ctx, "snap", ictx->snap_ids.begin()->second);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessLegacySnapshotV2) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, -EOPNOTSUPP);
expect_get_parent_legacy(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_get_snapshots(mock_image_ctx, true, -EOPNOTSUPP);
expect_get_snapshots_legacy(mock_image_ctx, true, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
expect_add_snap(mock_image_ctx, "snap", ictx->snap_ids.begin()->second);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessLegacySnapshotNoTimestampV2) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, -EOPNOTSUPP);
expect_get_parent_legacy(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_get_snapshots(mock_image_ctx, true, -EOPNOTSUPP);
expect_get_snapshots_legacy(mock_image_ctx, true, -EOPNOTSUPP);
expect_get_snapshots_legacy(mock_image_ctx, false, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
expect_add_snap(mock_image_ctx, "snap", ictx->snap_ids.begin()->second);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessSetSnapshotV2) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
ASSERT_EQ(0, librbd::api::Image<>::snap_set(ictx,
cls::rbd::UserSnapshotNamespace(),
"snap"));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockObjectMap mock_object_map;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_get_snapshots(mock_image_ctx, false, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
expect_open_object_map(mock_image_ctx, &mock_object_map, 0);
}
expect_add_snap(mock_image_ctx, "snap", ictx->snap_ids.begin()->second);
expect_get_snap_id(mock_image_ctx, "snap", ictx->snap_ids.begin()->second);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SnapshotV2EnoentRetriesLimit) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockGetMetadataRequest mock_get_metadata_request;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
for (int i = 0; i < RefreshRequest<>::MAX_ENOENT_RETRIES + 1; ++i) {
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_get_snapshots(mock_image_ctx, false, -ENOENT);
}
C_SaferCond ctx;
auto req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(-ENOENT, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessChild) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
librbd::ImageCtx *ictx2 = nullptr;
std::string clone_name = get_temp_image_name();
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
ASSERT_EQ(0, snap_protect(*ictx, "snap"));
BOOST_SCOPE_EXIT_ALL((&)) {
if (ictx2 != nullptr) {
close_image(ictx2);
}
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, librbd::api::Image<>::remove(m_ioctx, clone_name, no_op));
ASSERT_EQ(0, ictx->operations->snap_unprotect(cls::rbd::UserSnapshotNamespace(), "snap"));
};
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap", m_ioctx,
clone_name.c_str(), ictx->features, &order, 0, 0));
ASSERT_EQ(0, open_image(clone_name, &ictx2));
MockRefreshImageCtx mock_image_ctx(*ictx2);
MockRefreshParentRequest *mock_refresh_parent_request = new MockRefreshParentRequest();
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
mock_image_ctx.features &= ~RBD_FEATURE_OPERATIONS;
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, mock_image_ctx.features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(*mock_refresh_parent_request, true);
expect_refresh_parent_send(mock_image_ctx, *mock_refresh_parent_request, 0);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
expect_refresh_parent_apply(*mock_refresh_parent_request);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
expect_refresh_parent_finalize(mock_image_ctx, *mock_refresh_parent_request, 0);
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessChildDontOpenParent) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
librbd::ImageCtx *ictx2 = nullptr;
std::string clone_name = get_temp_image_name();
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
ASSERT_EQ(0, snap_protect(*ictx, "snap"));
BOOST_SCOPE_EXIT_ALL((&)) {
if (ictx2 != nullptr) {
close_image(ictx2);
}
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, librbd::api::Image<>::remove(m_ioctx, clone_name, no_op));
ASSERT_EQ(0, ictx->operations->snap_unprotect(cls::rbd::UserSnapshotNamespace(), "snap"));
};
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap", m_ioctx,
clone_name.c_str(), ictx->features, &order, 0, 0));
ASSERT_EQ(0, open_image(clone_name, &ictx2));
MockRefreshImageCtx mock_image_ctx(*ictx2);
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
mock_image_ctx.features &= ~RBD_FEATURE_OPERATIONS;
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, mock_image_ctx.features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, true, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessChildBeingFlattened) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
librbd::ImageCtx *ictx2 = nullptr;
std::string clone_name = get_temp_image_name();
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
ASSERT_EQ(0, snap_protect(*ictx, "snap"));
BOOST_SCOPE_EXIT_ALL((&)) {
if (ictx2 != nullptr) {
close_image(ictx2);
}
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, librbd::api::Image<>::remove(m_ioctx, clone_name, no_op));
ASSERT_EQ(0, ictx->operations->snap_unprotect(
cls::rbd::UserSnapshotNamespace(), "snap"));
};
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap", m_ioctx,
clone_name.c_str(), ictx->features, &order, 0, 0));
ASSERT_EQ(0, open_image(clone_name, &ictx2));
MockRefreshImageCtx mock_image_ctx(*ictx2);
auto mock_refresh_parent_request = new MockRefreshParentRequest();
MockRefreshParentRequest mock_refresh_parent_request_ext;
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
mock_image_ctx.features &= ~RBD_FEATURE_OPERATIONS;
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, mock_image_ctx.features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(*mock_refresh_parent_request, true);
expect_refresh_parent_send(mock_image_ctx, *mock_refresh_parent_request,
-ENOENT);
expect_get_mutable_metadata(mock_image_ctx, mock_image_ctx.features, 0);
expect_get_parent(mock_image_ctx, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request_ext, false);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
auto req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, ChildEnoentRetriesLimit) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
librbd::ImageCtx *ictx2 = nullptr;
std::string clone_name = get_temp_image_name();
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, snap_create(*ictx, "snap"));
ASSERT_EQ(0, snap_protect(*ictx, "snap"));
BOOST_SCOPE_EXIT_ALL((&)) {
if (ictx2 != nullptr) {
close_image(ictx2);
}
librbd::NoOpProgressContext no_op;
ASSERT_EQ(0, librbd::api::Image<>::remove(m_ioctx, clone_name, no_op));
ASSERT_EQ(0, ictx->operations->snap_unprotect(
cls::rbd::UserSnapshotNamespace(), "snap"));
};
int order = ictx->order;
ASSERT_EQ(0, librbd::clone(m_ioctx, m_image_name.c_str(), "snap", m_ioctx,
clone_name.c_str(), ictx->features, &order, 0, 0));
ASSERT_EQ(0, open_image(clone_name, &ictx2));
MockRefreshImageCtx mock_image_ctx(*ictx2);
constexpr int num_tries = RefreshRequest<>::MAX_ENOENT_RETRIES + 1;
MockRefreshParentRequest* mock_refresh_parent_requests[num_tries];
for (auto& mock_refresh_parent_request : mock_refresh_parent_requests) {
mock_refresh_parent_request = new MockRefreshParentRequest();
}
MockGetMetadataRequest mock_get_metadata_request;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
mock_image_ctx.features &= ~RBD_FEATURE_OPERATIONS;
InSequence seq;
for (auto mock_refresh_parent_request : mock_refresh_parent_requests) {
expect_get_mutable_metadata(mock_image_ctx, mock_image_ctx.features, 0);
expect_get_parent(mock_image_ctx, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(*mock_refresh_parent_request, true);
expect_refresh_parent_send(mock_image_ctx, *mock_refresh_parent_request,
-ENOENT);
}
expect_refresh_parent_apply(*mock_refresh_parent_requests[num_tries - 1]);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
expect_refresh_parent_finalize(
mock_image_ctx, *mock_refresh_parent_requests[num_tries - 1], 0);
C_SaferCond ctx;
auto req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(-ENOENT, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, SuccessOpFeatures) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
mock_image_ctx.features |= RBD_FEATURE_OPERATIONS;
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, mock_image_ctx.features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_op_features(mock_image_ctx, 4096, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(4096U, mock_image_ctx.op_features);
ASSERT_TRUE(mock_image_ctx.operations_disabled);
}
TEST_F(TestMockImageRefreshRequest, DisableExclusiveLock) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
MockJournal mock_journal;
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
mock_image_ctx.journal = &mock_journal;
}
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
}
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_OBJECT_MAP,
false));
}
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_EXCLUSIVE_LOCK,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
// verify that exclusive lock is properly handled when object map
// and journaling were never enabled (or active)
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
expect_shut_down_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, DisableExclusiveLockWhileAcquiringLock) {
REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
}
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_OBJECT_MAP,
false));
}
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_EXCLUSIVE_LOCK,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
// verify that exclusive lock is properly handled when object map
// and journaling were never enabled (or active)
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, true, false, &ctx);
req->send();
ASSERT_EQ(-ERESTART, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, JournalDisabledByPolicy) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
if (ictx->test_features(RBD_FEATURE_FAST_DIFF)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_FAST_DIFF,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockJournal mock_journal;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
expect_is_exclusive_lock_owner(mock_exclusive_lock, true);
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
MockJournalPolicy mock_journal_policy;
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_journal_disabled(mock_journal_policy, true);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, EnableJournalWithExclusiveLock) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
if (ictx->test_features(RBD_FEATURE_FAST_DIFF)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_FAST_DIFF,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockJournal mock_journal;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
expect_is_exclusive_lock_owner(mock_exclusive_lock, true);
// journal should be immediately opened if exclusive lock owned
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
MockJournalPolicy mock_journal_policy;
expect_get_journal_policy(mock_image_ctx, mock_journal_policy);
expect_journal_disabled(mock_journal_policy, false);
expect_open_journal(mock_image_ctx, mock_journal, 0);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, EnableJournalWithoutExclusiveLock) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_OBJECT_MAP,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
expect_is_exclusive_lock_owner(mock_exclusive_lock, false);
// do not open the journal if exclusive lock is not owned
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
expect_set_require_lock(mock_exclusive_lock, librbd::io::DIRECTION_BOTH);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, DisableJournal) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
MockJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
// verify journal is closed if feature disabled
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
expect_block_writes(mock_image_ctx, 0);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
if (!mock_image_ctx.clone_copy_on_read) {
expect_unset_require_lock(mock_exclusive_lock, librbd::io::DIRECTION_READ);
}
expect_close_journal(mock_image_ctx, mock_journal, 0);
expect_unblock_writes(mock_image_ctx);
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false,
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, EnableObjectMapWithExclusiveLock) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockObjectMap mock_object_map;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
expect_is_exclusive_lock_owner(mock_exclusive_lock, true);
// object map should be immediately opened if exclusive lock owned
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
expect_open_object_map(mock_image_ctx, &mock_object_map, 0);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, EnableObjectMapWithoutExclusiveLock) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
expect_is_exclusive_lock_owner(mock_exclusive_lock, false);
// do not open the object map if exclusive lock is not owned
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, DisableObjectMap) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
MockJournal mock_journal;
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
mock_image_ctx.journal = &mock_journal;
}
if (ictx->test_features(RBD_FEATURE_FAST_DIFF)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_FAST_DIFF,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
// verify object map is closed if feature disabled
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
expect_close_object_map(mock_image_ctx, mock_object_map, 0);
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, OpenObjectMapError) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockObjectMap mock_object_map;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
expect_is_exclusive_lock_owner(mock_exclusive_lock, true);
// object map should be immediately opened if exclusive lock owned
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
expect_open_object_map(mock_image_ctx, &mock_object_map, -EBLOCKLISTED);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false,
&ctx);
req->send();
ASSERT_EQ(-EBLOCKLISTED, ctx.wait());
ASSERT_EQ(nullptr, mock_image_ctx.object_map);
}
TEST_F(TestMockImageRefreshRequest, OpenObjectMapTooLarge) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
if (ictx->test_features(RBD_FEATURE_JOURNALING)) {
ASSERT_EQ(0, ictx->operations->update_features(RBD_FEATURE_JOURNALING,
false));
}
ASSERT_EQ(0, ictx->state->refresh());
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
MockObjectMap mock_object_map;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
expect_is_exclusive_lock_owner(mock_exclusive_lock, true);
// object map should be immediately opened if exclusive lock owned
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
expect_open_object_map(mock_image_ctx, &mock_object_map, -EFBIG);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false,
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
ASSERT_EQ(nullptr, mock_image_ctx.object_map);
}
TEST_F(TestMockImageRefreshRequest, ApplyMetadataError) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
expect_get_mutable_metadata(mock_image_ctx, ictx->features, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, -EINVAL);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx;
MockRefreshRequest *req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRefreshRequest, NonPrimaryFeature) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockRefreshImageCtx mock_image_ctx(*ictx);
MockRefreshParentRequest mock_refresh_parent_request;
MockExclusiveLock mock_exclusive_lock;
expect_op_work_queue(mock_image_ctx);
expect_test_features(mock_image_ctx);
InSequence seq;
// ensure the image is put into read-only mode
expect_get_mutable_metadata(mock_image_ctx,
ictx->features | RBD_FEATURE_NON_PRIMARY, 0);
expect_get_parent(mock_image_ctx, 0);
MockGetMetadataRequest mock_get_metadata_request;
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx1;
auto req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx1);
req->send();
ASSERT_EQ(0, ctx1.wait());
ASSERT_TRUE(mock_image_ctx.read_only);
ASSERT_EQ(IMAGE_READ_ONLY_FLAG_NON_PRIMARY, mock_image_ctx.read_only_flags);
// try again but permit R/W against non-primary image
mock_image_ctx.read_only_mask = ~IMAGE_READ_ONLY_FLAG_NON_PRIMARY;
expect_get_mutable_metadata(mock_image_ctx,
ictx->features | RBD_FEATURE_NON_PRIMARY, 0);
expect_get_parent(mock_image_ctx, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request,
mock_image_ctx.header_oid, {}, 0);
expect_get_metadata(mock_image_ctx, mock_get_metadata_request, RBD_INFO, {},
0);
expect_apply_metadata(mock_image_ctx, 0);
expect_get_group(mock_image_ctx, 0);
expect_refresh_parent_is_required(mock_refresh_parent_request, false);
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
expect_init_exclusive_lock(mock_image_ctx, mock_exclusive_lock, 0);
}
EXPECT_CALL(mock_image_ctx, rebuild_data_io_context());
C_SaferCond ctx2;
req = new MockRefreshRequest(mock_image_ctx, false, false, &ctx2);
req->send();
ASSERT_EQ(0, ctx2.wait());
ASSERT_FALSE(mock_image_ctx.read_only);
ASSERT_EQ(0U, mock_image_ctx.read_only_flags);
}
} // namespace image
} // namespace librbd
| 66,295 | 36.711035 | 110 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_RemoveRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockContextWQ.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/image/TypeTraits.h"
#include "librbd/image/DetachChildRequest.h"
#include "librbd/image/PreRemoveRequest.h"
#include "librbd/image/RemoveRequest.h"
#include "librbd/journal/RemoveRequest.h"
#include "librbd/journal/TypeTraits.h"
#include "librbd/mirror/DisableRequest.h"
#include "librbd/operation/TrimRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <arpa/inet.h>
#include <list>
#include <boost/scope_exit.hpp>
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
static MockTestImageCtx* s_instance;
static MockTestImageCtx* create(const std::string &image_name,
const std::string &image_id,
const char *snap, librados::IoCtx& p,
bool read_only) {
ceph_assert(s_instance != nullptr);
return s_instance;
}
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
s_instance = this;
}
};
MockTestImageCtx* MockTestImageCtx::s_instance = nullptr;
} // anonymous namespace
template<>
struct Journal<MockTestImageCtx> {
static void get_work_queue(CephContext*, MockContextWQ**) {
}
};
namespace image {
template <>
struct TypeTraits<MockTestImageCtx> {
typedef librbd::MockContextWQ ContextWQ;
};
template <>
class DetachChildRequest<MockTestImageCtx> {
public:
static DetachChildRequest *s_instance;
static DetachChildRequest *create(MockTestImageCtx &image_ctx,
Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
Context *on_finish = nullptr;
DetachChildRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
DetachChildRequest<MockTestImageCtx> *DetachChildRequest<MockTestImageCtx>::s_instance;
template <>
class PreRemoveRequest<MockTestImageCtx> {
public:
static PreRemoveRequest *s_instance;
static PreRemoveRequest *create(MockTestImageCtx* image_ctx, bool force,
Context* on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
Context *on_finish = nullptr;
PreRemoveRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
PreRemoveRequest<MockTestImageCtx> *PreRemoveRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace image
namespace journal {
template <>
struct TypeTraits<MockTestImageCtx> {
typedef librbd::MockContextWQ ContextWQ;
};
} // namespace journal
namespace operation {
template <>
class TrimRequest<MockTestImageCtx> {
public:
static TrimRequest *s_instance;
static TrimRequest *create(MockTestImageCtx &image_ctx, Context *on_finish,
uint64_t original_size, uint64_t new_size,
ProgressContext &prog_ctx) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
Context *on_finish = nullptr;
TrimRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
TrimRequest<MockTestImageCtx> *TrimRequest<MockTestImageCtx>::s_instance;
} // namespace operation
namespace journal {
template <>
class RemoveRequest<MockTestImageCtx> {
private:
typedef ::librbd::image::TypeTraits<MockTestImageCtx> TypeTraits;
typedef typename TypeTraits::ContextWQ ContextWQ;
public:
static RemoveRequest *s_instance;
static RemoveRequest *create(IoCtx &ioctx, const std::string &imageid,
const std::string &client_id,
ContextWQ *op_work_queue, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
Context *on_finish = nullptr;
RemoveRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
RemoveRequest<MockTestImageCtx> *RemoveRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace journal
namespace mirror {
template<>
class DisableRequest<MockTestImageCtx> {
public:
static DisableRequest *s_instance;
Context *on_finish = nullptr;
static DisableRequest *create(MockTestImageCtx *image_ctx, bool force,
bool remove, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
DisableRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
DisableRequest<MockTestImageCtx> *DisableRequest<MockTestImageCtx>::s_instance;
} // namespace mirror
} // namespace librbd
// template definitions
#include "librbd/image/RemoveRequest.cc"
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::Invoke;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::WithArg;
using ::testing::SetArgPointee;
using ::testing::StrEq;
class TestMockImageRemoveRequest : public TestMockFixture {
public:
typedef ::librbd::image::TypeTraits<MockTestImageCtx> TypeTraits;
typedef typename TypeTraits::ContextWQ ContextWQ;
typedef RemoveRequest<MockTestImageCtx> MockRemoveRequest;
typedef PreRemoveRequest<MockTestImageCtx> MockPreRemoveRequest;
typedef DetachChildRequest<MockTestImageCtx> MockDetachChildRequest;
typedef librbd::operation::TrimRequest<MockTestImageCtx> MockTrimRequest;
typedef librbd::journal::RemoveRequest<MockTestImageCtx> MockJournalRemoveRequest;
typedef librbd::mirror::DisableRequest<MockTestImageCtx> MockMirrorDisableRequest;
librbd::ImageCtx *m_test_imctx = NULL;
MockTestImageCtx *m_mock_imctx = NULL;
void SetUp() override {
TestMockFixture::SetUp();
ASSERT_EQ(0, open_image(m_image_name, &m_test_imctx));
m_mock_imctx = new MockTestImageCtx(*m_test_imctx);
librbd::MockTestImageCtx::s_instance = m_mock_imctx;
}
void TearDown() override {
librbd::MockTestImageCtx::s_instance = NULL;
delete m_mock_imctx;
TestMockFixture::TearDown();
}
void expect_state_open(MockTestImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.state, open(_, _))
.WillOnce(Invoke([r](bool open_parent, Context *on_ready) {
on_ready->complete(r);
}));
}
void expect_state_close(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.state, close(_))
.WillOnce(Invoke([](Context *on_ready) {
on_ready->complete(0);
}));
}
void expect_wq_queue(ContextWQ &wq, int r) {
EXPECT_CALL(wq, queue(_, r))
.WillRepeatedly(Invoke([](Context *on_ready, int r) {
on_ready->complete(r);
}));
}
void expect_pre_remove_image(MockTestImageCtx &mock_image_ctx,
MockPreRemoveRequest& mock_request, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(FinishRequest(&mock_request, r, &mock_image_ctx));
}
void expect_trim(MockTestImageCtx &mock_image_ctx,
MockTrimRequest &mock_trim_request, int r) {
EXPECT_CALL(mock_trim_request, send())
.WillOnce(FinishRequest(&mock_trim_request, r, &mock_image_ctx));
}
void expect_journal_remove(MockTestImageCtx &mock_image_ctx,
MockJournalRemoveRequest &mock_journal_remove_request, int r) {
EXPECT_CALL(mock_journal_remove_request, send())
.WillOnce(FinishRequest(&mock_journal_remove_request, r, &mock_image_ctx));
}
void expect_mirror_disable(MockTestImageCtx &mock_image_ctx,
MockMirrorDisableRequest &mock_mirror_disable_request, int r) {
EXPECT_CALL(mock_mirror_disable_request, send())
.WillOnce(FinishRequest(&mock_mirror_disable_request, r, &mock_image_ctx));
}
void expect_remove_mirror_image(librados::IoCtx &ioctx, int r) {
EXPECT_CALL(get_mock_io_ctx(ioctx),
exec(StrEq("rbd_mirroring"), _, StrEq("rbd"),
StrEq("mirror_image_remove"), _, _, _, _))
.WillOnce(Return(r));
}
void expect_dir_remove_image(librados::IoCtx &ioctx, int r) {
EXPECT_CALL(get_mock_io_ctx(ioctx),
exec(RBD_DIRECTORY, _, StrEq("rbd"), StrEq("dir_remove_image"),
_, _, _, _))
.WillOnce(Return(r));
}
void expect_detach_child(MockTestImageCtx &mock_image_ctx,
MockDetachChildRequest& mock_request, int r) {
EXPECT_CALL(mock_request, send())
.WillOnce(FinishRequest(&mock_request, r, &mock_image_ctx));
}
};
TEST_F(TestMockImageRemoveRequest, SuccessV1) {
REQUIRE_FORMAT_V1();
expect_op_work_queue(*m_mock_imctx);
InSequence seq;
expect_state_open(*m_mock_imctx, 0);
MockPreRemoveRequest mock_pre_remove_request;
expect_pre_remove_image(*m_mock_imctx, mock_pre_remove_request, 0);
MockTrimRequest mock_trim_request;
expect_trim(*m_mock_imctx, mock_trim_request, 0);
expect_state_close(*m_mock_imctx);
ContextWQ op_work_queue;
expect_wq_queue(op_work_queue, 0);
C_SaferCond ctx;
librbd::NoOpProgressContext no_op;
MockRemoveRequest *req = MockRemoveRequest::create(m_ioctx, m_image_name, "",
true, false, no_op, &op_work_queue, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRemoveRequest, OpenFailV1) {
REQUIRE_FORMAT_V1();
InSequence seq;
expect_state_open(*m_mock_imctx, -ENOENT);
ContextWQ op_work_queue;
expect_wq_queue(op_work_queue, 0);
C_SaferCond ctx;
librbd::NoOpProgressContext no_op;
MockRemoveRequest *req = MockRemoveRequest::create(m_ioctx, m_image_name, "",
true, false, no_op, &op_work_queue, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRemoveRequest, SuccessV2CloneV1) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
expect_op_work_queue(*m_mock_imctx);
m_mock_imctx->parent_md.spec.pool_id = m_ioctx.get_id();
m_mock_imctx->parent_md.spec.image_id = "parent id";
m_mock_imctx->parent_md.spec.snap_id = 234;
InSequence seq;
expect_state_open(*m_mock_imctx, 0);
MockPreRemoveRequest mock_pre_remove_request;
expect_pre_remove_image(*m_mock_imctx, mock_pre_remove_request, 0);
MockTrimRequest mock_trim_request;
expect_trim(*m_mock_imctx, mock_trim_request, 0);
MockDetachChildRequest mock_detach_child_request;
expect_detach_child(*m_mock_imctx, mock_detach_child_request, 0);
MockMirrorDisableRequest mock_mirror_disable_request;
expect_mirror_disable(*m_mock_imctx, mock_mirror_disable_request, 0);
expect_state_close(*m_mock_imctx);
MockJournalRemoveRequest mock_journal_remove_request;
expect_journal_remove(*m_mock_imctx, mock_journal_remove_request, 0);
expect_remove_mirror_image(m_ioctx, 0);
expect_dir_remove_image(m_ioctx, 0);
C_SaferCond ctx;
librbd::NoOpProgressContext no_op;
ContextWQ op_work_queue;
MockRemoveRequest *req = MockRemoveRequest::create(
m_ioctx, m_image_name, "", true, false, no_op, &op_work_queue, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRemoveRequest, SuccessV2CloneV2) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
expect_op_work_queue(*m_mock_imctx);
m_mock_imctx->parent_md.spec.pool_id = m_ioctx.get_id();
m_mock_imctx->parent_md.spec.image_id = "parent id";
m_mock_imctx->parent_md.spec.snap_id = 234;
InSequence seq;
expect_state_open(*m_mock_imctx, 0);
MockPreRemoveRequest mock_pre_remove_request;
expect_pre_remove_image(*m_mock_imctx, mock_pre_remove_request, 0);
MockTrimRequest mock_trim_request;
expect_trim(*m_mock_imctx, mock_trim_request, 0);
MockDetachChildRequest mock_detach_child_request;
expect_detach_child(*m_mock_imctx, mock_detach_child_request, 0);
MockMirrorDisableRequest mock_mirror_disable_request;
expect_mirror_disable(*m_mock_imctx, mock_mirror_disable_request, 0);
expect_state_close(*m_mock_imctx);
MockJournalRemoveRequest mock_journal_remove_request;
expect_journal_remove(*m_mock_imctx, mock_journal_remove_request, 0);
expect_remove_mirror_image(m_ioctx, 0);
expect_dir_remove_image(m_ioctx, 0);
C_SaferCond ctx;
librbd::NoOpProgressContext no_op;
ContextWQ op_work_queue;
MockRemoveRequest *req = MockRemoveRequest::create(
m_ioctx, m_image_name, "", true, false, no_op, &op_work_queue, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageRemoveRequest, NotExistsV2) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
expect_op_work_queue(*m_mock_imctx);
m_mock_imctx->parent_md.spec.pool_id = m_ioctx.get_id();
m_mock_imctx->parent_md.spec.image_id = "parent id";
m_mock_imctx->parent_md.spec.snap_id = 234;
InSequence seq;
expect_state_open(*m_mock_imctx, 0);
MockPreRemoveRequest mock_pre_remove_request;
expect_pre_remove_image(*m_mock_imctx, mock_pre_remove_request, 0);
MockTrimRequest mock_trim_request;
expect_trim(*m_mock_imctx, mock_trim_request, 0);
MockDetachChildRequest mock_detach_child_request;
expect_detach_child(*m_mock_imctx, mock_detach_child_request, 0);
MockMirrorDisableRequest mock_mirror_disable_request;
expect_mirror_disable(*m_mock_imctx, mock_mirror_disable_request, 0);
expect_state_close(*m_mock_imctx);
MockJournalRemoveRequest mock_journal_remove_request;
expect_journal_remove(*m_mock_imctx, mock_journal_remove_request, 0);
expect_remove_mirror_image(m_ioctx, 0);
expect_dir_remove_image(m_ioctx, -ENOENT);
C_SaferCond ctx;
librbd::NoOpProgressContext no_op;
ContextWQ op_work_queue;
MockRemoveRequest *req = MockRemoveRequest::create(
m_ioctx, m_image_name, "", true, false, no_op, &op_work_queue, &ctx);
req->send();
ASSERT_EQ(-ENOENT, ctx.wait());
}
} // namespace image
} // namespace librbd
| 14,182 | 28.486486 | 93 |
cc
|
null |
ceph-main/src/test/librbd/image/test_mock_ValidatePoolRequest.cc
|
// -*- mode:c++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
} // namespace librbd
// template definitions
#include "librbd/image/ValidatePoolRequest.cc"
namespace librbd {
namespace image {
using ::testing::_;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;
class TestMockImageValidatePoolRequest : public TestMockFixture {
public:
typedef ValidatePoolRequest<MockTestImageCtx> MockValidatePoolRequest;
void SetUp() override {
TestMockFixture::SetUp();
m_ioctx.remove(RBD_INFO);
ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
}
void expect_clone(librados::MockTestMemIoCtxImpl &mock_io_ctx) {
EXPECT_CALL(mock_io_ctx, clone())
.WillOnce(Invoke([&mock_io_ctx]() {
mock_io_ctx.get();
return &mock_io_ctx;
}));
}
void expect_read_rbd_info(librados::MockTestMemIoCtxImpl &mock_io_ctx,
const std::string& data, int r) {
auto& expect = EXPECT_CALL(
mock_io_ctx, read(StrEq(RBD_INFO), 0, 0, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(WithArg<3>(Invoke([data](bufferlist* bl) {
bl->append(data);
return 0;
})));
}
}
void expect_write_rbd_info(librados::MockTestMemIoCtxImpl &mock_io_ctx,
const std::string& data, int r) {
bufferlist bl;
bl.append(data);
EXPECT_CALL(mock_io_ctx, write(StrEq(RBD_INFO), ContentsEqual(bl),
data.length(), 0, _))
.WillOnce(Return(r));
}
void expect_allocate_snap_id(librados::MockTestMemIoCtxImpl &mock_io_ctx,
int r) {
auto &expect = EXPECT_CALL(mock_io_ctx, selfmanaged_snap_create(_));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_release_snap_id(librados::MockTestMemIoCtxImpl &mock_io_ctx,
int r) {
auto &expect = EXPECT_CALL(mock_io_ctx, selfmanaged_snap_remove(_));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
librbd::ImageCtx *image_ctx;
};
TEST_F(TestMockImageValidatePoolRequest, Success) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
InSequence seq;
expect_clone(mock_io_ctx);
expect_read_rbd_info(mock_io_ctx, "", -ENOENT);
expect_allocate_snap_id(mock_io_ctx, 0);
expect_write_rbd_info(mock_io_ctx, "validate", 0);
expect_release_snap_id(mock_io_ctx, 0);
expect_write_rbd_info(mock_io_ctx, "overwrite validated", 0);
C_SaferCond ctx;
auto req = new MockValidatePoolRequest(m_ioctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageValidatePoolRequest, AlreadyValidated) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
InSequence seq;
expect_clone(mock_io_ctx);
expect_read_rbd_info(mock_io_ctx, "overwrite validated", 0);
C_SaferCond ctx;
auto req = new MockValidatePoolRequest(m_ioctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageValidatePoolRequest, SnapshotsValidated) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
InSequence seq;
expect_clone(mock_io_ctx);
expect_read_rbd_info(mock_io_ctx, "validate", 0);
expect_write_rbd_info(mock_io_ctx, "overwrite validated", 0);
C_SaferCond ctx;
auto req = new MockValidatePoolRequest(m_ioctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageValidatePoolRequest, ReadError) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
InSequence seq;
expect_clone(mock_io_ctx);
expect_read_rbd_info(mock_io_ctx, "", -EPERM);
C_SaferCond ctx;
auto req = new MockValidatePoolRequest(m_ioctx, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockImageValidatePoolRequest, CreateSnapshotError) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
InSequence seq;
expect_clone(mock_io_ctx);
expect_read_rbd_info(mock_io_ctx, "", 0);
expect_allocate_snap_id(mock_io_ctx, -EPERM);
C_SaferCond ctx;
auto req = new MockValidatePoolRequest(m_ioctx, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockImageValidatePoolRequest, WriteError) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
InSequence seq;
expect_clone(mock_io_ctx);
expect_read_rbd_info(mock_io_ctx, "", -ENOENT);
expect_allocate_snap_id(mock_io_ctx, 0);
expect_write_rbd_info(mock_io_ctx, "validate", -EPERM);
expect_release_snap_id(mock_io_ctx, -EINVAL);
C_SaferCond ctx;
auto req = new MockValidatePoolRequest(m_ioctx, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockImageValidatePoolRequest, RemoveSnapshotError) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
InSequence seq;
expect_clone(mock_io_ctx);
expect_read_rbd_info(mock_io_ctx, "", -ENOENT);
expect_allocate_snap_id(mock_io_ctx, 0);
expect_write_rbd_info(mock_io_ctx, "validate", 0);
expect_release_snap_id(mock_io_ctx, -EPERM);
expect_write_rbd_info(mock_io_ctx, "overwrite validated", 0);
C_SaferCond ctx;
auto req = new MockValidatePoolRequest(m_ioctx, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockImageValidatePoolRequest, OverwriteError) {
librados::MockTestMemIoCtxImpl &mock_io_ctx(get_mock_io_ctx(m_ioctx));
InSequence seq;
expect_clone(mock_io_ctx);
expect_read_rbd_info(mock_io_ctx, "", -ENOENT);
expect_allocate_snap_id(mock_io_ctx, 0);
expect_write_rbd_info(mock_io_ctx, "validate", 0);
expect_release_snap_id(mock_io_ctx, 0);
expect_write_rbd_info(mock_io_ctx, "overwrite validated", -EOPNOTSUPP);
C_SaferCond ctx;
auto req = new MockValidatePoolRequest(m_ioctx, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
} // namespace image
} // namespace librbd
| 6,527 | 28.142857 | 75 |
cc
|
null |
ceph-main/src/test/librbd/io/test_mock_CopyupRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockExclusiveLock.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockJournal.h"
#include "test/librbd/mock/MockObjectMap.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "include/rbd/librbd.hpp"
#include "librbd/api/Io.h"
#include "librbd/deep_copy/ObjectCopyRequest.h"
#include "librbd/io/CopyupRequest.h"
#include "librbd/io/ImageDispatchSpec.h"
#include "librbd/io/ObjectRequest.h"
#include "librbd/io/ReadResult.h"
#include "librbd/io/Utils.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx,
MockTestImageCtx* mock_parent_image_ctx = nullptr)
: MockImageCtx(image_ctx) {
parent = mock_parent_image_ctx;
}
~MockTestImageCtx() override {
// copyups need to complete prior to attempting to delete this object
wait_for_async_ops();
}
std::map<uint64_t, librbd::io::CopyupRequest<librbd::MockTestImageCtx>*> copyup_list;
};
} // anonymous namespace
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
namespace deep_copy {
template <>
struct ObjectCopyRequest<librbd::MockTestImageCtx> {
static ObjectCopyRequest* s_instance;
static ObjectCopyRequest* create(librbd::MockImageCtx* parent_image_ctx,
librbd::MockTestImageCtx* image_ctx,
librados::snap_t src_snap_id_start,
librados::snap_t dst_snap_id_start,
const SnapMap &snap_map,
uint64_t object_number, uint32_t flags,
Handler*, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->object_number = object_number;
s_instance->flatten = (
(flags & deep_copy::OBJECT_COPY_REQUEST_FLAG_FLATTEN) != 0);
s_instance->on_finish = on_finish;
return s_instance;
}
uint64_t object_number;
bool flatten;
Context *on_finish;
ObjectCopyRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
ObjectCopyRequest<librbd::MockTestImageCtx>* ObjectCopyRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
} // namespace deep_copy
namespace io {
namespace util {
template <>
void area_to_object_extents(MockTestImageCtx* image_ctx, uint64_t offset,
uint64_t length, ImageArea area,
uint64_t buffer_offset,
striper::LightweightObjectExtents* object_extents) {
Striper::file_to_extents(image_ctx->cct, &image_ctx->layout, offset, length,
0, buffer_offset, object_extents);
}
template <>
std::pair<Extents, ImageArea> object_to_area_extents(
MockTestImageCtx* image_ctx, uint64_t object_no,
const Extents& object_extents) {
Extents extents;
for (auto [off, len] : object_extents) {
Striper::extent_to_file(image_ctx->cct, &image_ctx->layout, object_no, off,
len, extents);
}
return {std::move(extents), ImageArea::DATA};
}
} // namespace util
template <>
struct ObjectRequest<librbd::MockTestImageCtx> {
static void add_write_hint(librbd::MockTestImageCtx&,
neorados::WriteOp*) {
}
};
template <>
struct AbstractObjectWriteRequest<librbd::MockTestImageCtx> {
C_SaferCond ctx;
void handle_copyup(int r) {
ctx.complete(r);
}
MOCK_CONST_METHOD0(get_pre_write_object_map_state, uint8_t());
MOCK_CONST_METHOD0(is_empty_write_op, bool());
MOCK_METHOD1(add_copyup_ops, void(neorados::WriteOp*));
};
} // namespace io
} // namespace librbd
static bool operator==(const SnapContext& rhs, const SnapContext& lhs) {
return (rhs.seq == lhs.seq && rhs.snaps == lhs.snaps);
}
#include "librbd/AsyncObjectThrottle.cc"
#include "librbd/io/CopyupRequest.cc"
MATCHER_P(IsRead, image_extents, "") {
auto req = boost::get<librbd::io::ImageDispatchSpec::Read>(&arg->request);
return (req != nullptr && image_extents == arg->image_extents);
}
namespace librbd {
namespace io {
using ::testing::_;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrEq;
using ::testing::WithArg;
using ::testing::WithArgs;
using ::testing::WithoutArgs;
struct TestMockIoCopyupRequest : public TestMockFixture {
typedef CopyupRequest<librbd::MockTestImageCtx> MockCopyupRequest;
typedef ObjectRequest<librbd::MockTestImageCtx> MockObjectRequest;
typedef AbstractObjectWriteRequest<librbd::MockTestImageCtx> MockAbstractObjectWriteRequest;
typedef deep_copy::ObjectCopyRequest<librbd::MockTestImageCtx> MockObjectCopyRequest;
void SetUp() override {
TestMockFixture::SetUp();
if (!is_feature_enabled(RBD_FEATURE_LAYERING)) {
return;
}
m_parent_image_name = m_image_name;
m_image_name = get_temp_image_name();
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_parent_image_name.c_str(),
nullptr));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
uint64_t features;
ASSERT_EQ(0, image.features(&features));
image.close();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_parent_image_name.c_str(), "one", m_ioctx,
m_image_name.c_str(), features, &order));
}
void expect_get_parent_overlap(MockTestImageCtx& mock_image_ctx,
librados::snap_t snap_id, uint64_t overlap,
int r) {
EXPECT_CALL(mock_image_ctx, get_parent_overlap(snap_id, _))
.WillOnce(WithArg<1>(Invoke([overlap, r](uint64_t *o) {
*o = overlap;
return r;
})));
}
void expect_prune_parent_extents(MockTestImageCtx& mock_image_ctx,
uint64_t overlap, uint64_t object_overlap) {
EXPECT_CALL(mock_image_ctx, prune_parent_extents(_, _, overlap, _))
.WillOnce(WithoutArgs(Invoke([object_overlap]() {
return object_overlap;
})));
}
void expect_read_parent(librbd::MockTestImageCtx& mock_image_ctx,
const Extents& image_extents,
const std::string& data, int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher,
send(IsRead(image_extents)))
.WillOnce(Invoke(
[&mock_image_ctx, image_extents, data, r](io::ImageDispatchSpec* spec) {
auto req = boost::get<librbd::io::ImageDispatchSpec::Read>(
&spec->request);
ASSERT_TRUE(req != nullptr);
if (r < 0) {
spec->fail(r);
return;
}
spec->dispatch_result = DISPATCH_RESULT_COMPLETE;
auto aio_comp = spec->aio_comp;
aio_comp->read_result = std::move(req->read_result);
aio_comp->read_result.set_image_extents(image_extents);
aio_comp->set_request_count(1);
auto ctx = new ReadResult::C_ImageReadRequest(aio_comp, 0,
image_extents);
ctx->bl.append(data);
mock_image_ctx.image_ctx->op_work_queue->queue(ctx, r);
}));
}
void expect_copyup(MockTestImageCtx& mock_image_ctx, uint64_t snap_id,
const std::string& oid, const std::string& data, int r) {
bufferlist in_bl;
in_bl.append(data);
SnapContext snapc;
if (snap_id == CEPH_NOSNAP) {
snapc = mock_image_ctx.snapc;
}
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
EXPECT_CALL(mock_io_ctx,
exec(oid, _, StrEq("rbd"), StrEq("copyup"),
ContentsEqual(in_bl), _, _, snapc))
.WillOnce(Return(r));
}
void expect_sparse_copyup(MockTestImageCtx &mock_image_ctx, uint64_t snap_id,
const std::string &oid,
const std::map<uint64_t, uint64_t> &extent_map,
const std::string &data, int r) {
bufferlist data_bl;
data_bl.append(data);
bufferlist in_bl;
encode(extent_map, in_bl);
encode(data_bl, in_bl);
SnapContext snapc;
if (snap_id == CEPH_NOSNAP) {
snapc = mock_image_ctx.snapc;
}
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
EXPECT_CALL(mock_io_ctx,
exec(oid, _, StrEq("rbd"), StrEq("sparse_copyup"),
ContentsEqual(in_bl), _, _, snapc))
.WillOnce(Return(r));
}
void expect_write(MockTestImageCtx& mock_image_ctx, uint64_t snap_id,
const std::string& oid, int r) {
SnapContext snapc;
if (snap_id == CEPH_NOSNAP) {
snapc = mock_image_ctx.snapc;
}
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
EXPECT_CALL(mock_io_ctx, write(oid, _, 0, 0, snapc))
.WillOnce(Return(r));
}
void expect_test_features(MockTestImageCtx& mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, test_features(_, _))
.WillRepeatedly(WithArg<0>(Invoke([&mock_image_ctx](uint64_t features) {
return (mock_image_ctx.features & features) != 0;
})));
}
void expect_is_lock_owner(MockTestImageCtx& mock_image_ctx) {
if (mock_image_ctx.exclusive_lock != nullptr) {
EXPECT_CALL(*mock_image_ctx.exclusive_lock,
is_lock_owner()).WillRepeatedly(Return(true));
}
}
void expect_is_empty_write_op(MockAbstractObjectWriteRequest& mock_write_request,
bool is_empty) {
EXPECT_CALL(mock_write_request, is_empty_write_op())
.WillOnce(Return(is_empty));
}
void expect_add_copyup_ops(MockAbstractObjectWriteRequest& mock_write_request) {
EXPECT_CALL(mock_write_request, add_copyup_ops(_))
.WillOnce(Invoke([](neorados::WriteOp* op) {
op->write(0, bufferlist{});
}));
}
void expect_get_pre_write_object_map_state(MockTestImageCtx& mock_image_ctx,
MockAbstractObjectWriteRequest& mock_write_request,
uint8_t state) {
if (mock_image_ctx.object_map != nullptr) {
EXPECT_CALL(mock_write_request, get_pre_write_object_map_state())
.WillOnce(Return(state));
}
}
void expect_object_map_at(MockTestImageCtx& mock_image_ctx,
uint64_t object_no, uint8_t state) {
if (mock_image_ctx.object_map != nullptr) {
EXPECT_CALL(*mock_image_ctx.object_map, at(object_no))
.WillOnce(Return(state));
}
}
void expect_object_map_update(MockTestImageCtx& mock_image_ctx,
uint64_t snap_id, uint64_t object_no,
uint8_t state, bool updated, int ret_val) {
if (mock_image_ctx.object_map != nullptr) {
if (!mock_image_ctx.image_ctx->test_features(RBD_FEATURE_FAST_DIFF) &&
state == OBJECT_EXISTS_CLEAN) {
state = OBJECT_EXISTS;
}
EXPECT_CALL(*mock_image_ctx.object_map,
aio_update(snap_id, object_no, object_no + 1, state,
boost::optional<uint8_t>(), _,
(snap_id != CEPH_NOSNAP), _))
.WillOnce(WithArg<7>(Invoke([&mock_image_ctx, updated, ret_val](Context *ctx) {
if (updated) {
mock_image_ctx.op_work_queue->queue(ctx, ret_val);
}
return updated;
})));
}
}
void expect_object_copy(MockTestImageCtx& mock_image_ctx,
MockObjectCopyRequest& mock_object_copy_request,
bool flatten, int r) {
EXPECT_CALL(mock_object_copy_request, send())
.WillOnce(Invoke(
[&mock_image_ctx, &mock_object_copy_request, flatten, r]() {
ASSERT_EQ(flatten, mock_object_copy_request.flatten);
mock_image_ctx.op_work_queue->queue(
mock_object_copy_request.on_finish, r);
}));
}
void expect_prepare_copyup(MockTestImageCtx& mock_image_ctx, int r = 0) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher,
prepare_copyup(_, _)).WillOnce(Return(r));
}
void expect_prepare_copyup(MockTestImageCtx& mock_image_ctx,
const SparseBufferlist& in_sparse_bl,
const SparseBufferlist& out_sparse_bl) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher,
prepare_copyup(_, _))
.WillOnce(WithArg<1>(Invoke(
[in_sparse_bl, out_sparse_bl]
(SnapshotSparseBufferlist* snap_sparse_bl) {
auto& sparse_bl = (*snap_sparse_bl)[0];
EXPECT_EQ(in_sparse_bl, sparse_bl);
sparse_bl = out_sparse_bl;
return 0;
})));
}
void flush_async_operations(librbd::ImageCtx* ictx) {
api::Io<>::flush(*ictx);
}
std::string m_parent_image_name;
};
TEST_F(TestMockIoCopyupRequest, Standard) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
MockAbstractObjectWriteRequest mock_write_request;
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{{0, 4096}}, data, 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, StandardWithSnaps) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->image_lock.lock();
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "2", 2, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "1", 1, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->snapc = {2, {2, 1}};
ictx->image_lock.unlock();
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_test_features(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
MockAbstractObjectWriteRequest mock_write_request;
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, 1, 0, OBJECT_EXISTS, true, 0);
expect_object_map_update(mock_image_ctx, 2, 0, OBJECT_EXISTS_CLEAN, true, 0);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, 0, ictx->get_object_name(0), {{0, 4096}},
data, 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, CopyOnRead) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{{0, 4096}}, data, 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->send();
flush_async_operations(ictx);
}
TEST_F(TestMockIoCopyupRequest, CopyOnReadWithSnaps) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->image_lock.lock();
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "1", 1, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->snapc = {1, {1}};
ictx->image_lock.unlock();
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_test_features(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, 1, 0, OBJECT_EXISTS, true, 0);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS_CLEAN,
true, 0);
expect_sparse_copyup(mock_image_ctx, 0, ictx->get_object_name(0), {{0, 4096}},
data, 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->send();
flush_async_operations(ictx);
}
TEST_F(TestMockIoCopyupRequest, DeepCopy) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
MockAbstractObjectWriteRequest mock_write_request;
MockObjectCopyRequest mock_object_copy_request;
mock_image_ctx.migration_info = {1, "", "", "image id", "", {}, ictx->size,
true};
expect_is_empty_write_op(mock_write_request, false);
expect_object_copy(mock_image_ctx, mock_object_copy_request, true, 0);
expect_is_empty_write_op(mock_write_request, false);
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{}, "", 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, DeepCopyOnRead) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
MockObjectCopyRequest mock_object_copy_request;
mock_image_ctx.migration_info = {1, "", "", "image id", "", {}, ictx->size,
false};
expect_object_copy(mock_image_ctx, mock_object_copy_request, true, 0);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->send();
flush_async_operations(ictx);
}
TEST_F(TestMockIoCopyupRequest, DeepCopyWithPostSnaps) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->image_lock.lock();
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "3", 3, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "2", 2, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "1", 1, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->snapc = {3, {3, 2, 1}};
ictx->image_lock.unlock();
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_test_features(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
MockAbstractObjectWriteRequest mock_write_request;
MockObjectCopyRequest mock_object_copy_request;
mock_image_ctx.migration_info = {1, "", "", "image id", "",
{{CEPH_NOSNAP, {2, 1}}},
ictx->size, true};
expect_is_empty_write_op(mock_write_request, false);
expect_object_copy(mock_image_ctx, mock_object_copy_request, true, 0);
expect_is_empty_write_op(mock_write_request, false);
expect_get_parent_overlap(mock_image_ctx, 1, 0, 0);
expect_get_parent_overlap(mock_image_ctx, 2, 1, 0);
expect_prune_parent_extents(mock_image_ctx, 1, 1);
expect_get_parent_overlap(mock_image_ctx, 3, 1, 0);
expect_prune_parent_extents(mock_image_ctx, 1, 1);
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, 2, 0, OBJECT_EXISTS, true, 0);
expect_object_map_update(mock_image_ctx, 3, 0, OBJECT_EXISTS_CLEAN, true, 0);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{}, "", 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, DeepCopyWithPreAndPostSnaps) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->image_lock.lock();
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "4", 4, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "3", 3, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "2", 2, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "1", 1, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->snapc = {4, {4, 3, 2, 1}};
ictx->image_lock.unlock();
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_test_features(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
MockAbstractObjectWriteRequest mock_write_request;
MockObjectCopyRequest mock_object_copy_request;
mock_image_ctx.migration_info = {1, "", "", "image id", "",
{{CEPH_NOSNAP, {2, 1}}, {10, {1}}},
ictx->size, true};
expect_is_empty_write_op(mock_write_request, false);
expect_object_copy(mock_image_ctx, mock_object_copy_request, true, 0);
expect_is_empty_write_op(mock_write_request, false);
expect_get_parent_overlap(mock_image_ctx, 2, 0, 0);
expect_get_parent_overlap(mock_image_ctx, 3, 1, 0);
expect_prune_parent_extents(mock_image_ctx, 1, 1);
expect_get_parent_overlap(mock_image_ctx, 4, 1, 0);
expect_prune_parent_extents(mock_image_ctx, 1, 1);
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, 3, 0, OBJECT_EXISTS_CLEAN, true, 0);
expect_object_map_update(mock_image_ctx, 4, 0, OBJECT_EXISTS_CLEAN, true, 0);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{}, "", 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, ZeroedCopyup) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
MockAbstractObjectWriteRequest mock_write_request;
expect_prepare_copyup(mock_image_ctx);
expect_is_empty_write_op(mock_write_request, false);
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{}, "", 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, ZeroedCopyOnRead) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '\0');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{}, "", 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->send();
flush_async_operations(ictx);
}
TEST_F(TestMockIoCopyupRequest, NoOpCopyup) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, "", -ENOENT);
expect_prepare_copyup(mock_image_ctx);
MockAbstractObjectWriteRequest mock_write_request;
expect_is_empty_write_op(mock_write_request, true);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, RestartWrite) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
MockAbstractObjectWriteRequest mock_write_request1;
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request1,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
expect_add_copyup_ops(mock_write_request1);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{{0, 4096}}, data, 0);
MockAbstractObjectWriteRequest mock_write_request2;
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
EXPECT_CALL(mock_io_ctx, write(ictx->get_object_name(0), _, 0, 0, _))
.WillOnce(WithoutArgs(Invoke([req, &mock_write_request2]() {
req->append_request(&mock_write_request2, {});
return 0;
})));
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request1, {});
req->send();
ASSERT_EQ(0, mock_write_request1.ctx.wait());
ASSERT_EQ(-ERESTART, mock_write_request2.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, ReadFromParentError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, "", -EPERM);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
MockAbstractObjectWriteRequest mock_write_request;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(-EPERM, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, PrepareCopyupError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx, -EIO);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
MockAbstractObjectWriteRequest mock_write_request;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(-EIO, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, DeepCopyError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
MockAbstractObjectWriteRequest mock_write_request;
MockObjectCopyRequest mock_object_copy_request;
mock_image_ctx.migration_info = {1, "", "", "image id", "", {}, ictx->size,
true};
expect_is_empty_write_op(mock_write_request, false);
expect_object_copy(mock_image_ctx, mock_object_copy_request, true, -EPERM);
expect_is_empty_write_op(mock_write_request, false);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(-EPERM, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, UpdateObjectMapError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
MockAbstractObjectWriteRequest mock_write_request;
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
-EINVAL);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(-EINVAL, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, CopyupError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->image_lock.lock();
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "1", 1, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->snapc = {1, {1}};
ictx->image_lock.unlock();
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_test_features(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
MockAbstractObjectWriteRequest mock_write_request;
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, 1, 0, OBJECT_EXISTS, true, 0);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, 0, ictx->get_object_name(0), {{0, 4096}},
data, -EPERM);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(-EPERM, mock_write_request.ctx.wait());
flush_async_operations(ictx);
}
TEST_F(TestMockIoCopyupRequest, SparseCopyupNotSupported) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
mock_image_ctx.enable_sparse_copyup = false;
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
expect_prepare_copyup(mock_image_ctx);
MockAbstractObjectWriteRequest mock_write_request;
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), data, 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, ProcessCopyup) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
bufferlist in_prepare_bl;
in_prepare_bl.append(std::string(3072, '1'));
bufferlist out_prepare_bl;
out_prepare_bl.substr_of(in_prepare_bl, 0, 1024);
expect_prepare_copyup(
mock_image_ctx,
{{1024U, {3072U, {SPARSE_EXTENT_STATE_DATA, 3072,
std::move(in_prepare_bl)}}}},
{{2048U, {1024U, {SPARSE_EXTENT_STATE_DATA, 1024,
std::move(out_prepare_bl)}}}});
MockAbstractObjectWriteRequest mock_write_request;
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0),
{{2048, 1024}}, data.substr(0, 1024), 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {{0, 1024}});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
TEST_F(TestMockIoCopyupRequest, ProcessCopyupOverwrite) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->image_lock.lock();
ictx->add_snap(cls::rbd::UserSnapshotNamespace(), "1", 1, ictx->size,
ictx->parent_md, RBD_PROTECTION_STATUS_UNPROTECTED,
0, {});
ictx->snapc = {1, {1}};
ictx->image_lock.unlock();
MockTestImageCtx mock_parent_image_ctx(*ictx->parent);
MockTestImageCtx mock_image_ctx(*ictx, &mock_parent_image_ctx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_test_features(mock_image_ctx);
expect_op_work_queue(mock_image_ctx);
expect_is_lock_owner(mock_image_ctx);
InSequence seq;
std::string data(4096, '1');
expect_read_parent(mock_parent_image_ctx, {{0, 4096}}, data, 0);
bufferlist in_prepare_bl;
in_prepare_bl.append(data);
bufferlist out_prepare_bl;
out_prepare_bl.substr_of(in_prepare_bl, 0, 1024);
expect_prepare_copyup(
mock_image_ctx,
{{0, {4096, {SPARSE_EXTENT_STATE_DATA, 4096,
std::move(in_prepare_bl)}}}},
{{0, {1024, {SPARSE_EXTENT_STATE_DATA, 1024, bufferlist{out_prepare_bl}}}},
{2048, {1024, {SPARSE_EXTENT_STATE_DATA, 1024,
bufferlist{out_prepare_bl}}}}});
MockAbstractObjectWriteRequest mock_write_request;
expect_get_pre_write_object_map_state(mock_image_ctx, mock_write_request,
OBJECT_EXISTS);
expect_object_map_at(mock_image_ctx, 0, OBJECT_NONEXISTENT);
expect_object_map_update(mock_image_ctx, 1, 0, OBJECT_EXISTS, true, 0);
expect_object_map_update(mock_image_ctx, CEPH_NOSNAP, 0, OBJECT_EXISTS, true,
0);
expect_add_copyup_ops(mock_write_request);
expect_sparse_copyup(mock_image_ctx, 0, ictx->get_object_name(0),
{{0, 1024}, {2048, 1024}}, data.substr(0, 2048), 0);
expect_write(mock_image_ctx, CEPH_NOSNAP, ictx->get_object_name(0), 0);
auto req = new MockCopyupRequest(&mock_image_ctx, 0, {{0, 4096}},
ImageArea::DATA, {});
mock_image_ctx.copyup_list[0] = req;
req->append_request(&mock_write_request, {{0, 1024}});
req->send();
ASSERT_EQ(0, mock_write_request.ctx.wait());
}
} // namespace io
} // namespace librbd
| 48,591 | 35.235645 | 111 |
cc
|
null |
ceph-main/src/test/librbd/io/test_mock_ImageRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockJournal.h"
#include "test/librbd/mock/cache/MockImageCache.h"
#include "librbd/io/ImageRequest.h"
#include "librbd/io/ObjectDispatchSpec.h"
#include "librbd/io/Utils.h"
namespace librbd {
namespace {
struct MockTestImageCtx;
struct MockTestJournal : public MockJournal {
MOCK_METHOD4(append_write_event, uint64_t(uint64_t, size_t,
const bufferlist &, bool));
MOCK_METHOD5(append_compare_and_write_event, uint64_t(uint64_t, size_t,
const bufferlist &,
const bufferlist &,
bool));
MOCK_METHOD5(append_io_event_mock, uint64_t(const journal::EventEntry&,
uint64_t, size_t, bool, int));
uint64_t append_io_event(journal::EventEntry &&event_entry,
uint64_t offset, size_t length,
bool flush_entry, int filter_ret_val) {
// googlemock doesn't support move semantics
return append_io_event_mock(event_entry, offset, length, flush_entry,
filter_ret_val);
}
MOCK_METHOD2(commit_io_event, void(uint64_t, int));
};
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
MockTestJournal* journal;
};
} // anonymous namespace
namespace util {
inline ImageCtx *get_image_ctx(MockTestImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
} // namespace librbd
#include "librbd/io/ImageRequest.cc"
namespace librbd {
namespace io {
namespace util {
template <>
void area_to_object_extents(MockTestImageCtx* image_ctx, uint64_t offset,
uint64_t length, ImageArea area,
uint64_t buffer_offset,
striper::LightweightObjectExtents* object_extents) {
Striper::file_to_extents(image_ctx->cct, &image_ctx->layout, offset, length,
0, buffer_offset, object_extents);
}
template <>
std::pair<Extents, ImageArea> object_to_area_extents(
MockTestImageCtx* image_ctx, uint64_t object_no,
const Extents& object_extents) {
Extents extents;
for (auto [off, len] : object_extents) {
Striper::extent_to_file(image_ctx->cct, &image_ctx->layout, object_no, off,
len, extents);
}
return {std::move(extents), ImageArea::DATA};
}
} // namespace util
using ::testing::_;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArg;
using ::testing::WithoutArgs;
using ::testing::Exactly;
struct TestMockIoImageRequest : public TestMockFixture {
typedef ImageRequest<librbd::MockTestImageCtx> MockImageRequest;
typedef ImageReadRequest<librbd::MockTestImageCtx> MockImageReadRequest;
typedef ImageWriteRequest<librbd::MockTestImageCtx> MockImageWriteRequest;
typedef ImageDiscardRequest<librbd::MockTestImageCtx> MockImageDiscardRequest;
typedef ImageFlushRequest<librbd::MockTestImageCtx> MockImageFlushRequest;
typedef ImageWriteSameRequest<librbd::MockTestImageCtx> MockImageWriteSameRequest;
typedef ImageCompareAndWriteRequest<librbd::MockTestImageCtx> MockImageCompareAndWriteRequest;
typedef ImageListSnapsRequest<librbd::MockTestImageCtx> MockImageListSnapsRequest;
void expect_is_journal_appending(MockTestJournal &mock_journal, bool appending) {
EXPECT_CALL(mock_journal, is_journal_appending())
.WillOnce(Return(appending));
}
void expect_get_modify_timestamp(MockTestImageCtx &mock_image_ctx,
bool needs_update) {
if (needs_update) {
mock_image_ctx.mtime_update_interval = 5;
EXPECT_CALL(mock_image_ctx, get_modify_timestamp())
.WillOnce(Return(ceph_clock_now() - utime_t(10,0)));
} else {
mock_image_ctx.mtime_update_interval = 600;
EXPECT_CALL(mock_image_ctx, get_modify_timestamp())
.WillOnce(Return(ceph_clock_now()));
}
}
void expect_journal_append_io_event(MockTestJournal &mock_journal, uint64_t journal_tid,
uint64_t offset, size_t length) {
EXPECT_CALL(mock_journal, append_io_event_mock(_, offset, length, _, _))
.WillOnce(Return(journal_tid));
}
void expect_object_discard_request(MockTestImageCtx &mock_image_ctx,
uint64_t object_no, uint64_t offset,
uint32_t length, int r) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher, send(_))
.WillOnce(Invoke([&mock_image_ctx, object_no, offset, length, r]
(ObjectDispatchSpec* spec) {
auto* discard_spec = boost::get<ObjectDispatchSpec::DiscardRequest>(&spec->request);
ASSERT_TRUE(discard_spec != nullptr);
ASSERT_EQ(object_no, discard_spec->object_no);
ASSERT_EQ(offset, discard_spec->object_off);
ASSERT_EQ(length, discard_spec->object_len);
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
mock_image_ctx.image_ctx->op_work_queue->queue(&spec->dispatcher_ctx, r);
}));
}
void expect_object_request_send(MockTestImageCtx &mock_image_ctx,
int r) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher, send(_))
.WillOnce(Invoke([&mock_image_ctx, r](ObjectDispatchSpec* spec) {
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
mock_image_ctx.image_ctx->op_work_queue->queue(&spec->dispatcher_ctx, r);
}));
}
void expect_object_list_snaps_request(MockTestImageCtx &mock_image_ctx,
uint64_t object_no,
const SnapshotDelta& snap_delta,
int r) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher, send(_))
.WillOnce(
Invoke([&mock_image_ctx, object_no, snap_delta, r]
(ObjectDispatchSpec* spec) {
auto request = boost::get<
librbd::io::ObjectDispatchSpec::ListSnapsRequest>(
&spec->request);
ASSERT_TRUE(request != nullptr);
ASSERT_EQ(object_no, request->object_no);
*request->snapshot_delta = snap_delta;
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
mock_image_ctx.image_ctx->op_work_queue->queue(&spec->dispatcher_ctx, r);
}));
}
};
TEST_F(TestMockIoImageRequest, AioWriteModifyTimestamp) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
mock_image_ctx.mtime_update_interval = 5;
utime_t dummy = ceph_clock_now();
dummy -= utime_t(10,0);
EXPECT_CALL(mock_image_ctx, get_modify_timestamp())
.Times(Exactly(3))
.WillOnce(Return(dummy))
.WillOnce(Return(dummy))
.WillOnce(Return(dummy + utime_t(10,0)));
EXPECT_CALL(mock_image_ctx, set_modify_timestamp(_))
.Times(Exactly(1));
InSequence seq;
expect_is_journal_appending(mock_journal, false);
expect_object_request_send(mock_image_ctx, 0);
C_SaferCond aio_comp_ctx_1, aio_comp_ctx_2;
AioCompletion *aio_comp_1 = AioCompletion::create_and_start(
&aio_comp_ctx_1, ictx, AIO_TYPE_WRITE);
AioCompletion *aio_comp_2 = AioCompletion::create_and_start(
&aio_comp_ctx_2, ictx, AIO_TYPE_WRITE);
bufferlist bl;
bl.append("1");
MockImageWriteRequest mock_aio_image_write_1(
mock_image_ctx, aio_comp_1, {{0, 1}}, ImageArea::DATA, std::move(bl),
0, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_write_1.send();
}
ASSERT_EQ(0, aio_comp_ctx_1.wait());
expect_is_journal_appending(mock_journal, false);
expect_object_request_send(mock_image_ctx, 0);
bl.append("1");
MockImageWriteRequest mock_aio_image_write_2(
mock_image_ctx, aio_comp_2, {{0, 1}}, ImageArea::DATA, std::move(bl),
0, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_write_2.send();
}
ASSERT_EQ(0, aio_comp_ctx_2.wait());
}
TEST_F(TestMockIoImageRequest, AioReadAccessTimestamp) {
REQUIRE_FORMAT_V2();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
mock_image_ctx.atime_update_interval = 5;
utime_t dummy = ceph_clock_now();
dummy -= utime_t(10,0);
EXPECT_CALL(mock_image_ctx, get_access_timestamp())
.Times(Exactly(3))
.WillOnce(Return(dummy))
.WillOnce(Return(dummy))
.WillOnce(Return(dummy + utime_t(10,0)));
EXPECT_CALL(mock_image_ctx, set_access_timestamp(_))
.Times(Exactly(1));
InSequence seq;
expect_object_request_send(mock_image_ctx, 0);
C_SaferCond aio_comp_ctx_1, aio_comp_ctx_2;
AioCompletion *aio_comp_1 = AioCompletion::create_and_start(
&aio_comp_ctx_1, ictx, AIO_TYPE_READ);
ReadResult rr;
MockImageReadRequest mock_aio_image_read_1(
mock_image_ctx, aio_comp_1, {{0, 1}}, ImageArea::DATA, std::move(rr),
mock_image_ctx.get_data_io_context(), 0, 0, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_read_1.send();
}
ASSERT_EQ(1, aio_comp_ctx_1.wait());
AioCompletion *aio_comp_2 = AioCompletion::create_and_start(
&aio_comp_ctx_2, ictx, AIO_TYPE_READ);
expect_object_request_send(mock_image_ctx, 0);
MockImageReadRequest mock_aio_image_read_2(
mock_image_ctx, aio_comp_2, {{0, 1}}, ImageArea::DATA, std::move(rr),
mock_image_ctx.get_data_io_context(), 0, 0, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_read_2.send();
}
ASSERT_EQ(1, aio_comp_ctx_2.wait());
}
TEST_F(TestMockIoImageRequest, PartialDiscard) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->discard_granularity_bytes = 0;
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.journal = nullptr;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_object_discard_request(mock_image_ctx, 0, 16, 63, 0);
expect_object_discard_request(mock_image_ctx, 0, 84, 100, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp, {{16, 63}, {84, 100}}, ImageArea::DATA,
ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, TailDiscard) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, resize(ictx, ictx->layout.object_size));
ictx->discard_granularity_bytes = 2 * ictx->layout.object_size;
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.journal = nullptr;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_object_discard_request(
mock_image_ctx, 0, ictx->layout.object_size - 1024, 1024, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp,
{{ictx->layout.object_size - 1024, 1024}}, ImageArea::DATA,
ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, DiscardGranularity) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, resize(ictx, ictx->layout.object_size));
ictx->discard_granularity_bytes = 32;
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.journal = nullptr;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_object_discard_request(mock_image_ctx, 0, 32, 32, 0);
expect_object_discard_request(mock_image_ctx, 0, 96, 64, 0);
expect_object_discard_request(
mock_image_ctx, 0, ictx->layout.object_size - 32, 32, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp,
{{16, 63}, {96, 31}, {84, 100}, {ictx->layout.object_size - 33, 33}},
ImageArea::DATA, ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, PartialDiscardJournalAppendEnabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->discard_granularity_bytes = 0;
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, true);
expect_journal_append_io_event(mock_journal, 0, 16, 63);
expect_journal_append_io_event(mock_journal, 1, 84, 100);
expect_object_discard_request(mock_image_ctx, 0, 16, 63, 0);
expect_object_discard_request(mock_image_ctx, 0, 84, 100, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp, {{16, 63}, {84, 100}}, ImageArea::DATA,
ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, TailDiscardJournalAppendEnabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, resize(ictx, ictx->layout.object_size));
ictx->discard_granularity_bytes = 2 * ictx->layout.object_size;
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, true);
expect_journal_append_io_event(
mock_journal, 0, ictx->layout.object_size - 1024, 1024);
expect_object_discard_request(
mock_image_ctx, 0, ictx->layout.object_size - 1024, 1024, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp,
{{ictx->layout.object_size - 1024, 1024}}, ImageArea::DATA,
ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, PruneRequiredDiscardJournalAppendEnabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->discard_granularity_bytes = 32;
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, true);
EXPECT_CALL(mock_journal, append_io_event_mock(_, _, _, _, _)).Times(0);
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher, send(_)).Times(0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp, {{96, 31}}, ImageArea::DATA,
ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, LengthModifiedDiscardJournalAppendEnabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->discard_granularity_bytes = 32;
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, true);
expect_journal_append_io_event(mock_journal, 0, 32, 32);
expect_object_discard_request(mock_image_ctx, 0, 32, 32, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp, {{16, 63}}, ImageArea::DATA,
ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, DiscardGranularityJournalAppendEnabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ASSERT_EQ(0, resize(ictx, ictx->layout.object_size));
ictx->discard_granularity_bytes = 32;
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, true);
expect_journal_append_io_event(mock_journal, 0, 32, 32);
expect_journal_append_io_event(mock_journal, 1, 96, 64);
expect_journal_append_io_event(
mock_journal, 2, ictx->layout.object_size - 32, 32);
expect_object_discard_request(mock_image_ctx, 0, 32, 32, 0);
expect_object_discard_request(mock_image_ctx, 0, 96, 64, 0);
expect_object_discard_request(
mock_image_ctx, 0, ictx->layout.object_size - 32, 32, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp,
{{16, 63}, {96, 31}, {84, 100}, {ictx->layout.object_size - 33, 33}},
ImageArea::DATA, ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, AioWriteJournalAppendDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, false);
expect_object_request_send(mock_image_ctx, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_WRITE);
bufferlist bl;
bl.append("1");
MockImageWriteRequest mock_aio_image_write(
mock_image_ctx, aio_comp, {{0, 1}}, ImageArea::DATA, std::move(bl), 0, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_write.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, AioDiscardJournalAppendDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->discard_granularity_bytes = 0;
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, false);
expect_object_request_send(mock_image_ctx, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_DISCARD);
MockImageDiscardRequest mock_aio_image_discard(
mock_image_ctx, aio_comp, {{0, 1}}, ImageArea::DATA,
ictx->discard_granularity_bytes, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_discard.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, AioFlushJournalAppendDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_is_journal_appending(mock_journal, false);
expect_object_request_send(mock_image_ctx, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_FLUSH);
MockImageFlushRequest mock_aio_image_flush(mock_image_ctx, aio_comp,
FLUSH_SOURCE_USER, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_flush.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, AioWriteSameJournalAppendDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, false);
expect_object_request_send(mock_image_ctx, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_WRITESAME);
bufferlist bl;
bl.append("1");
MockImageWriteSameRequest mock_aio_image_writesame(
mock_image_ctx, aio_comp, {{0, 1}}, ImageArea::DATA, std::move(bl), 0, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_writesame.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, AioCompareAndWriteJournalAppendDisabled) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockTestJournal mock_journal;
mock_image_ctx.journal = &mock_journal;
InSequence seq;
expect_get_modify_timestamp(mock_image_ctx, false);
expect_is_journal_appending(mock_journal, false);
expect_object_request_send(mock_image_ctx, 0);
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_COMPARE_AND_WRITE);
bufferlist cmp_bl;
cmp_bl.append("1");
bufferlist write_bl;
write_bl.append("1");
uint64_t mismatch_offset;
MockImageCompareAndWriteRequest mock_aio_image_write(
mock_image_ctx, aio_comp, {{0, 1}}, ImageArea::DATA,
std::move(cmp_bl), std::move(write_bl), &mismatch_offset, 0, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_aio_image_write.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
}
TEST_F(TestMockIoImageRequest, ListSnaps) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.layout.object_size = 16384;
mock_image_ctx.layout.stripe_unit = 4096;
mock_image_ctx.layout.stripe_count = 2;
InSequence seq;
SnapshotDelta object_snapshot_delta;
object_snapshot_delta[{5,6}].insert(
0, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
object_snapshot_delta[{5,5}].insert(
4096, 4096, {SPARSE_EXTENT_STATE_ZEROED, 4096});
expect_object_list_snaps_request(mock_image_ctx, 0, object_snapshot_delta, 0);
object_snapshot_delta = {};
object_snapshot_delta[{5,6}].insert(
1024, 3072, {SPARSE_EXTENT_STATE_DATA, 3072});
object_snapshot_delta[{5,5}].insert(
2048, 2048, {SPARSE_EXTENT_STATE_ZEROED, 2048});
expect_object_list_snaps_request(mock_image_ctx, 1, object_snapshot_delta, 0);
SnapshotDelta snapshot_delta;
C_SaferCond aio_comp_ctx;
AioCompletion *aio_comp = AioCompletion::create_and_start(
&aio_comp_ctx, ictx, AIO_TYPE_GENERIC);
MockImageListSnapsRequest mock_image_list_snaps_request(
mock_image_ctx, aio_comp, {{0, 16384}, {16384, 16384}}, ImageArea::DATA,
{0, CEPH_NOSNAP}, 0, &snapshot_delta, {});
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
mock_image_list_snaps_request.send();
}
ASSERT_EQ(0, aio_comp_ctx.wait());
SnapshotDelta expected_snapshot_delta;
expected_snapshot_delta[{5,6}].insert(
0, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
expected_snapshot_delta[{5,6}].insert(
5120, 3072, {SPARSE_EXTENT_STATE_DATA, 3072});
expected_snapshot_delta[{5,5}].insert(
6144, 6144, {SPARSE_EXTENT_STATE_ZEROED, 6144});
ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
}
} // namespace io
} // namespace librbd
| 25,764 | 33.864682 | 102 |
cc
|
null |
ceph-main/src/test/librbd/io/test_mock_ObjectRequest.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockObjectMap.h"
#include "test/librbd/mock/cache/MockImageCache.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "include/rbd/librbd.hpp"
#include "librbd/io/CopyupRequest.h"
#include "librbd/io/ImageRequest.h"
#include "librbd/io/ObjectRequest.h"
#include "librbd/io/Utils.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace util {
inline ImageCtx *get_image_ctx(MockImageCtx *image_ctx) {
return image_ctx->image_ctx;
}
} // namespace util
namespace io {
template <>
struct CopyupRequest<librbd::MockImageCtx> {
MOCK_METHOD0(send, void());
MOCK_METHOD2(append_request, void(AbstractObjectWriteRequest<librbd::MockTestImageCtx>*,
const Extents&));
};
template <>
struct CopyupRequest<librbd::MockTestImageCtx> : public CopyupRequest<librbd::MockImageCtx> {
static CopyupRequest* s_instance;
static CopyupRequest* create(librbd::MockTestImageCtx *ictx,
uint64_t objectno, Extents &&image_extents,
ImageArea area,
const ZTracer::Trace& parent_trace) {
return s_instance;
}
CopyupRequest() {
s_instance = this;
}
};
CopyupRequest<librbd::MockTestImageCtx>* CopyupRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
template <>
struct ImageListSnapsRequest<librbd::MockTestImageCtx> {
static ImageListSnapsRequest* s_instance;
AioCompletion* aio_comp;
Extents image_extents;
SnapshotDelta* snapshot_delta;
ImageListSnapsRequest() {
s_instance = this;
}
ImageListSnapsRequest(
librbd::MockImageCtx& image_ctx, AioCompletion* aio_comp,
Extents&& image_extents, ImageArea area, SnapIds&& snap_ids,
int list_snaps_flags, SnapshotDelta* snapshot_delta,
const ZTracer::Trace& parent_trace) {
ceph_assert(s_instance != nullptr);
s_instance->aio_comp = aio_comp;
s_instance->image_extents = image_extents;
s_instance->snapshot_delta = snapshot_delta;
}
MOCK_METHOD0(execute_send, void());
void send() {
ceph_assert(s_instance != nullptr);
s_instance->execute_send();
}
};
ImageListSnapsRequest<librbd::MockTestImageCtx>* ImageListSnapsRequest<librbd::MockTestImageCtx>::s_instance = nullptr;
namespace util {
template <>
void area_to_object_extents(MockTestImageCtx* image_ctx, uint64_t offset,
uint64_t length, ImageArea area,
uint64_t buffer_offset,
striper::LightweightObjectExtents* object_extents) {
Striper::file_to_extents(image_ctx->cct, &image_ctx->layout, offset, length,
0, buffer_offset, object_extents);
}
template <>
std::pair<Extents, ImageArea> object_to_area_extents(
MockTestImageCtx* image_ctx, uint64_t object_no,
const Extents& object_extents) {
Extents extents;
for (auto [off, len] : object_extents) {
Striper::extent_to_file(image_ctx->cct, &image_ctx->layout, object_no, off,
len, extents);
}
return {std::move(extents), ImageArea::DATA};
}
namespace {
struct Mock {
static Mock* s_instance;
Mock() {
s_instance = this;
}
MOCK_METHOD6(read_parent,
void(librbd::MockTestImageCtx *, uint64_t, ReadExtents*,
librados::snap_t, const ZTracer::Trace &, Context*));
};
Mock *Mock::s_instance = nullptr;
} // anonymous namespace
template<> void read_parent(
librbd::MockTestImageCtx *image_ctx, uint64_t object_no,
ReadExtents* extents, librados::snap_t snap_id,
const ZTracer::Trace &trace, Context* on_finish) {
Mock::s_instance->read_parent(image_ctx, object_no, extents, snap_id, trace,
on_finish);
}
} // namespace util
} // namespace io
} // namespace librbd
#include "librbd/io/ObjectRequest.cc"
namespace librbd {
namespace io {
using ::testing::_;
using ::testing::DoDefault;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArg;
using ::testing::WithArgs;
struct TestMockIoObjectRequest : public TestMockFixture {
typedef ObjectRequest<librbd::MockTestImageCtx> MockObjectRequest;
typedef ObjectReadRequest<librbd::MockTestImageCtx> MockObjectReadRequest;
typedef ObjectWriteRequest<librbd::MockTestImageCtx> MockObjectWriteRequest;
typedef ObjectDiscardRequest<librbd::MockTestImageCtx> MockObjectDiscardRequest;
typedef ObjectWriteSameRequest<librbd::MockTestImageCtx> MockObjectWriteSameRequest;
typedef ObjectCompareAndWriteRequest<librbd::MockTestImageCtx> MockObjectCompareAndWriteRequest;
typedef ObjectListSnapsRequest<librbd::MockTestImageCtx> MockObjectListSnapsRequest;
typedef AbstractObjectWriteRequest<librbd::MockTestImageCtx> MockAbstractObjectWriteRequest;
typedef CopyupRequest<librbd::MockTestImageCtx> MockCopyupRequest;
typedef ImageListSnapsRequest<librbd::MockTestImageCtx> MockImageListSnapsRequest;
typedef util::Mock MockUtils;
void expect_object_may_exist(MockTestImageCtx &mock_image_ctx,
uint64_t object_no, bool exists) {
if (mock_image_ctx.object_map != nullptr) {
EXPECT_CALL(*mock_image_ctx.object_map, object_may_exist(object_no))
.WillOnce(Return(exists));
}
}
void expect_get_object_size(MockTestImageCtx &mock_image_ctx) {
EXPECT_CALL(mock_image_ctx, get_object_size()).WillRepeatedly(Return(
mock_image_ctx.layout.object_size));
}
void expect_get_parent_overlap(MockTestImageCtx &mock_image_ctx,
librados::snap_t snap_id, uint64_t overlap,
int r) {
EXPECT_CALL(mock_image_ctx, get_parent_overlap(snap_id, _))
.WillOnce(WithArg<1>(Invoke([overlap, r](uint64_t *o) {
*o = overlap;
return r;
})));
}
void expect_prune_parent_extents(MockTestImageCtx &mock_image_ctx,
const Extents& extents,
uint64_t overlap, uint64_t object_overlap) {
EXPECT_CALL(mock_image_ctx, prune_parent_extents(_, _, overlap, _))
.WillOnce(WithArg<0>(Invoke([extents, object_overlap](Extents& e) {
e = extents;
return object_overlap;
})));
}
void expect_get_read_flags(MockTestImageCtx &mock_image_ctx,
librados::snap_t snap_id, int flags) {
EXPECT_CALL(mock_image_ctx, get_read_flags(snap_id))
.WillOnce(Return(flags));
}
void expect_is_lock_owner(MockExclusiveLock& mock_exclusive_lock) {
EXPECT_CALL(mock_exclusive_lock, is_lock_owner()).WillRepeatedly(
Return(true));
}
void expect_read(MockTestImageCtx &mock_image_ctx,
const std::string& oid, uint64_t off, uint64_t len,
const std::string& data, int r) {
bufferlist bl;
bl.append(data);
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto& expect = EXPECT_CALL(mock_io_ctx, read(oid, len, off, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(WithArg<3>(Invoke([bl](bufferlist *out_bl) {
out_bl->append(bl);
return bl.length();
})));
}
}
void expect_sparse_read(MockTestImageCtx &mock_image_ctx,
const std::string& oid, uint64_t off, uint64_t len,
const std::string& data, int r) {
bufferlist bl;
bl.append(data);
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto& expect = EXPECT_CALL(mock_io_ctx,
sparse_read(oid, off, len, _, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(WithArg<4>(Invoke([bl](bufferlist *out_bl) {
out_bl->append(bl);
return bl.length();
})));
}
}
void expect_read_parent(MockUtils &mock_utils, uint64_t object_no,
ReadExtents* extents, librados::snap_t snap_id,
int r) {
EXPECT_CALL(mock_utils,
read_parent(_, object_no, extents, snap_id, _, _))
.WillOnce(WithArg<5>(CompleteContext(r, static_cast<asio::ContextWQ*>(nullptr))));
}
void expect_copyup(MockCopyupRequest& mock_copyup_request, int r) {
EXPECT_CALL(mock_copyup_request, send())
.WillOnce(Invoke([]() {}));
}
void expect_copyup(MockCopyupRequest& mock_copyup_request,
MockAbstractObjectWriteRequest** write_request, int r) {
EXPECT_CALL(mock_copyup_request, append_request(_, _))
.WillOnce(WithArg<0>(
Invoke([write_request](MockAbstractObjectWriteRequest *req) {
*write_request = req;
})));
EXPECT_CALL(mock_copyup_request, send())
.WillOnce(Invoke([write_request, r]() {
(*write_request)->handle_copyup(r);
}));
}
void expect_object_map_update(MockTestImageCtx &mock_image_ctx,
uint64_t start_object, uint64_t end_object,
uint8_t state,
const boost::optional<uint8_t> ¤t_state,
bool updated, int ret_val) {
if (mock_image_ctx.object_map != nullptr) {
EXPECT_CALL(*mock_image_ctx.object_map,
aio_update(CEPH_NOSNAP, start_object, end_object, state,
current_state, _, false, _))
.WillOnce(WithArg<7>(Invoke([&mock_image_ctx, updated, ret_val](Context *ctx) {
if (updated) {
mock_image_ctx.op_work_queue->queue(ctx, ret_val);
}
return updated;
})));
}
}
void expect_assert_exists(MockTestImageCtx &mock_image_ctx, int r) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
EXPECT_CALL(mock_io_ctx, assert_exists(_, _))
.WillOnce(Return(r));
}
void expect_write(MockTestImageCtx &mock_image_ctx,
uint64_t offset, uint64_t length, int r) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto &expect = EXPECT_CALL(mock_io_ctx,
write(_, _, length, offset, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_write_full(MockTestImageCtx &mock_image_ctx, int r) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto &expect = EXPECT_CALL(mock_io_ctx, write_full(_, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_writesame(MockTestImageCtx &mock_image_ctx,
uint64_t offset, uint64_t length, int r) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto &expect = EXPECT_CALL(mock_io_ctx, writesame(_, _, length, offset, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_remove(MockTestImageCtx &mock_image_ctx, int r) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto &expect = EXPECT_CALL(mock_io_ctx, remove(_, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_create(MockTestImageCtx &mock_image_ctx, bool exclusive) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
EXPECT_CALL(mock_io_ctx, create(_, exclusive, _))
.Times(1);
}
void expect_truncate(MockTestImageCtx &mock_image_ctx, int offset, int r) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto &expect = EXPECT_CALL(mock_io_ctx, truncate(_, offset, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_zero(MockTestImageCtx &mock_image_ctx, int offset, int length,
int r) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto &expect = EXPECT_CALL(mock_io_ctx, zero(_, offset, length, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_cmpext(MockTestImageCtx &mock_image_ctx, int offset, int r) {
auto& mock_io_ctx = librados::get_mock_io_ctx(
mock_image_ctx.rados_api, *mock_image_ctx.get_data_io_context());
auto &expect = EXPECT_CALL(mock_io_ctx, cmpext(_, offset, _, _));
if (r < 0) {
expect.WillOnce(Return(r));
} else {
expect.WillOnce(DoDefault());
}
}
void expect_list_snaps(MockTestImageCtx &mock_image_ctx,
const librados::snap_set_t& snap_set, int r) {
auto io_context = *mock_image_ctx.get_data_io_context();
io_context.read_snap(CEPH_SNAPDIR);
auto& mock_io_ctx = librados::get_mock_io_ctx(mock_image_ctx.rados_api,
io_context);
EXPECT_CALL(mock_io_ctx, list_snaps(_, _))
.WillOnce(WithArg<1>(Invoke(
[snap_set, r](librados::snap_set_t* out_snap_set) {
*out_snap_set = snap_set;
return r;
})));
}
void expect_image_list_snaps(MockImageListSnapsRequest& req,
const Extents& image_extents,
const SnapshotDelta& image_snapshot_delta,
int r) {
EXPECT_CALL(req, execute_send())
.WillOnce(Invoke(
[&req, image_extents, image_snapshot_delta, r]() {
ASSERT_EQ(image_extents, req.image_extents);
*req.snapshot_delta = image_snapshot_delta;
auto aio_comp = req.aio_comp;
aio_comp->set_request_count(1);
aio_comp->add_request();
aio_comp->complete_request(r);
}));
}
};
TEST_F(TestMockIoObjectRequest, Read) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->sparse_read_threshold_bytes = 8096;
MockTestImageCtx mock_image_ctx(*ictx);
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_object_may_exist(mock_image_ctx, 0, true);
expect_get_read_flags(mock_image_ctx, CEPH_NOSNAP, 0);
expect_read(mock_image_ctx, ictx->get_object_name(0), 0, 4096,
std::string(4096, '1'), 0);
expect_read(mock_image_ctx, ictx->get_object_name(0), 8192, 4096,
std::string(4096, '2'), 0);
C_SaferCond ctx;
uint64_t version;
ReadExtents extents = {{0, 4096}, {8192, 4096}};
auto req = MockObjectReadRequest::create(
&mock_image_ctx, 0, &extents,
mock_image_ctx.get_data_io_context(), 0, 0, {},
&version, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
bufferlist expected_bl1;
expected_bl1.append(std::string(4096, '1'));
bufferlist expected_bl2;
expected_bl2.append(std::string(4096, '2'));
ASSERT_EQ(extents[0].extent_map.size(), 0);
ASSERT_EQ(extents[1].extent_map.size(), 0);
ASSERT_TRUE(extents[0].bl.contents_equal(expected_bl1));
ASSERT_TRUE(extents[1].bl.contents_equal(expected_bl2));
}
TEST_F(TestMockIoObjectRequest, SparseReadThreshold) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->sparse_read_threshold_bytes = ictx->get_object_size();
MockTestImageCtx mock_image_ctx(*ictx);
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_object_may_exist(mock_image_ctx, 0, true);
expect_get_read_flags(mock_image_ctx, CEPH_NOSNAP, 0);
expect_sparse_read(mock_image_ctx, ictx->get_object_name(0), 0,
ictx->sparse_read_threshold_bytes,
std::string(ictx->sparse_read_threshold_bytes, '1'), 0);
C_SaferCond ctx;
ReadExtents extents = {{0, ictx->sparse_read_threshold_bytes}};
auto req = MockObjectReadRequest::create(
&mock_image_ctx, 0, &extents,
mock_image_ctx.get_data_io_context(), 0, 0, {}, nullptr, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, ReadError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
ictx->sparse_read_threshold_bytes = 8096;
MockTestImageCtx mock_image_ctx(*ictx);
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_object_may_exist(mock_image_ctx, 0, true);
expect_get_read_flags(mock_image_ctx, CEPH_NOSNAP, 0);
expect_read(mock_image_ctx, ictx->get_object_name(0), 0, 4096, "", -EPERM);
C_SaferCond ctx;
ReadExtents extents = {{0, 4096}};
auto req = MockObjectReadRequest::create(
&mock_image_ctx, 0, &extents,
mock_image_ctx.get_data_io_context(), 0, 0, {},
nullptr, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, ParentRead) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
ictx->sparse_read_threshold_bytes = 8096;
ictx->clone_copy_on_read = false;
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.parent = &mock_image_ctx;
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_object_may_exist(mock_image_ctx, 0, true);
expect_get_read_flags(mock_image_ctx, CEPH_NOSNAP, 0);
expect_read(mock_image_ctx, ictx->get_object_name(0), 0, 4096, "", -ENOENT);
MockUtils mock_utils;
ReadExtents extents = {{0, 4096}};
expect_read_parent(mock_utils, 0, &extents, CEPH_NOSNAP, 0);
C_SaferCond ctx;
auto req = MockObjectReadRequest::create(
&mock_image_ctx, 0, &extents,
mock_image_ctx.get_data_io_context(), 0, 0, {},
nullptr, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, ParentReadError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
ictx->sparse_read_threshold_bytes = 8096;
ictx->clone_copy_on_read = false;
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.parent = &mock_image_ctx;
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_object_may_exist(mock_image_ctx, 0, true);
expect_get_read_flags(mock_image_ctx, CEPH_NOSNAP, 0);
expect_read(mock_image_ctx, ictx->get_object_name(0), 0, 4096, "", -ENOENT);
MockUtils mock_utils;
ReadExtents extents = {{0, 4096}};
expect_read_parent(mock_utils, 0, &extents, CEPH_NOSNAP, -EPERM);
C_SaferCond ctx;
auto req = MockObjectReadRequest::create(
&mock_image_ctx, 0, &extents,
mock_image_ctx.get_data_io_context(), 0, 0, {},
nullptr, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, SkipParentRead) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
ictx->sparse_read_threshold_bytes = 8096;
ictx->clone_copy_on_read = false;
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.parent = &mock_image_ctx;
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_object_may_exist(mock_image_ctx, 0, true);
expect_get_read_flags(mock_image_ctx, CEPH_NOSNAP, 0);
expect_read(mock_image_ctx, ictx->get_object_name(0), 0, 4096, "", -ENOENT);
ReadExtents extents = {{0, 4096}};
C_SaferCond ctx;
auto req = MockObjectReadRequest::create(
&mock_image_ctx, 0, &extents, mock_image_ctx.get_data_io_context(), 0,
READ_FLAG_DISABLE_READ_FROM_PARENT, {}, nullptr, &ctx);
req->send();
ASSERT_EQ(-ENOENT, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, CopyOnRead) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
ictx->sparse_read_threshold_bytes = 8096;
ictx->clone_copy_on_read = true;
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.parent = &mock_image_ctx;
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_object_may_exist(mock_image_ctx, 0, true);
expect_get_read_flags(mock_image_ctx, CEPH_NOSNAP, 0);
expect_read(mock_image_ctx, ictx->get_object_name(0), 0, 4096, "", -ENOENT);
MockUtils mock_utils;
ReadExtents extents = {{0, 4096}};
expect_read_parent(mock_utils, 0, &extents, CEPH_NOSNAP, 0);
MockCopyupRequest mock_copyup_request;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
expect_copyup(mock_copyup_request, 0);
C_SaferCond ctx;
auto req = MockObjectReadRequest::create(
&mock_image_ctx, 0, &extents,
mock_image_ctx.get_data_io_context(), 0, 0, {},
nullptr, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, Write) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_write(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, WriteWithCreateExclusiveFlag) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
// exclusive create should succeed
{
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false,
0);
expect_write(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl),
mock_image_ctx.get_data_io_context(), 0,
OBJECT_WRITE_FLAG_CREATE_EXCLUSIVE, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
// exclusive create should fail since object already exists
{
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false,
0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl),
mock_image_ctx.get_data_io_context(), 0,
OBJECT_WRITE_FLAG_CREATE_EXCLUSIVE, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(-EEXIST, ctx.wait());
}
}
TEST_F(TestMockIoObjectRequest, WriteWithAssertVersion) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
// write an object
{
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false,
0);
expect_write(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl),
mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
// assert version should succeed (version = 1)
{
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false,
0);
expect_write(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl),
mock_image_ctx.get_data_io_context(), 0, 0,
std::make_optional(1), {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
// assert with wrong (lower) version (version = 2)
{
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false,
0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl),
mock_image_ctx.get_data_io_context(), 0, 0,
std::make_optional(1), {}, &ctx);
req->send();
ASSERT_EQ(-ERANGE, ctx.wait());
}
// assert with wrong (higher) version (version = 2)
{
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false,
0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl),
mock_image_ctx.get_data_io_context(), 0, 0, std::make_optional(3),
{}, &ctx);
req->send();
ASSERT_EQ(-EOVERFLOW, ctx.wait());
}
}
TEST_F(TestMockIoObjectRequest, WriteFull) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist bl;
bl.append(std::string(ictx->get_object_size(), '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_write_full(mock_image_ctx, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, WriteObjectMap) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, true, 0);
expect_write(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, WriteError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_write(mock_image_ctx, 0, 4096, -EPERM);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, Copyup) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_assert_exists(mock_image_ctx, -ENOENT);
MockAbstractObjectWriteRequest *write_request = nullptr;
MockCopyupRequest mock_copyup_request;
expect_copyup(mock_copyup_request, &write_request, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, CopyupRestart) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_assert_exists(mock_image_ctx, -ENOENT);
MockAbstractObjectWriteRequest *write_request = nullptr;
MockCopyupRequest mock_copyup_request;
expect_copyup(mock_copyup_request, &write_request, -ERESTART);
expect_assert_exists(mock_image_ctx, 0);
expect_write(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, CopyupOptimization) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING | RBD_FEATURE_OBJECT_MAP);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
expect_object_may_exist(mock_image_ctx, 0, false);
MockAbstractObjectWriteRequest *write_request = nullptr;
MockCopyupRequest mock_copyup_request;
expect_copyup(mock_copyup_request, &write_request, 0);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, CopyupError) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
expect_assert_exists(mock_image_ctx, -ENOENT);
MockAbstractObjectWriteRequest *write_request = nullptr;
MockCopyupRequest mock_copyup_request;
expect_copyup(mock_copyup_request, &write_request, -EPERM);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, DiscardRemove) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_PENDING, {}, false, 0);
expect_remove(mock_image_ctx, 0);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_NONEXISTENT,
OBJECT_PENDING, false, 0);
C_SaferCond ctx;
auto req = MockObjectDiscardRequest::create_discard(
&mock_image_ctx, 0, 0, mock_image_ctx.get_object_size(),
mock_image_ctx.get_data_io_context(), 0, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, DiscardRemoveTruncate) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
uint64_t features;
ASSERT_EQ(0, image.features(&features));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), features, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
expect_object_may_exist(mock_image_ctx, 0, false);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_create(mock_image_ctx, false);
expect_truncate(mock_image_ctx, 0, 0);
C_SaferCond ctx;
auto req = MockObjectDiscardRequest::create_discard(
&mock_image_ctx, 0, 0, mock_image_ctx.get_object_size(),
mock_image_ctx.get_data_io_context(),
OBJECT_DISCARD_FLAG_DISABLE_CLONE_REMOVE, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, DiscardTruncateAssertExists) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
uint64_t features;
ASSERT_EQ(0, image.features(&features));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), features, &order));
ASSERT_EQ(0, rbd.open(m_ioctx, image, clone_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
image.close();
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_assert_exists(mock_image_ctx, 0);
expect_truncate(mock_image_ctx, 0, 0);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.data_ctx), create(_, _, _))
.Times(0);
C_SaferCond ctx;
auto req = MockObjectDiscardRequest::create_discard(
&mock_image_ctx, 0, 0, mock_image_ctx.get_object_size(),
mock_image_ctx.get_data_io_context(),
OBJECT_DISCARD_FLAG_DISABLE_CLONE_REMOVE, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, DiscardTruncate) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_truncate(mock_image_ctx, 1, 0);
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.data_ctx), create(_, _, _))
.Times(0);
C_SaferCond ctx;
auto req = MockObjectDiscardRequest::create_discard(
&mock_image_ctx, 0, 1, mock_image_ctx.get_object_size() - 1,
mock_image_ctx.get_data_io_context(), 0, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, DiscardZero) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_zero(mock_image_ctx, 1, 1, 0);
C_SaferCond ctx;
auto req = MockObjectDiscardRequest::create_discard(
&mock_image_ctx, 0, 1, 1, mock_image_ctx.get_data_io_context(), 0, {},
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, DiscardDisableObjectMapUpdate) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_remove(mock_image_ctx, 0);
C_SaferCond ctx;
auto req = MockObjectDiscardRequest::create_discard(
&mock_image_ctx, 0, 0, mock_image_ctx.get_object_size(),
mock_image_ctx.get_data_io_context(),
OBJECT_DISCARD_FLAG_DISABLE_CLONE_REMOVE |
OBJECT_DISCARD_FLAG_DISABLE_OBJECT_MAP_UPDATE, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, DiscardNoOp) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, false);
C_SaferCond ctx;
auto req = MockObjectDiscardRequest::create_discard(
&mock_image_ctx, 0, 0, mock_image_ctx.get_object_size(),
mock_image_ctx.get_data_io_context(),
OBJECT_DISCARD_FLAG_DISABLE_CLONE_REMOVE |
OBJECT_DISCARD_FLAG_DISABLE_OBJECT_MAP_UPDATE, {},
&ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, WriteSame) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_writesame(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
auto req = MockObjectWriteSameRequest::create_write_same(
&mock_image_ctx, 0, 0, 4096, std::move(bl),
mock_image_ctx.get_data_io_context(), 0, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, CompareAndWrite) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist cmp_bl;
cmp_bl.append_zero(4096);
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_cmpext(mock_image_ctx, 0, 0);
expect_write(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
uint64_t mismatch_offset;
auto req = MockObjectWriteSameRequest::create_compare_and_write(
&mock_image_ctx, 0, 0, std::move(cmp_bl), std::move(bl),
mock_image_ctx.get_data_io_context(), &mismatch_offset, 0, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, CompareAndWriteFull) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist cmp_bl;
cmp_bl.append_zero(ictx->get_object_size());
bufferlist bl;
bl.append(std::string(ictx->get_object_size(), '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_cmpext(mock_image_ctx, 0, 0);
expect_write_full(mock_image_ctx, 0);
C_SaferCond ctx;
uint64_t mismatch_offset;
auto req = MockObjectWriteSameRequest::create_compare_and_write(
&mock_image_ctx, 0, 0, std::move(cmp_bl), std::move(bl),
mock_image_ctx.get_data_io_context(), &mismatch_offset, 0, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, CompareAndWriteCopyup) {
REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
librbd::Image image;
librbd::RBD rbd;
ASSERT_EQ(0, rbd.open(m_ioctx, image, m_image_name.c_str(), NULL));
ASSERT_EQ(0, image.snap_create("one"));
ASSERT_EQ(0, image.snap_protect("one"));
image.close();
std::string clone_name = get_temp_image_name();
int order = 0;
ASSERT_EQ(0, rbd.clone(m_ioctx, m_image_name.c_str(), "one", m_ioctx,
clone_name.c_str(), RBD_FEATURE_LAYERING, &order));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(clone_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist cmp_bl;
cmp_bl.append_zero(4096);
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_assert_exists(mock_image_ctx, -ENOENT);
MockAbstractObjectWriteRequest *write_request = nullptr;
MockCopyupRequest mock_copyup_request;
expect_copyup(mock_copyup_request, &write_request, 0);
expect_assert_exists(mock_image_ctx, 0);
expect_cmpext(mock_image_ctx, 0, 0);
expect_write(mock_image_ctx, 0, 4096, 0);
C_SaferCond ctx;
uint64_t mismatch_offset;
auto req = MockObjectWriteSameRequest::create_compare_and_write(
&mock_image_ctx, 0, 0, std::move(cmp_bl), std::move(bl),
mock_image_ctx.get_data_io_context(), &mismatch_offset, 0, {}, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, CompareAndWriteMismatch) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
if (ictx->test_features(RBD_FEATURE_EXCLUSIVE_LOCK)) {
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
}
MockObjectMap mock_object_map;
if (ictx->test_features(RBD_FEATURE_OBJECT_MAP)) {
mock_image_ctx.object_map = &mock_object_map;
}
bufferlist cmp_bl;
cmp_bl.append_zero(4096);
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, false, 0);
expect_cmpext(mock_image_ctx, 0, -MAX_ERRNO - 1);
C_SaferCond ctx;
uint64_t mismatch_offset;
auto req = MockObjectWriteSameRequest::create_compare_and_write(
&mock_image_ctx, 0, 0, std::move(cmp_bl), std::move(bl),
mock_image_ctx.get_data_io_context(), &mismatch_offset, 0, {}, &ctx);
req->send();
ASSERT_EQ(-EILSEQ, ctx.wait());
ASSERT_EQ(1ULL, mismatch_offset);
}
TEST_F(TestMockIoObjectRequest, ObjectMapError) {
REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_op_work_queue(mock_image_ctx);
expect_get_object_size(mock_image_ctx);
MockExclusiveLock mock_exclusive_lock;
mock_image_ctx.exclusive_lock = &mock_exclusive_lock;
expect_is_lock_owner(mock_exclusive_lock);
MockObjectMap mock_object_map;
mock_image_ctx.object_map = &mock_object_map;
bufferlist bl;
bl.append(std::string(4096, '1'));
InSequence seq;
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 0, 0);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_object_map_update(mock_image_ctx, 0, 1, OBJECT_EXISTS, {}, true,
-EBLOCKLISTED);
C_SaferCond ctx;
auto req = MockObjectWriteRequest::create_write(
&mock_image_ctx, 0, 0, std::move(bl), mock_image_ctx.get_data_io_context(),
0, 0, std::nullopt, {}, &ctx);
req->send();
ASSERT_EQ(-EBLOCKLISTED, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, ListSnaps) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.snaps = {3, 4, 5, 6, 7};
librados::snap_set_t snap_set;
snap_set.seq = 6;
librados::clone_info_t clone_info;
clone_info.cloneid = 3;
clone_info.snaps = {3};
clone_info.overlap = std::vector<std::pair<uint64_t,uint64_t>>{
{0, 4194304}};
clone_info.size = 4194304;
snap_set.clones.push_back(clone_info);
clone_info.cloneid = 4;
clone_info.snaps = {4};
clone_info.overlap = std::vector<std::pair<uint64_t,uint64_t>>{
{278528, 4096}, {442368, 4096}, {1859584, 4096}, {2224128, 4096},
{2756608, 4096}, {3227648, 4096}, {3739648, 4096}, {3903488, 4096}};
clone_info.size = 4194304;
snap_set.clones.push_back(clone_info);
clone_info.cloneid = 6;
clone_info.snaps = {5, 6};
clone_info.overlap = std::vector<std::pair<uint64_t,uint64_t>>{
{425984, 4096}, {440320, 1024}, {1925120, 4096}, {2125824, 4096},
{2215936, 5120}, {3067904, 4096}};
clone_info.size = 3072000;
snap_set.clones.push_back(clone_info);
clone_info.cloneid = CEPH_NOSNAP;
clone_info.snaps = {};
clone_info.overlap = {};
clone_info.size = 4194304;
snap_set.clones.push_back(clone_info);
expect_list_snaps(mock_image_ctx, snap_set, 0);
SnapshotDelta snapshot_delta;
C_SaferCond ctx;
auto req = MockObjectListSnapsRequest::create(
&mock_image_ctx, 0,
{{440320, 1024}, {2122728, 1024}, {2220032, 2048}, {3072000, 4096}},
{3, 4, 5, 6, 7, CEPH_NOSNAP}, 0, {}, &snapshot_delta, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
SnapshotDelta expected_snapshot_delta;
expected_snapshot_delta[{5,6}].insert(
440320, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
expected_snapshot_delta[{5,6}].insert(
2122728, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
expected_snapshot_delta[{5,6}].insert(
2220032, 2048, {SPARSE_EXTENT_STATE_DATA, 2048});
expected_snapshot_delta[{7,CEPH_NOSNAP}].insert(
2122728, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
expected_snapshot_delta[{7,CEPH_NOSNAP}].insert(
2221056, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
expected_snapshot_delta[{7,CEPH_NOSNAP}].insert(
3072000, 4096, {SPARSE_EXTENT_STATE_DATA, 4096});
expected_snapshot_delta[{5,5}].insert(
3072000, 4096, {SPARSE_EXTENT_STATE_ZEROED, 4096});
ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
}
TEST_F(TestMockIoObjectRequest, ListSnapsENOENT) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_list_snaps(mock_image_ctx, {}, -ENOENT);
SnapshotDelta snapshot_delta;
C_SaferCond ctx;
auto req = MockObjectListSnapsRequest::create(
&mock_image_ctx, 0,
{{440320, 1024}},
{0, CEPH_NOSNAP}, 0, {}, &snapshot_delta, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
SnapshotDelta expected_snapshot_delta;
expected_snapshot_delta[{0,0}].insert(
440320, 1024, {SPARSE_EXTENT_STATE_DNE, 1024});
ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
}
TEST_F(TestMockIoObjectRequest, ListSnapsDNE) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.snaps = {2, 3, 4};
librados::snap_set_t snap_set;
snap_set.seq = 6;
librados::clone_info_t clone_info;
clone_info.cloneid = 4;
clone_info.snaps = {3, 4};
clone_info.overlap = std::vector<std::pair<uint64_t,uint64_t>>{
{0, 4194304}};
clone_info.size = 4194304;
snap_set.clones.push_back(clone_info);
expect_list_snaps(mock_image_ctx, snap_set, 0);
SnapshotDelta snapshot_delta;
C_SaferCond ctx;
auto req = MockObjectListSnapsRequest::create(
&mock_image_ctx, 0,
{{440320, 1024}},
{2, 3, 4}, 0, {}, &snapshot_delta, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
SnapshotDelta expected_snapshot_delta;
expected_snapshot_delta[{2,2}].insert(
440320, 1024, {SPARSE_EXTENT_STATE_DNE, 1024});
expected_snapshot_delta[{3,4}].insert(
440320, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
}
TEST_F(TestMockIoObjectRequest, ListSnapsEmpty) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_list_snaps(mock_image_ctx, {}, 0);
SnapshotDelta snapshot_delta;
C_SaferCond ctx;
auto req = MockObjectListSnapsRequest::create(
&mock_image_ctx, 0,
{{440320, 1024}},
{0, CEPH_NOSNAP}, 0, {}, &snapshot_delta, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
SnapshotDelta expected_snapshot_delta;
expected_snapshot_delta[{0,0}].insert(
440320, 1024, {SPARSE_EXTENT_STATE_ZEROED, 1024});
ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
}
TEST_F(TestMockIoObjectRequest, ListSnapsError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
expect_list_snaps(mock_image_ctx, {}, -EPERM);
SnapshotDelta snapshot_delta;
C_SaferCond ctx;
auto req = MockObjectListSnapsRequest::create(
&mock_image_ctx, 0,
{{440320, 1024}, {2122728, 1024}, {2220032, 2048}, {3072000, 4096}},
{3, CEPH_NOSNAP}, 0, {}, &snapshot_delta, &ctx);
req->send();
ASSERT_EQ(-EPERM, ctx.wait());
}
TEST_F(TestMockIoObjectRequest, ListSnapsParent) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.parent = &mock_image_ctx;
InSequence seq;
expect_list_snaps(mock_image_ctx, {}, -ENOENT);
expect_get_parent_overlap(mock_image_ctx, CEPH_NOSNAP, 4096, 0);
expect_prune_parent_extents(mock_image_ctx, {{0, 4096}}, 4096, 4096);
MockImageListSnapsRequest mock_image_list_snaps_request;
SnapshotDelta image_snapshot_delta;
image_snapshot_delta[{1,6}].insert(
0, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
expect_image_list_snaps(mock_image_list_snaps_request,
{{0, 4096}}, image_snapshot_delta, 0);
SnapshotDelta snapshot_delta;
C_SaferCond ctx;
auto req = MockObjectListSnapsRequest::create(
&mock_image_ctx, 0,
{{440320, 1024}, {2122728, 1024}, {2220032, 2048}, {3072000, 4096}},
{0, CEPH_NOSNAP}, 0, {}, &snapshot_delta, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
SnapshotDelta expected_snapshot_delta;
expected_snapshot_delta[{0,0}].insert(
0, 1024, {SPARSE_EXTENT_STATE_DATA, 1024});
ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
}
TEST_F(TestMockIoObjectRequest, ListSnapsWholeObject) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
mock_image_ctx.parent = &mock_image_ctx;
InSequence seq;
librados::snap_set_t snap_set;
snap_set.seq = 3;
librados::clone_info_t clone_info;
clone_info.cloneid = 3;
clone_info.snaps = {3};
clone_info.overlap = std::vector<std::pair<uint64_t,uint64_t>>{{0, 1}};
clone_info.size = 4194304;
snap_set.clones.push_back(clone_info);
clone_info.cloneid = CEPH_NOSNAP;
clone_info.snaps = {};
clone_info.overlap = {};
clone_info.size = 4194304;
snap_set.clones.push_back(clone_info);
expect_list_snaps(mock_image_ctx, snap_set, 0);
SnapshotDelta snapshot_delta;
C_SaferCond ctx;
auto req = MockObjectListSnapsRequest::create(
&mock_image_ctx, 0, {{0, mock_image_ctx.layout.object_size - 1}},
{0, CEPH_NOSNAP}, 0, {}, &snapshot_delta, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
SnapshotDelta expected_snapshot_delta;
expected_snapshot_delta[{CEPH_NOSNAP,CEPH_NOSNAP}].insert(
0, mock_image_ctx.layout.object_size - 1,
{SPARSE_EXTENT_STATE_DATA, mock_image_ctx.layout.object_size - 1});
ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
}
} // namespace io
} // namespace librbd
| 65,050 | 32.037583 | 119 |
cc
|
null |
ceph-main/src/test/librbd/io/test_mock_QosImageDispatch.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/exclusive_lock/MockPolicy.h"
#include "librbd/io/ImageDispatchSpec.h"
#include "librbd/io/ImageRequestWQ.h"
#include "librbd/io/ImageRequest.h"
namespace librbd {
namespace io {
TEST_F(TestMockIoImageRequestWQ, QosNoLimit) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageDispatchSpec mock_queued_image_request;
expect_was_throttled(mock_queued_image_request, false);
expect_set_throttled(mock_queued_image_request);
InSequence seq;
MockImageRequestWQ mock_image_request_wq(&mock_image_ctx, "io", 60, nullptr);
mock_image_request_wq.apply_qos_limit(IMAGE_DISPATCH_FLAG_QOS_BPS_THROTTLE, 0,
0, 1);
expect_front(mock_image_request_wq, &mock_queued_image_request);
expect_is_refresh_request(mock_image_ctx, false);
expect_is_write_op(mock_queued_image_request, true);
expect_dequeue(mock_image_request_wq, &mock_queued_image_request);
ASSERT_TRUE(mock_image_request_wq.invoke_dequeue() == &mock_queued_image_request);
}
TEST_F(TestMockIoImageRequestWQ, BPSQosNoBurst) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageDispatchSpec mock_queued_image_request;
expect_was_throttled(mock_queued_image_request, false);
expect_set_throttled(mock_queued_image_request);
InSequence seq;
MockImageRequestWQ mock_image_request_wq(&mock_image_ctx, "io", 60, nullptr);
mock_image_request_wq.apply_qos_limit(IMAGE_DISPATCH_FLAG_QOS_BPS_THROTTLE, 1,
0, 1);
expect_front(mock_image_request_wq, &mock_queued_image_request);
expect_tokens_requested(mock_queued_image_request, 2, true);
expect_dequeue(mock_image_request_wq, &mock_queued_image_request);
expect_all_throttled(mock_queued_image_request, true);
expect_requeue_back(mock_image_request_wq);
expect_signal(mock_image_request_wq);
ASSERT_TRUE(mock_image_request_wq.invoke_dequeue() == nullptr);
}
TEST_F(TestMockIoImageRequestWQ, BPSQosWithBurst) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockImageDispatchSpec mock_queued_image_request;
expect_was_throttled(mock_queued_image_request, false);
expect_set_throttled(mock_queued_image_request);
InSequence seq;
MockImageRequestWQ mock_image_request_wq(&mock_image_ctx, "io", 60, nullptr);
mock_image_request_wq.apply_qos_limit(IMAGE_DISPATCH_FLAG_QOS_BPS_THROTTLE, 1,
1, 1);
expect_front(mock_image_request_wq, &mock_queued_image_request);
expect_tokens_requested(mock_queued_image_request, 2, true);
expect_dequeue(mock_image_request_wq, &mock_queued_image_request);
expect_all_throttled(mock_queued_image_request, true);
expect_requeue_back(mock_image_request_wq);
expect_signal(mock_image_request_wq);
ASSERT_TRUE(mock_image_request_wq.invoke_dequeue() == nullptr);
}
} // namespace io
} // namespace librbd
| 3,296 | 35.633333 | 84 |
cc
|
null |
ceph-main/src/test/librbd/io/test_mock_SimpleSchedulerObjectDispatch.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockSafeTimer.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "test/librados_test_stub/MockTestMemRadosClient.h"
#include "include/rbd/librbd.hpp"
#include "librbd/io/ObjectDispatchSpec.h"
#include "librbd/io/SimpleSchedulerObjectDispatch.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace io {
template <>
struct TypeTraits<MockTestImageCtx> {
typedef ::MockSafeTimer SafeTimer;
};
template <>
struct FlushTracker<MockTestImageCtx> {
FlushTracker(MockTestImageCtx*) {
}
void shut_down() {
}
void flush(Context*) {
}
void start_io(uint64_t) {
}
void finish_io(uint64_t) {
}
};
} // namespace io
} // namespace librbd
#include "librbd/io/SimpleSchedulerObjectDispatch.cc"
namespace librbd {
namespace io {
using ::testing::_;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
struct TestMockIoSimpleSchedulerObjectDispatch : public TestMockFixture {
typedef SimpleSchedulerObjectDispatch<librbd::MockTestImageCtx> MockSimpleSchedulerObjectDispatch;
MockSafeTimer m_mock_timer;
ceph::mutex m_mock_timer_lock =
ceph::make_mutex("TestMockIoSimpleSchedulerObjectDispatch::Mutex");
TestMockIoSimpleSchedulerObjectDispatch() {
MockTestImageCtx::set_timer_instance(&m_mock_timer, &m_mock_timer_lock);
EXPECT_EQ(0, _rados.conf_set("rbd_io_scheduler_simple_max_delay", "1"));
}
void expect_get_object_name(MockTestImageCtx &mock_image_ctx,
uint64_t object_no) {
EXPECT_CALL(mock_image_ctx, get_object_name(object_no))
.WillRepeatedly(Return(
mock_image_ctx.image_ctx->get_object_name(object_no)));
}
void expect_dispatch_delayed_requests(MockTestImageCtx &mock_image_ctx,
int r) {
EXPECT_CALL(*mock_image_ctx.io_object_dispatcher, send(_))
.WillOnce(Invoke([&mock_image_ctx, r](ObjectDispatchSpec* spec) {
spec->dispatch_result = io::DISPATCH_RESULT_COMPLETE;
mock_image_ctx.image_ctx->op_work_queue->queue(
&spec->dispatcher_ctx, r);
}));
}
void expect_cancel_timer_task(Context *timer_task) {
EXPECT_CALL(m_mock_timer, cancel_event(timer_task))
.WillOnce(Invoke([](Context *timer_task) {
delete timer_task;
return true;
}));
}
void expect_add_timer_task(Context **timer_task) {
EXPECT_CALL(m_mock_timer, add_event_at(_, _))
.WillOnce(Invoke([timer_task](ceph::real_clock::time_point, Context *task) {
*timer_task = task;
return task;
}));
}
void expect_schedule_dispatch_delayed_requests(Context *current_task,
Context **new_task) {
if (current_task != nullptr) {
expect_cancel_timer_task(current_task);
}
if (new_task != nullptr) {
expect_add_timer_task(new_task);
}
}
void run_timer_task(Context *timer_task) {
std::lock_guard timer_locker{m_mock_timer_lock};
timer_task->complete(0);
}
};
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, Read) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
C_SaferCond cond;
Context *on_finish = &cond;
io::ReadExtents extents = {{0, 4096}, {8192, 4096}};
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.read(
0, &extents, mock_image_ctx.get_data_io_context(), 0, 0, {}, nullptr,
nullptr, nullptr, &on_finish, nullptr));
ASSERT_EQ(on_finish, &cond); // not modified
on_finish->complete(0);
ASSERT_EQ(0, cond.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, Discard) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
C_SaferCond cond;
Context *on_finish = &cond;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.discard(
0, 0, 4096, mock_image_ctx.get_data_io_context(), 0, {}, nullptr, nullptr,
nullptr, &on_finish, nullptr));
ASSERT_NE(on_finish, &cond);
on_finish->complete(0);
ASSERT_EQ(0, cond.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, Write) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
ceph::bufferlist data;
data.append("X");
int object_dispatch_flags = 0;
C_SaferCond cond;
Context *on_finish = &cond;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr,
&on_finish, nullptr));
ASSERT_NE(on_finish, &cond);
on_finish->complete(0);
ASSERT_EQ(0, cond.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, WriteSame) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
io::LightweightBufferExtents buffer_extents;
ceph::bufferlist data;
C_SaferCond cond;
Context *on_finish = &cond;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write_same(
0, 0, 4096, std::move(buffer_extents), std::move(data),
mock_image_ctx.get_data_io_context(), 0, {}, nullptr, nullptr, nullptr,
&on_finish, nullptr));
ASSERT_NE(on_finish, &cond);
on_finish->complete(0);
ASSERT_EQ(0, cond.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, CompareAndWrite) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
ceph::bufferlist cmp_data;
ceph::bufferlist write_data;
C_SaferCond cond;
Context *on_finish = &cond;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.compare_and_write(
0, 0, std::move(cmp_data), std::move(write_data),
mock_image_ctx.get_data_io_context(), 0, {}, nullptr, nullptr, nullptr,
nullptr, &on_finish, nullptr));
ASSERT_NE(on_finish, &cond);
on_finish->complete(0);
ASSERT_EQ(0, cond.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, Flush) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
io::DispatchResult dispatch_result;
C_SaferCond cond;
Context *on_finish = &cond;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.flush(
FLUSH_SOURCE_USER, {}, nullptr, &dispatch_result, &on_finish, nullptr));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_EQ(on_finish, &cond); // not modified
on_finish->complete(0);
ASSERT_EQ(0, cond.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, WriteDelayed) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
expect_get_object_name(mock_image_ctx, 0);
InSequence seq;
ceph::bufferlist data;
data.append("X");
int object_dispatch_flags = 0;
C_SaferCond cond1;
Context *on_finish1 = &cond1;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr, &on_finish1,
nullptr));
ASSERT_NE(on_finish1, &cond1);
Context *timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
io::DispatchResult dispatch_result;
C_SaferCond cond2;
Context *on_finish2 = &cond2;
C_SaferCond on_dispatched;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish2, &on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish2, &cond2);
ASSERT_NE(timer_task, nullptr);
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, nullptr);
on_finish1->complete(0);
ASSERT_EQ(0, cond1.wait());
ASSERT_EQ(0, on_dispatched.wait());
on_finish2->complete(0);
ASSERT_EQ(0, cond2.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, WriteDelayedFlush) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
expect_get_object_name(mock_image_ctx, 0);
InSequence seq;
ceph::bufferlist data;
data.append("X");
int object_dispatch_flags = 0;
C_SaferCond cond1;
Context *on_finish1 = &cond1;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr, &on_finish1,
nullptr));
ASSERT_NE(on_finish1, &cond1);
Context *timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
io::DispatchResult dispatch_result;
C_SaferCond cond2;
Context *on_finish2 = &cond2;
C_SaferCond on_dispatched;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish2, &on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish2, &cond2);
ASSERT_NE(timer_task, nullptr);
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, nullptr);
C_SaferCond cond3;
Context *on_finish3 = &cond3;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.flush(
FLUSH_SOURCE_USER, {}, nullptr, &dispatch_result, &on_finish3, nullptr));
ASSERT_EQ(io::DISPATCH_RESULT_CONTINUE, dispatch_result);
ASSERT_EQ(on_finish3, &cond3);
on_finish1->complete(0);
ASSERT_EQ(0, cond1.wait());
ASSERT_EQ(0, on_dispatched.wait());
on_finish2->complete(0);
ASSERT_EQ(0, cond2.wait());
on_finish3->complete(0);
ASSERT_EQ(0, cond3.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, WriteMerged) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
expect_get_object_name(mock_image_ctx, 0);
InSequence seq;
ceph::bufferlist data;
data.append("X");
int object_dispatch_flags = 0;
C_SaferCond cond1;
Context *on_finish1 = &cond1;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr, &on_finish1,
nullptr));
ASSERT_NE(on_finish1, &cond1);
Context *timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
uint64_t object_off = 20;
data.clear();
data.append(std::string(10, 'A'));
io::DispatchResult dispatch_result;
C_SaferCond cond2;
Context *on_finish2 = &cond2;
C_SaferCond on_dispatched2;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish2, &on_dispatched2));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish2, &cond2);
ASSERT_NE(timer_task, nullptr);
object_off = 0;
data.clear();
data.append(std::string(10, 'B'));
C_SaferCond cond3;
Context *on_finish3 = &cond3;
C_SaferCond on_dispatched3;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish3, &on_dispatched3));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish3, &cond3);
object_off = 10;
data.clear();
data.append(std::string(10, 'C'));
C_SaferCond cond4;
Context *on_finish4 = &cond4;
C_SaferCond on_dispatched4;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {},&object_dispatch_flags, nullptr, &dispatch_result,
&on_finish4, &on_dispatched4));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish4, &cond4);
object_off = 30;
data.clear();
data.append(std::string(10, 'D'));
C_SaferCond cond5;
Context *on_finish5 = &cond5;
C_SaferCond on_dispatched5;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish5, &on_dispatched5));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish5, &cond5);
object_off = 50;
data.clear();
data.append(std::string(10, 'E'));
C_SaferCond cond6;
Context *on_finish6 = &cond6;
C_SaferCond on_dispatched6;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish6, &on_dispatched6));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish6, &cond6);
// expect two requests dispatched:
// 0~40 (merged 0~10, 10~10, 20~10, 30~10) and 50~10
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, nullptr);
on_finish1->complete(0);
ASSERT_EQ(0, cond1.wait());
ASSERT_EQ(0, on_dispatched2.wait());
ASSERT_EQ(0, on_dispatched3.wait());
ASSERT_EQ(0, on_dispatched4.wait());
ASSERT_EQ(0, on_dispatched5.wait());
ASSERT_EQ(0, on_dispatched6.wait());
on_finish2->complete(0);
on_finish3->complete(0);
on_finish4->complete(0);
on_finish5->complete(0);
on_finish6->complete(0);
ASSERT_EQ(0, cond2.wait());
ASSERT_EQ(0, cond3.wait());
ASSERT_EQ(0, cond4.wait());
ASSERT_EQ(0, cond5.wait());
ASSERT_EQ(0, cond6.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, WriteNonSequential) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
expect_get_object_name(mock_image_ctx, 0);
InSequence seq;
ceph::bufferlist data;
int object_dispatch_flags = 0;
C_SaferCond cond1;
Context *on_finish1 = &cond1;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr, &on_finish1,
nullptr));
ASSERT_NE(on_finish1, &cond1);
Context *timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
uint64_t object_off = 0;
data.clear();
data.append(std::string(10, 'X'));
io::DispatchResult dispatch_result;
C_SaferCond cond2;
Context *on_finish2 = &cond2;
C_SaferCond on_dispatched2;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish2, &on_dispatched2));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish2, &cond2);
ASSERT_NE(timer_task, nullptr);
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, nullptr);
object_off = 5;
data.clear();
data.append(std::string(10, 'Y'));
C_SaferCond cond3;
Context *on_finish3 = &cond3;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish3, nullptr));
ASSERT_NE(on_finish3, &cond3);
on_finish1->complete(0);
ASSERT_EQ(0, cond1.wait());
ASSERT_EQ(0, on_dispatched2.wait());
on_finish2->complete(0);
ASSERT_EQ(0, cond2.wait());
on_finish3->complete(0);
ASSERT_EQ(0, cond3.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, Mixed) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
expect_get_object_name(mock_image_ctx, 0);
InSequence seq;
// write (1) 0~0 (in-flight)
// will wrap on_finish with dispatch_seq=1 to dispatch future delayed writes
ceph::bufferlist data;
int object_dispatch_flags = 0;
C_SaferCond cond1;
Context *on_finish1 = &cond1;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr, &on_finish1,
nullptr));
ASSERT_NE(on_finish1, &cond1);
// write (2) 0~10 (delayed)
// will wait for write (1) to finish or a non-seq io comes
Context *timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
uint64_t object_off = 0;
data.clear();
data.append(std::string(10, 'A'));
io::DispatchResult dispatch_result;
C_SaferCond cond2;
Context *on_finish2 = &cond2;
C_SaferCond on_dispatched2;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish2, &on_dispatched2));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish2, &cond2);
ASSERT_NE(timer_task, nullptr);
// write (3) 10~10 (delayed)
// will be merged with write (2)
object_off = 10;
data.clear();
data.append(std::string(10, 'B'));
C_SaferCond cond3;
Context *on_finish3 = &cond3;
C_SaferCond on_dispatched3;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish3, &on_dispatched3));
ASSERT_NE(on_finish3, &cond3);
// discard (1) (non-seq io)
// will dispatch the delayed writes (2) and (3) and wrap on_finish
// with dispatch_seq=2 to dispatch future delayed writes
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, nullptr);
C_SaferCond cond4;
Context *on_finish4 = &cond4;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.discard(
0, 4096, 4096, mock_image_ctx.get_data_io_context(), 0, {}, nullptr,
nullptr, nullptr, &on_finish4, nullptr));
ASSERT_NE(on_finish4, &cond4);
ASSERT_EQ(0, on_dispatched2.wait());
ASSERT_EQ(0, on_dispatched3.wait());
// write (4) 20~10 (delayed)
// will wait for discard (1) to finish or a non-seq io comes
timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
object_off = 20;
data.clear();
data.append(std::string(10, 'C'));
C_SaferCond cond5;
Context *on_finish5 = &cond5;
C_SaferCond on_dispatched5;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish5, &on_dispatched5));
ASSERT_NE(on_finish5, &cond5);
ASSERT_NE(timer_task, nullptr);
// discard (2) (non-seq io)
// will dispatch the delayed write (4) and wrap on_finish with dispatch_seq=3
// to dispatch future delayed writes
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, nullptr);
C_SaferCond cond6;
Context *on_finish6 = &cond6;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.discard(
0, 4096, 4096, mock_image_ctx.get_data_io_context(), 0, {}, nullptr,
nullptr, nullptr, &on_finish6, nullptr));
ASSERT_NE(on_finish6, &cond6);
ASSERT_EQ(0, on_dispatched5.wait());
// write (5) 30~10 (delayed)
// will wait for discard (2) to finish or a non-seq io comes
timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
object_off = 30;
data.clear();
data.append(std::string(10, 'D'));
C_SaferCond cond7;
Context *on_finish7 = &cond7;
C_SaferCond on_dispatched7;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, object_off, std::move(data), mock_image_ctx.get_data_io_context(), 0,
0, std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish7, &on_dispatched7));
ASSERT_NE(on_finish7, &cond7);
ASSERT_NE(timer_task, nullptr);
// write (1) finishes
// on_finish wrapper will skip dispatch delayed write (5)
// due to dispatch_seq(1) < m_dispatch_seq(3)
on_finish1->complete(0);
ASSERT_EQ(0, cond1.wait());
// writes (2) and (3) finish ("dispatch delayed" is not called)
on_finish2->complete(0);
on_finish3->complete(0);
ASSERT_EQ(0, cond2.wait());
ASSERT_EQ(0, cond3.wait());
// discard (1) finishes
// on_finish wrapper will skip dispatch delayed write (5)
// due to dispatch_seq(2) < m_dispatch_seq(3)
on_finish4->complete(0);
ASSERT_EQ(0, cond4.wait());
// writes (4) finishes ("dispatch delayed" is not called)
on_finish5->complete(0);
ASSERT_EQ(0, cond5.wait());
// discard (2) finishes
// on_finish wrapper will dispatch the delayed write (5)
// due to dispatch_seq(3) == m_dispatch_seq(3)
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, nullptr);
on_finish6->complete(0);
ASSERT_EQ(0, cond6.wait());
ASSERT_EQ(0, on_dispatched7.wait());
// write (5) finishes ("dispatch delayed" is not called)
on_finish7->complete(0);
ASSERT_EQ(0, cond7.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, DispatchQueue) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
expect_get_object_name(mock_image_ctx, 0);
expect_get_object_name(mock_image_ctx, 1);
InSequence seq;
// send 2 writes to object 0
uint64_t object_no = 0;
ceph::bufferlist data;
int object_dispatch_flags = 0;
C_SaferCond cond1;
Context *on_finish1 = &cond1;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
object_no, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr, &on_finish1,
nullptr));
ASSERT_NE(on_finish1, &cond1);
Context *timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
data.clear();
io::DispatchResult dispatch_result;
C_SaferCond cond2;
Context *on_finish2 = &cond2;
C_SaferCond on_dispatched2;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
object_no, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish2, &on_dispatched2));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish2, &cond2);
ASSERT_NE(timer_task, nullptr);
// send 2 writes to object 1
object_no = 1;
data.clear();
C_SaferCond cond3;
Context *on_finish3 = &cond3;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
object_no, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr, &on_finish3,
nullptr));
ASSERT_NE(on_finish3, &cond3);
data.clear();
C_SaferCond cond4;
Context *on_finish4 = &cond4;
C_SaferCond on_dispatched4;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
object_no, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish4, &on_dispatched4));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish4, &cond4);
// finish write (1) to object 0
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, &timer_task);
on_finish1->complete(0);
ASSERT_EQ(0, cond1.wait());
ASSERT_EQ(0, on_dispatched2.wait());
// finish write (2) to object 0
on_finish2->complete(0);
ASSERT_EQ(0, cond2.wait());
// finish write (1) to object 1
expect_dispatch_delayed_requests(mock_image_ctx, 0);
expect_schedule_dispatch_delayed_requests(timer_task, nullptr);
on_finish3->complete(0);
ASSERT_EQ(0, cond3.wait());
ASSERT_EQ(0, on_dispatched4.wait());
// finish write (2) to object 1
on_finish4->complete(0);
ASSERT_EQ(0, cond4.wait());
}
TEST_F(TestMockIoSimpleSchedulerObjectDispatch, Timer) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockTestImageCtx mock_image_ctx(*ictx);
MockSimpleSchedulerObjectDispatch
mock_simple_scheduler_object_dispatch(&mock_image_ctx);
expect_get_object_name(mock_image_ctx, 0);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
ceph::bufferlist data;
int object_dispatch_flags = 0;
C_SaferCond cond1;
Context *on_finish1 = &cond1;
ASSERT_FALSE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, nullptr, &on_finish1,
nullptr));
ASSERT_NE(on_finish1, &cond1);
Context *timer_task = nullptr;
expect_schedule_dispatch_delayed_requests(nullptr, &timer_task);
data.clear();
io::DispatchResult dispatch_result;
C_SaferCond cond2;
Context *on_finish2 = &cond2;
C_SaferCond on_dispatched;
ASSERT_TRUE(mock_simple_scheduler_object_dispatch.write(
0, 0, std::move(data), mock_image_ctx.get_data_io_context(), 0, 0,
std::nullopt, {}, &object_dispatch_flags, nullptr, &dispatch_result,
&on_finish2, &on_dispatched));
ASSERT_EQ(dispatch_result, io::DISPATCH_RESULT_COMPLETE);
ASSERT_NE(on_finish2, &cond2);
ASSERT_NE(timer_task, nullptr);
expect_dispatch_delayed_requests(mock_image_ctx, 0);
run_timer_task(timer_task);
ASSERT_EQ(0, on_dispatched.wait());
on_finish1->complete(0);
ASSERT_EQ(0, cond1.wait());
on_finish2->complete(0);
ASSERT_EQ(0, cond2.wait());
}
} // namespace io
} // namespace librbd
| 27,984 | 32.962379 | 100 |
cc
|
null |
ceph-main/src/test/librbd/journal/test_Entries.cc
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "librbd/internal.h"
#include "librbd/Journal.h"
#include "librbd/api/Io.h"
#include "librbd/io/AioCompletion.h"
#include "librbd/journal/Types.h"
#include "journal/Journaler.h"
#include "journal/ReplayEntry.h"
#include "journal/ReplayHandler.h"
#include "journal/Settings.h"
#include <list>
#include <boost/variant.hpp>
void register_test_journal_entries() {
}
namespace librbd {
namespace journal {
class TestJournalEntries : public TestFixture {
public:
typedef std::list<::journal::Journaler *> Journalers;
struct ReplayHandler : public ::journal::ReplayHandler {
ceph::mutex lock = ceph::make_mutex("ReplayHandler::lock");
ceph::condition_variable cond;
bool entries_available;
bool complete;
ReplayHandler()
: entries_available(false), complete(false) {
}
void handle_entries_available() override {
std::lock_guard locker{lock};
entries_available = true;
cond.notify_all();
}
void handle_complete(int r) override {
std::lock_guard locker{lock};
complete = true;
cond.notify_all();
}
};
ReplayHandler m_replay_handler;
Journalers m_journalers;
void TearDown() override {
for (Journalers::iterator it = m_journalers.begin();
it != m_journalers.end(); ++it) {
::journal::Journaler *journaler = *it;
journaler->stop_replay();
journaler->shut_down();
delete journaler;
}
TestFixture::TearDown();
}
::journal::Journaler *create_journaler(librbd::ImageCtx *ictx) {
::journal::Journaler *journaler = new ::journal::Journaler(
ictx->md_ctx, ictx->id, "dummy client", {}, nullptr);
int r = journaler->register_client(bufferlist());
if (r < 0) {
ADD_FAILURE() << "failed to register journal client";
delete journaler;
return NULL;
}
C_SaferCond cond;
journaler->init(&cond);
r = cond.wait();
if (r < 0) {
ADD_FAILURE() << "failed to initialize journal client";
delete journaler;
return NULL;
}
journaler->start_live_replay(&m_replay_handler, 0.1);
m_journalers.push_back(journaler);
return journaler;
}
bool wait_for_entries_available(librbd::ImageCtx *ictx) {
using namespace std::chrono_literals;
std::unique_lock locker{m_replay_handler.lock};
while (!m_replay_handler.entries_available) {
if (m_replay_handler.cond.wait_for(locker, 10s) == std::cv_status::timeout) {
return false;
}
}
m_replay_handler.entries_available = false;
return true;
}
bool get_event_entry(const ::journal::ReplayEntry &replay_entry,
librbd::journal::EventEntry *event_entry) {
try {
bufferlist data_bl = replay_entry.get_data();
auto it = data_bl.cbegin();
decode(*event_entry, it);
} catch (const buffer::error &err) {
return false;
}
return true;
}
};
TEST_F(TestJournalEntries, AioWrite) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
::journal::Journaler *journaler = create_journaler(ictx);
ASSERT_TRUE(journaler != NULL);
std::string buffer(512, '1');
bufferlist write_bl;
write_bl.append(buffer);
C_SaferCond cond_ctx;
auto c = librbd::io::AioCompletion::create(&cond_ctx);
c->get();
api::Io<>::aio_write(*ictx, c, 123, buffer.size(), std::move(write_bl), 0,
true);
ASSERT_EQ(0, c->wait_for_complete());
c->put();
ASSERT_TRUE(wait_for_entries_available(ictx));
::journal::ReplayEntry replay_entry;
ASSERT_TRUE(journaler->try_pop_front(&replay_entry));
librbd::journal::EventEntry event_entry;
ASSERT_TRUE(get_event_entry(replay_entry, &event_entry));
ASSERT_EQ(librbd::journal::EVENT_TYPE_AIO_WRITE,
event_entry.get_event_type());
librbd::journal::AioWriteEvent aio_write_event =
boost::get<librbd::journal::AioWriteEvent>(event_entry.event);
ASSERT_EQ(123U, aio_write_event.offset);
ASSERT_EQ(buffer.size(), aio_write_event.length);
bufferlist buffer_bl;
buffer_bl.append(buffer);
ASSERT_TRUE(aio_write_event.data.contents_equal(buffer_bl));
ASSERT_EQ(librbd::journal::AioWriteEvent::get_fixed_size() +
aio_write_event.data.length(), replay_entry.get_data().length());
}
TEST_F(TestJournalEntries, AioDiscard) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
CephContext* cct = reinterpret_cast<CephContext*>(_rados.cct());
REQUIRE(!cct->_conf.get_val<bool>("rbd_skip_partial_discard"));
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
::journal::Journaler *journaler = create_journaler(ictx);
ASSERT_TRUE(journaler != NULL);
C_SaferCond cond_ctx;
auto c = librbd::io::AioCompletion::create(&cond_ctx);
c->get();
api::Io<>::aio_discard(*ictx, c, 123, 234, ictx->discard_granularity_bytes,
true);
ASSERT_EQ(0, c->wait_for_complete());
c->put();
ASSERT_TRUE(wait_for_entries_available(ictx));
::journal::ReplayEntry replay_entry;
ASSERT_TRUE(journaler->try_pop_front(&replay_entry));
librbd::journal::EventEntry event_entry;
ASSERT_TRUE(get_event_entry(replay_entry, &event_entry));
ASSERT_EQ(librbd::journal::EVENT_TYPE_AIO_DISCARD,
event_entry.get_event_type());
librbd::journal::AioDiscardEvent aio_discard_event =
boost::get<librbd::journal::AioDiscardEvent>(event_entry.event);
ASSERT_EQ(123U, aio_discard_event.offset);
ASSERT_EQ(234U, aio_discard_event.length);
}
TEST_F(TestJournalEntries, AioFlush) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
::journal::Journaler *journaler = create_journaler(ictx);
ASSERT_TRUE(journaler != NULL);
C_SaferCond cond_ctx;
auto c = librbd::io::AioCompletion::create(&cond_ctx);
c->get();
api::Io<>::aio_flush(*ictx, c, true);
ASSERT_EQ(0, c->wait_for_complete());
c->put();
ASSERT_TRUE(wait_for_entries_available(ictx));
::journal::ReplayEntry replay_entry;
ASSERT_TRUE(journaler->try_pop_front(&replay_entry));
librbd::journal::EventEntry event_entry;
ASSERT_TRUE(get_event_entry(replay_entry, &event_entry));
ASSERT_EQ(librbd::journal::EVENT_TYPE_AIO_FLUSH,
event_entry.get_event_type());
}
} // namespace journal
} // namespace librbd
| 6,527 | 27.50655 | 83 |
cc
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.