Skip to content

Commit

Permalink
merge branch 'master' of github.com:klboke/apollo
Browse files Browse the repository at this point in the history
  • Loading branch information
klboke committed Aug 7, 2023
2 parents 17714de + 1dd9d18 commit 1e228d3
Show file tree
Hide file tree
Showing 34 changed files with 823 additions and 153 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ Apollo 2.2.0
* [Fix the issue of the system permission management page retrieving non-existent users](https://github.com/apolloconfig/apollo/pull/4802)
* [Add release history cleaning function](https://github.com/apolloconfig/apollo/pull/4813)
* [[Multi-Database Support][pg] Make JdbcUserDetailsManager compat with postgre](https://github.com/apolloconfig/apollo/pull/4790)
* [refactor(apollo logging): Simplify the default log path to `/opt/logs`](https://github.com/apolloconfig/apollo/pull/4833)
* [Add a configuration config-service.cache.key.ignore-case to control whether the cache key is case-sensitive](https://github.com/apolloconfig/apollo/pull/4820)
* [feat: check port use by another process or not when startup](https://github.com/apolloconfig/apollo/pull/4656)
* [Bump springboot version from 2.7.9 to 2.7.11](https://github.com/apolloconfig/apollo/pull/4828)
* [[Multi-Database Support][h2] Support run on h2](https://github.com/apolloconfig/apollo/pull/4851)
* [Fix the issue that env special case handling is missing in some case](https://github.com/apolloconfig/apollo/pull/4887)
* [Fix the issue that namespace content being cleared when identical content is pasted into the namespace](https://github.com/apolloconfig/apollo/pull/4922)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/13?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
#Used for apollo-assembly
spring.application.name= apollo-adminservice
server.port= 8090
logging.file.name= /opt/logs/100003172/apollo-adminservice.log
logging.file.name= /opt/logs/apollo-adminservice.log
spring.jmx.default-domain = apollo-adminservice
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MODE=service
PID_FOLDER=.
# console appender log file folder
LOG_FOLDER=/opt/logs/100003172/
LOG_FOLDER=/opt/logs/
# console appender file name
LOG_FILENAME=console.log
# write application logs only to file appender
Expand Down
2 changes: 1 addition & 1 deletion apollo-adminservice/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ server:

logging:
file:
name: /opt/logs/100003172/apollo-adminservice.log
name: /opt/logs/apollo-adminservice.log

eureka:
instance:
Expand Down
78 changes: 54 additions & 24 deletions apollo-adminservice/src/main/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
SERVICE_NAME=apollo-adminservice
## Adjust log dir if necessary
LOG_DIR=/opt/logs/100003172
LOG_DIR=/opt/logs
## Adjust server port if necessary
SERVER_PORT=${SERVER_PORT:=8090}

Expand Down Expand Up @@ -45,7 +45,11 @@ export APP_NAME=$SERVICE_NAME
PATH_TO_JAR=$SERVICE_NAME".jar"
SERVER_URL="http://localhost:$SERVER_PORT"

function checkPidAlive {
function getPid() {
pgrep -f $SERVICE_NAME
}

function checkPidAlive() {
for i in `ls -t $APP_NAME/$APP_NAME.pid 2>/dev/null`
do
read pid < $i
Expand All @@ -63,6 +67,22 @@ function checkPidAlive {
exit 1;
}

function existProcessUsePort() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]; then
true
else
false
fi
}

function isServiceRunning() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 $SERVER_URL/health | grep "UP")" != "" ]; then
true
else
false
fi
}

if [ "$(uname)" == "Darwin" ]; then
windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
Expand Down Expand Up @@ -97,14 +117,14 @@ if [[ "$javaexe" ]]; then
version=$(echo "$version" | awk -F. '{printf("%03d%03d",$1,$2);}')
# now version is of format 009003 (9.3.x)
if [ $version -ge 011000 ]; then
JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/$SERVICE_NAME.gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
elif [ $version -ge 010000 ]; then
JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/$SERVICE_NAME.gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
elif [ $version -ge 009000 ]; then
JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/$SERVICE_NAME.gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
else
JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC"
JAVA_OPTS="$JAVA_OPTS -Xloggc:$LOG_DIR/gc.log -XX:+PrintGCDetails"
JAVA_OPTS="$JAVA_OPTS -Xloggc:$LOG_DIR/$SERVICE_NAME.gc.log -XX:+PrintGCDetails"
JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUnloadingEnabled -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=9 -XX:+CMSClassUnloadingEnabled -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=5M"
fi
fi
Expand Down Expand Up @@ -136,12 +156,27 @@ fi
if [[ -n "$APOLLO_RUN_MODE" ]] && [[ "$APOLLO_RUN_MODE" == "Docker" ]]; then
exec $javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $PATH_TO_JAR
else
if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
# before running check there is another process use port or not
if existProcessUsePort; then
if isServiceRunning; then
echo "$(date) ==== $SERVICE_NAME is running already with port $SERVER_PORT, pid $(getPid)"
exit 0
else
echo "$(date) ==== $SERVICE_NAME failed to start. The port $SERVER_PORT already be in use by another process"
echo "maybe you can figure out which process use port $SERVER_PORT by following ways:"
echo "1. access http://change-to-this-machine-ip:$SERVER_PORT by browser"
echo "2. run command 'curl $SERVER_URL'"
echo "3. run command 'sudo netstat -tunlp | grep :$SERVER_PORT'"
echo "4. run command 'sudo lsof -nP -iTCP:$SERVER_PORT -sTCP:LISTEN'"
exit 1
fi
fi

printf "$(date) ==== Starting ==== \n"
printf "$(date) ==== $SERVICE_NAME Starting ==== \n"

if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
fi
ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar"
./$SERVICE_NAME".jar" start
Expand All @@ -159,24 +194,19 @@ else
declare -i total_time=0

printf "Waiting for server startup"
until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]];
until [[ (( counter -ge max_counter )) ]];
do
printf "."
counter+=1
sleep 5
counter+=1
total_time=$((counter*5))

checkPidAlive
if isServiceRunning; then
printf "\n$(date) Server started in $total_time seconds!\n"
exit 0;
fi
done

total_time=counter*5

if [[ (( counter -ge max_counter )) ]];
then
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi

printf "\n$(date) Server started in $total_time seconds!\n"

exit 0;
fi
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public boolean isConfigServiceCacheEnabled() {
return getBooleanProperty("config-service.cache.enabled", false);
}

public boolean isConfigServiceCacheKeyIgnoreCase() {
return getBooleanProperty("config-service.cache.key.ignore-case", false);
}

int checkInt(int value, int min, int max, int defaultValue) {
if (value >= min && value <= max) {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Objects;

Expand All @@ -43,17 +51,27 @@ public class BizDBPropertySource extends RefreshablePropertySource {

private final ServerConfigRepository serverConfigRepository;

public BizDBPropertySource(final String name,
final Map<String, Object> source,
final ServerConfigRepository serverConfigRepository) {
super(name, source);
this.serverConfigRepository = serverConfigRepository;
}
private final DataSource dataSource;

private final Environment env;

@Autowired
public BizDBPropertySource(final ServerConfigRepository serverConfigRepository) {
public BizDBPropertySource(final ServerConfigRepository serverConfigRepository, DataSource dataSource,
final Environment env) {
super("DBConfig", Maps.newConcurrentMap());
this.serverConfigRepository = serverConfigRepository;
this.dataSource = dataSource;
this.env = env;
}

@PostConstruct
public void runSqlScript() throws Exception {
if (env.acceptsProfiles(Profiles.of("h2"))) {
Resource resource = new ClassPathResource("jpa/init.h2.sql");
if (resource.exists()) {
DatabasePopulatorUtils.execute(new ResourceDatabasePopulator(resource), dataSource);
}
}
}

String getCurrentDataCenter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.test.util.ReflectionTestUtils;

import javax.sql.DataSource;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

/**
Expand All @@ -40,11 +45,14 @@ public class BizConfigTest {
@Mock
private ServerConfigRepository serverConfigRepository;

@Mock
private DataSource dataSource;

private BizConfig bizConfig;

@Before
public void setUp() throws Exception {
bizConfig = new BizConfig(new BizDBPropertySource(serverConfigRepository));
bizConfig = new BizConfig(new BizDBPropertySource(serverConfigRepository, dataSource, environment));
ReflectionTestUtils.setField(bizConfig, "environment", environment);
}

Expand Down Expand Up @@ -124,4 +132,11 @@ public void testCheckInt() throws Exception {
assertEquals(someValidValue, bizConfig.checkInt(someValidValue, Integer.MIN_VALUE, Integer.MAX_VALUE,
someDefaultValue));
}

@Test
public void testIsConfigServiceCacheKeyIgnoreCase() {
assertFalse(bizConfig.isConfigServiceCacheKeyIgnoreCase());
when(environment.getProperty("config-service.cache.key.ignore-case")).thenReturn("true");
assertTrue(bizConfig.isConfigServiceCacheKeyIgnoreCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand All @@ -43,6 +45,13 @@ public class BizDBPropertySourceTest extends AbstractUnitTest {

@Mock
private ServerConfigRepository serverConfigRepository;

@Mock
private DataSource dataSource;

@Mock
private Environment environment;

private BizDBPropertySource propertySource;

private String clusterConfigKey = "clusterKey";
Expand All @@ -54,7 +63,7 @@ public class BizDBPropertySourceTest extends AbstractUnitTest {

@Before
public void initTestData() {
propertySource = spy(new BizDBPropertySource(serverConfigRepository));
propertySource = spy(new BizDBPropertySource(serverConfigRepository, dataSource, environment));

List<ServerConfig> configs = Lists.newLinkedList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public GrayReleaseRulesHolder grayReleaseRulesHolder() {
public ConfigService configService() {
if (bizConfig.isConfigServiceCacheEnabled()) {
return new ConfigServiceWithCache(releaseService, releaseMessageService,
grayReleaseRulesHolder());
grayReleaseRulesHolder(), bizConfig);
}
return new DefaultConfigService(releaseService, grayReleaseRulesHolder());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.ctrip.framework.apollo.configservice.service.config;

import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolder;
import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.google.common.base.Strings;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class ConfigServiceWithCache extends AbstractConfigService {

private final ReleaseService releaseService;
private final ReleaseMessageService releaseMessageService;
private final BizConfig bizConfig;

private LoadingCache<String, ConfigCacheEntry> configCache;

Expand All @@ -70,10 +72,12 @@ public class ConfigServiceWithCache extends AbstractConfigService {

public ConfigServiceWithCache(final ReleaseService releaseService,
final ReleaseMessageService releaseMessageService,
final GrayReleaseRulesHolder grayReleaseRulesHolder) {
final GrayReleaseRulesHolder grayReleaseRulesHolder,
final BizConfig bizConfig) {
super(grayReleaseRulesHolder);
this.releaseService = releaseService;
this.releaseMessageService = releaseMessageService;
this.bizConfig = bizConfig;
nullConfigCacheEntry = new ConfigCacheEntry(ConfigConsts.NOTIFICATION_ID_PLACEHOLDER, null);
}

Expand Down Expand Up @@ -147,18 +151,23 @@ protected Release findActiveOne(long id, ApolloNotificationMessages clientMessag
@Override
protected Release findLatestActiveRelease(String appId, String clusterName, String namespaceName,
ApolloNotificationMessages clientMessages) {
String key = ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName);
String messageKey = ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName);
String cacheKey = messageKey;

Tracer.logEvent(TRACER_EVENT_CACHE_GET, key);
if (bizConfig.isConfigServiceCacheKeyIgnoreCase()) {
cacheKey = cacheKey.toLowerCase();
}

Tracer.logEvent(TRACER_EVENT_CACHE_GET, cacheKey);

ConfigCacheEntry cacheEntry = configCache.getUnchecked(key);
ConfigCacheEntry cacheEntry = configCache.getUnchecked(cacheKey);

//cache is out-dated
if (clientMessages != null && clientMessages.has(key) &&
clientMessages.get(key) > cacheEntry.getNotificationId()) {
if (clientMessages != null && clientMessages.has(messageKey) &&
clientMessages.get(messageKey) > cacheEntry.getNotificationId()) {
//invalidate the cache and try to load from db again
invalidate(key);
cacheEntry = configCache.getUnchecked(key);
invalidate(cacheKey);
cacheEntry = configCache.getUnchecked(cacheKey);
}

return cacheEntry.getRelease();
Expand All @@ -177,10 +186,14 @@ public void handleMessage(ReleaseMessage message, String channel) {
}

try {
invalidate(message.getMessage());
String messageKey = message.getMessage();
if (bizConfig.isConfigServiceCacheKeyIgnoreCase()) {
messageKey = messageKey.toLowerCase();
}
invalidate(messageKey);

//warm up the cache
configCache.getUnchecked(message.getMessage());
configCache.getUnchecked(messageKey);
} catch (Throwable ex) {
//ignore
}
Expand Down
Loading

0 comments on commit 1e228d3

Please sign in to comment.