diff --git a/server/api/label.go b/server/api/label.go index ac738419662..890ecfeffab 100644 --- a/server/api/label.go +++ b/server/api/label.go @@ -16,6 +16,7 @@ package api import ( "net/http" "regexp" + "strings" "github.com/juju/errors" "github.com/pingcap/kvproto/pkg/metapb" @@ -47,8 +48,8 @@ func (h *labelsHandler) Get(w http.ResponseWriter, r *http.Request) { for _, s := range stores { ls := s.GetLabels() for _, l := range ls { - if _, ok := m[l.Key+l.Value]; !ok { - m[l.Key+l.Value] = struct{}{} + if _, ok := m[strings.ToLower(l.Key+l.Value)]; !ok { + m[strings.ToLower(l.Key+l.Value)] = struct{}{} labels = append(labels, l) } } @@ -100,11 +101,12 @@ type storesLabelFilter struct { } func newStoresLabelFilter(name, value string) (*storesLabelFilter, error) { - keyPattern, err := regexp.Compile(name) + // add (?i) to set a case-insensitive flag + keyPattern, err := regexp.Compile("(?i)" + name) if err != nil { return nil, errors.Trace(err) } - valuePattern, err := regexp.Compile(value) + valuePattern, err := regexp.Compile("(?i)" + value) if err != nil { return nil, errors.Trace(err) } diff --git a/server/api/label_test.go b/server/api/label_test.go index 9000d579271..fe5f4bd676b 100644 --- a/server/api/label_test.go +++ b/server/api/label_test.go @@ -128,7 +128,7 @@ func (s *testLabelsStoreSuite) TestStoresLabelFilter(c *C) { want []*metapb.Store }{ { - name: "zone", + name: "Zone", want: s.stores[:], }, { @@ -137,22 +137,22 @@ func (s *testLabelsStoreSuite) TestStoresLabelFilter(c *C) { }, { name: "zone", - value: "us-west-1", + value: "Us-west-1", want: s.stores[:1], }, { - name: "zone", + name: "Zone", value: "west", want: s.stores[:2], }, { - name: "zo", - value: "beijing", + name: "Zo", + value: "Beijing", want: s.stores[2:3], }, { - name: "zone", - value: "ssd", + name: "ZONE", + value: "SSD", want: []*metapb.Store{}, }, } diff --git a/server/api/store_test.go b/server/api/store_test.go index 76cc347ed2b..a3fce509f97 100644 --- a/server/api/store_test.go +++ b/server/api/store_test.go @@ -145,7 +145,7 @@ func (s *testStoreSuite) TestStoreLabel(c *C) { } // Test merge. - labels = map[string]string{"zack": "zack1", "host": "host1"} + labels = map[string]string{"zack": "zack1", "Host": "host1"} b, err = json.Marshal(labels) c.Assert(err, IsNil) err = postJSON(&http.Client{}, url+"/label", b) diff --git a/server/core/store.go b/server/core/store.go index dfd16d27274..a7fd4eb884f 100644 --- a/server/core/store.go +++ b/server/core/store.go @@ -14,6 +14,7 @@ package core import ( + "strings" "time" log "github.com/Sirupsen/logrus" @@ -193,7 +194,7 @@ func (s *StoreInfo) IsDisconnected() bool { // GetLabelValue returns a label's value (if exists). func (s *StoreInfo) GetLabelValue(key string) string { for _, label := range s.GetLabels() { - if label.GetKey() == key { + if strings.EqualFold(label.GetKey(), key) { return label.GetValue() } } @@ -207,7 +208,7 @@ func (s *StoreInfo) CompareLocation(other *StoreInfo, labels []string) int { v1, v2 := s.GetLabelValue(key), other.GetLabelValue(key) // If label is not set, the store is considered at the same location // with any other store. - if v1 != "" && v2 != "" && v1 != v2 { + if v1 != "" && v2 != "" && !strings.EqualFold(v1, v2) { return i } } @@ -220,7 +221,7 @@ func (s *StoreInfo) MergeLabels(labels []*metapb.StoreLabel) { L: for _, newLabel := range labels { for _, label := range s.Labels { - if label.Key == newLabel.Key { + if strings.EqualFold(label.Key, newLabel.Key) { label.Value = newLabel.Value continue L }