datastore no longer implements logstore (#1013)

* datastore no longer implements logstore

the underlying implementation of our sql store implements both the datastore
and the logstore interface, however going forward we are likely to encounter
datastore implementers that would mock out the logstore interface and not use
its methods - signalling a poor interface. this remedies that, now they are 2
completely separate things, which our sqlstore happens to implement both of.

related to some recent changes around wrapping, this keeps the imposed metrics
and validation wrapping of a servers logstore and datastore, just moving it
into New instead of in the opts - this is so that a user can have the
underlying datastore in order to set the logstore to it, since wrapping it in
a validator/metrics would render it no longer a logstore implementer (i.e.
validate datastore doesn't implement the logstore interface), we need to do
this after setting the logstore to the datastore if one wasn't provided
explicitly.

* splits logstore and datastore metrics & validation logic
* `make test` should be `make full-test` always. got rid of that so that
nobody else has to wait for CI to blow up on them after the tests pass locally
ever again.

* fix new tests
This commit is contained in:
Reed Allman
2018-06-04 00:08:16 -07:00
committed by GitHub
parent e0425abd19
commit 00c29b8bf3
17 changed files with 250 additions and 127 deletions

View File

@@ -8,7 +8,9 @@ import (
"github.com/fnproject/fn/api/common"
"github.com/fnproject/fn/api/datastore/sql"
"github.com/fnproject/fn/api/logs/metrics"
"github.com/fnproject/fn/api/logs/s3"
"github.com/fnproject/fn/api/logs/validator"
"github.com/fnproject/fn/api/models"
"github.com/sirupsen/logrus"
)
@@ -20,12 +22,19 @@ func New(ctx context.Context, dbURL string) (models.LogStore, error) {
log.WithError(err).WithFields(logrus.Fields{"url": dbURL}).Fatal("bad DB URL")
}
log.WithFields(logrus.Fields{"db": u.Scheme}).Debug("creating log store")
var ls models.LogStore
switch u.Scheme {
case "sqlite3", "postgres", "mysql":
return sql.New(ctx, u)
ls, err = sql.New(ctx, u)
case "s3":
return s3.New(u)
ls, err = s3.New(u)
default:
return nil, fmt.Errorf("db type not supported %v", u.Scheme)
err = fmt.Errorf("db type not supported %v", u.Scheme)
}
return ls, err
}
func Wrap(ls models.LogStore) models.LogStore {
return validator.NewValidator(metrics.NewLogstore(ls))
}

View File

@@ -0,0 +1,51 @@
package metrics
import (
"context"
"io"
"github.com/fnproject/fn/api/models"
"go.opencensus.io/trace"
)
func NewLogstore(ls models.LogStore) models.LogStore {
return &metricls{ls}
}
type metricls struct {
ls models.LogStore
}
func (m *metricls) InsertCall(ctx context.Context, call *models.Call) error {
ctx, span := trace.StartSpan(ctx, "ls_insert_call")
defer span.End()
return m.ls.InsertCall(ctx, call)
}
func (m *metricls) GetCall(ctx context.Context, appName, callID string) (*models.Call, error) {
ctx, span := trace.StartSpan(ctx, "ls_get_call")
defer span.End()
return m.ls.GetCall(ctx, appName, callID)
}
func (m *metricls) GetCalls(ctx context.Context, filter *models.CallFilter) ([]*models.Call, error) {
ctx, span := trace.StartSpan(ctx, "ls_get_calls")
defer span.End()
return m.ls.GetCalls(ctx, filter)
}
func (m *metricls) InsertLog(ctx context.Context, appName, callID string, callLog io.Reader) error {
ctx, span := trace.StartSpan(ctx, "ls_insert_log")
defer span.End()
return m.ls.InsertLog(ctx, appName, callID, callLog)
}
func (m *metricls) GetLog(ctx context.Context, appName, callID string) (io.Reader, error) {
ctx, span := trace.StartSpan(ctx, "ls_get_log")
defer span.End()
return m.ls.GetLog(ctx, appName, callID)
}
func (m *metricls) Close() error {
return m.ls.Close()
}

View File

@@ -0,0 +1,60 @@
package validator
import (
"context"
"io"
"github.com/fnproject/fn/api/models"
)
func NewValidator(ls models.LogStore) models.LogStore {
return &validator{ls}
}
type validator struct {
models.LogStore
}
// callID or appID will never be empty.
func (v *validator) InsertLog(ctx context.Context, appID, callID string, callLog io.Reader) error {
if callID == "" {
return models.ErrDatastoreEmptyCallID
}
if appID == "" {
return models.ErrDatastoreEmptyAppID
}
return v.LogStore.InsertLog(ctx, appID, callID, callLog)
}
// callID or appID will never be empty.
func (v *validator) GetLog(ctx context.Context, appID, callID string) (io.Reader, error) {
if callID == "" {
return nil, models.ErrDatastoreEmptyCallID
}
if appID == "" {
return nil, models.ErrDatastoreEmptyAppID
}
return v.LogStore.GetLog(ctx, appID, callID)
}
// callID or appID will never be empty.
func (v *validator) InsertCall(ctx context.Context, call *models.Call) error {
if call.ID == "" {
return models.ErrDatastoreEmptyCallID
}
if call.AppID == "" {
return models.ErrDatastoreEmptyAppID
}
return v.LogStore.InsertCall(ctx, call)
}
// callID or appID will never be empty.
func (v *validator) GetCall(ctx context.Context, appID, callID string) (*models.Call, error) {
if callID == "" {
return nil, models.ErrDatastoreEmptyCallID
}
if appID == "" {
return nil, models.ErrDatastoreEmptyAppID
}
return v.LogStore.GetCall(ctx, appID, callID)
}