~akopytov/percona-xtrabackup/bug1166888-2.0

« back to all changes in this revision

Viewing changes to src/libarchive/libarchive/archive_read_open_file.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) 2003-2007 Tim Kientzle
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 * 1. Redistributions of source code must retain the above copyright
 
9
 *    notice, this list of conditions and the following disclaimer.
 
10
 * 2. Redistributions in binary form must reproduce the above copyright
 
11
 *    notice, this list of conditions and the following disclaimer in the
 
12
 *    documentation and/or other materials provided with the distribution.
 
13
 *
 
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 
15
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
16
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
17
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 
18
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
19
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
20
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
21
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
22
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 
23
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
#include "archive_platform.h"
 
27
__FBSDID("$FreeBSD: head/lib/libarchive/archive_read_open_file.c 201093 2009-12-28 02:28:44Z kientzle $");
 
28
 
 
29
#ifdef HAVE_SYS_STAT_H
 
30
#include <sys/stat.h>
 
31
#endif
 
32
#ifdef HAVE_ERRNO_H
 
33
#include <errno.h>
 
34
#endif
 
35
#ifdef HAVE_FCNTL_H
 
36
#include <fcntl.h>
 
37
#endif
 
38
#ifdef HAVE_IO_H
 
39
#include <io.h>
 
40
#endif
 
41
#ifdef HAVE_STDLIB_H
 
42
#include <stdlib.h>
 
43
#endif
 
44
#ifdef HAVE_STRING_H
 
45
#include <string.h>
 
46
#endif
 
47
#ifdef HAVE_UNISTD_H
 
48
#include <unistd.h>
 
49
#endif
 
50
 
 
51
#include "archive.h"
 
52
 
 
53
struct read_FILE_data {
 
54
        FILE    *f;
 
55
        size_t   block_size;
 
56
        void    *buffer;
 
57
        char     can_skip;
 
58
};
 
59
 
 
60
static int      file_close(struct archive *, void *);
 
61
static ssize_t  file_read(struct archive *, void *, const void **buff);
 
62
#if ARCHIVE_API_VERSION < 2
 
63
static ssize_t  file_skip(struct archive *, void *, size_t request);
 
64
#else
 
65
static off_t    file_skip(struct archive *, void *, off_t request);
 
66
#endif
 
67
 
 
68
int
 
69
archive_read_open_FILE(struct archive *a, FILE *f)
 
70
{
 
71
        struct stat st;
 
72
        struct read_FILE_data *mine;
 
73
        size_t block_size = 128 * 1024;
 
74
        void *b;
 
75
 
 
76
        archive_clear_error(a);
 
77
        mine = (struct read_FILE_data *)malloc(sizeof(*mine));
 
78
        b = malloc(block_size);
 
79
        if (mine == NULL || b == NULL) {
 
80
                archive_set_error(a, ENOMEM, "No memory");
 
81
                free(mine);
 
82
                free(b);
 
83
                return (ARCHIVE_FATAL);
 
84
        }
 
85
        mine->block_size = block_size;
 
86
        mine->buffer = b;
 
87
        mine->f = f;
 
88
        /*
 
89
         * If we can't fstat() the file, it may just be that it's not
 
90
         * a file.  (FILE * objects can wrap many kinds of I/O
 
91
         * streams, some of which don't support fileno()).)
 
92
         */
 
93
        if (fstat(fileno(mine->f), &st) == 0 && S_ISREG(st.st_mode)) {
 
94
                archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
 
95
                /* Enable the seek optimization only for regular files. */
 
96
                mine->can_skip = 1;
 
97
        } else
 
98
                mine->can_skip = 0;
 
99
 
 
100
#if defined(__CYGWIN__) || defined(_WIN32)
 
101
        setmode(fileno(mine->f), O_BINARY);
 
102
#endif
 
103
 
 
104
        return (archive_read_open2(a, mine, NULL, file_read,
 
105
                    file_skip, file_close));
 
106
}
 
107
 
 
108
static ssize_t
 
109
file_read(struct archive *a, void *client_data, const void **buff)
 
110
{
 
111
        struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
 
112
        ssize_t bytes_read;
 
113
 
 
114
        *buff = mine->buffer;
 
115
        bytes_read = fread(mine->buffer, 1, mine->block_size, mine->f);
 
116
        if (bytes_read < 0) {
 
117
                archive_set_error(a, errno, "Error reading file");
 
118
        }
 
119
        return (bytes_read);
 
120
}
 
121
 
 
122
#if ARCHIVE_API_VERSION < 2
 
123
static ssize_t
 
124
file_skip(struct archive *a, void *client_data, size_t request)
 
125
#else
 
126
static off_t
 
127
file_skip(struct archive *a, void *client_data, off_t request)
 
128
#endif
 
129
{
 
130
        struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
 
131
 
 
132
        (void)a; /* UNUSED */
 
133
 
 
134
        /*
 
135
         * If we can't skip, return 0 as the amount we did step and
 
136
         * the caller will work around by reading and discarding.
 
137
         */
 
138
        if (!mine->can_skip)
 
139
                return (0);
 
140
        if (request == 0)
 
141
                return (0);
 
142
 
 
143
#if HAVE_FSEEKO
 
144
        if (fseeko(mine->f, request, SEEK_CUR) != 0)
 
145
#else
 
146
        if (fseek(mine->f, request, SEEK_CUR) != 0)
 
147
#endif
 
148
        {
 
149
                mine->can_skip = 0;
 
150
                return (0);
 
151
        }
 
152
        return (request);
 
153
}
 
154
 
 
155
static int
 
156
file_close(struct archive *a, void *client_data)
 
157
{
 
158
        struct read_FILE_data *mine = (struct read_FILE_data *)client_data;
 
159
 
 
160
        (void)a; /* UNUSED */
 
161
        if (mine->buffer != NULL)
 
162
                free(mine->buffer);
 
163
        free(mine);
 
164
        return (ARCHIVE_OK);
 
165
}