Skip to content

Commit 182e5c6

Browse files
authored
chore(query): add max node quota (#18474)
* chore(query): add max node quota * chore(query): add max node quota * chore(query): add max node quota * chore(query): add max node quota
1 parent 473202d commit 182e5c6

File tree

8 files changed

+47
-11
lines changed

8 files changed

+47
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/license/src/license.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use serde::Serialize;
2222

2323
#[derive(Debug, Clone, Eq, Ord, PartialOrd, PartialEq, serde::Serialize, serde::Deserialize)]
2424
pub struct ComputeQuota {
25-
threads_num: Option<usize>,
26-
memory_usage: Option<usize>,
25+
pub threads_num: Option<usize>,
26+
pub memory_usage: Option<usize>,
2727
}
2828

2929
#[derive(Debug, Clone, Eq, Ord, PartialOrd, PartialEq, serde::Serialize, serde::Deserialize)]
@@ -85,6 +85,10 @@ pub enum Feature {
8585
VectorIndex,
8686
#[serde(alias = "private_task", alias = "PRIVATE_TASK")]
8787
PrivateTask,
88+
#[serde(alias = "max_node_quota", alias = "MAX_NODE_QUOTA")]
89+
MaxNodeQuota(usize),
90+
#[serde(alias = "max_cpu_quota", alias = "MAX_CPU_QUOTA")]
91+
MaxCpuQuota(usize),
8892
#[serde(other)]
8993
Unknown,
9094
}
@@ -138,13 +142,18 @@ impl fmt::Display for Feature {
138142
Feature::VectorIndex => write!(f, "vector_index"),
139143
Feature::PrivateTask => write!(f, "private_task"),
140144
Feature::Unknown => write!(f, "unknown"),
145+
Feature::MaxCpuQuota(v) => write!(f, "max_cpu_quota({})", v),
146+
Feature::MaxNodeQuota(v) => write!(f, "max_node_quota({})", v),
141147
}
142148
}
143149
}
144150

145151
impl Feature {
146152
pub fn verify_default(&self, message: impl Into<String>) -> Result<(), ErrorCode> {
147-
Err(ErrorCode::LicenseKeyInvalid(message.into()))
153+
match self {
154+
Feature::MaxCpuQuota(_) | Feature::MaxNodeQuota(_) => Ok(()),
155+
_ => Err(ErrorCode::LicenseKeyInvalid(message.into())),
156+
}
148157
}
149158

150159
pub fn verify(&self, feature: &Feature) -> Result<bool, ErrorCode> {
@@ -173,6 +182,8 @@ impl Feature {
173182

174183
Ok(true)
175184
}
185+
(Feature::MaxCpuQuota(c), Feature::MaxCpuQuota(v)) => Ok(c > v),
186+
(Feature::MaxNodeQuota(c), Feature::MaxNodeQuota(v)) => Ok(c > v),
176187
(Feature::Test, Feature::Test)
177188
| (Feature::AggregateIndex, Feature::AggregateIndex)
178189
| (Feature::ComputedColumn, Feature::ComputedColumn)
@@ -380,6 +391,11 @@ mod tests {
380391
serde_json::from_str::<Feature>("\"private_task\"").unwrap()
381392
);
382393

394+
assert_eq!(
395+
Feature::MaxNodeQuota(1),
396+
serde_json::from_str::<Feature>("{\"MaxNodeQuota\": 1}").unwrap()
397+
);
398+
383399
assert_eq!(
384400
Feature::Unknown,
385401
serde_json::from_str::<Feature>("\"ssss\"").unwrap()

src/meta/types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ log = { workspace = true }
1919
map-api = { workspace = true }
2020
num-derive = { workspace = true }
2121
num-traits = { workspace = true }
22+
num_cpus = { workspace = true }
2223
openraft = { workspace = true }
2324
pretty_assertions = { workspace = true }
2425
prost = { workspace = true }

src/meta/types/src/cluster.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ impl NodeInfo {
5656
pub fn create(
5757
id: String,
5858
secret: String,
59-
cpu_nums: u64,
6059
http_address: String,
6160
flight_address: String,
6261
discovery_address: String,
6362
binary_version: String,
6463
cache_id: String,
6564
) -> NodeInfo {
65+
let cpu_nums = num_cpus::get() as u64;
6666
NodeInfo {
6767
id,
6868
secret,

src/query/catalog/tests/it/partitions.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ fn create_node(cache_id: String) -> Arc<NodeInfo> {
7979
Arc::new(NodeInfo::create(
8080
GlobalUniqName::unique(),
8181
String::new(),
82-
0,
8382
String::new(),
8483
String::new(),
8584
String::new(),

src/query/service/src/clusters/cluster.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ use databend_common_config::InnerConfig;
4343
use databend_common_exception::ErrorCode;
4444
use databend_common_exception::Result;
4545
use databend_common_grpc::ConnectionFactory;
46+
use databend_common_license::license::Feature;
47+
use databend_common_license::license_manager::LicenseManagerSwitch;
4648
use databend_common_management::WarehouseApi;
4749
use databend_common_management::WarehouseMgr;
50+
use databend_common_meta_app::tenant::Tenant;
4851
use databend_common_meta_store::MetaStore;
4952
use databend_common_meta_store::MetaStoreProvider;
5053
use databend_common_meta_types::NodeInfo;
5154
use databend_common_meta_types::SeqV;
5255
use databend_common_metrics::cluster::*;
56+
use databend_common_settings::Settings;
5357
use databend_common_version::DATABEND_COMMIT_VERSION;
5458
use databend_enterprise_resources_management::ResourcesManagement;
5559
use futures::future::select;
@@ -322,7 +326,6 @@ impl ClusterDiscovery {
322326
let mut node_info = NodeInfo::create(
323327
config.query.node_id.clone(),
324328
config.query.node_secret.clone(),
325-
config.query.num_cpus,
326329
format!(
327330
"{}:{}",
328331
config.query.http_handler_host, config.query.http_handler_port
@@ -387,7 +390,6 @@ impl ClusterDiscovery {
387390
let mut node_info = NodeInfo::create(
388391
config.query.node_id.clone(),
389392
config.query.node_secret.clone(),
390-
config.query.num_cpus,
391393
format!(
392394
"{}:{}",
393395
config.query.http_handler_host, config.query.http_handler_port
@@ -535,7 +537,6 @@ impl ClusterDiscovery {
535537

536538
#[async_backtrace::framed]
537539
pub async fn register_to_metastore(self: &Arc<Self>, cfg: &InnerConfig) -> Result<()> {
538-
let cpus = cfg.query.num_cpus;
539540
let mut address = cfg.query.flight_api_address.clone();
540541
let mut http_address = format!(
541542
"{}:{}",
@@ -601,7 +602,6 @@ impl ClusterDiscovery {
601602
let mut node_info = NodeInfo::create(
602603
self.local_id.clone(),
603604
self.local_secret.clone(),
604-
cpus,
605605
http_address,
606606
address,
607607
discovery_address,
@@ -614,6 +614,9 @@ impl ClusterDiscovery {
614614

615615
self.drop_invalid_nodes(&node_info).await?;
616616

617+
let online_nodes = self.warehouse_manager.list_online_nodes().await?;
618+
self.check_license_key(online_nodes).await?;
619+
617620
match self.warehouse_manager.start_node(node_info).await {
618621
Ok(seq_node) => self.start_heartbeat(seq_node).await,
619622
Err(cause) => Err(cause.add_message_back("(while cluster api add_node).")),
@@ -628,6 +631,24 @@ impl ClusterDiscovery {
628631
heartbeat.start(node_info, seq);
629632
Ok(())
630633
}
634+
635+
async fn check_license_key(&self, nodes: Vec<NodeInfo>) -> Result<()> {
636+
let license_key = Self::get_license_key(&self.tenant_id).await?;
637+
638+
let total_cpu_nums = nodes.iter().map(|x| x.cpu_nums).sum::<u64>();
639+
LicenseManagerSwitch::instance()
640+
.check_enterprise_enabled(license_key.clone(), Feature::MaxNodeQuota(nodes.len()))?;
641+
642+
LicenseManagerSwitch::instance()
643+
.check_enterprise_enabled(license_key, Feature::MaxCpuQuota(total_cpu_nums as usize))
644+
}
645+
646+
async fn get_license_key(tenant: &str) -> Result<String> {
647+
// We must get the license key from settings. It may be in the configuration file.
648+
let settings = Settings::create(Tenant::new_literal(tenant));
649+
settings.load_changes().await?;
650+
Ok(settings.get_enterprise_license())
651+
}
631652
}
632653

633654
struct ClusterHeartbeat {

src/query/service/src/test_kits/cluster.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ impl ClusterDescriptor {
3838
new_nodes.push(Arc::new(NodeInfo::create(
3939
id.clone(),
4040
"".to_string(),
41-
0,
4241
"".to_string(),
4342
addr.into(),
4443
"".to_string(),

src/query/service/tests/it/sql/planner/optimizer/optimizer_test.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,6 @@ fn create_node(local_id: &str) -> Arc<NodeInfo> {
507507
let mut node_info = NodeInfo::create(
508508
local_id.to_string(),
509509
String::new(),
510-
0,
511510
String::new(),
512511
String::new(),
513512
String::new(),

0 commit comments

Comments
 (0)