-
Notifications
You must be signed in to change notification settings - Fork 38
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
Fix IndexHostSingleton
holding onto one instance of ManageIndexesAPI
#224
Fix IndexHostSingleton
holding onto one instance of ManageIndexesAPI
#224
Conversation
indexOperationsApi = indexOperationsBuilder(config); | ||
} | ||
|
||
const indexOperationsApi = indexOperationsBuilder(config); |
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.
I really don't know what I was doing here, seems really silly to me now lol.
@@ -34,19 +29,17 @@ export const IndexHostSingleton = (function () { | |||
} | |||
}; | |||
|
|||
const key = (config, indexName) => `${config.apiKey}-${indexName}`; | |||
const _key = (config: PineconeConfiguration, indexName: string) => |
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.
Minor tweak, underscore just to make it more clear it's internal to the singleton.
const singleton = { | ||
getHostUrl: async (config: PineconeConfiguration, indexName: IndexName) => { | ||
const cacheKey = _key(config, indexName); | ||
if (cacheKey in hostUrls) { | ||
return hostUrls[cacheKey]; | ||
} else { | ||
const hostUrl = await _describeIndex(config, indexName); | ||
this._set(config, indexName, hostUrl); | ||
singleton._set(config, indexName, hostUrl); |
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 primarily a stylistic change, I wanted the context of this
to be more clear. I think this reads a bit better overall but if anyone has any other feedback let me know.
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.
How does this call to singleton._set
work, since singleton doesn't exist yet?
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.
Ok, I see now after talking this over with you. By the time the function that refers to singleton
runs, singleton
has already been initialized. Fancy!
…e errorHandling test which now fails with the appropriate error response
@@ -70,9 +70,9 @@ describe('Error handling', () => { | |||
await p.index('foo-index').query({ topK: 10, id: '1' }); | |||
} catch (e) { | |||
const err = e as PineconeConnectionError; | |||
expect(err.name).toEqual('PineconeAuthorizationError'); | |||
expect(err.name).toEqual('PineconeConnectionError'); |
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 get the correct error here now, which is great.
Problem
When you instantiate multiple instances of
Pinecone
using different API keys, there's a conflict when theIndexHostSingleton
attempts to resolve unknown index host URLs through the control plane by creating an instance ofManageIndexesApi
. InsideIndexHostSingleton
there's a variableindexOperationsApi
which is built once usingPineconeConfiguration
, but then we re-use that same API key for subsequent_describeIndex
calls.This is bad, and leads to issues like this: #220
Solution
Rebuild the
ManageIndexesApi
object each time we need to call_describeIndex
to add an index host to the cache, because thePineconeConfiguration
and API key may have changed. If there are multiple client instances for different keys, they're using the same cache.Bonus
While running integration tests locally, I noticed we were seeing a lot of warnings about open handles from possibly non-awaited async functions:
We were missing a bunch of
awaits
in front ofassertWithRetries
calls, so I cleaned that up as well.Type of Change
Test Plan
Added a unit test to specifically validate the behavior inside
IndexHostSingleton
.To test you can use a simple script locally to validate instantiating multiple clients with multiple API keys. This is what I was working off of to test:
Running the above on current
main
(v2.2.1)Running the same with the fix implemented