Motr  M0
fol.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "be/tx.h"
24 #include "fol/fol.h"
25 #include "fol/fol_private.h"
26 #include "fol/fol_xc.h"
27 #include "ut/be.h"
28 /* XXX FIXME: Do not use ut/ directory of other subsystem. */
29 #include "be/ut/helper.h" /* m0_be_ut_backend */
30 
31 #include "fid/fid_xc.h"
32 #include "rpc/rpc_opcodes.h"
33 #include "lib/memory.h"
34 #include "lib/misc.h" /* M0_SET0 */
35 
36 #include "ut/ut.h"
37 #include "lib/ub.h"
38 
39 static struct m0_fol g_fol;
40 static struct m0_fol_rec g_rec;
41 static struct m0_be_ut_backend g_ut_be;
42 static struct m0_be_ut_seg g_ut_seg;
43 static struct m0_be_tx g_tx;
44 
45 static int verify_frag_data(struct m0_fol_frag *frag,
46  struct m0_be_tx *tx);
48  NULL, NULL);
49 
50 static void test_init(void)
51 {
52  M0_SET0(&g_fol);
53  M0_SET0(&g_rec);
54  M0_SET0(&g_ut_be);
55  M0_SET0(&g_ut_seg);
56  M0_SET0(&g_tx);
57 
59 
63 }
64 
65 static void test_fini(void)
66 {
70 }
71 
72 static void test_fol_frag_type_reg(void)
73 {
74  int rc;
75 
76  ut_frag_type = M0_FOL_FRAG_TYPE_XC_OPS("UT record frag", m0_fid_xc,
77  &ut_frag_type_ops);
78  rc = m0_fol_frag_type_register(&ut_frag_type);
79  M0_ASSERT(rc == 0);
80  M0_ASSERT(ut_frag_type.rpt_index > 0);
81 }
82 
83 static void test_fol_frag_type_unreg(void)
84 {
85  m0_fol_frag_type_deregister(&ut_frag_type);
86  M0_ASSERT(ut_frag_type.rpt_ops == NULL);
87  M0_ASSERT(ut_frag_type.rpt_xt == NULL);
88  M0_ASSERT(ut_frag_type.rpt_index == 0);
89 }
90 
91 static int verify_frag_data(struct m0_fol_frag *frag,
92  struct m0_be_tx *_)
93 {
94  struct m0_fid *dec_rec;
95 
96  dec_rec = frag->rp_data;
97  M0_UT_ASSERT(dec_rec->f_container == 22);
98  M0_UT_ASSERT(dec_rec->f_key == 33);
99  return 0;
100 }
101 
102 static void test_fol_frag_encdec(void)
103 {
104  struct m0_fid *rec;
105  struct m0_fol_rec dec_rec;
106  struct m0_fol_frag *dec_frag;
107  struct m0_fol_frag ut_rec_frag = {};
108  struct m0_buf *buf = &g_tx.t_payload;
109  int rc;
110 
112 
113  M0_ALLOC_PTR(rec);
114  M0_UT_ASSERT(rec != NULL);
115  *rec = (struct m0_fid){ .f_container = 22, .f_key = 33 };
116 
117  m0_fol_frag_init(&ut_rec_frag, rec, &ut_frag_type);
118  m0_fol_frag_add(&g_rec, &ut_rec_frag);
119 
120  /* Note, this function sets actual tx payload size for the buf. */
122  M0_UT_ASSERT(rc == 0);
123 
125 
126  m0_fol_rec_init(&dec_rec, &g_fol);
127  rc = m0_fol_rec_decode(&dec_rec, buf);
128  M0_UT_ASSERT(rc == 0);
129 
130  m0_tl_for(m0_rec_frag, &dec_rec.fr_frags, dec_frag) {
131  /* Call verify_part_data() for each part. */
132  dec_frag->rp_ops->rpo_undo(dec_frag, &g_tx);
133  } m0_tl_endfor;
134  m0_fol_rec_fini(&dec_rec);
135 }
136 
137 struct m0_ut_suite fol_ut = {
138  .ts_name = "fol-ut",
139  .ts_init = NULL,
140  .ts_fini = NULL,
141  .ts_tests = {
142  /*
143  * Note, that there are dependencies between these tests.
144  * Do not reorder them willy-nilly.
145  */
146  { "fol-init", test_init },
147  { "fol-frag-type-reg", test_fol_frag_type_reg },
148  { "fol-frag-test", test_fol_frag_encdec },
149  { "fol-frag-type-unreg", test_fol_frag_type_unreg },
150  { "fol-fini", test_fini },
151  { NULL, NULL }
152  }
153 };
154 
155 /* ------------------------------------------------------------------
156  * UB
157  * ------------------------------------------------------------------ */
158 
159 enum { UB_ITER = 100000 };
160 
161 static int ub_init(const char *opts M0_UNUSED)
162 {
163  test_init();
165  return 0;
166 }
167 
168 static void ub_fini(void)
169 {
171  test_fini();
172 }
173 
174 #if 0 /* Convert to BE. */
175 static m0_lsn_t last;
176 
177 static void checkpoint()
178 {
179  rc = m0_db_tx_commit(&tx);
180  M0_ASSERT(rc == 0);
181 
182  rc = m0_db_tx_init(&tx, &db, 0);
183  M0_ASSERT(rc == 0);
184 }
185 
186 static void ub_insert(int i)
187 {
188  d->rd_lsn = m0_fol_lsn_allocate(&fol);
189  rc = m0_fol_rec_encode(&fol, &tx, &r);
190  M0_ASSERT(rc == 0);
191  last = d->rd_lsn;
192  if (i % 1000 == 0)
193  checkpoint();
194 }
195 
196 static void ub_lookup(int i)
197 {
198  m0_lsn_t lsn;
199  struct m0_fol_rec rec;
200 
201  lsn = last - i;
202 
203  rc = m0_fol_rec_lookup(&fol, &tx, lsn, &rec);
204  M0_ASSERT(rc == 0);
205  m0_fol_lookup_rec_fini(&rec);
206  if (i % 1000 == 0)
207  checkpoint();
208 }
209 
210 static void ub_insert_buf(int i)
211 {
212  d->rd_lsn = m0_fol_lsn_allocate(&fol);
213  rc = m0_fol_add_buf(&fol, &tx, d, &buf);
214  M0_ASSERT(rc == 0);
215  if (i % 1000 == 0)
216  checkpoint();
217 }
218 #endif
219 
221  .us_name = "fol-ub",
222  .us_init = ub_init,
223  .us_fini = ub_fini,
224  .us_run = {
225 #if 0 /* Convert to BE. */
226  { .ub_name = "insert",
227  .ub_iter = UB_ITER,
228  .ub_round = ub_insert },
229 
230  { .ub_name = "lookup",
231  .ub_iter = UB_ITER,
232  .ub_round = ub_lookup },
233 
234  { .ub_name = "insert-buf",
235  .ub_iter = UB_ITER,
236  .ub_round = ub_insert_buf },
237 #endif
238  { .ub_name = NULL }
239  }
240 };
241 
242 /*
243  * Local variables:
244  * c-indentation-style: "K&R"
245  * c-basic-offset: 8
246  * tab-width: 8
247  * fill-column: 80
248  * scroll-step: 1
249  * End:
250  */
Definition: fol.h:140
M0_INTERNAL void m0_fol_fini(struct m0_fol *fol)
Definition: fol.c:89
M0_INTERNAL void m0_ut_backend_fini(struct m0_be_ut_backend *be, struct m0_be_ut_seg *seg)
Definition: be.c:50
int(* rpo_undo)(struct m0_fol_frag *frag, struct m0_be_tx *tx)
Definition: fol.h:284
static struct m0_be_tx g_tx
Definition: fol.c:43
#define NULL
Definition: misc.h:38
static struct m0_be_ut_backend g_ut_be
Definition: fol.c:41
M0_INTERNAL void m0_fol_rec_fini(struct m0_fol_rec *rec)
Definition: fol.c:104
static void test_fol_frag_type_reg(void)
Definition: fol.c:72
const struct m0_fol_frag_ops * rp_ops
Definition: fol.h:244
Definition: fol.c:159
M0_INTERNAL int m0_fol_frag_type_register(struct m0_fol_frag_type *type)
Definition: fol.c:178
M0_INTERNAL void m0_fol_frag_init(struct m0_fol_frag *frag, void *data, const struct m0_fol_frag_type *type)
Definition: fol.c:121
static void checkpoint(void)
Definition: extmap.c:91
struct m0_tl fr_frags
Definition: fol.h:201
M0_INTERNAL void m0_ut_be_tx_end(struct m0_be_tx *tx)
Definition: be.c:82
void * rp_data
Definition: fol.h:249
#define M0_SET0(obj)
Definition: misc.h:64
Definition: ut.h:77
M0_INTERNAL void m0_ut_be_tx_begin2(struct m0_be_tx *tx, struct m0_be_ut_backend *ut_be, struct m0_be_tx_credit *cred, m0_bcount_t payload_cred)
Definition: be.c:68
static void test_fini(void)
Definition: fol.c:65
static struct m0_fol_rec g_rec
Definition: fol.c:40
Definition: sock.c:887
static void ub_fini(void)
Definition: fol.c:168
#define m0_tl_endfor
Definition: tlist.h:700
#define M0_BE_TX_CREDIT(nr, size)
Definition: tx_credit.h:94
static void test_fol_frag_encdec(void)
Definition: fol.c:102
Definition: buf.h:37
M0_INTERNAL void m0_fol_rec_init(struct m0_fol_rec *rec, struct m0_fol *fol)
Definition: fol.c:98
int i
Definition: dir.c:1033
#define M0_ASSERT(cond)
const char * us_name
Definition: ub.h:76
static void test_init(void)
Definition: fol.c:50
M0_INTERNAL void m0_fol_init(struct m0_fol *fol)
Definition: fol.c:84
uint64_t f_container
Definition: fid.h:39
static int verify_frag_data(struct m0_fol_frag *frag, struct m0_be_tx *tx)
Definition: fol.c:91
static struct m0_fol g_fol
Definition: fol.c:39
static struct m0_be_ut_seg g_ut_seg
Definition: fol.c:42
struct m0_ut_suite fol_ut
Definition: fol.c:137
static void ub_insert(int i)
Definition: list.c:165
M0_FOL_FRAG_TYPE_DECLARE(ut_frag, static, verify_frag_data, NULL, NULL, NULL)
const char * ts_name
Definition: ut.h:99
static int ub_init(const char *opts M0_UNUSED)
Definition: fol.c:161
Definition: fid.h:38
uint64_t f_key
Definition: fid.h:40
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
static int r[NR]
Definition: thread.c:46
M0_INTERNAL void m0_fol_frag_add(struct m0_fol_rec *rec, struct m0_fol_frag *frag)
Definition: fol.c:468
#define M0_FOL_FRAG_TYPE_XC_OPS(name, frag_xc, frag_type_ops)
Definition: fol.h:342
struct m0_buf t_payload
Definition: tx.h:364
M0_INTERNAL void m0_fol_frag_type_deregister(struct m0_fol_frag_type *type)
Definition: fol.c:201
struct m0_ub_set m0_fol_ub
Definition: fol.c:220
#define m0_tl_for(name, head, obj)
Definition: tlist.h:695
int32_t rc
Definition: trigger_fop.h:47
Definition: ub.h:74
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL void m0_ut_backend_init(struct m0_be_ut_backend *be, struct m0_be_ut_seg *seg)
Definition: be.c:43
static struct m0_addb2_frame_header last
Definition: storage.c:93
M0_INTERNAL int m0_fol_rec_encode(struct m0_fol_rec *rec, struct m0_buf *at)
Definition: fol.c:314
static void test_fol_frag_type_unreg(void)
Definition: fol.c:83
Definition: tx.h:280
#define M0_UNUSED
Definition: misc.h:380
M0_INTERNAL int m0_fol_rec_decode(struct m0_fol_rec *rec, struct m0_buf *at)
Definition: fol.c:331