~akopytov/percona-xtrabackup/bug1166888-2.0

« back to all changes in this revision

Viewing changes to src/local.c

  • Committer: Alexey Kopytov
  • Date: 2012-02-10 20:05:56 UTC
  • mto: This revision was merged to the branch mainline in revision 390.
  • Revision ID: akopytov@gmail.com-20120210200556-6kx41z8wwrqfucro
Rebase of the parallel compression patch on new trunk + post-review
fixes.

Implementation of parallel compression and streaming for XtraBackup.

This revision implements the following changes:

* InnoDB files are now streamed by the xtrabackup binary rather than
innobackupex. As a result, integrity is now verified by xtrabackup and
thus tar4ibd is no longer needed, so it was removed.

* xtrabackup binary now accepts the new '--stream' option which has
exactly the same semantics as the '--stream' option in
innobackupex: it tells xtrabackup to stream all files to the standard
output in the specified format rather than storing them locally.

* The xtrabackup binary can now do parallel compression using the
quicklz library. Two new options were added to xtrabackup to support
this feature:

- '--compress' tells xtrabackup to compress all output data, including
the transaction log file and meta data files, using the specified
compression algorithm. The only currently supported algorithm is
'quicklz'. The resulting files have the qpress archive format,
i.e. every *.qp file produced by xtrabackup is essentially a one-file
qpress archive and can be extracted and uncompressed by the qpress
file archiver (http://www.quicklz.com/).

- '--compress-threads' specifies the number of worker threads used by
xtrabackup for parallel data compression. This option defaults to 1.

Parallel compression ('--compress-threads') can be used together with
parallel file copying ('--parallel'). For example, '--parallel=4
--compress --compress-threads=2' will create 4 IO threads that will
read the data and pipe it to 2 compression threads.

* To support simultaneous compression and streaming, a new custom
streaming format called 'xbstream' was introduced to XtraBackup in
addition to the 'tar' format. That was required to overcome some
limitations of traditional archive formats such as 'tar', 'cpio' and
others that do not allow streaming dynamically generated files, for
example dynamically compressed files.  Other advantages of xbstream over
traditional streaming/archive formats include ability to stream multiple
files concurrently (so it is possible to use streaming in the xbstream
format together with the --parallel option) and more compact data
storage.

* To allow streaming and extracting files to/from the xbstream format
produced by xtrabackup, a new utility aptly called 'xbstream' was
added to the XtraBackup distribution. This utility has a tar-like
interface:

- with the '-x' option it extracts files from the stream read from its
standard input to the current directory unless specified otherwise
with the '-C' option.

- with the '-c' option it streams files specified on the command line
to its standard output.

The utility also tries to minimize its impact on the OS page cache by
using the appropriate posix_fadvise() calls when available.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************
 
2
Copyright (c) 2011 Percona Inc.
 
3
 
 
4
Local datasink implementation for XtraBackup.
 
5
 
 
6
This program is free software; you can redistribute it and/or modify
 
7
it under the terms of the GNU General Public License as published by
 
8
the Free Software Foundation; version 2 of the License.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 
 
19
*******************************************************/
 
20
 
 
21
#include <my_base.h>
 
22
#include <mysys_err.h>
 
23
#include "common.h"
 
24
#include "datasink.h"
 
25
 
 
26
typedef struct {
 
27
        File fd;
 
28
} ds_local_file_t;
 
29
 
 
30
static ds_ctxt_t *local_init(const char *root);
 
31
static ds_file_t *local_open(ds_ctxt_t *ctxt, const char *path,
 
32
                             MY_STAT *mystat);
 
33
static int local_write(ds_file_t *file, const void *buf, size_t len);
 
34
static int local_close(ds_file_t *file);
 
35
static void local_deinit(ds_ctxt_t *ctxt);
 
36
 
 
37
datasink_t datasink_local = {
 
38
        &local_init,
 
39
        &local_open,
 
40
        &local_write,
 
41
        &local_close,
 
42
        &local_deinit
 
43
};
 
44
 
 
45
static
 
46
ds_ctxt_t *
 
47
local_init(const char *root)
 
48
{
 
49
        ds_ctxt_t *ctxt;
 
50
 
 
51
        if (my_mkdir(root, 0777, MYF(0)) < 0 && my_errno != EEXIST)
 
52
        {
 
53
                my_error(EE_CANT_MKDIR, MYF(ME_BELL | ME_WAITTANG),
 
54
                         root, my_errno);
 
55
                return NULL;
 
56
        }
 
57
 
 
58
        ctxt = my_malloc(sizeof(ds_ctxt_t), MYF(MY_FAE));
 
59
 
 
60
        ctxt->datasink = &datasink_local;
 
61
        ctxt->root = my_strdup(root, MYF(MY_FAE));
 
62
 
 
63
        return ctxt;
 
64
}
 
65
 
 
66
static
 
67
ds_file_t *
 
68
local_open(ds_ctxt_t *ctxt, const char *path,
 
69
           MY_STAT *mystat __attribute__((unused)))
 
70
{
 
71
        char            fullpath[FN_REFLEN];
 
72
        char            dirpath[FN_REFLEN];
 
73
        size_t          dirpath_len;
 
74
        size_t          path_len;
 
75
        ds_local_file_t *local_file;
 
76
        ds_file_t       *file;
 
77
        File            fd;
 
78
 
 
79
        fn_format(fullpath, path, ctxt->root, "", MYF(MY_RELATIVE_PATH));
 
80
 
 
81
        /* Create the directory if needed */
 
82
        dirname_part(dirpath, fullpath, &dirpath_len);
 
83
        if (my_mkdir(dirpath, 0777, MYF(0)) < 0 && my_errno != EEXIST) {
 
84
                my_error(EE_CANT_MKDIR, MYF(ME_BELL | ME_WAITTANG),
 
85
                         dirpath, my_errno);
 
86
                return NULL;
 
87
        }
 
88
 
 
89
        fd = my_create(fullpath, 0, O_WRONLY | O_BINARY | O_EXCL | O_NOFOLLOW,
 
90
                     MYF(MY_WME));
 
91
        if (fd < 0) {
 
92
                return NULL;
 
93
        }
 
94
 
 
95
        path_len = strlen(fullpath) + 1; /* terminating '\0' */
 
96
 
 
97
        file = (ds_file_t *) my_malloc(sizeof(ds_file_t) +
 
98
                                       sizeof(ds_local_file_t) +
 
99
                                       path_len,
 
100
                                       MYF(MY_FAE));
 
101
        local_file = (ds_local_file_t *) (file + 1);
 
102
 
 
103
        local_file->fd = fd;
 
104
 
 
105
        file->path = (char *) local_file + sizeof(ds_local_file_t);
 
106
        memcpy(file->path, fullpath, path_len);
 
107
 
 
108
        file->ptr = local_file;
 
109
 
 
110
        return file;
 
111
}
 
112
 
 
113
static
 
114
int
 
115
local_write(ds_file_t *file, const void *buf, size_t len)
 
116
{
 
117
        File fd = ((ds_local_file_t *) file->ptr)->fd;
 
118
 
 
119
        if (!my_write(fd, buf, len, MYF(MY_WME | MY_NABP))) {
 
120
#ifdef USE_POSIX_FADVISE
 
121
                posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
 
122
#endif
 
123
                return 0;
 
124
        }
 
125
 
 
126
        return 1;
 
127
}
 
128
 
 
129
static
 
130
int
 
131
local_close(ds_file_t *file)
 
132
{
 
133
        File fd = ((ds_local_file_t *) file->ptr)->fd;
 
134
 
 
135
        MY_FREE(file);
 
136
 
 
137
        my_sync(fd, MYF(MY_WME));
 
138
 
 
139
        return my_close(fd, MYF(MY_WME));
 
140
}
 
141
 
 
142
static
 
143
void
 
144
local_deinit(ds_ctxt_t *ctxt)
 
145
{
 
146
        MY_FREE(ctxt->root);
 
147
        MY_FREE(ctxt);
 
148
}