专栏首页码匠的流水账聊聊chronos的BackupDB
原创

聊聊chronos的BackupDB

本文主要研究一下chronos的BackupDB

BackupDB

DDMQ/carrera-chronos/src/main/java/com/xiaojukeji/chronos/db/BackupDB.java

public class BackupDB {
    private static final Logger LOGGER = LogUtils.BACKUP_RESTORE_LOGGER;
​
    public static final String DB_PATH_BACKUP = ConfigManager.getConfig().getDbConfig().getDbPathBackup();
    public static final String DB_PATH_RESTORE = ConfigManager.getConfig().getDbConfig().getDbPathRestore();
    private static volatile boolean backuping = false;
    private static volatile boolean restoring = false;
​
    public static BackupState backup() {
        if (backuping) {
            LOGGER.info("is backuping, return");
            return BackupState.BEING_BACKUP;
        }
​
        LOGGER.info("start backup");
        backuping = true;
        try (final BackupableDBOptions bopt = new BackupableDBOptions(DB_PATH_BACKUP);
             final BackupEngine be = BackupEngine.open(Env.getDefault(), bopt)) {
​
            /**
             * Captures the state of the database in the latest backup
             *
             * @param db The database to backup
             * @param flushBeforeBackup When true, the Backup Engine will first issue a
             *                          memtable flush and only then copy the DB files to
             *                          the backup directory. Doing so will prevent log
             *                          files from being copied to the backup directory
             *                          (since flush will delete them).
             *                          When false, the Backup Engine will not issue a
             *                          flush before starting the backup. In that case,
             *                          the backup will also include log files
             *                          corresponding to live memtables. The backup will
             *                          always be consistent with the current state of the
             *                          database regardless of the flushBeforeBackup
             *                          parameter.
             *
             * Note - This method is not thread safe
             *
             * @throws RocksDBException thrown if a new backup could not be created
             */
            boolean flushBeforeBackup = false;
            be.createNewBackup(RDB.DB, flushBeforeBackup);
​
            List<BackupInfo> backupInfos = be.getBackupInfo();
            for (int i = 0; i < backupInfos.size(); i++) {
                LOGGER.info("backupInfo[{}}, backupId:{}, timestamp:{}, size:{}, numberFiles:{}", i, backupInfos.get(i).backupId(),
                        backupInfos.get(i).timestamp(), backupInfos.get(i).size(), backupInfos.get(i).numberFiles());
            }
​
            return BackupState.SUCCESS;
        } catch (RocksDBException e) {
            LOGGER.error("error while backup, path:{}, err:{}", DB_PATH_BACKUP, e.getMessage(), e);
            return BackupState.FAIL;
        } finally {
            backuping = false;
            LOGGER.info("end backup");
        }
    }
​
    public static RestoreState restore() throws RocksDBException {
        if (restoring) {
            LOGGER.info("is restoring, return");
            return RestoreState.BEING_RESTORE;
        }
​
        LOGGER.info("start restore");
        restoring = true;
        RocksDB restoreDB = null;
        try (final BackupableDBOptions bopt = new BackupableDBOptions(DB_PATH_BACKUP);
             final BackupEngine be = BackupEngine.open(Env.getDefault(), bopt)) {
            // restore db from first backup
​
            /**
             * @param keepLogFiles If true, restore won't overwrite the existing log files
             *   in wal_dir. It will also move all log files from archive directory to
             *   wal_dir. Use this option in combination with
             *   BackupableDBOptions::backup_log_files = false for persisting in-memory
             *   databases.
             *   Default: false
             */
            boolean keepLogFiles = false;
            be.restoreDbFromLatestBackup(DB_PATH_RESTORE, DB_PATH_RESTORE, new RestoreOptions(keepLogFiles));
            // open database again.
            restoreDB = RocksDB.open(OptionsConfig.DB_OPTIONS, DB_PATH_RESTORE, CFManager.CF_DESCRIPTORS, CFManager.CF_HANDLES);
​
            int i = 0;
            try (RocksIterator it = restoreDB.newIterator()) {
                for (it.seekToFirst(); it.isValid(); it.next()) {
                    LOGGER.info("i:{}, key:{}, value:{}", i++, new String(it.key()), new String(it.value()));
                    if (i == 10) {
                        break;
                    }
                }
            }
​
            return RestoreState.SUCCESS;
        } catch (RocksDBException e) {
            LOGGER.error("error while restore, path:{}, err:{}", DB_PATH_RESTORE, e.getMessage(), e);
            return RestoreState.FAIL;
        } finally {
            if (restoreDB != null) {
                restoreDB.close();
            }
​
            restoring = false;
            LOGGER.info("end restore");
        }
    }
}
  • BackupDB提供了两个静态方法,分别是backup及restore;backup方法通过BackupEngine的createNewBackup来进行backup;restore方法通过BackupEngine的restoreDbFromLatestBackup来进行restore

BackupEngine

rocksdbjni-5.7.2-sources.jar!/org/rocksdb/BackupEngine.java

public class BackupEngine extends RocksObject implements AutoCloseable {
​
  protected BackupEngine(final long nativeHandle) {
    super(nativeHandle);
  }
​
  public void createNewBackup(
      final RocksDB db, final boolean flushBeforeBackup)
      throws RocksDBException {
    assert (isOwningHandle());
    createNewBackup(nativeHandle_, db.nativeHandle_, flushBeforeBackup);
  }
​
  private native void createNewBackup(final long handle, final long dbHandle,
      final boolean flushBeforeBackup) throws RocksDBException;
​
​
  public void restoreDbFromLatestBackup(
      final String dbDir, final String walDir,
      final RestoreOptions restoreOptions) throws RocksDBException {
    assert (isOwningHandle());
    restoreDbFromLatestBackup(nativeHandle_, dbDir, walDir,
        restoreOptions.nativeHandle_);
  }
​
  private native void restoreDbFromLatestBackup(final long handle,
      final String dbDir, final String walDir, final long restoreOptionsHandle)
      throws RocksDBException;
​
  //......
}
  • createNewBackup方法接收RocksDB及flushBeforeBackup参数;restoreDbFromLatestBackup接收dbDir、walDir、restoreOptions参数

小结

BackupDB提供了两个静态方法,分别是backup及restore;backup方法通过BackupEngine的createNewBackup来进行backup;restore方法通过BackupEngine的restoreDbFromLatestBackup来进行restore

doc

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊HystrixEventStream

    hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/metric/HystrixEventStream.j...

    codecraft
  • 聊聊flink的RestClusterClientConfiguration

    本文主要研究一下flink的RestClusterClientConfiguration

    codecraft
  • 聊聊jpa的batch操作的实现

    hibernate-core-5.0.12.Final-sources.jar!/org/hibernate/internal/SessionImpl.java

    codecraft
  • SDN产业联盟与ONOS战略合作

    4月22日,“2015中国SDN/NFV大会”在京举行。大会上SDN产业联盟与开源组织ONOS(Open Networking Operating System...

    SDNLAB
  • 网络层【第三篇】

    路由信息协议 RIP (Routing Information Protocol) 是内部网关协议 IGP 中最先得到广泛使用的协议。

    Java3y
  • HTTP 压力测试工具 wrk

    简介 wrk 是一个HTTP压力测试工具,根据官方的介绍,他有2个明显的特点: 集成了多线程设计与事件通知系统(如 epoll、kqueue)提升并发量 可以通...

    dys
  • iFrame/Frame嵌入Silverlight时自适应高度的处理

    iFrame如果嵌入的是普通网页,这种场景下的iframe高度自适应解决方案,百度一下随手都能找到一大把。

    菩提树下的杨过
  • 英伟达刚刚发布全球最大GPU:GTC2018黄仁勋演讲核心内容都在这了

    机器之心报道 参与:李泽南、李亚洲 昨天,第九届年度 GPU 技术大会(GTC)在加州圣何塞 McEnery 会议中心正式开幕。在刚刚结束的 Keynote 演...

    机器之心
  • CES 2017 | Nvidia黄仁勋领衔:从云上的游戏电脑 到与奥迪合作真正AI车(完整视频)

    大数据文摘
  • 让你的笔记本更快一点——我的笔记本的性能测试和虚拟硬盘(把内存当成硬盘)的使用感觉

        以前是使用公司的笔记本,但是现在辞职没有本本用了,去外地的时候不太方便,于是一咬牙买了一个自己的本本(惭愧呀,人家一咬牙拿出几十万开个公司创业,而我只能...

    用户1174620

扫码关注云+社区

领取腾讯云代金券