Skip to content

server/schedule/labeler: support ttl for region label#4695

Merged
ti-chi-bot merged 21 commits intotikv:masterfrom
AndreMouche:labeler_ttl
Mar 29, 2022
Merged

server/schedule/labeler: support ttl for region label#4695
ti-chi-bot merged 21 commits intotikv:masterfrom
AndreMouche:labeler_ttl

Conversation

@AndreMouche
Copy link
Member

@AndreMouche AndreMouche commented Mar 2, 2022

Signed-off-by: shirly AndreMouche@126.com

What problem does this PR solve?

Issue Number: Close #4694

What is changed and how it works?

support ttl for region label

usage , post a rule with ttl

#!/bin/bash

curl -X POST http://yourip:2379/pd/api/v1/config/region-label/rule -H "Content-Type: application/json" --data-binary @- <<DATA
{"id":"ttltest5",
 "labels": [{
   			"key": "schedule", 
                          "value": "deny",
			  "ttl":"2m" }], 
 "rule_type":"key-range",
 "data": [{
            "start_key": "7480000000000000ff455f720000000000fa",
            "end_key": "7480000000000000ff465f720000000000fa"
        }]
}
DATA

Check List

Tests

  • - Unit test
  • - Integration test

Release note

support `ttl` for region label.

Signed-off-by: shirly <AndreMouche@126.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Mar 2, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • disksing
  • nolouch

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Details

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Mar 2, 2022
@ti-chi-bot ti-chi-bot requested review from nolouch and rleungx March 2, 2022 10:22
@AndreMouche
Copy link
Member Author

/wip

@codecov
Copy link

codecov bot commented Mar 3, 2022

Codecov Report

Merging #4695 (e73ce60) into master (86e8d08) will increase coverage by 0.02%.
The diff coverage is 82.65%.

@@            Coverage Diff             @@
##           master    #4695      +/-   ##
==========================================
+ Coverage   75.21%   75.24%   +0.02%     
==========================================
  Files         294      294              
  Lines       28325    28416      +91     
==========================================
+ Hits        21306    21382      +76     
- Misses       5142     5155      +13     
- Partials     1877     1879       +2     
Flag Coverage Δ
unittests 75.24% <82.65%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
server/cluster/cluster.go 84.93% <ø> (-0.26%) ⬇️
server/schedule/labeler/rules.go 87.20% <77.50%> (-8.45%) ⬇️
server/schedule/labeler/labeler.go 76.77% <85.96%> (+3.69%) ⬆️
pkg/mock/mockcluster/mockcluster.go 94.98% <100.00%> (ø)
pkg/tempurl/tempurl.go 45.00% <0.00%> (-15.00%) ⬇️
server/schedulers/shuffle_hot_region.go 55.55% <0.00%> (-10.11%) ⬇️
server/schedulers/random_merge.go 60.00% <0.00%> (-3.34%) ⬇️
server/schedule/operator/step.go 74.07% <0.00%> (-1.02%) ⬇️
server/config/persist_options.go 92.88% <0.00%> (-0.75%) ⬇️
server/grpc_service.go 52.04% <0.00%> (-0.65%) ⬇️
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 86e8d08...e73ce60. Read the comment docs.

Signed-off-by: shirly <AndreMouche@126.com>
Signed-off-by: shirly <AndreMouche@126.com>
Signed-off-by: shirly <AndreMouche@126.com>
Signed-off-by: shirly <AndreMouche@126.com>
Signed-off-by: shirly <AndreMouche@126.com>
Signed-off-by: shirly <AndreMouche@126.com>
@nolouch nolouch requested review from disksing and removed request for nolouch March 8, 2022 03:47
Signed-off-by: shirly <AndreMouche@126.com>
Signed-off-by: shirly <AndreMouche@126.com>
Signed-off-by: shirly <AndreMouche@126.com>

var backgroundJobInterval = 10 * time.Second

const regionLabelGCInterval = time.Hour
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe a shorter time?There is earlistExpireTime, most of the time do not need to do any thing.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we needn't check the earlistExpireTime too frequently because lots of labels expired in 1 hour shouldn't be a normal phenomenon?


func (l *RegionLabeler) getAndCheckRule(id string, now time.Time) *LabelRule {
l.Lock()
defer l.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

can release the lock before save to storage?

Copy link
Member Author

Choose a reason for hiding this comment

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

sorry we can't , since the rule is a pointer get from labelRules, Without lock it could be updated in the other goroutine(s) which is not thread safe.

l.RLock()
defer l.RUnlock()
return l.labelRules[id]
return l.getAndCheckRule(id, time.Now())
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we reduce the passing of the now parameter, which currently seems unnecessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

sorry, we can't. since we should check if there exist expired labels before return the current rule.

continue
}
if len(rule.Labels) == 0 {
err = l.storage.DeleteRegionRule(key)
Copy link
Member

Choose a reason for hiding this comment

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

What will happen if we fail to delete in storage but successfully in memory?

Copy link
Member Author

@AndreMouche AndreMouche Mar 17, 2022

Choose a reason for hiding this comment

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

It will just log the error info and the iteam wouldn't be deleted until the next restart. However, if there is something wrong with the storage last for long, the restart of the server would be long with too many expired items.

return rule
}
if len(rule.Labels) == 0 {
l.storage.DeleteRule(id)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Member Author

Choose a reason for hiding this comment

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

Here It will do nothing here and the item wouldn't be deleted in the storage until restart. However, if there is something wrong with the storage last for long, the restart of the server would be long with too many expired items.


labeler.checkAndClearExpiredLabels()

labeler.RLock()
Copy link
Member

Choose a reason for hiding this comment

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

Why need a lock here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because there is a goroutine(The GC function) running which may change the labeler.labelRules concurrently

Signed-off-by: shirly <AndreMouche@126.com>
Signed-off-by: shirly <AndreMouche@126.com>
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 19, 2022
Signed-off-by: shirly <AndreMouche@126.com>
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 22, 2022
@disksing disksing requested review from nolouch and rleungx March 22, 2022 08:07
@AndreMouche
Copy link
Member Author

Manual test:

tidb+pd+tikv

set region-label with ttl by pd for table sbtest2

#!/bin/bash

curl -X POST http://yourip:2379/pd/api/v1/config/region-label/rule -H "Content-Type: application/json" --data-binary @- <<DATA

{"id":"schema/sbtest/sbtest2",
 "labels": [{
   			  "key": "schedule", 
               "value": "deny",
			   "ttl":"2h" 
			  },
            {
               "key":"maintainer",
               "value":"shirly"
            }
           ], 
 "rule_type":"key-range",
 "data": [{
            "start_key": "7480000000000000ff455f720000000000fa",
            "end_key": "7480000000000000ff465f720000000000fa"
        }]
}
DATA

List region-label in tidb-client

mysql> select * from information_schema.attributes\G;
*************************** 1. row ***************************
        ID: schema/sbtest/sbtest2
      TYPE: key-range
ATTRIBUTES: "schedule=deny","maintainer=shirly"
    RANGES: [7480000000000000ff455f720000000000fa, 7480000000000000ff465f720000000000fa]

Copy link
Contributor

@nolouch nolouch left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 23, 2022
delete(l.labelRules, id)
return nil
}
l.storage.SaveRegionRule(id, rule)
Copy link
Contributor

Choose a reason for hiding this comment

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

why need save here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because there exist expired label which need to be cleared.

Copy link
Contributor

@disksing disksing left a comment

Choose a reason for hiding this comment

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

LGTM

storage: storage,
labelRules: make(map[string]*LabelRule),
ctx: ctx,
minExpire: nil,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
minExpire: nil,

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Mar 29, 2022
@disksing
Copy link
Contributor

/merge

@ti-chi-bot
Copy link
Member

@disksing: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

DetailsCommit hash: e73ce60

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Mar 29, 2022
@ti-chi-bot ti-chi-bot merged commit 6e335aa into tikv:master Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note Denotes a PR that will be considered when it comes time to generate release notes. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support TTL for region labels

6 participants