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

Commit

Permalink
fix(cluster): setting resource value that is being used by virtual nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Jul 3, 2023
1 parent 531ffa0 commit bd1ed1b
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 274 deletions.
2 changes: 1 addition & 1 deletion bucket/ddc_bucket/bucket/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl DdcBucket {
let mut bucket = self.buckets.get(bucket_id)?;
let mut cluster = self.clusters.get(bucket.cluster_id)?;
Self::only_owner_or_cluster_manager(&bucket, &cluster)?;

// todo: fix resource allocation
cluster.take_resource(resource)?;
self.clusters.update(bucket.cluster_id, &cluster)?;
bucket.put_resource(resource);
Expand Down
11 changes: 4 additions & 7 deletions bucket/ddc_bucket/cluster/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ impl Cluster {
pub fn new(
manager_id: AccountId,
cluster_params: ClusterParams,
resource_per_v_node: Resource,
) -> Result<Self> {

let mut cluster = Cluster {
manager_id,
cluster_params: ClusterParams::default(),
nodes_keys: Vec::new(),
resource_per_v_node: 0,
resource_per_v_node,
resource_used: 0,
revenues: Cash(0),
total_rent: 0,
Expand Down Expand Up @@ -138,12 +139,8 @@ impl Cluster {
rent
}

pub fn set_rent(&mut self, rent: Balance) {
self.total_rent = rent;
}

pub fn put_resource(&mut self, amount: Resource) {
self.resource_per_v_node += amount;
pub fn set_resource_per_v_node(&mut self, resource_per_v_node: Resource) {
self.resource_per_v_node = resource_per_v_node;
}

pub fn take_resource(&mut self, amount: Resource) -> Result<()> {
Expand Down
59 changes: 33 additions & 26 deletions bucket/ddc_bucket/cluster/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ impl DdcBucket {
pub fn message_cluster_create(
&mut self,
cluster_params: ClusterParams,
resource_per_v_node: Resource,
) -> Result<ClusterId> {
let caller = Self::env().caller();

let cluster_id = self.clusters.create(
caller,
cluster_params.clone()
cluster_params.clone(),
resource_per_v_node
)?;

self.topology.create_topology(cluster_id)?;
Expand Down Expand Up @@ -63,13 +65,13 @@ impl DdcBucket {
cluster.only_manager(caller)?;

node.set_cluster(cluster_id, NodeStatusInCluster::ADDING);
self.nodes.update(node_key, &node)?;

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;
cluster.total_rent += node.rent_v_node_per_month;
}

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

self.topology.add_node(cluster_id, node_key, v_nodes)?;
Expand Down Expand Up @@ -101,14 +103,14 @@ impl DdcBucket {
node.only_with_cluster(cluster_id)?;

node.unset_cluster();
self.nodes.update(node_key, &node)?;

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;
cluster.total_rent -= node.rent_v_node_per_month;
}

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

self.topology.remove_node(cluster_id, node_key)?;
Expand Down Expand Up @@ -180,21 +182,22 @@ impl DdcBucket {

let old_v_nodes = self.topology.get_v_nodes_by_node(node_key);

if new_v_nodes.len() > old_v_nodes.len() {

for _i in 0..new_v_nodes.len() - old_v_nodes.len() {
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)?;
if new_v_nodes.len() != old_v_nodes.len() {

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

for _i in 0..new_v_nodes.len() - old_v_nodes.len() {
node.reserve_resource(cluster.resource_per_v_node)?;
cluster.total_rent += node.rent_v_node_per_month;
}

for _i in 0..old_v_nodes.len() - new_v_nodes.len() {
node.release_resource(cluster.resource_per_v_node);
cluster.total_rent -= node.rent_per_month;
} else if new_v_nodes.len() < old_v_nodes.len() {

for _i in 0..old_v_nodes.len() - new_v_nodes.len() {
node.release_resource(cluster.resource_per_v_node);
cluster.total_rent -= node.rent_v_node_per_month;
}
}

self.nodes.update(node_key, &node)?;
Expand Down Expand Up @@ -402,30 +405,34 @@ impl DdcBucket {
}


pub fn message_cluster_reserve_resource(
pub fn message_cluster_set_resource_per_v_node(
&mut self,
cluster_id: ClusterId,
resource: Resource,
new_resource_per_v_node: Resource,
) -> Result<()> {
let caller = Self::env().caller();
let mut cluster = self.clusters.get(cluster_id)?;
cluster.only_manager(caller)?;
cluster.put_resource(resource);
self.clusters.update(cluster_id, &cluster)?;

let old_resource_per_v_node = cluster.resource_per_v_node;

cluster.set_resource_per_v_node(new_resource_per_v_node);
let cluster_v_nodes = self.topology.get_v_nodes_by_cluster(cluster_id);
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.reserve_resource(resource)?;
node.release_resource(old_resource_per_v_node);
node.reserve_resource(new_resource_per_v_node)?;
self.nodes.update(node_key, &node)?;
}

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

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

Ok(())
}

Expand Down
6 changes: 4 additions & 2 deletions bucket/ddc_bucket/cluster/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use ink_storage::traits::{SpreadAllocate, SpreadLayout};
use ink_storage::Mapping;
use crate::ddc_bucket::{AccountId, Error::*, Result};
use crate::ddc_bucket::{AccountId, Resource, Error::*, Result};
use super::entity::{Cluster, ClusterId, ClusterParams};


Expand All @@ -18,13 +18,15 @@ impl ClusterStore {
&mut self,
manager_id: AccountId,
cluster_params: ClusterParams,
resource_per_v_node: Resource,
) -> Result<ClusterId> {
let cluster_id = self.next_cluster_id;
self.next_cluster_id = self.next_cluster_id + 1;

let cluster = Cluster::new(
manager_id,
cluster_params
cluster_params,
resource_per_v_node
)?;

self.clusters.insert(&cluster_id, &cluster);
Expand Down
6 changes: 3 additions & 3 deletions bucket/ddc_bucket/node/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub type Resource = u32;
#[cfg_attr(feature = "std", derive(Debug, scale_info::TypeInfo))]
pub struct Node {
pub provider_id: ProviderId,
pub rent_per_month: Balance,
pub rent_v_node_per_month: Balance,
pub free_resource: Resource,
pub node_params: NodeParams,
pub cluster_id: Option<ClusterId>,
Expand Down Expand Up @@ -72,13 +72,13 @@ impl Node {
provider_id: AccountId,
node_params: NodeParams,
capacity: Resource,
rent_per_month: Balance,
rent_v_node_per_month: Balance,
) -> Result<Self> {
let mut node = Node {
provider_id,
node_params: NodeParams::default(),
free_resource: capacity,
rent_per_month,
rent_v_node_per_month,
cluster_id: None,
status_in_cluster: None,
};
Expand Down
6 changes: 3 additions & 3 deletions bucket/ddc_bucket/node/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl DdcBucket {
node_key: NodeKey,
node_params: NodeParams,
capacity: Resource,
rent_per_month: Balance,
rent_v_node_per_month: Balance,
) -> Result<NodeKey> {

let caller = Self::env().caller();
Expand All @@ -22,13 +22,13 @@ impl DdcBucket {
caller,
node_params.clone(),
capacity,
rent_per_month
rent_v_node_per_month
)?;

Self::env().emit_event(NodeCreated {
node_key,
provider_id: caller,
rent_per_month,
rent_v_node_per_month,
node_params,
});

Expand Down
4 changes: 2 additions & 2 deletions bucket/ddc_bucket/node/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl NodeStore {
provider_id: AccountId,
node_params: NodeParams,
capacity: Resource,
rent_per_month: Balance,
rent_v_node_per_month: Balance,
) -> Result<NodeKey> {

if self.nodes.contains(&node_key) {
Expand All @@ -40,7 +40,7 @@ impl NodeStore {
provider_id,
node_params,
capacity,
rent_per_month
rent_v_node_per_month
)?;

self.nodes.insert(node_key, &node);
Expand Down
Loading

0 comments on commit bd1ed1b

Please sign in to comment.