Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make http, grpc and elastic search request timeout configurable #60

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func runServer(ctx context.Context, cfg *Config) error {
if err != nil {
return fmt.Errorf("create new asset repository: %w", err)
}
discoveryRepository := esStore.NewDiscoveryRepository(esClient, logger)
discoveryRepository := esStore.NewDiscoveryRepository(esClient, logger, cfg.Elasticsearch.RequestTimeoutInSeconds)
lineageRepository, err := postgres.NewLineageRepository(pgClient)
if err != nil {
return fmt.Errorf("create new lineage repository: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cli/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func runWorker(ctx context.Context, cfg *Config) error {

mgr, err := workermanager.New(ctx, workermanager.Deps{
Config: cfg.Worker,
DiscoveryRepo: elasticsearch.NewDiscoveryRepository(esClient, logger),
DiscoveryRepo: elasticsearch.NewDiscoveryRepository(esClient, logger, cfg.Elasticsearch.RequestTimeoutInSeconds),
Logger: logger,
})
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type Config struct {
BaseUrl string `mapstructure:"baseurl" default:"localhost:8080"`

// User Identity
Identity IdentityConfig `mapstructure:"identity"`

Identity IdentityConfig `mapstructure:"identity"`
RequestTimeoutInSeconds int `mapstructure:"request_timeout_in_seconds" default:"10"`
// GRPC Config
GRPC GRPCConfig `mapstructure:"grpc"`
}
Expand All @@ -55,9 +55,10 @@ type IdentityConfig struct {
}

type GRPCConfig struct {
Port int `yaml:"port" mapstructure:"port" default:"8081"`
MaxRecvMsgSize int `yaml:"max_recv_msg_size" mapstructure:"max_recv_msg_size" default:"33554432"`
MaxSendMsgSize int `yaml:"max_send_msg_size" mapstructure:"max_send_msg_size" default:"33554432"`
Port int `yaml:"port" mapstructure:"port" default:"8081"`
RequestTimeoutInSeconds int `yaml:"request_timeout_in_seconds" mapstructure:"request_timeout_in_seconds" default:"5"`
MaxRecvMsgSize int `yaml:"max_recv_msg_size" mapstructure:"max_recv_msg_size" default:"33554432"`
MaxSendMsgSize int `yaml:"max_send_msg_size" mapstructure:"max_send_msg_size" default:"33554432"`
}

func Serve(
Expand Down Expand Up @@ -105,7 +106,7 @@ func Serve(
grpc_health_v1.RegisterHealthServer(grpcServer, healthHandler)

// init http proxy
grpcDialCtx, grpcDialCancel := context.WithTimeout(ctx, time.Second*5)
grpcDialCtx, grpcDialCancel := context.WithTimeout(ctx, time.Second*time.Duration(config.GRPC.RequestTimeoutInSeconds))
defer grpcDialCancel()

headerMatcher := makeHeaderMatcher(config)
Expand Down Expand Up @@ -155,12 +156,13 @@ func Serve(
}()

logger.Info("Starting server", "http_port", config.addr(), "grpc_port", config.grpcAddr())

if err := mux.Serve(
ctx,
mux.WithHTTPTarget(config.addr(), &http.Server{
Handler: gwmux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
WriteTimeout: time.Duration(config.RequestTimeoutInSeconds) * time.Second,
IdleTimeout: 120 * time.Second,
}),
mux.WithGRPCTarget(config.grpcAddr(), grpcServer),
Expand Down
12 changes: 7 additions & 5 deletions internal/store/elasticsearch/discovery_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import (
// DiscoveryRepository implements discovery.Repository
// with elasticsearch as the backing store.
type DiscoveryRepository struct {
cli *Client
logger log.Logger
cli *Client
logger log.Logger
requestTimeout int
}

func NewDiscoveryRepository(cli *Client, logger log.Logger) *DiscoveryRepository {
func NewDiscoveryRepository(cli *Client, logger log.Logger, requestTimeout int) *DiscoveryRepository {
return &DiscoveryRepository{
cli: cli,
logger: logger,
cli: cli,
logger: logger,
requestTimeout: requestTimeout,
}
}

Expand Down
20 changes: 10 additions & 10 deletions internal/store/elasticsearch/discovery_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestDiscoveryRepositoryUpsert(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)
err = repo.Upsert(ctx, asset.Asset{
ID: "",
Type: asset.TypeTable,
Expand All @@ -50,7 +50,7 @@ func TestDiscoveryRepositoryUpsert(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)
err = repo.Upsert(ctx, asset.Asset{
ID: "sample-id",
Type: asset.Type("unknown-type"),
Expand All @@ -69,7 +69,7 @@ func TestDiscoveryRepositoryUpsert(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

// upsert with create_time as a object
err = repo.Upsert(ctx, asset.Asset{
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestDiscoveryRepositoryUpsert(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)
err = repo.Upsert(ctx, ast)
assert.NoError(t, err)

Expand Down Expand Up @@ -178,7 +178,7 @@ func TestDiscoveryRepositoryUpsert(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

err = repo.Upsert(ctx, existingAsset)
assert.NoError(t, err)
Expand Down Expand Up @@ -219,7 +219,7 @@ func TestDiscoveryRepositoryDeleteByID(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)
err = repo.DeleteByID(ctx, "")
assert.ErrorIs(t, err, asset.ErrEmptyID)
})
Expand All @@ -241,7 +241,7 @@ func TestDiscoveryRepositoryDeleteByID(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

err = repo.Upsert(ctx, ast)
require.NoError(t, err)
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestDiscoveryRepositoryDeleteByID(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

err = repo.Upsert(ctx, ast1)
require.NoError(t, err)
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestDiscoveryRepositoryDeleteByURN(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

t.Run("should return error if the given urn is empty", func(t *testing.T) {
err = repo.DeleteByURN(ctx, "")
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestDiscoveryRepositoryDeleteByURN(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

err = repo.Upsert(ctx, ast1)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions internal/store/elasticsearch/discovery_search_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (repo *DiscoveryRepository) Search(ctx context.Context, cfg asset.SearchCon
}

search := repo.cli.client.Search

res, err := search(
search.WithBody(query),
search.WithIndex(defaultSearchIndex),
Expand All @@ -67,6 +68,7 @@ func (repo *DiscoveryRepository) Search(ctx context.Context, cfg asset.SearchCon
search.WithIgnoreUnavailable(true),
search.WithSourceIncludes(returnedAssetFieldsResult...),
search.WithContext(ctx),
search.WithTimeout(time.Duration(repo.requestTimeout)*time.Second),
)
if err != nil {
return nil, asset.DiscoveryError{Op: "Search", Err: fmt.Errorf("execute search: %w", err)}
Expand Down
10 changes: 5 additions & 5 deletions internal/store/elasticsearch/discovery_search_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestSearcherSearch(t *testing.T) {
err = loadTestFixture(cli, esClient, "./testdata/search-test-fixture.json")
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

type expectedRow struct {
Type string
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestSearcherSuggest(t *testing.T) {
err = loadTestFixture(cli, esClient, "./testdata/suggest-test-fixture.json")
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

t.Run("fixtures", func(t *testing.T) {
testCases := []struct {
Expand Down Expand Up @@ -424,7 +424,7 @@ func loadTestFixture(cli *elasticsearch.Client, esClient *store.Client, filePath

ctx := context.TODO()
for _, testdata := range data {
repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)
for _, ast := range testdata.Assets {
if err := repo.Upsert(ctx, ast); err != nil {
return err
Expand Down Expand Up @@ -452,7 +452,7 @@ func TestGroupAssets(t *testing.T) {
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)
_, err = repo.GroupAssets(ctx, asset.GroupConfig{
GroupBy: []string{""},
})
Expand All @@ -473,7 +473,7 @@ func TestGroupAssets(t *testing.T) {
err = loadTestFixture(cli, esClient, "./testdata/search-test-fixture.json")
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
repo := store.NewDiscoveryRepository(esClient, log.NewNoop(), 10)

type groupTest struct {
Description string
Expand Down
3 changes: 2 additions & 1 deletion internal/store/elasticsearch/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const (
)

type Config struct {
Brokers string `mapstructure:"brokers" default:"http://localhost:9200"`
Brokers string `mapstructure:"brokers" default:"http://localhost:9200"`
RequestTimeoutInSeconds int `mapstructure:"request_timeout_in_seconds" default:"10"`
anjali9791 marked this conversation as resolved.
Show resolved Hide resolved
}

type searchHit struct {
Expand Down
Loading