Skip to content

Fixes ClearML server is down - and then hangfire crashes #65 --ECL - #66

Merged
johnml1135 merged 1 commit into
masterfrom
resilient_clearml_auth_#65
Sep 7, 2023
Merged

johnml1135 merged 1 commit into
masterfrom
resilient_clearml_auth_#65

Conversation

@Enkidu93

@Enkidu93 Enkidu93 commented Sep 1, 2023 •

Copy link
Copy Markdown
Collaborator

Also relevant to #50.

  • Used Polly to auto-retry
  • Refactored ClearMLAuthenticationService a bit to avoid issue where token wasn't properly populating immediately
  • Small fixes in the ClearMLHealthCheck and to logging
  • (Added ".history" to the gitignore - vs code extension generates files in this folder on my box and, I presume, others)

This change is Reviewable

@ddaspit ddaspit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed 10 of 10 files at r1, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Enkidu93 and @johnml1135)


src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 28 at r1 (raw file):

    public async Task<string> GetAuthToken(CancellationToken cancellationToken = default)
    {
        if (_authToken is "")

This could result in a race condition where the auth token that is returned is no longer valid. I think it would be better to move this back to the StartAsync method. The other option is to use an async lock.


src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs line 29 at r1 (raw file):

        catch (Exception e)
        {
            _logger.LogError(

One of the overloads of the LogError method allows you to directly pass the exception.

@Enkidu93

Enkidu93 commented Sep 5, 2023

Copy link
Copy Markdown
Collaborator Author

src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 28 at r1 (raw file):

Previously, ddaspit (Damien Daspit) wrote…

This could result in a race condition where the auth token that is returned is no longer valid. I think it would be better to move this back to the StartAsync method. The other option is to use an async lock.

The issue I was having here was that if it cannot fetch the token on startup (even with the retries), then it's left waiting an hour to try again, right?

@ddaspit ddaspit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Enkidu93 and @johnml1135)


src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 28 at r1 (raw file):

Previously, Enkidu93 (Eli C. Lowry) wrote…

The issue I was having here was that if it cannot fetch the token on startup (even with the retries), then it's left waiting an hour to try again, right?

Won't it fail after 3 attempts?

@Enkidu93 Enkidu93 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewable status: 9 of 10 files reviewed, 2 unresolved discussions (waiting on @ddaspit and @johnml1135)


src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 28 at r1 (raw file):

Previously, ddaspit (Damien Daspit) wrote…

Won't it fail after 3 attempts?

It ought to. What happens if StartAsync throws an exception? In testing it out, I noticed that if I started everything up without internet connectivity and then turned then connected after a few minutes, it wouldn't fetch the token. I'll investigate.


src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs line 29 at r1 (raw file):

Previously, ddaspit (Damien Daspit) wrote…

One of the overloads of the LogError method allows you to directly pass the exception.

I'm not sure I'm seeing that overload. I found one that includes the exception. Let me know if this is what you wanted.

@ddaspit ddaspit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Enkidu93 and @johnml1135)


src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 28 at r1 (raw file):

Previously, Enkidu93 (Eli C. Lowry) wrote…

It ought to. What happens if StartAsync throws an exception? In testing it out, I noticed that if I started everything up without internet connectivity and then turned then connected after a few minutes, it wouldn't fetch the token. I'll investigate.

I'm guessing that the server will simply crash and not startup. That is probably not what we want. Let's add an async lock around the if statement. SmtTransferEngineState has an example of using a lock. You should also rename this method to GetAuthTokenAsync. This is the naming convention for async methods.


src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs line 29 at r1 (raw file):

Previously, Enkidu93 (Eli C. Lowry) wrote…

I'm not sure I'm seeing that overload. I found one that includes the exception. Let me know if this is what you wanted.

This is the overload I am referring to.

@johnml1135

Copy link
Copy Markdown
Collaborator

src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 66 at r2 (raw file):

        var base64EncodedAuthenticationString = Convert.ToBase64String(Encoding.ASCII.GetBytes(authenticationString));
        request.Headers.Add("Authorization", $"Basic {base64EncodedAuthenticationString}");
        HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken);

Can you give a general explanation "this is what I am trying to do with refactoring this"?

@johnml1135 johnml1135 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed 9 of 10 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @Enkidu93)

@ddaspit ddaspit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @Enkidu93 and @johnml1135)


src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 66 at r2 (raw file):

Previously, johnml1135 (John Lambert) wrote…

Can you give a general explanation "this is what I am trying to do with refactoring this"?

Failed requests are now handled by Polly. See the new http error policy that was added in IMachineBuilderExtensions.cs.

@Enkidu93

Enkidu93 commented Sep 6, 2023

Copy link
Copy Markdown
Collaborator Author

src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 66 at r2 (raw file):

Previously, ddaspit (Damien Daspit) wrote…

Failed requests are now handled by Polly. See the new http error policy that was added in IMachineBuilderExtensions.cs.

Yes, exactly.

@johnml1135

Copy link
Copy Markdown
Collaborator

src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 66 at r2 (raw file):

Previously, Enkidu93 (Eli C. Lowry) wrote…

Yes, exactly.

Thanks. I'm fine with it.

@johnml1135 johnml1135 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Dismissed @johnml1135 from a discussion.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @ddaspit and @Enkidu93)

@Enkidu93 Enkidu93 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewable status: 7 of 10 files reviewed, 3 unresolved discussions (waiting on @ddaspit and @johnml1135)


src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 28 at r1 (raw file):

Previously, ddaspit (Damien Daspit) wrote…

I'm guessing that the server will simply crash and not startup. That is probably not what we want. Let's add an async lock around the if statement. SmtTransferEngineState has an example of using a lock. You should also rename this method to GetAuthTokenAsync. This is the naming convention for async methods.

Done.


src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs line 29 at r1 (raw file):

Previously, ddaspit (Damien Daspit) wrote…

This is the overload I am referring to.

I see. I got it now - it wasn't showing up in my IDE for some reason, but VScode has been sketchy lately I think because of some of the extensions. I do wonder if we really need to log this at all though since it's automatically logged given it's an unhealthy healthcheck response.

@ddaspit ddaspit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed 2 of 3 files at r3, 3 of 3 files at r4, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @Enkidu93)


src/SIL.Machine.AspNetCore/Services/ClearMLHealthCheck.cs line 29 at r1 (raw file):

Previously, Enkidu93 (Eli C. Lowry) wrote…

I see. I got it now - it wasn't showing up in my IDE for some reason, but VScode has been sketchy lately I think because of some of the extensions. I do wonder if we really need to log this at all though since it's automatically logged given it's an unhealthy healthcheck response.

I agree. We don't need this log.


tests/SIL.Machine.AspNetCore.Tests/Services/ClearMLServiceTests.cs line 31 at r4 (raw file):

        );
        var authService = Substitute.For<IClearMLAuthenticationService>();
        authService.GetAuthTokenAsync().Returns("accessToken");

This should return a task, i.e. Task.FromResult("accessToken").

@ddaspit ddaspit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @Enkidu93)


src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 40 at r4 (raw file):

    }

    public override async Task StartAsync(CancellationToken cancellationToken)

You can remove this method.


src/SIL.Machine.AspNetCore/Services/ClearMLAuthenticationService.cs line 53 at r4 (raw file):

            {
                await Task.Delay(TimeSpan.FromSeconds(RefreshPeriod), stoppingToken);
                await AuthorizeAsync(stoppingToken);

You need to lock this line of code as well.

@Enkidu93

Enkidu93 commented Sep 6, 2023

Copy link
Copy Markdown
Collaborator Author

I think that's it, @ddaspit.

@ddaspit ddaspit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 3 of 3 files at r5, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Enkidu93)

@ddaspit ddaspit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Dismissed @johnml1135 from a discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @Enkidu93)

* Simplified logging --ECL
* Added lock to token refresh;  changed logging format  --ECL
* Changed name to ...Async --ECL
* Fixed typo; removed unused var --ECL
@johnml1135
johnml1135 force-pushed the resilient_clearml_auth_#65 branch from 166e46c to 8da55c4 Compare September 7, 2023 00:48

@johnml1135 johnml1135 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r6, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @Enkidu93)

@johnml1135 johnml1135 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @Enkidu93)

@johnml1135
johnml1135 merged commit e739885 into master Sep 7, 2023
johnml1135 added a commit that referenced this pull request Oct 28, 2026
Rows 43 and 44 move from in progress to done. R1 on #546 is draft PR #577
(commit bbb81a0, shadow 0 violations); R3 ordered look-ahead is draft
PR #576 (shadow 0 violations). Adds the round-doc section for both and
the PanGloss port issues (sillsdev/PanGloss #65, #66).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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