##// END OF EJS Templates
repair: migrate revlogs during upgrade...
repair: migrate revlogs during upgrade Our next step for in-place upgrade is to migrate store data. Revlogs are the biggest source of data within the store and a store is useless without them, so we implement their migration first. Our strategy for migrating revlogs is to walk the store and call `revlog.clone()` on each revlog. There are some minor complications. Because revlogs have different storage options (e.g. changelog has generaldelta and delta chains disabled), we need to obtain the correct class of revlog so inserted data is encoded properly for its type. Various attempts at implementing progress indicators that didn't lead to frustration from false "it's almost done" indicators were made. I initially used a single progress bar based on number of revlogs. However, this quickly churned through all filelogs, got to 99% then effectively froze at 99.99% when it got to the manifest. So I converted the progress bar to total revision count. This was a little bit better. But the manifest was still significantly slower than filelogs and it took forever to process the last few percent. I then tried both revision/chunk bytes and raw bytes as the denominator. This had the opposite effect: because so much data is in manifests, it would churn through filelogs without showing much progress. When it got to manifests, it would fill in 90+% of the progress bar. I finally gave up having a unified progress bar and instead implemented 3 progress bars: 1 for filelog revisions, 1 for manifest revisions, and 1 for changelog revisions. I added extra messages indicating the total number of revisions of each so users know there are more progress bars coming. I also added extra messages before and after each stage to give extra details about what is happening. Strictly speaking, this isn't necessary. But the numbers are impressive. For example, when converting a non-generaldelta mozilla-central repository, the messages you see are: migrating 2475593 total revisions (1833043 in filelogs, 321156 in manifests, 321394 in changelog) migrating 1.67 GB in store; 2508 GB tracked data migrating 267868 filelogs containing 1833043 revisions (1.09 GB in store; 57.3 GB tracked data) finished migrating 1833043 filelog revisions across 267868 filelogs; change in size: -415776 bytes migrating 1 manifests containing 321156 revisions (518 MB in store; 2451 GB tracked data) That "2508 GB" figure really blew me away. I had no clue that the raw tracked data in mozilla-central was that large. Granted, 2451 GB is in the manifest and "only" 57.3 GB is in filelogs. But still. It's worth noting that gratuitous loading of source revlogs in order to display numbers and progress bars does serve a purpose: it ensures we can open all source revlogs. We don't want to spend several minutes copying revlogs only to encounter a permissions error or similar later. As part of this commit, we also add swapping of the store directory to the upgrade function. After revlogs are converted, we move the old store into the backup directory then move the temporary repo's store into the old store's location. On well-behaved systems, this should be 2 atomic operations and the window of inconsistency show be very narrow. There are still a few improvements to be made to store copying and upgrading. But this commit gets the bulk of the work out of the way.

File last commit:

r29749:155f0cc3 default
r30779:38aa1ca9 default
Show More
mpatch_module.c
195 lines | 4.3 KiB | text/x-c | CLexer
/*
mpatch.c - efficient binary patching for Mercurial
This implements a patch algorithm that's O(m + nlog n) where m is the
size of the output and n is the number of patches.
Given a list of binary patches, it unpacks each into a hunk list,
then combines the hunk lists with a treewise recursion to form a
single hunk list. This hunk list is then applied to the original
text.
The text (or binary) fragments are copied directly from their source
Python objects into a preallocated output string to avoid the
allocation of intermediate Python objects. Working memory is about 2x
the total number of hunks.
Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
This software may be used and distributed according to the terms
of the GNU General Public License, incorporated herein by reference.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "bitmanipulation.h"
#include "compat.h"
#include "mpatch.h"
static char mpatch_doc[] = "Efficient binary patching.";
static PyObject *mpatch_Error;
static void setpyerr(int r)
{
switch (r) {
case MPATCH_ERR_NO_MEM:
PyErr_NoMemory();
break;
case MPATCH_ERR_CANNOT_BE_DECODED:
PyErr_SetString(mpatch_Error, "patch cannot be decoded");
break;
case MPATCH_ERR_INVALID_PATCH:
PyErr_SetString(mpatch_Error, "invalid patch");
break;
}
}
struct mpatch_flist *cpygetitem(void *bins, ssize_t pos)
{
const char *buffer;
struct mpatch_flist *res;
ssize_t blen;
int r;
PyObject *tmp = PyList_GetItem((PyObject*)bins, pos);
if (!tmp)
return NULL;
if (PyObject_AsCharBuffer(tmp, &buffer, (Py_ssize_t*)&blen))
return NULL;
if ((r = mpatch_decode(buffer, blen, &res)) < 0) {
if (!PyErr_Occurred())
setpyerr(r);
return NULL;
}
return res;
}
static PyObject *
patches(PyObject *self, PyObject *args)
{
PyObject *text, *bins, *result;
struct mpatch_flist *patch;
const char *in;
int r = 0;
char *out;
Py_ssize_t len, outlen, inlen;
if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins))
return NULL;
len = PyList_Size(bins);
if (!len) {
/* nothing to do */
Py_INCREF(text);
return text;
}
if (PyObject_AsCharBuffer(text, &in, &inlen))
return NULL;
patch = mpatch_fold(bins, cpygetitem, 0, len);
if (!patch) { /* error already set or memory error */
if (!PyErr_Occurred())
PyErr_NoMemory();
return NULL;
}
outlen = mpatch_calcsize(inlen, patch);
if (outlen < 0) {
r = (int)outlen;
result = NULL;
goto cleanup;
}
result = PyBytes_FromStringAndSize(NULL, outlen);
if (!result) {
result = NULL;
goto cleanup;
}
out = PyBytes_AsString(result);
if ((r = mpatch_apply(out, in, inlen, patch)) < 0) {
Py_DECREF(result);
result = NULL;
}
cleanup:
mpatch_lfree(patch);
if (!result && !PyErr_Occurred())
setpyerr(r);
return result;
}
/* calculate size of a patched file directly */
static PyObject *
patchedsize(PyObject *self, PyObject *args)
{
long orig, start, end, len, outlen = 0, last = 0, pos = 0;
Py_ssize_t patchlen;
char *bin;
if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen))
return NULL;
while (pos >= 0 && pos < patchlen) {
start = getbe32(bin + pos);
end = getbe32(bin + pos + 4);
len = getbe32(bin + pos + 8);
if (start > end)
break; /* sanity check */
pos += 12 + len;
outlen += start - last;
last = end;
outlen += len;
}
if (pos != patchlen) {
if (!PyErr_Occurred())
PyErr_SetString(mpatch_Error, "patch cannot be decoded");
return NULL;
}
outlen += orig - last;
return Py_BuildValue("l", outlen);
}
static PyMethodDef methods[] = {
{"patches", patches, METH_VARARGS, "apply a series of patches\n"},
{"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"},
{NULL, NULL}
};
#ifdef IS_PY3K
static struct PyModuleDef mpatch_module = {
PyModuleDef_HEAD_INIT,
"mpatch",
mpatch_doc,
-1,
methods
};
PyMODINIT_FUNC PyInit_mpatch(void)
{
PyObject *m;
m = PyModule_Create(&mpatch_module);
if (m == NULL)
return NULL;
mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError",
NULL, NULL);
Py_INCREF(mpatch_Error);
PyModule_AddObject(m, "mpatchError", mpatch_Error);
return m;
}
#else
PyMODINIT_FUNC
initmpatch(void)
{
Py_InitModule3("mpatch", methods, mpatch_doc);
mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError",
NULL, NULL);
}
#endif