Skip to content

Commit

Permalink
use max key size
Browse files Browse the repository at this point in the history
  • Loading branch information
michalpristas committed Aug 30, 2022
1 parent af77ec6 commit f15ff5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
1 change: 0 additions & 1 deletion internal/pkg/api/handleAck.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ func cleanRoles(roles json.RawMessage) (json.RawMessage, int, error) {
if strings.HasSuffix(k, "-rdstale") {
keys = append(keys, k)
}

}

if len(keys) == 0 {
Expand Down
17 changes: 10 additions & 7 deletions internal/pkg/bulk/opApiKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (
)

const (
expectedAPIKeySize = 64 // 64B
envelopeSize = 64 // 64B
safeBuffer = 0.9
envelopeSize = 64 // 64B
safeBuffer = 0.9
)

// The ApiKey API's are not yet bulk enabled. Stub the calls in the bulker
Expand Down Expand Up @@ -101,6 +100,7 @@ func (b *Bulker) flushUpdateAPIKey(ctx context.Context, queue queueT) error {
responses := make(map[int]int)
idxToID := make(map[int32]string)
IDToResponse := make(map[string]int)
maxKeySize := 0

// merge ids
for n := queue.head; n != nil; n = n.next {
Expand Down Expand Up @@ -132,6 +132,9 @@ func (b *Bulker) flushUpdateAPIKey(ctx context.Context, queue queueT) error {
// last one wins, it may be policy change and ack are in the same queue
rolePerID[req.ID] = req.RolesHash
idxToID[n.idx] = req.ID
if maxKeySize < len(req.ID) {
maxKeySize = len(req.ID)
}
}

for id, roleHash := range rolePerID {
Expand All @@ -145,7 +148,7 @@ func (b *Bulker) flushUpdateAPIKey(ctx context.Context, queue queueT) error {

responseIdx := 0
for hash, role := range roles {
idsPerBatch := b.getIdsCountPerBatch(len(role))
idsPerBatch := b.getIdsCountPerBatch(len(role), maxKeySize)
ids := idsPerRole[hash]
if idsPerBatch <= 0 {
log.Error().Str("err", "request too large").Msg("No API Key ID could fit request size for bulk update")
Expand Down Expand Up @@ -239,10 +242,10 @@ func (b *Bulker) flushUpdateAPIKey(ctx context.Context, queue queueT) error {
return nil
}

func (b *Bulker) getIdsCountPerBatch(roleSize int) int {
func (b *Bulker) getIdsCountPerBatch(roleSize, maxKeySize int) int {
spareSpace := b.opts.apikeyMaxReqSize - roleSize - envelopeSize
if spareSpace > expectedAPIKeySize {
return int(float64(spareSpace) * safeBuffer / expectedAPIKeySize)
if spareSpace > maxKeySize {
return int(float64(spareSpace) * safeBuffer / float64(maxKeySize))
}
return 0
}

0 comments on commit f15ff5d

Please sign in to comment.