@@ -50,13 +50,13 @@ namespace loader {
5050
5151CentralClient::CentralClient (boost::asio::io_service& ioService_,
5252 std::string const & hostName, ClientConfig const & cfg)
53- : Central(ioService_, cfg.getMasterHost(), cfg.getMasterPortUdp(), cfg.getThreadPoolSize(), cfg.getLoopSleepTime()),
53+ : Central(ioService_, cfg.getMasterHost(), cfg.getMasterPortUdp(), cfg.getThreadPoolSize(), cfg.getLoopSleepTime(), cfg.getIOThreads() ),
5454 _hostName (hostName), _udpPort(cfg.getClientPortUdp()),
5555 _defWorkerHost(cfg.getDefWorkerHost()),
5656 _defWorkerPortUdp(cfg.getDefWorkerPortUdp()),
5757 _doListMaxLookups(cfg.getMaxLookups()),
5858 _doListMaxInserts(cfg.getMaxInserts()),
59- _maxRequestSleepTime(cfg.getMaxRequestSleepTime()){
59+ _maxRequestSleepTime(cfg.getMaxRequestSleepTime()) {
6060}
6161
6262
@@ -65,45 +65,45 @@ void CentralClient::start() {
6565}
6666
6767
68- void CentralClient::handleKeyInfo (LoaderMsg const & inMsg, BufferUdp::Ptr const & data) {
69- LOGS (_log, LOG_LVL_DEBUG, " CentralClient::handleKeyInfo " );
68+ void CentralClient::handleKeyLookup (LoaderMsg const & inMsg, BufferUdp::Ptr const & data) {
69+ LOGS (_log, LOG_LVL_DEBUG, " CentralClient::handleKeyLookup " );
7070
71- StringElement::Ptr sData = std::dynamic_pointer_cast<StringElement>(MsgElement::retrieve (*data));
71+ auto const sData = std::dynamic_pointer_cast<StringElement>(MsgElement::retrieve (*data));
7272 if (sData == nullptr ) {
73- LOGS (_log, LOG_LVL_WARN, " CentralClient::handleKeyInsertComplete Failed to parse list" );
73+ LOGS (_log, LOG_LVL_WARN, " CentralClient::handleKeyLookup Failed to parse list" );
7474 return ;
7575 }
7676 auto protoData = sData ->protoParse <proto::KeyInfo>();
7777 if (protoData == nullptr ) {
78- LOGS (_log, LOG_LVL_WARN, " CentralClient::handleKeyInsertComplete Failed to parse list" );
78+ LOGS (_log, LOG_LVL_WARN, " CentralClient::handleKeyLookup Failed to parse list" );
7979 return ;
8080 }
8181
8282 // TODO put in separate thread
83- _handleKeyInfo (inMsg, protoData);
83+ _handleKeyLookup (inMsg, protoData);
8484}
8585
8686
87- void CentralClient::_handleKeyInfo (LoaderMsg const & inMsg, std::unique_ptr<proto::KeyInfo>& protoBuf) {
87+ void CentralClient::_handleKeyLookup (LoaderMsg const & inMsg, std::unique_ptr<proto::KeyInfo>& protoBuf) {
8888 std::unique_ptr<proto::KeyInfo> protoData (std::move (protoBuf));
8989
9090 CompositeKey key (protoData->keyint (), protoData->keystr ());
9191 ChunkSubchunk chunkInfo (protoData->chunk (), protoData->subchunk ());
9292
9393 LOGS (_log, LOG_LVL_DEBUG, " trying to remove oneShot for lookup key=" << key << " " << chunkInfo);
9494 // / Locate the original one shot and mark it as done.
95- CentralClient::KeyInfoReqOneShot ::Ptr keyInfoOneShot ;
95+ CentralClient::KeyLookupReqOneShot ::Ptr keyLookupOneShot ;
9696 {
97- std::lock_guard<std::mutex> lck (_waitingKeyInfoMtx );
98- auto iter = _waitingKeyInfoMap .find (key);
99- if (iter == _waitingKeyInfoMap .end ()) {
100- LOGS (_log, LOG_LVL_WARN, " handleKeyInfoComplete could not find key=" << key);
97+ std::lock_guard<std::mutex> lck (_waitingKeyLookupMtx );
98+ auto iter = _waitingKeyLookupMap .find (key);
99+ if (iter == _waitingKeyLookupMap .end ()) {
100+ LOGS (_log, LOG_LVL_WARN, " _handleKeyLookup could not find key=" << key);
101101 return ;
102102 }
103- keyInfoOneShot = iter->second ;
104- _waitingKeyInfoMap .erase (iter);
103+ keyLookupOneShot = iter->second ;
104+ _waitingKeyLookupMap .erase (iter);
105105 }
106- keyInfoOneShot ->keyInfoComplete (key, chunkInfo.chunk , chunkInfo.subchunk , protoData->success ());
106+ keyLookupOneShot ->keyInfoComplete (key, chunkInfo.chunk , chunkInfo.subchunk , protoData->success ());
107107 LOGS (_log, LOG_LVL_INFO, " Successfully found key=" << key << " " << chunkInfo);
108108}
109109
@@ -139,14 +139,14 @@ void CentralClient::_handleKeyInsertComplete(LoaderMsg const& inMsg, std::unique
139139 size_t mapSize;
140140 {
141141 std::lock_guard<std::mutex> lck (_waitingKeyInsertMtx);
142- mapSize = _waitingKeyInsertMap.size ();
143142 auto iter = _waitingKeyInsertMap.find (key);
144143 if (iter == _waitingKeyInsertMap.end ()) {
145144 LOGS (_log, LOG_LVL_WARN, " handleKeyInsertComplete could not find key=" << key);
146145 return ;
147146 }
148147 keyInsertOneShot = iter->second ;
149148 _waitingKeyInsertMap.erase (iter);
149+ mapSize = _waitingKeyInsertMap.size ();
150150 }
151151 keyInsertOneShot->keyInsertComplete ();
152152 LOGS (_log, LOG_LVL_INFO, " Successfully inserted key=" << key << " " << chunkInfo <<
@@ -178,6 +178,7 @@ KeyInfoData::Ptr CentralClient::keyInsertReq(CompositeKey const& key, int chunk,
178178 LOGS (_log, LOG_LVL_INFO, " keyInsertReq waiting key=" << key <<
179179 " size=" << sz << " loopCount=" << loopCount);
180180 }
181+ // Let the CPU do something else while waiting for some requests to finish.
181182 usleep (_maxRequestSleepTime);
182183 ++loopCount;
183184 lck.lock ();
@@ -230,60 +231,64 @@ void CentralClient::_keyInsertReq(CompositeKey const& key, int chunk, int subchu
230231 strElem.appendToData (msgData);
231232 try {
232233 sendBufferTo (getDefWorkerHost (), getDefWorkerPortUdp (), msgData);
233- } catch (boost::system::system_error e) {
234+ } catch (boost::system::system_error const & e) {
234235 LOGS (_log, LOG_LVL_ERROR, " CentralClient::_keyInsertReq boost system_error=" << e.what () <<
235236 " key=" << key << " chunk=" << chunk << " sub=" << subchunk);
236- exit (-1 ); // TODO:&&& The correct course of action is unclear and requires thought,
237- // so just blow up so it's unmistakable something bad happened for now.
238237 }
239238}
240239
241240
242- KeyInfoData::Ptr CentralClient::keyInfoReq (CompositeKey const & key) {
241+ KeyInfoData::Ptr CentralClient::keyLookupReq (CompositeKey const & key) {
243242 // Returns a pointer to a Tracker object that can be used to track job
244243 // completion and job status. keyInsertOneShot will call _keyInsertReq until
245244 // it knows the task was completed. _handleKeyInfoComplete marks
246245 // the jobs complete as the messages come in from workers.
247246 // Insert a oneShot DoListItem to keep trying to add the key until
248247 // we get word that it has been added successfully.
249248 LOGS (_log, LOG_LVL_INFO, " Trying to lookup key=" << key);
250- auto keyInfoOneShot = std::make_shared<CentralClient::KeyInfoReqOneShot >(this , key);
249+ auto keyLookupOneShot = std::make_shared<CentralClient::KeyLookupReqOneShot >(this , key);
251250 {
252- std::unique_lock<std::mutex> lck (_waitingKeyInfoMtx );
251+ std::unique_lock<std::mutex> lck (_waitingKeyLookupMtx );
253252 // Limit the number of concurrent lookups.
254253 // If the key is already in the map, there is no point in blocking.
255254 int loopCount = 0 ;
256- auto iter = _waitingKeyInfoMap.find (key);
257- while (_waitingKeyInfoMap.size () > _doListMaxLookups
258- && iter == _waitingKeyInfoMap.end ()) {
259- size_t sz = _waitingKeyInfoMap.size ();
255+ uint64_t sleptForMicroSec = 0 ;
256+ uint64_t const tenSec = 10000000 ;
257+ auto iter = _waitingKeyLookupMap.find (key);
258+ while (_waitingKeyLookupMap.size () > _doListMaxLookups
259+ && iter == _waitingKeyLookupMap.end ()) {
260+ size_t sz = _waitingKeyLookupMap.size ();
260261 lck.unlock ();
261- if (loopCount % 100 == 0 ) {
262+ // Log a message about this about once every 10 seconds.
263+ if (sleptForMicroSec > tenSec) sleptForMicroSec = 0 ;
264+ if (sleptForMicroSec == 0 ) {
262265 LOGS (_log, LOG_LVL_INFO, " keyInfoReq waiting key=" << key <<
263266 " size=" << sz << " loopCount=" << loopCount);
264267 }
268+ // Let the CPU do something else while waiting for some requests to finish.
265269 usleep (_maxRequestSleepTime);
270+ sleptForMicroSec += _maxRequestSleepTime;
266271 ++loopCount;
267272 lck.lock ();
268- iter = _waitingKeyInfoMap .find (key);
273+ iter = _waitingKeyLookupMap .find (key);
269274 }
270275
271276 // Use the existing lookup, if there is one.
272- if (iter != _waitingKeyInfoMap .end ()) {
277+ if (iter != _waitingKeyLookupMap .end ()) {
273278 auto cData = iter->second ->cmdData ;
274279 return cData;
275280 }
276281
277- _waitingKeyInfoMap [key] = keyInfoOneShot ;
282+ _waitingKeyLookupMap [key] = keyLookupOneShot ;
278283 }
279- runAndAddDoListItem (keyInfoOneShot );
280- return keyInfoOneShot ->cmdData ;
284+ runAndAddDoListItem (keyLookupOneShot );
285+ return keyLookupOneShot ->cmdData ;
281286}
282287
283288
284- void CentralClient::_keyInfoReq (CompositeKey const & key) {
285- LOGS (_log, LOG_LVL_INFO, " CentralClient::_keyInfoReq trying key=" << key);
286- LoaderMsg msg (LoaderMsg::KEY_INFO_REQ , getNextMsgId (), getHostName (), getUdpPort ());
289+ void CentralClient::_keyLookupReq (CompositeKey const & key) {
290+ LOGS (_log, LOG_LVL_INFO, " CentralClient::_keyLookupReq trying key=" << key);
291+ LoaderMsg msg (LoaderMsg::KEY_LOOKUP_REQ , getNextMsgId (), getHostName (), getUdpPort ());
287292 BufferUdp msgData;
288293 msg.appendToData (msgData);
289294 // create the proto buffer
@@ -305,11 +310,9 @@ void CentralClient::_keyInfoReq(CompositeKey const& key) {
305310
306311 try {
307312 sendBufferTo (getDefWorkerHost (), getDefWorkerPortUdp (), msgData);
308- } catch (boost::system::system_error e) {
313+ } catch (boost::system::system_error const & e) {
309314 LOGS (_log, LOG_LVL_ERROR, " CentralClient::_keyInfoReq boost system_error=" << e.what () <<
310315 " key=" << key);
311- exit (-1 ); // TODO:&&& The correct course of action is unclear and requires thought.
312- // So just blow up so it's unmistakable something bad happened for now.
313316 }
314317}
315318
@@ -341,11 +344,11 @@ void CentralClient::KeyInsertReqOneShot::keyInsertComplete() {
341344}
342345
343346
344- util::CommandTracked::Ptr CentralClient::KeyInfoReqOneShot ::createCommand () {
347+ util::CommandTracked::Ptr CentralClient::KeyLookupReqOneShot ::createCommand () {
345348 struct KeyInfoReqCmd : public util ::CommandTracked {
346349 KeyInfoReqCmd (KeyInfoData::Ptr& cd, CentralClient* cent_) : cData(cd), cent(cent_) {}
347350 void action (util::CmdData*) override {
348- cent->_keyInfoReq (cData->key );
351+ cent->_keyLookupReq (cData->key );
349352 }
350353 KeyInfoData::Ptr cData;
351354 CentralClient* cent;
@@ -354,7 +357,7 @@ util::CommandTracked::Ptr CentralClient::KeyInfoReqOneShot::createCommand() {
354357}
355358
356359
357- void CentralClient::KeyInfoReqOneShot ::keyInfoComplete (CompositeKey const & key,
360+ void CentralClient::KeyLookupReqOneShot ::keyInfoComplete (CompositeKey const & key,
358361 int chunk, int subchunk, bool success) {
359362 if (key == cmdData->key ) {
360363 cmdData->chunk = chunk;
0 commit comments