Beispiel #1
0
        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 + "\"");
        }
Beispiel #2
0
        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());
        }
Beispiel #3
0
        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
            }
        }
Beispiel #4
0
        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");
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
        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;
            }
        }