conf.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package conf
  15. import (
  16. "fmt"
  17. "github.com/lestrrat-go/file-rotatelogs"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/errorx"
  20. "github.com/sirupsen/logrus"
  21. "io"
  22. "os"
  23. "path"
  24. "time"
  25. )
  26. const ConfFileName = "kuiper.yaml"
  27. var (
  28. Config *KuiperConf
  29. IsTesting bool
  30. )
  31. type tlsConf struct {
  32. Certfile string `yaml:"certfile"`
  33. Keyfile string `yaml:"keyfile"`
  34. }
  35. type SinkConf struct {
  36. MemoryCacheThreshold int `json:"memoryCacheThreshold" yaml:"memoryCacheThreshold"`
  37. MaxDiskCache int `json:"maxDiskCache" yaml:"maxDiskCache"`
  38. BufferPageSize int `json:"bufferPageSize" yaml:"bufferPageSize"`
  39. EnableCache bool `json:"enableCache" yaml:"enableCache"`
  40. ResendInterval int `json:"resendInterval" yaml:"resendInterval"`
  41. CleanCacheAtStop bool `json:"cleanCacheAtStop" yaml:"cleanCacheAtStop"`
  42. }
  43. // Validate the configuration and reset to the default value for invalid values.
  44. func (sc SinkConf) Validate() error {
  45. e := make(errorx.MultiError)
  46. if sc.MemoryCacheThreshold < 0 {
  47. sc.MemoryCacheThreshold = 1024
  48. Log.Warnf("memoryCacheThreshold is less than 0, set to 1024")
  49. e["memoryCacheThreshold"] = fmt.Errorf("memoryCacheThreshold must be positive")
  50. }
  51. if sc.MaxDiskCache < 0 {
  52. sc.MaxDiskCache = 1024000
  53. Log.Warnf("maxDiskCache is less than 0, set to 1024000")
  54. e["maxDiskCache"] = fmt.Errorf("maxDiskCache must be positive")
  55. }
  56. if sc.BufferPageSize < 0 {
  57. sc.BufferPageSize = 256
  58. Log.Warnf("bufferPageSize is less than 0, set to 256")
  59. e["bufferPageSize"] = fmt.Errorf("bufferPageSize must be positive")
  60. }
  61. if sc.ResendInterval < 0 {
  62. sc.ResendInterval = 0
  63. Log.Warnf("resendInterval is less than 0, set to 0")
  64. e["resendInterval"] = fmt.Errorf("resendInterval must be positive")
  65. }
  66. if sc.BufferPageSize > sc.MemoryCacheThreshold {
  67. sc.MemoryCacheThreshold = sc.BufferPageSize
  68. Log.Warnf("memoryCacheThreshold is less than bufferPageSize, set to %d", sc.BufferPageSize)
  69. e["memoryCacheThresholdTooSmall"] = fmt.Errorf("memoryCacheThreshold must be greater than or equal to bufferPageSize")
  70. }
  71. if sc.MemoryCacheThreshold%sc.BufferPageSize != 0 {
  72. sc.MemoryCacheThreshold = sc.BufferPageSize * (sc.MemoryCacheThreshold/sc.BufferPageSize + 1)
  73. Log.Warnf("memoryCacheThreshold is not a multiple of bufferPageSize, set to %d", sc.MemoryCacheThreshold)
  74. e["memoryCacheThresholdNotMultiple"] = fmt.Errorf("memoryCacheThreshold must be a multiple of bufferPageSize")
  75. }
  76. if sc.BufferPageSize > sc.MaxDiskCache {
  77. sc.MaxDiskCache = sc.BufferPageSize
  78. Log.Warnf("maxDiskCache is less than bufferPageSize, set to %d", sc.BufferPageSize)
  79. e["maxDiskCacheTooSmall"] = fmt.Errorf("maxDiskCache must be greater than bufferPageSize")
  80. }
  81. if sc.MaxDiskCache%sc.BufferPageSize != 0 {
  82. sc.MaxDiskCache = sc.BufferPageSize * (sc.MaxDiskCache/sc.BufferPageSize + 1)
  83. Log.Warnf("maxDiskCache is not a multiple of bufferPageSize, set to %d", sc.MaxDiskCache)
  84. e["maxDiskCacheNotMultiple"] = fmt.Errorf("maxDiskCache must be a multiple of bufferPageSize")
  85. }
  86. return e.GetError()
  87. }
  88. type KuiperConf struct {
  89. Basic struct {
  90. Debug bool `yaml:"debug"`
  91. ConsoleLog bool `yaml:"consoleLog"`
  92. FileLog bool `yaml:"fileLog"`
  93. RotateTime int `yaml:"rotateTime"`
  94. MaxAge int `yaml:"maxAge"`
  95. Ip string `yaml:"ip"`
  96. Port int `yaml:"port"`
  97. RestIp string `yaml:"restIp"`
  98. RestPort int `yaml:"restPort"`
  99. RestTls *tlsConf `yaml:"restTls"`
  100. Prometheus bool `yaml:"prometheus"`
  101. PrometheusPort int `yaml:"prometheusPort"`
  102. PluginHosts string `yaml:"pluginHosts"`
  103. Authentication bool `yaml:"authentication"`
  104. IgnoreCase bool `yaml:"ignoreCase"`
  105. }
  106. Rule api.RuleOption
  107. Sink *SinkConf
  108. Store struct {
  109. Type string `yaml:"type"`
  110. Redis struct {
  111. Host string `yaml:"host"`
  112. Port int `yaml:"port"`
  113. Password string `yaml:"password"`
  114. Timeout int `yaml:"timeout"`
  115. ConnectionSelector string `yaml:"connectionSelector"`
  116. }
  117. Sqlite struct {
  118. Name string `yaml:"name"`
  119. }
  120. }
  121. Portable struct {
  122. PythonBin string `yaml:"pythonBin"`
  123. }
  124. }
  125. func InitConf() {
  126. cpath, err := GetConfLoc()
  127. if err != nil {
  128. panic(err)
  129. }
  130. kc := KuiperConf{
  131. Rule: api.RuleOption{
  132. LateTol: 1000,
  133. Concurrency: 1,
  134. BufferLength: 1024,
  135. CheckpointInterval: 300000, //5 minutes
  136. SendError: true,
  137. },
  138. }
  139. err = LoadConfigFromPath(path.Join(cpath, ConfFileName), &kc)
  140. if err != nil {
  141. Log.Fatal(err)
  142. panic(err)
  143. }
  144. Config = &kc
  145. if 0 == len(Config.Basic.Ip) {
  146. Config.Basic.Ip = "0.0.0.0"
  147. }
  148. if 0 == len(Config.Basic.RestIp) {
  149. Config.Basic.RestIp = "0.0.0.0"
  150. }
  151. if Config.Basic.Debug {
  152. Log.SetLevel(logrus.DebugLevel)
  153. }
  154. if Config.Basic.FileLog {
  155. logDir, err := GetLoc(logDir)
  156. if err != nil {
  157. Log.Fatal(err)
  158. }
  159. file := path.Join(logDir, logFileName)
  160. logWriter, err := rotatelogs.New(
  161. file+".%Y-%m-%d_%H-%M-%S",
  162. rotatelogs.WithLinkName(file),
  163. rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)),
  164. rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)),
  165. )
  166. if err != nil {
  167. fmt.Println("Failed to init log file settings..." + err.Error())
  168. Log.Infof("Failed to log to file, using default stderr.")
  169. } else if Config.Basic.ConsoleLog {
  170. mw := io.MultiWriter(os.Stdout, logWriter)
  171. Log.SetOutput(mw)
  172. } else if !Config.Basic.ConsoleLog {
  173. Log.SetOutput(logWriter)
  174. }
  175. } else if Config.Basic.ConsoleLog {
  176. Log.SetOutput(os.Stdout)
  177. }
  178. if Config.Store.Type == "redis" && Config.Store.Redis.ConnectionSelector != "" {
  179. if err := RedisStorageConSelectorApply(Config.Store.Redis.ConnectionSelector, Config); err != nil {
  180. Log.Fatal(err)
  181. }
  182. }
  183. if Config.Portable.PythonBin == "" {
  184. Config.Portable.PythonBin = "python"
  185. }
  186. _ = Config.Sink.Validate()
  187. }
  188. func init() {
  189. InitLogger()
  190. InitClock()
  191. }