Motr  M0
pvers.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #include "conf/pvers.h"
23 #include "conf/helpers.h" /* m0_conf_pvers */
24 #include "conf/ut/common.h" /* m0_conf_ut_cache */
25 #include "lib/errno.h" /* ENOENT */
26 #include "ut/ut.h"
27 
28 static void test_pver_fid(void)
29 {
30  const struct {
31  enum m0_conf_pver_kind t_kind;
32  uint64_t t_container;
33  uint64_t t_key;
34  } tests[] = {
35  { M0_CONF_PVER_ACTUAL, 1, 25 },
36  { M0_CONF_PVER_FORMULAIC, 1, 25 },
37  { M0_CONF_PVER_VIRTUAL, 1, 25 },
38  /* XXX TODO: add more tests */
39  };
40  struct m0_fid fid;
41  enum m0_conf_pver_kind kind;
42  uint64_t container;
43  uint64_t key;
44  unsigned i;
45  int rc;
46 
47  for (i = 0; i < ARRAY_SIZE(tests); ++i) {
48  /* encode */
49  fid = m0_conf_pver_fid(tests[i].t_kind, tests[i].t_container,
50  tests[i].t_key);
52  /* decode */
53  rc = m0_conf_pver_fid_read(&fid, &kind, &container, &key);
54  M0_UT_ASSERT(rc == 0);
55  M0_UT_ASSERT(kind == tests[i].t_kind);
56  M0_UT_ASSERT(container == tests[i].t_container);
57  M0_UT_ASSERT(key == tests[i].t_key);
58  }
59 }
60 
61 static void conf_ut_ha_state_set(const struct m0_conf_cache *cache,
62  const struct m0_fid *objid,
63  enum m0_ha_obj_state state)
64 {
65  struct m0_conf_obj *obj;
66  struct m0_conf_pver_subtree *pvsub;
67 
68  obj = m0_conf_cache_lookup(cache, objid);
69  M0_UT_ASSERT(obj != NULL);
70  M0_UT_ASSERT(obj->co_ha_state != state);
71  obj->co_ha_state = state;
72 
73  pvsub = &m0_conf_pvers(obj)[0]->pv_u.subtree;
74  if (state == M0_NC_ONLINE)
76  else
78 }
79 
80 static void test_pver_find(void)
81 {
82  const struct m0_fid base = M0_FID_TINIT('v', 1, 0); /* pver-0 */
83  const struct m0_fid failed[] = {
84  M0_FID_TINIT('k', 1, 2), /* disk-2 */
85  M0_FID_TINIT('k', 1, 6), /* disk-6 */
86  M0_FID_TINIT('c', 1, 1) /* controller-1 */
87  };
89  struct m0_conf_pool *pool;
90  struct m0_conf_pver *pver;
91  struct m0_conf_pver *pver_virt;
92  struct m0_conf_root *root;
93  unsigned i;
94  const uint32_t tolvec[M0_CONF_PVER_HEIGHT] = {0, 0, 0, 1, 2};
95  int rc;
96 
97  m0_conf_ut_cache_from_file(cache, M0_SRC_PATH("conf/ut/pvers.xc"));
98  /*
99  * m0_conf_pver_find() tests.
100  */
102  &M0_FID_TINIT('o', 1, 0)),
103  m0_conf_pool);
105  M0_UT_ASSERT(rc == 0);
107  M0_UT_ASSERT(m0_fid_eq(&pver->pv_obj.co_id, &base));
108  M0_UT_ASSERT(M0_IS0(&pver->pv_u.subtree.pvs_recd));
109 
110  rc = m0_conf_pver_find(pool, &pver->pv_obj.co_id, &pver);
111  M0_UT_ASSERT(rc == -ENOENT);
112 
115  M0_UT_ASSERT(rc == -ENOENT);
116  for (i = 1; i < ARRAY_SIZE(failed); ++i)
118  rc = m0_conf_pver_find(pool, NULL, &pver_virt);
119  M0_UT_ASSERT(rc == 0);
121  M0_UT_ASSERT(!m0_fid_eq(&pver_virt->pv_obj.co_id, &base));
122  M0_UT_ASSERT(pver_virt->pv_u.subtree.pvs_attr.pa_P <
123  pver->pv_u.subtree.pvs_attr.pa_P);
124 
126  M0_UT_ASSERT(rc == 0);
127  M0_UT_ASSERT(pver == pver_virt);
128  M0_UT_ASSERT(!memcmp(pver->pv_u.subtree.pvs_tolerance,
129  tolvec, sizeof tolvec));
130  for (i = 0; i < ARRAY_SIZE(failed); ++i)
133  M0_UT_ASSERT(rc == 0);
135  M0_UT_ASSERT(!memcmp(pver->pv_u.subtree.pvs_tolerance,
136  tolvec, sizeof tolvec));
137 
138  /*
139  * m0_conf_pver_find_by_fid() tests.
140  */
142  &M0_FID_TINIT('t', 1, 0)),
143  m0_conf_root);
144  rc = m0_conf_pver_find_by_fid(&pver_virt->pv_obj.co_id, root, &pver);
145  M0_UT_ASSERT(rc == 0);
146  M0_UT_ASSERT(pver == pver_virt);
147  {
148  const struct m0_conf_pver *fpver;
149 
151  &fpver);
152  M0_UT_ASSERT(rc == 0);
155  /* pver_f-0 */
156  &M0_FID_INIT(0x7640000000000001, 0)));
157  }
158  /*
159  * Now let us request a slightly different fid.
160  *
161  * The value added to .f_key (1) is carefully chosen to be compatible
162  * with the structure of base pver subtree and the allowance vector.
163  */
165  &M0_FID_INIT(pver_virt->pv_obj.co_id.f_container,
166  pver_virt->pv_obj.co_id.f_key + 1), root, &pver);
167  M0_UT_ASSERT(rc == 0);
169  M0_UT_ASSERT(pver != pver_virt);
170  M0_UT_ASSERT(pver->pv_u.subtree.pvs_attr.pa_P ==
171  pver_virt->pv_u.subtree.pvs_attr.pa_P);
173  &M0_FID_INIT(pver_virt->pv_obj.co_id.f_container,
174  /*
175  * This cid is not compatible with the
176  * allowance vector.
177  */
178  pver_virt->pv_obj.co_id.f_key + 2), root, &pver);
179  M0_UT_ASSERT(rc == -EINVAL);
180 }
181 
183  .ts_name = "conf-pvers-ut",
184  .ts_init = m0_conf_ut_cache_init,
185  .ts_fini = m0_conf_ut_cache_fini,
186  .ts_tests = {
187  { "fid", test_pver_fid },
188  { "pver-find", test_pver_find },
189  { NULL, NULL }
190  }
191 };
struct m0_fid co_id
Definition: obj.h:208
Definition: beck.c:235
M0_INTERNAL int m0_conf_ut_cache_init(void)
Definition: common.c:107
struct m0_pdclust_attr pvs_attr
Definition: obj.h:481
#define NULL
Definition: misc.h:38
M0_INTERNAL struct m0_conf_obj * m0_conf_cache_lookup(const struct m0_conf_cache *cache, const struct m0_fid *id)
Definition: cache.c:106
struct m0_container container
const struct m0_conf_obj_type * m0_conf_fid_type(const struct m0_fid *fid)
Definition: obj.c:368
const struct m0_conf_obj_type M0_CONF_PVER_TYPE
Definition: pver.c:260
#define M0_FID_INIT(container, key)
Definition: fid.h:84
static char * tests
Definition: st_kmain.c:52
uint32_t pvs_recd[M0_CONF_PVER_HEIGHT]
Definition: obj.h:477
static void test_pver_find(void)
Definition: pvers.c:80
struct m0_conf_obj pv_obj
Definition: obj.h:533
#define M0_SRC_PATH(name)
Definition: misc.h:48
Definition: ut.h:77
M0_INTERNAL struct m0_conf_pver ** m0_conf_pvers(const struct m0_conf_obj *obj)
Definition: helpers.c:400
M0_INTERNAL unsigned m0_conf_pver_level(const struct m0_conf_obj *obj)
Definition: pvers.c:394
m0_conf_pver_kind
Definition: obj.h:516
static struct foo * obj
Definition: tlist.c:302
static void test_pver_fid(void)
Definition: pvers.c:28
enum m0_conf_pver_kind pv_kind
Definition: obj.h:535
struct m0_fid fid
Definition: di.c:46
int i
Definition: dir.c:1033
struct m0_conf_cache m0_conf_ut_cache
Definition: common.c:31
struct m0_conf_root * root
Definition: note.c:50
static int key
Definition: locality.c:283
#define M0_FID_TINIT(type, container, key)
Definition: fid.h:90
struct m0_fid pver
Definition: idx_dix.c:74
union m0_conf_pver::@122 pv_u
M0_INTERNAL struct m0_fid m0_conf_pver_fid(enum m0_conf_pver_kind kind, uint64_t container, uint64_t key)
Definition: pvers.c:309
m0_ha_obj_state
Definition: note.h:119
static void conf_ut_ha_state_set(const struct m0_conf_cache *cache, const struct m0_fid *objid, enum m0_ha_obj_state state)
Definition: pvers.c:61
M0_INTERNAL int m0_conf_ut_cache_fini(void)
Definition: common.c:114
uint64_t f_container
Definition: fid.h:39
M0_INTERNAL int m0_conf_pver_fid_read(const struct m0_fid *fid, enum m0_conf_pver_kind *kind, uint64_t *container, uint64_t *key)
Definition: pvers.c:352
#define M0_CONF_CAST(ptr, type)
Definition: obj.h:780
struct m0_ut_suite conf_pvers_ut
Definition: pvers.c:182
static struct m0_pool pool
Definition: iter_ut.c:58
struct m0_conf_pver_subtree subtree
Definition: obj.h:537
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
#define M0_CNT_INC(cnt)
Definition: arith.h:226
Definition: fid.h:38
uint64_t f_key
Definition: fid.h:40
M0_INTERNAL int m0_conf_pver_find_by_fid(const struct m0_fid *fid, const struct m0_conf_root *root, struct m0_conf_pver **out)
Definition: pvers.c:219
#define M0_IS0(obj)
Definition: misc.h:70
M0_INTERNAL void m0_conf_ut_cache_from_file(struct m0_conf_cache *cache, const char *path)
Definition: common.c:123
uint32_t pa_P
Definition: pdclust.h:115
#define M0_CNT_DEC(cnt)
Definition: arith.h:219
static uint64_t base
Definition: dump.c:1504
M0_INTERNAL int m0_conf_pver_find(const struct m0_conf_pool *pool, const struct m0_fid *pver_to_skip, struct m0_conf_pver **out)
Definition: pvers.c:207
M0_INTERNAL int m0_conf_pver_formulaic_from_virtual(const struct m0_conf_pver *virtual, const struct m0_conf_root *root, const struct m0_conf_pver **out)
Definition: pvers.c:244
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
Definition: idx_mock.c:47