-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add controlled autovacuum for small databases #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marco6
wants to merge
2
commits into
master
Choose a base branch
from
autovacuum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| BasedOnStyle: Google | ||
| IndentWidth: 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #include "extensions.h" | ||
|
|
||
| #include <assert.h> | ||
| #include <sqlite3.h> | ||
| #include <stdatomic.h> | ||
| #include <stdint.h> | ||
| #include <string.h> | ||
|
|
||
| #define UNUSED(x) (void)(x) | ||
|
|
||
| struct vacuum_config_s { | ||
| size_t percent; | ||
| size_t max_pages; | ||
| } vacuum_config; | ||
|
|
||
| static unsigned int autovacuum_pages_callback(void *pClientData, | ||
| const char *zSchema, | ||
| unsigned int nDbPage, | ||
| unsigned int nFreePage, | ||
| unsigned int nBytePerPage) { | ||
| UNUSED(pClientData); | ||
| UNUSED(zSchema); | ||
| UNUSED(nBytePerPage); | ||
|
|
||
| assert(nDbPage >= nFreePage); | ||
|
|
||
| unsigned int keep_amount = | ||
| (nDbPage - nFreePage) * vacuum_config.percent / 100; | ||
| if (nFreePage < keep_amount) { | ||
| return 0; | ||
| } | ||
|
|
||
| unsigned int free_amount = nFreePage - keep_amount; | ||
| if (free_amount > vacuum_config.max_pages) { | ||
| free_amount = vacuum_config.max_pages; | ||
| } | ||
| return free_amount; | ||
| } | ||
|
|
||
| static sqlite3_error auto_instrument_connection( | ||
| sqlite3 *connection, const char **pzErrMsg, | ||
| const struct sqlite3_api_routines *pThunk) { | ||
| UNUSED(pzErrMsg); | ||
| UNUSED(pThunk); | ||
|
|
||
| return sqlite3_autovacuum_pages(connection, autovacuum_pages_callback, NULL, | ||
| NULL); | ||
| } | ||
|
|
||
| sqlite3_error sqlite3_enable_autovacuum_limit(size_t percent, | ||
| size_t max_pages) { | ||
| vacuum_config.percent = percent; | ||
| vacuum_config.max_pages = max_pages; | ||
|
|
||
| return sqlite3_auto_extension((void (*)())(auto_instrument_connection)); | ||
| } | ||
|
|
||
| void sqlite3_disable_autovacuum_limit() { | ||
| sqlite3_cancel_auto_extension((void (*)())(auto_instrument_connection)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package sqlite | ||
|
|
||
| // #cgo LDFLAGS: -lsqlite3 | ||
| // #include "extensions.h" | ||
| import "C" | ||
|
|
||
| import "github.com/mattn/go-sqlite3" | ||
|
|
||
| func init() { | ||
| if err := C.sqlite3_enable_autovacuum_limit(10, 16); err != C.SQLITE_OK { | ||
| panic(sqlite3.ErrNo(err)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include <memory.h> // for size_t | ||
| #include <sqlite3.h> | ||
| #include <stdint.h> | ||
|
|
||
| typedef int sqlite3_error; | ||
|
|
||
| // sqlite3_enable_autovacuum_limit limits the amount of autovacuumed | ||
| // pages to a percentage of the total database size. This is useful | ||
| // to decide on a compromise between performance and resource usage | ||
| // - percent: percentage of database size to keep, so that the databse | ||
| // so that the databse does not need to grow again | ||
| // - max_pages: maximum number of pages to autovacuum | ||
| sqlite3_error sqlite3_enable_autovacuum_limit(size_t percent, size_t max_pages); | ||
|
|
||
| // sqlite3_disable_autovacuum_limit disables the autovacuum limit enabled | ||
| // by @sqlite3_enable_autovacuum_limit. | ||
| void sqlite3_disable_autovacuum_limit(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ func TestCompaction(t *testing.T) { | |
| g.Expect(finalSize).To(BeNumerically("==", initialSize)) // Expecting no compaction. | ||
| }) | ||
|
|
||
| t.Run("LargeDatabaseDeleteFivePercent", func(t *testing.T) { | ||
| t.Run("LargeDatabaseDeleteFew", func(t *testing.T) { | ||
| g := NewWithT(t) | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
|
|
@@ -60,7 +60,7 @@ func TestCompaction(t *testing.T) { | |
| if _, err := insertMany(ctx, tx, "key", 100, 10_000); err != nil { | ||
| return err | ||
| } | ||
| if _, err := updateMany(ctx, tx, "key", 100, 500); err != nil { | ||
| if _, err := updateMany(ctx, tx, "key", 100, 5_000); err != nil { | ||
| return err | ||
| } | ||
| if _, err := deleteMany(ctx, tx, "key", 500); err != nil { | ||
|
|
@@ -76,6 +76,49 @@ func TestCompaction(t *testing.T) { | |
| err = server.backend.DoCompact(ctx) | ||
| g.Expect(err).To(BeNil()) | ||
|
|
||
| // Expect compaction not to increase the size. | ||
| finalSize, err := server.backend.DbSize(ctx) | ||
| g.Expect(err).To(BeNil()) | ||
| g.Expect(finalSize).To(BeNumerically("<=", initialSize)) | ||
|
|
||
| // Expect for keys to still be there. | ||
| rev, count, err := server.backend.Count(ctx, []byte("key/"), []byte("key0"), 0) | ||
| g.Expect(err).To(BeNil()) | ||
| g.Expect(count).To(Equal(int64(10_000 - 500))) | ||
|
|
||
| // Expect old revisions not to be there anymore. | ||
| _, _, err = server.backend.List(ctx, []byte("key/"), []byte("key0"), 0, rev-400) | ||
| g.Expect(err).To(Not(BeNil())) | ||
| }) | ||
|
|
||
| t.Run("LargeDatabaseDeleteMost", func(t *testing.T) { | ||
| g := NewWithT(t) | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| server := newK8sDqliteServer(ctx, t, &k8sDqliteConfig{ | ||
| backendType: backendType, | ||
| setup: func(ctx context.Context, tx *sql.Tx) error { | ||
| if _, err := insertMany(ctx, tx, "key", 100, 10_000); err != nil { | ||
| return err | ||
| } | ||
| if _, err := updateMany(ctx, tx, "key", 100, 500); err != nil { | ||
| return err | ||
| } | ||
| if _, err := deleteMany(ctx, tx, "key", 9_500); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| }, | ||
| }) | ||
|
|
||
| initialSize, err := server.backend.DbSize(ctx) | ||
| g.Expect(err).To(BeNil()) | ||
|
|
||
| err = server.backend.DoCompact(ctx) | ||
| g.Expect(err).To(BeNil()) | ||
|
|
||
| // Expect compaction to reduce the size. | ||
| finalSize, err := server.backend.DbSize(ctx) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we please be more precise here and check whether the size aligns with the percentage we set in qlite3_enable_autovacuum_limit? |
||
| g.Expect(err).To(BeNil()) | ||
|
|
@@ -84,7 +127,7 @@ func TestCompaction(t *testing.T) { | |
| // Expect for keys to still be there. | ||
| rev, count, err := server.backend.Count(ctx, []byte("key/"), []byte("key0"), 0) | ||
| g.Expect(err).To(BeNil()) | ||
| g.Expect(count).To(Equal(int64(10_000 - 500))) | ||
| g.Expect(count).To(Equal(int64(10_000 - 9_500))) | ||
|
|
||
| // Expect old revisions not to be there anymore. | ||
| _, _, err = server.backend.List(ctx, []byte("key/"), []byte("key0"), 0, rev-400) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we/users going to need to tune the percentage and max pages used in auto-vacuuming?