]> rtime.felk.cvut.cz Git - frescor/frsh.git/blobdiff - fres/cbroker/fcb.c
Unify parameters of forb_sequence_*()
[frescor/frsh.git] / fres / cbroker / fcb.c
index ead63e483a4ccbb74cbeb56c7f3a3a4ae872c001..f34d47f003ad5348d4667957410bfd65c26622ef 100644 (file)
  * 
  * 
  */
+#include <semaphore.h>
+#include <getopt.h>
 #include <forb.h>
+#include <forb/config.h>
 #include <fcb.h>
+#include <fcb_contact_info.h>
 #include <error.h>
 #include <errno.h>
 #include <stdio.h>
 #include <ul_gavlcust.h>
 #include <string.h>
 #include <ul_log.h>
+#include <ul_logreg.h>
 #include <forb/server_id.h>
 #include <fres_contract.h>
 #include "fcb_config.h"
 #include <fosa_clocks_and_timers.h>
-#ifdef CONFIG_FCB_INET
+#include "contract_log.h"
+#if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
 #include <forb/proto_inet.h>
 #endif
 
 UL_LOG_CUST(ulogd_fcb);
-ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "fcb"};
-
+ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
+UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
+       
+bool opt_daemon = false;
+char *opt_pidfile = NULL;
 fosa_abs_time_t start_time;
 
 
@@ -89,6 +98,7 @@ struct res_key {
 struct resource {
        gavl_node_t node;
        struct res_key key;
+       char *name;
        fres_resource_manager mng; /**< Object reference of the resource manager */
        gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
        ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
@@ -312,6 +322,7 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                        if (!get_res_key(c, &key)) {
                                return FRSH_ERR_RESOURCE_ID_INVALID;
                        }
+                       log_contract("Negotiation request", i, c);
                } else {
                        fc = fcb_contract_find(fcb, &c->id);
                        if (!fc) {
@@ -324,11 +335,13 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                                if (fres_contract_get_num_blocks(c) == 0) {
                                        /* Cancelation */
                                        get_res_key(fc->user_contract, &key);
+                                       log_contract("Cancelation request", i, fc->user_contract);
                                } else {
                                        /* Renegotiation */
                                        if (!get_res_key(c, &key)) {
                                                return FRSH_ERR_RESOURCE_ID_INVALID;
                                        }
+                                       log_contract("Renegotiation request", i, fc->user_contract);
                                }
                        }
                }
@@ -341,7 +354,7 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
                        if (!*resource) {
                                ul_logerr("No resource manager for %d.%d registered\n",
                                          key.type, key.id);
-                               return FRSH_ERR_RESOURCE_ID_INVALID;
+                               return FRES_ERR_NO_RESOURCE_MANAGER;
                        }
                }
                else if (key.type != key2.type ||
@@ -364,11 +377,6 @@ prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
        return 0;
 }
 
-static void fres_contract_ptr_destroy(struct fres_contract **p)
-{
-       fres_contract_destroy(*p);
-}
-
 /**
  * @param resource Resource for which to rebalance capacity and negotiate new contracts
  * @param fcb_contract New requests to negotiate
@@ -420,7 +428,9 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
        fres_block_spare_capacity *s;
 
        fres_contract_ptr_seq contracts;
-       forb_sequence_alloc_buf(contracts, rl->length);
+       if (!forb_sequence_alloc_buf(&contracts, rl->length)) {
+               return errno;
+       }
        contracts._length = rl->length;
        i=0;
        /* Initialize optimization */
@@ -431,7 +441,7 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
                        NULL;
                assert(fc->to_be_reserved_contract != NULL);
 
-               forb_sequence_elem(contracts, i) = fc->to_be_reserved_contract;
+               forb_sequence_elem(&contracts, i) = fc->to_be_reserved_contract;
                i++;
                
                s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
@@ -456,7 +466,7 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
                        /* TODO: Simulate continuous granularity by discretization */
                        if (s && s->granularity == FRSH_GR_DISCRETE) {
                                fres_container_copy(fc->to_be_reserved_contract->container,
-                                                   forb_sequence_elem(s->variants, fc->sc_variant.try));
+                                                   forb_sequence_elem(&s->variants, fc->sc_variant.try));
                                criterion += fc->sc_variant.try;
 
                                if (fcb_changed == NULL) {
@@ -481,7 +491,7 @@ rebalance_spare_capacity_and_reserve(struct resource *resource,
                        /* Reserve contract */
                        ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
                        if (forb_exception_occurred(&ev)) {
-                               ret = FRES_ERR_FORB_EXCEPTION;
+                               ret = fres_forbex2err(&ev);
                                ul_logerr("FORB exception when reserving contracts\n");
                                goto err;
                        }
@@ -514,7 +524,7 @@ err:
                fres_contract_destroy(fc->to_be_reserved_contract);
                fc->to_be_reserved_contract = NULL;
        }
-       forb_sequence_free_buf(contracts, forb_no_destructor);
+       forb_sequence_free_buf(&contracts, forb_no_destructor);
        return ret;
 }
 
@@ -532,16 +542,20 @@ change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
 {
        struct res_alloc *last_ra = NULL;
        fres_contract_ptr_seq vreses;
-       int i, ret;
+       int i, ret = 0;
        CORBA_Environment ev;
        
-       forb_sequence_alloc_buf(vreses, schedulable_contracts->_length);
+       if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
+               return errno;
+       }
        vreses._length = 0;
        
        for (i=0; i<schedulable_contracts->_length; i++) {
                struct fcb_contract *fc;
                struct fres_contract *sc = schedulable_contracts->_buffer[i];
 
+               log_contract("Changing VRES", i, sc);
+
                fc = fcb_contract_find(fcb, &sc->id);
                assert(fc != NULL);
 
@@ -551,7 +565,7 @@ change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
                                        ret = fres_resource_allocator_change_vreses(last_ra->ra,
                                                                                    &vreses, &ev);
                                        if (forb_exception_occurred(&ev)) {
-                                               ret = FRES_ERR_FORB_EXCEPTION;
+                                               ret = fres_forbex2err(&ev);
                                                goto err;
                                        }
                                        if (ret) goto err;
@@ -566,12 +580,14 @@ change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
                
                }
        }
-       ret = fres_resource_allocator_change_vreses(last_ra->ra,
-                                                   &vreses, &ev);
-       if (forb_exception_occurred(&ev))
-               ret = FRES_ERR_FORB_EXCEPTION;
+       if (last_ra) {
+               ret = fres_resource_allocator_change_vreses(last_ra->ra,
+                                                           &vreses, &ev);
+               if (forb_exception_occurred(&ev))
+                       ret = fres_forbex2err(&ev);
+       }
 err:
-       forb_sequence_free_buf(vreses, forb_no_destructor);
+       forb_sequence_free_buf(&vreses, forb_no_destructor);
        return ret;
 }
 
@@ -582,8 +598,8 @@ negotiate_contracts(fres_contract_broker obj,
                    CORBA_Environment *ev)
 {
        struct fcb *fcb = o2fcb(obj);
-       struct resource *resource;
-       int ret;
+       struct resource *resource = NULL;
+       int ret = 0;
        forb_server_id app;
        fres_contract_ptr_seq *schedulable_contracts;
        struct fcb_contract **fcb_contracts, *fc;
@@ -621,8 +637,7 @@ negotiate_contracts(fres_contract_broker obj,
        /* Allocate all the needed memory before doing reservation. If
         * there is no enough memory, it has no sense to call resource
         * manager. */
-       forb_sequence_alloc_buf(commit_ids, rl.length);
-       if (!commit_ids._buffer) {
+       if (!forb_sequence_alloc_buf(&commit_ids, rl.length)) {
                ret = errno;
                goto err_free_fcb_contracts;
        }
@@ -644,14 +659,14 @@ negotiate_contracts(fres_contract_broker obj,
        commit_ids._length = rl.length;
        i=0;
        ul_list_for_each(reservation_list, &rl, fc) {
-               forb_sequence_elem(commit_ids, i) = fc->id;
+               forb_sequence_elem(&commit_ids, i) = fc->id;
                i++;
        }
        
        fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
                                               &schedulable_contracts, ev);
        if (forb_exception_occurred(ev)) {
-               ret = FRES_ERR_FORB_EXCEPTION;
+               ret = fres_forbex2err(ev);
                goto err_cancel_reservation;
        }
 
@@ -691,15 +706,14 @@ negotiate_contracts(fres_contract_broker obj,
 
 
        /* Return IDs and delete canceled contracts from out database */
-       forb_sequence_alloc_buf(**ids_out, contracts->_length);
-       if (!(*ids_out)->_buffer) {
+       if (!forb_sequence_alloc_buf(*ids_out, contracts->_length)) {
                ev->major = FORB_EX_NO_MEMORY;
                goto err_cancel_contracts;
        }
        (*ids_out)->_length = contracts->_length;
        for (i=0; i<contracts->_length; i++) {
                struct fcb_contract *fc =  fcb_contracts[i];
-               forb_sequence_elem(**ids_out, i) = fc->id;
+               forb_sequence_elem(*ids_out, i) = fc->id;
 
                if (fc->requested_contract &&
                    fres_contract_get_num_blocks(fc->requested_contract) == 0) {
@@ -789,11 +803,12 @@ CORBA_long register_resource(fres_contract_broker obj,
                }
        }
        res->mng = forb_object_duplicate(desc->manager);
+       res->name = desc->name;
 
        fcb_alloc_init_root_field(res);
        sc_contracts_init_head(res);
-       ul_logmsg("Registering manager for resource %d.%d\n",
-                 restype, resid);
+       ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
+                 res->name, restype, resid);
        fcb_resource_insert(fcb, res);
        return 0;
 free_err:
@@ -815,6 +830,7 @@ CORBA_long register_allocator(fres_contract_broker obj,
        struct res_key resource;
        forb_server_id server_id;
        char server_id_str[40];
+       int ret;
 
        forb_get_server_id(ra_obj, &server_id);
        forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
@@ -827,6 +843,7 @@ CORBA_long register_allocator(fres_contract_broker obj,
        if (!res) {
                ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
                          restype, resid);
+               ret = FRES_ERR_NO_RESOURCE_MANAGER;
                goto err;
        }
        ra = fcb_alloc_find(res, &server_id);
@@ -835,10 +852,12 @@ CORBA_long register_allocator(fres_contract_broker obj,
                ul_logerr("Allocator from already registered (%s)\n",
                          str);
                forb_free(str);
+               ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
                goto err;
        }
        ra = malloc(sizeof(*ra));
        if (!ra) {
+               ret = ENOMEM;
                goto err;
        }
        ra->app = server_id;
@@ -846,7 +865,7 @@ CORBA_long register_allocator(fres_contract_broker obj,
        fcb_alloc_insert(res, ra);
        return 0;
 err:
-       return FRSH_ERR_RESOURCE_ID_INVALID;
+       return ret;
 }
 
 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
@@ -877,13 +896,14 @@ void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORB
                seq->_buffer[n].restype = res->key.type;
                seq->_buffer[n].resid = res->key.id;
                seq->_buffer[n].desc.manager = res->mng;
+               seq->_buffer[n].desc.name = res->name;
                n++;
        }
 
        *resources = seq;
 }
 
-#ifdef CONFIG_FCB_INET
+#if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
 static int register_inet_port(forb_orb orb)
 {
        forb_port_t *port = forb_malloc(sizeof(*port));
@@ -894,7 +914,7 @@ static int register_inet_port(forb_orb orb)
                return -1;
        memset(port, 0, sizeof(*port));
        listen_on.s_addr = INADDR_ANY;
-       ret = forb_inet_port_init(&port->desc, listen_on);
+       ret = forb_inet_port_init(&port->desc, listen_on, 0);
        if (ret)
                return ret;
        ret = forb_register_port(orb, port);
@@ -920,6 +940,10 @@ void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
 #if 0
        ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
 #endif
+
+       if (orb_id == NULL)
+               return;
+
        if (strcmp(orb_id, "org.frescor.fcb") == 0) {
                fosa_abs_time_t now;
                fosa_rel_time_t delay;
@@ -934,6 +958,26 @@ void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
 {
 }
 
+static struct option long_opts[] = {
+    { "daemon",   optional_argument, NULL, 'd' },
+    { "loglevel", required_argument, NULL, 'l' },
+    { 0, 0, 0, 0}
+};
+
+static void
+usage(void)
+{
+       printf("usage: fcb [ options ]\n");
+       printf("  -d, --daemon [pid-file]   go to background after FORB initialization\n");
+       printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
+}
+
+int print_log_domain(ul_log_domain_t *domain, void *context)
+{
+       printf("%s = %d\n", domain->name, domain->level);
+       return 0;
+}
+
 int main(int argc, char *argv[])
 {
        forb_orb orb;
@@ -945,15 +989,48 @@ int main(int argc, char *argv[])
                .orb_id = "org.frescor.fcb",
                .peer_discovery_callback = peer_discovery_callback,
                .peer_dead_callback = peer_dead_callback,
+               .fixed_tcp_port = FCB_TCP_PORT,
+#ifdef CONFIG_FORB_PROTO_INET_DEFAULT          
+               .fixed_server_id = FCB_SERVER_ID,
+               .redistribute_hellos = true,
+#endif
        };
-
+       int  opt;
+
+       while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
+               switch (opt) {
+                       case 'l':
+                               if (*optarg == '?') {
+                                       ul_logreg_for_each_domain(print_log_domain, NULL);
+                                       exit(0);
+                               }
+                               {
+                                       int ret;
+                                       ret = ul_log_domain_arg2levels(optarg);
+                                       if (ret) 
+                                               error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
+                               }
+                               break;
+                       case 'd':
+                               opt_daemon = true;
+                               opt_pidfile = optarg;
+                               break;
+                       case 'h':
+                       /*default:*/
+                               usage();
+                               exit(opt == 'h' ? 0 : 1);
+               }
+       }
 
        fosa_clock_get_time(CLOCK_REALTIME, &start_time);
 
+       if (opt_daemon)
+               forb_daemon_prepare(opt_pidfile);
+
        orb = forb_init(&argc, &argv, &attr);
        if (!orb) error(1, errno, "FORB initialization failed");
 
-#ifdef CONFIG_FCB_INET
+#if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
        ret = register_inet_port(orb);
        if (ret) error(0, errno, "INET port registration failed");
 #endif
@@ -976,6 +1053,9 @@ int main(int argc, char *argv[])
        if (ret) error(1, errno, "forb_register_reference() failed");
 
        ul_logmsg("Waiting for requests\n");
+       if (opt_daemon)
+               forb_daemon_ready();
+
        ret = forb_executor_run(&executor);
        if (ret) error(1, errno, "forb_executor_run failed");