~akopytov/percona-xtrabackup/bug1166888-2.0

« back to all changes in this revision

Viewing changes to src/libarchive/libarchive/archive_read_support_format_raw.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-2009 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
#include "archive_platform.h"
 
26
__FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_raw.c 201107 2009-12-28 03:25:33Z kientzle $");
 
27
 
 
28
#ifdef HAVE_ERRNO_H
 
29
#include <errno.h>
 
30
#endif
 
31
#include <stdio.h>
 
32
#ifdef HAVE_STDLIB_H
 
33
#include <stdlib.h>
 
34
#endif
 
35
 
 
36
#include "archive.h"
 
37
#include "archive_entry.h"
 
38
#include "archive_private.h"
 
39
#include "archive_read_private.h"
 
40
 
 
41
struct raw_info {
 
42
        int64_t offset; /* Current position in the file. */
 
43
        int     end_of_file;
 
44
};
 
45
 
 
46
static int      archive_read_format_raw_bid(struct archive_read *);
 
47
static int      archive_read_format_raw_cleanup(struct archive_read *);
 
48
static int      archive_read_format_raw_read_data(struct archive_read *,
 
49
                    const void **, size_t *, off_t *);
 
50
static int      archive_read_format_raw_read_data_skip(struct archive_read *);
 
51
static int      archive_read_format_raw_read_header(struct archive_read *,
 
52
                    struct archive_entry *);
 
53
 
 
54
int
 
55
archive_read_support_format_raw(struct archive *_a)
 
56
{
 
57
        struct raw_info *info;
 
58
        struct archive_read *a = (struct archive_read *)_a;
 
59
        int r;
 
60
 
 
61
        info = (struct raw_info *)calloc(1, sizeof(*info));
 
62
        if (info == NULL) {
 
63
                archive_set_error(&a->archive, ENOMEM,
 
64
                    "Can't allocate raw_info data");
 
65
                return (ARCHIVE_FATAL);
 
66
        }
 
67
 
 
68
        r = __archive_read_register_format(a,
 
69
            info,
 
70
            "raw",
 
71
            archive_read_format_raw_bid,
 
72
            NULL,
 
73
            archive_read_format_raw_read_header,
 
74
            archive_read_format_raw_read_data,
 
75
            archive_read_format_raw_read_data_skip,
 
76
            archive_read_format_raw_cleanup);
 
77
        if (r != ARCHIVE_OK)
 
78
                free(info);
 
79
        return (r);
 
80
}
 
81
 
 
82
/*
 
83
 * Bid 1 if this is a non-empty file.  Anyone who can really support
 
84
 * this should outbid us, so it should generally be safe to use "raw"
 
85
 * in conjunction with other formats.  But, this could really confuse
 
86
 * folks if there are bid errors or minor file damage, so we don't
 
87
 * include "raw" as part of support_format_all().
 
88
 */
 
89
static int
 
90
archive_read_format_raw_bid(struct archive_read *a)
 
91
{
 
92
 
 
93
        if (__archive_read_ahead(a, 1, NULL) == NULL)
 
94
                return (-1);
 
95
        return (1);
 
96
}
 
97
 
 
98
/*
 
99
 * Mock up a fake header.
 
100
 */
 
101
static int
 
102
archive_read_format_raw_read_header(struct archive_read *a,
 
103
    struct archive_entry *entry)
 
104
{
 
105
        struct raw_info *info;
 
106
 
 
107
        info = (struct raw_info *)(a->format->data);
 
108
        if (info->end_of_file)
 
109
                return (ARCHIVE_EOF);
 
110
 
 
111
        a->archive.archive_format = ARCHIVE_FORMAT_RAW;
 
112
        a->archive.archive_format_name = "Raw data";
 
113
        archive_entry_set_pathname(entry, "data");
 
114
        /* XXX should we set mode to mimic a regular file? XXX */
 
115
        /* I'm deliberately leaving most fields unset here. */
 
116
        return (ARCHIVE_OK);
 
117
}
 
118
 
 
119
static int
 
120
archive_read_format_raw_read_data(struct archive_read *a,
 
121
    const void **buff, size_t *size, off_t *offset)
 
122
{
 
123
        struct raw_info *info;
 
124
        ssize_t avail;
 
125
 
 
126
        info = (struct raw_info *)(a->format->data);
 
127
        if (info->end_of_file)
 
128
                return (ARCHIVE_EOF);
 
129
 
 
130
        /* Get whatever bytes are immediately available. */
 
131
        *buff = __archive_read_ahead(a, 1, &avail);
 
132
        if (avail > 0) {
 
133
                /* Consume and return the bytes we just read */
 
134
                __archive_read_consume(a, avail);
 
135
                *size = avail;
 
136
                *offset = info->offset;
 
137
                info->offset += *size;
 
138
                return (ARCHIVE_OK);
 
139
        } else if (0 == avail) {
 
140
                /* Record and return end-of-file. */
 
141
                info->end_of_file = 1;
 
142
                *size = 0;
 
143
                *offset = info->offset;
 
144
                return (ARCHIVE_EOF);
 
145
        } else {
 
146
                /* Record and return an error. */
 
147
                *size = 0;
 
148
                *offset = info->offset;
 
149
                return (avail);
 
150
        }
 
151
}
 
152
 
 
153
static int
 
154
archive_read_format_raw_read_data_skip(struct archive_read *a)
 
155
{
 
156
        struct raw_info *info;
 
157
        off_t bytes_skipped;
 
158
        int64_t request = 1024 * 1024 * 1024UL; /* Skip 1 GB at a time. */
 
159
 
 
160
        info = (struct raw_info *)(a->format->data);
 
161
        if (info->end_of_file)
 
162
                return (ARCHIVE_EOF);
 
163
        info->end_of_file = 1;
 
164
 
 
165
        for (;;) {
 
166
                bytes_skipped = __archive_read_skip_lenient(a, request);
 
167
                if (bytes_skipped < 0)
 
168
                        return (ARCHIVE_FATAL);
 
169
                if (bytes_skipped < request)
 
170
                        return (ARCHIVE_OK);
 
171
                /* We skipped all the bytes we asked for.  There might
 
172
                 * be more, so try again. */
 
173
        }
 
174
}
 
175
 
 
176
static int
 
177
archive_read_format_raw_cleanup(struct archive_read *a)
 
178
{
 
179
        struct raw_info *info;
 
180
 
 
181
        info = (struct raw_info *)(a->format->data);
 
182
        free(info);
 
183
        a->format->data = NULL;
 
184
        return (ARCHIVE_OK);
 
185
}