Merge pull request #314 from fnproject/302

Implementing force delete for apps
This commit is contained in:
Reed Allman
2017-09-13 10:01:59 -07:00
committed by GitHub
16 changed files with 85 additions and 94 deletions

View File

@@ -51,10 +51,11 @@ func NewFuncLogger(ctx context.Context, appName, path, image, reqID string, logD
// we don't need to log per line to db, but we do need to limit it
limitw := newLimitWriter(MB, &dbWriter{
Buffer: dbuf,
db: logDB,
ctx: ctx,
reqID: reqID,
Buffer: dbuf,
db: logDB,
ctx: ctx,
reqID: reqID,
appName: appName,
})
// TODO / NOTE: we want linew to be first because limitw may error if limit
@@ -177,15 +178,16 @@ func (li *lineWriter) Close() error {
type dbWriter struct {
*bytes.Buffer
db models.LogStore
ctx context.Context
reqID string
db models.LogStore
ctx context.Context
reqID string
appName string
}
func (w *dbWriter) Close() error {
span, ctx := opentracing.StartSpanFromContext(context.Background(), "agent_log_write")
defer span.Finish()
return w.db.InsertLog(ctx, w.reqID, w.String())
return w.db.InsertLog(ctx, w.appName, w.reqID, w.String())
}
func (w *dbWriter) Write(b []byte) (int, error) {

View File

@@ -100,22 +100,22 @@ func (m *metricds) GetCalls(ctx context.Context, filter *models.CallFilter) ([]*
return m.ds.GetCalls(ctx, filter)
}
func (m *metricds) InsertLog(ctx context.Context, callID string, callLog string) error {
func (m *metricds) InsertLog(ctx context.Context, appName, callID, callLog string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_insert_log")
defer span.Finish()
return m.ds.InsertLog(ctx, callID, callLog)
return m.ds.InsertLog(ctx, appName, callID, callLog)
}
func (m *metricds) GetLog(ctx context.Context, callID string) (*models.CallLog, error) {
func (m *metricds) GetLog(ctx context.Context, appName, callID string) (*models.CallLog, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_get_log")
defer span.Finish()
return m.ds.GetLog(ctx, callID)
return m.ds.GetLog(ctx, appName, callID)
}
func (m *metricds) DeleteLog(ctx context.Context, callID string) error {
func (m *metricds) DeleteLog(ctx context.Context, appName, callID string) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "ds_delete_log")
defer span.Finish()
return m.ds.DeleteLog(ctx, callID)
return m.ds.DeleteLog(ctx, appName, callID)
}
// instant & no context ;)

View File

@@ -138,8 +138,8 @@ func (v *validator) GetCall(ctx context.Context, appName, callID string) (*model
return v.Datastore.GetCall(ctx, appName, callID)
}
func (v *validator) DeleteLog(ctx context.Context, callID string) error {
return v.Datastore.DeleteLog(ctx, callID)
func (v *validator) DeleteLog(ctx context.Context, appName, callID string) error {
return v.Datastore.DeleteLog(ctx, appName, callID)
}
// GetDatabase returns the underlying sqlx database implementation

View File

@@ -60,6 +60,8 @@ func (m *mock) UpdateApp(ctx context.Context, app *models.App) (*models.App, err
}
func (m *mock) RemoveApp(ctx context.Context, appName string) error {
m.batchDeleteCalls(ctx, appName)
m.batchDeleteRoutes(ctx, appName)
for i, a := range m.Apps {
if a.Name == appName {
m.Apps = append(m.Apps[:i], m.Apps[i+1:]...)
@@ -157,6 +159,28 @@ func (m *mock) GetCalls(ctx context.Context, filter *models.CallFilter) ([]*mode
return m.Calls, nil
}
func (m *mock) batchDeleteCalls(ctx context.Context, appName string) error {
newCalls := []*models.Call{}
for _, c := range m.Calls {
if c.AppName != appName {
newCalls = append(newCalls, c)
}
}
m.Calls = newCalls
return nil
}
func (m *mock) batchDeleteRoutes(ctx context.Context, appName string) error {
newRoutes := []*models.Route{}
for _, c := range m.Routes {
if c.AppName != appName {
newRoutes = append(newRoutes, c)
}
}
m.Routes = newRoutes
return nil
}
// GetDatabase returns nil here since shouldn't really be used
func (m *mock) GetDatabase() *sqlx.DB {
return nil

View File

@@ -61,6 +61,7 @@ var tables = [...]string{`CREATE TABLE IF NOT EXISTS routes (
`CREATE TABLE IF NOT EXISTS logs (
id varchar(256) NOT NULL PRIMARY KEY,
app_name varchar(256) NOT NULL,
log text NOT NULL
);`,
}
@@ -220,9 +221,18 @@ func (ds *sqlStore) UpdateApp(ctx context.Context, newapp *models.App) (*models.
}
func (ds *sqlStore) RemoveApp(ctx context.Context, appName string) error {
query := ds.db.Rebind(`DELETE FROM apps WHERE name = ?`)
_, err := ds.db.ExecContext(ctx, query, appName)
return err
res, err := ds.db.ExecContext(ctx, ds.db.Rebind(
`DELETE FROM apps WHERE name = ?;
DELETE FROM logs WHERE app_name=?;
DELETE FROM calls WHERE app_name=?;
DELETE FROM routes WHERE app_name=?;`), appName, appName, appName, appName)
if err != nil {
return err
}
if _, err := res.RowsAffected(); err != nil {
return models.ErrAppsNotFound
}
return nil
}
func (ds *sqlStore) GetApp(ctx context.Context, name string) (*models.App, error) {
@@ -589,15 +599,15 @@ func (ds *sqlStore) GetCalls(ctx context.Context, filter *models.CallFilter) ([]
return res, nil
}
func (ds *sqlStore) InsertLog(ctx context.Context, callID, callLog string) error {
query := ds.db.Rebind(`INSERT INTO logs (id, log) VALUES (?, ?);`)
_, err := ds.db.ExecContext(ctx, query, callID, callLog)
func (ds *sqlStore) InsertLog(ctx context.Context, appName, callID, callLog string) error {
query := ds.db.Rebind(`INSERT INTO logs (id, app_name, log) VALUES (?, ?, ?);`)
_, err := ds.db.ExecContext(ctx, query, callID, appName, callLog)
return err
}
func (ds *sqlStore) GetLog(ctx context.Context, callID string) (*models.CallLog, error) {
query := ds.db.Rebind(`SELECT log FROM logs WHERE id=?`)
row := ds.db.QueryRowContext(ctx, query, callID)
func (ds *sqlStore) GetLog(ctx context.Context, appName, callID string) (*models.CallLog, error) {
query := ds.db.Rebind(`SELECT log FROM logs WHERE id=? AND app_name=?`)
row := ds.db.QueryRowContext(ctx, query, callID, appName)
var log string
err := row.Scan(&log)
@@ -609,14 +619,15 @@ func (ds *sqlStore) GetLog(ctx context.Context, callID string) (*models.CallLog,
}
return &models.CallLog{
CallID: callID,
Log: log,
CallID: callID,
Log: log,
AppName: appName,
}, nil
}
func (ds *sqlStore) DeleteLog(ctx context.Context, callID string) error {
query := ds.db.Rebind(`DELETE FROM logs WHERE id=?`)
_, err := ds.db.ExecContext(ctx, query, callID)
func (ds *sqlStore) DeleteLog(ctx context.Context, appName, callID string) error {
query := ds.db.Rebind(`DELETE FROM logs WHERE id=? AND app_name=?`)
_, err := ds.db.ExecContext(ctx, query, callID, appName)
return err
}

View File

@@ -27,12 +27,12 @@ func (m *mock) SetDatastore(ctx context.Context, ds models.Datastore) {
m.ds = ds
}
func (m *mock) InsertLog(ctx context.Context, callID string, callLog string) error {
func (m *mock) InsertLog(ctx context.Context, appName, callID, callLog string) error {
m.Logs[callID] = &models.CallLog{CallID: callID, Log: callLog}
return nil
}
func (m *mock) GetLog(ctx context.Context, callID string) (*models.CallLog, error) {
func (m *mock) GetLog(ctx context.Context, appName, callID string) (*models.CallLog, error) {
logEntry := m.Logs[callID]
if logEntry == nil {
return nil, errors.New("Call log not found")
@@ -41,7 +41,7 @@ func (m *mock) GetLog(ctx context.Context, callID string) (*models.CallLog, erro
return m.Logs[callID], nil
}
func (m *mock) DeleteLog(ctx context.Context, callID string) error {
func (m *mock) DeleteLog(ctx context.Context, appName, callID string) error {
delete(m.Logs, callID)
return nil
}

View File

@@ -44,7 +44,7 @@ func Test(t *testing.T, fnl models.LogStore, ds models.Datastore) {
if err != nil {
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
}
err = fnl.InsertLog(ctx, call.ID, "test")
err = fnl.InsertLog(ctx, call.AppName, call.ID, "test")
if err != nil {
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
}
@@ -56,11 +56,11 @@ func Test(t *testing.T, fnl models.LogStore, ds models.Datastore) {
if err != nil {
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
}
err = fnl.InsertLog(ctx, call.ID, logText)
err = fnl.InsertLog(ctx, call.AppName, call.ID, logText)
if err != nil {
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
}
logEntry, err := fnl.GetLog(ctx, call.ID)
logEntry, err := fnl.GetLog(ctx, call.AppName, call.ID)
if !strings.Contains(logEntry.Log, logText) {
t.Fatalf("Test GetLog(ctx, call.ID, logText): unexpected error, log mismatch. "+
"Expected: `%v`. Got `%v`.", logText, logEntry.Log)
@@ -73,16 +73,16 @@ func Test(t *testing.T, fnl models.LogStore, ds models.Datastore) {
if err != nil {
t.Fatalf("Test InsertCall(ctx, &call): unexpected error `%v`", err)
}
err = fnl.InsertLog(ctx, call.ID, logText)
err = fnl.InsertLog(ctx, call.AppName, call.ID, logText)
if err != nil {
t.Fatalf("Test InsertLog(ctx, call.ID, logText): unexpected error during inserting log `%v`", err)
}
logEntry, err := fnl.GetLog(ctx, call.ID)
logEntry, err := fnl.GetLog(ctx, call.AppName, call.ID)
if !strings.Contains(logEntry.Log, logText) {
t.Fatalf("Test GetLog(ctx, call.ID, logText): unexpected error, log mismatch. "+
"Expected: `%v`. Got `%v`.", logText, logEntry.Log)
}
err = fnl.DeleteLog(ctx, call.ID)
err = fnl.DeleteLog(ctx, call.AppName, call.ID)
if err != nil {
t.Fatalf("Test DeleteLog(ctx, call.ID): unexpected error during deleting log `%v`", err)
}

View File

@@ -23,8 +23,9 @@ const (
var possibleStatuses = [...]string{"delayed", "queued", "running", "success", "error", "cancelled"}
type CallLog struct {
CallID string `json:"call_id"`
Log string `json:"log"`
CallID string `json:"call_id"`
Log string `json:"log"`
AppName string `json:"app_name"`
}
// Call is a representation of a specific invocation of a route.

View File

@@ -28,7 +28,6 @@ type Datastore interface {
// RemoveApp removes the App named appName. Returns ErrDatastoreEmptyAppName if appName is empty.
// Returns ErrAppsNotFound if an App is not found.
// TODO remove routes automatically? #528
RemoveApp(ctx context.Context, appName string) error
// GetRoute looks up a matching Route for appName and the literal request route routePath.

View File

@@ -11,13 +11,13 @@ type LogStore interface {
// InsertLog will insert the log at callID, overwriting if it previously
// existed.
InsertLog(ctx context.Context, callID string, callLog string) error
InsertLog(ctx context.Context, appName, callID string, callLog string) error
// GetLog will return the log at callID, an error will be returned if the log
// cannot be found.
GetLog(ctx context.Context, callID string) (*CallLog, error)
GetLog(ctx context.Context, appName, callID string) (*CallLog, error)
// DeleteLog will remove the log at callID, it will not return an error if
// the log does not exist before removal.
DeleteLog(ctx context.Context, callID string) error
DeleteLog(ctx context.Context, appName, callID string) error
}

View File

@@ -15,30 +15,7 @@ func (s *Server) handleAppDelete(c *gin.Context) {
app := &models.App{Name: c.MustGet(api.AppName).(string)}
routes, err := s.Datastore.GetRoutesByApp(ctx, app.Name, &models.RouteFilter{})
if err != nil {
log.WithError(err).Error("error getting route in app delete")
handleErrorResponse(c, err)
return
}
//TODO allow this? #528
if len(routes) > 0 {
handleErrorResponse(c, models.ErrDeleteAppsWithRoutes)
return
}
err = s.FireBeforeAppDelete(ctx, app)
if err != nil {
log.WithError(err).Error("error firing before app delete")
handleErrorResponse(c, err)
return
}
app, err = s.Datastore.GetApp(ctx, app.Name)
if err != nil {
handleErrorResponse(c, err)
return
}
err := s.FireBeforeAppDelete(ctx, app)
err = s.Datastore.RemoveApp(ctx, app.Name)
if err != nil {

View File

@@ -18,7 +18,7 @@ func (s *Server) handleCallLogGet(c *gin.Context) {
return
}
callObj, err := s.LogDB.GetLog(ctx, callID)
callObj, err := s.LogDB.GetLog(ctx, appName, callID)
if err != nil {
handleErrorResponse(c, err)
return
@@ -37,7 +37,7 @@ func (s *Server) handleCallLogDelete(c *gin.Context) {
handleErrorResponse(c, err)
return
}
err = s.LogDB.DeleteLog(ctx, callID)
err = s.LogDB.DeleteLog(ctx, appName, callID)
if err != nil {
handleErrorResponse(c, err)
return

View File

@@ -121,7 +121,6 @@ func TestFullStack(t *testing.T) {
{"execute myroute2", "POST", "/r/myapp/myroute2", `{ "name": "Teste" }`, http.StatusInternalServerError, 2},
{"get myroute2", "GET", "/v1/apps/myapp/routes/myroute2", ``, http.StatusOK, 2},
{"delete myroute", "DELETE", "/v1/apps/myapp/routes/myroute", ``, http.StatusOK, 1},
{"delete app (fail)", "DELETE", "/v1/apps/myapp", ``, http.StatusConflict, 1},
{"delete myroute2", "DELETE", "/v1/apps/myapp/routes/myroute2", ``, http.StatusOK, 0},
{"delete app (success)", "DELETE", "/v1/apps/myapp", ``, http.StatusOK, 0},
{"get deleted app", "GET", "/v1/apps/myapp", ``, http.StatusNotFound, 0},