Motr  M0
db.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 "conf/db.h"
24 #include "conf/obj.h" /* m0_conf_fid_type */
25 #include "conf/onwire.h" /* m0_confx_obj, m0_confx */
26 #include "conf/preload.h" /* m0_confstr_parse, m0_confx_free */
27 #include "conf/ut/confc.h" /* m0_ut_conf_fids */
28 #include "conf/ut/common.h"
29 #include "lib/finject.h" /* m0_fi_enable */
30 #include "lib/fs.h" /* m0_file_read */
31 #include "be/ut/helper.h" /* m0_be_ut_backend_init */
32 #include "ut/misc.h" /* M0_UT_PATH */
33 #include "ut/ut.h"
34 
35 static struct m0_be_ut_backend ut_be;
36 static struct m0_be_ut_seg ut_seg;
37 static struct m0_be_seg *seg;
38 
39 #define XCAST(xobj, type) ((struct type *)(&(xobj)->xo_u))
40 
41 static void profile_check(const struct m0_confx_obj *xobj)
42 {
47 }
48 
49 static void node_check(const struct m0_confx_obj *xobj)
50 {
51  const struct m0_confx_node *x = XCAST(xobj, m0_confx_node);
52 
57 
58  M0_UT_ASSERT(x->xn_memsize == 16000);
59  M0_UT_ASSERT(x->xn_nr_cpu == 2);
60  M0_UT_ASSERT(x->xn_last_state == 3);
61  M0_UT_ASSERT(x->xn_flags == 2);
62 
63  M0_UT_ASSERT(x->xn_processes.af_count == 2);
64  M0_UT_ASSERT(m0_fid_eq(&x->xn_processes.af_elems[0],
66  M0_UT_ASSERT(m0_fid_eq(&x->xn_processes.af_elems[1],
68 }
69 
70 static void diskv_check(const struct m0_confx_obj *xobj)
71 {
72  const struct m0_confx_objv *x = XCAST(xobj, m0_confx_objv);
77  M0_UT_ASSERT(m0_fid_eq(&x->xj_real,
79  M0_UT_ASSERT(x->xj_children.af_count == 0);
80 }
81 
82 static void conf_ut_db_init()
83 {
85  m0_be_ut_seg_init(&ut_seg, &ut_be, 1ULL << 24);
86  seg = ut_seg.bus_seg;
87 }
88 
89 static void conf_ut_db_fini()
90 {
91  /*
92  * XXX: Call m0_ut_backend_fini_with_reqh() after
93  * fixing m0_confdb_destroy().
94  */
97 }
98 
99 static int conf_ut_be_tx_create(struct m0_be_tx *tx,
100  struct m0_be_ut_backend *ut_be,
101  struct m0_be_tx_credit *accum)
102 {
103  m0_be_ut_tx_init(tx, ut_be);
104  m0_be_tx_prep(tx, accum);
105  return m0_be_tx_open_sync(tx);
106 }
107 
108 static void conf_ut_be_tx_fini(struct m0_be_tx *tx)
109 {
111  m0_be_tx_fini(tx);
112 }
113 
114 static void test_confdb(void)
115 {
116  struct m0_confx *enc;
117  struct m0_confx *dec;
118  struct m0_be_tx_credit accum = {};
119  struct m0_be_tx tx = {};
120  char *confstr = NULL;
121  bool error_desired;
122  int i;
123  int j;
124  int hit;
125  int rc;
126  struct {
127  const struct m0_fid *fid;
128  void (*check)(const struct m0_confx_obj *xobj);
129  } tests[] = {
133  };
134 
135  M0_SET0(&ut_be);
136  M0_SET0(&ut_seg);
137  seg = NULL;
138 
139  rc = m0_file_read(M0_UT_PATH("conf.xc"), &confstr);
140  M0_UT_ASSERT(rc == 0);
141 
142  rc = m0_confstr_parse("[0]", &enc);
143  M0_UT_ASSERT(rc == 0);
144  M0_UT_ASSERT(enc->cx_nr == 0);
145  m0_confx_free(enc);
146 
147  rc = m0_confstr_parse(confstr, &enc);
148  M0_UT_ASSERT(rc == 0);
149  M0_UT_ASSERT(enc->cx_nr == M0_UT_CONF_NR_OBJS);
150  m0_free0(&confstr);
151 
152  conf_ut_db_init();
153 
154  for (i = 0; i < 3; ++i) {
155  M0_SET0(&accum);
156  rc = m0_confdb_create_credit(seg, enc, &accum);
157  M0_UT_ASSERT(rc == 0);
158  M0_SET0(&tx);
159  rc = conf_ut_be_tx_create(&tx, &ut_be, &accum);
160  M0_UT_ASSERT(rc == 0);
161 
162  switch (i) {
163  case 0:
164  m0_fi_enable("confdb_table_init",
165  "ut_confdb_create_failure");
166  error_desired = true;
167  break;
168  case 1:
169  m0_fi_enable("confx_obj_dup",
170  "ut_confx_obj_dup_failure");
171  error_desired = true;
172  break;
173  case 2:
174  /* Usual case. Should complete without errors. */
175  error_desired = false;
176  break;
177  }
178  rc = m0_confdb_create(seg, &tx, enc, &M0_FID_TINIT('b', 0, 1));
179  M0_UT_ASSERT(ergo(error_desired, rc < 0));
180  M0_UT_ASSERT(ergo(!error_desired, rc == 0));
181 
182  switch (i) {
183  case 0:
184  m0_fi_disable("confdb_table_init",
185  "ut_confdb_create_failure");
186  break;
187  case 1:
188  m0_fi_disable("confx_obj_dup",
189  "ut_confx_obj_dup_failure");
190  error_desired = true;
191  break;
192  case 2:
193  break;
194  }
195  conf_ut_be_tx_fini(&tx);
196  }
197 
198  rc = m0_confdb_read(seg, &dec);
199  M0_UT_ASSERT(rc == 0);
200  M0_UT_ASSERT(enc->cx_nr == M0_UT_CONF_NR_OBJS);
201  /*
202  * @dec can be re-ordered w.r.t. to @enc.
203  */
204  for (hit = 0, i = 0; i < dec->cx_nr; ++i) {
205  struct m0_confx_obj *o = M0_CONFX_AT(dec, i);
206 
207  for (j = 0; j < ARRAY_SIZE(tests); ++j) {
208  if (m0_fid_eq(m0_conf_objx_fid(o), tests[j].fid)) {
209  tests[j].check(o);
210  hit++;
211  }
212  }
213  }
214  M0_UT_ASSERT(hit == ARRAY_SIZE(tests));
215 
216  m0_confx_free(enc);
217  m0_free(dec->cx__objs);
218  m0_free(dec);
220  M0_SET0(&accum);
221  m0_confdb_destroy_credit(seg, &accum);
222  rc = conf_ut_be_tx_create(&tx, &ut_be, &accum);
223  M0_UT_ASSERT(rc == 0);
224  rc = m0_confdb_destroy(seg, &tx);
225  M0_UT_ASSERT(rc == 0);
226  conf_ut_be_tx_fini(&tx);
227  conf_ut_db_fini();
228 }
229 
231  .ts_name = "confstr-ut",
232  .ts_tests = {
233  { "db", test_confdb },
234  { NULL, NULL }
235  }
236 };
struct m0_ut_suite confstr_ut
Definition: db.c:230
void m0_be_ut_seg_fini(struct m0_be_ut_seg *ut_seg)
Definition: stubs.c:267
const struct m0_conf_obj_type M0_CONF_OBJV_TYPE
Definition: objv.c:151
static void node_check(const struct m0_confx_obj *xobj)
Definition: db.c:49
M0_INTERNAL void m0_confx_free(struct m0_confx *enc)
Definition: preload.c:33
static void conf_ut_db_fini()
Definition: db.c:89
#define NULL
Definition: misc.h:38
const struct m0_conf_obj_type M0_CONF_PROFILE_TYPE
Definition: profile.c:97
#define ergo(a, b)
Definition: misc.h:293
static bool x
Definition: sm.c:168
const struct m0_conf_obj_type * m0_conf_fid_type(const struct m0_fid *fid)
Definition: obj.c:368
M0_INTERNAL int m0_file_read(const char *path, char **out)
Definition: fs.c:61
struct m0_be_seg * bus_seg
Definition: helper.h:119
M0_INTERNAL void m0_be_tx_fini(struct m0_be_tx *tx)
Definition: stubs.c:163
static char * tests
Definition: st_kmain.c:52
static struct m0_be_ut_backend ut_be
Definition: db.c:35
M0_INTERNAL void m0_be_tx_prep(struct m0_be_tx *tx, const struct m0_be_tx_credit *credit)
Definition: stubs.c:175
static void test_confdb(void)
Definition: db.c:114
void m0_be_ut_seg_init(struct m0_be_ut_seg *ut_seg, struct m0_be_ut_backend *ut_be, m0_bcount_t size)
Definition: stubs.c:256
#define M0_CONFX_AT(cx, idx)
Definition: onwire.h:271
#define M0_SET0(obj)
Definition: misc.h:64
Definition: ut.h:77
M0_INTERNAL void m0_confdb_destroy_credit(struct m0_be_seg *seg, struct m0_be_tx_credit *accum)
Definition: db.c:380
struct m0_fid fid
Definition: di.c:46
int i
Definition: dir.c:1033
#define M0_FID_TINIT(type, container, key)
Definition: fid.h:90
uint32_t cx_nr
Definition: onwire.h:259
M0_INTERNAL void m0_fi_disable(const char *fp_func, const char *fp_tag)
Definition: finject.c:485
static void conf_ut_be_tx_fini(struct m0_be_tx *tx)
Definition: db.c:108
static void m0_fi_enable(const char *func, const char *tag)
Definition: finject.h:276
#define m0_free0(pptr)
Definition: memory.h:77
M0_INTERNAL void m0_confdb_fini(struct m0_be_seg *seg)
Definition: db.c:464
M0_INTERNAL int m0_confdb_read(struct m0_be_seg *seg, struct m0_confx **out)
Definition: db.c:544
M0_INTERNAL int m0_be_tx_open_sync(struct m0_be_tx *tx)
Definition: stubs.c:199
static int conf_ut_be_tx_create(struct m0_be_tx *tx, struct m0_be_ut_backend *ut_be, struct m0_be_tx_credit *accum)
Definition: db.c:99
void m0_be_ut_backend_init(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:238
static const struct m0_fid m0_ut_conf_fids[]
Definition: confc.h:54
Definition: seg.h:66
M0_INTERNAL int m0_confdb_destroy(struct m0_be_seg *seg, struct m0_be_tx *tx)
Definition: db.c:443
M0_INTERNAL int m0_confdb_create(struct m0_be_seg *seg, struct m0_be_tx *tx, const struct m0_confx *conf, const struct m0_fid *btree_fid)
Definition: db.c:331
M0_INTERNAL bool m0_fid_eq(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:164
const char * ts_name
Definition: ut.h:99
M0_INTERNAL int m0_confstr_parse(const char *str, struct m0_confx **out)
Definition: preload.c:41
Definition: fid.h:38
const struct m0_conf_obj_type M0_CONF_NODE_TYPE
Definition: node.c:128
static void conf_ut_db_init()
Definition: db.c:82
#define XCAST(xobj, type)
Definition: db.c:39
static struct m0_be_seg * seg
Definition: db.c:37
static void diskv_check(const struct m0_confx_obj *xobj)
Definition: db.c:70
static void profile_check(const struct m0_confx_obj *xobj)
Definition: db.c:41
void m0_be_ut_tx_init(struct m0_be_tx *tx, struct m0_be_ut_backend *ut_be)
Definition: stubs.c:286
void m0_be_ut_backend_fini(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:242
void check(struct workload *w)
M0_INTERNAL int m0_confdb_create_credit(struct m0_be_seg *seg, const struct m0_confx *conf, struct m0_be_tx_credit *accum)
Definition: db.c:255
#define M0_UT_PATH(name)
Definition: misc.h:41
void m0_free(void *data)
Definition: memory.c:146
static struct m0_be_ut_seg ut_seg
Definition: db.c:36
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static void dec(struct m0_addb2__context *ctx, const uint64_t *v, char *buf)
Definition: dump.c:408
const struct m0_fid * m0_conf_objx_fid(const struct m0_confx_obj *obj)
Definition: obj_ops.c:266
M0_INTERNAL void m0_be_tx_close_sync(struct m0_be_tx *tx)
Definition: stubs.c:205
Definition: tx.h:280