Skip to content

Commit

Permalink
fix config_center does not work for new options api (#2616)
Browse files Browse the repository at this point in the history
fixes #2614
  • Loading branch information
FinalT authored Mar 10, 2024
1 parent c75b90e commit 333cba2
Show file tree
Hide file tree
Showing 2 changed files with 388 additions and 0 deletions.
387 changes: 387 additions & 0 deletions compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,390 @@ func compatMetricPrometheusGateway(g *global.PushgatewayConfig) *config.Pushgate
PushInterval: g.PushInterval,
}
}

func compatInstanceOptions(cr *config.RootConfig, rc *InstanceOptions) {
if cr == nil {
return
}
proCompat := make(map[string]*global.ProtocolConfig)
for k, v := range cr.Protocols {
proCompat[k] = compatGlobalProtocolConfig(v)
}

regCompat := make(map[string]*global.RegistryConfig)
for k, v := range cr.Registries {
regCompat[k] = compatGlobalRegistryConfig(v)
}

rc.Application = compatGlobalApplicationConfig(cr.Application)
rc.Protocols = proCompat
rc.Registries = regCompat
rc.ConfigCenter = compatGlobalCenterConfig(cr.ConfigCenter)
rc.MetadataReport = compatGlobalMetadataReportConfig(cr.MetadataReport)
rc.Provider = compatGlobalProviderConfig(cr.Provider)
rc.Consumer = compatGlobalConsumerConfig(cr.Consumer)
rc.Metrics = compatGlobalMetricConfig(cr.Metrics)
rc.Otel = compatGlobalOtelConfig(cr.Otel)
rc.Logger = compatGlobalLoggerConfig(cr.Logger)
rc.Shutdown = compatGlobalShutdownConfig(cr.Shutdown)
rc.EventDispatcherType = cr.EventDispatcherType
rc.CacheFile = cr.CacheFile
rc.Custom = compatGlobalCustomConfig(cr.Custom)
rc.Profiles = compatGlobalProfilesConfig(cr.Profiles)
rc.TLSConfig = compatGlobalTLSConfig(cr.TLSConfig)
}

func compatGlobalProtocolConfig(c *config.ProtocolConfig) *global.ProtocolConfig {
if c == nil {
return nil
}
return &global.ProtocolConfig{
Name: c.Name,
Ip: c.Ip,
Port: c.Port,
Params: c.Params,
MaxServerSendMsgSize: c.MaxServerSendMsgSize,
MaxServerRecvMsgSize: c.MaxServerRecvMsgSize,
}
}

func compatGlobalRegistryConfig(c *config.RegistryConfig) *global.RegistryConfig {
if c == nil {
return nil
}
return &global.RegistryConfig{
Protocol: c.Protocol,
Timeout: c.Timeout,
Group: c.Group,
Namespace: c.Namespace,
TTL: c.TTL,
Address: c.Address,
Username: c.Username,
Password: c.Password,
Simplified: c.Simplified,
Preferred: c.Preferred,
Zone: c.Zone,
Weight: c.Weight,
Params: c.Params,
RegistryType: c.RegistryType,
UseAsMetaReport: c.UseAsMetaReport,
UseAsConfigCenter: c.UseAsConfigCenter,
}
}

func compatGlobalApplicationConfig(c *config.ApplicationConfig) *global.ApplicationConfig {
if c == nil {
return nil
}
return &global.ApplicationConfig{
Organization: c.Organization,
Name: c.Name,
Module: c.Module,
Group: c.Group,
Version: c.Version,
Owner: c.Owner,
Environment: c.Environment,
MetadataType: c.MetadataType,
Tag: c.Tag,
}
}

func compatGlobalCenterConfig(c *config.CenterConfig) *global.CenterConfig {
if c == nil {
return nil
}
return &global.CenterConfig{
Protocol: c.Protocol,
Address: c.Address,
DataId: c.DataId,
Cluster: c.Cluster,
Group: c.Group,
Username: c.Username,
Password: c.Password,
Namespace: c.Namespace,
AppID: c.AppID,
Timeout: c.Timeout,
Params: c.Params,
FileExtension: c.FileExtension,
}
}

func compatGlobalMetadataReportConfig(c *config.MetadataReportConfig) *global.MetadataReportConfig {
if c == nil {
return nil
}
return &global.MetadataReportConfig{
Protocol: c.Protocol,
Address: c.Address,
Username: c.Username,
Password: c.Password,
Timeout: c.Timeout,
Group: c.Group,
Namespace: c.Namespace,
Params: c.Params,
}
}

func compatGlobalProviderConfig(c *config.ProviderConfig) *global.ProviderConfig {
if c == nil {
return nil
}
services := make(map[string]*global.ServiceConfig)
for key, svc := range c.Services {
services[key] = compatGlobalServiceConfig(svc)
}
return &global.ProviderConfig{
Filter: c.Filter,
Register: c.Register,
RegistryIDs: c.RegistryIDs,
ProtocolIDs: c.ProtocolIDs,
TracingKey: c.TracingKey,
Services: services,
ProxyFactory: c.ProxyFactory,
FilterConf: c.FilterConf,
ConfigType: c.ConfigType,
AdaptiveService: c.AdaptiveService,
AdaptiveServiceVerbose: c.AdaptiveServiceVerbose,
}
}

func compatGlobalServiceConfig(c *config.ServiceConfig) *global.ServiceConfig {
if c == nil {
return nil
}
methods := make([]*global.MethodConfig, len(c.Methods))
for i, method := range c.Methods {
methods[i] = compatGlobalMethodConfig(method)
}
protocols := make(map[string]*global.ProtocolConfig)
for key, pro := range c.RCProtocolsMap {
protocols[key] = compatGlobalProtocolConfig(pro)
}
registries := make(map[string]*global.RegistryConfig)
for key, reg := range c.RCRegistriesMap {
registries[key] = compatGlobalRegistryConfig(reg)
}
return &global.ServiceConfig{
Filter: c.Filter,
ProtocolIDs: c.ProtocolIDs,
Interface: c.Interface,
RegistryIDs: c.RegistryIDs,
Cluster: c.Cluster,
Loadbalance: c.Loadbalance,
Group: c.Group,
Version: c.Version,
Methods: methods,
Warmup: c.Warmup,
Retries: c.Retries,
Serialization: c.Serialization,
Params: c.Params,
Token: c.Token,
AccessLog: c.AccessLog,
TpsLimiter: c.TpsLimiter,
TpsLimitInterval: c.TpsLimitInterval,
TpsLimitRate: c.TpsLimitRate,
TpsLimitStrategy: c.TpsLimitStrategy,
TpsLimitRejectedHandler: c.TpsLimitRejectedHandler,
ExecuteLimit: c.ExecuteLimit,
ExecuteLimitRejectedHandler: c.ExecuteLimitRejectedHandler,
Auth: c.Auth,
NotRegister: c.NotRegister,
ParamSign: c.ParamSign,
Tag: c.Tag,
TracingKey: c.TracingKey,
RCProtocolsMap: protocols,
RCRegistriesMap: registries,
ProxyFactoryKey: c.ProxyFactoryKey,
}
}

func compatGlobalMethodConfig(c *config.MethodConfig) *global.MethodConfig {
if c == nil {
return nil
}
return &global.MethodConfig{
InterfaceId: c.InterfaceId,
InterfaceName: c.InterfaceName,
Name: c.Name,
Retries: c.Retries,
LoadBalance: c.LoadBalance,
Weight: c.Weight,
TpsLimitInterval: c.TpsLimitInterval,
TpsLimitRate: c.TpsLimitRate,
TpsLimitStrategy: c.TpsLimitStrategy,
ExecuteLimit: c.ExecuteLimit,
ExecuteLimitRejectedHandler: c.ExecuteLimitRejectedHandler,
Sticky: c.Sticky,
RequestTimeout: c.RequestTimeout,
}
}

func compatGlobalConsumerConfig(c *config.ConsumerConfig) *global.ConsumerConfig {
if c == nil {
return nil
}
return &global.ConsumerConfig{
Filter: c.Filter,
RegistryIDs: c.RegistryIDs,
Protocol: c.Protocol,
RequestTimeout: c.RequestTimeout,
ProxyFactory: c.ProxyFactory,
Check: c.Check,
AdaptiveService: c.AdaptiveService,
TracingKey: c.TracingKey,
FilterConf: c.FilterConf,
MaxWaitTimeForServiceDiscovery: c.MaxWaitTimeForServiceDiscovery,
MeshEnabled: c.MeshEnabled,
}
}

func compatGlobalMetricConfig(c *config.MetricsConfig) *global.MetricsConfig {
if c == nil {
return nil
}
return &global.MetricsConfig{
Enable: c.Enable,
Port: c.Port,
Path: c.Path,
Prometheus: compatGlobalMetricPrometheusConfig(c.Prometheus),
Aggregation: compatGlobalMetricAggregationConfig(c.Aggregation),
Protocol: c.Protocol,
EnableMetadata: c.EnableMetadata,
EnableRegistry: c.EnableRegistry,
EnableConfigCenter: c.EnableConfigCenter,
}
}

func compatGlobalMetricPrometheusConfig(c *config.PrometheusConfig) *global.PrometheusConfig {
if c == nil {
return nil
}
return &global.PrometheusConfig{
Exporter: compatGlobalMetricPrometheusExporter(c.Exporter),
Pushgateway: compatGlobalMetricPrometheusGateway(c.Pushgateway),
}
}

func compatGlobalMetricAggregationConfig(a *config.AggregateConfig) *global.AggregateConfig {
if a == nil {
return nil
}
return &global.AggregateConfig{
Enabled: a.Enabled,
BucketNum: a.BucketNum,
TimeWindowSeconds: a.TimeWindowSeconds,
}
}

func compatGlobalMetricPrometheusExporter(e *config.Exporter) *global.Exporter {
if e == nil {
return nil
}
return &global.Exporter{
Enabled: e.Enabled,
}
}

func compatGlobalMetricPrometheusGateway(g *config.PushgatewayConfig) *global.PushgatewayConfig {
if g == nil {
return nil
}
return &global.PushgatewayConfig{
Enabled: g.Enabled,
BaseUrl: g.BaseUrl,
Job: g.Job,
Username: g.Username,
Password: g.Password,
PushInterval: g.PushInterval,
}
}

func compatGlobalOtelConfig(c *config.OtelConfig) *global.OtelConfig {
if c == nil {
return nil
}
return &global.OtelConfig{
TracingConfig: &global.OtelTraceConfig{
Enable: c.TraceConfig.Enable,
Exporter: c.TraceConfig.Exporter,
Endpoint: c.TraceConfig.Endpoint,
Propagator: c.TraceConfig.Propagator,
SampleMode: c.TraceConfig.SampleMode,
SampleRatio: c.TraceConfig.SampleRatio,
},
}
}

func compatGlobalLoggerConfig(c *config.LoggerConfig) *global.LoggerConfig {
if c == nil {
return nil
}
return &global.LoggerConfig{
Driver: c.Driver,
Level: c.Level,
Format: c.Format,
Appender: c.Appender,
File: compatGlobalFile(c.File),
}
}

func compatGlobalFile(c *config.File) *global.File {
if c == nil {
return nil
}
return &global.File{
Name: c.Name,
MaxSize: c.MaxSize,
MaxBackups: c.MaxBackups,
MaxAge: c.MaxAge,
Compress: c.Compress,
}
}

func compatGlobalShutdownConfig(c *config.ShutdownConfig) *global.ShutdownConfig {
if c == nil {
return nil
}
cfg := &global.ShutdownConfig{
Timeout: c.Timeout,
StepTimeout: c.StepTimeout,
ConsumerUpdateWaitTime: c.ConsumerUpdateWaitTime,
RejectRequestHandler: c.RejectRequestHandler,
InternalSignal: c.InternalSignal,
OfflineRequestWindowTimeout: c.OfflineRequestWindowTimeout,
RejectRequest: atomic.Bool{},
}
cfg.RejectRequest.Store(c.RejectRequest.Load())

return cfg
}

func compatGlobalCustomConfig(c *config.CustomConfig) *global.CustomConfig {
if c == nil {
return nil
}
return &global.CustomConfig{
ConfigMap: c.ConfigMap,
}
}

func compatGlobalProfilesConfig(c *config.ProfilesConfig) *global.ProfilesConfig {
if c == nil {
return nil
}
return &global.ProfilesConfig{
Active: c.Active,
}
}

func compatGlobalTLSConfig(c *config.TLSConfig) *global.TLSConfig {
if c == nil {
return nil
}
return &global.TLSConfig{
CACertFile: c.CACertFile,
TLSCertFile: c.TLSCertFile,
TLSKeyFile: c.TLSKeyFile,
TLSServerName: c.TLSServerName,
}
}
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (rc *InstanceOptions) init(opts ...InstanceOption) error {
log.Infof("[Config Center] Config center doesn't start")
log.Debugf("config center doesn't start because %s", err)
} else {
compatInstanceOptions(rcCompat, rc)
if err = rcCompat.Logger.Init(); err != nil { // init logger using config from config center again
return err
}
Expand Down

0 comments on commit 333cba2

Please sign in to comment.