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

Patches for v2 #716

Merged
merged 12 commits into from
Jul 30, 2024
Merged

Patches for v2 #716

merged 12 commits into from
Jul 30, 2024

Conversation

lieut-data
Copy link
Member

@lieut-data lieut-data commented Jul 25, 2024

Summary

A handful of fixes to reported and observed issues after the bug bash:

  • The site statistics on the system console weren't updating, and also hadn't been changed to reflect the one-way sync.
  • During testing, it was observed that if OnConfigurationChange fails, it often clears out otherwise good values for various fields. This is super painful for the admin, so just reset to sensible values instead.
  • We were would always renew subscriptions from MS Teams lifecycle events, even if we didn't rely on them anymore. Stop doing that.
  • Watch out for panics in the metrics job (none found, but preemptive change), and fix the issue that would reset metrics on plugin upgrade.

Ticket Link

Fixes: https://mattermost.atlassian.net/browse/MM-59876
Fixes: https://mattermost.atlassian.net/browse/MM-59862
Fixes: https://mattermost.atlassian.net/browse/MM-59860
Fixes: https://mattermost.atlassian.net/browse/MM-59859
Fixes: https://mattermost.atlassian.net/browse/MM-59861

@lieut-data lieut-data added 2: Dev Review Requires review by a core committer QA/Review Not Required labels Jul 25, 2024
@@ -86,8 +86,8 @@ func NewAPI(p *Plugin, store store.Store) *API {

// returnJSON writes the given data as json with the provided httpStatus
func (a *API) returnJSON(w http.ResponseWriter, data any) {
w.WriteHeader(http.StatusOK)
Copy link
Member Author

Choose a reason for hiding this comment

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

From https://pkg.go.dev/net/http:

Changing the header map after a call to [ResponseWriter.WriteHeader] has no effect unless the HTTP status code was of the 1xx class or the modified headers are trailers.

I think I broke this a while back when fixing a different endpoint.

@@ -879,31 +879,23 @@ func (a *API) siteStats(w http.ResponseWriter, r *http.Request) {
http.Error(w, "unable to get whitelisted users count", http.StatusInternalServerError)
return
}
receiving, err := a.p.store.GetActiveUsersReceivingCount(metricsActiveUsersRange)
Copy link
Member Author

Choose a reason for hiding this comment

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

Just simplifying.

@@ -222,7 +221,7 @@ func TestProcessLifecycle(t *testing.T) {
Resource: "mockResource",
ChangeType: "mockChangeType",
ClientState: "mockClientState",
LifecycleEvent: "mockLifecycleEvent",
LifecycleEvent: "reauthorizationRequired",
Copy link
Member Author

Choose a reason for hiding this comment

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

Use real events, since we now check and discard unrecognized ones.

@@ -40,6 +40,12 @@ func (c *configuration) ProcessConfiguration() {
c.ClientSecret = strings.TrimSpace(c.ClientSecret)
c.EncryptionKey = strings.TrimSpace(c.EncryptionKey)
c.WebhookSecret = strings.TrimSpace(c.WebhookSecret)
if c.MaxSizeForCompleteDownload < 0 {
Copy link
Member Author

Choose a reason for hiding this comment

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

There's a bug with the system console where values can sometimes not get saved, making it brutal to keep losing the various secrets. If this happens for these values, just use sensible values.

Copy link
Contributor

Choose a reason for hiding this comment

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

So what are we actually fixing here, some default values that were missing? Anyone looking at the bug you mention?

Copy link
Member Author

Choose a reason for hiding this comment

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

ah.plugin.GetMetrics().ObserveLifecycleEvent(event.LifecycleEvent, metrics.DiscardedReasonFailedToRefresh)
return
}
if event.LifecycleEvent != "reauthorizationRequired" {
Copy link
Member Author

Choose a reason for hiding this comment

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

Check the event type upfront.

@@ -256,6 +256,9 @@ func (p *Plugin) start(isRestart bool) {
if err != nil {
p.API.LogError("failed to start metrics job", "error", err)
}

// Run the job above right away so we immediately populate metrics.
go p.updateMetrics()
Copy link
Member Author

Choose a reason for hiding this comment

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

We'd always get dips otherwise:

CleanShot 2024-07-25 at 17 30 45@2x

@@ -294,7 +297,7 @@ func (p *Plugin) start(isRestart bool) {
p.checkCredentialsJob = checkCredentialsJob

// Run the job above right away so we immediately populate metrics.
p.checkCredentials()
go p.checkCredentials()
Copy link
Member Author

Choose a reason for hiding this comment

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

Faster, Spock!

@@ -540,6 +543,16 @@ func (p *Plugin) GetRemoteID() string {
}

func (p *Plugin) updateMetrics() {
defer func() {
Copy link
Member Author

Choose a reason for hiding this comment

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

This was run in an implicit goroutine already, so just tracking it like a proper worker.


err = s.getQueryBuilder(db).
Select("count(*)").
Select("count(mmUserID)").
Copy link
Member Author

Choose a reason for hiding this comment

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

Stop COUNT(*) which requires a table scan.

From(usersTableName).
Where(sq.GtOrEq{"LastChatReceivedAt": now.Add(-dur).UnixMicro()}).
Where(sq.LtOrEq{"LastChatReceivedAt": now.UnixMicro()}).
Copy link
Member Author

Choose a reason for hiding this comment

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

This is redundant.

Copy link
Contributor

@enahum enahum left a comment

Choose a reason for hiding this comment

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

Fantastic

@lieut-data lieut-data added this to the v2.0 milestone Jul 26, 2024
Copy link
Member

@calebroseland calebroseland left a comment

Choose a reason for hiding this comment

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

LTGM, just that one text bit that needs changing

server/connect.go Outdated Show resolved Hide resolved
server/connect.go Show resolved Hide resolved
Co-authored-by: Caleb Roseland <caleb@calebroseland.com>
@lieut-data lieut-data merged commit f079753 into main Jul 30, 2024
9 checks passed
@lieut-data lieut-data deleted the patches-for-v2 branch July 30, 2024 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2: Dev Review Requires review by a core committer QA/Review Not Required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants