Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge alibaba #3

Merged
merged 7 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 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,15 @@
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven.jar.version}</version>
</plugin>
</plugins>
</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 @@ -69,23 +69,14 @@ 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);
fis.close();

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("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue);
}
}
}

Expand Down Expand Up @@ -131,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 @@ -141,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 @@ -56,7 +56,6 @@ public static void doInit() {
} catch (Exception ex) {
RecordLog.info("[Sentinel InitExecutor] Init failed", ex);
ex.printStackTrace();
System.exit(-1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ public class CommandCenterLog extends LogBase {
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 @@ -27,7 +27,12 @@

class DateFileLogHandler extends Handler {

private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
private final ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
public SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};

private volatile FileHandler handler;

Expand Down Expand Up @@ -64,17 +69,27 @@ public void flush() {

@Override
public void publish(LogRecord record) {
synchronized (monitor) {
if (endDate < record.getMillis() || !logFileExits()) { rotateDate(); }
if (shouldRotate(record)) {
synchronized (monitor) {
if (shouldRotate(record)) {
rotateDate();
}
}
}

if (System.currentTimeMillis() - startDate > 25 * 60 * 60 * 1000) {
String msg = record.getMessage();
record.setMessage("missed file rolling at: " + new Date(endDate) + "\n" + msg);
}
handler.publish(record);
}

private boolean shouldRotate(LogRecord record) {
if (endDate <= record.getMillis() || !logFileExits()) {
return true;
}
return false;
}

@Override
public void setFormatter(Formatter newFormatter) {
super.setFormatter(newFormatter);
Expand All @@ -83,7 +98,13 @@ public void setFormatter(Formatter newFormatter) {

private boolean logFileExits() {
try {
File logFile = new File(pattern);
SimpleDateFormat format = dateFormatThreadLocal.get();
String fileName = pattern.replace("%d", format.format(new Date()));
// When file count is not 1, the first log file name will end with ".0"
if (count != 1) {
fileName += ".0";
}
File logFile = new File(fileName);
return logFile.exists();
} catch (Throwable e) {

Expand All @@ -93,7 +114,10 @@ private boolean logFileExits() {

private void rotateDate() {
this.startDate = System.currentTimeMillis();
if (handler != null) { handler.close(); }
if (handler != null) {
handler.close();
}
SimpleDateFormat format = dateFormatThreadLocal.get();
String newPattern = pattern.replace("%d", format.format(new Date()));
// Get current date.
Calendar next = Calendar.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ private static String addSeparator(String logDir) {
}
return logDir;
}

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 All @@ -76,7 +96,7 @@ protected static Handler makeLogger(String logName, Logger heliumRecordLog) {
String fileName = LogBase.getLogBaseDir() + logName + ".pid" + PidUtil.getPid();
Handler handler = null;
try {
handler = new DateFileLogHandler(fileName + ".%d", 1024 * 1024 * 200, 1, true);
handler = new DateFileLogHandler(fileName + ".%d", 1024 * 1024 * 200, 4, true);
handler.setFormatter(formatter);
handler.setEncoding(LOG_CHARSET);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,20 @@ public class RecordLog extends LogBase {
static {
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,39 @@
/*
* 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 version of Sentinel from {@code MANIFEST.MF} file.
*
* @author jason
* @since 0.2.1
*/
public final class VersionUtil {

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

private VersionUtil() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +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 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 @@ -56,7 +56,7 @@ public String getResource() {

@JsonIgnore
public int getBlockGrade() {
return rule.getBlockGrade();
return rule.getGrade();
}

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<td style="word-wrap:break-word;word-break:break-all;">{{ruleEntity.rule.resource}}</td>
<td style="word-wrap:break-word;word-break:break-all;">{{ruleEntity.rule.paramIdx}}</td>
<td>
{{ruleEntity.rule.blockGrade == 1 ? 'QPS' : '未知'}}
{{ruleEntity.rule.grade == 1 ? 'QPS' : '未知'}}
</td>
<td style="word-wrap:break-word;word-break:break-all;">
{{ruleEntity.rule.count}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static void initHotParamFlowRules() {
// QPS mode, threshold is 5 for every frequent "hot spot" parameter in index 0 (the first arg).
ParamFlowRule rule = new ParamFlowRule(RESOURCE_KEY)
.setParamIdx(0)
.setBlockGrade(RuleConstant.FLOW_GRADE_QPS)
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setCount(5);
// We can set threshold count for specific parameter value individually.
// Here we add an exception item. That means: QPS threshold of entries with parameter `PARAM_B` (type: int)
Expand Down
Loading