[1761] | 1 | #include "daemons_manager.hpp" |
---|
| 2 | #include "services_manager.hpp" |
---|
| 3 | #include "ressources_manager.hpp" |
---|
| 4 | #include "cxios.hpp" |
---|
| 5 | #include "pool_ressource.hpp" |
---|
| 6 | #include "type.hpp" |
---|
| 7 | #include "server.hpp" |
---|
| 8 | #include "servers_ressource.hpp" |
---|
[2246] | 9 | #include "timer.hpp" |
---|
[1761] | 10 | |
---|
| 11 | namespace xios |
---|
| 12 | { |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | CServicesManager::CServicesManager(bool isXiosServer) |
---|
| 17 | { |
---|
| 18 | |
---|
| 19 | int commRank ; |
---|
| 20 | xiosComm_ = CXios::getXiosComm() ; |
---|
| 21 | MPI_Comm_rank(xiosComm_, &commRank) ; |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | // The global manager leader will be the process of rank 0 |
---|
| 25 | // By "xiosComm" communicator construction |
---|
| 26 | // - if servers exits it will be the root process of the servers communicator |
---|
| 27 | // - otherwise the root process of the first model |
---|
| 28 | |
---|
| 29 | managerGlobalLeader_ = 0 ; |
---|
| 30 | |
---|
| 31 | MPI_Comm_rank(xiosComm_, &commRank) ; |
---|
| 32 | winNotify_ = new CWindowManager(xiosComm_, maxBufferSize_) ; |
---|
[2517] | 33 | winNotify_->updateToExclusiveWindow(commRank, this, &CServicesManager::notificationsDumpOut) ; |
---|
[1761] | 34 | |
---|
| 35 | winServices_ = new CWindowManager(xiosComm_, maxBufferSize_) ; |
---|
[2517] | 36 | winServices_->updateToExclusiveWindow(commRank, this, &CServicesManager::servicesDumpOut) ; |
---|
| 37 | |
---|
[1761] | 38 | MPI_Barrier(xiosComm_) ; |
---|
| 39 | } |
---|
| 40 | |
---|
[1764] | 41 | CServicesManager::~CServicesManager() |
---|
| 42 | { |
---|
| 43 | delete winNotify_ ; |
---|
| 44 | delete winServices_ ; |
---|
| 45 | } |
---|
| 46 | |
---|
[1761] | 47 | bool CServicesManager::createServices(const std::string& poolId, const std::string& serviceId, |
---|
| 48 | int type, int size, int nbPartitions, bool wait) |
---|
| 49 | { |
---|
| 50 | |
---|
| 51 | int leader ; |
---|
[2458] | 52 | int poolSize, poolFreeSize ; |
---|
[1761] | 53 | |
---|
[2246] | 54 | info(40)<<"CServicesManager : waiting for pool info : "<<poolId<<endl ; ; |
---|
[2458] | 55 | bool ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, poolFreeSize, leader) ; |
---|
[1761] | 56 | if (wait) |
---|
| 57 | { |
---|
| 58 | while (!ok) |
---|
| 59 | { |
---|
| 60 | CXios::getDaemonsManager()->eventLoop() ; |
---|
[2458] | 61 | ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, poolFreeSize, leader) ; |
---|
[1761] | 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if (ok) |
---|
| 66 | { |
---|
[2246] | 67 | info(40)<<"CServicesManager : create service notification to leader "<<leader<<", serviceId : "<<serviceId<<", size : "<<size<<endl ; |
---|
[2458] | 68 | CXios::getRessourcesManager()->decreasePoolFreeSize(poolId ,size) ; |
---|
[1761] | 69 | createServicesNotify(leader, serviceId, type, size, nbPartitions) ; |
---|
| 70 | return true ; |
---|
| 71 | } |
---|
| 72 | else return false ; |
---|
| 73 | } |
---|
| 74 | |
---|
[2404] | 75 | bool CServicesManager::createServicesOnto(const std::string& poolId, const std::string& serviceId, int type, const std::string& OnServiceId, bool wait) |
---|
[2403] | 76 | { |
---|
[1761] | 77 | |
---|
[2403] | 78 | int leader ; |
---|
| 79 | int poolSize ; |
---|
[2458] | 80 | int poolFreeSize ; |
---|
[2403] | 81 | |
---|
| 82 | info(40)<<"CServicesManager : waiting for pool info : "<<poolId<<endl ; ; |
---|
[2458] | 83 | bool ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, poolFreeSize, leader) ; |
---|
[2403] | 84 | if (wait) |
---|
| 85 | { |
---|
| 86 | while (!ok) |
---|
| 87 | { |
---|
| 88 | CXios::getDaemonsManager()->eventLoop() ; |
---|
[2458] | 89 | ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, poolFreeSize, leader) ; |
---|
[2403] | 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | if (ok) |
---|
| 94 | { |
---|
| 95 | info(40)<<"CServicesManager : create service on other, notification to leader "<<leader<<", serviceId : "<<serviceId<<", service onto : "<<OnServiceId<<endl ; |
---|
[2404] | 96 | createServicesOntoNotify(leader, serviceId, type, OnServiceId) ; |
---|
[2403] | 97 | return true ; |
---|
| 98 | } |
---|
| 99 | else return false ; |
---|
| 100 | } |
---|
| 101 | |
---|
[1761] | 102 | void CServicesManager::createServicesNotify(int rank, const string& serviceId, int type, int size, int nbPartitions) |
---|
| 103 | { |
---|
[2403] | 104 | notifyType_=NOTIFY_CREATE_SERVICE ; |
---|
| 105 | notifyCreateService_=make_tuple(serviceId, type, size, nbPartitions ) ; |
---|
| 106 | sendNotification(rank) ; |
---|
[1761] | 107 | } |
---|
| 108 | |
---|
[2403] | 109 | |
---|
[2404] | 110 | void CServicesManager::createServicesOntoNotify(int rank, const string& serviceId, int type, const string& OnServiceId) |
---|
[1761] | 111 | { |
---|
[2403] | 112 | notifyType_=NOTIFY_CREATE_SERVICE_ONTO ; |
---|
[2404] | 113 | notifyCreateServiceOnto_=make_tuple(serviceId, type, OnServiceId) ; |
---|
[2403] | 114 | sendNotification(rank) ; |
---|
| 115 | } |
---|
[1761] | 116 | |
---|
[2403] | 117 | void CServicesManager::sendNotification(int rank) |
---|
| 118 | { |
---|
[2517] | 119 | winNotify_->pushToExclusiveWindow(rank, this, &CServicesManager::notificationsDumpOut) ; |
---|
[1761] | 120 | } |
---|
| 121 | |
---|
[2403] | 122 | |
---|
[1761] | 123 | void CServicesManager::eventLoop(void) |
---|
| 124 | { |
---|
[2246] | 125 | CTimer::get("CServicesManager::eventLoop").resume(); |
---|
[2260] | 126 | int flag ; |
---|
| 127 | MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE); |
---|
[2246] | 128 | double time=MPI_Wtime() ; |
---|
| 129 | if (time-lastEventLoop_ > eventLoopLatency_) |
---|
| 130 | { |
---|
[2403] | 131 | checkNotifications() ; |
---|
[2246] | 132 | lastEventLoop_=time ; |
---|
| 133 | } |
---|
| 134 | CTimer::get("CServicesManager::eventLoop").suspend(); |
---|
[1761] | 135 | } |
---|
| 136 | |
---|
[2403] | 137 | |
---|
| 138 | |
---|
| 139 | void CServicesManager::checkNotifications(void) |
---|
| 140 | { |
---|
| 141 | int commRank ; |
---|
| 142 | MPI_Comm_rank(xiosComm_, &commRank) ; |
---|
[2517] | 143 | winNotify_->popFromExclusiveWindow(commRank, this, &CServicesManager::notificationsDumpIn) ; |
---|
[2403] | 144 | if (notifyType_==NOTIFY_CREATE_SERVICE) createService() ; |
---|
| 145 | else if (notifyType_==NOTIFY_CREATE_SERVICE_ONTO) createServiceOnto() ; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void CServicesManager::createService(void) |
---|
| 149 | { |
---|
| 150 | auto& arg=notifyCreateService_ ; |
---|
| 151 | CServer::getServersRessource()->getPoolRessource()->createService(get<0>(arg), get<1>(arg), get<2>(arg), get<3>(arg)) ; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | void CServicesManager::createServiceOnto(void) |
---|
| 155 | { |
---|
[2404] | 156 | auto& arg=notifyCreateServiceOnto_ ; |
---|
| 157 | CServer::getServersRessource()->getPoolRessource()->createServiceOnto(get<0>(arg), get<1>(arg), get<2>(arg)) ; |
---|
[2403] | 158 | } |
---|
| 159 | |
---|
[1761] | 160 | void CServicesManager::notificationsDumpOut(CBufferOut& buffer) |
---|
| 161 | { |
---|
[2403] | 162 | |
---|
[1761] | 163 | buffer.realloc(maxBufferSize_) ; |
---|
| 164 | |
---|
[2403] | 165 | if (notifyType_==NOTIFY_CREATE_SERVICE) |
---|
| 166 | { |
---|
| 167 | auto& arg=notifyCreateService_ ; |
---|
| 168 | buffer << notifyType_<< get<0>(arg) << get<1>(arg) << std::get<2>(arg) << get<3>(arg) ; |
---|
| 169 | } |
---|
| 170 | else if (notifyType_==NOTIFY_CREATE_SERVICE_ONTO) |
---|
| 171 | { |
---|
| 172 | auto& arg=notifyCreateServiceOnto_ ; |
---|
[2404] | 173 | buffer << notifyType_<< get<0>(arg) << get<1>(arg) << get<2>(arg) ; |
---|
[2403] | 174 | } |
---|
[1761] | 175 | } |
---|
| 176 | |
---|
| 177 | void CServicesManager::notificationsDumpIn(CBufferIn& buffer) |
---|
| 178 | { |
---|
[2403] | 179 | if (buffer.bufferSize() == 0) notifyType_= NOTIFY_NOTHING ; |
---|
| 180 | else |
---|
[1761] | 181 | { |
---|
[2403] | 182 | buffer>>notifyType_; |
---|
| 183 | if (notifyType_==NOTIFY_CREATE_SERVICE) |
---|
| 184 | { |
---|
| 185 | auto& arg=notifyCreateService_ ; |
---|
| 186 | buffer >> get<0>(arg) >> get<1>(arg) >> std::get<2>(arg)>> get<3>(arg) ; |
---|
| 187 | } |
---|
| 188 | else if (notifyType_==NOTIFY_CREATE_SERVICE_ONTO) |
---|
| 189 | { |
---|
| 190 | auto& arg=notifyCreateServiceOnto_ ; |
---|
[2404] | 191 | buffer >> get<0>(arg) >> get<1>(arg) >> get<2>(arg) ; |
---|
[2403] | 192 | } |
---|
[1761] | 193 | } |
---|
[2403] | 194 | } |
---|
[1761] | 195 | |
---|
| 196 | void CServicesManager::servicesDumpOut(CBufferOut& buffer) |
---|
| 197 | { |
---|
| 198 | |
---|
| 199 | buffer.realloc(maxBufferSize_) ; |
---|
| 200 | |
---|
| 201 | buffer<<(int)services_.size(); |
---|
| 202 | |
---|
| 203 | for(auto it=services_.begin();it!=services_.end(); ++it) |
---|
| 204 | { |
---|
| 205 | auto key = it->first ; |
---|
| 206 | auto val = it->second ; |
---|
| 207 | buffer << std::get<0>(key) << std::get<1>(key) << std::get<2>(key) |
---|
| 208 | << static_cast<int>(std::get<0>(val)) << std::get<1>(val) << std::get<2>(val) << std::get<3>(val) ; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | void CServicesManager::servicesDumpIn(CBufferIn& buffer) |
---|
| 213 | { |
---|
| 214 | std::string poolId, serviceId ; |
---|
| 215 | int partitionId ; |
---|
| 216 | int type ; |
---|
| 217 | int size; |
---|
| 218 | int nbPartitions ; |
---|
| 219 | int leader ; |
---|
| 220 | |
---|
| 221 | int nbServices ; |
---|
| 222 | buffer>>nbServices ; |
---|
[2507] | 223 | bool newServices = nbServices != services_.size() ; |
---|
[2494] | 224 | |
---|
[2507] | 225 | services_.clear() ; |
---|
| 226 | |
---|
[1761] | 227 | for(int i=0;i<nbServices;i++) |
---|
| 228 | { |
---|
| 229 | buffer>>poolId>>serviceId>>partitionId>>type>>size>>nbPartitions>>leader ; |
---|
| 230 | services_[std::tuple<std::string,std::string,int>(poolId,serviceId,partitionId)]=std::make_tuple(type,size,nbPartitions,leader) ; |
---|
[2507] | 231 | if (newServices) |
---|
| 232 | info(40)<<"Receive new services informations : "<<poolId<<"::"<<serviceId<<"::"<<partitionId<<" => type : "<<type<<" size : "<<size<<" nbPartitions : "<<nbPartitions<<" leader : "<<leader<<endl ; |
---|
[1761] | 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | void CServicesManager::registerService(const std::string& poolId, const std::string& serviceId, const int& partitionId, int type, |
---|
| 237 | int size, int nbPartitions, int leader) |
---|
| 238 | { |
---|
| 239 | |
---|
[2246] | 240 | info(40)<<"CServicesManager : registering service, poolId : "<<poolId<<", serviceId : "<<serviceId<<endl ; ; |
---|
| 241 | |
---|
| 242 | winServices_->lockWindowExclusive(managerGlobalLeader_) ; |
---|
| 243 | winServices_->updateFromLockedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ; |
---|
| 244 | winServices_->flushWindow(managerGlobalLeader_) ; |
---|
| 245 | services_[std::tuple<std::string, std::string,int>(poolId,serviceId,partitionId)]=std::make_tuple(type,size,nbPartitions,leader) ; |
---|
| 246 | winServices_->updateToLockedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpOut) ; |
---|
[2517] | 247 | winServices_->unlockWindowExclusive(managerGlobalLeader_) ; |
---|
[2246] | 248 | |
---|
[1761] | 249 | } |
---|
| 250 | |
---|
| 251 | bool CServicesManager::getServiceInfo(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& type, |
---|
[2458] | 252 | int& size, int& nbPartitions, int& leader, bool wait) |
---|
[1761] | 253 | { |
---|
[2517] | 254 | winServices_->updateFromSharedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ; |
---|
[2507] | 255 | |
---|
| 256 | if (wait) waitServiceRegistration(poolId, serviceId, partitionId) ; |
---|
[1761] | 257 | auto it=services_.find(std::tuple<std::string,std::string,int>(poolId,serviceId,partitionId)) ; |
---|
[2507] | 258 | if ( it == services_.end()) return false ; |
---|
[1761] | 259 | else |
---|
| 260 | { |
---|
| 261 | type= std::get<0>(it->second); |
---|
| 262 | size= std::get<1>(it->second); |
---|
| 263 | nbPartitions = std::get<2>(it->second); |
---|
| 264 | leader = std::get<3>(it->second); |
---|
| 265 | return true ; |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
[2458] | 269 | bool CServicesManager::getServiceLeader(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& leader, bool wait) |
---|
[1761] | 270 | { |
---|
| 271 | int type; |
---|
| 272 | int size ; |
---|
| 273 | int nbPartitions; |
---|
[2507] | 274 | return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader, wait) ; |
---|
[1761] | 275 | } |
---|
| 276 | |
---|
[2458] | 277 | bool CServicesManager::getServiceType(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& type, bool wait) |
---|
[1761] | 278 | { |
---|
| 279 | int size ; |
---|
| 280 | int nbPartitions; |
---|
| 281 | int leader; |
---|
[2507] | 282 | return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader, wait) ; |
---|
[1761] | 283 | } |
---|
| 284 | |
---|
[2458] | 285 | bool CServicesManager::getServiceNbPartitions(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& nbPartitions, bool wait) |
---|
[1761] | 286 | { |
---|
| 287 | int size ; |
---|
| 288 | int type; |
---|
| 289 | int leader; |
---|
[2507] | 290 | return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader, wait) ; |
---|
[1761] | 291 | } |
---|
| 292 | |
---|
| 293 | bool CServicesManager::hasService(const std::string& poolId, const std::string& serviceId, const int& partitionId) |
---|
| 294 | { |
---|
[2517] | 295 | winServices_->updateFromSharedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ; |
---|
[1761] | 296 | auto it=services_.find(std::tuple<std::string, std::string, int>(poolId, serviceId, partitionId)) ; |
---|
| 297 | if ( it == services_.end()) return false ; |
---|
| 298 | else return true ; |
---|
| 299 | } |
---|
[2458] | 300 | |
---|
| 301 | void CServicesManager::waitServiceRegistration(const std::string& poolId, const std::string& serviceId, const int& partitionId) |
---|
| 302 | { |
---|
| 303 | while(!hasService(poolId,serviceId,partitionId)) CXios::getDaemonsManager()->servicesEventLoop() ; |
---|
| 304 | } |
---|
[1761] | 305 | |
---|
[2458] | 306 | } |
---|