-
Notifications
You must be signed in to change notification settings - Fork 411
fix(data-connect): Include connector in DataConnect cache key #3055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(data-connect): Include connector in DataConnect cache key #3055
Conversation
The `DataConnectService.getDataConnect()` method was caching
DataConnect instances using only `location` and `serviceId` as the
cache key, ignoring the `connector` field.
This caused a bug where calling `getDataConnect()` with different
connector configs but the same location and serviceId would return
the same (first) cached instance instead of creating separate
instances for each connector.
**Problem:**
```typescript
const dc1 = getDataConnect({ location: 'us-west2', serviceId: 'svc', connector: 'public' });
const dc2 = getDataConnect({ location: 'us-west2', serviceId: 'svc', connector: 'user' });
// dc1 === dc2 was true (BUG!)
// dc2.connectorConfig.connector was 'public' instead of 'user'
```
**Solution:**
Include `connector` in the cache key:
```typescript
const id = `${location}-${serviceId}-${connector ?? ''}`;
```
**Impact:**
This bug affects any application using multiple Data Connect connectors
with the same location and serviceId. Operations intended for one
connector would be incorrectly routed to another, causing "operation
not found" errors.
**Tests:**
Added 5 new tests to verify cache key behavior with different
connector configurations.
Summary of ChangesHello @itok01, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical caching issue within the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request correctly fixes a caching bug in DataConnectService.getDataConnect() by including the connector property in the cache key. The accompanying unit tests are well-written and cover the intended fix. My review includes a suggestion to make the cache key generation more robust to prevent potential key collisions, which is a subtle bug in the current implementation. I've also recommended adding a test case to cover this specific scenario.
Address code review feedback from gemini-code-assist: - Use JSON.stringify instead of hyphen separator for cache key to prevent potential collisions when location/serviceId contain hyphens - Add test case for ambiguous key collision scenario
stephenarosaj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a solid PR @itok01! I went ahead and requested some small changes throughout the changed files. Take a look and feel free to reach out with any questions :)
And thanks so much for submitting this!!
src/data-connect/data-connect.ts
Outdated
|
|
||
| getDataConnect(connectorConfig: ConnectorConfig): DataConnect { | ||
| const id = `${connectorConfig.location}-${connectorConfig.serviceId}`; | ||
| const id = JSON.stringify([connectorConfig.location, connectorConfig.serviceId, connectorConfig.connector ?? '']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This successfully makes the cache keys unique per connector. JSON.stringify() is also what we use in Data Connect's Client JS SDK (link), so that's definitely the approach we want.
I feel that instead of using an empty string when the connector field is undefined, we should keep it undefined and just call JSON.stringify(connectorConfig), so that we're aligned with the Client JS SDK. This also makes it so that if a user explicitly sets their connector field to be an empty string, there's no collision with a config that has the field unset.
I also think that in both the Client JS SDK and here, we should be ordering our keys before we stringify. That way, two ConnectorConfig objects defined as { serviceId: "s", location: "l" } and { location: "l", serviceId: "s" } return the same DataConnect instance. Something like:
const orderedConfig = Object.keys(connectorConfig)
.sort()
.reduce((obj, key) => {
obj[key] = connectorConfig[key as keyof ConnectorConfig];
return obj;
}, {} as any);
const id = JSON.stringify(orderedConfig);| }); | ||
| }); | ||
|
|
||
| describe('getDataConnect() caching', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you rename this section just 'getDataConnect()'?
| const dc1 = getDataConnect(config); | ||
| const dc2 = getDataConnect(config); | ||
|
|
||
| expect(dc1).to.equal(dc2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably use .to.deep.equal when checking DataConnect instances
| const dc1 = getDataConnect(config1); | ||
| const dc2 = getDataConnect(config2); | ||
|
|
||
| expect(dc1).to.not.equal(dc2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above - .to.not.deep.equal
| expect(dc1).to.not.equal(dc2); | ||
| }); | ||
|
|
||
| it('should handle connector being undefined vs defined', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you make this more descriptive? something like "should consider configs with connector undefined and defined as different"
| expect(dc1).to.not.equal(dc2); | ||
| }); | ||
|
|
||
| it('should handle connector being undefined vs defined', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add a test here to make sure the difference between empty connector: "" and no defined value for connector is picked up on by the code
| expect(dc2.connectorConfig.connector).to.be.undefined; | ||
| }); | ||
|
|
||
| it('should not have cache collisions with ambiguous keys', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a good test. could you also add one for different ordering of ConnectorConfig keys?
- Sort ConnectorConfig keys before JSON.stringify for key ordering consistency - Keep connector undefined instead of empty string (align with Client JS SDK) - Update tests: use deep.equal, add empty string vs undefined test, add key ordering test
|
Thank you for the thorough review @stephenarosaj! I've addressed all your feedback: Cache key generation:
Test updates:
All 165 Data Connect tests pass. Let me know if there's anything else! |
The
DataConnectService.getDataConnect()method was cachingDataConnectinstances using onlylocationandserviceIdas the cache key, ignoring theconnectorfield fromConnectorConfig.This caused incorrect behavior when calling
getDataConnect()with different connector configs that share the samelocationandserviceId:Fixed by including
connectorin the cache key. Added 5 new unit tests for cache key behavior.Fixes #3054.