-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ksql-connect): introduce ConnectClient for REST requests (#3137)
- Loading branch information
Showing
17 changed files
with
477 additions
and
14 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
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; | ||
} | ||
} | ||
|
||
} |
113 changes: 113 additions & 0 deletions
113
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,113 @@ | ||
/* | ||
* 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 io.confluent.ksql.util.KsqlServerException; | ||
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 due to invalid URI: " + connectURI, 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 KsqlServerException(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.