Skip to content

Commit

Permalink
Add:
Browse files Browse the repository at this point in the history
* VersionUtil to get version from jar
* sentinel-transport-netty-http will also retry when port specified has been used

Fix:
* LogBase repackage and support variable arguments
* SentinelConfig remove duplicated JVM properties overriding
  • Loading branch information
jasonjoo2010 authored and jason-joo committed Sep 29, 2018
1 parent 94dc796 commit fc48c91
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 70 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<maven.deploy.version>2.8.2</maven.deploy.version>
<maven.gpg.version>1.6</maven.gpg.version>
<maven.jacoco.version>0.8.1</maven.jacoco.version>
<maven.jar.version>3.1.0</maven.jar.version>
</properties>

<modules>
Expand Down Expand Up @@ -182,6 +183,13 @@
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven.jar.version}</version>
</plugin>
</pluginManagement>
</build>

<profiles>
Expand Down
16 changes: 15 additions & 1 deletion sentinel-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,19 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<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,6 +24,7 @@
import com.alibaba.csp.sentinel.util.PidUtil;

/**
*
* @author leyou
*/
public class LogBase {
Expand All @@ -36,6 +37,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,36 @@
/*
* 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 com.alibaba.csp.sentinel.log.RecordLog;

/**
* Get Sentinel version from MANIFEST.MF
*
* @author jason
* @since 0.2.1
*/
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
Expand Up @@ -18,21 +18,25 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.log.CommandCenterLog;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.util.StringUtil;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
* @author Eric Zhao
*/
@SuppressWarnings("rawtypes")
public final class HttpServer {

private static final int DEFAULT_PORT = 8719;
Expand Down Expand Up @@ -60,13 +64,40 @@ public void start() throws Exception {
} catch (Exception e) {
throw new IllegalArgumentException("Illegal port: " + TransportConfig.getPort());
}
channel = b.bind(port).sync().channel();

int retryCount = 0;
ChannelFuture channelFuture = null;
// loop for an successful binding
while (true) {
int newPort = getNewPort(port, retryCount);
try {
channelFuture = b.bind(newPort).sync();
TransportConfig.setRuntimePort(newPort);
break;
} catch (Exception e) {
TimeUnit.MILLISECONDS.sleep(30);
RecordLog.warn("netty server bind error, port={0}, retry={1}", newPort, retryCount);
retryCount ++;
}
}
channel = channelFuture.channel();
channel.closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}

/**
* increase port number every 3 tries
*
* @param basePort
* @param retryCount
* @return
*/
private int getNewPort(int basePort, int retryCount) {
return basePort + retryCount / 3;
}

public void close() {
channel.close();
Expand Down
Loading

0 comments on commit fc48c91

Please sign in to comment.