Skip to content

Commit

Permalink
ZOOKEEPER-2249: CRC check failed when preAllocSize smaller than node …
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
afine authored and Abraham Fine committed Dec 19, 2017
1 parent 9e30b9b commit 44ad057
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
Expand Up @@ -211,7 +211,7 @@ public static long padLogFile(FileOutputStream f,long currentSize,
long preAllocSize) throws IOException{
long position = f.getChannel().position();
if (position + 4096 >= currentSize) {
currentSize = currentSize + preAllocSize;
currentSize = position + preAllocSize;
fill.position(0);
f.getChannel().write(fill, currentSize-fill.remaining());
}
Expand Down
@@ -0,0 +1,75 @@
/**
* 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.test;

import org.apache.zookeeper.ZKTestCase;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.server.persistence.FileTxnLog;
import org.apache.zookeeper.txn.CreateTxn;
import org.apache.zookeeper.txn.TxnHeader;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class FileTxnLogPaddingCorruptionTest extends ZKTestCase {
protected static final Logger LOG = LoggerFactory.getLogger(FileTxnLogPaddingCorruptionTest.class);

@Test
public void testPreAllocSizeSmallerThanTxnData() throws IOException {
File logDir = ClientBase.createTmpDir();
FileTxnLog fileTxnLog = new FileTxnLog(logDir);

// Set a small preAllocSize (.5 MB)
final int preAllocSize = 500000;
fileTxnLog.setPreallocSize(preAllocSize);

// Create dummy txn larger than preAllocSize + 4KB (the size used in Util#padLogFile to determine if padding is necessary)
// Since the file padding inserts a 0, we will fill the data with 0xff to ensure we corrupt the data if we put the 0 in the data
byte[] data = new byte[2 * preAllocSize];
Arrays.fill(data, (byte) 0xff);

// Append and commit 2 transactions to the log
// Prior to ZOOKEEPER-2249, attempting to pad in association with the second transaction will corrupt the first
fileTxnLog.append(new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.create),
new CreateTxn("/testPreAllocSizeSmallerThanTxnData1", data, ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 0));
fileTxnLog.commit();
fileTxnLog.append(new TxnHeader(1, 1, 2, 2, ZooDefs.OpCode.create),
new CreateTxn("/testPreAllocSizeSmallerThanTxnData2", new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 0));
fileTxnLog.commit();
fileTxnLog.close();

// Read the log back from disk, this will throw a java.io.IOException: CRC check failed prior to ZOOKEEPER-2249
FileTxnLog.FileTxnIterator fileTxnIterator = new FileTxnLog.FileTxnIterator(logDir, 0);

// Verify the data in the first transaction
CreateTxn createTxn = (CreateTxn) fileTxnIterator.getTxn();
Assert.assertTrue(Arrays.equals(createTxn.getData(), data));

// Verify the data in the second transaction
fileTxnIterator.next();
createTxn = (CreateTxn) fileTxnIterator.getTxn();
Assert.assertTrue(Arrays.equals(createTxn.getData(), new byte[]{}));
}

}

0 comments on commit 44ad057

Please sign in to comment.