Skip to content

Commit

Permalink
fix lint problems and readWrite lock optimization (#1455)
Browse files Browse the repository at this point in the history
* fix lint problems (#1451)

Co-authored-by: l00618052 <lishitong1@huawei.com>

* readWrite lock optimization

* add local storage test

---------

Co-authored-by: l00618052 <lishitong1@huawei.com>
  • Loading branch information
Dantlian and l00618052 committed Jan 17, 2024
1 parent 803802a commit 58dd5cd
Show file tree
Hide file tree
Showing 60 changed files with 218 additions and 156 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/static_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ jobs:
sudo docker-compose -f ./scripts/docker-compose.yaml up -d
sleep 20
bash -x scripts/ut_test_in_docker.sh mongo
local:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.18
uses: actions/setup-go@v1
with:
go-version: 1.18
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: UT-LOCAL_STORAGE
run: |
rm -rf /data/schemas
bash -x scripts/ut_test_in_docker.sh local
integration-test:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Client struct {
Cfg Config
}

func (c *Client) CommonHeaders(ctx context.Context) http.Header {
func (c *Client) CommonHeaders(_ context.Context) http.Header {
var headers = make(http.Header)
// TODO overwrote by context values
if len(c.Cfg.Token) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/apache/servicecomb-service-center/pkg/util"
)

func (c *LBClient) WebsocketDial(ctx context.Context, api string, headers http.Header) (conn *websocket.Conn, err error) {
func (c *LBClient) WebsocketDial(_ context.Context, api string, headers http.Header) (conn *websocket.Conn, err error) {
dialer := &websocket.Dialer{TLSClientConfig: c.TLS}
var errs []string
for i := 0; i < c.Retries; i++ {
Expand Down
4 changes: 2 additions & 2 deletions datasource/dependency_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func toString(in *discovery.MicroServiceKey) string {
return path.GenerateProviderDependencyRuleKey(in.Tenant, in)
}

func ParseAddOrUpdateRules(ctx context.Context, dep *Dependency, oldProviderRules *discovery.MicroServiceDependency) {
func ParseAddOrUpdateRules(_ context.Context, dep *Dependency, oldProviderRules *discovery.MicroServiceDependency) {
deleteDependencyRuleList := make([]*discovery.MicroServiceKey, 0, len(oldProviderRules.Dependency))
createDependencyRuleList := make([]*discovery.MicroServiceKey, 0, len(dep.ProvidersRule))
existDependencyRuleList := make([]*discovery.MicroServiceKey, 0, len(oldProviderRules.Dependency))
Expand All @@ -83,7 +83,7 @@ func ParseAddOrUpdateRules(ctx context.Context, dep *Dependency, oldProviderRule
setDep(dep, createDependencyRuleList, existDependencyRuleList, deleteDependencyRuleList)
}

func ParseOverrideRules(ctx context.Context, dep *Dependency, oldProviderRules *discovery.MicroServiceDependency) {
func ParseOverrideRules(_ context.Context, dep *Dependency, oldProviderRules *discovery.MicroServiceDependency) {
deleteDependencyRuleList := make([]*discovery.MicroServiceKey, 0, len(oldProviderRules.Dependency))
createDependencyRuleList := make([]*discovery.MicroServiceKey, 0, len(dep.ProvidersRule))
existDependencyRuleList := make([]*discovery.MicroServiceKey, 0, len(oldProviderRules.Dependency))
Expand Down
4 changes: 2 additions & 2 deletions datasource/etcd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func init() {
rbac.Install("embedded_etcd", NewRbacDAO)
}

func NewRbacDAO(opts rbac.Options) (rbac.DAO, error) {
func NewRbacDAO(_ rbac.Options) (rbac.DAO, error) {
return &RbacDAO{}, nil
}

Expand Down Expand Up @@ -187,7 +187,7 @@ func (ds *RbacDAO) DeleteAccount(ctx context.Context, names []string) (bool, err
return true, nil
}

func (ds *RbacDAO) UpdateAccount(ctx context.Context, name string, account *crbac.Account) error {
func (ds *RbacDAO) UpdateAccount(ctx context.Context, _ string, account *crbac.Account) error {
var (
opts []etcdadpt.OpOptions
err error
Expand Down
2 changes: 1 addition & 1 deletion datasource/etcd/cache/filter_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (f *ConsumerFilter) Name(ctx context.Context, _ *cache.Node) string {
return ctx.Value(CtxConsumerID).(string)
}

func (f *ConsumerFilter) Init(ctx context.Context, parent *cache.Node) (node *cache.Node, err error) {
func (f *ConsumerFilter) Init(_ context.Context, _ *cache.Node) (node *cache.Node, err error) {
node = cache.NewNode()
node.Cache.Set(DepResult, &DependencyRuleItem{})
return
Expand Down
2 changes: 1 addition & 1 deletion datasource/etcd/cache/filter_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (f *ServiceFilter) Name(ctx context.Context, _ *cache.Node) string {
provider.ServiceName}, "/")
}

func (f *ServiceFilter) Init(ctx context.Context, parent *cache.Node) (node *cache.Node, err error) {
func (f *ServiceFilter) Init(_ context.Context, _ *cache.Node) (node *cache.Node, err error) {
node = cache.NewNode()
return
}
2 changes: 1 addition & 1 deletion datasource/etcd/cache/filter_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (f *VersionFilter) Name(ctx context.Context, _ *cache.Node) string {
return ""
}

func (f *VersionFilter) Init(ctx context.Context, parent *cache.Node) (node *cache.Node, err error) {
func (f *VersionFilter) Init(ctx context.Context, _ *cache.Node) (node *cache.Node, err error) {
instance, ok := ctx.Value(CtxProviderInstanceKey).(*pb.HeartbeatSetElement)
if ok {
node = cache.NewNode()
Expand Down
6 changes: 3 additions & 3 deletions datasource/etcd/cache/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func (f *FindInstancesCache) GetWithProviderID(ctx context.Context, consumer *pb
func (f *FindInstancesCache) Remove(provider *pb.MicroServiceKey) {
f.Tree.Remove(context.WithValue(context.Background(), CtxProviderKey, provider))
if len(provider.Alias) > 0 {
copy := *provider
copy.ServiceName = copy.Alias
f.Tree.Remove(context.WithValue(context.Background(), CtxProviderKey, &copy))
copyProvider := *provider
copyProvider.ServiceName = copyProvider.Alias
f.Tree.Remove(context.WithValue(context.Background(), CtxProviderKey, &copyProvider))
}
}
31 changes: 17 additions & 14 deletions datasource/etcd/ms.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,11 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/apache/servicecomb-service-center/datasource/local"
"os"
"path/filepath"
"strconv"
"time"

"github.com/apache/servicecomb-service-center/syncer/service/event"
pb "github.com/go-chassis/cari/discovery"
"github.com/go-chassis/cari/pkg/errsvc"
"github.com/go-chassis/cari/sync"
"github.com/go-chassis/foundation/gopool"
"github.com/little-cui/etcdadpt"

"github.com/apache/servicecomb-service-center/datasource"
"github.com/apache/servicecomb-service-center/datasource/etcd/cache"
"github.com/apache/servicecomb-service-center/datasource/etcd/path"
Expand All @@ -42,12 +35,19 @@ import (
esync "github.com/apache/servicecomb-service-center/datasource/etcd/sync"
eutil "github.com/apache/servicecomb-service-center/datasource/etcd/util"
serviceUtil "github.com/apache/servicecomb-service-center/datasource/etcd/util"
"github.com/apache/servicecomb-service-center/datasource/local"
"github.com/apache/servicecomb-service-center/datasource/schema"
"github.com/apache/servicecomb-service-center/pkg/log"
"github.com/apache/servicecomb-service-center/pkg/util"
"github.com/apache/servicecomb-service-center/server/core"
"github.com/apache/servicecomb-service-center/server/plugin/uuid"
quotasvc "github.com/apache/servicecomb-service-center/server/service/quota"
"github.com/apache/servicecomb-service-center/syncer/service/event"
pb "github.com/go-chassis/cari/discovery"
"github.com/go-chassis/cari/pkg/errsvc"
"github.com/go-chassis/cari/sync"
"github.com/go-chassis/foundation/gopool"
"github.com/little-cui/etcdadpt"
)

type MetadataManager struct {
Expand Down Expand Up @@ -188,7 +188,7 @@ func (ds *MetadataManager) RegisterService(ctx context.Context, request *pb.Crea
}, nil
}

func (ds *MetadataManager) ListService(ctx context.Context, request *pb.GetServicesRequest) (
func (ds *MetadataManager) ListService(ctx context.Context, _ *pb.GetServicesRequest) (
*pb.GetServicesResponse, error) {
services, err := eutil.GetAllServiceUtil(ctx)
if err != nil {
Expand Down Expand Up @@ -217,7 +217,7 @@ func (ds *MetadataManager) GetService(ctx context.Context, request *pb.GetServic
return singleService, nil
}

func (ds *MetadataManager) GetOverview(ctx context.Context, request *pb.GetServicesRequest) (
func (ds *MetadataManager) GetOverview(ctx context.Context, _ *pb.GetServicesRequest) (
*pb.Statistics, error) {
ctx = util.WithCacheOnly(ctx)
st, err := statistics(ctx, false)
Expand Down Expand Up @@ -879,10 +879,9 @@ func (ds *MetadataManager) SendManyHeartbeat(ctx context.Context, request *pb.He
log.Warn(fmt.Sprintf("instance[%s/%s] is duplicate request heartbeat set",
heartbeatElement.ServiceId, heartbeatElement.InstanceId))
continue
} else {
existFlag[heartbeatElement.ServiceId+heartbeatElement.InstanceId] = true
noMultiCounter++
}
existFlag[heartbeatElement.ServiceId+heartbeatElement.InstanceId] = true
noMultiCounter++
gopool.Go(getHeartbeatFunc(ctx, domainProject, instancesHbRst, heartbeatElement))
}
count := 0
Expand Down Expand Up @@ -944,7 +943,7 @@ func (ds *MetadataManager) SendHeartbeat(ctx context.Context, request *pb.Heartb
return nil
}

func (ds *MetadataManager) ListManyInstances(ctx context.Context, request *pb.GetAllInstancesRequest) (*pb.GetAllInstancesResponse, error) {
func (ds *MetadataManager) ListManyInstances(ctx context.Context, _ *pb.GetAllInstancesRequest) (*pb.GetAllInstancesResponse, error) {
domainProject := util.ParseDomainProject(ctx)
key := path.GetInstanceRootKey(domainProject) + path.SPLIT
opts := append(eutil.FromContext(ctx), etcdadpt.WithStrKey(key), etcdadpt.WithPrefix())
Expand Down Expand Up @@ -1506,6 +1505,10 @@ func (ds *MetadataManager) UnregisterService(ctx context.Context, request *pb.De
if rollbackErr != nil {
log.Error("clean tmp dir error when rollback in UnregisterService", err)
}
rollbackErr = os.Remove(originPath)
if rollbackErr != nil {
log.Error("clean origin dir error when rollback in UnregisterService", err)
}
}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion datasource/etcd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func init() {
schema.Install("embedded_etcd", NewSchemaDAO)
}

func NewSchemaDAO(opts schema.Options) (schema.DAO, error) {
func NewSchemaDAO(_ schema.Options) (schema.DAO, error) {
return &SchemaDAO{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion datasource/etcd/sd/aggregate/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func (r *Repository) New(t kvstore.Type, cfg *kvstore.Options) state.State {
return NewAggregator(t, cfg)
}

func NewRepository(opts state.Config) state.Repository {
func NewRepository(_ state.Config) state.Repository {
return &Repository{}
}
2 changes: 1 addition & 1 deletion datasource/etcd/sd/k8s/adaptor/listwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type k8sListWatcher struct {
cb OnEventFunc
}

func (w *k8sListWatcher) Handle(ctx context.Context, obj interface{}) {
func (w *k8sListWatcher) Handle(_ context.Context, obj interface{}) {
if w.cb == nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion datasource/etcd/sd/k8s/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ func (r *Repository) New(t kvstore.Type, cfg *kvstore.Options) state.State {
return adaptor.NewK8sAdaptor(t, cfg)
}

func NewRepository(opts state.Config) state.Repository {
func NewRepository(_ state.Config) state.Repository {
return &Repository{}
}
2 changes: 1 addition & 1 deletion datasource/etcd/sd/servicecenter/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func (r *Repository) New(t kvstore.Type, cfg *kvstore.Options) state.State {
return NewServiceCenterAdaptor(t, cfg)
}

func NewRepository(opts state.Config) state.Repository {
func NewRepository(_ state.Config) state.Repository {
return &Repository{}
}
2 changes: 1 addition & 1 deletion datasource/etcd/sd/servicecenter/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (c *Syncer) checkWithConflictHandleFunc(local *Cacher, remote dump.Getter,
}
}

func (c *Syncer) skipHandleFunc(origin *dump.KV, conflict dump.Getter, index int) {
func (c *Syncer) skipHandleFunc(_ *dump.KV, _ dump.Getter, _ int) {
}

func (c *Syncer) logConflictFunc(origin *dump.KV, conflict dump.Getter, index int) {
Expand Down
2 changes: 1 addition & 1 deletion datasource/etcd/state/etcd/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func (r *Repository) New(t kvstore.Type, cfg *kvstore.Options) state.State {
return NewEtcdState(t.String(), cfg)
}

func NewRepository(opts state.Config) state.Repository {
func NewRepository(_ state.Config) state.Repository {
return &Repository{}
}
22 changes: 11 additions & 11 deletions datasource/etcd/state/kvstore/cache_null.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ var (
type nullCache struct {
}

func (n *nullCache) Name() string { return "NULL" }
func (n *nullCache) Size() int { return 0 }
func (n *nullCache) Get(k string) *KeyValue { return nil }
func (n *nullCache) GetAll(arr *[]*KeyValue) int { return 0 }
func (n *nullCache) GetPrefix(prefix string, arr *[]*KeyValue) int { return 0 }
func (n *nullCache) ForEach(iter func(k string, v *KeyValue) (next bool)) {}
func (n *nullCache) Put(k string, v *KeyValue) {}
func (n *nullCache) Remove(k string) {}
func (n *nullCache) MarkDirty() {}
func (n *nullCache) Dirty() bool { return false }
func (n *nullCache) Clear() {}
func (n *nullCache) Name() string { return "NULL" }
func (n *nullCache) Size() int { return 0 }
func (n *nullCache) Get(_ string) *KeyValue { return nil }
func (n *nullCache) GetAll(_ *[]*KeyValue) int { return 0 }
func (n *nullCache) GetPrefix(_ string, _ *[]*KeyValue) int { return 0 }
func (n *nullCache) ForEach(_ func(k string, v *KeyValue) (next bool)) {}
func (n *nullCache) Put(_ string, _ *KeyValue) {}
func (n *nullCache) Remove(_ string) {}
func (n *nullCache) MarkDirty() {}
func (n *nullCache) Dirty() bool { return false }
func (n *nullCache) Clear() {}

type nullCacher struct {
}
Expand Down
2 changes: 1 addition & 1 deletion datasource/etcd/state/kvstore/indexer_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type CacheIndexer struct {
Cache CacheReader
}

func (i *CacheIndexer) Search(ctx context.Context, opts ...etcdadpt.OpOption) (resp *Response, _ error) {
func (i *CacheIndexer) Search(_ context.Context, opts ...etcdadpt.OpOption) (resp *Response, _ error) {
op := etcdadpt.OpGet(opts...)
if op.Prefix {
resp = i.searchByPrefix(op)
Expand Down
9 changes: 4 additions & 5 deletions datasource/etcd/util/dependency_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ func (dr *DependencyRelation) GetDependencyConsumers(opts ...DependencyRelationF
log.Warn(fmt.Sprintf("consumer[%s/%s/%s/%s] does not exist",
consumer.Environment, consumer.AppId, consumer.ServiceName, consumer.Version))
continue
} else {
return nil, err
}
return nil, err
}

if op.NonSelf && service.ServiceId == dr.provider.ServiceId {
Expand Down Expand Up @@ -234,9 +233,9 @@ func (dr *DependencyRelation) GetDependencyConsumersOfProvider() ([]*pb.MicroSer
}

func (dr *DependencyRelation) GetConsumerOfSameServiceNameAndAppID(provider *pb.MicroServiceKey) ([]*pb.MicroServiceKey, error) {
copy := *provider
copy.Version = ""
prefix := path.GenerateProviderDependencyRuleKey(dr.domainProject, &copy)
copyProvider := *provider
copyProvider.Version = ""
prefix := path.GenerateProviderDependencyRuleKey(dr.domainProject, &copyProvider)

opts := append(FromContext(dr.ctx),
etcdadpt.WithStrKey(prefix),
Expand Down
37 changes: 33 additions & 4 deletions datasource/etcd/util/microservice_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"

pb "github.com/go-chassis/cari/discovery"
Expand All @@ -31,6 +32,8 @@ import (
"github.com/apache/servicecomb-service-center/datasource/etcd/sd"
"github.com/apache/servicecomb-service-center/datasource/etcd/state/kvstore"
"github.com/apache/servicecomb-service-center/datasource/etcd/sync"
"github.com/apache/servicecomb-service-center/datasource/local"
"github.com/apache/servicecomb-service-center/datasource/schema"
"github.com/apache/servicecomb-service-center/pkg/log"
"github.com/apache/servicecomb-service-center/pkg/util"
"github.com/apache/servicecomb-service-center/server/config"
Expand Down Expand Up @@ -163,17 +166,17 @@ func searchServiceIDFromAlias(ctx context.Context, key *pb.MicroServiceKey) (str
}

func GetServiceAllVersions(ctx context.Context, key *pb.MicroServiceKey, alias bool) (*kvstore.Response, error) {
copy := *key
copy.Version = ""
copyKey := *key
copyKey.Version = ""
var (
prefix string
indexer kvstore.Indexer
)
if alias {
prefix = path.GenerateServiceAliasKey(&copy)
prefix = path.GenerateServiceAliasKey(&copyKey)
indexer = sd.ServiceAlias()
} else {
prefix = path.GenerateServiceIndexKey(&copy)
prefix = path.GenerateServiceIndexKey(&copyKey)
indexer = sd.ServiceIndex()
}
opts := append(FromContext(ctx),
Expand Down Expand Up @@ -247,6 +250,32 @@ func UpdateService(ctx context.Context, domainProject string, serviceID string,
log.Error("marshal service file failed", err)
return opts, err
}

if schema.StorageType == "local" {
contents := make([]*schema.ContentItem, len(service.Schemas))
err = schema.Instance().PutManyContent(ctx, &schema.PutManyContentRequest{
ServiceID: service.ServiceId,
SchemaIDs: service.Schemas,
Contents: contents,
Init: true,
})
if err != nil {
return nil, err
}

serviceMutex := local.GetOrCreateMutex(service.ServiceId)
serviceMutex.Lock()
defer serviceMutex.Unlock()
}
defer func() {
if schema.StorageType == "local" && err != nil {
cleanDirErr := local.CleanDir(filepath.Join(schema.RootFilePath, domainProject, service.ServiceId))
if cleanDirErr != nil {
log.Error("clean dir error when rollback in RegisterService", cleanDirErr)
}
}
}()

opt := etcdadpt.OpPut(etcdadpt.WithStrKey(key), etcdadpt.WithValue(data))
opts = append(opts, opt)
syncOpts, err := sync.GenUpdateOpts(ctx, datasource.ResourceKV, data, sync.WithOpts(map[string]string{"key": key}))
Expand Down
Loading

0 comments on commit 58dd5cd

Please sign in to comment.