Skip to content

Commit

Permalink
Code enhancement for #161
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed Oct 16, 2018
1 parent ba10d10 commit 7626bf4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static void loadProps() {
String fileName = LogBase.getLogBaseDir() + appName + ".properties";
File file = new File(fileName);
if (file.exists()) {
RecordLog.info("read SentinelConfig from " + fileName);
RecordLog.info("[SentinelConfig] Reading config from " + fileName);
FileInputStream fis = new FileInputStream(fileName);
Properties fileProps = new Properties();
fileProps.load(fis);
Expand All @@ -90,7 +90,7 @@ private static void loadProps() {
String configValueOld = getConfig(configKey);
SentinelConfig.setConfig(configKey, configValue);
if (configValueOld != null) {
RecordLog.info("JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue);
RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue);
}
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ public static long singleMetricFileSize() {
try {
return Long.parseLong(props.get(SINGLE_METRIC_FILE_SIZE));
} catch (Throwable throwable) {
RecordLog.info("SentinelConfig get singleMetricFileSize fail, use default value: "
RecordLog.info("[SentinelConfig] Parse singleMetricFileSize fail, use default value: "
+ DEFAULT_SINGLE_METRIC_FILE_SIZE, throwable);
return DEFAULT_SINGLE_METRIC_FILE_SIZE;
}
Expand All @@ -138,7 +138,7 @@ public static int totalMetricFileCount() {
try {
return Integer.parseInt(props.get(TOTAL_METRIC_FILE_COUNT));
} catch (Throwable throwable) {
RecordLog.info("SentinelConfig get totalMetricFileCount fail, use default value: "
RecordLog.info("[SentinelConfig] Parse totalMetricFileCount fail, use default value: "
+ DEFAULT_TOTAL_METRIC_FILE_COUNT, throwable);
return DEFAULT_TOTAL_METRIC_FILE_COUNT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class CommandCenterLog extends LogBase {
logHandler = makeLogger(FILE_NAME, heliumRecordLog);
}


public static void info(String detail, Object... params) {
log(heliumRecordLog, logHandler, Level.INFO, detail, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@
import com.alibaba.csp.sentinel.log.RecordLog;

/**
* Get Sentinel version from MANIFEST.MF
* Get version of Sentinel from {@code MANIFEST.MF} file.
*
* @author jason
* @since 0.2.1
*/
public class VersionUtil {
public final class VersionUtil {

public static String getVersion(String defaultVersion) {
try {
String version = VersionUtil.class.getPackage().getImplementationVersion();
return version == null || version.length() == 0 ? defaultVersion : version;
return StringUtil.isBlank(version) ? defaultVersion : version;
} catch (Throwable e) {
RecordLog.warn("return default version, ignore exception", e);
RecordLog.warn("Using default version, ignore exception", e);
return defaultVersion;
}
}

private VersionUtil() {}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.util;

import org.junit.Assert;
import org.junit.Test;

public class VersionUtilTest {

@Test
public void versionTest() {
String version = VersionUtil.getVersion("1.0");
/**
* manifest cannot be load before package
*/
Assert.assertEquals("1.0", version);
public void testGetDefaultVersion() {
String defaultVersion = "1.0";
String version = VersionUtil.getVersion(defaultVersion);
// Manifest cannot be load before package.
Assert.assertEquals(defaultVersion, version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void start() throws Exception {
port = Integer.parseInt(TransportConfig.getPort());
}
} catch (Exception e) {
// Will cause the application exit.
throw new IllegalArgumentException("Illegal port: " + TransportConfig.getPort());
}

Expand All @@ -73,10 +74,11 @@ public void start() throws Exception {
try {
channelFuture = b.bind(newPort).sync();
TransportConfig.setRuntimePort(newPort);
CommandCenterLog.info("[NettyHttpCommandCenter] Begin listening at port " + newPort);
break;
} catch (Exception e) {
TimeUnit.MILLISECONDS.sleep(30);
RecordLog.warn("netty server bind error, port={0}, retry={1}", newPort, retryCount);
RecordLog.warn("[HttpServer] Netty server bind error, port={0}, retry={1}", newPort, retryCount);
retryCount ++;
}
}
Expand All @@ -89,11 +91,11 @@ public void start() throws Exception {
}

/**
* increase port number every 3 tries
* Increase port number every 3 tries.
*
* @param basePort
* @param retryCount
* @return
* @param basePort base port to start
* @param retryCount retry count
* @return next calculated port
*/
private int getNewPort(int basePort, int retryCount) {
return basePort + retryCount / 3;
Expand All @@ -109,7 +111,7 @@ public void registerCommand(String commandName, CommandHandler handler) {
}

if (handlerMap.containsKey(commandName)) {
CommandCenterLog.info("Register failed (duplicate command): " + commandName);
CommandCenterLog.warn("[NettyHttpCommandCenter] Register failed (duplicate command): " + commandName);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,11 @@ public void run() {
}

/**
* Get a server socket from an avaliable port from a base port.<br>
* Increasement on port number will happen when the port has already been
* used.<br>
* Get a server socket from an available port from a base port.<br>
* Increasing on port number will occur when the port has already been used.
*
* @param basePort
* @return
* @param basePort base port to start
* @return new socket with available port
*/
private static ServerSocket getServerSocketFromBasePort(int basePort) {
int tryCount = 0;
Expand Down Expand Up @@ -229,7 +228,7 @@ public static void registerCommand(String commandName, CommandHandler handler) {
}

if (handlerMap.containsKey(commandName)) {
CommandCenterLog.info("Register failed (duplicate command): " + commandName);
CommandCenterLog.warn("Register failed (duplicate command): " + commandName);
return;
}

Expand Down

0 comments on commit 7626bf4

Please sign in to comment.