]> rtime.felk.cvut.cz Git - frescor/frsh.git/blobdiff - fres/resmng/frm_generic.c
Implemented support for contract renegotiation
[frescor/frsh.git] / fres / resmng / frm_generic.c
index 27ff4c98b59551a60f49f6f84054e9759e8de6f4..f1a80dd3d8f3b29d837d512e8ecca18a5673234c 100644 (file)
@@ -60,7 +60,7 @@
 #include <fcb.h>
 
 UL_LOG_CUST(ulogd_frm_generic);
-ul_log_domain_t ulogd_frm_generic = {UL_LOGL_MSG, "frm"};
+ul_log_domain_t ulogd_frm_generic = {UL_LOGL_DEB, "frm"};
 
 #define object_to_frm(o) (struct frm_data*)forb_instance_data(o)
 #define save_errno(cmd) do { int _e = errno; cmd; errno = _e; } while(0)
@@ -116,6 +116,7 @@ static CORBA_long reserve_contracts(fres_resource_manager obj,
        
        for (i=0; i<contracts->_length; i++) {
                struct fres_contract *cin = contracts->_buffer[i];
+
                c = fres_sa_scenario_find_contract(scenario, &cin->id);
                if (!c) {
                        c = fres_sa_contract_new();
@@ -125,6 +126,17 @@ static CORBA_long reserve_contracts(fres_resource_manager obj,
                        }
                }
                if (!c) goto err;
+
+               {
+                       char id[40];
+                       char *operation;
+                       fres_contract_id_to_string(id, &c->id, sizeof(id));
+                       if (fres_contract_get_num_blocks(cin) == 0) operation = "cancelation";
+                       else if (c->committed) operation = "renegotiation";
+                       else if (c->reserved) operation = "negotiation (already reserved)";
+                       else operation = "negotiation";
+                       ul_logdeb("  reserve contract %s %s\n", id, operation);
+               }
                assert(c->new == NULL);
                c->new = fres_contract_duplicate(cin);
                c->contract = c->new;
@@ -133,7 +145,7 @@ static CORBA_long reserve_contracts(fres_resource_manager obj,
 
        ret = frm->desc->admission_test(scenario, frm->desc->priv, &schedulable);
        if (ret) {
-               ul_logerr("admission_test failed: %d\n", ret);
+               ul_logmsg("admission_test failed: %d\n", ret);
                goto err;
        }
 
@@ -149,9 +161,9 @@ err:
 }
 
 static void commit_contracts(fres_resource_manager obj,
-                     const fres_contract_id_seq* ids,
-                     fres_contract_ptr_seq** contracts_with_scheduling_data,
-                     CORBA_Environment *ev)
+                            const fres_contract_id_seq* ids,
+                            fres_contract_ptr_seq** contracts_with_scheduling_data,
+                            CORBA_Environment *ev)
 {
        struct frm_data *frm = object_to_frm(obj);
        int i, num;
@@ -174,43 +186,74 @@ static void commit_contracts(fres_resource_manager obj,
         * priorities). Question: How to recognize which contracts are
         * changed because of this commit? */
        for (i=0; i < num; i++) {
+               char id[40];
+               const char *operation;
+               fres_contract_id_to_string(id, &ids->_buffer[i], sizeof(id));
+               
                c = fres_sa_scenario_find_contract(frm->scenario, &ids->_buffer[i]);
                if (c && c->reserved) {
-                       if (c->committed) fres_contract_destroy(c->committed);
-                       c->committed = c->reserved;
-                       c->reserved = NULL;
-                       contracts->_buffer[i] = fres_contract_duplicate(c->committed);
+                       if (fres_contract_get_num_blocks(c->reserved) == 0) {
+                               /* Cancelation request */
+                               operation = "cancelation";
+                               contracts->_buffer[i] = c->reserved;
+                               c->reserved = NULL;
+                               fres_sa_scenario_del_contract(frm->scenario, c);
+                               fres_sa_contract_destroy(c);
+                       } else {
+                               /* Normal reservation */
+                               if (c->committed) {
+                                       operation = "renegotiation";
+                                       fres_contract_destroy(c->committed);
+                               } else {
+                                       operation = "negotiation";
+                               }
+                               c->committed = c->reserved;
+                               c->reserved = NULL;
+                               contracts->_buffer[i] = fres_contract_duplicate(c->committed);
+                       }
                } else {
+                       operation = "error";
                        contracts->_buffer[i] = NULL;
                        if (!c) ul_logerr("Commit to unknown contract ID\n");
                        else if (!c->reserved) ul_logerr("Commit to not reserved contract\n");
                }
+
+               ul_logdeb("  commit contract %s %s\n", id, operation);
        }
-       
+
        *contracts_with_scheduling_data = contracts;
 err:;
 }
 
-static void cancel_contracts(fres_resource_manager obj,
-                     const fres_contract_id_seq* ids,
-                     CORBA_Environment *ev)
+static void cancel_reservations(fres_resource_manager obj,
+                               const fres_contract_id_seq* ids,
+                               CORBA_Environment *ev)
 {
        int i;
        struct frm_data *frm = object_to_frm(obj);
 
-       ul_logmsg("cancel_contracts\n");
+       ul_logmsg("cancel_reservations\n");
 
        for (i=0; i<ids->_length; i++) {
                struct fres_sa_contract *c;
                c = fres_sa_scenario_find_contract(frm->scenario, &ids->_buffer[i]);
 
-               if (c) {
-                       fres_sa_scenario_del_contract(frm->scenario, c);
-                       fres_sa_contract_destroy(c);
+               if (c->reserved) {
+                       if (c->committed) {
+                               /* Roll-back renegotiation */
+                               fres_contract_destroy(c->reserved);
+                               c->reserved = NULL;
+                               c->contract = c->committed;
+                       } else {
+                               /* Roll-back negotiation */
+                               fres_sa_scenario_del_contract(frm->scenario, c);
+                               fres_sa_contract_destroy(c);
+                       }
                }
        }
 }
 
+
 static void get_contracts(fres_resource_manager obj,
                          fres_contract_ptr_seq** contracts_out,
                          CORBA_long* utilization, CORBA_Environment *ev)
@@ -246,7 +289,7 @@ err:;
 static const struct forb_fres_resource_manager_impl frm_impl = {
        .reserve_contracts = reserve_contracts,
        .commit_contracts  = commit_contracts,
-       .cancel_contracts  = cancel_contracts,
+       .cancel_reservations = cancel_reservations,
        .get_contracts  = get_contracts,
 };