Skip to content

Commit

Permalink
feat: Add return type to Initiator.certify() (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmarek-kindred authored Oct 12, 2023
1 parent c809e90 commit c76a702
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion cohort_banking_initiator_js/src/banking-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class BankingApp {
let stateDuration = 0
let ooinstallEnd = 0
let ooinstallDuration = 0
await this.initiator.certify(
let result = await this.initiator.certify(
async () => {
const s = Date.now()
const newRequest = await this.createNewRequest(tx) as any
Expand All @@ -114,6 +114,8 @@ export class BankingApp {
}
)

// logger.info("tx: %s - [%s]; Completed in [attempts=%s, ms=%s]", result.xid, result.decision, result.metadata.attempts, result.metadata.durationMs)

return { stateDuration, stateEnd, ooinstallDuration, ooinstallEnd }
}

Expand Down
6 changes: 6 additions & 0 deletions cohort_sdk_client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { Initiator } from "./initiator"
import { Replicator } from "./replicator"
import {
JsCertificationRequestPayload,
JsCertificationResponse,
JsDecision,
JsInitiatorConfig,
JsKafkaConfig,
JsReplicatorConfig,
JsResponseMetadata,
JsStatemapAndSnapshot,
JsOutOfOrderInstallOutcome,
OutOfOrderRequest,
Expand All @@ -20,10 +23,13 @@ class TalosSdkError extends Error {

export {
Initiator,
JsDecision,
JsInitiatorConfig,
JsCertificationResponse,
JsCertificationRequestPayload,
JsKafkaConfig,
JsReplicatorConfig,
JsResponseMetadata,
JsStatemapAndSnapshot,
JsOutOfOrderInstallOutcome,
OutOfOrderRequest,
Expand Down
4 changes: 2 additions & 2 deletions cohort_sdk_client/src/initiator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InternalInitiator, JsInitiatorConfig, OutOfOrderRequest, SdkErrorKind } from "@kindredgroup/cohort_sdk_js"
import { InternalInitiator, JsInitiatorConfig, OutOfOrderRequest, SdkErrorKind, JsCertificationResponse } from "@kindredgroup/cohort_sdk_js"
import { isSdkError } from "./internal"
import { TalosSdkError } from "."

Expand All @@ -19,7 +19,7 @@ export class Initiator {

constructor(readonly impl: InternalInitiator) {}

async certify(makeNewRequestCallback: () => Promise<any>, oooCallback: (err: Error | null, value: OutOfOrderRequest) => any): Promise<void> {
async certify(makeNewRequestCallback: () => Promise<any>, oooCallback: (err: Error | null, value: OutOfOrderRequest) => any): Promise<JsCertificationResponse> {
try {
return await this.impl.certify(makeNewRequestCallback, oooCallback)
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions packages/cohort_sdk_js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ log = { workspace = true }
tokio = { workspace = true, features = ["full"] }

cohort_sdk = { path = "../cohort_sdk" }
talos_agent = { path = "../talos_agent" }
talos_cohort_replicator = { path = "../talos_cohort_replicator" }
talos_certifier = { path = "../talos_certifier" }
talos_certifier_adapters = { path = "../talos_certifier_adapters" }
Expand Down
10 changes: 7 additions & 3 deletions packages/cohort_sdk_js/scripts/postinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ echo "D: lenMinusOne=$lenMinusOne, lenMinusTwo=$lenMinusTwo, lenMinusThree=$lenM
lastPathToken="${pathTokens[lenMinusFour]}/${pathTokens[lenMinusThree]}/${pathTokens[lenMinusTwo]}"/"${pathTokens[lenMinusOne]}"
echo "D: lastPathToken=$lastPathToken"

if [ "$lastPathToken" == "cohort_sdk_client/node_modules/@kindredgroup/cohort_sdk_js" ]; then
echo "Post install script of 'cohort_sdk_js' is designed to be executed when module is installed as transitional dependency. Currently, 'npm install' is running as part of installing 'cohort_sdk_client'. Postinstall script is skipped."
exit 0
if [ "$COHORT_SDK_CLIENT_DEV_MODE" == "true" ]; then
echo "Post install script of 'cohort_sdk_js' is running as transitional dependency. Enforced by COHORT_SDK_CLIENT_DEV_MODE=${COHORT_SDK_CLIENT_DEV_MODE}"
else
if [ "$lastPathToken" == "cohort_sdk_client/node_modules/@kindredgroup/cohort_sdk_js" ]; then
echo "Post install script of 'cohort_sdk_js' is designed to be executed when module is installed as transitional dependency. Currently, 'npm install' is running as part of installing 'cohort_sdk_client'. Postinstall script is skipped."
exit 0
fi
fi

echo "Post install. Current directory is: $CURRENT_DIR"
Expand Down
50 changes: 45 additions & 5 deletions packages/cohort_sdk_js/src/initiator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::models::{JsBackoffConfig, JsKafkaConfig};
use crate::models::{JsBackoffConfig, JsDecision, JsKafkaConfig};
use crate::sdk_errors::SdkErrorContainer;
use async_trait::async_trait;
use cohort_sdk::cohort::Cohort;
use cohort_sdk::model::callback::{
CertificationCandidate, CertificationCandidateCallbackResponse, CertificationRequestPayload, OutOfOrderInstallOutcome, OutOfOrderInstallRequest,
OutOfOrderInstaller,
};
use cohort_sdk::model::{ClientError, Config};
use cohort_sdk::model::{CertificationResponse, ClientError, Config, ResponseMetadata};
use napi::bindgen_prelude::FromNapiValue;
use napi::bindgen_prelude::Promise;
use napi::bindgen_prelude::ToNapiValue;
Expand Down Expand Up @@ -126,6 +126,46 @@ impl From<JsOutOfOrderInstallOutcome> for OutOfOrderInstallOutcome {
}
}

#[napi(object)]
pub struct JsCertificationResponse {
pub xid: String,
pub decision: JsDecision,
pub version: i64,
pub safepoint: Option<i64>,
pub conflict: Option<i64>,
pub metadata: JsResponseMetadata,
pub statemaps: Option<Vec<HashMap<String, Value>>>,
}

impl From<CertificationResponse> for JsCertificationResponse {
fn from(value: CertificationResponse) -> Self {
Self {
xid: value.xid,
decision: value.decision.into(),
version: value.version as i64,
safepoint: value.safepoint.map(|v| v as i64),
conflict: value.conflict.map(|v| v as i64),
metadata: value.metadata.into(),
statemaps: value.statemaps,
}
}
}

impl From<ResponseMetadata> for JsResponseMetadata {
fn from(value: ResponseMetadata) -> Self {
Self {
attempts: value.attempts,
duration_ms: value.duration_ms as i64,
}
}
}

#[napi(object)]
pub struct JsResponseMetadata {
pub attempts: u32,
pub duration_ms: i64,
}

#[napi]
pub struct InternalInitiator {
cohort: Cohort,
Expand All @@ -144,12 +184,12 @@ impl InternalInitiator {
&self,
#[napi(ts_arg_type = "() => Promise<any>")] make_new_request_callback: ThreadsafeFunction<()>,
ooo_callback: ThreadsafeFunction<OutOfOrderRequest>,
) -> napi::Result<()> {
) -> napi::Result<JsCertificationResponse> {
let new_request_provider = NewRequestProvider { make_new_request_callback };
let ooo_impl = OutOfOrderInstallerImpl { ooo_callback };
let make_new_request = || new_request_provider.make_new_request();
let _res = self.cohort.certify(&make_new_request, &ooo_impl).await.map_err(map_error)?;
Ok(())
let response = self.cohort.certify(&make_new_request, &ooo_impl).await.map_err(map_error)?;
Ok(response.into())
}
}

Expand Down
23 changes: 20 additions & 3 deletions packages/cohort_sdk_js/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use cohort_sdk::model::BackoffConfig;
use std::collections::HashMap;
use talos_agent::messaging::api::Decision;
use talos_rdkafka_utils::kafka_config::KafkaConfig;

// $coverage:ignore-start
use cohort_sdk::model::BackoffConfig;
use napi::bindgen_prelude::FromNapiValue;
use napi::bindgen_prelude::ToNapiValue;
use napi_derive::napi;
use talos_rdkafka_utils::kafka_config::KafkaConfig;

#[napi(object)]
pub struct JsBackoffConfig {
Expand Down Expand Up @@ -57,3 +59,18 @@ impl From<JsKafkaConfig> for KafkaConfig {
}
}
}

#[napi(string_enum)]
pub enum JsDecision {
Committed,
Aborted,
}

impl From<Decision> for JsDecision {
fn from(value: Decision) -> Self {
match value {
Decision::Committed => JsDecision::Committed,
Decision::Aborted => JsDecision::Aborted,
}
}
}

0 comments on commit c76a702

Please sign in to comment.