Skip to content

Commit

Permalink
[ISSUE #9415]update setExternalStorage logic,support more db check,ad…
Browse files Browse the repository at this point in the history
…d 8 count unit test (#9470)

* [impr]update setExternalStorage method logic,support more db check,add 8 count unit test method

* update comment desc
  • Loading branch information
wuchubuzai2018 authored Nov 7, 2022
1 parent 898cbb1 commit 2168395
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class PropertiesConstant {

public static final String MYSQL = "mysql";

public static final String DERBY = "derby";

public static final String EMPTY_DATASOURCE_PLATFORM = "";

public static final String EMBEDDED_STORAGE = "embeddedStorage";

}
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,11 @@ private void loadSetting() {
setCorrectUsageDelay(getInt(PropertiesConstant.CORRECT_USAGE_DELAY, correctUsageDelay));
setInitialExpansionPercent(getInt(PropertiesConstant.INITIAL_EXPANSION_PERCENT, initialExpansionPercent));
// External data sources are used by default in cluster mode
setUseExternalDB(PropertiesConstant.MYSQL
.equalsIgnoreCase(getString(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "")));
String platform = getString(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "");
boolean useExternalStorage = !PropertiesConstant.EMPTY_DATASOURCE_PLATFORM.equalsIgnoreCase(platform)
&& !PropertiesConstant.DERBY.equalsIgnoreCase(platform);

setUseExternalDB(useExternalStorage);

// must initialize after setUseExternalDB
// This value is true in stand-alone mode and false in cluster mode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright 1999-2022 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.nacos.config.server.utils;

import com.alibaba.nacos.config.server.constant.PropertiesConstant;
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceServiceImpl;
import com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl;
import com.alibaba.nacos.sys.env.Constants;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils;

/**
* ClusterExternalStorage unit test.
*
* @author Long Yu
* @since 2.2.0
*/
@RunWith(MockitoJUnitRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ClusterExternalStorageTest {

@InjectMocks
private DynamicDataSource dataSource;

private MockEnvironment environment;

@Mock
private LocalDataSourceServiceImpl localDataSourceService;

@Mock
private ExternalDataSourceServiceImpl basicDataSourceService;

PropertyUtil propertyUtil = new PropertyUtil();

@Before
public void setUp() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
dataSource = DynamicDataSource.getInstance();
ReflectionTestUtils.setField(dataSource, "localDataSourceService", localDataSourceService);
ReflectionTestUtils.setField(dataSource, "basicDataSourceService", basicDataSourceService);

}

@Test
public void test005WithClusterAndNullDatabase() {
// 模拟设置环境05:指定集群,未指定数据库,UseExternalDB是true,数据库类型是mysql
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "false");
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "");
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME));
PropertyUtil.setEmbeddedStorage(EnvUtil.getStandaloneMode());

// 模拟初始化
propertyUtil.initialize(null);

Assert.assertFalse(EnvUtil.getStandaloneMode());
Assert.assertTrue(PropertyUtil.isUseExternalDB());
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl);
}

@Test
public void test006WithClusterAndMysqlDatabase() {
// 模拟设置环境06:指定集群,指定数据库mysql,UseExternalDB是true,数据库类型是mysql
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "false");
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "mysql");
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME));
PropertyUtil.setEmbeddedStorage(EnvUtil.getStandaloneMode());

// 模拟初始化
propertyUtil.initialize(null);

Assert.assertFalse(EnvUtil.getStandaloneMode());
Assert.assertTrue(PropertyUtil.isUseExternalDB());
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl);
}

@Test
public void test007WithClusterAndDerbyDatabase() {
// 模拟设置环境07:指定集群,指定数据库derby,UseExternalDB是false,数据库类型是derby
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "false");
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "derby");
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME));
PropertyUtil.setEmbeddedStorage(true);

// 模拟初始化
propertyUtil.initialize(null);

Assert.assertFalse(EnvUtil.getStandaloneMode());
Assert.assertFalse(PropertyUtil.isUseExternalDB());
Assert.assertTrue(dataSource.getDataSource() instanceof LocalDataSourceServiceImpl);
}

@Test
public void test008WithClusterAndOtherDatabase() {
// 模拟设置环境08: 指定集群,指定数据库其他,UseExternalDB是true,数据库类型是其他
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "false");
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "postgresql");
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME));
PropertyUtil.setEmbeddedStorage(EnvUtil.getStandaloneMode());

// 模拟初始化
propertyUtil.initialize(null);

Assert.assertFalse(EnvUtil.getStandaloneMode());
Assert.assertTrue(PropertyUtil.isUseExternalDB());
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright 1999-2022 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.nacos.config.server.utils;

import com.alibaba.nacos.config.server.constant.PropertiesConstant;
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceServiceImpl;
import com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl;
import com.alibaba.nacos.sys.env.Constants;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils;

/**
* StandaloneExternalStorage unit test.
*
* @author Long Yu
* @since 2.2.0
*/
@RunWith(MockitoJUnitRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class StandaloneExternalStorageTest {

@InjectMocks
private DynamicDataSource dataSource;

private MockEnvironment environment;

@Mock
private LocalDataSourceServiceImpl localDataSourceService;

@Mock
private ExternalDataSourceServiceImpl basicDataSourceService;

PropertyUtil propertyUtil = new PropertyUtil();

@Before
public void setUp() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
dataSource = DynamicDataSource.getInstance();
ReflectionTestUtils.setField(dataSource, "localDataSourceService", localDataSourceService);
ReflectionTestUtils.setField(dataSource, "basicDataSourceService", basicDataSourceService);
}

@Test
public void test001WithStandaloneAndNullDatabase() {
// 模拟设置环境01:指定单例,未指定数据库,UseExternalDB是false
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true");
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "");
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME));

// 模拟初始化
propertyUtil.initialize(null);

Assert.assertTrue(EnvUtil.getStandaloneMode());
Assert.assertTrue(dataSource.getDataSource() instanceof LocalDataSourceServiceImpl);
Assert.assertFalse(PropertyUtil.isUseExternalDB());
}

@Test
public void test002WithStandaloneAndDerbyDatabase() {
// 模拟设置环境02:指定单例,指定数据库derby,UseExternalDB是false
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true");
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "derby");
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME));
// 模拟初始化

propertyUtil.initialize(null);

Assert.assertTrue(EnvUtil.getStandaloneMode());
Assert.assertTrue(dataSource.getDataSource() instanceof LocalDataSourceServiceImpl);
Assert.assertFalse(PropertyUtil.isUseExternalDB());
}

@Test
public void test003WithStandaloneAndMysqlDatabase() {
// 模拟设置环境03:指定单例,指定数据库为mysql, UseExternalDB是true
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true");
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "mysql");
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME));
// 模拟初始化

propertyUtil.initialize(null);

Assert.assertTrue(EnvUtil.getStandaloneMode());
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl);
Assert.assertTrue(PropertyUtil.isUseExternalDB());
}

@Test
public void test004WithStandaloneAndOtherDatabase() {
// 模拟设置环境04:指定单例,指定数据库为其他, UseExternalDB是true
System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true");
environment.setProperty(PropertiesConstant.SPRING_DATASOURCE_PLATFORM, "postgresql");
EnvUtil.setIsStandalone(Boolean.getBoolean(Constants.STANDALONE_MODE_PROPERTY_NAME));
// 模拟初始化

propertyUtil.initialize(null);

Assert.assertTrue(EnvUtil.getStandaloneMode());
Assert.assertTrue(dataSource.getDataSource() instanceof ExternalDataSourceServiceImpl);
Assert.assertTrue(PropertyUtil.isUseExternalDB());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class StartingApplicationListener implements NacosApplicationListener {

private static final String DEFAULT_DATABASE = "mysql";

private static final String DERBY_DATABASE = "derby";

private static final String DATASOURCE_PLATFORM_PROPERTY = "spring.datasource.platform";

private static final String DEFAULT_DATASOURCE_PLATFORM = "";
Expand Down Expand Up @@ -243,8 +245,10 @@ private void logStarting() {
private void judgeStorageMode(ConfigurableEnvironment env) {

// External data sources are used by default in cluster mode
boolean useExternalStorage = (DEFAULT_DATABASE.equalsIgnoreCase(
env.getProperty(DATASOURCE_PLATFORM_PROPERTY, DEFAULT_DATASOURCE_PLATFORM)));
// External data sources are used by default in cluster mode
String platform = env.getProperty(DATASOURCE_PLATFORM_PROPERTY, DEFAULT_DATASOURCE_PLATFORM);
boolean useExternalStorage = !DEFAULT_DATASOURCE_PLATFORM.equalsIgnoreCase(platform)
&& !DERBY_DATABASE.equalsIgnoreCase(platform);

// must initialize after setUseExternalDB
// This value is true in stand-alone mode and false in cluster mode
Expand Down

0 comments on commit 2168395

Please sign in to comment.