summaryrefslogtreecommitdiffstats
path: root/src/test/journal/test_JournalRecorder.cc
blob: 466ee2741286b29b7f3eb41681a6e51f5d6c00b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "journal/JournalRecorder.h"
#include "journal/Entry.h"
#include "journal/JournalMetadata.h"
#include "test/journal/RadosTestFixture.h"
#include <limits>
#include <list>
#include <memory>

class TestJournalRecorder : public RadosTestFixture {
public:
  using JournalRecorderPtr = std::unique_ptr<journal::JournalRecorder,
					     std::function<void(journal::JournalRecorder*)>>;
  JournalRecorderPtr create_recorder(
      const std::string &oid, const ceph::ref_t<journal::JournalMetadata>& metadata) {
    JournalRecorderPtr recorder{
      new journal::JournalRecorder(m_ioctx, oid + ".", metadata, 0),
      [](journal::JournalRecorder* recorder) {
	C_SaferCond cond;
	recorder->shut_down(&cond);
	cond.wait();
	delete recorder;
      }
    };
    recorder->set_append_batch_options(0, std::numeric_limits<uint32_t>::max(), 0);
    return recorder;
  }
};

TEST_F(TestJournalRecorder, Append) {
  std::string oid = get_temp_oid();
  ASSERT_EQ(0, create(oid, 12, 2));
  ASSERT_EQ(0, client_register(oid));

  auto metadata = create_metadata(oid);
  ASSERT_EQ(0, init_metadata(metadata));

  JournalRecorderPtr recorder = create_recorder(oid, metadata);

  journal::Future future1 = recorder->append(123, create_payload("payload"));

  C_SaferCond cond;
  future1.flush(&cond);
  ASSERT_EQ(0, cond.wait());
}

TEST_F(TestJournalRecorder, AppendKnownOverflow) {
  std::string oid = get_temp_oid();
  ASSERT_EQ(0, create(oid, 12, 2));
  ASSERT_EQ(0, client_register(oid));

  auto metadata = create_metadata(oid);
  ASSERT_EQ(0, init_metadata(metadata));
  ASSERT_EQ(0U, metadata->get_active_set());

  JournalRecorderPtr recorder = create_recorder(oid, metadata);

  recorder->append(123, create_payload(std::string(metadata->get_object_size() -
                                                   journal::Entry::get_fixed_size(), '1')));
  journal::Future future2 = recorder->append(123, create_payload(std::string(1, '2')));

  C_SaferCond cond;
  future2.flush(&cond);
  ASSERT_EQ(0, cond.wait());

  ASSERT_EQ(1U, metadata->get_active_set());
}

TEST_F(TestJournalRecorder, AppendDelayedOverflow) {
  std::string oid = get_temp_oid();
  ASSERT_EQ(0, create(oid, 12, 2));
  ASSERT_EQ(0, client_register(oid));

  auto metadata = create_metadata(oid);
  ASSERT_EQ(0, init_metadata(metadata));
  ASSERT_EQ(0U, metadata->get_active_set());

  JournalRecorderPtr recorder1 = create_recorder(oid, metadata);
  JournalRecorderPtr recorder2 = create_recorder(oid, metadata);

  recorder1->append(234, create_payload(std::string(1, '1')));
  recorder2->append(123, create_payload(std::string(metadata->get_object_size() -
                                                    journal::Entry::get_fixed_size(), '2')));

  journal::Future future = recorder2->append(123, create_payload(std::string(1, '3')));

  C_SaferCond cond;
  future.flush(&cond);
  ASSERT_EQ(0, cond.wait());

  ASSERT_EQ(1U, metadata->get_active_set());
}

TEST_F(TestJournalRecorder, FutureFlush) {
  std::string oid = get_temp_oid();
  ASSERT_EQ(0, create(oid, 12, 2));
  ASSERT_EQ(0, client_register(oid));

  auto metadata = create_metadata(oid);
  ASSERT_EQ(0, init_metadata(metadata));

  JournalRecorderPtr recorder = create_recorder(oid, metadata);

  journal::Future future1 = recorder->append(123, create_payload("payload1"));
  journal::Future future2 = recorder->append(123, create_payload("payload2"));

  C_SaferCond cond;
  future2.flush(&cond);
  ASSERT_EQ(0, cond.wait());
  ASSERT_TRUE(future1.is_complete());
  ASSERT_TRUE(future2.is_complete());
}

TEST_F(TestJournalRecorder, Flush) {
  std::string oid = get_temp_oid();
  ASSERT_EQ(0, create(oid, 12, 2));
  ASSERT_EQ(0, client_register(oid));

  auto metadata = create_metadata(oid);
  ASSERT_EQ(0, init_metadata(metadata));

  JournalRecorderPtr recorder = create_recorder(oid, metadata);

  journal::Future future1 = recorder->append(123, create_payload("payload1"));
  journal::Future future2 = recorder->append(123, create_payload("payload2"));

  C_SaferCond cond1;
  recorder->flush(&cond1);
  ASSERT_EQ(0, cond1.wait());

  C_SaferCond cond2;
  future2.wait(&cond2);
  ASSERT_EQ(0, cond2.wait());
  ASSERT_TRUE(future1.is_complete());
  ASSERT_TRUE(future2.is_complete());
}

TEST_F(TestJournalRecorder, OverflowCommitObjectNumber) {
  std::string oid = get_temp_oid();
  ASSERT_EQ(0, create(oid, 12, 2));
  ASSERT_EQ(0, client_register(oid));

  auto metadata = create_metadata(oid);
  ASSERT_EQ(0, init_metadata(metadata));
  ASSERT_EQ(0U, metadata->get_active_set());

  JournalRecorderPtr recorder = create_recorder(oid, metadata);

  recorder->append(123, create_payload(std::string(metadata->get_object_size() -
                                                   journal::Entry::get_fixed_size(), '1')));
  journal::Future future2 = recorder->append(124, create_payload(std::string(1, '2')));

  C_SaferCond cond;
  future2.flush(&cond);
  ASSERT_EQ(0, cond.wait());

  ASSERT_EQ(1U, metadata->get_active_set());

  uint64_t object_num;
  uint64_t tag_tid;
  uint64_t entry_tid;
  metadata->get_commit_entry(1, &object_num, &tag_tid, &entry_tid);
  ASSERT_EQ(0U, object_num);
  ASSERT_EQ(123U, tag_tid);
  ASSERT_EQ(0U, entry_tid);

  metadata->get_commit_entry(2, &object_num, &tag_tid, &entry_tid);
  ASSERT_EQ(2U, object_num);
  ASSERT_EQ(124U, tag_tid);
  ASSERT_EQ(0U, entry_tid);
}