Skip to content

Commit

Permalink
Merge a088da5 into 2720808
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulijian1 committed Mar 2, 2020
2 parents 2720808 + a088da5 commit cea35a3
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
7 changes: 6 additions & 1 deletion pkg/stringutil/string_util.go
Expand Up @@ -22,10 +22,15 @@ import (
"strings"
)

const (
// LabelNone is the format string when the map is none
LabelNone = "none"
)

//FormatMap format map to string
func FormatMap(m map[string]string) string {
if len(m) == 0 {
return "none"
return LabelNone
}
sb := strings.Builder{}
s := make([]string, 0, len(m))
Expand Down
29 changes: 29 additions & 0 deletions pkg/util/util.go
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 util

import "reflect"

//IsEquivalentLabel compares whether two labels are equal.
//In particular, if one is nil and another is an empty map, it return true
func IsEquivalentLabel(x, y map[string]string) bool {
if len(x) == 0 && len(y) == 0 {
return true
}
return reflect.DeepEqual(x, y)
}
44 changes: 44 additions & 0 deletions pkg/util/util_test.go
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 util_test

import (
"testing"

"github.com/apache/servicecomb-kie/pkg/util"
"github.com/stretchr/testify/assert"
)

func TestIsEquivalentLabel(t *testing.T) {
var m1 map[string]string
m2 := make(map[string]string)
m3 := map[string]string{
"foo": "bar",
}
m4 := map[string]string{
"foo": "bar",
}
m5 := map[string]string{
"bar": "foo",
}
assert.Equal(t, util.IsEquivalentLabel(m1, m1), true)
assert.Equal(t, util.IsEquivalentLabel(m1, m2), true)
assert.Equal(t, util.IsEquivalentLabel(m2, m3), false)
assert.Equal(t, util.IsEquivalentLabel(m3, m4), true)
assert.Equal(t, util.IsEquivalentLabel(m3, m5), false)
}
2 changes: 1 addition & 1 deletion server/pubsub/event_handler.go
Expand Up @@ -49,7 +49,7 @@ func handleKVEvent(e serf.Event) {
topics.Range(func(key, value interface{}) bool { //range all topics
t, err := ParseTopicString(key.(string))
if err != nil {
openlogging.Error("can not parse topic:" + key.(string))
openlogging.Error("can not parse topic " + key.(string) + ": " + err.Error())
return true
}
if t.Match(ke) {
Expand Down
16 changes: 14 additions & 2 deletions server/pubsub/struct.go
Expand Up @@ -20,10 +20,11 @@ package pubsub
import (
"encoding/json"
"errors"
"reflect"
"strings"

"github.com/apache/servicecomb-kie/pkg/common"
"github.com/apache/servicecomb-kie/pkg/stringutil"
"github.com/apache/servicecomb-kie/pkg/util"
)

// const
Expand Down Expand Up @@ -67,6 +68,9 @@ func ParseTopicString(s string) (*Topic, error) {
if err != nil {
return nil, err
}
if t.LabelsFormat == stringutil.LabelNone {
return t, nil
}
ls := strings.Split(t.LabelsFormat, "::")
if len(ls) != 0 {
for _, l := range ls {
Expand All @@ -81,6 +85,11 @@ func ParseTopicString(s string) (*Topic, error) {
}

//Match compare event with topic
//If the match type is set to exact in long pulling request, only update request with exactly
//the same label of pulling request will match the request and will trigger an immediate return.
//
//If the match type is not set, it will be matched when pulling request labels is equal to
//update request labels or a subset of it.
func (t *Topic) Match(event *KVChangeEvent) bool {
match := false
if t.Key != "" {
Expand All @@ -89,10 +98,13 @@ func (t *Topic) Match(event *KVChangeEvent) bool {
}
}
if t.MatchType == common.PatternExact {
if !reflect.DeepEqual(t.Labels, event.Labels) {
if !util.IsEquivalentLabel(t.Labels, event.Labels) {
return false
}
}
if len(t.Labels) == 0 {
return true
}
for k, v := range t.Labels {
if event.Labels[k] != v {
return false
Expand Down
4 changes: 2 additions & 2 deletions server/service/mongo/kv/kv_service.go
Expand Up @@ -20,10 +20,10 @@ package kv
import (
"context"
"errors"
"reflect"
"time"

"github.com/apache/servicecomb-kie/pkg/model"
"github.com/apache/servicecomb-kie/pkg/util"
"github.com/apache/servicecomb-kie/server/service"
"github.com/apache/servicecomb-kie/server/service/mongo/label"
"github.com/apache/servicecomb-kie/server/service/mongo/session"
Expand Down Expand Up @@ -180,7 +180,7 @@ func (s *Service) List(ctx context.Context, domain, project string, options ...s
return nil, err
}
if opts.ExactLabels {
if !reflect.DeepEqual(opts.Labels, curKV.Labels) {
if !util.IsEquivalentLabel(opts.Labels, curKV.Labels) {
continue
}
}
Expand Down

0 comments on commit cea35a3

Please sign in to comment.