Skip to content

Commit

Permalink
api,app,db: create log collection on app creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ggarnier committed Dec 6, 2018
1 parent 1555af3 commit b390dce
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 48 deletions.
3 changes: 2 additions & 1 deletion api/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4292,7 +4292,8 @@ func (s *S) TestAppLogSelectByLinesShouldReturnTheLastestEntries(c *check.C) {
err := app.CreateApp(&a, s.user)
c.Assert(err, check.IsNil)
now := time.Now()
coll := s.logConn.Logs(a.Name)
coll, err := s.logConn.CreateAppLogCollection(a.Name)
c.Assert(err, check.IsNil)
for i := 0; i < 15; i++ {
l := app.Applog{
Date: now.Add(time.Duration(i) * time.Hour),
Expand Down
2 changes: 1 addition & 1 deletion api/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *BuildSuite) TearDownSuite(c *check.C) {
config.Unset("docker:router")
pool.RemovePool("pool1")
s.conn.Apps().Database.DropDatabase()
s.logConn.Logs("myapp").Database.DropDatabase()
s.logConn.AppLogCollection("myapp").Database.DropDatabase()
s.conn.Close()
s.logConn.Close()
s.reset()
Expand Down
2 changes: 1 addition & 1 deletion api/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (s *DeploySuite) TearDownSuite(c *check.C) {
config.Unset("docker:router")
pool.RemovePool("pool1")
s.conn.Apps().Database.DropDatabase()
s.logConn.Logs("myapp").Database.DropDatabase()
s.logConn.AppLogCollection("myapp").Database.DropDatabase()
s.conn.Close()
s.logConn.Close()
s.reset()
Expand Down
2 changes: 1 addition & 1 deletion api/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *EventSuite) SetUpSuite(c *check.C) {

func (s *EventSuite) TearDownSuite(c *check.C) {
s.conn.Apps().Database.DropDatabase()
s.logConn.Logs("myapp").Database.DropDatabase()
s.logConn.AppLogCollection("myapp").Database.DropDatabase()
s.conn.Close()
s.logConn.Close()
}
Expand Down
6 changes: 4 additions & 2 deletions api/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func (s *S) SetUpTest(c *check.C) {
dbtest.ClearAllCollections(s.conn.Apps().Database)
s.logConn, err = db.LogConn()
c.Assert(err, check.IsNil)
dbtest.ClearAllCollections(s.logConn.Logs("myapp").Database)
appLogColl := s.logConn.AppLogCollection("myapp")
appLogColl.DropCollection()
dbtest.ClearAllCollections(appLogColl.Database)
s.createUserAndTeam(c)
s.provisioner = provisiontest.ProvisionerInstance
s.provisioner.Reset()
Expand Down Expand Up @@ -187,7 +189,7 @@ func (s *S) TearDownSuite(c *check.C) {
logConn, err := db.LogConn()
c.Assert(err, check.IsNil)
defer logConn.Close()
logConn.Logs("myapp").Database.DropDatabase()
logConn.AppLogCollection("myapp").Database.DropDatabase()
}

func userWithPermission(c *check.C, perm ...permission.Permission) auth.Token {
Expand Down
73 changes: 52 additions & 21 deletions app/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,68 @@ var insertApp = action.Action{
default:
return nil, errors.New("First parameter must be *App.")
}
conn, err := db.Conn()
err := createApp(app)
if err != nil {
return nil, err
}
defer conn.Close()
if app.Quota == (quota.Quota{}) {
app.Quota = quota.UnlimitedQuota
}
var limit int
if limit, err = config.GetInt("quota:units-per-app"); err == nil {
app.Quota.Limit = limit
}
err = conn.Apps().Insert(app)
if mgo.IsDup(err) {
return nil, ErrAppAlreadyExists
}
return app, err
return app, nil
},
Backward: func(ctx action.BWContext) {
app := ctx.FWResult.(*App)
conn, err := db.Conn()
if err != nil {
log.Errorf("Could not connect to the database: %s", err)
return
}
defer conn.Close()
conn.Apps().Remove(bson.M{"name": app.Name})
removeApp(app)
},
MinParams: 1,
}

func createApp(app *App) error {
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
if app.Quota == (quota.Quota{}) {
app.Quota = quota.UnlimitedQuota
}
var limit int
if limit, err = config.GetInt("quota:units-per-app"); err == nil {
app.Quota.Limit = limit
}
err = conn.Apps().Insert(app)
if mgo.IsDup(err) {
return ErrAppAlreadyExists
}

logConn, err := db.LogConn()
if err != nil {
return err
}
defer logConn.Close()
_, err = logConn.CreateAppLogCollection(app.Name)
return err
}

func removeApp(app *App) error {
logConn, err := db.LogConn()
if err != nil {
log.Errorf("Could not connect to the log database: %s", err)
} else {
defer logConn.Close()
err = logConn.AppLogCollection(app.Name).DropCollection()
if err != nil {
log.Errorf("Unable to remove logs collection: %s", err)
}
}

conn, err := db.Conn()
if err != nil {
log.Errorf("Could not connect to the database: %s", err)
return err
}
defer conn.Close()
conn.Apps().Remove(bson.M{"name": app.Name})
return nil
}

// createAppToken generates a token for the app and saves it in the database.
// It requires a pointer to an App instance as the first parameter.
var createAppToken = action.Action{
Expand Down
20 changes: 20 additions & 0 deletions app/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/tsuru/tsuru/action"
"github.com/tsuru/tsuru/app/bind"
"github.com/tsuru/tsuru/auth"
"github.com/tsuru/tsuru/db"
"github.com/tsuru/tsuru/provision"
"github.com/tsuru/tsuru/provision/pool"
"github.com/tsuru/tsuru/provision/provisiontest"
Expand All @@ -23,6 +24,21 @@ import (
check "gopkg.in/check.v1"
)

func (s *S) appLogCollectionExists(appName string) bool {
_, dbname := db.DbConfig("logdb-")
collNames, err := s.logConn.Database(dbname).CollectionNames()
if err != nil {
return false
}
appLogCollName := "logs_" + appName
for _, collName := range collNames {
if collName == appLogCollName {
return true
}
}
return false
}

func (s *S) TestReserveUserAppName(c *check.C) {
c.Assert(reserveUserApp.Name, check.Equals, "reserve-user-app")
}
Expand Down Expand Up @@ -73,6 +89,8 @@ func (s *S) TestInsertAppForward(c *check.C) {
gotApp, err := GetByName(app.Name)
c.Assert(err, check.IsNil)
c.Assert(gotApp.Quota, check.DeepEquals, quota.UnlimitedQuota)

c.Assert(s.appLogCollectionExists(app.Name), check.Equals, true)
}

func (s *S) TestInsertAppForwardWithQuota(c *check.C) {
Expand Down Expand Up @@ -147,6 +165,8 @@ func (s *S) TestInsertAppBackward(c *check.C) {
n, err := s.conn.Apps().Find(bson.M{"name": app.Name}).Count()
c.Assert(err, check.IsNil)
c.Assert(n, check.Equals, 0)

c.Assert(s.appLogCollectionExists(app.Name), check.Equals, false)
}

func (s *S) TestInsertAppMinimumParams(c *check.C) {
Expand Down
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func Delete(app *App, evt *event.Event, requestID string) error {
logConn, err := db.LogConn()
if err == nil {
defer logConn.Close()
err = logConn.Logs(appName).DropCollection()
err = logConn.AppLogCollection(appName).DropCollection()
}
if err != nil {
logErr("Unable to remove logs collection", err)
Expand Down Expand Up @@ -1750,7 +1750,7 @@ func (app *App) Log(message, source, unit string) error {
return err
}
defer conn.Close()
return conn.Logs(app.Name).Insert(logs...)
return conn.AppLogCollection(app.Name).Insert(logs...)
}
return nil
}
Expand Down Expand Up @@ -1796,7 +1796,7 @@ func (app *App) lastLogs(lines int, filterLog Applog, invertFilter bool) ([]Appl
q[k] = bson.M{"$ne": v}
}
}
err = conn.Logs(app.Name).Find(q).Sort("-$natural").Limit(lines).All(&logs)
err = conn.AppLogCollection(app.Name).Find(q).Sort("-$natural").Limit(lines).All(&logs)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *S) TestDelete(c *check.C) {
c.Assert(s.provisioner.Provisioned(&a), check.Equals, false)
err = servicemanager.UserQuota.Inc(s.user.Email, 1)
c.Assert(err, check.IsNil)
count, err := s.logConn.Logs(app.Name).Count()
count, err := s.logConn.AppLogCollection(app.Name).Count()
c.Assert(err, check.IsNil)
c.Assert(count, check.Equals, 0)
_, err = repository.Manager().GetRepository(a.Name)
Expand Down Expand Up @@ -2504,12 +2504,12 @@ func (s *S) TestLog(c *check.C) {
err := CreateApp(&a, s.user)
c.Assert(err, check.IsNil)
defer func() {
s.logConn.Logs(a.Name).DropCollection()
s.logConn.AppLogCollection(a.Name).DropCollection()
}()
err = a.Log("last log msg", "tsuru", "outermachine")
c.Assert(err, check.IsNil)
var logs []Applog
err = s.logConn.Logs(a.Name).Find(nil).All(&logs)
err = s.logConn.AppLogCollection(a.Name).Find(nil).All(&logs)
c.Assert(err, check.IsNil)
c.Assert(logs, check.HasLen, 1)
c.Assert(logs[0].Message, check.Equals, "last log msg")
Expand All @@ -2523,12 +2523,12 @@ func (s *S) TestLogShouldAddOneRecordByLine(c *check.C) {
err := CreateApp(&a, s.user)
c.Assert(err, check.IsNil)
defer func() {
s.logConn.Logs(a.Name).DropCollection()
s.logConn.AppLogCollection(a.Name).DropCollection()
}()
err = a.Log("last log msg\nfirst log", "source", "machine")
c.Assert(err, check.IsNil)
var logs []Applog
err = s.logConn.Logs(a.Name).Find(nil).Sort("$natural").All(&logs)
err = s.logConn.AppLogCollection(a.Name).Find(nil).Sort("$natural").All(&logs)
c.Assert(err, check.IsNil)
c.Assert(logs, check.HasLen, 2)
c.Assert(logs[0].Message, check.Equals, "last log msg")
Expand All @@ -2543,7 +2543,7 @@ func (s *S) TestLogShouldNotLogBlankLines(c *check.C) {
c.Assert(err, check.IsNil)
err = a.Log("", "", "")
c.Assert(err, check.IsNil)
count, err := s.logConn.Logs(a.Name).Find(nil).Count()
count, err := s.logConn.AppLogCollection(a.Name).Find(nil).Count()
c.Assert(err, check.IsNil)
c.Assert(count, check.Equals, 1)
}
Expand Down Expand Up @@ -2571,7 +2571,7 @@ func (s *S) TestLogWithListeners(c *check.C) {
}()
err = a.Log("last log msg", "tsuru", "machine")
c.Assert(err, check.IsNil)
defer s.logConn.Logs(a.Name).DropCollection()
defer s.logConn.AppLogCollection(a.Name).DropCollection()
done := make(chan bool, 1)
q := make(chan bool)
go func(quit chan bool) {
Expand Down
4 changes: 2 additions & 2 deletions app/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func NewLogListener(a *App, filterLog Applog) (*LogListener, error) {
}
c := make(chan Applog, 10)
quit := make(chan struct{})
coll := conn.Logs(a.Name)
coll := conn.AppLogCollection(a.Name)
var lastLog Applog
err = coll.Find(nil).Sort("-_id").Limit(1).One(&lastLog)
if err == mgo.ErrNotFound {
Expand Down Expand Up @@ -306,7 +306,7 @@ func (d *appLogDispatcher) flush(msgs []interface{}, lastMessage *msgWithTS) boo
log.Errorf("[log flusher] unable to connect to mongodb: %s", err)
return false
}
coll := conn.Logs(d.appName)
coll := conn.AppLogCollection(d.appName)
err = coll.Insert(msgs...)
coll.Close()
if err != nil {
Expand Down

0 comments on commit b390dce

Please sign in to comment.