Skip to content

Commit 231588e

Browse files
authored
Adapt RocksDB 9.1.1 (#151)
1 parent 205af2d commit 231588e

10 files changed

+138
-30
lines changed

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ cd $BUILD_PATH && wget https://github.com/facebook/zstd/archive/v${zstd_version}
3737

3838
# Note: if you don't have a good reason, please do not set -DPORTABLE=ON
3939
# This one is set here on purpose of compatibility with github action runtime processor
40-
rocksdb_version="8.11.4"
40+
rocksdb_version="9.1.1"
4141
cd $BUILD_PATH && wget https://github.com/facebook/rocksdb/archive/v${rocksdb_version}.tar.gz && tar xzf v${rocksdb_version}.tar.gz && cd rocksdb-${rocksdb_version}/ && \
4242
mkdir -p build_place && cd build_place && cmake -DCMAKE_BUILD_TYPE=Release $CMAKE_REQUIRED_PARAMS -DCMAKE_PREFIX_PATH=$INSTALL_PREFIX -DWITH_TESTS=OFF -DWITH_GFLAGS=OFF \
4343
-DWITH_BENCHMARK_TOOLS=OFF -DWITH_TOOLS=OFF -DWITH_MD_LIBRARY=OFF -DWITH_RUNTIME_DEBUG=OFF -DROCKSDB_BUILD_SHARED=OFF -DWITH_SNAPPY=ON -DWITH_LZ4=ON -DWITH_ZLIB=ON -DWITH_LIBURING=OFF \

c.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,9 @@ extern ROCKSDB_LIBRARY_API const rocksdb_snapshot_t* rocksdb_create_snapshot(
608608
extern ROCKSDB_LIBRARY_API void rocksdb_release_snapshot(
609609
rocksdb_t* db, const rocksdb_snapshot_t* snapshot);
610610

611+
extern ROCKSDB_LIBRARY_API uint64_t
612+
rocksdb_snapshot_get_sequence_number(const rocksdb_snapshot_t* snapshot);
613+
611614
/* Returns NULL if property name is unknown.
612615
Else returns a pointer to a malloc()-ed null-terminated value. */
613616
extern ROCKSDB_LIBRARY_API char* rocksdb_property_value(rocksdb_t* db,
@@ -691,8 +694,8 @@ extern ROCKSDB_LIBRARY_API void rocksdb_flush_wal(rocksdb_t* db,
691694
extern ROCKSDB_LIBRARY_API void rocksdb_disable_file_deletions(rocksdb_t* db,
692695
char** errptr);
693696

694-
extern ROCKSDB_LIBRARY_API void rocksdb_enable_file_deletions(
695-
rocksdb_t* db, unsigned char force, char** errptr);
697+
extern ROCKSDB_LIBRARY_API void rocksdb_enable_file_deletions(rocksdb_t* db,
698+
char** errptr);
696699

697700
/* Management operations */
698701

@@ -1152,10 +1155,16 @@ extern ROCKSDB_LIBRARY_API void rocksdb_options_set_env(rocksdb_options_t*,
11521155
rocksdb_env_t*);
11531156
extern ROCKSDB_LIBRARY_API void rocksdb_options_set_info_log(rocksdb_options_t*,
11541157
rocksdb_logger_t*);
1158+
extern ROCKSDB_LIBRARY_API rocksdb_logger_t* rocksdb_options_get_info_log(
1159+
rocksdb_options_t* opt);
11551160
extern ROCKSDB_LIBRARY_API void rocksdb_options_set_info_log_level(
11561161
rocksdb_options_t*, int);
11571162
extern ROCKSDB_LIBRARY_API int rocksdb_options_get_info_log_level(
11581163
rocksdb_options_t*);
1164+
extern ROCKSDB_LIBRARY_API rocksdb_logger_t*
1165+
rocksdb_logger_create_stderr_logger(int log_level, const char* prefix);
1166+
extern ROCKSDB_LIBRARY_API void rocksdb_logger_destroy(
1167+
rocksdb_logger_t* logger);
11591168
extern ROCKSDB_LIBRARY_API void rocksdb_options_set_write_buffer_size(
11601169
rocksdb_options_t*, size_t);
11611170
extern ROCKSDB_LIBRARY_API size_t
@@ -1499,10 +1508,6 @@ extern ROCKSDB_LIBRARY_API void rocksdb_options_set_advise_random_on_open(
14991508
rocksdb_options_t*, unsigned char);
15001509
extern ROCKSDB_LIBRARY_API unsigned char
15011510
rocksdb_options_get_advise_random_on_open(rocksdb_options_t*);
1502-
extern ROCKSDB_LIBRARY_API void
1503-
rocksdb_options_set_access_hint_on_compaction_start(rocksdb_options_t*, int);
1504-
extern ROCKSDB_LIBRARY_API int
1505-
rocksdb_options_get_access_hint_on_compaction_start(rocksdb_options_t*);
15061511
extern ROCKSDB_LIBRARY_API void rocksdb_options_set_use_adaptive_mutex(
15071512
rocksdb_options_t*, unsigned char);
15081513
extern ROCKSDB_LIBRARY_API unsigned char rocksdb_options_get_use_adaptive_mutex(
@@ -1684,6 +1689,10 @@ extern ROCKSDB_LIBRARY_API rocksdb_ratelimiter_t*
16841689
rocksdb_ratelimiter_create_auto_tuned(int64_t rate_bytes_per_sec,
16851690
int64_t refill_period_us,
16861691
int32_t fairness);
1692+
extern ROCKSDB_LIBRARY_API rocksdb_ratelimiter_t*
1693+
rocksdb_ratelimiter_create_with_mode(int64_t rate_bytes_per_sec,
1694+
int64_t refill_period_us, int32_t fairness,
1695+
int mode, bool auto_tuned);
16871696
extern ROCKSDB_LIBRARY_API void rocksdb_ratelimiter_destroy(
16881697
rocksdb_ratelimiter_t*);
16891698

cf_ts_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestColumnFamilyMultiGetWithTS(t *testing.T) {
112112
givenVal1 = []byte("world1")
113113
givenVal2 = []byte("world2")
114114
givenVal3 = []byte("world3")
115-
givenTs1 = marshalTimestamp(1)
115+
givenTs1 = marshalTimestamp(0)
116116
givenTs2 = marshalTimestamp(2)
117117
givenTs3 = marshalTimestamp(3)
118118
)
@@ -177,7 +177,7 @@ func TestColumnFamilyMultiGetWithTS(t *testing.T) {
177177
require.EqualValues(t, values[1].Data(), givenVal2)
178178
require.EqualValues(t, values[2].Data(), givenVal3)
179179

180-
require.EqualValues(t, times[0].Data(), givenTs1)
180+
require.EqualValues(t, times[0].Data(), []byte{})
181181
require.EqualValues(t, times[1].Data(), givenTs2)
182182
require.EqualValues(t, times[2].Data(), givenTs3)
183183
}

db.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ func (db *DB) SingleDeleteCFWithTS(opts *WriteOptions, cf *ColumnFamilyHandle, k
10171017
}
10181018

10191019
// DeleteRangeCF deletes keys that are between [startKey, endKey)
1020-
func (db *DB) DeleteRangeCF(opts *WriteOptions, cf *ColumnFamilyHandle, startKey []byte, endKey []byte) (err error) {
1020+
func (db *DB) DeleteRangeCF(opts *WriteOptions, cf *ColumnFamilyHandle, startKey, endKey []byte) (err error) {
10211021
var (
10221022
cErr *C.char
10231023
cStartKey = refGoBytes(startKey)
@@ -1087,7 +1087,7 @@ func (db *DB) SingleDeleteCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []b
10871087
}
10881088

10891089
// Merge merges the data associated with the key with the actual data in the database.
1090-
func (db *DB) Merge(opts *WriteOptions, key []byte, value []byte) (err error) {
1090+
func (db *DB) Merge(opts *WriteOptions, key, value []byte) (err error) {
10911091
var (
10921092
cErr *C.char
10931093
cKey = refGoBytes(key)
@@ -1102,7 +1102,7 @@ func (db *DB) Merge(opts *WriteOptions, key []byte, value []byte) (err error) {
11021102

11031103
// MergeCF merges the data associated with the key with the actual data in the
11041104
// database and column family.
1105-
func (db *DB) MergeCF(opts *WriteOptions, cf *ColumnFamilyHandle, key []byte, value []byte) (err error) {
1105+
func (db *DB) MergeCF(opts *WriteOptions, cf *ColumnFamilyHandle, key, value []byte) (err error) {
11061106
var (
11071107
cErr *C.char
11081108
cKey = refGoBytes(key)
@@ -1662,10 +1662,10 @@ func (db *DB) DisableFileDeletions() (err error) {
16621662
}
16631663

16641664
// EnableFileDeletions enables file deletions for the database.
1665-
func (db *DB) EnableFileDeletions(force bool) (err error) {
1665+
func (db *DB) EnableFileDeletions() (err error) {
16661666
var cErr *C.char
16671667

1668-
C.rocksdb_enable_file_deletions(db.c, boolToChar(force), &cErr)
1668+
C.rocksdb_enable_file_deletions(db.c, &cErr)
16691669
err = fromCError(cErr)
16701670

16711671
return

db_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ func newTestDBMultiCF(t *testing.T, columns []string, applyOpts func(opts *Optio
222222
dir := t.TempDir()
223223

224224
opts := NewDefaultOptions()
225+
rateLimiter := NewGenericRateLimiter(1024, 100*1000, 10, RateLimiterModeAllIo, true)
226+
opts.SetRateLimiter(rateLimiter)
225227
opts.SetCreateIfMissingColumnFamilies(true)
226228
opts.SetCreateIfMissing(true)
227229
opts.SetCompression(ZSTDCompression)

logger.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package grocksdb
2+
3+
// #include "rocksdb/c.h"
4+
// #include "grocksdb.h"
5+
import "C"
6+
import "unsafe"
7+
8+
// Logger struct.
9+
type Logger struct {
10+
c *C.rocksdb_logger_t
11+
}
12+
13+
func NewStderrLogger(level InfoLogLevel, prefix string) *Logger {
14+
prefix_ := C.CString(prefix)
15+
defer C.free(unsafe.Pointer(prefix_))
16+
17+
return &Logger{
18+
c: C.rocksdb_logger_create_stderr_logger(C.int(level), prefix_),
19+
}
20+
}
21+
22+
// Destroy Logger.
23+
func (l *Logger) Destroy() {
24+
C.rocksdb_logger_destroy(l.c)
25+
l.c = nil
26+
}

options.go

+27-14
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,18 @@ func (opts *Options) SetEnv(env *Env) {
353353
env.c = nil
354354
}
355355

356+
// SetInfoLog sets info logger.
357+
func (opts *Options) SetInfoLog(logger *Logger) {
358+
C.rocksdb_options_set_info_log(opts.c, logger.c)
359+
}
360+
361+
// GetInfoLog gets info logger.
362+
func (opts *Options) GetInfoLog() *Logger {
363+
return &Logger{
364+
c: C.rocksdb_options_get_info_log(opts.c),
365+
}
366+
}
367+
356368
// SetInfoLogLevel sets the info log level.
357369
//
358370
// Default: InfoInfoLogLevel
@@ -1451,20 +1463,21 @@ func (opts *Options) GetDbWriteBufferSize() uint64 {
14511463
return uint64(C.rocksdb_options_get_db_write_buffer_size(opts.c))
14521464
}
14531465

1454-
// SetAccessHintOnCompactionStart specifies the file access pattern
1455-
// once a compaction is started.
1456-
//
1457-
// It will be applied to all input files of a compaction.
1458-
// Default: NormalCompactionAccessPattern
1459-
func (opts *Options) SetAccessHintOnCompactionStart(value CompactionAccessPattern) {
1460-
C.rocksdb_options_set_access_hint_on_compaction_start(opts.c, C.int(value))
1461-
}
1462-
1463-
// GetAccessHintOnCompactionStart returns the file access pattern
1464-
// once a compaction is started.
1465-
func (opts *Options) GetAccessHintOnCompactionStart() CompactionAccessPattern {
1466-
return CompactionAccessPattern(C.rocksdb_options_get_access_hint_on_compaction_start(opts.c))
1467-
}
1466+
// Deprecation in rocksdb v9.x
1467+
// // SetAccessHintOnCompactionStart specifies the file access pattern
1468+
// // once a compaction is started.
1469+
// //
1470+
// // It will be applied to all input files of a compaction.
1471+
// // Default: NormalCompactionAccessPattern
1472+
// func (opts *Options) SetAccessHintOnCompactionStart(value CompactionAccessPattern) {
1473+
// C.rocksdb_options_set_access_hint_on_compaction_start(opts.c, C.int(value))
1474+
// }
1475+
//
1476+
// // GetAccessHintOnCompactionStart returns the file access pattern
1477+
// // once a compaction is started.
1478+
// func (opts *Options) GetAccessHintOnCompactionStart() CompactionAccessPattern {
1479+
// return CompactionAccessPattern(C.rocksdb_options_get_access_hint_on_compaction_start(opts.c))
1480+
// }
14681481

14691482
// SetUseAdaptiveMutex enable/disable adaptive mutex, which spins
14701483
// in the user space before resorting to kernel.

options_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ func TestOptions(t *testing.T) {
101101
opts.SetAdviseRandomOnOpen(true)
102102
require.EqualValues(t, true, opts.AdviseRandomOnOpen())
103103

104-
opts.SetAccessHintOnCompactionStart(SequentialCompactionAccessPattern)
105-
require.EqualValues(t, SequentialCompactionAccessPattern, opts.GetAccessHintOnCompactionStart())
104+
// opts.SetAccessHintOnCompactionStart(SequentialCompactionAccessPattern)
105+
// require.EqualValues(t, SequentialCompactionAccessPattern, opts.GetAccessHintOnCompactionStart())
106106

107107
opts.SetDbWriteBufferSize(1 << 30)
108108
require.EqualValues(t, 1<<30, opts.GetDbWriteBufferSize())
@@ -401,6 +401,10 @@ func TestOptions(t *testing.T) {
401401

402402
opts.SetWriteBufferManager(wbm)
403403

404+
lg := NewStderrLogger(InfoInfoLogLevel, "prefix")
405+
opts.SetInfoLog(lg)
406+
require.NotNil(t, opts.GetInfoLog())
407+
404408
// cloning
405409
cl := opts.Clone()
406410
require.EqualValues(t, 5, cl.GetTableCacheNumshardbits())

ratelimiter.go

+49
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ package grocksdb
44
// #include "rocksdb/c.h"
55
import "C"
66

7+
type RateLimiterMode int
8+
9+
const (
10+
RateLimiterModeReadsOnly RateLimiterMode = iota
11+
RateLimiterModeWritesOnly
12+
RateLimiterModeAllIo
13+
)
14+
715
// RateLimiter is used to control write rate of flush and
816
// compaction.
917
type RateLimiter struct {
@@ -52,6 +60,47 @@ func NewAutoTunedRateLimiter(rateBytesPerSec, refillPeriodMicros int64, fairness
5260
return newNativeRateLimiter(cR)
5361
}
5462

63+
// NewGenericRateLimiter creates a RateLimiter object, which can be shared among RocksDB instances to
64+
// control write rate of flush and compaction.
65+
//
66+
// @rate_bytes_per_sec: this is the only parameter you want to set most of the
67+
// time. It controls the total write rate of compaction and flush in bytes per
68+
// second. Currently, RocksDB does not enforce rate limit for anything other
69+
// than flush and compaction, e.g. write to WAL.
70+
//
71+
// @refill_period_us: this controls how often tokens are refilled. For example,
72+
// when rate_bytes_per_sec is set to 10MB/s and refill_period_us is set to
73+
// 100ms, then 1MB is refilled every 100ms internally. Larger value can lead to
74+
// burstier writes while smaller value introduces more CPU overhead.
75+
// The default should work for most cases.
76+
//
77+
// @fairness: RateLimiter accepts high-pri requests and low-pri requests.
78+
// A low-pri request is usually blocked in favor of hi-pri request. Currently,
79+
// RocksDB assigns low-pri to request from compaction and high-pri to request
80+
// from flush. Low-pri requests can get blocked if flush requests come in
81+
// continuously. This fairness parameter grants low-pri requests permission by
82+
// 1/fairness chance even though high-pri requests exist to avoid starvation.
83+
// You should be good by leaving it at default 10.
84+
//
85+
// @mode: Mode indicates which types of operations count against the limit.
86+
//
87+
// @auto_tuned: Enables dynamic adjustment of rate limit within the range
88+
// `[rate_bytes_per_sec / 20, rate_bytes_per_sec]`, according to
89+
// the recent demand for background I/O.
90+
func NewGenericRateLimiter(
91+
rateBytesPerSec, refillPeriodMicros int64, fairness int32,
92+
mode RateLimiterMode, autoTuned bool,
93+
) *RateLimiter {
94+
cR := C.rocksdb_ratelimiter_create_with_mode(
95+
C.int64_t(rateBytesPerSec),
96+
C.int64_t(refillPeriodMicros),
97+
C.int32_t(fairness),
98+
C.int(mode),
99+
C.bool(autoTuned),
100+
)
101+
return newNativeRateLimiter(cR)
102+
}
103+
55104
// NewNativeRateLimiter creates a native RateLimiter object.
56105
func newNativeRateLimiter(c *C.rocksdb_ratelimiter_t) *RateLimiter {
57106
return &RateLimiter{c: c}

snapshot.go

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ func newNativeSnapshot(c *C.rocksdb_snapshot_t) *Snapshot {
1414
return &Snapshot{c: c}
1515
}
1616

17+
// GetSequenceNumber gets sequence number of the Snapshot.
18+
func (snapshot *Snapshot) GetSequenceNumber() uint64 {
19+
return uint64(C.rocksdb_snapshot_get_sequence_number(snapshot.c))
20+
}
21+
1722
// Destroy deallocates the Snapshot object.
1823
func (snapshot *Snapshot) Destroy() {
1924
C.rocksdb_free(unsafe.Pointer(snapshot.c))

0 commit comments

Comments
 (0)