Skip to content

WiP restructure of Space_Managers and responsibility of cache,stage and commit #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions crates/coordinator/core_schema/src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use hdi::prelude::debug;

use hdk::prelude::info;
use holons::commit_manager::{CommitManager, CommitResponse};
use holons::commit_service::CommitResponse;
use holons::context::HolonsContext;
use holons::holon_error::HolonError;
use holons::space_manager::HolonStagingBehavior;
use strum::IntoEnumIterator;
// use holons::holon::Holon;
use holons::holon_reference::HolonReference;
Expand Down Expand Up @@ -53,8 +54,8 @@ pub fn load_core_schema(context: &HolonsContext) -> Result<CommitResponse, Holon
info!("vvvvvvvv Entered: load_core_schema vvvvvvvvv");
// Begin by staging `schema`. It's HolonReference becomes the target of
// the COMPONENT_OF relationship for all schema components
let space_reference = context
.get_local_holon_space()
let _ = context
.get_local_space_holon()
.ok_or(HolonError::HolonNotFound("Local holon space not found".to_string()));

let schema = Schema::new(
Expand All @@ -66,13 +67,11 @@ pub fn load_core_schema(context: &HolonsContext) -> Result<CommitResponse, Holon

info!("Staging Schema...");
let staged_schema_ref = HolonReference::Staged(
context.commit_manager.borrow_mut().stage_new_holon(schema.0.clone())?,
context.space_manager.borrow().stage_new_holon(schema.0.clone())?,
);

context.add_reference_to_dance_state(staged_schema_ref.clone())?;

//context.local_holon_space.clone_from(source).borrow_mut().get_holon(commit_holon(staged_schema_ref.clone())?;

let initial_load_set = get_initial_load_set();

for type_name in initial_load_set {
Expand All @@ -95,7 +94,7 @@ pub fn load_core_schema(context: &HolonsContext) -> Result<CommitResponse, Holon

info!("^^^^^^^ STAGING COMPLETE: Committing schema...");

let response = CommitManager::commit(context);
let response = context.space_manager.borrow().commit(context)?;

let r = response.clone();

Expand Down
27 changes: 19 additions & 8 deletions crates/coordinator/dances/src/dance_request.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use hdk::prelude::*;
use holons::cache_manager::HolonCacheManager;
use holons::commit_manager::StagedIndex;
use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_reference::HolonReference;
use holons::query::{NodeCollection, QueryExpression};
use holons::relationship::RelationshipName;
use holons::space_manager::HolonSpaceManager;
use holons::staged_reference::StagedIndex;

use crate::session_state::SessionState;
use shared_types_holon::{HolonId, LocalId, MapString, PropertyMap};
Expand Down Expand Up @@ -101,13 +101,24 @@ impl DanceRequest {
pub fn get_state_mut(&mut self) -> &mut SessionState {
&mut self.state
}
pub fn init_context_from_state(&self) -> HolonsContext {
let commit_manager = self.get_state().get_staging_area().to_commit_manager();
// assert_eq!(request.staging_area.staged_holons.len(),commit_manager.staged_holons.len());

let local_holon_space = self.get_state().get_local_holon_space();
debug!("initializing context from session state in dance request");
HolonsContext::init_context(commit_manager, HolonCacheManager::new(), local_holon_space)
// Method to initialize the HolonsContext + HolonSpaceManger from the session state
// This method creates a nursery and a local space holon from the session state if available
// If the session is available it will creata a space manager from session otherwise a new one
//lastly, it will initialize the HolonsContext with the space manager and return it
pub fn init_context_from_state(&self) -> HolonsContext {
let staged_holons = self.get_state().get_staging_area().get_staged_rc_holons();//from_stage_to_nursery();
let stage_index =self.get_state().get_staging_area().get_staged_index();
let local_space_holon = self.get_state().get_local_holon_space();
let space_manager = HolonSpaceManager::new_from_session(staged_holons, stage_index, local_space_holon);
//let space_manager = match local_space_holon {
// None => HolonSpaceManager::new(),
// Some(local_space_holon) => {
// debug!("Space manager created from session state in dance request");
// HolonSpaceManager::new_from_session(nursery, local_space_holon)
// }
//};
HolonsContext::init_context(space_manager)
}
// Method to summarize the DanceResponse for logging purposes
pub fn summarize(&self) -> String {
Expand Down
24 changes: 12 additions & 12 deletions crates/coordinator/dances/src/dance_response.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use derive_new::new;
use holons::staged_reference::StagedIndex;
use std::fmt;

use crate::session_state::SessionState;
use crate::staging_area::StagingArea;
use hdk::prelude::*;
use holons::commit_manager::StagedIndex;
use holons::context::HolonsContext;
use holons::helpers::summarize_holons;
use holons::holon::Holon;
use holons::holon_error::HolonError;
Expand Down Expand Up @@ -112,15 +110,17 @@ impl DanceResponse {
) -> DanceResponse {
DanceResponse { status_code, description, body, descriptor, state }
}
/// Restores the session state within the DanceResponse from context. This should always
/// be called before returning DanceResponse since the state is intended to be "ping-ponged"
/// between client and guest.
/// NOTE: Errors in restoring the state are not handled (i.e., will cause panic)
pub fn restore_state(&mut self, context: &HolonsContext) {
self.state
.set_staging_area(StagingArea::from_commit_manager(&context.commit_manager.borrow()));
self.state.set_local_holon_space(context.get_local_holon_space());
}

//moved to the dancer
/*pub fn restore_state(&mut self, context: &HolonsContext) {
let space_manager = &context.space_manager.borrow();
let staged_holons = space_manager.get_holon_stage();
let staged_index = space_manager.get_stage_key_index();
let staging_area = StagingArea::new_from_references(staged_holons, staged_index);
let local_space_holon = space_manager.get_space_holon();
self.state.set_staging_area(staging_area);
self.state.set_local_holon_space(local_space_holon);
}*/
// Method to summarize the DanceResponse for logging purposes
pub fn summarize(&self) -> String {
let body_summary = match &self.body {
Expand Down
45 changes: 35 additions & 10 deletions crates/coordinator/dances/src/dancer.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::collections::HashMap;

use hdk::prelude::*;

use holons::space_manager::HolonStagingBehavior;
//use hdi::map_extern::ExternResult;
use crate::dance_request::DanceRequest;
use crate::dance_response::{DanceResponse, ResponseBody, ResponseStatusCode};
use crate::descriptors_dance_adapter::*;
use crate::holon_dance_adapter::*;
use crate::session_state::SessionState;
use crate::staging_area::StagingArea;
use holons::context::HolonsContext;
use holons::holon_error::HolonError;
use holons::holon_space_manager::HolonSpaceManager;
use shared_types_holon::MapString;

use crate::holon_dance_adapter::{
Expand Down Expand Up @@ -47,24 +47,34 @@ pub fn dance(request: DanceRequest) -> ExternResult<DanceResponse> {
}

let context = request.init_context_from_state();
let holon_space_manager = HolonSpaceManager::new(&context);
debug!("context initialized");
let mut mutable_space_manager = context.space_manager.borrow_mut();


// ------------------ ENSURE LOCAL HOLON SPACE IS IN CONTEXT ---------------------------------
let space_reference = holon_space_manager.ensure_local_holon_space_in_context();
// ------------------ ENSURE LOCAL SPACE HOLON IS COMMITTED ---------------------------------

//note at this point the space_manager cannot be borrowed until mutable release
let space_reference = mutable_space_manager.ensure_local_holon_space(&context);
if let Err(space_error) = space_reference {
let error_message = extract_error_message(&space_error);

//release the mutable borrow of the space manager
drop(mutable_space_manager);

// Construct DanceResponse with error details
let response = DanceResponse {
status_code: ResponseStatusCode::from(space_error), // Convert HolonError to ResponseStatusCode
description: MapString(error_message),
body: ResponseBody::None, // No body since it's an error
descriptor: None, // Provide appropriate value if needed
state: SessionState::restore_session_state_from_context(&context),
state: restore_session_state_from_space_manager(&context),
};
return Ok(response);
}

//release the mutable borrow of the space manager
drop(mutable_space_manager);
debug!("space manager ready to dance");

// Get the Dancer
let dancer = Dancer::new();

Expand All @@ -83,7 +93,7 @@ pub fn dance(request: DanceRequest) -> ExternResult<DanceResponse> {

let result = process_dispatch_result(&context, dispatch_result);

// assert_eq!(result.staging_area.staged_holons.len(), context.commit_manager.borrow().staged_holons.len());
// assert_eq!(result.staging_area.staged_holons.len(), context.space_manager.borrow().staged_holons.len());

info!("\n======== RETURNING FROM {:?} Dance with {}", request.dance_name.0, result.summarize());

Expand Down Expand Up @@ -159,6 +169,21 @@ impl Dancer {
}
}


/// Restores the session state for the DanceResponse from context. This should always
/// be called before returning DanceResponse since the state is intended to be "ping-ponged"
/// between client and guest.
/// NOTE: Errors in restoring the state are not handled (i.e., will cause panic)
pub fn restore_session_state_from_space_manager(context: &HolonsContext)-> SessionState {
let space_manager = &context.space_manager.borrow();
let staged_holons = space_manager.get_holon_stage();
let staged_index = space_manager.get_stage_key_index();
let staging_area = StagingArea::new_from_references(staged_holons, staged_index);
let local_space_holon = space_manager.get_space_holon();
SessionState::new(staging_area, local_space_holon)
}


/// This function creates a DanceResponse from a `dispatch_result`.
///
/// If `dispatch_result` is `Ok`,
Expand Down Expand Up @@ -187,7 +212,7 @@ fn process_dispatch_result(
description: MapString("Success".to_string()),
body,
descriptor: None,
state: SessionState::restore_session_state_from_context(context),
state: restore_session_state_from_space_manager(context),
}
}
Err(error) => {
Expand All @@ -198,7 +223,7 @@ fn process_dispatch_result(
description: MapString(error_message),
body: ResponseBody::None, // No body since it's an error
descriptor: None, // Provide appropriate value if needed
state: SessionState::restore_session_state_from_context(context),
state: restore_session_state_from_space_manager(context),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/coordinator/dances/src/descriptors_dance_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::dance_response::ResponseBody;
use crate::session_state::SessionState;
use core_schema::loader::load_core_schema;
use hdk::prelude::*;
use holons::commit_manager::CommitRequestStatus::*;
use holons::commit_service::CommitRequestStatus;
use holons::context::HolonsContext;
use holons::holon_error::HolonError;
use shared_types_holon::MapString;
Expand All @@ -45,8 +45,8 @@ pub fn load_core_schema_dance(
let result = load_core_schema(context);
match result {
Ok(commit_response) => match commit_response.status {
Complete => Ok(ResponseBody::None),
Incomplete => Err(HolonError::CommitFailure("Incomplete commit".to_string())),
CommitRequestStatus::Complete => Ok(ResponseBody::None),
CommitRequestStatus::Incomplete => Err(HolonError::CommitFailure("Incomplete commit".to_string())),
},
Err(e) => Err(e),
}
Expand Down
46 changes: 29 additions & 17 deletions crates/coordinator/dances/src/holon_dance_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
//! mapping any errors into an appropriate ResponseStatus and returning results in the body.

use hdk::prelude::*;
use holons::commit_manager::CommitRequestStatus::*;
use holons::commit_manager::{CommitManager, StagedIndex};
use holons::commit_service::CommitRequestStatus::*;
use holons::context::HolonsContext;
use holons::holon::Holon;
use holons::holon_error::HolonError;
use holons::holon_reference::HolonReference;
use holons::query::*;
use holons::relationship::RelationshipName;
use holons::smart_reference::SmartReference;
use holons::space_manager::{HolonCacheBehavior, HolonStagingBehavior};
use holons::staged_reference::StagedIndex;
use shared_types_holon::{HolonId, LocalId};
use shared_types_holon::{MapString, PropertyMap};

Expand Down Expand Up @@ -51,10 +52,10 @@ pub fn add_related_holons_dance(
DanceType::CommandMethod(staged_index) => {
// Borrow a read-only reference to the CommitManager
let staged_reference_result = {
let commit_manager = context.commit_manager.borrow();
let space_manager = context.space_manager.borrow();
debug!("Matched CommandMethod as dance_type.");
// Convert the staged_index into a StagedReference
commit_manager.to_staged_reference(staged_index)
space_manager.to_staged_reference(staged_index)
};

// Handle the result of to_staged_reference
Expand Down Expand Up @@ -123,7 +124,7 @@ pub fn commit_dance(
_request: DanceRequest,
) -> Result<ResponseBody, HolonError> {
info!("----- Entered commit_dance");
let commit_response = CommitManager::commit(context);
let commit_response = context.space_manager.borrow().commit(context)?;

match commit_response.status {
Complete => Ok(ResponseBody::Holons(commit_response.saved_holons)),
Expand Down Expand Up @@ -224,6 +225,7 @@ pub fn get_all_holons_dance(
Ok(holons) => Ok(ResponseBody::Holons(holons)),
Err(holon_error) => Err(holon_error.into()),
}

}

/// Builds a DanceRequest for retrieving all holons from the persistent store
Expand Down Expand Up @@ -263,11 +265,11 @@ pub fn get_holon_by_id_dance(
))
}
};
debug!("getting cache_manager from context");
let cache_manager = context.cache_manager.borrow();
debug!("getting space_manager from context");
let space_manager = context.space_manager.borrow();

debug!("asking cache_manager to get rc_holon");
let rc_holon = cache_manager.get_rc_holon(&holon_id)?;
debug!("asking space_manager to get rc_holon");
let rc_holon = space_manager.get_rc_holon(&holon_id)?;

let holon = rc_holon.borrow().clone();
Ok(ResponseBody::Holon(holon))
Expand Down Expand Up @@ -362,10 +364,10 @@ pub fn remove_related_holons_dance(
DanceType::CommandMethod(staged_index) => {
// Borrow a read-only reference to the CommitManager
let staged_reference_result = {
let commit_manager = context.commit_manager.borrow();
let space_manager = context.space_manager.borrow();
debug!("Matched CommandMethod as dance_type.");
// Convert the staged_index into a StagedReference
commit_manager.to_staged_reference(staged_index)
space_manager.to_staged_reference(staged_index)
};

// Handle the result of to_staged_reference
Expand Down Expand Up @@ -500,7 +502,7 @@ pub fn stage_new_holon_dance(
debug!("Response body matched successfully for holon:{:#?}", new_holon);

// Stage the new holon
let staged_reference = context.commit_manager.borrow_mut().stage_new_holon(new_holon)?;
let staged_reference = context.space_manager.borrow().stage_new_holon(new_holon)?;
// This operation will have added the staged_holon to the CommitManager's vector and returned a
// StagedReference to it.

Expand Down Expand Up @@ -600,8 +602,8 @@ pub fn with_properties_dance(
DanceType::CommandMethod(staged_index) => {
debug!("looking for StagedHolon at index: {:#?}", staged_index);
// Try to get a mutable reference to the staged holon referenced by its index
let commit_manager = match context.commit_manager.try_borrow() {
Ok(commit_manager) => commit_manager,
let space_manager = match context.space_manager.try_borrow() {
Ok(space_manager) => space_manager,
Err(borrow_error) => {
error!(
"Failed to borrow commit_manager, it is already borrowed mutably: {:?}",
Expand All @@ -610,7 +612,12 @@ pub fn with_properties_dance(
return Err(HolonError::FailedToBorrow(format!("{:?}", borrow_error)));
}
};
let staged_holon = commit_manager.get_mut_holon_by_index(staged_index.clone());
//let staged_holon = space_manager.get_mut_holon_by_index(staged_index.clone());
let holon = space_manager.get_holon_by_index(staged_index.clone())?;
let staged_holon = holon.try_borrow_mut()
.map_err(|e| {
HolonError::FailedToBorrow(format!("Unable to borrow holon immutably: {}", e))
});

match staged_holon {
Ok(mut holon_mut) => {
Expand Down Expand Up @@ -682,9 +689,14 @@ pub fn abandon_staged_changes_dance(
DanceType::CommandMethod(staged_index) => {
debug!("trying to borrow_mut commit_manager");
// Try to get a mutable reference to the staged holon referenced by its index
let commit_manager_mut = context.commit_manager.borrow_mut();
let space_manager = context.space_manager.borrow();
debug!("commit_manager borrowed_mut");
let staged_holon = commit_manager_mut.get_mut_holon_by_index(staged_index.clone());
// let staged_holon = space_manager_mut.get_mut_holon_by_index(staged_index.clone());
let holon = space_manager.get_holon_by_index(staged_index.clone())?;
let staged_holon = holon.try_borrow_mut()
.map_err(|e| {
HolonError::FailedToBorrow(format!("Unable to borrow holon immutably: {}", e))
});
//debug!("Result of borrow_mut on the staged holon {:#?}", staged_holon.clone());

match staged_holon {
Expand Down
Loading