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

geyser: allow custom name in config file #33550

Merged
merged 7 commits into from
Dec 5, 2023

Conversation

fanatid
Copy link
Contributor

@fanatid fanatid commented Oct 5, 2023

Problem

Not possible to use the same plugin twice. If we want to use the same plugin with different configs we receive an error, for example rpcpool/solana-accountsdb-plugin-kafka#23

Summary of Changes

Allow optional libname field in plugin config.

cc @mohsen-vybenetwork @Lusitaniae

Q: do we have some docs in the repo which should be changed?
Q2: should we put Library into LoadedGeyserPlugin? I think it should be easier to manage loading/unloading

When PR will be merged I'll work on backport to 1.16

};

#[derive(Debug)]
pub struct LoadedGeyserPlugin {
name: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe libname? I do not see how this utilized to solve the problem. We intentionally request the plugin has different names so that load/unload can work reliably. Why cannot you give different names to different plugins?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why cannot you give different names to different plugins?

because for a new name you need to re-compile the whole plugin with hardcoded name in the code 🙂

fn name(&self) -> &'static str;

Copy link
Contributor

@lijunwangs lijunwangs Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this problem can be solved by the plugin itself without the framework's involvement. The &static reference makes it a little tricky but not insurmountable. Here is some sample code to the effect:

use std::fs;
use std::sync::{Mutex, Once};

use std::collections::HashMap;

struct Plugin {
    name: Mutex<HashMap<i32, &'static str>>,
    initialized: Once,
}

impl Plugin {
    pub fn new() -> Self {
        Self {
            name: Mutex::new(HashMap::default()),
            initialized: Once::new(),
        }
    }

    pub fn load(&mut self, file: String) {
        self.initialized.call_once(|| {
            let file_content: String = fs::read_to_string(file).unwrap();
            let s = Box::new(file_content);
            let s = Box::leak(s); // it leaks once and only once, so still safe!
            self.name.lock().unwrap().insert(1, s);
        })
    }

    fn name(&self) -> &'static str {
        let data = self.name.lock().unwrap();
        let data = data.get(&1).unwrap();
        data
    }
}

fn main() {
    let mut s = Plugin::new();
    s.load("config.txt".to_string());
    println!("This is my content {}", s.name());
}

This way, you don't have to hard code the plugin name in the code and load it from the config file yourself.

Copy link
Contributor Author

@fanatid fanatid Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, nice workaround
I think name() happens before load() so call_once should be moved in this case

while this code probably would work I still think that supporting libname through config would be nice
for plugins users, it should be much easier to add one line to config instead of patching the plugin with the proposed code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, confirmed
on_load is called after name, so we can not take the name from the config file in the plugin

if self
.plugins
.iter()
.any(|plugin| plugin.name().eq(new_plugin.name()))
{
return Err(jsonrpc_core::Error {
code: ErrorCode::InvalidRequest,
message: format!(
"There already exists a plugin named {} loaded. Did not load requested plugin",
new_plugin.name()
),
data: None,
});
}
// Call on_load and push plugin
new_plugin
.on_load(new_config_file)
.map_err(|on_load_err| jsonrpc_core::Error {

@lijunwangs lijunwangs added the CI Pull Request is ready to enter CI label Oct 9, 2023
@solana-grimes solana-grimes removed the CI Pull Request is ready to enter CI label Oct 9, 2023
@@ -307,6 +343,8 @@ pub(crate) fn load_plugin_from_config(
libpath = config_dir.join(libpath);
}

let libname = result["libname"].as_str().map(|s| s.to_owned());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make it optional now. If it is set in the config file, use it, otherwise old behavior to keep it backward compatible. I would like to still call it "name" instead of libname, we do not care about the libname. The lib path has the libname anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed libname to name. Value is already optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lijunwangs ping :)

@github-actions github-actions bot added the stale [bot only] Added to stale content; results in auto-close after a week. label Oct 26, 2023
@github-actions github-actions bot closed this Nov 3, 2023
@lijunwangs lijunwangs reopened this Nov 3, 2023
@lijunwangs lijunwangs added the CI Pull Request is ready to enter CI label Nov 6, 2023
@solana-grimes solana-grimes removed the CI Pull Request is ready to enter CI label Nov 6, 2023
@lijunwangs lijunwangs added the CI Pull Request is ready to enter CI label Nov 6, 2023
@solana-grimes solana-grimes removed the CI Pull Request is ready to enter CI label Nov 6, 2023
@github-actions github-actions bot removed the stale [bot only] Added to stale content; results in auto-close after a week. label Nov 6, 2023
Copy link

codecov bot commented Nov 10, 2023

Codecov Report

Merging #33550 (d9932ea) into master (8fbe033) will increase coverage by 0.0%.
Report is 13 commits behind head on master.
The diff coverage is 58.3%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #33550   +/-   ##
=======================================
  Coverage    81.9%    81.9%           
=======================================
  Files         819      819           
  Lines      220084   220103   +19     
=======================================
+ Hits       180347   180420   +73     
+ Misses      39737    39683   -54     

@github-actions github-actions bot added the stale [bot only] Added to stale content; results in auto-close after a week. label Nov 27, 2023
@github-actions github-actions bot closed this Dec 4, 2023
@lijunwangs lijunwangs reopened this Dec 5, 2023
Copy link
Contributor

@lijunwangs lijunwangs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks

@lijunwangs
Copy link
Contributor

@fanatid Looks like there are some merge issues.

Copy link
Contributor

@lijunwangs lijunwangs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to address merge issues

@lijunwangs lijunwangs added the CI Pull Request is ready to enter CI label Dec 5, 2023
@solana-grimes solana-grimes removed the CI Pull Request is ready to enter CI label Dec 5, 2023
@lijunwangs lijunwangs removed the stale [bot only] Added to stale content; results in auto-close after a week. label Dec 5, 2023
@fanatid
Copy link
Contributor Author

fanatid commented Dec 5, 2023

Updated

Copy link
Contributor

@lijunwangs lijunwangs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you

@lijunwangs lijunwangs merged commit c82fc6c into solana-labs:master Dec 5, 2023
36 checks passed
@fanatid fanatid deleted the geyser-multiple-plugins branch December 5, 2023 23:26
@lijunwangs lijunwangs added the v1.17 PRs that should be backported to v1.17 label Jan 5, 2024
Copy link
Contributor

mergify bot commented Jan 5, 2024

Backports to the beta branch are to be avoided unless absolutely necessary for fixing bugs, security issues, and perf regressions. Changes intended for backport should be structured such that a minimum effective diff can be committed separately from any refactoring, plumbing, cleanup, etc that are not strictly necessary to achieve the goal. Any of the latter should go only into master and ride the normal stabilization schedule. Exceptions include CI/metrics changes, CLI improvements and documentation updates on a case by case basis.

mergify bot pushed a commit that referenced this pull request Jan 5, 2024
Allow loading the plugin name from the json config file as opposed to use plugin.name which is called before config file is passed to it. Allowing different plugins using the same executable to use different names.

(cherry picked from commit c82fc6c)

# Conflicts:
#	geyser-plugin-manager/src/geyser_plugin_manager.rs
lijunwangs added a commit that referenced this pull request Jan 20, 2024
…34669)

* geyser: allow custom name in config file (#33550)

Allow loading the plugin name from the json config file as opposed to use plugin.name which is called before config file is passed to it. Allowing different plugins using the same executable to use different names.

(cherry picked from commit c82fc6c)

# Conflicts:
#	geyser-plugin-manager/src/geyser_plugin_manager.rs

* Fixed merge conflicts

---------

Co-authored-by: Kirill Fomichev <fanatid@ya.ru>
Co-authored-by: Lijun Wang <83639177+lijunwangs@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Community contribution need:merge-assist v1.17 PRs that should be backported to v1.17
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants