AssetsRequest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Timers;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Serialization;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Region.CoreModules.World.Archiver
  38. {
  39. /// <summary>
  40. /// Encapsulate the asynchronous requests for the assets required for an archive operation
  41. /// </summary>
  42. class AssetsRequest
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. enum RequestState
  46. {
  47. Initial,
  48. Running,
  49. Completed,
  50. Aborted
  51. };
  52. /// <value>
  53. /// Timeout threshold if we still need assets or missing asset notifications but have stopped receiving them
  54. /// from the asset service
  55. /// </value>
  56. protected const int TIMEOUT = 60 * 1000;
  57. /// <value>
  58. /// If a timeout does occur, limit the amount of UUID information put to the console.
  59. /// </value>
  60. protected const int MAX_UUID_DISPLAY_ON_TIMEOUT = 3;
  61. protected System.Timers.Timer m_requestCallbackTimer;
  62. /// <value>
  63. /// State of this request
  64. /// </value>
  65. private RequestState m_requestState = RequestState.Initial;
  66. /// <value>
  67. /// uuids to request
  68. /// </value>
  69. protected IDictionary<UUID, AssetType> m_uuids;
  70. /// <value>
  71. /// Callback used when all the assets requested have been received.
  72. /// </value>
  73. protected AssetsRequestCallback m_assetsRequestCallback;
  74. /// <value>
  75. /// List of assets that were found. This will be passed back to the requester.
  76. /// </value>
  77. protected List<UUID> m_foundAssetUuids = new List<UUID>();
  78. /// <value>
  79. /// Maintain a list of assets that could not be found. This will be passed back to the requester.
  80. /// </value>
  81. protected List<UUID> m_notFoundAssetUuids = new List<UUID>();
  82. /// <value>
  83. /// Record the number of asset replies required so we know when we've finished
  84. /// </value>
  85. private int m_repliesRequired;
  86. /// <value>
  87. /// Asset service used to request the assets
  88. /// </value>
  89. protected IAssetService m_assetService;
  90. protected AssetsArchiver m_assetsArchiver;
  91. protected internal AssetsRequest(
  92. AssetsArchiver assetsArchiver, IDictionary<UUID, AssetType> uuids,
  93. IAssetService assetService, AssetsRequestCallback assetsRequestCallback)
  94. {
  95. m_assetsArchiver = assetsArchiver;
  96. m_uuids = uuids;
  97. m_assetsRequestCallback = assetsRequestCallback;
  98. m_assetService = assetService;
  99. m_repliesRequired = uuids.Count;
  100. m_requestCallbackTimer = new System.Timers.Timer(TIMEOUT);
  101. m_requestCallbackTimer.AutoReset = false;
  102. m_requestCallbackTimer.Elapsed += new ElapsedEventHandler(OnRequestCallbackTimeout);
  103. }
  104. protected internal void Execute()
  105. {
  106. m_requestState = RequestState.Running;
  107. m_log.DebugFormat("[ARCHIVER]: AssetsRequest executed looking for {0} assets", m_repliesRequired);
  108. // We can stop here if there are no assets to fetch
  109. if (m_repliesRequired == 0)
  110. {
  111. m_requestState = RequestState.Completed;
  112. PerformAssetsRequestCallback(null);
  113. return;
  114. }
  115. foreach (KeyValuePair<UUID, AssetType> kvp in m_uuids)
  116. {
  117. m_assetService.Get(kvp.Key.ToString(), kvp.Value, PreAssetRequestCallback);
  118. }
  119. m_requestCallbackTimer.Enabled = true;
  120. }
  121. protected void OnRequestCallbackTimeout(object source, ElapsedEventArgs args)
  122. {
  123. try
  124. {
  125. lock (this)
  126. {
  127. // Take care of the possibilty that this thread started but was paused just outside the lock before
  128. // the final request came in (assuming that such a thing is possible)
  129. if (m_requestState == RequestState.Completed)
  130. return;
  131. m_requestState = RequestState.Aborted;
  132. }
  133. // Calculate which uuids were not found. This is an expensive way of doing it, but this is a failure
  134. // case anyway.
  135. List<UUID> uuids = new List<UUID>();
  136. foreach (UUID uuid in m_uuids.Keys)
  137. {
  138. uuids.Add(uuid);
  139. }
  140. foreach (UUID uuid in m_foundAssetUuids)
  141. {
  142. uuids.Remove(uuid);
  143. }
  144. foreach (UUID uuid in m_notFoundAssetUuids)
  145. {
  146. uuids.Remove(uuid);
  147. }
  148. m_log.ErrorFormat(
  149. "[ARCHIVER]: Asset service failed to return information about {0} requested assets", uuids.Count);
  150. int i = 0;
  151. foreach (UUID uuid in uuids)
  152. {
  153. m_log.ErrorFormat("[ARCHIVER]: No information about asset {0} received", uuid);
  154. if (++i >= MAX_UUID_DISPLAY_ON_TIMEOUT)
  155. break;
  156. }
  157. if (uuids.Count > MAX_UUID_DISPLAY_ON_TIMEOUT)
  158. m_log.ErrorFormat(
  159. "[ARCHIVER]: (... {0} more not shown)", uuids.Count - MAX_UUID_DISPLAY_ON_TIMEOUT);
  160. m_log.Error("[ARCHIVER]: OAR save aborted.");
  161. }
  162. catch (Exception e)
  163. {
  164. m_log.ErrorFormat("[ARCHIVER]: Timeout handler exception {0}", e);
  165. }
  166. finally
  167. {
  168. m_assetsArchiver.ForceClose();
  169. }
  170. }
  171. protected void PreAssetRequestCallback(string fetchedAssetID, object assetType, AssetBase fetchedAsset)
  172. {
  173. // Check for broken asset types and fix them with the AssetType gleaned by UuidGatherer
  174. if (fetchedAsset != null && fetchedAsset.Type == (sbyte)AssetType.Unknown)
  175. {
  176. AssetType type = (AssetType)assetType;
  177. m_log.InfoFormat("[ARCHIVER]: Rewriting broken asset type for {0} to {1}", fetchedAsset.ID, type);
  178. fetchedAsset.Type = (sbyte)type;
  179. }
  180. AssetRequestCallback(fetchedAssetID, this, fetchedAsset);
  181. }
  182. /// <summary>
  183. /// Called back by the asset cache when it has the asset
  184. /// </summary>
  185. /// <param name="assetID"></param>
  186. /// <param name="asset"></param>
  187. public void AssetRequestCallback(string id, object sender, AssetBase asset)
  188. {
  189. try
  190. {
  191. lock (this)
  192. {
  193. //m_log.DebugFormat("[ARCHIVER]: Received callback for asset {0}", id);
  194. m_requestCallbackTimer.Stop();
  195. if (m_requestState == RequestState.Aborted)
  196. {
  197. m_log.WarnFormat(
  198. "[ARCHIVER]: Received information about asset {0} after archive save abortion. Ignoring.",
  199. id);
  200. return;
  201. }
  202. if (asset != null)
  203. {
  204. // m_log.DebugFormat("[ARCHIVER]: Writing asset {0}", id);
  205. m_foundAssetUuids.Add(asset.FullID);
  206. m_assetsArchiver.WriteAsset(asset);
  207. }
  208. else
  209. {
  210. // m_log.DebugFormat("[ARCHIVER]: Recording asset {0} as not found", id);
  211. m_notFoundAssetUuids.Add(new UUID(id));
  212. }
  213. if (m_foundAssetUuids.Count + m_notFoundAssetUuids.Count == m_repliesRequired)
  214. {
  215. m_requestState = RequestState.Completed;
  216. m_log.DebugFormat(
  217. "[ARCHIVER]: Successfully added {0} assets ({1} assets notified missing)",
  218. m_foundAssetUuids.Count, m_notFoundAssetUuids.Count);
  219. // We want to stop using the asset cache thread asap
  220. // as we now need to do the work of producing the rest of the archive
  221. Util.FireAndForget(PerformAssetsRequestCallback);
  222. }
  223. else
  224. {
  225. m_requestCallbackTimer.Start();
  226. }
  227. }
  228. }
  229. catch (Exception e)
  230. {
  231. m_log.ErrorFormat("[ARCHIVER]: AssetRequestCallback failed with {0}", e);
  232. }
  233. }
  234. /// <summary>
  235. /// Perform the callback on the original requester of the assets
  236. /// </summary>
  237. protected void PerformAssetsRequestCallback(object o)
  238. {
  239. try
  240. {
  241. m_assetsRequestCallback(m_foundAssetUuids, m_notFoundAssetUuids);
  242. }
  243. catch (Exception e)
  244. {
  245. m_log.ErrorFormat(
  246. "[ARCHIVER]: Terminating archive creation since asset requster callback failed with {0}", e);
  247. }
  248. }
  249. }
  250. }