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

chore: Add SDK readme. #87

Merged
merged 10 commits into from
Oct 17, 2023
2 changes: 1 addition & 1 deletion cohort_banking_initiator_js/src/banking-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class BankingApp {
stateDuration = n - s
return { newRequest }
},
async (_e, request: OutOfOrderRequest) => {
async (request: OutOfOrderRequest) => {
const s = Date.now()
const r = await this.installOutOfOrder(tx, request) as any
const n = Date.now()
Expand Down
2 changes: 1 addition & 1 deletion cohort_banking_replicator_js/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ new Promise(async (_resolve) => {
try {
await replicator.run(
async () => await database.getSnapshot(),
async (_, params) => await database.install(params, { delayMs: 2, maxAttempts: 50 })
async (data) => await database.install(data, { delayMs: 2, maxAttempts: 50 })
)
logger.info("Replciator is running ...")
} catch (e) {
Expand Down
305 changes: 304 additions & 1 deletion cohort_sdk_client/README.md

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions cohort_sdk_client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,5 @@
"@types/pg": "^8.10.2",
"ts-node": "^10.9.1",
"winston": "^3.10.0"
},
"optionalDependencies": {
"cohort_sdk_js-darwin-arm64": "0.0.1",
"cohort_sdk_js-darwin-universal": "0.0.1",
"cohort_sdk_js-darwin-x64": "0.0.1",
"cohort_sdk_js-linux-x64-gnu": "0.0.1",
"cohort_sdk_js-win32-x64-msvc": "0.0.1"
}
}
19 changes: 16 additions & 3 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, JsCertificationResponse } from "@kindredgroup/cohort_sdk_js"
import { InternalInitiator, JsInitiatorConfig, OutOfOrderRequest, SdkErrorKind, JsCertificationResponse, JsCertificationCandidateCallbackResponse } from "@kindredgroup/cohort_sdk_js"
import { isSdkError } from "./internal"
import { TalosSdkError } from "."

Expand All @@ -19,9 +19,22 @@ export class Initiator {

constructor(readonly impl: InternalInitiator) {}

async certify(makeNewRequestCallback: () => Promise<any>, oooCallback: (err: Error | null, value: OutOfOrderRequest) => any): Promise<JsCertificationResponse> {
async certify(makeNewRequestCallback: () => Promise<JsCertificationCandidateCallbackResponse>, oooInstallCallback: (oooRequest: OutOfOrderRequest) => Promise<JsCertificationCandidateCallbackResponse>): Promise<JsCertificationResponse> {
try {
return await this.impl.certify(makeNewRequestCallback, oooCallback)
// This will hide the 'error' parameter from callback (it comes from NAPI).
const adaptedOooInstallCallback = async (error: Error | null, oooRequest: OutOfOrderRequest): Promise<JsCertificationCandidateCallbackResponse> => {
if (error) {
throw new TalosSdkError(
SdkErrorKind.Internal,
"Call from native code into 'oooInstallCallback' function provided by JS has failed at the NAPI layer. See the 'cause' field for more details.",
{ cause: error }
)
} else {
return await oooInstallCallback(oooRequest)
}
}

return await this.impl.certify(makeNewRequestCallback, adaptedOooInstallCallback)
} catch (e) {
const reason: string = e.message
if (isSdkError(reason)) {
Expand Down
17 changes: 15 additions & 2 deletions cohort_sdk_client/src/replicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@ export class Replicator {

constructor(readonly impl: InternalReplicator) {}

async run(snapshotProviderCallback: () => Promise<number>, statemapInstallerCallback: (err: Error | null, value: JsStatemapAndSnapshot) => any) {
async run(snapshotProviderCallback: () => Promise<number>, statemapInstallerCallback: (data: JsStatemapAndSnapshot) => Promise<void>) {
try {
await this.impl.run(snapshotProviderCallback, statemapInstallerCallback)
// This will hide the 'error' parameter from callback (it comes from NAPI).
const adaptedStatemapInstallerCallback = async (error: Error | null, data: JsStatemapAndSnapshot): Promise<void> => {
if (error) {
throw new TalosSdkError(
SdkErrorKind.Internal,
"Call from native code into 'statemapInstallerCallback' function provided by JS has failed at the NAPI layer. See the 'cause' field for more details.",
{ cause: error }
)
} else {
return await statemapInstallerCallback(data)
}
}

await this.impl.run(snapshotProviderCallback, adaptedStatemapInstallerCallback)
} catch(e) {
const reason: string = e.message
if (isSdkError(reason)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cohort_sdk/src/cohort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl Cohort {

// The loop exits when either of the below conditions are met.
// 1. When commit decision is received from talos agent/certifier.
// 2. When an clientabort is requested.
// 2. When an client abort is requested.
// 3. When all retries are exhausted.
let final_result = loop {
// Await for snapshot and build the certification request payload.
Expand Down
3 changes: 2 additions & 1 deletion packages/cohort_sdk_js/src/initiator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ impl InternalInitiator {
#[napi]
pub async fn certify(
&self,
#[napi(ts_arg_type = "() => Promise<any>")] make_new_request_callback: ThreadsafeFunction<()>,
#[napi(ts_arg_type = "() => Promise<JsCertificationCandidateCallbackResponse>")] make_new_request_callback: ThreadsafeFunction<()>,
#[napi(ts_arg_type = "(error: Error | null, ooRequest: OutOfOrderRequest) => Promise<JsCertificationCandidateCallbackResponse>")]
ooo_callback: ThreadsafeFunction<OutOfOrderRequest>,
) -> napi::Result<JsCertificationResponse> {
let new_request_provider = NewRequestProvider { make_new_request_callback };
Expand Down
4 changes: 3 additions & 1 deletion packages/cohort_sdk_js/src/installer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ impl InternalReplicator {
pub async fn run(
&self,
#[napi(ts_arg_type = "() => Promise<number>")] snapshot_provider_callback: ThreadsafeFunction<()>,
statemap_installer_callback: ThreadsafeFunction<JsStatemapAndSnapshot>,
#[napi(ts_arg_type = "(error: Error | null, data: JsStatemapAndSnapshot) => Promise<void>")] statemap_installer_callback: ThreadsafeFunction<
JsStatemapAndSnapshot,
>,
) -> napi::Result<()> {
let kafka_consumer = KafkaConsumer::new(&self.kafka_config.clone().into());
let brokers = &self.kafka_config.brokers.clone();
Expand Down