This is an automated email from the ASF dual-hosted git repository.
benjobs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampark.git
The following commit(s) were added to refs/heads/dev by this push:
new 299257800 [polish] fix resource leak; avoid NullPointException ; fix
Long equals to int statement ; (#2154)
299257800 is described below
commit 299257800eac01f198ff829b9200da62490bbf0e
Author: gongzhongqiang <[email protected]>
AuthorDate: Tue Dec 13 15:22:04 2022 +0800
[polish] fix resource leak; avoid NullPointException ; fix Long equals to
int statement ; (#2154)
* [polish] fix resource leak; avoid nullPointException ; fix Long equals to
int statement ;
Co-authored-by: gongzhongqiang <[email protected]>
---
.../common/conf/InternalConfigHolder.scala | 1 -
.../interceptor/FileHeaderCheckInterceptor.java | 12 +++++-----
.../streampark/console/base/util/FileUtils.java | 28 ++++++++++++----------
.../apache/streampark/console/core/bean/Note.java | 23 ++++++++++--------
.../core/controller/ApplicationController.java | 16 ++++++-------
.../console/core/enums/ResourceFrom.java | 2 +-
.../console/core/service/VariableServiceTest.java | 4 ++--
.../profiling/profiler/CpuAndMemoryProfiler.java | 10 ++++----
8 files changed, 50 insertions(+), 46 deletions(-)
diff --git
a/streampark-common/src/main/scala/org/apache/streampark/common/conf/InternalConfigHolder.scala
b/streampark-common/src/main/scala/org/apache/streampark/common/conf/InternalConfigHolder.scala
index a91b381b4..6b69a39bb 100644
---
a/streampark-common/src/main/scala/org/apache/streampark/common/conf/InternalConfigHolder.scala
+++
b/streampark-common/src/main/scala/org/apache/streampark/common/conf/InternalConfigHolder.scala
@@ -187,7 +187,6 @@ object Converter {
def convert[T](v: String, classType: Class[_]): T = {
classType match {
- case c if c == classOf[String] => v.asInstanceOf[T]
case c if c == classOf[Int] => v.toInt.asInstanceOf[T]
case c if c == classOf[Long] => v.toLong.asInstanceOf[T]
case c if c == classOf[Boolean] => v.toBoolean.asInstanceOf[T]
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/interceptor/FileHeaderCheckInterceptor.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/interceptor/FileHeaderCheckInterceptor.java
index 7c847352a..882ff56bb 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/interceptor/FileHeaderCheckInterceptor.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/interceptor/FileHeaderCheckInterceptor.java
@@ -37,11 +37,11 @@ import java.util.Map;
@Component
public class FileHeaderCheckInterceptor implements HandlerInterceptor {
- private static List<String> fileHeaders = new ArrayList<>();
- private int headerLength = 8;
+ private static final List<String> FILE_HEADERS = new ArrayList<>();
+ private static final int HEADER_LENGTH = 8;
static {
- fileHeaders.add(FileType.JAR.getMagicNumber());
+ FILE_HEADERS.add(FileType.JAR.getMagicNumber());
}
@Override
@@ -54,9 +54,9 @@ public class FileHeaderCheckInterceptor implements
HandlerInterceptor {
String formKey = iterator.next();
MultipartFile multipartFile =
multipartRequest.getFile(formKey);
byte[] file = multipartFile.getBytes();
- if (file.length > headerLength) {
+ if (file.length > HEADER_LENGTH) {
StringBuilder sb = new StringBuilder();
- for (int i = 0; i < headerLength; i++) {
+ for (int i = 0; i < HEADER_LENGTH; i++) {
int v = file[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
@@ -66,7 +66,7 @@ public class FileHeaderCheckInterceptor implements
HandlerInterceptor {
}
boolean isFound = false;
String fileHead = sb.toString().toUpperCase();
- for (String header : fileHeaders) {
+ for (String header : FILE_HEADERS) {
if (fileHead.startsWith(header)) {
isFound = true;
break;
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/FileUtils.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/FileUtils.java
index 1d4053da3..d7402c5f0 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/FileUtils.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/base/util/FileUtils.java
@@ -36,14 +36,16 @@ public class FileUtils {
*/
public static byte[] readEndOfFile(File file, long maxSize) throws
IOException {
long readSize = maxSize;
- RandomAccessFile raFile = new RandomAccessFile(file, "r");
- if (raFile.length() > maxSize) {
- raFile.seek(raFile.length() - maxSize);
- } else if (raFile.length() < maxSize) {
- readSize = (int) raFile.length();
+ byte[] fileContent;
+ try (RandomAccessFile raFile = new RandomAccessFile(file, "r")) {
+ if (raFile.length() > maxSize) {
+ raFile.seek(raFile.length() - maxSize);
+ } else if (raFile.length() < maxSize) {
+ readSize = (int) raFile.length();
+ }
+ fileContent = new byte[(int) readSize];
+ raFile.read(fileContent);
}
- byte[] fileContent = new byte[(int) readSize];
- raFile.read(fileContent);
return fileContent;
}
@@ -60,11 +62,13 @@ public class FileUtils {
throw new IllegalArgumentException(
String.format("The startOffset %s is great than the file
length %s", startOffset, file.length()));
}
- RandomAccessFile raFile = new RandomAccessFile(file, "r");
- long readSize = Math.min(maxSize, file.length() - startOffset);
- raFile.seek(startOffset);
- byte[] fileContent = new byte[(int) readSize];
- raFile.read(fileContent);
+ byte[] fileContent;
+ try (RandomAccessFile raFile = new RandomAccessFile(file, "r")) {
+ long readSize = Math.min(maxSize, file.length() - startOffset);
+ raFile.seek(startOffset);
+ fileContent = new byte[(int) readSize];
+ raFile.read(fileContent);
+ }
return fileContent;
}
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/Note.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/Note.java
index f98a25238..83eb21d8f 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/Note.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/bean/Note.java
@@ -38,16 +38,19 @@ public class Note {
public Content getContent() {
if (this.content == null) {
- Scanner scanner = new Scanner(this.text);
- Properties properties = new Properties();
- StringBuilder codeBuilder = new StringBuilder();
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- if (line.startsWith("%flink.")) {
- String[] dyProp = line.trim().split("=");
- properties.setProperty(dyProp[0].substring(1), dyProp[1]);
- } else {
- codeBuilder.append(line).append("\n");
+ Properties properties;
+ StringBuilder codeBuilder;
+ try (Scanner scanner = new Scanner(this.text)) {
+ properties = new Properties();
+ codeBuilder = new StringBuilder();
+ while (scanner.hasNextLine()) {
+ String line = scanner.nextLine();
+ if (line.startsWith("%flink.")) {
+ String[] dyProp = line.trim().split("=");
+ properties.setProperty(dyProp[0].substring(1),
dyProp[1]);
+ } else {
+ codeBuilder.append(line).append("\n");
+ }
}
}
this.content = new Content(properties, codeBuilder.toString());
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationController.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationController.java
index a97a514ca..1fece41ce 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationController.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationController.java
@@ -113,7 +113,7 @@ public class ApplicationController {
Long id = applicationService.copy(app);
Map<String, String> data = new HashMap<>();
data.put("id", Long.toString(id));
- return id.equals(0) ? RestResponse.success(false).data(data) :
RestResponse.success(true).data(data);
+ return id.equals(0L) ? RestResponse.success(false).data(data) :
RestResponse.success(true).data(data);
}
@PostMapping("update")
@@ -311,18 +311,16 @@ public class ApplicationController {
final String scheme = uri.getScheme();
final String pathPart = uri.getPath();
RestResponse restResponse = RestResponse.success(true);
- String error;
+ String error = null;
if (scheme == null) {
error = "The scheme (hdfs://, file://, etc) is null. Please
specify the file system scheme explicitly in the URI.";
- restResponse.data(false).message(error);
- }
- if (pathPart == null) {
+ } else if (pathPart == null) {
error = "The path to store the checkpoint data in is null. Please
specify a directory path for the checkpoint data.";
- restResponse.data(false).message(error);
- }
- if (pathPart.length() == 0 || pathPart.equals("/")) {
+ } else if (pathPart.length() == 0 || pathPart.equals("/")) {
error = "Cannot use the root directory for checkpoints.";
- restResponse.data(false).message(error);
+ }
+ if (error != null) {
+ restResponse = RestResponse.success(false).message(error);
}
return restResponse;
}
diff --git
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceFrom.java
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceFrom.java
index c01076070..a05226a8d 100644
---
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceFrom.java
+++
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/ResourceFrom.java
@@ -39,7 +39,7 @@ public enum ResourceFrom implements Serializable {
}
public static ResourceFrom of(Integer value) {
- return Arrays.stream(values()).filter((x) -> x.value ==
value).findFirst().orElse(null);
+ return Arrays.stream(values()).filter((x) ->
x.value.equals(value)).findFirst().orElse(null);
}
public Integer getValue() {
diff --git
a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/VariableServiceTest.java
b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/VariableServiceTest.java
index c4ffac7cf..3a0af9a41 100644
---
a/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/VariableServiceTest.java
+++
b/streampark-console/streampark-console-service/src/test/java/org/apache/streampark/console/core/service/VariableServiceTest.java
@@ -54,7 +54,7 @@ class VariableServiceTest {
variable.setTeamId(teamId);
variableService.save(variable);
Variable findVariable = variableService.findByVariableCode(teamId,
variableCode);
- Assertions.assertTrue(findVariable != null);
+ Assertions.assertNotNull(findVariable);
String paramWithPlaceholders = "--kafka.brokers ${" + variableCode +
"}";
String realParam = variableService.replaceVariable(teamId,
paramWithPlaceholders);
Assertions.assertEquals(realParam, "--kafka.brokers " +
variableVariable);
@@ -78,7 +78,7 @@ class VariableServiceTest {
variable.setTeamId(teamId);
variableService.save(variable);
Variable findVariable = variableService.findByVariableCode(teamId,
variableCode);
- Assertions.assertTrue(findVariable != null);
+ Assertions.assertNotNull(findVariable);
String paramWithPlaceholders = "--kafka.brokers ${" + variableCode +
"}";
String realParam = variableService.replaceVariable(teamId,
paramWithPlaceholders);
Assertions.assertNotEquals("--kafka.brokers " + variableVariable,
realParam);
diff --git
a/streampark-plugin/streampark-jvm-profiler/src/main/java/org/apache/streampark/plugin/profiling/profiler/CpuAndMemoryProfiler.java
b/streampark-plugin/streampark-jvm-profiler/src/main/java/org/apache/streampark/plugin/profiling/profiler/CpuAndMemoryProfiler.java
index 1bb8a59eb..332811019 100644
---
a/streampark-plugin/streampark-jvm-profiler/src/main/java/org/apache/streampark/plugin/profiling/profiler/CpuAndMemoryProfiler.java
+++
b/streampark-plugin/streampark-jvm-profiler/src/main/java/org/apache/streampark/plugin/profiling/profiler/CpuAndMemoryProfiler.java
@@ -134,8 +134,8 @@ public class CpuAndMemoryProfiler extends ProfilerBase
implements Profiler {
for (GarbageCollectorMXBean gcMXBean : gcMXBeans) {
Map<String, Object> gcMap = new HashMap<>();
gcMap.put("name", gcMXBean.getName());
- gcMap.put("collectionCount", new
Long(gcMXBean.getCollectionCount()));
- gcMap.put("collectionTime", new
Long(gcMXBean.getCollectionTime()));
+ gcMap.put("collectionCount",
Long.valueOf(gcMXBean.getCollectionCount()));
+ gcMap.put("collectionTime",
Long.valueOf(gcMXBean.getCollectionTime()));
gcMetrics.add(gcMap);
}
@@ -167,9 +167,9 @@ public class CpuAndMemoryProfiler extends ProfilerBase
implements Profiler {
Map<String, Object> bufferPoolMap = new HashMap<>();
bufferPoolMap.put("name", pool.getName());
- bufferPoolMap.put("count", new Long(pool.getCount()));
- bufferPoolMap.put("memoryUsed", new
Long(pool.getMemoryUsed()));
- bufferPoolMap.put("totalCapacity", new
Long(pool.getTotalCapacity()));
+ bufferPoolMap.put("count", Long.valueOf(pool.getCount()));
+ bufferPoolMap.put("memoryUsed",
Long.valueOf(pool.getMemoryUsed()));
+ bufferPoolMap.put("totalCapacity",
Long.valueOf(pool.getTotalCapacity()));
bufferPoolsMetrics.add(bufferPoolMap);
}