Skip to content

Commit

Permalink
readWrite lock optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
l00618052 committed Jan 15, 2024
1 parent 803802a commit 218e28e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 24 deletions.
23 changes: 22 additions & 1 deletion .github/workflows/eventbase-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,25 @@ jobs:
export TEST_DB_MODE=etcd
export TEST_DB_URI=http://127.0.0.1:2379
cd eventbase
time go test -short -covermode=atomic $(go list ./... | grep -v mongo | grep -v third_party | grep -v examples)
time go test -short -covermode=atomic $(go list ./... | grep -v mongo | grep -v third_party | grep -v examples)
etcd-storage-with-localstorage:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.18
id: go
- name: Check out source code
uses: actions/checkout@v1
- name: UT for etcd
run: |
time docker run -d -p 2379:2379 --name etcd quay.io/coreos/etcd etcd -name etcd --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379
while ! nc -z 127.0.0.1 2379; do
sleep 1
done
export TEST_DB_MODE=etcd
export TEST_DB_URI=http://127.0.0.1:2379
export SCHEMA_ROOT_PATH=/data/kvs
cd eventbase
sudo time go test -short -covermode=atomic $(go list ./... | grep -v mongo | grep -v third_party | grep -v examples)
20 changes: 12 additions & 8 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 @@ -1506,6 +1506,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
25 changes: 13 additions & 12 deletions datasource/local/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (
"context"
"encoding/json"
"fmt"
"io/fs"
"os"
pathutil "path"
"strings"
"sync"

"github.com/apache/servicecomb-service-center/datasource"
"github.com/apache/servicecomb-service-center/datasource/etcd/path"
etcdsync "github.com/apache/servicecomb-service-center/datasource/etcd/sync"
Expand All @@ -30,33 +36,28 @@ import (
"github.com/go-chassis/cari/discovery"
"github.com/go-chassis/openlog"
"github.com/little-cui/etcdadpt"
"io/fs"
"os"
pathutil "path"

"path/filepath"
"strings"
"sync"
)

var MutexMap = make(map[string]*sync.Mutex)
var MutexMap = make(map[string]*sync.RWMutex)
var mutexMapLock = &sync.Mutex{}
var rollbackMutexLock = &sync.Mutex{}
var createDirMutexLock = &sync.Mutex{}

func init() {
schema.Install("local_with_embeded_etcd", NewSchemaDAO)
schema.Install("local_with_embedded_etcd", NewSchemaDAO)
schema.Install("local", NewSchemaDAO)
}

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

func GetOrCreateMutex(path string) *sync.Mutex {
func GetOrCreateMutex(path string) *sync.RWMutex {
mutexMapLock.Lock()
mutex, ok := MutexMap[path]
if !ok {
mutex = &sync.Mutex{}
mutex = &sync.RWMutex{}
MutexMap[path] = mutex
}
mutexMapLock.Unlock()
Expand Down Expand Up @@ -244,8 +245,8 @@ func CleanDir(dir string) error {

func ReadFile(filepath string) ([]byte, error) {
mutex := GetOrCreateMutex(filepath)
mutex.Lock()
defer mutex.Unlock()
mutex.RLock()
defer mutex.RUnlock()

// check the file is empty
content, err := os.ReadFile(filepath)
Expand Down
8 changes: 5 additions & 3 deletions datasource/schema/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package schema

import (
"fmt"
"strings"

"github.com/apache/servicecomb-service-center/pkg/log"
"github.com/apache/servicecomb-service-center/server/config"
"strings"
)

var StorageType = ""
Expand All @@ -46,8 +47,9 @@ func Init(opts Options) error {
return nil
}
kind := opts.Kind
if strings.Trim(config.GetRegistry().SchemaRootPath, " ") != "" {
kind = "local_with_embeded_etcd"
if strings.Trim(config.GetString("schema.root.path", "", config.WithStandby("schemaRootPath")), " ") != "" {
log.Warn("llllllllllocal")
kind = "local"
StorageType = "local"
RootFilePath = config.GetRegistry().SchemaRootPath
}
Expand Down

0 comments on commit 218e28e

Please sign in to comment.