From e93defd0ca326754d1bd5a1a503d6d47428272be Mon Sep 17 00:00:00 2001 From: Mic Bowman Date: Fri, 8 Feb 2013 15:07:43 -0800 Subject: [PATCH] Adds size limits to JsonStore. Adds a separate configuration variable to enable binding to dynamic attributes. --- .../Scripting/JsonStore/JsonStore.cs | 51 ++++++++++++++++++- .../Scripting/JsonStore/JsonStoreModule.cs | 17 +++++++ bin/OpenSimDefaults.ini | 4 ++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStore.cs b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStore.cs index c7f0001e06..088d0cdf88 100644 --- a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStore.cs +++ b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStore.cs @@ -93,6 +93,15 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore // extract the internals of a has reference protected static Regex m_HashPattern = new Regex("{([^}]+)}"); + // ----------------------------------------------------------------- + /// + /// This is a simple estimator for the size of the stored data, it + /// is not precise, but should be close enough to implement reasonable + /// limits on the storage space used + /// + // ----------------------------------------------------------------- + public int StringSpace { get; set; } + // ----------------------------------------------------------------- /// /// @@ -110,6 +119,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore // ----------------------------------------------------------------- public JsonStore() { + StringSpace = 0; m_TakeStore = new List(); m_ReadStore = new List(); } @@ -247,6 +257,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore if (path.Count == 0) { ValueStore = ovalue; + StringSpace = 0; return true; } @@ -278,8 +289,13 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore { string npkey = String.Format("[{0}]",amap.Count); - amap.Add(ovalue); - InvokeNextCallback(pexpr + npkey); + if (ovalue != null) + { + StringSpace += ComputeSizeOf(ovalue); + + amap.Add(ovalue); + InvokeNextCallback(pexpr + npkey); + } return true; } @@ -287,9 +303,14 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore if (0 <= aval && aval < amap.Count) { if (ovalue == null) + { + StringSpace -= ComputeSizeOf(amap[aval]); amap.RemoveAt(aval); + } else { + StringSpace -= ComputeSizeOf(amap[aval]); + StringSpace += ComputeSizeOf(ovalue); amap[aval] = ovalue; InvokeNextCallback(pexpr + pkey); } @@ -313,6 +334,9 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore OSDMap hmap = result as OSDMap; if (ovalue != null) { + StringSpace -= ComputeSizeOf(hmap[hkey]); + StringSpace += ComputeSizeOf(ovalue); + hmap[hkey] = ovalue; InvokeNextCallback(pexpr + pkey); return true; @@ -321,6 +345,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore // this is the remove case if (hmap.ContainsKey(hkey)) { + StringSpace -= ComputeSizeOf(hmap[hkey]); hmap.Remove(hkey); return true; } @@ -531,8 +556,27 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore return pkey; } + + // ----------------------------------------------------------------- + /// + /// + /// + // ----------------------------------------------------------------- + protected static int ComputeSizeOf(OSD value) + { + string sval; + + if (ConvertOutputValue(value,out sval,true)) + return sval.Length; + + return 0; + } } + // ----------------------------------------------------------------- + /// + /// + // ----------------------------------------------------------------- public class JsonObjectStore : JsonStore { private static readonly ILog m_log = @@ -566,6 +610,9 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore { m_scene = scene; m_objectID = oid; + + // the size limit is imposed on whatever is already in the store + StringSpace = ComputeSizeOf(ValueStore); } } diff --git a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreModule.cs b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreModule.cs index 3249aa3216..f1ce856995 100644 --- a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreModule.cs +++ b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreModule.cs @@ -54,6 +54,9 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore private IConfig m_config = null; private bool m_enabled = false; + private bool m_enableObjectStore = false; + private int m_maxStringSpace = Int32.MaxValue; + private Scene m_scene = null; private Dictionary m_JsonValueStore; @@ -90,6 +93,10 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore } m_enabled = m_config.GetBoolean("Enabled", m_enabled); + m_enableObjectStore = m_config.GetBoolean("EnableObjectStore", m_enableObjectStore); + m_maxStringSpace = m_config.GetInt("MaxStringSpace", m_maxStringSpace); + if (m_maxStringSpace == 0) + m_maxStringSpace = Int32.MaxValue; } catch (Exception e) { @@ -178,6 +185,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore public bool AttachObjectStore(UUID objectID) { if (! m_enabled) return false; + if (! m_enableObjectStore) return false; SceneObjectPart sop = m_scene.GetSceneObjectPart(objectID); if (sop == null) @@ -311,7 +319,16 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore try { lock (map) + { + if (map.StringSpace > m_maxStringSpace) + { + m_log.WarnFormat("[JsonStore] {0} exceeded string size; {1} bytes used of {2} limit", + storeID,map.StringSpace,m_maxStringSpace); + return false; + } + return map.SetValue(path,value,useJson); + } } catch (Exception e) { diff --git a/bin/OpenSimDefaults.ini b/bin/OpenSimDefaults.ini index 7bdfd1ce30..2512428757 100644 --- a/bin/OpenSimDefaults.ini +++ b/bin/OpenSimDefaults.ini @@ -1652,6 +1652,10 @@ [JsonStore] Enabled = False +;; Enable direct access to the SOP dynamic attributes +EnableObjectStore = False +MaxStringSpace = 0 + ;; ;; These are defaults that are overwritten below in [Architecture]. ;; These defaults allow OpenSim to work out of the box with