forked from apache/zookeeper
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZOOKEEPER-2249: CRC check failed when preAllocSize smaller than node …
…data
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/java/test/org/apache/zookeeper/test/FileTxnLogPaddingCorruptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[]{})); | ||
} | ||
|
||
} |