Skip to content

Commit

Permalink
Merge pull request facebook#366 from fyrz/RocksJava-Backup-Restore-Im…
Browse files Browse the repository at this point in the history
…provements

[RocksJava] - BackupInfos & Restore-/BackupableDB enhancements
  • Loading branch information
yhchiang committed Oct 28, 2014
2 parents a39e931 + f7c9730 commit c49dedb
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 14 deletions.
67 changes: 67 additions & 0 deletions java/org/rocksdb/BackupInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
package org.rocksdb;

/**
* Instances of this class describe a Backup made by
* {@link org.rocksdb.BackupableDB}.
*/
public class BackupInfo {

/**
* Package private constructor used to create instances
* of BackupInfo by {@link org.rocksdb.BackupableDB} and
* {@link org.rocksdb.RestoreBackupableDB}.
*
* @param backupId id of backup
* @param timestamp timestamp of backup
* @param size size of backup
* @param numberFiles number of files related to this backup.
*/
BackupInfo(int backupId, long timestamp, long size,
int numberFiles) {
backupId_ = backupId;
timestamp_ = timestamp;
size_ = size;
numberFiles_ = numberFiles;
}

/**
*
* @return the backup id.
*/
public int backupId() {
return backupId_;
}

/**
*
* @return the timestamp of the backup.
*/
public long timestamp() {
return timestamp_;
}

/**
*
* @return the size of the backup
*/
public long size() {
return size_;
}

/**
*
* @return the number of files of this backup.
*/
public int numberFiles() {
return numberFiles_;
}

private int backupId_;
private long timestamp_;
private long size_;
private int numberFiles_;
}
38 changes: 34 additions & 4 deletions java/org/rocksdb/BackupableDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.rocksdb;

import java.util.List;

/**
* A subclass of RocksDB which supports backup-related operations.
*
Expand Down Expand Up @@ -43,20 +45,43 @@ public static BackupableDB open(
*
* @param flushBeforeBackup if true, then all data will be flushed
* before creating backup.
* @throws org.rocksdb.RocksDBException
*/
public void createNewBackup(boolean flushBeforeBackup) {
public void createNewBackup(boolean flushBeforeBackup)
throws RocksDBException {
createNewBackup(nativeHandle_, flushBeforeBackup);
}

/**
* Deletes old backups, keeping latest numBackupsToKeep alive.
*
* @param numBackupsToKeep Number of latest backups to keep.
* @throws org.rocksdb.RocksDBException
*/
public void purgeOldBackups(int numBackupsToKeep) {
public void purgeOldBackups(int numBackupsToKeep)
throws RocksDBException {
purgeOldBackups(nativeHandle_, numBackupsToKeep);
}

/**
* Deletes a specific backup.
*
* @param backupId of backup to delete.
* @throws org.rocksdb.RocksDBException
*/
public void deleteBackup(int backupId) throws RocksDBException {
deleteBackup0(nativeHandle_, backupId);
}

/**
* Returns a list of {@link BackupInfo} instances, which describe
* already made backups.
*
* @return List of {@link BackupInfo} instances.
*/
public List<BackupInfo> getBackupInfos() {
return getBackupInfo(nativeHandle_);
}

/**
* Close the BackupableDB instance and release resource.
Expand Down Expand Up @@ -85,6 +110,11 @@ protected BackupableDB() {
}

protected native void open(long rocksDBHandle, long backupDBOptionsHandle);
protected native void createNewBackup(long handle, boolean flag);
protected native void purgeOldBackups(long handle, int numBackupsToKeep);
protected native void createNewBackup(long handle, boolean flag)
throws RocksDBException;
protected native void purgeOldBackups(long handle, int numBackupsToKeep)
throws RocksDBException;
private native void deleteBackup0(long nativeHandle, int backupId)
throws RocksDBException;
protected native List<BackupInfo> getBackupInfo(long handle);
}
29 changes: 24 additions & 5 deletions java/org/rocksdb/RestoreBackupableDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.rocksdb;

import java.util.List;

/**
* This class is used to access information about backups and restore from them.
*
Expand Down Expand Up @@ -65,6 +67,7 @@ public void restoreDBFromLatestBackup(String dbDir, String walDir,
* Deletes old backups, keeping latest numBackupsToKeep alive.
*
* @param numBackupsToKeep of latest backups to keep
* @throws org.rocksdb.RocksDBException
*/
public void purgeOldBackups(int numBackupsToKeep) throws RocksDBException {
purgeOldBackups0(nativeHandle_, numBackupsToKeep);
Expand All @@ -74,11 +77,22 @@ public void purgeOldBackups(int numBackupsToKeep) throws RocksDBException {
* Deletes a specific backup.
*
* @param backupId of backup to delete.
* @throws org.rocksdb.RocksDBException
*/
public void deleteBackup(long backupId) throws RocksDBException {
public void deleteBackup(int backupId) throws RocksDBException {
deleteBackup0(nativeHandle_, backupId);
}

/**
* Returns a list of {@link BackupInfo} instances, which describe
* already made backups.
*
* @return List of {@link BackupInfo} instances.
*/
public List<BackupInfo> getBackupInfos() {
return getBackupInfo(nativeHandle_);
}

/**
* Release the memory allocated for the current instance
* in the c++ side.
Expand All @@ -90,10 +104,15 @@ public void deleteBackup(long backupId) throws RocksDBException {

private native long newRestoreBackupableDB(long options);
private native void restoreDBFromBackup0(long nativeHandle, long backupId,
String dbDir, String walDir, long restoreOptions) throws RocksDBException;
String dbDir, String walDir, long restoreOptions)
throws RocksDBException;
private native void restoreDBFromLatestBackup0(long nativeHandle,
String dbDir, String walDir, long restoreOptions) throws RocksDBException;
private native void purgeOldBackups0(long nativeHandle, int numBackupsToKeep);
private native void deleteBackup0(long nativeHandle, long backupId);
String dbDir, String walDir, long restoreOptions)
throws RocksDBException;
private native void purgeOldBackups0(long nativeHandle, int numBackupsToKeep)
throws RocksDBException;
private native void deleteBackup0(long nativeHandle, int backupId)
throws RocksDBException;
protected native List<BackupInfo> getBackupInfo(long handle);
private native void dispose(long nativeHandle);
}
64 changes: 63 additions & 1 deletion java/org/rocksdb/test/BackupableDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.rocksdb.*;

import java.util.List;

public class BackupableDBTest {
static final String db_path = "/tmp/rocksdbjni_backupable_db_test";
static final String backup_path = "/tmp/rocksdbjni_backupable_db_backup_test";
Expand All @@ -21,14 +23,34 @@ public static void main(String[] args) {
BackupableDBOptions bopt = new BackupableDBOptions(backup_path, false,
true, false, true, 0, 0);
BackupableDB bdb = null;

List<BackupInfo> backupInfos;
List<BackupInfo> restoreInfos;
try {
bdb = BackupableDB.open(opt, bopt, db_path);

bdb.put("abc".getBytes(), "def".getBytes());
bdb.put("ghi".getBytes(), "jkl".getBytes());

backupInfos = bdb.getBackupInfos();
assert(backupInfos.size() == 0);

bdb.createNewBackup(true);

backupInfos = bdb.getBackupInfos();
assert(backupInfos.size() == 1);

// Retrieving backup infos twice shall not
// lead to different results
List<BackupInfo> tmpBackupInfo = bdb.getBackupInfos();
assert(tmpBackupInfo.get(0).backupId() ==
backupInfos.get(0).backupId());
assert(tmpBackupInfo.get(0).timestamp() ==
backupInfos.get(0).timestamp());
assert(tmpBackupInfo.get(0).size() ==
backupInfos.get(0).size());
assert(tmpBackupInfo.get(0).numberFiles() ==
backupInfos.get(0).numberFiles());

// delete record after backup
bdb.remove("abc".getBytes());
byte[] value = bdb.get("abc".getBytes());
Expand All @@ -38,8 +60,26 @@ public static void main(String[] args) {
// restore from backup
RestoreOptions ropt = new RestoreOptions(false);
RestoreBackupableDB rdb = new RestoreBackupableDB(bopt);

// getting backup infos from restorable db should
// lead to the same infos as from backupable db
restoreInfos = rdb.getBackupInfos();
assert(restoreInfos.size() == backupInfos.size());
assert(restoreInfos.get(0).backupId() ==
backupInfos.get(0).backupId());
assert(restoreInfos.get(0).timestamp() ==
backupInfos.get(0).timestamp());
assert(restoreInfos.get(0).size() ==
backupInfos.get(0).size());
assert(restoreInfos.get(0).numberFiles() ==
backupInfos.get(0).numberFiles());

rdb.restoreDBFromLatestBackup(db_path, db_path,
ropt);
// do nothing because there is only one backup
rdb.purgeOldBackups(1);
restoreInfos = rdb.getBackupInfos();
assert(restoreInfos.size() == 1);
rdb.dispose();
ropt.dispose();

Expand All @@ -48,6 +88,28 @@ public static void main(String[] args) {
value = bdb.get("abc".getBytes());
assert(new String(value).equals("def"));

bdb.createNewBackup(false);
// after new backup there must be two backup infos
backupInfos = bdb.getBackupInfos();
assert(backupInfos.size() == 2);
// deleting the backup must be possible using the
// id provided by backup infos
bdb.deleteBackup(backupInfos.get(1).backupId());
// after deletion there should only be one info
backupInfos = bdb.getBackupInfos();
assert(backupInfos.size() == 1);
bdb.createNewBackup(false);
bdb.createNewBackup(false);
bdb.createNewBackup(false);
backupInfos = bdb.getBackupInfos();
assert(backupInfos.size() == 4);
// purge everything and keep two
bdb.purgeOldBackups(2);
// backup infos need to be two
backupInfos = bdb.getBackupInfos();
assert(backupInfos.size() == 2);
assert(backupInfos.get(0).backupId() == 4);
assert(backupInfos.get(1).backupId() == 5);
System.out.println("Backup and restore test passed");
} catch (RocksDBException e) {
System.err.format("[ERROR]: %s%n", e);
Expand Down
32 changes: 31 additions & 1 deletion java/rocksjni/backupablejni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdlib.h>
#include <jni.h>
#include <string>
#include <vector>

#include "include/org_rocksdb_BackupableDB.h"
#include "include/org_rocksdb_BackupableDBOptions.h"
Expand Down Expand Up @@ -53,7 +54,7 @@ void Java_org_rocksdb_BackupableDB_createNewBackup(
* Signature: (JI)V
*/
void Java_org_rocksdb_BackupableDB_purgeOldBackups(
JNIEnv* env, jobject jbdb, jlong jhandle, jboolean jnumBackupsToKeep) {
JNIEnv* env, jobject jbdb, jlong jhandle, jint jnumBackupsToKeep) {
rocksdb::Status s =
reinterpret_cast<rocksdb::BackupableDB*>(jhandle)->
PurgeOldBackups(jnumBackupsToKeep);
Expand All @@ -62,6 +63,35 @@ void Java_org_rocksdb_BackupableDB_purgeOldBackups(
}
}

/*
* Class: org_rocksdb_BackupableDB
* Method: deleteBackup0
* Signature: (JI)V
*/
void Java_org_rocksdb_BackupableDB_deleteBackup0(JNIEnv* env,
jobject jobj, jlong jhandle, jint jbackup_id) {
auto rdb = reinterpret_cast<rocksdb::BackupableDB*>(jhandle);
rocksdb::Status s = rdb->DeleteBackup(jbackup_id);

if (!s.ok()) {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
}

/*
* Class: org_rocksdb_BackupableDB
* Method: getBackupInfo
* Signature: (J)Ljava/util/List;
*/
jobject Java_org_rocksdb_BackupableDB_getBackupInfo(
JNIEnv* env, jobject jbdb, jlong jhandle) {
std::vector<rocksdb::BackupInfo> backup_infos;
reinterpret_cast<rocksdb::BackupableDB*>(jhandle)->
GetBackupInfo(&backup_infos);
return rocksdb::BackupInfoListJni::getBackupInfo(env,
backup_infos);
}

///////////////////////////////////////////////////////////////////////////
// BackupDBOptions

Expand Down
1 change: 0 additions & 1 deletion java/rocksjni/comparatorjnicallback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ BaseComparatorJniCallback::BaseComparatorJniCallback(
const ComparatorJniCallbackOptions* copt)
: mtx_compare(new port::Mutex(copt->use_adaptive_mutex)),
mtx_findShortestSeparator(new port::Mutex(copt->use_adaptive_mutex)) {

// Note: Comparator methods may be accessed by multiple threads,
// so we ref the jvm not the env
const jint rs = env->GetJavaVM(&m_jvm);
Expand Down
Loading

0 comments on commit c49dedb

Please sign in to comment.