luozenglin commented on code in PR #18184: URL: https://github.com/apache/doris/pull/18184#discussion_r1152157163
########## fe/fe-core/src/main/java/org/apache/doris/catalog/ResourceGroupMgr.java: ########## @@ -0,0 +1,225 @@ +// 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.catalog; + +import org.apache.doris.analysis.CreateResourceGroupStmt; +import org.apache.doris.common.DdlException; +import org.apache.doris.common.UserException; +import org.apache.doris.common.io.Text; +import org.apache.doris.common.io.Writable; +import org.apache.doris.common.proc.BaseProcResult; +import org.apache.doris.common.proc.ProcNodeInterface; +import org.apache.doris.common.proc.ProcResult; +import org.apache.doris.persist.gson.GsonUtils; +import org.apache.doris.thrift.TPipelineResourceGroup; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.gson.annotations.SerializedName; +import org.apache.commons.lang.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +public class ResourceGroupMgr implements Writable { Review Comment: done ########## fe/fe-core/src/main/java/org/apache/doris/catalog/ResourceGroupMgr.java: ########## @@ -0,0 +1,225 @@ +// 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.catalog; + +import org.apache.doris.analysis.CreateResourceGroupStmt; +import org.apache.doris.common.DdlException; +import org.apache.doris.common.UserException; +import org.apache.doris.common.io.Text; +import org.apache.doris.common.io.Writable; +import org.apache.doris.common.proc.BaseProcResult; +import org.apache.doris.common.proc.ProcNodeInterface; +import org.apache.doris.common.proc.ProcResult; +import org.apache.doris.persist.gson.GsonUtils; +import org.apache.doris.thrift.TPipelineResourceGroup; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.gson.annotations.SerializedName; +import org.apache.commons.lang.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +public class ResourceGroupMgr implements Writable { + + private static final Logger LOG = LogManager.getLogger(ResourceGroupMgr.class); + + public static final String DEFAULT_GROUP_NAME = "default"; + + private static final String CPU_SCHEDULING_WEIGHT = "cpu_scheduling_weight"; + + private static final String CONCURRENCY_LIMIT = "concurrency_limit"; + + public static final ImmutableList<String> RESOURCE_GROUP_PROC_NODE_TITLE_NAMES = new ImmutableList.Builder<String>() + .add("Id").add("Name").add("Item").add("Value") + .build(); + + private static final ImmutableSet<String> REQUIRED_PROPERTIES_NAME = new ImmutableSet.Builder<String>().add( + CPU_SCHEDULING_WEIGHT).build(); + + private static final ImmutableSet<String> ALL_PROPERTIES_NAME = new ImmutableSet.Builder<String>().add( + CPU_SCHEDULING_WEIGHT).add(CONCURRENCY_LIMIT).build(); + + @SerializedName(value = "nameToResourceGroup") + private final Map<String, PipelineResourceGroup> nameToResourceGroup = Maps.newHashMap(); + + private final ResourceProcNode procNode = new ResourceProcNode(); + + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + + public ResourceGroupMgr() { + } + + private void readLock() { + lock.readLock().lock(); + } + + private void readUnlock() { + lock.readLock().unlock(); + } + + private void writeLock() { + lock.writeLock().lock(); + } + + private void writeUnlock() { + lock.writeLock().unlock(); + } + + public void init() { + checkAndCreateDefaultGroup(); + } + + public List<TPipelineResourceGroup> getResourceGroup(String groupName) throws UserException { + List<TPipelineResourceGroup> resourceGroups = Lists.newArrayList(); + readLock(); + try { + if (!nameToResourceGroup.containsKey(groupName)) { + throw new UserException("Resource group " + groupName + " does not exist"); + } + // need to check resource group privs + PipelineResourceGroup resourceGroup; + do { + resourceGroup = nameToResourceGroup.get(groupName); + resourceGroups.add(resourceGroup.toThrift()); + groupName = resourceGroup.getParentName(); + } while (groupName != null); + } finally { + readUnlock(); + } + return resourceGroups; + } + + private void checkAndCreateDefaultGroup() { + PipelineResourceGroup defaultResourceGroup; + writeLock(); + try { + if (nameToResourceGroup.containsKey(DEFAULT_GROUP_NAME)) { + return; + } + Map<String, String> properties = Maps.newHashMap(); + properties.put(CPU_SCHEDULING_WEIGHT, "10"); + defaultResourceGroup = new PipelineResourceGroup(Env.getCurrentEnv().getNextId(), + DEFAULT_GROUP_NAME, properties, null); + nameToResourceGroup.put(DEFAULT_GROUP_NAME, defaultResourceGroup); + } finally { + writeUnlock(); + } + + Env.getCurrentEnv().getEditLog().logCreateResourceGroup(defaultResourceGroup); + LOG.info("Create resource group success: {}", defaultResourceGroup); + } + + public void createResourceGroup(CreateResourceGroupStmt stmt) throws DdlException { + checkProperties(stmt.getProperties()); + PipelineResourceGroup resourceGroup = new PipelineResourceGroup(Env.getCurrentEnv().getNextId(), + stmt.getResourceGroupName(), + stmt.getProperties(), null); + String resourceGroupNameName = resourceGroup.getName(); + writeLock(); + try { + if (nameToResourceGroup.putIfAbsent(resourceGroupNameName, resourceGroup) != null) { + if (stmt.isIfNotExists()) { + return; + } else { + throw new DdlException("Resource group(" + resourceGroupNameName + ") already exist"); + } + } + } finally { + writeUnlock(); + } + + Env.getCurrentEnv().getEditLog().logCreateResourceGroup(resourceGroup); Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
