-
Notifications
You must be signed in to change notification settings - Fork 269
feat(shard): create shard crate and generic IggyShard
#2811
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
base: master
Are you sure you want to change the base?
Changes from all commits
0a202f9
d094b3f
fbf14e0
10854c9
5557565
85bf433
f9e0fc7
ed75391
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,7 @@ jobs: | |
| sdk | ||
| security | ||
| server | ||
| shard | ||
| test | ||
| web | ||
| consensus | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| [package] | ||
| name = "shard" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| consensus = { path = "../consensus" } | ||
| iggy_common = { path = "../common" } | ||
| journal = { path = "../journal" } | ||
| message_bus = { path = "../message_bus" } | ||
| metadata = { path = "../metadata" } | ||
| partitions = { path = "../partitions" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use consensus::{MuxPlane, NamespacedPipeline, Plane, PlaneIdentity, VsrConsensus}; | ||
| use iggy_common::header::{GenericHeader, PrepareHeader, PrepareOkHeader, RequestHeader}; | ||
| use iggy_common::message::{Message, MessageBag}; | ||
| use iggy_common::sharding::IggyNamespace; | ||
| use iggy_common::variadic; | ||
| use journal::{Journal, JournalHandle}; | ||
| use message_bus::MessageBus; | ||
| use metadata::IggyMetadata; | ||
| use metadata::stm::StateMachine; | ||
| use partitions::IggyPartitions; | ||
|
|
||
| // variadic!(Metadata, Partitions) = (Metadata, (Partitions, ())) | ||
| type PlaneInner<B, J, S, M> = ( | ||
| IggyMetadata<VsrConsensus<B>, J, S, M>, | ||
| (IggyPartitions<VsrConsensus<B, NamespacedPipeline>>, ()), | ||
| ); | ||
|
|
||
| pub type ShardPlane<B, J, S, M> = MuxPlane<PlaneInner<B, J, S, M>>; | ||
|
|
||
| pub struct IggyShard<B, J, S, M> | ||
| where | ||
| B: MessageBus, | ||
| { | ||
| pub id: u8, | ||
| pub name: String, | ||
| pub plane: ShardPlane<B, J, S, M>, | ||
| } | ||
|
|
||
| impl<B, J, S, M> IggyShard<B, J, S, M> | ||
| where | ||
| B: MessageBus, | ||
| { | ||
| /// Create a new shard from pre-built metadata and partition planes. | ||
| pub fn new( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets pass in already constructed consensus, metadata instances in the constructor
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I've realized my mistake and refactored the code. I've pushed the refactor, please take a look now. |
||
| id: u8, | ||
| name: String, | ||
| metadata: IggyMetadata<VsrConsensus<B>, J, S, M>, | ||
| partitions: IggyPartitions<VsrConsensus<B, NamespacedPipeline>>, | ||
| ) -> Self { | ||
| let plane = MuxPlane::new(variadic!(metadata, partitions)); | ||
| Self { id, name, plane } | ||
| } | ||
|
|
||
| /// Dispatch an incoming network message to the appropriate consensus plane. | ||
| /// | ||
| /// Routes requests, replication messages, and acks to either the metadata | ||
| /// plane or the partitions plane based on `PlaneIdentity::is_applicable`. | ||
| pub async fn on_message(&self, message: Message<GenericHeader>) | ||
| where | ||
| B: MessageBus<Replica = u8, Data = Message<GenericHeader>, Client = u128>, | ||
| J: JournalHandle, | ||
| <J as JournalHandle>::Target: Journal< | ||
| <J as JournalHandle>::Storage, | ||
| Entry = Message<PrepareHeader>, | ||
| Header = PrepareHeader, | ||
| >, | ||
| M: StateMachine<Input = Message<PrepareHeader>>, | ||
| { | ||
| match MessageBag::from(message) { | ||
| MessageBag::Request(request) => self.on_request(request).await, | ||
| MessageBag::Prepare(prepare) => self.on_replicate(prepare).await, | ||
| MessageBag::PrepareOk(prepare_ok) => self.on_ack(prepare_ok).await, | ||
| } | ||
| } | ||
|
|
||
| pub async fn on_request(&self, request: Message<RequestHeader>) | ||
| where | ||
| B: MessageBus<Replica = u8, Data = Message<GenericHeader>, Client = u128>, | ||
| J: JournalHandle, | ||
| <J as JournalHandle>::Target: Journal< | ||
| <J as JournalHandle>::Storage, | ||
| Entry = Message<PrepareHeader>, | ||
| Header = PrepareHeader, | ||
| >, | ||
| M: StateMachine<Input = Message<PrepareHeader>>, | ||
| { | ||
| let planes = self.plane.inner(); | ||
| if planes.0.is_applicable(&request) { | ||
| planes.0.on_request(request).await; | ||
| } else { | ||
| planes.1.0.on_request(request).await; | ||
| } | ||
| } | ||
|
|
||
| pub async fn on_replicate(&self, prepare: Message<PrepareHeader>) | ||
| where | ||
| B: MessageBus<Replica = u8, Data = Message<GenericHeader>, Client = u128>, | ||
| J: JournalHandle, | ||
| <J as JournalHandle>::Target: Journal< | ||
| <J as JournalHandle>::Storage, | ||
| Entry = Message<PrepareHeader>, | ||
| Header = PrepareHeader, | ||
| >, | ||
| M: StateMachine<Input = Message<PrepareHeader>>, | ||
| { | ||
| let planes = self.plane.inner(); | ||
| if planes.0.is_applicable(&prepare) { | ||
| planes.0.on_replicate(prepare).await; | ||
| } else { | ||
| planes.1.0.on_replicate(prepare).await; | ||
| } | ||
| } | ||
|
|
||
| pub async fn on_ack(&self, prepare_ok: Message<PrepareOkHeader>) | ||
| where | ||
| B: MessageBus<Replica = u8, Data = Message<GenericHeader>, Client = u128>, | ||
| J: JournalHandle, | ||
| <J as JournalHandle>::Target: Journal< | ||
| <J as JournalHandle>::Storage, | ||
| Entry = Message<PrepareHeader>, | ||
| Header = PrepareHeader, | ||
| >, | ||
| M: StateMachine<Input = Message<PrepareHeader>>, | ||
| { | ||
| let planes = self.plane.inner(); | ||
| if planes.0.is_applicable(&prepare_ok) { | ||
| planes.0.on_ack(prepare_ok).await; | ||
| } else { | ||
| planes.1.0.on_ack(prepare_ok).await; | ||
| } | ||
| } | ||
|
|
||
| pub fn init_partition(&mut self, namespace: IggyNamespace) | ||
| where | ||
| B: MessageBus< | ||
| Replica = u8, | ||
| Data = iggy_common::message::Message<iggy_common::header::GenericHeader>, | ||
| Client = u128, | ||
| >, | ||
| { | ||
| let partitions = &mut self.plane.inner_mut().1.0; | ||
| partitions.init_partition_in_memory(namespace); | ||
| partitions.register_namespace_in_pipeline(namespace.inner()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could import the
variadic!macro in there and use it ?