Skip to content
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

Reason simplification #858

Merged
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
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ ya-sb-router = { path = "service-bus/router" }
ya-sb-util = { path = "service-bus/util" }

## CLIENT
ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "ed4d8ac62a3335391fb64e47cc968d5797875c52"}
ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "ed4d8ac62a3335391fb64e47cc968d5797875c52"}
ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "8c99a296eced0ee46054490237fd36fd4268a0ad"}
ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "8c99a296eced0ee46054490237fd36fd4268a0ad"}

#ya-client = { path = "../ya-client" }
#ya-client-model = { path = "../ya-client/model" }
Comment on lines +177 to +178
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to remember to clean this


## OTHERS
gftp = { path = "core/gftp" }
Expand Down
14 changes: 7 additions & 7 deletions agent/provider/src/market/mock_negotiator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ya_agreement_utils::AgreementView;
use ya_agreement_utils::OfferDefinition;
use ya_client_model::market::{NewOffer, Proposal};
use ya_client_model::market::{NewOffer, Proposal, Reason};

use super::negotiator::Negotiator;
use crate::market::negotiator::{AgreementResponse, AgreementResult, ProposalResponse};
Expand Down Expand Up @@ -92,10 +92,10 @@ impl Negotiator for LimitAgreementsNegotiator {
demand.proposal_id
);
Ok(ProposalResponse::RejectProposal {
reason: Some(format!(
reason: Some(Reason::new(format!(
"Proposal expires at: {} which is less than 5 min or more than 30 min from now",
expiration
)),
))),
})
} else if self.has_free_slot() {
Ok(ProposalResponse::AcceptProposal)
Expand All @@ -105,10 +105,10 @@ impl Negotiator for LimitAgreementsNegotiator {
demand.proposal_id
);
Ok(ProposalResponse::RejectProposal {
reason: Some(format!(
reason: Some(Reason::new(format!(
"No capacity available. Reached Agreements limit: {}",
self.max_agreements
)),
))),
})
}
}
Expand All @@ -124,10 +124,10 @@ impl Negotiator for LimitAgreementsNegotiator {
agreement.agreement_id
);
Ok(AgreementResponse::RejectAgreement {
reason: Some(format!(
reason: Some(Reason::new(format!(
"No capacity available. Reached Agreements limit: {}",
self.max_agreements
)),
))),
})
}
}
Expand Down
9 changes: 5 additions & 4 deletions agent/provider/src/market/negotiator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::Result;
use derive_more::Display;

use crate::market::termination_reason::BreakReason;
use ya_client::model::market::Reason;

/// Response for requestor proposals.
#[derive(Debug, Display)]
Expand All @@ -21,7 +22,7 @@ pub enum ProposalResponse {
"reason.as_ref().map(|r| format!(\" (reason: {})\", r)).unwrap_or(\"\".into())"
)]
RejectProposal {
reason: Option<String>,
reason: Option<Reason>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

},
///< Don't send any message to requestor. Could be useful to wait for other offers.
IgnoreProposal,
Expand All @@ -37,7 +38,7 @@ pub enum AgreementResponse {
"reason.as_ref().map(|r| format!(\" (reason: {})\", r)).unwrap_or(\"\".into())"
)]
RejectAgreement {
reason: Option<String>,
reason: Option<Reason>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

},
}

Expand Down Expand Up @@ -81,7 +82,7 @@ mod tests {
};
let no_reason = ProposalResponse::RejectProposal { reason: None };

assert_eq!(reason.to_string(), "RejectProposal (reason: zima)");
assert_eq!(reason.to_string(), "RejectProposal (reason: 'zima')");
assert_eq!(no_reason.to_string(), "RejectProposal");
}

Expand All @@ -92,7 +93,7 @@ mod tests {
};
let no_reason = AgreementResponse::RejectAgreement { reason: None };

assert_eq!(reason.to_string(), "RejectAgreement (reason: lato)");
assert_eq!(reason.to_string(), "RejectAgreement (reason: 'lato')");
assert_eq!(no_reason.to_string(), "RejectAgreement");
}
}
21 changes: 7 additions & 14 deletions agent/provider/src/market/provider_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ use backoff::backoff::Backoff;
use chrono::Utc;
use derive_more::Display;
use futures::prelude::*;
use futures_util::FutureExt;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::Arc;

use ya_agreement_utils::{AgreementView, OfferDefinition};
use ya_client::market::MarketProviderApi;
use ya_client_model::market::{
Agreement, AgreementOperationEvent as AgreementEvent, NewOffer, Proposal, ProviderEvent, Reason,
agreement_event::AgreementTerminator, Agreement, AgreementOperationEvent as AgreementEvent,
NewOffer, Proposal, ProviderEvent, Reason,
};
use ya_utils_actix::{
actix_handler::ResultTypeGetter,
Expand All @@ -27,9 +29,6 @@ use crate::market::config::MarketConfig;
use crate::market::mock_negotiator::LimitAgreementsNegotiator;
use crate::market::termination_reason::GolemReason;
use crate::tasks::{AgreementBroken, AgreementClosed, CloseAgreement};
use futures_util::FutureExt;
use ya_client::model::market::ConvertReason;
use ya_client_model::market::agreement_event::AgreementTerminator;

// =========================================== //
// Public exposed messages
Expand Down Expand Up @@ -352,11 +351,7 @@ async fn process_proposal(
ProposalResponse::IgnoreProposal => log::info!("Ignoring proposal {:?}", proposal_id),
ProposalResponse::RejectProposal { reason } => {
ctx.api
.reject_proposal_with_reason(
&subscription.id,
proposal_id,
reason.map(|r| Reason::new(r)),
)
.reject_proposal_with_reason(&subscription.id, proposal_id, &reason)
.await?;
}
},
Expand Down Expand Up @@ -427,7 +422,7 @@ async fn process_agreement(
}
AgreementResponse::RejectAgreement { reason } => {
ctx.api
.reject_agreement(&agreement.agreement_id, reason.map(|r| Reason::new(r)))
.reject_agreement(&agreement.agreement_id, &reason)
.await?;
}
},
Expand Down Expand Up @@ -481,9 +476,7 @@ async fn collect_agreement_events(ctx: AsyncCtx) {
// Notify market about termination.
let msg = OnAgreementTerminated {
id: agreement_id,
reason: reason
.map(|reason| Reason::from_json_reason(reason).ok())
.flatten(),
reason,
};
ctx.market.send(msg).await.ok();
}
Expand Down Expand Up @@ -697,7 +690,7 @@ async fn terminate_agreement(api: Arc<MarketProviderApi>, msg: AgreementFinalize
);

let mut repeats = get_backoff();
while let Err(e) = api.terminate_agreement(&id, Some(reason.clone())).await {
while let Err(e) = api.terminate_agreement(&id, &reason.to_client()).await {
let delay = match repeats.next_backoff() {
Some(delay) => delay,
None => {
Expand Down
26 changes: 25 additions & 1 deletion agent/provider/src/market/termination_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::time::Duration;
use strum::EnumMessage;
use strum_macros::*;

use ya_client::model::market::Reason;

#[derive(Display, EnumMessage, Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum BreakReason {
Expand All @@ -21,7 +23,7 @@ pub enum BreakReason {
NoActivity(Duration),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct GolemReason {
#[serde(rename = "message")]
pub message: String,
Expand All @@ -47,4 +49,26 @@ impl GolemReason {
extra: HashMap::new(),
}
}

pub fn to_client(&self) -> Option<Reason> {
match Reason::from_value(self) {
Ok(r) => Some(r),
Err(e) => {
log::warn!("{}", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message could be bettter

None
}
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_try_convert_self() {
let g = GolemReason::success();
let g1: GolemReason = g.to_client().unwrap().to_value().unwrap();
assert_eq!(g, g1)
}
}
4 changes: 2 additions & 2 deletions core/market/src/db/model/agreement_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::db::model::{AgreementId, OwnerType};
use crate::db::schema::market_agreement_event;

use ya_client::model::market::agreement_event::AgreementTerminator;
use ya_client::model::market::{AgreementOperationEvent as ClientEvent, JsonReason};
use ya_client::model::market::{AgreementOperationEvent as ClientEvent, Reason};
use ya_diesel_utils::DbTextField;

#[derive(
Expand Down Expand Up @@ -54,7 +54,7 @@ impl AgreementEvent {
let event_date = DateTime::<Utc>::from_utc(self.timestamp, Utc);
let reason = self
.reason
.map(|reason| serde_json::from_str::<JsonReason>(&reason))
.map(|reason| serde_json::from_str::<Reason>(&reason))
.map(|result| result.map_err(|e| {
log::warn!(
"Agreement Event with not parsable Reason in database. Error: {}. Shouldn't happen \
Expand Down
15 changes: 3 additions & 12 deletions core/market/tests/test_agreement_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ya_market::testing::MarketsNetwork;
use ya_market::testing::{ApprovalStatus, OwnerType};

use ya_client::model::market::agreement_event::AgreementTerminator;
use ya_client::model::market::{AgreementOperationEvent as AgreementEvent, ConvertReason, Reason};
use ya_client::model::market::AgreementOperationEvent as AgreementEvent;

const REQ_NAME: &str = "Node-1";
const PROV_NAME: &str = "Node-2";
Expand Down Expand Up @@ -233,12 +233,7 @@ async fn test_agreement_terminated_event() -> Result<()> {
assert_eq!(agreement_id, &negotiation.p_agreement.into_client());
assert_eq!(terminator, &AgreementTerminator::Provider);
assert_ne!(reason, &None);

let reason = reason
.as_ref()
.map(|json| Reason::from_json_reason(json.clone()).unwrap())
.unwrap();
assert_eq!(&reason.message, "Expired");
assert_eq!(reason.as_ref().unwrap().message, "Expired");
}
_ => panic!("Expected AgreementEvent::AgreementTerminatedEvent"),
};
Expand All @@ -261,11 +256,7 @@ async fn test_agreement_terminated_event() -> Result<()> {
assert_eq!(terminator, &AgreementTerminator::Provider);
assert!(reason.is_some());

let reason = reason
.as_ref()
.map(|json| Reason::from_json_reason(json.clone()).unwrap())
.unwrap();
assert_eq!(&reason.message, "Expired");
assert_eq!(reason.as_ref().unwrap().message, "Expired");
}
_ => panic!("Expected AgreementEvent::AgreementTerminatedEvent"),
};
Expand Down
9 changes: 5 additions & 4 deletions core/market/tests/test_rest_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use serde::de::DeserializeOwned;
use serde_json::json;

use ya_client::model::market::{
agreement as client_agreement, Agreement, AgreementOperationEvent, Demand, JsonReason,
NewDemand, NewOffer, Offer, Proposal,
agreement as client_agreement, Agreement, AgreementOperationEvent, Demand, NewDemand, NewOffer,
Offer, Proposal, Reason,
};
use ya_client::model::ErrorMessage;
use ya_client::web::QueryParamsBuilder;
Expand Down Expand Up @@ -456,8 +456,9 @@ async fn test_terminate_agreement() -> anyhow::Result<()> {
let req_id = network.get_default_id(REQ_NAME);
let prov_id = network.get_default_id(PROV_NAME);

let reason = JsonReason {
json: serde_json::json!({"ala":"ma kota","message": "coś"}),
let reason = Reason {
message: "coś".into(),
extra: serde_json::json!({"ala":"ma kota"}),
};
let url = format!(
"/market-api/v1/agreements/{}/terminate",
Expand Down