From 4c61cfef58b34c3d45d2108479762795d891db4a Mon Sep 17 00:00:00 2001 From: "Craig Boger (from Dev Box)" Date: Fri, 16 Feb 2024 18:18:20 -0800 Subject: [PATCH] Actually working client regeneration. Garbage collection still not cleaning up old clients. --- src/Common/CosmosClient.ts | 42 ++++++++++++++----------- src/Common/dataAccess/queryDocuments.ts | 10 +++--- src/Common/dataAccess/readDocument.ts | 18 +++++------ 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/Common/CosmosClient.ts b/src/Common/CosmosClient.ts index 982f35e95..e4f857b1d 100644 --- a/src/Common/CosmosClient.ts +++ b/src/Common/CosmosClient.ts @@ -139,30 +139,31 @@ enum SDKSupportedCapabilities { // changeClientEndpoint....... let _client: Cosmos.CosmosClient; +let _currentClientEndpoint: string; export function client(): Cosmos.CosmosClient { console.log(`Called primary client`); const currentUserContextDocumentEndpoint = userContext?.databaseAccount?.properties?.documentEndpoint; console.log(`Current selected endpoint in userContext: ${currentUserContextDocumentEndpoint}`); - let mydatabaseAccountEndpoint = "Ahhhhhhhhh"; - if (_client) { - _client - .getDatabaseAccount() - .then((databaseAccount) => { - console.log( - `Current primary client endpoint contacted: ${JSON.stringify( - databaseAccount.diagnostics.clientSideRequestStatistics.locationEndpointsContacted, - )}`, - ); - mydatabaseAccountEndpoint = - databaseAccount.diagnostics.clientSideRequestStatistics.locationEndpointsContacted[0]; - }) - .catch((error) => { - console.error("Error getting database account:", error); - }); - } + // let mydatabaseAccountEndpoint = "Ahhhhhhhhh"; + // if (_client) { + // _client + // .getDatabaseAccount() + // .then((databaseAccount) => { + // console.log( + // `Current primary client endpoint contacted: ${JSON.stringify( + // databaseAccount.diagnostics.clientSideRequestStatistics.locationEndpointsContacted, + // )}`, + // ); + // mydatabaseAccountEndpoint = + // databaseAccount.diagnostics.clientSideRequestStatistics.locationEndpointsContacted[0]; + // }) + // .catch((error) => { + // console.error("Error getting database account:", error); + // }); + // } - if (_client && currentUserContextDocumentEndpoint === mydatabaseAccountEndpoint) { + if (_client && currentUserContextDocumentEndpoint === _currentClientEndpoint) { return _client; } @@ -182,8 +183,11 @@ export function client(): Cosmos.CosmosClient { _defaultHeaders["x-ms-cosmos-priority-level"] = PriorityLevel.Default; } + const clientEndpoint = endpoint() || "https://cosmos.azure.com"; + _currentClientEndpoint = clientEndpoint; + const options: Cosmos.CosmosClientOptions = { - endpoint: endpoint() || "https://cosmos.azure.com", // CosmosClient gets upset if we pass a bad URL. This should never actually get called + endpoint: clientEndpoint, // CosmosClient gets upset if we pass a bad URL. This should never actually get called key: userContext.masterKey, tokenProvider, userAgentSuffix: "Azure Portal", diff --git a/src/Common/dataAccess/queryDocuments.ts b/src/Common/dataAccess/queryDocuments.ts index bb8eb1241..582fa4c6d 100644 --- a/src/Common/dataAccess/queryDocuments.ts +++ b/src/Common/dataAccess/queryDocuments.ts @@ -1,8 +1,8 @@ import { FeedOptions, ItemDefinition, QueryIterator, Resource } from "@azure/cosmos"; import { LocalStorageUtility, StorageKey } from "../../Shared/StorageUtility"; import { Queries } from "../Constants"; -import { client2 } from "../ReadRegionCosmosClient"; -// import { client } from "../CosmosClient"; +// import { client2 } from "../ReadRegionCosmosClient"; +import { client } from "../CosmosClient"; export const queryDocuments = ( databaseId: string, @@ -11,9 +11,9 @@ export const queryDocuments = ( options: FeedOptions, ): QueryIterator => { options = getCommonQueryOptions(options); - console.log(`${JSON.stringify(client2().getReadEndpoint())}`); - return client2().database(databaseId).container(containerId).items.query(query, options); - // return client().database(databaseId).container(containerId).items.query(query, options); + // console.log(`${JSON.stringify(client2().getReadEndpoint())}`); + // return client2().database(databaseId).container(containerId).items.query(query, options); + return client().database(databaseId).container(containerId).items.query(query, options); }; export const getCommonQueryOptions = (options: FeedOptions): FeedOptions => { diff --git a/src/Common/dataAccess/readDocument.ts b/src/Common/dataAccess/readDocument.ts index 78b20bfe7..12d3f0fcb 100644 --- a/src/Common/dataAccess/readDocument.ts +++ b/src/Common/dataAccess/readDocument.ts @@ -3,10 +3,10 @@ import { CollectionBase } from "../../Contracts/ViewModels"; import DocumentId from "../../Explorer/Tree/DocumentId"; import { logConsoleProgress } from "../../Utils/NotificationConsoleUtils"; import { HttpHeaders } from "../Constants"; -// import { client } from "../CosmosClient"; +import { client } from "../CosmosClient"; import { getEntityName } from "../DocumentUtility"; import { handleError } from "../ErrorHandlingUtils"; -import { client2 } from "../ReadRegionCosmosClient"; +// import { client2 } from "../ReadRegionCosmosClient"; import { getPartitionKeyValue } from "./getPartitionKeyValue"; export const readDocument = async (collection: CollectionBase, documentId: DocumentId): Promise => { @@ -20,18 +20,18 @@ export const readDocument = async (collection: CollectionBase, documentId: Docum [HttpHeaders.partitionKey]: documentId.partitionKeyValue, } : {}; - // const response = await client() - // .database(collection.databaseId) - // .container(collection.id()) - // .item(documentId.id(), getPartitionKeyValue(documentId)) - // .read(options); - - const response = await client2() + const response = await client() .database(collection.databaseId) .container(collection.id()) .item(documentId.id(), getPartitionKeyValue(documentId)) .read(options); + // const response = await client2() + // .database(collection.databaseId) + // .container(collection.id()) + // .item(documentId.id(), getPartitionKeyValue(documentId)) + // .read(options); + return response?.resource; } catch (error) { handleError(error, "ReadDocument", `Failed to read ${entityName} ${documentId.id()}`);