Search in sources :

Example 6 with CanalMetaManagerException

use of com.alibaba.otter.canal.meta.exception.CanalMetaManagerException in project canal by alibaba.

the class FileMixedLogPositionManager 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(new Function<String, File>() {

        public File apply(String destination) {
            return getDataFile(destination);
        }
    });
    executor = Executors.newScheduledThreadPool(1);
    positions = MigrateMap.makeComputingMap(new Function<String, LogPosition>() {

        public LogPosition apply(String destination) {
            LogPosition logPosition = loadDataFromFile(dataFileCaches.get(destination));
            if (logPosition == null) {
                return nullPosition;
            } else {
                return logPosition;
            }
        }
    });
    persistTasks = Collections.synchronizedSet(new HashSet<String>());
    // 启动定时工作任务
    executor.scheduleAtFixedRate(new Runnable() {

        public void run() {
            List<String> tasks = new ArrayList<String>(persistTasks);
            for (String destination : tasks) {
                try {
                    // 定时将内存中的最新值刷到file中,多次变更只刷一次
                    flushDataToFile(destination);
                    persistTasks.remove(destination);
                } catch (Throwable e) {
                    // ignore
                    logger.error("period update" + destination + " curosr failed!", e);
                }
            }
        }
    }, period, period, TimeUnit.MILLISECONDS);
}
Also used : Function(com.google.common.base.Function) CanalMetaManagerException(com.alibaba.otter.canal.meta.exception.CanalMetaManagerException) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition) HashSet(java.util.HashSet)

Aggregations

CanalMetaManagerException (com.alibaba.otter.canal.meta.exception.CanalMetaManagerException)6 ArrayList (java.util.ArrayList)4 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)3 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)3 IOException (java.io.IOException)3 Position (com.alibaba.otter.canal.protocol.position.Position)2 Function (com.google.common.base.Function)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 PositionRange (com.alibaba.otter.canal.protocol.position.PositionRange)1 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)1 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)1