~akopytov/percona-xtrabackup/bug1166888-2.0

« back to all changes in this revision

Viewing changes to src/libarchive/libarchive/test/test_write_compress_gzip.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) 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
 *    in this position and unchanged.
 
11
 * 2. Redistributions in binary form must reproduce the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer in the
 
13
 *    documentation and/or other materials provided with the distribution.
 
14
 *
 
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 
16
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
17
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
18
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 
19
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
20
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
21
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
22
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
25
 */
 
26
 
 
27
#include "test.h"
 
28
__FBSDID("$FreeBSD: head/lib/libarchive/test/test_write_compress_gzip.c 191183 2009-04-17 01:06:31Z kientzle $");
 
29
 
 
30
/*
 
31
 * A basic exercise of gzip reading and writing.
 
32
 *
 
33
 * TODO: Add a reference file and make sure we can decompress that.
 
34
 */
 
35
 
 
36
DEFINE_TEST(test_write_compress_gzip)
 
37
{
 
38
        struct archive_entry *ae;
 
39
        struct archive* a;
 
40
        char *buff, *data;
 
41
        size_t buffsize, datasize;
 
42
        char path[16];
 
43
        size_t used1, used2;
 
44
        int i, r;
 
45
 
 
46
        buffsize = 2000000;
 
47
        assert(NULL != (buff = (char *)malloc(buffsize)));
 
48
 
 
49
        datasize = 10000;
 
50
        assert(NULL != (data = (char *)malloc(datasize)));
 
51
        memset(data, 0, datasize);
 
52
 
 
53
        /*
 
54
         * Write a 100 files and read them all back.
 
55
         */
 
56
        assert((a = archive_write_new()) != NULL);
 
57
        assertA(0 == archive_write_set_format_ustar(a));
 
58
        r = archive_write_set_compression_gzip(a);
 
59
        if (r == ARCHIVE_FATAL) {
 
60
                skipping("gzip writing not supported on this platform");
 
61
                assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
 
62
                return;
 
63
        }
 
64
        assertEqualIntA(a, ARCHIVE_OK,
 
65
            archive_write_set_bytes_per_block(a, 10));
 
66
        assertEqualInt(ARCHIVE_COMPRESSION_GZIP, archive_compression(a));
 
67
        assertEqualString("gzip", archive_compression_name(a));
 
68
        assertA(0 == archive_write_open_memory(a, buff, buffsize, &used1));
 
69
        assertEqualInt(ARCHIVE_COMPRESSION_GZIP, archive_compression(a));
 
70
        assertEqualString("gzip", archive_compression_name(a));
 
71
        assert((ae = archive_entry_new()) != NULL);
 
72
        archive_entry_set_filetype(ae, AE_IFREG);
 
73
        archive_entry_set_size(ae, datasize);
 
74
        for (i = 0; i < 100; i++) {
 
75
                sprintf(path, "file%03d", i);
 
76
                archive_entry_copy_pathname(ae, path);
 
77
                assertA(0 == archive_write_header(a, ae));
 
78
                assertA(datasize
 
79
                    == (size_t)archive_write_data(a, data, datasize));
 
80
        }
 
81
        archive_entry_free(ae);
 
82
        archive_write_close(a);
 
83
        assert(0 == archive_write_finish(a));
 
84
 
 
85
        assert((a = archive_read_new()) != NULL);
 
86
        assertA(0 == archive_read_support_format_all(a));
 
87
        r = archive_read_support_compression_gzip(a);
 
88
        if (r == ARCHIVE_WARN) {
 
89
                skipping("Can't verify gzip writing by reading back;"
 
90
                    " gzip reading not fully supported on this platform");
 
91
        } else {
 
92
                assertEqualIntA(a, ARCHIVE_OK,
 
93
                    archive_read_support_compression_all(a));
 
94
                assertEqualIntA(a, ARCHIVE_OK,
 
95
                    archive_read_open_memory(a, buff, used1));
 
96
                for (i = 0; i < 100; i++) {
 
97
                        sprintf(path, "file%03d", i);
 
98
                        if (!assertEqualInt(ARCHIVE_OK,
 
99
                                archive_read_next_header(a, &ae)))
 
100
                                break;
 
101
                        assertEqualString(path, archive_entry_pathname(ae));
 
102
                        assertEqualInt((int)datasize, archive_entry_size(ae));
 
103
                }
 
104
                assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
 
105
        }
 
106
        assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
 
107
 
 
108
        /*
 
109
         * Repeat the cycle again, this time setting some compression
 
110
         * options.
 
111
         */
 
112
        assert((a = archive_write_new()) != NULL);
 
113
        assertA(0 == archive_write_set_format_ustar(a));
 
114
        assertEqualIntA(a, ARCHIVE_OK,
 
115
            archive_write_set_bytes_per_block(a, 10));
 
116
        assertA(0 == archive_write_set_compression_gzip(a));
 
117
        assertEqualIntA(a, ARCHIVE_WARN,
 
118
            archive_write_set_compressor_options(a, "nonexistent-option=0"));
 
119
        assertEqualIntA(a, ARCHIVE_WARN,
 
120
            archive_write_set_compressor_options(a, "compression-level=abc"));
 
121
        assertEqualIntA(a, ARCHIVE_WARN,
 
122
            archive_write_set_compressor_options(a, "compression-level=99"));
 
123
        assertEqualIntA(a, ARCHIVE_OK,
 
124
            archive_write_set_compressor_options(a, "compression-level=9"));
 
125
        assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
 
126
        for (i = 0; i < 100; i++) {
 
127
                sprintf(path, "file%03d", i);
 
128
                assert((ae = archive_entry_new()) != NULL);
 
129
                archive_entry_copy_pathname(ae, path);
 
130
                archive_entry_set_size(ae, datasize);
 
131
                archive_entry_set_filetype(ae, AE_IFREG);
 
132
                assertA(0 == archive_write_header(a, ae));
 
133
                assertA(datasize == (size_t)archive_write_data(a, data, datasize));
 
134
                archive_entry_free(ae);
 
135
        }
 
136
        archive_write_close(a);
 
137
        assert(0 == archive_write_finish(a));
 
138
 
 
139
        /* Curiously, this test fails; the test data above compresses
 
140
         * better at default compression than at level 9. */
 
141
        /*
 
142
        failure("compression-level=9 wrote %d bytes, default wrote %d bytes",
 
143
            (int)used2, (int)used1);
 
144
        assert(used2 < used1);
 
145
        */
 
146
 
 
147
        assert((a = archive_read_new()) != NULL);
 
148
        assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
 
149
        r = archive_read_support_compression_gzip(a);
 
150
        if (r == ARCHIVE_WARN) {
 
151
                skipping("gzip reading not fully supported on this platform");
 
152
        } else {
 
153
                assertEqualIntA(a, ARCHIVE_OK,
 
154
                    archive_read_support_compression_all(a));
 
155
                assertEqualIntA(a, ARCHIVE_OK,
 
156
                    archive_read_open_memory(a, buff, used2));
 
157
                for (i = 0; i < 100; i++) {
 
158
                        sprintf(path, "file%03d", i);
 
159
                        if (!assertEqualInt(ARCHIVE_OK,
 
160
                                archive_read_next_header(a, &ae)))
 
161
                                break;
 
162
                        assertEqualString(path, archive_entry_pathname(ae));
 
163
                        assertEqualInt((int)datasize, archive_entry_size(ae));
 
164
                }
 
165
                assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
 
166
        }
 
167
        assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
 
168
 
 
169
        /*
 
170
         * Repeat again, with much lower compression.
 
171
         */
 
172
        assert((a = archive_write_new()) != NULL);
 
173
        assertA(0 == archive_write_set_format_ustar(a));
 
174
        assertEqualIntA(a, ARCHIVE_OK,
 
175
            archive_write_set_bytes_per_block(a, 10));
 
176
        assertA(0 == archive_write_set_compression_gzip(a));
 
177
        assertEqualIntA(a, ARCHIVE_OK,
 
178
            archive_write_set_compressor_options(a, "compression-level=0"));
 
179
        assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
 
180
        for (i = 0; i < 100; i++) {
 
181
                sprintf(path, "file%03d", i);
 
182
                assert((ae = archive_entry_new()) != NULL);
 
183
                archive_entry_copy_pathname(ae, path);
 
184
                archive_entry_set_size(ae, datasize);
 
185
                archive_entry_set_filetype(ae, AE_IFREG);
 
186
                assertA(0 == archive_write_header(a, ae));
 
187
                failure("Writing file %s", path);
 
188
                assertEqualIntA(a, datasize,
 
189
                    (size_t)archive_write_data(a, data, datasize));
 
190
                archive_entry_free(ae);
 
191
        }
 
192
        archive_write_close(a);
 
193
        assert(0 == archive_write_finish(a));
 
194
 
 
195
        /* Level 0 really does result in larger data. */
 
196
        failure("Compression-level=0 wrote %d bytes; default wrote %d bytes",
 
197
            (int)used2, (int)used1);
 
198
        assert(used2 > used1);
 
199
 
 
200
        assert((a = archive_read_new()) != NULL);
 
201
        assertA(0 == archive_read_support_format_all(a));
 
202
        assertA(0 == archive_read_support_compression_all(a));
 
203
        r = archive_read_support_compression_gzip(a);
 
204
        if (r == ARCHIVE_WARN) {
 
205
                skipping("gzip reading not fully supported on this platform");
 
206
        } else {
 
207
                assertEqualIntA(a, ARCHIVE_OK,
 
208
                    archive_read_open_memory(a, buff, used2));
 
209
                for (i = 0; i < 100; i++) {
 
210
                        sprintf(path, "file%03d", i);
 
211
                        if (!assertEqualInt(ARCHIVE_OK,
 
212
                                archive_read_next_header(a, &ae)))
 
213
                                break;
 
214
                        assertEqualString(path, archive_entry_pathname(ae));
 
215
                        assertEqualInt((int)datasize, archive_entry_size(ae));
 
216
                }
 
217
                assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
 
218
        }
 
219
        assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
 
220
 
 
221
        /*
 
222
         * Test various premature shutdown scenarios to make sure we
 
223
         * don't crash or leak memory.
 
224
         */
 
225
        assert((a = archive_write_new()) != NULL);
 
226
        assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_gzip(a));
 
227
        assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
 
228
 
 
229
        assert((a = archive_write_new()) != NULL);
 
230
        assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_gzip(a));
 
231
        assertEqualInt(ARCHIVE_OK, archive_write_close(a));
 
232
        assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
 
233
 
 
234
        assert((a = archive_write_new()) != NULL);
 
235
        assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
 
236
        assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_gzip(a));
 
237
        assertEqualInt(ARCHIVE_OK, archive_write_close(a));
 
238
        assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
 
239
 
 
240
        assert((a = archive_write_new()) != NULL);
 
241
        assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a));
 
242
        assertEqualIntA(a, ARCHIVE_OK, archive_write_set_compression_gzip(a));
 
243
        assertA(0 == archive_write_open_memory(a, buff, buffsize, &used2));
 
244
        assertEqualInt(ARCHIVE_OK, archive_write_close(a));
 
245
        assertEqualInt(ARCHIVE_OK, archive_write_finish(a));
 
246
 
 
247
        /*
 
248
         * Clean up.
 
249
         */
 
250
        free(data);
 
251
        free(buff);
 
252
}