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