Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/alibaba/nacos into devel…
Browse files Browse the repository at this point in the history
…op-import-v2

* 'develop' of https://github.com/alibaba/nacos:
  优化 FileTypeEnum 取值逻辑, 避免使用异常当作逻辑分支 (alibaba#4607)
  for alibaba#4594,alibaba#4594 Fix IO close problem. (alibaba#4606)
  console-ui config排序优化 (alibaba#4599)
  • Loading branch information
wjm0729 committed Dec 31, 2020
2 parents 85c88ed + 4da3c82 commit faa9fc7
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ derby.log
work
test/logs
derby.log
yarn.lock
yarn.lock
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 @@ -271,9 +272,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 @@ -177,9 +177,25 @@ class ConfigurationManagement extends React.Component {
hasdash: false,
});
} else {
// 前端默认排序
let sortList = [];
for (let j = 0, len = res.data.length; j < len; j++) {
let item = res.data[j];
sortList.push({
k: item.appName || '' + item.group || '' + item.dataId || '',
v: item,
});
}
sortList.sort(function(a, b) {
return a.k.localeCompare(b.k);
});
let showList = [];
for (let j = 0, len = sortList.length; j < len; j++) {
showList.push(sortList[j].v);
}
this.setState({
hasdash: true,
contentList: res.data,
contentList: showList,
});
}
}
Expand Down Expand Up @@ -425,6 +441,15 @@ class ConfigurationManagement extends React.Component {
);
}

onChangeSort(dataIndex, order) {
this.dataSource.sort(function(a, b) {
if (order === 'asc') {
return (a[dataIndex] + '').localeCompare(b[dataIndex] + '');
}
return (b[dataIndex] + '').localeCompare(a[dataIndex] + '');
});
}

handlePageSizeChange(pageSize) {
this.setState({ pageSize }, () => this.changePage(1));
}
Expand Down Expand Up @@ -1363,10 +1388,13 @@ class ConfigurationManagement extends React.Component {
ref="dataTable"
loading={this.state.loading}
rowSelection={this.state.rowSelection}
onSort={this.onChangeSort}
>
<Table.Column title={'Data Id'} dataIndex={'dataId'} />
<Table.Column title={'Group'} dataIndex={'group'} />
{!this.inApp && <Table.Column title={locale.application} dataIndex="appName" />}
<Table.Column sortable={true} title={'Data Id'} dataIndex={'dataId'} />
<Table.Column sortable={true} title={'Group'} dataIndex={'group'} />
{!this.inApp && (
<Table.Column sortable={true} title={locale.application} dataIndex="appName" />
)}
<Table.Column title={locale.operation} cell={this.renderCol.bind(this)} />
</Table>
{configurations.totalCount > 0 && (
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 faa9fc7

Please sign in to comment.