Skip to content

[reboot-cause] Use UTC to ensure consistent sorting#293

Merged
judyjoseph merged 5 commits intosonic-net:masterfrom
jianyuewu:reboot_cause_timezone_update
Aug 25, 2025
Merged

[reboot-cause] Use UTC to ensure consistent sorting#293
judyjoseph merged 5 commits intosonic-net:masterfrom
jianyuewu:reboot_cause_timezone_update

Conversation

@jianyuewu
Copy link
Contributor

@jianyuewu jianyuewu commented Jul 29, 2025

Previously reboot cause uses local timezone. This could result in the timestamp being in UTC, IDT, or any other timezone depending on the system configuration. When reboot cause history files are sorted, mixing different timezones can lead to incorrect chronological order and cause reboot cause test failure.
Now change the code to use datetime.datetime.utcnow() for reboot_cause_gen_time, ensuring that all timestamps are consistently in UTC.

Details:
Previously reboot-cause history will sort by name, name is using system time, could be UTC or IDT.
2025_07_28_10_48_14 reboot Mon Jul 28 01:45:45 PM IDT 2025 admin N/A
Here left side 10_48_14 is UTC time, while right side 01:45:45 is IDT time.
2025_07_28_13_41_44 reboot Mon Jul 28 01:39:13 PM IDT 2025 admin N/A
Left and right are both IDT time.
We need to make left side time aligned, so sort index will be always correct.

show reboot-cause history cmd:

admin@r-bison-06:~$ show reboot-cause history 
Name                 Cause       Time                             User    Comment                                                                                                                                                                                                                                
-------------------  ----------  -------------------------------  ------  ---------                                                                                                                                                                                                                                      
2025_07_28_13_41_44  reboot      Mon Jul 28 01:39:13 PM IDT 2025  admin   N/A                                                                                                                                                                                                                                           
2025_07_28_13_34_54  reboot      Mon Jul 28 01:32:25 PM IDT 2025  admin   N/A                                                                                                                                                                                                                                            
2025_07_28_13_28_13  Watchdog    N/A                              N/A     Unknown                                                                                                                                                                                                              
2025_07_28_13_22_28  reboot      Mon Jul 28 01:19:58 PM IDT 2025  admin   N/A                                                                                                                                                                                                                                  
2025_07_28_13_17_05  reboot      Mon Jul 28 10:15:33 AM UTC 2025  admin   N/A    // Time should be 01:15:33 AM
2025_07_28_10_48_14  reboot      Mon Jul 28 01:45:45 PM IDT 2025  admin   N/A    // This entry should be after 01:39:13 PM IDT                                                                                                                                                                                                                     
2025_07_28_09_57_42  Power Loss  N/A                              N/A     Unknown                                                                                                           
2025_07_28_09_52_04  Power Loss  N/A                              N/A     Unknown                                                                                                                                                                                                                                        
2025_07_28_09_46_15  Power Loss  N/A                              N/A     Unknown                                                                                                          
2025_07_28_09_40_22  Power Loss  N/A                              N/A     Unknown

Reboot cause history in test:

13:53:45 reboot.check_reboot_cause_history        L0532 INFO   | index:  6, reboot cause: Watchdog, reboot cause from DUT: reboot                                                                                                                                                                                        
13:53:45 reboot.check_reboot_cause_history        L0537 ERROR  | The 6 reboot-cause not match. expected_reboot type=Watchdog, actual_reboot_cause=reboot                                                                                                                                                                 
13:53:45 reboot.check_reboot_cause_history        L0541 INFO   | Current reboot_type_history_queue content:                                                                                                                                                                                                              
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=0, reboot_type=power off
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=1, reboot_type=power off
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=2, reboot_type=power off
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=3, reboot_type=power off
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=4, reboot_type=cold
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=5, reboot_type=cold
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=6, reboot_type=watchdog
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=7, reboot_type=cold
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=8, reboot_type=cold
13:53:45 reboot.check_reboot_cause_history        L0543 INFO   |   index=9, reboot_type=cold

After fix, now both timestamps use UTC.

$ show reboot-cause history 
Name                 Cause        Time                             User    Comment
-------------------  -----------  -------------------------------  ------  ---------
2025_08_06_05_45_15  reboot       Wed Aug  6 05:44:00 AM UTC 2025  admin   N/A
2025_08_06_05_43_13  reboot       Wed Aug  6 05:41:59 AM UTC 2025  admin   N/A
2025_08_06_05_40_56  reboot       Wed Aug  6 05:39:41 AM UTC 2025  admin   N/A
2025_08_06_05_38_42  reboot       Wed Aug  6 05:35:43 AM UTC 2025  admin   N/A
2025_08_06_03_20_24  reboot       Wed Aug  6 03:17:30 AM UTC 2025  admin   N/A
2025_08_01_14_18_34  fast-reboot  Fri Aug  1 02:17:17 PM UTC 2025  admin   N/A
2025_08_01_13_54_35  fast-reboot  Fri Aug  1 01:53:17 PM UTC 2025  admin   N/A
2025_08_01_13_28_36  fast-reboot  Fri Aug  1 01:27:18 PM UTC 2025  admin   N/A
2025_08_01_13_23_31  fast-reboot  Fri Aug  1 01:22:14 PM UTC 2025  admin   N/A
2025_08_01_10_29_28  fast-reboot  Fri Aug  1 10:28:12 AM UTC 2025  admin   N/A

Previously reboot cause uses local timezone. This could result in the
timestamp being in UTC, IDT, or any other timezone depending on the
system configuration. When reboot cause history files are sorted,
mixing different timezones can lead to incorrect chronological
order and cause reboot cause test failure.
Now change the code to use datetime.datetime.utcnow() for
reboot_cause_gen_time, ensuring that all timestamps are consistently in
UTC.

Signed-off-by: Jianyue Wu <[email protected]>
Signed-off-by: Jianyue Wu <[email protected]>
@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jianyuewu jianyuewu marked this pull request as draft July 29, 2025 10:07
@jianyuewu jianyuewu marked this pull request as ready for review July 30, 2025 05:44
@keboliu keboliu requested a review from judyjoseph July 30, 2025 06:32
@r12f r12f requested a review from vvolam July 31, 2025 15:31
@r12f
Copy link

r12f commented Jul 31, 2025

hi @vvolam , do you mind to help reviewing this PR?

Copy link
Contributor

@vvolam vvolam left a comment

Choose a reason for hiding this comment

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

LGTM, same change might be needed in scripts/procdockerstatsd as we are still using systemtime.

datetimeobj = datetime.now()

Could you also update the PR description with the reboot history command after the fix?

@jianyuewu
Copy link
Contributor Author

LGTM, same change might be needed in scripts/procdockerstatsd as we are still using systemtime.

datetimeobj = datetime.now()

Could you also update the PR description with the reboot history command after the fix?

OK, will also update this timestamp, and also put new reboot history.

Update 2nd timestamp in reboot cause, also use UTC.
Mon Jul 28 10:15:33 AM UTC 2025

Signed-off-by: Jianyue Wu <[email protected]>
@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Jianyue Wu <[email protected]>
@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jianyuewu
Copy link
Contributor Author

LGTM, same change might be needed in scripts/procdockerstatsd as we are still using systemtime.

datetimeobj = datetime.now()

Could you also update the PR description with the reboot history command after the fix?

OK, will also update this timestamp, and also put new reboot history.

Code updated, new reboot history put in description.

@jianyuewu
Copy link
Contributor Author

/azpw run

@mssonicbld
Copy link

/AzurePipelines run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Jianyue Wu <[email protected]>
@mssonicbld
Copy link

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jianyuewu
Copy link
Contributor Author

jianyuewu commented Aug 6, 2025

I tried to run in local via pdb, seems that updated line is covered. Not sure why that line is still not in coverage report.

PDB set_trace (IO-capturing turned off)
/sonic/src/sonic-host-services/scripts/procdockerstatsd(250)run()
-> datetimeobj = datetime.utcnow()

@dgsudharsan dgsudharsan requested a review from judyjoseph August 11, 2025 23:27
@jianyuewu
Copy link
Contributor Author

@judyjoseph Could you help review this change? Thanks😊

@jianyuewu
Copy link
Contributor Author

jianyuewu commented Aug 21, 2025

@vvolam @judyjoseph , Could you help also review PR for those branches? Thank you~
202505 PR:
#303
For 202412, seems didn't find related branch in this repo, could you help indicate if this repo is correct? Thank you~
https://github.com/[Azure/sonic-host-services.msft](https://github.com/Azure/sonic-host-services.msft#)#

Copy link
Contributor

@judyjoseph judyjoseph left a comment

Choose a reason for hiding this comment

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

LGTM

@judyjoseph judyjoseph merged commit 805ff5b into sonic-net:master Aug 25, 2025
5 checks passed
@kperumalbfn
Copy link

@jianyuewu could you provide 202411 PR as this has CP conflict

@yejianquan
Copy link

yejianquan commented Aug 26, 2025

Hi @judyjoseph , could you help to suggest whether this PR should go to 202505 branch, thanks in advance

@r12f
Copy link

r12f commented Aug 26, 2025

hi @jianyuewu , do you mind helping pick this change to 202411?

@jianyuewu
Copy link
Contributor Author

hi @jianyuewu , do you mind helping pick this change to 202411?

@r12f Sure, backport PR created: #306

@yejianquan
Copy link

offline synced with Judy, good to backport to 202505

@r12f
Copy link

r12f commented Aug 28, 2025

hi @jianyuewu , do you mind helping pick this change to 202411?

@r12f Sure, backport PR created: #306

hi @jianyuewu , looks like the PR is created to master, might need some update.

@jianyuewu
Copy link
Contributor Author

jianyuewu commented Aug 28, 2025

hi @jianyuewu , do you mind helping pick this change to 202411?

@r12f Sure, backport PR created: #306

hi @jianyuewu , looks like the PR is created to master, might need some update.
Hi @r12f,
Ah, so sorry, rebased to 202411. #306

@r12f
Copy link

r12f commented Sep 22, 2025

Removing 202412 tag since the PR is already merged in 202411.

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.

9 participants