comparison OrthancFramework/Sources/FileStorage/StorageAccessor.cpp @ 5791:7fadeb395359 default

fixed incremented metrics that were not published + added orthanc_storage_cache_miss_count & orthanc_storage_cache_hit_count
author Alain Mazy <am@orthanc.team>
date Wed, 18 Sep 2024 10:38:02 +0200
parents f7adfb22e20e
children
comparison
equal deleted inserted replaced
5767:b4fbd9d2c907 5791:7fadeb395359
42 static const std::string METRICS_CREATE_DURATION = "orthanc_storage_create_duration_ms"; 42 static const std::string METRICS_CREATE_DURATION = "orthanc_storage_create_duration_ms";
43 static const std::string METRICS_READ_DURATION = "orthanc_storage_read_duration_ms"; 43 static const std::string METRICS_READ_DURATION = "orthanc_storage_read_duration_ms";
44 static const std::string METRICS_REMOVE_DURATION = "orthanc_storage_remove_duration_ms"; 44 static const std::string METRICS_REMOVE_DURATION = "orthanc_storage_remove_duration_ms";
45 static const std::string METRICS_READ_BYTES = "orthanc_storage_read_bytes"; 45 static const std::string METRICS_READ_BYTES = "orthanc_storage_read_bytes";
46 static const std::string METRICS_WRITTEN_BYTES = "orthanc_storage_written_bytes"; 46 static const std::string METRICS_WRITTEN_BYTES = "orthanc_storage_written_bytes";
47 static const std::string METRICS_CACHE_HIT_COUNT = "orthanc_storage_cache_hit_count";
48 static const std::string METRICS_CACHE_MISS_COUNT = "orthanc_storage_cache_miss_count";
47 49
48 50
49 namespace Orthanc 51 namespace Orthanc
50 { 52 {
51 class StorageAccessor::MetricsTimer : public boost::noncopyable 53 class StorageAccessor::MetricsTimer : public boost::noncopyable
206 { 208 {
207 StorageCache::Accessor cacheAccessor(*cache_); 209 StorageCache::Accessor cacheAccessor(*cache_);
208 210
209 if (!cacheAccessor.Fetch(content, info.GetUuid(), info.GetContentType())) 211 if (!cacheAccessor.Fetch(content, info.GetUuid(), info.GetContentType()))
210 { 212 {
213 if (metrics_ != NULL)
214 {
215 metrics_->IncrementIntegerValue(METRICS_CACHE_MISS_COUNT, 1);
216 }
217
211 ReadWholeInternal(content, info); 218 ReadWholeInternal(content, info);
212 219
213 // always store the uncompressed data in cache 220 // always store the uncompressed data in cache
214 cacheAccessor.Add(info.GetUuid(), info.GetContentType(), content); 221 cacheAccessor.Add(info.GetUuid(), info.GetContentType(), content);
222 }
223 else if (metrics_ != NULL)
224 {
225 metrics_->IncrementIntegerValue(METRICS_CACHE_HIT_COUNT, 1);
215 } 226 }
216 } 227 }
217 } 228 }
218 229
219 void StorageAccessor::ReadWholeInternal(std::string& content, 230 void StorageAccessor::ReadWholeInternal(std::string& content,
282 {// use the cache only if the data is uncompressed. 293 {// use the cache only if the data is uncompressed.
283 StorageCache::Accessor cacheAccessor(*cache_); 294 StorageCache::Accessor cacheAccessor(*cache_);
284 295
285 if (!cacheAccessor.Fetch(content, info.GetUuid(), info.GetContentType())) 296 if (!cacheAccessor.Fetch(content, info.GetUuid(), info.GetContentType()))
286 { 297 {
298 if (metrics_ != NULL)
299 {
300 metrics_->IncrementIntegerValue(METRICS_CACHE_MISS_COUNT, 1);
301 }
302
287 ReadRawInternal(content, info); 303 ReadRawInternal(content, info);
288 304
289 cacheAccessor.Add(info.GetUuid(), info.GetContentType(), content); 305 cacheAccessor.Add(info.GetUuid(), info.GetContentType(), content);
306 }
307 else if (metrics_ != NULL)
308 {
309 metrics_->IncrementIntegerValue(METRICS_CACHE_HIT_COUNT, 1);
290 } 310 }
291 } 311 }
292 } 312 }
293 313
294 void StorageAccessor::ReadRawInternal(std::string& content, 314 void StorageAccessor::ReadRawInternal(std::string& content,
351 else 371 else
352 { 372 {
353 StorageCache::Accessor accessorStartRange(*cache_); 373 StorageCache::Accessor accessorStartRange(*cache_);
354 if (!accessorStartRange.FetchStartRange(target, info.GetUuid(), info.GetContentType(), end)) 374 if (!accessorStartRange.FetchStartRange(target, info.GetUuid(), info.GetContentType(), end))
355 { 375 {
356 ReadStartRangeInternal(target, info, end); 376 // the start range is not in cache, let's check if the whole file is
357 accessorStartRange.AddStartRange(info.GetUuid(), info.GetContentType(), target);
358 }
359 else
360 {
361 StorageCache::Accessor accessorWhole(*cache_); 377 StorageCache::Accessor accessorWhole(*cache_);
362 if (!accessorWhole.Fetch(target, info.GetUuid(), info.GetContentType())) 378 if (!accessorWhole.Fetch(target, info.GetUuid(), info.GetContentType()))
363 { 379 {
364 ReadWholeInternal(target, info); 380 if (metrics_ != NULL)
365 accessorWhole.Add(info.GetUuid(), info.GetContentType(), target); 381 {
366 } 382 metrics_->IncrementIntegerValue(METRICS_CACHE_MISS_COUNT, 1);
367 383 }
368 if (target.size() < end) 384
369 { 385 // if nothing is in the cache, let's read and cache only the start
370 throw OrthancException(ErrorCode_CorruptedFile); 386 ReadStartRangeInternal(target, info, end);
371 } 387 accessorStartRange.AddStartRange(info.GetUuid(), info.GetContentType(), target);
372 388 }
373 target.resize(end); 389 else
390 {
391 if (metrics_ != NULL)
392 {
393 metrics_->IncrementIntegerValue(METRICS_CACHE_HIT_COUNT, 1);
394 }
395
396 // we have read the whole file, check size and resize if needed
397 if (target.size() < end)
398 {
399 throw OrthancException(ErrorCode_CorruptedFile);
400 }
401
402 target.resize(end);
403 }
404 }
405 else if (metrics_ != NULL)
406 {
407 metrics_->IncrementIntegerValue(METRICS_CACHE_HIT_COUNT, 1);
374 } 408 }
375 } 409 }
376 } 410 }
377 411
378 void StorageAccessor::ReadStartRangeInternal(std::string& target, 412 void StorageAccessor::ReadStartRangeInternal(std::string& target,