From 3ac39ac2c10b2c264c5b6c46a61263e1d0d14759 Mon Sep 17 00:00:00 2001 From: Varun Puranik Date: Fri, 14 Sep 2018 14:02:25 -0700 Subject: [PATCH] EdgeHub: Improve startup time (#291) * Parallelize EdgeHub initialization * Change default Operation timeout to 20 secs --- .../CloudConnection.cs | 2 +- .../EdgeHubConnection.cs | 29 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.CloudProxy/CloudConnection.cs b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.CloudProxy/CloudConnection.cs index 595cad4bf10..3f31c4348f5 100644 --- a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.CloudProxy/CloudConnection.cs +++ b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.CloudProxy/CloudConnection.cs @@ -25,7 +25,7 @@ class CloudConnection : ICloudConnection static readonly TimeSpan TokenExpiryBuffer = TimeSpan.FromMinutes(5); // Token is usable if it does not expire in 5 mins const int TokenTimeToLiveSeconds = 3600; // Unused - Token is generated by downstream clients const int TokenExpiryBufferPercentage = 8; // Assuming a standard token for 1 hr, we set expiry time to around 5 mins. - const uint OperationTimeoutMilliseconds = 1 * 60 * 1000; // 1 min + const uint OperationTimeoutMilliseconds = 20 * 1000; // 20 secs static readonly TimeSpan TokenRetryWaitTime = TimeSpan.FromSeconds(20); readonly Action connectionStatusChangedHandler; diff --git a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/EdgeHubConnection.cs b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/EdgeHubConnection.cs index 8c4d858fdaa..503be8f24fd 100644 --- a/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/EdgeHubConnection.cs +++ b/edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Core/EdgeHubConnection.cs @@ -75,27 +75,32 @@ IDeviceScopeIdentitiesCache deviceScopeIdentitiesCache Preconditions.CheckNotNull(deviceScopeIdentitiesCache, nameof(deviceScopeIdentitiesCache)); var edgeHubConnection = new EdgeHubConnection( - edgeHubCredentials.Identity as IModuleIdentity, twinManager, routeFactory, - twinCollectionMessageConverter, twinMessageConverter, + edgeHubCredentials.Identity as IModuleIdentity, + twinManager, + routeFactory, + twinCollectionMessageConverter, + twinMessageConverter, versionInfo ?? VersionInfo.Empty, deviceScopeIdentitiesCache ); - IDeviceProxy deviceProxy = new EdgeHubDeviceProxy(edgeHubConnection); - await connectionManager.AddDeviceConnection(edgeHubCredentials.Identity, deviceProxy); - - await edgeHub.AddSubscription(edgeHubCredentials.Identity.Id, DeviceSubscription.DesiredPropertyUpdates); - await edgeHub.AddSubscription(edgeHubCredentials.Identity.Id, DeviceSubscription.Methods); - - // Clear out all the reported devices. - await edgeHubConnection.ClearDeviceConnectionStatuses(); - + await InitEdgeHub(edgeHubConnection, connectionManager, edgeHubCredentials, edgeHub); connectionManager.DeviceConnected += edgeHubConnection.DeviceConnected; connectionManager.DeviceDisconnected += edgeHubConnection.DeviceDisconnected; Events.Initialized(edgeHubCredentials.Identity); return edgeHubConnection; } + static Task InitEdgeHub(EdgeHubConnection edgeHubConnection, IConnectionManager connectionManager, IClientCredentials edgeHubCredentials, IEdgeHub edgeHub) + { + IDeviceProxy deviceProxy = new EdgeHubDeviceProxy(edgeHubConnection); + Task addDeviceConnectionTask = connectionManager.AddDeviceConnection(edgeHubCredentials.Identity, deviceProxy); + Task desiredPropertyUpdatesSubscriptionTask = edgeHub.AddSubscription(edgeHubCredentials.Identity.Id, DeviceSubscription.DesiredPropertyUpdates); + Task methodsSubscriptionTask = edgeHub.AddSubscription(edgeHubCredentials.Identity.Id, DeviceSubscription.Methods); + Task clearDeviceConnectionStatusesTask = edgeHubConnection.ClearDeviceConnectionStatuses(); + return Task.WhenAll(addDeviceConnectionTask, desiredPropertyUpdatesSubscriptionTask, methodsSubscriptionTask, clearDeviceConnectionStatusesTask); + } + public async Task> GetConfig() { using (await this.edgeHubConfigLock.LockAsync()) @@ -359,7 +364,7 @@ internal async Task HandleMethodInvocation(DirectMethodReq { Events.ErrorRefreshingServiceIdentities(e); return new DirectMethodResponse(e, HttpStatusCode.InternalServerError); - } + } } else {