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 AlarmStateInitializer to store most recent kafka records from the alarm state topic #3079

Merged
merged 4 commits into from
Jul 23, 2024

Conversation

jbellister-slac
Copy link
Contributor

Fixes #3078

Change the end index of the substring() call to 6 so that it will be possible for type to equal the AlarmSystem.STATE_PREFIX in the equality check.

After I made this change, confirmed that acknowledgements do persist between alarm server reboots.

Although the change is minor this does cause this code block to be hit that wasn't possible before. Reading through the code it looks ok to me, and testing showed things seemed to be working as expected.

The eventual change this leads to outside this class is that initial_states will have something to pass to the AlarmServerPV here:

final AlarmServerPV pv = new AlarmServerPV(this, parent.getPathName(), name, initial_states.remove(path));

Copy link
Collaborator

@kasemir kasemir left a comment

Choose a reason for hiding this comment

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

What you propose looks better, but while you're at it, how about this, which is overall shorter

for (final ConsumerRecord<String, String> record : records)
{
    // Only handle state updates
    if (record.key().startsWith(AlarmSystem.STATE_PREFIX))
    {
      final String path = record.key().substring(6);
      ...
    }
}

@shroffk
Copy link
Member

shroffk commented Jul 23, 2024

if (record.key().startsWith(AlarmSystem.STATE_PREFIX))

+1 in addition to the small improvement in not having to create a new String object this is also much easier to read

@jbellister-slac
Copy link
Contributor Author

Thanks! Agreed that it is easier to read this way. Change made and confirmed it still works as expected.

@kasemir
Copy link
Collaborator

kasemir commented Jul 23, 2024

Also, one might wonder why "6" in here:

if (record.key().startsWith(AlarmSystem.STATE_PREFIX))
{
    final String path = record.key().substring(6);

It's because AlarmSystem.STATE_PREFIX happens to contain "state:" and we want to get what's after that.
It'll break if we ever changed the value of the state prefix to say "status:", and now we'd have to skip 7 chars.

This would be safer:

if (record.key().startsWith(AlarmSystem.STATE_PREFIX))
{
    final String path = record.key().substring(AlarmSystem.STATE_PREFIX.length());

... but now one could argue that just "6" is faster than calling String.length().
It would certainly hurt in C where strlen(some_string) actually counts all the chars for each call.
With Java strings, I guess it only fetches one number, so quite fast and certainly safer than hoping the length of AlarmSystem.STATE_PREFIX is and remains 6.

Copy link
Collaborator

@kasemir kasemir left a comment

Choose a reason for hiding this comment

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

Thanks, that looks better.
And if it actually works, that'd be perfect!

@kasemir kasemir merged commit d8754f1 into ControlSystemStudio:master Jul 23, 2024
1 check passed
@kasemir
Copy link
Collaborator

kasemir commented Jul 23, 2024

Thanks for looking into this!

@jbellister-slac jbellister-slac deleted the fix_alarm_init branch July 23, 2024 19:03
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.

Alarm acknowledgements do not persist between alarm server reboots
3 participants