AssetsRequest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 ICollection<UUID> 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, ICollection<UUID> 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();
  113. return;
  114. }
  115. foreach (UUID uuid in m_uuids)
  116. {
  117. m_assetService.Get(uuid.ToString(), this, AssetRequestCallback);
  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)
  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. /// <summary>
  172. /// Called back by the asset cache when it has the asset
  173. /// </summary>
  174. /// <param name="assetID"></param>
  175. /// <param name="asset"></param>
  176. public void AssetRequestCallback(string id, object sender, AssetBase asset)
  177. {
  178. try
  179. {
  180. lock (this)
  181. {
  182. //m_log.DebugFormat("[ARCHIVER]: Received callback for asset {0}", id);
  183. m_requestCallbackTimer.Stop();
  184. if (m_requestState == RequestState.Aborted)
  185. {
  186. m_log.WarnFormat(
  187. "[ARCHIVER]: Received information about asset {0} after archive save abortion. Ignoring.",
  188. id);
  189. return;
  190. }
  191. if (asset != null)
  192. {
  193. // m_log.DebugFormat("[ARCHIVER]: Recording asset {0} as found", id);
  194. m_foundAssetUuids.Add(asset.FullID);
  195. m_assetsArchiver.WriteAsset(asset);
  196. }
  197. else
  198. {
  199. // m_log.DebugFormat("[ARCHIVER]: Recording asset {0} as not found", id);
  200. m_notFoundAssetUuids.Add(new UUID(id));
  201. }
  202. if (m_foundAssetUuids.Count + m_notFoundAssetUuids.Count == m_repliesRequired)
  203. {
  204. m_requestState = RequestState.Completed;
  205. m_log.DebugFormat(
  206. "[ARCHIVER]: Successfully added {0} assets ({1} assets notified missing)",
  207. m_foundAssetUuids.Count, m_notFoundAssetUuids.Count);
  208. // We want to stop using the asset cache thread asap
  209. // as we now need to do the work of producing the rest of the archive
  210. Thread newThread = new Thread(PerformAssetsRequestCallback);
  211. newThread.Name = "OpenSimulator archiving thread post assets receipt";
  212. newThread.Start();
  213. }
  214. else
  215. {
  216. m_requestCallbackTimer.Start();
  217. }
  218. }
  219. }
  220. catch (Exception e)
  221. {
  222. m_log.ErrorFormat("[ARCHIVER]: AssetRequestCallback failed with {0}", e);
  223. }
  224. }
  225. /// <summary>
  226. /// Perform the callback on the original requester of the assets
  227. /// </summary>
  228. protected void PerformAssetsRequestCallback()
  229. {
  230. try
  231. {
  232. m_assetsRequestCallback(m_foundAssetUuids, m_notFoundAssetUuids);
  233. }
  234. catch (Exception e)
  235. {
  236. m_log.ErrorFormat(
  237. "[ARCHIVER]: Terminating archive creation since asset requster callback failed with {0}", e);
  238. }
  239. }
  240. }
  241. }