FlotsamAssetCache.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // Uncomment to make asset Get requests for existing
  28. // #define WAIT_ON_INPROGRESS_REQUESTS
  29. using System;
  30. using System.IO;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Reflection;
  34. using System.Runtime.Serialization;
  35. using System.Runtime.Serialization.Formatters.Binary;
  36. using System.Threading;
  37. using System.Timers;
  38. using log4net;
  39. using Nini.Config;
  40. using Mono.Addins;
  41. using OpenMetaverse;
  42. using OpenSim.Framework;
  43. using OpenSim.Framework.Console;
  44. using OpenSim.Region.Framework.Interfaces;
  45. using OpenSim.Region.Framework.Scenes;
  46. using OpenSim.Services.Interfaces;
  47. //[assembly: Addin("FlotsamAssetCache", "1.1")]
  48. //[assembly: AddinDependency("OpenSim", "0.5")]
  49. namespace OpenSim.Region.CoreModules.Asset
  50. {
  51. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "FlotsamAssetCache")]
  52. public class FlotsamAssetCache : ISharedRegionModule, IImprovedAssetCache, IAssetService
  53. {
  54. private static readonly ILog m_log =
  55. LogManager.GetLogger(
  56. MethodBase.GetCurrentMethod().DeclaringType);
  57. private bool m_Enabled;
  58. private const string m_ModuleName = "FlotsamAssetCache";
  59. private const string m_DefaultCacheDirectory = "./assetcache";
  60. private string m_CacheDirectory = m_DefaultCacheDirectory;
  61. private readonly List<char> m_InvalidChars = new List<char>();
  62. private int m_LogLevel = 0;
  63. private ulong m_HitRateDisplay = 100; // How often to display hit statistics, given in requests
  64. private static ulong m_Requests;
  65. private static ulong m_RequestsForInprogress;
  66. private static ulong m_DiskHits;
  67. private static ulong m_MemoryHits;
  68. #if WAIT_ON_INPROGRESS_REQUESTS
  69. private Dictionary<string, ManualResetEvent> m_CurrentlyWriting = new Dictionary<string, ManualResetEvent>();
  70. private int m_WaitOnInprogressTimeout = 3000;
  71. #else
  72. private HashSet<string> m_CurrentlyWriting = new HashSet<string>();
  73. #endif
  74. private bool m_FileCacheEnabled = true;
  75. private ExpiringCache<string, AssetBase> m_MemoryCache;
  76. private bool m_MemoryCacheEnabled = false;
  77. // Expiration is expressed in hours.
  78. private const double m_DefaultMemoryExpiration = 2;
  79. private const double m_DefaultFileExpiration = 48;
  80. private TimeSpan m_MemoryExpiration = TimeSpan.FromHours(m_DefaultMemoryExpiration);
  81. private TimeSpan m_FileExpiration = TimeSpan.FromHours(m_DefaultFileExpiration);
  82. private TimeSpan m_FileExpirationCleanupTimer = TimeSpan.FromHours(0.166);
  83. private static int m_CacheDirectoryTiers = 1;
  84. private static int m_CacheDirectoryTierLen = 3;
  85. private static int m_CacheWarnAt = 30000;
  86. private System.Timers.Timer m_CacheCleanTimer;
  87. private IAssetService m_AssetService;
  88. private List<Scene> m_Scenes = new List<Scene>();
  89. public FlotsamAssetCache()
  90. {
  91. m_InvalidChars.AddRange(Path.GetInvalidPathChars());
  92. m_InvalidChars.AddRange(Path.GetInvalidFileNameChars());
  93. }
  94. public Type ReplaceableInterface
  95. {
  96. get { return null; }
  97. }
  98. public string Name
  99. {
  100. get { return m_ModuleName; }
  101. }
  102. public void Initialise(IConfigSource source)
  103. {
  104. IConfig moduleConfig = source.Configs["Modules"];
  105. if (moduleConfig != null)
  106. {
  107. string name = moduleConfig.GetString("AssetCaching", String.Empty);
  108. if (name == Name)
  109. {
  110. m_MemoryCache = new ExpiringCache<string, AssetBase>();
  111. m_Enabled = true;
  112. m_log.InfoFormat("[FLOTSAM ASSET CACHE]: {0} enabled", this.Name);
  113. IConfig assetConfig = source.Configs["AssetCache"];
  114. if (assetConfig == null)
  115. {
  116. m_log.Debug(
  117. "[FLOTSAM ASSET CACHE]: AssetCache section missing from config (not copied config-include/FlotsamCache.ini.example? Using defaults.");
  118. }
  119. else
  120. {
  121. m_FileCacheEnabled = assetConfig.GetBoolean("FileCacheEnabled", m_FileCacheEnabled);
  122. m_CacheDirectory = assetConfig.GetString("CacheDirectory", m_DefaultCacheDirectory);
  123. m_MemoryCacheEnabled = assetConfig.GetBoolean("MemoryCacheEnabled", m_MemoryCacheEnabled);
  124. m_MemoryExpiration = TimeSpan.FromHours(assetConfig.GetDouble("MemoryCacheTimeout", m_DefaultMemoryExpiration));
  125. #if WAIT_ON_INPROGRESS_REQUESTS
  126. m_WaitOnInprogressTimeout = assetConfig.GetInt("WaitOnInprogressTimeout", 3000);
  127. #endif
  128. m_LogLevel = assetConfig.GetInt("LogLevel", m_LogLevel);
  129. m_HitRateDisplay = (ulong)assetConfig.GetLong("HitRateDisplay", (long)m_HitRateDisplay);
  130. m_FileExpiration = TimeSpan.FromHours(assetConfig.GetDouble("FileCacheTimeout", m_DefaultFileExpiration));
  131. m_FileExpirationCleanupTimer
  132. = TimeSpan.FromHours(
  133. assetConfig.GetDouble("FileCleanupTimer", m_FileExpirationCleanupTimer.TotalHours));
  134. m_CacheDirectoryTiers = assetConfig.GetInt("CacheDirectoryTiers", m_CacheDirectoryTiers);
  135. m_CacheDirectoryTierLen = assetConfig.GetInt("CacheDirectoryTierLength", m_CacheDirectoryTierLen);
  136. m_CacheWarnAt = assetConfig.GetInt("CacheWarnAt", m_CacheWarnAt);
  137. }
  138. m_log.InfoFormat("[FLOTSAM ASSET CACHE]: Cache Directory {0}", m_CacheDirectory);
  139. if (m_FileCacheEnabled && (m_FileExpiration > TimeSpan.Zero) && (m_FileExpirationCleanupTimer > TimeSpan.Zero))
  140. {
  141. m_CacheCleanTimer = new System.Timers.Timer(m_FileExpirationCleanupTimer.TotalMilliseconds);
  142. m_CacheCleanTimer.AutoReset = true;
  143. m_CacheCleanTimer.Elapsed += CleanupExpiredFiles;
  144. lock (m_CacheCleanTimer)
  145. m_CacheCleanTimer.Start();
  146. }
  147. if (m_CacheDirectoryTiers < 1)
  148. {
  149. m_CacheDirectoryTiers = 1;
  150. }
  151. else if (m_CacheDirectoryTiers > 3)
  152. {
  153. m_CacheDirectoryTiers = 3;
  154. }
  155. if (m_CacheDirectoryTierLen < 1)
  156. {
  157. m_CacheDirectoryTierLen = 1;
  158. }
  159. else if (m_CacheDirectoryTierLen > 4)
  160. {
  161. m_CacheDirectoryTierLen = 4;
  162. }
  163. MainConsole.Instance.Commands.AddCommand("Assets", true, "fcache status", "fcache status", "Display cache status", HandleConsoleCommand);
  164. MainConsole.Instance.Commands.AddCommand("Assets", true, "fcache clear", "fcache clear [file] [memory]", "Remove all assets in the cache. If file or memory is specified then only this cache is cleared.", HandleConsoleCommand);
  165. MainConsole.Instance.Commands.AddCommand("Assets", true, "fcache assets", "fcache assets", "Attempt a deep scan and cache of all assets in all scenes", HandleConsoleCommand);
  166. MainConsole.Instance.Commands.AddCommand("Assets", true, "fcache expire", "fcache expire <datetime>", "Purge cached assets older then the specified date/time", HandleConsoleCommand);
  167. }
  168. }
  169. }
  170. public void PostInitialise()
  171. {
  172. }
  173. public void Close()
  174. {
  175. }
  176. public void AddRegion(Scene scene)
  177. {
  178. if (m_Enabled)
  179. {
  180. scene.RegisterModuleInterface<IImprovedAssetCache>(this);
  181. m_Scenes.Add(scene);
  182. }
  183. }
  184. public void RemoveRegion(Scene scene)
  185. {
  186. if (m_Enabled)
  187. {
  188. scene.UnregisterModuleInterface<IImprovedAssetCache>(this);
  189. m_Scenes.Remove(scene);
  190. }
  191. }
  192. public void RegionLoaded(Scene scene)
  193. {
  194. if (m_Enabled && m_AssetService == null)
  195. m_AssetService = scene.RequestModuleInterface<IAssetService>();
  196. }
  197. ////////////////////////////////////////////////////////////
  198. // IImprovedAssetCache
  199. //
  200. private void UpdateMemoryCache(string key, AssetBase asset)
  201. {
  202. m_MemoryCache.AddOrUpdate(key, asset, m_MemoryExpiration);
  203. }
  204. private void UpdateFileCache(string key, AssetBase asset)
  205. {
  206. string filename = GetFileName(key);
  207. try
  208. {
  209. // If the file is already cached, don't cache it, just touch it so access time is updated
  210. if (File.Exists(filename))
  211. {
  212. // We don't really want to know about sharing
  213. // violations here. If the file is locked, then
  214. // the other thread has updated the time for us.
  215. try
  216. {
  217. lock (m_CurrentlyWriting)
  218. {
  219. if (!m_CurrentlyWriting.Contains(filename))
  220. File.SetLastAccessTime(filename, DateTime.Now);
  221. }
  222. }
  223. catch
  224. {
  225. }
  226. }
  227. else
  228. {
  229. // Once we start writing, make sure we flag that we're writing
  230. // that object to the cache so that we don't try to write the
  231. // same file multiple times.
  232. lock (m_CurrentlyWriting)
  233. {
  234. #if WAIT_ON_INPROGRESS_REQUESTS
  235. if (m_CurrentlyWriting.ContainsKey(filename))
  236. {
  237. return;
  238. }
  239. else
  240. {
  241. m_CurrentlyWriting.Add(filename, new ManualResetEvent(false));
  242. }
  243. #else
  244. if (m_CurrentlyWriting.Contains(filename))
  245. {
  246. return;
  247. }
  248. else
  249. {
  250. m_CurrentlyWriting.Add(filename);
  251. }
  252. #endif
  253. }
  254. Util.FireAndForget(
  255. delegate { WriteFileCache(filename, asset); });
  256. }
  257. }
  258. catch (Exception e)
  259. {
  260. m_log.ErrorFormat(
  261. "[FLOTSAM ASSET CACHE]: Failed to update cache for asset {0}. Exception {1} {2}",
  262. asset.ID, e.Message, e.StackTrace);
  263. }
  264. }
  265. public void Cache(AssetBase asset)
  266. {
  267. // TODO: Spawn this off to some seperate thread to do the actual writing
  268. if (asset != null)
  269. {
  270. //m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Caching asset with id {0}", asset.ID);
  271. if (m_MemoryCacheEnabled)
  272. UpdateMemoryCache(asset.ID, asset);
  273. if (m_FileCacheEnabled)
  274. UpdateFileCache(asset.ID, asset);
  275. }
  276. }
  277. /// <summary>
  278. /// Try to get an asset from the in-memory cache.
  279. /// </summary>
  280. /// <param name="id"></param>
  281. /// <returns></returns>
  282. private AssetBase GetFromMemoryCache(string id)
  283. {
  284. AssetBase asset = null;
  285. if (m_MemoryCache.TryGetValue(id, out asset))
  286. m_MemoryHits++;
  287. return asset;
  288. }
  289. private bool CheckFromMemoryCache(string id)
  290. {
  291. return m_MemoryCache.Contains(id);
  292. }
  293. /// <summary>
  294. /// Try to get an asset from the file cache.
  295. /// </summary>
  296. /// <param name="id"></param>
  297. /// <returns>An asset retrieved from the file cache. null if there was a problem retrieving an asset.</returns>
  298. private AssetBase GetFromFileCache(string id)
  299. {
  300. string filename = GetFileName(id);
  301. #if WAIT_ON_INPROGRESS_REQUESTS
  302. // Check if we're already downloading this asset. If so, try to wait for it to
  303. // download.
  304. if (m_WaitOnInprogressTimeout > 0)
  305. {
  306. m_RequestsForInprogress++;
  307. ManualResetEvent waitEvent;
  308. if (m_CurrentlyWriting.TryGetValue(filename, out waitEvent))
  309. {
  310. waitEvent.WaitOne(m_WaitOnInprogressTimeout);
  311. return Get(id);
  312. }
  313. }
  314. #else
  315. // Track how often we have the problem that an asset is requested while
  316. // it is still being downloaded by a previous request.
  317. if (m_CurrentlyWriting.Contains(filename))
  318. {
  319. m_RequestsForInprogress++;
  320. return null;
  321. }
  322. #endif
  323. AssetBase asset = null;
  324. if (File.Exists(filename))
  325. {
  326. try
  327. {
  328. using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  329. {
  330. BinaryFormatter bformatter = new BinaryFormatter();
  331. asset = (AssetBase)bformatter.Deserialize(stream);
  332. m_DiskHits++;
  333. }
  334. }
  335. catch (System.Runtime.Serialization.SerializationException e)
  336. {
  337. m_log.WarnFormat(
  338. "[FLOTSAM ASSET CACHE]: Failed to get file {0} for asset {1}. Exception {2} {3}",
  339. filename, id, e.Message, e.StackTrace);
  340. // If there was a problem deserializing the asset, the asset may
  341. // either be corrupted OR was serialized under an old format
  342. // {different version of AssetBase} -- we should attempt to
  343. // delete it and re-cache
  344. File.Delete(filename);
  345. }
  346. catch (Exception e)
  347. {
  348. m_log.WarnFormat(
  349. "[FLOTSAM ASSET CACHE]: Failed to get file {0} for asset {1}. Exception {2} {3}",
  350. filename, id, e.Message, e.StackTrace);
  351. }
  352. }
  353. return asset;
  354. }
  355. private bool CheckFromFileCache(string id)
  356. {
  357. bool found = false;
  358. string filename = GetFileName(id);
  359. if (File.Exists(filename))
  360. {
  361. try
  362. {
  363. using (FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
  364. {
  365. if (stream != null)
  366. found = true;
  367. }
  368. }
  369. catch (Exception e)
  370. {
  371. m_log.ErrorFormat(
  372. "[FLOTSAM ASSET CACHE]: Failed to check file {0} for asset {1}. Exception {2} {3}",
  373. filename, id, e.Message, e.StackTrace);
  374. }
  375. }
  376. return found;
  377. }
  378. public AssetBase Get(string id)
  379. {
  380. m_Requests++;
  381. AssetBase asset = null;
  382. if (m_MemoryCacheEnabled)
  383. asset = GetFromMemoryCache(id);
  384. if (asset == null && m_FileCacheEnabled)
  385. {
  386. asset = GetFromFileCache(id);
  387. if (m_MemoryCacheEnabled && asset != null)
  388. UpdateMemoryCache(id, asset);
  389. }
  390. if (((m_LogLevel >= 1)) && (m_HitRateDisplay != 0) && (m_Requests % m_HitRateDisplay == 0))
  391. {
  392. m_log.InfoFormat("[FLOTSAM ASSET CACHE]: Cache Get :: {0} :: {1}", id, asset == null ? "Miss" : "Hit");
  393. GenerateCacheHitReport().ForEach(l => m_log.InfoFormat("[FLOTSAM ASSET CACHE]: {0}", l));
  394. }
  395. return asset;
  396. }
  397. public bool Check(string id)
  398. {
  399. if (m_MemoryCacheEnabled && CheckFromMemoryCache(id))
  400. return true;
  401. if (m_FileCacheEnabled && CheckFromFileCache(id))
  402. return true;
  403. return false;
  404. }
  405. public AssetBase GetCached(string id)
  406. {
  407. return Get(id);
  408. }
  409. public void Expire(string id)
  410. {
  411. if (m_LogLevel >= 2)
  412. m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Expiring Asset {0}", id);
  413. try
  414. {
  415. if (m_FileCacheEnabled)
  416. {
  417. string filename = GetFileName(id);
  418. if (File.Exists(filename))
  419. {
  420. File.Delete(filename);
  421. }
  422. }
  423. if (m_MemoryCacheEnabled)
  424. m_MemoryCache.Remove(id);
  425. }
  426. catch (Exception e)
  427. {
  428. m_log.WarnFormat(
  429. "[FLOTSAM ASSET CACHE]: Failed to expire cached file {0}. Exception {1} {2}",
  430. id, e.Message, e.StackTrace);
  431. }
  432. }
  433. public void Clear()
  434. {
  435. if (m_LogLevel >= 2)
  436. m_log.Debug("[FLOTSAM ASSET CACHE]: Clearing caches.");
  437. if (m_FileCacheEnabled)
  438. {
  439. foreach (string dir in Directory.GetDirectories(m_CacheDirectory))
  440. {
  441. Directory.Delete(dir);
  442. }
  443. }
  444. if (m_MemoryCacheEnabled)
  445. m_MemoryCache.Clear();
  446. }
  447. private void CleanupExpiredFiles(object source, ElapsedEventArgs e)
  448. {
  449. if (m_LogLevel >= 2)
  450. m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Checking for expired files older then {0}.", m_FileExpiration);
  451. // Purge all files last accessed prior to this point
  452. DateTime purgeLine = DateTime.Now - m_FileExpiration;
  453. // An asset cache may contain local non-temporary assets that are not in the asset service. Therefore,
  454. // before cleaning up expired files we must scan the objects in the scene to make sure that we retain
  455. // such local assets if they have not been recently accessed.
  456. TouchAllSceneAssets(false);
  457. foreach (string dir in Directory.GetDirectories(m_CacheDirectory))
  458. {
  459. CleanExpiredFiles(dir, purgeLine);
  460. }
  461. }
  462. /// <summary>
  463. /// Recurses through specified directory checking for asset files last
  464. /// accessed prior to the specified purge line and deletes them. Also
  465. /// removes empty tier directories.
  466. /// </summary>
  467. /// <param name="dir"></param>
  468. /// <param name="purgeLine"></param>
  469. private void CleanExpiredFiles(string dir, DateTime purgeLine)
  470. {
  471. try
  472. {
  473. foreach (string file in Directory.GetFiles(dir))
  474. {
  475. if (File.GetLastAccessTime(file) < purgeLine)
  476. {
  477. File.Delete(file);
  478. }
  479. }
  480. // Recurse into lower tiers
  481. foreach (string subdir in Directory.GetDirectories(dir))
  482. {
  483. CleanExpiredFiles(subdir, purgeLine);
  484. }
  485. // Check if a tier directory is empty, if so, delete it
  486. int dirSize = Directory.GetFiles(dir).Length + Directory.GetDirectories(dir).Length;
  487. if (dirSize == 0)
  488. {
  489. Directory.Delete(dir);
  490. }
  491. else if (dirSize >= m_CacheWarnAt)
  492. {
  493. m_log.WarnFormat(
  494. "[FLOTSAM ASSET CACHE]: Cache folder exceeded CacheWarnAt limit {0} {1}. Suggest increasing tiers, tier length, or reducing cache expiration",
  495. dir, dirSize);
  496. }
  497. }
  498. catch (Exception e)
  499. {
  500. m_log.Warn(
  501. string.Format("[FLOTSAM ASSET CACHE]: Could not complete clean of expired files in {0}, exception ", dir), e);
  502. }
  503. }
  504. /// <summary>
  505. /// Determines the filename for an AssetID stored in the file cache
  506. /// </summary>
  507. /// <param name="id"></param>
  508. /// <returns></returns>
  509. private string GetFileName(string id)
  510. {
  511. // Would it be faster to just hash the darn thing?
  512. foreach (char c in m_InvalidChars)
  513. {
  514. id = id.Replace(c, '_');
  515. }
  516. string path = m_CacheDirectory;
  517. for (int p = 1; p <= m_CacheDirectoryTiers; p++)
  518. {
  519. string pathPart = id.Substring((p - 1) * m_CacheDirectoryTierLen, m_CacheDirectoryTierLen);
  520. path = Path.Combine(path, pathPart);
  521. }
  522. return Path.Combine(path, id);
  523. }
  524. /// <summary>
  525. /// Writes a file to the file cache, creating any nessesary
  526. /// tier directories along the way
  527. /// </summary>
  528. /// <param name="filename"></param>
  529. /// <param name="asset"></param>
  530. private void WriteFileCache(string filename, AssetBase asset)
  531. {
  532. Stream stream = null;
  533. // Make sure the target cache directory exists
  534. string directory = Path.GetDirectoryName(filename);
  535. // Write file first to a temp name, so that it doesn't look
  536. // like it's already cached while it's still writing.
  537. string tempname = Path.Combine(directory, Path.GetRandomFileName());
  538. try
  539. {
  540. try
  541. {
  542. if (!Directory.Exists(directory))
  543. {
  544. Directory.CreateDirectory(directory);
  545. }
  546. stream = File.Open(tempname, FileMode.Create);
  547. BinaryFormatter bformatter = new BinaryFormatter();
  548. bformatter.Serialize(stream, asset);
  549. }
  550. catch (IOException e)
  551. {
  552. m_log.WarnFormat(
  553. "[FLOTSAM ASSET CACHE]: Failed to write asset {0} to temporary location {1} (final {2}) on cache in {3}. Exception {4} {5}.",
  554. asset.ID, tempname, filename, directory, e.Message, e.StackTrace);
  555. return;
  556. }
  557. finally
  558. {
  559. if (stream != null)
  560. stream.Close();
  561. }
  562. try
  563. {
  564. // Now that it's written, rename it so that it can be found.
  565. //
  566. // File.Copy(tempname, filename, true);
  567. // File.Delete(tempname);
  568. //
  569. // For a brief period, this was done as a separate copy and then temporary file delete operation to
  570. // avoid an IOException caused by move if some competing thread had already written the file.
  571. // However, this causes exceptions on Windows when other threads attempt to read a file
  572. // which is still being copied. So instead, go back to moving the file and swallow any IOException.
  573. //
  574. // This situation occurs fairly rarely anyway. We assume in this that moves are atomic on the
  575. // filesystem.
  576. File.Move(tempname, filename);
  577. if (m_LogLevel >= 2)
  578. m_log.DebugFormat("[FLOTSAM ASSET CACHE]: Cache Stored :: {0}", asset.ID);
  579. }
  580. catch (IOException)
  581. {
  582. // If we see an IOException here it's likely that some other competing thread has written the
  583. // cache file first, so ignore. Other IOException errors (e.g. filesystem full) should be
  584. // signally by the earlier temporary file writing code.
  585. }
  586. }
  587. finally
  588. {
  589. // Even if the write fails with an exception, we need to make sure
  590. // that we release the lock on that file, otherwise it'll never get
  591. // cached
  592. lock (m_CurrentlyWriting)
  593. {
  594. #if WAIT_ON_INPROGRESS_REQUESTS
  595. ManualResetEvent waitEvent;
  596. if (m_CurrentlyWriting.TryGetValue(filename, out waitEvent))
  597. {
  598. m_CurrentlyWriting.Remove(filename);
  599. waitEvent.Set();
  600. }
  601. #else
  602. m_CurrentlyWriting.Remove(filename);
  603. #endif
  604. }
  605. }
  606. }
  607. /// <summary>
  608. /// Scan through the file cache, and return number of assets currently cached.
  609. /// </summary>
  610. /// <param name="dir"></param>
  611. /// <returns></returns>
  612. private int GetFileCacheCount(string dir)
  613. {
  614. int count = Directory.GetFiles(dir).Length;
  615. foreach (string subdir in Directory.GetDirectories(dir))
  616. {
  617. count += GetFileCacheCount(subdir);
  618. }
  619. return count;
  620. }
  621. /// <summary>
  622. /// This notes the last time the Region had a deep asset scan performed on it.
  623. /// </summary>
  624. /// <param name="regionID"></param>
  625. private void StampRegionStatusFile(UUID regionID)
  626. {
  627. string RegionCacheStatusFile = Path.Combine(m_CacheDirectory, "RegionStatus_" + regionID.ToString() + ".fac");
  628. try
  629. {
  630. if (File.Exists(RegionCacheStatusFile))
  631. {
  632. File.SetLastWriteTime(RegionCacheStatusFile, DateTime.Now);
  633. }
  634. else
  635. {
  636. File.WriteAllText(
  637. RegionCacheStatusFile,
  638. "Please do not delete this file unless you are manually clearing your Flotsam Asset Cache.");
  639. }
  640. }
  641. catch (Exception e)
  642. {
  643. m_log.Warn(
  644. string.Format(
  645. "[FLOTSAM ASSET CACHE]: Could not stamp region status file for region {0}. Exception ",
  646. regionID),
  647. e);
  648. }
  649. }
  650. /// <summary>
  651. /// Iterates through all Scenes, doing a deep scan through assets
  652. /// to update the access time of all assets present in the scene or referenced by assets
  653. /// in the scene.
  654. /// </summary>
  655. /// <param name="storeUncached">
  656. /// If true, then assets scanned which are not found in cache are added to the cache.
  657. /// </param>
  658. /// <returns>Number of distinct asset references found in the scene.</returns>
  659. private int TouchAllSceneAssets(bool storeUncached)
  660. {
  661. UuidGatherer gatherer = new UuidGatherer(m_AssetService);
  662. HashSet<UUID> uniqueUuids = new HashSet<UUID>();
  663. Dictionary<UUID, sbyte> assets = new Dictionary<UUID, sbyte>();
  664. foreach (Scene s in m_Scenes)
  665. {
  666. StampRegionStatusFile(s.RegionInfo.RegionID);
  667. s.ForEachSOG(delegate(SceneObjectGroup e)
  668. {
  669. gatherer.GatherAssetUuids(e, assets);
  670. foreach (UUID assetID in assets.Keys)
  671. {
  672. uniqueUuids.Add(assetID);
  673. string filename = GetFileName(assetID.ToString());
  674. if (File.Exists(filename))
  675. {
  676. File.SetLastAccessTime(filename, DateTime.Now);
  677. }
  678. else if (storeUncached)
  679. {
  680. AssetBase cachedAsset = m_AssetService.Get(assetID.ToString());
  681. if (cachedAsset == null && assets[assetID] != (sbyte)AssetType.Unknown)
  682. m_log.DebugFormat(
  683. "[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",
  684. assetID, assets[assetID], e.Name, e.AbsolutePosition, s.Name);
  685. }
  686. }
  687. assets.Clear();
  688. });
  689. }
  690. return uniqueUuids.Count;
  691. }
  692. /// <summary>
  693. /// Deletes all cache contents
  694. /// </summary>
  695. private void ClearFileCache()
  696. {
  697. foreach (string dir in Directory.GetDirectories(m_CacheDirectory))
  698. {
  699. try
  700. {
  701. Directory.Delete(dir, true);
  702. }
  703. catch (Exception e)
  704. {
  705. m_log.WarnFormat(
  706. "[FLOTSAM ASSET CACHE]: Couldn't clear asset cache directory {0} from {1}. Exception {2} {3}",
  707. dir, m_CacheDirectory, e.Message, e.StackTrace);
  708. }
  709. }
  710. foreach (string file in Directory.GetFiles(m_CacheDirectory))
  711. {
  712. try
  713. {
  714. File.Delete(file);
  715. }
  716. catch (Exception e)
  717. {
  718. m_log.WarnFormat(
  719. "[FLOTSAM ASSET CACHE]: Couldn't clear asset cache file {0} from {1}. Exception {1} {2}",
  720. file, m_CacheDirectory, e.Message, e.StackTrace);
  721. }
  722. }
  723. }
  724. private List<string> GenerateCacheHitReport()
  725. {
  726. List<string> outputLines = new List<string>();
  727. double fileHitRate = (double)m_DiskHits / m_Requests * 100.0;
  728. outputLines.Add(
  729. string.Format("File Hit Rate: {0}% for {1} requests", fileHitRate.ToString("0.00"), m_Requests));
  730. if (m_MemoryCacheEnabled)
  731. {
  732. double memHitRate = (double)m_MemoryHits / m_Requests * 100.0;
  733. outputLines.Add(
  734. string.Format("Memory Hit Rate: {0}% for {1} requests", memHitRate.ToString("0.00"), m_Requests));
  735. }
  736. outputLines.Add(
  737. string.Format(
  738. "Unnecessary requests due to requests for assets that are currently downloading: {0}",
  739. m_RequestsForInprogress));
  740. return outputLines;
  741. }
  742. #region Console Commands
  743. private void HandleConsoleCommand(string module, string[] cmdparams)
  744. {
  745. ICommandConsole con = MainConsole.Instance;
  746. if (cmdparams.Length >= 2)
  747. {
  748. string cmd = cmdparams[1];
  749. switch (cmd)
  750. {
  751. case "status":
  752. if (m_MemoryCacheEnabled)
  753. con.OutputFormat("Memory Cache: {0} assets", m_MemoryCache.Count);
  754. else
  755. con.OutputFormat("Memory cache disabled");
  756. if (m_FileCacheEnabled)
  757. {
  758. int fileCount = GetFileCacheCount(m_CacheDirectory);
  759. con.OutputFormat("File Cache: {0} assets", fileCount);
  760. }
  761. else
  762. {
  763. con.Output("File cache disabled");
  764. }
  765. GenerateCacheHitReport().ForEach(l => con.Output(l));
  766. if (m_FileCacheEnabled)
  767. {
  768. con.Output("Deep scans have previously been performed on the following regions:");
  769. foreach (string s in Directory.GetFiles(m_CacheDirectory, "*.fac"))
  770. {
  771. string RegionID = s.Remove(0,s.IndexOf("_")).Replace(".fac","");
  772. DateTime RegionDeepScanTMStamp = File.GetLastWriteTime(s);
  773. con.OutputFormat("Region: {0}, {1}", RegionID, RegionDeepScanTMStamp.ToString("MM/dd/yyyy hh:mm:ss"));
  774. }
  775. }
  776. break;
  777. case "clear":
  778. if (cmdparams.Length < 2)
  779. {
  780. con.Output("Usage is fcache clear [file] [memory]");
  781. break;
  782. }
  783. bool clearMemory = false, clearFile = false;
  784. if (cmdparams.Length == 2)
  785. {
  786. clearMemory = true;
  787. clearFile = true;
  788. }
  789. foreach (string s in cmdparams)
  790. {
  791. if (s.ToLower() == "memory")
  792. clearMemory = true;
  793. else if (s.ToLower() == "file")
  794. clearFile = true;
  795. }
  796. if (clearMemory)
  797. {
  798. if (m_MemoryCacheEnabled)
  799. {
  800. m_MemoryCache.Clear();
  801. con.Output("Memory cache cleared.");
  802. }
  803. else
  804. {
  805. con.Output("Memory cache not enabled.");
  806. }
  807. }
  808. if (clearFile)
  809. {
  810. if (m_FileCacheEnabled)
  811. {
  812. ClearFileCache();
  813. con.Output("File cache cleared.");
  814. }
  815. else
  816. {
  817. con.Output("File cache not enabled.");
  818. }
  819. }
  820. break;
  821. case "assets":
  822. con.Output("Ensuring assets are cached for all scenes.");
  823. Util.RunThreadNoTimeout(delegate {
  824. int assetReferenceTotal = TouchAllSceneAssets(true);
  825. con.OutputFormat("Completed check with {0} assets.", assetReferenceTotal);
  826. }, "TouchAllSceneAssets", null);
  827. break;
  828. case "expire":
  829. if (cmdparams.Length < 3)
  830. {
  831. con.OutputFormat("Invalid parameters for Expire, please specify a valid date & time", cmd);
  832. break;
  833. }
  834. string s_expirationDate = "";
  835. DateTime expirationDate;
  836. if (cmdparams.Length > 3)
  837. {
  838. s_expirationDate = string.Join(" ", cmdparams, 2, cmdparams.Length - 2);
  839. }
  840. else
  841. {
  842. s_expirationDate = cmdparams[2];
  843. }
  844. if (!DateTime.TryParse(s_expirationDate, out expirationDate))
  845. {
  846. con.OutputFormat("{0} is not a valid date & time", cmd);
  847. break;
  848. }
  849. if (m_FileCacheEnabled)
  850. CleanExpiredFiles(m_CacheDirectory, expirationDate);
  851. else
  852. con.OutputFormat("File cache not active, not clearing.");
  853. break;
  854. default:
  855. con.OutputFormat("Unknown command {0}", cmd);
  856. break;
  857. }
  858. }
  859. else if (cmdparams.Length == 1)
  860. {
  861. con.Output("fcache assets - Attempt a deep cache of all assets in all scenes");
  862. con.Output("fcache expire <datetime> - Purge assets older then the specified date & time");
  863. con.Output("fcache clear [file] [memory] - Remove cached assets");
  864. con.Output("fcache status - Display cache status");
  865. }
  866. }
  867. #endregion
  868. #region IAssetService Members
  869. public AssetMetadata GetMetadata(string id)
  870. {
  871. AssetBase asset = Get(id);
  872. return asset.Metadata;
  873. }
  874. public byte[] GetData(string id)
  875. {
  876. AssetBase asset = Get(id);
  877. return asset.Data;
  878. }
  879. public bool Get(string id, object sender, AssetRetrieved handler)
  880. {
  881. AssetBase asset = Get(id);
  882. handler(id, sender, asset);
  883. return true;
  884. }
  885. public string Store(AssetBase asset)
  886. {
  887. if (asset.FullID == UUID.Zero)
  888. {
  889. asset.FullID = UUID.Random();
  890. }
  891. Cache(asset);
  892. return asset.ID;
  893. }
  894. public bool UpdateContent(string id, byte[] data)
  895. {
  896. AssetBase asset = Get(id);
  897. asset.Data = data;
  898. Cache(asset);
  899. return true;
  900. }
  901. public bool Delete(string id)
  902. {
  903. Expire(id);
  904. return true;
  905. }
  906. #endregion
  907. }
  908. }