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 <assert.h>
|
---|
33 | #include <errno.h>
|
---|
34 | #include <fibril_synch.h>
|
---|
35 |
|
---|
36 | #include "repo.h"
|
---|
37 | #include "edge.h"
|
---|
38 | #include "log.h"
|
---|
39 |
|
---|
40 | LIST_INITIALIZE(units);
|
---|
41 |
|
---|
42 | static hash_table_t units_by_name;
|
---|
43 | static hash_table_t units_by_handle;
|
---|
44 |
|
---|
45 | /* Hash table functions */
|
---|
46 | static size_t units_by_handle_ht_hash(const ht_link_t *item)
|
---|
47 | {
|
---|
48 | unit_t *unit =
|
---|
49 | hash_table_get_inst(item, unit_t, units_by_handle);
|
---|
50 | return unit->handle;
|
---|
51 | }
|
---|
52 |
|
---|
53 | static size_t units_by_handle_ht_key_hash(void *key)
|
---|
54 | {
|
---|
55 | return *(unit_handle_t *)key;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static bool units_by_handle_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
|
---|
59 | {
|
---|
60 | return
|
---|
61 | hash_table_get_inst(item1, unit_t, units_by_handle) ==
|
---|
62 | hash_table_get_inst(item2, unit_t, units_by_handle);
|
---|
63 | }
|
---|
64 |
|
---|
65 | static bool units_by_handle_ht_key_equal(void *key, const ht_link_t *item)
|
---|
66 | {
|
---|
67 | return *(unit_handle_t *)key ==
|
---|
68 | hash_table_get_inst(item, unit_t, units_by_handle)->handle;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static hash_table_ops_t units_by_handle_ht_ops = {
|
---|
72 | .hash = &units_by_handle_ht_hash,
|
---|
73 | .key_hash = &units_by_handle_ht_key_hash,
|
---|
74 | .equal = &units_by_handle_ht_equal,
|
---|
75 | .key_equal = &units_by_handle_ht_key_equal,
|
---|
76 | .remove_callback = NULL
|
---|
77 | };
|
---|
78 |
|
---|
79 | static size_t units_by_name_ht_hash(const ht_link_t *item)
|
---|
80 | {
|
---|
81 | unit_t *unit =
|
---|
82 | hash_table_get_inst(item, unit_t, units_by_name);
|
---|
83 | return hash_string(unit->name);
|
---|
84 | }
|
---|
85 |
|
---|
86 | static size_t units_by_name_ht_key_hash(void *key)
|
---|
87 | {
|
---|
88 | return hash_string((const char *)key);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static bool units_by_name_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
|
---|
92 | {
|
---|
93 | return
|
---|
94 | hash_table_get_inst(item1, unit_t, units_by_handle) ==
|
---|
95 | hash_table_get_inst(item2, unit_t, units_by_handle);
|
---|
96 | }
|
---|
97 |
|
---|
98 | static bool units_by_name_ht_key_equal(void *key, const ht_link_t *item)
|
---|
99 | {
|
---|
100 | return str_cmp((const char *)key,
|
---|
101 | hash_table_get_inst(item, unit_t, units_by_name)->name) == 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | static hash_table_ops_t units_by_name_ht_ops = {
|
---|
106 | .hash = &units_by_name_ht_hash,
|
---|
107 | .key_hash = &units_by_name_ht_key_hash,
|
---|
108 | .equal = &units_by_name_ht_equal,
|
---|
109 | .key_equal = &units_by_name_ht_key_equal,
|
---|
110 | .remove_callback = NULL
|
---|
111 | };
|
---|
112 |
|
---|
113 | /* Repository functions */
|
---|
114 |
|
---|
115 | static void repo_remove_unit_internal(unit_t *u)
|
---|
116 | {
|
---|
117 | hash_table_remove_item(&units_by_name, &u->units_by_name);
|
---|
118 | hash_table_remove_item(&units_by_handle, &u->units_by_handle);
|
---|
119 | list_remove(&u->units);
|
---|
120 |
|
---|
121 | // TODO decrease refcount of unit
|
---|
122 | // unit may be referenced e.g. from running job, thus we cannot simply destroy it
|
---|
123 | }
|
---|
124 |
|
---|
125 | void repo_init(void)
|
---|
126 | {
|
---|
127 | hash_table_create(&units_by_name, 0, 0, &units_by_name_ht_ops);
|
---|
128 | hash_table_create(&units_by_handle, 0, 0, &units_by_handle_ht_ops);
|
---|
129 | }
|
---|
130 |
|
---|
131 | int repo_add_unit(unit_t *unit)
|
---|
132 | {
|
---|
133 | assert(unit);
|
---|
134 | assert(unit->repo_state == REPO_EMBRYO);
|
---|
135 | assert(unit->handle == 0);
|
---|
136 | assert(unit->name != NULL);
|
---|
137 | sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
|
---|
138 |
|
---|
139 | if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
|
---|
140 | /* Pointers are same size as unit_handle_t both on 32b and 64b */
|
---|
141 | unit->handle = (unit_handle_t)unit;
|
---|
142 |
|
---|
143 | hash_table_insert(&units_by_handle, &unit->units_by_handle);
|
---|
144 | list_append(&unit->units, &units);
|
---|
145 | return EOK;
|
---|
146 | } else {
|
---|
147 | return EEXISTS;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | int repo_remove_unit(unit_t *unit)
|
---|
152 | {
|
---|
153 | unit->repo_state = REPO_ZOMBIE;
|
---|
154 | return EOK; /* We could check that unit is present in repo etc... */
|
---|
155 | }
|
---|
156 |
|
---|
157 | void repo_begin_update(void) {
|
---|
158 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
159 | }
|
---|
160 |
|
---|
161 | static bool repo_commit_unit(ht_link_t *ht_link, void *arg)
|
---|
162 | {
|
---|
163 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
164 | if (unit->repo_state == REPO_ZOMBIE) {
|
---|
165 | repo_remove_unit_internal(unit);
|
---|
166 | return true;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (unit->repo_state == REPO_EMBRYO) {
|
---|
170 | unit->repo_state = REPO_LIVING;
|
---|
171 | }
|
---|
172 |
|
---|
173 | list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
|
---|
174 | e->commited = true;
|
---|
175 | }
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Marks newly added units_by_name as usable (via state change) */
|
---|
180 | void repo_commit(void)
|
---|
181 | {
|
---|
182 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Apply commit to all units_by_name, each commited unit commits its
|
---|
186 | * outgoing deps, thus eventually commiting all embryo deps as well.
|
---|
187 | *
|
---|
188 | * TODO why not iterate over units list?
|
---|
189 | */
|
---|
190 | hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
|
---|
191 | }
|
---|
192 |
|
---|
193 | static bool repo_rollback_unit(ht_link_t *ht_link, void *arg)
|
---|
194 | {
|
---|
195 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
196 |
|
---|
197 | list_foreach_safe(unit->edges_out, cur_link, next_link) {
|
---|
198 | unit_edge_t *e =
|
---|
199 | list_get_instance(cur_link, unit_edge_t, edges_out);
|
---|
200 | if (!e->commited) {
|
---|
201 | edge_remove(&e);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (unit->repo_state == REPO_EMBRYO) {
|
---|
206 | repo_remove_unit_internal(unit);
|
---|
207 | } else if (unit->repo_state == REPO_ZOMBIE) {
|
---|
208 | unit->repo_state = REPO_LIVING;
|
---|
209 | }
|
---|
210 |
|
---|
211 | return true;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Remove all uncommited units_by_name and edges from configuratio
|
---|
215 | *
|
---|
216 | * Memory used by removed object is released.
|
---|
217 | */
|
---|
218 | void repo_rollback(void)
|
---|
219 | {
|
---|
220 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
221 |
|
---|
222 | hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
|
---|
223 | }
|
---|
224 |
|
---|
225 | static bool repo_resolve_unit(ht_link_t *ht_link, void *arg)
|
---|
226 | {
|
---|
227 | bool *has_error_ptr = arg;
|
---|
228 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
229 |
|
---|
230 | list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
|
---|
231 | assert(e->input == unit);
|
---|
232 | assert((e->output != NULL) != (e->output_name != NULL));
|
---|
233 | if (e->output) {
|
---|
234 | continue;
|
---|
235 | }
|
---|
236 |
|
---|
237 | unit_t *output =
|
---|
238 | repo_find_unit_by_name(e->output_name);
|
---|
239 | if (output == NULL) {
|
---|
240 | sysman_log(LVL_ERROR,
|
---|
241 | "Cannot resolve dependency of '%s' to unit '%s'",
|
---|
242 | unit_name(unit), e->output_name);
|
---|
243 | *has_error_ptr = true;
|
---|
244 | // TODO should we just leave the sprout untouched?
|
---|
245 | } else {
|
---|
246 | edge_resolve_output(e, output);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | return true;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /** Resolve unresolved dependencies between any pair of units_by_name
|
---|
254 | *
|
---|
255 | * @return EOK on success
|
---|
256 | * @return ENOENT when one or more resolution fails, information is logged
|
---|
257 | */
|
---|
258 | int repo_resolve_references(void)
|
---|
259 | {
|
---|
260 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
261 |
|
---|
262 | bool has_error = false;
|
---|
263 | hash_table_apply(&units_by_name, &repo_resolve_unit, &has_error);
|
---|
264 |
|
---|
265 | return has_error ? ENOENT : EOK;
|
---|
266 | }
|
---|
267 |
|
---|
268 | unit_t *repo_find_unit_by_name(const char *name)
|
---|
269 | {
|
---|
270 | ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
|
---|
271 | if (ht_link != NULL) {
|
---|
272 | return hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
273 | } else {
|
---|
274 | return NULL;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | unit_t *repo_find_unit_by_handle(unit_handle_t handle)
|
---|
279 | {
|
---|
280 | ht_link_t *ht_link = hash_table_find(&units_by_handle, &handle);
|
---|
281 | if (ht_link != NULL) {
|
---|
282 | return hash_table_get_inst(ht_link, unit_t, units_by_handle);
|
---|
283 | } else {
|
---|
284 | return NULL;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|