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 | |
---|
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_) ; |
---|
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 ; |
---|
57 | |
---|
58 | info(40)<<"CServicesManager : waiting for pool info : "<<poolId<<endl ; ; |
---|
59 | bool ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, leader) ; |
---|
60 | if (wait) |
---|
61 | { |
---|
62 | while (!ok) |
---|
63 | { |
---|
64 | CXios::getDaemonsManager()->eventLoop() ; |
---|
65 | ok=CXios::getRessourcesManager()->getPoolInfo(poolId, poolSize, leader) ; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | if (ok) |
---|
70 | { |
---|
71 | info(40)<<"CServicesManager : create service notification to leader "<<leader<<", serviceId : "<<serviceId<<", size : "<<size<<endl ; |
---|
72 | createServicesNotify(leader, serviceId, type, size, nbPartitions) ; |
---|
73 | return true ; |
---|
74 | } |
---|
75 | else return false ; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void CServicesManager::createServicesNotify(int rank, const string& serviceId, int type, int size, int nbPartitions) |
---|
80 | { |
---|
81 | winNotify_->lockWindow(rank,0) ; |
---|
82 | winNotify_->updateFromWindow(rank, this, &CServicesManager::notificationsDumpIn) ; |
---|
83 | notifications_.push_back(std::make_tuple(serviceId,type,size,nbPartitions)) ; |
---|
84 | winNotify_->updateToWindow(rank, this, &CServicesManager::notificationsDumpOut) ; |
---|
85 | winNotify_->unlockWindow(rank,0) ; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void CServicesManager::checkCreateServicesNotification(void) |
---|
90 | { |
---|
91 | int commRank ; |
---|
92 | MPI_Comm_rank(xiosComm_,&commRank) ; |
---|
93 | winNotify_->lockWindow(commRank,0) ; |
---|
94 | winNotify_->updateFromWindow(commRank, this, &CServicesManager::notificationsDumpIn) ; |
---|
95 | |
---|
96 | if (!notifications_.empty()) |
---|
97 | { |
---|
98 | auto info = notifications_.front() ; |
---|
99 | xios::info(40)<<"CServicesManager : receive create service notification : "<<get<0>(info)<<endl ; |
---|
100 | CServer::getServersRessource()->getPoolRessource()->createService(get<0>(info), get<1>(info), get<2>(info), get<3>(info)) ; |
---|
101 | notifications_.pop_front() ; |
---|
102 | winNotify_->updateToWindow(commRank, this, &CServicesManager::notificationsDumpOut) ; |
---|
103 | } |
---|
104 | winNotify_->unlockWindow(commRank,0) ; |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | void CServicesManager::eventLoop(void) |
---|
109 | { |
---|
110 | CTimer::get("CServicesManager::eventLoop").resume(); |
---|
111 | double time=MPI_Wtime() ; |
---|
112 | if (time-lastEventLoop_ > eventLoopLatency_) |
---|
113 | { |
---|
114 | checkCreateServicesNotification() ; |
---|
115 | lastEventLoop_=time ; |
---|
116 | } |
---|
117 | CTimer::get("CServicesManager::eventLoop").suspend(); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | void CServicesManager::notificationsDumpOut(CBufferOut& buffer) |
---|
122 | { |
---|
123 | |
---|
124 | buffer.realloc(maxBufferSize_) ; |
---|
125 | |
---|
126 | buffer<<(int)notifications_.size(); |
---|
127 | |
---|
128 | for(auto it=notifications_.begin();it!=notifications_.end(); ++it) |
---|
129 | buffer << std::get<0>(*it) << static_cast<int>(std::get<1>(*it))<< std::get<2>(*it) << std::get<3>(*it) ; |
---|
130 | } |
---|
131 | |
---|
132 | void CServicesManager::notificationsDumpIn(CBufferIn& buffer) |
---|
133 | { |
---|
134 | std::string id ; |
---|
135 | int type ; |
---|
136 | int size; |
---|
137 | int nbPartitions ; |
---|
138 | |
---|
139 | notifications_.clear() ; |
---|
140 | int nbNotifications ; |
---|
141 | buffer>>nbNotifications ; |
---|
142 | for(int i=0;i<nbNotifications;i++) |
---|
143 | { |
---|
144 | buffer>>id>>type>>size>>nbPartitions ; |
---|
145 | notifications_.push_back(std::make_tuple(id,type,size,nbPartitions)) ; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | void CServicesManager::servicesDumpOut(CBufferOut& buffer) |
---|
151 | { |
---|
152 | |
---|
153 | buffer.realloc(maxBufferSize_) ; |
---|
154 | |
---|
155 | buffer<<(int)services_.size(); |
---|
156 | |
---|
157 | for(auto it=services_.begin();it!=services_.end(); ++it) |
---|
158 | { |
---|
159 | auto key = it->first ; |
---|
160 | auto val = it->second ; |
---|
161 | buffer << std::get<0>(key) << std::get<1>(key) << std::get<2>(key) |
---|
162 | << static_cast<int>(std::get<0>(val)) << std::get<1>(val) << std::get<2>(val) << std::get<3>(val) ; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | void CServicesManager::servicesDumpIn(CBufferIn& buffer) |
---|
167 | { |
---|
168 | std::string poolId, serviceId ; |
---|
169 | int partitionId ; |
---|
170 | int type ; |
---|
171 | int size; |
---|
172 | int nbPartitions ; |
---|
173 | int leader ; |
---|
174 | |
---|
175 | services_.clear() ; |
---|
176 | int nbServices ; |
---|
177 | buffer>>nbServices ; |
---|
178 | for(int i=0;i<nbServices;i++) |
---|
179 | { |
---|
180 | buffer>>poolId>>serviceId>>partitionId>>type>>size>>nbPartitions>>leader ; |
---|
181 | services_[std::tuple<std::string,std::string,int>(poolId,serviceId,partitionId)]=std::make_tuple(type,size,nbPartitions,leader) ; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | void CServicesManager::registerService(const std::string& poolId, const std::string& serviceId, const int& partitionId, int type, |
---|
186 | int size, int nbPartitions, int leader) |
---|
187 | { |
---|
188 | |
---|
189 | info(40)<<"CServicesManager : registering service, poolId : "<<poolId<<", serviceId : "<<serviceId<<endl ; ; |
---|
190 | |
---|
191 | winServices_->lockWindowExclusive(managerGlobalLeader_) ; |
---|
192 | winServices_->updateFromLockedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ; |
---|
193 | winServices_->flushWindow(managerGlobalLeader_) ; |
---|
194 | services_[std::tuple<std::string, std::string,int>(poolId,serviceId,partitionId)]=std::make_tuple(type,size,nbPartitions,leader) ; |
---|
195 | winServices_->updateToLockedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpOut) ; |
---|
196 | winServices_->unlockWindow(managerGlobalLeader_) ; |
---|
197 | |
---|
198 | /* |
---|
199 | winServices_->lockWindow(managerGlobalLeader_,0) ; |
---|
200 | winServices_->updateFromWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ; |
---|
201 | services_[std::tuple<std::string, std::string,int>(poolId,serviceId,partitionId)]=std::make_tuple(type,size,nbPartitions,leader) ; |
---|
202 | winServices_->updateToWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpOut) ; |
---|
203 | winServices_->unlockWindow(managerGlobalLeader_,0) ;*/ |
---|
204 | } |
---|
205 | |
---|
206 | bool CServicesManager::getServiceInfo(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& type, |
---|
207 | int& size, int& nbPartitions, int& leader) |
---|
208 | { |
---|
209 | |
---|
210 | winServices_->lockWindowShared(managerGlobalLeader_) ; |
---|
211 | winServices_->updateFromLockedWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ; |
---|
212 | winServices_->unlockWindow(managerGlobalLeader_) ; |
---|
213 | /* |
---|
214 | winServices_->lockWindow(managerGlobalLeader_,0) ; |
---|
215 | winServices_->updateFromWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ; |
---|
216 | winServices_->unlockWindow(managerGlobalLeader_,0) ;*/ |
---|
217 | |
---|
218 | auto it=services_.find(std::tuple<std::string,std::string,int>(poolId,serviceId,partitionId)) ; |
---|
219 | if ( it == services_.end()) return false ; |
---|
220 | else |
---|
221 | { |
---|
222 | type= std::get<0>(it->second); |
---|
223 | size= std::get<1>(it->second); |
---|
224 | nbPartitions = std::get<2>(it->second); |
---|
225 | leader = std::get<3>(it->second); |
---|
226 | return true ; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | bool CServicesManager::getServiceLeader(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& leader) |
---|
231 | { |
---|
232 | int type; |
---|
233 | int size ; |
---|
234 | int nbPartitions; |
---|
235 | return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader) ; |
---|
236 | } |
---|
237 | |
---|
238 | bool CServicesManager::getServiceType(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& type) |
---|
239 | { |
---|
240 | int size ; |
---|
241 | int nbPartitions; |
---|
242 | int leader; |
---|
243 | return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader) ; |
---|
244 | } |
---|
245 | |
---|
246 | bool CServicesManager::getServiceNbPartitions(const std::string& poolId, const std::string& serviceId, const int& partitionId, int& nbPartitions) |
---|
247 | { |
---|
248 | int size ; |
---|
249 | int type; |
---|
250 | int leader; |
---|
251 | return getServiceInfo(poolId, serviceId, partitionId, type, size, nbPartitions, leader) ; |
---|
252 | } |
---|
253 | |
---|
254 | bool CServicesManager::hasService(const std::string& poolId, const std::string& serviceId, const int& partitionId) |
---|
255 | { |
---|
256 | winServices_->lockWindow(managerGlobalLeader_,0) ; |
---|
257 | winServices_->updateFromWindow(managerGlobalLeader_, this, &CServicesManager::servicesDumpIn) ; |
---|
258 | winServices_->unlockWindow(managerGlobalLeader_,0) ; |
---|
259 | auto it=services_.find(std::tuple<std::string, std::string, int>(poolId, serviceId, partitionId)) ; |
---|
260 | if ( it == services_.end()) return false ; |
---|
261 | else return true ; |
---|
262 | } |
---|
263 | |
---|
264 | } |
---|