1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <adt/hash.h>
|
---|
30 | #include <adt/hash_table.h>
|
---|
31 | #include <adt/list.h>
|
---|
32 | #include <str.h>
|
---|
33 | #include <assert.h>
|
---|
34 | #include <errno.h>
|
---|
35 | #include <fibril_synch.h>
|
---|
36 |
|
---|
37 | #include "repo.h"
|
---|
38 | #include "edge.h"
|
---|
39 | #include "log.h"
|
---|
40 |
|
---|
41 | LIST_INITIALIZE(units_);
|
---|
42 |
|
---|
43 | static hash_table_t units_by_name;
|
---|
44 | static hash_table_t units_by_handle;
|
---|
45 | /** Lock to protect units_by_name and units_by_handle, so that
|
---|
46 | * repo_find_unit_by_* can be called also from non-event loop fibrils.
|
---|
47 | */
|
---|
48 | static FIBRIL_RWLOCK_INITIALIZE(repo_lock);
|
---|
49 |
|
---|
50 | /* Hash table functions */
|
---|
51 | static size_t units_by_handle_ht_hash(const ht_link_t *item)
|
---|
52 | {
|
---|
53 | unit_t *unit =
|
---|
54 | hash_table_get_inst(item, unit_t, units_by_handle);
|
---|
55 | return unit->handle;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static size_t units_by_handle_ht_key_hash(const void *key)
|
---|
59 | {
|
---|
60 | return *(unit_handle_t *)key;
|
---|
61 | }
|
---|
62 |
|
---|
63 | static bool units_by_handle_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
|
---|
64 | {
|
---|
65 | return
|
---|
66 | hash_table_get_inst(item1, unit_t, units_by_handle) ==
|
---|
67 | hash_table_get_inst(item2, unit_t, units_by_handle);
|
---|
68 | }
|
---|
69 |
|
---|
70 | static bool units_by_handle_ht_key_equal(const void *key, const ht_link_t *item)
|
---|
71 | {
|
---|
72 | return *(unit_handle_t *)key ==
|
---|
73 | hash_table_get_inst(item, unit_t, units_by_handle)->handle;
|
---|
74 | }
|
---|
75 |
|
---|
76 | static hash_table_ops_t units_by_handle_ht_ops = {
|
---|
77 | .hash = &units_by_handle_ht_hash,
|
---|
78 | .key_hash = &units_by_handle_ht_key_hash,
|
---|
79 | .equal = &units_by_handle_ht_equal,
|
---|
80 | .key_equal = &units_by_handle_ht_key_equal,
|
---|
81 | .remove_callback = NULL
|
---|
82 | };
|
---|
83 |
|
---|
84 | static size_t units_by_name_ht_hash(const ht_link_t *item)
|
---|
85 | {
|
---|
86 | unit_t *unit =
|
---|
87 | hash_table_get_inst(item, unit_t, units_by_name);
|
---|
88 | return hash_string(unit->name);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static size_t units_by_name_ht_key_hash(const void *key)
|
---|
92 | {
|
---|
93 | return hash_string((const char *)key);
|
---|
94 | }
|
---|
95 |
|
---|
96 | static bool units_by_name_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
|
---|
97 | {
|
---|
98 | return
|
---|
99 | hash_table_get_inst(item1, unit_t, units_by_handle) ==
|
---|
100 | hash_table_get_inst(item2, unit_t, units_by_handle);
|
---|
101 | }
|
---|
102 |
|
---|
103 | static bool units_by_name_ht_key_equal(const void *key, const ht_link_t *item)
|
---|
104 | {
|
---|
105 | return str_cmp((const char *)key,
|
---|
106 | hash_table_get_inst(item, unit_t, units_by_name)->name) == 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | static hash_table_ops_t units_by_name_ht_ops = {
|
---|
110 | .hash = &units_by_name_ht_hash,
|
---|
111 | .key_hash = &units_by_name_ht_key_hash,
|
---|
112 | .equal = &units_by_name_ht_equal,
|
---|
113 | .key_equal = &units_by_name_ht_key_equal,
|
---|
114 | .remove_callback = NULL
|
---|
115 | };
|
---|
116 |
|
---|
117 | /* Repository functions */
|
---|
118 |
|
---|
119 | static void repo_remove_unit_internal(unit_t *u)
|
---|
120 | {
|
---|
121 | assert(fibril_rwlock_is_write_locked(&repo_lock));
|
---|
122 |
|
---|
123 | hash_table_remove_item(&units_by_name, &u->units_by_name);
|
---|
124 | hash_table_remove_item(&units_by_handle, &u->units_by_handle);
|
---|
125 | list_remove(&u->units);
|
---|
126 |
|
---|
127 | // TODO decrease refcount of unit
|
---|
128 | // unit may be referenced e.g. from running job, thus we cannot simply destroy it
|
---|
129 | }
|
---|
130 |
|
---|
131 | static unit_t *repo_find_unit_by_name_internal(const char *name, bool lock)
|
---|
132 | {
|
---|
133 | sysman_log(LVL_DEBUG2, "%s(%s, %i)", __func__, name, lock);
|
---|
134 | if (lock)
|
---|
135 | fibril_rwlock_read_lock(&repo_lock);
|
---|
136 | ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
|
---|
137 | if (lock)
|
---|
138 | fibril_rwlock_read_unlock(&repo_lock);
|
---|
139 |
|
---|
140 | if (ht_link != NULL) {
|
---|
141 | return hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
142 | } else {
|
---|
143 | return NULL;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | void repo_init(void)
|
---|
148 | {
|
---|
149 | hash_table_create(&units_by_name, 0, 0, &units_by_name_ht_ops);
|
---|
150 | hash_table_create(&units_by_handle, 0, 0, &units_by_handle_ht_ops);
|
---|
151 | }
|
---|
152 |
|
---|
153 | errno_t repo_add_unit(unit_t *unit)
|
---|
154 | {
|
---|
155 | assert(unit);
|
---|
156 | assert(unit->repo_state == REPO_EMBRYO);
|
---|
157 | assert(unit->handle == 0);
|
---|
158 | assert(unit->name != NULL);
|
---|
159 | assert(fibril_rwlock_is_write_locked(&repo_lock));
|
---|
160 | sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
|
---|
161 |
|
---|
162 | if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
|
---|
163 | /* Pointers are same size as unit_handle_t both on 32b and 64b */
|
---|
164 | unit->handle = (unit_handle_t)unit;
|
---|
165 |
|
---|
166 | hash_table_insert(&units_by_handle, &unit->units_by_handle);
|
---|
167 | list_append(&unit->units, &units_);
|
---|
168 | return EOK;
|
---|
169 | } else {
|
---|
170 | return EEXIST;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | errno_t repo_remove_unit(unit_t *unit)
|
---|
175 | {
|
---|
176 | unit->repo_state = REPO_ZOMBIE;
|
---|
177 | return EOK; /* We could check that unit is present in repo etc... */
|
---|
178 | }
|
---|
179 |
|
---|
180 | void repo_begin_update(void)
|
---|
181 | {
|
---|
182 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
183 | fibril_rwlock_write_lock(&repo_lock);
|
---|
184 | }
|
---|
185 |
|
---|
186 | static bool repo_commit_unit(ht_link_t *ht_link, void *arg)
|
---|
187 | {
|
---|
188 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
189 | if (unit->repo_state == REPO_ZOMBIE) {
|
---|
190 | repo_remove_unit_internal(unit);
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (unit->repo_state == REPO_EMBRYO) {
|
---|
195 | unit->repo_state = REPO_LIVING;
|
---|
196 | }
|
---|
197 |
|
---|
198 | list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
|
---|
199 | e->commited = true;
|
---|
200 | }
|
---|
201 | return true;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /** Marks newly added units_by_name as usable (via state change) */
|
---|
205 | void repo_commit(void)
|
---|
206 | {
|
---|
207 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * Apply commit to all units_by_name, each commited unit commits its
|
---|
211 | * outgoing deps, thus eventually commiting all embryo deps as well.
|
---|
212 | *
|
---|
213 | * TODO why not iterate over units list?
|
---|
214 | */
|
---|
215 | hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
|
---|
216 | fibril_rwlock_write_unlock(&repo_lock);
|
---|
217 | }
|
---|
218 |
|
---|
219 | static bool repo_rollback_unit(ht_link_t *ht_link, void *arg)
|
---|
220 | {
|
---|
221 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
222 |
|
---|
223 | list_foreach_safe(unit->edges_out, cur_link, next_link) {
|
---|
224 | unit_edge_t *e =
|
---|
225 | list_get_instance(cur_link, unit_edge_t, edges_out);
|
---|
226 | if (!e->commited) {
|
---|
227 | edge_remove(&e);
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (unit->repo_state == REPO_EMBRYO) {
|
---|
232 | repo_remove_unit_internal(unit);
|
---|
233 | } else if (unit->repo_state == REPO_ZOMBIE) {
|
---|
234 | unit->repo_state = REPO_LIVING;
|
---|
235 | }
|
---|
236 |
|
---|
237 | return true;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /** Remove all uncommited units_by_name and edges from configuratio
|
---|
241 | *
|
---|
242 | * Memory used by removed object is released.
|
---|
243 | */
|
---|
244 | void repo_rollback(void)
|
---|
245 | {
|
---|
246 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
247 |
|
---|
248 | hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
|
---|
249 | fibril_rwlock_write_unlock(&repo_lock);
|
---|
250 | }
|
---|
251 |
|
---|
252 | static bool repo_resolve_unit(ht_link_t *ht_link, void *arg)
|
---|
253 | {
|
---|
254 | bool *has_error_ptr = arg;
|
---|
255 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
256 |
|
---|
257 | list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
|
---|
258 | assert(e->input == unit);
|
---|
259 | assert((e->output != NULL) != (e->output_name != NULL));
|
---|
260 | if (e->output) {
|
---|
261 | continue;
|
---|
262 | }
|
---|
263 |
|
---|
264 | unit_t *output =
|
---|
265 | repo_find_unit_by_name_unsafe(e->output_name);
|
---|
266 | if (output == NULL) {
|
---|
267 | sysman_log(LVL_ERROR,
|
---|
268 | "Cannot resolve dependency of '%s' to unit '%s'",
|
---|
269 | unit_name(unit), e->output_name);
|
---|
270 | *has_error_ptr = true;
|
---|
271 | // TODO should we just leave the sprout untouched?
|
---|
272 | } else {
|
---|
273 | edge_resolve_output(e, output);
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | return true;
|
---|
278 | }
|
---|
279 |
|
---|
280 | static bool repo_clean_up_unit(ht_link_t *ht_link, void *arg)
|
---|
281 | {
|
---|
282 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
283 |
|
---|
284 | if (unit->conditions != UNIT_CONDITION_NONE) {
|
---|
285 | unit_log_condition(unit);
|
---|
286 |
|
---|
287 | list_foreach_safe(unit->edges_in, current_link, iter) {
|
---|
288 | unit_edge_t *e = list_get_instance(current_link, unit_edge_t, edges_in);
|
---|
289 | edge_remove(&e);
|
---|
290 | }
|
---|
291 |
|
---|
292 | list_foreach_safe(unit->edges_out, current_link, iter) {
|
---|
293 | unit_edge_t *e = list_get_instance(current_link, unit_edge_t, edges_out);
|
---|
294 | edge_remove(&e);
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | return true;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /** Resolve unresolved dependencies between any pair of units_by_name
|
---|
302 | *
|
---|
303 | * @return EOK on success
|
---|
304 | * @return ENOENT when one or more resolution fails, information is logged
|
---|
305 | */
|
---|
306 | errno_t repo_resolve_references(void)
|
---|
307 | {
|
---|
308 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
309 |
|
---|
310 | bool has_error = false;
|
---|
311 | hash_table_apply(&units_by_name, &repo_resolve_unit, &has_error);
|
---|
312 |
|
---|
313 | if (!has_error) {
|
---|
314 | //once all references have been built test if there are
|
---|
315 | //units which dont meet the conditions. If yes, remove the edges
|
---|
316 | hash_table_apply(&units_by_name, &repo_clean_up_unit, &has_error);
|
---|
317 | }
|
---|
318 |
|
---|
319 | return has_error ? ENOENT : EOK;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * The function can be safely called from non-event loop fibrils
|
---|
324 | */
|
---|
325 | unit_t *repo_find_unit_by_name(const char *name)
|
---|
326 | {
|
---|
327 | return repo_find_unit_by_name_internal(name, true);
|
---|
328 | }
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * @note Caller must hold repo_lock (at least reader)
|
---|
332 | */
|
---|
333 | unit_t *repo_find_unit_by_name_unsafe(const char *name)
|
---|
334 | {
|
---|
335 | return repo_find_unit_by_name_internal(name, false);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * The function can be safely called from non-event loop fibrils
|
---|
340 | */
|
---|
341 | unit_t *repo_find_unit_by_handle(unit_handle_t handle)
|
---|
342 | {
|
---|
343 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
344 | fibril_rwlock_read_lock(&repo_lock);
|
---|
345 | ht_link_t *ht_link = hash_table_find(&units_by_handle, &handle);
|
---|
346 | fibril_rwlock_read_unlock(&repo_lock);
|
---|
347 |
|
---|
348 | if (ht_link != NULL) {
|
---|
349 | return hash_table_get_inst(ht_link, unit_t, units_by_handle);
|
---|
350 | } else {
|
---|
351 | return NULL;
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | void repo_rlock(void)
|
---|
356 | {
|
---|
357 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
358 | fibril_rwlock_read_lock(&repo_lock);
|
---|
359 | }
|
---|
360 |
|
---|
361 | void repo_runlock(void)
|
---|
362 | {
|
---|
363 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
364 | fibril_rwlock_read_unlock(&repo_lock);
|
---|
365 | }
|
---|