Skip to content

Commit

Permalink
Update BinLogWriter.java
Browse files Browse the repository at this point in the history
Add fix for file descriptor leak on cutover by closing channel when cutover happens. Prevent leak of newRaf when there is an error.
  • Loading branch information
espaillato authored Jun 24, 2022
1 parent 6449f59 commit 9034c95
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions modules/binlog/src/main/java/org/jpos/binlog/BinLogWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,43 @@ public void cutover () throws IOException {
if (readStatus() != Status.OPEN)
throw new IOException ("BinLog not open");
AsynchronousFileChannel newRaf = openOrCreateFile(dir, ++fileNumber);
ByteBuffer index = ByteBuffer.allocate(4);
index.putInt(fileNumber);
index.flip();
try {
int write = raf.write(index, NEXT_LOG_INDEX_OFFSET).get();
if (write != 4) {
throw new IOException ("Failed to write 4 byte NEXT_LOG_INDEX_OFFSET, return: " + write);
ByteBuffer index = ByteBuffer.allocate(4);
index.putInt(fileNumber);
index.flip();
try {
int write = raf.write(index, NEXT_LOG_INDEX_OFFSET).get();
if (write != 4) {
throw new IOException ("Failed to write 4 byte NEXT_LOG_INDEX_OFFSET, return: " + write);
}
} catch (InterruptedException e) {
throw new IOException (e.getMessage());
} catch (ExecutionException e) {
throw new IOException (e.getMessage());
}
} catch (InterruptedException e) {
throw new IOException (e.getMessage());
} catch (ExecutionException e) {
throw new IOException (e.getMessage());
}
ByteBuffer status = ByteBuffer.allocate(2);
status.putShort(Status.CLOSED.shortValue());
status.flip();
try {
int write = raf.write(status, STATUS_OFFSET).get();
if (write != 2) {
throw new IOException ("Failed to write 2 byte STATUS_OFFSET, return: " + write);
ByteBuffer status = ByteBuffer.allocate(2);
status.putShort(Status.CLOSED.shortValue());
status.flip();
try {
int write = raf.write(status, STATUS_OFFSET).get();
if (write != 2) {
throw new IOException ("Failed to write 2 byte STATUS_OFFSET, return: " + write);
}
} catch (InterruptedException e) {
throw new IOException (e.getMessage());
} catch (ExecutionException e) {
throw new IOException (e.getMessage());
}
channel.force(false);
raf = newRaf;
} finally {
lock.release();
if (newRaf == raf) {
channel.close();
} else {
newRaf.close();
}
} catch (InterruptedException e) {
throw new IOException (e.getMessage());
} catch (ExecutionException e) {
throw new IOException (e.getMessage());
}
channel.force(false);
raf = newRaf;
lock.release();
} else {
throw new IOException ("Failed to acquire file lock");
}
Expand Down

0 comments on commit 9034c95

Please sign in to comment.