Skip to content

Commit 74793b3

Browse files
committed
internal-api-client - JobRuntimeResolver - resolve component data once per job
1 parent 62ea4b2 commit 74793b3

File tree

2 files changed

+74
-54
lines changed

2 files changed

+74
-54
lines changed

src/JobFactory/JobRuntimeResolver.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class JobRuntimeResolver
2222

2323
private StorageClientPlainFactory $storageClientFactory;
2424
private ?array $configuration;
25+
private array $componentData;
2526
private array $jobData;
2627

2728
public function __construct(StorageClientPlainFactory $storageClientFactory)
@@ -35,6 +36,9 @@ public function resolveJobData(array $jobData, array $tokenInfo): array
3536
$this->jobData = $jobData;
3637

3738
try {
39+
$this->componentData = $this->getComponentsApiClient(null)
40+
->getComponent($this->jobData['componentId']);
41+
3842
$tag = $this->resolveTag();
3943
$variableValues = $this->resolveVariables();
4044
$backend = $this->resolveBackend($tokenInfo);
@@ -68,10 +72,8 @@ private function resolveTag(): string
6872
if (!empty($configuration['runtime']['image_tag'])) {
6973
return (string) $configuration['runtime']['image_tag'];
7074
}
71-
$componentsApi = $this->getComponentsApiClient(null);
72-
$componentData = $componentsApi->getComponent($this->jobData['componentId']);
73-
if (!empty($componentData['data']['definition']['tag'])) {
74-
return $componentData['data']['definition']['tag'];
75+
if (!empty($this->componentData['data']['definition']['tag'])) {
76+
return $this->componentData['data']['definition']['tag'];
7577
} else {
7678
throw new ClientException(sprintf('The component "%s" is not runnable.', $this->jobData['componentId']));
7779
}
@@ -133,17 +135,13 @@ private function resolveBackend(array $tokenInfo): Backend
133135
return new Backend(
134136
$tempBackend->getType(),
135137
$tempBackend->getContainerType(),
136-
$this->getDefaultBackendContext(
137-
$this->getComponentsApiClient(null)
138-
->getComponent($this->jobData['componentId'])['type']
139-
)
138+
$this->getDefaultBackendContext($this->componentData['type'])
140139
);
141140
}
142141

143142
// decide whether to set "type' (aka workspaceSize) or containerType (aka containerSize)
144-
$component = $this->getComponentsApiClient(null)->getComponent($this->jobData['componentId']);
145-
$stagingStorage = $component['data']['staging_storage']['input'] ?? '';
146-
$backendContext = $tempBackend->getContext() ?? $this->getDefaultBackendContext($component['type']);
143+
$stagingStorage = $this->componentData['data']['staging_storage']['input'] ?? '';
144+
$backendContext = $tempBackend->getContext() ?? $this->getDefaultBackendContext($this->componentData['type']);
147145

148146
/* Possible values of staging storage: https://github.com/keboola/docker-bundle/blob/ec9a628b614a70d0ed8a6ec36f2b6003a8e07ed4/src/Docker/Configuration/Component.php#L87
149147
For the purpose of setting backend, we consider: 'local', 's3', 'abs', 'none' to use container.

tests/JobFactory/JobRuntimeResolverTest.php

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ public function testResolveRuntimeSettingsInConfiguration(): void
225225
$clientMock = self::createMock(Client::class);
226226
$clientMock->expects(self::exactly(2))->method('apiGet')
227227
->withConsecutive(
228+
['branch/default/components/keboola.ex-db-snowflake'],
228229
['branch/default/components/keboola.ex-db-snowflake/configs/454124290'],
229-
['branch/default/components/keboola.ex-db-snowflake']
230230
)->willReturnOnConsecutiveCalls(
231-
$configuration,
232-
$componentData
231+
$componentData,
232+
$configuration
233233
);
234234
$clientWrapperMock = self::createMock(ClientWrapper::class);
235235
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
@@ -301,11 +301,11 @@ public function testResolveRuntimeSettingsInConfigurationOfTransformations(): vo
301301
$clientMock = self::createMock(Client::class);
302302
$clientMock->expects(self::exactly(2))->method('apiGet')
303303
->withConsecutive(
304-
['branch/default/components/keboola.ex-db-snowflake/configs/454124290'],
305-
['branch/default/components/keboola.ex-db-snowflake']
304+
['branch/default/components/keboola.ex-db-snowflake'],
305+
['branch/default/components/keboola.ex-db-snowflake/configs/454124290']
306306
)->willReturnOnConsecutiveCalls(
307-
$configuration,
308-
$componentData
307+
$componentData,
308+
$configuration
309309
);
310310
$clientWrapperMock = self::createMock(ClientWrapper::class);
311311
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
@@ -379,11 +379,11 @@ public function testResolveRuntimeSettingsPriority(): void
379379
$clientMock = self::createMock(Client::class);
380380
$clientMock->expects(self::exactly(2))->method('apiGet')
381381
->withConsecutive(
382-
['branch/default/components/keboola.ex-db-snowflake/configs/454124290'],
383382
['branch/default/components/keboola.ex-db-snowflake'],
383+
['branch/default/components/keboola.ex-db-snowflake/configs/454124290']
384384
)->willReturnOnConsecutiveCalls(
385-
$configuration,
386-
$this->getTestComponentData()
385+
$this->getTestComponentData(),
386+
$configuration
387387
);
388388
$clientWrapperMock = self::createMock(ClientWrapper::class);
389389
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
@@ -441,21 +441,19 @@ public function testResolveRuntimeSettingsNowhere(): void
441441
];
442442

443443
$clientMock = self::createMock(Client::class);
444-
$clientMock->expects(self::exactly(3))->method('apiGet')
444+
$clientMock->expects(self::exactly(2))->method('apiGet')
445445
->withConsecutive(
446-
['branch/default/components/keboola.ex-db-snowflake/configs/454124290'],
447-
['branch/default/components/keboola.ex-db-snowflake'],
448446
['branch/default/components/keboola.ex-db-snowflake'],
447+
['branch/default/components/keboola.ex-db-snowflake/configs/454124290']
449448
)->willReturnOnConsecutiveCalls(
450-
$configuration,
451449
$this->getTestExtractorComponentData(),
452-
$this->getTestExtractorComponentData()
450+
$configuration
453451
);
454452
$clientWrapperMock = self::createMock(ClientWrapper::class);
455453
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
456454
$storageClientFactoryMock = self::createMock(StorageClientPlainFactory::class);
457455
$storageClientFactoryMock
458-
->expects(self::exactly(3))
456+
->expects(self::exactly(2))
459457
->method('createClientWrapper')
460458
->willReturn($clientWrapperMock);
461459

@@ -495,8 +493,19 @@ public function testResolveInvalidConfigurationFailsWithClientException(): void
495493
'parameters' => ['foo' => 'bar'],
496494
];
497495

496+
$clientMock = self::createMock(Client::class);
497+
$clientMock->expects(self::exactly(1))->method('apiGet')
498+
->with('branch/default/components/keboola.ex-db-snowflake')->willReturn(
499+
$this->getTestExtractorComponentData()
500+
)
501+
;
502+
$clientWrapperMock = self::createMock(ClientWrapper::class);
503+
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
498504
$storageClientFactoryMock = self::createMock(StorageClientPlainFactory::class);
499-
$storageClientFactoryMock->expects(self::never())->method('createClientWrapper');
505+
$storageClientFactoryMock
506+
->expects(self::exactly(1))
507+
->method('createClientWrapper')
508+
->willReturn($clientWrapperMock);
500509

501510
$jobRuntimeResolver = new JobRuntimeResolver($storageClientFactoryMock);
502511
self::expectException(ClientException::class);
@@ -509,15 +518,24 @@ public function testResolveRuntimeSettingsConfigurationNotFound(): void
509518
{
510519
$jobData = self::JOB_DATA;
511520
$clientMock = self::createMock(Client::class);
512-
$clientMock->expects(self::once())->method('apiGet')
513-
->with(
514-
'branch/default/components/keboola.ex-db-snowflake/configs/454124290',
515-
)->willThrowException(new StorageClientException('Configuration "454124290" not found', 404));
521+
$countMatcher = self::exactly(2);
522+
$clientMock->expects($countMatcher)->method('apiGet')
523+
->withConsecutive(
524+
['branch/default/components/keboola.ex-db-snowflake'],
525+
['branch/default/components/keboola.ex-db-snowflake/configs/454124290']
526+
)->willReturnCallback(function () use ($countMatcher) {
527+
if ($countMatcher->getInvocationCount() === 1) {
528+
return $this->getTestComponentData();
529+
}
530+
531+
throw new StorageClientException('Configuration "454124290" not found', 404);
532+
})
533+
;
516534
$clientWrapperMock = self::createMock(ClientWrapper::class);
517535
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
518536
$storageClientFactoryMock = self::createMock(StorageClientPlainFactory::class);
519537
$storageClientFactoryMock
520-
->expects(self::once())
538+
->expects(self::exactly(2))
521539
->method('createClientWrapper')
522540
->willReturn($clientWrapperMock);
523541

@@ -534,7 +552,7 @@ public function testResolveNoConfiguration(): void
534552
unset($jobData['configId']);
535553

536554
$clientMock = self::createMock(Client::class);
537-
$clientMock->expects(self::exactly(2))->method('apiGet')
555+
$clientMock->expects(self::exactly(1))->method('apiGet')
538556
->with('branch/default/components/keboola.ex-db-snowflake')->willReturn(
539557
$this->getTestExtractorComponentData()
540558
)
@@ -543,7 +561,7 @@ public function testResolveNoConfiguration(): void
543561
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
544562
$storageClientFactoryMock = self::createMock(StorageClientPlainFactory::class);
545563
$storageClientFactoryMock
546-
->expects(self::exactly(2))
564+
->expects(self::exactly(1))
547565
->method('createClientWrapper')
548566
->willReturn($clientWrapperMock);
549567

@@ -579,7 +597,7 @@ public function testResolveEmptyConfiguration(): void
579597
$jobData['configId'] = '';
580598

581599
$clientMock = self::createMock(Client::class);
582-
$clientMock->expects(self::exactly(2))->method('apiGet')
600+
$clientMock->expects(self::exactly(1))->method('apiGet')
583601
->with('branch/default/components/keboola.ex-db-snowflake')->willReturn(
584602
$this->getTestExtractorComponentData()
585603
)
@@ -588,7 +606,7 @@ public function testResolveEmptyConfiguration(): void
588606
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
589607
$storageClientFactoryMock = self::createMock(StorageClientPlainFactory::class);
590608
$storageClientFactoryMock
591-
->expects(self::exactly(2))
609+
->expects(self::exactly(1))
592610
->method('createClientWrapper')
593611
->willReturn($clientWrapperMock);
594612

@@ -625,22 +643,20 @@ public function testResolveEmptyConfigurationData(): void
625643
$jobData['configId'] = '123456';
626644

627645
$clientMock = self::createMock(Client::class);
628-
$clientMock->expects(self::exactly(3))->method('apiGet')
646+
$clientMock->expects(self::exactly(2))->method('apiGet')
629647
->withConsecutive(
630-
['branch/default/components/keboola.ex-db-snowflake/configs/123456'],
631-
['branch/default/components/keboola.ex-db-snowflake'],
632648
['branch/default/components/keboola.ex-db-snowflake'],
649+
['branch/default/components/keboola.ex-db-snowflake/configs/123456'],
633650
)
634651
->willReturn(
635-
['configuration' => null],
636652
$this->getTestExtractorComponentData(),
637-
$this->getTestExtractorComponentData()
653+
['configuration' => null]
638654
);
639655
$clientWrapperMock = self::createMock(ClientWrapper::class);
640656
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
641657
$storageClientFactoryMock = self::createMock(StorageClientPlainFactory::class);
642658
$storageClientFactoryMock
643-
->expects(self::exactly(3))
659+
->expects(self::exactly(2))
644660
->method('createClientWrapper')
645661
->willReturn($clientWrapperMock);
646662

@@ -698,16 +714,16 @@ public function testInternalCacheIsClearedForEveryCall(): void
698714
$clientMock = self::createMock(Client::class);
699715
$clientMock->expects(self::exactly(4))->method('apiGet')
700716
->withConsecutive(
701-
['branch/default/components/keboola.ex-db-snowflake/configs/454124290'],
702717
['branch/default/components/keboola.ex-db-snowflake'],
703718
['branch/default/components/keboola.ex-db-snowflake/configs/454124290'],
704-
['branch/default/components/keboola.ex-db-snowflake']
719+
['branch/default/components/keboola.ex-db-snowflake'],
720+
['branch/default/components/keboola.ex-db-snowflake/configs/454124290']
705721
)
706722
->willReturnOnConsecutiveCalls(
707-
$configuration,
708723
$this->getTestComponentData(),
709724
$configuration,
710-
$this->getTestComponentData()
725+
$this->getTestComponentData(),
726+
$configuration
711727
);
712728
$clientWrapperMock = self::createMock(ClientWrapper::class);
713729
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
@@ -778,9 +794,9 @@ public function testResolveBranchConfiguration(): void
778794
$clientMock = self::createMock(BranchAwareClient::class);
779795
$clientMock->expects(self::exactly(2))->method('apiGet')
780796
->withConsecutive(
781-
['components/keboola.ex-db-snowflake/configs/454124290'],
782797
['components/keboola.ex-db-snowflake'],
783-
)->willReturnOnConsecutiveCalls($configuration, $this->getTestComponentData());
798+
['components/keboola.ex-db-snowflake/configs/454124290']
799+
)->willReturnOnConsecutiveCalls($this->getTestComponentData(), $configuration);
784800
$clientWrapperMock = self::createMock(ClientWrapper::class);
785801
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
786802
$storageClientFactoryMock = self::createMock(StorageClientPlainFactory::class);
@@ -789,8 +805,8 @@ public function testResolveBranchConfiguration(): void
789805
->method('createClientWrapper')
790806
// this is the important bit - branchId is passed as 2nd argument
791807
->withConsecutive(
792-
[new ClientOptions(null, 'KBC::ProjectSecure::token', 'dev-branch')],
793-
[new ClientOptions(null, 'KBC::ProjectSecure::token', null)]
808+
[new ClientOptions(null, 'KBC::ProjectSecure::token', null)],
809+
[new ClientOptions(null, 'KBC::ProjectSecure::token', 'dev-branch')]
794810
)
795811
->willReturn($clientWrapperMock);
796812

@@ -841,13 +857,19 @@ public function testConfigurationDisabledException(): void
841857
];
842858

843859
$clientMock = self::createMock(Client::class);
844-
$clientMock->expects(self::once())->method('apiGet')
845-
->with('branch/default/components/keboola.ex-db-snowflake/configs/454124290')->willReturn($configuration);
860+
$clientMock->expects(self::exactly(2))->method('apiGet')
861+
->withConsecutive(
862+
['branch/default/components/keboola.ex-db-snowflake'],
863+
['branch/default/components/keboola.ex-db-snowflake/configs/454124290']
864+
)->willReturnOnConsecutiveCalls(
865+
$this->getTestComponentData(),
866+
$configuration
867+
);
846868
$clientWrapperMock = self::createMock(ClientWrapper::class);
847869
$clientWrapperMock->method('getBranchClientIfAvailable')->willReturn($clientMock);
848870
$storageClientFactoryMock = self::createMock(StorageClientPlainFactory::class);
849871
$storageClientFactoryMock
850-
->expects(self::once())
872+
->expects(self::exactly(2))
851873
->method('createClientWrapper')
852874
->willReturn($clientWrapperMock);
853875
$jobRuntimeResolver = new JobRuntimeResolver($storageClientFactoryMock);

0 commit comments

Comments
 (0)