Skip to content

Commit 853b695

Browse files
Merge branch 'unstable' of https://github.com/wanghan-sanechips/kvrocks into unstable
2 parents a2ddc86 + 7a34463 commit 853b695

5 files changed

Lines changed: 101 additions & 3 deletions

File tree

cmake/rocksdb.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ endif()
2626
include(cmake/utils.cmake)
2727

2828
FetchContent_DeclareGitHubWithMirror(rocksdb
29-
facebook/rocksdb v10.2.1
30-
MD5=6dc9686856ec407cd98d78be06d53fa7
29+
facebook/rocksdb v10.4.2
30+
MD5=2581c62b42cbb1c2d158bb2c54355b6e
3131
)
3232

3333
FetchContent_GetProperties(jemalloc)

src/commands/cmd_server.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,18 @@ class CommandFlushMemTable : public Commander {
15271527
rocksdb::FlushOptions flush_options_;
15281528
};
15291529

1530+
class CommandFlushBlockCache : public Commander {
1531+
public:
1532+
Status Execute([[maybe_unused]] engine::Context &ctx, Server *srv, [[maybe_unused]] Connection *conn,
1533+
std::string *output) override {
1534+
srv->storage->FlushBlockCache();
1535+
1536+
*output = redis::RESP_OK;
1537+
info("FLUSHBLOCKCACHE is triggered and executed successfully");
1538+
return Status::OK();
1539+
}
1540+
};
1541+
15301542
REDIS_REGISTER_COMMANDS(Server, MakeCmdAttr<CommandAuth>("auth", 2, "read-only ok-loading auth", NO_KEY),
15311543
MakeCmdAttr<CommandPing>("ping", -1, "read-only", NO_KEY),
15321544
MakeCmdAttr<CommandSelect>("select", 2, "read-only", NO_KEY),
@@ -1570,5 +1582,6 @@ REDIS_REGISTER_COMMANDS(Server, MakeCmdAttr<CommandAuth>("auth", 2, "read-only o
15701582
MakeCmdAttr<CommandDump>("dump", 2, "read-only", 1, 1, 1),
15711583
MakeCmdAttr<CommandPollUpdates>("pollupdates", -2, "read-only admin", NO_KEY),
15721584
MakeCmdAttr<CommandSST>("sst", -3, "write exclusive admin", 1, 1, 1),
1573-
MakeCmdAttr<CommandFlushMemTable>("flushmemtable", -1, "exclusive write", NO_KEY), )
1585+
MakeCmdAttr<CommandFlushMemTable>("flushmemtable", -1, "exclusive write", NO_KEY),
1586+
MakeCmdAttr<CommandFlushBlockCache>("flushblockcache", 1, "exclusive write", NO_KEY), )
15741587
} // namespace redis

src/storage/storage.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,8 @@ rocksdb::Status Storage::ingestSST(rocksdb::ColumnFamilyHandle *cf_handle,
843843
return db_->IngestExternalFile(cf_handle, sst_file_names, options);
844844
}
845845

846+
void Storage::FlushBlockCache() { shared_block_cache_->EraseUnRefEntries(); }
847+
846848
Status Storage::ReplicaApplyWriteBatch(rocksdb::WriteBatch *batch) {
847849
return applyWriteBatch(default_write_opts_, batch);
848850
}

src/storage/storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class Storage {
276276
const rocksdb::FlushOptions &options);
277277
[[nodiscard]] StatusOr<int> IngestSST(const std::string &folder,
278278
const rocksdb::IngestExternalFileOptions &ingest_options);
279+
void FlushBlockCache();
279280

280281
rocksdb::DB *GetDB();
281282
bool IsClosing() const { return db_closing_; }
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package flushblockcache
21+
22+
import (
23+
"context"
24+
"strconv"
25+
"testing"
26+
27+
"github.com/apache/kvrocks/tests/gocase/util"
28+
"github.com/redis/go-redis/v9"
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
func getBlockCacheSize(rdb *redis.Client) (int64, error) {
33+
value := util.FindInfoEntry(rdb, "block_cache_usage", "rocksdb")
34+
return strconv.ParseInt(value, 10, 64)
35+
}
36+
37+
func TestFlushBlockCache(t *testing.T) {
38+
configs := map[string]string{}
39+
srv := util.StartServer(t, configs)
40+
defer srv.Close()
41+
42+
rdb := srv.NewClient()
43+
defer func() {
44+
require.NoError(t, rdb.Close())
45+
}()
46+
47+
ctx := context.Background()
48+
49+
t.Run("flushblockcache", func(t *testing.T) {
50+
_, err := rdb.Do(ctx, "SET", "A", "KVROCKS").Result()
51+
require.NoError(t, err)
52+
_, err = rdb.Do(ctx, "FLUSHMEMTABLE").Result()
53+
require.NoError(t, err)
54+
_, err = rdb.Do(ctx, "GET", "A").Result()
55+
require.NoError(t, err)
56+
initCacheSize, err := getBlockCacheSize(rdb)
57+
require.NoError(t, err)
58+
_, err = rdb.Do(ctx, "FLUSHBLOCKCACHE").Result()
59+
require.NoError(t, err)
60+
cacheSize, err := getBlockCacheSize(rdb)
61+
require.NoError(t, err)
62+
require.Less(t, cacheSize, initCacheSize)
63+
require.Equal(t, "KVROCKS", rdb.Do(ctx, "GET", "A").Val())
64+
})
65+
}
66+
67+
func TestFlushBlockCacheInvalid(t *testing.T) {
68+
srv := util.StartServer(t, map[string]string{})
69+
defer srv.Close()
70+
71+
rdb := srv.NewClient()
72+
defer func() {
73+
require.NoError(t, rdb.Close())
74+
}()
75+
76+
ctx := context.Background()
77+
78+
t.Run("invalid arguments", func(t *testing.T) {
79+
_, err := rdb.Do(ctx, "FLUSHBLOCKCACHE", "ARG").Result()
80+
require.Contains(t, err.Error(), "wrong number of arguments")
81+
})
82+
}

0 commit comments

Comments
 (0)