summaryrefslogtreecommitdiff
path: root/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java
blob: 847b12c28ff5c1a803e3a2e5d9469277208705d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.zookeeper.server.persistence;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.jute.InputArchive;
import org.apache.jute.OutputArchive;
import org.apache.zookeeper.common.AtomicFileOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xerial.snappy.SnappyCodec;
import org.xerial.snappy.SnappyInputStream;
import org.xerial.snappy.SnappyOutputStream;

/**
 * Represent the Stream used in serialize and deserialize the Snapshot.
 */
public class SnapStream {

    private static final Logger LOG = LoggerFactory.getLogger(SnapStream.class);

    public static final String ZOOKEEPER_SHAPSHOT_STREAM_MODE = "zookeeper.snapshot.compression.method";

    private static StreamMode streamMode = StreamMode.fromString(
        System.getProperty(ZOOKEEPER_SHAPSHOT_STREAM_MODE,
                           StreamMode.DEFAULT_MODE.getName()));

    static {
        LOG.info("{} = {}", ZOOKEEPER_SHAPSHOT_STREAM_MODE, streamMode);
    }

    public enum StreamMode {
        GZIP("gz"),
        SNAPPY("snappy"),
        CHECKED("");

        public static final StreamMode DEFAULT_MODE = CHECKED;

        private String name;

        StreamMode(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public String getFileExtension() {
            return name.isEmpty() ? "" : "." + name;
        }

        public static StreamMode fromString(String name) {
            for (StreamMode c : values()) {
                if (c.getName().compareToIgnoreCase(name) == 0) {
                    return c;
                }
            }
            return DEFAULT_MODE;
        }
    }

    /**
     * Return the CheckedInputStream based on the extension of the fileName.
     *
     * @param file the file the InputStream read from
     * @return the specific InputStream
     * @throws IOException
     */
    public static CheckedInputStream getInputStream(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        InputStream is;
        try {
            switch (getStreamMode(file.getName())) {
                case GZIP:
                    is = new GZIPInputStream(fis);
                    break;
                case SNAPPY:
                    is = new SnappyInputStream(fis);
                    break;
                case CHECKED:
                default:
                    is = new BufferedInputStream(fis);
            }
            return new CheckedInputStream(is, new Adler32());
        } catch (IOException e) {
            fis.close();
            throw e;
        }
    }

    /**
     * Return the OutputStream based on predefined stream mode.
     *
     * @param file the file the OutputStream writes to
     * @param fsync sync the file immediately after write
     * @return the specific OutputStream
     * @throws IOException
     */
    public static CheckedOutputStream getOutputStream(File file, boolean fsync) throws IOException {
        OutputStream fos = fsync ? new AtomicFileOutputStream(file) : new FileOutputStream(file);
        OutputStream os;
        switch (streamMode) {
        case GZIP:
            try {
                os = new GZIPOutputStream(fos);
            } catch (IOException e) {
                fos.close();
                throw e;
            }
            break;
        case SNAPPY:
            // Unlike SnappyInputStream, the SnappyOutputStream
            // constructor cannot throw an IOException.
            os = new SnappyOutputStream(fos);
            break;
        case CHECKED:
        default:
            os = new BufferedOutputStream(fos);
        }
        return new CheckedOutputStream(os, new Adler32());
    }

    /**
     * Write specific seal to the OutputArchive and close the OutputStream.
     * Currently, only CheckedOutputStream will write it's checkSum to the
     * end of the stream.
     *
     */
    public static void sealStream(CheckedOutputStream os, OutputArchive oa) throws IOException {
        long val = os.getChecksum().getValue();
        oa.writeLong(val, "val");
        oa.writeString("/", "path");
    }

    /**
     * Verify the integrity of the seal, only CheckedInputStream will verify
     * the checkSum of the content.
     *
     */
    public static void checkSealIntegrity(CheckedInputStream is, InputArchive ia) throws IOException {
        long checkSum = is.getChecksum().getValue();
        long val = ia.readLong("val");
        ia.readString("path");  // Read and ignore "/" written by SealStream.
        if (val != checkSum) {
            throw new IOException("CRC corruption");
        }
    }

    /**
     * Verifies that the file is a valid snapshot. Snapshot may be invalid if
     * it's incomplete as in a situation when the server dies while in the
     * process of storing a snapshot. Any files that are improperly formated
     * or corrupted are invalid. Any file that is not a snapshot is also an
     * invalid snapshot.
     *
     * @param file file to verify
     * @return true if the snapshot is valid
     * @throws IOException
     */
    public static boolean isValidSnapshot(File file) throws IOException {
        if (file == null || Util.getZxidFromName(file.getName(), FileSnap.SNAPSHOT_FILE_PREFIX) == -1) {
            return false;
        }

        boolean isValid = false;
        switch (getStreamMode(file.getName())) {
        case GZIP:
            isValid = isValidGZipStream(file);
            break;
        case SNAPPY:
            isValid = isValidSnappyStream(file);
            break;
        case CHECKED:
        default:
            isValid = isValidCheckedStream(file);
        }
        return isValid;
    }

    public static void setStreamMode(StreamMode mode) {
        streamMode = mode;
    }

    public static StreamMode getStreamMode() {
        return streamMode;
    }

    /**
     * Detect the stream mode from file name extension
     *
     * @param fileName
     * @return the stream mode detected
     */
    public static StreamMode getStreamMode(String fileName) {
        String[] splitSnapName = fileName.split("\\.");

        // Use file extension to detect format
        if (splitSnapName.length > 1) {
            String mode = splitSnapName[splitSnapName.length - 1];
            return StreamMode.fromString(mode);
        }

        return StreamMode.CHECKED;
    }

    /**
     * Certify the GZip stream integrity by checking the header
     * for the GZip magic string
     *
     * @param f file to verify
     * @return true if it has the correct GZip magic string
     * @throws IOException
     */
    private static boolean isValidGZipStream(File f) throws IOException {
        byte[] byteArray = new byte[2];
        try (FileInputStream fis = new FileInputStream(f)) {
            if (2 != fis.read(byteArray, 0, 2)) {
                LOG.error("Read incorrect number of bytes from {}", f.getName());
                return false;
            }
            ByteBuffer bb = ByteBuffer.wrap(byteArray);
            byte[] magicHeader = new byte[2];
            bb.get(magicHeader, 0, 2);
            int magic = magicHeader[0] & 0xff | ((magicHeader[1] << 8) & 0xff00);
            return magic == GZIPInputStream.GZIP_MAGIC;
        } catch (FileNotFoundException e) {
            LOG.error("Unable to open file {}", f.getName(), e);
            return false;
        }
    }

    /**
     * Certify the Snappy stream integrity by checking the header
     * for the Snappy magic string
     *
     * @param f file to verify
     * @return true if it has the correct Snappy magic string
     * @throws IOException
     */
    private static boolean isValidSnappyStream(File f) throws IOException {
        byte[] byteArray = new byte[SnappyCodec.MAGIC_LEN];
        try (FileInputStream fis = new FileInputStream(f)) {
            if (SnappyCodec.MAGIC_LEN != fis.read(byteArray, 0, SnappyCodec.MAGIC_LEN)) {
                LOG.error("Read incorrect number of bytes from {}", f.getName());
                return false;
            }
            ByteBuffer bb = ByteBuffer.wrap(byteArray);
            byte[] magicHeader = new byte[SnappyCodec.MAGIC_LEN];
            bb.get(magicHeader, 0, SnappyCodec.MAGIC_LEN);
            return Arrays.equals(magicHeader, SnappyCodec.getMagicHeader());
        } catch (FileNotFoundException e) {
            LOG.error("Unable to open file {}", f.getName(), e);
            return false;
        }
    }

    /**
     * Certify the Checked stream integrity by checking the header
     * length and format
     *
     * @param f file to verify
     * @return true if it has the correct header
     * @throws IOException
     */
    private static boolean isValidCheckedStream(File f) throws IOException {
        try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
            // including the header and the last / bytes
            // the snapshot should be at least 10 bytes
            if (raf.length() < 10) {
                return false;
            }

            raf.seek(raf.length() - 5);
            byte[] bytes = new byte[5];
            int readlen = 0;
            int l;
            while (readlen < 5 && (l = raf.read(bytes, readlen, bytes.length - readlen)) >= 0) {
                readlen += l;
            }
            if (readlen != bytes.length) {
                LOG.info("Invalid snapshot {}. too short, len = {} bytes", f.getName(), readlen);
                return false;
            }
            ByteBuffer bb = ByteBuffer.wrap(bytes);
            int len = bb.getInt();
            byte b = bb.get();
            if (len != 1 || b != '/') {
                LOG.info("Invalid snapshot {}. len = {}, byte = {}", f.getName(), len, (b & 0xff));
                return false;
            }
        }

        return true;
    }

}