Skip to content

Commit

Permalink
fix: disallow produce mirror topic from home (#4029)
Browse files Browse the repository at this point in the history
* fix: disallow produce mirror topic from home

* test: add is_home_mirror partition test
  • Loading branch information
fraidev committed May 30, 2024
1 parent f75fc8b commit 716f0a2
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 4 additions & 0 deletions crates/fluvio-controlplane-metadata/src/partition/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ impl PartitionMirrorConfig {
Self::Home(h) => h.remote_cluster.clone(),
}
}

pub fn is_home_mirror(&self) -> bool {
matches!(self, Self::Home(_))
}
}

impl std::fmt::Display for PartitionMirrorConfig {
Expand Down
2 changes: 1 addition & 1 deletion crates/fluvio-protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "fluvio-protocol"
edition = "2021"
version = "0.10.13"
version = "0.10.14"
authors = ["Fluvio Contributors <team@fluvio.io>"]
description = "Fluvio streaming protocol"
repository = "https://github.com/infinyon/fluvio"
Expand Down
3 changes: 3 additions & 0 deletions crates/fluvio-protocol/src/link/error_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ pub enum ErrorCode {
#[fluvio(tag = 11002)]
#[error("the mirror already exists")]
MirrorAlreadyExists,
#[fluvio(tag = 11003)]
#[error("produce from home is not allowed")]
MirrorProduceFromHome,

// Specs
#[fluvio(tag = 12001)]
Expand Down
11 changes: 11 additions & 0 deletions crates/fluvio-spu/src/services/public/produce_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ async fn handle_produce_topic(
}
};

if let Some(mirror) = &leader_state.get_replica().mirror {
if mirror.is_home_mirror() {
debug!(%replica_id, "Mirror replica is not supported for produce");
topic_result.partitions.push(PartitionWriteResult::error(
replica_id,
ErrorCode::MirrorProduceFromHome,
));
continue;
}
}

if let Err(err) = apply_smartmodules(
&mut partition_request,
smartmodules,
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/mirroring_smoke_tests/export.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ setup_file() {

REMOTE_NAME=remote-test-1
export REMOTE_NAME
debug_msg "Topic name: $REMOTE_NAME"
debug_msg "Remote name: $REMOTE_NAME"

MESSAGE="$(random_string 7)"
export MESSAGE
Expand Down
49 changes: 49 additions & 0 deletions tests/cli/mirroring_smoke_tests/mirror-topic.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bats

TEST_HELPER_DIR="$BATS_TEST_DIRNAME/../test_helper"
export TEST_HELPER_DIR

load "$TEST_HELPER_DIR"/tools_check.bash
load "$TEST_HELPER_DIR"/fluvio_dev.bash
load "$TEST_HELPER_DIR"/bats-support/load.bash
load "$TEST_HELPER_DIR"/bats-assert/load.bash

setup_file() {
CURRENT_DATE=$(date +%Y-%m)
export CURRENT_DATE

REMOTE_NAME="$(random_string 7)"
export REMOTE_NAME
debug_msg "Remote name: $REMOTE_NAME"

MESSAGE="$(random_string 7)"
export MESSAGE
debug_msg "$MESSAGE"

TOPIC_NAME="$(random_string 7)"
export TOPIC_NAME
debug_msg "Topic name: $TOPIC_NAME"
}

@test "Can register an remote cluster" {
run timeout 15s "$FLUVIO_BIN" remote register "$REMOTE_NAME"

assert_output "remote cluster \"$REMOTE_NAME\" was registered"
assert_success
}

@test "Can create a mirror topic" {
echo "[\"$REMOTE_NAME\"]" > remotes.json
run timeout 15s "$FLUVIO_BIN" topic create "$TOPIC_NAME" --mirror-apply remotes.json

assert_output "topic \"$TOPIC_NAME\" created"
assert_success
}

@test "Can't produce to a mirror topic from home" {
MESSAGE="$(random_string 7)"
run bash -c 'echo "$MESSAGE" | timeout 15s "$FLUVIO_BIN" produce "$TOPIC_NAME"'

assert_output "Producer error: Producer received an error code: produce from home is not allowed"
assert_failure
}
2 changes: 1 addition & 1 deletion tests/cli/mirroring_smoke_tests/remote.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ setup_file() {

REMOTE_NAME=remote-test-1
export REMOTE_NAME
debug_msg "Topic name: $REMOTE_NAME"
debug_msg "Remote name: $REMOTE_NAME"

MESSAGE="$(random_string 7)"
export MESSAGE
Expand Down

0 comments on commit 716f0a2

Please sign in to comment.