in src/java/org/apache/cassandra/io/sstable/format/bti/BtiTableScrubber.java [96:248]
public void scrubInternal(SSTableRewriter writer)
{
if (indexAvailable() && indexIterator.dataPosition() != 0)
{
outputHandler.warn("First position reported by index should be 0, was " +
indexIterator.dataPosition() +
", continuing without index.");
indexIterator.close();
indexIterator = null;
}
DecoratedKey prevKey = null;
while (!dataFile.isEOF())
{
if (scrubInfo.isStopRequested())
throw new CompactionInterruptedException(scrubInfo.getCompactionInfo());
// position in a data file where the partition starts
long dataStart = dataFile.getFilePointer();
outputHandler.debug("Reading row at %d", dataStart);
DecoratedKey key = null;
Throwable keyReadError = null;
try
{
ByteBuffer raw = ByteBufferUtil.readWithShortLength(dataFile);
if (!isIndex)
partitionKeyType.validate(raw);
key = sstable.decorateKey(raw);
}
catch (Throwable th)
{
keyReadError = th;
throwIfFatal(th);
// check for null key below
}
// position of the partition in a data file, it points to the beginning of the partition key
long dataStartFromIndex = -1;
// size of the partition (including partition key)
long dataSizeFromIndex = -1;
ByteBuffer currentIndexKey = null;
if (indexAvailable())
{
currentIndexKey = indexIterator.key();
dataStartFromIndex = indexIterator.dataPosition();
if (!indexIterator.isExhausted())
{
try
{
indexIterator.advance();
if (!indexIterator.isExhausted())
dataSizeFromIndex = indexIterator.dataPosition() - dataStartFromIndex;
}
catch (Throwable th)
{
throwIfFatal(th);
outputHandler.warn(th,
"Failed to advance to the next index position. Index is corrupted. " +
"Continuing without the index. Last position read is %d.",
indexIterator.dataPosition());
indexIterator.close();
indexIterator = null;
currentIndexKey = null;
dataStartFromIndex = -1;
dataSizeFromIndex = -1;
}
}
}
String keyName = key == null ? "(unreadable key)" : keyString(key);
outputHandler.debug("partition %s is %s", keyName, FBUtilities.prettyPrintMemory(dataSizeFromIndex));
try
{
if (key == null)
throw new IOError(new IOException("Unable to read partition key from data file", keyReadError));
if (currentIndexKey != null && !key.getKey().equals(currentIndexKey))
{
throw new IOError(new IOException(String.format("Key from data file (%s) does not match key from index file (%s)",
ByteBufferUtil.bytesToHex(key.getKey()), ByteBufferUtil.bytesToHex(currentIndexKey))));
}
if (indexIterator != null && dataSizeFromIndex > dataFile.length())
throw new IOError(new IOException("Impossible partition size (greater than file length): " + dataSizeFromIndex));
if (indexIterator != null && dataStart != dataStartFromIndex)
outputHandler.warn("Data file partition position %d differs from index file row position %d", dataStart, dataStartFromIndex);
if (tryAppend(prevKey, key, writer))
prevKey = key;
}
catch (Throwable th)
{
throwIfFatal(th);
outputHandler.warn(th, "Error reading partition %s (stacktrace follows):", keyName);
if (currentIndexKey != null
&& (key == null || !key.getKey().equals(currentIndexKey) || dataStart != dataStartFromIndex))
{
// position where the row should start in a data file (right after the partition key)
long rowStartFromIndex = dataStartFromIndex + TypeSizes.SHORT_SIZE + currentIndexKey.remaining();
outputHandler.output("Retrying from partition index; data is %s bytes starting at %s",
dataSizeFromIndex, rowStartFromIndex);
key = sstable.decorateKey(currentIndexKey);
try
{
if (!isIndex)
partitionKeyType.validate(key.getKey());
dataFile.seek(rowStartFromIndex);
if (tryAppend(prevKey, key, writer))
prevKey = key;
}
catch (Throwable th2)
{
throwIfFatal(th2);
throwIfCannotContinue(key, th2);
outputHandler.warn(th2, "Retry failed too. Skipping to next partition (retry's stacktrace follows)");
badPartitions++;
if (!seekToNextPartition())
break;
}
}
else
{
throwIfCannotContinue(key, th);
badPartitions++;
if (indexIterator != null)
{
outputHandler.warn("Partition starting at position %d is unreadable; skipping to next", dataStart);
if (!seekToNextPartition())
break;
}
else
{
outputHandler.warn("Unrecoverable error while scrubbing %s." +
"Scrubbing cannot continue. The sstable will be marked for deletion. " +
"You can attempt manual recovery from the pre-scrub snapshot. " +
"You can also run nodetool repair to transfer the data from a healthy replica, if any.",
sstable);
// There's no way to resync and continue. Give up.
break;
}
}
}
}
}