Skip to content

Commit

Permalink
fix critical bug: miss used index (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang authored Jan 17, 2019
1 parent 7e78d21 commit b972510
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
22 changes: 2 additions & 20 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@ func (c *RegistryClient) BatchFindInstances(consumerID string, keys []*proto.Fin
if err != nil {
return nil, NewJSONException(err, string(rBody))
}
openlogging.Debug("request uri:" + url)
openlogging.Debug("request body:" + string(rBody))
resp, err := c.HTTPDo("POST", url, http.Header{"X-ConsumerId": []string{consumerID}}, rBody)
if err != nil {
Expand All @@ -538,31 +537,14 @@ func (c *RegistryClient) BatchFindInstances(consumerID string, keys []*proto.Fin
return nil, fmt.Errorf("BatchFindInstances failed, response is empty")
}
body := httputil.ReadBody(resp)
openlogging.Debug("response body:" + string(body))
if resp.StatusCode == 200 {
instanceMap := make(map[string][]*proto.MicroServiceInstance, 0)
var response proto.BatchFindInstancesResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, NewJSONException(err, string(body))
}
if response.Services != nil {
for i, result := range response.Services.Updated {
if len(result.Instances) == 0 {
continue
}
for _, instance := range result.Instances {
instance.ServiceName = keys[i].Service.ServiceName
instance.App = keys[i].Service.AppId
instances, ok := instanceMap[instance.ServiceName]
if !ok {
instances = make([]*proto.MicroServiceInstance, 0)
instanceMap[instance.ServiceName] = instances
}
instanceMap[instance.ServiceName] = append(instances, instance)
}

}
}
instanceMap := RegroupInstances(keys, response)
return instanceMap, nil
}
return nil, fmt.Errorf("batch find failed, status %d, body %s", resp.StatusCode, body)
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func TestRegistryClient_FindMicroServiceInstances(t *testing.T) {
t.Log(instances)
assert.NoError(t, err)

fs = []*proto.FindService{f, f2}
fs = []*proto.FindService{f2, f}
instances, err = registryClient.BatchFindInstances(sid, fs)
t.Log(instances)
assert.NoError(t, err)
Expand Down
24 changes: 24 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/url"
"time"
"github.com/go-chassis/go-sc-client/proto"
)

func getBackOff(backoffType string) backoff.BackOff {
Expand Down Expand Up @@ -39,3 +40,26 @@ func getProtocolMap(eps []string) map[string]string {
}
return m
}

func RegroupInstances(keys []*proto.FindService,response proto.BatchFindInstancesResponse) map[string][]*proto.MicroServiceInstance{
instanceMap := make(map[string][]*proto.MicroServiceInstance, 0)
if response.Services != nil {
for _, result := range response.Services.Updated {
if len(result.Instances) == 0 {
continue
}
for _, instance := range result.Instances {
instance.ServiceName = keys[result.Index].Service.ServiceName
instance.App = keys[result.Index].Service.AppId
instances, ok := instanceMap[instance.ServiceName]
if !ok {
instances = make([]*proto.MicroServiceInstance, 0)
instanceMap[instance.ServiceName] = instances
}
instanceMap[instance.ServiceName] = append(instances, instance)
}

}
}
return instanceMap
}
43 changes: 43 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package client_test

import (
"github.com/go-chassis/go-sc-client"
"github.com/go-chassis/go-sc-client/proto"
"github.com/stretchr/testify/assert"
"testing"
)

func TestRegroupInstances(t *testing.T) {
keys := []*proto.FindService{
{
Service: &proto.MicroServiceKey{
ServiceName: "Service1",
},
},
{
Service: &proto.MicroServiceKey{
ServiceName: "Service2",
},
},
{
Service: &proto.MicroServiceKey{
ServiceName: "Service3",
},
},
}
resp := proto.BatchFindInstancesResponse{
Services: &proto.BatchFindResult{
Updated: []*proto.FindResult{
{Index: 2,
Instances: []*proto.MicroServiceInstance{{
InstanceId: "1",
}}},
},
},
}
m := client.RegroupInstances(keys, resp)
t.Log(m)
assert.Equal(t, 1, len(m["Service3"]))
assert.Equal(t, 0, len(m["Service1"]))
assert.Equal(t, 0, len(m["Service2"]))
}

0 comments on commit b972510

Please sign in to comment.