Share via


Gerenciar simultaneidade na Pesquisa de IA do Azure

Ao gerenciar recursos do Azure AI Search, como índices e fontes de dados, é importante atualizar os recursos com segurança, especialmente se os recursos forem acessados simultaneamente por diferentes componentes do seu aplicativo. Quando dois clientes atualizam simultaneamente um recurso sem coordenação, as condições de corrida são possíveis. Para evitar isso, a Pesquisa de IA do Azure usa um modelo de simultaneidade otimista. Não há bloqueios em um recurso. Em vez disso, há um ETag para cada recurso que identifica a versão do recurso para que você possa formular solicitações que evitem substituições acidentais.

Como funciona

A simultaneidade otimista é implementada por meio de verificações de condições de acesso em chamadas de API gravando em índices, indexadores, fontes de dados, conjuntos de habilidades e recursos do synonymMap.

Todos os recursos têm uma marca de entidade (ETag) que fornece informações de versão do objeto. Ao verificar o ETag primeiro, você pode evitar atualizações simultâneas em um fluxo de trabalho típico (obter, modificar localmente, atualizar) garantindo que o ETag do recurso corresponda à sua cópia local.

  • A API REST usa um ETag no cabeçalho da solicitação.

  • O SDK do Azure para .NET define o ETag por meio de um objeto accessCondition, definindo o If-Match | Cabeçalho If-Match-None no recurso. Objetos que usam ETags, como SynonymMap.ETag e SearchIndex.ETag, têm um objeto accessCondition.

Toda vez que você atualiza um recurso, seu ETag muda automaticamente. Quando você implementa o gerenciamento de simultaneidade, tudo o que você está fazendo é colocar uma pré-condição na solicitação de atualização que exige que o recurso remoto tenha o mesmo ETag que a cópia do recurso que você modificou no cliente. Se outro processo alterar o recurso remoto, o ETag não corresponderá à pré-condição e a solicitação falhará com HTTP 412. Se você estiver usando o SDK do .NET, essa falha se manifesta como uma exceção onde o IsAccessConditionFailed() método de extensão retorna true.

Nota

Existe apenas um mecanismo de simultaneidade. Ele é sempre usado, independentemente de qual API ou SDK é usado para atualizações de recursos.

Exemplo

O código a seguir demonstra simultaneidade otimista para uma operação de atualização. Ele falha na segunda atualização porque o ETag do objeto é alterado por uma atualização anterior. Mais especificamente, quando o ETag no cabeçalho da solicitação não corresponde mais ao ETag do objeto, o serviço de pesquisa retorna um código de status de 400 (solicitação incorreta) e a atualização falha.

using Azure;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using System;
using System.Net;
using System.Threading.Tasks;

namespace AzureSearch.SDKHowTo
{
    class Program
    {
        // This sample shows how ETags work by performing conditional updates and deletes
        // on an Azure Search index.
        static void Main(string[] args)
        {
            string serviceName = "PLACEHOLDER FOR YOUR SEARCH SERVICE NAME";
            string apiKey = "PLACEHOLDER FOR YOUR SEARCH SERVICE ADMIN API KEY";

            // Create a SearchIndexClient to send create/delete index commands
            Uri serviceEndpoint = new Uri($"https://{serviceName}.search.windows.net/");
            AzureKeyCredential credential = new AzureKeyCredential(apiKey);
            SearchIndexClient adminClient = new SearchIndexClient(serviceEndpoint, credential);

            // Delete index if it exists
            Console.WriteLine("Check for index and delete if it already exists...\n");
            DeleteTestIndexIfExists(adminClient);

            // Every top-level resource in Azure Search has an associated ETag that keeps track of which version
            // of the resource you're working on. When you first create a resource such as an index, its ETag is
            // empty.
            SearchIndex index = DefineTestIndex();

            Console.WriteLine(
                $"Test searchIndex hasn't been created yet, so its ETag should be blank. ETag: '{index.ETag}'");

            // Once the resource exists in Azure Search, its ETag is populated. Make sure to use the object
            // returned by the SearchIndexClient. Otherwise, you will still have the old object with the
            // blank ETag.
            Console.WriteLine("Creating index...\n");
            index = adminClient.CreateIndex(index);
            Console.WriteLine($"Test index created; Its ETag should be populated. ETag: '{index.ETag}'");


            // ETags prevent concurrent updates to the same resource. If another
            // client tries to update the resource, it will fail as long as all clients are using the right
            // access conditions.
            SearchIndex indexForClientA = index;
            SearchIndex indexForClientB = adminClient.GetIndex("test-idx");

            Console.WriteLine("Simulating concurrent update. To start, clients A and B see the same ETag.");
            Console.WriteLine($"ClientA ETag: '{indexForClientA.ETag}' ClientB ETag: '{indexForClientB.ETag}'");

            // indexForClientA successfully updates the index.
            indexForClientA.Fields.Add(new SearchField("a", SearchFieldDataType.Int32));
            indexForClientA = adminClient.CreateOrUpdateIndex(indexForClientA);

            Console.WriteLine($"Client A updates test-idx by adding a new field. The new ETag for test-idx is: '{indexForClientA.ETag}'");

            // indexForClientB tries to update the index, but fails due to the ETag check.
            try
            {
                indexForClientB.Fields.Add(new SearchField("b", SearchFieldDataType.Boolean));
                adminClient.CreateOrUpdateIndex(indexForClientB);

                Console.WriteLine("Whoops; This shouldn't happen");
                Environment.Exit(1);
            }
            catch (RequestFailedException e) when (e.Status == 400)
            {
                Console.WriteLine("Client B failed to update the index, as expected.");
            }

            // Uncomment the next line to remove test-idx
            //adminClient.DeleteIndex("test-idx");
            Console.WriteLine("Complete.  Press any key to end application...\n");
            Console.ReadKey();
        }


        private static void DeleteTestIndexIfExists(SearchIndexClient adminClient)
        {
            try
            {
                if (adminClient.GetIndex("test-idx") != null)
                {
                    adminClient.DeleteIndex("test-idx");
                }
            }
            catch (RequestFailedException e) when (e.Status == 404)
            {
                //if an exception occurred and status is "Not Found", this is working as expected
                Console.WriteLine("Failed to find index and this is because it's not there.");
            }
        }

        private static SearchIndex DefineTestIndex() =>
            new SearchIndex("test-idx", new[] { new SearchField("id", SearchFieldDataType.String) { IsKey = true } });
    }
}

Padrão de estruturação

Um padrão de design para implementar simultaneidade otimista deve incluir um loop que tente novamente a verificação da condição de acesso, um teste para a condição de acesso e, opcionalmente, recupere um recurso atualizado antes de tentar reaplicar as alterações.

Este trecho de código ilustra a adição de um synonymMap a um índice que já existe.

O snippet obtém o índice "hotels", verifica a versão do objeto em uma operação de atualização, lança uma exceção se a condição falhar e, em seguida, tenta novamente a operação (até três vezes), começando com a recuperação do índice do servidor para obter a versão mais recente.

private static void EnableSynonymsInHotelsIndexSafely(SearchServiceClient serviceClient)
{
    int MaxNumTries = 3;

    for (int i = 0; i < MaxNumTries; ++i)
    {
        try
        {
            Index index = serviceClient.Indexes.Get("hotels");
            index = AddSynonymMapsToFields(index);

            // The IfNotChanged condition ensures that the index is updated only if the ETags match.
            serviceClient.Indexes.CreateOrUpdate(index, accessCondition: AccessCondition.IfNotChanged(index));

            Console.WriteLine("Updated the index successfully.\n");
            break;
        }
        catch (Exception e) when (e.IsAccessConditionFailed())
        {
            Console.WriteLine($"Index update failed : {e.Message}. Attempt({i}/{MaxNumTries}).\n");
        }
    }
}

private static Index AddSynonymMapsToFields(Index index)
{
    index.Fields.First(f => f.Name == "category").SynonymMaps = new[] { "desc-synonymmap" };
    index.Fields.First(f => f.Name == "tags").SynonymMaps = new[] { "desc-synonymmap" };
    return index;
}

Consulte também