void Application_Start(object sender, EventArgs e) { // Code that runs on application startup BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterOpenAuth(); if (imageStorePath == null) { ImageStorePath = WebConfigurationManager.AppSettings["ImageStorePath"]; } // initialize storage account configuration setting publisher CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { string connectionString = RoleEnvironment.GetConfigurationSettingValue(configName); configSetter(connectionString); }); try { // initialize the local cache for the Azure drive LocalResource cache = RoleEnvironment.GetLocalResource("LocalDriveCache"); CloudDrive.InitializeCache(cache.RootPath + "cache", cache.MaximumSizeInMegabytes); // retrieve storage account CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); // retrieve URI for the page blob that contains the cloud drive from configuration settings string imageStoreBlobUri = RoleEnvironment.GetConfigurationSettingValue("ImageStoreBlobUri"); // unmount any previously mounted drive. foreach (var drive in CloudDrive.GetMountedDrives()) { var mountedDrive = new CloudDrive(drive.Value, account.Credentials); mountedDrive.Unmount(); } // create the Windows Azure drive and its associated page blob CloudDrive imageStoreDrive = account.CreateCloudDrive(imageStoreBlobUri); if (CloudDrive.GetMountedDrives().Count == 0) { try { imageStoreDrive.Create(16); } catch (CloudDriveException) { // drive already exists } } // mount the drive and initialize the application with the path to the image store on the Azure drive Global.ImageStorePath = imageStoreDrive.Mount(cache.MaximumSizeInMegabytes / 2, DriveMountOptions.None); } catch (CloudDriveException driveException) { Trace.WriteLine("Error: " + driveException.Message); } }
public static string MountDrive(string containerName, string vhdName, int driveSize, int driveLocalReadCacheSize) { var client = GetCloudClientInstance(); // Create the container for the drive if it does not already exist. var container = new CloudBlobContainer(containerName, client); if (container.CreateIfNotExist()) { container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }); } var cloudDrive = new CloudDrive(container.GetPageBlobReference(vhdName).Uri, client.Credentials); try { cloudDrive.Create(driveSize); } catch (CloudDriveException ex) { Logger.Info(string.Format("Cloud drive already exists. Uri: {0}", cloudDrive.Uri), ex); } string pathToDrive = cloudDrive.Mount(driveLocalReadCacheSize, DriveMountOptions.Force); return(pathToDrive); }
private void MountCloudDrive() { LocalResource localCache; try { Log.Info("Mounting CloudDrive..."); localCache = RoleEnvironment.GetLocalResource("RavenCache"); Log.Info("Local cache: RootPath = {0}, Size = {1}", localCache.RootPath, localCache.MaximumSizeInMegabytes); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes); Log.Info("Local cache initialized."); //var ravenDataStorageAccount = CloudStorageAccount.DevelopmentStorageAccount; var ravenDataStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageAccount")); var blobClient = ravenDataStorageAccount.CreateCloudBlobClient(); var ravenDrives = blobClient.GetContainerReference("ravendrives"); ravenDrives.CreateIfNotExist(); var vhdUrl = blobClient.GetContainerReference("ravendrives").GetPageBlobReference("RavenData.vhd").Uri.ToString(); Log.Info("CloudDrive Blob URL: {0}", vhdUrl); BlobContainerPermissions permissions = ravenDrives.GetPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; ravenDrives.SetPermissions(permissions); Log.Info("Permissions set"); _ravenDataDrive = ravenDataStorageAccount.CreateCloudDrive(vhdUrl); } catch (Exception ex) { Log.Error("{0}: {1}", ex.GetType().Name, ex.Message); throw; } try { _ravenDataDrive.Create(localCache.MaximumSizeInMegabytes); Log.Info("CloudDrive instance created"); } catch (CloudDriveException ex) { Log.Error("ravenDataDrive.Create threw exception: " + ex.Message); } _ravenDrivePath = _ravenDataDrive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.Force); Log.Info("Drive mounted as {0}", _ravenDrivePath); if (!Directory.Exists(Path.Combine(_ravenDrivePath, "Raven"))) { Directory.CreateDirectory(Path.Combine(_ravenDrivePath, "Raven")); } }
private void CreateDrive() { try { var vhdUrl = _blobClient.GetContainerReference(_containerName).GetPageBlobReference(_vhdName).Uri.ToString(); _cloudDrive = _ravenDataStorageAccount.CreateCloudDrive(vhdUrl); _cloudDrive.Create(_localCache.MaximumSizeInMegabytes); } catch (CloudDriveException ex) { Trace.TraceWarning(ex.Message); } }
public static string GetMountedPathFromBlob(IStorageConfiguration config, string localCachePath, string cloudDir, string containerName, string blobName, int driveSize, out CloudDrive drive) { Diagnostics.TraceInformation(string.Format("In mounting cloud drive for dir {0} on {1} with {2}", cloudDir, containerName, blobName)); var blobClient = config.Account.CreateCloudBlobClient(); Diagnostics.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) { Diagnostics.Trace("Exception when creating container", e); } var blobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); Diagnostics.TraceInformation(string.Format("Blob uri obtained {0}", blobUri)); // create the cloud drive drive = config.Account.CreateCloudDrive(blobUri); try { drive.Create(driveSize); } catch {} Diagnostics.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 Diagnostics.TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); var driveLetter = drive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); Diagnostics.TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance {1}", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return driveLetter; }
/// <summary> /// /// </summary> /// <param name="csa"></param> /// <param name="vhdUri"></param> /// <param name="azureDriveCacheDirName"></param> /// <param name="azureDriveCacheSizeInMB"></param> /// <param name="azureDriveSizeInMB"></param> /// <param name="driveOptions"></param> /// <returns></returns> public static string MountAzureDrive(CloudStorageAccount csa, Uri vhdUri, string azureDriveCacheDirName, int azureDriveCacheSizeInMB, int azureDriveSizeInMB, DriveMountOptions driveOptions, out bool wasAlreadyMounted) { wasAlreadyMounted = false; // Initialize the cache for mounting the drive. CloudDrive.InitializeCache(azureDriveCacheDirName, azureDriveCacheSizeInMB); CloudDrive drive = null; drive = new CloudDrive(vhdUri, csa.Credentials); Uri driveUri = CloudDrive.GetMountedDrives().Values.Where(tuple => tuple == vhdUri).FirstOrDefault(); // Find out whether the drive has already been mounted by some other instance. if (driveUri == null) { try { // Create the drive. Currently no method is provided to verify whether the // drive is already created or not. drive.Create(azureDriveSizeInMB); } catch (CloudDriveException) { // An exception can be thrown if the drive already exists. Hence ignore the // exception here. If anything is not right, the Mount() will fail. } // Mount the drive. string driveLetter = drive.Mount(azureDriveCacheSizeInMB, driveOptions); return(driveLetter); } else { //Drive is already mounted. So get the drive letter for the drive IDictionary <string, Uri> drives = CloudDrive.GetMountedDrives(); IEnumerator <KeyValuePair <string, Uri> > e = drives.GetEnumerator(); while (e.MoveNext()) { if (e.Current.Value == vhdUri) { wasAlreadyMounted = true; return(e.Current.Key); } } } throw new Exception("Unable to mount the drive." + vhdUri.ToString()); }
private String CreateSolrStorageVhd() { CloudStorageAccount storageAccount; LocalResource localCache; CloudBlobClient client; CloudBlobContainer drives; // get the version of solr we are using _solrVersion = Decimal.Parse(RoleEnvironment.GetConfigurationSettingValue("SolrVersion")); localCache = RoleEnvironment.GetLocalResource("AzureDriveCache"); Log(String.Format("AzureDriveCache {0} {1} MB", localCache.RootPath, localCache.MaximumSizeInMegabytes - 50), "Information"); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes - 50); storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")); client = storageAccount.CreateCloudBlobClient(); string roleId = RoleEnvironment.CurrentRoleInstance.Id; string containerAddress = ContainerNameFromRoleId(roleId); drives = client.GetContainerReference(containerAddress); try { drives.CreateIfNotExist(); } catch { }; var vhdUrl = client.GetContainerReference(containerAddress).GetBlobReference("SolrStorage.vhd").Uri.ToString(); Log(String.Format("SolrStorage.vhd {0}", vhdUrl), "Information"); _solrStorageDrive = storageAccount.CreateCloudDrive(vhdUrl); int cloudDriveSizeInMB = int.Parse(RoleEnvironment.GetConfigurationSettingValue("CloudDriveSize")); try { _solrStorageDrive.Create(cloudDriveSizeInMB); } catch (CloudDriveException) { } Log(String.Format("CloudDriveSize {0} MB", cloudDriveSizeInMB), "Information"); var dataPath = _solrStorageDrive.Mount(localCache.MaximumSizeInMegabytes - 50, DriveMountOptions.Force); Log(String.Format("Mounted as {0}", dataPath), "Information"); return(dataPath); }
private String CreateElasticStorageVhd() { Log("ElasticSearch - creating VHD", "Information"); CloudStorageAccount storageAccount; LocalResource localCache; CloudBlobClient client; CloudBlobContainer drives; localCache = RoleEnvironment.GetLocalResource("ESLocation"); Log(String.Format("ESLocation {0} {1} MB", localCache.RootPath, localCache.MaximumSizeInMegabytes - 50), "Information"); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes - 50); storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")); client = storageAccount.CreateCloudBlobClient(); string roleId = RoleEnvironment.CurrentRoleInstance.Id; string containerAddress = ContainerNameFromRoleId(roleId); drives = client.GetContainerReference(containerAddress); try { drives.CreateIfNotExist(); } catch { }; var vhdUrl = client.GetContainerReference(containerAddress).GetBlobReference("ElasticStorage.vhd").Uri.ToString(); Log(String.Format("ElasticStorage.vhd {0}", vhdUrl), "Information"); _elasticStorageDrive = storageAccount.CreateCloudDrive(vhdUrl); int cloudDriveSizeInMB = int.Parse(RoleEnvironment.GetConfigurationSettingValue("CloudDriveSize")); try { _elasticStorageDrive.Create(cloudDriveSizeInMB); } catch (CloudDriveException) { } Log(String.Format("CloudDriveSize {0} MB", cloudDriveSizeInMB), "Information"); //var dataPath = _elasticStorageDrive.Mount(localCache.MaximumSizeInMegabytes - 50, DriveMountOptions.Force); var dataPath = _elasticStorageDrive.Mount(localCache.MaximumSizeInMegabytes - 50, DriveMountOptions.Force); Log(String.Format("Mounted as {0}", dataPath), "Information"); Log("ElasticSearch - created VHD", "Information"); return dataPath; }
public static string MountDrive(string containerName, string vhdName, int driveSize, int driveLocalReadCacheSize) { var client = GetCloudClientInstance(); // Create the container for the drive if it does not already exist. var container = new CloudBlobContainer(containerName, client); if (container.CreateIfNotExist()) { container.SetPermissions(new BlobContainerPermissions {PublicAccess = BlobContainerPublicAccessType.Off}); } var cloudDrive = new CloudDrive(container.GetPageBlobReference(vhdName).Uri, client.Credentials); try { cloudDrive.Create(driveSize); } catch (CloudDriveException ex) { Logger.Info(string.Format("Cloud drive already exists. Uri: {0}", cloudDrive.Uri), ex); } string pathToDrive = cloudDrive.Mount(driveLocalReadCacheSize, DriveMountOptions.Force); return pathToDrive; }
private static String CreateSolrStorageVhd() { CloudStorageAccount storageAccount; LocalResource localCache; CloudBlobClient client; CloudBlobContainer drives; localCache = RoleEnvironment.GetLocalResource("AzureDriveCache"); Log(String.Format(CultureInfo.InvariantCulture, "AzureDriveCache {0} {1} MB", localCache.RootPath, localCache.MaximumSizeInMegabytes - 50), "Information"); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes - 50); storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")); client = storageAccount.CreateCloudBlobClient(); string roleId = RoleEnvironment.CurrentRoleInstance.Id; string containerAddress = ContainerNameFromRoleId(roleId); drives = client.GetContainerReference(containerAddress); try { drives.CreateIfNotExist(); } catch (StorageClientException) { }; string vhdName = string.Format(CultureInfo.InvariantCulture, "SolrStorage_{0}.vhd", RoleEnvironment.GetConfigurationSettingValue("SolrMajorVersion")); var vhdUrl = client.GetContainerReference(containerAddress).GetBlobReference(vhdName).Uri.ToString(); Log(String.Format(CultureInfo.InvariantCulture, "{0} {1}", vhdName, vhdUrl), "Information"); _solrStorageDrive = storageAccount.CreateCloudDrive(vhdUrl); int cloudDriveSizeInMB = int.Parse(RoleEnvironment.GetConfigurationSettingValue("CloudDriveSize"), CultureInfo.InvariantCulture); try { _solrStorageDrive.Create(cloudDriveSizeInMB); } catch (CloudDriveException) { } Log(String.Format(CultureInfo.InvariantCulture, "CloudDriveSize {0} MB", cloudDriveSizeInMB), "Information"); var dataPath = _solrStorageDrive.Mount(localCache.MaximumSizeInMegabytes - 50, DriveMountOptions.Force); Log(String.Format(CultureInfo.InvariantCulture, "Mounted as {0}", dataPath), "Information"); return dataPath; }
private void MountCloudDrive(string container, string vhdName, int size) { Tracer.WriteLine("Configuring CloudDrive", "Information"); LocalResource localCache = RoleEnvironment.GetLocalResource("MyAzureDriveCache"); const int TRIES = 30; // Temporary workaround for ERROR_UNSUPPORTED_OS seen with Windows Azure Drives // See http://blogs.msdn.com/b/windowsazurestorage/archive/2010/12/17/error-unsupported-os-seen-with-windows-azure-drives.aspx for (int i = 0; i < TRIES; i++) { try { CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes); break; } catch (CloudDriveException ex) { if (!ex.Message.Equals("ERROR_UNSUPPORTED_OS")) { throw; } if (i >= (TRIES - 1)) { // If the workaround fails then it would be dangerous to continue silently, so exit Tracer.WriteLine("Workaround for ERROR_UNSUPPORTED_OS see http://bit.ly/fw7qzo FAILED", "Error"); System.Environment.Exit(-1); } Tracer.WriteLine("Using temporary workaround for ERROR_UNSUPPORTED_OS see http://bit.ly/fw7qzo", "Information"); Thread.Sleep(10000); } } CloudStorageAccount cloudDriveStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(CLOUD_DRIVE_CONNECTION_STRING)); CloudBlobClient blobClient = cloudDriveStorageAccount.CreateCloudBlobClient(); blobClient.GetContainerReference(container).CreateIfNotExist(); CloudPageBlob pageBlob = blobClient .GetContainerReference(container) .GetPageBlobReference(vhdName); cloudDrive = cloudDriveStorageAccount.CreateCloudDrive(pageBlob.Uri.ToString()); try { if (!pageBlob.Exists()) { Tracer.WriteLine(string.Format("Creating page blob {0}", cloudDrive.Uri), "Information"); cloudDrive.Create(size); } Tracer.WriteLine(string.Format("Mounting {0}", cloudDrive.Uri), "Information"); cloudDrive.Mount(25, DriveMountOptions.Force); } catch (CloudDriveException e) { Tracer.WriteLine(e, "Error"); } Tracer.WriteLine(string.Format("CloudDrive {0} mounted at {1}", cloudDrive.Uri, cloudDrive.LocalPath), "Information"); }
private string GetMountedPathFromBlob( string localCachePath, string cloudDir, string containerName, string blobName, int driveSize, bool waitOnMount, bool fallback, out CloudDrive mongoDrive) { TraceInformation(string.Format("In mounting cloud drive for dir {0}", cloudDir)); CloudStorageAccount storageAccount = null; if (fallback) { try { storageAccount = CloudStorageAccount.FromConfigurationSetting(cloudDir); } catch { // case for fallback to data dir for log also TraceInformation(string.Format("{0} is not found. using backup", cloudDir)); mongoDrive = mongoDataDrive; return mongoDataDriveLetter; } } else { storageAccount = CloudStorageAccount.FromConfigurationSetting(cloudDir); } var blobClient = storageAccount.CreateCloudBlobClient(); 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) { TraceInformation("Exception when creating container"); TraceInformation(e.Message); TraceInformation(e.StackTrace); } var mongoBlobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); 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 TraceInformation("Exception when creating cloud drive. safe to ignore"); TraceInformation(e.Message); TraceInformation(e.StackTrace); } 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 if (!waitOnMount) { try { TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); var driveLetter = mongoDrive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return driveLetter; } catch (Exception e) { TraceWarning("could not acquire blob lock."); TraceWarning(e.Message); TraceWarning(e.StackTrace); throw; } } else { string driveLetter; TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); while (true) { try { driveLetter = mongoDrive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return driveLetter; } catch { } Thread.Sleep(MountSleep); } } }
/// <summary> /// ON START /// </summary> /// <returns></returns> public override bool OnStart() { Trace.TraceInformation("RoutingConfigRole entry point called - OnStart Method"); // Set the maximum number of concurrent connections ServicePointManager.DefaultConnectionLimit = 10 * 12 * Environment.ProcessorCount; //Disable small packet assembling, reducing network lattency ServicePointManager.UseNagleAlgorithm = false; //Enable TCP KeepAlive 2h, 1s ServicePointManager.SetTcpKeepAlive(true, 2 * 3600 * 1000, 1000); Trace.TraceInformation("Instance start - Configuring RoleEnvironment handlers"); #region Setup CloudStorageAccount Configuration Setting Publisher // This code sets up a handler to update CloudStorageAccount instances when their corresponding // configuration settings change in the service configuration file. CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { // Provide the configSetter with the initial value configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); RoleEnvironment.Changed += (sender, arg) => { if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>().Any((change) => (change.ConfigurationSettingName == configName))) { // If the autorestart config changed, do nothing if (configName != "AutoRestart") { // The corresponding configuration setting has changed, propagate the value if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName))) { Trace.TraceInformation("RoleEnvironmentConfigurationSettingChange : Requesting a recycle"); // In this case, the change to the storage account credentials in the // service configuration is significant enough that the role needs to be // recycled in order to use the latest settings. (for example, the // endpoint has changed) RoleEnvironment.RequestRecycle(); } } } }; }); #endregion // For information on handling configuration changes // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. RoleEnvironment.Changing += new EventHandler<RoleEnvironmentChangingEventArgs>(RoleEnvironment_Changing); RoleEnvironment.Stopping += new EventHandler<RoleEnvironmentStoppingEventArgs>(RoleEnvironment_Stopping); RoleEnvironment.StatusCheck += new EventHandler<RoleInstanceStatusCheckEventArgs>(RoleEnvironment_StatusCheck); Trace.TraceInformation("Instance start - Configuring Diagnostic Monitor"); #region Diagnostic Monitor configuration var cfg = DiagnosticMonitor.GetDefaultInitialConfiguration(); cfg.Logs.ScheduledTransferLogLevelFilter = LogLevel.Information; cfg.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1); cfg.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration { CounterSpecifier = @"\Processor(_Total)\% Processor Time", SampleRate = TimeSpan.FromSeconds(10d) }); cfg.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration { CounterSpecifier = @"\Memory\Available Bytes", SampleRate = TimeSpan.FromSeconds(10d) }); cfg.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration { CounterSpecifier = @"\PhysicalDisk(_Total)\% Idle Time", SampleRate = TimeSpan.FromSeconds(10d) }); cfg.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration { CounterSpecifier = @"\PhysicalDisk(_Total)\Avg. Disk Queue Length", SampleRate = TimeSpan.FromSeconds(10d) }); cfg.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(1d); DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", cfg); #endregion #if !DEBUG // Configuration of the firewall // FirewallManagement.ConfigureFirewall(); #else Trace.TraceInformation("DEBUG Mode : Firewall not configured !!"); #endif SetCurrentState(RoutingConfigRoleState.Starting); #region Path for the storage //Drive only for S+C role if (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole == MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingAndConfigRole) { Trace.TraceInformation("Instance start - Mounting cloud drive for MongoC data"); // Get the local cache for the cloud drive LocalResource localCache = RoleEnvironment.GetLocalResource("MongocCache"); // we'll use all the cache space we can (note: InitializeCache doesn't work with trailing slash) CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes); // connect to the storage account storageAccount = CloudStorageAccount.FromConfigurationSetting("MongoDbData"); // client for talking to our blob files CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // the container that our dive is going to live in CloudBlobContainer drives = blobClient.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName")); // create blob container (it has to exist before creating the cloud drive) try { drives.CreateIfNotExist(); } catch { } // get the url to the vhd page blob we'll be using var vhdUrl = drives.GetPageBlobReference(string.Format("mongoc-{0}.vhd", RoleEnvironment.CurrentRoleInstance.Id)).Uri.ToString(); mongoDrive = storageAccount.CreateCloudDrive(vhdUrl); try { mongoDrive.Create(localCache.MaximumSizeInMegabytes); } catch (CloudDriveException) { // exception is thrown if all is well but the drive already exists } dbConfigPath = mongoDrive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.Force) + @"\"; Trace.TraceInformation("Instance start - Cloud drive mounted"); } #endregion switch (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole) { case MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingAndConfigRole: Trace.TraceInformation(string.Format("Instance Start (S+C) - Registering mongoc and mongos instance as 'stopped', MongoConfig IP is {0}, MongoS IP is {1}", MongoDBAzurePlatform.Instance.MyMongoCAddress.ToString(),MongoDBAzurePlatform.Instance.MyMongoSAddress.ToString())); break; case MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingOnlyRole: Trace.TraceInformation(string.Format("Instance Start (S only) - Registering mongoc and mongos instance as 'stopped', MongoS IP is {0}", MongoDBAzurePlatform.Instance.MyMongoSAddress.ToString())); break; default: Trace.TraceError("On Start : Wrong Functionnal Role : " + (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole.ToString())); break; } MongoHelper.RegisterInstanceOrUpdate(MongoHelper.RoutingConfigRoleMongoProcessState.Stopped, "mongoc"); MongoHelper.RegisterInstanceOrUpdate(MongoHelper.RoutingConfigRoleMongoProcessState.Stopped, "mongos"); //Start MongoDB performance counters collection for MongoC process Trace.TraceInformation("Instance start - Starting custom performance counters"); if (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole == MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingAndConfigRole) MongoDB.PerformanceCounters.PerformanceMonitor.Start(MongoDBAzurePlatform.Instance.MyMongoCAddress.Host, MongoDBAzurePlatform.Instance.MyMongoCAddress.Port, int.Parse(RoleEnvironment.GetConfigurationSettingValue("PerfCountersSamplingIntervalInMs"))); Trace.TraceInformation("Instance start - end of instance start"); switch (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole) { case MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingAndConfigRole: SetCurrentState(RoutingConfigRoleState.MongoConfig_NotRunning); break; case MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingOnlyRole: SetCurrentState(RoutingConfigRoleState.MongoShard_NotRunning); break; default: Trace.TraceError("On Start : Wrong Functionnal Role : "+(MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole.ToString())); break; } return base.OnStart(); }
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; } }
private void MountCloudDrive() { LocalResource localCache; try { Log.Info("Mounting CloudDrive..."); localCache = RoleEnvironment.GetLocalResource("RavenCache"); Log.Info("Local cache: RootPath = {0}, Size = {1}", localCache.RootPath, localCache.MaximumSizeInMegabytes); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes); Log.Info("Local cache initialized."); //var ravenDataStorageAccount = CloudStorageAccount.DevelopmentStorageAccount; var ravenDataStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageAccount")); var blobClient = ravenDataStorageAccount.CreateCloudBlobClient(); var ravenDrives = blobClient.GetContainerReference("ravendrives"); ravenDrives.CreateIfNotExist(); var vhdUrl = blobClient.GetContainerReference("ravendrives").GetPageBlobReference("RavenData.vhd").Uri.ToString(); Log.Info("CloudDrive Blob URL: {0}", vhdUrl); BlobContainerPermissions permissions = ravenDrives.GetPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; ravenDrives.SetPermissions(permissions); Log.Info("Permissions set"); _ravenDataDrive = ravenDataStorageAccount.CreateCloudDrive(vhdUrl); } catch (Exception ex) { Log.Error("{0}: {1}", ex.GetType().Name, ex.Message); throw; } try { _ravenDataDrive.Create(localCache.MaximumSizeInMegabytes); Log.Info("CloudDrive instance created"); } catch (CloudDriveException ex) { Log.Error("ravenDataDrive.Create threw exception: " + ex.Message); } _ravenDrivePath = _ravenDataDrive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.Force); Log.Info("Drive mounted as {0}", _ravenDrivePath); if (!Directory.Exists(Path.Combine(_ravenDrivePath, "Raven"))) { Directory.CreateDirectory(Path.Combine(_ravenDrivePath, "Raven")); } }
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; } }
// Mount XDrive public bool MountXDrive() { // Create HTTP storage endpoint, needed by Windows Azure Drive CloudStorageAccount xdriveStorageAccount = WindowsAzureVMManager.GetStorageAccount(false); // Initialize local cache LocalResource localCache = RoleEnvironment.GetLocalResource("XDriveLocalCache"); Char[] backSlash = { '\\' }; String localCachePath = localCache.RootPath.TrimEnd(backSlash); CloudDrive.InitializeCache(localCachePath, localCache.MaximumSizeInMegabytes); // Get Windows Azure Drive container and blob names from service configuration file string xdriveContainerName = RoleEnvironment.GetConfigurationSettingValue("PHPApplicationsBackupContainerName"); string xdriveBlobName = RoleEnvironment.GetConfigurationSettingValue("XDrivePageBlobName"); // Create blob container, if it does not exist CloudBlobClient blobClient = xdriveStorageAccount.CreateCloudBlobClient(); blobClient.GetContainerReference(xdriveContainerName).CreateIfNotExist(); // Get Windows Azure Drive page blob reference xdrivePageBlob = blobClient .GetContainerReference(xdriveContainerName) .GetPageBlobReference(xdriveBlobName); // Get a reference to the requested Windows Azure Drive drive = xdriveStorageAccount.CreateCloudDrive( xdrivePageBlob.Uri.ToString() ); // Create drive try { drive.Create(int.Parse(RoleEnvironment.GetConfigurationSettingValue("XDriveSizeInMB"))); } catch (CloudDriveException) { // exception is also thrown if all is well but the drive already exists, hence ignore exception } try { // This is 1 VM solution only, so always mount drive in write mode. string xdriveLetter = drive.Mount( int.Parse(RoleEnvironment.GetConfigurationSettingValue("XDriveCacheSizeInMB")), DriveMountOptions.None); Trace.TraceInformation("Mounted Windows Azure Drive at uri {0}", drive.Uri); Trace.TraceInformation("Applications are durable to Windows Azure Page Blob."); // Use different mechanism for devfabric and cloud to determine applicationsAndRuntimeResourceFolder if (RoleEnvironment.DeploymentId.StartsWith("deployment")) { // This is Windows Azure SDK 1.3 Specific, so might break in future string csrunEnv = Environment.GetEnvironmentVariable("_CSRUN_STATE_DIRECTORY"); if (string.IsNullOrEmpty(csrunEnv)) { applicationsAndRuntimeResourceFolder = Path.Combine( Environment.GetEnvironmentVariable("LOCALAPPDATA"), @"dftmp\wadd\devstoreaccount1\"); } else { applicationsAndRuntimeResourceFolder = Path.Combine(csrunEnv, @"wadd\devstoreaccount1\"); } // Get Windows Azure Drive container and blob names from service configuration file applicationsAndRuntimeResourceFolder = Path.Combine(applicationsAndRuntimeResourceFolder, xdriveContainerName); applicationsAndRuntimeResourceFolder = Path.Combine(applicationsAndRuntimeResourceFolder, xdriveBlobName); } else { applicationsAndRuntimeResourceFolder = xdriveLetter; } return true; } catch (Exception ex) { applicationsAndRuntimeResourceFolder = null; Trace.TraceError("Unable to Mount Windows Azure Drive. Error: {0}, StackTrace: {1}", ex.Message, ex.StackTrace); return false; } }