First pass at fixing justincc's feedback v2 ( http://opensimulator.org/mantis/view.php?id=5440 )

Fixing everything here (I think) except the per-region config. That's next.
bulletsim
Sean McNamara 2011-05-02 02:20:50 -04:00
parent 587aa91e36
commit 2aab033aaa
2 changed files with 868 additions and 768 deletions

View File

@ -1,30 +1,29 @@
#pragma warning disable 1587 /*
/// * Copyright (c) Contributors, http://opensimulator.org/
/// Copyright (c) Contributors, http://opensimulator.org/ * See CONTRIBUTORS.TXT for a full list of copyright holders.
/// See CONTRIBUTORS.TXT for a full list of copyright holders. *
/// * Redistribution and use in source and binary forms, with or without
/// Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met:
/// modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright
/// * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
/// notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright
/// * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the
/// notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
/// documentation and/or other materials provided with the distribution. * * Neither the name of the OpenSimulator Project nor the
/// * Neither the name of the OpenSimulator Project nor the * names of its contributors may be used to endorse or promote products
/// names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission.
/// derived from this software without specific prior written permission. *
/// * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
/// THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
/// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
/// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
/// DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
/// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
/// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
/// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
/// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
/// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
///
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -39,49 +38,14 @@ using OpenSim.Framework;
using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Framework.Scenes; using OpenSim.Region.Framework.Scenes;
///
/// Config Settings Documentation.
/// At the TOP LEVEL, e.g. in OpenSim.ini, we have the following options:
/// EACH REGION, in OpenSim.ini, can have the following settings under the [AutoBackupModule] section.
/// IMPORTANT: You may optionally specify the key name as follows for a per-region key: <Region Name>.<Key Name>
/// Example: My region is named Foo.
/// If I wanted to specify the "AutoBackupInterval" key just for this region, I would name my key "Foo.AutoBackupInterval", under the [AutoBackupModule] section of OpenSim.ini.
/// Instead of specifying them on a per-region basis, you can also omit the region name to specify the default setting for all regions.
/// Region-specific settings take precedence.
///
/// AutoBackupModuleEnabled: True/False. Default: False. If True, use the auto backup module. This setting does not support per-region basis.
/// All other settings under [AutoBackupModule] are ignored if AutoBackupModuleEnabled is false, even per-region settings!
/// AutoBackup: True/False. Default: False. If True, activate auto backup functionality.
/// This is the only required option for enabling auto-backup; the other options have sane defaults.
/// If False for a particular region, the auto-backup module becomes a no-op for the region, and all other AutoBackup* settings are ignored.
/// If False globally (the default), only regions that specifically override this with "FooRegion.AutoBackup = true" will get AutoBackup functionality.
/// AutoBackupInterval: Double, non-negative value. Default: 720 (12 hours).
/// The number of minutes between each backup attempt.
/// If a negative or zero value is given, it is equivalent to setting AutoBackup = False.
/// AutoBackupBusyCheck: True/False. Default: True.
/// If True, we will only take an auto-backup if a set of conditions are met.
/// These conditions are heuristics to try and avoid taking a backup when the sim is busy.
/// AutoBackupScript: String. Default: not specified (disabled).
/// File path to an executable script or binary to run when an automatic backup is taken.
/// The file should really be (Windows) an .exe or .bat, or (Linux/Mac) a shell script or binary.
/// Trying to "run" directories, or things with weird file associations on Win32, might cause unexpected results!
/// argv[1] of the executed file/script will be the file name of the generated OAR.
/// If the process can't be spawned for some reason (file not found, no execute permission, etc), write a warning to the console.
/// AutoBackupNaming: string. Default: Time.
/// One of three strings (case insensitive):
/// "Time": Current timestamp is appended to file name. An existing file will never be overwritten.
/// "Sequential": A number is appended to the file name. So if RegionName_x.oar exists, we'll save to RegionName_{x+1}.oar next. An existing file will never be overwritten.
/// "Overwrite": Always save to file named "${AutoBackupDir}/RegionName.oar", even if we have to overwrite an existing file.
/// AutoBackupDir: String. Default: "." (the current directory).
/// A directory (absolute or relative) where backups should be saved.
/// AutoBackupDilationThreshold: float. Default: 0.5. Lower bound on time dilation required for BusyCheck heuristics to pass.
/// If the time dilation is below this value, don't take a backup right now.
/// AutoBackupAgentThreshold: int. Default: 10. Upper bound on # of agents in region required for BusyCheck heuristics to pass.
/// If the number of agents is greater than this value, don't take a backup right now.
///
namespace OpenSim.Region.OptionalModules.World.AutoBackup namespace OpenSim.Region.OptionalModules.World.AutoBackup
{ {
/// <summary>
/// Choose between ways of naming the backup files that are generated.
/// </summary>
/// <remarks>Time: OARs are named by a timestamp.
/// Sequential: OARs are named by counting (Region_1.oar, Region_2.oar, etc.)
/// Overwrite: Only one file per region is created; it's overwritten each time a backup is made.</remarks>
public enum NamingType public enum NamingType
{ {
Time, Time,
@ -89,13 +53,55 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
Overwrite Overwrite
} }
///<summary>
/// AutoBackupModule: save OAR region backups to disk periodically
/// </summary>
/// <remarks>
/// Config Settings Documentation.
/// At the TOP LEVEL, e.g. in OpenSim.ini, we have the following options:
/// EACH REGION, in OpenSim.ini, can have the following settings under the [AutoBackupModule] section.
/// IMPORTANT: You may optionally specify the key name as follows for a per-region key: [Region Name].[Key Name]
/// Example: My region is named Foo.
/// If I wanted to specify the "AutoBackupInterval" key just for this region, I would name my key "Foo.AutoBackupInterval", under the [AutoBackupModule] section of OpenSim.ini.
/// Instead of specifying them on a per-region basis, you can also omit the region name to specify the default setting for all regions.
/// Region-specific settings take precedence.
///
/// AutoBackupModuleEnabled: True/False. Default: False. If True, use the auto backup module. This setting does not support per-region basis.
/// All other settings under [AutoBackupModule] are ignored if AutoBackupModuleEnabled is false, even per-region settings!
/// AutoBackup: True/False. Default: False. If True, activate auto backup functionality.
/// This is the only required option for enabling auto-backup; the other options have sane defaults.
/// If False for a particular region, the auto-backup module becomes a no-op for the region, and all other AutoBackup* settings are ignored.
/// If False globally (the default), only regions that specifically override this with "FooRegion.AutoBackup = true" will get AutoBackup functionality.
/// AutoBackupInterval: Double, non-negative value. Default: 720 (12 hours).
/// The number of minutes between each backup attempt.
/// If a negative or zero value is given, it is equivalent to setting AutoBackup = False.
/// AutoBackupBusyCheck: True/False. Default: True.
/// If True, we will only take an auto-backup if a set of conditions are met.
/// These conditions are heuristics to try and avoid taking a backup when the sim is busy.
/// AutoBackupScript: String. Default: not specified (disabled).
/// File path to an executable script or binary to run when an automatic backup is taken.
/// The file should really be (Windows) an .exe or .bat, or (Linux/Mac) a shell script or binary.
/// Trying to "run" directories, or things with weird file associations on Win32, might cause unexpected results!
/// argv[1] of the executed file/script will be the file name of the generated OAR.
/// If the process can't be spawned for some reason (file not found, no execute permission, etc), write a warning to the console.
/// AutoBackupNaming: string. Default: Time.
/// One of three strings (case insensitive):
/// "Time": Current timestamp is appended to file name. An existing file will never be overwritten.
/// "Sequential": A number is appended to the file name. So if RegionName_x.oar exists, we'll save to RegionName_{x+1}.oar next. An existing file will never be overwritten.
/// "Overwrite": Always save to file named "${AutoBackupDir}/RegionName.oar", even if we have to overwrite an existing file.
/// AutoBackupDir: String. Default: "." (the current directory).
/// A directory (absolute or relative) where backups should be saved.
/// AutoBackupDilationThreshold: float. Default: 0.5. Lower bound on time dilation required for BusyCheck heuristics to pass.
/// If the time dilation is below this value, don't take a backup right now.
/// AutoBackupAgentThreshold: int. Default: 10. Upper bound on # of agents in region required for BusyCheck heuristics to pass.
/// If the number of agents is greater than this value, don't take a backup right now
/// Save memory by setting low initial capacities. Minimizes impact in common cases of all regions using same interval, and instances hosting 1 ~ 4 regions.
/// Also helps if you don't want AutoBackup at all.
/// </remarks>
public class AutoBackupModule : ISharedRegionModule public class AutoBackupModule : ISharedRegionModule
{ {
private static readonly ILog m_log = private static readonly ILog m_log =
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// Save memory by setting low initial capacities. Minimizes impact in common cases of all regions using same interval, and instances hosting 1 ~ 4 regions.
/// Also helps if you don't want AutoBackup at all
private readonly Dictionary<Guid, IScene> m_pendingSaves = new Dictionary<Guid, IScene>(1); private readonly Dictionary<Guid, IScene> m_pendingSaves = new Dictionary<Guid, IScene>(1);
private readonly AutoBackupModuleState m_defaultState = new AutoBackupModuleState(); private readonly AutoBackupModuleState m_defaultState = new AutoBackupModuleState();
private readonly Dictionary<IScene, AutoBackupModuleState> m_states = private readonly Dictionary<IScene, AutoBackupModuleState> m_states =
@ -106,11 +112,16 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
private bool m_enabled; private bool m_enabled;
/// <summary>
/// Whether the shared module should be enabled at all. NOT the same as m_Enabled in AutoBackupModuleState! /// Whether the shared module should be enabled at all. NOT the same as m_Enabled in AutoBackupModuleState!
/// </summary>
private bool m_closed; private bool m_closed;
private IConfigSource m_configSource; private IConfigSource m_configSource;
/// <summary>
/// Required by framework.
/// </summary>
public bool IsSharedModule public bool IsSharedModule
{ {
get { return true; } get { return true; }
@ -118,19 +129,29 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
#region ISharedRegionModule Members #region ISharedRegionModule Members
/// <summary>
/// Identifies the module to the system.
/// </summary>
string IRegionModuleBase.Name string IRegionModuleBase.Name
{ {
get { return "AutoBackupModule"; } get { return "AutoBackupModule"; }
} }
/// <summary>
/// We don't implement an interface, this is a single-use module.
/// </summary>
Type IRegionModuleBase.ReplaceableInterface Type IRegionModuleBase.ReplaceableInterface
{ {
get { return null; } get { return null; }
} }
/// <summary>
/// Called once in the lifetime of the module at startup.
/// </summary>
/// <param name="source">The input config source for OpenSim.ini.</param>
void IRegionModuleBase.Initialise(IConfigSource source) void IRegionModuleBase.Initialise(IConfigSource source)
{ {
/// Determine if we have been enabled at all in OpenSim.ini -- this is part and parcel of being an optional module // Determine if we have been enabled at all in OpenSim.ini -- this is part and parcel of being an optional module
this.m_configSource = source; this.m_configSource = source;
IConfig moduleConfig = source.Configs["AutoBackupModule"]; IConfig moduleConfig = source.Configs["AutoBackupModule"];
if (moduleConfig == null) if (moduleConfig == null)
@ -163,6 +184,9 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
m_log.Debug(abms.ToString()); m_log.Debug(abms.ToString());
} }
/// <summary>
/// Called once at de-init (sim shutting down).
/// </summary>
void IRegionModuleBase.Close() void IRegionModuleBase.Close()
{ {
if (!this.m_enabled) if (!this.m_enabled)
@ -170,15 +194,22 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
return; return;
} }
/// We don't want any timers firing while the sim's coming down; strange things may happen. // We don't want any timers firing while the sim's coming down; strange things may happen.
this.StopAllTimers(); this.StopAllTimers();
} }
/// <summary>
/// Currently a no-op for AutoBackup because we have to wait for region to be fully loaded.
/// </summary>
/// <param name="scene"></param>
void IRegionModuleBase.AddRegion(Scene scene) void IRegionModuleBase.AddRegion(Scene scene)
{ {
/// NO-OP. Wait for the region to be loaded.
} }
/// <summary>
/// Here we just clean up some resources and stop the OAR backup (if any) for the given scene.
/// </summary>
/// <param name="scene">The scene (region) to stop performing AutoBackup on.</param>
void IRegionModuleBase.RemoveRegion(Scene scene) void IRegionModuleBase.RemoveRegion(Scene scene)
{ {
if (!this.m_enabled) if (!this.m_enabled)
@ -190,12 +221,12 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
{ {
AutoBackupModuleState abms = this.m_states[scene]; AutoBackupModuleState abms = this.m_states[scene];
/// Remove this scene out of the timer map list // Remove this scene out of the timer map list
Timer timer = abms.Timer; Timer timer = abms.Timer;
List<IScene> list = this.m_timerMap[timer]; List<IScene> list = this.m_timerMap[timer];
list.Remove(scene); list.Remove(scene);
/// Shut down the timer if this was the last scene for the timer // Shut down the timer if this was the last scene for the timer
if (list.Count == 0) if (list.Count == 0)
{ {
this.m_timerMap.Remove(timer); this.m_timerMap.Remove(timer);
@ -206,6 +237,11 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
} }
} }
/// <summary>
/// Most interesting/complex code paths in AutoBackup begin here.
/// We read lots of Nini config, maybe set a timer, add members to state tracking Dictionaries, etc.
/// </summary>
/// <param name="scene">The scene to (possibly) perform AutoBackup on.</param>
void IRegionModuleBase.RegionLoaded(Scene scene) void IRegionModuleBase.RegionLoaded(Scene scene)
{ {
if (!this.m_enabled) if (!this.m_enabled)
@ -213,7 +249,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
return; return;
} }
/// This really ought not to happen, but just in case, let's pretend it didn't... // This really ought not to happen, but just in case, let's pretend it didn't...
if (scene == null) if (scene == null)
{ {
return; return;
@ -224,13 +260,22 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
m_log.Debug((abms == null ? "DEFAULT" : abms.ToString())); m_log.Debug((abms == null ? "DEFAULT" : abms.ToString()));
} }
/// <summary>
/// Currently a no-op.
/// </summary>
void ISharedRegionModule.PostInitialise() void ISharedRegionModule.PostInitialise()
{ {
/// I don't care right now.
} }
#endregion #endregion
/// <summary>
/// Set up internal state for a given scene. Fairly complex code.
/// When this method returns, we've started auto-backup timers, put members in Dictionaries, and created a State object for this scene.
/// </summary>
/// <param name="scene">The scene to look at.</param>
/// <param name="parseDefault">Whether this call is intended to figure out what we consider the "default" config (applied to all regions unless overridden by per-region settings).</param>
/// <returns>An AutoBackupModuleState contains most information you should need to know relevant to auto-backup, as applicable to a single region.</returns>
private AutoBackupModuleState ParseConfig(IScene scene, bool parseDefault) private AutoBackupModuleState ParseConfig(IScene scene, bool parseDefault)
{ {
string sRegionName; string sRegionName;
@ -253,11 +298,11 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
state = null; state = null;
} }
/// Read the config settings and set variables. // Read the config settings and set variables.
IConfig config = this.m_configSource.Configs["AutoBackupModule"]; IConfig config = this.m_configSource.Configs["AutoBackupModule"];
if (config == null) if (config == null)
{ {
/// defaultState would be disabled too if the section doesn't exist. // defaultState would be disabled too if the section doesn't exist.
state = this.m_defaultState; state = this.m_defaultState;
m_log.Info("[AUTO BACKUP]: Region " + sRegionLabel + " is NOT AutoBackup enabled."); m_log.Info("[AUTO BACKUP]: Region " + sRegionLabel + " is NOT AutoBackup enabled.");
return state; return state;
@ -275,10 +320,9 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
state.Enabled = tmpEnabled; state.Enabled = tmpEnabled;
} }
/// If you don't want AutoBackup, we stop. // If you don't want AutoBackup, we stop.
if ((state == null && !this.m_defaultState.Enabled) || (state != null && !state.Enabled)) if ((state == null && !this.m_defaultState.Enabled) || (state != null && !state.Enabled))
{ {
m_log.Info("[AUTO BACKUP]: Region " + sRegionLabel + " is NOT AutoBackup enabled.");
return state; return state;
} }
else else
@ -286,7 +330,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
m_log.Info("[AUTO BACKUP]: Region " + sRegionLabel + " is AutoBackup ENABLED."); m_log.Info("[AUTO BACKUP]: Region " + sRegionLabel + " is AutoBackup ENABLED.");
} }
/// Borrow an existing timer if one exists for the same interval; otherwise, make a new one. // Borrow an existing timer if one exists for the same interval; otherwise, make a new one.
double interval = double interval =
config.GetDouble(prepend + "AutoBackupInterval", this.m_defaultState.IntervalMinutes)* config.GetDouble(prepend + "AutoBackupInterval", this.m_defaultState.IntervalMinutes)*
60000.0; 60000.0;
@ -306,7 +350,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
} }
else else
{ {
/// 0 or negative interval == do nothing. // 0 or negative interval == do nothing.
if (interval <= 0.0 && state != null) if (interval <= 0.0 && state != null)
{ {
state.Enabled = false; state.Enabled = false;
@ -325,7 +369,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
tim.Start(); tim.Start();
} }
/// Add the current region to the list of regions tied to this timer. // Add the current region to the list of regions tied to this timer.
if (scene != null) if (scene != null)
{ {
if (state != null) if (state != null)
@ -368,7 +412,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
state.BusyCheck = tmpBusyCheck; state.BusyCheck = tmpBusyCheck;
} }
/// Set file naming algorithm // Set file naming algorithm
string stmpNamingType = config.GetString(prepend + "AutoBackupNaming", string stmpNamingType = config.GetString(prepend + "AutoBackupNaming",
this.m_defaultState.NamingType.ToString()); this.m_defaultState.NamingType.ToString());
NamingType tmpNamingType; NamingType tmpNamingType;
@ -422,7 +466,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
if (state != null) if (state != null)
{ {
state.BackupDir = tmpBackupDir; state.BackupDir = tmpBackupDir;
/// Let's give the user *one* convenience and auto-mkdir // Let's give the user some convenience and auto-mkdir
if (state.BackupDir != ".") if (state.BackupDir != ".")
{ {
try try
@ -447,14 +491,21 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
return state; return state;
} }
/// <summary>
/// Called when any auto-backup timer expires. This starts the code path for actually performing a backup.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HandleElapsed(object sender, ElapsedEventArgs e) private void HandleElapsed(object sender, ElapsedEventArgs e)
{ {
/// TODO?: heuristic thresholds are per-region, so we should probably run heuristics once per region // TODO: heuristic thresholds are per-region, so we should probably run heuristics once per region
/// XXX: Running heuristics once per region could add undue performance penalty for something that's supposed to // XXX: Running heuristics once per region could add undue performance penalty for something that's supposed to
/// check whether the region is too busy! Especially on sims with LOTS of regions. // check whether the region is too busy! Especially on sims with LOTS of regions.
/// Alternative: make heuristics thresholds global to the module rather than per-region. Less flexible, // Alternative: make heuristics thresholds global to the module rather than per-region. Less flexible,
/// but would allow us to be semantically correct while being easier on perf. // but would allow us to be semantically correct while being easier on perf.
/// Alternative 2: Run heuristics once per unique set of heuristics threshold parameters! Ay yi yi... // Alternative 2: Run heuristics once per unique set of heuristics threshold parameters! Ay yi yi...
// Alternative 3: Don't support per-region heuristics at all; just accept them as a global only parameter.
// Since this is pretty experimental, I haven't decided which alternative makes the most sense.
if (this.m_closed) if (this.m_closed)
{ {
return; return;
@ -474,18 +525,18 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
AutoBackupModuleState state = this.m_states[scene]; AutoBackupModuleState state = this.m_states[scene];
bool heuristics = state.BusyCheck; bool heuristics = state.BusyCheck;
/// Fast path: heuristics are on; already ran em; and sim is fine; OR, no heuristics for the region. // Fast path: heuristics are on; already ran em; and sim is fine; OR, no heuristics for the region.
if ((heuristics && heuristicsRun && heuristicsPassed) || !heuristics) if ((heuristics && heuristicsRun && heuristicsPassed) || !heuristics)
{ {
this.DoRegionBackup(scene); this.DoRegionBackup(scene);
/// Heuristics are on; ran but we're too busy -- keep going. Maybe another region will have heuristics off! // Heuristics are on; ran but we're too busy -- keep going. Maybe another region will have heuristics off!
} }
else if (heuristicsRun) else if (heuristicsRun)
{ {
m_log.Info("[AUTO BACKUP]: Heuristics: too busy to backup " + m_log.Info("[AUTO BACKUP]: Heuristics: too busy to backup " +
scene.RegionInfo.RegionName + " right now."); scene.RegionInfo.RegionName + " right now.");
continue; continue;
/// Logical Deduction: heuristics are on but haven't been run // Logical Deduction: heuristics are on but haven't been run
} }
else else
{ {
@ -503,11 +554,15 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
} }
} }
/// <summary>
/// Save an OAR, register for the callback for when it's done, then call the AutoBackupScript (if applicable).
/// </summary>
/// <param name="scene"></param>
private void DoRegionBackup(IScene scene) private void DoRegionBackup(IScene scene)
{ {
if (scene.RegionStatus != RegionStatus.Up) if (scene.RegionStatus != RegionStatus.Up)
{ {
/// We won't backup a region that isn't operating normally. // We won't backup a region that isn't operating normally.
m_log.Warn("[AUTO BACKUP]: Not backing up region " + scene.RegionInfo.RegionName + m_log.Warn("[AUTO BACKUP]: Not backing up region " + scene.RegionInfo.RegionName +
" because its status is " + scene.RegionStatus); " because its status is " + scene.RegionStatus);
return; return;
@ -518,7 +573,6 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
string savePath = BuildOarPath(scene.RegionInfo.RegionName, string savePath = BuildOarPath(scene.RegionInfo.RegionName,
state.BackupDir, state.BackupDir,
state.NamingType); state.NamingType);
/// m_log.Debug("[AUTO BACKUP]: savePath = " + savePath);
if (savePath == null) if (savePath == null)
{ {
m_log.Warn("[AUTO BACKUP]: savePath is null in HandleElapsed"); m_log.Warn("[AUTO BACKUP]: savePath is null in HandleElapsed");
@ -531,17 +585,26 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
iram.ArchiveRegion(savePath, guid, null); iram.ArchiveRegion(savePath, guid, null);
} }
/// <summary>
/// Called by the Event Manager when the OnOarFileSaved event is fired.
/// </summary>
/// <param name="guid"></param>
/// <param name="message"></param>
void EventManager_OnOarFileSaved(Guid guid, string message) void EventManager_OnOarFileSaved(Guid guid, string message)
{
// Ignore if the OAR save is being done by some other part of the system
if (m_pendingSaves.ContainsKey(guid))
{ {
AutoBackupModuleState abms = m_states[(m_pendingSaves[guid])]; AutoBackupModuleState abms = m_states[(m_pendingSaves[guid])];
ExecuteScript(abms.Script, abms.LiveRequests[guid]); ExecuteScript(abms.Script, abms.LiveRequests[guid]);
m_pendingSaves.Remove(guid); m_pendingSaves.Remove(guid);
abms.LiveRequests.Remove(guid); abms.LiveRequests.Remove(guid);
} }
}
/// This format may turn out to be too unwieldy to keep... /// <summary>This format may turn out to be too unwieldy to keep...
/// Besides, that's what ctimes are for. But then how do I name each file uniquely without using a GUID? /// Besides, that's what ctimes are for. But then how do I name each file uniquely without using a GUID?
/// Sequential numbers, right? Ugh. Almost makes TOO much sense. /// Sequential numbers, right? We support those, too!</summary>
private static string GetTimeString() private static string GetTimeString()
{ {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
@ -565,9 +628,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
return output; return output;
} }
/// /// <summary>Return value of true ==> not too busy; false ==> too busy to backup an OAR right now, or error.</summary>
/// Return value of true ==> not too busy; false ==> too busy to backup an OAR right now, or error.
///
private bool RunHeuristics(IScene region) private bool RunHeuristics(IScene region)
{ {
try try
@ -581,12 +642,13 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
} }
} }
/// /// <summary>
/// If the time dilation right at this instant is less than the threshold specified in AutoBackupDilationThreshold (default 0.5), /// If the time dilation right at this instant is less than the threshold specified in AutoBackupDilationThreshold (default 0.5),
/// then we return false and trip the busy heuristic's "too busy" path (i.e. don't save an OAR). /// then we return false and trip the busy heuristic's "too busy" path (i.e. don't save an OAR).
/// AutoBackupDilationThreshold is a _LOWER BOUND_. Lower Time Dilation is bad, so if you go lower than our threshold, it's "too busy". /// AutoBackupDilationThreshold is a _LOWER BOUND_. Lower Time Dilation is bad, so if you go lower than our threshold, it's "too busy".
/// Return value of "true" ==> not too busy. Return value of "false" ==> too busy! /// </summary>
/// /// <param name="region"></param>
/// <returns>Returns true if we're not too busy; false means we've got worse time dilation than the threshold.</returns>
private bool RunTimeDilationHeuristic(IScene region) private bool RunTimeDilationHeuristic(IScene region)
{ {
string regionName = region.RegionInfo.RegionName; string regionName = region.RegionInfo.RegionName;
@ -595,19 +657,20 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
regionName + ".AutoBackupDilationThreshold", 0.5f); regionName + ".AutoBackupDilationThreshold", 0.5f);
} }
/// /// <summary>
/// If the root agent count right at this instant is less than the threshold specified in AutoBackupAgentThreshold (default 10), /// If the root agent count right at this instant is less than the threshold specified in AutoBackupAgentThreshold (default 10),
/// then we return false and trip the busy heuristic's "too busy" path (i.e., don't save an OAR). /// then we return false and trip the busy heuristic's "too busy" path (i.e., don't save an OAR).
/// AutoBackupAgentThreshold is an _UPPER BOUND_. Higher Agent Count is bad, so if you go higher than our threshold, it's "too busy". /// AutoBackupAgentThreshold is an _UPPER BOUND_. Higher Agent Count is bad, so if you go higher than our threshold, it's "too busy".
/// Return value of "true" ==> not too busy. Return value of "false" ==> too busy! /// </summary>
/// /// <param name="region"></param>
/// <returns>Returns true if we're not too busy; false means we've got more agents on the sim than the threshold.</returns>
private bool RunAgentLimitHeuristic(IScene region) private bool RunAgentLimitHeuristic(IScene region)
{ {
string regionName = region.RegionInfo.RegionName; string regionName = region.RegionInfo.RegionName;
try try
{ {
Scene scene = (Scene) region; Scene scene = (Scene) region;
/// TODO: Why isn't GetRootAgentCount() a method in the IScene interface? Seems generally useful... // TODO: Why isn't GetRootAgentCount() a method in the IScene interface? Seems generally useful...
return scene.GetRootAgentCount() <= return scene.GetRootAgentCount() <=
this.m_configSource.Configs["AutoBackupModule"].GetInt( this.m_configSource.Configs["AutoBackupModule"].GetInt(
regionName + ".AutoBackupAgentThreshold", 10); regionName + ".AutoBackupAgentThreshold", 10);
@ -618,13 +681,20 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
"[AUTO BACKUP]: I NEED MAINTENANCE: IScene is not a Scene; can't get root agent count!", "[AUTO BACKUP]: I NEED MAINTENANCE: IScene is not a Scene; can't get root agent count!",
ice); ice);
return true; return true;
/// Non-obstructionist safest answer... // Non-obstructionist safest answer...
} }
} }
/// <summary>
/// Run the script or executable specified by the "AutoBackupScript" config setting.
/// Of course this is a security risk if you let anyone modify OpenSim.ini and they want to run some nasty bash script.
/// But there are plenty of other nasty things that can be done with an untrusted OpenSim.ini, such as running high threat level scripting functions.
/// </summary>
/// <param name="scriptName"></param>
/// <param name="savePath"></param>
private static void ExecuteScript(string scriptName, string savePath) private static void ExecuteScript(string scriptName, string savePath)
{ {
//Fast path out // Do nothing if there's no script.
if (scriptName == null || scriptName.Length <= 0) if (scriptName == null || scriptName.Length <= 0)
{ {
return; return;
@ -649,12 +719,20 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
} }
} }
/// <summary>
/// Called if a running script process writes to stderr.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void HandleProcErrorDataReceived(object sender, DataReceivedEventArgs e) private static void HandleProcErrorDataReceived(object sender, DataReceivedEventArgs e)
{ {
m_log.Warn("ExecuteScript hook " + ((Process) sender).ProcessName + m_log.Warn("ExecuteScript hook " + ((Process) sender).ProcessName +
" is yacking on stderr: " + e.Data); " is yacking on stderr: " + e.Data);
} }
/// <summary>
/// Quickly stop all timers from firing.
/// </summary>
private void StopAllTimers() private void StopAllTimers()
{ {
foreach (Timer t in this.m_timerMap.Keys) foreach (Timer t in this.m_timerMap.Keys)
@ -664,18 +742,31 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
this.m_closed = true; this.m_closed = true;
} }
/// <summary>
/// Determine the next unique filename by number, for "Sequential" AutoBackupNamingType.
/// </summary>
/// <param name="dirName"></param>
/// <param name="regionName"></param>
/// <returns></returns>
private static string GetNextFile(string dirName, string regionName) private static string GetNextFile(string dirName, string regionName)
{ {
FileInfo uniqueFile = null; FileInfo uniqueFile = null;
long biggestExistingFile = GetNextOarFileNumber(dirName, regionName); long biggestExistingFile = GetNextOarFileNumber(dirName, regionName);
biggestExistingFile++; biggestExistingFile++;
//We don't want to overwrite the biggest existing file; we want to write to the NEXT biggest. // We don't want to overwrite the biggest existing file; we want to write to the NEXT biggest.
uniqueFile = uniqueFile =
new FileInfo(dirName + Path.DirectorySeparatorChar + regionName + "_" + new FileInfo(dirName + Path.DirectorySeparatorChar + regionName + "_" +
biggestExistingFile + ".oar"); biggestExistingFile + ".oar");
return uniqueFile.FullName; return uniqueFile.FullName;
} }
/// <summary>
/// Top-level method for creating an absolute path to an OAR backup file based on what naming scheme the user wants.
/// </summary>
/// <param name="regionName">Name of the region to save.</param>
/// <param name="baseDir">Absolute or relative path to the directory where the file should reside.</param>
/// <param name="naming">The naming scheme for the file name.</param>
/// <returns></returns>
private static string BuildOarPath(string regionName, string baseDir, NamingType naming) private static string BuildOarPath(string regionName, string baseDir, NamingType naming)
{ {
FileInfo path = null; FileInfo path = null;
@ -690,7 +781,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
GetTimeString() + ".oar"); GetTimeString() + ".oar");
return path.FullName; return path.FullName;
case NamingType.Sequential: case NamingType.Sequential:
/// All codepaths in GetNextFile should return a file name ending in .oar // All codepaths in GetNextFile should return a file name ending in .oar
path = new FileInfo(GetNextFile(baseDir, regionName)); path = new FileInfo(GetNextFile(baseDir, regionName));
return path.FullName; return path.FullName;
default: default:
@ -701,6 +792,12 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
return null; return null;
} }
/// <summary>
/// Helper function for Sequential file naming type (see BuildOarPath and GetNextFile).
/// </summary>
/// <param name="dirName"></param>
/// <param name="regionName"></param>
/// <returns></returns>
private static long GetNextOarFileNumber(string dirName, string regionName) private static long GetNextOarFileNumber(string dirName, string regionName)
{ {
long retval = 1; long retval = 1;
@ -717,7 +814,7 @@ namespace OpenSim.Region.OptionalModules.World.AutoBackup
while (!worked && subtract <= fi.LongLength) while (!worked && subtract <= fi.LongLength)
{ {
/// Pick the file with the last natural ordering // Pick the file with the last natural ordering
string biggestFileName = fi[fi.LongLength - subtract].Name; string biggestFileName = fi[fi.LongLength - subtract].Name;
MatchCollection matches = reg.Matches(biggestFileName); MatchCollection matches = reg.Matches(biggestFileName);
long l = 1; long l = 1;

View File

@ -1,30 +1,29 @@
#pragma warning disable 1587 /*
/// * Copyright (c) Contributors, http://opensimulator.org/
/// Copyright (c) Contributors, http://opensimulator.org/ * See CONTRIBUTORS.TXT for a full list of copyright holders.
/// See CONTRIBUTORS.TXT for a full list of copyright holders. *
/// * Redistribution and use in source and binary forms, with or without
/// Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met:
/// modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright
/// * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
/// notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright
/// * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the
/// notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
/// documentation and/or other materials provided with the distribution. * * Neither the name of the OpenSimulator Project nor the
/// * Neither the name of the OpenSimulator Project nor the * names of its contributors may be used to endorse or promote products
/// names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission.
/// derived from this software without specific prior written permission. *
/// * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
/// THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
/// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
/// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
/// DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
/// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
/// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
/// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
/// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
/// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
///
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -32,7 +31,11 @@ using System.Collections.Generic;
namespace OpenSim.Region.OptionalModules.World.AutoBackup namespace OpenSim.Region.OptionalModules.World.AutoBackup
{ {
/// AutoBackupModuleState: Auto-Backup state for one region (scene). /// <summary>AutoBackupModuleState: Auto-Backup state for one region (scene).
/// If you use this class in any way outside of AutoBackupModule, you should treat the class as opaque.
/// Since it is not part of the framework, you really should not rely upon it outside of the AutoBackupModule implementation.
/// </summary>
///
public class AutoBackupModuleState public class AutoBackupModuleState
{ {
private Dictionary<Guid, string> m_liveRequests = null; private Dictionary<Guid, string> m_liveRequests = null;