source: XIOS3/trunk/src/manager/services_manager.cpp @ 2458

Last change on this file since 2458 was 2458, checked in by ymipsl, 17 months ago

Merge XIOS_FILE_SERVICE dev branch into trunk

YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.6 KB
Line 
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"
9#include "timer.hpp"
10
11namespace 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_) ;
33    winNotify_->lockWindow(commRank,0) ;
34    winNotify_->updateToWindow(commRank, this, &CServicesManager::notificationsDumpOut) ;
35    winNotify_->unlockWindow(commRank,0) ;
36
37    winServices_ = new CWindowManager(xiosComm_, maxBufferSize_) ;
38    winServices_->lockWindow(commRank,0) ;
39    winServices_->updateToWindow(commRank, this, &CServicesManager::servicesDumpOut) ;
40    winServices_->unlockWindow(commRank,0) ;
41
42    MPI_Barrier(xiosComm_)  ;   
43  }
44
45  CServicesManager::~CServicesManager()
46  {
47    delete winNotify_ ;
48    delete winServices_ ;
49  }
50
51  bool CServicesManager::createServices(const std::string& poolId, const std::string& serviceId, 
52                                        int type, int size, int nbPartitions, bool wait) 
53  {
54
55    int leader ;
56    int poolSize, poolFreeSize ;
57   
58    info(40)<<"CServicesManager : waiting for pool info : "<<poolId<<endl ; ;
59    bool ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, poolFreeSize, leader) ;
60    if (wait)
61    {
62      while (!ok) 
63      {
64        CXios::getDaemonsManager()->eventLoop() ;
65        ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, poolFreeSize, leader) ;
66      }
67    }
68
69    if (ok) 
70    {
71      info(40)<<"CServicesManager : create service notification to leader "<<leader<<", serviceId : "<<serviceId<<", size : "<<size<<endl ;
72      CXios::getRessourcesManager()->decreasePoolFreeSize(poolId ,size) ;
73      createServicesNotify(leader, serviceId, type, size, nbPartitions) ;
74      return true ;
75    }
76    else return false ;
77  }
78
79  bool CServicesManager::createServicesOnto(const std::string& poolId, const std::string& serviceId, int type, const std::string& OnServiceId, bool wait)
80  {
81
82    int leader ;
83    int poolSize ;
84    int poolFreeSize ;
85   
86    info(40)<<"CServicesManager : waiting for pool info : "<<poolId<<endl ; ;
87    bool ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, poolFreeSize, leader) ;
88    if (wait)
89    {
90      while (!ok) 
91      {
92        CXios::getDaemonsManager()->eventLoop() ;
93        ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, poolFreeSize, leader) ;
94      }
95    }
96
97    if (ok) 
98    {
99      info(40)<<"CServicesManager : create service on other, notification to leader "<<leader<<", serviceId : "<<serviceId<<", service onto : "<<OnServiceId<<endl ;
100      createServicesOntoNotify(leader, serviceId, type, OnServiceId) ;
101      return true ;
102    }
103    else return false ;
104  }
105
106  void CServicesManager::createServicesNotify(int rank, const string& serviceId, int type, int size, int nbPartitions)
107  {
108    notifyType_=NOTIFY_CREATE_SERVICE ;
109    notifyCreateService_=make_tuple(serviceId, type, size, nbPartitions ) ;
110    sendNotification(rank) ;
111  }
112
113
114  void CServicesManager::createServicesOntoNotify(int rank, const string& serviceId, int type, const string& OnServiceId)
115  {
116    notifyType_=NOTIFY_CREATE_SERVICE_ONTO ;
117    notifyCreateServiceOnto_=make_tuple(serviceId, type, OnServiceId) ;
118    sendNotification(rank) ;
119  }
120
121  void CServicesManager::sendNotification(int rank)
122  {
123    winNotify_->lockWindowExclusive(rank) ;
124    winNotify_->pushToLockedWindow(rank, this, &CServicesManager::notificationsDumpOut) ;
125    winNotify_->unlockWindow(rank) ;
126  }
127
128 
129  void CServicesManager::eventLoop(void)
130  {
131    CTimer::get("CServicesManager::eventLoop").resume();
132    int flag ;
133    MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
134    double time=MPI_Wtime() ;
135    if (time-lastEventLoop_ > eventLoopLatency_) 
136    {
137      checkNotifications() ;
138      lastEventLoop_=time ;
139    }
140    CTimer::get("CServicesManager::eventLoop").suspend();
141  }
142
143
144
145  void CServicesManager::checkNotifications(void)
146  {
147    int commRank ;
148    MPI_Comm_rank(xiosComm_, &commRank) ;
149    winNotify_->lockWindowExclusive(commRank) ;
150    winNotify_->popFromLockedWindow(commRank, this, &CServicesManager::notificationsDumpIn) ;
151    winNotify_->unlockWindow(commRank) ;
152    if (notifyType_==NOTIFY_CREATE_SERVICE) createService() ;
153    else if (notifyType_==NOTIFY_CREATE_SERVICE_ONTO) createServiceOnto() ;
154  }
155
156  void CServicesManager::createService(void)
157  {
158    auto& arg=notifyCreateService_ ;
159    CServer::getServersRessource()->getPoolRessource()->createService(get<0>(arg), get<1>(arg), get<2>(arg), get<3>(arg)) ;
160  }
161
162  void CServicesManager::createServiceOnto(void)
163  {
164    auto& arg=notifyCreateServiceOnto_ ;
165    CServer::getServersRessource()->getPoolRessource()->createServiceOnto(get<0>(arg), get<1>(arg), get<2>(arg)) ;
166  }
167
168  void CServicesManager::notificationsDumpOut(CBufferOut& buffer)
169  {
170
171    buffer.realloc(maxBufferSize_) ;
172   
173    if (notifyType_==NOTIFY_CREATE_SERVICE)
174    {
175      auto& arg=notifyCreateService_ ;
176      buffer << notifyType_<< get<0>(arg) << get<1>(arg) << std::get<2>(arg) << get<3>(arg) ;
177    }
178    else if (notifyType_==NOTIFY_CREATE_SERVICE_ONTO)
179    {
180      auto& arg=notifyCreateServiceOnto_ ;
181      buffer << notifyType_<< get<0>(arg) << get<1>(arg) << get<2>(arg)  ;
182    }
183  }
184
185  void CServicesManager::notificationsDumpIn(CBufferIn& buffer)
186  {
187    if (buffer.bufferSize() == 0) notifyType_= NOTIFY_NOTHING ;
188    else
189    {
190      buffer>>notifyType_;
191      if (notifyType_==NOTIFY_CREATE_SERVICE)
192      {
193        auto& arg=notifyCreateService_ ;
194        buffer >> get<0>(arg) >> get<1>(arg) >> std::get<2>(arg)>> get<3>(arg) ;
195      }
196      else if (notifyType_==NOTIFY_CREATE_SERVICE_ONTO)
197      {
198        auto& arg=notifyCreateServiceOnto_ ;
199        buffer >> get<0>(arg) >> get<1>(arg) >> get<2>(arg) ;
200      }
201    }
202  } 
203 
204  void CServicesManager::servicesDumpOut(CBufferOut& buffer)
205  {
206   
207    buffer.realloc(maxBufferSize_) ;
208   
209    buffer<<(int)services_.size();
210   
211    for(auto it=services_.begin();it!=services_.end(); ++it)
212    { 
213      auto key = it->first ;
214      auto val = it->second ; 
215      buffer << std::get<0>(key) << std::get<1>(key) << std::get<2>(key) 
216             <<  static_cast<int>(std::get<0>(val)) << std::get<1>(val) << std::get<2>(val) << std::get<3>(val) ;
217    }
218  }
219
220  void CServicesManager::servicesDumpIn(CBufferIn& buffer)
221  {
222    std::string poolId, serviceId ;
223    int partitionId ;
224    int type ;
225    int size; 
226    int nbPartitions ;
227    int leader ;
228
229    services_.clear() ;
230    int nbServices ;
231    buffer>>nbServices ;
232    for(int i=0;i<nbServices;i++) 
233    {
234      buffer>>poolId>>serviceId>>partitionId>>type>>size>>nbPartitions>>leader ;
235      services_[std::tuple<std::string,std::string,int>(poolId,serviceId,partitionId)]=std::make_tuple(type,size,nbPartitions,leader) ;
236    }
237  }
238
239  void CServicesManager::registerService(const std::string& poolId, const std::string& serviceId, const int& partitionId, int type, 
240                                         int size, int nbPartitions, int leader)
241  {
242   
243    info(40)<<"CServicesManager : registering service, poolId : "<<poolId<<", serviceId : "<<serviceId<<endl ; ;
244
245    winServices_->lockWindowExclusive(managerGlobalLeader_) ;
246    winServices_->updateFromLockedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ;
247    winServices_->flushWindow(managerGlobalLeader_) ;
248    services_[std::tuple<std::string, std::string,int>(poolId,serviceId,partitionId)]=std::make_tuple(type,size,nbPartitions,leader) ;
249    winServices_->updateToLockedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpOut) ;
250    winServices_->unlockWindow(managerGlobalLeader_) ;
251
252  }
253
254  bool CServicesManager::getServiceInfo(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& type, 
255                                        int& size, int& nbPartitions, int& leader, bool wait)
256  {
257   
258    winServices_->lockWindowShared(managerGlobalLeader_) ;
259    winServices_->updateFromLockedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ;
260    winServices_->unlockWindow(managerGlobalLeader_) ;
261
262    auto it=services_.find(std::tuple<std::string,std::string,int>(poolId,serviceId,partitionId)) ;
263    if ( it == services_.end() && !wait) return false ;
264    else
265    {
266      if (wait) waitServiceRegistration(poolId, serviceId, partitionId) ;
267      type= std::get<0>(it->second); 
268      size= std::get<1>(it->second); 
269      nbPartitions = std::get<2>(it->second); 
270      leader = std::get<3>(it->second); 
271      return true ;
272    }
273  }
274
275  bool CServicesManager::getServiceLeader(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& leader, bool wait)
276  {
277    int type;
278    int size ;
279    int nbPartitions;
280    return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader) ;
281  }
282
283  bool CServicesManager::getServiceType(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& type, bool wait)
284  {
285    int size ;
286    int nbPartitions;
287    int leader;
288    return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader) ;
289  }
290
291  bool CServicesManager::getServiceNbPartitions(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& nbPartitions, bool wait)
292  {
293    int size ;
294    int type;
295    int leader;
296    return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader) ;
297  }
298
299  bool CServicesManager::hasService(const std::string& poolId, const std::string& serviceId, const int& partitionId)
300  {
301    winServices_->lockWindow(managerGlobalLeader_,0) ;
302    winServices_->updateFromWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ;
303    winServices_->unlockWindow(managerGlobalLeader_,0) ;
304    auto it=services_.find(std::tuple<std::string, std::string, int>(poolId, serviceId, partitionId)) ;
305    if ( it == services_.end()) return false ;
306    else return true ;
307  }
308 
309  void CServicesManager::waitServiceRegistration(const std::string& poolId, const std::string& serviceId, const int& partitionId)
310  {
311    while(!hasService(poolId,serviceId,partitionId)) CXios::getDaemonsManager()->servicesEventLoop() ;
312  }
313
314}
Note: See TracBrowser for help on using the repository browser.