-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit_hash:45eab2973f7551af2af6ed620bfe839e0984ce52
- Loading branch information
Showing
6 changed files
with
105 additions
and
17 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
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
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
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
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
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,58 @@ | ||
package warmup | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/doublecloud/transfer/pkg/parsers" | ||
"github.com/doublecloud/transfer/pkg/schemaregistry/confluent" | ||
"github.com/doublecloud/transfer/pkg/util" | ||
"github.com/doublecloud/transfer/pkg/util/set" | ||
"go.ytsaurus.tech/library/go/core/log" | ||
) | ||
|
||
// It's important to warn-up Schema-Registry cache single-thread, to not to DDoS Schema-Registry | ||
// mutex we need not bcs of something is thread-unsafe, but to reduce schema-registry RPS | ||
func WarmUpSRCache(logger log.Logger, mutex *sync.Mutex, batch parsers.MessageBatch, schemaRegistryClient *confluent.SchemaRegistryClient, notFoundIsOk bool) { | ||
extractSchemaID := func(buf []byte) (uint32, []byte) { | ||
msgLen := len(buf) | ||
if len(buf) != 0 { | ||
if buf[0] == 0 { | ||
zeroIndex := bytes.Index(buf[5:], []byte{0}) | ||
if zeroIndex != -1 { | ||
msgLen = 5 + zeroIndex | ||
} | ||
} | ||
} | ||
return binary.BigEndian.Uint32(buf[1:5]), buf[msgLen:] | ||
} | ||
|
||
schemaIDs := set.New[uint32]() | ||
var schemaID uint32 = 0 | ||
for _, currMsg := range batch.Messages { | ||
leastBuf := currMsg.Value | ||
for { | ||
if len(leastBuf) == 0 { | ||
break | ||
} | ||
schemaID, leastBuf = extractSchemaID(leastBuf) | ||
schemaIDs.Add(schemaID) | ||
} | ||
} | ||
|
||
mutex.Lock() | ||
defer mutex.Unlock() | ||
for _, currSchemaID := range schemaIDs.Slice() { | ||
_ = backoff.RetryNotify(func() error { | ||
_, err := schemaRegistryClient.GetSchema(int(currSchemaID)) | ||
if notFoundIsOk && err != nil && strings.Contains(err.Error(), "Error code: 404") { | ||
return nil | ||
} | ||
return err | ||
}, backoff.NewConstantBackOff(time.Second), util.BackoffLogger(logger, "getting schema (warm-up cache)")) | ||
} | ||
} |