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

[ENH] add vacuum CLI command #2519

Merged
merged 17 commits into from
Jul 30, 2024
Merged

[ENH] add vacuum CLI command #2519

merged 17 commits into from
Jul 30, 2024

Conversation

codetheweb
Copy link
Contributor

@codetheweb codetheweb commented Jul 15, 2024

Implementation of the CLI command described in this CIP: #2498. Terminal UI demo:

Screen.Recording.2024-07-24.at.4.48.03.PM.mov

Warning that's logged when automatic pruning is disabled:

Screenshot 2024-07-24 at 5 37 32 PM

Copy link

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

@@ -19,6 +19,7 @@ posthog>=2.4.0
pydantic>=1.9
pypika>=0.48.9
PyYAML>=6.0.0
rich>=10.11.0
Copy link
Contributor Author

Choose a reason for hiding this comment

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

rich was already a subdependency of typer, so this is not a new dependency

id INT PRIMARY KEY,
timestamp INT NOT NULL,
operation TEXT NOT NULL
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

may be useful in the future, but currently only used for assertions in tests

raise typer.Exit(code=1)

if not force and not typer.confirm(
"Are you sure you want to vacuum the database? This will block both reads and writes to the database, and may take a while."
Copy link
Contributor Author

Choose a reason for hiding this comment

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

VACUUM can be safely run with concurrent users (e.g. the chroma server running) but will block all queries as it takes an exclusive lock

Copy link
Contributor

Choose a reason for hiding this comment

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

For very large DBs this can take up to a min or more. There are at least two options here:

In prior implementations I did force the whole connection pool to close all connections and do a global lockout to ensure no incoming requests are processed, but for that to work this command needs to execute within the fastapi process/worker.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, not clear to me what your comment is addressing, why would we want to enable WAL mode on SQLite?

I do change the busy_timeout before running VACUUM just so that it's not indefinitely blocked by long-running writes (our global timeout is set to 16m currently).

settings.persist_directory = path
system = System(settings=settings)
sqlite = system.instance(SqliteDB)

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 would like to add a check before vacuuming that there's enough free disk space (> current db size) but this turned out to be fairly difficult:

  • SQLite doesn't directly expose its temporary directory (or at least the PRAGMA that does is deprecated)
  • how would we set up tests to simulate a mount point running out of space?

If the vacuum does fail because there's not enough space, that error will propagate back to stdout.

Copy link
Contributor

@tazarov tazarov Jul 16, 2024

Choose a reason for hiding this comment

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

@codetheweb, here are some thoughts:

  • You can vacuum into a new file VACUUM into <file> - this mimics copy on write, which I think might be a good fit here as we control where things go and how we do checks. This will not work if the DB is live, though, as files need to be moved/removed.
  • As far as testing goes, it could do the trick of restricting space in a docker test.
services:
  chroma:
    image: chroma
    tmpfs:
      - /chroma/chroma:size=100M

Copy link
Contributor Author

@codetheweb codetheweb Jul 16, 2024

Choose a reason for hiding this comment

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

You can vacuum into a new file VACUUM into - this mimics copy on write, which I think might be a good fit here as we control where things go and how we do checks. This will not work if the DB is live, though, as files need to be moved/removed.

I think the fact that this puts the correctness burden on us makes it a dealbreaker.

In practice I'm not sure if this will be a real issue; if the WAL is pruned prior to VACUUM in theory the amount of temporary space needed should be much less than the current database size; assuming the size of most databases is dominated by the WAL.

@codetheweb codetheweb marked this pull request as ready for review July 15, 2024 22:40
@codetheweb codetheweb requested a review from atroyn July 15, 2024 22:40
@tazarov
Copy link
Contributor

tazarov commented Jul 16, 2024

In one of the prior incarnations of this, we also reported on the stats of what the vacuum did, e.g., the size of the SQLite3 file before/after and, optionally, the number of free_pages before/after. This could be good information to help users understand the effects of vacuuming.

@codetheweb
Copy link
Contributor Author

In one of the prior incarnations of this, we also reported on the stats of what the vacuum did, e.g., the size of the SQLite3 file before/after and, optionally, the number of free_pages before/after. This could be good information to help users understand the effects of vacuuming.

We could expose this in the future, but I don't see this being particularly useful right now as vacuum operations after the initial one aren't expected to free much space. People can always do du -d0 -h {data_dir} to see the disk usage themselves too. :)

@atroyn
Copy link
Contributor

atroyn commented Jul 24, 2024

In one of the prior incarnations of this, we also reported on the stats of what the vacuum did, e.g., the size of the SQLite3 file before/after and, optionally, the number of free_pages before/after. This could be good information to help users understand the effects of vacuuming.

We could expose this in the future, but I don't see this being particularly useful right now as vacuum operations after the initial one aren't expected to free much space. People can always do du -d0 -h {data_dir} to see the disk usage themselves too. :)

I do think it's useful to tell users what the very first vacuum did for them, and then if they try doing it again when they don't need to, to see that it does not do much for them.

@codetheweb
Copy link
Contributor Author

codetheweb commented Jul 24, 2024

In one of the prior incarnations of this, we also reported on the stats of what the vacuum did, e.g., the size of the SQLite3 file before/after and, optionally, the number of free_pages before/after. This could be good information to help users understand the effects of vacuuming.

We could expose this in the future, but I don't see this being particularly useful right now as vacuum operations after the initial one aren't expected to free much space. People can always do du -d0 -h {data_dir} to see the disk usage themselves too. :)

I do think it's useful to tell users what the very first vacuum did for them, and then if they try doing it again when they don't need to, to see that it does not do much for them.

Screenshot 2024-07-24 at 3 29 39 PM

@codetheweb
Copy link
Contributor Author

codetheweb commented Jul 24, 2024

I think we need to hold off on adding a warning on startup if vacuum hasn't been run; that's dependent on storing the auto-pruning setting (if a user inits 0.6 on a fresh data directory, vacuum doesn't need to be run). I'll add it separately to our internal tracker. decided to put this in the same stack instead

@codetheweb codetheweb changed the base branch from main to feat-automatically-clean-wal July 24, 2024 22:40
@codetheweb codetheweb force-pushed the feat-automatically-clean-wal branch from 0d57374 to 2b61a2b Compare July 24, 2024 22:54
Copy link
Contributor Author

codetheweb commented Jul 24, 2024

chromadb/cli/cli.py Outdated Show resolved Hide resolved
@codetheweb codetheweb changed the base branch from feat-automatically-clean-wal to main July 29, 2024 23:16
@codetheweb codetheweb merged commit ea3ec08 into main Jul 30, 2024
68 checks passed
Copy link
Contributor Author

Merge activity

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.

3 participants