[09a8006] | 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 |
|
---|
[bb154c6] | 29 | #include <adt/hash.h>
|
---|
| 30 | #include <adt/hash_table.h>
|
---|
[f42ee6f] | 31 | #include <adt/list.h>
|
---|
[241f1985] | 32 | #include <str.h>
|
---|
[f42ee6f] | 33 | #include <assert.h>
|
---|
| 34 | #include <errno.h>
|
---|
| 35 | #include <fibril_synch.h>
|
---|
| 36 |
|
---|
[af92309] | 37 | #include "repo.h"
|
---|
[9532981] | 38 | #include "edge.h"
|
---|
[6efec7e3] | 39 | #include "log.h"
|
---|
[f42ee6f] | 40 |
|
---|
[015b147] | 41 | LIST_INITIALIZE(units_);
|
---|
[dda2602] | 42 |
|
---|
| 43 | static hash_table_t units_by_name;
|
---|
[b55f62a] | 44 | static hash_table_t units_by_handle;
|
---|
[ff20afc] | 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);
|
---|
[bb154c6] | 49 |
|
---|
| 50 | /* Hash table functions */
|
---|
[b55f62a] | 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 |
|
---|
[241f1985] | 58 | static size_t units_by_handle_ht_key_hash(const void *key)
|
---|
[b55f62a] | 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 |
|
---|
[241f1985] | 70 | static bool units_by_handle_ht_key_equal(const void *key, const ht_link_t *item)
|
---|
[b55f62a] | 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,
|
---|
[8ae8262] | 81 | .remove_callback = NULL
|
---|
[b55f62a] | 82 | };
|
---|
| 83 |
|
---|
[dda2602] | 84 | static size_t units_by_name_ht_hash(const ht_link_t *item)
|
---|
[bb154c6] | 85 | {
|
---|
| 86 | unit_t *unit =
|
---|
[dda2602] | 87 | hash_table_get_inst(item, unit_t, units_by_name);
|
---|
[bb154c6] | 88 | return hash_string(unit->name);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[241f1985] | 91 | static size_t units_by_name_ht_key_hash(const void *key)
|
---|
[bb154c6] | 92 | {
|
---|
| 93 | return hash_string((const char *)key);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[dda2602] | 96 | static bool units_by_name_ht_equal(const ht_link_t *item1, const ht_link_t *item2)
|
---|
[bb154c6] | 97 | {
|
---|
[b55f62a] | 98 | return
|
---|
| 99 | hash_table_get_inst(item1, unit_t, units_by_handle) ==
|
---|
| 100 | hash_table_get_inst(item2, unit_t, units_by_handle);
|
---|
[bb154c6] | 101 | }
|
---|
| 102 |
|
---|
[241f1985] | 103 | static bool units_by_name_ht_key_equal(const void *key, const ht_link_t *item)
|
---|
[bb154c6] | 104 | {
|
---|
| 105 | return str_cmp((const char *)key,
|
---|
[dda2602] | 106 | hash_table_get_inst(item, unit_t, units_by_name)->name) == 0;
|
---|
[bb154c6] | 107 | }
|
---|
| 108 |
|
---|
[dda2602] | 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,
|
---|
[8ae8262] | 114 | .remove_callback = NULL
|
---|
[bb154c6] | 115 | };
|
---|
| 116 |
|
---|
[af92309] | 117 | /* Repository functions */
|
---|
[f42ee6f] | 118 |
|
---|
[8ae8262] | 119 | static void repo_remove_unit_internal(unit_t *u)
|
---|
| 120 | {
|
---|
[015b147] | 121 | assert(fibril_rwlock_is_write_locked(&repo_lock));
|
---|
| 122 |
|
---|
[8ae8262] | 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 |
|
---|
[015b147] | 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);
|
---|
[102f641] | 134 | if (lock)
|
---|
| 135 | fibril_rwlock_read_lock(&repo_lock);
|
---|
[015b147] | 136 | ht_link_t *ht_link = hash_table_find(&units_by_name, (void *)name);
|
---|
[102f641] | 137 | if (lock)
|
---|
| 138 | fibril_rwlock_read_unlock(&repo_lock);
|
---|
[015b147] | 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 |
|
---|
[af92309] | 147 | void repo_init(void)
|
---|
[f42ee6f] | 148 | {
|
---|
[dda2602] | 149 | hash_table_create(&units_by_name, 0, 0, &units_by_name_ht_ops);
|
---|
[b55f62a] | 150 | hash_table_create(&units_by_handle, 0, 0, &units_by_handle_ht_ops);
|
---|
[f42ee6f] | 151 | }
|
---|
| 152 |
|
---|
[25697163] | 153 | errno_t repo_add_unit(unit_t *unit)
|
---|
[f42ee6f] | 154 | {
|
---|
| 155 | assert(unit);
|
---|
[8ae8262] | 156 | assert(unit->repo_state == REPO_EMBRYO);
|
---|
[b55f62a] | 157 | assert(unit->handle == 0);
|
---|
[bb154c6] | 158 | assert(unit->name != NULL);
|
---|
[015b147] | 159 | assert(fibril_rwlock_is_write_locked(&repo_lock));
|
---|
[bb154c6] | 160 | sysman_log(LVL_DEBUG2, "%s('%s')", __func__, unit_name(unit));
|
---|
[f42ee6f] | 161 |
|
---|
[dda2602] | 162 | if (hash_table_insert_unique(&units_by_name, &unit->units_by_name)) {
|
---|
[b55f62a] | 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);
|
---|
[015b147] | 167 | list_append(&unit->units, &units_);
|
---|
[bb154c6] | 168 | return EOK;
|
---|
| 169 | } else {
|
---|
[241f1985] | 170 | return EEXIST;
|
---|
[bb154c6] | 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[25697163] | 174 | errno_t repo_remove_unit(unit_t *unit)
|
---|
[8ae8262] | 175 | {
|
---|
| 176 | unit->repo_state = REPO_ZOMBIE;
|
---|
| 177 | return EOK; /* We could check that unit is present in repo etc... */
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[102f641] | 180 | void repo_begin_update(void)
|
---|
| 181 | {
|
---|
[bb154c6] | 182 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
[015b147] | 183 | fibril_rwlock_write_lock(&repo_lock);
|
---|
[bb154c6] | 184 | }
|
---|
| 185 |
|
---|
[af92309] | 186 | static bool repo_commit_unit(ht_link_t *ht_link, void *arg)
|
---|
[bb154c6] | 187 | {
|
---|
[dda2602] | 188 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
[8ae8262] | 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;
|
---|
[bb154c6] | 196 | }
|
---|
| 197 |
|
---|
[9532981] | 198 | list_foreach(unit->edges_out, edges_out, unit_edge_t, e) {
|
---|
| 199 | e->commited = true;
|
---|
[bb154c6] | 200 | }
|
---|
| 201 | return true;
|
---|
[f42ee6f] | 202 | }
|
---|
| 203 |
|
---|
[dda2602] | 204 | /** Marks newly added units_by_name as usable (via state change) */
|
---|
[af92309] | 205 | void repo_commit(void)
|
---|
[f42ee6f] | 206 | {
|
---|
[6efec7e3] | 207 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
| 208 |
|
---|
[bb154c6] | 209 | /*
|
---|
[8ae8262] | 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?
|
---|
[bb154c6] | 214 | */
|
---|
[af92309] | 215 | hash_table_apply(&units_by_name, &repo_commit_unit, NULL);
|
---|
[015b147] | 216 | fibril_rwlock_write_unlock(&repo_lock);
|
---|
[bb154c6] | 217 | }
|
---|
| 218 |
|
---|
[af92309] | 219 | static bool repo_rollback_unit(ht_link_t *ht_link, void *arg)
|
---|
[bb154c6] | 220 | {
|
---|
[dda2602] | 221 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
[bb154c6] | 222 |
|
---|
[9532981] | 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);
|
---|
[bb154c6] | 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[8ae8262] | 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;
|
---|
[bb154c6] | 235 | }
|
---|
| 236 |
|
---|
| 237 | return true;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[dda2602] | 240 | /** Remove all uncommited units_by_name and edges from configuratio
|
---|
[5559712] | 241 | *
|
---|
| 242 | * Memory used by removed object is released.
|
---|
| 243 | */
|
---|
[af92309] | 244 | void repo_rollback(void)
|
---|
[bb154c6] | 245 | {
|
---|
| 246 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
| 247 |
|
---|
[af92309] | 248 | hash_table_apply(&units_by_name, &repo_rollback_unit, NULL);
|
---|
[015b147] | 249 | fibril_rwlock_write_unlock(&repo_lock);
|
---|
[bb154c6] | 250 | }
|
---|
| 251 |
|
---|
[af92309] | 252 | static bool repo_resolve_unit(ht_link_t *ht_link, void *arg)
|
---|
[bb154c6] | 253 | {
|
---|
| 254 | bool *has_error_ptr = arg;
|
---|
[dda2602] | 255 | unit_t *unit = hash_table_get_inst(ht_link, unit_t, units_by_name);
|
---|
[bb154c6] | 256 |
|
---|
[9532981] | 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) {
|
---|
[bb154c6] | 261 | continue;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[9532981] | 264 | unit_t *output =
|
---|
[015b147] | 265 | repo_find_unit_by_name_unsafe(e->output_name);
|
---|
[9532981] | 266 | if (output == NULL) {
|
---|
[bb154c6] | 267 | sysman_log(LVL_ERROR,
|
---|
| 268 | "Cannot resolve dependency of '%s' to unit '%s'",
|
---|
[9532981] | 269 | unit_name(unit), e->output_name);
|
---|
[bb154c6] | 270 | *has_error_ptr = true;
|
---|
| 271 | // TODO should we just leave the sprout untouched?
|
---|
| 272 | } else {
|
---|
[9532981] | 273 | edge_resolve_output(e, output);
|
---|
[f42ee6f] | 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[bb154c6] | 277 | return true;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[aa0faeca] | 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 |
|
---|
[dda2602] | 301 | /** Resolve unresolved dependencies between any pair of units_by_name
|
---|
[bb154c6] | 302 | *
|
---|
| 303 | * @return EOK on success
|
---|
[5559712] | 304 | * @return ENOENT when one or more resolution fails, information is logged
|
---|
[bb154c6] | 305 | */
|
---|
[25697163] | 306 | errno_t repo_resolve_references(void)
|
---|
[bb154c6] | 307 | {
|
---|
| 308 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
| 309 |
|
---|
| 310 | bool has_error = false;
|
---|
[af92309] | 311 | hash_table_apply(&units_by_name, &repo_resolve_unit, &has_error);
|
---|
[bb154c6] | 312 |
|
---|
[aa0faeca] | 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 |
|
---|
[bb154c6] | 319 | return has_error ? ENOENT : EOK;
|
---|
[f42ee6f] | 320 | }
|
---|
[bb154c6] | 321 |
|
---|
[015b147] | 322 | /**
|
---|
| 323 | * The function can be safely called from non-event loop fibrils
|
---|
| 324 | */
|
---|
[be07995] | 325 | unit_t *repo_find_unit_by_name(const char *name)
|
---|
[bb154c6] | 326 | {
|
---|
[015b147] | 327 | return repo_find_unit_by_name_internal(name, true);
|
---|
| 328 | }
|
---|
[ff20afc] | 329 |
|
---|
[015b147] | 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);
|
---|
[bb154c6] | 336 | }
|
---|
| 337 |
|
---|
[015b147] | 338 | /**
|
---|
| 339 | * The function can be safely called from non-event loop fibrils
|
---|
| 340 | */
|
---|
[af92309] | 341 | unit_t *repo_find_unit_by_handle(unit_handle_t handle)
|
---|
[b55f62a] | 342 | {
|
---|
[015b147] | 343 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
[ff20afc] | 344 | fibril_rwlock_read_lock(&repo_lock);
|
---|
[b55f62a] | 345 | ht_link_t *ht_link = hash_table_find(&units_by_handle, &handle);
|
---|
[ff20afc] | 346 | fibril_rwlock_read_unlock(&repo_lock);
|
---|
| 347 |
|
---|
[b55f62a] | 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 |
|
---|
[ff20afc] | 355 | void repo_rlock(void)
|
---|
| 356 | {
|
---|
[015b147] | 357 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
[ff20afc] | 358 | fibril_rwlock_read_lock(&repo_lock);
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | void repo_runlock(void)
|
---|
| 362 | {
|
---|
[015b147] | 363 | sysman_log(LVL_DEBUG2, "%s", __func__);
|
---|
[ff20afc] | 364 | fibril_rwlock_read_unlock(&repo_lock);
|
---|
| 365 | }
|
---|