aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs')
-rw-r--r--OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs161
1 files changed, 60 insertions, 101 deletions
diff --git a/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs b/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs
index f1fee63..b270de9 100644
--- a/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs
+++ b/OpenSim/Region/CoreModules/Asset/FlotsamAssetCache.cs
@@ -248,71 +248,68 @@ namespace OpenSim.Region.CoreModules.Asset
248 248
249 private void UpdateFileCache(string key, AssetBase asset) 249 private void UpdateFileCache(string key, AssetBase asset)
250 { 250 {
251 // TODO: Spawn this off to some seperate thread to do the actual writing 251 string filename = GetFileName(key);
252 if (asset != null)
253 {
254 string filename = GetFileName(key);
255 252
256 try 253 try
254 {
255 // If the file is already cached, don't cache it, just touch it so access time is updated
256 if (File.Exists(filename))
257 { 257 {
258 // If the file is already cached, don't cache it, just touch it so access time is updated 258 // We don't really want to know about sharing
259 if (File.Exists(filename)) 259 // violations here. If the file is locked, then
260 // the other thread has updated the time for us.
261 try
260 { 262 {
261 // We don't really want to know about sharing 263 lock (m_CurrentlyWriting)
262 // violations here. If the file is locked, then
263 // the other thread has updated the time for us.
264 try
265 { 264 {
266 lock (m_CurrentlyWriting) 265 if (!m_CurrentlyWriting.Contains(filename))
267 { 266 File.SetLastAccessTime(filename, DateTime.Now);
268 if (!m_CurrentlyWriting.Contains(filename))
269 File.SetLastAccessTime(filename, DateTime.Now);
270 }
271 } 267 }
272 catch 268 }
269 catch
270 {
271 }
272 }
273 else
274 {
275 // Once we start writing, make sure we flag that we're writing
276 // that object to the cache so that we don't try to write the
277 // same file multiple times.
278 lock (m_CurrentlyWriting)
279 {
280#if WAIT_ON_INPROGRESS_REQUESTS
281 if (m_CurrentlyWriting.ContainsKey(filename))
273 { 282 {
283 return;
274 } 284 }
275 } else { 285 else
276
277 // Once we start writing, make sure we flag that we're writing
278 // that object to the cache so that we don't try to write the
279 // same file multiple times.
280 lock (m_CurrentlyWriting)
281 { 286 {
282#if WAIT_ON_INPROGRESS_REQUESTS 287 m_CurrentlyWriting.Add(filename, new ManualResetEvent(false));
283 if (m_CurrentlyWriting.ContainsKey(filename)) 288 }
284 {
285 return;
286 }
287 else
288 {
289 m_CurrentlyWriting.Add(filename, new ManualResetEvent(false));
290 }
291 289
292#else 290#else
293 if (m_CurrentlyWriting.Contains(filename)) 291 if (m_CurrentlyWriting.Contains(filename))
294 { 292 {
295 return; 293 return;
296 }
297 else
298 {
299 m_CurrentlyWriting.Add(filename);
300 }
301#endif
302
303 } 294 }
295 else
296 {
297 m_CurrentlyWriting.Add(filename);
298 }
299#endif
304 300
305 Util.FireAndForget(
306 delegate { WriteFileCache(filename, asset); });
307 } 301 }
308 } 302
309 catch (Exception e) 303 Util.FireAndForget(
310 { 304 delegate { WriteFileCache(filename, asset); });
311 m_log.ErrorFormat(
312 "[FLOTSAM ASSET CACHE]: Failed to update cache for asset {0}. Exception {1} {2}",
313 asset.ID, e.Message, e.StackTrace);
314 } 305 }
315 } 306 }
307 catch (Exception e)
308 {
309 m_log.ErrorFormat(
310 "[FLOTSAM ASSET CACHE]: Failed to update cache for asset {0}. Exception {1} {2}",
311 asset.ID, e.Message, e.StackTrace);
312 }
316 } 313 }
317 314
318 public void Cache(AssetBase asset) 315 public void Cache(AssetBase asset)
@@ -347,15 +344,9 @@ namespace OpenSim.Region.CoreModules.Asset
347 344
348 private bool CheckFromMemoryCache(string id) 345 private bool CheckFromMemoryCache(string id)
349 { 346 {
350 AssetBase asset = null; 347 return m_MemoryCache.Contains(id);
351
352 if (m_MemoryCache.TryGetValue(id, out asset))
353 return true;
354
355 return false;
356 } 348 }
357 349
358
359 /// <summary> 350 /// <summary>
360 /// Try to get an asset from the file cache. 351 /// Try to get an asset from the file cache.
361 /// </summary> 352 /// </summary>
@@ -393,15 +384,16 @@ namespace OpenSim.Region.CoreModules.Asset
393 384
394 if (File.Exists(filename)) 385 if (File.Exists(filename))
395 { 386 {
396 FileStream stream = null;
397 try 387 try
398 { 388 {
399 stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read); 389 using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
400 BinaryFormatter bformatter = new BinaryFormatter(); 390 {
391 BinaryFormatter bformatter = new BinaryFormatter();
401 392
402 asset = (AssetBase)bformatter.Deserialize(stream); 393 asset = (AssetBase)bformatter.Deserialize(stream);
403 394
404 m_DiskHits++; 395 m_DiskHits++;
396 }
405 } 397 }
406 catch (System.Runtime.Serialization.SerializationException e) 398 catch (System.Runtime.Serialization.SerializationException e)
407 { 399 {
@@ -420,12 +412,6 @@ namespace OpenSim.Region.CoreModules.Asset
420 m_log.WarnFormat( 412 m_log.WarnFormat(
421 "[FLOTSAM ASSET CACHE]: Failed to get file {0} for asset {1}. Exception {2} {3}", 413 "[FLOTSAM ASSET CACHE]: Failed to get file {0} for asset {1}. Exception {2} {3}",
422 filename, id, e.Message, e.StackTrace); 414 filename, id, e.Message, e.StackTrace);
423
424 }
425 finally
426 {
427 if (stream != null)
428 stream.Close();
429 } 415 }
430 } 416 }
431 417
@@ -437,36 +423,19 @@ namespace OpenSim.Region.CoreModules.Asset
437 bool found = false; 423 bool found = false;
438 424
439 string filename = GetFileName(id); 425 string filename = GetFileName(id);
426
440 if (File.Exists(filename)) 427 if (File.Exists(filename))
441 { 428 {
442 // actually check if we can open it, and so update expire
443 FileStream stream = null;
444 try 429 try
445 { 430 {
446 stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read); 431 using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
447 if (stream != null)
448 { 432 {
449 found = true; 433 if (stream != null)
450 stream.Close(); 434 found = true;
451 } 435 }
452
453 }
454 catch (System.Runtime.Serialization.SerializationException e)
455 {
456 found = false;
457 m_log.ErrorFormat(
458 "[FLOTSAM ASSET CACHE]: Failed to check file {0} for asset {1}. Exception {2} {3}",
459 filename, id, e.Message, e.StackTrace);
460
461 // If there was a problem deserializing the asset, the asset may
462 // either be corrupted OR was serialized under an old format
463 // {different version of AssetBase} -- we should attempt to
464 // delete it and re-cache
465 File.Delete(filename);
466 } 436 }
467 catch (Exception e) 437 catch (Exception e)
468 { 438 {
469 found = false;
470 m_log.ErrorFormat( 439 m_log.ErrorFormat(
471 "[FLOTSAM ASSET CACHE]: Failed to check file {0} for asset {1}. Exception {2} {3}", 440 "[FLOTSAM ASSET CACHE]: Failed to check file {0} for asset {1}. Exception {2} {3}",
472 filename, id, e.Message, e.StackTrace); 441 filename, id, e.Message, e.StackTrace);
@@ -518,11 +487,6 @@ namespace OpenSim.Region.CoreModules.Asset
518 return Get(id); 487 return Get(id);
519 } 488 }
520 489
521 public AssetBase CheckCached(string id)
522 {
523 return Get(id);
524 }
525
526 public void Expire(string id) 490 public void Expire(string id)
527 { 491 {
528 if (m_LogLevel >= 2) 492 if (m_LogLevel >= 2)
@@ -807,7 +771,7 @@ namespace OpenSim.Region.CoreModules.Asset
807 UuidGatherer gatherer = new UuidGatherer(m_AssetService); 771 UuidGatherer gatherer = new UuidGatherer(m_AssetService);
808 772
809 HashSet<UUID> uniqueUuids = new HashSet<UUID>(); 773 HashSet<UUID> uniqueUuids = new HashSet<UUID>();
810 Dictionary<UUID, AssetType> assets = new Dictionary<UUID, AssetType>(); 774 Dictionary<UUID, sbyte> assets = new Dictionary<UUID, sbyte>();
811 775
812 foreach (Scene s in m_Scenes) 776 foreach (Scene s in m_Scenes)
813 { 777 {
@@ -830,7 +794,7 @@ namespace OpenSim.Region.CoreModules.Asset
830 else if (storeUncached) 794 else if (storeUncached)
831 { 795 {
832 AssetBase cachedAsset = m_AssetService.Get(assetID.ToString()); 796 AssetBase cachedAsset = m_AssetService.Get(assetID.ToString());
833 if (cachedAsset == null && assets[assetID] != AssetType.Unknown) 797 if (cachedAsset == null && assets[assetID] != (sbyte)AssetType.Unknown)
834 m_log.DebugFormat( 798 m_log.DebugFormat(
835 "[FLOTSAM ASSET CACHE]: Could not find asset {0}, type {1} referenced by object {2} at {3} in scene {4} when pre-caching all scene assets", 799 "[FLOTSAM ASSET CACHE]: Could not find asset {0}, type {1} referenced by object {2} at {3} in scene {4} when pre-caching all scene assets",
836 assetID, assets[assetID], e.Name, e.AbsolutePosition, s.Name); 800 assetID, assets[assetID], e.Name, e.AbsolutePosition, s.Name);
@@ -1067,11 +1031,6 @@ namespace OpenSim.Region.CoreModules.Asset
1067 return asset.Data; 1031 return asset.Data;
1068 } 1032 }
1069 1033
1070 public bool CheckData(string id)
1071 {
1072 return Check(id); ;
1073 }
1074
1075 public bool Get(string id, object sender, AssetRetrieved handler) 1034 public bool Get(string id, object sender, AssetRetrieved handler)
1076 { 1035 {
1077 AssetBase asset = Get(id); 1036 AssetBase asset = Get(id);