Skip to content

Commit

Permalink
Remove storage cache layer (#324)
Browse files Browse the repository at this point in the history
The following changes have been made:
* Open *sql.DB outside of storage (and reuse the same DB for all storage
  objects)
* Test refactors (storage/mysql):
  * Moved DB-handling methods to storage_test (which contains TestMain)
  * Added a *sql.DB parameter to methods that access storage
  * Open DB connection only once for all tests
  * Cleaned DB at the beginning of every test

The changes above render the storage caching layer obsolete.
  • Loading branch information
Alan Parra committed Jan 25, 2017
1 parent a7e8e00 commit 7ac8e0d
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 453 deletions.
19 changes: 15 additions & 4 deletions extension/builtin/default_registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builtin

import (
"database/sql"
"flag"

_ "github.com/go-sql-driver/mysql" // Load MySQL driver
Expand All @@ -15,19 +16,29 @@ var MySQLURIFlag = flag.String("mysql_uri", "test:zaphod@tcp(127.0.0.1:3306)/tes
"uri to use with mysql storage")

// Default implementation of extension.Registry.
type defaultRegistry struct{}
type defaultRegistry struct {
db *sql.DB
}

// TODO(codingllama): Get rid of the error return
func (r defaultRegistry) GetLogStorage(treeID int64) (storage.LogStorage, error) {
return mysql.NewLogStorage(treeID, *MySQLURIFlag)
return mysql.NewLogStorage(treeID, r.db)
}

// TODO(codingllama): Get rid of the error return
func (r defaultRegistry) GetMapStorage(treeID int64) (storage.MapStorage, error) {
return mysql.NewMapStorage(treeID, *MySQLURIFlag)
return mysql.NewMapStorage(treeID, r.db)
}

// NewDefaultExtensionRegistry returns the default extension.Registry implementation, which is
// backed by a MySQL database and configured via flags.
// The returned registry is wraped in a cached registry.
func NewDefaultExtensionRegistry() (extension.Registry, error) {
return extension.NewCachedRegistry(defaultRegistry{}), nil
db, err := mysql.OpenDB(*MySQLURIFlag)
if err != nil {
return nil, err
}
return &defaultRegistry{
db: db,
}, nil
}
72 changes: 0 additions & 72 deletions extension/cached_registry.go

This file was deleted.

129 changes: 0 additions & 129 deletions extension/cached_registry_test.go

This file was deleted.

9 changes: 5 additions & 4 deletions storage/mysql/log_admin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mysql

import (
"database/sql"
"fmt"

"github.com/google/trillian/crypto"
Expand Down Expand Up @@ -29,10 +30,10 @@ const (

// CreateTree instantiates a new log with default parameters.
// TODO(codinglama): Move to admin API when the admin API is created.
func CreateTree(treeID int64, dbURL string) error {
func CreateTree(treeID int64, db *sql.DB) error {
// TODO(codinglama) replace with a GetDatabase from the new extension API when LogID is removed.
th := merkle.NewRFC6962TreeHasher(crypto.NewSHA256())
m, err := newTreeStorage(treeID, dbURL, th.Size(), defaultLogStrata, cache.PopulateLogSubtreeNodes(th))
m, err := newTreeStorage(treeID, db, th.Size(), defaultLogStrata, cache.PopulateLogSubtreeNodes(th))
if err != nil {
return fmt.Errorf("couldn't create a new treeStorage: %s", err)
}
Expand Down Expand Up @@ -60,10 +61,10 @@ func CreateTree(treeID int64, dbURL string) error {
}

// DeleteTree deletes a tree by the treeID.
func DeleteTree(treeID int64, dbURL string) error {
func DeleteTree(treeID int64, db *sql.DB) error {
// TODO(codinglama) replace with a GetDatabase from the new extension API when LogID is removed.
th := merkle.NewRFC6962TreeHasher(crypto.NewSHA256())
m, err := newTreeStorage(treeID, dbURL, th.Size(), defaultLogStrata, cache.PopulateLogSubtreeNodes(th))
m, err := newTreeStorage(treeID, db, th.Size(), defaultLogStrata, cache.PopulateLogSubtreeNodes(th))
if err != nil {
return fmt.Errorf("couldn't create a new treeStorage: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions storage/mysql/log_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ type mySQLLogStorage struct {
}

// NewLogStorage creates a mySQLLogStorage instance for the specified MySQL URL.
func NewLogStorage(id int64, dbURL string) (storage.LogStorage, error) {
func NewLogStorage(id int64, db *sql.DB) (storage.LogStorage, error) {
// TODO(al): pass this through/configure from DB
th := merkle.NewRFC6962TreeHasher(crypto.NewSHA256())
ts, err := newTreeStorage(id, dbURL, th.Size(), defaultLogStrata, cache.PopulateLogSubtreeNodes(th))
ts, err := newTreeStorage(id, db, th.Size(), defaultLogStrata, cache.PopulateLogSubtreeNodes(th))
if err != nil {
return nil, fmt.Errorf("couldn't create a new treeStorage: %s", err)
}
Expand Down
Loading

0 comments on commit 7ac8e0d

Please sign in to comment.