-
Notifications
You must be signed in to change notification settings - Fork 666
chore(spanner): executor framework instance admin actions implementation #8093
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
Open
alkatrivedi
wants to merge
3
commits into
main
Choose a base branch
from
executor-framework-admin-actions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−22
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,13 +19,22 @@ import {Spanner} from '../../src'; | |
| import {trace, context, Tracer} from '@opentelemetry/api'; | ||
| import * as protos from '../../protos/protos'; | ||
| import {CloudUtil} from './cloud-util'; | ||
| import {OutcomeSender, ExecutionFlowContextInterface} from './cloud-executor'; | ||
| import { | ||
| OutcomeSender, | ||
| ExecutionFlowContextInterface, | ||
| CloudExecutor, | ||
| } from './cloud-executor'; | ||
| import spanner = protos.google.spanner; | ||
| import SpannerAsyncActionRequest = spanner.executor.v1.SpannerAsyncActionRequest; | ||
| import SpannerAsyncActionResponse = spanner.executor.v1.SpannerAsyncActionResponse; | ||
| import SpannerActionOutcome = spanner.executor.v1.SpannerActionOutcome; | ||
| import ISpannerAction = spanner.executor.v1.ISpannerAction; | ||
| import IAdminAction = spanner.executor.v1.IAdminAction; | ||
| import ICreateCloudInstanceAction = spanner.executor.v1.ICreateCloudInstanceAction; | ||
| import IUpdateCloudInstanceAction = spanner.executor.v1.IUpdateCloudInstanceAction; | ||
| import IDeleteCloudInstanceAction = spanner.executor.v1.IDeleteCloudInstanceAction; | ||
| import IListCloudInstancesAction = spanner.executor.v1.IListCloudInstancesAction; | ||
| import IGetCloudInstanceAction = spanner.executor.v1.IGetCloudInstanceAction; | ||
|
|
||
| /** | ||
| * Context for a single stream connection. | ||
|
|
@@ -66,9 +75,11 @@ export class ExecutionFlowContext implements ExecutionFlowContextInterface { | |
| * Sends an error back to the client. | ||
| */ | ||
| public onError(error: Error): void { | ||
| const stream = this.call as any; | ||
|
|
||
| if (this.call.cancelled || stream.destroyed || stream.writable === false) { | ||
| if ( | ||
| this.call.cancelled || | ||
| this.call.destroyed || | ||
| this.call.writable === false | ||
| ) { | ||
| console.warn( | ||
| 'Attempted to emit error to a closed or cancelled stream.', | ||
| error, | ||
|
|
@@ -99,6 +110,23 @@ export class CloudClientExecutor { | |
| action as ICreateCloudInstanceAction, | ||
| sender, | ||
| ), | ||
| updateCloudInstance: (action, sender) => | ||
| this.executeUpdateCloudInstance( | ||
| action as IUpdateCloudInstanceAction, | ||
| sender, | ||
| ), | ||
| deleteCloudInstance: (action, sender) => | ||
| this.executeDeleteCloudInstance( | ||
| action as IDeleteCloudInstanceAction, | ||
| sender, | ||
| ), | ||
| listCloudInstances: (action, sender) => | ||
| this.executeListCloudInstances( | ||
| action as IListCloudInstancesAction, | ||
| sender, | ||
| ), | ||
| getCloudInstance: (action, sender) => | ||
| this.executeGetCloudInstance(action as IGetCloudInstanceAction, sender), | ||
| }; | ||
|
|
||
| private readonly actionRegistry: Record<string, ActionHandler> = { | ||
|
|
@@ -155,10 +183,8 @@ export class CloudClientExecutor { | |
| action: ISpannerAction, | ||
| ): Promise<void> { | ||
| const actionType = | ||
| Object.keys(action).find( | ||
| k => | ||
| action[k as keyof typeof action] !== undefined && | ||
| !!this.actionRegistry[k], | ||
| Object.keys(this.actionRegistry).find( | ||
| k => action[k as keyof typeof action] !== undefined, | ||
| ) || 'unknown'; | ||
| const span = this.tracer.startSpan(`performaction_${actionType}`); | ||
|
|
||
|
|
@@ -195,10 +221,8 @@ export class CloudClientExecutor { | |
| sender: OutcomeSender, | ||
| ): Promise<void> { | ||
| try { | ||
| const adminType = Object.keys(action).find( | ||
| k => | ||
| action[k as keyof typeof action] !== undefined && | ||
| !!this.adminActionRegistry[k], | ||
| const adminType = Object.keys(this.adminActionRegistry).find( | ||
| k => action[k as keyof typeof action] !== undefined, | ||
| ); | ||
|
|
||
| if (adminType && this.adminActionRegistry[adminType]) { | ||
|
|
@@ -226,21 +250,28 @@ export class CloudClientExecutor { | |
| console.log(`Creating instance: \n${JSON.stringify(action, null, 2)}`); | ||
|
|
||
| const instanceId = action.instanceId!; | ||
| const projectId = action.projectId!; | ||
| const projectId = action.projectId || CloudExecutor.PROJECT_ID; | ||
| const configId = action.instanceConfigId!; | ||
|
|
||
| const instanceAdminClient = this.spanner.getInstanceAdminClient(); | ||
|
|
||
| const instancePayload: any = { | ||
| config: instanceAdminClient.instanceConfigPath(projectId, configId), | ||
| displayName: instanceId, | ||
| labels: action.labels || {}, | ||
| }; | ||
|
|
||
| if (action.nodeCount !== undefined) { | ||
| instancePayload.nodeCount = action.nodeCount; | ||
| } | ||
| if (action.processingUnits !== undefined) { | ||
| instancePayload.processingUnits = action.processingUnits; | ||
| } | ||
|
|
||
| const [operation] = await instanceAdminClient.createInstance({ | ||
| parent: instanceAdminClient.projectPath(projectId), | ||
| instanceId: instanceId, | ||
| instance: { | ||
| config: instanceAdminClient.instanceConfigPath(projectId, configId), | ||
| displayName: instanceId, | ||
| nodeCount: action.nodeCount || 1, | ||
| processingUnits: action.processingUnits, | ||
| labels: action.labels || {}, | ||
| }, | ||
| instance: instancePayload, | ||
| }); | ||
|
|
||
| console.log('Waiting for instance creation operation to complete...'); | ||
|
|
@@ -259,4 +290,144 @@ export class CloudClientExecutor { | |
| sender.finishWithError(err); | ||
| } | ||
| } | ||
|
|
||
| private async executeUpdateCloudInstance( | ||
| action: IUpdateCloudInstanceAction, | ||
| sender: OutcomeSender, | ||
| ): Promise<void> { | ||
| try { | ||
| console.log(`Updating instance: \n${JSON.stringify(action, null, 2)}`); | ||
|
|
||
| const instanceId = action.instanceId!; | ||
| const projectId = action.projectId || CloudExecutor.PROJECT_ID; | ||
|
|
||
| const instanceAdminClient = this.spanner.getInstanceAdminClient(); | ||
|
|
||
| const paths: string[] = []; | ||
| if (action.displayName !== undefined) paths.push('display_name'); | ||
| if (action.nodeCount !== undefined) paths.push('node_count'); | ||
| if (action.processingUnits !== undefined) paths.push('processing_units'); | ||
|
Comment on lines
+308
to
+309
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow the same approach for setting |
||
| if (action.labels && Object.keys(action.labels).length > 0) | ||
| paths.push('labels'); | ||
|
|
||
| const [operation] = await instanceAdminClient.updateInstance({ | ||
| instance: { | ||
| name: instanceAdminClient.instancePath(projectId, instanceId), | ||
| displayName: action.displayName, | ||
| nodeCount: action.nodeCount, | ||
| processingUnits: action.processingUnits, | ||
| labels: action.labels, | ||
| }, | ||
| fieldMask: {paths: paths}, | ||
| }); | ||
|
|
||
| console.log('Waiting for instance update operation to complete...'); | ||
| await operation.promise(); | ||
|
|
||
| console.log(`Instance ${instanceId} updated successfully.`); | ||
|
|
||
| sender.finishWithOK(); | ||
| } catch (err: any) { | ||
| console.error('Failed to update instance:', err); | ||
| sender.finishWithError(err); | ||
| } | ||
| } | ||
|
|
||
| private async executeDeleteCloudInstance( | ||
| action: IDeleteCloudInstanceAction, | ||
| sender: OutcomeSender, | ||
| ): Promise<void> { | ||
| try { | ||
| console.log(`Deleting instance: \n${JSON.stringify(action, null, 2)}`); | ||
|
|
||
| const instanceId = action.instanceId!; | ||
| const projectId = action.projectId || CloudExecutor.PROJECT_ID; | ||
|
|
||
| const instanceAdminClient = this.spanner.getInstanceAdminClient(); | ||
|
|
||
| await instanceAdminClient.deleteInstance({ | ||
| name: instanceAdminClient.instancePath(projectId, instanceId), | ||
| }); | ||
|
|
||
| console.log(`Instance ${instanceId} deleted successfully.`); | ||
|
|
||
| sender.finishWithOK(); | ||
| } catch (err: any) { | ||
| console.error('Failed to delete instance:', err); | ||
| sender.finishWithError(err); | ||
| } | ||
| } | ||
|
|
||
| private async executeListCloudInstances( | ||
| action: IListCloudInstancesAction, | ||
| sender: OutcomeSender, | ||
| ): Promise<void> { | ||
| try { | ||
| console.log(`Listing instances: \n${JSON.stringify(action, null, 2)}`); | ||
|
|
||
| const projectId = action.projectId || CloudExecutor.PROJECT_ID; | ||
|
|
||
| const instanceAdminClient = this.spanner.getInstanceAdminClient(); | ||
|
|
||
| const [instances, , response] = await instanceAdminClient.listInstances({ | ||
| parent: instanceAdminClient.projectPath(projectId), | ||
| filter: action.filter, | ||
| pageSize: action.pageSize, | ||
| pageToken: action.pageToken, | ||
| }); | ||
|
|
||
| console.log(`Found ${instances.length} instances.`); | ||
|
|
||
| const outcome = SpannerActionOutcome.create({ | ||
| status: CloudExecutor.toProto(status.OK), | ||
| commitTime: {seconds: 0, nanos: 0}, | ||
| adminResult: { | ||
| instanceResponse: { | ||
| listedInstances: instances, | ||
| nextPageToken: response?.nextPageToken || '', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| sender.sendOutcome(outcome); | ||
| } catch (err: any) { | ||
| console.error('Failed to list instances:', err); | ||
| sender.finishWithError(err); | ||
| } | ||
| } | ||
|
|
||
| private async executeGetCloudInstance( | ||
| action: IGetCloudInstanceAction, | ||
| sender: OutcomeSender, | ||
| ): Promise<void> { | ||
| try { | ||
| console.log(`Getting instance: \n${JSON.stringify(action, null, 2)}`); | ||
|
|
||
| const instanceId = action.instanceId!; | ||
| const projectId = action.projectId || CloudExecutor.PROJECT_ID; | ||
|
|
||
| const instanceAdminClient = this.spanner.getInstanceAdminClient(); | ||
|
|
||
| const [instance] = await instanceAdminClient.getInstance({ | ||
| name: instanceAdminClient.instancePath(projectId, instanceId), | ||
| }); | ||
|
|
||
| console.log(`Found instance: ${instance.name}`); | ||
|
|
||
| const outcome = SpannerActionOutcome.create({ | ||
| status: CloudExecutor.toProto(status.OK), | ||
| commitTime: {seconds: 0, nanos: 0}, | ||
| adminResult: { | ||
| instanceResponse: { | ||
| instance: instance, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| sender.sendOutcome(outcome); | ||
| } catch (err: any) { | ||
| console.error('Failed to get instance:', err); | ||
| sender.finishWithError(err); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is no default value set for
nodeCountorprocessingUnits, if none of it is provided. Also only one of these should be set