Search in sources :

Example 26 with Position

use of com.alibaba.otter.canal.protocol.position.Position in project canal by alibaba.

the class MemoryEventStorePutAndGetTest method testFullPutBatchGet.

@Test
public void testFullPutBatchGet() {
    int bufferSize = 16;
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(bufferSize);
    eventStore.start();
    for (int i = 0; i < bufferSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        sleep(100L);
        Assert.assertTrue(result);
    }
    Position first = eventStore.getFirstPosition();
    Position lastest = eventStore.getLatestPosition();
    Assert.assertEquals(first, CanalEventUtils.createPosition(buildEvent("1", 1L, 1L)));
    Assert.assertEquals(lastest, CanalEventUtils.createPosition(buildEvent("1", 1L, 1L + bufferSize - 1)));
    System.out.println("start get");
    Events<Event> entrys1 = eventStore.tryGet(first, bufferSize);
    System.out.println("first get size : " + entrys1.getEvents().size());
    Assert.assertTrue(entrys1.getEvents().size() == bufferSize);
    Assert.assertEquals(first, entrys1.getPositionRange().getStart());
    Assert.assertEquals(lastest, entrys1.getPositionRange().getEnd());
    Assert.assertEquals(first, CanalEventUtils.createPosition(entrys1.getEvents().get(0)));
    Assert.assertEquals(lastest, CanalEventUtils.createPosition(entrys1.getEvents().get(bufferSize - 1)));
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) Position(com.alibaba.otter.canal.protocol.position.Position) Event(com.alibaba.otter.canal.store.model.Event) Test(org.junit.Test)

Example 27 with Position

use of com.alibaba.otter.canal.protocol.position.Position in project canal by alibaba.

the class FileMixedMetaManager method start.

public void start() {
    super.start();
    Assert.notNull(dataDir);
    if (!dataDir.exists()) {
        try {
            FileUtils.forceMkdir(dataDir);
        } catch (IOException e) {
            throw new CanalMetaManagerException(e);
        }
    }
    if (!dataDir.canRead() || !dataDir.canWrite()) {
        throw new CanalMetaManagerException("dir[" + dataDir.getPath() + "] can not read/write");
    }
    dataFileCaches = MigrateMap.makeComputingMap(this::getDataFile);
    executor = Executors.newScheduledThreadPool(1);
    destinations = MigrateMap.makeComputingMap(this::loadClientIdentity);
    cursors = MigrateMap.makeComputingMap(clientIdentity -> {
        Position position = loadCursor(clientIdentity.getDestination(), clientIdentity);
        if (position == null) {
            // 返回一个空对象标识,避免出现异常
            return nullCursor;
        } else {
            return position;
        }
    });
    updateCursorTasks = Collections.synchronizedSet(new HashSet<>());
    // 启动定时工作任务
    executor.scheduleAtFixedRate(() -> {
        List<ClientIdentity> tasks = new ArrayList<>(updateCursorTasks);
        for (ClientIdentity clientIdentity : tasks) {
            MDC.put("destination", String.valueOf(clientIdentity.getDestination()));
            try {
                updateCursorTasks.remove(clientIdentity);
                // 定时将内存中的最新值刷到file中,多次变更只刷一次
                if (logger.isInfoEnabled()) {
                    LogPosition cursor = (LogPosition) getCursor(clientIdentity);
                    logger.info("clientId:{} cursor:[{},{},{},{},{}] address[{}]", clientIdentity.getClientId(), cursor.getPostion().getJournalName(), cursor.getPostion().getPosition(), cursor.getPostion().getTimestamp(), cursor.getPostion().getServerId(), cursor.getPostion().getGtid(), cursor.getIdentity().getSourceAddress().toString());
                }
                flushDataToFile(clientIdentity.getDestination());
            } catch (Throwable e) {
                // ignore
                logger.error("period update" + clientIdentity.toString() + " curosr failed!", e);
            }
        }
    }, period, period, TimeUnit.MILLISECONDS);
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) JsonUtils(com.alibaba.otter.canal.common.utils.JsonUtils) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CanalMetaManagerException(com.alibaba.otter.canal.meta.exception.CanalMetaManagerException) Position(com.alibaba.otter.canal.protocol.position.Position) Logger(org.slf4j.Logger) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) MigrateMap(com.google.common.collect.MigrateMap) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) MDC(org.slf4j.MDC) Collections(java.util.Collections) Assert(org.springframework.util.Assert) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) Position(com.alibaba.otter.canal.protocol.position.Position) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition) ArrayList(java.util.ArrayList) CanalMetaManagerException(com.alibaba.otter.canal.meta.exception.CanalMetaManagerException) IOException(java.io.IOException) HashSet(java.util.HashSet) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition)

Example 28 with Position

use of com.alibaba.otter.canal.protocol.position.Position in project canal by alibaba.

the class AbstractMetaManagerTest method doCursorTest.

public Position doCursorTest(CanalMetaManager metaManager) {
    metaManager.subscribe(clientIdentity);
    Position position1 = metaManager.getCursor(clientIdentity);
    Assert.assertNull(position1);
    PositionRange range = buildRange(1);
    metaManager.updateCursor(clientIdentity, range.getStart());
    Position position2 = metaManager.getCursor(clientIdentity);
    Assert.assertEquals(range.getStart(), position2);
    metaManager.updateCursor(clientIdentity, range.getEnd());
    Position position3 = metaManager.getCursor(clientIdentity);
    Assert.assertEquals(range.getEnd(), position3);
    return position3;
}
Also used : Position(com.alibaba.otter.canal.protocol.position.Position) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition) EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition) PositionRange(com.alibaba.otter.canal.protocol.position.PositionRange)

Example 29 with Position

use of com.alibaba.otter.canal.protocol.position.Position in project canal by alibaba.

the class FileMixedMetaManagerTest method testCursorAll.

@Test
public void testCursorAll() {
    FileMixedMetaManager metaManager = new FileMixedMetaManager();
    metaManager.setDataDirByFile(dataDir);
    metaManager.setPeriod(100);
    metaManager.start();
    Position lastPosition = doCursorTest(metaManager);
    sleep(1000L);
    // 重新构建一次,能获得上一次zk上的记录
    FileMixedMetaManager metaManager2 = new FileMixedMetaManager();
    metaManager2.setDataDirByFile(dataDir);
    metaManager2.setPeriod(100);
    metaManager2.start();
    Position position = metaManager2.getCursor(clientIdentity);
    Assert.assertEquals(position, lastPosition);
    metaManager.stop();
}
Also used : Position(com.alibaba.otter.canal.protocol.position.Position) Test(org.junit.Test)

Example 30 with Position

use of com.alibaba.otter.canal.protocol.position.Position in project canal by alibaba.

the class MixedMetaManagerTest method testCursorAll.

@Test
public void testCursorAll() {
    MixedMetaManager metaManager = new MixedMetaManager();
    ZooKeeperMetaManager zooKeeperMetaManager = new ZooKeeperMetaManager();
    zooKeeperMetaManager.setZkClientx(zkclientx);
    metaManager.setZooKeeperMetaManager(zooKeeperMetaManager);
    metaManager.start();
    Position lastPosition = doCursorTest(metaManager);
    sleep(1000L);
    // 重新构建一次,能获得上一次zk上的记录
    MixedMetaManager metaManager2 = new MixedMetaManager();
    metaManager2.setZooKeeperMetaManager(zooKeeperMetaManager);
    metaManager2.start();
    Position position = metaManager2.getCursor(clientIdentity);
    Assert.assertEquals(position, lastPosition);
    metaManager.stop();
}
Also used : Position(com.alibaba.otter.canal.protocol.position.Position) Test(org.junit.Test)

Aggregations

Position (com.alibaba.otter.canal.protocol.position.Position)32 Test (org.junit.Test)23 Event (com.alibaba.otter.canal.store.model.Event)22 MemoryEventStoreWithBuffer (com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer)20 ArrayList (java.util.ArrayList)13 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)7 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)4 CanalStoreException (com.alibaba.otter.canal.store.CanalStoreException)4 List (java.util.List)4 ExecutorService (java.util.concurrent.ExecutorService)4 CanalInstance (com.alibaba.otter.canal.instance.core.CanalInstance)3 CanalMetaManagerException (com.alibaba.otter.canal.meta.exception.CanalMetaManagerException)3 PositionRange (com.alibaba.otter.canal.protocol.position.PositionRange)3 MigrateMap (com.google.common.collect.MigrateMap)3 Map (java.util.Map)3 Message (com.alibaba.otter.canal.protocol.Message)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2