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

Make db connect more robust #5738

Merged
merged 22 commits into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a3937ad
Add retry loop to DB connect logic
Jan 14, 2019
461665d
make DB connect vars configurable
Jan 15, 2019
a2122a6
Merge branch 'master' into Make_DB_Connect_More_Robust
techknowlogick Jan 15, 2019
16db020
Update DB retry field names, add as setting subfield, add to US local…
Jan 15, 2019
b74d936
spaces->tabs
Jan 15, 2019
5dc2abc
fix new DB setting declaration
Jan 15, 2019
38dbb4d
Add retry loop to DB connect logic
Jan 14, 2019
f6fb9aa
Support CORS headers to git smart http protocol (#5719)
lunny Jan 14, 2019
aa178f7
make DB connect vars configurable
Jan 15, 2019
72c624f
Update DB retry field names, add as setting subfield, add to US local…
Jan 15, 2019
2f6f57a
spaces->tabs
Jan 15, 2019
8544971
fix new DB setting declaration
Jan 15, 2019
d57e3a4
Merge branch 'Make_DB_Connect_More_Robust' of github.com:pbrackin/git…
Jan 15, 2019
990691b
Moving retry logic to separate func. More spaces->tabs.
Jan 16, 2019
9113c75
spaces->tabs
Jan 16, 2019
602159c
fix spacing fmt gripe, I think.
Jan 16, 2019
c2060d0
add additional logging per connect attempt
Jan 18, 2019
eb90cc7
Merge branch 'master' into Make_DB_Connect_More_Robust
techknowlogick Jan 18, 2019
eff081e
Change backoff timer ini setting to a Duration vs constant
Jan 18, 2019
edb820d
Merge branch 'Make_DB_Connect_More_Robust' of github.com:pbrackin/git…
Jan 18, 2019
9b6c7ef
Update backoff timer to Duration in cheatsheet
Jan 18, 2019
b05babf
Merge branch 'master' into Make_DB_Connect_More_Robust
techknowlogick Jan 19, 2019
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
5 changes: 5 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ SQLITE_TIMEOUT = 500
ITERATE_BUFFER_SIZE = 50
; Show the database generated SQL
LOG_SQL = true
; Maximum number of DB Connect retries
DB_RETRIES = 10
; Backoff time per DB retry (seconds)
DB_RETRY_BACKOFF = 3
zeripath marked this conversation as resolved.
Show resolved Hide resolved


[indexer]
ISSUE_INDEXER_PATH = indexers/issues.bleve
Expand Down
2 changes: 2 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `SSL_MODE`: **disable**: For PostgreSQL and MySQL only.
- `PATH`: **data/gitea.db**: For SQLite3 only, the database file path.
- `LOG_SQL`: **true**: Log the executed SQL.
- `DB_RETRIES`: **10**: How many ORM init / DB connect attempts allowed.
- `DB_RETRY_BACKOFF`: **3**: Seconds to wait before trying another ORM init / DB connect attempt, if failure occured.
zeripath marked this conversation as resolved.
Show resolved Hide resolved

## Indexer (`indexer`)

Expand Down
16 changes: 10 additions & 6 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,14 @@ var (
DisableGitHooks bool

// Database settings
UseSQLite3 bool
UseMySQL bool
UseMSSQL bool
UsePostgreSQL bool
UseTiDB bool
LogSQL bool
UseSQLite3 bool
UseMySQL bool
UseMSSQL bool
UsePostgreSQL bool
UseTiDB bool
LogSQL bool
DBConnectRetries int
DBConnectBackoff time.Duration

// Indexer settings
Indexer struct {
Expand Down Expand Up @@ -986,6 +988,8 @@ func NewContext() {
}
IterateBufferSize = Cfg.Section("database").Key("ITERATE_BUFFER_SIZE").MustInt(50)
LogSQL = Cfg.Section("database").Key("LOG_SQL").MustBool(true)
DBConnectRetries = Cfg.Section("database").Key("DB_RETRIES").MustInt(10)
DBConnectBackoff = Cfg.Section("database").Key("DB_RETRY_BACKOFF").MustDuration(3 * time.Second)

sec = Cfg.Section("attachment")
AttachmentPath = sec.Key("PATH").MustString(path.Join(AppDataPath, "attachments"))
Expand Down
28 changes: 24 additions & 4 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package routers
import (
"path"
"strings"
"time"

"code.gitea.io/git"
"code.gitea.io/gitea/models"
Expand Down Expand Up @@ -42,6 +43,24 @@ func NewServices() {
cache.NewContext()
}

// In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
func initDBEngine() (err error) {
log.Info("Beginning ORM engine initialization.")
for i := 0; i < setting.DBConnectRetries; i++ {
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.DBConnectRetries)
if err := models.NewEngine(migrations.Migrate); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here should have been:

if err = ...

The reason the compiler didn't alarm is because there was a named err always available. I knew I never liked that feature of the language!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ur right. I did have it that way the first go around, then I changed it due to a compile error (which was for another unrelated problem). I currently have em on separate lines tho. u want me to condense it back down?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Push your current changes up, and let's see.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed it already. It didn't show up here I guess becz its merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I hit the "compare & new PR" button. u reckon?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well i went ahead and condensed it back down to a one-liner, sans the re-declaration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK the new PR is is #5780

break
} else if i == setting.DBConnectRetries-1 {
return err
}
log.Debug("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.DBConnectRetries, err)
log.Info("Backing off for %d seconds", int64(setting.DBConnectBackoff/time.Second))
time.Sleep(setting.DBConnectBackoff)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
}
models.HasEngine = true
return nil
}

// GlobalInit is for global configuration reload-able.
func GlobalInit() {
setting.NewContext()
Expand All @@ -56,11 +75,12 @@ func GlobalInit() {
if setting.InstallLock {
highlight.NewContext()
markup.Init()

if err := models.NewEngine(migrations.Migrate); err != nil {
log.Fatal(4, "Failed to initialize ORM engine: %v", err)
if err := initDBEngine(); err == nil {
log.Info("ORM engine initialization successful!")
} else {
log.Fatal(4, "ORM engine initialization failed: %v", err)
}
models.HasEngine = true

if err := models.InitOAuth2(); err != nil {
log.Fatal(4, "Failed to initialize OAuth2 support: %v", err)
}
Expand Down