Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Jan 3, 2024
1 parent 18ac9b7 commit e2544f7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
13 changes: 10 additions & 3 deletions binding/python/raftify.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import abc
from dataclasses import dataclass
from typing import Any, Callable, Final, Optional

# TODO: Make these abstract types available in the Python side.
class AbstractLogEntry(metaclass=abc.ABCMeta):
@abc.abstractmethod
def encode(self) -> bytes:
Expand All @@ -18,13 +19,19 @@ class AbstractStateMachine(metaclass=abc.ABCMeta):
"""

@abc.abstractmethod
async def apply(self, message: bytes) -> bytes:
def apply(self, message: bytes) -> bytes:
raise NotImplementedError
@abc.abstractmethod
async def snapshot(self) -> bytes:
def snapshot(self) -> bytes:
raise NotImplementedError
@abc.abstractmethod
async def restore(self, snapshot: bytes) -> None:
def restore(self, snapshot: bytes) -> None:
raise NotImplementedError
@abc.abstractmethod
def encode(self) -> bytes:
raise NotImplementedError
@classmethod
def decode(cls, packed: bytes) -> "AbstractStateMachine":
raise NotImplementedError

class Raft:
Expand Down
14 changes: 1 addition & 13 deletions binding/python/src/bindings/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@ use raftify::Config;
use super::raft_rs::config::PyRaftConfig;

#[derive(Clone)]
#[pyclass(name = "Config")]
#[pyclass(get_all, set_all, name = "Config")]
pub struct PyConfig {
#[pyo3(get, set)]
pub raft_config: PyRaftConfig,

#[pyo3(get, set)]
pub log_dir: String,
#[pyo3(get, set)]
pub save_compacted_logs: bool,
#[pyo3(get, set)]
pub compacted_log_dir: String,
#[pyo3(get, set)]
pub compacted_log_size_threshold: u64,
#[pyo3(get, set)]
pub snapshot_interval: f32,
#[pyo3(get, set)]
pub tick_interval: f32,
#[pyo3(get, set)]
pub lmdb_map_size: u64,
#[pyo3(get, set)]
pub cluster_id: String,
#[pyo3(get, set)]
pub terminate_on_remove: bool,
#[pyo3(get, set)]
pub conf_change_request_timeout: f32,
}

Expand Down
17 changes: 8 additions & 9 deletions binding/python/src/bindings/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pyo3::{prelude::*, types::PyBytes};
use raftify::{AbstractLogEntry, AbstractStateMachine, Error, Result};
use std::{fmt, sync::Mutex};

use super::errors::{ApplyError, DecodingError, RestoreError, SnapshotError};
use super::errors::{ApplyError, RestoreError, SnapshotError};
use super::utils::get_python_repr;

pub static ENTRY_LOG_ENTRY_DESERIALIZE_CB: Lazy<Mutex<Option<PyObject>>> =
Expand All @@ -22,7 +22,7 @@ pub fn set_fsm_deserializer(cb: PyObject) {
}

#[derive(Clone)]
#[pyclass(name = "AbstractLogEntry")]
#[pyclass]
pub struct PyLogEntry {
pub log_entry: Py<PyAny>,
}
Expand Down Expand Up @@ -88,17 +88,11 @@ impl PyLogEntry {
}

#[derive(Clone)]
#[pyclass(name = "AbstractStateMachine")]
#[pyclass]
pub struct PyFSM {
pub store: Py<PyAny>,
}

impl PyFSM {
pub fn new(store: Py<PyAny>) -> Self {
Self { store }
}
}

impl fmt::Debug for PyFSM {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Python::with_gil(|py| {
Expand All @@ -125,6 +119,11 @@ impl fmt::Display for PyFSM {

#[pymethods]
impl PyFSM {
#[new]
pub fn new(store: Py<PyAny>) -> Self {
Self { store }
}

fn __getattr__(&self, py: Python, attr: &str) -> PyResult<PyObject> {
let store: &PyAny = self.store.as_ref(py);
let attr_value = store.getattr(attr)?;
Expand Down
1 change: 0 additions & 1 deletion binding/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod bindings;
fn raftify(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<bindings::config::PyConfig>()?;
m.add_class::<bindings::raft_rs::config::PyRaftConfig>()?;
m.add_class::<bindings::state_machine::PyFSM>()?;
m.add_class::<bindings::raft_facade::PyRaftFacade>()?;
m.add_class::<bindings::peers::PyPeers>()?;
m.add_class::<bindings::raft_client::PyRaftServiceClient>()?;
Expand Down

0 comments on commit e2544f7

Please sign in to comment.