-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scheduler client that pulls from bucket cache (#5605)
- Loading branch information
Showing
6 changed files
with
292 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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 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
149 changes: 149 additions & 0 deletions
149
...uler/client/src/main/java/io/airbyte/scheduler/client/BucketSpecCacheSchedulerClient.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2020 Airbyte | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package io.airbyte.scheduler.client; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.google.api.client.util.Preconditions; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import io.airbyte.commons.json.Jsons; | ||
import io.airbyte.config.DestinationConnection; | ||
import io.airbyte.config.JobConfig.ConfigType; | ||
import io.airbyte.config.SourceConnection; | ||
import io.airbyte.config.StandardCheckConnectionOutput; | ||
import io.airbyte.protocol.models.AirbyteCatalog; | ||
import io.airbyte.protocol.models.AirbyteProtocolSchema; | ||
import io.airbyte.protocol.models.ConnectorSpecification; | ||
import io.airbyte.validation.json.JsonSchemaValidator; | ||
import io.airbyte.validation.json.JsonValidationException; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Path; | ||
import java.time.Instant; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.function.Function; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class BucketSpecCacheSchedulerClient implements SynchronousSchedulerClient { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(BucketSpecCacheSchedulerClient.class); | ||
|
||
private final SynchronousSchedulerClient client; | ||
private final Function<String, Optional<ConnectorSpecification>> bucketSpecFetcher; | ||
|
||
public BucketSpecCacheSchedulerClient(final SynchronousSchedulerClient client, final String bucketName) { | ||
this( | ||
client, | ||
dockerImage -> attemptToFetchSpecFromBucket(StorageOptions.getDefaultInstance().getService(), bucketName, dockerImage)); | ||
} | ||
|
||
@VisibleForTesting | ||
BucketSpecCacheSchedulerClient(final SynchronousSchedulerClient client, | ||
final Function<String, Optional<ConnectorSpecification>> bucketSpecFetcher) { | ||
this.client = client; | ||
this.bucketSpecFetcher = bucketSpecFetcher; | ||
} | ||
|
||
@Override | ||
public SynchronousResponse<StandardCheckConnectionOutput> createSourceCheckConnectionJob(final SourceConnection source, final String dockerImage) | ||
throws IOException { | ||
return client.createSourceCheckConnectionJob(source, dockerImage); | ||
} | ||
|
||
@Override | ||
public SynchronousResponse<StandardCheckConnectionOutput> createDestinationCheckConnectionJob(final DestinationConnection destination, | ||
final String dockerImage) | ||
throws IOException { | ||
return client.createDestinationCheckConnectionJob(destination, dockerImage); | ||
} | ||
|
||
@Override | ||
public SynchronousResponse<AirbyteCatalog> createDiscoverSchemaJob(final SourceConnection source, final String dockerImage) throws IOException { | ||
return client.createDiscoverSchemaJob(source, dockerImage); | ||
} | ||
|
||
@Override | ||
public SynchronousResponse<ConnectorSpecification> createGetSpecJob(final String dockerImage) throws IOException { | ||
Optional<ConnectorSpecification> cachedSpecOptional; | ||
// never want to fail because we could not fetch from off board storage. | ||
try { | ||
cachedSpecOptional = bucketSpecFetcher.apply(dockerImage); | ||
} catch (final RuntimeException e) { | ||
cachedSpecOptional = Optional.empty(); | ||
} | ||
|
||
if (cachedSpecOptional.isPresent()) { | ||
final long now = Instant.now().toEpochMilli(); | ||
final SynchronousJobMetadata mockMetadata = new SynchronousJobMetadata( | ||
UUID.randomUUID(), | ||
ConfigType.GET_SPEC, | ||
null, | ||
now, | ||
now, | ||
true, | ||
Path.of("")); | ||
return new SynchronousResponse<>(cachedSpecOptional.get(), mockMetadata); | ||
} else { | ||
return client.createGetSpecJob(dockerImage); | ||
} | ||
} | ||
|
||
private static void validateConfig(final JsonNode json) throws JsonValidationException { | ||
final JsonSchemaValidator jsonSchemaValidator = new JsonSchemaValidator(); | ||
final JsonNode specJsonSchema = JsonSchemaValidator.getSchema(AirbyteProtocolSchema.PROTOCOL.getFile(), "ConnectorSpecification"); | ||
jsonSchemaValidator.ensure(specJsonSchema, json); | ||
} | ||
|
||
private static Optional<ConnectorSpecification> attemptToFetchSpecFromBucket(final Storage storage, | ||
final String bucketName, | ||
final String dockerImage) { | ||
final String[] dockerImageComponents = dockerImage.split(":"); | ||
Preconditions.checkArgument(dockerImageComponents.length == 2, "Invalidate docker image: " + dockerImage); | ||
final String dockerImageName = dockerImageComponents[0]; | ||
final String dockerImageTag = dockerImageComponents[1]; | ||
final Blob specAsBlob = | ||
storage.get(bucketName, Path.of("specs").resolve(dockerImageName).resolve(dockerImageTag).resolve("spec.json").toString()); | ||
|
||
// if null it means the object was not found. | ||
if (specAsBlob == null) { | ||
LOGGER.warn("Spec not found in bucket storage"); | ||
return Optional.empty(); | ||
} | ||
|
||
final String specAsString = new String(specAsBlob.getContent(), StandardCharsets.UTF_8); | ||
try { | ||
validateConfig(Jsons.deserialize(specAsString)); | ||
} catch (final JsonValidationException e) { | ||
LOGGER.error("Received invalid spec from bucket store. Received: {}", specAsString); | ||
return Optional.empty(); | ||
} | ||
return Optional.of(Jsons.deserialize(specAsString, ConnectorSpecification.class)); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
.../client/src/test/java/io/airbyte/scheduler/client/BucketSpecCacheSchedulerClientTest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2020 Airbyte | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package io.airbyte.scheduler.client; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.airbyte.protocol.models.ConnectorSpecification; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class BucketSpecCacheSchedulerClientTest { | ||
|
||
private SynchronousSchedulerClient defaultClientMock; | ||
private Function<String, Optional<ConnectorSpecification>> bucketSpecFetcherMock; | ||
|
||
@SuppressWarnings("unchecked") | ||
@BeforeEach | ||
void setup() { | ||
defaultClientMock = mock(SynchronousSchedulerClient.class); | ||
bucketSpecFetcherMock = mock(Function.class); | ||
} | ||
|
||
@Test | ||
void testGetsSpecIfPresent() throws IOException { | ||
when(bucketSpecFetcherMock.apply("source-pokeapi:0.1.0")).thenReturn(Optional.of(new ConnectorSpecification())); | ||
final BucketSpecCacheSchedulerClient client = new BucketSpecCacheSchedulerClient(defaultClientMock, bucketSpecFetcherMock); | ||
assertEquals(new ConnectorSpecification(), client.createGetSpecJob("source-pokeapi:0.1.0").getOutput()); | ||
verifyNoInteractions(defaultClientMock); | ||
} | ||
|
||
@Test | ||
void testCallsDelegateIfNotPresent() throws IOException { | ||
when(bucketSpecFetcherMock.apply("source-pokeapi:0.1.0")).thenReturn(Optional.empty()); | ||
when(defaultClientMock.createGetSpecJob("source-pokeapi:0.1.0")) | ||
.thenReturn(new SynchronousResponse<>(new ConnectorSpecification(), mock(SynchronousJobMetadata.class))); | ||
final BucketSpecCacheSchedulerClient client = new BucketSpecCacheSchedulerClient(defaultClientMock, bucketSpecFetcherMock); | ||
assertEquals(new ConnectorSpecification(), client.createGetSpecJob("source-pokeapi:0.1.0").getOutput()); | ||
} | ||
|
||
@Test | ||
void testCallsDelegateIfException() throws IOException { | ||
when(bucketSpecFetcherMock.apply("source-pokeapi:0.1.0")).thenThrow(new RuntimeException("induced exception")); | ||
when(defaultClientMock.createGetSpecJob("source-pokeapi:0.1.0")) | ||
.thenReturn(new SynchronousResponse<>(new ConnectorSpecification(), mock(SynchronousJobMetadata.class))); | ||
final BucketSpecCacheSchedulerClient client = new BucketSpecCacheSchedulerClient(defaultClientMock, bucketSpecFetcherMock); | ||
assertEquals(new ConnectorSpecification(), client.createGetSpecJob("source-pokeapi:0.1.0").getOutput()); | ||
} | ||
|
||
// todo (cgardens) - this is essentially an integration test. run it manually to sanity check that | ||
// the client can pull. from the spec cache bucket. when we have a better setup for integation | ||
// testing for the platform we should move it there. | ||
@Disabled | ||
@Test | ||
void testGetsSpecFromBucket() throws IOException { | ||
when(bucketSpecFetcherMock.apply("source-pokeapi:0.1.0")).thenReturn(Optional.of(new ConnectorSpecification())); | ||
// todo (cgardens) - replace with prod bucket. | ||
final BucketSpecCacheSchedulerClient client = new BucketSpecCacheSchedulerClient(defaultClientMock, "cg-specs"); | ||
final ConnectorSpecification actualSpec = client.createGetSpecJob("source-pokeapi:0.1.0").getOutput(); | ||
assertTrue(actualSpec.getDocumentationUrl().toString().contains("poke")); | ||
} | ||
|
||
} |
Oops, something went wrong.