From 202212bcbcb64ad1889025dedc34b48d0704de96 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sat, 30 Jul 2016 03:13:23 +0100 Subject: add a regionInfoCache get by world 2D position --- .../ServiceConnectorsOut/Grid/RegionInfoCache.cs | 63 +++++++++++++++++++++- .../Grid/RemoteGridServiceConnector.cs | 24 ++++----- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Grid/RegionInfoCache.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Grid/RegionInfoCache.cs index 4dc9033..8403362 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Grid/RegionInfoCache.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Grid/RegionInfoCache.cs @@ -109,6 +109,21 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid return null; } + + public GridRegion Get(UUID scopeID, int x, int y, out bool inCache) + { + inCache = false; + + GridRegion rinfo = null; + if (m_Cache.TryGetValue(scopeID, x, y, out rinfo)) + { + inCache = true; + return rinfo; + } + + return null; + } + } @@ -222,8 +237,8 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid public sealed class RegionsExpiringCache { - const double CACHE_PURGE_HZ = 60; - const int MAX_LOCK_WAIT = 5000; // milliseconds + const double CACHE_PURGE_HZ = 60; // seconds + const int MAX_LOCK_WAIT = 10000; // milliseconds /// For thread safety object syncRoot = new object(); @@ -463,6 +478,50 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid return false; } + // gets a region that contains world position (x,y) + // hopefull will not take ages + public bool TryGetValue(UUID scope, int x, int y, out GridRegion value) + { + if (!Monitor.TryEnter(syncRoot, MAX_LOCK_WAIT)) + throw new ApplicationException("Lock could not be acquired after " + MAX_LOCK_WAIT + "ms"); + try + { + value = null; + + if(timedStorage.Count == 0) + return false; + + foreach(KeyValuePair kvp in timedStorage) + { + if(kvp.Key.ScopeID != scope) + continue; + + GridRegion r = kvp.Value; + if(r == null) // ?? + continue; + int test = r.RegionLocX; + if(x < test) + continue; + test += r.RegionSizeX; + if(x >= test) + continue; + test = r.RegionLocY; + if(y < test) + continue; + test += r.RegionSizeY; + if (y < test) + { + value = r; + return true; + } + } + } + finally { Monitor.Exit(syncRoot); } + + value = null; + return false; + } + public bool Update(UUID scope, GridRegion region, double expirationSeconds) { if (!Monitor.TryEnter(syncRoot, MAX_LOCK_WAIT)) diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Grid/RemoteGridServiceConnector.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Grid/RemoteGridServiceConnector.cs index 9e27abe..1e1e8f8 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Grid/RemoteGridServiceConnector.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Grid/RemoteGridServiceConnector.cs @@ -193,26 +193,18 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid // The coordinates are world coords (meters), NOT region units. public GridRegion GetRegionByPosition(UUID scopeID, int x, int y) { + // try in cache by handler first ulong regionHandle = Util.RegionWorldLocToHandle((uint)x, (uint)y); - uint regionX = Util.WorldToRegionLoc((uint)x); - uint regionY = Util.WorldToRegionLoc((uint)y); -/* this is no longer valid - // Sanity check - if ((Util.RegionToWorldLoc(regionX) != (uint)x) || (Util.RegionToWorldLoc(regionY) != (uint)y)) - { - m_log.WarnFormat("[REMOTE GRID CONNECTOR]: GetRegionByPosition. Bad position requested: not the base of the region. Requested Pos=<{0},{1}>, Should Be=<{2},{3}>", - x, y, Util.RegionToWorldLoc(regionX), Util.RegionToWorldLoc(regionY)); - } -*/ bool inCache = false; GridRegion rinfo = m_RegionInfoCache.Get(scopeID, regionHandle, out inCache); if (inCache) - { -// m_log.DebugFormat("[REMOTE GRID CONNECTOR]: GetRegionByPosition. Found region {0} in cache. Pos=<{1},{2}>, RegionHandle={3}", -// (rinfo == null) ? "" : rinfo.RegionName, regionX, regionY, (rinfo == null) ? regionHandle : rinfo.RegionHandle); return rinfo; - } + + // try in cache by slower position next + rinfo = m_RegionInfoCache.Get(scopeID, x, y, out inCache); + if (inCache) + return rinfo; rinfo = m_LocalGridService.GetRegionByPosition(scopeID, x, y); if (rinfo == null) @@ -221,7 +213,11 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid m_RegionInfoCache.Cache(rinfo); if (rinfo == null) + { + uint regionX = Util.WorldToRegionLoc((uint)x); + uint regionY = Util.WorldToRegionLoc((uint)y); m_log.WarnFormat("[REMOTE GRID CONNECTOR]: Requested region {0}-{1} not found", regionX, regionY); + } else m_log.DebugFormat("[REMOTE GRID CONNECTOR]: GetRegionByPosition. Added region {0} to the cache. Pos=<{1},{2}>, RegionHandle={3}", rinfo.RegionName, rinfo.RegionCoordX, rinfo.RegionCoordY, (rinfo == null) ? regionHandle : rinfo.RegionHandle); -- cgit v1.1