static async Task Main(string[] args) { if (args.Length != 1) { Console.WriteLine("SYNTAX: config.json", args); return; } CosmosClient client; Config conf; { var configFileName = args[0]; conf = JsonConvert.DeserializeObject <Config>(File.ReadAllText(configFileName)); string epUrl = string.Format("https://{0}.documents.azure.com", conf.Instance); var ops = new CosmosClientOptions() { ConnectionMode = ConnectionMode.Gateway, }; client = new CosmosClient(epUrl, conf.AuthKey, ops); } var account = await client.ReadAccountAsync(); var container = client.GetContainer(conf.DbID, conf.ContainerID); await FindLoansAsync(container); }
public async Task <bool> ValidateOptions(MigrationOptions options) { _client = new CosmosClient(options.ConnectionString); try { _logger.LogInformation("Getting account settings to check connection"); await _client.ReadAccountAsync(); _logger.LogInformation("Account settings retrieved"); } catch (Exception e) { _logger.LogError(e, "Failed to get account settings, please check connection string"); return(false); } if (!File.Exists(options.FileLocation)) { _logger.LogError("Cannot find JSON File, please check it exists and has been entered correctly"); return(false); } _baseFilePath = options.FileLocation; return(true); }
static async Task Main(string[] args) { Console.WriteLine("Hello Azure Data Fest 2020 Attendees!"); string endpoint = "'your-cosmosdb-host'"; string authKey = "'your-unique-key-from-cosmosdb'"; try { using (CosmosClient client = new CosmosClient(endpoint, authKey)) { await client.ReadAccountAsync(); } Console.WriteLine(); Console.WriteLine("Read account operation succeeded."); Console.WriteLine(); } catch (CosmosException cosex) { Console.WriteLine("Cosmos Exception Message: {0}", cosex.ToString()); } catch (Exception ex) { Exception baseException = ex.GetBaseException(); Console.WriteLine("Base Exception: {0}, Message: {1}", ex.Message, baseException.Message); } finally { Console.WriteLine("End of demo, press any key to exit."); Console.ReadKey(); } }
public async Task AadMockRefreshTest() { int getAadTokenCount = 0; void GetAadTokenCallBack( TokenRequestContext context, CancellationToken token) { getAadTokenCount++; } (string endpoint, string authKey) = TestCommon.GetAccountInfo(); LocalEmulatorTokenCredential simpleEmulatorTokenCredential = new LocalEmulatorTokenCredential( authKey, GetAadTokenCallBack); CosmosClientOptions clientOptions = new CosmosClientOptions() { TokenCredentialBackgroundRefreshInterval = TimeSpan.FromSeconds(1) }; Assert.AreEqual(0, getAadTokenCount); using CosmosClient aadClient = new CosmosClient( endpoint, simpleEmulatorTokenCredential, clientOptions); DocumentClient documentClient = aadClient.ClientContext.DocumentClient; TokenCredentialCache tokenCredentialCache = ((AuthorizationTokenProviderTokenCredential)aadClient.AuthorizationTokenProvider).tokenCredentialCache; Assert.AreEqual(TimeSpan.FromSeconds(1), tokenCredentialCache.BackgroundTokenCredentialRefreshInterval); Assert.AreEqual(1, getAadTokenCount); await aadClient.ReadAccountAsync(); await aadClient.ReadAccountAsync(); await aadClient.ReadAccountAsync(); // Should use cached token Assert.AreEqual(1, getAadTokenCount); // Token should be refreshed after 1 second await Task.Delay(TimeSpan.FromSeconds(1.2)); Assert.AreEqual(2, getAadTokenCount); }
public async Task TestDispose() { CosmosClient cosmosClient = new CosmosClient(ConnectionString); Database database = cosmosClient.GetDatabase("asdf"); Container container = cosmosClient.GetContainer("asdf", "asdf"); TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey("asdf")); batch.ReadItem("Test"); FeedIterator <dynamic> feedIterator1 = container.GetItemQueryIterator <dynamic>(); FeedIterator <dynamic> feedIterator2 = container.GetItemQueryIterator <dynamic>(queryText: "select * from T"); FeedIterator <dynamic> feedIterator3 = database.GetContainerQueryIterator <dynamic>(queryText: "select * from T"); string userAgent = cosmosClient.ClientContext.UserAgent; // Dispose should be idempotent cosmosClient.Dispose(); cosmosClient.Dispose(); List <Func <Task> > validateAsync = new List <Func <Task> >() { () => cosmosClient.ReadAccountAsync(), () => cosmosClient.CreateDatabaseAsync("asdf"), () => database.CreateContainerAsync("asdf", "/pkpathasdf", 200), () => container.ReadItemAsync <dynamic>("asdf", new PartitionKey("test")), () => container.Scripts.ReadStoredProcedureAsync("asdf"), () => container.Scripts.ReadTriggerAsync("asdf"), () => container.Scripts.ReadUserDefinedFunctionAsync("asdf"), () => batch.ExecuteAsync(), () => feedIterator1.ReadNextAsync(), () => feedIterator2.ReadNextAsync(), () => feedIterator3.ReadNextAsync(), }; foreach (Func <Task> asyncFunc in validateAsync) { try { await asyncFunc(); Assert.Fail("Should throw ObjectDisposedException"); } catch (CosmosObjectDisposedException e) { string expectedMessage = $"Cannot access a disposed 'CosmosClient'. Follow best practices and use the CosmosClient as a singleton." + $" CosmosClient was disposed at: {cosmosClient.DisposedDateTimeUtc.Value.ToString("o", CultureInfo.InvariantCulture)}; CosmosClient Endpoint: https://localtestcosmos.documents.azure.com/; Created at: {cosmosClient.ClientConfigurationTraceDatum.ClientCreatedDateTimeUtc.ToString("o", CultureInfo.InvariantCulture)}; UserAgent: {userAgent};"; Assert.IsTrue(e.Message.Contains(expectedMessage)); string diagnostics = e.Diagnostics.ToString(); Assert.IsNotNull(diagnostics); Assert.IsFalse(diagnostics.Contains("NoOp")); Assert.IsTrue(diagnostics.Contains("Client Configuration")); string exceptionString = e.ToString(); Assert.IsTrue(exceptionString.Contains(diagnostics)); Assert.IsTrue(exceptionString.Contains(e.Message)); Assert.IsTrue(exceptionString.Contains(e.StackTrace)); } } }
private static async Task <IEnumerable <AccountRegion> > GetAvailableAzureReadRegionsAsync() { using (var client = new CosmosClient(EndpointUrl, AuthKey)) { var account = await client.ReadAccountAsync(); return(account.ReadableRegions); } }
public async Task <ActionResult <string> > HealthCheck() { try { await _cosmosClient.ReadAccountAsync().ConfigureAwait(false); } catch { return(Ok("PROBLEM! - Persistent Store Connectivity Health Check")); } return(Ok("SUCCESS! - Persistent Store Connectivity Health Check")); }
public async Task <HealthCheckResult> CheckHealthAsync( HealthCheckContext context, CancellationToken cancellationToken = default) { try { using var client = new CosmosClient(_connectionString); await client.ReadAccountAsync(); return(HealthCheckResult.Healthy("Database for Event Store responding.")); } catch (Exception ex) { return(HealthCheckResult.Unhealthy("Error reading from the Event Store.", ex)); } }
public async Task TestDispose() { CosmosClient cosmosClient = new CosmosClient(ConnectionString); Database database = cosmosClient.GetDatabase("asdf"); Container container = cosmosClient.GetContainer("asdf", "asdf"); TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey("asdf")); batch.ReadItem("Test"); // Dispose should be idempotent cosmosClient.Dispose(); cosmosClient.Dispose(); List <Func <Task> > validateAsync = new List <Func <Task> >() { () => cosmosClient.ReadAccountAsync(), () => cosmosClient.CreateDatabaseAsync("asdf"), () => database.CreateContainerAsync("asdf", "/pkpathasdf", 200), () => container.ReadItemAsync <dynamic>("asdf", new PartitionKey("test")), () => container.Scripts.ReadStoredProcedureAsync("asdf"), () => container.Scripts.ReadTriggerAsync("asdf"), () => container.Scripts.ReadUserDefinedFunctionAsync("asdf"), () => batch.ExecuteAsync(), () => container.GetItemQueryIterator <dynamic>(queryText: "select * from T").ReadNextAsync(), () => container.GetItemQueryIterator <dynamic>().ReadNextAsync(), }; foreach (Func <Task> asyncFunc in validateAsync) { try { await asyncFunc(); Assert.Fail("Should throw ObjectDisposedException"); } catch (ObjectDisposedException) { } } }
/// <summary> /// Run samples for Order By queries. /// </summary> /// <returns>a Task object.</returns> private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config) { using (CosmosClient cosmosClient = config.CreateCosmosClient(config.Key)) { Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database); if (config.CleanupOnStart) { await database.DeleteStreamAsync(); } ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient); Container container = containerResponse; int?currentContainerThroughput = await container.ReadThroughputAsync(); if (!currentContainerThroughput.HasValue) { // Container throughput is not configured. It is shared database throughput ThroughputResponse throughputResponse = await database.ReadThroughputAsync(requestOptions : null); throw new InvalidOperationException($"Using database {config.Database} with {throughputResponse.Resource.Throughput} RU/s. " + $"Container {config.Container} must have a configured throughput."); } Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s"); int taskCount = config.GetTaskCount(currentContainerThroughput.Value); Console.WriteLine("Starting Inserts with {0} tasks", taskCount); Console.WriteLine(); string partitionKeyPath = containerResponse.Resource.PartitionKeyPath; int opsPerTask = config.ItemCount / taskCount; // TBD: 2 clients SxS some overhead RunSummary runSummary; using (DocumentClient documentClient = config.CreateDocumentClient(config.Key)) { Func <IBenchmarkOperation> benchmarkOperationFactory = this.GetBenchmarkFactory( config, partitionKeyPath, cosmosClient, documentClient); if (config.DisableCoreSdkLogging) { // Do it after client initialization (HACK) Program.ClearCoreSdkListeners(); } IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory); runSummary = await execution.ExecuteAsync(taskCount, opsPerTask, config.TraceFailures, 0.01); } if (config.CleanupOnFinish) { Console.WriteLine($"Deleting Database {config.Database}"); await database.DeleteStreamAsync(); } runSummary.WorkloadType = config.WorkloadType; runSummary.id = $"{DateTime.UtcNow:yyyy-MM-dd:HH-mm}-{config.CommitId}"; runSummary.Commit = config.CommitId; runSummary.CommitDate = config.CommitDate; runSummary.CommitTime = config.CommitTime; runSummary.Date = DateTime.UtcNow.ToString("yyyy-MM-dd"); runSummary.Time = DateTime.UtcNow.ToString("HH-mm"); runSummary.BranchName = config.BranchName; runSummary.TotalOps = config.ItemCount; runSummary.Concurrency = taskCount; runSummary.Database = config.Database; runSummary.Container = config.Container; runSummary.AccountName = config.EndPoint; runSummary.pk = config.ResultsPartitionKeyValue; runSummary.MaxTcpConnectionsPerEndpoint = config.MaxTcpConnectionsPerEndpoint; runSummary.MaxRequestsPerTcpConnection = config.MaxRequestsPerTcpConnection; string consistencyLevel = config.ConsistencyLevel; if (string.IsNullOrWhiteSpace(consistencyLevel)) { AccountProperties accountProperties = await cosmosClient.ReadAccountAsync(); consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString(); } runSummary.ConsistencyLevel = consistencyLevel; if (config.PublishResults) { runSummary.Diagnostics = CosmosDiagnosticsLogger.GetDiagnostics(); await this.PublishResults( config, runSummary, cosmosClient); } return(runSummary); } }
/// <summary> /// Run samples for Order By queries. /// </summary> /// <returns>a Task object.</returns> private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config, string accountKey) { using (CosmosClient cosmosClient = config.CreateCosmosClient(accountKey)) { if (config.CleanupOnStart) { Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database); await database.DeleteStreamAsync(); } ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient); Container container = containerResponse; int?currentContainerThroughput = await container.ReadThroughputAsync(); Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s"); int taskCount = config.GetTaskCount(currentContainerThroughput.Value); Console.WriteLine("Starting Inserts with {0} tasks", taskCount); Console.WriteLine(); string partitionKeyPath = containerResponse.Resource.PartitionKeyPath; int opsPerTask = config.ItemCount / taskCount; // TBD: 2 clients SxS some overhead RunSummary runSummary; using (DocumentClient documentClient = config.CreateDocumentClient(accountKey)) { Func <IBenchmarkOperatrion> benchmarkOperationFactory = this.GetBenchmarkFactory( config, partitionKeyPath, cosmosClient, documentClient); IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory); runSummary = await execution.ExecuteAsync(taskCount, opsPerTask, config.TraceFailures, 0.01); } if (config.CleanupOnFinish) { Console.WriteLine($"Deleting Database {config.Database}"); Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database); await database.DeleteStreamAsync(); } runSummary.WorkloadType = config.WorkloadType; runSummary.id = $"{DateTime.UtcNow.ToString("yyyy-MM-dd:HH-mm")}-{config.CommitId}"; runSummary.Commit = config.CommitId; runSummary.CommitDate = config.CommitDate; runSummary.CommitTime = config.CommitTime; runSummary.Date = DateTime.UtcNow.ToString("yyyy-MM-dd"); runSummary.Time = DateTime.UtcNow.ToString("HH-mm"); runSummary.BranchName = config.BranchName; runSummary.TotalOps = config.ItemCount; runSummary.Concurrency = taskCount; runSummary.Database = config.Database; runSummary.Container = config.Container; runSummary.AccountName = config.EndPoint; runSummary.pk = config.ResultsPartitionKeyValue; string consistencyLevel = config.ConsistencyLevel; if (string.IsNullOrWhiteSpace(consistencyLevel)) { AccountProperties accountProperties = await cosmosClient.ReadAccountAsync(); consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString(); } runSummary.ConsistencyLevel = consistencyLevel; if (config.PublishResults) { Container resultsContainer = cosmosClient.GetContainer(config.Database, config.ResultsContainer); await resultsContainer.CreateItemAsync(runSummary, new PartitionKey(runSummary.pk)); } return(runSummary); } }
public async Task <bool> IsMultiMaster() { var accountProperties = await _client.ReadAccountAsync(); return(accountProperties.WritableRegions.Count() > 1); }
/// <summary> /// Executing benchmarks for V2/V3 cosmosdb SDK. /// </summary> /// <returns>a Task object.</returns> private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config) { // V3 SDK client initialization using (CosmosClient cosmosClient = config.CreateCosmosClient(config.Key)) { Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database); if (config.CleanupOnStart) { await database.DeleteStreamAsync(); } ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient); Container container = containerResponse; int?currentContainerThroughput = await container.ReadThroughputAsync(); if (!currentContainerThroughput.HasValue) { // Container throughput is not configured. It is shared database throughput ThroughputResponse throughputResponse = await database.ReadThroughputAsync(requestOptions : null); throw new InvalidOperationException($"Using database {config.Database} with {throughputResponse.Resource.Throughput} RU/s. " + $"Container {config.Container} must have a configured throughput."); } Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s"); int taskCount = config.GetTaskCount(currentContainerThroughput.Value); Console.WriteLine("Starting Inserts with {0} tasks", taskCount); Console.WriteLine(); string partitionKeyPath = containerResponse.Resource.PartitionKeyPath; int opsPerTask = config.ItemCount / taskCount; // TBD: 2 clients SxS some overhead RunSummary runSummary; // V2 SDK client initialization using (Microsoft.Azure.Documents.Client.DocumentClient documentClient = config.CreateDocumentClient(config.Key)) { Func <IBenchmarkOperation> benchmarkOperationFactory = this.GetBenchmarkFactory( config, partitionKeyPath, cosmosClient, documentClient); if (config.DisableCoreSdkLogging) { // Do it after client initialization (HACK) Program.ClearCoreSdkListeners(); } IExecutionStrategy execution = IExecutionStrategy.StartNew(benchmarkOperationFactory); runSummary = await execution.ExecuteAsync(config, taskCount, opsPerTask, 0.01); } if (config.CleanupOnFinish) { Console.WriteLine($"Deleting Database {config.Database}"); await database.DeleteStreamAsync(); } string consistencyLevel = config.ConsistencyLevel; if (string.IsNullOrWhiteSpace(consistencyLevel)) { AccountProperties accountProperties = await cosmosClient.ReadAccountAsync(); consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString(); } runSummary.ConsistencyLevel = consistencyLevel; if (config.PublishResults) { runSummary.Diagnostics = CosmosDiagnosticsLogger.GetDiagnostics(); await this.PublishResults( config, runSummary, cosmosClient); } return(runSummary); } }
public async Task EnsureUnauthorized_ThrowsCosmosClientException_ReadAccountAsync() { string authKey = ConfigurationManager.AppSettings["MasterKey"]; string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"]; // Take the key and change some middle character authKey = authKey.Replace("m", "M"); CosmosClient cosmosClient = new CosmosClient(endpoint, authKey); CosmosException exception1 = await Assert.ThrowsExceptionAsync <CosmosException>(() => cosmosClient.ReadAccountAsync()); Assert.AreEqual(HttpStatusCode.Unauthorized, exception1.StatusCode); }