forked from shellhub-io/shellhub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: optimize active session count query
This commit adds an index on `tenant_id` in the `active_sessions` collection to improve the performance of active session counting. It also updates code in MongoDB store to utilize this index. Closes shellhub-io#3228
- Loading branch information
1 parent
767ada7
commit 7864935
Showing
6 changed files
with
163 additions
and
27 deletions.
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
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,48 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
migrate "github.com/xakep666/mongo-migrate" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
var migration60 = migrate.Migration{ | ||
Version: 60, | ||
Description: "create index for tenant_id on active_sessions", | ||
Up: func(database *mongo.Database) error { | ||
logrus.WithFields(logrus.Fields{ | ||
"component": "migration", | ||
"version": 60, | ||
"action": "Up", | ||
}).Info("Applying migration up") | ||
indexName := "tenant_id" | ||
if _, err := database.Collection("active_sessions").Indexes().CreateOne(context.Background(), mongo.IndexModel{ | ||
Keys: bson.M{ | ||
"tenant_id": 1, | ||
}, | ||
Options: &options.IndexOptions{ //nolint:exhaustruct | ||
Name: &indexName, | ||
}, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
Down: func(database *mongo.Database) error { | ||
logrus.WithFields(logrus.Fields{ | ||
"component": "migration", | ||
"version": 60, | ||
"action": "Down", | ||
}).Info("Applying migration down") | ||
if _, err := database.Collection("active_sessions").Indexes().DropOne(context.Background(), "tenant_id"); err != nil { | ||
return err | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/shellhub-io/shellhub/api/pkg/dbtest" | ||
"github.com/shellhub-io/shellhub/pkg/envs" | ||
envMocks "github.com/shellhub-io/shellhub/pkg/envs/mocks" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
migrate "github.com/xakep666/mongo-migrate" | ||
"go.mongodb.org/mongo-driver/bson" | ||
) | ||
|
||
func TestMigration60(t *testing.T) { | ||
logrus.Info("Testing Migration 60") | ||
|
||
db := dbtest.DBServer{} | ||
defer db.Stop() | ||
|
||
mock := &envMocks.Backend{} | ||
envs.DefaultBackend = mock | ||
|
||
cases := []struct { | ||
description string | ||
test func() error | ||
}{ | ||
{ | ||
"Success to apply up on migration 60", | ||
func() error { | ||
mock.On("Get", "SHELLHUB_CLOUD").Return("true").Once() | ||
|
||
migrations := GenerateMigrations()[59:60] | ||
migrates := migrate.NewMigrate(db.Client().Database("test"), migrations...) | ||
err := migrates.Up(migrate.AllAvailable) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cursor, err := db.Client().Database("test").Collection("active_sessions").Indexes().List(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var found bool | ||
for cursor.Next(context.Background()) { | ||
var index bson.M | ||
if err := cursor.Decode(&index); err != nil { | ||
return err | ||
} | ||
|
||
if index["name"] == "tenant_id" { | ||
found = true | ||
} | ||
} | ||
|
||
if !found { | ||
return errors.New("index not created") | ||
} | ||
|
||
return nil | ||
}, | ||
}, | ||
{ | ||
"Success to apply down on migration 60", | ||
func() error { | ||
migrations := GenerateMigrations()[59:60] | ||
migrates := migrate.NewMigrate(db.Client().Database("test"), migrations...) | ||
err := migrates.Down(migrate.AllAvailable) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cursor, err := db.Client().Database("test").Collection("active_sessions").Indexes().List(context.Background()) | ||
if err != nil { | ||
return errors.New("index not dropped") | ||
} | ||
|
||
var found bool | ||
for cursor.Next(context.Background()) { | ||
var index bson.M | ||
if err := cursor.Decode(&index); err != nil { | ||
return err | ||
} | ||
|
||
if index["name"] == "tenant_id" { | ||
found = true | ||
} | ||
} | ||
|
||
if found { | ||
return errors.New("index not dropped") | ||
} | ||
|
||
return nil | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range cases { | ||
tc := test | ||
t.Run(tc.description, func(t *testing.T) { | ||
err := tc.test() | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
} |
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