Skip to content

Commit

Permalink
Bugfix/enable index metrics (Azure#26626)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR
@azure/cosmos

### Issues associated with this PR
This PR tends to enable index metrics for aggregate queries in node/JS
sdk

### Describe the problem that is addressed by this PR


### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_
Yes

### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)

---------

Co-authored-by: Manik Khandelwal <[email protected]>
  • Loading branch information
topshot99 and mkhandelwal-123 authored Sep 7, 2023
1 parent 9093cb7 commit 8c4eff9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ async function run(): Promise<void> {
console.log(`Patched ${JSON.stringify(patchSource)} to new ${JSON.stringify(patchSource3)}.`);
}
}

logStep("Query items with index metrics enabled");
const { resources: resultsIndexMetrics, indexMetrics } = await container.items
.query(querySpec, { populateIndexMetrics: true })
.fetchAll();
console.log("IndexMetrics: ", indexMetrics);
console.log("Query results: ", resultsIndexMetrics);

logStep("Delete item '" + item.id + "'");
await item.delete();

Expand Down
5 changes: 5 additions & 0 deletions sdk/cosmosdb/cosmos/src/queryExecutionContext/headerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ export function mergeHeaders(headers: CosmosHeaders, toBeMergedHeaders: CosmosHe
}
}
}

if (Constants.HttpHeaders.IndexUtilization in toBeMergedHeaders) {
headers[Constants.HttpHeaders.IndexUtilization] =
toBeMergedHeaders[Constants.HttpHeaders.IndexUtilization];
}
}
73 changes: 48 additions & 25 deletions sdk/cosmosdb/cosmos/test/public/integration/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
import assert from "assert";
import { Suite } from "mocha";
import { FeedOptions } from "../../../src";
import { Container, FeedOptions } from "../../../src";
import { getTestContainer, getTestDatabase, removeAllDatabases } from "../common/TestHelpers";

const doc = { id: "myId", pk: "pk" };
Expand Down Expand Up @@ -185,46 +185,69 @@ describe("aggregate query over null value", function (this: Suite) {

describe("Test Index metrics", function (this: Suite) {
this.timeout(process.env.MOCHA_TIMEOUT || 20000);
const collectionId = "testCollection3";

beforeEach(async function () {
await removeAllDatabases();
});

it("validate that index metrics are correct for a single partition query", async function () {
const database = await getTestDatabase("index metrics test db");
it("validate that index metrics are correct", async function () {
const collectionId = "testCollection3";
const createdContainerSinglePartition = await setupContainer(
"index metrics test db",
collectionId,
4000
);
const createdContainerMultiPartition = await setupContainer(
"index metrics test db multipartioned",
collectionId,
12000
);

await validateIndexMetrics(createdContainerSinglePartition, collectionId);
await validateIndexMetrics(createdContainerMultiPartition, collectionId);
});

async function validateIndexMetrics(container: Container, collectionId: string) {
const doc1 = { id: "myId1", pk: "pk1", name: "test1" };
const doc2 = { id: "myId2", pk: "pk2", name: "test2" };
const doc3 = { id: "myId3", pk: "pk2", name: "test2" };
await container.items.create(doc1);
await container.items.create(doc2);
await container.items.create(doc3);
// stremeable query
const query1 = "SELECT * from " + collectionId + " where " + collectionId + ".name = 'test2'";
// aggregate query
const query2 = "SELECT * from " + collectionId + " order by " + collectionId + ".name";
const queryList = [query1, query2];
const queryOptions: FeedOptions = { populateIndexMetrics: true, maxItemCount: 1 };
for (const query of queryList) {
const queryIterator = container.items.query(query, queryOptions);
while (queryIterator.hasMoreResults()) {
const { resources: results, indexMetrics } = await queryIterator.fetchNext();

if (results === undefined) {
break;
}
assert.notEqual(indexMetrics, undefined);
}
}
}
async function setupContainer(datbaseName: string, collectionId: string, throughput?: number) {
const database = await getTestDatabase(datbaseName);

const collectionDefinition = {
id: collectionId,
partitionKey: {
paths: ["/pk"],
},
};
const collectionOptions = { offerThroughput: 4000 };
const collectionOptions = { offerThroughput: throughput };

const { resource: createdCollectionDef } = await database.containers.create(
collectionDefinition,
collectionOptions
);
const createdContainer = database.container(createdCollectionDef.id);

const doc1 = { id: "myId1", pk: "pk1", name: "test1" };
const doc2 = { id: "myId2", pk: "pk2", name: "test2" };
const doc3 = { id: "myId3", pk: "pk2", name: "test2" };
await createdContainer.items.create(doc1);
await createdContainer.items.create(doc2);
await createdContainer.items.create(doc3);
const query = "SELECT * from " + collectionId + " where " + collectionId + ".name = 'test2'";
const queryOptions: FeedOptions = { populateIndexMetrics: true };
const queryIterator = createdContainer.items.query(query, queryOptions);

while (queryIterator.hasMoreResults()) {
const { resources: results, indexMetrics } = await queryIterator.fetchNext();

if (results === undefined) {
break;
}
assert.notEqual(indexMetrics, undefined);
}
});
return createdContainer;
}
});

0 comments on commit 8c4eff9

Please sign in to comment.