]> rtime.felk.cvut.cz Git - frescor/frsh.git/blobdiff - fres/cbroker/fcb.c
forb: executable resources and fcb changed into dynamic libs
[frescor/frsh.git] / fres / cbroker / fcb.c
index bd0f5cba892ac6ee48d80df7f49241ba7540370e..f931d2eb4573fe4aa0eec574baf91772f455ed1d 100644 (file)
@@ -846,8 +846,7 @@ err:
  * reference. Canceled contracts are removed below. */
 int
 fcb_remember_contracts(struct fcb *fcb,
-                      struct fcb_contract **fcb_contracts, int num,
-                      fres_contract_ptr_seq *schedulable_contracts)
+                      struct fcb_contract **fcb_contracts, int num)
 {
        struct fcb_contract *fc;
        int i;
@@ -873,14 +872,34 @@ fcb_remember_contracts(struct fcb *fcb,
                        /* 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;
 }
 
@@ -921,6 +940,10 @@ negotiate_contracts(fres_contract_broker obj,
                                    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)
@@ -946,8 +969,8 @@ negotiate_contracts(fres_contract_broker obj,
        if (ret)
                goto err_cancel_reservation;
 
-       fcb_remember_contracts(fcb, fcb_contracts, num,
-                              schedulable_contracts);
+       fcb_remember_contracts(fcb, fcb_contracts, num);
+       fcb_remember_schedulable_contracts(fcb, schedulable_contracts);
 
        ret = change_vreses(fcb, schedulable_contracts);
        if (ret)
@@ -1198,7 +1221,7 @@ negotiate_transaction(fres_contract_broker _obj,
        fres_contract_ptr_seq *schedulable_contracts;
        struct fcb_transaction *ft;
 
-       ul_logmsg("Negotiating transaction with %d contracts\n", num);
+       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;
@@ -1238,16 +1261,23 @@ negotiate_transaction(fres_contract_broker _obj,
                        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);
        }
-       fcb_remember_contracts(fcb, fcb_contracts, num,
-                              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) {
@@ -1310,20 +1340,71 @@ wait_transaction(fres_contract_broker _obj,
 CORBA_long
 allocate_transaction_vres(fres_contract_broker _obj,
                          const CORBA_char * name,
-                         const CORBA_long id,
+                         const CORBA_long index,
+                         fres_contract_id_t *id,
                          CORBA_Environment *ev)
 {
        struct fcb *fcb = o2fcb(_obj);
        struct fcb_transaction *ft;
+       struct fcb_contract *fc;
        int ret;
-       char * const n = (char * const)&name;
+       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)
 
-       ft = fcb_transaction_find(fcb, &n);
-       if (!ft) {
-               ret = FRES_ERR_TRANSACTION_NOT_FOUND;
-               goto err;
+       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;
 }
@@ -1388,7 +1469,6 @@ 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}
 };
@@ -1397,7 +1477,6 @@ 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");
 }
 
@@ -1407,9 +1486,8 @@ int print_log_domain(ul_log_domain_t *domain, void *context)
        return 0;
 }
 
-int main(int argc, char *argv[])
+int forb_main(forb_orb orb, int argc, char *argv[])
 {
-       forb_orb orb;
        struct fcb fcb_data;
        fres_contract_broker fcb;
        forb_executor_t executor;
@@ -1426,7 +1504,7 @@ int main(int argc, char *argv[])
        };
        int  opt;
 
-       while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
+       while ((opt = getopt_long(argc, argv, "hl:", &long_opts[0], NULL)) != EOF) {
                switch (opt) {
                        case 'l':
                                if (*optarg == '?') {
@@ -1440,10 +1518,6 @@ int main(int argc, char *argv[])
                                                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();
@@ -1457,9 +1531,6 @@ int main(int argc, char *argv[])
        if (opt_daemon)
                forb_daemon_prepare(opt_pidfile);
 
-       orb = forb_init(&argc, &argv, &attr);
-       if (!orb) error(1, errno, "FORB initialization failed");
-
 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
        ret = register_inet_port(orb);
        if (ret) error(0, errno, "INET port registration failed");