Skip to content

Commit

Permalink
added upgrades for version 3.9.0
Browse files Browse the repository at this point in the history
Signed-off-by: Kartikay <[email protected]>
  • Loading branch information
kartikaysaxena committed Aug 7, 2024
1 parent f918ad5 commit 8e9e7c5
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 125 deletions.
33 changes: 27 additions & 6 deletions chaoscenter/upgrade-agents/control-plane/CONTRIBUTING.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ We ask that before contributing, please make the effort to coordinate with the m

Use your judgement about what constitutes a large change. If you aren't sure, send a message to the **#litmus-dev** slack or submit an issue on GitHub.

<br />

### **Sign your work with Developer Certificate of Origin**

Expand All @@ -24,16 +23,38 @@ Use your real name (sorry, no pseudonyms or anonymous contributions.)

If you set your `user.name` and `user.email` git configs, you can sign your commit automatically with `git commit -s`. You can also use git [aliases](https://git-scm.com/book/tr/v2/Git-Basics-Git-Aliases) like `git config --global alias.ci 'commit -s'`. Now you can commit with git ci and the commit will be signed.

<br />
## **Development Guide**

### **Version Upgrade File**
Start MongoDB, Auth and GraphQL server as mentioned in the [ChaosCenter Development Guide](https://github.com/litmuschaos/litmus/wiki/ChaosCenter-Development-Guide) to set up the basic structure of the DB. Then start the upgrade manager located in `chaoscenter/upgrade-agents/control-plane` by setting up the environment variables

## Versions
```
export DB_SERVER="mongodb://m1:27015,m2:27016,m3:27017/?replicaSet=rs0"
export DB_USER=admin
export DB_PASSWORD=1234
export VERSION=<Version you need to upgrade to>
```
To run the upgrade-manager, run

```
go run main.go
```
The upgrade-manager would get the current version of Litmus through the DB.

## **Version Upgrade Files**

`<version>/` folder contains files for the upgrade logic

- `upgrade-manager.go` - Contains a map of versions with their corresponding version-managers for the versions.
- `vx.y.z/manager.go` - Instantiates the Version Manager and runs the upgradeExecutor.
- `vx.y.z/manager.go` - Instantiates the Version Manager and runs the upgradeExecutor in transactions which can be omitted if the operation doesn't support a transaction.
- `vx.y.z/upgrade.go` - Contains the logic of upgradeExecutor of what schema changes are to be made in the specific version.

There are some other files not mentioned here.
There are some other files not mentioned here.

## **Upgrade to version 3.9.0**

In version 3.9.0 the following changes are done in DB schema

- In projects collection (auth DB), member role is updated from `Editor` to `Executor`
- New `is_initial_login` field in users collection (auth DB) whose value is set as false

Run the upgrade-agent while specifying the version `VERSION=3.9.0` in the environmental variable, and it should now be upgraded.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

v3_4_0 "github.com/litmuschaos/litmus/chaoscenter/upgrader-agents/control-plane/versions/v3.4.0"

v3_8_0 "github.com/litmuschaos/litmus/chaoscenter/upgrader-agents/control-plane/versions/v3.8.0"

v3_9_0 "github.com/litmuschaos/litmus/chaoscenter/upgrader-agents/control-plane/versions/v3.9.0"

"github.com/litmuschaos/litmus/chaoscenter/upgrader-agents/control-plane/pkg/database"
Expand Down Expand Up @@ -86,7 +84,7 @@ func ParseVersion(version string) *Version {
func NewUpgradeManager(logger *log.Logger, dbClient *mongo.Client) (*UpgradeManager, error) {

// added for debug only to run version manager consistently
database.UpdateVersion(dbClient, "3.3.0")
// database.UpdateVersion(dbClient, "3.3.0")

currentVersion := os.Getenv("VERSION")
log.WithFields(log.Fields{
Expand Down Expand Up @@ -237,10 +235,6 @@ func (m *UpgradeManager) getVersionMap() map[string]UpgradeExecutor {
NextVersion: "",
VersionManager: nil,
},
"3.8.0": {
NextVersion: "",
VersionManager: v3_8_0.NewVersionManger(m.Logger, m.DBClient),
},
"3.9.0": {
NextVersion: "",
VersionManager: v3_9_0.NewVersionManger(m.Logger, m.DBClient),
Expand Down
126 changes: 62 additions & 64 deletions chaoscenter/upgrade-agents/control-plane/versions/v3.4.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,81 +20,79 @@ func upgradeMany(logger *log.Logger, dbClient *mongo.Client, ctx context.Context
// log.Fatal("error starting transaction: %w", err)
// }


var indexes []string
var environmentIDIndexName string

environmentCollection := dbClient.Database(database.LitmusDB).Collection(database.EnvironmentCollection)
indexView := environmentCollection.Indexes()
cursor, err := indexView.List(ctx)
if err != nil {
// session.AbortTransaction(ctx)
log.Error("error listing indexes: %w", err)
return err
}
defer cursor.Close(ctx)

environmentIDIndexExists := false

logVersion := log.Fields{
"version": "3.4.0",
var indexes []string
var environmentIDIndexName string

environmentCollection := dbClient.Database(database.LitmusDB).Collection(database.EnvironmentCollection)
indexView := environmentCollection.Indexes()
cursor, err := indexView.List(ctx)
if err != nil {
// session.AbortTransaction(ctx)
log.Error("error listing indexes: %w", err)
return err
}
defer cursor.Close(ctx)

environmentIDIndexExists := false

logVersion := log.Fields{
"version": "3.4.0",
}

for cursor.Next(ctx) {
var index bson.M
if err := cursor.Decode(&index); err != nil {
log.Fatal("error decoding index: %w", err)
}

for cursor.Next(ctx) {
var index bson.M
if err := cursor.Decode(&index); err != nil {
log.Fatal("error decoding index: %w", err)
}
indexes = append(indexes, index["name"].(string))
if keys, ok := index["key"].(bson.M); ok {
if _, found := keys["environment_id"]; found {
environmentIDIndexExists = true
environmentIDIndexName = index["name"].(string)
}
indexes = append(indexes, index["name"].(string))
if keys, ok := index["key"].(bson.M); ok {
if _, found := keys["environment_id"]; found {
environmentIDIndexExists = true
environmentIDIndexName = index["name"].(string)
}
}
}

// logIndexes := append(logVersion,)

logIndexes := log.Fields{
"version": "3.4.0",
"indexes": indexes,
}

logger.WithFields(logIndexes).Info("Indexes found in environment collection while upgrading to intermediate version v3.4.0")
// logIndexes := append(logVersion,)

logFields := log.Fields{
"collection": database.EnvironmentCollection,
"db": database.LitmusDB,
}
logIndexes := log.Fields{
"version": "3.4.0",
"indexes": indexes,
}

if environmentIDIndexExists {
_, err := environmentCollection.Indexes().DropOne(ctx, environmentIDIndexName)
if err != nil {
log.Fatal("error dropping index: %w", err)
}
logger.WithFields(logFields).Info("Deleted an existing index in environment collection while upgrading to intermediate v3.4.0")

indexModel := mongo.IndexModel{
Keys: bson.M{"environment_id": 1},
Options: options.Index().
SetUnique(true).
SetPartialFilterExpression(bson.D{
{"isRemoved", false},
}),
}
logger.WithFields(logIndexes).Info("Indexes found in environment collection while upgrading to intermediate version v3.4.0")

_, err = environmentCollection.Indexes().CreateOne(ctx, indexModel)
if err != nil {
log.Fatal("error creating index: %w", err)
}
logFields := log.Fields{
"collection": database.EnvironmentCollection,
"db": database.LitmusDB,
}

log.WithFields(logVersion).Info("Created a new index with partial filter expression environment_id while upgrading to intermediate v3.4.0")
if environmentIDIndexExists {
_, err := environmentCollection.Indexes().DropOne(ctx, environmentIDIndexName)
if err != nil {
log.Fatal("error dropping index: %w", err)
}
logger.WithFields(logFields).Info("Deleted an existing index in environment collection while upgrading to intermediate v3.4.0")

indexModel := mongo.IndexModel{
Keys: bson.M{"environment_id": 1},
Options: options.Index().
SetUnique(true).
SetPartialFilterExpression(bson.D{
{"isRemoved", false},
}),
}

} else {
log.Fatal("environment id index not found in version v3.4.0")
_, err = environmentCollection.Indexes().CreateOne(ctx, indexModel)
if err != nil {
log.Fatal("error creating index: %w", err)
}

log.WithFields(logVersion).Info("Created a new index with partial filter expression environment_id while upgrading to intermediate v3.4.0")

} else {
log.Fatal("environment id index not found in version v3.4.0")
}

// if err != nil {
// log.Fatal("error committing transaction: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,32 @@ func NewVersionManger(logger *log.Logger, dbClient *mongo.Client) *VersionManage
func (vm VersionManager) Run() error {
ctx := context.Background()
session, err := vm.DBClient.StartSession()

defer session.EndSession(ctx)

if err != nil {
log.Fatal("error starting session: %w", err)
}

defer session.EndSession(ctx)

err = mongo.WithSession(ctx, session, func(sc mongo.SessionContext) error {

err := session.StartTransaction()
if err != nil {
log.Fatal("error starting transaction: %w", err)
}

if err := upgradeExecutor(vm.Logger, vm.DBClient, sc); err != nil {
return err
}

err = session.CommitTransaction(sc)

if err != nil {
return err
}

return nil
})
if err != nil {
Expand Down
20 changes: 10 additions & 10 deletions chaoscenter/upgrade-agents/control-plane/versions/v3.8.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ func upgradeExecutor(logger *log.Logger, dbClient *mongo.Client, ctx context.Con
"version": "3.8.0",
}

res := dbClient.Database(database.AdminDB).RunCommand(
context.Background(),
bson.D{{"renameCollection", "auth.users"}, {"to", "auth.client"}},
)
if res.Err() != nil {
log.Fatal(res.Err().Error())
return res.Err()
}

logger.WithFields(logVersion).Info("Users collection renamed to admin while upgrading to intermediate version v3.8.0")
res := dbClient.Database(database.AdminDB).RunCommand(
context.Background(),
bson.D{{"renameCollection", "auth.users"}, {"to", "auth.client"}},
)
if res.Err() != nil {
log.Fatal(res.Err().Error())
return res.Err()
}

logger.WithFields(logVersion).Info("Users collection renamed to admin while upgrading to intermediate version v3.8.0")

logger.WithFields(logVersion).Info("Collection 'auth.users' renamed to 'auth.client' successfully.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,41 @@ func NewVersionManger(logger *log.Logger, dbClient *mongo.Client) *VersionManage
// to upgrade from the previous version to `this` version
func (vm VersionManager) Run() error {
ctx := context.Background()
err := upgradeExecutor(vm.Logger, vm.DBClient, ctx)
session, err := vm.DBClient.StartSession()

defer session.EndSession(ctx)

if err != nil {
log.Fatal("error starting session: %w", err)
}

defer session.EndSession(ctx)

err = mongo.WithSession(ctx, session, func(sc mongo.SessionContext) error {
// Start the transaction
err := session.StartTransaction()
if err != nil {
log.Fatal("error starting transaction: %w", err)
}

if err := upgradeExecutor(vm.Logger, vm.DBClient, sc); err != nil {
return err
}
// Commit the transaction
err = session.CommitTransaction(sc)

if err != nil {
return err
}

return nil
})
if err != nil {
return err
// Abort the transaction if it fails
abortError := session.AbortTransaction(ctx)
if abortError != nil {
log.Fatal("error committing transaction: %w", err)
}
}
return err
}
Loading

0 comments on commit 8e9e7c5

Please sign in to comment.