Skip to content

Commit

Permalink
Add:
Browse files Browse the repository at this point in the history
* VersionUtil to get version from jar
* NetUtil to get available port to bind conveniently

Fix:
* LogBase repackage and support variable arguments
* SentinelConfig remove duplicated JVM properties overriding
  • Loading branch information
jasonjoo2010 authored and jason-joo committed Sep 28, 2018
1 parent 94dc796 commit 0cd7c34
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 67 deletions.
17 changes: 16 additions & 1 deletion sentinel-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,20 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestEntries>
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.csp.sentinel.node.EntranceNode;
import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.util.VersionUtil;

/**
* @author qinan.qn
Expand All @@ -28,7 +29,7 @@
*/
public final class Constants {

public static final String SENTINEL_VERSION = "0.2.1";
public static final String SENTINEL_VERSION = VersionUtil.getVersion("0.2.1");

public final static int MAX_CONTEXT_NAME_SIZE = 2000;
public final static int MAX_SLOT_CHAIN_SIZE = 6000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,6 @@ private static void loadProps() {

for (Object key : fileProps.keySet()) {
SentinelConfig.setConfig((String)key, (String)fileProps.get(key));
try {
String systemValue = System.getProperty((String)key);
if (!StringUtil.isEmpty(systemValue)) {
SentinelConfig.setConfig((String)key, systemValue);
}
} catch (Exception e) {
RecordLog.info(e.getMessage(), e);
}
RecordLog.info(key + " value: " + SentinelConfig.getConfig((String)key));
}
}
} catch (Throwable ioe) {
Expand All @@ -94,7 +85,13 @@ private static void loadProps() {

// JVM parameter override file config.
for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
SentinelConfig.setConfig(entry.getKey().toString(), entry.getValue().toString());
String configKey = entry.getKey().toString();
String configValue = entry.getValue().toString();
String configValueOld = getConfig(configKey);
SentinelConfig.setConfig(configKey, configValue);
if (configValueOld != null) {
RecordLog.info("JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,19 @@ public static void resetLogBaseDir(String baseDir) {
logHandler = makeLogger(FILE_NAME, heliumRecordLog);
}

public static void info(String msg) {
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler);
heliumRecordLog.log(Level.INFO, msg);
public static void info(String detail, Object... params) {
log(heliumRecordLog, logHandler, Level.INFO, detail, params);
}

public static void info(String msg, Throwable e) {
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler);
heliumRecordLog.log(Level.INFO, msg, e);
public static void info(String detail, Throwable e) {
log(heliumRecordLog, logHandler, Level.INFO, detail, e);
}

public static void warn(String msg, Throwable e) {
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler);
heliumRecordLog.log(Level.WARNING, msg, e);
public static void warn(String detail, Object... params) {
log(heliumRecordLog, logHandler, Level.WARNING, detail, params);
}

public static void warn(String detail, Throwable e) {
log(heliumRecordLog, logHandler, Level.WARNING, detail, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import com.alibaba.csp.sentinel.util.PidUtil;

/**
* Add support for placeholders in java.util.logging
*
* @author leyou
* @author jason<hblzxsj@gmail.com>
*/
public class LogBase {
public static final String LOG_CHARSET = "utf-8";
Expand All @@ -36,6 +39,26 @@ public class LogBase {
String userHome = System.getProperty(USER_HOME);
setLogBaseDir(userHome);
}

protected static void log(Logger logger, Handler handler, Level level, String detail, Object... params) {
if (detail == null) {
return;
}
LoggerUtils.disableOtherHandlers(logger, handler);
if (params.length == 0) {
logger.log(level, detail);
} else {
logger.log(level, detail, params);
}
}

protected static void log(Logger logger, Handler handler, Level level, String detail, Throwable throwable) {
if (detail == null) {
return;
}
LoggerUtils.disableOtherHandlers(logger, handler);
logger.log(level, detail, throwable);
}

/**
* Get log file base directory path, the returned path is guaranteed end with {@link File#separator}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class RecordLog extends LogBase {
static {
logHandler = makeLogger(FILE_NAME, heliumRecordLog);
}

/**
* Change log dir, the dir will be created if not exits
*/
Expand All @@ -41,23 +41,19 @@ public static void resetLogBaseDir(String baseDir) {
logHandler = makeLogger(FILE_NAME, heliumRecordLog);
}

public static void info(String detail) {
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler);
heliumRecordLog.log(Level.INFO, detail);
public static void info(String detail, Object... params) {
log(heliumRecordLog, logHandler, Level.INFO, detail, params);
}

public static void info(String detail, Throwable e) {
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler);
heliumRecordLog.log(Level.INFO, detail, e);
log(heliumRecordLog, logHandler, Level.INFO, detail, e);
}

public static void warn(String detail) {
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler);
heliumRecordLog.log(Level.WARNING, detail);
public static void warn(String detail, Object... params) {
log(heliumRecordLog, logHandler, Level.WARNING, detail, params);
}

public static void warn(String detail, Throwable e) {
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler);
heliumRecordLog.log(Level.WARNING, detail, e);
log(heliumRecordLog, logHandler, Level.WARNING, detail, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.csp.sentinel.util;

import com.alibaba.csp.sentinel.log.RecordLog;

/**
* Get Sentinel version from MANIFEST.MF
*
* @author jason<jason@dayima.com> @ Sep 28, 2018
*
*/
public class VersionUtil {
public static String getVersion(String defaultVersion) {
try {
String version = VersionUtil.class.getPackage().getImplementationVersion();
return version == null || version.length() == 0 ? defaultVersion : version;
} catch (Throwable e) {
RecordLog.warn("return default version, ignore exception", e);
return defaultVersion;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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);
}
}
5 changes: 5 additions & 0 deletions sentinel-transport/sentinel-transport-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.alibaba.csp.sentinel.transport.util;

import java.io.IOException;
import java.net.ServerSocket;

/**
* net utils for transporting
*
* @author jason<jason@dayima.com> @ Sep 28, 2018
*
*/
public class NetUtil {

/**
* 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>
* <p>
* To avoid infinite loop 200 attemps will be made in most.<br>
* Default backlog is 100.
*
* @param port
* @return null for error
*/
public static ServerSocket getServerSocketFromBasePort(int port) {
return getServerSocketFromBasePort(port, 100, 200);
}

/**
* 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>
* <p>
* To avoid infinite loop 200 attemps will be made in most.
*
* @param port
* @param backlog
* @return null for error
*/
public static ServerSocket getServerSocketFromBasePort(int port, int backlog) {
return getServerSocketFromBasePort(port, backlog, 200);
}

/**
* 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>
*
* @param port
* @param backlog
* @param tries
* total count of increasement
* @return
*/
public static ServerSocket getServerSocketFromBasePort(int port, int backlog, int tries) {
while (tries-- > 0) {
try {
return new ServerSocket(port++, backlog);
} catch (IOException e) {
}
}
return null;
}

/**
* Get an available port to bind with maximum 200 tries
*
* @param port
* @return 0 for failure
*/
public static int getAvailablePortFromBase(int port) {
return getAvailablePortFromBase(port, 200);
}

/**
* Get an available port to bind
*
* @param port
* @param maxTries
* @return 0 for failure
*/
public static int getAvailablePortFromBase(int port, int maxTries) {
ServerSocket serverSocket = getServerSocketFromBasePort(port, 50, maxTries);
if (serverSocket != null) {
int finalPort = serverSocket.getLocalPort();
try {
serverSocket.close();
} catch (IOException e) {
}
return finalPort;
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.csp.sentinel.transport.util;

import java.io.IOException;
import java.net.ServerSocket;

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

public class NetUtilTest {
@Test
public void availablePortTest() {
// get a port first
int port = NetUtil.getAvailablePortFromBase(3000, 5000);
Assert.assertTrue(port > 0);
// try to create a socket based on it (should be just use it)
ServerSocket serverSocket = NetUtil.getServerSocketFromBasePort(port);
Assert.assertNotNull(serverSocket);
Assert.assertEquals(port, serverSocket.getLocalPort());
// get a port based on it again while the socket working,
// a new greater port will be returned
int portNew = NetUtil.getAvailablePortFromBase(port);
Assert.assertTrue(portNew > port);
// close the socket and fetch again, same port will be returned
try {
serverSocket.close();
} catch (IOException e) {
}
portNew = NetUtil.getAvailablePortFromBase(port);
Assert.assertEquals(portNew, port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.transport.util.NetUtil;
import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.log.CommandCenterLog;
import com.alibaba.csp.sentinel.util.StringUtil;
Expand All @@ -33,6 +34,7 @@
/**
* @author Eric Zhao
*/
@SuppressWarnings("rawtypes")
public final class HttpServer {

private static final int DEFAULT_PORT = 8719;
Expand All @@ -57,9 +59,15 @@ public void start() throws Exception {
} else {
port = Integer.parseInt(TransportConfig.getPort());
}
// find a really available one
port = NetUtil.getAvailablePortFromBase(port);
} catch (Exception e) {
throw new IllegalArgumentException("Illegal port: " + TransportConfig.getPort());
}
if (port == 0) {
throw new RuntimeException("Could not determine the transport server port");
}
TransportConfig.setRuntimePort(port);
channel = b.bind(port).sync().channel();
channel.closeFuture().sync();
} finally {
Expand Down
Loading

0 comments on commit 0cd7c34

Please sign in to comment.