Skip to content

Commit

Permalink
feat: add related cache for additional instance data.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian4 committed Oct 13, 2023
1 parent 1ee4956 commit fff45cf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 14 additions & 0 deletions cache/service/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,20 @@ func (ic *instanceCache) GetInstance(instanceID string) *model.Instance {
return value
}

// GetInstanceConsole 根据实例ID获取实例数据
func (ic *instanceCache) GetInstanceConsole(instanceConsoleID string) *model.InstanceConsole {
if instanceConsoleID == "" {
return nil
}

value, ok := ic.instanceConsoles.Load(instanceConsoleID)
if !ok {
return nil
}

return value
}

// GetInstancesByServiceID 根据ServiceID获取实例数据
func (ic *instanceCache) GetInstancesByServiceID(serviceID string) []*model.Instance {
if serviceID == "" {
Expand Down
18 changes: 12 additions & 6 deletions cache/service/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func TestInstanceCache_Update(t *testing.T) {
}

servicesCount, instancesCount := iteratorInstances(ic)
if servicesCount == 2 && instancesCount == 10+5 { // gen两次,有两个不同服务
instanceConsoleCounts := ic.instanceConsoles.Len()
if servicesCount == 2 && instancesCount == 10+5 && instanceConsoleCounts == 3 { // gen两次,有两个不同服务
t.Logf("pass")
} else {
t.Fatalf("error: %d, %d", servicesCount, instancesCount)
Expand All @@ -172,7 +173,8 @@ func TestInstanceCache_Update(t *testing.T) {
}

servicesCount, instancesCount := iteratorInstances(ic)
if servicesCount != 0 || instancesCount != 0 {
instanceConsoleCounts := ic.instanceConsoles.Len()
if servicesCount != 0 || instancesCount != 0 || instanceConsoleCounts != 0 {
t.Fatalf("error: %d %d", servicesCount, instancesCount)
}
})
Expand Down Expand Up @@ -265,13 +267,9 @@ func TestInstanceCache_Update2(t *testing.T) {
t.Run("对账发现缓存数据数量和存储层不一致", func(t *testing.T) {
_ = ic.Clear()
instances := genModelInstances("service-a", 20)
instanceConsoles := genModelInstancesConsole("console", 3)

queryCount := int32(0)
storage.EXPECT().GetInstancesCountTx(gomock.Any()).Return(uint32(0), nil).AnyTimes()
storage.EXPECT().
GetMoreInstanceConsoles(gomock.Any(), gomock.Any(), ic.IsFirstUpdate(), ic.needMeta, ic.systemServiceID).
Return(instanceConsoles, nil)
storage.EXPECT().
GetMoreInstances(gomock.Any(), gomock.Any(), ic.IsFirstUpdate(), ic.needMeta, ic.systemServiceID).
DoAndReturn(func(tx store.Tx, mtime time.Time, firstUpdate, needMeta bool, svcIds []string) (map[string]*model.Instance, error) {
Expand Down Expand Up @@ -315,6 +313,14 @@ func TestInstanceCache_GetInstance(t *testing.T) {
if instance := ic.GetInstance("test-instance-xx"); instance != nil {
t.Fatalf("error")
}

if instanceConsole := ic.GetInstanceConsole(instanceConsoles[fmt.Sprintf("InstanceConsole-%s-%d", "console", 2)].Id); instanceConsole == nil {
t.Fatalf("error")
}

if instanceConsole := ic.GetInstance("test-instanceConsole-xx"); instanceConsole != nil {
t.Fatalf("error")
}
})
}

Expand Down

0 comments on commit fff45cf

Please sign in to comment.