diff options
Diffstat (limited to 'OpenSim/Services/Connectors/Asset')
-rw-r--r-- | OpenSim/Services/Connectors/Asset/AssetServiceConnector.cs | 248 | ||||
-rw-r--r-- | OpenSim/Services/Connectors/Asset/HGAssetServiceConnector.cs | 23 |
2 files changed, 227 insertions, 44 deletions
diff --git a/OpenSim/Services/Connectors/Asset/AssetServiceConnector.cs b/OpenSim/Services/Connectors/Asset/AssetServiceConnector.cs index 84fbcd3..c753c6a 100644 --- a/OpenSim/Services/Connectors/Asset/AssetServiceConnector.cs +++ b/OpenSim/Services/Connectors/Asset/AssetServiceConnector.cs | |||
@@ -30,11 +30,11 @@ using System; | |||
30 | using System.Collections.Generic; | 30 | using System.Collections.Generic; |
31 | using System.IO; | 31 | using System.IO; |
32 | using System.Reflection; | 32 | using System.Reflection; |
33 | using System.Timers; | ||
33 | using Nini.Config; | 34 | using Nini.Config; |
34 | using OpenSim.Framework; | 35 | using OpenSim.Framework; |
35 | using OpenSim.Framework.Console; | 36 | using OpenSim.Framework.Console; |
36 | using OpenSim.Framework.Communications; | 37 | using OpenSim.Framework.Communications; |
37 | using OpenSim.Framework.Servers.HttpServer; | ||
38 | using OpenSim.Services.Interfaces; | 38 | using OpenSim.Services.Interfaces; |
39 | using OpenMetaverse; | 39 | using OpenMetaverse; |
40 | 40 | ||
@@ -48,6 +48,15 @@ namespace OpenSim.Services.Connectors | |||
48 | 48 | ||
49 | private string m_ServerURI = String.Empty; | 49 | private string m_ServerURI = String.Empty; |
50 | private IImprovedAssetCache m_Cache = null; | 50 | private IImprovedAssetCache m_Cache = null; |
51 | private int m_retryCounter; | ||
52 | private Dictionary<int, List<AssetBase>> m_retryQueue = new Dictionary<int, List<AssetBase>>(); | ||
53 | private Timer m_retryTimer; | ||
54 | private delegate void AssetRetrievedEx(AssetBase asset); | ||
55 | |||
56 | // Keeps track of concurrent requests for the same asset, so that it's only loaded once. | ||
57 | // Maps: Asset ID -> Handlers which will be called when the asset has been loaded | ||
58 | private Dictionary<string, AssetRetrievedEx> m_AssetHandlers = new Dictionary<string, AssetRetrievedEx>(); | ||
59 | private Dictionary<string, string> m_UriMap = new Dictionary<string, string>(); | ||
51 | 60 | ||
52 | public AssetServicesConnector() | 61 | public AssetServicesConnector() |
53 | { | 62 | { |
@@ -68,7 +77,7 @@ namespace OpenSim.Services.Connectors | |||
68 | IConfig assetConfig = source.Configs["AssetService"]; | 77 | IConfig assetConfig = source.Configs["AssetService"]; |
69 | if (assetConfig == null) | 78 | if (assetConfig == null) |
70 | { | 79 | { |
71 | m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpanSim.ini"); | 80 | m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpenSim.ini"); |
72 | throw new Exception("Asset connector init error"); | 81 | throw new Exception("Asset connector init error"); |
73 | } | 82 | } |
74 | 83 | ||
@@ -85,6 +94,83 @@ namespace OpenSim.Services.Connectors | |||
85 | MainConsole.Instance.Commands.AddCommand("asset", false, "dump asset", | 94 | MainConsole.Instance.Commands.AddCommand("asset", false, "dump asset", |
86 | "dump asset <id> <file>", | 95 | "dump asset <id> <file>", |
87 | "dump one cached asset", HandleDumpAsset); | 96 | "dump one cached asset", HandleDumpAsset); |
97 | |||
98 | m_retryTimer = new Timer(); | ||
99 | m_retryTimer.Elapsed += new ElapsedEventHandler(retryCheck); | ||
100 | m_retryTimer.Interval = 60000; | ||
101 | |||
102 | Uri serverUri = new Uri(m_ServerURI); | ||
103 | |||
104 | string groupHost = serverUri.Host; | ||
105 | |||
106 | for (int i = 0 ; i < 256 ; i++) | ||
107 | { | ||
108 | string prefix = i.ToString("x2"); | ||
109 | groupHost = assetConfig.GetString("AssetServerHost_"+prefix, groupHost); | ||
110 | |||
111 | m_UriMap[prefix] = groupHost; | ||
112 | //m_log.DebugFormat("[ASSET]: Using {0} for prefix {1}", groupHost, prefix); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | private string MapServer(string id) | ||
117 | { | ||
118 | UriBuilder serverUri = new UriBuilder(m_ServerURI); | ||
119 | |||
120 | string prefix = id.Substring(0, 2).ToLower(); | ||
121 | |||
122 | string host = m_UriMap[prefix]; | ||
123 | |||
124 | serverUri.Host = host; | ||
125 | |||
126 | // m_log.DebugFormat("[ASSET]: Using {0} for host name for prefix {1}", host, prefix); | ||
127 | |||
128 | return serverUri.Uri.AbsoluteUri; | ||
129 | } | ||
130 | |||
131 | protected void retryCheck(object source, ElapsedEventArgs e) | ||
132 | { | ||
133 | m_retryCounter++; | ||
134 | if (m_retryCounter > 60) m_retryCounter -= 60; | ||
135 | List<int> keys = new List<int>(); | ||
136 | foreach (int a in m_retryQueue.Keys) | ||
137 | { | ||
138 | keys.Add(a); | ||
139 | } | ||
140 | foreach (int a in keys) | ||
141 | { | ||
142 | //We exponentially fall back on frequency until we reach one attempt per hour | ||
143 | //The net result is that we end up in the queue for roughly 24 hours.. | ||
144 | //24 hours worth of assets could be a lot, so the hope is that the region admin | ||
145 | //will have gotten the asset connector back online quickly! | ||
146 | |||
147 | int timefactor = a ^ 2; | ||
148 | if (timefactor > 60) | ||
149 | { | ||
150 | timefactor = 60; | ||
151 | } | ||
152 | |||
153 | //First, find out if we care about this timefactor | ||
154 | if (timefactor % a == 0) | ||
155 | { | ||
156 | //Yes, we do! | ||
157 | List<AssetBase> retrylist = m_retryQueue[a]; | ||
158 | m_retryQueue.Remove(a); | ||
159 | |||
160 | foreach(AssetBase ass in retrylist) | ||
161 | { | ||
162 | Store(ass); //Store my ass. This function will put it back in the dictionary if it fails | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | |||
167 | if (m_retryQueue.Count == 0) | ||
168 | { | ||
169 | //It might only be one tick per minute, but I have | ||
170 | //repented and abandoned my wasteful ways | ||
171 | m_retryCounter = 0; | ||
172 | m_retryTimer.Stop(); | ||
173 | } | ||
88 | } | 174 | } |
89 | 175 | ||
90 | protected void SetCache(IImprovedAssetCache cache) | 176 | protected void SetCache(IImprovedAssetCache cache) |
@@ -94,13 +180,13 @@ namespace OpenSim.Services.Connectors | |||
94 | 180 | ||
95 | public AssetBase Get(string id) | 181 | public AssetBase Get(string id) |
96 | { | 182 | { |
97 | string uri = m_ServerURI + "/assets/" + id; | 183 | string uri = MapServer(id) + "/assets/" + id; |
98 | 184 | ||
99 | AssetBase asset = null; | 185 | AssetBase asset = null; |
100 | if (m_Cache != null) | 186 | if (m_Cache != null) |
101 | asset = m_Cache.Get(id); | 187 | asset = m_Cache.Get(id); |
102 | 188 | ||
103 | if (asset == null) | 189 | if (asset == null || asset.Data == null || asset.Data.Length == 0) |
104 | { | 190 | { |
105 | asset = SynchronousRestObjectRequester. | 191 | asset = SynchronousRestObjectRequester. |
106 | MakeRequest<int, AssetBase>("GET", uri, 0); | 192 | MakeRequest<int, AssetBase>("GET", uri, 0); |
@@ -129,7 +215,7 @@ namespace OpenSim.Services.Connectors | |||
129 | return fullAsset.Metadata; | 215 | return fullAsset.Metadata; |
130 | } | 216 | } |
131 | 217 | ||
132 | string uri = m_ServerURI + "/assets/" + id + "/metadata"; | 218 | string uri = MapServer(id) + "/assets/" + id + "/metadata"; |
133 | 219 | ||
134 | AssetMetadata asset = SynchronousRestObjectRequester. | 220 | AssetMetadata asset = SynchronousRestObjectRequester. |
135 | MakeRequest<int, AssetMetadata>("GET", uri, 0); | 221 | MakeRequest<int, AssetMetadata>("GET", uri, 0); |
@@ -146,7 +232,7 @@ namespace OpenSim.Services.Connectors | |||
146 | return fullAsset.Data; | 232 | return fullAsset.Data; |
147 | } | 233 | } |
148 | 234 | ||
149 | RestClient rc = new RestClient(m_ServerURI); | 235 | RestClient rc = new RestClient(MapServer(id)); |
150 | rc.AddResourcePath("assets"); | 236 | rc.AddResourcePath("assets"); |
151 | rc.AddResourcePath(id); | 237 | rc.AddResourcePath(id); |
152 | rc.AddResourcePath("data"); | 238 | rc.AddResourcePath("data"); |
@@ -171,31 +257,64 @@ namespace OpenSim.Services.Connectors | |||
171 | 257 | ||
172 | public bool Get(string id, Object sender, AssetRetrieved handler) | 258 | public bool Get(string id, Object sender, AssetRetrieved handler) |
173 | { | 259 | { |
174 | string uri = m_ServerURI + "/assets/" + id; | 260 | string uri = MapServer(id) + "/assets/" + id; |
175 | 261 | ||
176 | AssetBase asset = null; | 262 | AssetBase asset = null; |
177 | if (m_Cache != null) | 263 | if (m_Cache != null) |
178 | asset = m_Cache.Get(id); | 264 | asset = m_Cache.Get(id); |
179 | 265 | ||
180 | if (asset == null) | 266 | if (asset == null || asset.Data == null || asset.Data.Length == 0) |
181 | { | 267 | { |
182 | bool result = false; | 268 | lock (m_AssetHandlers) |
183 | 269 | { | |
184 | AsynchronousRestObjectRequester. | 270 | AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate(AssetBase _asset) { handler(id, sender, _asset); }); |
185 | MakeRequest<int, AssetBase>("GET", uri, 0, | 271 | |
272 | AssetRetrievedEx handlers; | ||
273 | if (m_AssetHandlers.TryGetValue(id, out handlers)) | ||
274 | { | ||
275 | // Someone else is already loading this asset. It will notify our handler when done. | ||
276 | handlers += handlerEx; | ||
277 | return true; | ||
278 | } | ||
279 | |||
280 | // Load the asset ourselves | ||
281 | handlers += handlerEx; | ||
282 | m_AssetHandlers.Add(id, handlers); | ||
283 | } | ||
284 | |||
285 | bool success = false; | ||
286 | try | ||
287 | { | ||
288 | AsynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0, | ||
186 | delegate(AssetBase a) | 289 | delegate(AssetBase a) |
187 | { | 290 | { |
188 | if (m_Cache != null) | 291 | if (m_Cache != null) |
189 | m_Cache.Cache(a); | 292 | m_Cache.Cache(a); |
190 | handler(id, sender, a); | ||
191 | result = true; | ||
192 | }); | ||
193 | 293 | ||
194 | return result; | 294 | AssetRetrievedEx handlers; |
295 | lock (m_AssetHandlers) | ||
296 | { | ||
297 | handlers = m_AssetHandlers[id]; | ||
298 | m_AssetHandlers.Remove(id); | ||
299 | } | ||
300 | handlers.Invoke(a); | ||
301 | }); | ||
302 | |||
303 | success = true; | ||
304 | } | ||
305 | finally | ||
306 | { | ||
307 | if (!success) | ||
308 | { | ||
309 | lock (m_AssetHandlers) | ||
310 | { | ||
311 | m_AssetHandlers.Remove(id); | ||
312 | } | ||
313 | } | ||
314 | } | ||
195 | } | 315 | } |
196 | else | 316 | else |
197 | { | 317 | { |
198 | //Util.FireAndForget(delegate { handler(id, sender, asset); }); | ||
199 | handler(id, sender, asset); | 318 | handler(id, sender, asset); |
200 | } | 319 | } |
201 | 320 | ||
@@ -204,38 +323,95 @@ namespace OpenSim.Services.Connectors | |||
204 | 323 | ||
205 | public string Store(AssetBase asset) | 324 | public string Store(AssetBase asset) |
206 | { | 325 | { |
207 | if (asset.Temporary || asset.Local) | 326 | // Have to assign the asset ID here. This isn't likely to |
327 | // trigger since current callers don't pass emtpy IDs | ||
328 | // We need the asset ID to route the request to the proper | ||
329 | // cluster member, so we can't have the server assign one. | ||
330 | if (asset.ID == string.Empty) | ||
208 | { | 331 | { |
209 | if (m_Cache != null) | 332 | if (asset.FullID == UUID.Zero) |
210 | m_Cache.Cache(asset); | 333 | { |
334 | asset.FullID = UUID.Random(); | ||
335 | } | ||
336 | asset.ID = asset.FullID.ToString(); | ||
337 | } | ||
338 | else if (asset.FullID == UUID.Zero) | ||
339 | { | ||
340 | UUID uuid = UUID.Zero; | ||
341 | if (UUID.TryParse(asset.ID, out uuid)) | ||
342 | { | ||
343 | asset.FullID = uuid; | ||
344 | } | ||
345 | else | ||
346 | { | ||
347 | asset.FullID = UUID.Random(); | ||
348 | } | ||
349 | } | ||
211 | 350 | ||
351 | if (m_Cache != null) | ||
352 | m_Cache.Cache(asset); | ||
353 | if (asset.Temporary || asset.Local) | ||
354 | { | ||
212 | return asset.ID; | 355 | return asset.ID; |
213 | } | 356 | } |
214 | 357 | ||
215 | string uri = m_ServerURI + "/assets/"; | 358 | string uri = MapServer(asset.FullID.ToString()) + "/assets/"; |
216 | 359 | ||
217 | string newID = string.Empty; | 360 | string newID = string.Empty; |
218 | try | 361 | try |
219 | { | 362 | { |
220 | newID = SynchronousRestObjectRequester. | 363 | newID = SynchronousRestObjectRequester. |
221 | MakeRequest<AssetBase, string>("POST", uri, asset); | 364 | MakeRequest<AssetBase, string>("POST", uri, asset, 25); |
365 | if (newID == null || newID == "") | ||
366 | { | ||
367 | newID = UUID.Zero.ToString(); | ||
368 | } | ||
222 | } | 369 | } |
223 | catch (Exception e) | 370 | catch (Exception e) |
224 | { | 371 | { |
225 | m_log.WarnFormat("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1}", asset.ID, e.Message); | 372 | newID = UUID.Zero.ToString(); |
226 | } | 373 | } |
227 | 374 | ||
228 | if (newID != String.Empty) | 375 | if (newID == UUID.Zero.ToString()) |
229 | { | 376 | { |
230 | // Placing this here, so that this work with old asset servers that don't send any reply back | 377 | //The asset upload failed, put it in a queue for later |
231 | // SynchronousRestObjectRequester returns somethins that is not an empty string | 378 | asset.UploadAttempts++; |
232 | if (newID != null) | 379 | if (asset.UploadAttempts > 30) |
233 | asset.ID = newID; | 380 | { |
234 | 381 | //By this stage we've been in the queue for a good few hours; | |
235 | if (m_Cache != null) | 382 | //We're going to drop the asset. |
236 | m_Cache.Cache(asset); | 383 | m_log.ErrorFormat("[Assets] Dropping asset {0} - Upload has been in the queue for too long.", asset.ID.ToString()); |
384 | } | ||
385 | else | ||
386 | { | ||
387 | if (!m_retryQueue.ContainsKey(asset.UploadAttempts)) | ||
388 | { | ||
389 | m_retryQueue.Add(asset.UploadAttempts, new List<AssetBase>()); | ||
390 | } | ||
391 | List<AssetBase> m_queue = m_retryQueue[asset.UploadAttempts]; | ||
392 | m_queue.Add(asset); | ||
393 | m_log.WarnFormat("[Assets] Upload failed: {0} - Requeuing asset for another run.", asset.ID.ToString()); | ||
394 | m_retryTimer.Start(); | ||
395 | } | ||
396 | } | ||
397 | else | ||
398 | { | ||
399 | if (asset.UploadAttempts > 0) | ||
400 | { | ||
401 | m_log.InfoFormat("[Assets] Upload of {0} succeeded after {1} failed attempts", asset.ID.ToString(), asset.UploadAttempts.ToString()); | ||
402 | } | ||
403 | if (newID != String.Empty) | ||
404 | { | ||
405 | // Placing this here, so that this work with old asset servers that don't send any reply back | ||
406 | // SynchronousRestObjectRequester returns somethins that is not an empty string | ||
407 | if (newID != null) | ||
408 | asset.ID = newID; | ||
409 | |||
410 | if (m_Cache != null) | ||
411 | m_Cache.Cache(asset); | ||
412 | } | ||
237 | } | 413 | } |
238 | return newID; | 414 | return asset.ID; |
239 | } | 415 | } |
240 | 416 | ||
241 | public bool UpdateContent(string id, byte[] data) | 417 | public bool UpdateContent(string id, byte[] data) |
@@ -251,12 +427,12 @@ namespace OpenSim.Services.Connectors | |||
251 | if (metadata == null) | 427 | if (metadata == null) |
252 | return false; | 428 | return false; |
253 | 429 | ||
254 | asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type); | 430 | asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString()); |
255 | asset.Metadata = metadata; | 431 | asset.Metadata = metadata; |
256 | } | 432 | } |
257 | asset.Data = data; | 433 | asset.Data = data; |
258 | 434 | ||
259 | string uri = m_ServerURI + "/assets/" + id; | 435 | string uri = MapServer(id) + "/assets/" + id; |
260 | 436 | ||
261 | if (SynchronousRestObjectRequester. | 437 | if (SynchronousRestObjectRequester. |
262 | MakeRequest<AssetBase, bool>("POST", uri, asset)) | 438 | MakeRequest<AssetBase, bool>("POST", uri, asset)) |
@@ -271,7 +447,7 @@ namespace OpenSim.Services.Connectors | |||
271 | 447 | ||
272 | public bool Delete(string id) | 448 | public bool Delete(string id) |
273 | { | 449 | { |
274 | string uri = m_ServerURI + "/assets/" + id; | 450 | string uri = MapServer(id) + "/assets/" + id; |
275 | 451 | ||
276 | if (SynchronousRestObjectRequester. | 452 | if (SynchronousRestObjectRequester. |
277 | MakeRequest<int, bool>("DELETE", uri, 0)) | 453 | MakeRequest<int, bool>("DELETE", uri, 0)) |
diff --git a/OpenSim/Services/Connectors/Asset/HGAssetServiceConnector.cs b/OpenSim/Services/Connectors/Asset/HGAssetServiceConnector.cs index 34df54a..5c31639 100644 --- a/OpenSim/Services/Connectors/Asset/HGAssetServiceConnector.cs +++ b/OpenSim/Services/Connectors/Asset/HGAssetServiceConnector.cs | |||
@@ -32,6 +32,8 @@ using System.Collections.Generic; | |||
32 | using System.Reflection; | 32 | using System.Reflection; |
33 | using OpenSim.Framework; | 33 | using OpenSim.Framework; |
34 | using OpenSim.Services.Interfaces; | 34 | using OpenSim.Services.Interfaces; |
35 | using OpenSim.Services.Connectors.Hypergrid; | ||
36 | using OpenSim.Services.Connectors.SimianGrid; | ||
35 | 37 | ||
36 | namespace OpenSim.Services.Connectors | 38 | namespace OpenSim.Services.Connectors |
37 | { | 39 | { |
@@ -41,7 +43,7 @@ namespace OpenSim.Services.Connectors | |||
41 | LogManager.GetLogger( | 43 | LogManager.GetLogger( |
42 | MethodBase.GetCurrentMethod().DeclaringType); | 44 | MethodBase.GetCurrentMethod().DeclaringType); |
43 | 45 | ||
44 | private Dictionary<string, AssetServicesConnector> m_connectors = new Dictionary<string, AssetServicesConnector>(); | 46 | private Dictionary<string, IAssetService> m_connectors = new Dictionary<string, IAssetService>(); |
45 | 47 | ||
46 | public HGAssetServiceConnector(IConfigSource source) | 48 | public HGAssetServiceConnector(IConfigSource source) |
47 | { | 49 | { |
@@ -81,7 +83,7 @@ namespace OpenSim.Services.Connectors | |||
81 | 83 | ||
82 | private IAssetService GetConnector(string url) | 84 | private IAssetService GetConnector(string url) |
83 | { | 85 | { |
84 | AssetServicesConnector connector = null; | 86 | IAssetService connector = null; |
85 | lock (m_connectors) | 87 | lock (m_connectors) |
86 | { | 88 | { |
87 | if (m_connectors.ContainsKey(url)) | 89 | if (m_connectors.ContainsKey(url)) |
@@ -90,12 +92,17 @@ namespace OpenSim.Services.Connectors | |||
90 | } | 92 | } |
91 | else | 93 | else |
92 | { | 94 | { |
93 | // We're instantiating this class explicitly, but this won't | 95 | // Still not as flexible as I would like this to be, |
94 | // work in general, because the remote grid may be running | 96 | // but good enough for now |
95 | // an asset server that has a different protocol. | 97 | string connectorType = new HeloServicesConnector(url).Helo(); |
96 | // Eventually we will want a piece of protocol asking | 98 | m_log.DebugFormat("[HG ASSET SERVICE]: HELO returned {0}", connectorType); |
97 | // the remote server about its kind. Definitely cool thing to do! | 99 | if (connectorType == "opensim-simian") |
98 | connector = new AssetServicesConnector(url); | 100 | { |
101 | connector = new SimianAssetServiceConnector(url); | ||
102 | } | ||
103 | else | ||
104 | connector = new AssetServicesConnector(url); | ||
105 | |||
99 | m_connectors.Add(url, connector); | 106 | m_connectors.Add(url, connector); |
100 | } | 107 | } |
101 | } | 108 | } |