Search in sources :

Example 1 with ReplicaSetConfig

use of org.apache.connect.mongo.replicator.ReplicaSetConfig 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 ReplicaSetConfig

use of org.apache.connect.mongo.replicator.ReplicaSetConfig 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 ReplicaSetConfig

use of org.apache.connect.mongo.replicator.ReplicaSetConfig 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 ReplicaSetConfig

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

the class MongoSourceConnectorTest method testPoll.

@Test
public void testPoll() throws Exception {
    LinkedBlockingQueue<SourceDataEntry> entries = new LinkedBlockingQueue<>();
    ReplicaSetsContext context = new ReplicaSetsContext(sourceTaskConfig);
    Field dataEntryQueue = ReplicaSetsContext.class.getDeclaredField("dataEntryQueue");
    dataEntryQueue.setAccessible(true);
    dataEntryQueue.set(context, entries);
    ReplicationEvent event = new ReplicationEvent();
    event.setOperationType(OperationType.INSERT);
    event.setNamespace("test.person");
    event.setTimestamp(new BsonTimestamp(1565609506, 1));
    event.setDocument(new Document("testKey", "testValue"));
    event.setH(324243242L);
    event.setEventData(Optional.ofNullable(new Document("testEventKey", "testEventValue")));
    event.setObjectId(Optional.empty());
    context.publishEvent(event, new ReplicaSetConfig("", "testReplicaName", "localhost:27027"));
    List<SourceDataEntry> sourceDataEntries = (List<SourceDataEntry>) context.poll();
    Assert.assertTrue(sourceDataEntries.size() == 1);
    SourceDataEntry sourceDataEntry = sourceDataEntries.get(0);
    Assert.assertEquals("test-person", sourceDataEntry.getQueueName());
    ByteBuffer sourcePartition = sourceDataEntry.getSourcePartition();
    Assert.assertEquals("testReplicaName", new String(sourcePartition.array()));
    ByteBuffer sourcePosition = sourceDataEntry.getSourcePosition();
    Position position = JSONObject.parseObject(new String(sourcePosition.array()), Position.class);
    Assert.assertEquals(position.getTimeStamp(), 1565609506);
    Assert.assertEquals(position.getInc(), 1);
    Assert.assertEquals(position.isInitSync(), false);
    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() == 6);
    Object[] payload = sourceDataEntry.getPayload();
    Assert.assertTrue(payload.length == 6);
}
Also used : SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Position(org.apache.connect.mongo.replicator.Position) Schema(io.openmessaging.connector.api.data.Schema) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) ReplicationEvent(org.apache.connect.mongo.replicator.event.ReplicationEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Document(org.bson.Document) ByteBuffer(java.nio.ByteBuffer) BsonTimestamp(org.bson.BsonTimestamp) Field(java.lang.reflect.Field) EntryType(io.openmessaging.connector.api.data.EntryType) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) List(java.util.List) JSONObject(com.alibaba.fastjson.JSONObject) Test(org.junit.Test)

Example 5 with ReplicaSetConfig

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

the class ReplicaContextTest method testCreateMongoClient.

@Test
public void testCreateMongoClient() {
    MongoClient mongoClient = context.createMongoClient(new ReplicaSetConfig("shardName1", "", "127.0.0.1:27027"));
    MongoIterable<String> collectionNames = mongoClient.getDatabase("local").listCollectionNames();
    MongoCursor<String> iterator = collectionNames.iterator();
    while (iterator.hasNext()) {
        Assert.assertTrue(StringUtils.isNoneBlank(iterator.next()));
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) Test(org.junit.Test)

Aggregations

ReplicaSetConfig (org.apache.connect.mongo.replicator.ReplicaSetConfig)10 Test (org.junit.Test)8 ReplicaSetsContext (org.apache.connect.mongo.replicator.ReplicaSetsContext)5 Field (java.lang.reflect.Field)4 List (java.util.List)4 ReplicaSet (org.apache.connect.mongo.replicator.ReplicaSet)4 ReplicaSetManager (org.apache.connect.mongo.replicator.ReplicaSetManager)3 JSONObject (com.alibaba.fastjson.JSONObject)2 EntryType (io.openmessaging.connector.api.data.EntryType)2 Schema (io.openmessaging.connector.api.data.Schema)2 SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)2 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 Document (org.bson.Document)2 Before (org.junit.Before)2 ConnectionString (com.mongodb.ConnectionString)1 MongoClient (com.mongodb.client.MongoClient)1 ArrayList (java.util.ArrayList)1