using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; using System.Threading; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Diagnostics; using Microsoft.WindowsAzure.ServiceRuntime; using Microsoft.WindowsAzure.StorageClient; using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.Autoscaling; using HeuristicLab.Services.Optimization.Scaler.Hive; namespace HeuristicLab.Services.Optimization.Scaler { public class WorkerRole : RoleEntryPoint { private const string WADConnectionString = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"; private Autoscaler autoscaler; public override void Run() { try { // This is a sample worker implementation. Replace with your logic. Trace.WriteLine("HeuristicLab.Services.Hive.Scaler entry point called", "Information"); var hiveGlobalUsageCounter = PerformanceCounterHelper.HiveGlobalUsage; while (true) { Thread.Sleep(10000); Trace.WriteLine("Updating counters...", "Information"); // Get current hive usage and update performance counter var usageInPercent = Convert.ToInt32(HiveHelper.GetGlobalUsage()); hiveGlobalUsageCounter.RawValue = usageInPercent; Trace.WriteLine(string.Format("Hive global usage: {0}", hiveGlobalUsageCounter.RawValue)); } } catch (Exception ex) { Trace.WriteLine(ex.ToString()); } } private void InitializeConfiguration() { try { DiagnosticMonitorConfiguration config = DiagnosticMonitor.GetDefaultInitialConfiguration(); // enable diagnostics logging (=> WADDiagnosticsInfrastrucutre table) config.DiagnosticInfrastructureLogs.ScheduledTransferLogLevelFilter = LogLevel.Verbose; config.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1d); // enable trace logging (=> WADLogs) config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose; config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1d); // enable performance counter logging (=> WADPerformanceCounters table) config.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(1d); config.PerformanceCounters.BufferQuotaInMB = 128; config.PerformanceCounters.DataSources.Add( new PerformanceCounterConfiguration() { // Add the global usage counter of hive CounterSpecifier = PerformanceCounterHelper.HiveCounterGlobalUsage, SampleRate = TimeSpan.FromSeconds(10d) } ); DiagnosticMonitor.Start(WADConnectionString, config); } catch (Exception ex) { Trace.Write(ex.ToString()); } } public override bool OnStart() { // Set the maximum number of concurrent connections ServicePointManager.DefaultConnectionLimit = 12; // Initializes all required performance counters for OaaS PerformanceCounterHelper.InitializePerformanceCounters(); // Transfer performance counters to windows azure InitializeConfiguration(); // Start auto scaler autoscaler = EnterpriseLibraryContainer.Current.GetInstance(); autoscaler.Start(); // For information on handling configuration changes // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. return base.OnStart(); } } }