Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom move operations for ObjectStore::Transaction #7303

Merged
merged 5 commits into from Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
118 changes: 90 additions & 28 deletions src/os/ObjectStore.h
Expand Up @@ -431,6 +431,36 @@ class ObjectStore {
largest_data_off_in_tbl(0),
fadvise_flags(0) { }

// override default move operations to reset default values
TransactionData(TransactionData&& other) :
ops(other.ops),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, do we need explicate std::move? otherwise it should be a copy construct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore this. I'm looking in the wrong

largest_data_len(other.largest_data_len),
largest_data_off(other.largest_data_off),
largest_data_off_in_tbl(other.largest_data_off_in_tbl),
fadvise_flags(other.fadvise_flags) {
other.ops = 0;
other.largest_data_len = 0;
other.largest_data_off = 0;
other.largest_data_off_in_tbl = 0;
other.fadvise_flags = 0;
}
TransactionData& operator=(TransactionData&& other) {
ops = other.ops;
largest_data_len = other.largest_data_len;
largest_data_off = other.largest_data_off;
largest_data_off_in_tbl = other.largest_data_off_in_tbl;
fadvise_flags = other.fadvise_flags;
other.ops = 0;
other.largest_data_len = 0;
other.largest_data_off = 0;
other.largest_data_off_in_tbl = 0;
other.fadvise_flags = 0;
return *this;
}

TransactionData(const TransactionData& other) = default;
TransactionData& operator=(const TransactionData& other) = default;

void encode(bufferlist& bl) const {
bl.append((char*)this, sizeof(TransactionData));
}
Expand All @@ -442,16 +472,16 @@ class ObjectStore {
private:
TransactionData data;

void *osr; // NULL on replay
void *osr {nullptr}; // NULL on replay

bool use_tbl; //use_tbl for encode/decode
bool use_tbl {false}; //use_tbl for encode/decode
bufferlist tbl;

map<coll_t, __le32> coll_index;
map<ghobject_t, __le32, ghobject_t::BitwiseComparator> object_index;

__le32 coll_id;
__le32 object_id;
__le32 coll_id {0};
__le32 object_id {0};

bufferlist data_bl;
bufferlist op_bl;
Expand All @@ -463,6 +493,62 @@ class ObjectStore {
list<Context *> on_applied_sync;

public:
Transaction() = default;

Transaction(bufferlist::iterator &dp) {
decode(dp);
}
Transaction(bufferlist &nbl) {
bufferlist::iterator dp = nbl.begin();
decode(dp);
}

// override default move operations to reset default values
Transaction(Transaction&& other) :
data(std::move(other.data)),
osr(other.osr),
use_tbl(other.use_tbl),
tbl(std::move(other.tbl)),
coll_index(std::move(other.coll_index)),
object_index(std::move(other.object_index)),
coll_id(other.coll_id),
object_id(other.object_id),
data_bl(std::move(other.data_bl)),
op_bl(std::move(other.op_bl)),
op_ptr(std::move(other.op_ptr)),
on_applied(std::move(other.on_applied)),
on_commit(std::move(other.on_commit)),
on_applied_sync(std::move(other.on_applied_sync)) {
other.osr = nullptr;
other.use_tbl = false;
other.coll_id = 0;
other.object_id = 0;
}

Transaction& operator=(Transaction&& other) {
data = std::move(other.data);
osr = other.osr;
use_tbl = other.use_tbl;
tbl = std::move(other.tbl);
coll_index = std::move(other.coll_index);
object_index = std::move(other.object_index);
coll_id = other.coll_id;
object_id = other.object_id;
data_bl = std::move(other.data_bl);
op_bl = std::move(other.op_bl);
op_ptr = std::move(other.op_ptr);
on_applied = std::move(other.on_applied);
on_commit = std::move(other.on_commit);
on_applied_sync = std::move(other.on_applied_sync);
other.osr = nullptr;
other.use_tbl = false;
other.coll_id = 0;
other.object_id = 0;
return *this;
}

Transaction(const Transaction& other) = default;
Transaction& operator=(const Transaction& other) = default;

/* Operations on callback contexts */
void register_on_applied(Context *c) {
Expand Down Expand Up @@ -1588,30 +1674,6 @@ class ObjectStore {
data.ops++;
}

// etc.
Transaction() :
osr(NULL),
use_tbl(false),
coll_id(0),
object_id(0) { }

Transaction(bufferlist::iterator &dp) :
osr(NULL),
use_tbl(false),
coll_id(0),
object_id(0) {
decode(dp);
}

Transaction(bufferlist &nbl) :
osr(NULL),
use_tbl(false),
coll_id(0),
object_id(0) {
bufferlist::iterator dp = nbl.begin();
decode(dp);
}

void encode(bufferlist& bl) const {
if (use_tbl) {
uint64_t ops = data.ops;
Expand Down
6 changes: 6 additions & 0 deletions src/test/CMakeLists.txt
Expand Up @@ -2160,6 +2160,12 @@ target_link_libraries(test_objectstore_workloadgen
${CMAKE_DL_LIBS}
)

# test_transaction
add_executable(unittest_transaction objectstore/test_transaction.cc)
add_test(unittest_transaction unittest_transaction)
set_target_properties(unittest_transaction PROPERTIES COMPILE_FLAGS ${UNITTEST_CXX_FLAGS})
target_link_libraries(unittest_transaction os common ${UNITTEST_LIBS})

#test_filestore
add_executable(test_filestore filestore/TestFileStore.cc)
set_target_properties(test_filestore PROPERTIES COMPILE_FLAGS
Expand Down
5 changes: 5 additions & 0 deletions src/test/Makefile-server.am
Expand Up @@ -94,6 +94,11 @@ ceph_test_filestore_idempotent_sequence_SOURCES = \
ceph_test_filestore_idempotent_sequence_LDADD = $(LIBOS) $(CEPH_GLOBAL)
bin_DEBUGPROGRAMS += ceph_test_filestore_idempotent_sequence

unittest_transaction_SOURCES = test/objectstore/test_transaction.cc
unittest_transaction_LDADD = $(LIBOS) $(UNITTEST_LDADD) $(CEPH_GLOBAL)
unittest_transaction_CXXFLAGS = $(UNITTEST_CXXFLAGS)
check_TESTPROGRAMS += unittest_transaction

ceph_xattr_bench_SOURCES = test/xattr_bench.cc
ceph_xattr_bench_LDADD = $(LIBOS) $(UNITTEST_LDADD) $(CEPH_GLOBAL)
ceph_xattr_bench_CXXFLAGS = $(UNITTEST_CXXFLAGS)
Expand Down
75 changes: 75 additions & 0 deletions src/test/objectstore/test_transaction.cc
@@ -0,0 +1,75 @@
// -*- 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 Casey Bodley <cbodley@redhat.com>
*
* 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 "os/ObjectStore.h"
#include <gtest/gtest.h>

TEST(Transaction, MoveConstruct)
{
auto a = ObjectStore::Transaction{};
a.nop();
ASSERT_FALSE(a.empty());

// move-construct in b
auto b = std::move(a);
ASSERT_TRUE(a.empty());
ASSERT_FALSE(b.empty());
}

TEST(Transaction, MoveAssign)
{
auto a = ObjectStore::Transaction{};
a.nop();
ASSERT_FALSE(a.empty());

auto b = ObjectStore::Transaction{};
b = std::move(a); // move-assign to b
ASSERT_TRUE(a.empty());
ASSERT_FALSE(b.empty());
}

TEST(Transaction, CopyConstruct)
{
auto a = ObjectStore::Transaction{};
a.nop();
ASSERT_FALSE(a.empty());

auto b = a; // copy-construct in b
ASSERT_FALSE(a.empty());
ASSERT_FALSE(b.empty());
}

TEST(Transaction, CopyAssign)
{
auto a = ObjectStore::Transaction{};
a.nop();
ASSERT_FALSE(a.empty());

auto b = ObjectStore::Transaction{};
b = a; // copy-assign to b
ASSERT_FALSE(a.empty());
ASSERT_FALSE(b.empty());
}

TEST(Transaction, Swap)
{
auto a = ObjectStore::Transaction{};
a.nop();
ASSERT_FALSE(a.empty());

auto b = ObjectStore::Transaction{};
std::swap(a, b); // swap a and b
ASSERT_TRUE(a.empty());
ASSERT_FALSE(b.empty());
}