Skip to content

Commit

Permalink
for alibaba#4594,alibaba#4594 Fix IO close problem. (alibaba#4606)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maijh97 authored Dec 31, 2020
1 parent dcd2fce commit 2af6784
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public String getStatusText() {
}

@Override
public void close() throws IOException {
public void close() {

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.client.config.utils;

import com.alibaba.nacos.common.utils.IoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -89,7 +90,7 @@ public static String getFileContent(File file, String charsetName) throws IOExce
rlock = null;
}
if (fis != null) {
fis.close();
IoUtils.closeQuietly(fis);
fis = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.client.naming.cache;

import com.alibaba.nacos.common.utils.IoUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -88,7 +90,7 @@ public static String getFileContent(File file, String charsetName) throws IOExce
rlock = null;
}
if (fis != null) {
fis.close();
IoUtils.closeQuietly(fis);
fis = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import com.alibaba.nacos.common.http.param.Header;
import com.alibaba.nacos.common.http.param.MediaType;
import com.alibaba.nacos.common.model.RequestHttpEntity;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.HashMap;
Expand Down Expand Up @@ -101,9 +103,10 @@ public HttpClientResponse execute(URI uri, String httpMethod, RequestHttpEntity
conn.setDoOutput(true);
byte[] b = bodyStr.getBytes();
conn.setRequestProperty("Content-Length", String.valueOf(b.length));
conn.getOutputStream().write(b, 0, b.length);
conn.getOutputStream().flush();
conn.getOutputStream().close();
OutputStream outputStream = conn.getOutputStream();
outputStream.write(b, 0, b.length);
outputStream.flush();
IoUtils.closeQuietly(outputStream);
}
}
conn.connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public interface HttpClientResponse extends Closeable {

/**
* close response InputStream.
*
* @throws IOException ex
*/
@Override
void close() throws IOException;
void close();
}
28 changes: 7 additions & 21 deletions common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ public static byte[] tryDecompress(InputStream raw) throws IOException {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
if (gis != null) {
gis.close();
}
closeQuietly(out);
closeQuietly(gis);
}

return null;
Expand All @@ -94,12 +90,8 @@ public static byte[] tryDecompress(byte[] raw) throws Exception {
IoUtils.copy(gis, out);
return out.toByteArray();
} finally {
if (out != null) {
out.close();
}
if (gis != null) {
gis.close();
}
closeQuietly(out);
closeQuietly(gis);
}
}

Expand All @@ -122,9 +114,7 @@ public static void writeStringToFile(File file, String data, String encoding) th
os.write(data.getBytes(encoding));
os.flush();
} finally {
if (null != os) {
os.close();
}
closeQuietly(os);
}
}

Expand Down Expand Up @@ -311,12 +301,8 @@ public static void copyFile(String source, String target) throws IOException {
sc = new FileInputStream(sf).getChannel();
sc.transferTo(0, sc.size(), tc);
} finally {
if (null != sc) {
sc.close();
}
if (null != tc) {
tc.close();
}
closeQuietly(sc);
closeQuietly(tc);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static Properties getResourceAsProperties(ClassLoader loader, String reso
Properties props = new Properties();
InputStream in = getResourceAsStream(loader, resource);
props.load(in);
in.close();
IoUtils.closeQuietly(in);
return props;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.controller;

import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.enums.FileTypeEnum;
import com.alibaba.nacos.config.server.model.CacheItem;
Expand Down Expand Up @@ -276,9 +277,7 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon

} finally {
releaseConfigReadLock(groupKey);
if (null != fis) {
fis.close();
}
IoUtils.closeQuietly(fis);
}
} else if (lockResult == 0) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
Expand Down Expand Up @@ -235,9 +236,7 @@ private List<String> loadSql(String sqlFile) throws Exception {
} catch (Exception ex) {
throw new Exception(ex.getMessage());
} finally {
if (sqlFileIn != null) {
sqlFileIn.close();
}
IoUtils.closeQuietly(sqlFileIn);
}
}

Expand All @@ -249,21 +248,15 @@ private List<String> loadSql(String sqlFile) throws Exception {
* @throws Exception Exception.
*/
private void execute(Connection conn, String sqlFile) throws Exception {
Statement stmt = null;
try {
try (Statement stmt = conn.createStatement()) {
List<String> sqlList = loadSql(sqlFile);
stmt = conn.createStatement();
for (String sql : sqlList) {
try {
stmt.execute(sql);
} catch (Exception e) {
LogUtil.DEFAULT_LOG.warn(e.getMessage());
}
}
} finally {
if (stmt != null) {
stmt.close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,16 @@ public synchronized void write(final Datum datum) throws Exception {
throw new IllegalStateException("can not make cache file: " + cacheFile.getName());
}

FileChannel fc = null;
ByteBuffer data;

data = ByteBuffer.wrap(JacksonUtils.toJson(datum).getBytes(StandardCharsets.UTF_8));

try {
fc = new FileOutputStream(cacheFile, false).getChannel();
try (FileChannel fc = new FileOutputStream(cacheFile, false).getChannel()) {
fc.write(data, data.position());
fc.force(true);
} catch (Exception e) {
MetricsMonitor.getDiskException().increment();
throw e;
} finally {
if (fc != null) {
fc.close();
}
}

// remove old format file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public String getStatusText() {
}

@Override
public void close() throws IOException {
public void close() {

}
};
Expand Down

0 comments on commit 2af6784

Please sign in to comment.