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.
This commit is contained in:
Craig Boger (from Dev Box) 2024-02-08 17:28:49 -08:00
parent 8d352e6e7d
commit 18f1b416b2
4 changed files with 63 additions and 41 deletions

View File

@ -88,7 +88,7 @@ export const tokenProvider = async (requestInfo: Cosmos.RequestInfo) => {
export const requestPlugin: Cosmos.Plugin<any> = 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 }];

View File

@ -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 }];

View File

@ -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<ItemDefinition & Resource> => {
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 => {

View File

@ -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<Item> => {
@ -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()}`);