0

I want to back up rocksdb while not blocking my writing calls, the status I expected is rocksdb should back up the data at that moment and ignoring the writing calls while backing up, is there a proper way to accomplish this?

I tested this in java using rocksdb-jni:

    RocksDB.loadLibrary();
    RocksDB rocksDb = RocksDB.open("C:/rocksdb/data");
    BackupEngine engine = BackupEngine.open(rocksDb.getEnv(), new BackupEngineOptions("C:/rocksdb/backup"));
    for(int i = 0;i < 100000;i++){
        byte[] k = ByteBuffer.allocate(4).putInt(0, i).array();
        rocksDb.put(k,k);
    }
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    executorService.execute(() -> {
        try {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            countDownLatch.await();
            engine.createNewBackup(rocksDb, false);
        } catch (RocksDBException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    });
    executorService.execute(() -> {
        try {
            Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
            countDownLatch.await();
            //Thread.sleep(1);
            for(int i = 100000;i < 200000;i++){
                byte[] k = ByteBuffer.allocate(4).putInt(0, i).array();
                rocksDb.put(k,k);
            }
        } catch (InterruptedException | RocksDBException e) {
            throw new RuntimeException(e);
        }
    });
    countDownLatch.countDown();

the result I expected from restoring is exactly 100000 records, but I always got more records

1 Answers1

1

Yes - both backup and checkpoint engines can achieve this i.e. take a live backup while not blocking writes

https://github.com/facebook/rocksdb/wiki/How-to-backup-RocksDB

Asad Awadia
  • 1,417
  • 2
  • 9
  • 15
  • In my tests, I put exactly 10000 entrys into the db, and then I started backing up the rocksdb and writing new kv into it, and then I restored rocksdb from backup, that's when I found it has more than 10000 entrys, that's not I wanted, I want rocksdb to backup exactly the database before I start backup and ignoring the following writes, is there a particular way to do it? –  Aug 03 '22 at 14:21
  • Can you share your test code – Asad Awadia Aug 03 '22 at 15:19
  • based on the code posted there is no guarantee that the backup started before the second for loop - and thus you are getting a few more records because the backup starts a bit after the first few iterations of the second for loop – Asad Awadia Aug 03 '22 at 17:26
  • Thank you for your answer! I used countdownlatch to rewrite my test, you are right about this, but I can't figure out how to ensure the backup is starting before I write new records into it? how to detect an one line code already running then do other tasks? I am really confused –  Aug 04 '22 at 04:21
  • Put a mutex that is locked before the backup/checkpoint starts and released after it is finished [line after backup] - and have your writes check if the mutex is available before writing – Asad Awadia Aug 04 '22 at 12:30