Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
8 changes: 4 additions & 4 deletions hudi-utilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,22 @@
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-avro-serializer</artifactId>
<version>3.0.0</version>
<version>5.3.4</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>common-config</artifactId>
<version>3.0.0</version>
<version>5.3.4</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>common-utils</artifactId>
<version>3.0.0</version>
<version>5.3.4</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-client</artifactId>
<version>3.0.0</version>
<version>5.3.4</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@
import org.apache.spark.api.java.JavaSparkContext;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Obtains latest schema from the Confluent/Kafka schema-registry.
Expand All @@ -48,19 +54,40 @@ public static class Config {
"hoodie.deltastreamer.schemaprovider.registry.targetUrl";
}

private static String fetchSchemaFromRegistry(String registryUrl) throws IOException {
URL registry = new URL(registryUrl);
public String fetchSchemaFromRegistry(String registryUrl) throws IOException {
URL registry;
HttpURLConnection connection;
Matcher matcher = Pattern.compile("://(.*?)@").matcher(registryUrl);
if (matcher.find()) {
String creds = matcher.group(1);
String urlWithoutCreds = registryUrl.replace(creds + "@", "");
registry = new URL(urlWithoutCreds);
connection = (HttpURLConnection) registry.openConnection();
setAuthorizationHeader(matcher.group(1), connection);
} else {
registry = new URL(registryUrl);
connection = (HttpURLConnection) registry.openConnection();
}
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(registry.openStream());
JsonNode node = mapper.readTree(getStream(connection));
return node.get("schema").asText();
}

public void setAuthorizationHeader(String creds, HttpURLConnection connection) {
Copy link
Contributor

Choose a reason for hiding this comment

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

may I know why this needs to be public?

Copy link
Member

Choose a reason for hiding this comment

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

Probably because it's being used in tests. If that's the only reason then maybe we can make the method package-private?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes thats right, this is implemented like this for testing purposes in order to allow for mocking/spying of the method. I've made the method package-private.

String encodedAuth = Base64.getEncoder().encodeToString(creds.getBytes(StandardCharsets.UTF_8));
connection.setRequestProperty("Authorization", "Basic " + encodedAuth);
}

public InputStream getStream(HttpURLConnection connection) throws IOException {
return connection.getInputStream();
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need a method for one line code. also, I don't see it being used in more than 1 place.

Copy link
Contributor Author

@jhsb25 jhsb25 Jun 29, 2021

Choose a reason for hiding this comment

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

this is implemented like this for testing purposes, in order to allow for mocking/spying of the InputStream. I've made the method protected.

}

public SchemaRegistryProvider(TypedProperties props, JavaSparkContext jssc) {
super(props, jssc);
DataSourceUtils.checkRequiredProperties(props, Collections.singletonList(Config.SRC_SCHEMA_REGISTRY_URL_PROP));
}

private static Schema getSchema(String registryUrl) throws IOException {
private Schema getSchema(String registryUrl) throws IOException {
return new Schema.Parser().parse(fetchSchemaFromRegistry(registryUrl));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private IndexedRecord createExtendUserRecord() {
}

/**
* Tests {@link KafkaAvroSchemaDeserializer#deserialize(boolean, String, Boolean, byte[], Schema)}.
* Tests {@link KafkaAvroSchemaDeserializer#deserialize(Boolean, String, Boolean, byte[], Schema)}.
*/
@Test
public void testKafkaAvroSchemaDeserializer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hudi.utilities.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.avro.Schema;
import org.apache.hudi.common.config.TypedProperties;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class SchemaRegistryProviderTest {
Copy link
Member

Choose a reason for hiding this comment

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

My bad for noticing the first time around. can we rename this class to start with - Test.... to be consistent with the rest of the code base

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


private final String basicAuth = "foo:bar";

private final String json = "{\"schema\":\"{\\\"type\\\": \\\"record\\\", \\\"namespace\\\": \\\"example\\\", "
+ "\\\"name\\\": \\\"FullName\\\",\\\"fields\\\": [{ \\\"name\\\": \\\"first\\\", \\\"type\\\": "
+ "\\\"string\\\" }]}\"}";

private TypedProperties getProps() {
return new TypedProperties() {{
put("hoodie.deltastreamer.schemaprovider.registry.baseUrl", "http://" + basicAuth + "@localhost");
put("hoodie.deltastreamer.schemaprovider.registry.urlSuffix", "-value");
put("hoodie.deltastreamer.schemaprovider.registry.url", "http://foo:bar@localhost");
put("hoodie.deltastreamer.source.kafka.topic", "foo");
}};
}

Schema getExpectedSchema(String response) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
return (new Schema.Parser()).parse(node.get("schema").asText());
}

@Test
public void testGetSourceSchemaShouldRequestSchemaWithCreds() throws IOException {
InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
SchemaRegistryProvider underTest = new SchemaRegistryProvider(getProps(), null);
SchemaRegistryProvider spyUnderTest = Mockito.spy(underTest);
Mockito.doReturn(is).when(spyUnderTest).getStream(Mockito.any());
Schema actual = spyUnderTest.getSourceSchema();
assertNotNull(actual);
assertEquals(actual, getExpectedSchema(json));
verify(spyUnderTest, times(1)).setAuthorizationHeader(eq(basicAuth),
Mockito.any(HttpURLConnection.class));
}

@Test
public void testGetTargetSchemaShouldRequestSchemaWithCreds() throws IOException {
InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we re-use code across previous test and this one. except for 1 or 2 lines, mostly its same. we can create a private method w/ an argument for whether its source schema or target schema

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

SchemaRegistryProvider underTest = new SchemaRegistryProvider(getProps(), null);
SchemaRegistryProvider spyUnderTest = Mockito.spy(underTest);
Mockito.doReturn(is).when(spyUnderTest).getStream(Mockito.any());
Schema actual = spyUnderTest.getTargetSchema();
assertNotNull(actual);
assertEquals(actual, getExpectedSchema(json));
verify(spyUnderTest, times(1)).setAuthorizationHeader(eq(basicAuth),
Mockito.any(HttpURLConnection.class));
}

@Test
public void testGetSourceSchemaShouldRequestSchemaWithoutCreds() throws IOException {
TypedProperties props = getProps();
props.put("hoodie.deltastreamer.schemaprovider.registry.url", "http://localhost");
InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
SchemaRegistryProvider underTest = new SchemaRegistryProvider(props, null);
SchemaRegistryProvider spyUnderTest = Mockito.spy(underTest);
Mockito.doReturn(is).when(spyUnderTest).getStream(Mockito.any());
Schema actual = spyUnderTest.getSourceSchema();
assertNotNull(actual);
assertEquals(actual, getExpectedSchema(json));
verify(spyUnderTest, times(0)).setAuthorizationHeader(Mockito.any(), Mockito.any());
}

@Test
public void testGetTargetSchemaShouldRequestSchemaWithoutCreds() throws IOException {
TypedProperties props = getProps();
props.put("hoodie.deltastreamer.schemaprovider.registry.url", "http://localhost");
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
SchemaRegistryProvider underTest = new SchemaRegistryProvider(props, null);
SchemaRegistryProvider spyUnderTest = Mockito.spy(underTest);
Mockito.doReturn(is).when(spyUnderTest).getStream(Mockito.any());
Schema actual = spyUnderTest.getTargetSchema();
assertNotNull(actual);
assertEquals(actual, getExpectedSchema(json));
verify(spyUnderTest, times(0)).setAuthorizationHeader(Mockito.any(), Mockito.any());
}
}
8 changes: 4 additions & 4 deletions packaging/hudi-integ-test-bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -615,25 +615,25 @@
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-avro-serializer</artifactId>
<version>3.0.0</version>
<version>5.3.4</version>
</dependency>

<dependency>
<groupId>io.confluent</groupId>
<artifactId>common-config</artifactId>
<version>3.0.0</version>
<version>5.3.4</version>
Copy link
Member

Choose a reason for hiding this comment

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

Pull this version into to a property in the root Pom? It will also allow everyone to override it and build against different versions if needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

</dependency>

<dependency>
<groupId>io.confluent</groupId>
<artifactId>common-utils</artifactId>
<version>3.0.0</version>
<version>5.3.4</version>
</dependency>

<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-schema-registry-client</artifactId>
<version>3.0.0</version>
<version>5.3.4</version>
</dependency>

<dependency>
Expand Down
1 change: 1 addition & 0 deletions packaging/hudi-utilities-bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<include>com.twitter:bijection-avro_${scala.binary.version}</include>
<include>com.twitter:bijection-core_${scala.binary.version}</include>
<include>io.confluent:kafka-avro-serializer</include>
<include>io.confluent:kafka-schema-serializer</include>
<include>io.confluent:common-config</include>
<include>io.confluent:common-utils</include>
<include>io.confluent:kafka-schema-registry-client</include>
Expand Down