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

Add metric for showing the errant GTIDs in VTOrc #13281

Merged
merged 5 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions go/test/endtoend/vtorc/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package api

import (
"encoding/json"
"fmt"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -197,5 +199,23 @@ func TestAPIEndpoints(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 400, status, resp)
assert.Equal(t, "Filtering by shard without keyspace isn't supported\n", resp)

// Also verify that the metric for errant GTIDs is reporting the correct information.
_, resp, err = utils.MakeAPICall(t, vtorc, "/debug/vars")
require.NoError(t, err)
resultMap := make(map[string]any)
err = json.Unmarshal([]byte(resp), &resultMap)
require.NoError(t, err)
errantGTIDMap := reflect.ValueOf(resultMap["ErrantGtidMap"])
errantGtidTablets := errantGTIDMap.MapKeys()
require.Len(t, errantGtidTablets, 3)

errantGTIDinReplica := ""
for _, tabletKey := range errantGtidTablets {
if tabletKey.String() == replica.Alias {
errantGTIDinReplica = errantGTIDMap.MapIndex(tabletKey).Interface().(string)
}
}
require.NotEmpty(t, errantGTIDinReplica)
})
}
10 changes: 10 additions & 0 deletions go/vt/vtorc/inst/instance_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/rcrowley/go-metrics"
"github.com/sjmudd/stopwatch"

"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/external/golib/sqlutils"

vitessmysql "vitess.io/vitess/go/mysql"
Expand All @@ -56,6 +57,10 @@ const (
var instanceReadChan = make(chan bool, backendDBConcurrency)
var instanceWriteChan = make(chan bool, backendDBConcurrency)

var (
errantGtidMap = make(map[string]string)
)

// Constant strings for Group Replication information
// See https://dev.mysql.com/doc/refman/8.0/en/replication-group-members-table.html for additional information.
const (
Expand Down Expand Up @@ -88,6 +93,9 @@ func init() {
_ = metrics.Register("instance.write", writeInstanceCounter)
_ = writeBufferLatency.AddMany([]string{"wait", "write"})
writeBufferLatency.Start("wait")
stats.NewStringMapFuncWithMultiLabels("ErrantGtidMap", "Metric to track the errant GTIDs detected by VTOrc", []string{"TabletAlias"}, "ErrantGtid", func() map[string]string {
return errantGtidMap
})

go initializeInstanceDao()
}
Expand Down Expand Up @@ -429,6 +437,8 @@ Cleanup:
instance.GtidErrant, err = vitessmysql.Subtract(redactedExecutedGtidSet.String(), redactedPrimaryExecutedGtidSet.String())
}
}
// update the errant gtid map
errantGtidMap[topoproto.TabletAliasString(tablet.Alias)] = instance.GtidErrant
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we be writing to and reading (the metric) from the map concurrently? In that case we will need to synchronize the accesses.

Also could there be multiple concurrent writes to this map? I see handleDiscoveryRequests() is discovering instances in parallel.

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 map is only used for a metric, so it should be okay to read and write concurrently to it, right? I can add locking around it, but I don't see the need for it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have never seen this in practice, but as per 1.6+ https://go.dev/doc/go1.6 says, :

Runtime
The runtime has added lightweight, best-effort detection of concurrent misuse of maps. As always, if one goroutine is writing to a map, no other goroutine should be reading or writing the map concurrently. If the runtime detects this condition, it prints a diagnosis and crashes the program. The best way to find out more about the problem is to run the program under the race detector, which will more reliably identify the race and give more detail.

Another reference: https://stackoverflow.com/questions/36167200/how-safe-are-golang-maps-for-concurrent-read-write-operations which shows a runtime panic can occur.

I think if you had a unit test around this the race detector would have failed it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed with @rohit-nayak-ps, this requires synchronization.

Copy link
Member Author

Choose a reason for hiding this comment

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

Gotcha, I'll add synchronization then.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

}

latency.Stop("instance")
Expand Down
Loading