@@ -50,13 +50,13 @@ namespace loader {
50
50
51
51
CentralClient::CentralClient (boost::asio::io_service& ioService_,
52
52
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() ),
54
54
_hostName (hostName), _udpPort(cfg.getClientPortUdp()),
55
55
_defWorkerHost(cfg.getDefWorkerHost()),
56
56
_defWorkerPortUdp(cfg.getDefWorkerPortUdp()),
57
57
_doListMaxLookups(cfg.getMaxLookups()),
58
58
_doListMaxInserts(cfg.getMaxInserts()),
59
- _maxRequestSleepTime(cfg.getMaxRequestSleepTime()){
59
+ _maxRequestSleepTime(cfg.getMaxRequestSleepTime()) {
60
60
}
61
61
62
62
@@ -65,45 +65,45 @@ void CentralClient::start() {
65
65
}
66
66
67
67
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 " );
70
70
71
- StringElement:: Ptr sData = std::dynamic_pointer_cast<StringElement>(MsgElement::retrieve (*data));
71
+ auto const sData = std::dynamic_pointer_cast<StringElement>(MsgElement::retrieve (*data));
72
72
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" );
74
74
return ;
75
75
}
76
76
auto protoData = sData ->protoParse <proto::KeyInfo>();
77
77
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" );
79
79
return ;
80
80
}
81
81
82
82
// TODO put in separate thread
83
- _handleKeyInfo (inMsg, protoData);
83
+ _handleKeyLookup (inMsg, protoData);
84
84
}
85
85
86
86
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) {
88
88
std::unique_ptr<proto::KeyInfo> protoData (std::move (protoBuf));
89
89
90
90
CompositeKey key (protoData->keyint (), protoData->keystr ());
91
91
ChunkSubchunk chunkInfo (protoData->chunk (), protoData->subchunk ());
92
92
93
93
LOGS (_log, LOG_LVL_DEBUG, " trying to remove oneShot for lookup key=" << key << " " << chunkInfo);
94
94
// / Locate the original one shot and mark it as done.
95
- CentralClient::KeyInfoReqOneShot ::Ptr keyInfoOneShot ;
95
+ CentralClient::KeyLookupReqOneShot ::Ptr keyLookupOneShot ;
96
96
{
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);
101
101
return ;
102
102
}
103
- keyInfoOneShot = iter->second ;
104
- _waitingKeyInfoMap .erase (iter);
103
+ keyLookupOneShot = iter->second ;
104
+ _waitingKeyLookupMap .erase (iter);
105
105
}
106
- keyInfoOneShot ->keyInfoComplete (key, chunkInfo.chunk , chunkInfo.subchunk , protoData->success ());
106
+ keyLookupOneShot ->keyInfoComplete (key, chunkInfo.chunk , chunkInfo.subchunk , protoData->success ());
107
107
LOGS (_log, LOG_LVL_INFO, " Successfully found key=" << key << " " << chunkInfo);
108
108
}
109
109
@@ -139,14 +139,14 @@ void CentralClient::_handleKeyInsertComplete(LoaderMsg const& inMsg, std::unique
139
139
size_t mapSize;
140
140
{
141
141
std::lock_guard<std::mutex> lck (_waitingKeyInsertMtx);
142
- mapSize = _waitingKeyInsertMap.size ();
143
142
auto iter = _waitingKeyInsertMap.find (key);
144
143
if (iter == _waitingKeyInsertMap.end ()) {
145
144
LOGS (_log, LOG_LVL_WARN, " handleKeyInsertComplete could not find key=" << key);
146
145
return ;
147
146
}
148
147
keyInsertOneShot = iter->second ;
149
148
_waitingKeyInsertMap.erase (iter);
149
+ mapSize = _waitingKeyInsertMap.size ();
150
150
}
151
151
keyInsertOneShot->keyInsertComplete ();
152
152
LOGS (_log, LOG_LVL_INFO, " Successfully inserted key=" << key << " " << chunkInfo <<
@@ -178,6 +178,7 @@ KeyInfoData::Ptr CentralClient::keyInsertReq(CompositeKey const& key, int chunk,
178
178
LOGS (_log, LOG_LVL_INFO, " keyInsertReq waiting key=" << key <<
179
179
" size=" << sz << " loopCount=" << loopCount);
180
180
}
181
+ // Let the CPU do something else while waiting for some requests to finish.
181
182
usleep (_maxRequestSleepTime);
182
183
++loopCount;
183
184
lck.lock ();
@@ -230,60 +231,64 @@ void CentralClient::_keyInsertReq(CompositeKey const& key, int chunk, int subchu
230
231
strElem.appendToData (msgData);
231
232
try {
232
233
sendBufferTo (getDefWorkerHost (), getDefWorkerPortUdp (), msgData);
233
- } catch (boost::system ::system_error e) {
234
+ } catch (boost::system ::system_error const & e) {
234
235
LOGS (_log, LOG_LVL_ERROR, " CentralClient::_keyInsertReq boost system_error=" << e.what () <<
235
236
" 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.
238
237
}
239
238
}
240
239
241
240
242
- KeyInfoData::Ptr CentralClient::keyInfoReq (CompositeKey const & key) {
241
+ KeyInfoData::Ptr CentralClient::keyLookupReq (CompositeKey const & key) {
243
242
// Returns a pointer to a Tracker object that can be used to track job
244
243
// completion and job status. keyInsertOneShot will call _keyInsertReq until
245
244
// it knows the task was completed. _handleKeyInfoComplete marks
246
245
// the jobs complete as the messages come in from workers.
247
246
// Insert a oneShot DoListItem to keep trying to add the key until
248
247
// we get word that it has been added successfully.
249
248
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);
251
250
{
252
- std::unique_lock<std::mutex> lck (_waitingKeyInfoMtx );
251
+ std::unique_lock<std::mutex> lck (_waitingKeyLookupMtx );
253
252
// Limit the number of concurrent lookups.
254
253
// If the key is already in the map, there is no point in blocking.
255
254
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 ();
260
261
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 ) {
262
265
LOGS (_log, LOG_LVL_INFO, " keyInfoReq waiting key=" << key <<
263
266
" size=" << sz << " loopCount=" << loopCount);
264
267
}
268
+ // Let the CPU do something else while waiting for some requests to finish.
265
269
usleep (_maxRequestSleepTime);
270
+ sleptForMicroSec += _maxRequestSleepTime;
266
271
++loopCount;
267
272
lck.lock ();
268
- iter = _waitingKeyInfoMap .find (key);
273
+ iter = _waitingKeyLookupMap .find (key);
269
274
}
270
275
271
276
// Use the existing lookup, if there is one.
272
- if (iter != _waitingKeyInfoMap .end ()) {
277
+ if (iter != _waitingKeyLookupMap .end ()) {
273
278
auto cData = iter->second ->cmdData ;
274
279
return cData;
275
280
}
276
281
277
- _waitingKeyInfoMap [key] = keyInfoOneShot ;
282
+ _waitingKeyLookupMap [key] = keyLookupOneShot ;
278
283
}
279
- runAndAddDoListItem (keyInfoOneShot );
280
- return keyInfoOneShot ->cmdData ;
284
+ runAndAddDoListItem (keyLookupOneShot );
285
+ return keyLookupOneShot ->cmdData ;
281
286
}
282
287
283
288
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 ());
287
292
BufferUdp msgData;
288
293
msg.appendToData (msgData);
289
294
// create the proto buffer
@@ -305,11 +310,9 @@ void CentralClient::_keyInfoReq(CompositeKey const& key) {
305
310
306
311
try {
307
312
sendBufferTo (getDefWorkerHost (), getDefWorkerPortUdp (), msgData);
308
- } catch (boost::system ::system_error e) {
313
+ } catch (boost::system ::system_error const & e) {
309
314
LOGS (_log, LOG_LVL_ERROR, " CentralClient::_keyInfoReq boost system_error=" << e.what () <<
310
315
" 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.
313
316
}
314
317
}
315
318
@@ -341,11 +344,11 @@ void CentralClient::KeyInsertReqOneShot::keyInsertComplete() {
341
344
}
342
345
343
346
344
- util::CommandTracked::Ptr CentralClient::KeyInfoReqOneShot ::createCommand () {
347
+ util::CommandTracked::Ptr CentralClient::KeyLookupReqOneShot ::createCommand () {
345
348
struct KeyInfoReqCmd : public util ::CommandTracked {
346
349
KeyInfoReqCmd (KeyInfoData::Ptr & cd, CentralClient* cent_) : cData(cd), cent(cent_) {}
347
350
void action (util::CmdData*) override {
348
- cent->_keyInfoReq (cData->key );
351
+ cent->_keyLookupReq (cData->key );
349
352
}
350
353
KeyInfoData::Ptr cData;
351
354
CentralClient* cent;
@@ -354,7 +357,7 @@ util::CommandTracked::Ptr CentralClient::KeyInfoReqOneShot::createCommand() {
354
357
}
355
358
356
359
357
- void CentralClient::KeyInfoReqOneShot ::keyInfoComplete (CompositeKey const & key,
360
+ void CentralClient::KeyLookupReqOneShot ::keyInfoComplete (CompositeKey const & key,
358
361
int chunk, int subchunk, bool success) {
359
362
if (key == cmdData->key ) {
360
363
cmdData->chunk = chunk;
0 commit comments