提交 93aeb497 编写于 作者: C caoyixiong 提交者: 彭勇升 pengys

fix 5.x too many open files (#1970)

* close inputStream safety

* FIX NPE in AlarmService

* fix 5.x too many open files bug

* checkstyle bug

* checkstyle bug

* checkstyle bug
上级 fd8d6bca
......@@ -21,7 +21,6 @@ package org.apache.skywalking.apm.collector.analysis.segment.parser.provider.buf
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.apache.skywalking.apm.collector.core.util.CollectionUtils;
......@@ -44,7 +43,6 @@ public enum OffsetManager {
private File offsetFile;
private Offset offset;
private boolean initialized = false;
private RandomAccessFile randomAccessFile = null;
private String lastOffsetRecord = Const.EMPTY_STRING;
public synchronized void initialize() throws IOException {
......@@ -95,7 +93,7 @@ public enum OffsetManager {
if (offsetFile.length() >= BufferFileConfig.BUFFER_OFFSET_MAX_FILE_SIZE) {
nextFile();
}
FileUtils.INSTANCE.writeAppendToLast(offsetFile, randomAccessFile, offsetRecord);
FileUtils.INSTANCE.writeAppendToLast(offsetFile, offsetRecord);
lastOffsetRecord = offsetRecord;
}
}
......
......@@ -55,6 +55,9 @@ public enum SegmentBufferManager {
if (StringUtils.isNotEmpty(writeFileName)) {
File dataFile = new File(BufferFileConfig.BUFFER_PATH + writeFileName);
if (dataFile.exists()) {
if (outputStream != null) {
outputStream.close();
}
outputStream = new FileOutputStream(new File(BufferFileConfig.BUFFER_PATH + writeFileName), true);
} else {
newDataFile();
......
......@@ -19,7 +19,6 @@
package org.apache.skywalking.apm.collector.core.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.slf4j.Logger;
......@@ -68,21 +67,24 @@ public enum FileUtils {
return Const.EMPTY_STRING;
}
public void writeAppendToLast(File file, RandomAccessFile randomAccessFile, String value) {
if (randomAccessFile == null) {
try {
randomAccessFile = new RandomAccessFile(file, "rwd");
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
}
}
public void writeAppendToLast(File file, String value) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "rwd");
long length = randomAccessFile.length();
randomAccessFile.seek(length);
randomAccessFile.writeBytes(System.lineSeparator());
randomAccessFile.writeBytes(value);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册