Search in sources :

Example 1 with ReplicaSet

use of org.apache.connect.mongo.replicator.ReplicaSet in project rocketmq-externals by apache.

the class MongoTest method testInitSyncCopy.

@Test
public void testInitSyncCopy() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
    MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("person");
    collection.deleteMany(new Document());
    int count = 1000;
    List<String> documents = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        Document document = new Document();
        document.put("name", "test" + i);
        document.put("age", i);
        document.put("sex", i % 2 == 0 ? "boy" : "girl");
        collection.insertOne(document);
        documents.add(document.getObjectId("_id").toHexString());
    }
    SourceTaskConfig sourceTaskConfig = new SourceTaskConfig();
    Map<String, List<String>> insterest = new HashMap<>();
    List<String> collections = new ArrayList<>();
    collections.add("*");
    insterest.put("test", collections);
    sourceTaskConfig.setInterestDbAndCollection(JSONObject.toJSONString(insterest));
    ReplicaSetConfig replicaSetConfig = new ReplicaSetConfig("", "test", "localhost");
    ReplicaSetsContext replicaSetsContext = new ReplicaSetsContext(sourceTaskConfig);
    ReplicaSet replicaSet = new ReplicaSet(replicaSetConfig, replicaSetsContext);
    Field running = ReplicaSet.class.getDeclaredField("running");
    running.setAccessible(true);
    running.set(replicaSet, new AtomicBoolean(true));
    InitSync initSync = new InitSync(replicaSetConfig, mongoClient, replicaSetsContext, replicaSet);
    initSync.start();
    int syncCount = 0;
    while (syncCount < count) {
        Collection<SourceDataEntry> sourceDataEntries = replicaSetsContext.poll();
        Assert.assertTrue(sourceDataEntries.size() > 0);
        for (SourceDataEntry sourceDataEntry : sourceDataEntries) {
            ByteBuffer sourcePartition = sourceDataEntry.getSourcePartition();
            Assert.assertEquals("test", new String(sourcePartition.array()));
            ByteBuffer sourcePosition = sourceDataEntry.getSourcePosition();
            Position position = new Position();
            position.setInitSync(true);
            position.setTimeStamp(0);
            position.setInc(0);
            Assert.assertEquals(position, JSONObject.parseObject(new String(sourcePosition.array()), Position.class));
            EntryType entryType = sourceDataEntry.getEntryType();
            Assert.assertEquals(EntryType.CREATE, entryType);
            String queueName = sourceDataEntry.getQueueName();
            Assert.assertEquals("test-person", queueName);
            Schema schema = sourceDataEntry.getSchema();
            Assert.assertTrue(schema.getFields().size() == 2);
            Object[] payload = sourceDataEntry.getPayload();
            Assert.assertTrue(payload.length == 2);
            Assert.assertEquals(payload[0].toString(), "test.person");
            Assert.assertTrue(documents.contains(JSONObject.parseObject(payload[1].toString(), Document.class).get("_id", JSONObject.class).getString("$oid")));
            syncCount++;
        }
    }
    Assert.assertTrue(syncCount == count);
}
Also used : HashMap(java.util.HashMap) Schema(io.openmessaging.connector.api.data.Schema) ArrayList(java.util.ArrayList) ConnectionString(com.mongodb.ConnectionString) Document(org.bson.Document) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) ReplicaSet(org.apache.connect.mongo.replicator.ReplicaSet) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Position(org.apache.connect.mongo.replicator.Position) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InitSync(org.apache.connect.mongo.initsync.InitSync) EntryType(io.openmessaging.connector.api.data.EntryType) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) Test(org.junit.Test)

Example 2 with ReplicaSet

use of org.apache.connect.mongo.replicator.ReplicaSet in project rocketmq-externals by apache.

the class ReplicaSetTest method before.

@Before
public void before() {
    this.sourceTaskConfig = new SourceTaskConfig();
    this.replicaSetConfig = new ReplicaSetConfig("shardName1", "", "127.0.0.1:27027");
    this.replicaSetsContext = new ReplicaSetsContext(sourceTaskConfig);
    this.replicaSet = new ReplicaSet(replicaSetConfig, replicaSetsContext);
}
Also used : ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) ReplicaSet(org.apache.connect.mongo.replicator.ReplicaSet) Before(org.junit.Before)

Example 3 with ReplicaSet

use of org.apache.connect.mongo.replicator.ReplicaSet in project rocketmq-externals by apache.

the class MongoSourceTaskTest method testEmptyContextStart.

@Test
public void testEmptyContextStart() throws NoSuchFieldException, IllegalAccessException {
    MongoSourceTask mongoSourceTask = new MongoSourceTask();
    DefaultKeyValue defaultKeyValue = new DefaultKeyValue();
    defaultKeyValue.put("mongoAddr", "test/127.0.0.1:27027");
    defaultKeyValue.put("positionTimeStamp", "11111111");
    defaultKeyValue.put("positionInc", "111");
    defaultKeyValue.put("serverSelectionTimeoutMS", "10");
    defaultKeyValue.put("dataSync", "true");
    Field context = SourceTask.class.getDeclaredField("context");
    context.setAccessible(true);
    context.set(mongoSourceTask, emptyTaskContext());
    mongoSourceTask.start(defaultKeyValue);
    Field replicaSetsContext = MongoSourceTask.class.getDeclaredField("replicaSetsContext");
    replicaSetsContext.setAccessible(true);
    ReplicaSetsContext setsContext = (ReplicaSetsContext) replicaSetsContext.get(mongoSourceTask);
    Field replicaSets = ReplicaSetsContext.class.getDeclaredField("replicaSets");
    replicaSets.setAccessible(true);
    List<ReplicaSet> replicaSetList = (List<ReplicaSet>) replicaSets.get(setsContext);
    Assert.assertTrue(replicaSetList.size() == 1);
    ReplicaSet replicaSet = replicaSetList.get(0);
    Field replicaSetConfig = ReplicaSet.class.getDeclaredField("replicaSetConfig");
    replicaSetConfig.setAccessible(true);
    ReplicaSetConfig replicaSetConfig1 = (ReplicaSetConfig) replicaSetConfig.get(replicaSet);
    Assert.assertTrue(StringUtils.equals(replicaSetConfig1.getReplicaSetName(), "test"));
    Assert.assertTrue(StringUtils.equals(replicaSetConfig1.getHost(), "127.0.0.1:27027"));
    Assert.assertTrue(replicaSetConfig1.getPosition().getTimeStamp() == 11111111);
    Assert.assertTrue(replicaSetConfig1.getPosition().getInc() == 111);
    Assert.assertTrue(replicaSetConfig1.getPosition().isInitSync());
}
Also used : DefaultKeyValue(io.openmessaging.internal.DefaultKeyValue) Field(java.lang.reflect.Field) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) List(java.util.List) ReplicaSet(org.apache.connect.mongo.replicator.ReplicaSet) MongoSourceTask(org.apache.connect.mongo.connector.MongoSourceTask) Test(org.junit.Test)

Example 4 with ReplicaSet

use of org.apache.connect.mongo.replicator.ReplicaSet in project rocketmq-externals by apache.

the class MongoSourceTask method start.

@Override
public void start(KeyValue config) {
    try {
        sourceTaskConfig = new SourceTaskConfig();
        sourceTaskConfig.load(config);
        replicaSetsContext = new ReplicaSetsContext(sourceTaskConfig);
        replicaSetManager = ReplicaSetManager.create(sourceTaskConfig.getMongoAddr());
        replicaSetManager.getReplicaConfigByName().forEach((replicaSetName, replicaSetConfig) -> {
            ByteBuffer byteBuffer = this.context.positionStorageReader().getPosition(ByteBuffer.wrap(replicaSetName.getBytes()));
            if (byteBuffer != null && byteBuffer.array().length > 0) {
                String positionJson = new String(byteBuffer.array(), StandardCharsets.UTF_8);
                Position position = JSONObject.parseObject(positionJson, Position.class);
                replicaSetConfig.setPosition(position);
            } else {
                Position position = new Position();
                position.setTimeStamp(sourceTaskConfig.getPositionTimeStamp());
                position.setInc(sourceTaskConfig.getPositionInc());
                position.setInitSync(sourceTaskConfig.isDataSync());
                replicaSetConfig.setPosition(position);
            }
            ReplicaSet replicaSet = new ReplicaSet(replicaSetConfig, replicaSetsContext);
            replicaSetsContext.addReplicaSet(replicaSet);
            replicaSet.start();
        });
    } catch (Throwable throwable) {
        logger.error("task start error", throwable);
        stop();
    }
}
Also used : Position(org.apache.connect.mongo.replicator.Position) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) SourceTaskConfig(org.apache.connect.mongo.SourceTaskConfig) ByteBuffer(java.nio.ByteBuffer) ReplicaSet(org.apache.connect.mongo.replicator.ReplicaSet)

Example 5 with ReplicaSet

use of org.apache.connect.mongo.replicator.ReplicaSet in project rocketmq-externals by apache.

the class MongoSourceTaskTest method testContextStart.

@Test
public void testContextStart() throws NoSuchFieldException, IllegalAccessException {
    MongoSourceTask mongoSourceTask = new MongoSourceTask();
    DefaultKeyValue defaultKeyValue = new DefaultKeyValue();
    defaultKeyValue.put("mongoAddr", "test/127.0.0.1:27027");
    defaultKeyValue.put("serverSelectionTimeoutMS", "10");
    Field context = SourceTask.class.getDeclaredField("context");
    context.setAccessible(true);
    context.set(mongoSourceTask, TaskContext());
    mongoSourceTask.start(defaultKeyValue);
    Field replicaSetsContext = MongoSourceTask.class.getDeclaredField("replicaSetsContext");
    replicaSetsContext.setAccessible(true);
    ReplicaSetsContext setsContext = (ReplicaSetsContext) replicaSetsContext.get(mongoSourceTask);
    Field replicaSets = ReplicaSetsContext.class.getDeclaredField("replicaSets");
    replicaSets.setAccessible(true);
    List<ReplicaSet> replicaSetList = (List<ReplicaSet>) replicaSets.get(setsContext);
    Assert.assertTrue(replicaSetList.size() == 1);
    ReplicaSet replicaSet = replicaSetList.get(0);
    Field replicaSetConfig = ReplicaSet.class.getDeclaredField("replicaSetConfig");
    replicaSetConfig.setAccessible(true);
    ReplicaSetConfig replicaSetConfig1 = (ReplicaSetConfig) replicaSetConfig.get(replicaSet);
    Assert.assertTrue(StringUtils.equals(replicaSetConfig1.getReplicaSetName(), "test"));
    Assert.assertTrue(StringUtils.equals(replicaSetConfig1.getHost(), "127.0.0.1:27027"));
    Assert.assertTrue(replicaSetConfig1.getPosition().getTimeStamp() == 22222222);
    Assert.assertTrue(replicaSetConfig1.getPosition().getInc() == 222);
    Assert.assertTrue(!replicaSetConfig1.getPosition().isInitSync());
}
Also used : DefaultKeyValue(io.openmessaging.internal.DefaultKeyValue) Field(java.lang.reflect.Field) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) List(java.util.List) ReplicaSet(org.apache.connect.mongo.replicator.ReplicaSet) MongoSourceTask(org.apache.connect.mongo.connector.MongoSourceTask) Test(org.junit.Test)

Aggregations

ReplicaSet (org.apache.connect.mongo.replicator.ReplicaSet)5 ReplicaSetsContext (org.apache.connect.mongo.replicator.ReplicaSetsContext)5 ReplicaSetConfig (org.apache.connect.mongo.replicator.ReplicaSetConfig)4 Field (java.lang.reflect.Field)3 List (java.util.List)3 Test (org.junit.Test)3 DefaultKeyValue (io.openmessaging.internal.DefaultKeyValue)2 ByteBuffer (java.nio.ByteBuffer)2 MongoSourceTask (org.apache.connect.mongo.connector.MongoSourceTask)2 Position (org.apache.connect.mongo.replicator.Position)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ConnectionString (com.mongodb.ConnectionString)1 EntryType (io.openmessaging.connector.api.data.EntryType)1 Schema (io.openmessaging.connector.api.data.Schema)1 SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SourceTaskConfig (org.apache.connect.mongo.SourceTaskConfig)1 InitSync (org.apache.connect.mongo.initsync.InitSync)1