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 underflow possibility on ColumnFamilyDbStore #5975

Merged
merged 6 commits into from
Jan 20, 2022

Conversation

nyanzebra
Copy link
Contributor

This fixes an issue where in certain conditions (EH has disparate info from Checkpointer and MessageStore and can happen when crash occurs before complete updates) it may be possible to remove from the store and go below 0.

Updated existing test to make sure underflow will be caught.

Azure IoT Edge PR checklist:

This checklist is used to make sure that common guidelines for a pull request are followed.

General Guidelines and Best Practices

  • I have read the contribution guidelines.
  • Title of the pull request is clear and informative.
  • Description of the pull request includes a concise summary of the enhancement or bug fix.

Testing Guidelines

  • Pull request includes test coverage for the included changes.
  • Description of the pull request includes
    • concise summary of tests added/modified
    • local testing done.

Draft PRs

  • Open the PR in Draft mode if it is:
    • Work in progress or not intended to be merged.
    • Encountering multiple pipeline failures and working on fixes.

Note: We use the kodiakhq bot to merge PRs once the necessary checks and approvals are in place. When it merges a PR, kodiakhq converts the PR title to the commit title, PR description to the commit description, and squashes all the commits in the PR to a single commit. The net effect is that entire PR becomes a single commit. Please follow the best practices mentioned here for the PR title and description

@@ -103,5 +104,53 @@ public async Task MessageCountTest()
Assert.Equal(0ul, await columnFamilyDbStore.Count());
}
}

[Fact]
public async Task MessageCountUnderflowTest()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made a separate test altogether

@nyanzebra
Copy link
Contributor Author

Will fix styling and other stuff once method of fix is approved above :)

@nyanzebra nyanzebra requested a review from micahl January 11, 2022 22:06
@@ -75,7 +75,20 @@ public async Task Remove(byte[] key, CancellationToken cancellationToken)

Action operation = () => this.db.Remove(key, this.Handle);
await operation.ExecuteUntilCancelled(cancellationToken);
Interlocked.Decrement(ref this.count);
// We don't want to wrap around the counter.
var currentCount = Interlocked.Read(ref this.count);
Copy link
Contributor

Choose a reason for hiding this comment

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

You will need to lock the entire block - if 3 threads read a value (say 2) and all 3 call decrement you will end up with -1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I am really trying to avoid the lock.... perhaps the better thing to do is just let it be long and as spoke about offline to make the Metrics count for whatever it really is and just make sure the metric count is checked to make sure we avoid this problem

@nyanzebra
Copy link
Contributor Author

Keeping this open until I have result from running for several days

@nyanzebra nyanzebra merged commit e9652b2 into Azure:master Jan 20, 2022
@nyanzebra nyanzebra deleted the fix/queue_length_underflow branch January 20, 2022 21:06
nyanzebra added a commit to nyanzebra/iotedge that referenced this pull request Jan 20, 2022
* Fix underflow

* Use compare exchange and make separate test

* Use Max for count instead of trying to make atomic check
nyanzebra added a commit to nyanzebra/iotedge that referenced this pull request Jan 20, 2022
* Fix underflow

* Use compare exchange and make separate test

* Use Max for count instead of trying to make atomic check
lt72 pushed a commit to lt72/iotedge that referenced this pull request Jan 31, 2022
* Fix underflow

* Use compare exchange and make separate test

* Use Max for count instead of trying to make atomic check
nyanzebra added a commit that referenced this pull request Feb 1, 2022
* Fix underflow

* Use compare exchange and make separate test

* Use Max for count instead of trying to make atomic check
nyanzebra added a commit that referenced this pull request Feb 1, 2022
* Fix underflow

* Use compare exchange and make separate test

* Use Max for count instead of trying to make atomic check
seanyen pushed a commit to seanyen/iotedge that referenced this pull request Feb 1, 2022
…6025)

* Fix underflow

* Use compare exchange and make separate test

* Use Max for count instead of trying to make atomic check
damonbarry pushed a commit to damonbarry/iotedge that referenced this pull request Apr 15, 2022
* Fix underflow

* Use compare exchange and make separate test

* Use Max for count instead of trying to make atomic check
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.

3 participants