Added in singleton clients for each location into the controller and running warmup queries against the db.

This commit is contained in:
Lee Farley 2017-04-19 16:10:41 +12:00
Родитель 1642724bce
Коммит cd5c6d4acb
2 изменённых файлов: 49 добавлений и 24 удалений

Просмотреть файл

@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using LabWeb.Models;
@ -16,32 +15,65 @@ namespace LabWeb.Controllers
public class HomeController : Controller
{
private static readonly string[] _availableRegions = { LocationNames.WestUS, LocationNames.CentralUS, LocationNames.NorthEurope, LocationNames.SoutheastAsia };
private object obj = new object();
private Dictionary<string, DocumentClient> _readonlyClients;
public ActionResult Index()
private static Dictionary<string, DocumentClient> _readonlyClients;
public async Task<ActionResult> Index()
{
// this is initializing the client connections.
await GetReadOnlyClient();
return View(new HomeViewModel(_availableRegions));
}
public DocumentClient GetReadOnlyClient(string locationName)
private async Task<DocumentClient> GetReadOnlyClient(string locationName = null)
{
if (_readonlyClients == null)
{
lock (obj)
{
var dbEndpoint = new Uri(ConfigurationManager.AppSettings["DocumentDBEndpoint"]);
var dbKey = ConfigurationManager.AppSettings["DocumentDBPrimaryReadonlyKey"];
var dbEndpoint = new Uri(ConfigurationManager.AppSettings["DocumentDBEndpoint"]);
var dbKey = ConfigurationManager.AppSettings["DocumentDBPrimaryReadonlyKey"];
_readonlyClients = _availableRegions.ToDictionary(location => location,
location => new DocumentClient(dbEndpoint, dbKey, new ConnectionPolicy() { ConnectionMode = ConnectionMode.Direct, PreferredLocations = { location } }));
var clients = new Dictionary<string, DocumentClient>();
var tasks = new List<Task>();
foreach (var region in _availableRegions)
{
var policy = new ConnectionPolicy()
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp,
PreferredLocations = { region }
};
var client = new DocumentClient(dbEndpoint, dbKey, policy);
tasks.Add(Task.Run(async () =>
{
await client.OpenAsync();
await WarmupConnection(client);
}));
clients.Add(region, client);
}
_readonlyClients = clients;
await Task.WhenAll(tasks);
}
if (locationName == null)
{
return null;
}
return _readonlyClients[locationName];
}
private const string WarmupQueryOne = "SELECT * FROM c";
private const string WarmupQueryTwo = "SELECT COUNT(1) FROM c";
private static readonly FeedOptions _feedOptions = new FeedOptions { MaxItemCount = 10, EnableScanInQuery = true };
private async Task WarmupConnection(IDocumentClient client)
{
var collectionUri = UriFactory.CreateDocumentCollectionUri(ConfigurationManager.AppSettings["DocumentDBName"], ConfigurationManager.AppSettings["DocumentDBCollectionName"]);
var query = client.CreateDocumentQuery(collectionUri, WarmupQueryOne, _feedOptions).AsDocumentQuery();
await query.ExecuteNextAsync();
query = client.CreateDocumentQuery(collectionUri, WarmupQueryTwo, _feedOptions).AsDocumentQuery();
await query.ExecuteNextAsync();
}
public async Task<ActionResult> Query(string query, string locationName)
{
var newModel = new QueryModel
@ -51,15 +83,8 @@ namespace LabWeb.Controllers
};
int numRetries = 0;
var collectionUri = UriFactory.CreateDocumentCollectionUri(ConfigurationManager.AppSettings["DocumentDBName"], ConfigurationManager.AppSettings["DocumentDBCollectionName"]);
IDocumentQuery<dynamic> docQuery = GetReadOnlyClient(LocationNames.SoutheastAsia).CreateDocumentQuery(
collectionUri,
query,
new FeedOptions
{
MaxItemCount = 10,
EnableScanInQuery = true
}
).AsDocumentQuery();
var client = await GetReadOnlyClient(locationName);
IDocumentQuery<dynamic> docQuery = client.CreateDocumentQuery(collectionUri, query, _feedOptions).AsDocumentQuery();
if (docQuery != null)
{

Просмотреть файл

@ -50,7 +50,7 @@
</select>
<label for="region-select" class="pull-right region-select-label">Query region: </label>
<div class="clearfix"></div>
<div id="region-query-repsonse-block" class="pull-right italic-p" style="display: none">response time: <span id="region-query-repsonse-time">100</span> ms</div>
<div id="region-query-repsonse-block" class="pull-right italic-p" style="display: none">response time: <span id="region-query-repsonse-time">0</span> ms</div>
</div>
</div>
</div>