package org.apache.cassandra.db.lifecycle;
import java.io.File;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterables;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.cassandra.db.compaction.OperationType;
import org.apache.cassandra.db.lifecycle.LogRecord.Type;
import org.apache.cassandra.io.sstable.SSTable;
import org.apache.cassandra.io.sstable.format.SSTableReader;
import org.apache.cassandra.io.sstable.format.big.BigFormat;
import org.apache.cassandra.utils.Throwables;
import static org.apache.cassandra.utils.Throwables.merge;
final class LogFile implements AutoCloseable
{
private static final Logger logger = LoggerFactory.getLogger(LogFile.class);
static String EXT = ".log";
static char SEP = '_';
static Pattern FILE_REGEX = Pattern.compile(String.format("^(.{2})_txn_(.*)_(.*)%s$", EXT));
private final LogReplicaSet replicas = new LogReplicaSet();
private final LinkedHashSet<LogRecord> records = new LinkedHashSet<>();
private final OperationType type;
private final UUID id;
static LogFile make(File logReplica)
{
return make(logReplica.getName(), Collections.singletonList(logReplica));
}
static LogFile make(String fileName, List<File> logReplicas)
{
Matcher matcher = LogFile.FILE_REGEX.matcher(fileName);
boolean matched = matcher.matches();
assert matched && matcher.groupCount() == 3;
OperationType operationType = OperationType.fromFileName(matcher.group(2));
UUID id = UUID.fromString(matcher.group(3));
return new LogFile(operationType, id, logReplicas);
}
Throwable syncDirectory(Throwable accumulate)
{
return replicas.syncDirectory(accumulate);
}
OperationType type()
{
return type;
}
UUID id()
{
return id;
}
Throwable removeUnfinishedLeftovers(Throwable accumulate)
{
try
{
Throwables.maybeFail(syncDirectory(accumulate));
deleteFilesForRecordsOfType(committed() ? Type.REMOVE : Type.ADD);
Throwables.maybeFail(syncDirectory(accumulate));
accumulate = replicas.delete(accumulate);
}
catch (Throwable t)
{
accumulate = merge(accumulate, t);
}
return accumulate;
}
static boolean isLogFile(File file)
{
return LogFile.FILE_REGEX.matcher(file.getName()).matches();
}
LogFile(OperationType type, UUID id, List<File> replicas)
{
this(type, id);
this.replicas.addReplicas(replicas);
}
LogFile(OperationType type, UUID id)
{
this.type = type;
this.id = id;
}
boolean verify()
{
records.clear();
if (!replicas.readRecords(records))
{
logger.error("Failed to read records for transaction log {}", this);
return false;
}
records.forEach(LogFile::verifyRecord);
Optional<LogRecord> firstInvalid = records.stream().filter(LogRecord::isInvalidOrPartial).findFirst();
if (!firstInvalid.isPresent())
return true;
LogRecord failedOn = firstInvalid.get();
if (getLastRecord() != failedOn)
{
setErrorInReplicas(failedOn);
return false;
}
records.stream().filter((r) -> r != failedOn).forEach(LogFile::verifyRecordWithCorruptedLastRecord);
if (records.stream()
.filter((r) -> r != failedOn)
.filter(LogRecord::isInvalid)
.map(this::setErrorInReplicas)
.findFirst().isPresent())
{
setErrorInReplicas(failedOn);
return false;
}
logger.warn("Last record of transaction {} is corrupt or incomplete [{}], " +
"but all previous records match state on disk; continuing",
id, failedOn.error());
return true;
}
LogRecord setErrorInReplicas(LogRecord record)
{
replicas.setErrorInReplicas(record);
return record;
}
static void verifyRecord(LogRecord record)
{
if (record.checksum != record.computeChecksum())
{
record.setError(String.format("Invalid checksum for sstable [%s]: [%d] should have been [%d]",
record.fileName(),
record.checksum,
record.computeChecksum()));
return;
}
if (record.type != Type.REMOVE)
return;
record.status.onDiskRecord = record.withExistingFiles();
if (record.updateTime != record.status.onDiskRecord.updateTime && record.status.onDiskRecord.updateTime > 0)
{
record.setError(String.format("Unexpected files detected for sstable [%s]: " +
"last update time [%tT] should have been [%tT]",
record.fileName(),
record.status.onDiskRecord.updateTime,
record.updateTime));
}
}
static void verifyRecordWithCorruptedLastRecord(LogRecord record)
{
if (record.type == Type.REMOVE && record.status.onDiskRecord.numFiles < record.numFiles)
{
record.setError(String.format("Incomplete fileset detected for sstable [%s]: " +
"number of files [%d] should have been [%d].",
record.fileName(),
record.status.onDiskRecord.numFiles,
record.numFiles));
}
}
void commit()
{
addRecord(LogRecord.makeCommit(System.currentTimeMillis()));
}
void abort()
{
addRecord(LogRecord.makeAbort(System.currentTimeMillis()));
}
private boolean isLastRecordValidWithType(Type type)
{
LogRecord lastRecord = getLastRecord();
return lastRecord != null &&
lastRecord.type == type &&
lastRecord.isValid();
}
boolean committed()
{
return isLastRecordValidWithType(Type.COMMIT);
}
boolean aborted()
{
return isLastRecordValidWithType(Type.ABORT);
}
boolean completed()
{
return committed() || aborted();
}
void add(Type type, SSTable table)
{
addRecord(makeRecord(type, table));
}
public void addAll(Type type, Iterable<SSTableReader> toBulkAdd)
{
for (LogRecord record : makeRecords(type, toBulkAdd).values())
addRecord(record);
}
Map<SSTable, LogRecord> makeRecords(Type type, Iterable<SSTableReader> tables)
{
assert type == Type.ADD || type == Type.REMOVE;
for (SSTableReader sstable : tables)
{
File directory = sstable.descriptor.directory;
String fileName = StringUtils.join(directory, File.separator, getFileName());
replicas.maybeCreateReplica(directory, fileName, records);
}
return LogRecord.make(type, tables);
}
private LogRecord makeRecord(Type type, SSTable table)
{
assert type == Type.ADD || type == Type.REMOVE;
File directory = table.descriptor.directory;
String fileName = StringUtils.join(directory, File.separator, getFileName());
replicas.maybeCreateReplica(directory, fileName, records);
return LogRecord.make(type, table);
}
private LogRecord makeRecord(Type type, SSTable table, LogRecord record)
{
assert type == Type.ADD || type == Type.REMOVE;
File directory = table.descriptor.directory;
String fileName = StringUtils.join(directory, File.separator, getFileName());
replicas.maybeCreateReplica(directory, fileName, records);
return record.asType(type);
}
void addRecord(LogRecord record)
{
if (completed())
throw new IllegalStateException("Transaction already completed");
if (records.contains(record))
throw new IllegalStateException("Record already exists");
replicas.append(record);
if (!records.add(record))
throw new IllegalStateException("Failed to add record");
}
void remove(Type type, SSTable table)
{
LogRecord record = makeRecord(type, table);
assert records.contains(record) : String.format("[%s] is not tracked by %s", record, id);
deleteRecordFiles(record);
records.remove(record);
}
boolean contains(Type type, SSTable table)
{
return contains(makeRecord(type, table));
}
boolean contains(Type type, SSTable sstable, LogRecord record)
{
return contains(makeRecord(type, sstable, record));
}
private boolean contains(LogRecord record)
{
return records.contains(record);
}
void deleteFilesForRecordsOfType(Type type)
{
records.stream()
.filter(type::matches)
.forEach(LogFile::deleteRecordFiles);
records.clear();
}
private static void deleteRecordFiles(LogRecord record)
{
List<File> files = record.getExistingFiles();
files.sort((f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified()));
files.forEach(LogTransaction::delete);
}
Map<LogRecord, Set<File>> getFilesOfType(Path folder, NavigableSet<File> files, Type type)
{
Map<LogRecord, Set<File>> ret = new HashMap<>();
records.stream()
.filter(type::matches)
.filter(LogRecord::isValid)
.filter(r -> r.isInFolder(folder))
.forEach((r) -> ret.put(r, getRecordFiles(files, r)));
return ret;
}
LogRecord getLastRecord()
{
return Iterables.getLast(records, null);
}
private static Set<File> getRecordFiles(NavigableSet<File> files, LogRecord record)
{
String fileName = record.fileName();
return files.stream().filter(f -> f.getName().startsWith(fileName)).collect(Collectors.toSet());
}
boolean exists()
{
return replicas.exists();
}
public void close()
{
replicas.close();
}
@Override
public String toString()
{
return toString(false);
}
public String toString(boolean showContents)
{
StringBuilder str = new StringBuilder();
str.append('[');
str.append(getFileName());
str.append(" in ");
str.append(replicas.getDirectories());
str.append(']');
if (showContents)
{
str.append(System.lineSeparator());
str.append("Files and contents follow:");
str.append(System.lineSeparator());
replicas.printContentsWithAnyErrors(str);
}
return str.toString();
}
@VisibleForTesting
List<File> getFiles()
{
return replicas.getFiles();
}
@VisibleForTesting
List<String> getFilePaths()
{
return replicas.getFilePaths();
}
private String getFileName()
{
return StringUtils.join(BigFormat.latestVersion,
LogFile.SEP,
"txn",
LogFile.SEP,
type.fileName,
LogFile.SEP,
id.toString(),
LogFile.EXT);
}
public boolean isEmpty()
{
return records.isEmpty();
}
}