Skip to content

Commit

Permalink
Support set ratio of incremental services
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Oct 29, 2019
1 parent 0fc6995 commit 8400093
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 93 deletions.
1 change: 1 addition & 0 deletions common/commonStructs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type MockParams struct {
MockPushDelay int64
MockServiceNamePrefix string
MockTestIncremental bool
MockIncrementalRatio int
}
3 changes: 3 additions & 0 deletions nacos-istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -40,6 +42,7 @@ func main() {
MockPushDelay: *mockPushDelay,
MockServiceNamePrefix: *mockServiceNamePrefix,
MockTestIncremental: *mockTestIncremental,
MockIncrementalRatio: *mockIncrementalRatio,
}

a := service.NewService(*grpcAddr, *mockParams)
Expand Down
102 changes: 16 additions & 86 deletions nacos/nacosPushService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -157,94 +152,29 @@ 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)
}
}
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
}
7 changes: 0 additions & 7 deletions service/nacosMcpService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8400093

Please sign in to comment.