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

[Service Bus] Bug Fix: Unexpected expiresAtUtc: Invalid Date in the receivedMessage #13543

Merged
15 commits merged into from Apr 14, 2021

Conversation

HarshaNalluru
Copy link
Member

@HarshaNalluru HarshaNalluru commented Feb 2, 2021

Fixes #12883

Bug

"Invalid date" for the expiresAtUtc property

azure:service-bus:messages:verbose AmqpMessage to ServiceBusReceivedMessage: {
  _amqpAnnotatedMessage: {
    header: { deliveryCount: 0 },
  .
  .
  .
  expiresAtUtc: Invalid Date,
  .
  .
  .
}

Background

"ttl" on the received message will be undefined if the sent message doesn't have the "ttl" property set to a value.
In this case, the service assumes the default infinite value for expiry time.
Reference: https://docs.microsoft.com/en-us/azure/service-bus-messaging/message-expiration#entity-level-expiration

Cause (expiresAtUtc)

In the SDK, for a received message, we calculate the expiresAtUtc using the "ttl" and the enqueued time.

  • If the msg.ttl > max_duration - msg.enqueuedTime, calculate msg.expiresAtUtc with max_duration
  • If the msg.ttl < max_duration - msg.enqueuedTime, calculate msg.expiresAtUtc with msg.enqueuedTime + msg.ttl
  • If the msg.ttl is not defined on the received message, we return Invalid Date for the expiresAtUtc property.

.NET SDK

TTL is defaulted to TimeSpan.MaxValue in .NET.
For ExpiresAt calculation:

  • if AbsoluteExpiryTime is populated we use that.
  • if TTL > maxDateTime - EnqueuedTime, use TimeSpan.MaxValue
  • Else use EnqueuedTime + TTL

Fix for JS SDK

If the msg.ttl is not defined, set it to the max value as the service says that. This allows the proper calculation of expiresAtUtc.

TODO

  • Bug Fix
  • Test
  • Changelog

@ghost ghost added the Service Bus label Feb 2, 2021
@check-enforcer
Copy link

check-enforcer bot commented Feb 2, 2021

This pull request is protected by Check Enforcer.

What is Check Enforcer?

Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass.

Why am I getting this message?

You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged.

What should I do now?

If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows:
/check-enforcer evaluate
Typically evaulation only takes a few seconds. If you know that your pull request is not covered by a pipeline and this is expected you can override Check Enforcer using the following command:
/check-enforcer override
Note that using the override command triggers alerts so that follow-up investigations can occur (PRs still need to be approved as normal).

What if I am onboarding a new service?

Often, new services do not have validation pipelines associated with them, in order to bootstrap pipelines for a new service, you can issue the following command as a pull request comment:
/azp run prepare-pipelines
This will run a pipeline that analyzes the source tree and creates the pipelines necessary to build and validate your pull request. Once the pipeline has been created you can trigger the pipeline using the following comment:
/azp run js - [service] - ci

@HarshaNalluru HarshaNalluru changed the title [Service Bus] [Service Bus] Bug Fix: Unexpected expiresAtUtc: Invalid Date in the receivedMessage Feb 2, 2021
Copy link
Contributor

@chradek chradek left a comment

Choose a reason for hiding this comment

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

Overall strategy looks good, but had some questions.

Comment on lines 557 to 558
deadLetterReason: sbmsg.applicationProperties?.DeadLetterReason as string,
deadLetterErrorDescription: sbmsg.applicationProperties?.DeadLetterErrorDescription as string
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need to add as string? Couldn't these be undefined?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, undefined is allowed.

@HarshaNalluru HarshaNalluru marked this pull request as ready for review April 14, 2021 09:37
@@ -1,5 +1,9 @@
# Release History

## 7.1.0-beta.2 (2021-04-07)

- [Bug Fix] `expiresAtUtc` is `Invalid Date` in the received message when the ttl is not defined. Has been fixed in [#13543](https://github.com/Azure/azure-sdk-for-js/pull/13543)
Copy link
Member

Choose a reason for hiding this comment

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

Just a note, I have another PR where I'm just removing the date for .beta.1 since we never released it. So your changes would actually go there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, forgot!

} else {
props.expiresAtUtc = new Date(props.enqueuedTimeUtc.getTime() + msg.ttl!);
if (msg.ttl == null) msg.ttl = Constants.maxDurationValue;
if (props.enqueuedTimeUtc) {
Copy link
Member

Choose a reason for hiding this comment

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

Is props.enqueuedTime ever not set if a message has been received from an actual broker?

Copy link
Member Author

Choose a reason for hiding this comment

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

I hadn't seen such a case, but this gives the flexibility to set the props properly even if the service messes up.

props.expiresAtUtc = new Date(props.enqueuedTimeUtc.getTime() + msg.ttl!);
if (msg.ttl == null) msg.ttl = Constants.maxDurationValue;
if (props.enqueuedTimeUtc) {
if (msg.ttl >= Constants.maxDurationValue - props.enqueuedTimeUtc.getTime()) {
Copy link
Member

Choose a reason for hiding this comment

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

(is this the same?)
props.expiresAtUtc = Math.min(props.enqueuedTimeUtc.getTime() + msg.ttl, Constants.maxDurationValue)

Copy link
Member Author

Choose a reason for hiding this comment

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

Great catch

Copy link
Member Author

Choose a reason for hiding this comment

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

props.expiresAtUtc = new Date(Math.min(props.enqueuedTimeUtc.getTime() + msg.ttl, Constants.maxDurationValue));

@@ -599,8 +606,10 @@ export function fromRheaMessage(
: undefined,
...sbmsg,
...props,
deadLetterReason: sbmsg.applicationProperties?.DeadLetterReason,
deadLetterErrorDescription: sbmsg.applicationProperties?.DeadLetterErrorDescription
deadLetterReason: sbmsg.applicationProperties?.DeadLetterReason as string | undefined,
Copy link
Member

Choose a reason for hiding this comment

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

You are on a type-roll! ;)

@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation.
Copy link
Member

Choose a reason for hiding this comment

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

These are perfect unit tests - can you move this into internal/unit?

Copy link
Member

@richardpark-msft richardpark-msft 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, glad you were able to remove yet one more spot where we didn't have typings :)

@ghost
Copy link

ghost commented Apr 14, 2021

Hello @HarshaNalluru!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 172df4f into Azure:master Apr 14, 2021
jay-most pushed a commit to jay-most/azure-sdk-for-js that referenced this pull request Apr 26, 2021
… receivedMessage (Azure#13543)

Fixes Azure#12883 
## Bug
"Invalid date" for the `expiresAtUtc` property
```
azure:service-bus:messages:verbose AmqpMessage to ServiceBusReceivedMessage: {
  _amqpAnnotatedMessage: {
    header: { deliveryCount: 0 },
  .
  .
  .
  expiresAtUtc: Invalid Date,
  .
  .
  .
}
```

## Background
"ttl" on the received message will be undefined if the sent message doesn't have the "ttl" property set to a value. 
In this case, the service assumes the default infinite value for expiry time.
Reference: https://docs.microsoft.com/en-us/azure/service-bus-messaging/message-expiration#entity-level-expiration

### Cause (expiresAtUtc)
In the SDK, for a received message, we calculate the expiresAtUtc using the "ttl" and the enqueued time.
- If the msg.ttl > max_duration - msg.enqueuedTime, calculate msg.expiresAtUtc with max_duration
- If the msg.ttl < max_duration - msg.enqueuedTime, calculate msg.expiresAtUtc with msg.enqueuedTime  + msg.ttl
- If the msg.ttl is not defined on the received message, we return Invalid Date for the expiresAtUtc property.

## .NET SDK
TTL is defaulted to TimeSpan.MaxValue in .NET.
For ExpiresAt calculation:
- if AbsoluteExpiryTime is populated we use that.
- if TTL > maxDateTime - EnqueuedTime, use TimeSpan.MaxValue
- Else use EnqueuedTime + TTL


## Fix for JS SDK
If the msg.ttl is not defined, set it to the max value as the service says that. This allows the proper calculation of `expiresAtUtc`.

## TODO
- [x] Bug Fix
- [x] Test
- [x] Changelog
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Service Bus] Bug: Unexpected expiresAtUtc: Invalid Date in the logged output
3 participants