Skip to content

Commit 6d08c33

Browse files
committed
Add documentation for generational compaction APIs
1 parent 3a4cc5e commit 6d08c33

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

src/docs/src/api/database/common.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@
156156
:query integer n: Replicas. The number of copies of the database in the
157157
cluster. The default is 3, unless overridden in the
158158
:config:option:`cluster config <cluster/n>` .
159+
:query integer gen: Maximum generation. The number of generation files in
160+
which to store database content. The default is 0, which means the
161+
database will not use generational storage.
159162
:query boolean partitioned: Whether to create a partitioned database.
160163
Default is false.
161164
:<header Accept: - :mimetype:`application/json`

src/docs/src/api/database/compact.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
compaction is currently running. See :ref:`api/server/active_tasks`.
4646

4747
:param db: Database name
48+
:query integer gen: Generation - which generation to compact. Default is 0.
4849
:<header Accept: - :mimetype:`application/json`
4950
- :mimetype:`text/plain`
5051
:<header Content-Type: :mimetype:`application/json`

src/docs/src/api/database/misc.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,65 @@ following behavior:
330330
"ok": true
331331
}
332332
333+
334+
.. _api/db/_max_generation:
335+
336+
==============================
337+
``/{db}/_max_generation``
338+
==============================
339+
340+
.. http:put:: /{db}/_max_generation
341+
:synopsis: Sets the maximum generation in the database.
342+
343+
Sets the maximum number of generation files that the database's content will
344+
be stored in. This is 0 by default, which means that the database will be
345+
stored in a single file. Increasing this limit means that the compactor can
346+
"promote" live data into the next generation file, moving it out of the base
347+
database file so that it does not need to be re-copied on subsequent
348+
compactions.
349+
350+
This number can only be increased, not decreased, so exercise caution when
351+
changing it.
352+
353+
:param db: Database name
354+
:<header Accept: - :mimetype:`application/json`
355+
- :mimetype:`text/plain`
356+
:<header Content-Type: :mimetype:`application/json`
357+
:>header Content-Type: - :mimetype:`application/json`
358+
- :mimetype:`text/plain; charset=utf-8`
359+
:>json boolean ok: Operation status
360+
:code 200: Request completed successfully
361+
:code 400: Invalid JSON data
362+
:code 401: Unauthorized request to a protected API
363+
:code 403: Insufficient permissions / :ref:`Too many requests with invalid credentials<error/403>`
364+
365+
**Request**:
366+
367+
.. code-block:: http
368+
369+
PUT /db/_max_generation HTTP/1.1
370+
Accept: application/json
371+
Content-Length: 1
372+
Content-Type: application/json
373+
Host: localhost:5984
374+
375+
2
376+
377+
**Response**:
378+
379+
.. code-block:: http
380+
381+
HTTP/1.1 200 OK
382+
Cache-Control: must-revalidate
383+
Content-Length: 12
384+
Content-Type: application/json
385+
Date: Wed, 14 Jun 2017 14:45:34 GMT
386+
Server: CouchDB (Erlang/OTP)
387+
388+
{
389+
"ok": true
390+
}
391+
333392
.. _api/db/missing_revs:
334393

335394
=======================

src/docs/src/maintenance/compaction.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,60 @@ exist anymore) you can trigger a :ref:`view cleanup <api/db/view_cleanup>`::
377377
.. code-block:: javascript
378378
379379
{"ok":true}
380+
381+
Generational Compaction
382+
=======================
383+
384+
Normally, compaction works by copying all live data (the document bodies and
385+
attachments for the latest versions of all documents) into a new file with the
386+
``.compact`` extension, and then replacing the normal database file with this
387+
new one. That is, if the database resides in a file named ``mydb.couch``, then
388+
compaction copies its live data into ``mydb.couch.compact``, and then renames
389+
``mydb.couch.compact`` to ``mydb.couch``.
390+
391+
If you have a lot of documents that do not change for long periods of time, then
392+
compaction will have to copy a lot of the same data every time it runs. For
393+
large data sets, especially those where individual documents or attachments are
394+
very large, this can be wasteful and it leads to compaction taking a long time
395+
to run.
396+
397+
To improve compaction times for such data sets, CouchDB offers a mode called
398+
`generational compaction`. When this is enabled, the database is stored in
399+
multiple files known as `generations`, named ``mydb.1.couch``, ``mydb.2.couch``
400+
and so on, rather than in a single file. Rather than building a whole new copy
401+
of the ``mydb.couch`` file, generational compaction `promotes` live data from
402+
its current generation to the next one up.
403+
404+
All new document and attachment data is originally stored in `generation 0`, and
405+
when it is compacted, its live data is promoted to `generation 1`. The next time
406+
generation 0 is compacted, CouchDB will not have to re-copy all the data that
407+
was already promoted to generation 1.
408+
409+
Generational storage and compaction can be enabled either by setting the ``gen``
410+
parameter when the database is first created:
411+
412+
.. code-block:: bash
413+
414+
curl -X PUT http://admin:pass@localhost:5984/dbname?gen=G
415+
416+
Or, by setting the ``max_generation`` property on an existing database:
417+
418+
.. codeb-block:: bash
419+
420+
curl -X PUT http://admin:pass@localhost:5984/dbname/_max_generation -d G
421+
422+
The value ``G`` sets the database's ``max_generation`` number; a value of ``0``
423+
selects the normal single-file storage model. A value of ``1`` produces a single
424+
extra generational file, ``2`` produces two extra files, and so on.
425+
426+
When compacting a generational database, you may specify a generation to compact
427+
via the ``gen`` parameter:
428+
429+
.. code-block:: bash
430+
431+
curl -X POST http://admin:pass@localhost:5984/mydb/_compact?gen=1
432+
433+
Compaction targets generation 0 by default. CouchDB will only ever compact a
434+
single generation of a database at the time, and Smoosh monitors the extra
435+
generation files for when they exceed a configured slack/ratio threshold, just
436+
like it does when using single-file storage.

0 commit comments

Comments
 (0)