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

Fix scenario tests with fragment input validation #7159

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from

Conversation

Elmacioro
Copy link
Contributor

@Elmacioro Elmacioro commented Nov 15, 2024

Describe your changes

Currently when we try to test a scenario which uses a fragment which has some validation set on parameter's expression there is an exception during serialization.

12:47:28.764 [nussknacker-designer-akka.actor.default-dispatcher-10] ERROR p.t.n.ui.api.NuDesignerErrorToHttp$ - Unknown error: The implementation of the RichFlatMapFunction is not serializable. The object probably contains or references non serializable fields.
org.apache.flink.api.common.InvalidProgramException: The implementation of the RichFlatMapFunction is not serializable. The object probably contains or references non serializable fields.
        at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:170)
        at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:69)
        at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.clean(StreamExecutionEnvironment.java:2360)
        at org.apache.flink.streaming.api.datastream.DataStream.clean(DataStream.java:202)
        at org.apache.flink.streaming.api.datastream.DataStream.flatMap(DataStream.java:631)
        at pl.touk.nussknacker.engine.process.registrar.FlinkProcessRegistrar.registerInterpretationPart$1(FlinkProcessRegistrar.scala:389)
        at pl.touk.nussknacker.engine.process.registrar.FlinkProcessRegistrar.registerSourcePart$1(FlinkProcessRegistrar.scala:189)
        at pl.touk.nussknacker.engine.process.registrar.FlinkProcessRegistrar.$anonfun$register$5(FlinkProcessRegistrar.scala:174)
        at scala.collection.LinearSeqOps.foldLeft(LinearSeq.scala:183)
...

This happens as deep down there is a classloader passed which cannot be serialized. The problem does not occur when there is a fragment without validation as variableEncoder is not passed and is therefore null which we can see on the right side of a screenshot. On the left side we can see the problematic situation with classloader

Zrzut ekranu z 2024-11-13 11-01-49

I added a test replicating the issue. It passes with the proposed change but would fail in the same way as on the environment without it:

Zrzut ekranu z 2024-11-18 14-45-40

In the proposed change i just add @transient to the problematic classloader as it's not needed during test execution. I'm not putting that annotation on the whole encoder as it seems that it's needed

Checklist before merge

  • Related issue ID is placed at the beginning of PR title in [brackets] (can be GH issue or Nu Jira issue)
  • Code is cleaned from temporary changes and commented out lines
  • Parts of the code that are not easy to understand are documented in the code
  • Changes are covered by automated tests
  • Showcase in dev-application.conf added to demonstrate the feature
  • Documentation added or updated
  • Added entry in Changelog.md describing the change from the perspective of a public distribution user
  • Added MigrationGuide.md entry in the appropriate subcategory if introducing a breaking change
  • Verify that PR will be squashed during merge

Summary by CodeRabbit

  • New Features

    • Enhanced testing capabilities for process fragments with parameter validation.
    • Added a new test case to ensure no exceptions are thrown during execution of validated fragments.
  • Bug Fixes

    • Updated ToJsonEncoder to prevent serialization of classLoader, improving handling in distributed contexts.

Copy link

coderabbitai bot commented Nov 15, 2024

Walkthrough

The changes in this pull request involve enhancements to the testing capabilities in the FlinkTestMainSpec.scala file, focusing on scenarios with fragments and parameter validation. A new test case ensures that processes with parameter validation do not throw exceptions during execution. Additionally, the ToJsonEncoder class in the utils package has been modified by adding the @transient annotation to the classLoader parameter, which prevents it from being serialized. These modifications improve the testing framework and serialization behavior.

Changes

File Change Summary
engine/flink/executor/src/test/scala/pl/touk/nussknacker/engine/process/runner/FlinkTestMainSpec.scala - Added imports for FragmentSpecificData, MetaData, FragmentInputDefinition, FragmentOutputDefinition, and ParameterValueCompileTimeValidation.
- Introduced a new test case to verify that a process fragment with parameter validation does not throw exceptions.
- Updated FlinkTestMainSpec with definitions for fragmentWithValidationName and processWithFragmentParameterValidation.
utils/utils/src/main/scala/pl/touk/nussknacker/engine/util/json/ToJsonEncoder.scala - Added @transient annotation to the classLoader parameter in ToJsonEncoder case class.

Sequence Diagram(s)

sequenceDiagram
    participant Test as FlinkTestMainSpec
    participant Fragment as Fragment
    participant Process as ProcessWithValidation

    Test->>Fragment: Setup fragment with validation
    Fragment->>Process: Validate parameters
    Process-->>Test: No exceptions thrown
Loading

🐰 "In the code we find delight,
With fragments and tests taking flight.
A loader now transient,
No more serialization incident,
Hopping through changes, all feels right!" 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Elmacioro Elmacioro marked this pull request as ready for review November 18, 2024 15:06
@Elmacioro Elmacioro force-pushed the fix-scenario-tests-with-fragment-input-validation branch from 8a7be19 to c7832ab Compare November 18, 2024 15:09
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
engine/flink/executor/src/test/scala/pl/touk/nussknacker/engine/process/runner/FlinkTestMainSpec.scala (2)

667-682: Enhance the test with specific assertions for fragment behavior

While the test verifies that no exceptions are thrown when a process fragment with parameter validation is defined, it would be more robust to include assertions that check the actual behavior of the fragment. This ensures that not only does the process run without exceptions, but it also produces the expected results.


739-766: Consolidate object and class to improve code organization

Defining an object FlinkTestMainSpec separately in the same file as class FlinkTestMainSpec can be confusing. Consider moving the contents of the object into the class as private members. This aligns with Scala best practices and enhances readability.

Apply this diff to move the definitions into the class:

-class FlinkTestMainSpec extends AnyWordSpec with Matchers with Inside with BeforeAndAfterEach with OptionValues {
+class FlinkTestMainSpec extends AnyWordSpec with Matchers with Inside with BeforeAndAfterEach with OptionValues {
+  private val fragmentWithValidationName = "fragmentWithValidation"
+
+  private val processWithFragmentParameterValidation: CanonicalProcess = {
+    // existing code from the object
+  }
+
   // Rest of the class code...
-}
-
-object FlinkTestMainSpec {
-  private val fragmentWithValidationName = "fragmentWithValidation"
-
-  private val processWithFragmentParameterValidation: CanonicalProcess = {
-    // existing code...
-  }
-}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5ebf989 and c7832ab.

📒 Files selected for processing (2)
  • engine/flink/executor/src/test/scala/pl/touk/nussknacker/engine/process/runner/FlinkTestMainSpec.scala (4 hunks)
  • utils/utils/src/main/scala/pl/touk/nussknacker/engine/util/json/ToJsonEncoder.scala (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • utils/utils/src/main/scala/pl/touk/nussknacker/engine/util/json/ToJsonEncoder.scala

Comment on lines +29 to +32
import pl.touk.nussknacker.engine.process.runner.FlinkTestMainSpec.{
fragmentWithValidationName,
processWithFragmentParameterValidation
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid unnecessary self-imports within the same file

Importing FlinkTestMainSpec.{fragmentWithValidationName, processWithFragmentParameterValidation} in the same file where they are defined can lead to confusion and is unnecessary. You can access these members directly since they are in the same scope.

Apply this diff to remove the unnecessary import:

-import pl.touk.nussknacker.engine.process.runner.FlinkTestMainSpec.{
-  fragmentWithValidationName,
-  processWithFragmentParameterValidation
-}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import pl.touk.nussknacker.engine.process.runner.FlinkTestMainSpec.{
fragmentWithValidationName,
processWithFragmentParameterValidation
}

Comment on lines +748 to +750
validationExpression = Expression.spel("true"),
validationFailedMessage = Some("param validation failed")
)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Provide a meaningful validationExpression for effective validation

The validationExpression is currently set to Expression.spel("true"), which means it will always pass validation. To properly test the parameter validation mechanism, consider using an expression that actually evaluates the parameter value. For example, you can check if the parameter is not empty or meets certain criteria.

Apply this diff to use a meaningful validation expression:

           valueCompileTimeValidation = Some(
             ParameterValueCompileTimeValidation(
-              validationExpression = Expression.spel("true"),
+              validationExpression = Expression.spel("#param != null and #param != ''"),
               validationFailedMessage = Some("param validation failed")
             )
           )

Committable suggestion skipped: line range outside the PR's diff.

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.

1 participant