|
| 1 | +/* |
| 2 | + * Copyright 2019 Aiven Oy |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.aiven.kafka.connect.transforms; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.ExecutionException; |
| 23 | + |
| 24 | +import org.apache.kafka.common.utils.Time; |
| 25 | +import org.apache.kafka.connect.runtime.Connect; |
| 26 | +import org.apache.kafka.connect.runtime.ConnectorConfig; |
| 27 | +import org.apache.kafka.connect.runtime.Herder; |
| 28 | +import org.apache.kafka.connect.runtime.Worker; |
| 29 | +import org.apache.kafka.connect.runtime.isolation.Plugins; |
| 30 | +import org.apache.kafka.connect.runtime.rest.RestServer; |
| 31 | +import org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo; |
| 32 | +import org.apache.kafka.connect.runtime.standalone.StandaloneConfig; |
| 33 | +import org.apache.kafka.connect.runtime.standalone.StandaloneHerder; |
| 34 | +import org.apache.kafka.connect.storage.MemoryOffsetBackingStore; |
| 35 | +import org.apache.kafka.connect.util.Callback; |
| 36 | +import org.apache.kafka.connect.util.FutureCallback; |
| 37 | + |
| 38 | +import org.slf4j.Logger; |
| 39 | +import org.slf4j.LoggerFactory; |
| 40 | + |
| 41 | +final class ConnectRunner { |
| 42 | + private static final Logger log = LoggerFactory.getLogger(ConnectRunner.class); |
| 43 | + |
| 44 | + private final File pluginDir; |
| 45 | + private final String bootstrapServers; |
| 46 | + |
| 47 | + private Herder herder; |
| 48 | + private Connect connect; |
| 49 | + |
| 50 | + public ConnectRunner(final File pluginDir, |
| 51 | + final String bootstrapServers) { |
| 52 | + this.pluginDir = pluginDir; |
| 53 | + this.bootstrapServers = bootstrapServers; |
| 54 | + } |
| 55 | + |
| 56 | + void start() { |
| 57 | + final Map<String, String> workerProps = new HashMap<>(); |
| 58 | + workerProps.put("bootstrap.servers", bootstrapServers); |
| 59 | + |
| 60 | + workerProps.put("offset.flush.interval.ms", "5000"); |
| 61 | + |
| 62 | + // These don't matter much (each connector sets its own converters), but need to be filled with valid classes. |
| 63 | + workerProps.put("key.converter", "org.apache.kafka.connect.converters.ByteArrayConverter"); |
| 64 | + workerProps.put("value.converter", "org.apache.kafka.connect.converters.ByteArrayConverter"); |
| 65 | + workerProps.put("internal.key.converter", "org.apache.kafka.connect.json.JsonConverter"); |
| 66 | + workerProps.put("internal.key.converter.schemas.enable", "false"); |
| 67 | + workerProps.put("internal.value.converter", "org.apache.kafka.connect.json.JsonConverter"); |
| 68 | + workerProps.put("internal.value.converter.schemas.enable", "false"); |
| 69 | + |
| 70 | + // Don't need it since we'll memory MemoryOffsetBackingStore. |
| 71 | + workerProps.put("offset.storage.file.filename", ""); |
| 72 | + |
| 73 | + workerProps.put("plugin.path", pluginDir.getPath()); |
| 74 | + |
| 75 | + final Time time = Time.SYSTEM; |
| 76 | + final String workerId = "test-worker"; |
| 77 | + |
| 78 | + final Plugins plugins = new Plugins(workerProps); |
| 79 | + final StandaloneConfig config = new StandaloneConfig(workerProps); |
| 80 | + |
| 81 | + final Worker worker = new Worker( |
| 82 | + workerId, time, plugins, config, new MemoryOffsetBackingStore()); |
| 83 | + herder = new StandaloneHerder(worker, "cluster-id"); |
| 84 | + |
| 85 | + final RestServer rest = new RestServer(config); |
| 86 | + |
| 87 | + connect = new Connect(herder, rest); |
| 88 | + |
| 89 | + connect.start(); |
| 90 | + } |
| 91 | + |
| 92 | + void createConnector(final Map<String, String> config) throws ExecutionException, InterruptedException { |
| 93 | + assert herder != null; |
| 94 | + |
| 95 | + final FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>( |
| 96 | + new Callback<Herder.Created<ConnectorInfo>>() { |
| 97 | + @Override |
| 98 | + public void onCompletion(final Throwable error, final Herder.Created<ConnectorInfo> info) { |
| 99 | + if (error != null) { |
| 100 | + log.error("Failed to create job"); |
| 101 | + } else { |
| 102 | + log.info("Created connector {}", info.result().name()); |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + herder.putConnectorConfig( |
| 107 | + config.get(ConnectorConfig.NAME_CONFIG), |
| 108 | + config, false, cb |
| 109 | + ); |
| 110 | + |
| 111 | + final Herder.Created<ConnectorInfo> connectorInfoCreated = cb.get(); |
| 112 | + assert connectorInfoCreated.created(); |
| 113 | + } |
| 114 | + |
| 115 | + void stop() { |
| 116 | + connect.stop(); |
| 117 | + } |
| 118 | + |
| 119 | + void awaitStop() { |
| 120 | + connect.awaitStop(); |
| 121 | + } |
| 122 | +} |
0 commit comments