Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.

chore: add support for class MultiplexedSession#2191

Closed
alkatrivedi wants to merge 9 commits intogoogleapis:mainfrom
alkatrivedi:class-multiplexed-session
Closed

chore: add support for class MultiplexedSession#2191
alkatrivedi wants to merge 9 commits intogoogleapis:mainfrom
alkatrivedi:class-multiplexed-session

Conversation

@alkatrivedi
Copy link
Copy Markdown
Contributor

This PR contains the code for the newly introduced class MultiplexedSession.

@alkatrivedi alkatrivedi requested review from a team November 13, 2024 15:26
@product-auto-label product-auto-label bot added size: l Pull request size is large. api: spanner Issues related to the googleapis/nodejs-spanner API. labels Nov 13, 2024
@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch 2 times, most recently from 27cf336 to 51df606 Compare November 18, 2024 08:20
@generated-files-bot
Copy link
Copy Markdown

Warning: This pull request is touching the following templated files:

@snippet-bot
Copy link
Copy Markdown

snippet-bot bot commented Nov 18, 2024

No region tags are edited in this PR.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch from 10899dc to ba378f2 Compare November 18, 2024 10:06
@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch from 04b8f76 to f6674b9 Compare November 19, 2024 10:12
@alkatrivedi alkatrivedi added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Nov 19, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 19, 2024
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 19, 2024
@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch 9 times, most recently from 25cec8e to 5101675 Compare November 25, 2024 08:58
@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch from 5101675 to 99a18ae Compare November 25, 2024 10:27
@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch from b978f8c to cda3ed3 Compare November 25, 2024 10:47
span.addEvent(
`Created multiplexed session ${this._multiplexedSession.formattedName_}`
);
this.emit('mux-session-available');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the constant MUX_SESSION_AVAILABLE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and everywhere else, in the tests as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch from cea4617 to 2815e24 Compare November 28, 2024 11:02
@alkatrivedi alkatrivedi added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Nov 29, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 29, 2024
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 29, 2024
@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch 2 times, most recently from c57a1be to e8935b4 Compare November 29, 2024 03:36
@alkatrivedi alkatrivedi added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Nov 29, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 29, 2024
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 29, 2024
// frequency to create new mux session
refreshRate: number;
_multiplexedSession: Session | null;
_pingHandle!: NodeJS.Timer;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used anywhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no where, I have removed it. thanks!

span.addEvent('Waiting for a multiplexed session to become available');
let removeListener: Function;
const promise = new Promise(resolve => {
this.once(MUX_SESSION_AVAILABLE, resolve);
Copy link
Copy Markdown
Contributor

@harshachinta harshachinta Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this event is never emitted? Is there any timeout?
As per my understanding the MUX_SESSION_AVAILABLE event will be emitted only when there is no error during initial multiplexed session creation

Copy link
Copy Markdown
Contributor Author

@alkatrivedi alkatrivedi Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out.

What happens if this event is never emitted?

In case MUX_SESSION_AVAILABLE event will not get emitted, an error event MUX_SESSION_CREATE_ERROR will get emitted.
Hence, in _getSession we either will wait for the MUX_SESSION_AVAILABLE or for the MUX_SESSION_CREATE_ERROR.

Is there any timeout?

There is no any timeout in the method, we are relying on the events

});

try {
await promise;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the multiplexed session creation fails with error, then will that error be captured by getsession and surfaced to customers?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the error will get surfaced to the customers

});
});

describe('_getSession', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a negative case when create session fails with error? What will be the behaviour of getSession?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since, we are listening to the error event in _getSession in case of the error from the _createSession, I have added a test for the case when the error event will get emitted, if we are able to listen to that event in _getSession

@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch 2 times, most recently from ed7e9b6 to 625787b Compare December 2, 2024 19:12
@alkatrivedi alkatrivedi added the owlbot:run Add this label to trigger the Owlbot post processor. label Dec 4, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Dec 4, 2024
@alkatrivedi alkatrivedi added the owlbot:run Add this label to trigger the Owlbot post processor. label Dec 5, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Dec 5, 2024
@alkatrivedi alkatrivedi force-pushed the class-multiplexed-session branch from e6b1c24 to 4584d5e Compare December 6, 2024 05:31
});
});

it('should propagate errors for Multiplexed Session which gets emitted', async () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no assert in this test ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion part is in the if condition

if (err === fakeError) {
  resolve();
}

if both the errors are same the promise will get resolve and test will get pass

else {
  reject(new Error('Unexpected error emitted'));
}

otherwise the promise will fail with the rejected promise

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our 1:1. I have updated the test, and have used assert instead of if condition

multiplexedSession.on(MUX_SESSION_CREATE_ERROR, () => {
done();
});
multiplexedSession._createSession().catch(err => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here will .on event called first ? In that case you are calling done here and then .catch will never be called ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will call the done as soon as the error event will get emitted, after that the pointer will go to throw e in getSession hence, we need to catch that error here.

.catch will never be called ?

it will get called, on not calling .catch here will result into the unhandled exception error

* This method sets up a periodic refresh interval for maintaining the session. The interval duration
* is determined by the @param refreshRate option, which is provided in days.
* The default value is 7 days.
* @throws {Error} In case the multiplexed session creation will get fail, and an error occurs within `_createSession`,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @throws {Error} In case the multiplexed session creation will get fail, and an error occurs within `_createSession`,
* @throws {Error} If the multiplexed session creation fails in `_createSession`, the error is caught and ignored. This is because the currently active multiplexed session has a 30-day expiry, providing the maintainer with four opportunities (one every 7 days) to refresh the active session.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const promises = [
new Promise((_, reject) => {
this.once(MUX_SESSION_CREATE_ERROR, reject);
removeErrorListener = this.removeListener.bind(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my understanding,
In get_session, we are listening only to the error event but not emitting any error? The actual error gets emitted from the create_Session call?
which means the error surfaced to customer is from the error event emitted by create_session and not get_session?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the _createSession call will emit the error along with the event, which the listener will listen in _getSession. Now, we have two promises in _getSession, for the error case the promise for error event will be there which on begin await will resolve into a rejected promise. hence, an error will get propagated to the parent call

* @returns {Promise<void>} A Promise that resolves when the session has been successfully created and assigned, an event
* `mux-session-available` will be emitted to signal that the session is ready.
*
* In case of error, an error will get emitted along with the erorr event.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* In case of error, an error will get emitted along with the erorr event.
* In case of error, an error will get emitted along with the error event.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*
* Waits for the `MUX_SESSION_AVAILABLE` event or for the `MUX_SESSION_CREATE_ERROR`
* to be emitted if the multiplexed session is not yet available. The method listens
* for these events, and once `mux-session-available` is emitted, it resolves and returns
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the behavior when error event is emitted?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@harshachinta
Copy link
Copy Markdown
Contributor

Marking it as chore: since we are not explicitly providing any functionality here for the customers to use?
Please feel free to modify this if that is not true.
@surbhigarg92 @alkatrivedi

@alkatrivedi
Copy link
Copy Markdown
Contributor Author

Closing this PR, since we are facing issues in presubmit check for Owlbot Processor. I have moved the changes into another branch which has been created from the main repo. Link to the new PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api: spanner Issues related to the googleapis/nodejs-spanner API. size: l Pull request size is large.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants