From c74cef0f4261191962959e42c7e349adafd42a04 Mon Sep 17 00:00:00 2001
From: Diva Canto
Date: Thu, 7 May 2015 19:24:08 -0700
Subject: Major change in the way inventory is downloaded: added a method
throughout IIventoryService that fetches sets of folders at once. Also added
folder id in the InventoryCollection data structure, so that we don't need to
go to inventory server again just for that. This reduces the chatter between
sims and inventory server by... a lot. On my tests, this reduces initial
inventory download down to 30% of what it currently is.
---
.../FetchInvDescHandler.cs | 449 +++++++++++++++++----
1 file changed, 365 insertions(+), 84 deletions(-)
(limited to 'OpenSim/Capabilities')
diff --git a/OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs b/OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs
index 451575f..a2f6740 100644
--- a/OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs
+++ b/OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs
@@ -57,104 +57,113 @@ namespace OpenSim.Capabilities.Handlers
m_LibraryService = libService;
}
+
public string FetchInventoryDescendentsRequest(string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
{
-// lock (m_fetchLock)
-// {
-// m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Received request {0}", request);
- // nasty temporary hack here, the linden client falsely
- // identifies the uuid 00000000-0000-0000-0000-000000000000
- // as a string which breaks us
- //
- // correctly mark it as a uuid
- //
- request = request.Replace("00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000000");
+ // nasty temporary hack here, the linden client falsely
+ // identifies the uuid 00000000-0000-0000-0000-000000000000
+ // as a string which breaks us
+ //
+ // correctly mark it as a uuid
+ //
+ request = request.Replace("00000000-0000-0000-0000-000000000000", "00000000-0000-0000-0000-000000000000");
- // another hack 1 results in a
- // System.ArgumentException: Object type System.Int32 cannot
- // be converted to target type: System.Boolean
- //
- request = request.Replace("fetch_folders0", "fetch_folders0");
- request = request.Replace("fetch_folders1", "fetch_folders1");
+ // another hack 1 results in a
+ // System.ArgumentException: Object type System.Int32 cannot
+ // be converted to target type: System.Boolean
+ //
+ request = request.Replace("fetch_folders0", "fetch_folders0");
+ request = request.Replace("fetch_folders1", "fetch_folders1");
- Hashtable hash = new Hashtable();
+ Hashtable hash = new Hashtable();
+ try
+ {
+ hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
+ }
+ catch (LLSD.LLSDParseException e)
+ {
+ m_log.ErrorFormat("[WEB FETCH INV DESC HANDLER]: Fetch error: {0}{1}" + e.Message, e.StackTrace);
+ m_log.Error("Request: " + request);
+ }
+
+ ArrayList foldersrequested = (ArrayList)hash["folders"];
+
+ string response = "";
+ string bad_folders_response = "";
+
+ List folders = new List();
+ for (int i = 0; i < foldersrequested.Count; i++)
+ {
+ Hashtable inventoryhash = (Hashtable)foldersrequested[i];
+
+ LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
+
try
{
- hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
+ LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
}
- catch (LLSD.LLSDParseException e)
+ catch (Exception e)
{
- m_log.ErrorFormat("[WEB FETCH INV DESC HANDLER]: Fetch error: {0}{1}" + e.Message, e.StackTrace);
- m_log.Error("Request: " + request);
+ m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e);
+ continue;
}
-
- ArrayList foldersrequested = (ArrayList)hash["folders"];
-
- string response = "";
- string bad_folders_response = "";
- for (int i = 0; i < foldersrequested.Count; i++)
- {
- string inventoryitemstr = "";
- Hashtable inventoryhash = (Hashtable)foldersrequested[i];
+ folders.Add(llsdRequest);
+ }
- LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
+ if (folders.Count > 0)
+ {
+ List invcollSet = Fetch(folders);
+ //m_log.DebugFormat("[XXX]: Got {0} folders from a request of {1}", invcollSet.Count, folders.Count);
+ if (invcollSet == null)
+ {
+ m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Multiple folder fetch failed. Trying old protocol.");
+ return FetchInventoryDescendentsRequest(foldersrequested, httpRequest, httpResponse);
+ }
- try
- {
- LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
- }
- catch (Exception e)
- {
- m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e);
- }
- LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
+ string inventoryitemstr = string.Empty;
+ foreach (InventoryCollectionWithDescendents icoll in invcollSet)
+ {
+ LLSDInventoryDescendents reply = ToLLSD(icoll.Collection, icoll.Descendents);
- if (null == reply)
- {
- bad_folders_response += "" + llsdRequest.folder_id.ToString() + "";
- }
- else
- {
- inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
- inventoryitemstr = inventoryitemstr.Replace("", "");
- }
+ inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
+ inventoryitemstr = inventoryitemstr.Replace("", "");
response += inventoryitemstr;
}
+ }
- if (response.Length == 0)
+ if (response.Length == 0)
+ {
+ /* Viewers expect a bad_folders array when not available */
+ if (bad_folders_response.Length != 0)
{
- /* Viewers expect a bad_folders array when not available */
- if (bad_folders_response.Length != 0)
- {
- response = "";
- }
- else
- {
- response = "";
- }
+ response = "";
}
else
{
- if (bad_folders_response.Length != 0)
- {
- response = "";
- }
- else
- {
- response = "";
- }
+ response = "";
}
+ }
+ else
+ {
+ if (bad_folders_response.Length != 0)
+ {
+ response = "";
+ }
+ else
+ {
+ response = "";
+ }
+ }
-// m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Replying to CAPS fetch inventory request");
- //m_log.Debug("[WEB FETCH INV DESC HANDLER] "+response);
+// m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Replying to CAPS fetch inventory request");
+// m_log.Debug("[WEB FETCH INV DESC HANDLER] "+response);
- return response;
+ return response;
-// }
}
///
@@ -203,18 +212,130 @@ namespace OpenSim.Capabilities.Handlers
contents.descendents = descendents;
contents.version = version;
-// m_log.DebugFormat(
-// "[WEB FETCH INV DESC HANDLER]: Replying to request for folder {0} (fetch items {1}, fetch folders {2}) with {3} items and {4} folders for agent {5}",
-// invFetch.folder_id,
-// invFetch.fetch_items,
-// invFetch.fetch_folders,
-// contents.items.Array.Count,
-// contents.categories.Array.Count,
-// invFetch.owner_id);
+ //m_log.DebugFormat(
+ // "[WEB FETCH INV DESC HANDLER]: Replying to request for folder {0} (fetch items {1}, fetch folders {2}) with {3} items and {4} folders for agent {5}",
+ // invFetch.folder_id,
+ // invFetch.fetch_items,
+ // invFetch.fetch_folders,
+ // contents.items.Array.Count,
+ // contents.categories.Array.Count,
+ // invFetch.owner_id);
return reply;
}
+ private LLSDInventoryDescendents ToLLSD(InventoryCollection inv, int descendents)
+ {
+ LLSDInventoryDescendents reply = new LLSDInventoryDescendents();
+ LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
+ contents.agent_id = inv.OwnerID;
+ contents.owner_id = inv.OwnerID;
+ contents.folder_id = inv.FolderID;
+
+ reply.folders.Array.Add(contents);
+
+ if (inv.Folders != null)
+ {
+ foreach (InventoryFolderBase invFolder in inv.Folders)
+ {
+ contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
+ }
+
+ descendents += inv.Folders.Count;
+ }
+
+ if (inv.Items != null)
+ {
+ foreach (InventoryItemBase invItem in inv.Items)
+ {
+ contents.items.Array.Add(ConvertInventoryItem(invItem));
+ }
+ }
+
+ contents.descendents = descendents;
+ contents.version = inv.Version;
+
+ return reply;
+ }
+ ///
+ /// Old style. Soon to be deprecated.
+ ///
+ ///
+ ///
+ ///
+ ///
+ [Obsolete]
+ private string FetchInventoryDescendentsRequest(ArrayList foldersrequested, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
+ {
+ //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Received request for {0} folders", foldersrequested.Count);
+
+ string response = "";
+ string bad_folders_response = "";
+
+ for (int i = 0; i < foldersrequested.Count; i++)
+ {
+ string inventoryitemstr = "";
+ Hashtable inventoryhash = (Hashtable)foldersrequested[i];
+
+ LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
+
+ try
+ {
+ LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
+ }
+ catch (Exception e)
+ {
+ m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e);
+ }
+
+ LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
+
+ if (null == reply)
+ {
+ bad_folders_response += "" + llsdRequest.folder_id.ToString() + "";
+ }
+ else
+ {
+ inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
+ inventoryitemstr = inventoryitemstr.Replace("", "");
+ }
+
+ response += inventoryitemstr;
+ }
+
+ if (response.Length == 0)
+ {
+ /* Viewers expect a bad_folders array when not available */
+ if (bad_folders_response.Length != 0)
+ {
+ response = "";
+ }
+ else
+ {
+ response = "";
+ }
+ }
+ else
+ {
+ if (bad_folders_response.Length != 0)
+ {
+ response = "";
+ }
+ else
+ {
+ response = "";
+ }
+ }
+
+ // m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Replying to CAPS fetch inventory request");
+ //m_log.Debug("[WEB FETCH INV DESC HANDLER] "+response);
+
+ return response;
+
+ // }
+ }
+
///
/// Handle the caps inventory descendents fetch.
///
@@ -226,6 +347,7 @@ namespace OpenSim.Capabilities.Handlers
///
///
/// An empty InventoryCollection if the inventory look up failed
+ [Obsolete]
private InventoryCollection Fetch(
UUID agentID, UUID folderID, UUID ownerID,
bool fetchFolders, bool fetchItems, int sortOrder, out int version, out int descendents)
@@ -264,7 +386,6 @@ namespace OpenSim.Capabilities.Handlers
m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Could not get contents of folder {0} for user {1}", folderID, agentID);
return contents;
}
-
contents = fetchedContents;
InventoryFolderBase containingFolder = new InventoryFolderBase();
containingFolder.ID = folderID;
@@ -273,9 +394,9 @@ namespace OpenSim.Capabilities.Handlers
if (containingFolder != null)
{
-// m_log.DebugFormat(
-// "[WEB FETCH INV DESC HANDLER]: Retrieved folder {0} {1} for agent id {2}",
-// containingFolder.Name, containingFolder.ID, agentID);
+ //m_log.DebugFormat(
+ // "[WEB FETCH INV DESC HANDLER]: Retrieved folder {0} {1} for agent id {2}",
+ // containingFolder.Name, containingFolder.ID, agentID);
version = containingFolder.Version;
@@ -410,6 +531,160 @@ namespace OpenSim.Capabilities.Handlers
return contents;
}
+
+ private void AddLibraryFolders(List fetchFolders, List result)
+ {
+ InventoryFolderImpl fold;
+ if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null)
+ {
+ List libfolders = fetchFolders.FindAll(f => f.owner_id == m_LibraryService.LibraryRootFolder.Owner);
+ fetchFolders.RemoveAll(f => libfolders.Contains(f));
+
+ foreach (LLSDFetchInventoryDescendents f in libfolders)
+ {
+ if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(f.folder_id)) != null)
+ {
+ InventoryCollectionWithDescendents ret = new InventoryCollectionWithDescendents();
+ ret.Collection = new InventoryCollection();
+ ret.Collection.Folders = new List();
+ ret.Collection.Items = fold.RequestListOfItems();
+ ret.Collection.OwnerID = m_LibraryService.LibraryRootFolder.Owner;
+ ret.Collection.FolderID = f.folder_id;
+ ret.Collection.Version = fold.Version;
+
+ ret.Descendents = ret.Collection.Items.Count;
+
+ result.Add(ret);
+ }
+ }
+ }
+ }
+
+ private List Fetch(List fetchFolders)
+ {
+ //m_log.DebugFormat(
+ // "[WEB FETCH INV DESC HANDLER]: Fetching {0} folders for owner {1}", fetchFolders.Count, fetchFolders[0].owner_id);
+
+ // FIXME MAYBE: We're not handling sortOrder!
+
+ List result = new List();
+
+ AddLibraryFolders(fetchFolders, result);
+
+ if (fetchFolders.Count > 0)
+ {
+ UUID[] fids = new UUID[fetchFolders.Count];
+ int i = 0;
+ foreach (LLSDFetchInventoryDescendents f in fetchFolders)
+ fids[i++] = f.folder_id;
+
+ InventoryCollection[] fetchedContents = m_InventoryService.GetMultipleFoldersContent(fetchFolders[0].owner_id, fids);
+
+ if (fetchedContents == null || (fetchedContents != null && fetchedContents.Length == 0))
+ {
+ //m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Could not get contents of multiple folders for user {0}", fetchFolders[0].owner_id);
+ return null;
+ }
+
+ i = 0;
+ // Do some post-processing. May need to fetch more from inv server for links
+ foreach (InventoryCollection contents in fetchedContents)
+ {
+ InventoryCollectionWithDescendents coll = new InventoryCollectionWithDescendents();
+ coll.Collection = contents;
+
+ // Find the original request
+ LLSDFetchInventoryDescendents freq = fetchFolders[i++];
+
+ // The inventory server isn't sending FolderID in the collection...
+ // Must fetch it individually
+ if (contents.FolderID == UUID.Zero)
+ {
+ InventoryFolderBase containingFolder = new InventoryFolderBase();
+ containingFolder.ID = freq.folder_id;
+ containingFolder.Owner = freq.owner_id;
+ containingFolder = m_InventoryService.GetFolder(containingFolder);
+
+ if (containingFolder != null)
+ {
+ contents.FolderID = containingFolder.ID;
+ contents.OwnerID = containingFolder.Owner;
+ contents.Version = containingFolder.Version;
+ }
+ else
+ {
+ m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Unable to fetch folder {0}", freq.folder_id);
+ continue;
+ }
+ }
+
+ if (freq.fetch_items && contents.Items != null)
+ {
+ List itemsToReturn = contents.Items;
+ List originalItems = new List(itemsToReturn);
+
+ // descendents must only include the links, not the linked items we add
+ coll.Descendents = originalItems.Count;
+
+ // Add target items for links in this folder before the links themselves.
+ foreach (InventoryItemBase item in originalItems)
+ {
+ if (item.AssetType == (int)AssetType.Link)
+ {
+ InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID));
+
+ // Take care of genuinely broken links where the target doesn't exist
+ // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
+ // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
+ // rather than having to keep track of every folder requested in the recursion.
+ if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
+ {
+ itemsToReturn.Insert(0, linkedItem);
+ }
+ }
+ }
+
+ // Now scan for folder links and insert the items they target and those links at the head of the return data
+ foreach (InventoryItemBase item in originalItems)
+ {
+ if (item.AssetType == (int)AssetType.LinkFolder)
+ {
+ InventoryCollection linkedFolderContents = m_InventoryService.GetFolderContent(coll.Collection.OwnerID, item.AssetID);
+ List links = linkedFolderContents.Items;
+
+ itemsToReturn.InsertRange(0, links);
+
+ foreach (InventoryItemBase link in linkedFolderContents.Items)
+ {
+ // Take care of genuinely broken links where the target doesn't exist
+ // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
+ // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
+ // rather than having to keep track of every folder requested in the recursion.
+ if (link != null)
+ {
+ //m_log.DebugFormat(
+ // "[WEB FETCH INV DESC HANDLER]: Adding item {0} {1} from folder {2} linked from {3}",
+ // link.Name, (AssetType)link.AssetType, item.AssetID, contents.FolderID);
+
+ InventoryItemBase linkedItem
+ = m_InventoryService.GetItem(new InventoryItemBase(link.AssetID));
+
+ if (linkedItem != null)
+ itemsToReturn.Insert(0, linkedItem);
+ }
+ }
+ }
+ }
+ }
+
+ result.Add(coll);
+ }
+ }
+
+ return result;
+ }
+
+
///
/// Convert an internal inventory folder object into an LLSD object.
///
@@ -462,4 +737,10 @@ namespace OpenSim.Capabilities.Handlers
return llsdItem;
}
}
+
+ struct InventoryCollectionWithDescendents
+ {
+ public InventoryCollection Collection;
+ public int Descendents;
+ }
}
\ No newline at end of file
--
cgit v1.1