Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

ContractBasedOptimizer.h

Go to the documentation of this file.
00001 #ifndef MEDUSA_CONTRACTBASEDOPT_H 00002 #define MEDUSA_CONTRACTBASEDOPT_H 00003 00004 #include "NHOptimizer.h" 00005 00006 MEDUSA_NAMESPACE_BEGIN 00007 00008 00009 // -------------------------------------------------- 00013 class Contract { 00014 public: 00015 Contract(string partner = "", double min_price = 0.0, double max_price = 0.0) 00016 : m_partner(partner), m_min_price(min_price), 00017 m_current_price(min_price), m_max_price(max_price), m_last_offer_rejected(false), m_broken(false) {} 00018 string m_partner; 00019 double m_min_price; 00020 double m_current_price; 00021 double m_max_price; 00022 bool m_last_offer_rejected; 00023 bool m_broken; // Indicates if there is something wrong with talking to this partner 00024 bool operator<(const Contract& c) const { 00025 00026 // Broken/unresponsive partners go last 00027 if ( c.m_broken != m_broken) { 00028 return c.m_broken; 00029 } 00030 00031 // Before last go all partners that last rejected our offer 00032 if ( c.m_last_offer_rejected != m_last_offer_rejected) { 00033 return c.m_last_offer_rejected; 00034 } 00035 00036 // Finally we sort on price 00037 return ( m_current_price < c.m_current_price); 00038 00039 // If prices are the same, this contract is lower if didn't reject while partner did 00040 //if ( c.m_current_price == m_current_price) { 00041 // return (!m_last_offer_rejected) && c.m_last_offer_rejected; 00042 //} else { 00043 // return ( m_current_price < c.m_current_price); 00044 //} 00045 } 00046 00047 NMSTL_SIMPLY_SERIALIZABLE(Contract, << m_partner << m_min_price << m_max_price); 00048 00049 string as_string() const { 00050 stringstream out; 00051 out.precision(3); 00052 out << "{Contract with " << m_partner << "[" << m_min_price << ":" 00053 << m_current_price << ":" << m_max_price << "]"; 00054 00055 return out.str(); 00056 } 00057 00058 }; 00059 NMSTL_TO_STRING(Contract); 00060 00061 00062 // -------------------------------------------------- 00066 class ContractBasedOptimizer : public NHOptimizer { 00067 00068 public: 00069 ContractBasedOptimizer(string id, string configFileName=DEFAULT_CONFIG_FILE); 00070 ~ContractBasedOptimizer() {} 00071 00072 protected: 00073 void in_thread_init(); 00074 void handle_load_offer(AsyncRPC< ptr<LoadOffer> > completion, ptr<LoadOffer> offer); 00075 00076 private: 00077 00078 class OverloadedException { 00079 public: 00080 string m_message; 00081 OverloadedException(string message): m_message(message) { } 00082 }; 00083 00084 //static const double EPSILON_COST = 0.002; 00085 static const double EPSILON_COST = 0.1; 00086 00087 // Cached version of latest stats 00088 typedef vector< Stats > StatsList; 00089 vector<Stats> m_operatorStats; 00090 double m_total_cpu; // Total used in percent 00091 double m_total_bw; // Total used in MBytes 00092 double m_bw_utilization; // Fraction total used (ratio) 00093 double m_cpu_utilization; // Fraction total used (ratio) 00094 double m_load_level; 00095 double m_marginal_cost; 00096 00097 00098 typedef list< Contract > Contracts; 00099 Contracts m_contracts; 00100 00102 void init_contracts(); 00103 00105 void update_stats(); 00106 void update_stats_2(RPC<StatsList> result); 00107 00109 double compute_avg_marginal_cost(double cpuUsage, double deltaCPU, double bwUsage, double deltaBW); 00110 00111 // Giving load 00112 bool m_offerPending; 00113 00118 void reset_partners(); 00119 void send_out_load_offers(); 00120 void pick_boxes(StatsList& src, StatsList& dst, double threshold); 00121 void handle_load_offer_response(string partner, ptr<LoadOffer> original_offer, 00122 RPC< ptr<LoadOffer> > counter_offer); 00123 void done_moving_load(RPC<void> stat); 00124 vector<Name> extract_names(vector<Stats> boxes); 00125 00126 // Taking load 00127 bool m_movementInProgress; 00128 typedef list< ptr<LoadOffer> > LoadOffers; 00129 typedef map < ptr<LoadOffer>, AsyncRPC< ptr<LoadOffer> > > LoadOfferCompletions; 00130 LoadOffers m_load_offers; 00131 LoadOfferCompletions m_load_offer_completions; 00132 void examine_load_offers(); 00133 void pick_new_boxes(StatsList& src, StatsList& dst, double threshold, 00134 double& acceptedDeltaCPU, double& acceptedDeltaBW); 00135 00136 00137 }; 00138 00139 MEDUSA_NAMESPACE_END 00140 00141 #endif

Generated on Fri Nov 12 15:15:20 2004 for Borealis by doxygen 1.3.8