private void RoleEnvironmentChanged(object sender, RoleEnvironmentChangedEventArgs e) { // Get the list of configuration changes var settingChanges = e.Changes.OfType <RoleEnvironmentConfigurationSettingChange>(); foreach (var settingChange in settingChanges) { var settingName = settingChange.ConfigurationSettingName; var value = RoleEnvironment.GetConfigurationSettingValue(settingName); DiagnosticsHelper.TraceInformation( string.Format("Setting {0} now has value {1} ", settingName, value)); } // Get the list of topology changes var topologyChanges = e.Changes.OfType <RoleEnvironmentTopologyChange>(); foreach (var topologyChange in topologyChanges) { var roleName = topologyChange.RoleName; DiagnosticsHelper.TraceInformation( string.Format("Role {0} now has {1} instance(s)", roleName, RoleEnvironment.Roles[roleName].Instances.Count)); } }
public override void OnStop() { DiagnosticsHelper.TraceInformation("MongoWorkerRole onstop called"); try { // should we instead call Process.stop? if ((mongodProcess != null) && !(mongodProcess.HasExited)) { DatabaseHelper.StepdownIfNeeded(mongodPort); } } catch (Exception e) { //Ignore exceptions caught on unmount DiagnosticsHelper.TraceWarning("Exception in onstop - stepdown failed"); DiagnosticsHelper.TraceWarning(e.Message); DiagnosticsHelper.TraceWarning(e.StackTrace); } try { // should we instead call Process.stop? DiagnosticsHelper.TraceInformation("Shutdown called on mongod"); if ((mongodProcess != null) && !(mongodProcess.HasExited)) { ShutdownMongo(); } DiagnosticsHelper.TraceInformation("Shutdown completed on mongod"); } catch (Exception e) { //Ignore exceptions caught on unmount DiagnosticsHelper.TraceWarning("Exception in onstop - mongo shutdown"); DiagnosticsHelper.TraceWarning(e.Message); DiagnosticsHelper.TraceWarning(e.StackTrace); } try { DiagnosticsHelper.TraceInformation("Unmount called on data drive"); if (mongoDataDrive != null) { mongoDataDrive.Unmount(); } DiagnosticsHelper.TraceInformation("Unmount completed on data drive"); } catch (Exception e) { //Ignore exceptions caught on unmount DiagnosticsHelper.TraceWarning("Exception in onstop - unmount of data drive"); DiagnosticsHelper.TraceWarning(e.Message); DiagnosticsHelper.TraceWarning(e.StackTrace); } DiagnosticsHelper.TraceInformation("Calling diagnostics shutdown"); // DiagnosticsHelper.ShutdownDiagnostics(); base.OnStop(); }
private string GetLogFile() { DiagnosticsHelper.TraceInformation("Getting log file base path"); var localStorage = RoleEnvironment.GetLocalResource(Settings.LogDirSetting); var logfile = Path.Combine(localStorage.RootPath + @"\", Settings.MongodLogFileName); return("\"" + logfile + "\""); }
public override bool OnStart() { DiagnosticsHelper.TraceInformation("MongoWorkerRole onstart called"); // For information on handling configuration changes // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. // Set the maximum number of concurrent connections ServicePointManager.DefaultConnectionLimit = 12; CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); RoleEnvironment.Changed += RoleEnvironmentChanged; replicaSetName = RoleEnvironment.GetConfigurationSettingValue(MongoDBAzureHelper.ReplicaSetNameSetting).ToLower(); instanceId = DatabaseHelper.ParseNodeInstanceId(RoleEnvironment.CurrentRoleInstance.Id); DiagnosticsHelper.TraceInformation(string.Format("ReplicaSetName={0}, InstanceId={1}", replicaSetName, instanceId)); SetHostAndPort(); DiagnosticsHelper.TraceInformation(string.Format("Obtained host={0}, port={1}", mongodHost, mongodPort)); StartMongoD(); DiagnosticsHelper.TraceInformation("Mongod process started"); var commandSucceeded = false; while (!commandSucceeded) { try { DatabaseHelper.RunCloudCommandLocally(instanceId, mongodPort); commandSucceeded = true; } catch (Exception e) { DiagnosticsHelper.TraceInformation(e.Message); commandSucceeded = false; Thread.Sleep(1000); } } DiagnosticsHelper.TraceInformation("Cloud command done on OnStart"); if (!DatabaseHelper.IsReplicaSetInitialized(mongodPort)) { DatabaseHelper.RunInitializeCommandLocally(replicaSetName, mongodPort); DiagnosticsHelper.TraceInformation("RSInit issued"); } DiagnosticsHelper.TraceInformation("Done with OnStart"); return(base.OnStart()); }
private void StartMongoD() { var mongoAppRoot = Path.Combine( Environment.GetEnvironmentVariable("RoleRoot") + @"\", Settings.MongoDBBinaryFolder); var mongodPath = Path.Combine(mongoAppRoot, @"mongod.exe"); var blobPath = GetMongoDataDirectory(); var logFile = GetLogFile(); var logLevel = Settings.MongodLogLevel; string cmdline; if (RoleEnvironment.IsEmulated) { cmdline = String.Format(Settings.MongodCommandLineEmulated, mongodPort, blobPath, logFile, replicaSetName, logLevel); } else { cmdline = String.Format(Settings.MongodCommandLineCloud, mongodPort, blobPath, logFile, replicaSetName, logLevel); } DiagnosticsHelper.TraceInformation(string.Format("Launching mongod as {0} {1}", mongodPath, cmdline)); // launch mongo try { mongodProcess = new Process() { StartInfo = new ProcessStartInfo(mongodPath, cmdline) { UseShellExecute = false, WorkingDirectory = mongoAppRoot, CreateNoWindow = false } }; mongodProcess.Start(); } catch (Exception e) { DiagnosticsHelper.TraceError("Can't start Mongo: " + e.Message); throw new ApplicationException("Can't start mongo: " + e.Message); // throwing an exception here causes the VM to recycle } }
public override void Run() { DiagnosticsHelper.TraceInformation("MongoWorkerRole run method called"); var mongodRunning = CheckIfMongodRunning(); while (mongodRunning) { DatabaseHelper.RunCloudCommandLocally(instanceId, mongodPort); Thread.Sleep(15000); mongodRunning = CheckIfMongodRunning(); } Thread.Sleep(60000); DiagnosticsHelper.TraceWarning("MongoWorkerRole run method exiting"); }
private string GetMongoDataDirectory() { DiagnosticsHelper.TraceInformation("Getting db path"); var dataBlobName = string.Format(Settings.MongodDataBlobName, instanceId); var containerName = string.Format(Settings.MongodDataBlobContainerName, replicaSetName); mongodDataDriveLetter = Utilities.GetMountedPathFromBlob( Settings.LocalCacheDirSetting, Settings.DataDirSetting, containerName, dataBlobName, Settings.MaxDBDriveSizeInMB, out mongoDataDrive); DiagnosticsHelper.TraceInformation(string.Format("Obtained data drive as {0}", mongodDataDriveLetter)); var dir = Directory.CreateDirectory(Path.Combine(mongodDataDriveLetter, @"data")); DiagnosticsHelper.TraceInformation(string.Format("Data directory is {0}", dir.FullName)); return(dir.FullName); }
internal static string GetMountedPathFromBlob( string localCachePath, string cloudDir, string containerName, string blobName, int driveSize, out CloudDrive mongoDrive) { DiagnosticsHelper.TraceInformation(string.Format("In mounting cloud drive for dir {0} on {1} with {2}", cloudDir, containerName, blobName)); CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting(cloudDir); var blobClient = storageAccount.CreateCloudBlobClient(); DiagnosticsHelper.TraceInformation("Get container"); // this should be the name of your replset var driveContainer = blobClient.GetContainerReference(containerName); // create blob container (it has to exist before creating the cloud drive) try { driveContainer.CreateIfNotExist(); } catch (Exception e) { DiagnosticsHelper.TraceInformation("Exception when creating container"); DiagnosticsHelper.TraceInformation(e.Message); DiagnosticsHelper.TraceInformation(e.StackTrace); } var mongoBlobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); DiagnosticsHelper.TraceInformation(string.Format("Blob uri obtained {0}", mongoBlobUri)); // create the cloud drive mongoDrive = storageAccount.CreateCloudDrive(mongoBlobUri); try { mongoDrive.Create(driveSize); } catch (Exception e) { // exception is thrown if all is well but the drive already exists DiagnosticsHelper.TraceInformation("Exception when creating cloud drive. safe to ignore"); DiagnosticsHelper.TraceInformation(e.Message); DiagnosticsHelper.TraceInformation(e.StackTrace); } DiagnosticsHelper.TraceInformation("Initialize cache"); var localStorage = RoleEnvironment.GetLocalResource(localCachePath); CloudDrive.InitializeCache(localStorage.RootPath.TrimEnd('\\'), localStorage.MaximumSizeInMegabytes); // mount the drive and get the root path of the drive it's mounted as try { DiagnosticsHelper.TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); var driveLetter = mongoDrive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); DiagnosticsHelper.TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return(driveLetter); } catch (Exception e) { DiagnosticsHelper.TraceWarning("could not acquire blob lock."); DiagnosticsHelper.TraceWarning(e.Message); DiagnosticsHelper.TraceWarning(e.StackTrace); throw; } }