Search in sources :

Example 6 with ConnAndTaskConfigs

use of org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs in project rocketmq-externals by apache.

the class AllocateConnAndTaskStrategyByConsistentHashTest method testAllocate.

@Test
public void testAllocate() {
    AllocateConnAndTaskStrategyByConsistentHash strategy = new AllocateConnAndTaskStrategyByConsistentHash();
    List<String> allWorker = new ArrayList<String>() {

        {
            add("workId1");
            add("workId2");
            add("workId3");
        }
    };
    Map<String, ConnectKeyValue> connectorConfigs = new HashMap<String, ConnectKeyValue>() {

        {
            put("connectorConfig1", new ConnectKeyValue());
            put("connectorConfig2", new ConnectKeyValue());
            put("connectorConfig3", new ConnectKeyValue());
        }
    };
    List<ConnectKeyValue> connectKVs = new ArrayList<>();
    for (int i = 0; i < 6; i++) {
        ConnectKeyValue kv = new ConnectKeyValue();
        kv.put("index", i);
        connectKVs.add(kv);
    }
    List<ConnectKeyValue> taskConfig1 = new ArrayList<ConnectKeyValue>() {

        {
            add(connectKVs.get(0));
        }
    };
    List<ConnectKeyValue> taskConfig2 = new ArrayList<ConnectKeyValue>() {

        {
            add(connectKVs.get(1));
            add(connectKVs.get(2));
        }
    };
    List<ConnectKeyValue> taskConfig3 = new ArrayList<ConnectKeyValue>() {

        {
            add(connectKVs.get(3));
            add(connectKVs.get(4));
            add(connectKVs.get(5));
        }
    };
    Map<String, List<ConnectKeyValue>> taskConfigs = new HashMap<String, List<ConnectKeyValue>>() {

        {
            put("connectorConfig1", taskConfig1);
            put("connectorConfig2", taskConfig2);
            put("connectorConfig3", taskConfig3);
        }
    };
    Set<String> connectorChecks = new HashSet<>();
    Map<String, List<ConnectKeyValue>> taskChecks = new HashMap<>();
    taskConfigs.keySet().stream().forEach(key -> {
        taskChecks.put(key, new ArrayList<>());
    });
    for (String worker : allWorker) {
        ConnAndTaskConfigs allocate = strategy.allocate(allWorker, worker, connectorConfigs, taskConfigs);
        assertNotNull(allocate);
        allocate.getConnectorConfigs().keySet().forEach(key -> {
            assertFalse(connectorChecks.contains(key));
            connectorChecks.add(key);
        });
        allocate.getTaskConfigs().forEach((connectorName, allocatedTasks) -> {
            List<ConnectKeyValue> checkKVs = taskChecks.computeIfAbsent(connectorName, k -> new ArrayList<>());
            allocatedTasks.forEach(task -> {
                assertFalse(checkKVs.contains(task));
                checkKVs.add(task);
            });
        });
    }
    assertEquals(connectorConfigs.size(), connectorChecks.size());
    long cnt = taskChecks.entrySet().stream().flatMap(entry -> entry.getValue().stream()).count();
    assertEquals(6, cnt);
}
Also used : HashSet(java.util.HashSet) ConnAndTaskConfigs(org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs) List(java.util.List) ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertNotNull(org.junit.Assert.assertNotNull) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Test(org.junit.Test) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConnAndTaskConfigs(org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs) ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ConnAndTaskConfigs

use of org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs in project rocketmq-externals by apache.

the class PositionManagementServiceImplTest method init.

@Before
public void init() throws Exception {
    connectConfig = new ConnectConfig();
    connectConfig.setHttpPort(8081);
    connectConfig.setNamesrvAddr("localhost:9876");
    connectConfig.setStorePathRootDir(System.getProperty("user.home") + File.separator + "testConnectorStore");
    connectConfig.setRmqConsumerGroup("testConsumerGroup");
    doAnswer(new Answer() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Exception {
            final Message message = invocation.getArgument(0);
            byte[] bytes = message.getBody();
            final Field dataSynchronizerField = PositionManagementServiceImpl.class.getDeclaredField("dataSynchronizer");
            dataSynchronizerField.setAccessible(true);
            BrokerBasedLog<String, Map> dataSynchronizer = (BrokerBasedLog<String, Map>) dataSynchronizerField.get(positionManagementService);
            final Method decodeKeyValueMethod = BrokerBasedLog.class.getDeclaredMethod("decodeKeyValue", byte[].class);
            decodeKeyValueMethod.setAccessible(true);
            Map<String, Map> map = (Map<String, Map>) decodeKeyValueMethod.invoke(dataSynchronizer, bytes);
            final Field dataSynchronizerCallbackField = BrokerBasedLog.class.getDeclaredField("dataSynchronizerCallback");
            dataSynchronizerCallbackField.setAccessible(true);
            final DataSynchronizerCallback<String, Map> dataSynchronizerCallback = (DataSynchronizerCallback<String, Map>) dataSynchronizerCallbackField.get(dataSynchronizer);
            for (String key : map.keySet()) {
                dataSynchronizerCallback.onCompletion(null, key, map.get(key));
            }
            return null;
        }
    }).when(producer).send(any(Message.class), any(SendCallback.class));
    positionManagementService = new PositionManagementServiceImpl(connectConfig);
    final Field dataSynchronizerField = PositionManagementServiceImpl.class.getDeclaredField("dataSynchronizer");
    dataSynchronizerField.setAccessible(true);
    final Field producerField = BrokerBasedLog.class.getDeclaredField("producer");
    producerField.setAccessible(true);
    producerField.set((BrokerBasedLog<String, ConnAndTaskConfigs>) dataSynchronizerField.get(positionManagementService), producer);
    final Field consumerField = BrokerBasedLog.class.getDeclaredField("consumer");
    consumerField.setAccessible(true);
    consumerField.set((BrokerBasedLog<String, ConnAndTaskConfigs>) dataSynchronizerField.get(positionManagementService), consumer);
    positionManagementService.start();
    Field positionStoreField = PositionManagementServiceImpl.class.getDeclaredField("positionStore");
    positionStoreField.setAccessible(true);
    positionStore = (KeyValueStore<ByteBuffer, ByteBuffer>) positionStoreField.get(positionManagementService);
    Field needSyncPartitionField = PositionManagementServiceImpl.class.getDeclaredField("needSyncPartition");
    needSyncPartitionField.setAccessible(true);
    needSyncPartition = (ConcurrentSet<ByteBuffer>) needSyncPartitionField.get(positionManagementService);
    sourcePartition = ByteBuffer.wrap("127.0.0.13306".getBytes("UTF-8"));
    JSONObject jsonObject = new JSONObject();
    // jsonObject.put(MysqlConstants.BINLOG_FILENAME, "binlogFilename");
    // jsonObject.put(MysqlConstants.NEXT_POSITION, "100");
    sourcePosition = ByteBuffer.wrap(jsonObject.toJSONString().getBytes());
    positions = new HashMap<ByteBuffer, ByteBuffer>() {

        {
            put(sourcePartition, sourcePosition);
        }
    };
}
Also used : Message(org.apache.rocketmq.common.message.Message) DataSynchronizerCallback(org.apache.rocketmq.connect.runtime.utils.datasync.DataSynchronizerCallback) Method(java.lang.reflect.Method) ByteBuffer(java.nio.ByteBuffer) ConnAndTaskConfigs(org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Field(java.lang.reflect.Field) JSONObject(com.alibaba.fastjson.JSONObject) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BrokerBasedLog(org.apache.rocketmq.connect.runtime.utils.datasync.BrokerBasedLog) ConnectConfig(org.apache.rocketmq.connect.runtime.config.ConnectConfig) HashMap(java.util.HashMap) Map(java.util.Map) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Before(org.junit.Before)

Example 8 with ConnAndTaskConfigs

use of org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs in project rocketmq-externals by apache.

the class AllocateConnAndTaskStrategyByConsistentHash method allocate.

@Override
public ConnAndTaskConfigs allocate(List<String> allWorker, String curWorker, Map<String, ConnectKeyValue> connectorConfigs, Map<String, List<ConnectKeyValue>> taskConfigs) {
    ConnAndTaskConfigs allocateResult = new ConnAndTaskConfigs();
    if (null == allWorker || 0 == allWorker.size()) {
        return allocateResult;
    }
    Collection<ClientNode> cidNodes = allWorker.stream().map(ClientNode::new).collect(Collectors.toList());
    ConsistentHashRouter router = getRouter(cidNodes);
    connectorConfigs.entrySet().stream().filter(task -> curWorker.equals(router.routeNode(task.getKey()).getKey())).forEach(task -> allocateResult.getConnectorConfigs().put(task.getKey(), task.getValue()));
    for (Map.Entry<String, List<ConnectKeyValue>> connector : taskConfigs.entrySet()) {
        connector.getValue().stream().filter(kv -> curWorker.equals(router.routeNode(kv.toString()).getKey())).forEach(allocateResult.getTaskConfigs().computeIfAbsent(connector.getKey(), k -> new ArrayList<>())::add);
    }
    log.debug("allocate result: " + allocateResult);
    return allocateResult;
}
Also used : Node(org.apache.rocketmq.common.consistenthash.Node) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) RuntimeConfigDefine(org.apache.rocketmq.connect.runtime.config.RuntimeConfigDefine) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashFunction(org.apache.rocketmq.common.consistenthash.HashFunction) ConnAndTaskConfigs(org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs) List(java.util.List) ConsistentHashRouter(org.apache.rocketmq.common.consistenthash.ConsistentHashRouter) ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) Map(java.util.Map) LoggerName(org.apache.rocketmq.connect.runtime.common.LoggerName) ConsistentHashRouter(org.apache.rocketmq.common.consistenthash.ConsistentHashRouter) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ConnAndTaskConfigs(org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs)

Example 9 with ConnAndTaskConfigs

use of org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs in project rocketmq-externals by apache.

the class ConnAndTaskConfigConverter method objectToByte.

@Override
public byte[] objectToByte(ConnAndTaskConfigs object) {
    try {
        ConnAndTaskConfigs configs = object;
        Map<String, String> connectorMap = new HashMap<>();
        Map<String, String> taskMap = new HashMap<>();
        for (String key : configs.getConnectorConfigs().keySet()) {
            connectorMap.put(key, TransferUtils.keyValueToString(configs.getConnectorConfigs().get(key)));
        }
        for (String key : configs.getTaskConfigs().keySet()) {
            taskMap.put(key, TransferUtils.keyValueListToString(configs.getTaskConfigs().get(key)));
        }
        return TransferUtils.toJsonString(connectorMap, taskMap).getBytes("UTF-8");
    } catch (Exception e) {
        log.error("ConnAndTaskConfigConverter#objectToByte failed", e);
    }
    return new byte[0];
}
Also used : HashMap(java.util.HashMap) ConnAndTaskConfigs(org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with ConnAndTaskConfigs

use of org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs in project rocketmq-externals by apache.

the class ConnAndTaskConfigConverter method byteToObject.

@Override
public ConnAndTaskConfigs byteToObject(byte[] bytes) {
    try {
        String jsonString = new String(bytes, "UTF-8");
        ConnAndTaskConfigs configs = TransferUtils.toConnAndTaskConfigs(jsonString);
        return configs;
    } catch (UnsupportedEncodingException e) {
        log.error("ConnAndTaskConfigConverter#byteToObject failed", e);
    }
    return null;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConnAndTaskConfigs(org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs)

Aggregations

ConnAndTaskConfigs (org.apache.rocketmq.connect.runtime.common.ConnAndTaskConfigs)13 List (java.util.List)8 ConnectKeyValue (org.apache.rocketmq.connect.runtime.common.ConnectKeyValue)8 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Map (java.util.Map)5 Test (org.junit.Test)3 JSONObject (com.alibaba.fastjson.JSONObject)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 SendCallback (org.apache.rocketmq.client.producer.SendCallback)2 Message (org.apache.rocketmq.common.message.Message)2 ConnectConfig (org.apache.rocketmq.connect.runtime.config.ConnectConfig)2 BrokerBasedLog (org.apache.rocketmq.connect.runtime.utils.datasync.BrokerBasedLog)2 DataSynchronizerCallback (org.apache.rocketmq.connect.runtime.utils.datasync.DataSynchronizerCallback)2 Before (org.junit.Before)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2