Skip to content
Open
Changes from all 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
11 changes: 8 additions & 3 deletions config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ type TiKVClient struct {
// If a Region has not been accessed for more than the given duration (in seconds), it
// will be reloaded from the PD.
RegionCacheTTL uint `toml:"region-cache-ttl" json:"region-cache-ttl"`
// RegionCacheTTLJitterSec is the jitter in seconds for region cache TTL.
// The actual TTL will be randomly chosen in the range [RegionCacheTTL, RegionCacheTTL + RegionCacheTTLJitterSec].
// This can help to avoid cache stampede when many regions expire at the same time.
RegionCacheTTLJitterSec uint `toml:"region-cache-ttl-jitter-sec" json:"region-cache-ttl-jitter-sec"`
Comment on lines +103 to +106

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Description: Check if RegionCacheTTLJitterSec config is actually consumed anywhere

# Search for usages of the new field
echo "=== Searching for RegionCacheTTLJitterSec usages ==="
rg -n 'RegionCacheTTLJitterSec' --type go

# Search for SetRegionCacheTTLWithJitter or similar setter functions
echo -e "\n=== Searching for TTL setter functions ==="
rg -n 'SetRegionCacheTTL' --type go -A 5

# Check how RegionCacheTTL (the existing field) is consumed for comparison
echo -e "\n=== Checking how existing RegionCacheTTL is consumed ==="
rg -n '\.RegionCacheTTL[^J]' --type go -C 2

Repository: tikv/client-go

Length of output: 4601


🏁 Script executed:

#!/bin/bash
# Search for where TiKVClient config is applied

# Find where RegionCacheTTLJitterSec is read from config
echo "=== Searching for code that reads RegionCacheTTLJitterSec from config ==="
rg -n 'RegionCacheTTLJitterSec' --type go -B 2 -A 2

# Look for client initialization code
echo -e "\n=== Searching for client initialization / config application ==="
rg -n 'NewClient\|NewKVClient' --type go -A 10 | head -100

# Check where SetRegionCacheTTLWithJitter is called (beyond tests)
echo -e "\n=== All calls to SetRegionCacheTTLWithJitter ==="
rg -n 'SetRegionCacheTTLWithJitter' --type go -B 2 -A 2

# Look for initialization code that uses the config
echo -e "\n=== Searching for code that applies cfg to region cache ==="
rg -n 'cfg\.RegionCacheTTL' --type go -B 2 -A 2

Repository: tikv/client-go

Length of output: 3893


🏁 Script executed:

#!/bin/bash
# Search for client creation and config usage more broadly

# Find files that might contain client initialization
echo "=== Files containing 'NewKV' or 'NewClient' ==="
fd -e go | xargs grep -l 'func New' | head -20

# Search in the top-level client files
echo -e "\n=== Checking client.go or similar files ==="
fd -e go -path "*client*" | head -20

# Look for where config is actually used
echo -e "\n=== Searching for where config struct fields are read ==="
rg -n 'c\.config\.' --type go -A 2 | head -60

# Check internal/client directory
echo -e "\n=== Files in internal/client ==="
fd -e go -path "*/internal/client*"

# Check if there's any initialization of region cache from config
echo -e "\n=== Searching for InitRegionCache or region cache initialization ==="
rg -n 'regionCache\s*=' --type go -B 2 -A 2 | head -80

Repository: tikv/client-go

Length of output: 1541


🏁 Script executed:

#!/bin/bash
# Check the size of client.go first
wc -l internal/client/client.go

# Look at the beginning of the file to understand structure
head -100 internal/client/client.go

# Search for config usage and NewKVClient definition
echo -e "\n=== Searching for NewKVClient definition ==="
grep -n "func.*NewKVClient\|func.*NewClient" internal/client/client.go

# Look for where config might be applied
echo -e "\n=== Searching for config in client.go ==="
grep -n "config\|Config" internal/client/client.go | head -30

Repository: tikv/client-go

Length of output: 4546


🏁 Script executed:

#!/bin/bash
# Look at the code around line 254 where config is read
sed -n '240,280p' internal/client/client.go

# Now search for where RegionCacheTTLWithJitter might be called
echo -e "\n=== Searching for any initialization of region cache TTL ==="
rg -n 'SetRegionCacheTTL' --type go | grep -v test

# Check tikv package for any initialization
echo -e "\n=== Checking tikv/rpc.go or similar for client setup ==="
fd 'rpc\.go' -path '*/tikv/*'

# Look for NewRawKVClient or other client creation
echo -e "\n=== Searching in tikv package ==="
ls -la tikv/
grep -r "SetRegionCacheTTL" tikv/ --include="*.go" | grep -v test

Repository: tikv/client-go

Length of output: 4011


🏁 Script executed:

#!/bin/bash
# Find where region cache is created/initialized
echo "=== Searching for region cache initialization ==="
rg -n 'NewRegionCache\|regionCache\s*:=' --type go -A 3 -B 1 | grep -v test | head -60

# Check kv.go for client initialization
echo -e "\n=== Checking kv.go for config usage in client setup ==="
wc -l tikv/kv.go
grep -n "config\|Config" tikv/kv.go | head -30

# Look for NewKVStore or similar
echo -e "\n=== Searching for NewKVStore ==="
grep -n "func New" tikv/kv.go | head -10

Repository: tikv/client-go

Length of output: 1157


🏁 Script executed:

#!/bin/bash
# Check the NewKVStore function implementation
sed -n '320,420p' tikv/kv.go

# Also check the opt package for client options
echo -e "\n=== Checking opt package for RegionCacheTTL ==="
fd opt.go | head -5
find . -name "opt" -type d 2>/dev/null | head -3

Repository: tikv/client-go

Length of output: 3336


Config field is not wired to runtime behavior — the feature is non-functional.

The field declaration and documentation are correct, but RegionCacheTTLJitterSec is never read from the config or applied to the region cache. The region cache is initialized in NewKVStore() (tikv/kv.go:340) without any TTL parameters, and SetRegionCacheTTLWithJitter() is only called in test code, not in production initialization. The global variables regionCacheTTLSec and regionCacheTTLJitterSec retain their hardcoded defaults (600 and 60 seconds) regardless of the config values. Users would need to manually invoke SetRegionCacheTTLWithJitter() after client creation to use this feature—the configuration has no effect.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@config/client.go` around lines 103 - 106, The RegionCacheTTLJitterSec config
field is never applied at runtime; update NewKVStore (tikv/kv.go: function
NewKVStore) to read the client's Config.RegionCacheTTL and
Config.RegionCacheTTLJitterSec (the struct field RegionCacheTTLJitterSec) and
apply them when initializing the region cache instead of relying on hardcoded
globals; call SetRegionCacheTTLWithJitter or set the variables regionCacheTTLSec
and regionCacheTTLJitterSec (or preferably pass TTL/jitter into the region cache
constructor) during NewKVStore so the configured values are used in production
rather than only in tests.

// If a store has been up to the limit, it will return error for successive request to
// prevent the store occupying too much token in dispatching level.
StoreLimit int64 `toml:"store-limit" json:"store-limit"`
Expand Down Expand Up @@ -176,9 +180,10 @@ func DefaultTiKVClient() TiKVClient {

EnableChunkRPC: true,

RegionCacheTTL: 600,
StoreLimit: 0,
StoreLivenessTimeout: DefStoreLivenessTimeout,
RegionCacheTTL: 600,
RegionCacheTTLJitterSec: 60,
StoreLimit: 0,
StoreLivenessTimeout: DefStoreLivenessTimeout,

TTLRefreshedTxnSize: 32 * 1024 * 1024,

Expand Down
Loading