-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: preventing sql read amplification by logging concurrent same qu…
…eries
- Loading branch information
Showing
3 changed files
with
109 additions
and
3 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,70 @@ | ||
package sqlx | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/zeromicro/go-zero/core/logx" | ||
) | ||
|
||
const ( | ||
concurrencyThreshold = 3 | ||
logInterval = 60 * 1000 // 1 minute | ||
) | ||
|
||
var logger = logx.NewLessLogger(logInterval) | ||
|
||
type ( | ||
concurrentReads struct { | ||
reads map[string]*queryReference | ||
lock sync.Mutex | ||
} | ||
|
||
queryReference struct { | ||
concurrency uint32 | ||
maxConcurrency uint32 | ||
} | ||
) | ||
|
||
func newConcurrentReads() *concurrentReads { | ||
return &concurrentReads{ | ||
reads: make(map[string]*queryReference), | ||
} | ||
} | ||
|
||
func (r *concurrentReads) add(query string) { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
|
||
if ref, ok := r.reads[query]; ok { | ||
ref.concurrency++ | ||
if ref.maxConcurrency < ref.concurrency { | ||
ref.maxConcurrency = ref.concurrency | ||
} | ||
} else { | ||
r.reads[query] = &queryReference{ | ||
concurrency: 1, | ||
maxConcurrency: 1, | ||
} | ||
} | ||
} | ||
|
||
func (r *concurrentReads) remove(query string) { | ||
r.lock.Lock() | ||
defer r.lock.Unlock() | ||
ref, ok := r.reads[query] | ||
if !ok { | ||
return | ||
} | ||
|
||
if ref.concurrency > 1 { | ||
ref.concurrency-- | ||
return | ||
} | ||
|
||
// last reference to remove | ||
delete(r.reads, query) | ||
if ref.maxConcurrency >= concurrencyThreshold { | ||
logger.Errorf("sql query amplified, query: %q, maxConcurrency: %d", | ||
query, ref.maxConcurrency) | ||
} | ||
} |
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 @@ | ||
package sqlx | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestName(t *testing.T) { | ||
const ( | ||
query = "select foo" | ||
times = 10 | ||
) | ||
cr := newConcurrentReads() | ||
assert.NotPanics(t, func() { | ||
cr.remove(query) | ||
}) | ||
|
||
for i := 0; i < times; i++ { | ||
cr.add(query) | ||
} | ||
|
||
ref := cr.reads[query] | ||
assert.Equal(t, uint32(times), ref.concurrency) | ||
|
||
for i := 0; i < times; i++ { | ||
cr.remove(query) | ||
} | ||
|
||
// just removed, not decremented | ||
assert.Equal(t, uint32(1), ref.concurrency) | ||
assert.Equal(t, uint32(times), ref.maxConcurrency) | ||
} |
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