Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoiding a corrupt image file when there is image.ckpt with non-zero … #9180

Merged
merged 3 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1966,8 +1966,13 @@ public void saveImage() throws IOException {
}

public void saveImage(File curFile, long replayedJournalId) throws IOException {
if (!curFile.exists()) {
curFile.createNewFile();
if (curFile.exists()) {
if (!curFile.delete()) {
throw new IOException(curFile.getName() + " can not be deleted.");
}
}
if (!curFile.createNewFile()) {
throw new IOException(curFile.getName() + " can not be created.");
}
MetaWriter.write(curFile, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public static void write(File imageFile, List<MetaIndex> metaIndices, long check
long endIndex = raf.length();
raf.writeLong(endIndex - startIndex);
MetaMagicNumber.write(raf);
raf.getFD().sync();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,15 @@ public static MetaHeader read(File imageFile) throws IOException {
}

public static long write(File imageFile) throws IOException {
if (imageFile.length() != 0) {
throw new IOException("Meta header has to be written to an empty file.");
}

try (RandomAccessFile raf = new RandomAccessFile(imageFile, "rw")) {
raf.seek(0);
MetaMagicNumber.write(raf);
MetaJsonHeader.write(raf);
raf.getFD().sync();
return raf.getFilePointer();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ public long doWork(String name, WriteMethod method) throws IOException {

public static void write(File imageFile, Catalog catalog) throws IOException {
// save image does not need any lock. because only checkpoint thread will call this method.
LOG.info("start save image to {}. is ckpt: {}", imageFile.getAbsolutePath(), Catalog.isCheckpointThread());

LOG.info("start to save image to {}. is ckpt: {}", imageFile.getAbsolutePath(), Catalog.isCheckpointThread());
FileOutputStream imageFileOut = new FileOutputStream(imageFile);
final Reference<Long> checksum = new Reference<>(0L);
long saveImageStartTime = System.currentTimeMillis();
// MetaHeader should use output stream in the future.
long startPosition = MetaHeader.write(imageFile);
List<MetaIndex> metaIndices = Lists.newArrayList();
try (CountingDataOutputStream dos = new CountingDataOutputStream(new BufferedOutputStream(
new FileOutputStream(imageFile, true)), startPosition)) {
imageFileOut), startPosition)) {
lide-reed marked this conversation as resolved.
Show resolved Hide resolved
writer.setDelegate(dos, metaIndices);
long replayedJournalId = catalog.getReplayedJournalId();
checksum.setRef(writer.doWork("header", () -> catalog.saveHeader(dos, replayedJournalId, checksum.getRef())));
Expand Down Expand Up @@ -129,6 +130,7 @@ public static void write(File imageFile, Catalog catalog) throws IOException {
checksum.setRef(writer.doWork("deleteHandler", () -> catalog.saveDeleteHandler(dos, checksum.getRef())));
checksum.setRef(writer.doWork("sqlBlockRule", () -> catalog.saveSqlBlockRule(dos, checksum.getRef())));
}
imageFileOut.getFD().sync();
MetaFooter.write(imageFile, metaIndices, checksum.getRef());

long saveImageEndTime = System.currentTimeMillis();
Expand Down