Skip to content

Commit

Permalink
feat: Persist project limits in project metadata (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
adilansari committed Jun 13, 2023
1 parent 908416e commit 235552d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/proto
Submodule proto updated from 2f9df7 to 5cc3ca
4 changes: 4 additions & 0 deletions api/server/v1/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func (x *CreateProjectRequest) Validate() error {
return isValidDatabase(x.Project)
}

func (x *UpdateProjectRequest) Validate() error {
return isValidDatabase(x.Project)
}

func (x *DeleteProjectRequest) Validate() error {
return isValidDatabase(x.Project)
}
Expand Down
6 changes: 6 additions & 0 deletions server/metadata/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ type ProjectMetadata struct {
CreatedAt int64
CachesMetadata []CacheMetadata
SearchMetadata []SearchMetadata
Limits *ProjectLimits
}

type ProjectLimits struct {
MaxCollections *int32
MaxSearchIndexes *int32
}

type CacheMetadata struct {
Expand Down
5 changes: 5 additions & 0 deletions server/metadata/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,11 @@ func (tenant *Tenant) ListProjects(_ context.Context) []string {
return projects
}

// GetProjectMetadata retrieves the metadata associated with the project.
func (tenant *Tenant) GetProjectMetadata(ctx context.Context, tx transaction.Tx, projName string) (*ProjectMetadata, error) {
return tenant.namespaceStore.GetProjectMetadata(ctx, tx, tenant.namespace.Id(), projName)
}

// DeleteTenant is used to delete tenant and all the content within it.
// This needs to be followed up with a "restart" of server to clear memory state.
// Be careful calling this.
Expand Down
4 changes: 4 additions & 0 deletions server/services/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ func (s *apiService) CreateProject(ctx context.Context, r *api.CreateProjectRequ
}, nil
}

func (*apiService) UpdateProject(_ context.Context, _ *api.UpdateProjectRequest) (*api.UpdateProjectResponse, error) {
return nil, errors.Unimplemented("Update project is not available")
}

func (s *apiService) DeleteProject(ctx context.Context, r *api.DeleteProjectRequest) (*api.DeleteProjectResponse, error) {
accessToken, _ := request.GetAccessToken(ctx)
runner := s.runnerFactory.GetProjectQueryRunner(accessToken)
Expand Down
26 changes: 23 additions & 3 deletions server/services/v1/database/database_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (runner *ProjectQueryRunner) SetDescribeDatabaseReq(describe *api.DescribeD
}

func (runner *ProjectQueryRunner) create(ctx context.Context, tx transaction.Tx, tenant *metadata.Tenant) (Response, context.Context, error) {
projMetadata, err := createProjectMetadata(ctx)
projMetadata, err := createProjectMetadata(ctx, runner.createReq.Options)
if err != nil {
return Response{}, ctx, err
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (runner *ProjectQueryRunner) delete(ctx context.Context, tx transaction.Tx,
return Response{Status: DroppedStatus}, ctx, nil
}

func (*ProjectQueryRunner) list(ctx context.Context, _ transaction.Tx, tenant *metadata.Tenant) (Response, context.Context, error) {
func (*ProjectQueryRunner) list(ctx context.Context, tx transaction.Tx, tenant *metadata.Tenant) (Response, context.Context, error) {
// listReq projects need not include any branches

projectList := tenant.ListProjects(ctx)
Expand All @@ -95,6 +95,17 @@ func (*ProjectQueryRunner) list(ctx context.Context, _ transaction.Tx, tenant *m
projects[i] = &api.ProjectInfo{
Project: l,
}

meta, err := tenant.GetProjectMetadata(ctx, tx, l)
if err != nil {
return Response{}, ctx, err
}
if meta != nil && meta.Limits != nil {
projects[i].Limits = &api.ProjectLimits{
MaxCollections: meta.Limits.MaxCollections,
MaxSearchIndexes: meta.Limits.MaxSearchIndexes,
}
}
}

return Response{
Expand Down Expand Up @@ -244,14 +255,23 @@ func (runner *BranchQueryRunner) Run(ctx context.Context, tx transaction.Tx, ten
return Response{}, ctx, errors.Unknown("unknown request path")
}

func createProjectMetadata(ctx context.Context) (*metadata.ProjectMetadata, error) {
func createProjectMetadata(ctx context.Context, r *api.ProjectOptions) (*metadata.ProjectMetadata, error) {
currentSub, err := auth.GetCurrentSub(ctx)
if err != nil && config.DefaultConfig.Auth.Enabled {
return nil, errors.Internal("Failed to createReq database metadata")
}
var limits *metadata.ProjectLimits
if r != nil && r.Limits != nil {
limits = &metadata.ProjectLimits{
MaxCollections: r.Limits.MaxCollections,
MaxSearchIndexes: r.Limits.MaxSearchIndexes,
}
}

return &metadata.ProjectMetadata{
ID: 0, // it will be set to right value later on
Creator: currentSub,
CreatedAt: time.Now().Unix(),
Limits: limits,
}, nil
}

0 comments on commit 235552d

Please sign in to comment.