-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feat(ksql-connect): introduce ConnectClient for REST requests #3137
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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
72 changes: 72 additions & 0 deletions
72
ksql-engine/src/main/java/io/confluent/ksql/services/ConnectClient.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,72 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.services; | ||
|
||
import io.confluent.ksql.util.KsqlPreconditions; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo; | ||
|
||
/** | ||
* An interface defining the common operations to communicate with | ||
* a Kafka Connect cluster. | ||
*/ | ||
public interface ConnectClient { | ||
|
||
/** | ||
* Creates a connector with {@code connector} as the name under the | ||
* specified configuration. | ||
* | ||
* @param connector the name of the connector | ||
* @param config the connector configuration | ||
*/ | ||
ConnectResponse<ConnectorInfo> create(String connector, Map<String, String> config); | ||
|
||
/** | ||
* An optionally successful response. Either contains a value of type | ||
* {@code <T>} or an error, which is the string representation of the | ||
* response entity. | ||
*/ | ||
class ConnectResponse<T> { | ||
private final Optional<T> datum; | ||
private final Optional<String> error; | ||
|
||
public static <T> ConnectResponse<T> of(final T datum) { | ||
return new ConnectResponse<>(datum, null); | ||
} | ||
|
||
public static <T> ConnectResponse<T> of(final String error) { | ||
return new ConnectResponse<>(null, error); | ||
} | ||
|
||
private ConnectResponse(final T datum, final String error) { | ||
KsqlPreconditions.checkArgument( | ||
datum != null ^ error != null, | ||
"expected exactly one of datum or error to be null"); | ||
this.datum = Optional.ofNullable(datum); | ||
this.error = Optional.ofNullable(error); | ||
} | ||
|
||
public Optional<T> datum() { | ||
return datum; | ||
} | ||
|
||
public Optional<String> error() { | ||
return error; | ||
} | ||
} | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
ksql-engine/src/main/java/io/confluent/ksql/services/DefaultConnectClient.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,111 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.services; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.collect.ImmutableMap; | ||
import io.confluent.ksql.json.JsonMapper; | ||
import io.confluent.ksql.util.KsqlException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.ResponseHandler; | ||
import org.apache.http.client.fluent.Request; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.util.EntityUtils; | ||
import org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The default implementation of {@code ConnectClient}. This implementation is | ||
* thread safe, and the methods are all <i>blocking</i> and are configured with | ||
* default timeouts of {@value #DEFAULT_TIMEOUT_MS}ms. | ||
*/ | ||
public class DefaultConnectClient implements ConnectClient { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DefaultConnectClient.class); | ||
private static final ObjectMapper MAPPER = JsonMapper.INSTANCE.mapper; | ||
|
||
private static final String CONNECTORS = "/connectors"; | ||
private static final int DEFAULT_TIMEOUT_MS = 5_000; | ||
|
||
private final URI connectURI; | ||
|
||
public DefaultConnectClient(final String connectURI) { | ||
Objects.requireNonNull(connectURI, "connectURI"); | ||
|
||
try { | ||
this.connectURI = new URI(connectURI); | ||
} catch (URISyntaxException e) { | ||
throw new KsqlException("Could not initialize connect client.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public ConnectResponse<ConnectorInfo> create( | ||
final String connector, | ||
final Map<String, String> config | ||
) { | ||
try { | ||
LOG.debug("Issuing request to Kafka Connect at URI {} with name {} and config {}", | ||
connectURI, | ||
connector, | ||
config); | ||
|
||
final ConnectResponse<ConnectorInfo> connectResponse = Request | ||
.Post(connectURI.resolve(CONNECTORS)) | ||
.socketTimeout(DEFAULT_TIMEOUT_MS) | ||
.connectTimeout(DEFAULT_TIMEOUT_MS) | ||
.bodyString( | ||
MAPPER.writeValueAsString( | ||
ImmutableMap.of( | ||
"name", connector, | ||
"config", config)), | ||
ContentType.APPLICATION_JSON | ||
) | ||
.execute() | ||
.handleResponse(createHandler(HttpStatus.SC_CREATED, ConnectorInfo.class)); | ||
|
||
connectResponse.error() | ||
.ifPresent(error -> LOG.warn("Did not CREATE connector {}: {}", connector, error)); | ||
|
||
return connectResponse; | ||
} catch (final Exception e) { | ||
throw new KsqlException(e); | ||
} | ||
} | ||
|
||
private static <T> ResponseHandler<ConnectResponse<T>> createHandler( | ||
final int expectedStatus, | ||
final Class<T> entityClass | ||
) { | ||
return httpResponse -> { | ||
if (httpResponse.getStatusLine().getStatusCode() != expectedStatus) { | ||
final String entity = EntityUtils.toString(httpResponse.getEntity()); | ||
return ConnectResponse.of(entity); | ||
} | ||
|
||
final T info = MAPPER.readValue( | ||
httpResponse.getEntity().getContent(), | ||
entityClass); | ||
|
||
return ConnectResponse.of(info); | ||
}; | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
ksql-engine/src/main/java/io/confluent/ksql/services/SandboxConnectClient.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,37 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.services; | ||
|
||
import static io.confluent.ksql.util.LimitedProxyBuilder.methodParams; | ||
|
||
import io.confluent.ksql.services.ConnectClient.ConnectResponse; | ||
import io.confluent.ksql.util.LimitedProxyBuilder; | ||
import java.util.Map; | ||
|
||
/** | ||
* Supplies {@link ConnectClient}s to use that do not make any | ||
* state changes to the external connect clusters. | ||
*/ | ||
final class SandboxConnectClient { | ||
|
||
private SandboxConnectClient() { } | ||
|
||
public static ConnectClient createProxy() { | ||
return LimitedProxyBuilder.forClass(ConnectClient.class) | ||
.swallow("create", methodParams(String.class, Map.class), ConnectResponse.of("sandbox")) | ||
.build(); | ||
} | ||
} |
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.
maybe define a new exception type for connect client errors that inherits from RuntimeException.
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.
I will reuse
KsqlServerException
here - if later we need a strong type for connect errors we can do that minor refactor. Don't want to unnecessarily add boilerplate.