Mercurial > hg > orthanc-stone
comparison Framework/Loaders/OracleScheduler.cpp @ 1228:c471a0aa137b broker
adding the next generation of loaders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Dec 2019 13:58:37 +0100 |
parents | |
children | 0ca50d275b9a |
comparison
equal
deleted
inserted
replaced
1227:a1c0c9c9f9af | 1228:c471a0aa137b |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "OracleScheduler.h" | |
23 | |
24 #include "../Oracle/ParseDicomFromFileCommand.h" | |
25 | |
26 namespace OrthancStone | |
27 { | |
28 class OracleScheduler::ReceiverPayload : public Orthanc::IDynamicObject | |
29 { | |
30 private: | |
31 Priority priority_; | |
32 boost::weak_ptr<IObserver> receiver_; | |
33 std::auto_ptr<IOracleCommand> command_; | |
34 | |
35 public: | |
36 ReceiverPayload(Priority priority, | |
37 boost::weak_ptr<IObserver> receiver, | |
38 IOracleCommand* command) : | |
39 priority_(priority), | |
40 receiver_(receiver), | |
41 command_(command) | |
42 { | |
43 if (command == NULL) | |
44 { | |
45 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
46 } | |
47 } | |
48 | |
49 Priority GetActivePriority() const | |
50 { | |
51 return priority_; | |
52 } | |
53 | |
54 boost::weak_ptr<IObserver> GetOriginalReceiver() const | |
55 { | |
56 return receiver_; | |
57 } | |
58 | |
59 const IOracleCommand& GetOriginalCommand() const | |
60 { | |
61 assert(command_.get() != NULL); | |
62 return *command_; | |
63 } | |
64 }; | |
65 | |
66 | |
67 class OracleScheduler::ScheduledCommand : public boost::noncopyable | |
68 { | |
69 private: | |
70 boost::weak_ptr<IObserver> receiver_; | |
71 std::auto_ptr<IOracleCommand> command_; | |
72 | |
73 public: | |
74 ScheduledCommand(boost::shared_ptr<IObserver> receiver, | |
75 IOracleCommand* command) : | |
76 receiver_(receiver), | |
77 command_(command) | |
78 { | |
79 if (command == NULL) | |
80 { | |
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
82 } | |
83 } | |
84 | |
85 boost::weak_ptr<IObserver> GetReceiver() | |
86 { | |
87 return receiver_; | |
88 } | |
89 | |
90 bool IsSameReceiver(boost::shared_ptr<OrthancStone::IObserver> receiver) const | |
91 { | |
92 boost::shared_ptr<IObserver> lock(receiver_.lock()); | |
93 | |
94 return (lock && | |
95 lock.get() == receiver.get()); | |
96 } | |
97 | |
98 IOracleCommand* WrapCommand(Priority priority) | |
99 { | |
100 if (command_.get() == NULL) | |
101 { | |
102 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
103 } | |
104 else | |
105 { | |
106 std::auto_ptr<IOracleCommand> wrapped(command_->Clone()); | |
107 dynamic_cast<OracleCommandBase&>(*wrapped).AcquirePayload(new ReceiverPayload(priority, receiver_, command_.release())); | |
108 return wrapped.release(); | |
109 } | |
110 } | |
111 }; | |
112 | |
113 | |
114 | |
115 void OracleScheduler::ClearQueue(Queue& queue) | |
116 { | |
117 for (Queue::iterator it = queue.begin(); it != queue.end(); ++it) | |
118 { | |
119 assert(it->second != NULL); | |
120 delete it->second; | |
121 | |
122 totalProcessed_ ++; | |
123 } | |
124 | |
125 queue.clear(); | |
126 } | |
127 | |
128 | |
129 void OracleScheduler::RemoveReceiverFromQueue(Queue& queue, | |
130 boost::shared_ptr<IObserver> receiver) | |
131 { | |
132 if (!receiver) | |
133 { | |
134 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
135 } | |
136 | |
137 Queue tmp; | |
138 | |
139 for (Queue::iterator it = queue.begin(); it != queue.end(); ++it) | |
140 { | |
141 assert(it->second != NULL); | |
142 | |
143 if (!(it->second->IsSameReceiver(receiver))) | |
144 { | |
145 // This promise is still active | |
146 tmp.insert(std::make_pair(it->first, it->second)); | |
147 } | |
148 else | |
149 { | |
150 delete it->second; | |
151 | |
152 totalProcessed_ ++; | |
153 } | |
154 } | |
155 | |
156 queue = tmp; | |
157 } | |
158 | |
159 | |
160 void OracleScheduler::CheckInvariants() const | |
161 { | |
162 #ifndef NDEBUG | |
163 /*char buf[1024]; | |
164 sprintf(buf, "active: %d %d %d ; pending: %lu %lu %lu", | |
165 activeHighPriorityCommands_, activeStandardPriorityCommands_, activeLowPriorityCommands_, | |
166 highPriorityQueue_.size(), standardPriorityQueue_.size(), lowPriorityQueue_.size()); | |
167 LOG(INFO) << buf;*/ | |
168 | |
169 assert(activeHighPriorityCommands_ <= maxHighPriorityCommands_); | |
170 assert(activeStandardPriorityCommands_ <= maxStandardPriorityCommands_); | |
171 assert(activeLowPriorityCommands_ <= maxLowPriorityCommands_); | |
172 assert(totalProcessed_ <= totalScheduled_); | |
173 | |
174 for (Queue::const_iterator it = standardPriorityQueue_.begin(); it != standardPriorityQueue_.end(); ++it) | |
175 { | |
176 assert(it->first > PRIORITY_HIGH && | |
177 it->first < PRIORITY_LOW); | |
178 } | |
179 | |
180 for (Queue::const_iterator it = highPriorityQueue_.begin(); it != highPriorityQueue_.end(); ++it) | |
181 { | |
182 assert(it->first <= PRIORITY_HIGH); | |
183 } | |
184 | |
185 for (Queue::const_iterator it = lowPriorityQueue_.begin(); it != lowPriorityQueue_.end(); ++it) | |
186 { | |
187 assert(it->first >= PRIORITY_LOW); | |
188 } | |
189 #endif | |
190 } | |
191 | |
192 | |
193 void OracleScheduler::SpawnFromQueue(Queue& queue, | |
194 Priority priority) | |
195 { | |
196 CheckInvariants(); | |
197 | |
198 Queue::iterator item = queue.begin(); | |
199 assert(item != queue.end()); | |
200 | |
201 std::auto_ptr<ScheduledCommand> command(dynamic_cast<ScheduledCommand*>(item->second)); | |
202 queue.erase(item); | |
203 | |
204 if (command.get() != NULL) | |
205 { | |
206 /** | |
207 * Only schedule the command for execution in the oracle, if its | |
208 * receiver has not been destroyed yet. | |
209 **/ | |
210 boost::shared_ptr<IObserver> observer(command->GetReceiver().lock()); | |
211 if (observer) | |
212 { | |
213 if (oracle_.Schedule(GetSharedObserver(), command->WrapCommand(priority))) | |
214 { | |
215 /** | |
216 * Executing this code if "Schedule()" returned "false" | |
217 * above, will result in a memory leak within | |
218 * "OracleScheduler", as the scheduler believes that some | |
219 * command is still active (i.e. pending to be executed by | |
220 * the oracle), hereby stalling the scheduler during its | |
221 * destruction, and not freeing the | |
222 * "shared_ptr<OracleScheduler>" of the Stone context (check | |
223 * out "sjo-playground/WebViewer/Backend/Leak") | |
224 **/ | |
225 | |
226 switch (priority) | |
227 { | |
228 case Priority_High: | |
229 activeHighPriorityCommands_ ++; | |
230 break; | |
231 | |
232 case Priority_Standard: | |
233 activeStandardPriorityCommands_ ++; | |
234 break; | |
235 | |
236 case Priority_Low: | |
237 activeLowPriorityCommands_ ++; | |
238 break; | |
239 | |
240 default: | |
241 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
242 } | |
243 } | |
244 else | |
245 { | |
246 totalProcessed_ ++; | |
247 } | |
248 } | |
249 } | |
250 else | |
251 { | |
252 LOG(ERROR) << "NULL command, should never happen"; | |
253 } | |
254 | |
255 CheckInvariants(); | |
256 } | |
257 | |
258 | |
259 void OracleScheduler::SpawnCommands() | |
260 { | |
261 // Send as many commands as possible to the oracle | |
262 while (!highPriorityQueue_.empty()) | |
263 { | |
264 if (activeHighPriorityCommands_ < maxHighPriorityCommands_) | |
265 { | |
266 // First fill the high-priority lane | |
267 SpawnFromQueue(highPriorityQueue_, Priority_High); | |
268 } | |
269 else if (activeStandardPriorityCommands_ < maxStandardPriorityCommands_) | |
270 { | |
271 // There remain too many high-priority commands for the | |
272 // high-priority lane, schedule them to the standard-priority lanes | |
273 SpawnFromQueue(highPriorityQueue_, Priority_Standard); | |
274 } | |
275 else if (activeLowPriorityCommands_ < maxLowPriorityCommands_) | |
276 { | |
277 SpawnFromQueue(highPriorityQueue_, Priority_Low); | |
278 } | |
279 else | |
280 { | |
281 return; // No slot available | |
282 } | |
283 } | |
284 | |
285 while (!standardPriorityQueue_.empty()) | |
286 { | |
287 if (activeStandardPriorityCommands_ < maxStandardPriorityCommands_) | |
288 { | |
289 SpawnFromQueue(standardPriorityQueue_, Priority_Standard); | |
290 } | |
291 else if (activeLowPriorityCommands_ < maxLowPriorityCommands_) | |
292 { | |
293 SpawnFromQueue(standardPriorityQueue_, Priority_Low); | |
294 } | |
295 else | |
296 { | |
297 return; | |
298 } | |
299 } | |
300 | |
301 while (!lowPriorityQueue_.empty()) | |
302 { | |
303 if (activeLowPriorityCommands_ < maxLowPriorityCommands_) | |
304 { | |
305 SpawnFromQueue(lowPriorityQueue_, Priority_Low); | |
306 } | |
307 else | |
308 { | |
309 return; | |
310 } | |
311 } | |
312 } | |
313 | |
314 | |
315 void OracleScheduler::RemoveActiveCommand(const ReceiverPayload& payload) | |
316 { | |
317 CheckInvariants(); | |
318 | |
319 totalProcessed_ ++; | |
320 | |
321 switch (payload.GetActivePriority()) | |
322 { | |
323 case Priority_High: | |
324 assert(activeHighPriorityCommands_ > 0); | |
325 activeHighPriorityCommands_ --; | |
326 break; | |
327 | |
328 case Priority_Standard: | |
329 assert(activeStandardPriorityCommands_ > 0); | |
330 activeStandardPriorityCommands_ --; | |
331 break; | |
332 | |
333 case Priority_Low: | |
334 assert(activeLowPriorityCommands_ > 0); | |
335 activeLowPriorityCommands_ --; | |
336 break; | |
337 | |
338 default: | |
339 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
340 } | |
341 | |
342 SpawnCommands(); | |
343 | |
344 CheckInvariants(); | |
345 } | |
346 | |
347 | |
348 void OracleScheduler::Handle(const GetOrthancImageCommand::SuccessMessage& message) | |
349 { | |
350 assert(message.GetOrigin().HasPayload()); | |
351 const ReceiverPayload& payload = dynamic_cast<const ReceiverPayload&>(message.GetOrigin().GetPayload()); | |
352 | |
353 RemoveActiveCommand(payload); | |
354 | |
355 GetOrthancImageCommand::SuccessMessage bis( | |
356 dynamic_cast<const GetOrthancImageCommand&>(payload.GetOriginalCommand()), | |
357 message.GetImage(), message.GetMimeType()); | |
358 emitter_.EmitMessage(payload.GetOriginalReceiver(), bis); | |
359 } | |
360 | |
361 | |
362 void OracleScheduler::Handle(const GetOrthancWebViewerJpegCommand::SuccessMessage& message) | |
363 { | |
364 assert(message.GetOrigin().HasPayload()); | |
365 const ReceiverPayload& payload = dynamic_cast<const ReceiverPayload&>(message.GetOrigin().GetPayload()); | |
366 | |
367 RemoveActiveCommand(payload); | |
368 | |
369 GetOrthancWebViewerJpegCommand::SuccessMessage bis( | |
370 dynamic_cast<const GetOrthancWebViewerJpegCommand&>(payload.GetOriginalCommand()), | |
371 message.GetImage()); | |
372 emitter_.EmitMessage(payload.GetOriginalReceiver(), bis); | |
373 } | |
374 | |
375 | |
376 void OracleScheduler::Handle(const HttpCommand::SuccessMessage& message) | |
377 { | |
378 assert(message.GetOrigin().HasPayload()); | |
379 const ReceiverPayload& payload = dynamic_cast<const ReceiverPayload&>(message.GetOrigin().GetPayload()); | |
380 | |
381 RemoveActiveCommand(payload); | |
382 | |
383 HttpCommand::SuccessMessage bis( | |
384 dynamic_cast<const HttpCommand&>(payload.GetOriginalCommand()), | |
385 message.GetAnswerHeaders(), message.GetAnswer()); | |
386 emitter_.EmitMessage(payload.GetOriginalReceiver(), bis); | |
387 } | |
388 | |
389 | |
390 void OracleScheduler::Handle(const OrthancRestApiCommand::SuccessMessage& message) | |
391 { | |
392 assert(message.GetOrigin().HasPayload()); | |
393 const ReceiverPayload& payload = dynamic_cast<const ReceiverPayload&>(message.GetOrigin().GetPayload()); | |
394 | |
395 RemoveActiveCommand(payload); | |
396 | |
397 OrthancRestApiCommand::SuccessMessage bis( | |
398 dynamic_cast<const OrthancRestApiCommand&>(payload.GetOriginalCommand()), | |
399 message.GetAnswerHeaders(), message.GetAnswer()); | |
400 emitter_.EmitMessage(payload.GetOriginalReceiver(), bis); | |
401 } | |
402 | |
403 | |
404 #if ORTHANC_ENABLE_DCMTK == 1 | |
405 void OracleScheduler::Handle(const ParseDicomSuccessMessage& message) | |
406 { | |
407 assert(message.GetOrigin().HasPayload()); | |
408 const ReceiverPayload& payload = dynamic_cast<const ReceiverPayload&>(message.GetOrigin().GetPayload()); | |
409 | |
410 RemoveActiveCommand(payload); | |
411 | |
412 ParseDicomSuccessMessage bis( | |
413 dynamic_cast<const OracleCommandBase&>(payload.GetOriginalCommand()), | |
414 message.GetDicom(), message.GetFileSize(), message.HasPixelData()); | |
415 emitter_.EmitMessage(payload.GetOriginalReceiver(), bis); | |
416 } | |
417 #endif | |
418 | |
419 | |
420 void OracleScheduler::Handle(const ReadFileCommand::SuccessMessage& message) | |
421 { | |
422 assert(message.GetOrigin().HasPayload()); | |
423 const ReceiverPayload& payload = dynamic_cast<const ReceiverPayload&>(message.GetOrigin().GetPayload()); | |
424 | |
425 RemoveActiveCommand(payload); | |
426 | |
427 ReadFileCommand::SuccessMessage bis( | |
428 dynamic_cast<const ReadFileCommand&>(payload.GetOriginalCommand()), | |
429 message.GetContent()); | |
430 emitter_.EmitMessage(payload.GetOriginalReceiver(), bis); | |
431 } | |
432 | |
433 | |
434 void OracleScheduler::Handle(const OracleCommandExceptionMessage& message) | |
435 { | |
436 const OracleCommandBase& command = dynamic_cast<const OracleCommandBase&>(message.GetOrigin()); | |
437 | |
438 assert(command.HasPayload()); | |
439 const ReceiverPayload& payload = dynamic_cast<const ReceiverPayload&>(command.GetPayload()); | |
440 | |
441 RemoveActiveCommand(payload); | |
442 | |
443 OracleCommandExceptionMessage bis(payload.GetOriginalCommand(), message.GetException()); | |
444 emitter_.EmitMessage(payload.GetOriginalReceiver(), bis); | |
445 } | |
446 | |
447 | |
448 OracleScheduler::OracleScheduler(IOracle& oracle, | |
449 IMessageEmitter& emitter, | |
450 unsigned int maxHighPriority, | |
451 unsigned int maxStandardPriority, | |
452 unsigned int maxLowPriority) : | |
453 oracle_(oracle), | |
454 emitter_(emitter), | |
455 maxHighPriorityCommands_(maxHighPriority), | |
456 maxStandardPriorityCommands_(maxStandardPriority), | |
457 maxLowPriorityCommands_(maxLowPriority), | |
458 activeHighPriorityCommands_(0), | |
459 activeStandardPriorityCommands_(0), | |
460 activeLowPriorityCommands_(0), | |
461 totalScheduled_(0), | |
462 totalProcessed_(0) | |
463 { | |
464 assert(PRIORITY_HIGH < 0 && | |
465 PRIORITY_LOW > 0); | |
466 | |
467 if (maxLowPriority <= 0) | |
468 { | |
469 // There must be at least 1 lane available to deal with low-priority commands | |
470 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
471 } | |
472 } | |
473 | |
474 | |
475 boost::shared_ptr<OracleScheduler> OracleScheduler::Create(IOracle& oracle, | |
476 IObservable& oracleObservable, | |
477 IMessageEmitter& emitter, | |
478 unsigned int maxHighPriority, | |
479 unsigned int maxStandardPriority, | |
480 unsigned int maxLowPriority) | |
481 { | |
482 boost::shared_ptr<OracleScheduler> scheduler | |
483 (new OracleScheduler(oracle, emitter, maxHighPriority, maxStandardPriority, maxLowPriority)); | |
484 scheduler->Register<GetOrthancImageCommand::SuccessMessage>(oracleObservable, &OracleScheduler::Handle); | |
485 scheduler->Register<GetOrthancWebViewerJpegCommand::SuccessMessage>(oracleObservable, &OracleScheduler::Handle); | |
486 scheduler->Register<HttpCommand::SuccessMessage>(oracleObservable, &OracleScheduler::Handle); | |
487 scheduler->Register<OrthancRestApiCommand::SuccessMessage>(oracleObservable, &OracleScheduler::Handle); | |
488 scheduler->Register<ReadFileCommand::SuccessMessage>(oracleObservable, &OracleScheduler::Handle); | |
489 scheduler->Register<OracleCommandExceptionMessage>(oracleObservable, &OracleScheduler::Handle); | |
490 | |
491 #if ORTHANC_ENABLE_DCMTK == 1 | |
492 scheduler->Register<ParseDicomSuccessMessage>(oracleObservable, &OracleScheduler::Handle); | |
493 #endif | |
494 | |
495 return scheduler; | |
496 } | |
497 | |
498 | |
499 OracleScheduler::~OracleScheduler() | |
500 { | |
501 CancelAllRequests(); | |
502 } | |
503 | |
504 | |
505 void OracleScheduler::CancelRequests(boost::shared_ptr<IObserver> receiver) | |
506 { | |
507 RemoveReceiverFromQueue(standardPriorityQueue_, receiver); | |
508 RemoveReceiverFromQueue(highPriorityQueue_, receiver); | |
509 RemoveReceiverFromQueue(lowPriorityQueue_, receiver); | |
510 } | |
511 | |
512 | |
513 void OracleScheduler::CancelAllRequests() | |
514 { | |
515 ClearQueue(standardPriorityQueue_); | |
516 ClearQueue(highPriorityQueue_); | |
517 ClearQueue(lowPriorityQueue_); | |
518 } | |
519 | |
520 | |
521 void OracleScheduler::Schedule(boost::shared_ptr<IObserver> receiver, | |
522 int priority, | |
523 IOracleCommand* command /* Takes ownership */) | |
524 { | |
525 std::auto_ptr<ScheduledCommand> pending(new ScheduledCommand(receiver, dynamic_cast<IOracleCommand*>(command))); | |
526 | |
527 /** | |
528 * Safeguard to remember that a new "Handle()" method and a call | |
529 * to "scheduler->Register()" must be implemented for each | |
530 * possible oracle command. | |
531 **/ | |
532 assert(command->GetType() == IOracleCommand::Type_GetOrthancImage || | |
533 command->GetType() == IOracleCommand::Type_GetOrthancWebViewerJpeg || | |
534 command->GetType() == IOracleCommand::Type_Http || | |
535 command->GetType() == IOracleCommand::Type_OrthancRestApi || | |
536 command->GetType() == IOracleCommand::Type_ParseDicomFromFile || | |
537 command->GetType() == IOracleCommand::Type_ParseDicomFromWado || | |
538 command->GetType() == IOracleCommand::Type_ReadFile); | |
539 | |
540 if (priority <= PRIORITY_HIGH) | |
541 { | |
542 highPriorityQueue_.insert(std::make_pair(priority, pending.release())); | |
543 } | |
544 else if (priority >= PRIORITY_LOW) | |
545 { | |
546 lowPriorityQueue_.insert(std::make_pair(priority, pending.release())); | |
547 } | |
548 else | |
549 { | |
550 standardPriorityQueue_.insert(std::make_pair(priority, pending.release())); | |
551 } | |
552 | |
553 totalScheduled_ ++; | |
554 | |
555 SpawnCommands(); | |
556 } | |
557 } |