Search in sources :

Example 1 with ReplicationEvent

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

the class ReplicatorTask method executorCursor.

private void executorCursor(MongoCursor<Document> cursor) {
    while (cursor.hasNext() && !replicaSet.isPause()) {
        Document document = cursor.next();
        ReplicationEvent event = Document2EventConverter.convert(document, replicaSetConfig.getReplicaSetName());
        if (replicaSetsContext.filterEvent(event)) {
            replicaSetsContext.publishEvent(event, replicaSetConfig);
        }
    }
}
Also used : ReplicationEvent(org.apache.connect.mongo.replicator.event.ReplicationEvent) Document(org.bson.Document)

Example 2 with ReplicationEvent

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

the class FilterTest method testFilterEvent.

@Test
public void testFilterEvent() {
    OperationFilter operationFilter = new OperationFilter(sourceTaskConfig);
    ReplicationEvent replicationEvent = new ReplicationEvent();
    replicationEvent.setOperationType(OperationType.NOOP);
    Assert.assertFalse(operationFilter.filterEvent(replicationEvent));
    replicationEvent.setOperationType(OperationType.DB_COMMAND);
    Assert.assertTrue(operationFilter.filterEvent(replicationEvent));
}
Also used : OperationFilter(org.apache.connect.mongo.replicator.OperationFilter) ReplicationEvent(org.apache.connect.mongo.replicator.event.ReplicationEvent) Test(org.junit.Test)

Example 3 with ReplicationEvent

use of org.apache.connect.mongo.replicator.event.ReplicationEvent 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 4 with ReplicationEvent

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

the class MongoTest method testConvertEvent.

@Test
public void testConvertEvent() {
    Document oplog = new Document();
    BsonTimestamp timestamp = new BsonTimestamp(1565074665, 10);
    oplog.put(Constants.TIMESTAMP, timestamp);
    oplog.put(Constants.NAMESPACE, "test.person");
    oplog.put(Constants.HASH, 11111L);
    oplog.put(Constants.OPERATION_TYPE, "i");
    Document document = new Document();
    document.put("test", "test");
    oplog.put(Constants.OPERATION, document);
    ReplicationEvent event = Document2EventConverter.convert(oplog, "testR");
    Assert.assertEquals(timestamp, event.getTimestamp());
    Assert.assertEquals("test.person", event.getNamespace());
    Assert.assertTrue(11111L == event.getH());
    Assert.assertEquals(OperationType.INSERT, event.getOperationType());
    Assert.assertEquals(EntryType.CREATE, event.getEntryType());
    Assert.assertEquals(document, event.getEventData().get());
    Assert.assertEquals("testR", event.getReplicaSetName());
}
Also used : ReplicationEvent(org.apache.connect.mongo.replicator.event.ReplicationEvent) Document(org.bson.Document) BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Aggregations

ReplicationEvent (org.apache.connect.mongo.replicator.event.ReplicationEvent)4 Document (org.bson.Document)3 Test (org.junit.Test)3 BsonTimestamp (org.bson.BsonTimestamp)2 JSONObject (com.alibaba.fastjson.JSONObject)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 Field (java.lang.reflect.Field)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 OperationFilter (org.apache.connect.mongo.replicator.OperationFilter)1 Position (org.apache.connect.mongo.replicator.Position)1 ReplicaSetConfig (org.apache.connect.mongo.replicator.ReplicaSetConfig)1 ReplicaSetsContext (org.apache.connect.mongo.replicator.ReplicaSetsContext)1