Skip to content

Commit

Permalink
chore: move away from parallel usages of licenses v2 and v3 (#6527)
Browse files Browse the repository at this point in the history
* chore: move all the usages of license v2 to license v3

* chore: added log lines for debugging
  • Loading branch information
vikrantgupta25 authored Nov 25, 2024
1 parent 2e4956c commit 24ab18d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
7 changes: 7 additions & 0 deletions ee/query-service/app/api/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func (ah *APIHandler) listLicenses(w http.ResponseWriter, r *http.Request) {
}

func (ah *APIHandler) applyLicense(w http.ResponseWriter, r *http.Request) {
if ah.UseLicensesV3 {
// if the licenses v3 is toggled on then do not apply license in v2 and run the validator!
// TODO: remove after migration to v3 and deprecation from zeus
zap.L().Info("early return from apply license v2 call")
render.Success(w, http.StatusOK, nil)
return
}
var l model.License

if err := json.NewDecoder(r.Body).Decode(&l); err != nil {
Expand Down
37 changes: 31 additions & 6 deletions ee/query-service/license/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (

// Repo is license repo. stores license keys in a secured DB
type Repo struct {
db *sqlx.DB
db *sqlx.DB
useLicensesV3 bool
}

// NewLicenseRepo initiates a new license repo
func NewLicenseRepo(db *sqlx.DB) Repo {
func NewLicenseRepo(db *sqlx.DB, useLicensesV3 bool) Repo {
return Repo{
db: db,
db: db,
useLicensesV3: useLicensesV3,
}
}

Expand Down Expand Up @@ -78,9 +80,7 @@ func (r *Repo) GetLicensesV3(ctx context.Context) ([]*model.LicenseV3, error) {
return licenseV3Data, nil
}

// GetActiveLicense fetches the latest active license from DB.
// If the license is not present, expect a nil license and a nil error in the output.
func (r *Repo) GetActiveLicense(ctx context.Context) (*model.License, *basemodel.ApiError) {
func (r *Repo) GetActiveLicenseV2(ctx context.Context) (*model.License, *basemodel.ApiError) {
var err error
licenses := []model.License{}

Expand Down Expand Up @@ -109,6 +109,31 @@ func (r *Repo) GetActiveLicense(ctx context.Context) (*model.License, *basemodel
return active, nil
}

// GetActiveLicense fetches the latest active license from DB.
// If the license is not present, expect a nil license and a nil error in the output.
func (r *Repo) GetActiveLicense(ctx context.Context) (*model.License, *basemodel.ApiError) {
if r.useLicensesV3 {
zap.L().Info("Using licenses v3 for GetActiveLicense")
activeLicenseV3, err := r.GetActiveLicenseV3(ctx)
if err != nil {
return nil, basemodel.InternalError(fmt.Errorf("failed to get active licenses from db: %v", err))
}

if activeLicenseV3 == nil {
return nil, nil
}
activeLicenseV2 := model.ConvertLicenseV3ToLicenseV2(activeLicenseV3)
return activeLicenseV2, nil

}

active, err := r.GetActiveLicenseV2(ctx)
if err != nil {
return nil, err
}
return active, nil
}

func (r *Repo) GetActiveLicenseV3(ctx context.Context) (*model.LicenseV3, error) {
var err error
licenses := []model.LicenseDB{}
Expand Down
8 changes: 5 additions & 3 deletions ee/query-service/license/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func StartManager(dbType string, db *sqlx.DB, useLicensesV3 bool, features ...ba
return LM, nil
}

repo := NewLicenseRepo(db)
repo := NewLicenseRepo(db, useLicensesV3)
err := repo.InitDB(dbType)

if err != nil {
Expand All @@ -69,7 +69,7 @@ func StartManager(dbType string, db *sqlx.DB, useLicensesV3 bool, features ...ba

if useLicensesV3 {
// get active license from the db
active, err := m.repo.GetActiveLicense(context.Background())
active, err := m.repo.GetActiveLicenseV2(context.Background())
if err != nil {
return m, err
}
Expand All @@ -88,6 +88,7 @@ func StartManager(dbType string, db *sqlx.DB, useLicensesV3 bool, features ...ba
if apiError != nil && apiError.Typ != model.ErrorConflict {
return m, apiError
}
zap.L().Info("Successfully inserted license from v2 to v3 table")
}
}

Expand Down Expand Up @@ -266,6 +267,7 @@ func (lm *Manager) GetLicensesV3(ctx context.Context) (response []*model.License

// Validator validates license after an epoch of time
func (lm *Manager) Validator(ctx context.Context) {
zap.L().Info("Validator started!")
defer close(lm.terminated)
tick := time.NewTicker(validationFrequency)
defer tick.Stop()
Expand All @@ -290,6 +292,7 @@ func (lm *Manager) Validator(ctx context.Context) {

// Validator validates license after an epoch of time
func (lm *Manager) ValidatorV3(ctx context.Context) {
zap.L().Info("ValidatorV3 started!")
defer close(lm.terminated)
tick := time.NewTicker(validationFrequency)
defer tick.Stop()
Expand Down Expand Up @@ -379,7 +382,6 @@ func (lm *Manager) Validate(ctx context.Context) (reterr error) {
return nil
}

// todo[vikrantgupta25]: check the comparison here between old and new license!
func (lm *Manager) RefreshLicense(ctx context.Context) *model.ApiError {

license, apiError := validate.ValidateLicenseV3(lm.activeLicenseV3.Key)
Expand Down
21 changes: 21 additions & 0 deletions ee/query-service/model/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,24 @@ func NewLicenseV3WithIDAndKey(id string, key string, data map[string]interface{}
licenseDataWithIdAndKey["key"] = key
return NewLicenseV3(licenseDataWithIdAndKey)
}

func ConvertLicenseV3ToLicenseV2(l *LicenseV3) *License {
planKeyFromPlanName, ok := MapOldPlanKeyToNewPlanName[l.PlanName]
if !ok {
planKeyFromPlanName = Basic
}
return &License{
Key: l.Key,
ActivationId: "",
PlanDetails: "",
FeatureSet: l.Features,
ValidationMessage: "",
IsCurrent: l.IsCurrent,
LicensePlan: LicensePlan{
PlanKey: planKeyFromPlanName,
ValidFrom: l.ValidFrom,
ValidUntil: l.ValidUntil,
Status: l.Status},
}

}

0 comments on commit 24ab18d

Please sign in to comment.