From 18f1b416b2a494348e14c4d6a5c86d0176ef8e06 Mon Sep 17 00:00:00 2001 From: "Craig Boger (from Dev Box)" Date: Thu, 8 Feb 2024 17:28:49 -0800 Subject: [PATCH] Setup so that client regenerates when another endpoint is selected. Also ran tests on queries in multiple tabs. Looks like the operations complete. Need to confirm old client is garbage collected. --- src/Common/CosmosClient.ts | 52 ++++++++++++++++++------- src/Common/ReadRegionCosmosClient.ts | 24 ++++++------ src/Common/dataAccess/queryDocuments.ts | 10 ++--- src/Common/dataAccess/readDocument.ts | 18 ++++----- 4 files changed, 63 insertions(+), 41 deletions(-) diff --git a/src/Common/CosmosClient.ts b/src/Common/CosmosClient.ts index 97184c621..982f35e95 100644 --- a/src/Common/CosmosClient.ts +++ b/src/Common/CosmosClient.ts @@ -88,7 +88,7 @@ export const tokenProvider = async (requestInfo: Cosmos.RequestInfo) => { export const requestPlugin: Cosmos.Plugin = async (requestContext, diagnosticNode, next) => { requestContext.endpoint = new URL(configContext.PROXY_PATH, window.location.href).href; requestContext.headers["x-ms-proxy-target"] = endpoint(); - console.log(`Request context: ${JSON.stringify(requestContext)}`); + // console.log(`Request context: ${JSON.stringify(requestContext)}`); return next(requestContext); }; @@ -142,7 +142,29 @@ let _client: Cosmos.CosmosClient; export function client(): Cosmos.CosmosClient { console.log(`Called primary client`); - if (_client) return _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); + }); + } + + if (_client && currentUserContextDocumentEndpoint === mydatabaseAccountEndpoint) { + return _client; + } let _defaultHeaders: Cosmos.CosmosHeaders = {}; _defaultHeaders["x-ms-cosmos-sdk-supportedcapabilities"] = @@ -176,24 +198,24 @@ export function client(): Cosmos.CosmosClient { }; // Account details from userContext. - console.log(`userContext details: ${JSON.stringify(userContext)}`); - console.log(`userContext.databaseaccount details: ${JSON.stringify(userContext.databaseAccount)}`); + // console.log(`userContext details: ${JSON.stringify(userContext)}`); + // console.log(`userContext.databaseaccount details: ${JSON.stringify(userContext.databaseAccount)}`); console.log( `userContext?.databaseAccount?.properties?.documentEndpoint details: ${JSON.stringify( userContext?.databaseAccount?.properties?.documentEndpoint, )}`, ); - console.log(`userContext?.endpoint details: ${JSON.stringify(userContext?.endpoint)}`); - console.log( - `userContext?.databaseAccount?.properties?.readLocations details: ${JSON.stringify( - userContext?.databaseAccount?.properties?.readLocations, - )}`, - ); - console.log( - `userContext?.databaseAccount?.properties?.writeLocations details: ${JSON.stringify( - userContext?.databaseAccount?.properties?.writeLocations, - )}`, - ); + // console.log(`userContext?.endpoint details: ${JSON.stringify(userContext?.endpoint)}`); + // console.log( + // `userContext?.databaseAccount?.properties?.readLocations details: ${JSON.stringify( + // userContext?.databaseAccount?.properties?.readLocations, + // )}`, + // ); + // console.log( + // `userContext?.databaseAccount?.properties?.writeLocations details: ${JSON.stringify( + // userContext?.databaseAccount?.properties?.writeLocations, + // )}`, + // ); if (configContext.PROXY_PATH !== undefined) { (options as any).plugins = [{ on: "request", plugin: requestPlugin }]; diff --git a/src/Common/ReadRegionCosmosClient.ts b/src/Common/ReadRegionCosmosClient.ts index 235fd6042..d10623731 100644 --- a/src/Common/ReadRegionCosmosClient.ts +++ b/src/Common/ReadRegionCosmosClient.ts @@ -166,24 +166,24 @@ export function client2(): Cosmos.CosmosClient { }; // Account details from userContext. - console.log(`userContext details: ${JSON.stringify(userContext)}`); - console.log(`userContext.databaseaccount details: ${JSON.stringify(userContext.databaseAccount)}`); + // console.log(`userContext details: ${JSON.stringify(userContext)}`); + // console.log(`userContext.databaseaccount details: ${JSON.stringify(userContext.databaseAccount)}`); console.log( `userContext?.databaseAccount?.properties?.documentEndpoint details: ${JSON.stringify( userContext?.databaseAccount?.properties?.documentEndpoint, )}`, ); console.log(`userContext?.endpoint details: ${JSON.stringify(userContext?.endpoint)}`); - console.log( - `userContext?.databaseAccount?.properties?.readLocations details: ${JSON.stringify( - userContext?.databaseAccount?.properties?.readLocations, - )}`, - ); - console.log( - `userContext?.databaseAccount?.properties?.writeLocations details: ${JSON.stringify( - userContext?.databaseAccount?.properties?.writeLocations, - )}`, - ); + // console.log( + // `userContext?.databaseAccount?.properties?.readLocations details: ${JSON.stringify( + // userContext?.databaseAccount?.properties?.readLocations, + // )}`, + // ); + // console.log( + // `userContext?.databaseAccount?.properties?.writeLocations details: ${JSON.stringify( + // userContext?.databaseAccount?.properties?.writeLocations, + // )}`, + // ); if (configContext.PROXY_PATH !== undefined) { (options as any).plugins = [{ on: "request", plugin: requestPlugin2 }]; diff --git a/src/Common/dataAccess/queryDocuments.ts b/src/Common/dataAccess/queryDocuments.ts index bb8eb1241..848c10c77 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(client().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()}`);