diff --git a/Dockerfile b/Dockerfile index 12815622..027c013a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ RUN mkdir -p /tmp/app/logs RUN cp ${BINARY} /tmp/app RUN cp -r /app/build/support-files/sql /tmp/app/sql -FROM to2false/sql-migrate:latest AS migrator +FROM to2false/sql-migration:latest AS migrator FROM debian:bullseye-slim COPY --from=builder /tmp/app /app diff --git a/pkg/database/dao/mock/subject_template_group.go b/pkg/database/dao/mock/subject_template_group.go index 274fb851..d9d8ce65 100644 --- a/pkg/database/dao/mock/subject_template_group.go +++ b/pkg/database/dao/mock/subject_template_group.go @@ -165,3 +165,18 @@ func (mr *MockSubjectTemplateGroupManagerMockRecorder) ListRelationBySubjectPKGr mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRelationBySubjectPKGroupPKs", reflect.TypeOf((*MockSubjectTemplateGroupManager)(nil).ListRelationBySubjectPKGroupPKs), subjectPK, groupPKs) } + +// ListThinRelationWithMaxExpiredAtByGroupPK mocks base method. +func (m *MockSubjectTemplateGroupManager) ListThinRelationWithMaxExpiredAtByGroupPK(groupPK int64) ([]dao.ThinSubjectRelation, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListThinRelationWithMaxExpiredAtByGroupPK", groupPK) + ret0, _ := ret[0].([]dao.ThinSubjectRelation) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListThinRelationWithMaxExpiredAtByGroupPK indicates an expected call of ListThinRelationWithMaxExpiredAtByGroupPK. +func (mr *MockSubjectTemplateGroupManagerMockRecorder) ListThinRelationWithMaxExpiredAtByGroupPK(groupPK interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListThinRelationWithMaxExpiredAtByGroupPK", reflect.TypeOf((*MockSubjectTemplateGroupManager)(nil).ListThinRelationWithMaxExpiredAtByGroupPK), groupPK) +} diff --git a/pkg/database/dao/subject_template_group.go b/pkg/database/dao/subject_template_group.go index f9ff41c9..6961ccdc 100644 --- a/pkg/database/dao/subject_template_group.go +++ b/pkg/database/dao/subject_template_group.go @@ -40,6 +40,7 @@ type SubjectTemplateGroupManager interface { ) (members []SubjectTemplateGroup, err error) ListRelationBySubjectPKGroupPKs(subjectPK int64, groupPKs []int64) ([]SubjectTemplateGroup, error) ListGroupDistinctSubjectPK(groupPK int64) (subjectPKs []int64, err error) + ListThinRelationWithMaxExpiredAtByGroupPK(groupPK int64) ([]ThinSubjectRelation, error) BulkCreateWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error BulkUpdateExpiredAtWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error @@ -210,3 +211,27 @@ func (m *subjectTemplateGroupManager) ListGroupDistinctSubjectPK(groupPK int64) } return } + +func (m *subjectTemplateGroupManager) ListThinRelationWithMaxExpiredAtByGroupPK( + groupPK int64, +) ([]ThinSubjectRelation, error) { + relations := []ThinSubjectRelation{} + + query := `SELECT + subject_pk, + MAX(expired_at) AS policy_expired_at + FROM subject_template_group + WHERE group_pk = ? + GROUP BY subject_pk` + + err := database.SqlxSelect(m.DB, &relations, query, groupPK) + if errors.Is(err, sql.ErrNoRows) { + return relations, nil + } + + for i := range relations { + relations[i].GroupPK = groupPK + } + + return relations, err +} diff --git a/pkg/database/dao/subject_template_group_test.go b/pkg/database/dao/subject_template_group_test.go index 3384937c..35875556 100644 --- a/pkg/database/dao/subject_template_group_test.go +++ b/pkg/database/dao/subject_template_group_test.go @@ -190,3 +190,25 @@ func Test_subjectTemplateGroupManager_BulkUpdateExpiredAtWithTx(t *testing.T) { assert.NoError(t, err, "query from db fail.") }) } + +func Test_subjectTemplateGroupManager_ListThinRelationWithMaxExpiredAtByGroupPK(t *testing.T) { + database.RunWithMock(t, func(db *sqlx.DB, mock sqlmock.Sqlmock, t *testing.T) { + groupPK := int64(1) + mockQuery := `^SELECT subject_pk, (.*) FROM subject_template_group WHERE group_pk` + + rows := sqlmock.NewRows([]string{"subject_pk", "policy_expired_at"}). + AddRow(int64(1), int64(1)). + AddRow(int64(2), int64(2)) + + mock.ExpectQuery(mockQuery).WithArgs(groupPK).WillReturnRows(rows) + + manager := &subjectTemplateGroupManager{DB: db} + relations, err := manager.ListThinRelationWithMaxExpiredAtByGroupPK(groupPK) + + assert.NoError(t, err, "query from db failed") + assert.Len(t, relations, 2, "did not get expected number of relations") + for _, rel := range relations { + assert.Equal(t, groupPK, rel.GroupPK, "GroupPK in relation does not match") + } + }) +} diff --git a/pkg/service/group.go b/pkg/service/group.go index 617d7745..32ad69f6 100644 --- a/pkg/service/group.go +++ b/pkg/service/group.go @@ -15,6 +15,9 @@ package service import ( "database/sql" "errors" + "fmt" + "strconv" + "strings" "time" "github.com/TencentBlueKing/gopkg/collection/set" @@ -441,7 +444,7 @@ func (l *groupService) UpdateGroupMembersExpiredAtWithTx( } for _, systemID := range systemIDs { - err = l.addOrUpdateSubjectSystemGroup(tx, m.SubjectPK, systemID, groupPK, m.ExpiredAt) + err = l.addOrUpdateSubjectSystemGroup(tx, m.SubjectPK, systemID, map[int64]int64{groupPK: m.ExpiredAt}) if err != nil { return errorWrapf( err, @@ -528,7 +531,7 @@ func (l *groupService) BulkDeleteGroupMembers( } for _, systemID := range systemIDs { - err = l.removeSubjectSystemGroup(tx, subjectPK, systemID, groupPK) + err = l.removeSubjectSystemGroup(tx, subjectPK, systemID, map[int64]int64{groupPK: 0}) if err != nil { return nil, errorWrapf( err, @@ -582,7 +585,7 @@ func (l *groupService) BulkCreateGroupMembersWithTx( } for _, systemID := range systemIDs { - err = l.addOrUpdateSubjectSystemGroup(tx, r.SubjectPK, systemID, groupPK, r.ExpiredAt) + err = l.addOrUpdateSubjectSystemGroup(tx, r.SubjectPK, systemID, map[int64]int64{groupPK: r.ExpiredAt}) if err != nil { return errorWrapf( err, @@ -629,6 +632,7 @@ func (l *groupService) BulkUpdateSubjectSystemGroupBySubjectTemplateGroupWithTx( "BulkUpdateSubjectSystemGroupBySubjectTemplateGroupWithTx", ) + subjectSystemGroup := newSubjectSystemGroupMerger() groupSystemIDCache := make(map[int64][]string) for _, relation := range relations { if !relation.NeedUpdate { @@ -637,7 +641,8 @@ func (l *groupService) BulkUpdateSubjectSystemGroupBySubjectTemplateGroupWithTx( systemIDs, ok := groupSystemIDCache[relation.GroupPK] if !ok { - systemIDs, err := l.ListGroupAuthSystemIDs(relation.GroupPK) + var err error + systemIDs, err = l.ListGroupAuthSystemIDs(relation.GroupPK) if err != nil { return errorWrapf(err, "listGroupAuthSystem groupPK=`%d` fail", relation.GroupPK) } @@ -646,25 +651,38 @@ func (l *groupService) BulkUpdateSubjectSystemGroupBySubjectTemplateGroupWithTx( } for _, systemID := range systemIDs { - err := l.addOrUpdateSubjectSystemGroup( - tx, + subjectSystemGroup.Add( relation.SubjectPK, systemID, relation.GroupPK, relation.ExpiredAt, ) - if err != nil { - return errorWrapf( - err, - "addOrUpdateSubjectSystemGroup systemID=`%s`, subjectPK=`%d`, groupPK=`%d`, expiredAt=`%d`, fail", - systemID, - relation.SubjectPK, - relation.GroupPK, - relation.ExpiredAt, - ) - } } } + + for key, groups := range subjectSystemGroup.subjectSystemGroup { + subjectPK, systemID, err := subjectSystemGroup.ParseKey(key) + if err != nil { + return errorWrapf(err, "parseKey key=`%s` fail", key) + } + + err = l.addOrUpdateSubjectSystemGroup( + tx, + subjectPK, + systemID, + groups, + ) + if err != nil { + return errorWrapf( + err, + "addOrUpdateSubjectSystemGroup systemID=`%s`, subjectPK=`%d`, groups=`%v`, fail", + systemID, + subjectPK, + groups, + ) + } + } + return nil } @@ -737,6 +755,7 @@ func (l *groupService) BulkDeleteSubjectTemplateGroupWithTx( return errorWrapf(err, "subjectTemplateGroupManager.BulkDeleteWithTx relations=`%+v` fail", daoRelations) } + subjectSystemGroup := newSubjectSystemGroupMerger() groupSystemIDCache := make(map[int64][]string) for _, relation := range relations { if !relation.NeedUpdate { @@ -755,16 +774,30 @@ func (l *groupService) BulkDeleteSubjectTemplateGroupWithTx( } for _, systemID := range systemIDs { - err = l.removeSubjectSystemGroup(tx, relation.SubjectPK, systemID, relation.GroupPK) - if err != nil { - return errorWrapf( - err, - "removeSubjectSystemGroup systemID=`%s`, subjectPK=`%d`, groupPK=`%d`, fail", - systemID, - relation.SubjectPK, - relation.GroupPK, - ) - } + subjectSystemGroup.Add(relation.SubjectPK, systemID, relation.GroupPK, 0) + } + } + + for key, groups := range subjectSystemGroup.subjectSystemGroup { + subjectPK, systemID, err := subjectSystemGroup.ParseKey(key) + if err != nil { + return errorWrapf(err, "parseKey key=`%s` fail", key) + } + + err = l.removeSubjectSystemGroup( + tx, + subjectPK, + systemID, + groups, + ) + if err != nil { + return errorWrapf( + err, + "removeSubjectSystemGroup systemID=`%s`, subjectPK=`%d`, groups=`%v`, fail", + systemID, + subjectPK, + groups, + ) } } return nil @@ -906,3 +939,45 @@ func (l *groupService) GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64, e return subjectTemplateGroupExpiredAt, nil } + +// subjectSystemGroupMerger 合并相同subject system的多个group同时变更, 用于subject template group的成员变更场景 +type subjectSystemGroupMerger struct { + subjectSystemGroup map[string]map[int64]int64 // key: subjectPK:systemID, map: groupPK-expiredAt +} + +func newSubjectSystemGroupMerger() *subjectSystemGroupMerger { + return &subjectSystemGroupMerger{ + subjectSystemGroup: make(map[string]map[int64]int64), + } +} + +// Add adds a group to the subjectSystemGroup map +func (h *subjectSystemGroupMerger) Add(subjectPK int64, systemID string, groupPK int64, expiredAt int64) { + key := h.generateKey(subjectPK, systemID) + if _, ok := h.subjectSystemGroup[key]; !ok { + h.subjectSystemGroup[key] = make(map[int64]int64) + } + + h.subjectSystemGroup[key][groupPK] = expiredAt +} + +// generateKey generates a key based on subjectPK and systemID +func (h *subjectSystemGroupMerger) generateKey(subjectPK int64, systemID string) string { + return fmt.Sprintf("%d:%s", subjectPK, systemID) +} + +// ParseKey parses a key into subjectPK and systemID +func (h *subjectSystemGroupMerger) ParseKey(key string) (subjectPK int64, systemID string, err error) { + parts := strings.Split(key, ":") + if len(parts) != 2 { + return 0, "", fmt.Errorf("invalid key format") + } + + subjectPK, err = strconv.ParseInt(parts[0], 10, 64) + if err != nil { + return 0, "", err + } + + systemID = parts[1] + return subjectPK, systemID, nil +} diff --git a/pkg/service/group_system_auth_type.go b/pkg/service/group_system_auth_type.go index 6b992376..4f88695a 100644 --- a/pkg/service/group_system_auth_type.go +++ b/pkg/service/group_system_auth_type.go @@ -15,6 +15,7 @@ import ( "errors" "time" + "github.com/TencentBlueKing/gopkg/collection/set" "github.com/TencentBlueKing/gopkg/errorx" "github.com/jmoiron/sqlx" @@ -53,12 +54,30 @@ func (s *groupService) AlterGroupAuthType( return false, errorWrapf(err, "manager.ListGroupMember groupPK=`%d` fail", groupPK) } - for _, member := range members { - err := s.removeSubjectSystemGroup(tx, member.SubjectPK, systemID, groupPK) + subjectSet := set.NewInt64Set() + for _, relation := range members { + subjectSet.Add(relation.SubjectPK) + } + + // 查询用户组模版成员 + relations, err := s.subjectTemplateGroupManager.ListThinRelationWithMaxExpiredAtByGroupPK(groupPK) + if err != nil { + return false, errorWrapf( + err, + "subjectTemplateGroupManager.ListThinRelationWithMaxExpiredAtByGroupPK groupPK=`%d` fail", + groupPK, + ) + } + for _, relation := range relations { + subjectSet.Add(relation.SubjectPK) + } + + for _, subjectPK := range subjectSet.ToSlice() { + err := s.removeSubjectSystemGroup(tx, subjectPK, systemID, map[int64]int64{groupPK: 0}) if err != nil { return false, errorWrapf( err, "removeSubjectSystemGroup member=`%d` systemID=`%s` groupPK=`%d` fail", - member.SubjectPK, systemID, groupPK, + subjectPK, systemID, groupPK, ) } } @@ -81,20 +100,45 @@ func (s *groupService) AlterGroupAuthType( return false, errorWrapf(err, "manager.ListGroupMember groupPK=`%d` fail", groupPK) } + // 查询用户组模版成员 + relations, err := s.subjectTemplateGroupManager.ListThinRelationWithMaxExpiredAtByGroupPK(groupPK) + if err != nil { + return false, errorWrapf( + err, + "subjectTemplateGroupManager.ListThinRelationWithMaxExpiredAtByGroupPK groupPK=`%d` fail", + groupPK, + ) + } + nowTS := time.Now().Unix() - for _, member := range members { - // NOTE: subject system group表中只需要保持未过期的记录 - if member.ExpiredAt < nowTS { + subjectExpiredAtMap := make(map[int64]int64, len(relations)+len(members)) + for _, relation := range members { + if relation.ExpiredAt < nowTS { continue } + subjectExpiredAtMap[relation.SubjectPK] = relation.ExpiredAt + } + + for _, relation := range relations { + if relation.ExpiredAt < nowTS { + continue + } + + // 取过期时间大的 + if relation.ExpiredAt > subjectExpiredAtMap[relation.SubjectPK] { + subjectExpiredAtMap[relation.SubjectPK] = relation.ExpiredAt + } + } + + for subjectPK, expiredAt := range subjectExpiredAtMap { err := s.addOrUpdateSubjectSystemGroup( - tx, member.SubjectPK, systemID, groupPK, member.ExpiredAt, + tx, subjectPK, systemID, map[int64]int64{groupPK: expiredAt}, ) if err != nil { return false, errorWrapf( err, "addOrUpdateSubjectSystemGroup member=`%d` systemID=`%s` groupPK=`%d` fail", - member.SubjectPK, systemID, groupPK, + subjectPK, systemID, groupPK, ) } } diff --git a/pkg/service/group_system_auth_type_test.go b/pkg/service/group_system_auth_type_test.go index b5fb67ca..43e68893 100644 --- a/pkg/service/group_system_auth_type_test.go +++ b/pkg/service/group_system_auth_type_test.go @@ -398,9 +398,15 @@ var _ = Describe("GroupService", func() { []dao.SubjectRelation{}, nil, ).AnyTimes() + mockSubjectTemplateGroupManager := mock.NewMockSubjectTemplateGroupManager(ctl) + mockSubjectTemplateGroupManager.EXPECT().ListThinRelationWithMaxExpiredAtByGroupPK(int64(1)).Return( + []dao.ThinSubjectRelation{}, nil, + ) + manager := &groupService{ - manager: mockSubjectRelationManger, - authTypeManger: mockGroupSystemAuthTypeManager, + manager: mockSubjectRelationManger, + authTypeManger: mockGroupSystemAuthTypeManager, + subjectTemplateGroupManager: mockSubjectTemplateGroupManager, } changed, err := manager.AlterGroupAuthType(nil, "test", 1, 0) diff --git a/pkg/service/group_test.go b/pkg/service/group_test.go index eba04781..4f04803b 100644 --- a/pkg/service/group_test.go +++ b/pkg/service/group_test.go @@ -840,3 +840,66 @@ var _ = Describe("GroupService", func() { }) }) }) + +var _ = Describe("SubjectSystemGroupHelper", func() { + var helper *subjectSystemGroupMerger + + BeforeEach(func() { + helper = &subjectSystemGroupMerger{ + subjectSystemGroup: make(map[string]map[int64]int64), + } + }) + + Describe("Adding groups", func() { + Context("When adding a new group", func() { + It("should add the group correctly", func() { + helper.Add(1, "system1", 1, 1000) + expected := map[string]map[int64]int64{ + "1:system1": {1: 1000}, + } + assert.Equal(GinkgoT(), helper.subjectSystemGroup, expected) + }) + }) + + Context("When adding multiple groups", func() { + It("should add the groups correctly", func() { + helper.Add(1, "system1", 1, 1000) + helper.Add(1, "system1", 2, 2000) + expected := map[string]map[int64]int64{ + "1:system1": { + 1: 1000, + 2: 2000, + }, + } + assert.Equal(GinkgoT(), helper.subjectSystemGroup, expected) + }) + }) + }) + + Describe("Generating key", func() { + Context("With valid subjectPK and systemID", func() { + It("should generate the correct key", func() { + key := helper.generateKey(1, "system1") + assert.Equal(GinkgoT(), key, "1:system1") + }) + }) + }) + + Describe("Parsing key", func() { + Context("With a valid key", func() { + It("should parse the key correctly", func() { + subjectPK, systemID, err := helper.ParseKey("1:system1") + assert.NoError(GinkgoT(), err) + assert.Equal(GinkgoT(), subjectPK, int64(1)) + assert.Equal(GinkgoT(), systemID, "system1") + }) + }) + + Context("With an invalid key", func() { + It("should return an error", func() { + _, _, err := helper.ParseKey("invalid") + assert.Error(GinkgoT(), err) + }) + }) + }) +}) diff --git a/pkg/service/subject_system_group.go b/pkg/service/subject_system_group.go index d37bbb7a..24f9cbeb 100644 --- a/pkg/service/subject_system_group.go +++ b/pkg/service/subject_system_group.go @@ -37,7 +37,8 @@ const RetryCount = 3 func (l *groupService) doUpdateSubjectSystemGroup( tx *sqlx.Tx, systemID string, - subjectPK, groupPK, expiredAt int64, + subjectPK int64, + groupExpiredAtMap map[int64]int64, createIfNotExists bool, updateGroupExpiredAtFunc func(groupExpiredAtMap map[int64]int64) (map[int64]int64, error), ) error { @@ -47,7 +48,7 @@ func (l *groupService) doUpdateSubjectSystemGroup( subjectSystemGroup, err := l.subjectSystemGroupManager.GetBySystemSubject(systemID, subjectPK) if createIfNotExists && errors.Is(err, sql.ErrNoRows) { // 查不到数据时, 如果需要创建, 则创建 - err = l.createSubjectSystemGroup(tx, systemID, subjectPK, groupPK, expiredAt) + err = l.createSubjectSystemGroup(tx, systemID, subjectPK, groupExpiredAtMap) if database.IsMysqlDuplicateEntryError(err) { return ErrNeedRetry } @@ -96,19 +97,21 @@ func (l *groupService) addOrUpdateSubjectSystemGroup( tx *sqlx.Tx, subjectPK int64, systemID string, - groupPK, expiredAt int64, + groupExpiredAtMap map[int64]int64, ) (err error) { errorWrapf := errorx.NewLayerFunctionErrorWrapf(SubjectSVC, "addOrUpdateSubjectSystemGroup") // 更新或创建新的关系 - addOrUpdateFunc := func(groupExpiredAtMap map[int64]int64) (map[int64]int64, error) { - groupExpiredAtMap[groupPK] = expiredAt - return groupExpiredAtMap, nil + addOrUpdateFunc := func(groups map[int64]int64) (map[int64]int64, error) { + for groupPK, expiredAt := range groupExpiredAtMap { + groups[groupPK] = expiredAt + } + return groups, nil } // 乐观锁, 重复提交, 最多3次 for i := 0; i < RetryCount; i++ { - err = l.doUpdateSubjectSystemGroup(tx, systemID, subjectPK, groupPK, expiredAt, true, addOrUpdateFunc) + err = l.doUpdateSubjectSystemGroup(tx, systemID, subjectPK, groupExpiredAtMap, true, addOrUpdateFunc) if err == nil { return nil } @@ -119,8 +122,8 @@ func (l *groupService) addOrUpdateSubjectSystemGroup( if err != nil { err = errorWrapf( - err, "addOrUpdateSubjectSystemGroup fail, systemID: %s, subjectPK: %d, groupPK: %d, expiredAt: %d", - systemID, subjectPK, groupPK, expiredAt, + err, "addOrUpdateSubjectSystemGroup fail, systemID: %s, subjectPK: %d, groupExpiredAtMap: %v", + systemID, subjectPK, groupExpiredAtMap, ) return } @@ -134,22 +137,26 @@ func (l *groupService) removeSubjectSystemGroup( tx *sqlx.Tx, subjectPK int64, systemID string, - groupPK int64, + groupExpiredAtMap map[int64]int64, ) (err error) { errorWrapf := errorx.NewLayerFunctionErrorWrapf(SubjectSVC, "removeSubjectSystemGroup") - removeFunc := func(groupExpiredAtMap map[int64]int64) (map[int64]int64, error) { - _, ok := groupExpiredAtMap[groupPK] - if !ok { + removeFunc := func(groups map[int64]int64) (map[int64]int64, error) { + length := len(groups) + for groupPK := range groupExpiredAtMap { + delete(groups, groupPK) + } + + if length == len(groups) { return nil, ErrNoSubjectSystemGroup } - delete(groupExpiredAtMap, groupPK) - return groupExpiredAtMap, nil + + return groups, nil } // 乐观锁, 重复提交, 最多3次 for i := 0; i < RetryCount; i++ { - err = l.doUpdateSubjectSystemGroup(tx, systemID, subjectPK, groupPK, 0, false, removeFunc) + err = l.doUpdateSubjectSystemGroup(tx, systemID, subjectPK, groupExpiredAtMap, false, removeFunc) if err == nil { return nil } @@ -160,15 +167,15 @@ func (l *groupService) removeSubjectSystemGroup( if errors.Is(err, sql.ErrNoRows) || errors.Is(err, ErrNoSubjectSystemGroup) { // 数据不存在时记录日志 - log.Warningf("removeSubjectSystemGroup not exists systemID=`%s`, subjectPK=`%d`, groupPK=`%d`", - systemID, subjectPK, groupPK) + log.Warningf("removeSubjectSystemGroup not exists systemID=`%s`, subjectPK=`%d`, groupExpiredAtMap=`%v`", + systemID, subjectPK, groupExpiredAtMap) return nil } if err != nil { err = errorWrapf( - err, "removeSubjectSystemGroup fail, systemID: %s, subjectPK: %d, groupPK: %d", - systemID, subjectPK, groupPK, + err, "removeSubjectSystemGroup fail, systemID: %s, subjectPK: %d, groupExpiredAtMap: %v", + systemID, subjectPK, groupExpiredAtMap, ) return } @@ -180,9 +187,10 @@ func (l *groupService) removeSubjectSystemGroup( func (l *groupService) createSubjectSystemGroup( tx *sqlx.Tx, systemID string, - subjectPK, groupPK, expiredAt int64, + subjectPK int64, + groupExpiredAtMap map[int64]int64, ) error { - groups, err := jsoniter.MarshalToString(map[int64]int64{groupPK: expiredAt}) + groups, err := jsoniter.MarshalToString(groupExpiredAtMap) if err != nil { return err } diff --git a/pkg/service/subject_system_group_test.go b/pkg/service/subject_system_group_test.go index b9460f1a..a2b8049a 100644 --- a/pkg/service/subject_system_group_test.go +++ b/pkg/service/subject_system_group_test.go @@ -47,7 +47,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.createSubjectSystemGroup(nil, "system", int64(1), 1, 1555555555) + err := manager.createSubjectSystemGroup(nil, "system", int64(1), map[int64]int64{1: 1555555555}) assert.Error(GinkgoT(), err) }) @@ -61,7 +61,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.createSubjectSystemGroup(nil, "system", int64(1), 1, 1555555555) + err := manager.createSubjectSystemGroup(nil, "system", int64(1), map[int64]int64{1: 1555555555}) assert.NoError(GinkgoT(), err) }) }) @@ -116,7 +116,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", 1, 1555555555) + err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{1: 1555555555}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "GetBySystemSubject") }) @@ -135,7 +135,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", 1, 1555555555) + err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{1: 1555555555}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "GetBySystemSubject") }) @@ -154,7 +154,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", 1, 1555555555) + err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{1: 1555555555}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "UpdateWithTx") }) @@ -183,7 +183,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", 1, 1555555555) + err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{1: 1555555555}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "UpdateWithTx") }) @@ -202,7 +202,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", 1, 1555555555) + err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{1: 1555555555}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "retry") }) @@ -221,7 +221,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", 1, 1555555555) + err := manager.addOrUpdateSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{1: 1555555555}) assert.NoError(GinkgoT(), err) }) }) @@ -247,7 +247,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.removeSubjectSystemGroup(nil, int64(1), "system", int64(2)) + err := manager.removeSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{2: 0}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "GetBySystemSubject") }) @@ -264,7 +264,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.removeSubjectSystemGroup(nil, int64(1), "system", int64(2)) + err := manager.removeSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{2: 0}) assert.NoError(GinkgoT(), err) }) @@ -284,7 +284,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.removeSubjectSystemGroup(nil, int64(1), "system", int64(2)) + err := manager.removeSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{2: 0}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "UpdateWithTx") }) @@ -305,7 +305,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.removeSubjectSystemGroup(nil, int64(1), "system", int64(2)) + err := manager.removeSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{2: 0}) assert.Error(GinkgoT(), err) assert.Contains(GinkgoT(), err.Error(), "retry") }) @@ -326,7 +326,7 @@ var _ = Describe("SubjectService", func() { subjectSystemGroupManager: mockSubjectSystemGroupManager, } - err := manager.removeSubjectSystemGroup(nil, int64(1), "system", int64(2)) + err := manager.removeSubjectSystemGroup(nil, int64(1), "system", map[int64]int64{2: 0}) assert.NoError(GinkgoT(), err) }) })