Skip to content

Commit

Permalink
Support GHA. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
YoEight authored Jul 2, 2024
1 parent 3b758a7 commit 8901816
Show file tree
Hide file tree
Showing 23 changed files with 232 additions and 106 deletions.
38 changes: 38 additions & 0 deletions .github/scripts/setup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
param (
[Parameter(Mandatory = $true)]
[string]$Runner
)

$global:ProtoCompilerVersion = "27.2"

function Get-Link
{
$baseUrl = "https://github.com/protocolbuffers/protobuf/releases/download/v$global:ProtoCompilerVersion/protoc"
switch ($Runner)
{
"ubuntu-latest" {
return "$baseUrl-$global:ProtoCompilerVersion-linux-x86_64.zip"
}

"windows-latest" {
return "$baseUrl-$global:ProtoCompilerVersion-win64.zip"
}

"macos-latest" {
return "$baseUrl-$global:ProtoCompilerVersion-osx-universal_binary.zip"
}

default {
throw "unsupported runner '{$Runner}'"
}
}
}

$link = Get-Link
$filename = "protobuf-compiler"
Invoke-WebRequest -Uri $link -OutFile "$filename.zip"
[System.IO.Compression.ZipFile]::ExtractToDirectory("$filename.zip", $filename)

$protobufBinDir = (Get-Item -Path "protobuf-compiler/bin").FullName

"protoc_bin=$protobufBinDir" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
81 changes: 81 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Rust

on:
push:
branches:
- "master"

pull_request:
branches:
- "master"

schedule:
- cron: '0 3 * * 0' # Every sunday at 3am UTC.

env:
CARGO_TERM_COLOR: always

jobs:
build:
continue-on-error: true
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- windows-latest
- macos-latest

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: Download and configure dependencies
id: configure
shell: pwsh
run: .github/scripts/setup.ps1 -Runner ${{ matrix.os }}

- name: Update system path
shell: pwsh
run: |
"${{ steps.configure.outputs.protoc_bin }}" | Out-File -FilePath $env:GITHUB_PATH -Append
- name: Build
run: cargo check --all-targets

- name: Run tests
run: cargo test --all-targets

linting:
needs: build
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4

- name: Download and configure dependencies
id: configure
shell: pwsh
run: .github/scripts/setup.ps1 -Runner ${{ matrix.os }}

- name: Update system path
shell: pwsh
run: |
"${{ steps.configure.outputs.protoc_bin }}" | Out-File -FilePath $env:GITHUB_PATH -Append
- name: Install tools
run: |
rustup component add clippy
rustup component add rustfmt
- name: Lint
run: cargo clippy --all-features -- -D warnings

- name: Format
run: cargo fmt -- --check
19 changes: 19 additions & 0 deletions .run/clippy.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="clippy" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="clippy --all-features -- -D warnings" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="true" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="true" />
<option name="allFeatures" value="false" />
<option name="withSudo" value="false" />
<option name="buildTarget" value="REMOTE" />
<option name="backtrace" value="SHORT" />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>
12 changes: 3 additions & 9 deletions geth-client/src/next/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::HashMap;

use uuid::Uuid;

use geth_common::{EndPoint, Operation, OperationIn, OperationOut};
use geth_common::generated::next::protocol;
use geth_common::{EndPoint, Operation, OperationIn, OperationOut};

use crate::next::{Command, connect_to_node, Connection, Mailbox};
use crate::next::{connect_to_node, Command, Connection, Mailbox};

pub struct Driver {
endpoint: EndPoint,
Expand Down Expand Up @@ -45,13 +45,7 @@ impl Driver {
let correlation = command.operation_in.correlation;
let operation_in: protocol::OperationIn = command.operation_in.clone().into();

if self
.connection
.as_ref()
.unwrap()
.send(operation_in.into())
.is_ok()
{
if self.connection.as_ref().unwrap().send(operation_in).is_ok() {
self.registry.insert(correlation, command);
return Ok(());
}
Expand Down
12 changes: 2 additions & 10 deletions geth-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,8 @@ pub struct OperationOut {
impl OperationOut {
pub fn is_streaming(&self) -> bool {
match &self.reply {
Reply::SubscriptionEvent(event) => match event {
SubscriptionEvent::Unsubscribed(_) => false,
_ => true,
},

Reply::StreamRead(event) => match event {
StreamRead::EventAppeared(_) => true,
_ => false,
},

Reply::SubscriptionEvent(event) => !matches!(event, SubscriptionEvent::Unsubscribed(_)),
Reply::StreamRead(event) => matches!(event, StreamRead::EventAppeared(_)),
_ => false,
}
}
Expand Down
6 changes: 1 addition & 5 deletions geth-consensus/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ where
granted = true;
}
} else {
let last_entry_id = if let Some(last) = storage.last_entry() {
last
} else {
EntryId::default()
};
let last_entry_id = storage.last_entry().unwrap_or_default();

granted = self.voted_for == Some(args.candidate_id.clone())
&& last_entry_id.index <= args.last_log_index
Expand Down
2 changes: 1 addition & 1 deletion geth-domain/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;

fn main() -> io::Result<()> {
prost_build::Config::new()
.bytes(&[".RecordedEvent.data", ".RecordedEvent.metadata"])
.bytes([".RecordedEvent.data", ".RecordedEvent.metadata"])
.format(true)
.default_package_filename("model")
.compile_protos(&["protos/events.proto"], &["protos/"])?;
Expand Down
6 changes: 3 additions & 3 deletions geth-domain/src/append_propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where

fn next(&mut self) -> Option<Self::Item> {
let propose = self.events.next()?;
let event = crate::binary::events::RecordedEvent {
let event = crate::binary::models::RecordedEvent {
id: propose.id.into(),
revision: self.revision,
stream_name: self.stream_name.clone(),
Expand All @@ -52,8 +52,8 @@ where
};

self.revision += 1;
let event = crate::binary::events::Events {
event: Some(crate::binary::events::Event::RecordedEvent(event)),
let event = crate::binary::models::Events {
event: Some(crate::binary::models::Event::RecordedEvent(event)),
};

event.encode(&mut self.buffer).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion geth-domain/src/binary.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod events {
pub mod models {
use uuid::Uuid;

pub use events::Event;
Expand Down
6 changes: 3 additions & 3 deletions geth-domain/src/index/lsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use geth_mikoshi::hashing::mikoshi_hash;
use geth_mikoshi::storage::{FileId, Storage};
use geth_mikoshi::wal::{WALRef, WriteAheadLog};

use crate::binary::events::Event;
use crate::binary::models::Event;
use crate::index::block::BlockEntry;
use crate::index::mem_table::MemTable;
use crate::index::ss_table::SsTable;
Expand All @@ -22,7 +22,7 @@ pub const LSM_DEFAULT_MEM_TABLE_SIZE: usize = 4_096;
pub const LSM_BASE_SSTABLE_BLOCK_COUNT: usize = 4;

pub fn sst_table_block_count_limit(level: u8) -> usize {
2 ^ (level as usize) * LSM_BASE_SSTABLE_BLOCK_COUNT
(2 ^ (level as usize)) * LSM_BASE_SSTABLE_BLOCK_COUNT
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -180,7 +180,7 @@ where
pub fn ss_table_first(&self) -> Option<SsTable<S>> {
let state = self.state.read().unwrap();
let ts = state.levels.get(&0)?;
ts.get(0).cloned()
ts.front().cloned()
}

pub fn put_values<V>(&self, values: V) -> io::Result<()>
Expand Down
10 changes: 6 additions & 4 deletions geth-domain/src/index/mem_table.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::index::block::BlockEntry;
use std::cmp::Ordering;
use std::collections::BTreeMap;

use bytes::{Buf, BufMut, BytesMut};

use geth_common::{Direction, Revision};
use std::cmp::Ordering;
use std::collections::BTreeMap;

use crate::index::block::BlockEntry;

pub const MEM_TABLE_ENTRY_SIZE: usize = 16;

Expand Down Expand Up @@ -39,7 +41,7 @@ impl MemTable {
}

pub fn scan(&self, key: u64, direction: Direction, start: Revision<u64>, count: usize) -> Scan {
let buffer = self.inner.get(&key).map(|s| s.clone()).unwrap_or_default();
let buffer = self.inner.get(&key).cloned().unwrap_or_default();

Scan::new(key, buffer, direction, start, count)
}
Expand Down
4 changes: 3 additions & 1 deletion geth-domain/src/iter/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use geth_common::IteratorIO;
use geth_mikoshi::wal::entries::EntryIter;
use geth_mikoshi::wal::WriteAheadLog;

use crate::binary::events::Event;
use crate::binary::models::Event;
use crate::parse_event;

pub struct EventIter<WAL> {
Expand All @@ -28,6 +28,8 @@ impl<WAL: WriteAheadLog> IteratorIO for EventIter<WAL> {
if let Event::RecordedEvent(event) = event.event.unwrap() {
return Ok(Some((item.position, crate::RecordedEvent::from(event))));
}

continue;
}

return Ok(None);
Expand Down
4 changes: 2 additions & 2 deletions geth-domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use uuid::Uuid;
pub use index::{Lsm, LsmSettings};

pub use crate::append_propose::AppendProposes;
use crate::binary::events::Events;
use crate::binary::models::Events;

mod append_propose;
pub mod binary;
Expand Down Expand Up @@ -39,7 +39,7 @@ pub struct RecordedEvent {
}

impl RecordedEvent {
pub fn from(inner: binary::events::RecordedEvent) -> RecordedEvent {
pub fn from(inner: binary::models::RecordedEvent) -> RecordedEvent {
Self {
id: inner.id.into(),
revision: inner.revision,
Expand Down
9 changes: 6 additions & 3 deletions geth-mikoshi/src/storage/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use crate::constants::CHUNK_SIZE;
use crate::storage::{FileCategory, FileId, Storage};
use bytes::{Bytes, BytesMut};
use std::collections::HashMap;
use std::fs::{read_dir, File, OpenOptions};
use std::io::{self, ErrorKind};
Expand All @@ -11,6 +8,11 @@ use std::os::windows::fs::FileExt;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use bytes::{Bytes, BytesMut};

use crate::constants::CHUNK_SIZE;
use crate::storage::{FileCategory, FileId, Storage};

#[derive(Clone)]
pub struct FileSystemStorage {
root: PathBuf,
Expand Down Expand Up @@ -55,6 +57,7 @@ impl FileSystemStorage {
.write(true)
.read(true)
.create(true)
.truncate(false)
.open(path)?;

Ok(file)
Expand Down
14 changes: 11 additions & 3 deletions geth-mikoshi/src/storage/in_mem.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use crate::constants::CHUNK_SIZE;
use crate::storage::{FileCategory, FileId, Storage};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::collections::HashMap;
use std::io;
use std::sync::{Arc, Mutex};

use bytes::{Buf, BufMut, Bytes, BytesMut};

use crate::constants::CHUNK_SIZE;
use crate::storage::{FileCategory, FileId, Storage};

#[derive(Clone)]
pub struct InMemoryStorage {
inner: Arc<Mutex<HashMap<FileId, Bytes>>>,
}

impl InMemoryStorage {
pub fn new() -> Self {
InMemoryStorage::default()
}
}

impl Default for InMemoryStorage {
fn default() -> Self {
Self {
inner: Arc::new(Mutex::new(Default::default())),
}
Expand Down
Loading

0 comments on commit 8901816

Please sign in to comment.