[improvement](resource-group) add test for resource group (#18575)

Co-authored-by: wangbo <youseebiggirl_t_t@qq.com>
This commit is contained in:
luozenglin
2023-04-18 20:20:50 +08:00
committed by GitHub
parent c323bc44ff
commit 5c076b738b
7 changed files with 297 additions and 6 deletions

View File

@ -1738,7 +1738,8 @@ public class Config extends ConfigBase {
@ConfField
public static boolean enable_pipeline_load = false;
@ConfField
// enable_resource_group should be immutable and temporarily set to mutable during the development test phase
@ConfField(mutable = true, masterOnly = true, expType = ExperimentalType.EXPERIMENTAL)
public static boolean enable_resource_group = false;
@ConfField(mutable = false, masterOnly = true)

View File

@ -65,7 +65,7 @@ public class ResourceGroup implements Writable {
this.version = 0;
}
public static ResourceGroup createResourceGroup(String name, Map<String, String> properties) throws DdlException {
public static ResourceGroup create(String name, Map<String, String> properties) throws DdlException {
checkProperties(properties);
return new ResourceGroup(Env.getCurrentEnv().getNextId(), name, properties);
}

View File

@ -84,7 +84,7 @@ public class ResourceGroupMgr implements Writable, GsonPostProcessable {
}
public void init() {
if (Config.enable_resource_group) {
if (Config.enable_resource_group || Config.use_fuzzy_session_variable /* for github workflow */) {
checkAndCreateDefaultGroup();
}
}
@ -114,7 +114,7 @@ public class ResourceGroupMgr implements Writable, GsonPostProcessable {
}
Map<String, String> properties = Maps.newHashMap();
properties.put(ResourceGroup.CPU_SHARE, "10");
defaultResourceGroup = ResourceGroup.createResourceGroup(DEFAULT_GROUP_NAME, properties);
defaultResourceGroup = ResourceGroup.create(DEFAULT_GROUP_NAME, properties);
nameToResourceGroup.put(DEFAULT_GROUP_NAME, defaultResourceGroup);
idToResourceGroup.put(defaultResourceGroup.getId(), defaultResourceGroup);
Env.getCurrentEnv().getEditLog().logCreateResourceGroup(defaultResourceGroup);
@ -127,8 +127,11 @@ public class ResourceGroupMgr implements Writable, GsonPostProcessable {
}
public void createResourceGroup(CreateResourceGroupStmt stmt) throws DdlException {
ResourceGroup resourceGroup = ResourceGroup.createResourceGroup(stmt.getResourceGroupName(),
stmt.getProperties());
if (!Config.enable_resource_group) {
throw new DdlException("unsupported feature now, coming soon.");
}
ResourceGroup resourceGroup = ResourceGroup.create(stmt.getResourceGroupName(), stmt.getProperties());
String resourceGroupNameName = resourceGroup.getName();
writeLock();
try {
@ -160,6 +163,16 @@ public class ResourceGroupMgr implements Writable, GsonPostProcessable {
return procNode.fetchResult().getRows();
}
// for ut
public Map<String, ResourceGroup> getNameToResourceGroup() {
return nameToResourceGroup;
}
// for ut
public Map<Long, ResourceGroup> getIdToResourceGroup() {
return idToResourceGroup;
}
@Override
public void write(DataOutput out) throws IOException {
String json = GsonUtils.GSON.toJson(this);

View File

@ -0,0 +1,146 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 org.apache.doris.resource.resourcegroup;
import org.apache.doris.analysis.CreateResourceGroupStmt;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.Config;
import org.apache.doris.common.DdlException;
import org.apache.doris.common.UserException;
import org.apache.doris.persist.EditLog;
import org.apache.doris.thrift.TPipelineResourceGroup;
import com.google.common.collect.Maps;
import mockit.Delegate;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
public class ResourceGroupMgrTest {
@Injectable
private EditLog editLog;
@Mocked
private Env env;
private AtomicLong id = new AtomicLong(10);
@Before
public void setUp() throws DdlException {
new Expectations() {
{
env.getEditLog();
minTimes = 0;
result = editLog;
env.getNextId();
minTimes = 0;
result = new Delegate() {
long delegate() {
return id.addAndGet(1);
}
};
editLog.logCreateResourceGroup((ResourceGroup) any);
minTimes = 0;
Env.getCurrentEnv();
minTimes = 0;
result = env;
}
};
}
@Test
public void testCreateResourceGroup() throws DdlException {
Config.enable_resource_group = true;
ResourceGroupMgr resourceGroupMgr = new ResourceGroupMgr();
Map<String, String> properties1 = Maps.newHashMap();
properties1.put(ResourceGroup.CPU_SHARE, "10");
String name1 = "g1";
CreateResourceGroupStmt stmt1 = new CreateResourceGroupStmt(false, name1, properties1);
resourceGroupMgr.createResourceGroup(stmt1);
Map<String, ResourceGroup> nameToRG = resourceGroupMgr.getNameToResourceGroup();
Assert.assertEquals(1, nameToRG.size());
Assert.assertTrue(nameToRG.containsKey(name1));
ResourceGroup group1 = nameToRG.get(name1);
Assert.assertEquals(name1, group1.getName());
Map<Long, ResourceGroup> idToRG = resourceGroupMgr.getIdToResourceGroup();
Assert.assertEquals(1, idToRG.size());
Assert.assertTrue(idToRG.containsKey(group1.getId()));
Map<String, String> properties2 = Maps.newHashMap();
properties2.put(ResourceGroup.CPU_SHARE, "20");
String name2 = "g2";
CreateResourceGroupStmt stmt2 = new CreateResourceGroupStmt(false, name2, properties2);
resourceGroupMgr.createResourceGroup(stmt2);
nameToRG = resourceGroupMgr.getNameToResourceGroup();
Assert.assertEquals(2, nameToRG.size());
Assert.assertTrue(nameToRG.containsKey(name2));
ResourceGroup group2 = nameToRG.get(name2);
idToRG = resourceGroupMgr.getIdToResourceGroup();
Assert.assertEquals(2, idToRG.size());
Assert.assertTrue(idToRG.containsKey(group2.getId()));
try {
resourceGroupMgr.createResourceGroup(stmt2);
Assert.fail();
} catch (DdlException e) {
Assert.assertTrue(e.getMessage().contains("already exist"));
}
CreateResourceGroupStmt stmt3 = new CreateResourceGroupStmt(true, name2, properties2);
resourceGroupMgr.createResourceGroup(stmt3);
Assert.assertEquals(2, resourceGroupMgr.getIdToResourceGroup().size());
Assert.assertEquals(2, resourceGroupMgr.getNameToResourceGroup().size());
}
@Test
public void testGetResourceGroup() throws UserException {
Config.enable_resource_group = true;
ResourceGroupMgr resourceGroupMgr = new ResourceGroupMgr();
Map<String, String> properties1 = Maps.newHashMap();
properties1.put(ResourceGroup.CPU_SHARE, "10");
String name1 = "g1";
CreateResourceGroupStmt stmt1 = new CreateResourceGroupStmt(false, name1, properties1);
resourceGroupMgr.createResourceGroup(stmt1);
List<TPipelineResourceGroup> tResourceGroups1 = resourceGroupMgr.getResourceGroup(name1);
Assert.assertEquals(1, tResourceGroups1.size());
TPipelineResourceGroup tResourceGroup1 = tResourceGroups1.get(0);
Assert.assertEquals(name1, tResourceGroup1.getName());
Assert.assertTrue(tResourceGroup1.getProperties().containsKey(ResourceGroup.CPU_SHARE));
try {
resourceGroupMgr.getResourceGroup("g2");
Assert.fail();
} catch (UserException e) {
Assert.assertTrue(e.getMessage().contains("does not exist"));
}
}
}

View File

@ -0,0 +1,100 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 org.apache.doris.resource.resourcegroup;
import org.apache.doris.common.DdlException;
import org.apache.doris.common.proc.BaseProcResult;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
import java.util.Map;
public class ResourceGroupTest {
@Test
public void testCreateNormal() throws DdlException {
Map<String, String> properties1 = Maps.newHashMap();
properties1.put(ResourceGroup.CPU_SHARE, "10");
String name1 = "g1";
ResourceGroup group1 = ResourceGroup.create(name1, properties1);
Assert.assertEquals(name1, group1.getName());
Assert.assertEquals(1, group1.getProperties().size());
Assert.assertTrue(group1.getProperties().containsKey(ResourceGroup.CPU_SHARE));
}
@Test(expected = DdlException.class)
public void testNotSupportProperty() throws DdlException {
Map<String, String> properties1 = Maps.newHashMap();
properties1.put(ResourceGroup.CPU_SHARE, "10");
properties1.put("share", "10");
String name1 = "g1";
ResourceGroup.create(name1, properties1);
}
@Test(expected = DdlException.class)
public void testRequiredProperty() throws DdlException {
Map<String, String> properties1 = Maps.newHashMap();
String name1 = "g1";
ResourceGroup.create(name1, properties1);
}
@Test
public void testCpuShareValue() {
Map<String, String> properties1 = Maps.newHashMap();
properties1.put(ResourceGroup.CPU_SHARE, "0");
String name1 = "g1";
try {
ResourceGroup.create(name1, properties1);
Assert.fail();
} catch (DdlException e) {
Assert.assertTrue(e.getMessage().contains(ResourceGroup.CPU_SHARE + " requires a positive integer."));
}
properties1.put(ResourceGroup.CPU_SHARE, "cpu");
try {
ResourceGroup.create(name1, properties1);
Assert.fail();
} catch (DdlException e) {
Assert.assertTrue(e.getMessage().contains(ResourceGroup.CPU_SHARE + " requires a positive integer."));
}
}
@Test
public void testGetProcNodeData() throws DdlException {
Map<String, String> properties1 = Maps.newHashMap();
properties1.put(ResourceGroup.CPU_SHARE, "10");
String name1 = "g1";
ResourceGroup group1 = ResourceGroup.create(name1, properties1);
BaseProcResult result = new BaseProcResult();
group1.getProcNodeData(result);
List<List<String>> rows = result.getRows();
Assert.assertEquals(1, rows.size());
List<List<String>> expectedRows = Lists.newArrayList();
expectedRows.add(Lists.newArrayList(String.valueOf(group1.getId()), name1, ResourceGroup.CPU_SHARE, "10"));
for (int i = 0; i < expectedRows.size(); ++i) {
for (int j = 0; j < expectedRows.get(i).size(); ++j) {
Assert.assertEquals(expectedRows.get(i).get(j), rows.get(i).get(j));
}
}
}
}

View File

@ -18,9 +18,13 @@
// This suit test the `resource_groups` tvf
// TO DO
suite("test_resource_groups_tvf") {
sql """ADMIN SET FRONTEND CONFIG ("experimental_enable_resource_group" = "true");"""
def name1 = "test";
sql "create resource group if not exists ${name1} properties('cpu_share'='10');"
List<List<Object>> table = sql """ select * from resource_groups(); """
assertTrue(table.size() > 0)
assertTrue(table[0].size == 4) // column should be 4
sql """ADMIN SET FRONTEND CONFIG ("experimental_enable_resource_group" = "false");"""
}

View File

@ -0,0 +1,27 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
suite("test_resource_group") {
sql """ADMIN SET FRONTEND CONFIG ("experimental_enable_resource_group" = "true");"""
def name1 = "g1";
sql "create resource group if not exists ${name1} properties('cpu_share'='10');"
List<List<Object>> results = sql "show resource groups;"
assertTrue(results.size() >= 2)
assertEquals(4, results[0].size())
sql """ADMIN SET FRONTEND CONFIG ("experimental_enable_resource_group" = "false");"""
}