Skip to content
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

WallHook v2 #517

Merged
merged 40 commits into from
Nov 30, 2023
Merged

WallHook v2 #517

merged 40 commits into from
Nov 30, 2023

Conversation

MarinPostma
Copy link
Contributor

No description provided.

@MarinPostma MarinPostma changed the title wip WallHook v2 Oct 27, 2023
@MarinPostma MarinPostma force-pushed the wal-hook-v2 branch 2 times, most recently from 5d21da9 to 3c75c70 Compare October 29, 2023 17:46
libsql-sqlite3/src/btree.h Outdated Show resolved Hide resolved
@@ -517,12 +517,7 @@ static const sqlite3_api_routines sqlite3Apis = {
sqlite3_stmt_explain
};

static const libsql_api_routines libsqlApis = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a delicate change since libsqlApis is public. If we want to nuke them it's better to do it now rather than later, but we need to clearly document and announce that, in case we have external users of the virtual WAL api.

@@ -1238,6 +1238,9 @@ static int sqlite3Close(sqlite3 *db, int forceZombie){
*/
sqlite3VtabRollback(db);

/* Destroy the create wal */
/* (db->create_wal.xDestroy)(db->create_wal.self); */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it commented out because it's a draft and it's going to be back later? Or are we changing the way wal is destroyed? In any case, we need a FIXME/TODO comment placed here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the quirk I wanted to talk about. With shared pagers, the ownership of the create_wal is shared as well, because pagers are lifted to the global scope. So the connection does not necessarily owns the create_wal. This is only ok for now because there is nothing to clean with the sqlite3_create_wal. I think we need to have that reference counted, but maybe this is done another way in C?

libsql-sqlite3/src/pager.h Outdated Show resolved Hide resolved
@@ -247,13 +247,28 @@
** that correspond to frames greater than the new K value are removed
** from the hash table at this point.
*/
#include "sqliteInt.h"
#include <sqlite3.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope - now you risk it will be taken from one of the standard paths like /usr/include/, and it's not what you want here. Why is this header needed? If just for a few types => forward-declare them

libsql-sqlite3/src/wal.c Outdated Show resolved Hide resolved
** already be opened on connection pDbFd. The buffer that zWalName points
** to must remain valid for the lifetime of the returned Wal* handle.
**
** Custom virtual methods may be provided via the pMethods parameter.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is out of date right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes yes, I haven't started looking at documentation, just on getting something in a working state

** an SQLite error code is returned and *ppWal is left unmodified.
*/
static int sqlite3WalOpen(
void *self,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow why this pointer is type-erased, but we must at least name it anyway. Similarly, sqlite3_file object is never really instantiated, but instead cast to a specific type before it's used, e.g. a unix file. I can't suggest a name for it before I understand what self is here, isn't it just an implementation of a specific WAL, so something like libsql_wal_impl *? This type can be public and still be opaque, same as sqlite3_file, you just declare it in one of the public headers as

typedef struct libsql_wal_impl libsql_wal_impl;

We can't afford void * here because this type name also serves as documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self is the method receiver. It's whatever state the WAL needs to function. In the case of sqlite3, this is a sqlite3_wal.

I don't get the analogy with sqlite3_file? the sqlite3_file has a pointer to the methods, but self can really be anything. whatever is returned by xOpen

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping: instead of void, it should be a named opaque type, something like libsql_wal_impl *, defined as typedef struct libsql_wal_impl *libsql_wal_impl.


struct libsql_wal {
libsql_wal_methods methods; /* virtual wal methods */
void* self; /* methods receiver */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, so it's a pointer-to-implementation. In that case let's name it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need a translation here 😄

** the WAL file is open. It is a good place for initialization routines, as WAL
** is otherwise open lazily.
*/
int (*xPreMainDbOpen)(void* self, const char *main_db_path, int main_db_path_len);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: since we overhaul the interface, we should consider removing this thing altogether, it makes very little sense. I thought I need it long time ago when bottomless was developed as a loadable module, but even then, it makes 10000x more sense to just expose an initialization method that users are supposed to call before a database is open, not a weird WAL methods hook. So if we decide on the shape of virtual WAL v2, it should also include nuking this xPreMainDbOpen hook.

@MarinPostma MarinPostma force-pushed the wal-hook-v2 branch 3 times, most recently from dbf9519 to cf0d62a Compare October 30, 2023 17:29
mz0in pushed a commit to mz0in/libsql-dqlite that referenced this pull request Nov 1, 2023
Each request for new frames from a replica to a primary
resets the idle timer but such request is a long poll.
It won't return until it gets some new frame.
This means that a replica won't send another request for
new frames in the mean-time and won't restart the idle timer.

This commit makes primary keep track of replicas waiting for
new frames and prevents IdleShutdownLayer from stopping
the primary if there is at least one replica still waiting
for the new frames.

When a replica is put into sleep, it will drop the connection
and the connected_replicas counter will be decreased so only
live replicas can prevent primary from going to sleep.

Fixes tursodatabase#506

Signed-off-by: Piotr Jastrzebski <[email protected]>
}
memset((void*)&(pPager->wal), 0, sizeof(libsql_wal));
destroy_create_wal(&pPager->create_wal);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that one should only be called if pagerUseWal(pPager), right? So it should be moved one line up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The create wal is always there, but a wal is only created if the wal journaling mode is enabled. So we always need to destroy it.

libsql-sqlite3/src/wal.c Outdated Show resolved Hide resolved
@MarinPostma MarinPostma force-pushed the wal-hook-v2 branch 6 times, most recently from 7c7a3bf to 227ca89 Compare November 9, 2023 13:01
@MarinPostma MarinPostma force-pushed the wal-hook-v2 branch 9 times, most recently from d58b5f6 to 977d9ea Compare November 15, 2023 09:58
@MarinPostma MarinPostma force-pushed the wal-hook-v2 branch 3 times, most recently from b83f752 to 5ee3bcc Compare November 20, 2023 13:20
@MarinPostma MarinPostma added this pull request to the merge queue Nov 30, 2023
Merged via the queue into main with commit 85279bd Nov 30, 2023
13 checks passed
@MarinPostma MarinPostma deleted the wal-hook-v2 branch November 30, 2023 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants