@@ -7,6 +7,7 @@ import * as cr from 'aws-cdk-lib/custom-resources';
77import { Construct } from 'constructs' ;
88import * as path from 'path' ;
99import { execSync } from 'child_process' ;
10+ import { trinoAfterDeployCommands , trinoUserDataCommands } from "./trino" ;
1011
1112const ROOT = path . join ( __dirname , '../../..' )
1213
@@ -18,7 +19,7 @@ interface CdkStackProps extends StackProps {
1819}
1920
2021export class CdkStack extends Stack {
21- constructor ( scope : Construct , id : string , props : CdkStackProps ) {
22+ constructor ( scope : Construct , id : string , props : CdkStackProps ) {
2223 super ( scope , id , props ) ;
2324
2425 const { config } = props ;
@@ -122,7 +123,8 @@ EOF`,
122123 // Enable and start the service
123124 'systemctl daemon-reload' ,
124125 'systemctl enable worker' ,
125- 'systemctl start worker'
126+ 'systemctl start worker' ,
127+ ...trinoUserDataCommands ( i )
126128 ) ;
127129
128130 const instance = new ec2 . Instance ( this , `BenchmarkInstance${ i } ` , {
@@ -161,33 +163,50 @@ sudo journalctl -u worker.service -f -o cat
161163 } ) ;
162164
163165 // Custom resource to restart worker service on every deploy
164- const restartWorker = new cr . AwsCustomResource ( this , 'RestartWorkerService' , {
165- onUpdate : {
166- service : 'SSM' ,
167- action : 'sendCommand' ,
168- parameters : {
169- DocumentName : 'AWS-RunShellScript' ,
170- InstanceIds : instances . map ( inst => inst . instanceId ) ,
171- Parameters : {
172- commands : [
173- `aws s3 cp s3://${ workerBinary . s3BucketName } /${ workerBinary . s3ObjectKey } /usr/local/bin/worker` ,
174- 'chmod +x /usr/local/bin/worker' ,
175- 'systemctl restart worker' ,
176- ] ,
177- } ,
166+ sendCommandsUnconditionally ( this , 'RestartWorkerService' , instances , [
167+ `aws s3 cp s3://${ workerBinary . s3BucketName } /${ workerBinary . s3ObjectKey } /usr/local/bin/worker` ,
168+ 'chmod +x /usr/local/bin/worker' ,
169+ 'systemctl restart worker' ,
170+ ] )
171+
172+ // Start coordinator first
173+ sendCommandsUnconditionally ( this , 'RestartTrinoCoordinator' , [ instances [ 0 ] ] , [
174+ 'systemctl start trino' ,
175+ ] )
176+
177+ // Then start workers (they will discover the coordinator)
178+ sendCommandsUnconditionally ( this , 'RestartTrinoWorkers' , instances . slice ( 1 ) , trinoAfterDeployCommands ( this . region ) )
179+ }
180+ }
181+
182+ function sendCommandsUnconditionally (
183+ construct : Construct ,
184+ name : string ,
185+ instances : ec2 . Instance [ ] ,
186+ commands : string [ ]
187+ ) {
188+ const cmd = new cr . AwsCustomResource ( construct , name , {
189+ onUpdate : {
190+ service : 'SSM' ,
191+ action : 'sendCommand' ,
192+ parameters : {
193+ DocumentName : 'AWS-RunShellScript' ,
194+ InstanceIds : instances . map ( inst => inst . instanceId ) ,
195+ Parameters : {
196+ commands
178197 } ,
179- physicalResourceId : cr . PhysicalResourceId . of ( `restart-${ Date . now ( ) } ` ) ,
180- ignoreErrorCodesMatching : '.*' ,
181198 } ,
182- policy : cr . AwsCustomResourcePolicy . fromStatements ( [
183- new iam . PolicyStatement ( {
184- actions : [ 'ssm:SendCommand' ] ,
185- resources : [ '*' ] ,
186- } ) ,
187- ] ) ,
188- } ) ;
189-
190- // Ensure instances are created before restarting
191- restartWorker . node . addDependency ( ...instances )
192- }
199+ physicalResourceId : cr . PhysicalResourceId . of ( `${ name } -${ Date . now ( ) } ` ) ,
200+ ignoreErrorCodesMatching : '.*' ,
201+ } ,
202+ policy : cr . AwsCustomResourcePolicy . fromStatements ( [
203+ new iam . PolicyStatement ( {
204+ actions : [ 'ssm:SendCommand' ] ,
205+ resources : [ '*' ] ,
206+ } ) ,
207+ ] ) ,
208+ } ) ;
209+
210+ // Ensure instances are created before restarting
211+ cmd . node . addDependency ( ...instances )
193212}
0 commit comments