Skip to content

Commit

Permalink
Unify type naming convention to PascalCase in Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Jul 10, 2023
1 parent 234e94b commit de4feb3
Show file tree
Hide file tree
Showing 42 changed files with 311 additions and 313 deletions.
12 changes: 6 additions & 6 deletions example/single_mem_node/use_coroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from rraft import (
Config,
ConfState,
Entry_Ref,
EntryRef,
EntryType,
Logger_Ref,
LoggerRef,
Logger,
MemStorage,
Message_Ref,
MessageRef,
OverflowStrategy,
InMemoryRawNode,
)
Expand All @@ -22,7 +22,7 @@ def now() -> int:
return int(datetime.now(tz=timezone.utc).timestamp() * 1000)


async def send_propose(logger: Logger | Logger_Ref) -> None:
async def send_propose(logger: Logger | LoggerRef) -> None:
# Wait some time and send the request to the Raft.
await asyncio.sleep(10)
logger.info("propose a request")
Expand Down Expand Up @@ -57,7 +57,7 @@ async def on_ready(
# Get the `Ready` with `RawNode::ready` interface.
ready = raft_group.ready()

async def handle_messages(msgs: List[Message_Ref]):
async def handle_messages(msgs: List[MessageRef]):
for _msg in msgs:
# Send messages to other peers.
continue
Expand All @@ -73,7 +73,7 @@ async def handle_messages(msgs: List[Message_Ref]):

_last_apply_index = 0

async def handle_committed_entries(committed_entries: List[Entry_Ref]):
async def handle_committed_entries(committed_entries: List[EntryRef]):
for entry in committed_entries:
# Mostly, you need to save the last apply index to resume applying
# after restart. Here we just ignore this because we use a Memory storage.
Expand Down
12 changes: 6 additions & 6 deletions example/single_mem_node/use_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from rraft import (
Config,
ConfState,
Entry_Ref,
EntryRef,
EntryType,
Logger_Ref,
LoggerRef,
Logger,
MemStorage,
Message_Ref,
MessageRef,
OverflowStrategy,
InMemoryRawNode,
)
Expand All @@ -23,7 +23,7 @@ def now() -> int:
return int(datetime.now(tz=timezone.utc).timestamp() * 1000)


def send_propose(logger: Logger | Logger_Ref) -> None:
def send_propose(logger: Logger | LoggerRef) -> None:
def _send_propose():
# Wait some time and send the request to the Raft.
sleep(10)
Expand Down Expand Up @@ -65,7 +65,7 @@ def on_ready(raft_group: InMemoryRawNode, cbs: Dict[str, Callable]) -> None:
# Get the `Ready` with `RawNode::ready` interface.
ready = raft_group.ready()

def handle_messages(msg_refs: List[Message_Ref]):
def handle_messages(msg_refs: List[MessageRef]):
for _msg_ref in msg_refs:
# Send messages to other peers.
continue
Expand All @@ -81,7 +81,7 @@ def handle_messages(msg_refs: List[Message_Ref]):

_last_apply_index = 0

def handle_committed_entries(committed_entries: List[Entry_Ref]):
def handle_committed_entries(committed_entries: List[EntryRef]):
for entry in committed_entries:
# Mostly, you need to save the last apply index to resume applying
# after restart. Here we just ignore this because we use a Memory storage.
Expand Down
8 changes: 4 additions & 4 deletions harness/src/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from rraft import (
Message,
Message_Ref,
MessageRef,
InMemoryRaft,
InMemoryRaftLog_Ref,
InMemoryRaftLogRef,
)


Expand Down Expand Up @@ -33,10 +33,10 @@ def __repr__(self) -> str:
return f"Interface {{ id: {self.raft.get_id()} }}"

@property
def raft_log(self) -> InMemoryRaftLog_Ref:
def raft_log(self) -> InMemoryRaftLogRef:
return self.raft.get_raft_log()

def step(self, message: Message | Message_Ref) -> None:
def step(self, message: Message | MessageRef) -> None:
"""
Step the raft, if it exists.
"""
Expand Down
22 changes: 11 additions & 11 deletions harness/src/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from rraft import (
Config,
ConfState,
Config_Ref,
ConfigRef,
Logger,
Logger_Ref,
LoggerRef,
MemStorage,
Message,
Message_Ref,
MessageRef,
MessageType,
NO_LIMIT,
InMemoryRaft,
Expand Down Expand Up @@ -66,7 +66,7 @@ def default_config() -> Config:
return cfg

@staticmethod
def new(peers: List[Optional[Interface]], l: Logger | Logger_Ref) -> "Network":
def new(peers: List[Optional[Interface]], l: Logger | LoggerRef) -> "Network":
"""
Initializes a network from `peers`.
Expand All @@ -80,8 +80,8 @@ def new(peers: List[Optional[Interface]], l: Logger | Logger_Ref) -> "Network":
@staticmethod
def new_with_config(
peers: List[Optional[Interface]],
config: Config | Config_Ref,
l: Logger | Logger_Ref,
config: Config | ConfigRef,
l: Logger | LoggerRef,
) -> "Network":
"""
Initialize a network from `peers` with explicitly specified `config`.
Expand Down Expand Up @@ -120,12 +120,12 @@ def ignore(self, t: MessageType) -> None:
self.ignorem[t] = True

def filter_(
self, msgs: List[Message] | List[Message_Ref]
self, msgs: List[Message] | List[MessageRef]
) -> List[Message]:
"""
Filter out messages that should be dropped according to rules set by `ignore` or `drop`.
"""
def should_be_filtered(m: Message | Message_Ref):
def should_be_filtered(m: Message | MessageRef):
if self.ignorem.get(m.get_msg_type()):
return False

Expand All @@ -149,7 +149,7 @@ def read_messages(self) -> List[Message]:
msgs.extend(p.read_messages())
return msgs

def send(self, msgs: List[Message] | List[Message_Ref]) -> None:
def send(self, msgs: List[Message] | List[MessageRef]) -> None:
"""
# Instruct the cluster to `step` through the given messages.
#
Expand All @@ -173,13 +173,13 @@ def send(self, msgs: List[Message] | List[Message_Ref]) -> None:
msgs = []
msgs.extend(new_msgs)

def filter_and_send(self, msgs: List[Message] | List[Message_Ref]) -> None:
def filter_and_send(self, msgs: List[Message] | List[MessageRef]) -> None:
"""
Filter `msgs` and then instruct the cluster to `step` through the given messages.
"""
self.send(self.filter_(msgs))

def dispatch(self, messages: List[Message] | List[Message_Ref]) -> None:
def dispatch(self, messages: List[Message] | List[MessageRef]) -> None:
"""
# Dispatches the given messages to the appropriate peers.
#
Expand Down
24 changes: 12 additions & 12 deletions harness/tests/test_raft.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
GetEntriesContext,
HardState,
Logger,
Logger_Ref,
LoggerRef,
MemStorage,
MemStorage_Ref,
MemStorageRef,
Message,
MessageType,
ProgressState,
InMemoryRaft,
InMemoryRaft_Ref,
InMemoryRaftLog_Ref,
InMemoryRaftRef,
InMemoryRaftLogRef,
ProposalDroppedError,
ReadOnlyOption,
Snapshot,
Expand All @@ -70,7 +70,7 @@ def ents_with_config(
pre_vote: bool,
id: int,
peers: List[int],
l: Logger | Logger_Ref,
l: Logger | LoggerRef,
) -> Interface:
store = MemStorage.new_with_conf_state(ConfState(peers, []))

Expand All @@ -87,7 +87,7 @@ def ents_with_config(

def assert_raft_log(
prefix: str,
raft_log: InMemoryRaftLog_Ref,
raft_log: InMemoryRaftLogRef,
committed: int,
applied: int,
last: int,
Expand All @@ -112,7 +112,7 @@ def voted_with_config(
pre_vote: bool,
id: int,
peers: List[int],
l: Logger | Logger_Ref,
l: Logger | LoggerRef,
) -> Interface:
cs = ConfState(peers, [])
store = MemStorage.new_with_conf_state(cs)
Expand All @@ -126,7 +126,7 @@ def voted_with_config(


# Persist committed index and fetch next entries.
def next_ents(r: InMemoryRaft_Ref, s: MemStorage_Ref) -> List[Entry]:
def next_ents(r: InMemoryRaftRef, s: MemStorageRef) -> List[Entry]:
unstable_refs = r.get_raft_log().unstable_entries()
unstable = list(map(lambda e: e.clone(), unstable_refs))

Expand Down Expand Up @@ -3452,7 +3452,7 @@ def test_leader_transfer_second_transfer_to_same_node():
check_leader_transfer_state(nt.peers[1].raft, StateRole.Leader, 1)


def check_leader_transfer_state(r: InMemoryRaft_Ref, state: StateRole, lead: int):
def check_leader_transfer_state(r: InMemoryRaftRef, state: StateRole, lead: int):
assert (
r.get_state() == state and r.get_leader_id() == lead
), f"after transferring, node has state {r.state} lead {state}, want state {r.get_leader_id()} lead {lead}"
Expand Down Expand Up @@ -3547,8 +3547,8 @@ def new_test_learner_raft(
learners: List[int],
election: int,
heartbeat: int,
storage: MemStorage_Ref,
logger: Logger_Ref,
storage: MemStorageRef,
logger: LoggerRef,
) -> Interface:
initial_state = storage.initial_state()

Expand All @@ -3564,7 +3564,7 @@ def new_test_learner_raft(


def new_test_learner_raft_with_prevote(
id: int, peers: List[int], learners: List[int], logger: Logger_Ref, prevote: bool
id: int, peers: List[int], learners: List[int], logger: LoggerRef, prevote: bool
) -> Interface:
storage = new_storage()
storage.initialize_with_conf_state(ConfState(peers, learners))
Expand Down
8 changes: 4 additions & 4 deletions harness/tests/test_raft_paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
ConfState,
Entry,
MemStorage,
MemStorage_Ref,
MemStorageRef,
Message,
Message_Ref,
MessageRef,
MessageType,
StateRole,
default_logger,
Expand All @@ -29,7 +29,7 @@
)


def commit_noop_entry(r: Interface, s: MemStorage_Ref):
def commit_noop_entry(r: Interface, s: MemStorageRef):
assert r.raft.get_state() == StateRole.Leader
r.raft.bcast_append()

Expand All @@ -56,7 +56,7 @@ def commit_noop_entry(r: Interface, s: MemStorage_Ref):
r.raft.commit_apply(committed)


def accept_and_reply(m: Message_Ref) -> Message:
def accept_and_reply(m: MessageRef) -> Message:
assert m.get_msg_type() == MessageType.MsgAppend
reply = new_message(m.get_to(), m.get_from(), MessageType.MsgAppendResponse, 0)
reply.set_term(m.get_term())
Expand Down
Loading

0 comments on commit de4feb3

Please sign in to comment.