-
Notifications
You must be signed in to change notification settings - Fork 23
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
backend: Add scalable PostgreSQL backend #74
Closed
Closed
Changes from all commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
5720905
last anchor cleanup
amass01 5177c57
add schema.md to describe sql tables
amass01 bca6341
add records table description
amass01 33df874
add anchors table
amass01 c566934
opps
amass01 b9dae82
typo
amass01 2533029
add postgres package & struct
amass01 52ec32f
add backend config & interface funcs signatures
amass01 a3bbf17
setup postgres when configured
amass01 62798d4
add postgres connection info to config and validate
amass01 73eb7fd
provide all postgres ssl vars & establish connection
amass01 3317ae5
add scripts to generate postgres ssl certs & initial setup
amass01 c7129c4
postgress connection last touches
amass01 49bcd14
add createTables func
amass01 086a6f7
create anchors table if doesn't exsist
amass01 9d38731
add pg log & create pg tables if doesn't exist
amass01 d9afd5a
Implement backend Put func
amass01 f6a96e0
polish Put func
amass01 708c017
add unique indexes for sql cols
amass01 07dc8c7
.md land
amass01 c16c9e8
add unique indexes in sql table def.
amass01 68add0f
move sql logic to separate file - postgres/sql.go
amass01 9f8c03e
move postgres user to postgres.go
amass01 dda5331
delete dbuser from log
amass01 f52214e
ditch `key` col & use digest as primary key in records table
amass01 b6cddb6
Implement Close interface func
amass01 0e893c4
add GetBalance impl.
amass01 7f3d4a7
get wip
amass01 6328827
handle digest doesn't exist case
amass01 34b3dfc
add missing cols to get & change merkle root col type to bytea
amass01 59c339a
ErrorCode = backend.ErrorOK when digest found in Get
amass01 5731cc0
add flush logic - collect unflushed reocrds and calc merkle
amass01 c742744
insert anchor data into db
amass01 0bac395
fix anchor merkle insert and update records' merkle col
amass01 e8d5d7c
lazy flush anchor on digest get when chain timestamp isn't
amass01 aef715e
cleanup lazyflush
amass01 6ea6e68
- Get DISTINCT timestamps when flushing,
amass01 8abb29b
implement GetTimestamps interface func
amass01 1beed9e
cleanup GetTimestamps func
amass01 74eb336
update schema col types
amass01 f369711
query string indentation & tx hash col type bytea
amass01 c0ee982
adjust code to tx hash col type to bytea
amass01 38f8adb
add LastAnchor interface func
amass01 eee5982
add Fsck :beach:
amass01 c52cd06
cleanup Fsck
amass01 7f271b1
add postgres backend support for dcrtime_dumpdb
amass01 9f88cf6
fix duplicates in postgres Dump and preprare for Restore
amass01 2e643a7
cleanup logs
amass01 5f8956b
fix cli
amass01 0c73d1f
add postgres Restore implementation
amass01 8fd25b4
cleanup
amass01 90f3674
delete commited log file
amass01 2bbdd99
log root only when filesystem is used
amass01 f5ea722
`dcrtime_fsck`: require -destination flag only for filesystem backend
amass01 67fb76b
add sql models
amass01 efaa1de
fix get timestamp
amass01 86248ab
dump hashes in reverse to keep insertion order
amass01 14cb20b
swallow "no rows in result" error when updating anchor's chain timestamp
amass01 7f78482
ditch testing flag from Postgres struct & add TestPostgres
amass01 45613db
check rows.Err()
amass01 6e8ff5d
satisfy linter
amass01 3642168
cleanup
amass01 1aebba6
opps
amass01 522cf11
add failing tests - chunk 1
amass01 ca27cd1
fix duplicated digest in tests
amass01 d3b8aaf
finish tests
amass01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2020 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/jessevdk/go-flags" | ||
) | ||
|
||
const defaultConfigFilename = "dcrtimed.conf" | ||
|
||
var ( | ||
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename) | ||
defaultBackend = "filesystem" | ||
) | ||
|
||
// config defines the configuration options for dcrtime_fsck | ||
// | ||
// See loadConfig for details on the configuration load process. | ||
type config struct { | ||
HomeDir string `short:"A" long:"appdata" description:"Path to application home directory"` | ||
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` | ||
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"` | ||
DataDir string `short:"b" long:"datadir" description:"Directory to store data"` | ||
LogDir string `long:"logdir" description:"Directory to log output."` | ||
TestNet bool `long:"testnet" description:"Use the test network"` | ||
SimNet bool `long:"simnet" description:"Use the simulation test network"` | ||
Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` | ||
CPUProfile string `long:"cpuprofile" description:"Write CPU profile to the specified file"` | ||
MemProfile string `long:"memprofile" description:"Write mem profile to the specified file"` | ||
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level for all subsystems {trace, debug, info, warn, error, critical} -- You may also specify <subsystem>=<level>,<subsystem2>=<level>,... to set the log level for individual subsystems -- Use show to list available subsystems"` | ||
Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 49152, testnet: 59152)"` | ||
WalletHost string `long:"wallethost" description:"Hostname for wallet server"` | ||
WalletCert string `long:"walletcert" description:"Certificate path for wallet server"` | ||
WalletPassphrase string `long:"walletpassphrase" description:"Passphrase for wallet server"` | ||
Version string | ||
HTTPSCert string `long:"httpscert" description:"File containing the https certificate file"` | ||
HTTPSKey string `long:"httpskey" description:"File containing the https certificate key"` | ||
StoreHost string `long:"storehost" description:"Enable proxy mode - send requests to the specified ip:port"` | ||
StoreCert string `long:"storecert" description:"File containing the https certificate file for storehost"` | ||
EnableCollections bool `long:"enablecollections" description:"Allow clients to query collection timestamps."` | ||
APITokens []string `long:"apitoken" description:"Token used to grant access to privileged API resources"` | ||
APIVersions string `long:"apiversions" description:"Enables API versions on the daemon"` | ||
Backend string `long:"backend" description:"Sets the cache layer type 'filesystem'/'postgres'"` | ||
PostgresHost string `long:"postgreshost" description:"Postgres ip:port"` | ||
PostgresRootCert string `long:"postgresrootcert" description:"File containing the CA certificate for postgres"` | ||
PostgresCert string `long:"postgrescert" description:"File containing the dcrtimed client certificate for postgres"` | ||
PostgresKey string `long:"postgreskey" description:"File containing the dcrtimed client certificate key for postgres"` | ||
} | ||
|
||
// loadConfig initializes and parses the config using a config file | ||
func loadConfig() (*config, error) { | ||
// Default config. | ||
cfg := config{ | ||
Backend: defaultBackend, | ||
} | ||
|
||
err := flags.IniParse(defaultConfigFile, &cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cfg, nil | ||
} |
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,67 @@ | ||
// Copyright (c) 2020 The Decred developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/jessevdk/go-flags" | ||
) | ||
|
||
const defaultConfigFilename = "dcrtimed.conf" | ||
|
||
var ( | ||
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename) | ||
defaultBackend = "filesystem" | ||
) | ||
|
||
// config defines the configuration options for dcrtime_fsck | ||
// | ||
// See loadConfig for details on the configuration load process. | ||
type config struct { | ||
HomeDir string `short:"A" long:"appdata" description:"Path to application home directory"` | ||
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"` | ||
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"` | ||
DataDir string `short:"b" long:"datadir" description:"Directory to store data"` | ||
LogDir string `long:"logdir" description:"Directory to log output."` | ||
TestNet bool `long:"testnet" description:"Use the test network"` | ||
SimNet bool `long:"simnet" description:"Use the simulation test network"` | ||
Profile string `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65536"` | ||
CPUProfile string `long:"cpuprofile" description:"Write CPU profile to the specified file"` | ||
MemProfile string `long:"memprofile" description:"Write mem profile to the specified file"` | ||
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level for all subsystems {trace, debug, info, warn, error, critical} -- You may also specify <subsystem>=<level>,<subsystem2>=<level>,... to set the log level for individual subsystems -- Use show to list available subsystems"` | ||
Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 49152, testnet: 59152)"` | ||
WalletHost string `long:"wallethost" description:"Hostname for wallet server"` | ||
WalletCert string `long:"walletcert" description:"Certificate path for wallet server"` | ||
WalletPassphrase string `long:"walletpassphrase" description:"Passphrase for wallet server"` | ||
Version string | ||
HTTPSCert string `long:"httpscert" description:"File containing the https certificate file"` | ||
HTTPSKey string `long:"httpskey" description:"File containing the https certificate key"` | ||
StoreHost string `long:"storehost" description:"Enable proxy mode - send requests to the specified ip:port"` | ||
StoreCert string `long:"storecert" description:"File containing the https certificate file for storehost"` | ||
EnableCollections bool `long:"enablecollections" description:"Allow clients to query collection timestamps."` | ||
APITokens []string `long:"apitoken" description:"Token used to grant access to privileged API resources"` | ||
APIVersions string `long:"apiversions" description:"Enables API versions on the daemon"` | ||
Backend string `long:"backend" description:"Sets the cache layer type 'filesystem'/'postgres'"` | ||
PostgresHost string `long:"postgreshost" description:"Postgres ip:port"` | ||
PostgresRootCert string `long:"postgresrootcert" description:"File containing the CA certificate for postgres"` | ||
PostgresCert string `long:"postgrescert" description:"File containing the dcrtimed client certificate for postgres"` | ||
PostgresKey string `long:"postgreskey" description:"File containing the dcrtimed client certificate key for postgres"` | ||
} | ||
|
||
// loadConfig initializes and parses the config using a config file | ||
func loadConfig() (*config, error) { | ||
// Default config. | ||
cfg := config{ | ||
Backend: defaultBackend, | ||
} | ||
|
||
err := flags.IniParse(defaultConfigFile, &cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cfg, nil | ||
} |
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 |
---|---|---|
|
@@ -934,33 +934,34 @@ func (fs *FileSystem) LastAnchor() (*backend.LastAnchorResult, error) { | |
var fr *backend.FlushRecord | ||
var me backend.LastAnchorResult | ||
payload, err := db.Get([]byte(flushedKey), nil) | ||
if err == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed from:
to:
|
||
fr, err = DecodeFlushRecord(payload) | ||
if err != nil { | ||
return &me, err | ||
} | ||
me.Tx = fr.Tx | ||
if err != nil { | ||
return &backend.LastAnchorResult{}, err | ||
} | ||
|
||
// Close db conection as we may | ||
// write & update it | ||
db.Close() | ||
fr, err = DecodeFlushRecord(payload) | ||
if err != nil { | ||
return &me, err | ||
} | ||
me.Tx = fr.Tx | ||
|
||
// Lookup anchored tx info, | ||
// and update db if info changed. | ||
txWalletInfo, err := fs.lazyFlush(flushedTs, fr) | ||
// Close db conection as we may | ||
// write & update it | ||
db.Close() | ||
|
||
// If no error, or no enough confirmations | ||
// err continue, else return err. | ||
if err != nil && err != errNotEnoughConfirmation { | ||
return &backend.LastAnchorResult{}, err | ||
} | ||
me.ChainTimestamp = fr.ChainTimestamp | ||
me.BlockHash = txWalletInfo.BlockHash.String() | ||
me.BlockHeight = txWalletInfo.BlockHeight | ||
return &me, nil | ||
// Lookup anchored tx info, | ||
// and update db if info changed. | ||
txWalletInfo, err := fs.lazyFlush(flushedTs, fr) | ||
|
||
// If no error, or no enough confirmations | ||
// err continue, else return err. | ||
if err != nil && err != errNotEnoughConfirmation { | ||
return &backend.LastAnchorResult{}, err | ||
} | ||
|
||
return &backend.LastAnchorResult{}, err | ||
me.ChainTimestamp = fr.ChainTimestamp | ||
me.BlockHash = txWalletInfo.BlockHash.String() | ||
me.BlockHeight = txWalletInfo.BlockHeight | ||
return &me, nil | ||
} | ||
|
||
// GetBalance provides the balance of the wallet and satisfies the | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we avoid listing all
dcrtimed
params here and use only the subset we need ? 🤔