Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
fix(cluster): reserving or releasing node resources while resetting a…
Browse files Browse the repository at this point in the history
… node
  • Loading branch information
yahortsaryk committed Jul 3, 2023
1 parent 8cb0c0a commit 531ffa0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
4 changes: 0 additions & 4 deletions bucket/ddc_bucket/cdn_node/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ impl CdnNode {
self.status_in_cluster = Some(status);
}

pub fn cdn_account_id(&self) -> AccountId {
self.provider_id
}

pub fn put_payment(&mut self, amount: Balance) {
self.undistributed_payment += amount;
}
Expand Down
24 changes: 16 additions & 8 deletions bucket/ddc_bucket/cluster/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl DdcBucket {

cluster.add_node(node_key)?;
for _v_node in &v_nodes {
node.reserve_resource(cluster.resource_per_v_node)?;
cluster.total_rent += node.rent_per_month;
}
self.clusters.update(cluster_id, &cluster)?;
Expand Down Expand Up @@ -105,6 +106,7 @@ impl DdcBucket {
cluster.remove_node(node_key);
let v_nodes = self.topology.get_v_nodes_by_node(node_key);
for _v_node in &v_nodes {
node.release_resource(cluster.resource_per_v_node);
cluster.total_rent -= node.rent_per_month;
}
self.clusters.update(cluster_id, &cluster)?;
Expand Down Expand Up @@ -139,11 +141,11 @@ impl DdcBucket {

// Give back resources to the old node
let mut old_node = self.nodes.get(old_node_key)?;
old_node.put_resource(cluster.resource_per_v_node);
old_node.release_resource(cluster.resource_per_v_node);
self.nodes.update(old_node_key, &old_node)?;

// Reserve resources on the new node.
new_node.take_resource(cluster.resource_per_v_node)?;
new_node.reserve_resource(cluster.resource_per_v_node)?;
self.nodes.update(new_node_key, &new_node)?;
}

Expand All @@ -170,7 +172,7 @@ impl DdcBucket {
) -> Result<()> {
let caller = Self::env().caller();

let cluster = self.clusters.get(cluster_id)?;
let mut cluster = self.clusters.get(cluster_id)?;

let mut node = self.nodes.get(node_key)?;
node.only_with_cluster(cluster_id)?;
Expand All @@ -181,17 +183,22 @@ impl DdcBucket {
if new_v_nodes.len() > old_v_nodes.len() {

for _i in 0..new_v_nodes.len() - old_v_nodes.len() {
node.take_resource(cluster.resource_per_v_node)?;
self.nodes.update(node_key, &node)?;
node.reserve_resource(cluster.resource_per_v_node)?;
cluster.total_rent += node.rent_per_month;
}

self.nodes.update(node_key, &node)?;
self.clusters.update(cluster_id, &cluster)?;

} else if new_v_nodes.len() < old_v_nodes.len() {

for _i in 0..old_v_nodes.len() - new_v_nodes.len() {
node.put_resource(cluster.resource_per_v_node);
self.nodes.update(node_key, &node)?;
node.release_resource(cluster.resource_per_v_node);
cluster.total_rent -= node.rent_per_month;
}

self.nodes.update(node_key, &node)?;
self.clusters.update(cluster_id, &cluster)?;
}

self.topology.reset_node(
Expand Down Expand Up @@ -410,14 +417,15 @@ impl DdcBucket {
for v_node in cluster_v_nodes {
let node_key = self.topology.get_node_by_v_node(cluster_id, v_node)?;
let mut node = self.nodes.get(node_key)?;
node.take_resource(resource)?;
node.reserve_resource(resource)?;
self.nodes.update(node_key, &node)?;
}

Self::env().emit_event(ClusterReserveResource {
cluster_id,
resource,
});

Ok(())
}

Expand Down
10 changes: 3 additions & 7 deletions bucket/ddc_bucket/node/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,12 @@ impl Node {
pub fn change_status_in_cluster(&mut self, status: NodeStatusInCluster) {
self.status_in_cluster = Some(status);
}

pub fn revenue_account_id(&self) -> AccountId {
self.provider_id
}

pub fn put_resource(&mut self, amount: Resource) {

pub fn release_resource(&mut self, amount: Resource) {
self.free_resource += amount;
}

pub fn take_resource(&mut self, amount: Resource) -> Result<()> {
pub fn reserve_resource(&mut self, amount: Resource) -> Result<()> {
if self.free_resource >= amount {
self.free_resource -= amount;
Ok(())
Expand Down

0 comments on commit 531ffa0

Please sign in to comment.