a few changes to get assets

This commit is contained in:
UbitUmarov
2021-07-16 03:08:31 +01:00
parent cdcf468e5c
commit a01046c24f
12 changed files with 244 additions and 22 deletions

View File

@@ -30,6 +30,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Threading;
using log4net;
using Nini.Config;
using OpenMetaverse;
@@ -122,7 +123,18 @@ namespace OpenSim.Capabilities.Handlers
if(!UUID.TryParse(assetStr, out assetID))
return;
AssetBase asset = m_assetService.Get(assetID.ToString(), serviceURL, false);
ManualResetEventSlim done = new ManualResetEventSlim(false);
AssetBase asset = null;
m_assetService.Get(assetID.ToString(), serviceURL, false, (AssetBase a) =>
{
asset = a;
done.Set();
});
done.Wait();
done.Dispose();
done = null;
if (asset == null)
{
// m_log.Warn("[GETASSET]: not found: " + query + " " + assetStr);

View File

@@ -53,6 +53,8 @@ namespace OpenSim.Framework
// as get without negative cache check
AssetBase GetCached(string id);
bool GetFromMemory(string id, out AssetBase asset);
/// <summary>
/// Check whether an asset with the specified id exists in the cache.
/// </summary>

View File

@@ -313,10 +313,10 @@ namespace OpenSim.Region.CoreModules.Asset
if (m_FileCacheEnabled && m_assetFileWriteWorker == null)
{
m_assetFileWriteWorker = new ObjectJobEngine(ProcessWrites, "FloatsamCacheWriter", 1000 , 1);
m_assetFileWriteWorker = new ObjectJobEngine(ProcessWrites, "FloatsamCacheWriter", 1000, 1);
}
if(!string.IsNullOrWhiteSpace(m_assetLoader) && scene.RegionInfo.RegionID == m_Scenes[0].RegionInfo.RegionID)
if (!string.IsNullOrWhiteSpace(m_assetLoader) && scene.RegionInfo.RegionID == m_Scenes[0].RegionInfo.RegionID)
{
IAssetLoader assetLoader = ServerUtils.LoadPlugin<IAssetLoader>(m_assetLoader, new object[] { });
if (assetLoader != null)
@@ -341,11 +341,11 @@ namespace OpenSim.Region.CoreModules.Asset
try
{
WriteAssetInfo wai = (WriteAssetInfo)o;
WriteFileCache(wai.filename,wai.asset,wai.replace);
WriteFileCache(wai.filename, wai.asset, wai.replace);
wai.asset = null;
Thread.Yield();
}
catch{ }
catch { }
}
////////////////////////////////////////////////////////////
@@ -644,6 +644,48 @@ namespace OpenSim.Region.CoreModules.Asset
return true;
}
public bool GetFromMemory(string id, out AssetBase asset)
{
asset = null;
m_Requests++;
if (id.Equals(Util.UUIDZeroString))
return false;
if (m_negativeCache.ContainsKey(id))
return false;
asset = GetFromWeakReference(id);
if (asset != null)
{
if (m_updateFileTimeOnCacheHit)
{
string filename = GetFileName(id);
UpdateFileLastAccessTime(filename);
}
if (m_MemoryCacheEnabled)
UpdateMemoryCache(id, asset);
return true;
}
if (m_MemoryCacheEnabled)
{
asset = GetFromMemoryCache(id);
if (asset != null)
{
UpdateWeakReference(id, asset);
if (m_updateFileTimeOnCacheHit)
{
string filename = GetFileName(id);
UpdateFileLastAccessTime(filename);
}
return true;
}
}
return true;
}
public bool Check(string id)
{
if(GetFromWeakReference(id) != null)
@@ -1578,6 +1620,11 @@ namespace OpenSim.Region.CoreModules.Asset
return true;
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
return;
}
public bool[] AssetsExist(string[] ids)
{
bool[] exist = new bool[ids.Length];

View File

@@ -267,6 +267,46 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
});
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
if (m_Cache != null)
{
AssetBase asset;
if (!m_Cache.GetFromMemory(id, out asset))
{
callBack(null);
return;
}
if (asset != null)
{
callBack(asset);
return;
}
}
if (id.Equals(Util.UUIDZeroString))
{
callBack(null);
return;
}
m_AssetService.Get(id, null, delegate (string assetID, object s, AssetBase a)
{
if (m_Cache != null)
{
if (a == null)
m_Cache.CacheNegative(assetID);
else
m_Cache.Cache(a);
}
//if (null == a)
//. m_log.WarnFormat("[LOCAL ASSET SERVICES CONNECTOR]: Could not asynchronously find asset with id {0}", id);
Util.FireAndForget(o => callBack(a), null, "LocalAssetServiceConnector.GotFromServiceCallback");
});
}
public bool[] AssetsExist(string[] ids)
{
return m_AssetService.AssetsExist(ids);

View File

@@ -51,7 +51,6 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
{
private static readonly ILog m_log = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType);
private delegate void AssetRetrievedEx(AssetBase asset);
private bool m_Enabled = false;
private Scene m_aScene;
@@ -69,9 +68,10 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
//private int m_retryCounter;
//private bool m_inRetries;
private Dictionary<string, List<AssetRetrievedEx>> m_AssetHandlers = new Dictionary<string, List<AssetRetrievedEx>>();
private Dictionary<string, List<SimpleAssetRetrieved>> m_AssetHandlers = new Dictionary<string, List<SimpleAssetRetrieved>>();
private ObjectJobEngine m_requestQueue;
private ObjectJobEngine m_localRequestsQueue;
private ObjectJobEngine m_remoteRequestsQueue;
public Type ReplaceableInterface
{
@@ -135,7 +135,8 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
m_AssetPerms = new AssetPermissions(hgConfig);
}
m_requestQueue = new ObjectJobEngine(AssetRequestProcessor, "GetAssetsWorkers", 2000, 2);
m_localRequestsQueue = new ObjectJobEngine(AssetRequestProcessor, "GetAssetsWorkers", 2000, 2);
m_remoteRequestsQueue = new ObjectJobEngine(AssetRequestProcessor, "GetRemoteAssetsWorkers", 2000, 2);
m_Enabled = true;
m_log.Info("[REGIONASSETCONNECTOR]: enabled");
}
@@ -151,8 +152,12 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
if (!m_Enabled)
return;
m_requestQueue.Dispose();
m_requestQueue = null;
m_localRequestsQueue.Dispose();
m_localRequestsQueue = null;
m_remoteRequestsQueue.Dispose();
m_remoteRequestsQueue = null;
}
public void AddRegion(Scene scene)
@@ -339,20 +344,26 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
AssetBase asset = null;
if (m_Cache != null)
{
if (!m_Cache.Get(id, out asset))
if (!m_Cache.GetFromMemory(id, out asset))
{
callBack(id, sender, null);
return false;
}
}
if (asset == null)
{
if (id.Equals(Util.UUIDZeroString))
{
callBack(id, sender, null);
return false;
}
lock (m_AssetHandlers)
{
AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate (AssetBase _asset) { callBack(id, sender, _asset); });
SimpleAssetRetrieved handlerEx = new SimpleAssetRetrieved(delegate (AssetBase _asset) { callBack(id, sender, _asset); _asset = null;});
List<AssetRetrievedEx> handlers;
List<SimpleAssetRetrieved> handlers;
if (m_AssetHandlers.TryGetValue(id, out handlers))
{
// Someone else is already loading this asset. It will notify our handler when done.
@@ -360,11 +371,11 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
return true;
}
handlers = new List<AssetRetrievedEx>();
handlers = new List<SimpleAssetRetrieved>();
handlers.Add(handlerEx);
m_AssetHandlers.Add(id, handlers);
m_requestQueue.Enqueue(id);
m_localRequestsQueue.Enqueue(id);
}
}
else
@@ -376,16 +387,93 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
return true;
}
public struct ForeignAssetServiceGetData
{
public string id;
public string ForeignAssetService;
public bool StoreOnLocalGrid;
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
AssetBase asset = null;
if (m_Cache != null)
{
if (!m_Cache.GetFromMemory(id, out asset))
{
callBack(null);
return;
}
}
if (asset == null)
{
if (id.Equals(Util.UUIDZeroString))
{
callBack(null);
return;
}
lock (m_AssetHandlers)
{
SimpleAssetRetrieved handlerEx = new SimpleAssetRetrieved(delegate (AssetBase _asset) { callBack(_asset); _asset = null;});
List<SimpleAssetRetrieved> handlers;
if (m_AssetHandlers.TryGetValue(id, out handlers))
{
// Someone else is already loading this asset. It will notify our handler when done.
handlers.Add(handlerEx);
return;
}
handlers = new List<SimpleAssetRetrieved>();
handlers.Add(handlerEx);
m_AssetHandlers.Add(id, handlers);
if(string.IsNullOrEmpty(ForeignAssetService))
m_localRequestsQueue.Enqueue(id);
else
{
ForeignAssetServiceGetData fasgd = new ForeignAssetServiceGetData
{
id = id,
ForeignAssetService = ForeignAssetService,
StoreOnLocalGrid = StoreOnLocalGrid
};
m_remoteRequestsQueue.Enqueue(fasgd);
}
}
}
else
{
if (asset != null && (asset.Data == null || asset.Data.Length == 0))
asset = null;
callBack(asset);
}
}
private void AssetRequestProcessor(object o)
{
string id = o as string;
if(id == null)
if( o == null)
return;
try
{
AssetBase a = Get(id);
List<AssetRetrievedEx> handlers;
AssetBase a;
string id;
if (o is ForeignAssetServiceGetData)
{
var fasgd = (ForeignAssetServiceGetData)o;
id = fasgd.id;
a = Get(id, fasgd.ForeignAssetService, fasgd.StoreOnLocalGrid);
}
else
{
id = (string)o;
a = Get(id);
}
List<SimpleAssetRetrieved> handlers;
lock (m_AssetHandlers)
{
handlers = m_AssetHandlers[id];
@@ -396,7 +484,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
{
Util.FireAndForget(x =>
{
foreach (AssetRetrievedEx h in handlers)
foreach (SimpleAssetRetrieved h in handlers)
{
try
{
@@ -405,6 +493,7 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
catch { }
}
handlers.Clear();
a = null;
});
}
}

View File

@@ -206,5 +206,9 @@ namespace OpenSim.Services.AssetService
return m_Database.Delete(id);
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
}
}
}

View File

@@ -227,5 +227,10 @@ namespace OpenSim.Services.AssetService
Store(asset);
m_ChainedAssetService.Delete(asset.ID);
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
}
}
}

View File

@@ -427,5 +427,10 @@ namespace OpenSim.Services.Connectors
string uri = m_ServerURI + "/assets/" + id;
return SynchronousRestObjectRequester.MakeRequest<int, bool>("DELETE", uri, 0, m_Auth);
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
return;
}
}
}

View File

@@ -91,6 +91,7 @@ namespace OpenSim.Services.Connectors
return connector.Get(id);
}
public AssetBase GetCached(string id)
{
string url = string.Empty;
@@ -140,6 +141,11 @@ namespace OpenSim.Services.Connectors
return false;
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
return;
}
private struct AssetAndIndex
{
public UUID assetID;

View File

@@ -834,5 +834,10 @@ namespace OpenSim.Services.FSAssetService
{
return Get(id);
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
return;
}
}
}

View File

@@ -130,6 +130,11 @@ namespace OpenSim.Services.HypergridService
return null;
}
public void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack)
{
return;
}
public AssetMetadata GetMetadata(string id)
{
AssetMetadata meta = m_assetService.GetMetadata(id);

View File

@@ -30,7 +30,8 @@ using OpenSim.Framework;
namespace OpenSim.Services.Interfaces
{
public delegate void AssetRetrieved(string id, Object sender, AssetBase asset);
public delegate void AssetRetrieved(string id, object sender, AssetBase asset);
public delegate void SimpleAssetRetrieved(AssetBase asset);
public interface IAssetService
{
@@ -76,6 +77,7 @@ namespace OpenSim.Services.Interfaces
/// </param>
/// <returns>True if the id was parseable, false otherwise</returns>
bool Get(string id, Object sender, AssetRetrieved handler);
void Get(string id, string ForeignAssetService, bool StoreOnLocalGrid, SimpleAssetRetrieved callBack);
/// <summary>
/// Check if assets exist in the database.