Skip to content
Draft
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions service/internal/sqldb/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["pool.go"],
importpath = "github.com/uber/submitqueue/service/internal/sqldb",
visibility = ["//service:__subpackages__"],
)

go_test(
name = "go_default_test",
srcs = ["pool_test.go"],
embed = [":go_default_library"],
deps = [
"@com_github_stretchr_testify//assert:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
],
)
47 changes: 47 additions & 0 deletions service/internal/sqldb/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2026 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package sqldb contains SQL database wiring helpers shared by services.
package sqldb

import (
"database/sql"
"fmt"
"strconv"
)

// ConfigureMaxOpenConnections applies a positive connection limit encoded as a
// decimal string. It retains up to the same number of idle connections so a
// busy bounded pool reuses sockets instead of repeatedly opening new ones. An
// empty string or zero preserves database/sql defaults.
func ConfigureMaxOpenConnections(db *sql.DB, value string) error {
if db == nil {
return fmt.Errorf("database is required")
}
if value == "" {
return nil
}
maxOpen, err := strconv.Atoi(value)
if err != nil {
return fmt.Errorf("parse maximum open connections %q: %w", value, err)
}
if maxOpen < 0 {
return fmt.Errorf("maximum open connections must be non-negative")
}
if maxOpen > 0 {
db.SetMaxOpenConns(maxOpen)
db.SetMaxIdleConns(maxOpen)
}
return nil
}
50 changes: 50 additions & 0 deletions service/internal/sqldb/pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2026 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sqldb

import (
"database/sql"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigureMaxOpenConnections(t *testing.T) {
tests := []struct {
name string
value string
want int
wantErr bool
}{
{name: "empty preserves default", value: "", want: 0},
{name: "zero preserves default", value: "0", want: 0},
{name: "positive applies limit", value: "32", want: 32},
{name: "negative is rejected", value: "-1", wantErr: true},
{name: "invalid is rejected", value: "many", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := &sql.DB{}
err := ConfigureMaxOpenConnections(db, tt.value)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, db.Stats().MaxOpenConnections)
})
}
}
1 change: 1 addition & 0 deletions service/runway/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//runway/controller/mergeconflictcheck:go_default_library",
"//runway/extension/merger:go_default_library",
"//runway/extension/merger/noop:go_default_library",
"//service/internal/sqldb:go_default_library",
"//sqsim/adapter/merger:go_default_library",
"//sqsim/model:go_default_library",
"@com_github_go_sql_driver_mysql//:go_default_library",
Expand Down
4 changes: 4 additions & 0 deletions service/runway/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/uber/submitqueue/runway/controller/mergeconflictcheck"
"github.com/uber/submitqueue/runway/extension/merger"
"github.com/uber/submitqueue/runway/extension/merger/noop"
"github.com/uber/submitqueue/service/internal/sqldb"
simmerger "github.com/uber/submitqueue/sqsim/adapter/merger"
"github.com/uber/submitqueue/sqsim/model"
"go.uber.org/zap"
Expand Down Expand Up @@ -131,6 +132,9 @@ func run() error {
return fmt.Errorf("failed to open queue database: %w", err)
}
defer queueDB.Close()
if err := sqldb.ConfigureMaxOpenConnections(queueDB, os.Getenv("QUEUE_MYSQL_MAX_OPEN_CONNECTIONS")); err != nil {
return fmt.Errorf("configure queue database connection limit: %w", err)
}

mysqlQueue, err := queueMySQL.NewQueue(queueMySQL.Params{
DB: queueDB,
Expand Down
3 changes: 3 additions & 0 deletions service/submitqueue/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ services:
- MYSQL_DSN=root:root@tcp(mysql-app:3306)/submitqueue?parseTime=true
# Queue infrastructure connection (separate database)
- QUEUE_MYSQL_DSN=root:root@tcp(mysql-queue:3306)/submitqueue?parseTime=true
- QUEUE_MYSQL_MAX_OPEN_CONNECTIONS=${QUEUE_MYSQL_MAX_OPEN_CONNECTIONS:-}
# Path to YAML queue configuration baked into the image
- QUEUE_CONFIG_PATH=/root/queues.yaml
# Stable subscriber name for the request-log consumer
Expand All @@ -75,6 +76,7 @@ services:
- MYSQL_DSN=root:root@tcp(mysql-app:3306)/submitqueue?parseTime=true
# Queue infrastructure connection (separate database)
- QUEUE_MYSQL_DSN=root:root@tcp(mysql-queue:3306)/submitqueue?parseTime=true
- QUEUE_MYSQL_MAX_OPEN_CONNECTIONS=${QUEUE_MYSQL_MAX_OPEN_CONNECTIONS:-}
- HOSTNAME=orchestrator-dev
- SQSIM_SCENARIO_PATH=${SQSIM_SCENARIO_PATH:-}
volumes:
Expand All @@ -100,6 +102,7 @@ services:
- PORT=:8080
# Queue infrastructure connection (shared with the orchestrator)
- QUEUE_MYSQL_DSN=root:root@tcp(mysql-queue:3306)/submitqueue?parseTime=true
- QUEUE_MYSQL_MAX_OPEN_CONNECTIONS=${QUEUE_MYSQL_MAX_OPEN_CONNECTIONS:-}
- HOSTNAME=runway-dev
- SQSIM_SCENARIO_PATH=${SQSIM_SCENARIO_PATH:-}
volumes:
Expand Down
1 change: 1 addition & 0 deletions service/submitqueue/gateway/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//platform/extension/counter/mysql:go_default_library",
"//platform/extension/messagequeue:go_default_library",
"//platform/extension/messagequeue/mysql:go_default_library",
"//service/internal/sqldb:go_default_library",
"//service/submitqueue/gateway/server/mapper:go_default_library",
"//submitqueue/core/topickey:go_default_library",
"//submitqueue/extension/queueconfig/yaml:go_default_library",
Expand Down
4 changes: 4 additions & 0 deletions service/submitqueue/gateway/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
mysqlcounter "github.com/uber/submitqueue/platform/extension/counter/mysql"
extqueue "github.com/uber/submitqueue/platform/extension/messagequeue"
queueMySQL "github.com/uber/submitqueue/platform/extension/messagequeue/mysql"
"github.com/uber/submitqueue/service/internal/sqldb"
"github.com/uber/submitqueue/service/submitqueue/gateway/server/mapper"
"github.com/uber/submitqueue/submitqueue/core/topickey"
yamlqueueconfig "github.com/uber/submitqueue/submitqueue/extension/queueconfig/yaml"
Expand Down Expand Up @@ -234,6 +235,9 @@ func run() error {
return fmt.Errorf("failed to open queue database: %w", err)
}
defer queueDB.Close()
if err := sqldb.ConfigureMaxOpenConnections(queueDB, os.Getenv("QUEUE_MYSQL_MAX_OPEN_CONNECTIONS")); err != nil {
return fmt.Errorf("configure queue database connection limit: %w", err)
}

// Initialize queue
mysqlQueue, err := queueMySQL.NewQueue(queueMySQL.Params{
Expand Down
1 change: 1 addition & 0 deletions service/submitqueue/orchestrator/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
"//platform/extension/messagequeue:go_default_library",
"//platform/extension/messagequeue/mysql:go_default_library",
"//platform/http:go_default_library",
"//service/internal/sqldb:go_default_library",
"//sqsim/adapter/buildrunner:go_default_library",
"//sqsim/model:go_default_library",
"//submitqueue/core/changeset:go_default_library",
Expand Down
4 changes: 4 additions & 0 deletions service/submitqueue/orchestrator/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
extqueue "github.com/uber/submitqueue/platform/extension/messagequeue"
queueMySQL "github.com/uber/submitqueue/platform/extension/messagequeue/mysql"
"github.com/uber/submitqueue/platform/http"
"github.com/uber/submitqueue/service/internal/sqldb"
simbuildrunner "github.com/uber/submitqueue/sqsim/adapter/buildrunner"
"github.com/uber/submitqueue/sqsim/model"
"github.com/uber/submitqueue/submitqueue/core/changeset"
Expand Down Expand Up @@ -196,6 +197,9 @@ func run() error {
return fmt.Errorf("failed to open queue database: %w", err)
}
defer queueDB.Close()
if err := sqldb.ConfigureMaxOpenConnections(queueDB, os.Getenv("QUEUE_MYSQL_MAX_OPEN_CONNECTIONS")); err != nil {
return fmt.Errorf("configure queue database connection limit: %w", err)
}

// Initialize queue
mysqlQueue, err := queueMySQL.NewQueue(queueMySQL.Params{
Expand Down
1 change: 1 addition & 0 deletions sqsim/runner/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func newLocalStack(repoRoot, profileDir string, stdout, stderr io.Writer) *local
"REPO_ROOT="+repoRoot,
"SQSIM_PROFILE_DIR="+profileDir,
"SQSIM_SCENARIO_PATH=/sqsim/profile.json",
"QUEUE_MYSQL_MAX_OPEN_CONNECTIONS=32",
),
}
}
Expand Down
6 changes: 6 additions & 0 deletions sqsim/runner/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package runner

import (
"slices"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -39,3 +40,8 @@ func TestFindRepoRootUsesWorkspaceEnvironment(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "/tmp/repo", root)
}

func TestLocalStackBoundsQueueDatabaseConnections(t *testing.T) {
stack := newLocalStack("/repo", "/profile", nil, nil)
assert.True(t, slices.Contains(stack.baseEnv, "QUEUE_MYSQL_MAX_OPEN_CONNECTIONS=32"))
}
8 changes: 8 additions & 0 deletions sqsim/scenarios/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ load("@rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"failure.go",
"happy_path.go",
"load.go",
"mixed.go",
"recovery.go",
"registry.go",
],
importpath = "github.com/uber/submitqueue/sqsim/scenarios",
Expand All @@ -14,7 +18,11 @@ go_library(
go_test(
name = "go_default_test",
srcs = [
"failure_test.go",
"happy_path_test.go",
"load_test.go",
"mixed_test.go",
"recovery_test.go",
"registry_test.go",
],
embed = [":go_default_library"],
Expand Down
51 changes: 51 additions & 0 deletions sqsim/scenarios/failure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2026 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package scenarios

import (
"time"

"github.com/uber/submitqueue/sqsim"
)

// BuildFailure returns a request whose Build Runner reports terminal failure.
func BuildFailure() (sqsim.Scenario, error) {
behavior := sqsim.NewBehavior().
BuildRunner(sqsim.BuildFailedAfter(time.Second)).
MergeConflictCheck(sqsim.SuccessfulMergeConflictCheck()).
Merge(sqsim.SuccessfulMerge())
return singleErrorScenario(behavior)
}

// MergeConflict returns a request rejected by the dry-run mergeability check.
func MergeConflict() (sqsim.Scenario, error) {
behavior := sqsim.NewBehavior().
BuildRunner(sqsim.SuccessfulBuildRunner()).
MergeConflictCheck(sqsim.ConflictingMergeConflictCheck()).
Merge(sqsim.SuccessfulMerge())
return singleErrorScenario(behavior)
}

func singleErrorScenario(behavior *sqsim.BehaviorBuilder) (sqsim.Scenario, error) {
return sqsim.NewScenario().
Timeout(30 * time.Second).
Land(
sqsim.NewLand("l1").
Queue("sqsim").
Behavior(behavior).
Expect(sqsim.RequestError),
).
Build()
}
38 changes: 38 additions & 0 deletions sqsim/scenarios/failure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2026 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package scenarios

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber/submitqueue/sqsim"
)

func TestFailureScenariosExpectError(t *testing.T) {
tests := map[string]Builder{
"build failure": BuildFailure,
"merge conflict": MergeConflict,
}
for name, build := range tests {
t.Run(name, func(t *testing.T) {
scenario, err := build()
require.NoError(t, err)
require.Len(t, scenario.Lands, 1)
assert.Equal(t, sqsim.RequestError, scenario.Lands[0].Expectation.Status)
})
}
}
Loading