comparison OrthancServer/Sources/ServerIndex.cpp @ 5603:b2a97dfd719f

monitoring of stable resources now also considers the resource type
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 08 May 2024 10:29:35 +0200
parents 4dd50c4b985a
children f7adfb22e20e
comparison
equal deleted inserted replaced
5599:3487684fd331 5603:b2a97dfd719f
183 { 183 {
184 return false; 184 return false;
185 } 185 }
186 }; 186 };
187 187
188 virtual void MarkAsUnstable(int64_t id, 188 virtual void MarkAsUnstable(ResourceType type,
189 Orthanc::ResourceType type, 189 int64_t id,
190 const std::string& publicId) ORTHANC_OVERRIDE 190 const std::string& publicId) ORTHANC_OVERRIDE
191 { 191 {
192 context_.GetIndex().MarkAsUnstable(id, type, publicId); 192 context_.GetIndex().MarkAsUnstable(type, id, publicId);
193 } 193 }
194 194
195 virtual bool IsUnstableResource(int64_t id) ORTHANC_OVERRIDE 195 virtual bool IsUnstableResource(ResourceType type,
196 { 196 int64_t id) ORTHANC_OVERRIDE
197 return context_.GetIndex().IsUnstableResource(id); 197 {
198 return context_.GetIndex().IsUnstableResource(type, id);
198 } 199 }
199 200
200 virtual void Commit() ORTHANC_OVERRIDE 201 virtual void Commit() ORTHANC_OVERRIDE
201 { 202 {
202 // We can remove the files once the SQLite transaction has 203 // We can remove the files once the SQLite transaction has
237 238
238 239
239 class ServerIndex::UnstableResourcePayload 240 class ServerIndex::UnstableResourcePayload
240 { 241 {
241 private: 242 private:
242 ResourceType type_;
243 std::string publicId_; 243 std::string publicId_;
244 boost::posix_time::ptime time_; 244 boost::posix_time::ptime time_;
245 245
246 public: 246 public:
247 UnstableResourcePayload() : type_(ResourceType_Instance) 247 UnstableResourcePayload()
248 { 248 {
249 } 249 }
250 250
251 UnstableResourcePayload(Orthanc::ResourceType type, 251 explicit UnstableResourcePayload(const std::string& publicId) :
252 const std::string& publicId) :
253 type_(type),
254 publicId_(publicId), 252 publicId_(publicId),
255 time_(boost::posix_time::second_clock::local_time()) 253 time_(boost::posix_time::second_clock::local_time())
256 { 254 {
257 } 255 }
258 256
259 unsigned int GetAge() const 257 unsigned int GetAge() const
260 { 258 {
261 return (boost::posix_time::second_clock::local_time() - time_).total_seconds(); 259 return (boost::posix_time::second_clock::local_time() - time_).total_seconds();
262 }
263
264 ResourceType GetResourceType() const
265 {
266 return type_;
267 } 260 }
268 261
269 const std::string& GetPublicId() const 262 const std::string& GetPublicId() const
270 { 263 {
271 return publicId_; 264 return publicId_;
307 300
308 LOG(INFO) << "Stopping the database flushing thread"; 301 LOG(INFO) << "Stopping the database flushing thread";
309 } 302 }
310 303
311 304
312 bool ServerIndex::IsUnstableResource(int64_t id) 305 bool ServerIndex::IsUnstableResource(ResourceType type,
306 int64_t id)
313 { 307 {
314 boost::mutex::scoped_lock lock(monitoringMutex_); 308 boost::mutex::scoped_lock lock(monitoringMutex_);
315 return unstableResources_.Contains(id); 309 return unstableResources_.Contains(std::make_pair(type, id));
316 } 310 }
317 311
318 312
319 ServerIndex::ServerIndex(ServerContext& context, 313 ServerIndex::ServerIndex(ServerContext& context,
320 IDatabaseWrapper& db, 314 IDatabaseWrapper& db,
458 // Check for stable resources each few seconds 452 // Check for stable resources each few seconds
459 boost::this_thread::sleep(boost::posix_time::milliseconds(threadSleepGranularityMilliseconds)); 453 boost::this_thread::sleep(boost::posix_time::milliseconds(threadSleepGranularityMilliseconds));
460 454
461 for (;;) 455 for (;;)
462 { 456 {
463 UnstableResourcePayload stableResource; 457 UnstableResourcePayload stablePayload;
458 ResourceType stableLevel;
464 int64_t stableId; 459 int64_t stableId;
465 460
466 { 461 {
467 boost::mutex::scoped_lock lock(that->monitoringMutex_); 462 boost::mutex::scoped_lock lock(that->monitoringMutex_);
468 463
469 if (!that->unstableResources_.IsEmpty() && 464 if (!that->unstableResources_.IsEmpty() &&
470 that->unstableResources_.GetOldestPayload().GetAge() > static_cast<unsigned int>(stableAge)) 465 that->unstableResources_.GetOldestPayload().GetAge() > static_cast<unsigned int>(stableAge))
471 { 466 {
472 // This DICOM resource has not received any new instance for 467 // This DICOM resource has not received any new instance for
473 // some time. It can be considered as stable. 468 // some time. It can be considered as stable.
474 stableId = that->unstableResources_.RemoveOldest(stableResource); 469 std::pair<ResourceType, int64_t> stableResource = that->unstableResources_.RemoveOldest(stablePayload);
475 //LOG(TRACE) << "Stable resource: " << EnumerationToString(stableResource.GetResourceType()) << " " << stableId; 470 stableLevel = stableResource.first;
471 stableId = stableResource.second;
472 //LOG(TRACE) << "Stable resource: " << EnumerationToString(stablePayload.GetResourceType()) << " " << stableId;
476 } 473 }
477 else 474 else
478 { 475 {
479 // No more stable DICOM resource, leave the internal loop 476 // No more stable DICOM resource, leave the internal loop
480 break; 477 break;
488 * "monitoringMutex_", as this could lead to deadlocks in 485 * "monitoringMutex_", as this could lead to deadlocks in
489 * other threads (typically, if "Store()" is being running in 486 * other threads (typically, if "Store()" is being running in
490 * another thread, which leads to calls to "MarkAsUnstable()", 487 * another thread, which leads to calls to "MarkAsUnstable()",
491 * which leads to two lockings of "monitoringMutex_"). 488 * which leads to two lockings of "monitoringMutex_").
492 **/ 489 **/
493 switch (stableResource.GetResourceType()) 490 switch (stableLevel)
494 { 491 {
495 case ResourceType_Patient: 492 case ResourceType_Patient:
496 that->LogChange(stableId, ChangeType_StablePatient, stableResource.GetPublicId(), ResourceType_Patient); 493 that->LogChange(stableId, ChangeType_StablePatient, stablePayload.GetPublicId(), ResourceType_Patient);
497 break; 494 break;
498 495
499 case ResourceType_Study: 496 case ResourceType_Study:
500 that->LogChange(stableId, ChangeType_StableStudy, stableResource.GetPublicId(), ResourceType_Study); 497 that->LogChange(stableId, ChangeType_StableStudy, stablePayload.GetPublicId(), ResourceType_Study);
501 break; 498 break;
502 499
503 case ResourceType_Series: 500 case ResourceType_Series:
504 that->LogChange(stableId, ChangeType_StableSeries, stableResource.GetPublicId(), ResourceType_Series); 501 that->LogChange(stableId, ChangeType_StableSeries, stablePayload.GetPublicId(), ResourceType_Series);
505 break; 502 break;
506 503
507 default: 504 default:
508 throw OrthancException(ErrorCode_InternalError); 505 throw OrthancException(ErrorCode_InternalError);
509 } 506 }
517 514
518 LOG(INFO) << "Closing the monitor thread for stable resources"; 515 LOG(INFO) << "Closing the monitor thread for stable resources";
519 } 516 }
520 517
521 518
522 void ServerIndex::MarkAsUnstable(int64_t id, 519 void ServerIndex::MarkAsUnstable(ResourceType type,
523 Orthanc::ResourceType type, 520 int64_t id,
524 const std::string& publicId) 521 const std::string& publicId)
525 { 522 {
526 assert(type == Orthanc::ResourceType_Patient || 523 assert(type == ResourceType_Patient ||
527 type == Orthanc::ResourceType_Study || 524 type == ResourceType_Study ||
528 type == Orthanc::ResourceType_Series); 525 type == ResourceType_Series);
529 526
530 { 527 {
531 boost::mutex::scoped_lock lock(monitoringMutex_); 528 boost::mutex::scoped_lock lock(monitoringMutex_);
532 UnstableResourcePayload payload(type, publicId); 529 UnstableResourcePayload payload(publicId);
533 unstableResources_.AddOrMakeMostRecent(id, payload); 530 unstableResources_.AddOrMakeMostRecent(std::make_pair(type, id), payload);
534 //LOG(INFO) << "Unstable resource: " << EnumerationToString(type) << " " << id; 531 //LOG(INFO) << "Unstable resource: " << EnumerationToString(type) << " " << id;
535 } 532 }
536 } 533 }
537 534
538 535