Skip to content
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

Refactoring the rust api #27

Open
wants to merge 69 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
b788219
Refactoring the rust api
MeirShpilraien Jul 11, 2022
f44d7fe
Merge branch 'master' into refactoring_rust_api
MeirShpilraien Jul 11, 2022
66facb7
Fix valgrind failure.
MeirShpilraien Jul 13, 2022
4cd53b3
Added linking libs to libmr project instead of test project.
MeirShpilraien Jul 13, 2022
aade142
Added the ability to run a remote task and shard responsible for a gi…
MeirShpilraien Jul 17, 2022
6b985a7
Changed the repository to be a cargo repository.
MeirShpilraien Jul 17, 2022
5cd89a8
added build.rs
MeirShpilraien Jul 17, 2022
8f3f3f0
change redismodule-rs dep.
MeirShpilraien Jul 17, 2022
7ca9a49
Change tests redismodule-rs version
MeirShpilraien Jul 17, 2022
12e5bc9
Added missing unblock client.
MeirShpilraien Jul 17, 2022
28e2ecd
Fix tests and build
MeirShpilraien Aug 4, 2022
008125f
Changes to support binary keys names for run_on_key API.
MeirShpilraien Sep 27, 2022
d68fa3b
Move to github action.
MeirShpilraien Oct 13, 2022
4d33039
Format fixes
MeirShpilraien Oct 13, 2022
2a65690
checkout submodules
MeirShpilraien Oct 13, 2022
0778185
run tests verbose
MeirShpilraien Oct 13, 2022
b606c26
Fix tests failure
MeirShpilraien Oct 13, 2022
10ecc8a
Added mac flow
MeirShpilraien Oct 13, 2022
1de3b28
run mac flow on PR
MeirShpilraien Oct 13, 2022
48e6cc1
Install automake on mac
MeirShpilraien Oct 13, 2022
eb190a3
Install openssl on macos.
MeirShpilraien Oct 13, 2022
4214233
Set PKG_CONFIG_PATH env var.
MeirShpilraien Oct 13, 2022
e79e242
Fix makefile to add openssl include path on macos.
MeirShpilraien Oct 13, 2022
3d147f1
Added ssl lib path to build.rs.
MeirShpilraien Oct 13, 2022
0cb8626
Format fixes
MeirShpilraien Oct 13, 2022
b5b5253
Install RLTest correctly on macos.
MeirShpilraien Oct 13, 2022
88712c6
Run tests with verbose output on macos.
MeirShpilraien Oct 13, 2022
dfc860b
Support to run the tests on macos.
MeirShpilraien Oct 13, 2022
3778a16
Run full tests (cluster and tls)
MeirShpilraien Oct 13, 2022
ea80516
Added missing file.
MeirShpilraien Oct 13, 2022
7153b83
Fix full tests script.
MeirShpilraien Oct 13, 2022
ab5fb14
Fix fluky tests.
MeirShpilraien Oct 14, 2022
7c894ff
Fix fluky tests.
MeirShpilraien Oct 14, 2022
160f458
Added valgrind run.
MeirShpilraien Oct 16, 2022
4b4daf4
Install valgrind
MeirShpilraien Oct 16, 2022
b7e4e28
Fix valgrind installation failure
MeirShpilraien Oct 16, 2022
64fa1bd
Fix ssl_ctx leak.
MeirShpilraien Oct 16, 2022
9ab9ed7
Added timeout to run_on_key API.
MeirShpilraien Oct 16, 2022
0317d9a
Fix tests compilation.
MeirShpilraien Oct 16, 2022
69dcbaf
Run mac tests with verbose output
MeirShpilraien Oct 16, 2022
38102a1
Small remote task fixes
MeirShpilraien Oct 16, 2022
d6849d8
Do not set timeout on SIZE_MAX.
MeirShpilraien Oct 23, 2022
b720acd
Remove tests verbosity on mac.
MeirShpilraien Oct 23, 2022
9e401e9
Added MR_RunOnAllShards API.
MeirShpilraien Oct 24, 2022
d92637c
Format fixes
MeirShpilraien Oct 24, 2022
a8426fc
Compile Redis with valgrind for testing.
MeirShpilraien Oct 24, 2022
2bc32ba
Fix valgrind failure
MeirShpilraien Oct 24, 2022
7d9e2dd
Fix timeout handling on run_on_all_shards api.
MeirShpilraien Oct 25, 2022
1f2658e
Improve run on all shards test.
MeirShpilraien Oct 25, 2022
60cbd70
Merge branch 'master' into refactoring_rust_api
MeirShpilraien Dec 1, 2022
4dc9715
Added license to all files.
MeirShpilraien Dec 1, 2022
c05e7d6
Set fixed python version to 3.10 on mac.
MeirShpilraien Dec 1, 2022
d00b1e8
Added libmr derive crate.
MeirShpilraien Dec 1, 2022
3642922
verbose logs on valgrind run
MeirShpilraien Dec 1, 2022
091cfaa
Decrease tests verbosity.
MeirShpilraien Dec 1, 2022
d11f43d
Merge branch 'master' into refactoring_rust_api
MeirShpilraien Dec 1, 2022
ae766a5
Added linkme to auto creation of register objects.
MeirShpilraien Dec 5, 2022
50dd24a
Small fixes to LibMR derive.
MeirShpilraien Dec 5, 2022
e45b8f1
Merge branch 'master' into refactoring_rust_api
MeirShpilraien Dec 11, 2022
ddd087c
Allow specifying custom OPENSSL_PREFIX for the project.
iddm Feb 22, 2023
278af8a
Merge pull request #36 from RedisGears/fix-openssl-on-macos
iddm Feb 22, 2023
dfb05cd
Specify PKG_CONFIG_PATH for libevent's configure script.
iddm Feb 23, 2023
0777afe
Merge pull request #37 from RedisGears/fix-openssl-on-macos
iddm Feb 23, 2023
546715b
Fix the openssl path for the macOS builds.
iddm Mar 3, 2023
3cab245
Merge pull request #38 from RedisGears/fix-openssl-on-macos
iddm Mar 6, 2023
fc6ff81
Change the timeout message text.
iddm Mar 6, 2023
c0c66e0
Merge pull request #39 from RedisGears/change-the-timeout-message-text
iddm Mar 6, 2023
86fa62c
Force the CI to work with the refactoring_rust_api branch.
iddm Mar 6, 2023
66db2ca
Merge pull request #40 from RedisGears/force-ci-to-work-with-the-refa…
iddm Mar 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Test ubuntu latest

on:
push:
branches: [ master, refactoring_rust_api ]
pull_request:
branches: [ master, refactoring_rust_api ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
test_args: ["", "-V --vg-suppressions ../leakcheck.supp"]

steps:
- uses: actions/checkout@v3
- name: Checkout submodules
run: git submodule update --init --recursive
- name: format
run: cargo fmt -- --check
- name: install rltest
run: python3 -m pip install RLTest gevent
- name: install redis
run: git clone https://github.com/redis/redis; cd redis; git checkout 7.0.5; BUILD_TLS=yes make valgrind install
- name: install valgrind
run: sudo apt-get install valgrind
- name: Build
run: |
cd tests/mr_test_module/
cargo build --verbose
- name: Run tests
run: |
cd tests/mr_test_module/pytests/
DEBUG=1 ./run_full_tests.sh ${{ matrix.test_args }}
42 changes: 42 additions & 0 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Test Macos

on:
push:
branches: [ master, refactoring_rust_api ]
pull_request:
branches: [ master, refactoring_rust_api ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: macos-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Checkout submodules
run: git submodule update --init --recursive
- name: format
run: cargo fmt -- --check
- name: install rltest
run: python3 -m pip install RLTest gevent
- name: install redis
run: git clone https://github.com/redis/redis; cd redis; git checkout 7.0.5; BUILD_TLS=yes make install
- name: install automake
run: brew install automake
- name: install openssl
run: brew install [email protected]
- name: Build
run: |
cd tests/mr_test_module/
export PKG_CONFIG_PATH=/usr/local/opt/[email protected]/lib/pkgconfig/
cargo build --verbose
- name: Run tests
run: |
cd tests/mr_test_module/pytests/
DEBUG=1 ./run_full_tests.sh
23 changes: 23 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "lib_mr"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
#redis-module = { version="0.22.0", features = ["experimental-api"]}
redis-module = { git = "https://github.com/RedisLabsModules/redismodule-rs", branch = "api_extentions", features = ["experimental-api"]}
serde_json = "1.0"
serde = "1.0"
serde_derive = "1.0"
libc = "0.2"
linkme = "0.3"

[build-dependencies]
bindgen = "0.59.2"

[lib]
crate-type = ["rlib"]
name = "mr"
path = "rust_api/lib.rs"
15 changes: 15 additions & 0 deletions LibMRDerive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "lib_mr_derive"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
syn = "1.0"
quote = "1.0"

[lib]
name = "mr_derive"
path = "src/lib.rs"
proc-macro = true
28 changes: 28 additions & 0 deletions LibMRDerive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use quote::format_ident;
use syn;

#[proc_macro_derive(BaseObject)]
pub fn base_object_derive(item: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(item).unwrap();
let name = &ast.ident;

let func_name = format_ident!("register_{}", name.to_string().to_lowercase());

let gen = quote! {
impl mr::libmr::base_object::BaseObject for #name {
fn get_name() -> &'static str {
concat!(stringify!(#name), "\0")
}
}

#[linkme::distributed_slice(mr::libmr::REGISTER_LIST)]
fn #func_name() {
#name::register();
}
};

gen.into()
}
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ clean: clean_libmr

build_deps:
make -C deps/
libmr: build_deps

libmr_only:
make -C src/

libmr: build_deps libmr_only

run_tests:
make -C ./tests/mr_test_module/ test
Expand Down
79 changes: 79 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
extern crate bindgen;

use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;

fn probe_paths<'a>(paths: &'a [&'a str]) -> Option<&'a str> {
paths.iter().find(|path| Path::new(path).exists()).copied()
}

fn find_macos_openssl_prefix_path() -> &'static str {
const PATHS: [&str; 3] = [
"/usr/local/opt/openssl",
"/usr/local/opt/[email protected]",
"/opt/homebrew/opt/[email protected]",
];
probe_paths(&PATHS).unwrap_or("")
}

fn main() {
println!("cargo:rerun-if-changed=src/*.c");
println!("cargo:rerun-if-changed=src/*.h");
println!("cargo:rerun-if-changed=src/utils/*.h");
println!("cargo:rerun-if-changed=src/utils/*.c");

if !Command::new("make")
.env(
"MODULE_NAME",
std::env::var("MODULE_NAME").expect("module name was not given"),
)
.status()
.expect("failed to compile libmr")
.success()
{
panic!("failed to compile libmr");
}

let output_dir = env::var("OUT_DIR").expect("Can not find out directory");

if !Command::new("cp")
.args(["src/libmr.a", &output_dir])
.status()
.expect("failed copy libmr.a to output directory")
.success()
{
panic!("failed copy libmr.a to output directory");
}

let build = bindgen::Builder::default();

let bindings = build
.header("src/mr.h")
.size_t_is_usize(true)
.layout_tests(false)
.generate()
.expect("error generating bindings");

let out_path = PathBuf::from(&output_dir);
bindings
.write_to_file(out_path.join("libmr_bindings.rs"))
.expect("failed to write bindings to file");

let open_ssl_prefix_path = match std::option_env!("OPENSSL_PREFIX") {
Some(p) => p,
None if std::env::consts::OS == "macos" => find_macos_openssl_prefix_path(),
_ => "",
};

let open_ssl_lib_path_link_argument = if open_ssl_prefix_path.is_empty() {
"".to_owned()
} else {
format!("-L{open_ssl_prefix_path}/lib/")
};

println!(
"cargo:rustc-flags=-L{} {} -lmr -lssl -lcrypto",
output_dir, open_ssl_lib_path_link_argument
);
}
16 changes: 14 additions & 2 deletions deps/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
all: build_hiredis build_libevent

build_hiredis:
ifneq ("$(wildcard built_hiredis)","")
echo hiredis already built
else
MAKEFLAGS='' USE_SSL=1 make -C ./hiredis/
endif
touch built_hiredis

build_libevent:
cd libevent; autoreconf -v -i -f; CFLAGS=-fPIC ./configure; make

ifneq ("$(wildcard built_libevent)","")
echo libevent already built
else
cd libevent; autoreconf -v -i -f; CFLAGS=-fPIC ./configure PKG_CONFIG_PATH=$(PKG_CONFIG_PATH); make
endif
touch built_libevent

clean:
make -C ./hiredis/ clean
make -C ./libevent/ clean
rm built_libevent
rm built_hiredis
11 changes: 11 additions & 0 deletions rust_api/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Redis Ltd. 2021 - present
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
* the Server Side Public License v1 (SSPLv1).
*/

#[macro_use]
extern crate serde_derive;

pub mod libmr;
pub mod libmr_c_raw;
65 changes: 65 additions & 0 deletions rust_api/libmr/accumulator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright Redis Ltd. 2021 - present
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
* the Server Side Public License v1 (SSPLv1).
*/

use crate::libmr_c_raw::bindings::{
ExecutionCtx, MR_ExecutionCtxSetError, MR_RegisterAccumulator, Record,
};

use crate::libmr::base_object::{register, BaseObject};
use crate::libmr::record;
use crate::libmr::record::MRBaseRecord;
use crate::libmr::RustMRError;

use std::os::raw::{c_char, c_void};

use std::ptr;

pub extern "C" fn rust_accumulate<Step: AccumulateStep>(
ectx: *mut ExecutionCtx,
accumulator: *mut Record,
r: *mut Record,
args: *mut c_void,
) -> *mut Record {
let s = unsafe { &*(args as *mut Step) };
let accumulator = if accumulator.is_null() {
None
} else {
let mut accumulator =
unsafe { *Box::from_raw(accumulator as *mut MRBaseRecord<Step::Accumulator>) };
Some(accumulator.record.take().unwrap())
};
let mut r = unsafe { Box::from_raw(r as *mut MRBaseRecord<Step::InRecord>) };
let res = match s.accumulate(accumulator, r.record.take().unwrap()) {
Ok(res) => res,
Err(e) => {
unsafe { MR_ExecutionCtxSetError(ectx, e.as_ptr() as *mut c_char, e.len()) };
return ptr::null_mut();
}
};
Box::into_raw(Box::new(MRBaseRecord::new(res))) as *mut Record
}

pub trait AccumulateStep: BaseObject {
type InRecord: record::Record;
type Accumulator: record::Record;

fn accumulate(
&self,
accumulator: Option<Self::Accumulator>,
r: Self::InRecord,
) -> Result<Self::Accumulator, RustMRError>;

fn register() {
let obj = register::<Self>();
unsafe {
MR_RegisterAccumulator(
Self::get_name().as_ptr() as *mut c_char,
Some(rust_accumulate::<Self>),
obj,
);
}
}
}
Loading