Skip to content
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

chore: directly retrieve connections from ICP when using the ICP #285

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions common/lib/plugins/read_write_splitting_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,21 @@ export class ReadWriteSplittingPlugin extends AbstractConnectionPlugin implement
try {
const targetClient = await this.pluginService.connect(writerHost, props);
this.isWriterClientFromInternalPool = this.pluginService.getConnectionProvider(writerHost, props) instanceof InternalPooledConnectionProvider;
this.setWriterClient(targetClient, writerHost);
await this.switchCurrentTargetClientTo(this.writerTargetClient, writerHost);
if (!this.isWriterClientFromInternalPool) {
this.setWriterClient(targetClient, writerHost);
} else {
// Do not cache clients from internal pools.
logger.debug(Messages.get("ReadWriteSplittingPlugin.setWriterClient", writerHost.url));
// Verify clients from internal pools.
if (!(await this.isTargetClientUsable(targetClient))) {
logger.debug(Messages.get("ReadWriteSplittingPlugin.failedToConnectToWriter", writerHost.url));
Copy link
Contributor

Choose a reason for hiding this comment

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

So targetClient isn't good. Should we get a new one?

}
if (this.isReaderClientFromInternalPool) {
// Release the previous pooled connection back to the pool.
await this.closeTargetClientIfIdle(this.pluginService.getCurrentClient().targetClient);
}
}
await this.switchCurrentTargetClientTo(targetClient, writerHost);
} catch (any) {
logger.warn(Messages.get("ReadWriteSplittingPlugin.failedToConnectToWriter", writerHost.url));
}
Expand Down Expand Up @@ -303,8 +316,22 @@ export class ReadWriteSplittingPlugin extends AbstractConnectionPlugin implement
return;
}
logger.debug(Messages.get("ReadWriteSplittingPlugin.successfullyConnectedToReader", readerHost.url));
this.setReaderClient(targetClient, readerHost);
await this.switchCurrentTargetClientTo(this.readerTargetClient, this._readerHostInfo);

if (!this.isReaderClientFromInternalPool) {
this.setReaderClient(targetClient, readerHost);
} else {
// Do not cache clients from internal pools.
logger.debug(Messages.get("ReadWriteSplittingPlugin.setReaderClient", readerHost.url));
// Verify clients from internal pools.
if (!(await this.isTargetClientUsable(targetClient))) {
logger.debug(Messages.get("ReadWriteSplittingPlugin.failedToConnectToReader", readerHost.url));
Copy link
Contributor

Choose a reason for hiding this comment

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

So targetClient isn't good. Should we get a new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we should log and throw here instead.

}
if (this.isWriterClientFromInternalPool) {
// Release the previous pooled connection back to the pool.
await this.closeTargetClientIfIdle(this.pluginService.getCurrentClient().targetClient);
}
}
await this.switchCurrentTargetClientTo(targetClient, readerHost);
}

async switchToWriterTargetClient(hosts: HostInfo[]) {
Expand All @@ -318,16 +345,13 @@ export class ReadWriteSplittingPlugin extends AbstractConnectionPlugin implement
if (!writerHost) {
return;
}
if (!(await this.isTargetClientUsable(this.writerTargetClient))) {
if (!this.writerTargetClient || this.isWriterClientFromInternalPool) {
await this.getNewWriterClient(writerHost);
} else if (this.writerTargetClient) {
} else if (await this.isTargetClientUsable(this.writerTargetClient)) {
await this.switchCurrentTargetClientTo(this.writerTargetClient, writerHost);
}

logger.debug(Messages.get("ReadWriteSplittingPlugin.switchedFromReaderToWriter", writerHost.url));
if (this.isReaderClientFromInternalPool) {
await this.closeTargetClientIfIdle(this.readerTargetClient);
}
}

async switchToReaderTargetClient(hosts: HostInfo[]) {
Expand All @@ -338,23 +362,20 @@ export class ReadWriteSplittingPlugin extends AbstractConnectionPlugin implement
}

this._inReadWriteSplit = true;
if (!(await this.isTargetClientUsable(this.readerTargetClient))) {
if (!this.readerTargetClient || this.isReaderClientFromInternalPool) {
await this.initializeReaderClient(hosts);
} else if (this.readerTargetClient != null && this._readerHostInfo != null) {
} else if (await this.isTargetClientUsable(this.readerTargetClient)) {
try {
await this.switchCurrentTargetClientTo(this.readerTargetClient, this._readerHostInfo);
logger.debug(Messages.get("ReadWriteSplittingPlugin.switchedFromWriterToReader", this._readerHostInfo.url));
logger.debug(Messages.get("ReadWriteSplittingPlugin.switchedFromWriterToReader", this._readerHostInfo?.url ?? ""));
} catch (error: any) {
logger.debug(Messages.get("ReadWriteSplittingPlugin.errorSwitchingToCachedReader", this._readerHostInfo.url));
logger.debug(Messages.get("ReadWriteSplittingPlugin.errorSwitchingToCachedReader", this._readerHostInfo?.url ?? ""));
await this.pluginService.abortTargetClient(this.readerTargetClient);
this.readerTargetClient = undefined;
this._readerHostInfo = undefined;
await this.initializeReaderClient(hosts);
}
}
if (this.isWriterClientFromInternalPool) {
await this.closeTargetClientIfIdle(this.writerTargetClient);
}
}

async isTargetClientUsable(targetClient: ClientWrapper | undefined): Promise<boolean> {
Expand Down
Loading