use of org.corfudb.runtime.exceptions.TrimmedException in project CorfuDB by CorfuDB.
the class StreamLogFilesTest method testPrefixTrim.
@Test
public void testPrefixTrim() {
String logDir = getContext().getServerConfig().get("--log-path") + File.separator + "log";
StreamLog log = new StreamLogFiles(getContext(), false);
// Write 50 segments and trim the first 25
final long numSegments = 50;
final long filesPerSegment = 3;
for (long x = 0; x < numSegments * StreamLogFiles.RECORDS_PER_LOG_FILE; x++) {
writeToLog(log, x);
}
File logs = new File(logDir);
assertThat((long) logs.list().length).isEqualTo(numSegments * filesPerSegment);
final long endSegment = 25;
long trimAddress = endSegment * StreamLogFiles.RECORDS_PER_LOG_FILE + 1;
log.prefixTrim(trimAddress);
log.compact();
// Verify that first 25 segments have been deleted
String[] afterTrimFiles = logs.list();
assertThat((long) afterTrimFiles.length).isEqualTo((numSegments - endSegment) * filesPerSegment);
Set<String> fileNames = new HashSet(Arrays.asList(afterTrimFiles));
for (long x = endSegment + 1; x < numSegments; x++) {
String logFile = Long.toString(x) + ".log";
String trimmedLogFile = StreamLogFiles.getTrimmedFilePath(logFile);
String pendingLogFile = StreamLogFiles.getPendingTrimsFilePath(logFile);
assertThat(fileNames).contains(logFile);
assertThat(fileNames).contains(trimmedLogFile);
assertThat(fileNames).contains(pendingLogFile);
}
// Try to trim an address that is less than the new starting address
assertThatThrownBy(() -> log.prefixTrim(trimAddress)).isInstanceOf(TrimmedException.class);
long trimmedExceptions = 0;
// Try to read trimmed addresses
for (long x = 0; x < numSegments * StreamLogFiles.RECORDS_PER_LOG_FILE; x++) {
try {
log.read(x);
} catch (TrimmedException e) {
trimmedExceptions++;
}
}
// Address 0 is not reflected in trimAddress
assertThat(trimmedExceptions).isEqualTo(trimAddress + 1);
}
use of org.corfudb.runtime.exceptions.TrimmedException in project CorfuDB by CorfuDB.
the class InMemoryStreamLog method append.
@Override
public synchronized void append(long address, LogData entry) {
try {
checkRange(address);
} catch (TrimmedException e) {
throw new OverwriteException();
}
if (logCache.containsKey(address)) {
throwLogUnitExceptionsIfNecessary(address, entry);
}
logCache.put(address, entry);
globalTail.getAndUpdate(maxTail -> entry.getGlobalAddress() > maxTail ? entry.getGlobalAddress() : maxTail);
}
use of org.corfudb.runtime.exceptions.TrimmedException in project CorfuDB by CorfuDB.
the class StreamLogFiles method append.
@Override
public void append(long address, LogData entry) {
//evict the data by getting the next pointer.
try {
// make sure the entry doesn't currently exist...
// (probably need a faster way to do this - high watermark?)
SegmentHandle fh = getSegmentHandleForAddress(address);
if (fh.getKnownAddresses().containsKey(address) || fh.getTrimmedAddresses().contains(address)) {
if (entry.getRank() == null) {
throw new OverwriteException();
} else {
// the method below might throw DataOutrankedException or ValueAdoptedException
assertAppendPermittedUnsafe(address, entry);
AddressMetaData addressMetaData = writeRecord(fh, address, entry);
fh.getKnownAddresses().put(address, addressMetaData);
}
} else {
AddressMetaData addressMetaData = writeRecord(fh, address, entry);
fh.getKnownAddresses().put(address, addressMetaData);
}
log.trace("Disk_write[{}]: Written to disk.", address);
} catch (IOException e) {
log.error("Disk_write[{}]: Exception", address, e);
throw new RuntimeException(e);
} catch (TrimmedException e) {
throw new OverwriteException();
}
}
Aggregations