Skip to content

Commit

Permalink
Merge pull request #6 from CaojiamingAlan/add_dbQuotaUsage_and_dbSize…
Browse files Browse the repository at this point in the history
…Free_varaibles

add dbQuotaUsage and dbSizeFree
  • Loading branch information
ahrtr authored May 9, 2023
2 parents d6ba016 + 3024ee6 commit 19c3216
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,21 @@ Currently, `etcd-defrag` supports three variables below,
|--------------- |-------------|
| `dbSize` | total size of the etcd database |
| `dbSizeInUse` | total size in use of the etcd database |
| `dbSizeFree` | total size not in use of the etcd database, defined as dbSize - dbSizeInUse|
| `dbQuota` | etcd storage quota in bytes (the value passed to etcd instance by flag --quota-backend-bytes)|
| `dbQuotaUsage` | total usage of the etcd storage quota, defined as dbSize/dbQuota |

For example, if you want to run defragmentation if the total db size is greater than 80%
of the quota **OR** there is at least 200MiB free space, the defragmentation rule is `dbSize > dbQuota*80/100 || dbSize - dbSizeInUse > 200*1024*1024`.
The complete command is below,
```
$ ./etcd-defrag --endpoints http://127.0.0.1:22379 --cluster --defrag-rule="dbSize > dbQuota*80/100 || dbSize - dbSizeInUse > 200*1024*1024"
```
Or,
```
$ ./etcd-defrag --endpoints http://127.0.0.1:22379 --cluster --defrag-rule="dbQuotaUsage > 0.8 || dbSizeFree > 200*1024*1024"
```

Output:
```
Validating configuration.
Expand Down
21 changes: 14 additions & 7 deletions evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ import (
)

const (
dbSize = "dbSize"
dbSizeInUse = "dbSizeInUse"
dbQuota = "dbQuota"
dbSize = "dbSize"
dbSizeInUse = "dbSizeInUse"
dbQuota = "dbQuota"
dbQuotaUsage = "dbQuotaUsage"
dbSizeFree = "dbSizeFree"
)

func defaultVariables() map[string]interface{} {
return map[string]interface{}{
variables := map[string]interface{}{
dbQuota: 2 * 1024 * 1024 * 1024, // 2GiB
dbSize: 100 * 1024 * 1024, // 100MiB
dbSizeInUse: 60 * 1024 * 1024, // 60MiB
}
variables[dbQuotaUsage] = float64(variables[dbSize].(int)) / float64(variables[dbQuota].(int))
variables[dbSizeFree] = variables[dbSize].(int) - variables[dbSizeInUse].(int)
return variables
}

func evaluate(gcfg globalConfig, es epStatus) (bool, error) {
Expand All @@ -26,9 +31,11 @@ func evaluate(gcfg globalConfig, es epStatus) (bool, error) {
}

variables := map[string]interface{}{
dbQuota: gcfg.dbQuotaBytes,
dbSize: int(es.Resp.DbSize),
dbSizeInUse: int(es.Resp.DbSizeInUse),
dbQuota: gcfg.dbQuotaBytes,
dbSize: int(es.Resp.DbSize),
dbSizeInUse: int(es.Resp.DbSizeInUse),
dbQuotaUsage: float64(es.Resp.DbSize) / float64(gcfg.dbQuotaBytes),
dbSizeFree: int(es.Resp.DbSize) - int(es.Resp.DbSizeInUse),
}
eval := goval.NewEvaluator()

Expand Down
52 changes: 52 additions & 0 deletions evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ func TestValidateRule(t *testing.T) {
rule: "(dbSize > dbQuota*80/100) || (dbSize - dbSizeInUse > 200*1024*1024)",
expectError: false,
},
{
name: "valid rule with dbQuotaUsage",
rule: "dbQuotaUsage > 0.3",
expectError: false,
},
{
name: "valid rule with dbSizeFree",
rule: "dbSizeFree > 100",
expectError: false,
},
{
name: "not a boolean expression",
rule: "dbSize - dbSizeInUse",
Expand Down Expand Up @@ -86,25 +96,51 @@ func TestEvaluate(t *testing.T) {
dbSize: 81,
evaluationResult: true,
},
{
name: "dbQuotaUsage is greater than 0.8",
rule: "dbQuotaUsage > 0.8",
dbQuota: 100,
dbSize: 81,
evaluationResult: true,
},
{
name: "dbSize is less than dbQuota*80/100",
rule: "dbSize > dbQuota*80/100",
dbQuota: 100,
dbSize: 79,
},
{
name: "dbQuotaUsage is less than 0.8",
rule: "dbQuotaUsage > 0.8",
dbQuota: 100,
dbSize: 79,
},
{
name: "free space is greater than 200",
rule: "dbSize - dbSizeInUse > 200",
dbSize: 301,
dbSizeInUse: 100,
evaluationResult: true,
},
{
name: "dbSizeFree is greater than 200",
rule: "dbSizeFree > 200",
dbSize: 301,
dbSizeInUse: 100,
evaluationResult: true,
},
{
name: "free space is less than 200",
rule: "dbSize - dbSizeInUse > 200",
dbSize: 299,
dbSizeInUse: 100,
},
{
name: "dbSizeFree is less than 200",
rule: "dbSizeFree > 200",
dbSize: 299,
dbSizeInUse: 100,
},
{
name: "dbSize is greater than dbQuota*80/100 AND free space is greater than 200",
rule: "dbSize > dbQuota*80/100 && dbSize - dbSizeInUse > 200",
Expand Down Expand Up @@ -136,6 +172,22 @@ func TestEvaluate(t *testing.T) {
dbSizeInUse: 60,
evaluationResult: true,
},
{
name: "dbQuotaUsage is greater than 0.8 AND free space is greater than 200",
rule: "dbQuotaUsage > 0.8 && dbSizeFree > 200",
dbQuota: 1000,
dbSize: 801,
dbSizeInUse: 600,
evaluationResult: true,
},
{
name: "dbQuotaUsage is greater than 0.8 OR free space is less than 200",
rule: "dbQuotaUsage > 0.8 || dbSizeFree > 200",
dbQuota: 100,
dbSize: 81,
dbSizeInUse: 60,
evaluationResult: true,
},
}
for _, tc := range testCases {
tc := tc
Expand Down

0 comments on commit 19c3216

Please sign in to comment.