diff --git a/common/commonStructs.go b/common/commonStructs.go index 8f6d340..12969ab 100644 --- a/common/commonStructs.go +++ b/common/commonStructs.go @@ -7,4 +7,5 @@ type MockParams struct { MockPushDelay int64 MockServiceNamePrefix string MockTestIncremental bool + MockIncrementalRatio int } diff --git a/nacos-istio.go b/nacos-istio.go index c162265..e05c89e 100644 --- a/nacos-istio.go +++ b/nacos-istio.go @@ -31,6 +31,8 @@ func main() { mockTestIncremental := flag.Bool("mockTestIncremental", false, "mock service is incremental") + mockIncrementalRatio := flag.Int("mockIncrementalRatio", 0, "ratio of incremental push services") + flag.Parse() mockParams := &common.MockParams{ @@ -40,6 +42,7 @@ func main() { MockPushDelay: *mockPushDelay, MockServiceNamePrefix: *mockServiceNamePrefix, MockTestIncremental: *mockTestIncremental, + MockIncrementalRatio: *mockIncrementalRatio, } a := service.NewService(*grpcAddr, *mockParams) diff --git a/nacos/nacosPushService.go b/nacos/nacosPushService.go index ae23131..17e2ec5 100644 --- a/nacos/nacosPushService.go +++ b/nacos/nacosPushService.go @@ -64,11 +64,6 @@ func (mockService *MockNacosService) constructServices() { collection := "istio/networking/v1alpha3/serviceentries" incremental := false - if mockService.MockParams.MockTestIncremental { - collection = "istio/networking/v1alpha3/synthetic/serviceentries" - incremental = true - } - mockService.Resources = &v1alpha1.Resources{ Collection: collection, Incremental: incremental, @@ -157,11 +152,25 @@ func (mockService *MockNacosService) constructServices() { } func (mockService *MockNacosService) notifyServiceChange() { - for { + incrementalResources := &v1alpha1.Resources{ + Collection: "istio/networking/v1alpha3/synthetic/serviceentries", + Incremental: true, + } + pushServiceCount := len(mockService.Resources.Resources) * mockService.MockParams.MockIncrementalRatio / 100 + for _, resource := range mockService.Resources.Resources { + if pushServiceCount <= 0 { + break + } + pushServiceCount-- + resource.Metadata.Annotations["networking.alpha.istio.io/endpointsVersion"] = strconv.FormatInt(time.Now().UnixNano()/1000, 10) + incrementalResources.Resources = append(incrementalResources.Resources, resource) + } + + for { for _, callback := range mockService.callbacks { if mockService.MockParams.MockTestIncremental { - go callback(mockService.generateIncrementalService(), nil) + go callback(incrementalResources, nil) } else { go callback(mockService.Resources, nil) } @@ -169,82 +178,3 @@ func (mockService *MockNacosService) notifyServiceChange() { time.Sleep(time.Duration(mockService.MockParams.MockPushDelay) * time.Millisecond) } } - -func (mockService *MockNacosService) generateIncrementalService() (rss *v1alpha1.Resources) { - - port := &v1alpha3.Port{ - Number: 8080, - Protocol: "HTTP", - Name: "http", - } - - totalInstanceCount := 0 - - labels := make(map[string]string) - labels["p"] = "hessian2" - labels["ROUTE"] = "0" - labels["APP"] = "ump" - labels["st"] = "na62" - labels["v"] = "2.0" - labels["TIMEOUT"] = "3000" - labels["ih2"] = "y" - labels["mg"] = "ump2_searchhost" - labels["WRITE_MODE"] = "unit" - labels["CONNECTTIMEOUT"] = "1000" - labels["SERIALIZETYPE"] = "hessian" - labels["ut"] = "UNZBMIX25G" - - rand.Seed(time.Now().Unix()) - svcName := mockService.MockParams.MockServiceNamePrefix + "." + strconv.Itoa(rand.Intn(1000000)) - se := &v1alpha3.ServiceEntry{ - Hosts: []string{svcName + ".nacos"}, - Resolution: v1alpha3.ServiceEntry_STATIC, - Location: 1, - Ports: []*v1alpha3.Port{port}, - } - - instanceCount := 10 - - totalInstanceCount += instanceCount - - for i := 0; i < instanceCount; i++ { - - ip := fmt.Sprintf("%d.%d.%d.%d", - byte(i>>24), byte(i>>16), byte(i>>8), byte(i)) - - endpoint := &v1alpha3.ServiceEntry_Endpoint{ - Labels: labels, - } - - endpoint.Address = ip - endpoint.Ports = map[string]uint32{ - "http": uint32(8080), - } - - se.Endpoints = append(se.Endpoints, endpoint) - } - - seAny, err := types.MarshalAny(se) - if err != nil { - return nil - } - - res := v1alpha1.Resource{ - Body: seAny, - Metadata: &v1alpha1.Metadata{ - Annotations: map[string]string{ - "virtual": "1", - }, - Name: "nacos" + "/" + svcName, // goes to model.Config.Name and Namespace - of course different syntax - }, - } - - rss = &v1alpha1.Resources{ - Collection: "istio/networking/v1alpha3/serviceentries", - Incremental: true, - } - - rss.Resources = append(rss.Resources, res) - - return rss -} diff --git a/service/nacosMcpService.go b/service/nacosMcpService.go index 2200a62..916a55d 100644 --- a/service/nacosMcpService.go +++ b/service/nacosMcpService.go @@ -137,13 +137,6 @@ func NewService(addr string, mockParams common.MockParams) *NacosMcpService { return } - // For incremental push, always update endpoint version for all services: - if nacosMcpService.nacosPushService.MockParams.MockTestIncremental { - for _, resource := range resources.Resources { - resource.Metadata.Annotations["networking.alpha.istio.io/endpointsVersion"] = strconv.FormatInt(time.Now().UnixNano()/1000, 10) - } - } - for _, con := range nacosMcpService.clients { if con.LastRequestAcked == false {