-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tablet throttler: read and use MySQL host metrics (#16904)
Signed-off-by: Shlomi Noach <[email protected]>
- Loading branch information
1 parent
2c6e053
commit fb79106
Showing
25 changed files
with
735 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package osutil | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// parseLoadAvg parses the load average from the content of /proc/loadavg or sysctl output. | ||
// Input such as "1.00 0.99 0.98 1/1 1", "2.83 3.01 3.36" | ||
func parseLoadAvg(content string) (float64, error) { | ||
fields := strings.Fields(content) | ||
if len(fields) == 0 { | ||
return 0, fmt.Errorf("unexpected loadavg content: %s", content) | ||
} | ||
return strconv.ParseFloat(fields[0], 64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//go:build darwin | ||
|
||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package osutil | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
// LoadAvg returns the past 1 minute system load average. This works on linux and darwin systems. | ||
// On other systems, it returns 0 with no error. | ||
func LoadAvg() (float64, error) { | ||
cmd := exec.Command("sysctl", "-n", "vm.loadavg") | ||
// Sample output: `{ 2.83 3.01 3.36 }` | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return 0, err | ||
} | ||
if len(output) < 1 { | ||
return 0, fmt.Errorf("unexpected sysctl output: %q", output) | ||
} | ||
output = output[1:] // Remove the leading `{ ` | ||
return parseLoadAvg(string(output)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//go:build linux | ||
|
||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package osutil | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// LoadAvg returns the past 1 minute system load average. This works on linux and darwin systems. | ||
// On other systems, it returns 0 with no error. | ||
func LoadAvg() (float64, error) { | ||
content, err := os.ReadFile("/proc/loadavg") | ||
if err != nil { | ||
return 0, err | ||
} | ||
return parseLoadAvg(string(content)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build !linux && !darwin | ||
|
||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package osutil | ||
|
||
// LoadAvg returns the past 1 minute system load average. This works on linux and darwin systems. | ||
// On other systems, it returns 0 with no error. | ||
func LoadAvg() (float64, error) { | ||
return 0, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package osutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLoadAvgValue(t *testing.T) { | ||
tcases := []struct { | ||
input string | ||
loadavg float64 | ||
isError bool | ||
}{ | ||
{ | ||
input: "", | ||
isError: true, | ||
}, | ||
{ | ||
input: "{}", | ||
isError: true, | ||
}, | ||
{ | ||
input: "{ x y z }", | ||
isError: true, | ||
}, | ||
{ | ||
input: "1", | ||
loadavg: 1.0, | ||
}, | ||
{ | ||
input: "0.00 0.00 0.00 1/1 1", | ||
loadavg: 0.0, | ||
}, | ||
{ | ||
input: "2.72 2.89 3.17", | ||
loadavg: 2.72, | ||
}, | ||
{ | ||
input: " 2.72 2.89 3.17", | ||
loadavg: 2.72, | ||
}, | ||
} | ||
for _, tcase := range tcases { | ||
t.Run(tcase.input, func(t *testing.T) { | ||
loadavg, err := parseLoadAvg(tcase.input) | ||
if tcase.isError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tcase.loadavg, loadavg) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLoadAvg(t *testing.T) { | ||
loadavg, err := LoadAvg() | ||
assert.NoError(t, err) | ||
assert.GreaterOrEqual(t, loadavg, 0.0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.