Search in sources :

Example 1 with DefaultConnectorContext

use of org.apache.rocketmq.connect.runtime.service.DefaultConnectorContext in project rocketmq-externals by apache.

the class RestHandlerTest method init.

@Before
public void init() throws Exception {
    workerState = new AtomicReference<>(WorkerState.STARTED);
    when(connectController.getConnectConfig()).thenReturn(connectConfig);
    when(connectConfig.getHttpPort()).thenReturn(8081);
    when(connectController.getConfigManagementService()).thenReturn(configManagementService);
    when(configManagementService.putConnectorConfig(anyString(), any(ConnectKeyValue.class))).thenReturn("");
    String connectName = "testConnector";
    ConnectKeyValue connectKeyValue = new ConnectKeyValue();
    connectKeyValue.put(RuntimeConfigDefine.CONNECTOR_CLASS, "org.apache.rocketmq.connect.runtime.service.TestConnector");
    connectKeyValue.put(RuntimeConfigDefine.SOURCE_RECORD_CONVERTER, "source-record-converter");
    ConnectKeyValue connectKeyValue1 = new ConnectKeyValue();
    connectKeyValue1.put(RuntimeConfigDefine.CONNECTOR_CLASS, "org.apache.rocketmq.connect.runtime.service.TestConnector");
    connectKeyValue1.put(RuntimeConfigDefine.SOURCE_RECORD_CONVERTER, "source-record-converter1");
    List<ConnectKeyValue> connectKeyValues = new ArrayList<ConnectKeyValue>(8) {

        {
            add(connectKeyValue);
        }
    };
    connectorConfigs = new HashMap<String, ConnectKeyValue>() {

        {
            put(connectName, connectKeyValue);
        }
    };
    taskConfigs = new HashMap<String, List<ConnectKeyValue>>() {

        {
            put(connectName, connectKeyValues);
        }
    };
    when(configManagementService.getConnectorConfigs()).thenReturn(connectorConfigs);
    when(configManagementService.getTaskConfigs()).thenReturn(taskConfigs);
    aliveWorker = new ArrayList<String>() {

        {
            add("workerId1");
            add("workerId2");
        }
    };
    when(connectController.getClusterManagementService()).thenReturn(clusterManagementService);
    when(clusterManagementService.getAllAliveWorkers()).thenReturn(aliveWorker);
    sourcePartition = "127.0.0.13306".getBytes("UTF-8");
    JSONObject jsonObject = new JSONObject();
    // jsonObject.put(MysqlConstants.BINLOG_FILENAME, "binlogFilename");
    // jsonObject.put(MysqlConstants.NEXT_POSITION, "100");
    sourcePosition = jsonObject.toJSONString().getBytes();
    positions = new HashMap<ByteBuffer, ByteBuffer>() {

        {
            put(ByteBuffer.wrap(sourcePartition), ByteBuffer.wrap(sourcePosition));
        }
    };
    WorkerConnector workerConnector1 = new WorkerConnector("testConnectorName1", connector, connectKeyValue, new DefaultConnectorContext("testConnectorName1", connectController));
    WorkerConnector workerConnector2 = new WorkerConnector("testConnectorName2", connector, connectKeyValue1, new DefaultConnectorContext("testConnectorName2", connectController));
    workerConnectors = new HashSet<WorkerConnector>() {

        {
            add(workerConnector1);
            add(workerConnector2);
        }
    };
    WorkerSourceTask workerSourceTask1 = new WorkerSourceTask("testConnectorName1", sourceTask, connectKeyValue, positionManagementServiceImpl, converter, producer, workerState);
    WorkerSourceTask workerSourceTask2 = new WorkerSourceTask("testConnectorName2", sourceTask, connectKeyValue1, positionManagementServiceImpl, converter, producer, workerState);
    workerTasks = new HashSet<Runnable>() {

        {
            add(workerSourceTask1);
            add(workerSourceTask2);
        }
    };
    when(connectController.getWorker()).thenReturn(worker);
    when(worker.getWorkingConnectors()).thenReturn(workerConnectors);
    when(worker.getWorkingTasks()).thenReturn(workerTasks);
    restHandler = new RestHandler(connectController);
    httpClient = HttpClientBuilder.create().build();
}
Also used : WorkerConnector(org.apache.rocketmq.connect.runtime.connectorwrapper.WorkerConnector) WorkerSourceTask(org.apache.rocketmq.connect.runtime.connectorwrapper.WorkerSourceTask) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString) ByteBuffer(java.nio.ByteBuffer) ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) DefaultConnectorContext(org.apache.rocketmq.connect.runtime.service.DefaultConnectorContext) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Before(org.junit.Before)

Example 2 with DefaultConnectorContext

use of org.apache.rocketmq.connect.runtime.service.DefaultConnectorContext in project rocketmq-externals by apache.

the class Worker method startConnectors.

/**
 * Start a collection of connectors with the given configs. If a connector is already started with the same configs,
 * it will not start again. If a connector is already started but not contained in the new configs, it will stop.
 *
 * @param connectorConfigs
 * @param connectController
 * @throws Exception
 */
public synchronized void startConnectors(Map<String, ConnectKeyValue> connectorConfigs, ConnectController connectController) throws Exception {
    Set<WorkerConnector> stoppedConnector = new HashSet<>();
    for (WorkerConnector workerConnector : workingConnectors) {
        String connectorName = workerConnector.getConnectorName();
        ConnectKeyValue keyValue = connectorConfigs.get(connectorName);
        if (null == keyValue || 0 != keyValue.getInt(RuntimeConfigDefine.CONFIG_DELETED)) {
            workerConnector.stop();
            log.info("Connector {} stop", workerConnector.getConnectorName());
            stoppedConnector.add(workerConnector);
        } else if (!keyValue.equals(workerConnector.getKeyValue())) {
            workerConnector.reconfigure(keyValue);
        }
    }
    workingConnectors.removeAll(stoppedConnector);
    if (null == connectorConfigs || 0 == connectorConfigs.size()) {
        return;
    }
    Map<String, ConnectKeyValue> newConnectors = new HashMap<>();
    for (String connectorName : connectorConfigs.keySet()) {
        boolean isNewConnector = true;
        for (WorkerConnector workerConnector : workingConnectors) {
            if (workerConnector.getConnectorName().equals(connectorName)) {
                isNewConnector = false;
                break;
            }
        }
        if (isNewConnector) {
            newConnectors.put(connectorName, connectorConfigs.get(connectorName));
        }
    }
    for (String connectorName : newConnectors.keySet()) {
        ConnectKeyValue keyValue = newConnectors.get(connectorName);
        String connectorClass = keyValue.getString(RuntimeConfigDefine.CONNECTOR_CLASS);
        ClassLoader loader = plugin.getPluginClassLoader(connectorClass);
        final ClassLoader currentThreadLoader = plugin.currentThreadLoader();
        Class clazz;
        boolean isolationFlag = false;
        if (loader instanceof PluginClassLoader) {
            clazz = ((PluginClassLoader) loader).loadClass(connectorClass, false);
            isolationFlag = true;
        } else {
            clazz = Class.forName(connectorClass);
        }
        final Connector connector = (Connector) clazz.getDeclaredConstructor().newInstance();
        WorkerConnector workerConnector = new WorkerConnector(connectorName, connector, connectorConfigs.get(connectorName), new DefaultConnectorContext(connectorName, connectController));
        if (isolationFlag) {
            Plugin.compareAndSwapLoaders(loader);
        }
        workerConnector.initialize();
        workerConnector.start();
        log.info("Connector {} start", workerConnector.getConnectorName());
        Plugin.compareAndSwapLoaders(currentThreadLoader);
        this.workingConnectors.add(workerConnector);
    }
}
Also used : Connector(io.openmessaging.connector.api.Connector) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) DefaultConnectorContext(org.apache.rocketmq.connect.runtime.service.DefaultConnectorContext) PluginClassLoader(org.apache.rocketmq.connect.runtime.utils.PluginClassLoader) HashSet(java.util.HashSet) PluginClassLoader(org.apache.rocketmq.connect.runtime.utils.PluginClassLoader)

Aggregations

ConnectKeyValue (org.apache.rocketmq.connect.runtime.common.ConnectKeyValue)2 DefaultConnectorContext (org.apache.rocketmq.connect.runtime.service.DefaultConnectorContext)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Connector (io.openmessaging.connector.api.Connector)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 WorkerConnector (org.apache.rocketmq.connect.runtime.connectorwrapper.WorkerConnector)1 WorkerSourceTask (org.apache.rocketmq.connect.runtime.connectorwrapper.WorkerSourceTask)1 PluginClassLoader (org.apache.rocketmq.connect.runtime.utils.PluginClassLoader)1 Before (org.junit.Before)1 Mockito.anyString (org.mockito.Mockito.anyString)1