*
*
*/
+#define WITH_C99 /* For ul_gsa.h iterators */
#include <semaphore.h>
#include <getopt.h>
#include <forb.h>
#include <errno.h>
#include <stdio.h>
#include <ul_gavlcust.h>
+#include <ul_gsacust.h>
#include <string.h>
#include <ul_log.h>
#include <ul_logreg.h>
#include <forb/proto_inet.h>
#endif
+#define COMPILE_TIME_ASSERT(cond, msg) \
+ typedef char msg[(cond) ? 1 : -1]
+
+
UL_LOG_CUST(ulogd_fcb);
ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
fres_resource_allocator ra;
};
+struct fcb_transaction;
+
struct fcb_contract {
fres_contract_id_t id;
struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
gavl_node_t node_fcb;
ul_list_node_t node_sc;
ul_list_node_t node_reservation;
+ struct fcb_transaction *transaction;
struct {
int initial;
int try;
struct fres_contract *schedulable_contract;
};
+struct fcb_transaction {
+ char *name;
+ gavl_node_t node;
+ gsa_array_field_t contracts;
+};
+
/**
* Contract broker data
*/
struct fcb {
+ fres_contract_id_t contract_counter;
gavl_cust_root_field_t resources; /**< Registered resources */
gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
+ gavl_cust_root_field_t transactions; /**< Transactions negotiated by this FCB */
};
struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
free(fcb_contract);
}
+static inline int
+unsorted(const void *a, const void *b)
+{ abort(); }
+
+/* Unordered dynamic array of contracts in transaction */
+GSA_CUST_DEC(tran_contract /* cust_prefix */,
+ struct fcb_transaction /* cust_array_t */,
+ struct fcb_contract/* cust_item_t */,
+ fres_contract_id_t /* cust_key_t */,
+ contracts /* cust_array_field */,
+ id /* cust_item_key */,
+ unsorted /* cust_cmp_fnc */);
+
+GSA_CUST_IMP(tran_contract /* cust_prefix */,
+ struct fcb_transaction /* cust_array_t */,
+ struct fcb_contract/* cust_item_t */,
+ fres_contract_id_t /* cust_key_t */,
+ contracts /* cust_array_field */,
+ id /* cust_item_key */,
+ unsorted /* cust_cmp_fnc */,
+ true /* cust_ins_fl */);
+
+struct fcb_transaction *fcb_transaction_new(char *name)
+{
+ struct fcb_transaction *fcb_transaction;
+
+ fcb_transaction = malloc(sizeof(*fcb_transaction));
+ if (!fcb_transaction) {
+ return NULL;
+ }
+ memset(fcb_transaction, 0, sizeof(*fcb_transaction));
+ fcb_transaction->name = strdup(name);
+
+ tran_contract_init_array_field(fcb_transaction);
+
+ return fcb_transaction;
+}
+
+void fcb_transaction_destroy(struct fcb_transaction *fcb_transaction)
+{
+ free(fcb_transaction->name);
+ free(fcb_transaction);
+}
+
static inline int res_key_cmp(const struct res_key *a,
const struct res_key *b)
{
#include "fcb_contract_gavl.inc"
#endif
+static inline int
+fcb_transaction_cmp(char * const *a, char * const *b)
+{ return strcmp(*a, *b); }
+
+GAVL_CUST_NODE_INT_DEC(fcb_transaction /* cust_prefix */, \
+ struct fcb /* cust_root_t */, \
+ struct fcb_transaction /* cust_item_t */, \
+ char* /* cust_key_t */, \
+ transactions /* cust_root_node */, \
+ node /* cust_item_node */, \
+ name /* cust_item_key */, \
+ fcb_transaction_cmp /* cust_cmp_fnc */);
+
+GAVL_CUST_NODE_INT_IMP(fcb_transaction /* cust_prefix */, \
+ struct fcb /* cust_root_t */, \
+ struct fcb_transaction /* cust_item_t */, \
+ char* /* cust_key_t */, \
+ transactions /* cust_root_node */, \
+ node /* cust_item_node */, \
+ name /* cust_item_key */, \
+ fcb_transaction_cmp /* cust_cmp_fnc */);
+
+struct res_array {
+ gsa_array_field_t array;
+};
+
+GSA_CUST_DEC(res_array /* cust_prefix */,
+ struct res_array /* cust_array_t */,
+ struct resource /* cust_item_t */,
+ struct res_key /* cust_key_t */,
+ array /* cust_array_field */,
+ key /* cust_item_key */,
+ res_key_cmp /* cust_cmp_fnc */);
+
+GSA_CUST_IMP(res_array /* cust_prefix */,
+ struct res_array /* cust_array_t */,
+ struct resource /* cust_item_t */,
+ struct res_key /* cust_key_t */,
+ array /* cust_array_field */,
+ key /* cust_item_key */,
+ res_key_cmp /* cust_cmp_fnc */,
+ true /* cust_ins_fl */);
+
+
#define o2fcb(o) (struct fcb*)forb_instance_data(o)
if (fres_contract_id_is_empty(&c->id)) {
/* Normal negotiation request */
- forb_uuid_generate((forb_uuid_t *)&c->id);
+ COMPILE_TIME_ASSERT(sizeof(c->id) == sizeof(fcb->contract_counter),
+ wrong_size_of_contract_id);
+ c->id = ++fcb->contract_counter;
fc = fcb_contract_new(&c->id);
if (!fc)
return errno;
}
}
+static int
+reserve_resource(struct resource *resource)
+{
+ int ret, i;
+ fres_contract_ptr_seq contracts;
+ struct fres_contract *c;
+ struct fcb_contract *fc;
+ CORBA_Environment ev;
+
+ if (!forb_sequence_alloc_buf(&contracts, resource->rl.length)) {
+ ret = errno;
+ goto err;
+ }
+ forb_sequence_length(&contracts) = resource->rl.length;
+
+ i=0;
+ /* Prepare FORB sequence */
+ ul_list_for_each(reservation_list, &resource->rl, fc) {
+ c = NULL;
+ if (fc->to_be_reserved_contract)
+ /* Contract without spare capacity */
+ c = fc->to_be_reserved_contract;
+ else if (fc->requested_contract)
+ c = fc->requested_contract;
+ assert(c);
+ forb_sequence_elem(&contracts, i++) = c;
+ }
+
+ /* Reserve contract */
+ ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
+ if (forb_exception_occurred(&ev)) {
+ ret = fres_forbex2err(&ev);
+ ul_logerr("FORB exception when reserving contracts\n");
+ goto err_free;
+ }
+ if (ret < 0) {
+ ul_logerr("Contract reservation error %d\n", ret);
+ ret = FRES_ERR_ADMISSION_TEST;
+ goto err_free;
+ } else if (ret == 0)
+ ret = FRSH_NO_ERROR;
+ else if (ret == 1)
+ ret = FRSH_ERR_CONTRACT_REJECTED;
+ else
+ assert(false);
+err_free:
+ forb_sequence_free_buf(&contracts, forb_no_destructor);
+err:
+ return ret;
+}
+
/**
*
*
rebalance_spare_capacity_and_reserve(struct resource *resource)
{
int ret;
- unsigned i;
struct reservation_list *rl = &resource->rl;
struct fcb_contract *fc;
fres_block_spare_capacity *s;
- fres_contract_ptr_seq contracts;
- if (!forb_sequence_alloc_buf(&contracts, rl->length)) {
- return errno;
- }
- contracts._length = rl->length;
- i=0;
/* Initialize optimization */
ul_list_for_each(reservation_list, rl, fc) {
fc->to_be_reserved_contract =
NULL;
assert(fc->to_be_reserved_contract != NULL);
- forb_sequence_elem(&contracts, i) = fc->to_be_reserved_contract;
- i++;
-
s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
if (s && s->granularity == FRSH_GR_DISCRETE) {
fc->sc_variant.initial = s->variants._length - 1;
}
if (criterion > best_criterion) {
- CORBA_Environment ev;
- /* Reserve contract */
- ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
- if (forb_exception_occurred(&ev)) {
- ret = fres_forbex2err(&ev);
- ul_logerr("FORB exception when reserving contracts\n");
- goto err;
- }
- if (ret < 0) {
- ul_logerr("Contract reservation error %d\n", ret);
- ret = FRES_ERR_ADMISSION_TEST;
- goto err;
- }
- if (ret == 0) { /* negotiation succeeded */
+ ret = reserve_resource(resource);
+ switch (ret) {
+ case FRSH_NO_ERROR:
best_criterion = criterion;
+ break;
+ case FRSH_ERR_CONTRACT_REJECTED:
+ break;
+ default:
+ goto err;
}
}
} while (!all_combinations_tried);
fres_contract_destroy(fc->to_be_reserved_contract);
fc->to_be_reserved_contract = NULL;
}
- forb_sequence_free_buf(&contracts, forb_no_destructor);
return ret;
}
}
vreses._buffer[vreses._length] = sc;
vreses._length++;
- fres_contract_destroy(fc->schedulable_contract);
- fc->schedulable_contract = fres_contract_duplicate(sc);
last_ra = fc->ra;
}
ret = fres_forbex2err(&ev);
goto err_free;
}
+
return 0;
err_free:
forb_sequence_free_buf(&commit_ids, forb_no_destructor);
return ret;
}
+/* Add new contracts to our fcb database for later
+ * reference. Canceled contracts are removed below. */
+int
+fcb_remember_contracts(struct fcb *fcb,
+ struct fcb_contract **fcb_contracts, int num)
+{
+ struct fcb_contract *fc;
+ int i;
+
+ for (i=0; i<num; i++) {
+ fc = fcb_contracts[i];
+
+ if (fc->user_contract) {
+ if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
+ /* Renegotiation */
+ fres_contract_destroy(fc->user_contract);
+ fc->user_contract = fres_contract_duplicate(fc->requested_contract);
+ /* Note: requested_contract is also
+ * pointed by contracts parameter and
+ * will be freed by FORB. */
+ fc->requested_contract = NULL;
+ }
+ } else {
+ /* Insert new contracts */
+ fcb_contract_insert(fcb, fcb_contracts[i]);
+ fc->user_contract = fres_contract_duplicate(fc->requested_contract);
+ fc->requested_contract = NULL;
+ /* See the note above. */
+ }
+ }
+ return 0;
+}
+
+int
+fcb_remember_schedulable_contracts(struct fcb *fcb,
+ fres_contract_ptr_seq *schedulable_contracts)
+{
+ struct fcb_contract *fc;
+ int i;
+ for (i=0; i<schedulable_contracts->_length; i++) {
+ struct fres_contract *sc = schedulable_contracts->_buffer[i];
+ fc = fcb_contract_find(fcb, &sc->id);
+ assert(fc != NULL);
+ fres_contract_destroy(fc->schedulable_contract);
+ fc->schedulable_contract = fres_contract_duplicate(sc);
+ assert(fc->schedulable_contract);
+ }
+ return 0;
+}
+
+static int
+reject_transactions(struct fcb_contract *fcb_contracts[], int num)
+{
+ int i;
+ for (i = 0; i < num; i++) {
+ if (fcb_contracts[i]->transaction)
+ return FRES_ERR_VRES_PART_OF_TRANSACTION;
+ }
+ return 0;
+}
CORBA_long
negotiate_contracts(fres_contract_broker obj,
int ret = 0;
forb_server_id app;
fres_contract_ptr_seq *schedulable_contracts;
- struct fcb_contract **fcb_contracts, *fc;
+ struct fcb_contract **fcb_contracts;
unsigned i;
int num = contracts->_length;
contracts->_buffer, num);
if (ret)
goto err_free_fcb_contracts;
+
+ ret = reject_transactions(fcb_contracts, num);
+ if (ret)
+ goto err_free_fcb_contracts;
ret = check_and_setup_resource(fcb, fcb_contracts, &resource,
&app, num);
if (ret)
if (ret)
goto err_cancel_reservation;
- /* Add new contracts to our fcb database for later
- * reference. Canceled contracts are removed below. */
- for (i=0; i<num; i++) {
- fc = fcb_contracts[i];
-
- if (fc->user_contract) {
- if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
- /* Renegotiation */
- fres_contract_destroy(fc->user_contract);
- fc->user_contract = fres_contract_duplicate(fc->requested_contract);
- /* Note: requested_contract is also
- * pointed by contracts parameter and
- * will be freed by FORB. */
- fc->requested_contract = NULL;
- }
- } else {
- /* Insert new contracts */
- fcb_contract_insert(fcb, fcb_contracts[i]);
- fc->user_contract = fres_contract_duplicate(fc->requested_contract);
- fc->requested_contract = NULL;
- /* See the note above. */
- }
- }
+ fcb_remember_contracts(fcb, fcb_contracts, num);
+ fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
ret = change_vreses(fcb, schedulable_contracts);
if (ret)
static int
transaction_get_resources(struct fcb *fcb, struct fcb_contract *fc[],
- int num, struct resource **resources[])
+ int num, struct res_array *res_array)
{
- int i, j, r, alloc;
- struct resource **res;
+ int i;
struct resource *resource;
struct res_key key;
+ int ret;
- alloc = 10;
- res = malloc(alloc*sizeof(*res));
- if (!res)
- return errno;
- res[0] = NULL;
- for (r = 0, i = 0; i < num; i++) {
+ for (i = 0; i < num; i++) {
get_fc_res_key(fc[i], &key);
resource = fcb_resource_find(fcb, &key);
- /* TODO: Check whether to use GSA for resources. */
+ ret = res_array_insert(res_array, resource);
+ if (ret == -1) {
+ ret = FRSH_ERR_INTERNAL_ERROR;
+ goto err;
+ }
}
- res[r] = NULL;
- return 0;
+ ret = 0;
+err:
+ return ret;
}
-
-
CORBA_long
negotiate_transaction(fres_contract_broker _obj,
const fres_transaction_t* transaction,
CORBA_Environment *ev)
{
struct fcb *fcb = o2fcb(_obj);
- struct fcb_contract **fcb_contracts;
- const fres_contract_ptr_seq* contracts = &transaction->contracts;
- int num = contracts->_length;
- int ret;
+ struct fcb_contract **fcb_contracts, *fc;
+ const fres_contract_ptr_seq* user_contracts = &transaction->contracts;
+ int i, ret, num = user_contracts->_length;
+ struct res_array res_array;
+ struct res_key key;
+ struct resource *resource;
+ fres_contract_ptr_seq *schedulable_contracts;
+ struct fcb_transaction *ft;
+ ul_logmsg("Negotiating transaction '%s' with %d contracts\n", transaction->name, num);
if (transaction_has_spare_capacity(transaction)) {
ret = FRES_ERR_SPARE_CAPACITY_NOT_SUPPORTED;
goto err;
memset(fcb_contracts, 0, sizeof(*fcb_contracts)*num);
ret = prepare_fcb_contracts(fcb, fcb_contracts,
- contracts->_buffer, num);
+ user_contracts->_buffer, num);
if (ret)
goto err_free_fcb_contracts;
- //ret = transaction_get_resources(fcb_contracts, num, &resources);
-
- return FRSH_ERR_NOT_IMPLEMENTED;
+ res_array_init_array_field(&res_array);
+ ret = transaction_get_resources(fcb, fcb_contracts, num, &res_array);
+
+ gsa_cust_for_each(res_array, &res_array, resource) {
+ reservation_list_init_head(&resource->rl);
+ resource->rl.length = 0;
+ for (i = 0; i < num; i++) {
+ fc = fcb_contracts[i];
+ get_fc_res_key(fc, &key);
+ if (res_key_cmp(&key, &resource->key) == 0) {
+ reservation_list_insert(&resource->rl, fc);
+ resource->rl.length++;
+ }
+ }
+ }
+ gsa_cust_for_each(res_array, &res_array, resource) {
+ ret = reserve_resource(resource);
+ if (ret) {
+ ul_logerr("Reservation failed\n");
+ goto err_cancel_reservation;
+ }
+ }
+ fcb_remember_contracts(fcb, fcb_contracts, num);
+ gsa_cust_for_each(res_array, &res_array, resource) {
+ ret = commit_resource(resource, &schedulable_contracts);
+ if (ret) {
+ ul_logerr("Commit failed\n");
+ goto err_cancel_reservation;
+ }
+ fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
+ forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
+ }
+ /* TODO: After we distinguish between APP and a real
+ * allocator, allocate VRESes (and perform mode change) here.
+ * Only if the resource allocator is not known at this point,
+ * wait with allocation the same way as it is done now. For
+ * this we will need more information about allocators i.e.
+ * its type (centralized, distributed, in app, ...). Too
+ * compilcated ;-( */
+
+ ft = fcb_transaction_new(transaction->name);
+ if (!ft) {
+ ret = errno;
+ goto err_cancel_reservation;
+ }
+ enum { NONE, CANCELATION, NEGOTIATION } op = NONE;
+ for (i = 0; i < num; i++) {
+ fc = fcb_contracts[i];
+ if (fc->requested_contract &&
+ fres_contract_get_num_blocks(fc->requested_contract) == 0) {
+ assert(i == 0 || op == CANCELATION);
+ op = CANCELATION;
+ /* TODO: Review and test transaction cancelation!!! */
+ fcb_contract_delete(fcb, fc);
+ /* Note: requested_contract is also pointed by
+ * contracts parameter and will be freed by FORB. */
+ fc->requested_contract = NULL;
+ fcb_contract_destroy(fc);
+ } else {
+ assert(i == 0 || op == NEGOTIATION);
+ op = NEGOTIATION;
+ tran_contract_insert_at(ft, fc, i);
+ fc->transaction = ft;
+ }
+ }
+ switch (op) {
+ case NEGOTIATION:
+ fcb_transaction_insert(fcb, ft);
+ break;
+ case CANCELATION:
+ fcb_transaction_delete(fcb, ft);
+ fcb_transaction_destroy(ft);
+ break;
+ default:
+ assert(false);
+ }
+ res_array_delete_all(&res_array);
+ return 0;
+err_cancel_reservation:
+ gsa_cust_for_each(res_array, &res_array, resource) {
+ cancel_reservations(resource);
+ }
+ res_array_delete_all(&res_array);
err_free_fcb_contracts:
free_fcb_contracts(fcb_contracts, num);
err:
CORBA_long
allocate_transaction_vres(fres_contract_broker _obj,
- const CORBA_long id,
+ const CORBA_char * name,
+ const CORBA_long index,
+ fres_contract_id_t *id,
CORBA_Environment *ev)
{
- return FRSH_ERR_NOT_IMPLEMENTED;
+ struct fcb *fcb = o2fcb(_obj);
+ struct fcb_transaction *ft;
+ struct fcb_contract *fc;
+ int ret;
+ struct res_alloc *ra;
+ char * const *n = (char * const *)&name;
+ struct res_key key;
+ struct resource *resource;
+ fres_contract_ptr_seq vreses;
+ CORBA_Environment ev2;
+ forb_server_id app;
+#define GOTO(label, error) do { ret = (error); goto label; } while(0)
+
+ ul_logmsg("Allocate transaction VRES '%s'[%d]\n", name, index);
+
+ ft = fcb_transaction_find(fcb, n);
+ if (!ft) GOTO(err, FRES_ERR_TRANSACTION_NOT_FOUND);
+
+ fc = tran_contract_indx2item(ft, index);
+ if (!fc) GOTO(err, FRSH_ERR_TOO_LARGE);
+
+ /* TODO: If we allow migration of task between resources (due
+ * to spare capacity reallocation), the following check will
+ * need reworking/enhancing. */
+ if (fc->ra) GOTO(err, FRES_ERR_VRES_ALREADY_ALLOCATED);
+
+ if (!get_fc_res_key(fc, &key))
+ GOTO(err, FRSH_ERR_RESOURCE_ID_INVALID);
+ resource = fcb_resource_find(fcb, &key);
+ if (!resource)
+ GOTO(err, FRES_ERR_NO_RESOURCE_MANAGER);
+
+ forb_get_req_source(_obj, &app);
+ /* Find allocator for the requested VRES */
+ ra = fcb_alloc_find(resource, &app);
+ if (!ra) {
+ char str[60];
+ forb_server_id_to_string(str, &app, sizeof(str));
+ ul_logerr("No resource allocator found for %d.%d and %s\n",
+ resource->key.type, resource->key.id, str);
+ GOTO(err, FRES_ERR_NO_RESOURCE_ALLOCATOR);
+ }
+ fc->ra = ra;
+
+ if (!forb_sequence_alloc_buf(&vreses, 1))
+ GOTO(err, errno);
+
+ assert(fc->schedulable_contract);
+ forb_sequence_elem(&vreses, 0) = fc->schedulable_contract;
+ forb_sequence_length(&vreses) = 1;
+ ret = fres_resource_allocator_change_vreses(ra->ra, &vreses, &ev2);
+ ul_logerr("Err = %d\n", ret);
+ if (forb_exception_occurred(&ev2))
+ GOTO(err_free, fres_forbex2err(&ev2));
+ if (ret)
+ goto err_free;
+ *id = fc->id;
+
+ ret = 0;
+err_free:
+ forb_sequence_free_buf(&vreses, forb_no_destructor);
+err:
+ return ret;
}
};
int opt;
- while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
+ while ((opt = getopt_long(argc, argv, "d::hl:", &long_opts[0], NULL)) != EOF) {
switch (opt) {
case 'l':
if (*optarg == '?') {
}
}
+ memset(&fcb_data, 0, sizeof(fcb_data));
fosa_clock_get_time(CLOCK_REALTIME, &start_time);
if (opt_daemon)
fcb_resource_init_root_field(&fcb_data);
fcb_contract_init_root_field(&fcb_data);
+ fcb_transaction_init_root_field(&fcb_data);
fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");