Skip to content

Commit

Permalink
[fix] fix some environment problems (#1475)
Browse files Browse the repository at this point in the history
Co-authored-by: songshiyuan 00649746 <[email protected]>
  • Loading branch information
tornado-ssy and songshiyuan 00649746 committed Jun 6, 2024
1 parent b9e258c commit 59149cd
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
7 changes: 2 additions & 5 deletions datasource/etcd/ms.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
"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"
"github.com/apache/servicecomb-service-center/server/service/disco"
quotasvc "github.com/apache/servicecomb-service-center/server/service/quota"
"github.com/apache/servicecomb-service-center/syncer/service/event"
)
Expand Down Expand Up @@ -1724,10 +1723,11 @@ func (ds *MetadataManager) RegisterEnvironment(ctx context.Context, request *ev.
envIndex := path.GenerateEnvironmentIndexKey(envKey)
// 产生全局environment id
requestEnvID := env.ID
if len(requestEnvID) == 0 {
if len(requestEnvID) == 0 && len(env.Name) != 0 {
ctx = util.SetContext(ctx, uuid.ContextKey, envIndex)
env.ID = uuid.Generator().GetEnvID(ctx)
}
envIndex = util.StringJoin([]string{envIndex, env.ID}, "/")
data, err := json.Marshal(env)
if err != nil {
log.Error(fmt.Sprintf("create Environment[%s] failed, json marshal environment failed, operator: %s",
Expand Down Expand Up @@ -1765,7 +1765,6 @@ func (ds *MetadataManager) RegisterEnvironment(ctx context.Context, request *ev.
if resp.Succeeded {
log.Info(fmt.Sprintf("create environment[%s][%s] successfully, operator: %s",
env.ID, envFlag, remoteIP))
disco.EnvMap.Store(request.Environment.ID, struct{}{})
return &ev.CreateEnvironmentResponse{
EnvId: env.ID,
}, nil
Expand All @@ -1790,7 +1789,6 @@ func (ds *MetadataManager) RegisterEnvironment(ctx context.Context, request *ev.
existEnvironmentID := util.BytesToStringWithNoCopy(resp.Kvs[0].Value)
log.Warn(fmt.Sprintf("create environment[%s][%s] failed, environment already exists, operator: %s",
existEnvironmentID, envFlag, remoteIP))
disco.EnvMap.Store(request.Environment.ID, struct{}{})
return &ev.CreateEnvironmentResponse{
EnvId: existEnvironmentID,
}, nil
Expand Down Expand Up @@ -1916,6 +1914,5 @@ func (ds *MetadataManager) UnregisterEnvironment(ctx context.Context, request *e
quotasvc.RemandEnvironment(ctx)

log.Info(fmt.Sprintf("del environment[%s] successfully, operator: %s", environmentId, remoteIP))
disco.EnvMap.Delete(environment.ID)
return nil
}
7 changes: 7 additions & 0 deletions datasource/etcd/util/environment_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ func getEnvironmentsRawData(ctx context.Context, domainProject string) ([]*kvsto
if err != nil {
return nil, err
}
opts1 := append(FromContext(ctx),
etcdadpt.WithStrKey(key))
resp1, err := sd.Environment().Search(ctx, opts1...)
if err != nil {
return nil, err
}
resp.Kvs = append(resp.Kvs, resp1.Kvs[0])
return resp.Kvs, err
}

Expand Down
2 changes: 0 additions & 2 deletions server/service/disco/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
sync1 "sync"
"time"

pb "github.com/go-chassis/cari/discovery"
Expand All @@ -18,7 +17,6 @@ import (
)

var PreEnv util.CtxKey = "_pre_env"
var EnvMap sync1.Map

func ListEnvironments(ctx context.Context) (*ev.GetEnvironmentsResponse, error) {
return datasource.GetMetadataManager().ListEnvironments(ctx)
Expand Down
6 changes: 4 additions & 2 deletions server/service/disco/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/go-chassis/foundation/gopool"

"github.com/apache/servicecomb-service-center/datasource"
eutil "github.com/apache/servicecomb-service-center/datasource/etcd/util"
"github.com/apache/servicecomb-service-center/pkg/log"
"github.com/apache/servicecomb-service-center/pkg/util"
"github.com/apache/servicecomb-service-center/server/core"
Expand Down Expand Up @@ -62,8 +63,9 @@ func registerService(ctx context.Context, request *pb.CreateServiceRequest) (*pb
serviceFlag := util.StringJoin([]string{
service.Environment, service.AppId, service.ServiceName, service.Version}, "/")
if !core.IsSCInstance(ctx) {
_, ok := EnvMap.Load(service.Environment)
if !ok {
domainProject := util.ParseDomainProject(ctx)
_, err := eutil.GetEnvironment(ctx, domainProject, service.Environment)
if err != nil {
log.Error(fmt.Sprintf("create micro-service[%s] failed, operator: %s", serviceFlag, remoteIP), errors.New("env not exist"))
return nil, pb.NewError(pb.ErrInvalidParams, "env not exist")
}
Expand Down
2 changes: 1 addition & 1 deletion server/service/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func SelfEnvRegister(ctx context.Context) error {
func selfEnvRegister(pCtx context.Context) error {
ctx := addDefaultEnvContextValue(pCtx)
var req = new(env.CreateEnvironmentRequest)
preEnv := env.Environment{Name: ""}
preEnv := env.Environment{Name: "", ID: "", Description: "this is prepared env,whose id and name are empty"}
req.Environment = &preEnv
_, err := discosvc.RegistryEnvironment(ctx, req)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/service/validator/microservice_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var (

func MicroServiceKeyValidator() *validate.Validator {
return microServiceKeyValidator.Init(func(v *validate.Validator) {
v.AddRule("Environment", &validate.Rule{Min: 1, Max: 128, Regexp: envRegex})
v.AddRule("Environment", &validate.Rule{Min: 0, Max: 128, Regexp: envRegex})
v.AddRule("AppId", &validate.Rule{Min: 1, Max: 160, Regexp: nameRegex})
v.AddRule("ServiceName", &validate.Rule{Max: 128, Regexp: nameRegex})
v.AddRule("Version", &validate.Rule{Min: 1, Max: 64, Regexp: versionRegex})
Expand Down

0 comments on commit 59149cd

Please sign in to comment.