Skip to content

Commit c729c21

Browse files
authored
Reintroduce replication factor parameter (#2718)
1 parent 1f4207d commit c729c21

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

crates/cli/src/subcommands/publish.rs

+11
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ pub fn cli() -> clap::Command {
4848
.conflicts_with("build_options")
4949
.help("The system path (absolute or relative) to the compiled wasm binary we should publish, instead of building the project."),
5050
)
51+
.arg(
52+
Arg::new("num_replicas")
53+
.long("num-replicas")
54+
.hide(true)
55+
.help("UNSTABLE: The number of replicas the database should have")
56+
)
5157
.arg(
5258
common_args::anonymous()
5359
)
@@ -79,6 +85,7 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
7985
let wasm_file = args.get_one::<PathBuf>("wasm_file");
8086
let database_host = config.get_host_url(server)?;
8187
let build_options = args.get_one::<String>("build_options").unwrap();
88+
let num_replicas = args.get_one::<u8>("num_replicas");
8289

8390
// If the user didn't specify an identity and we didn't specify an anonymous identity, then
8491
// we want to use the default identity
@@ -152,6 +159,10 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
152159
}
153160
builder = builder.query(&[("clear", true)]);
154161
}
162+
if let Some(n) = num_replicas {
163+
eprintln!("WARNING: Use of unstable option `--num-replicas`.\n");
164+
builder = builder.query(&[("num_replicas", *n)]);
165+
}
155166

156167
println!("Publishing module...");
157168

crates/client-api/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::num::NonZeroU8;
12
use std::sync::Arc;
23

34
use async_trait::async_trait;
@@ -149,7 +150,9 @@ pub struct DatabaseDef {
149150
/// The compiled program of the database module.
150151
pub program_bytes: Vec<u8>,
151152
/// The desired number of replicas the database shall have.
152-
pub num_replicas: u32,
153+
///
154+
/// If `None`, the edition default is used.
155+
pub num_replicas: Option<NonZeroU8>,
153156
/// The host type of the supplied program.
154157
pub host_type: HostType,
155158
}

crates/client-api/src/routes/database.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::num::NonZeroU8;
12
use std::str::FromStr;
23
use std::time::Duration;
34

@@ -471,6 +472,7 @@ pub struct PublishDatabaseParams {
471472
pub struct PublishDatabaseQueryParams {
472473
#[serde(default)]
473474
clear: bool,
475+
num_replicas: Option<usize>,
474476
}
475477

476478
use std::env;
@@ -498,7 +500,7 @@ fn allow_creation(auth: &SpacetimeAuth) -> Result<(), ErrorResponse> {
498500
pub async fn publish<S: NodeDelegate + ControlStateDelegate>(
499501
State(ctx): State<S>,
500502
Path(PublishDatabaseParams { name_or_identity }): Path<PublishDatabaseParams>,
501-
Query(PublishDatabaseQueryParams { clear }): Query<PublishDatabaseQueryParams>,
503+
Query(PublishDatabaseQueryParams { clear, num_replicas }): Query<PublishDatabaseQueryParams>,
502504
Extension(auth): Extension<SpacetimeAuth>,
503505
body: Bytes,
504506
) -> axum::response::Result<axum::Json<PublishResult>> {
@@ -572,13 +574,21 @@ pub async fn publish<S: NodeDelegate + ControlStateDelegate>(
572574
}
573575
};
574576

577+
let num_replicas = num_replicas
578+
.map(|n| {
579+
let n = u8::try_from(n).map_err(|_| (StatusCode::BAD_REQUEST, "Replication factor {n} out of bounds"))?;
580+
Ok::<_, ErrorResponse>(NonZeroU8::new(n))
581+
})
582+
.transpose()?
583+
.flatten();
584+
575585
let maybe_updated = ctx
576586
.publish_database(
577587
&auth.identity,
578588
DatabaseDef {
579589
database_identity,
580590
program_bytes: body.into(),
581-
num_replicas: 1,
591+
num_replicas,
582592
host_type: HostType::Wasm,
583593
},
584594
)

crates/standalone/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ impl spacetimedb_client_api::ControlStateWriteAccess for StandaloneEnv {
230230
) -> anyhow::Result<Option<UpdateDatabaseResult>> {
231231
let existing_db = self.control_db.get_database_by_identity(&spec.database_identity)?;
232232

233+
// standalone does not support replication.
234+
let num_replicas = 1;
235+
233236
match existing_db {
234237
// The database does not already exist, so we'll create it.
235238
None => {
@@ -258,7 +261,7 @@ impl spacetimedb_client_api::ControlStateWriteAccess for StandaloneEnv {
258261
let database_id = self.control_db.insert_database(database.clone())?;
259262
database.id = database_id;
260263

261-
self.schedule_replicas(database_id, spec.num_replicas).await?;
264+
self.schedule_replicas(database_id, num_replicas).await?;
262265

263266
Ok(None)
264267
}
@@ -275,7 +278,6 @@ impl spacetimedb_client_api::ControlStateWriteAccess for StandaloneEnv {
275278
let database_id = database.id;
276279
let database_identity = database.database_identity;
277280

278-
let num_replicas = spec.num_replicas;
279281
let leader = self
280282
.leader(database_id)
281283
.await?
@@ -416,7 +418,7 @@ impl StandaloneEnv {
416418
Ok(())
417419
}
418420

419-
async fn schedule_replicas(&self, database_id: u64, num_replicas: u32) -> Result<(), anyhow::Error> {
421+
async fn schedule_replicas(&self, database_id: u64, num_replicas: u8) -> Result<(), anyhow::Error> {
420422
// Just scheduling a bunch of replicas to the only machine
421423
for i in 0..num_replicas {
422424
let replica = Replica {

crates/testing/src/modules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl CompiledModule {
193193
DatabaseDef {
194194
database_identity: db_identity,
195195
program_bytes,
196-
num_replicas: 1,
196+
num_replicas: None,
197197
host_type: HostType::Wasm,
198198
},
199199
)

0 commit comments

Comments
 (0)