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: locus DTOs not handled if an earlier one causes an error #3982

Merged
merged 3 commits into from
Nov 18, 2024

Conversation

marcin-bazyl
Copy link
Collaborator

@marcin-bazyl marcin-bazyl commented Nov 18, 2024

COMPLETES #SPARK-559101

This pull request addresses

When an error is thrown while processing some Locus DTO, SDK gets stuck and will not process any further updates from Locus.

by making the following changes

Catching the error in the right place, so that the LocusInfo parser state is updated and doesn't get stuck on "WORKING" preventing any further locus DTOs from being processed.

Also fixed an error log in mercury, because it was not really showing the error that was caught - intead it would show: error={}

Change Type

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Tooling change
  • Internal code refactor

The following scenarios where tested

unit tests, manual test with web app

I certified that

  • I have read and followed contributing guidelines

  • I discussed changes with code owners prior to submitting this pull request

  • I have not skipped any automated checks

  • All existing and new tests passed

  • I have updated the documentation accordingly


Make sure to have followed the contributing guidelines before submitting.

Summary by CodeRabbit

  • New Features

    • Enhanced error handling in the Mercury connection and event processing, improving overall robustness.
    • Added a new test case to verify error resilience in Locus delta event processing.
  • Bug Fixes

    • Improved logging clarity for connection and disconnection processes, aiding in debugging.
  • Documentation

    • Deprecated methods listen() and stopListening() in favor of connect() and disconnect(), respectively.

@marcin-bazyl marcin-bazyl requested review from a team as code owners November 18, 2024 13:50
Copy link

coderabbitai bot commented Nov 18, 2024

Walkthrough

The pull request introduces several modifications across the Mercury class and Parser class, focusing on enhancing error handling, logging clarity, and connection management. In mercury.js, improvements include more detailed error messages, robust connection handling, and better management of WebSocket closure codes. The locus-info/parser.ts file sees the addition of error handling in the processDeltaEvent method. Additionally, a new test case is added to ensure the system can handle errors without disrupting subsequent event processing.

Changes

File Path Change Summary
packages/@webex/internal-plugin-mercury/src/mercury.js - Updated error handling in _emit method for clearer logging.
- Modified disconnect method for promise resolution.
- Enhanced connect method for connection handling.
- Improved logic in _onclose method for WebSocket closure codes.
- Deprecated listen() and stopListening() methods.
packages/@webex/plugin-meetings/src/locus-info/parser.ts - Added error handling in processDeltaEvent method to catch and log errors from onDeltaAction.
packages/@webex/plugin-meetings/test/unit/spec/locus-info/index.js - Introduced a new test for error handling in LocusDeltaEvents to ensure subsequent delta events are processed despite errors.

Possibly related PRs

Suggested reviewers

  • antsukanova
  • edvujic
  • szotrabh
  • SomeBody16

🐰 In the land of code where rabbits play,
Improvements hop in a bright new way.
With clearer logs and errors caught,
The Mercury class is better, we thought!
Delta events now dance with glee,
As we code our dreams, just wait and see! 🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

warning eslint@8.57.1: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/config-array@0.13.0: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/object-schema@2.0.3: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning glob@7.2.3: Glob versions prior to v9 are no longer supported
warning intern > glob@7.1.7: Glob versions prior to v9 are no longer supported
warning intern > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning jasmine > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning jest > jest-cli > jest-config > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > jest-runtime > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > @jest/reporters > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning jest > @jest/core > @jest/transform > babel-plugin-istanbul > test-exclude > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning mocha > glob@7.2.0: Glob versions prior to v9 are no longer supported
warning mocha > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning sinon@9.2.4: 16.1.1
warning wd > q@1.5.1: You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScript promise now. Thank you literally everyone for joining me in this bet against the odds. Be excellent to each other.

(For a CapTP with native promises, see @endo/eventual-send and @endo/captp)
warning wd > request@2.88.0: request has been deprecated, see request/request#3142
warning wd > archiver > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning wd > request > uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
warning wd > request > har-validator@5.1.5: this library is no longer supported
warning wd > archiver > archiver-utils > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning @babel/cli > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning @babel/plugin-proposal-async-generator-functions@7.20.7: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-async-generator-functions instead.
warning @babel/plugin-proposal-class-properties@7.18.6: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-class-properties instead.
warning @babel/plugin-proposal-export-namespace-from@7.18.9: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-export-namespace-from instead.
warning @babel/plugin-proposal-nullish-coalescing-operator@7.18.6: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-nullish-coalescing-operator instead.
warning @babel/plugin-proposal-object-rest-spread@7.20.7: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-object-rest-spread instead.
warning @babel/plugin-proposal-optional-chaining@7.21.0: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-optional-chaining instead.
warning @babel/polyfill@7.12.1: 🚨 This package has been deprecated in favor of separate inclusion of a polyfill and regenerator-runtime (when needed). See the @babel/polyfill docs (https://babeljs.io/docs/en/babel-polyfill) for more information.
warning @babel/polyfill > core-js@2.6.12: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.
warning @babel/runtime-corejs2 > core-js@2.6.12: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.
warning babel-plugin-lodash > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > eslint@8.57.1: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > sinon@9.2.4: 16.1.1
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > @babel/plugin-proposal-async-generator-functions@7.20.7: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-async-generator-functions instead.
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > @babel/plugin-proposal-class-properties@7.18.6: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-class-properties instead.
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > @babel/plugin-proposal-export-namespace-from@7.18.9: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-export-namespace-from instead.
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > @babel/plugin-proposal-nullish-coalescing-operator@7.18.6: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-nullish-coalescing-operator instead.
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > @babel/plugin-proposal-object-rest-spread@7.20.7: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-object-rest-spread instead.
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > @babel/plugin-proposal-optional-chaining@7.21.0: This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-optional-chaining instead.
warning workspace-aggregator-1b51e89c-af03-413d-964c-00a94e50d76c > @babel/polyfill@7.12.1: 🚨 This package has been deprecated in favor of separate inclusion of a polyfill and regenerator-runtime (when needed). See the @babel/polyfill docs (https://babeljs.io/docs/en/babel-polyfill) for more information.
error eslint-plugin-jsdoc@40.3.0: The engine "node" is incompatible with this module. Expected version "^14 || ^16 || ^17 || ^18 || ^19". Got "22.9.0"
error Found incompatible module.


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.

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: 1

🧹 Outside diff range and nitpick comments (6)
packages/@webex/plugin-meetings/src/locus-info/parser.ts (3)

774-781: Enhance error handling with metrics and better error reporting.

The addition of error handling is good and aligns with the PR objectives. However, consider these improvements:

Apply this diff to enhance the error handling:

       try {
         this.onDeltaAction(lociComparison, newLoci);
       } catch (error) {
+        // Send metric to track error frequency
+        Metrics.sendBehavioralMetric(BEHAVIORAL_METRICS.LOCUS_DELTA_ACTION_ERROR, {
+          error: error.message,
+          comparison: lociComparison,
+          stack: error.stack
+        });
+
         LoggerProxy.logger.error(
-          'Locus-info:parser#processDeltaEvent --> Error in onDeltaAction',
+          'Locus-info:parser#processDeltaEvent --> Error processing delta action',
+          {
+            error,
+            comparison: lociComparison,
+            delta: Parser.locus2string(newLoci)
+          }
         );
+
+        // Emit error event for higher-level error handling
+        this.emit('error', {
+          type: 'DELTA_ACTION_ERROR',
+          error,
+          comparison: lociComparison,
+          delta: newLoci
+        });
       }

These changes will:

  1. Track error frequency using metrics
  2. Provide more context in error logs
  3. Allow higher-level components to handle errors through events

Line range hint 632-782: Consider refactoring processDeltaEvent for better maintainability.

The method is quite long and handles multiple responsibilities. Consider breaking it down into smaller, more focused methods.

Here are some suggestions for improvement:

  1. Extract the locus comparison logic into a separate method:
private handleLocusComparison(lociComparison: string, newLoci: LocusDeltaDto): boolean {
  const {DESYNC, USE_INCOMING, WAIT, LOCUS_URL_CHANGED} = Parser.loci;
  
  switch (lociComparison) {
    case DESYNC:
      this.pause();
      return false;
    case USE_INCOMING:
    case LOCUS_URL_CHANGED:
      this.workingCopy = newLoci;
      return false;
    case WAIT:
      this.queue.enqueue(newLoci);
      return true;
    default:
      return false;
  }
}
  1. Extract the wait state handling into a separate method:
private handleWaitState(needToWait: boolean, newLoci: LocusDeltaDto): void {
  if (needToWait) {
    this.status = 'BLOCKED';
    this.startSyncTimer();
  } else {
    this.stopSyncTimer();
    if (this.status === 'BLOCKED') {
      this.status = 'WORKING';
      LoggerProxy.logger.info(
        `Locus-info:parser#processDeltaEvent --> received delta that we were waiting for ${Parser.locus2string(
          newLoci
        )}, not blocked anymore`
      );
    }
  }
}
  1. Add stronger typing for the parser status:
type ParserStatus = 'IDLE' | 'PAUSED' | 'WORKING' | 'BLOCKED';

These refactoring suggestions would make the code more maintainable and easier to test.


Line range hint 509-524: Improve error handling for invalid locus objects.

The current error handling for invalid locus objects could be enhanced with better error reporting and metrics.

Apply this diff to improve the error handling:

   isValidLocus(newLoci) {
     let isValid = false;
     const {isLoci} = Parser;
 
     // one or both objects are not locus delta events
     if (!isLoci(this.workingCopy) || !isLoci(newLoci)) {
+      const reason = !isLoci(this.workingCopy) ? 'invalid working copy' : 'invalid new locus';
+      Metrics.sendBehavioralMetric(BEHAVIORAL_METRICS.INVALID_LOCUS_OBJECT, {
+        reason,
+        stack: new Error().stack
+      });
       LoggerProxy.logger.info(
-        'Locus-info:parser#processDeltaEvent --> Ignoring non-locus object. workingCopy:',
-        this.workingCopy,
-        'newLoci:',
-        newLoci
+        'Locus-info:parser#processDeltaEvent --> Ignoring non-locus object',
+        {
+          reason,
+          workingCopy: this.workingCopy,
+          newLoci
+        }
       );
     } else {
       isValid = true;
     }
 
     return isValid;
   }

This change will:

  1. Track invalid locus objects with metrics
  2. Provide better context about which object is invalid
  3. Structure the log message better
packages/@webex/plugin-meetings/test/unit/spec/locus-info/index.js (1)

1810-1837: Test case looks good but could be enhanced.

The test case correctly verifies that the system can handle subsequent Locus delta events after an error, which aligns with the PR objectives. However, consider enhancing it with additional assertions and error scenarios.

Consider adding:

  1. Assertions to verify error handling:
 locusInfo.handleLocusDelta(fakeLocusDelta, testMeeting);
+assert.throws(() => locusParser.onDeltaAction(), Error, 'fake error');

 // simulate a second locus delta coming - it should be processed without errors
 locusInfo.handleLocusDelta(fakeLocusDelta, testMeeting);
+assert.calledTwice(locusParser.onDeltaAction);
  1. Additional test cases for different error scenarios:
it('handles different types of errors during delta processing', () => {
  // Test TypeError
  locusParser.onDeltaAction = sandbox.stub()
    .onCall(0)
    .throws(new TypeError('type error'));
  
  // Test RangeError
  locusParser.onDeltaAction = sandbox.stub()
    .onCall(0)
    .throws(new RangeError('range error'));
});

it('handles async errors during delta processing', async () => {
  locusParser.onDeltaAction = sandbox.stub()
    .onCall(0)
    .rejects(new Error('async error'));
});
packages/@webex/internal-plugin-mercury/src/mercury.js (2)

Line range hint 219-219: Storing the last connection error for diagnostics

Setting this.lastError in the _attemptConnection method allows retrieval of the most recent connection error via getLastError(). This is helpful for debugging and monitoring connection issues. Ensure that lastError is cleared or updated appropriately to reflect the current state.


Line range hint 479-481: Consistent use of the reason variable in logging

In the _onclose method, consider using the reason variable instead of event.reason in the log message for consistency, since reason has been processed (e.g., transformed to lowercase). This ensures the logged reason matches the one used in the reconnection logic.

Apply this diff to address the inconsistency:

 this.logger.info(
-  `${this.namespace}: socket disconnected; will not reconnect: ${event.reason}`
+  `${this.namespace}: socket disconnected; will not reconnect: ${reason}`
 );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9cc28e7 and 30038d0.

📒 Files selected for processing (3)
  • packages/@webex/internal-plugin-mercury/src/mercury.js (1 hunks)
  • packages/@webex/plugin-meetings/src/locus-info/parser.ts (1 hunks)
  • packages/@webex/plugin-meetings/test/unit/spec/locus-info/index.js (1 hunks)
🔇 Additional comments (3)
packages/@webex/plugin-meetings/test/unit/spec/locus-info/index.js (1)

Line range hint 1-100: Well-structured test file with good organization.

The test file follows testing best practices with:

  • Clear test organization using describe blocks
  • Consistent setup/teardown patterns
  • Descriptive test names
  • Good use of stubs and spies
packages/@webex/internal-plugin-mercury/src/mercury.js (2)

Line range hint 45-57: Efficient management of concurrent connections with @oneFlight decorator

The use of the @oneFlight decorator on the connect method ensures that simultaneous calls to connect are handled gracefully, preventing multiple concurrent connection attempts. This improves the stability of the connection logic.


Line range hint 67-77: Safe handling of multiple disconnection attempts

Applying the @oneFlight decorator to the disconnect method effectively manages concurrent calls to disconnect, avoiding potential race conditions or inconsistent states during disconnection.

Copy link

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-3982.d3m3l2kee0btzx.amplifyapp.com

@marcin-bazyl marcin-bazyl added the validated If the pull request is validated for automation. label Nov 18, 2024
Copy link
Contributor

@chburket chburket 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!

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: 0

🧹 Outside diff range and nitpick comments (1)
packages/@webex/internal-plugin-mercury/test/unit/spec/mercury.js (1)

Line range hint 349-360: Avoid commenting out test cases for NotFound errors

The test case handling the NotFound error has been commented out. Commenting out tests can reduce test coverage and may lead to unhandled error scenarios. If this test is no longer applicable, consider removing it entirely. If the functionality is still required, update and re-enable the test.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 30038d0 and ced645a.

📒 Files selected for processing (1)
  • packages/@webex/internal-plugin-mercury/test/unit/spec/mercury.js (1 hunks)
🔇 Additional comments (2)
packages/@webex/internal-plugin-mercury/test/unit/spec/mercury.js (2)

Line range hint 11-11: Verify the commented-out NotFound import

The NotFound import has been commented out. Please confirm whether handling for NotFound errors is intentionally being removed. If NotFound errors are still relevant, ensure they are properly handled and tested.


709-715: Test correctly asserts enhanced error logging in _emit method

The added test ensures that mercury.logger.error is called with the expected arguments when an error occurs in an event handler, which improves debugging and log clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
validated If the pull request is validated for automation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants