-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* | ||
* Copyright 2016 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package grpclb | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/balancer/roundrobin" | ||
) | ||
|
||
type serviceConfig struct { | ||
LoadBalancingConfig *[]map[string]*grpclbServiceConfig | ||
} | ||
|
||
type grpclbServiceConfig struct { | ||
ChildPolicy *[]map[string]json.RawMessage | ||
} | ||
|
||
func parseFullServiceConfig(s string) *serviceConfig { | ||
var ret serviceConfig | ||
err := json.Unmarshal([]byte(s), &ret) | ||
if err != nil { | ||
return nil | ||
} | ||
return &ret | ||
} | ||
|
||
func parseServiceConfig(s string) *grpclbServiceConfig { | ||
parsedSC := parseFullServiceConfig(s) | ||
if parsedSC == nil { | ||
return nil | ||
} | ||
lbConfigs := parsedSC.LoadBalancingConfig | ||
if lbConfigs == nil { | ||
return nil | ||
} | ||
for _, lbC := range *lbConfigs { | ||
if v, ok := lbC[grpclbName]; ok { | ||
return v | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
const ( | ||
roundRobinName = roundrobin.Name | ||
pickFirstName = grpc.PickFirstBalancerName | ||
) | ||
|
||
func childIsPickFirst(s string) bool { | ||
parsedSC := parseServiceConfig(s) | ||
if parsedSC == nil { | ||
return false | ||
} | ||
childConfigs := parsedSC.ChildPolicy | ||
if childConfigs == nil { | ||
return false | ||
} | ||
for _, childC := range *childConfigs { | ||
// If round_robin exists before pick_first, return false | ||
if _, ok := childC[roundRobinName]; ok { | ||
return false | ||
} | ||
// If pick_first is before round_robin, return true | ||
if _, ok := childC[pickFirstName]; ok { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* | ||
* Copyright 2016 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package grpclb | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_parseFullServiceConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
s string | ||
want *serviceConfig | ||
}{ | ||
{ | ||
name: "empty", | ||
s: "", | ||
want: nil, | ||
}, | ||
{ | ||
name: "success1", | ||
s: `{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"pick_first":{}}]}}]}`, | ||
want: &serviceConfig{ | ||
LoadBalancingConfig: &[]map[string]*grpclbServiceConfig{ | ||
{"grpclb": &grpclbServiceConfig{ | ||
ChildPolicy: &[]map[string]json.RawMessage{ | ||
{"pick_first": json.RawMessage("{}")}, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "success2", | ||
s: `{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"round_robin":{}},{"pick_first":{}}]}}]}`, | ||
want: &serviceConfig{ | ||
LoadBalancingConfig: &[]map[string]*grpclbServiceConfig{ | ||
{"grpclb": &grpclbServiceConfig{ | ||
ChildPolicy: &[]map[string]json.RawMessage{ | ||
{"round_robin": json.RawMessage("{}")}, | ||
{"pick_first": json.RawMessage("{}")}, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := parseFullServiceConfig(tt.s); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("parseFullServiceConfig() = %+v, want %+v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_parseServiceConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
s string | ||
want *grpclbServiceConfig | ||
}{ | ||
{ | ||
name: "empty", | ||
s: "", | ||
want: nil, | ||
}, | ||
{ | ||
name: "success1", | ||
s: `{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"pick_first":{}}]}}]}`, | ||
want: &grpclbServiceConfig{ | ||
ChildPolicy: &[]map[string]json.RawMessage{ | ||
{"pick_first": json.RawMessage("{}")}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "success2", | ||
s: `{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"round_robin":{}},{"pick_first":{}}]}}]}`, | ||
want: &grpclbServiceConfig{ | ||
ChildPolicy: &[]map[string]json.RawMessage{ | ||
{"round_robin": json.RawMessage("{}")}, | ||
{"pick_first": json.RawMessage("{}")}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "no_grpclb", | ||
s: `{"loadBalancingConfig":[{"notgrpclb":{"childPolicy":[{"round_robin":{}},{"pick_first":{}}]}}]}`, | ||
want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := parseServiceConfig(tt.s); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("parseFullServiceConfig() = %+v, want %+v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_childIsPickFirst(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
s string | ||
want bool | ||
}{ | ||
{ | ||
name: "invalid", | ||
s: "", | ||
want: false, | ||
}, | ||
{ | ||
name: "pickfirst_only", | ||
s: `{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"pick_first":{}}]}}]}`, | ||
want: true, | ||
}, | ||
{ | ||
name: "pickfirst_before_rr", | ||
s: `{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"pick_first":{}},{"round_robin":{}}]}}]}`, | ||
want: true, | ||
}, | ||
{ | ||
name: "rr_before_pickfirst", | ||
s: `{"loadBalancingConfig":[{"grpclb":{"childPolicy":[{"round_robin":{}},{"pick_first":{}}]}}]}`, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := childIsPickFirst(tt.s); got != tt.want { | ||
t.Errorf("childIsPickFirst() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters