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

Add dynamic load data #413

Merged
merged 14 commits into from
Apr 19, 2023
Merged

Add dynamic load data #413

merged 14 commits into from
Apr 19, 2023

Conversation

kaaboaye
Copy link
Contributor

@kaaboaye kaaboaye commented Dec 6, 2021

Makes it possible to compute load_data at the runtime.

Example use case it to provide priv path to the nif.

defmodule NIF do
  use Rustler, load_data_fun: {Deployment, :nif_data}
end

defmodule Deployment do
  def nif_data do
    :code.priv_dir(:surfer_local) |> IO.iodata_to_binary()
  end
end

Also added explicit errors to indicate that user provided invalid config.

image
image

@hansihe
Copy link
Member

hansihe commented Dec 7, 2021

I like the idea here, but I think dynamic load_data should be a separate argument, something like load_data_fun?

As is, this will break existing load_datas that are atom pairs.

@kaaboaye
Copy link
Contributor Author

kaaboaye commented Dec 8, 2021

Sure, I'll update it in the free time and ping you

@kaaboaye
Copy link
Contributor Author

@hansihe refactored and tested on my project

@evnu
Copy link
Member

evnu commented Nov 9, 2022

@kaaboaye do you think you could add a test for this? In rustler_test, any NIF could also load some data with that function. We could then test that this data is available.

EDIT: Also, I fixed clippy on master, so rebasing should fix clippy problems here as well I think.

@kaaboaye
Copy link
Contributor Author

kaaboaye commented Nov 9, 2022

I've added test making use of dynamic load.

However, is this output with a couple of panics is expected?

image

@evnu
Copy link
Member

evnu commented Nov 9, 2022

Thanks for the tests! Yes, the output is expected.

@evnu
Copy link
Member

evnu commented Nov 10, 2022

The pipeline complains on windows:

 error[E0433]: failed to resolve: could not find `unix` in `os`
 --> rustler_tests\native\dynamic_load\src/lib.rs:2:47
Warning:   |
2 | use std::{ffi::OsStr, fs::read_to_string, os::unix::prelude::OsStrExt, path::PathBuf};
  |                                               ^^^^ could not find `unix` in `os`

error[E0599]: no function or associated item named `from_bytes` found for struct `OsStr` in the current scope
  --> rustler_tests\native\dynamic_load\src/lib.rs:32:28
Warning:    |
32 |     let priv_path = OsStr::from_bytes(priv_path);
   |                            ^^^^^^^^^^ function or associated item not found in `OsStr`

@kaaboaye
Copy link
Contributor Author

I can fix this test for Windows but I could figure out some other case which doesn’t rely on platform specific API.

Windows fix would work as an example usage of this feature with real world use case.

but it would also mean that when adding support to other platforms this test could fail again

Should I fix it for windows or figure out something different?

@evnu
Copy link
Member

evnu commented Nov 10, 2022

Should I fix it for windows or figure out something different?

I think fixing it for windows is sufficient. We have runners for linux, macos and windows, it suffices to only make them work for now.

@kaaboaye
Copy link
Contributor Author

Looking into the issue of initialization I've realized that those modules can be also unloaded while updating. erl_nif exposes unload function for that purpose. It's mainly development issue. Hot module reloads will leak the DATASET unless upgrade and/or unload is provided.

So maybe instead of making sure that the DATASET is initialized only once we should make sure that upgrades are properly handled?

Does anyone here have any experience with how upgrades work? Does the library get new "static" memory space or is it reusing the old memory?

@evnu
Copy link
Member

evnu commented Nov 15, 2022

Does anyone here have any experience with how upgrades work? Does the library get new "static" memory space or is it reusing the old memory?

We have a long-standing issue w.r.t. upgrades: #13. Currently, upgrading is not allowed.

So maybe instead of making sure that the DATASET is initialized only once we should make sure that upgrades are properly handled?

As upgrades are not supported right now, we can probably revisit that part later on. But maybe we need to figure out how to handle unload. I think this is probably not called usually, but only if an upgrade will happen?

@kaaboaye
Copy link
Contributor Author

kaaboaye commented Nov 17, 2022

But maybe we need to figure out how to handle unload.

I think unsafe { DATASET = None } should be enough. Rust should drop Some(dataset) after that. Assuming that OTP will wait with unload until all pending function calls return.

@evnu
Copy link
Member

evnu commented Nov 22, 2022

Ok, so only the test on windows is left here I think.

@kaaboaye
Copy link
Contributor Author

Added windows support, all tests are green on windows now.

@evnu
Copy link
Member

evnu commented Mar 28, 2023

@kaaboaye thanks for all your work! Can you try to rebase one more time on master? We had some changes to the CI, which should make the tests run more reliably. With that, I think we can then merge here.

ishitatsuyuki and others added 6 commits March 29, 2023 12:50
`Some(load)` has been replaced with `load = load`.

Also add a stub for showing how `load` should look like.
According to https://github.com/foresterre/cargo-msrv, the current MSRV
is 1.56.1. Clippy needs to obey this, as new lints may not be available
to old versions of Rust.
@kaaboaye
Copy link
Contributor Author

Done

@evnu
Copy link
Member

evnu commented Mar 29, 2023

Seems that there is a merge conflict, can you resolve that?

@evnu
Copy link
Member

evnu commented Apr 19, 2023

@philss resolved the merge conflict, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants