Skip to content

Commit

Permalink
[Improvement] Implemented try-with-resources for instances of Autoclo…
Browse files Browse the repository at this point in the history
…seable
  • Loading branch information
dk2k committed Jun 25, 2024
1 parent e200aa3 commit 3030303
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,24 @@ private synchronized BufferedLogChannel allocateNewLog(File dirForNextEntryLog,
} while (testLogFile == null);

File newLogFile = new File(dirForNextEntryLog, logFileName);
FileChannel channel = new RandomAccessFile(newLogFile, "rw").getChannel();
try (FileChannel channel = new RandomAccessFile(newLogFile, "rw").getChannel()) {

BufferedLogChannel logChannel = new BufferedLogChannel(byteBufAllocator, channel, conf.getWriteBufferBytes(),
conf.getReadBufferBytes(), preallocatedLogId, newLogFile, conf.getFlushIntervalInBytes());
logfileHeader.readerIndex(0);
logChannel.write(logfileHeader);
BufferedLogChannel logChannel = new BufferedLogChannel(byteBufAllocator, channel, conf.getWriteBufferBytes(),
conf.getReadBufferBytes(), preallocatedLogId, newLogFile, conf.getFlushIntervalInBytes());
logfileHeader.readerIndex(0);
logChannel.write(logfileHeader);

for (File f : ledgersDirs) {
setLastLogId(f, preallocatedLogId);
}
for (File f : ledgersDirs) {
setLastLogId(f, preallocatedLogId);
}

if (suffix.equals(DefaultEntryLogger.LOG_FILE_SUFFIX)) {
recentlyCreatedEntryLogsStatus.createdEntryLog(preallocatedLogId);
}
if (suffix.equals(DefaultEntryLogger.LOG_FILE_SUFFIX)) {
recentlyCreatedEntryLogsStatus.createdEntryLog(preallocatedLogId);
}

log.info("Created new entry log file {} for logId {}.", newLogFile, preallocatedLogId);
return logChannel;
log.info("Created new entry log file {} for logId {}.", newLogFile, preallocatedLogId);
return logChannel;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,20 @@ public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
if (params != null && params.containsKey("ledger_id")) {
ClientConfiguration clientConf = new ClientConfiguration();
clientConf.addConfiguration(conf);
BookKeeper bk = new BookKeeper(clientConf);
Long ledgerId = Long.parseLong(params.get("ledger_id"));
try (BookKeeper bk = new BookKeeper(clientConf)) {
Long ledgerId = Long.parseLong(params.get("ledger_id"));

bk.deleteLedger(ledgerId);
bk.deleteLedger(ledgerId);

String output = "Deleted ledger: " + ledgerId;
String jsonResponse = JsonUtil.toJson(output);
if (LOG.isDebugEnabled()) {
LOG.debug("output body:" + jsonResponse);
String output = "Deleted ledger: " + ledgerId;
String jsonResponse = JsonUtil.toJson(output);
if (LOG.isDebugEnabled()) {
LOG.debug("output body:" + jsonResponse);
}
response.setBody(jsonResponse);
response.setCode(HttpServer.StatusCode.OK);
return response;
}
response.setBody(jsonResponse);
response.setCode(HttpServer.StatusCode.OK);
return response;
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not ledger found. Should provide ledger_id=<id>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ public void deleteLog(String logName)
if (!uri.isPresent()) {
throw new LogNotFoundException("Log " + logName + " isn't found.");
}
DistributedLogManager dlm = openLogInternal(
try (DistributedLogManager dlm = openLogInternal(
uri.get(),
logName,
Optional.empty(),
Optional.empty());
dlm.delete();
Optional.empty())) {
dlm.delete();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,7 @@ public Map<String, byte[]> enumerateLogsWithMetadataInNamespace()
String namespaceRootPath = namespace.getPath();
HashMap<String, byte[]> result = new HashMap<String, byte[]>();
ZooKeeperClient zkc = writerZKC;
try {
ZooKeeper zk = Utils.sync(zkc, namespaceRootPath);
try (ZooKeeper zk = Utils.sync(zkc, namespaceRootPath)) {
Stat currentStat = zk.exists(namespaceRootPath, false);
if (currentStat == null) {
return result;
Expand Down

0 comments on commit 3030303

Please sign in to comment.