Esempio n. 1
0
        private async Task UpdateDocument(FailoverTestClient defaultClient, IEnumerable <FailoverTestClient> replicaClients)
        {
            while (!_exit)
            {
                var response1 = await defaultClient.UpsertItem("ATL", "A", "11", "S123");

                Console.WriteLine($"{DateTime.Now}: Upsert to region: {defaultClient.WriteRegion,-15}; latency: {response1.Item1}");
                foreach (var replicaClient in replicaClients)
                {
                    // read replica region
                    var response2 = await replicaClient.GetItem("ATL", "A", "11", "S123");

                    Console.WriteLine($"{DateTime.Now}: Read from region: {replicaClient.ReadRegion,-15}; latency: {response2.Item1}");
                    var region = replicaClient.ReadRegion;
                    if (response1.Item2.text != response2.Item2.text)
                    {
                        throw new Exception("Replication failed!!!");
                    }
                }
                // read default region (primary)
                var response3 = await defaultClient.GetItem("ATL", "A", "11", "S123");

                Console.WriteLine($"{DateTime.Now}: Read from region: {defaultClient.ReadRegion,-15}; latency: {response3.Item1}");
            }
        }
Esempio n. 2
0
        public static async Task <FailoverTestClient> GetFailoverTestClient(Uri cosmosDbEndpoint, string authorizationKey, string databaseName, string collectionName, int collectionThroughput, IEnumerable <string> regions = null, bool cleanupOnStart = false)
        {
            var client = new FailoverTestClient(cosmosDbEndpoint, authorizationKey, regions);

            client._databaseName   = databaseName;
            client._collectionName = collectionName;

            await client.DocumentClient.GetOrCreateCollection(client._databaseName, client._collectionName, collectionThroughput, "/partitionKey", cleanupOnStart);

            client._databaseUri   = UriFactory.CreateDatabaseUri(client._databaseName);
            client._collectionUri = UriFactory.CreateDocumentCollectionUri(client._databaseName, client._collectionName);

            return(client);
        }
Esempio n. 3
0
        private async Task RunAsync()
        {
            await GetCosmosDbAccount(_config.ResourceGroup, _config.CosmosDbName, _config.Regions);

            Console.WriteLine($"CosmosDbFailover starting...");
            Console.WriteLine($"Cosmos DB: {_config.CosmosDbName}");
            Console.WriteLine($"Regions:");
            foreach (var region in _cosmosDbAccount.Regions)
            {
                Console.WriteLine($"\t{region.LocationName}");
            }
            Console.WriteLine($"Collection: {_config.DatabaseName}.{_config.CollectionName}");
            Console.WriteLine($"RU's: {_config.CollectionThroughput}");


            Console.WriteLine("Hit Enter to start failover");
            Console.WriteLine("Hit Esc to exit test");

            var cosmosDbEndpoint = new Uri(_cosmosDbAccount.EndPoint);

            var replicaClients = new List <FailoverTestClient>();

            var defaultClient = await FailoverTestClient.GetFailoverTestClient(cosmosDbEndpoint, _cosmosDbAccount.MasterKey, _config.DatabaseName, _config.CollectionName, _config.CollectionThroughput);


            // Skip the first region, which is the primary region, to get replica regions
            var replicaRegions = _cosmosDbAccount.Regions.OrderBy(r => r.FailoverPriority).Skip(1);

            // Create DocumentDb API client in each replica region
            foreach (var replicaRegion in replicaRegions)
            {
                var replicaClient = await FailoverTestClient.GetFailoverTestClient(cosmosDbEndpoint, _cosmosDbAccount.MasterKey, _config.DatabaseName, _config.CollectionName, _config.CollectionThroughput, new List <string>() { replicaRegion.LocationName });

                replicaClients.Add(replicaClient);
            }

            try
            {
                // Create DocumentDb API client using default region


                var tasks = new List <Task>();

                var readConsoleTask = ReadConsoleKeys();
                tasks.Add(readConsoleTask);

                var writeTask = UpdateDocument(defaultClient, replicaClients);
                tasks.Add(writeTask);

                var failoverTask = Failover();
                tasks.Add(failoverTask);

                await Task.WhenAll(tasks.ToArray());
            }
            finally
            {
                if (_config.CleanupDatabaseOnFinish)
                {
                    await DocumentDbUtility.DeleteDatabase(defaultClient.DocumentClient, _config.DatabaseName);
                }
                defaultClient.Dispose();
                foreach (var replicaClient in replicaClients)
                {
                    replicaClient.Dispose();
                }
                if (_config.CleanupAcountOnFinish)
                {
                    Console.WriteLine($"Deleting {_cosmosDbAccount.EndPoint}");
                    await _cosmosDbAccount.DeleteAsync();
                }
            }
        }