Skip to content

Commit

Permalink
rocksdb: add WaitForCompact() (#54)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #54

RocksDB 8.7 was released with C bindings for `WaitForCompact()` call that was added on our request. Update CGO wrapper with those bindings.

Reviewed By: leoleovich

Differential Revision: D51029439

fbshipit-source-id: 7ad58bf8992a8a22b9872fb6f7bee1a1b2ad6b60
  • Loading branch information
abulimov authored and facebook-github-bot committed Nov 7, 2023
1 parent 2da5c5b commit a135ec1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-22.04
env:
CGO_LDFLAGS_ALLOW: .*
CGO_CFLAGS_ALLOW: .*
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- uses: actions/setup-go@v2
with:
go-version: 1.18
- name: "Install Rocks"
run: sudo bash .github/scripts/install_rocks_stable.sh
- name: "Install RocksDB"
run: sudo bash .github/scripts/install_rocks_head.sh
- uses: golangci/golangci-lint-action@v3
with:
working-directory: dnsrocks
Expand Down
11 changes: 11 additions & 0 deletions dnsrocks/cgo-rocksdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ func (db *RocksDB) CompactRangeAll() {
)
}

// WaitForCompact waits for all currently running compactions to finish, optionally closing the DB afterwards
func (db *RocksDB) WaitForCompact(options *WaitForCompactOptions) error {
var cError *C.char
C.rocksdb_wait_for_compact(db.cDB, options.cOptions, &cError)
if cError != nil {
defer C.rocksdb_free(unsafe.Pointer(cError))
return errors.New(C.GoString(cError))
}
return nil
}

// GetProperty returns value of db property
func (db *RocksDB) GetProperty(prop string) string {
cprop := C.CString(prop)
Expand Down
68 changes: 67 additions & 1 deletion dnsrocks/cgo-rocksdb/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const int BOOL_INT_TRUE = 1;
import (
"C"
)
import "unsafe"
import (
"time"
"unsafe"
)

// BoolToChar is a helper to convert boolean value to C.uchar
func BoolToChar(b bool) C.uchar {
Expand All @@ -37,6 +40,11 @@ func BoolToChar(b bool) C.uchar {
return C.BOOL_CHAR_FALSE
}

// CharToBool converts C.uchar to boolean
func CharToBool(cChar C.uchar) bool {
return cChar != C.BOOL_CHAR_FALSE
}

// DefaultCompactionMemtableMemoryBudget is the default for compaction
// memory usage (src/include/rocksdb/options.h).
const DefaultCompactionMemtableMemoryBudget uint64 = 512 * 1024 * 1024
Expand Down Expand Up @@ -474,3 +482,61 @@ func (readOptions *ReadOptions) SetIterateUpperBound(key []byte) {
cKeyPtr, cKeyLen := bytesToPtr(key)
C.rocksdb_readoptions_set_iterate_upper_bound(readOptions.cReadOptions, cKeyPtr, cKeyLen)
}

// WaitForCompactOptions is a set of options for WaitForCompact call
type WaitForCompactOptions struct {
cOptions *C.rocksdb_wait_for_compact_options_t
}

// NewWaitForCompactOptions creates new WaitForCompactOptions
func NewWaitForCompactOptions() *WaitForCompactOptions {
return &WaitForCompactOptions{
cOptions: C.rocksdb_wait_for_compact_options_create(),
}
}

// SetAbortOnPause sets option to abort waiting in case of a pause (PauseBackgroundWork() called, see
// https://github.com/facebook/rocksdb/blob/2648e0a747303e63796315049b9005c7320356c0/include/rocksdb/options.h#L2165
func (options *WaitForCompactOptions) SetAbortOnPause(v bool) {
C.rocksdb_wait_for_compact_options_set_abort_on_pause(options.cOptions, BoolToChar(v))
}

// GetAbortOnPause returns value of AbortOnPause option
func (options *WaitForCompactOptions) GetAbortOnPause() bool {
return CharToBool(C.rocksdb_wait_for_compact_options_get_abort_on_pause(options.cOptions))
}

// SetFlush sets option to flush all column families before starting to wait, see
// https://github.com/facebook/rocksdb/blob/2648e0a747303e63796315049b9005c7320356c0/include/rocksdb/options.h#L2165
func (options *WaitForCompactOptions) SetFlush(v bool) {
C.rocksdb_wait_for_compact_options_set_flush(options.cOptions, BoolToChar(v))
}

// GetFlush returns value of Flush option
func (options *WaitForCompactOptions) GetFlush() bool {
return CharToBool(C.rocksdb_wait_for_compact_options_get_flush(options.cOptions))
}

// SetCloseDB sets option to call Close() after waiting is done, see
// https://github.com/facebook/rocksdb/blob/2648e0a747303e63796315049b9005c7320356c0/include/rocksdb/options.h#L2165
func (options *WaitForCompactOptions) SetCloseDB(v bool) {
C.rocksdb_wait_for_compact_options_set_close_db(options.cOptions, BoolToChar(v))
}

// GetCloseDB returns value of CloseDB option
func (options *WaitForCompactOptions) GetCloseDB() bool {
return CharToBool(C.rocksdb_wait_for_compact_options_get_close_db(options.cOptions))
}

// SetTimeout sets timeout for waiting for compaction to complete.
// When timeout == 0, WaitForCompact() will wait as long as there's background work to finish.
// See https://github.com/facebook/rocksdb/blob/2648e0a747303e63796315049b9005c7320356c0/include/rocksdb/options.h#L2165
func (options *WaitForCompactOptions) SetTimeout(d time.Duration) {
C.rocksdb_wait_for_compact_options_set_timeout(options.cOptions, C.uint64_t(d.Microseconds()))
}

// GetTimeout returns value of Timeout option
func (options *WaitForCompactOptions) GetTimeout() time.Duration {
t := C.rocksdb_wait_for_compact_options_get_timeout(options.cOptions)
return time.Duration(t) * time.Microsecond
}
2 changes: 1 addition & 1 deletion dnsrocks/docs/building.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Building DNSRocks

## Prerequisites
- [Rocksdb](https://github.com/facebook/rocksdb/releases) 7.3 or newer
- [Rocksdb](https://github.com/facebook/rocksdb/releases) 8.7 or newer
- [Go 1.18](https://github.com/facebook/dns/blob/main/dnsrocks/go.mod#L3)


Expand Down

0 comments on commit a135ec1

Please sign in to comment.