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);
}
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);
}
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());
}
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);
}
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()));
}
}
Aggregations