From 038b8792e322dfc34c2c255509dcc1e05ad7e684 Mon Sep 17 00:00:00 2001 From: Cheng Zhang Date: Wed, 10 Jun 2015 18:57:42 +0800 Subject: [PATCH] CP-12624: Upload a CallHome report to CIS on demand 1. Add Upload a CallHome report to CIS on demand logic 2. Add test cases to check the function Signed-off-by: Cheng Zhang --- XenModel/XenAPI-Extensions/Pool.cs | 1 + XenServerHealthCheck/RequestUploadTask.cs | 57 ++++++++++++++++--- .../XenServerHealthCheckService.cs | 18 +++--- XenServerHealthCheckTests/Program.cs | 1 + .../RequestUploadTaskTests.cs | 42 ++++++++++++++ 5 files changed, 101 insertions(+), 18 deletions(-) diff --git a/XenModel/XenAPI-Extensions/Pool.cs b/XenModel/XenAPI-Extensions/Pool.cs index 824798498..712f7e8df 100644 --- a/XenModel/XenAPI-Extensions/Pool.cs +++ b/XenModel/XenAPI-Extensions/Pool.cs @@ -453,6 +453,7 @@ namespace XenAPI public const string UPLOAD_LOCK = "CallHome.UploadLock"; public const string LAST_SUCCESSFUL_UPLOAD = "CallHome.LastSuccessfulUpload"; public const string LAST_FAILED_UPLOAD = "CallHome.LastFailedUpload"; + public const string NEW_UPLOAD_REQUEST = "CallHome.NewUploadRequest"; public CallHomeSettings(CallHomeStatus status, int intervalInDays, DayOfWeek dayOfWeek, int timeOfDay, int retryInterval) { diff --git a/XenServerHealthCheck/RequestUploadTask.cs b/XenServerHealthCheck/RequestUploadTask.cs index 08228e240..22e6d511b 100755 --- a/XenServerHealthCheck/RequestUploadTask.cs +++ b/XenServerHealthCheck/RequestUploadTask.cs @@ -78,10 +78,22 @@ namespace XenServerHealthCheck } private static int SleepForLockConfirm = 10 * 1000; // 10 seconds - public static bool Request(IXenConnection connection, Session _session) + private static bool getLock(IXenConnection connection, Session session) + { + Dictionary config = new Dictionary(); + string newUploadLock = System.Configuration.ConfigurationManager.AppSettings["UUID"]; + newUploadLock += "|" + DateTime.UtcNow.ToString(); + config[CallHomeSettings.UPLOAD_LOCK] = newUploadLock; + Pool.set_gui_config(session, connection.Cache.Pools[0].opaque_ref, config); + System.Threading.Thread.Sleep(SleepForLockConfirm); + config = Pool.get_gui_config(session, connection.Cache.Pools[0].opaque_ref); + return config[CallHomeSettings.UPLOAD_LOCK] == newUploadLock; + } + + public static bool Request(IXenConnection connection, Session session) { bool needRetry = false; - Dictionary config = Pool.get_gui_config(_session, connection.Cache.Pools[0].opaque_ref); + Dictionary config = Pool.get_gui_config(session, connection.Cache.Pools[0].opaque_ref); if (BoolKey(config, CallHomeSettings.STATUS) == false) { log.InfoFormat("Will not report for XenServer {0} that was not Enroll", connection.Hostname); @@ -172,14 +184,41 @@ namespace XenServerHealthCheck } } - string newUploadLock = System.Configuration.ConfigurationManager.AppSettings["UUID"]; - newUploadLock += "|" + currentTime.ToString(); - config[CallHomeSettings.UPLOAD_LOCK] = newUploadLock; - Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config); - System.Threading.Thread.Sleep(SleepForLockConfirm); - config = Pool.get_gui_config(_session, connection.Cache.Pools[0].opaque_ref); + return getLock(connection, session); + } - return config[CallHomeSettings.UPLOAD_LOCK] == newUploadLock; + private static int DemandTimeOutMinutes = 30; + public static bool OnDemandRequest(IXenConnection connection, Session session) + { + Dictionary config = Pool.get_gui_config(session, connection.Cache.Pools[0].opaque_ref); + if (BoolKey(config, CallHomeSettings.STATUS) == false) + { + log.InfoFormat("Will not report on demand for XenServer {0} that was not Enroll", connection.Hostname); + return false; + } + + //Check if there already some service doing the uploading already + if (CanLock(Get(config, CallHomeSettings.UPLOAD_LOCK)) == false) + { + log.InfoFormat("Will not report for XenServer {0} that already locked", connection.Hostname); + return false; + } + + if (config.ContainsKey(CallHomeSettings.NEW_UPLOAD_REQUEST)) + { + DateTime newUploadRequestDueTime = DateTime.Parse(Get(config, CallHomeSettings.NEW_UPLOAD_REQUEST)).AddMinutes(DemandTimeOutMinutes); + if (DateTime.Compare(newUploadRequestDueTime, DateTime.UtcNow) >= 0) + { + return getLock(connection, session); + } + else + { + log.InfoFormat("Will not report on demand for XenServer {0} since the demand due", connection.Hostname); + return false; + } + } + else + return false; } } } diff --git a/XenServerHealthCheck/XenServerHealthCheckService.cs b/XenServerHealthCheck/XenServerHealthCheckService.cs index 814ed2179..d4ad1ab95 100755 --- a/XenServerHealthCheck/XenServerHealthCheckService.cs +++ b/XenServerHealthCheck/XenServerHealthCheckService.cs @@ -98,24 +98,24 @@ namespace XenServerHealthCheck foreach (IXenConnection connection in Connections) { log.InfoFormat("Check server {0} with user {1}", connection.Hostname, connection.Username); - Session _session = new Session(connection.Hostname, 80); - _session.APIVersion = API_Version.LATEST; + Session session = new Session(connection.Hostname, 80); + session.APIVersion = API_Version.LATEST; try { - _session.login_with_password(connection.Username, connection.Password); - connection.LoadCache(_session); - if (RequestUploadTask.Request(connection, _session)) + session.login_with_password(connection.Username, connection.Password); + connection.LoadCache(session); + if (RequestUploadTask.Request(connection, session) || RequestUploadTask.OnDemandRequest(connection, session)) { //Create thread to do log uploading log.InfoFormat("Will upload report for XenServer {0}", connection.Hostname); } - _session.logout(); - _session = null; + session.logout(); + session = null; } catch (Exception exn) { - if (_session != null) - _session.logout(); + if (session != null) + session.logout(); log.Error(exn, exn); } } diff --git a/XenServerHealthCheckTests/Program.cs b/XenServerHealthCheckTests/Program.cs index c77dc000a..42ce7f7ff 100755 --- a/XenServerHealthCheckTests/Program.cs +++ b/XenServerHealthCheckTests/Program.cs @@ -7,6 +7,7 @@ namespace XenServerHealthCheckTests { RequestUploadTaskTests requestUploadTaskTests = new RequestUploadTaskTests(); requestUploadTaskTests.checkUploadLock(); + requestUploadTaskTests.checkDemandLock(); } } } diff --git a/XenServerHealthCheckTests/RequestUploadTaskTests.cs b/XenServerHealthCheckTests/RequestUploadTaskTests.cs index 0551887a0..8438d092c 100755 --- a/XenServerHealthCheckTests/RequestUploadTaskTests.cs +++ b/XenServerHealthCheckTests/RequestUploadTaskTests.cs @@ -127,5 +127,47 @@ namespace XenServerHealthCheckTests catch (Exception) {} } + + public void checkDemandLock() + { + DatabaseManager.CreateNewConnection(dbName); + IXenConnection connection = DatabaseManager.ConnectionFor(dbName); + Session _session = DatabaseManager.ConnectionFor(dbName).Session; + DatabaseManager.ConnectionFor(dbName).LoadCache(_session); + try + { + Dictionary config = cleanStack(); + connection.LoadCache(_session); + //1 Uploading is inprocess by current service, demand will be ignore + config = cleanStack(); + config[CallHomeSettings.UPLOAD_LOCK] = UUID + "|" + DateTime.UtcNow.ToString(); + config[CallHomeSettings.NEW_UPLOAD_REQUEST] = DateTime.UtcNow.ToString(); + Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config); + Assert.IsFalse(RequestUploadTask.OnDemandRequest(connection, _session)); + + //2 Uploading is inprocess by other service, demand will be ignore + config = cleanStack(); + config[CallHomeSettings.UPLOAD_LOCK] = "test2-test2" + "|" + DateTime.UtcNow.ToString(); + config[CallHomeSettings.NEW_UPLOAD_REQUEST] = DateTime.UtcNow.ToString(); + Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config); + Assert.IsFalse(RequestUploadTask.OnDemandRequest(connection, _session)); + + //3 Uploading is not due and demand due, demand will be ignore + config = cleanStack(); + config[CallHomeSettings.UPLOAD_LOCK] = "test2-test2" + "|" + DateTime.UtcNow.Subtract(TimeSpan.FromDays(14)).ToString(); + config[CallHomeSettings.NEW_UPLOAD_REQUEST] = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(31)).ToString(); + Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config); + Assert.IsFalse(RequestUploadTask.OnDemandRequest(connection, _session)); + + //4 Uploading is due and demand not due, lock will be set + config = cleanStack(); + config[CallHomeSettings.UPLOAD_LOCK] = "test2-test2" + "|" + DateTime.UtcNow.Subtract(TimeSpan.FromDays(14)).ToString(); + config[CallHomeSettings.NEW_UPLOAD_REQUEST] = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(28)).ToString(); + Pool.set_gui_config(_session, connection.Cache.Pools[0].opaque_ref, config); + Assert.IsTrue(RequestUploadTask.OnDemandRequest(connection, _session)); + } + catch (Exception) + { } + } } }