Skip to content

Commit

Permalink
support autotenant toggling on MT-enabled classes
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerduckworth committed May 1, 2024
1 parent 851bcbd commit 2bfb133
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions cluster/store/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (db *localDB) UpdateClass(cmd *command.ApplyRequest, nodeID string, schemaO
meta.Class.VectorIndexConfig = u.VectorIndexConfig
meta.Class.InvertedIndexConfig = u.InvertedIndexConfig
meta.Class.VectorConfig = u.VectorConfig
meta.Class.MultiTenancyConfig = u.MultiTenancyConfig
meta.ClassVersion = cmd.Version
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions entities/schema/multi_tenancy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ func MultiTenancyEnabled(class *models.Class) bool {
return false
}

func AutoTenantCreationEnabled(class *models.Class) bool {
if class == nil {
return false
}

if class.MultiTenancyConfig != nil {
return class.MultiTenancyConfig.AutoTenantCreation
}
return false
}

func ActivityStatus(status string) string {
if status == "" {
return models.TenantActivityStatusHOT
Expand Down
88 changes: 88 additions & 0 deletions test/acceptance/multi_tenancy/class_creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
"testing"

"github.com/go-openapi/strfmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaviate/weaviate/client/batch"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/test/helper"
)
Expand Down Expand Up @@ -55,3 +57,89 @@ func TestClassMultiTenancyDisabledSchemaPrint(t *testing.T) {
classReturn := helper.GetClass(t, testClass.Class)
require.NotNil(t, classReturn.MultiTenancyConfig)
}

func TestClassMultiTenancyToggleAutoTenant(t *testing.T) {
createObjectToCheckAutoTenant := func(t *testing.T, object *models.Object) *models.ObjectsGetResponse {
t.Helper()
params := batch.NewBatchObjectsCreateParams().
WithBody(batch.BatchObjectsCreateBody{
Objects: []*models.Object{object},
})
resp, err := helper.Client(t).Batch.BatchObjectsCreate(params, nil)
helper.AssertRequestOk(t, resp, err, nil)
require.NotNil(t, resp)
require.Len(t, resp.Payload, 1)
return resp.Payload[0]
}

testClass := models.Class{
Class: "AutoTenantToggle",
MultiTenancyConfig: &models.MultiTenancyConfig{
AutoTenantCreation: false,
Enabled: true,
},
}
helper.CreateClass(t, &testClass)
defer func() {
helper.DeleteClass(t, testClass.Class)
}()

t.Run("autotenant not set, fail to create object with nonexistent tenant", func(t *testing.T) {
resp := createObjectToCheckAutoTenant(t,
&models.Object{
Class: "AutoTenantToggle",
Properties: map[string]interface{}{
"stringProp": "value",
},
Tenant: "non-existent",
},
)

require.NotNil(t, resp.Result)
require.NotNil(t, resp.Result.Errors)
require.Len(t, resp.Result.Errors.Error, 1)
assert.Equal(t, resp.Result.Errors.Error[0].Message, `tenant not found: "non-existent"`)
})

t.Run("autotenant toggled on, successfully create object", func(t *testing.T) {
fetched := helper.GetClass(t, testClass.Class)
fetched.MultiTenancyConfig.AutoTenantCreation = true
helper.UpdateClass(t, fetched)

resp := createObjectToCheckAutoTenant(t,
&models.Object{
Class: "AutoTenantToggle",
Properties: map[string]interface{}{
"stringProp": "value",
},
Tenant: "now-exists",
},
)

require.NotNil(t, resp.Result)
require.Nil(t, resp.Result.Errors)
success := "SUCCESS"
assert.EqualValues(t, resp.Result.Status, &success)
})

t.Run("autotenant toggled back off, fail to create object with nonexistent tenant", func(t *testing.T) {
fetched := helper.GetClass(t, testClass.Class)
fetched.MultiTenancyConfig.AutoTenantCreation = false
helper.UpdateClass(t, fetched)

resp := createObjectToCheckAutoTenant(t,
&models.Object{
Class: "AutoTenantToggle",
Properties: map[string]interface{}{
"stringProp": "value",
},
Tenant: "still-nonexistent",
},
)

require.NotNil(t, resp.Result)
require.NotNil(t, resp.Result.Errors)
require.Len(t, resp.Result.Errors.Error, 1)
assert.Equal(t, resp.Result.Errors.Error[0].Message, `tenant not found: "still-nonexistent"`)
})
}
3 changes: 3 additions & 0 deletions usecases/schema/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,9 @@ func validateUpdatingMT(current, update *models.Class) (enabled bool, err error)
err = fmt.Errorf("enabling multi-tenancy for an existing class is not supported")
}
}
if !enabled && schema.AutoTenantCreationEnabled(update) {
err = fmt.Errorf("can't enabled autoTenantCreation on a non-multi-tenant class")
}
return
}

Expand Down

0 comments on commit 2bfb133

Please sign in to comment.