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

Update server-warp to server-axum & Update server-tflite/Cargo.toml #22

Merged
merged 3 commits into from
May 27, 2024

Conversation

L-jasmine
Copy link
Contributor

No description provided.

Signed-off-by: csh <458761603@qq.com>
Signed-off-by: csh <458761603@qq.com>
Signed-off-by: csh <458761603@qq.com>
Copy link
Member

juntao commented May 8, 2024

Hello, I am a code review bot on flows.network. Here are my reviews of changed source code files in this PR.


.github/workflows/examples.yml

Overall, the source code looks fine, but here are some potential issues and recommendations:

  1. Dependency Versions:
  • The code is using specific versions of WasmEdge and related dependencies. It's essential to periodically check for updates and ensure that newer versions do not introduce breaking changes or security vulnerabilities.
  1. Error Handling:
  • The script contains some error-prone operations like wget and curl commands. It's good to add error handling and validation after these commands to ensure they executed successfully.
  1. Permissions:
  • The script is running several commands with sudo, which can be risky. Make sure these commands are necessary and secure to run with elevated privileges.
  1. No Unit Tests:
  • It's recommended to have unit tests for critical functionalities to ensure that changes don't introduce regressions.
  1. Security Concerns:
  • The script involves downloading and executing code from external sources. It's important to validate the sources to prevent security issues like code injection or malicious scripts.
  1. Kill Signal 9:
  • The use of kill -9 is often discouraged as it's a forceful termination that doesn't give processes a chance to clean up. Consider using softer termination signals or handle shutdown gracefully within your application.
  1. Curl Post Requests:
  • The script makes POST requests using curl with data directly in the command. If the data contains special characters, this approach might be prone to errors. It's better to read the data from files for more complex scenarios.
  1. Code Duplication:
  • There is duplication in the post-build validation logic across different tasks. Consider abstracting this logic into reusable functions to avoid redundancy and promote maintainability.

Make sure to address these points to enhance the reliability, security, and maintainability of the script.

The key changes in the patch are:

  • The job name has been updated from "Warp example" to "Axum example".
  • The directory server-warp has been changed to server-axum to reflect the switch from Warp to Axum.
  • The target wasm file names have been updated from wasmedge_warp_server.wasm to wasmedge_axum_server.wasm to match the Axum server implementation.
  • The commands for building and running the Axum server have been updated accordingly.

These changes indicate a shift from using the Warp server implementation to the Axum server implementation. All the relevant commands and file paths have been updated to align with this transition.

server-axum/.cargo/config.toml

The source code snippet you provided is a Cargo.toml file configuration extracted from a Rust project. From the configuration provided, the project seems to be targeting the WebAssembly System Interface (wasi) platform with the target = "wasm32-wasi" specification.

However, there are a couple of aspects that could be improved or require attention:

  1. Missing Dependencies Information: The code snippet you provided only shows the [build] section, which contains information about the project build configuration. Normally, a Cargo.toml file usually includes sections like [dependencies], [dev-dependencies], and others to specify dependencies required by the project. It's important to ensure that all necessary dependencies are properly managed and declared.

  2. Rust Flags and Configuration: The rustflags setting enables passing additional flags to the Rust compiler during the build process. In this case, --cfg flags are being used to set conditional compilation flags. It's essential to verify that these are necessary for the project and are used correctly. The usage of --cfg indicates that certain feature flags might be enabled depending on the configuration.

  3. Hardcoded Configuration: Hardcoding specific configurations directly in the Cargo.toml file can sometimes lead to maintenance issues or inflexibility in the long run. It might be better to externalize configurations that are subject to change or might vary between different environments.

As you mentioned the need for updating from server-warp to server-axum and updating server-tflite, the provided code snippet seems to be missing the information related to these dependencies and their version specifications. Ensuring that the dependencies for server-axum and server-tflite are correctly specified and updated in the Cargo.toml file will be crucial for the successful transition.

In summary, while the provided code snippet seems to be a configuration for building a Rust project targeting the wasm32-wasi platform, it lacks the details related to dependencies and the update from server-warp to server-axum, which will be essential parts of the update process. Make sure to address these aspects as part of the update to server-axum and server-tflite in the project.

The patch provided shows the addition of a [build] configuration section in the Cargo.toml file. The key changes introduced in this patch are as follows:

  • Added a target = "wasm32-wasi" directive, specifying the target platform for the project to be WebAssembly System Interface (WASI).

  • Included rustflags setting with ["--cfg", "wasmedge", "--cfg", "tokio_unstable"], which passes additional flags to the Rust compiler during the build process. These flags are used for conditional compilation and indicate that certain features might be enabled based on the specified flags.

server-axum/Cargo.toml

Here is the review of the Cargo.toml file related to the subject of "Update server-warp to server-axum & Update server-tflite/Cargo.toml":

  1. Axum Version Update:

    • The axum dependency is set to version "0.6". Make sure this version is compatible with the functionality required by the project.
    • Consider checking if there are any breaking changes or new features in the updated version that could impact the existing codebase.
  2. Dependencies:

    • bytes and futures-util dependencies are not explicitly specifying the version. Ensure that the latest compatible versions are being used to avoid potential issues with future updates.
    • The tokio dependency is set with specified features such as "rt", "macros", "net", "time", "io-util". Check if these features are necessary for the project and adjust them accordingly.
  3. Patch Crates Update:

    • The patch section updates the tokio, socket2, and hyper dependencies from specific repositories and branches. It seems to be referencing custom versions or forked versions. Ensure that these patch versions are maintained and updated correctly to align with the project's requirements.
  4. Edition:

    • The edition is set to "2021". Confirm if this edition is required for the project or if an older edition is sufficient.

Overall, ensure that the dependencies are up to date and compatible with the project's requirements. Additionally, verify the custom patches for dependencies to ensure consistent and reliable builds.

Key changes in the provided patch are as follows:

  1. Package Information:

    • Added package information specifying the name, version, and edition as "wasmedge_axum_server", "0.1.0", and "2021" respectively.
  2. Patch to Crates-io Dependencies:

    • Added patch definitions for tokio, socket2, and hyper dependencies pointing to specific repositories and branches.
  3. Dependencies:

    • Added axum dependency with version "0.6".
    • Added bytes dependency with an unspecified version.
    • Added futures-util dependency with version "0.3.30".
    • Added tokio dependency version "1" with specified features such as "rt", "macros", "net", "time", "io-util".

These changes reflect updates to package metadata, dependencies, and patch definitions for certain external crates in the project's Cargo.toml file.

server-axum/src/main.rs

The provided source code seems to be updating a server implementation from using server-warp to server-axum. Here are some potential issues and improvements that could be made:

  1. Error Handling:

    • The code uses unwrap() when binding the TcpListener and serving the application. It's better to handle potential errors more gracefully by using proper error handling techniques like match or ? operator to propagate errors up the call stack.
  2. Async Function Return Types:

    • In the async fn help() -> &'static str function, the return type should be String instead of &'static str as the response will be owned by the function. This will avoid potential lifetime issues.
  3. Use of Streams:

    • In the async fn echo function, the code assumes only one chunk of data will be received. If the intention is to echo back the entire body of the request, consider collecting all chunks until the stream completes (e.g., using stream.concat().await) to ensure correctness for larger request bodies.
  4. Comments:

    • Adding comments to explain the purpose of functions and key parts of the code, especially for those unfamiliar with the implementation details of the server, can enhance code readability and maintainability.
  5. Testing:

    • It's advisable to add unit tests or integration tests to validate the behavior of the server routes and functions.
  6. Dependency Versions:

    • Ensure that dependencies in the Cargo.toml file are up to date, including the versions of axum, tokio, futures_util, and other dependencies used in the project.

Overall, the code appears to be a good start for transitioning from server-warp to server-axum. By addressing the points mentioned above, the code can be improved in terms of error handling, correctness, and maintainability.

The provided patch file includes the following key changes:

  1. Addition of use Statements:

    • Added use statements for bytes::Bytes and futures_util::StreamExt to import the necessary dependencies for the code to function correctly.
  2. Transition to Axum:

    • Replaced the usage of warp with axum by importing axum::{extract::BodyStream, routing::get, routing::post, Router}.
  3. Server Initialization:

    • Updated the server initialization logic to use axum::Server instead of previous warp server configurations.
  4. Entry Point Function:

    • Modified the async fn main() function to use tokio::main(flavor = "current_thread") and updated the server creation logic to work with axum::Server.
  5. Request Handling Functions:

    • Transferred the help and echo request handling functions without changes, ensuring compatibility with the axum framework.

Overall, the patch primarily focuses on transitioning the server implementation from warp to axum, updating the necessary imports, and adjusting the server initialization logic and entry point function to work with the axum web framework.

server-tflite/.cargo/config.toml

The given source code snippet provides some configuration settings related to building a Rust project targeting WebAssembly with Wasmer runtime. Here are some potential problems and recommendations based on the provided information:

  1. Rust Edition: The code does not specify the Rust edition being used. It is recommended to explicitly define the Rust edition to avoid any confusion or potential compatibility issues. For example, adding edition = "2018" could specify the Rust 2018 edition.

  2. Explicit Feature Selection: The code can benefit from explicitly specifying any required features for dependency crates. Ensuring that only the necessary features are enabled helps reduce unnecessary dependencies and potentially improves the build performance.

  3. Rustflags Usage: The use of rustflags to pass additional arguments to the Rust compiler is generally fine. However, it's important to ensure that the flags provided are necessary and compatible with the target platform and desired behavior.

  4. tokio_unstable Configuration: The presence of --cfg tokio_unstable indicates that some code parts might be opting into unstable Tokio features. This could potentially introduce instability or breaking changes in future Tokio releases. It's important to continually review and adapt the code as Tokio evolves.

  5. Documentation and Comments: While not directly related to the configuration shown, it's beneficial to have clear and concise documentation or comments explaining the purpose of the configuration options to aid understanding for other developers or future maintainers.

Overall, this configuration snippet seems specific to the project's requirements for building a WebAssembly target with Wasmer and Tokio integration. Regularly reassessing these configurations as the project evolves can help in maintaining a clean and efficient build setup.

The patch introduces the following key changes to the configuration:

  1. target Setting: The target setting has been explicitly set to "wasm32-wasi", indicating a target platform of WebAssembly with the WASI (WebAssembly System Interface) runtime environment. This change ensures that the project is built specifically for a WebAssembly target supporting the WASI interface.

  2. rustflags Addition: The rustflags configuration has been updated to include ["--cfg", "wasmedge", "--cfg", "tokio_unstable"]. These flags are likely used to enable certain conditional compilation flags or feature flags within the Rust codebase. The inclusion of --cfg wasmedge and --cfg tokio_unstable suggests that specific features or configurations dependent on these conditions are being activated during the build process.

Overall, the key changes in this patch focus on targeting WebAssembly with the WASI runtime environment and enabling specific configuration flags related to Wasmedge and Tokio unstable features during the build process.

server-tflite/Cargo.toml

The source code you provided is a Cargo.toml file for a Rust project named wasmedge_hyper_server_tflite. Below are some potential issues and recommendations for improvement:

  1. Dependencies Versions:

    • It's generally a good practice to always specify the exact versions of dependencies to ensure reproducibility and avoid unexpectedly breaking changes when new versions are released. For example, pin the version of tokio and other dependencies to a specific version instead of using ranges like "1".
  2. Unnecessary Features:

    • In the tokio dependency, "rt", "macros", "net", "time", and "io-util" features are being enabled explicitly. Make sure these features are necessary for your project. If not, consider removing these features to reduce unnecessary dependencies and potential compatibility issues with future versions.
  3. Deprecated Dependencies:

    • The hyper dependency is using a git path that seems to be a custom repository (https://github.com/second-state/wasi_hyper.git). It's recommended to use the official crates from crates.io whenever possible to ensure stability and reliability. Check if there is an officially supported crate for your needs or consider if the custom repository is maintained properly.
  4. No Upgrade to server-axum:

    • The title mentions updating server-warp to server-axum, but there are no changes related to server-axum in this Cargo.toml file. Ensure that this change is applied in the necessary code files or dependencies.
  5. Review wasi_tokio and socket2 Git Repositories:

    • Since you're using custom repositories for tokio and socket2 dependencies, make sure these repositories are maintained, secure, and serve the code you expect. Regularly review these custom repositories for any updates or changes.
  6. Check for Latest Versions:

    • Regularly check for updates of the dependencies used in your project. Keeping dependencies up-to-date can help incorporate bug fixes, performance improvements, and new features provided by the library maintainers.

Make sure to address these points to enhance the maintainability, stability, and security of the project.

The key changes in the provided patch are as follows:

  1. Git Patch for Dependencies:

    • Updated the tokio, socket2, and hyper dependencies to be fetched from specific git repositories (https://github.com/second-state/wasi_tokio.git, https://github.com/second-state/socket2.git, https://github.com/second-state/wasi_hyper.git) with defined branches (v1.36.x, v0.5.x, v0.14.x respectively). This change allows for using custom versions of these dependencies.
  2. Dependency Version Changes:

    • Reverted the hyper_wasi and tokio_wasi dependencies back to hyper and tokio respectively with version 0.14 and 1. Additionally, features are specified for the tokio dependency to include "rt", "macros", "net", "time", and "io-util".
  3. No Update to server-axum:

    • There is no indication of updating server-warp to server-axum in this patch. If that change was intended, it's missing in this patch.

These changes primarily focus on adjusting dependencies to point to custom git repositories, specifying versions, and setting features accordingly. Remember to ensure that the custom repositories are reliable and provide the necessary code for your project.

server-tflite/src/main.rs

Overall, the code provided appears to be well-structured and clear. However, there are a few areas that could be potentially improved or updated:

  1. Error Handling: The code does handle errors using Result and ? for propagating errors. However, there are some sections that are commented out (marked with /* ... */) which seem to involve error handling related to the wasi_nn crate. It's essential to ensure that all error handling paths are properly implemented and not left as comments.

  2. Code Redundancy: There are sections of code that are currently commented out and replaced by updated code. For example, the section where graph is created using GraphBuilder and ExecutionTarget is now replaced by the commented out wasi_nn APIs. It's recommended to remove the obsolete code to enhance code clarity and reduce redundancy.

  3. Unused Code: The main function contains both the tokio implementation and a commented-out alternative implementation using a loop and spawning tasks. It's better to remove the unused code to avoid confusion and reduce clutter.

  4. Logging: The code uses println! for debugging purposes. Consider using a proper logging framework like log in the project to improve logging capabilities and maintainability.

  5. Cargo.toml Updates: The code references hyper, image, tokio, and wasi_nn crates. Ensure that the dependencies in the Cargo.toml file are updated with the correct versions and reflect any changes mentioned in the code.

Regarding the specific change related to updating server-warp to server-axum, the code itself does not use server-warp, so no changes related to the server-warp to server-axum update are needed in this file.

However, if there are other files or dependencies related to the web server implementation that involve server-warp, those would need to be updated accordingly. Please ensure that the migration from server-warp to server-axum is done consistently across all relevant files.

If there are additional changes or updates required in specific areas, feel free to provide more context or details for a more targeted review.

The key changes in the provided patch are as follows:

  1. Import changes:

    • The import statements for hyper::service::{make_service_fn, service_fn} and Method have been removed.
    • tokio::net::TcpListener is now imported from tokio for handling network connectivity.
  2. New Dependencies:

    • The tokio::net::TcpListener is being introduced to handle incoming network connections.
  3. Code Refactoring:

    • The code that previously used hyper::Server::bind is refactored to use tokio::net::TcpListener::bind for creating a listener for incoming network connections.
    • The server creation and binding now utilize Server::from_tcp(listener.into_std()?)? to leverage the new TcpListener.
  4. Code Cleanup:

    • The obsolete and commented-out code related to error handling for the wasi_nn crate has been removed, improving code cleanliness and readability.
  5. Code Execution:

    • The main function now awaits the binding of the TcpListener and creates the server using Server::from_tcp to handle incoming connections more efficiently.

These changes reflect a shift towards using tokio for network-related operations and enhancing the server handling logic for improved performance and clarity within the codebase.

server-warp/Cargo.toml

The initial source code snippet you provided appears to be a Cargo.toml file for a Rust project named "wasmedge_warp_server." It specifies the project's name, version, edition, and dependencies.

Here are a few observations and potential issues:

  1. Dependency Versions:

    • The bytes dependency is set to version 1 without specifying the exact version number. This may cause compatibility issues if a newer version of the bytes crate introduces breaking changes. Consider specifying a specific version or a version range to ensure predictable builds.
    • The tokio_wasi dependency is set to version 1, which is good practice for specifying versions.
    • The warp_wasi dependency is set to version 0.3, which is also good for clarity and predictability.
  2. Cargo Features:

    • The tokio_wasi dependency specifies certain features such as "rt", "macros", "net", "time", "io-util". Ensure that these features are necessary for the project and won't conflict with each other.

Overall, the Cargo.toml is structured correctly, specifying the project's dependencies and basic information. However, it's important to consider the specific versions of the dependencies, especially for production-level applications.

Now, let's move on to the patch related to updating the dependencies:

[dependencies]
bytes = "1"
tokio = { version = "1", features = ["full"] }
axum = "0.2.5"
tflite = "1.0.0"

In the patch, the following updates were made to dependencies:

  • tokio_wasi was replaced with just tokio and the features were updated to ["full"]. This change might bring additional functionality through the "full" feature set of Tokio.
  • warp_wasi was replaced with axum version 0.2.5. This can be a significant change as Axum is an alternative to Warp and might require code changes.
  • A new dependency tflite with version 1.0.0 was added. This indicates the introduction of a new dependency related to TensorFlow Lite.

Potential Issues to consider in the patch:

  • Architecture Changes: Moving from warp to axum might introduce architectural changes in the codebase. Make sure the necessary modifications in code and configuration are done to accommodate this change.
  • Feature Changes: Utilizing the "full" feature in tokio might introduce additional functionalities that need to be handled in the codebase.
  • New Dependency: Adding a new dependency tflite might increase the binary size and complexity, so ensure it's necessary for the project.

In conclusion, the patch seems to provide updates to dependencies, but these changes may introduce significant modifications and complexities to the project. It's crucial to assess the impact of these changes on the existing codebase carefully.

Key Changes in the Patch:

  • Removal of the entire [package] section specifying the project's name, version, and edition.
  • Removal of the [dependencies] section that included dependencies on bytes, tokio_wasi, and warp_wasi.

In summary, the patch removes the project metadata (name, version, edition) and dependency information related to bytes, tokio_wasi, and warp_wasi from the Cargo.toml file. This indicates a complete removal of these configurations from the file, suggesting a significant change in project setup or dependencies.

server-warp/src/main.rs

Overall, the provided source code snippet is a simple Rust application using the warp library to create HTTP routes for handling GET and POST requests. Here are some potential problems and suggestions for improvement:

  1. Error Handling: The code snippet lacks error handling mechanisms. It would be beneficial to include error handling for potential failures during request processing, such as parsing request body or handling invalid requests.

  2. Input Validation: Given that the POST endpoint /echo simply echoes back whatever is posted to it, there is no input validation or sanitization implemented. Depending on the use case, it might be crucial to add validation logic to prevent malicious input.

  3. Security Concerns: When dealing with raw bytes from the request body, such as in the echo endpoint, it's essential to consider potential security vulnerabilities like buffer overflows or injections. It's advisable to decode and handle the input data carefully.

  4. Testing: The code lacks any unit tests or integration tests to verify the functionality and behavior of the endpoints. Adding tests would help ensure the correctness of the code and catch potential regressions.

  5. Code Modularity: As the application grows, it may become more challenging to manage all routes in a single main function. Consider breaking down the routes into separate modules for better organization and maintainability.

  6. Performance Optimization: Depending on the expected load and performance requirements, it might be necessary to optimize the code further. This could involve profiling, identifying bottlenecks, and applying optimizations as needed.

Regarding the proposed changes to update from server-warp to server-axum and update server-tflite/Cargo.toml, ensure that after the update, the code still functions correctly and that any dependencies or configurations impacted by the update are adjusted accordingly.

If you have specific details on the changes made to switch to server-axum or the modifications in the Cargo.toml file related to server-tflite, please provide them for a more detailed review of the modifications.

Key changes in the patch:

  • The removal of the use warp::Filter; statement.
  • Removal of all the code inside the async main function that defined routes for handling GET and POST requests using warp filters.
  • The entire async main function has been removed, which means all route handling logic using warp has been eliminated.

The patch essentially removes the entire existing code related to handling HTTP requests with warp and tokio in the Rust application. It indicates a significant shift or restructuring in the codebase, possibly involving a change in the HTTP server framework being used or an architectural adjustment in how requests are handled.

@juntao juntao merged commit cbd7612 into WasmEdge:main May 27, 2024
2 checks passed
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.

None yet

2 participants