Skip to content

Latest commit

 

History

History
1097 lines (710 loc) · 35.9 KB

06-troubleshooting_failed_requests.md

File metadata and controls

1097 lines (710 loc) · 35.9 KB

Troubleshooting and Tuning Azure Cosmos DB Requests

Required Software

Software Download Link
.NET Core 2.1 (or greater) SDK /download.microsoft.com/dotnet-sdk-2.1
Visual Studio Code /code.visualstudio.com/download
Azure Cosmos DB Data Migration Tool /cosmosdb-data-migration-tool

Setup

Before starting any lab in this workshop, you will need to create the various Azure resources necessary to complete the lab. In this exercise, you will create an Azure Cosmos DB account, database and collection and then populate the collection with a collection of JSON documents.

Download Required Files

A JSON file has been provided that will contain a collection 50,000 students. You will use this file later to import documents into your collection.

  1. Download the transactions.json file and save it to your local machine.

Create Azure Cosmos DB Assets

You will now create an Azure Cosmos DB account to use in this lab.

  1. In a new window, sign in to the Azure Portal (http://portal.azure.com).

  2. On the left side of the portal, click the Create a resource link.

    Create a resource

  3. At the top of the New blade, locate the Search the Marketplace field.

    Search the Marketplace

  4. Enter the text Cosmos into the search field and press Enter.

  5. In the Everything search results blade, select the Azure Cosmos DB result.

    Cosmos search results

  6. In the Azure Cosmos DB blade, click the Create button.

    Create Cosmos instance

  7. In the new Azure Cosmos DB blade, perform the following actions:

    1. In the ID field, enter a globally unique value.

    2. In the API list, select the SQL option.

    3. Leave the Subscription field set to its default value.

    4. In the Resource group section, select the Create new option.

    5. In the Resource group section, enter the value LABTRBL into the empty field.

    6. In the Location field, select the West US location.

    7. Click the Create button.

    Create Cosmos instance

  8. Wait for the creation task to complete before moving on with this lab.

Create Azure Cosmos DB Database and Collection

You will now create a database and collection within your Azure Cosmos DB account.

  1. On the left side of the portal, click the Resource groups link.

    Resource groups

  2. In the Resource groups blade, locate and select the LABTRBL Resource Group.

    Lab resource group

  3. In the LABQURY blade, select the Azure Cosmos DB account you recently created.

    Cosmos resource

  4. In the Azure Cosmos DB blade, locate and click the Overview link on the left side of the blade.

    Overview pane

  5. At the top of the Azure Cosmos DB blade, click the Add Collection button.

    Add collection

  6. In the Add Collection popup, perform the following actions:

    1. In the Database id field, enter the value FinancialDatabase.

    2. In the Collection id field, enter the value TransactionCollection.

    3. In the Storage capacity section, select the Unlimited option.

    4. In the Partition key field, enter the value /costCenter.

    5. In the Throughput field, enter the value 1000.

    6. Click the OK button.

    Add collection

  7. Wait for the creation of the new database and collection to finish before moving on with this lab.

Retrieve Account Credentials

The Data Migration Tool and .NET SDKs both require credentials to connect to your Azure Cosmos DB account. You will collect and store these credentials for use throughout the lab.

  1. On the left side of the Azure Cosmos DB blade, locate the Settings section and click the Keys link.

    Keys pane

  2. In the Keys pane, record the values in the URI and PRIMARY KEY fields. You will use these values later in this lab.

    Credentials

Import Lab Data Into Collection

Finally, you will import the JSON documents contained in the students.json file you downloaded earlier in this lab.

  1. On your local machine, open the Azure Cosmos DB Data Migration Tool.

  2. In the Welcome step of the tool, click the Next button to begin the migration wizard.

    Data Migration Tool - Welcome

  3. In the Source Information step of the tool, perform the following actions:

    1. In the Import from list, select the JSON file(s) option.

    2. Click the Add Files button.

    3. In the Windows Explorer dialog that opens, locate and select the transactions.json file you downloaded earlier in this lab. Click the Open button to add the file.

    4. Select the Decompress data checkbox.

    5. Click the Next button.

    Data Migration Tool - Source

  4. In the Target Information step of the tool, perform the following actions:

    1. In the Export to list, select the Azure Cosmos DB - Sequential record import (partitioned collection) option.

    2. In the Connection String field, enter a newly constructed connection string replacing the placeholders with values from your Azure Cosmos DB account recorded earlier in this lab: AccountEndpoint=[uri];AccountKey=[key];Database=[database name];. Make sure you replace the [uri], [key], and [database name] placeholders with the corresponding values from your Azure Cosmos DB account. For example, if your uri is https://labtrbl.documents.azure.com:443/, your key is yFMff7GiFN7AaGPC2WehNcgnvt8kP2EbkLnuVNWYgKoFmQ5dYyZ94cBXd8wBgV0TCKzmIO3z2y8WoRkViL2waA== and your database's name is FinancialDatabase, then your connection string will look like this: AccountEndpoint=https://labtrbl.documents.azure.com:443/;AccountKey=yFMff7GiFN7AaGPC2WehNcgnvt8kP2EbkLnuVNWYgKoFmQ5dYyZ94cBXd8wBgV0TCKzmIO3z2y8WoRkViL2waA==;Database=FinancialDatabase;

    3. Click the Verify button to validate your connection string.

    4. In the Collection field, enter the value TransactionCollection.

    5. In the Partition Key field, enter the value /costCenter.

    6. In the Collection Throughput field, enter the value 1000.

    7. Click the Next button.

    Data Migration Tool - Target

  5. In the Advanced step of the tool, leave the existing options set to their default values and click the Next button.

    Data Migration Tool - Advanced

  6. In the Summary step of the tool, review your options and then click the Import button.

    Data Migration Tool - Summary

  7. Wait for the import process to complete.

    Data Migration Tool - Progress

    You will know that the tool has run successfully once it has transferred 50000 records and the progress bar's animation ends. This step can take two to five minutes.

    Data Migration Tool - Results

  8. Once the import process has completed, close the Azure Cosmos DB Data Migration Tool.

Create a .NET Core Project

**

  1. On your local machine, create a new folder that will be used to contain the content of your .NET Core project.

  2. In the new folder, right-click the folder and select the Open with Code menu option.

    Open with Visual Studio Code

    Alternatively, you can run a command prompt in your current directory and execute the code . command.

  3. In the Visual Studio Code window that appears, right-click the Explorer pane and select the Open in Command Prompt menu option.

    Open in Command Prompt

  4. In the open terminal pane, enter and execute the following command:

    dotnet new console --output .

    This command will create a new .NET Core 2.1 project. The project will be a console project and the project will be created in the current directly since you used the --output . option.

  5. In the terminal pane, enter and execute the following command:

    dotnet add package Microsoft.Azure.DocumentDB.Core --version 1.9.1

    This command will add the Microsoft.Azure.DocumentDB.Core NuGet package as a project dependency.

  6. In the terminal pane, enter and execute the following command:

    dotnet add package Bogus --version 22.0.7

    This command will add the Bogus NuGet package as a project dependency.

  7. In the terminal pane, enter and execute the following command:

    dotnet restore

    This command will restore all packages specified as dependencies in the project.

  8. In the terminal pane, enter and execute the following command:

    dotnet build

    This command will build the project.

  9. Click the 🗙 symbol to close the terminal pane.

  10. Observe the Program.cs and vscodetemple.csproj files created by the .NET Core CLI.

    Project files

  11. Double-click the Program.cs link in the Explorer pane to open the file in the editor.

    Open editor

Create DocumentClient Instance

**

  1. Within the Program.cs editor tab, Add the following using blocks to the top of the editor:

    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Net;
    using System.Threading.Tasks;
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.Documents.Client;
    using Microsoft.Azure.Documents.Linq;
  2. Locate the Program class and replace it with the following class:

    public class Program
    {
        public static void Main(string[] args)
        {         
        }
    
        private static async Task ExecuteLogic(DocumentClient client)
        {
        }
    }
  3. Within the Program class, add the following lines of code to create variables for your connection information:

    private static readonly Uri _endpointUri = new Uri("");
    private static readonly string _primaryKey = "";
  4. For the _endpointUri variable, replace the placeholder value with the URI value from your Azure Cosmos DB account that you recorded earlier in this lab:

    For example, if your uri is https://labtrbl.documents.azure.com:443/, your new variable assignment will look like this: private static readonly Uri _endpointUri = new Uri("https://labtrbl.documents.azure.com:443/");.

  5. For the _primaryKey variable, replace the placeholder value with the PRIMARY KEY value from your Azure Cosmos DB account that you recorded earlier in this lab:

    For example, if your primary key is yFMff7GiFN7AaGPC2WehNcgnvt8kP2EbkLnuVNWYgKoFmQ5dYyZ94cBXd8wBgV0TCKzmIO3z2y8WoRkViL2waA==, your new variable assignment will look like this: private static readonly string _primaryKey = "yFMff7GiFN7AaGPC2WehNcgnvt8kP2EbkLnuVNWYgKoFmQ5dYyZ94cBXd8wBgV0TCKzmIO3z2y8WoRkViL2waA==";.

  6. Locate the Main method:

    public static void Main(string[] args)
    { 
    }
  7. Within the Main method, add the following lines of code to author a using block that creates and disposes a DocumentClient instance:

    using (DocumentClient client = new DocumentClient(endpointUri, primaryKey))
    {
        
    }
  8. Within the using block, add the following line of code to call the static ExecuteLogic method passing in the DocumentClient instance and waiting for the asynchronous execution to complete.

    ExecuteLogic(client).Wait();
  9. Locate the ExecuteLogic method:

    private static async Task ExecuteLogic(DocumentClient client)
    {       
    }
  10. Within the ExecuteLogic method, add the following line of code to asynchronously open a connection:

    await client.OpenAsync();
  11. Your Program class definition should now look like this:

    public class Program
    { 
        private static readonly Uri _endpointUri = new Uri("<your uri>");
        private static readonly string _primaryKey = "<your key>";
    
        public static void Main(string[] args)
        {    
            using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey))
            {
                ExecuteLogic(client).Wait();
            }
        }
    
        private static async Task ExecuteLogic(DocumentClient client)
        {  
            await client.OpenAsync();     
        }
    }

    We will now execute build the application to make sure our code compiles successfully.

  12. Save all of your open editor tabs.

  13. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  14. In the open terminal pane, enter and execute the following command:

    dotnet build

    This command will build the console project.

  15. Click the 🗙 symbol to close the terminal pane.

  16. Close all open editor tabs.

Troubleshooting Requests

Handling 429 Errors

**

  1. In the Visual Studio Code window, right-click the Explorer pane and select the New File menu option.

    New File

  2. Name the new file Transaction.cs. The editor tab will automatically open for the new file.

  3. Paste in the following code for the IInteraction interface:

    public class Transaction
    {
        public double amount { get; set; }
        public bool processed { get; set; }
        public string paidBy { get; set; }
        public string costCenter { get; set; }
    }
  4. Save all of your open editor tabs.

  5. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  6. In the open terminal pane, enter and execute the following command:

    dotnet build

    This command will build the console project.

  7. Click the 🗙 symbol to close the terminal pane.

  8. Close all open editor tabs.

  9. Double-click the Program.cs link in the Explorer pane to open the file in the editor.

  10. Locate the ExecuteLogic method and delete any existing code:

    private static async Task ExecuteLogic(DocumentClient client)
    {       
    }
  11. Add the following code to the method to create an asynchronous connection:

    await client.OpenAsync();
  12. Add the following line of code to create a variable named collectionLink that is a reference (self-link) to an existing collection:

    Uri collectionSelfLink = UriFactory.CreateDocumentCollectionUri("FinancialDatabase", "TransactionCollection");
  13. Observe the code in the ExecuteLogic method.

    For the next few instructions, we will use the Bogus library to create test data. This library allows you to create a collection of objects with fake data set on each object's property. For this lab, our intent is to focus on Azure Cosmos DB instead of this library. With that intent in mind, the next set of instructions will expedite the process of creating test data.

  14. Add the following code to create a collection of Transaction instances:

    var transactions = new Bogus.Faker<Transaction>()
        .RuleFor(t => t.amount, (fake) => Math.Round(fake.Random.Double(5, 500), 2))
        .RuleFor(t => t.processed, (fake) => fake.Random.Bool(0.6f))
        .RuleFor(t => t.paidBy, (fake) => $"{fake.Name.FirstName().ToLower()}.{fake.Name.LastName().ToLower()}")
        .RuleFor(t => t.costCenter, (fake) => fake.Commerce.Department(1).ToLower())
        .GenerateLazy(1000);
  15. Add the following foreach block to iterate over the PurchaseFoodOrBeverage instances:

    foreach(var transaction in transactions)
    {
    }
  16. Within the foreach block, add the following line of code to asynchronously create a document and save the result of the creation task to a variable:

    ResourceResponse<Document> result = await client.CreateDocumentAsync(collectionSelfLink, transaction);

    The CreateDocumentAsync method of the DocumentClient class takes in a self-link for a collection and an object that you would like to serialize into JSON and store as a document within the specified collection.

  17. Still within the foreach block, add the following line of code to write the value of the newly created resource's id property to the console:

    await Console.Out.WriteLineAsync($"Document Created\t{result.Resource.Id}");

    The ResourceResponse type has a property named Resource that can give you access to interesting data about a document such as it's unique id, time-to-live value, self-link, ETag, timestamp, and attachments.

  18. Your ExecuteLogic method should look like this:

    private static async Task ExecuteLogic(DocumentClient client)
    {
        await client.OpenAsync();  
        Uri collectionSelfLink = UriFactory.CreateDocumentCollectionUri("FinancialDatabase", "TransactionCollection");
        var transactions = new Bogus.Faker<Transaction>()
            .RuleFor(t => t.amount, (fake) => Math.Round(fake.Random.Double(5, 500), 2))
            .RuleFor(t => t.processed, (fake) => fake.Random.Bool(0.6f))
            .RuleFor(t => t.paidBy, (fake) => $"{fake.Name.FirstName().ToLower()}.{fake.Name.LastName().ToLower()}")
            .RuleFor(t => t.costCenter, (fake) => fake.Commerce.Department(1).ToLower())
            .GenerateLazy(500);
        foreach(var transaction in transactions)
        {
            ResourceResponse<Document> result = await client.CreateDocumentAsync(collectionSelfLink, transaction);
            await Console.Out.WriteLineAsync($"Document Created\t{result.Resource.Id}");
        }            
    }
  19. Save all of your open editor tabs.

  20. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  21. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  22. Observe the output of the console application.

    You should see a list of document ids associated with new documents that are being created by this tool.

  23. Click the 🗙 symbol to close the terminal pane.

  24. Back in the code editor tab, locate the following lines of code:

    foreach(var transaction in transactions)
    {
        ResourceResponse<Document> result = await client.CreateDocumentAsync(collectionSelfLink, transaction);
        await Console.Out.WriteLineAsync($"Document Created\t{result.Resource.Id}");
    } 

    Replace those lines of code with the following code:

    List<Task<ResourceResponse<Document>>> tasks = new List<Task<ResourceResponse<Document>>>();
    foreach(var transaction in transactions)
    {
        Task<ResourceResponse<Document>> resultTask = client.CreateDocumentAsync(collectionSelfLink, transaction);
        tasks.Add(resultTask);
    }    
    Task.WaitAll(tasks.ToArray());
    foreach(var task in tasks)
    {
        await Console.Out.WriteLineAsync($"Document Created\t{task.Result.Resource.Id}");
    }  

    We are going to attempt to run as many of these creation tasks in parallel as possible. Remember, our collection is configured at 1,000 RU/s.

  25. Your ExecuteLogic method should look like this:

    private static async Task ExecuteLogic(DocumentClient client)
    {
        await client.OpenAsync();  
        Uri collectionSelfLink = UriFactory.CreateDocumentCollectionUri("FinancialDatabase", "TransactionCollection");
        var transactions = new Bogus.Faker<Transaction>()
            .RuleFor(t => t.amount, (fake) => Math.Round(fake.Random.Double(5, 500), 2))
            .RuleFor(t => t.processed, (fake) => fake.Random.Bool(0.6f))
            .RuleFor(t => t.paidBy, (fake) => $"{fake.Name.FirstName().ToLower()}.{fake.Name.LastName().ToLower()}")
            .RuleFor(t => t.costCenter, (fake) => fake.Commerce.Department(1).ToLower())
            .GenerateLazy(500);
        List<Task<ResourceResponse<Document>>> tasks = new List<Task<ResourceResponse<Document>>>();
        foreach(var transaction in transactions)
        {
            Task<ResourceResponse<Document>> resultTask = client.CreateDocumentAsync(collectionSelfLink, transaction);
            tasks.Add(resultTask);
        }    
        Task.WaitAll(tasks.ToArray());
        foreach(var task in tasks)
        {
            await Console.Out.WriteLineAsync($"Document Created\t{task.Result.Resource.Id}");
        }             
    }
  26. Save all of your open editor tabs.

  27. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  28. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  29. Observe the output of the console application.

    This query should execute successfully. We are only creating 500 documents and we most likely will not run into any throughput issues here.

  30. Click the 🗙 symbol to close the terminal pane.

  31. Back in the code editor tab, locate the following line of code:

    .GenerateLazy(500);

    Replace that line of code with the following code:

    .GenerateLazy(5000);

    We are going to try and create 5000 documents in parallel to see if we can hit out throughput limit.

  32. Save all of your open editor tabs.

  33. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  34. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  35. Observe that the application will crash.

    This query will most likely hit our throughput limit. You will see multiple error messages indicating that specific requests have failed.

  36. Click the 🗙 symbol to close the terminal pane.

Use .NET SDK to Attempt Requests

Tuning Requests

Measuing RU Charge

**

  1. Locate the ExecuteLogic method and delete any existing code:

    private static async Task ExecuteLogic(DocumentClient client)
    {       
    }
  2. Add the following code to the method to create an asynchronous connection:

    await client.OpenAsync();
  3. Add the following code to the method to create a self-link to an existing collection:

    Uri collectionSelfLink = UriFactory.CreateDocumentCollectionUri("FinancialDatabase", "TransactionCollection");
  4. Add the following lines of code to configure options for a query:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        PopulateQueryMetrics = true
    };
  5. Add the following line of code that will store a SQL query in a string variable:

    string sql = "SELECT TOP 1000 * FROM c WHERE c.processed = true ORDER BY c.amount DESC";

    This query will perform a cross-partition ORDER BY and only return the top 1000 out of 50000 documents.

  6. Add the following line of code to create a document query instance:

    IDocumentQuery<Document> query = client.CreateDocumentQuery<Document>(collectionSelfLink, sql, options).AsDocumentQuery();
  7. Add the following line of code to get the first "page" of results:

    var result = await query.ExecuteNextAsync();

    We will not enumerate the full result set. We are only interested in the metrics for the first page of results.

  8. Add the following lines of code to print out all of the query metrics to the console:

    foreach(string key in result.QueryMetrics.Keys)
    {
        await Console.Out.WriteLineAsync($"{key}\t{result.QueryMetrics[key]}");
    }
  9. Save all of your open editor tabs.

  10. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  11. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  12. Observe the output of the console application.

    You should see multiple metrics printed out in your console window. Pay close attention to the Total Query Execution Time, Request Charge and Retrieved Document Size metrics.

  13. Click the 🗙 symbol to close the terminal pane.

  14. Back in the code editor tab, locate the following line of code:

    string sql = "SELECT TOP 1000 * FROM c WHERE c.processed = true ORDER BY c.amount DESC";

    Replace that line of code with the following code:

    string sql = "SELECT * FROM c WHERE c.processed = true";

    This new query does not perform a cross-partition ORDER BY.

  15. Save all of your open editor tabs.

  16. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  17. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  18. Observe the output of the console application.

    You should see a reduction in both the Request Charge and Total Query Execution Time values.

  19. Back in the code editor tab, locate the following line of code:

    string sql = "SELECT * FROM c WHERE c.processed = true";

    Replace that line of code with the following code:

    string sql = "SELECT * FROM c";

    This new query does not filter the result set.

  20. Save all of your open editor tabs.

  21. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  22. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  23. Observe the output of the console application.

    Observe the slight differences in the various metric values.

  24. Back in the code editor tab, locate the following line of code:

    string sql = "SELECT * FROM c";

    Replace that line of code with the following code:

    string sql = "SELECT c.id FROM c";

    This new query does not filter the result set.

  25. Save all of your open editor tabs.

  26. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  27. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  28. Observe the output of the console application.

    Observe the slight differences in the various metric values.

Managing SDK Query Options

**

  1. Locate the ExecuteLogic method and delete any existing code:

    private static async Task ExecuteLogic(DocumentClient client)
    {       
    }
  2. Add the following code to the method to create an asynchronous connection:

    await client.OpenAsync();
  3. Add the following code to the method to create a self-link to an existing collection:

    Uri collectionSelfLink = UriFactory.CreateDocumentCollectionUri("FinancialDatabase", "TransactionCollection");
  4. Add the following line of code to create a high-precision timer:

    Stopwatch timer = new Stopwatch();
  5. Add the following lines of code to configure options for a query:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 100,
        MaxDegreeOfParallelism = 0,
        MaxBufferedItemCount = 0
    }; 
  6. Add the following lines of code to write various values to the console window:

    await Console.Out.WriteLineAsync($"MaxItemCount:\t{options.MaxItemCount}");
    await Console.Out.WriteLineAsync($"MaxDegreeOfParallelism:\t{options.MaxDegreeOfParallelism}");
    await Console.Out.WriteLineAsync($"MaxBufferedItemCount:\t{options.MaxBufferedItemCount}");
  7. Add the following line of code that will store a SQL query in a string variable:

    string sql = "SELECT * FROM c WHERE c.processed = true ORDER BY c.amount DESC";

    This query will perform a cross-partition ORDER BY on a filtered result set.

  8. Add the following line of code to start the timer:

    timer.Start();
  9. Add the following line of code to create a document query instance:

    IDocumentQuery<Document> query = client.CreateDocumentQuery<Document>(collectionSelfLink, sql, options).AsDocumentQuery();
  10. Add the following lines of code to enumerate the result set.

    while (query.HasMoreResults)  
    {
        var result = await query.ExecuteNextAsync<Document>();
    }

    Since the results are paged, we will need to call the ExecuteNextAsync method multiple times in a while loop.

  11. Add the following line of code stop the timer:

    timer.Stop();
  12. Add the following line of code to write the timer's results to the console window:

    await Console.Out.WriteLineAsync($"Elapsed Time:\t{timer.Elapsed.TotalSeconds}");
  13. Save all of your open editor tabs.

  14. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  15. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  16. Observe the output of the console application.

    This initial query should take an unexpectedly long amount of time. This will require us to optimize our SDK options.

  17. Back in the code editor tab, locate the following line of code:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 100,
        MaxDegreeOfParallelism = 0,
        MaxBufferedItemCount = 0
    }; 

    Replace that line of code with the following code:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 100,
        MaxDegreeOfParallelism = -1,
        MaxBufferedItemCount = -1
    };   

    Setting the MaxDegreeOfParallelism and MaxBufferedItemCount properties to a value of -1 effectively tells the SDK to manage these settings.

  18. Save all of your open editor tabs.

  19. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  20. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  21. Observe the output of the console application.

    Depending on your system, the elapsed time may be unchanged.

  22. Back in the code editor tab, locate the following line of code:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 100,
        MaxDegreeOfParallelism = -1,
        MaxBufferedItemCount = -1
    };     

    Replace that line of code with the following code:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 500,
        MaxDegreeOfParallelism = -1,
        MaxBufferedItemCount = -1
    };   

    We are increasing the amount of items returned per "page" in an attempt to improve the performance of the query.

  23. Save all of your open editor tabs.

  24. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  25. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  26. Observe the output of the console application.

    You will notice that the query performance improved dramatically. This may be an indicator that our query was bottlenecked by the client computer.

  27. Back in the code editor tab, locate the following line of code:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 500,
        MaxDegreeOfParallelism = -1,
        MaxBufferedItemCount = -1
    };   

    Replace that line of code with the following code:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 1000,
        MaxDegreeOfParallelism = -1,
        MaxBufferedItemCount = -1
    }; 

    For large queries, it is recommended that you increase the page size up to a value of 1000.

  28. Save all of your open editor tabs.

  29. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  30. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  31. Observe the output of the console application.

    By increasing the page size, you have sped up the query even more.

  32. Back in the code editor tab, locate the following line of code:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 1000,
        MaxDegreeOfParallelism = -1,
        MaxBufferedItemCount = -1
    }; 

    Replace that line of code with the following code:

    FeedOptions options = new FeedOptions
    {
        EnableCrossPartitionQuery = true,
        MaxItemCount = 1000,
        MaxDegreeOfParallelism = -1,
        MaxBufferedItemCount = 50000
    };  

    Setting MaxBufferedItemCount to the expected number of results returned (or a higher number) allows the query to receive maximum benefit from pre-fetching.

  33. Save all of your open editor tabs.

  34. In the Visual Studio Code window, right-click the Explorer pane and select the Open in Command Prompt menu option.

  35. In the open terminal pane, enter and execute the following command:

    dotnet run

    This command will build and execute the console project.

  36. Observe the output of the console application.

    This change should have decreased your query time by a small amount.

Lab Cleanup

Open Cloud Shell

  1. At the top of the portal, click the Cloud Shell icon to open a new shell instance.

    If this is your first time using the cloud shell, you may need to configure the default Storage account and SMB file share.

Use Azure CLI to Delete Resource Group

  1. In the Cloud Shell command prompt at the bottom of the portal, type in the following command and press Enter to list all resource groups in the subscription:

    az group list
    
  2. Type in the following command and press Enter to delete the LABTRBL Resource Group:

    az group delete --name LABTRBL --no-wait --yes
    
  3. Close the Cloud Shell prompt at the bottom of the portal.