conf.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2021-2023 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 or equal to 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 SourceConf struct {
  89. HttpServerIp string `json:"httpServerIp" yaml:"httpServerIp"`
  90. HttpServerPort int `json:"httpServerPort" yaml:"httpServerPort"`
  91. HttpServerTls *tlsConf `json:"httpServerTls" yaml:"httpServerTls"`
  92. }
  93. func (sc *SourceConf) Validate() error {
  94. e := make(errorx.MultiError)
  95. if sc.HttpServerIp == "" {
  96. sc.HttpServerIp = "0.0.0.0"
  97. }
  98. if sc.HttpServerPort <= 0 || sc.HttpServerPort > 65535 {
  99. Log.Warnf("invalid source.httpServerPort configuration %d, set to 10081", sc.HttpServerPort)
  100. e["invalidHttpServerPort"] = fmt.Errorf("httpServerPort must between 0 and 65535")
  101. sc.HttpServerPort = 10081
  102. }
  103. return e
  104. }
  105. type SQLConf struct {
  106. MaxConnections int `yaml:"maxConnections"`
  107. }
  108. type KuiperConf struct {
  109. Basic struct {
  110. Debug bool `yaml:"debug"`
  111. ConsoleLog bool `yaml:"consoleLog"`
  112. FileLog bool `yaml:"fileLog"`
  113. RotateTime int `yaml:"rotateTime"`
  114. MaxAge int `yaml:"maxAge"`
  115. Ip string `yaml:"ip"`
  116. Port int `yaml:"port"`
  117. RestIp string `yaml:"restIp"`
  118. RestPort int `yaml:"restPort"`
  119. RestTls *tlsConf `yaml:"restTls"`
  120. Prometheus bool `yaml:"prometheus"`
  121. PrometheusPort int `yaml:"prometheusPort"`
  122. PluginHosts string `yaml:"pluginHosts"`
  123. Authentication bool `yaml:"authentication"`
  124. IgnoreCase bool `yaml:"ignoreCase"`
  125. SQLConf *SQLConf `yaml:"sql"`
  126. }
  127. Rule api.RuleOption
  128. Sink *SinkConf
  129. Source *SourceConf
  130. Store struct {
  131. Type string `yaml:"type"`
  132. Redis struct {
  133. Host string `yaml:"host"`
  134. Port int `yaml:"port"`
  135. Password string `yaml:"password"`
  136. Timeout int `yaml:"timeout"`
  137. ConnectionSelector string `yaml:"connectionSelector"`
  138. }
  139. Sqlite struct {
  140. Name string `yaml:"name"`
  141. }
  142. }
  143. Portable struct {
  144. PythonBin string `yaml:"pythonBin"`
  145. InitTimeout int `yaml:"initTimeout"`
  146. }
  147. }
  148. func InitConf() {
  149. cpath, err := GetConfLoc()
  150. if err != nil {
  151. panic(err)
  152. }
  153. kc := KuiperConf{
  154. Rule: api.RuleOption{
  155. LateTol: 1000,
  156. Concurrency: 1,
  157. BufferLength: 1024,
  158. CheckpointInterval: 300000, //5 minutes
  159. SendError: true,
  160. Restart: &api.RestartStrategy{
  161. Attempts: 0,
  162. Delay: 1000,
  163. Multiplier: 2,
  164. MaxDelay: 30000,
  165. JitterFactor: 0.1,
  166. },
  167. },
  168. }
  169. err = LoadConfigFromPath(path.Join(cpath, ConfFileName), &kc)
  170. if err != nil {
  171. Log.Fatal(err)
  172. panic(err)
  173. }
  174. Config = &kc
  175. if 0 == len(Config.Basic.Ip) {
  176. Config.Basic.Ip = "0.0.0.0"
  177. }
  178. if 0 == len(Config.Basic.RestIp) {
  179. Config.Basic.RestIp = "0.0.0.0"
  180. }
  181. if Config.Basic.Debug {
  182. Log.SetLevel(logrus.DebugLevel)
  183. }
  184. if Config.Basic.FileLog {
  185. logDir, err := GetLoc(logDir)
  186. if err != nil {
  187. Log.Fatal(err)
  188. }
  189. file := path.Join(logDir, logFileName)
  190. logWriter, err := rotatelogs.New(
  191. file+".%Y-%m-%d_%H-%M-%S",
  192. rotatelogs.WithLinkName(file),
  193. rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)),
  194. rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)),
  195. )
  196. if err != nil {
  197. fmt.Println("Failed to init log file settings..." + err.Error())
  198. Log.Infof("Failed to log to file, using default stderr.")
  199. } else if Config.Basic.ConsoleLog {
  200. mw := io.MultiWriter(os.Stdout, logWriter)
  201. Log.SetOutput(mw)
  202. } else if !Config.Basic.ConsoleLog {
  203. Log.SetOutput(logWriter)
  204. }
  205. } else if Config.Basic.ConsoleLog {
  206. Log.SetOutput(os.Stdout)
  207. }
  208. if Config.Store.Type == "redis" && Config.Store.Redis.ConnectionSelector != "" {
  209. if err := RedisStorageConSelectorApply(Config.Store.Redis.ConnectionSelector, Config); err != nil {
  210. Log.Fatal(err)
  211. }
  212. }
  213. if Config.Portable.PythonBin == "" {
  214. Config.Portable.PythonBin = "python"
  215. }
  216. if Config.Portable.InitTimeout <= 0 {
  217. Config.Portable.InitTimeout = 5000
  218. }
  219. if Config.Source == nil {
  220. Config.Source = &SourceConf{}
  221. }
  222. _ = Config.Source.Validate()
  223. if Config.Sink == nil {
  224. Config.Sink = &SinkConf{}
  225. }
  226. _ = Config.Sink.Validate()
  227. _ = ValidateRuleOption(&Config.Rule)
  228. }
  229. func ValidateRuleOption(option *api.RuleOption) error {
  230. e := make(errorx.MultiError)
  231. if option.CheckpointInterval < 0 {
  232. option.CheckpointInterval = 0
  233. Log.Warnf("checkpointInterval is negative, set to 0")
  234. e["invalidCheckpointInterval"] = fmt.Errorf("checkpointInterval must be greater than 0")
  235. }
  236. if option.Concurrency < 0 {
  237. option.Concurrency = 1
  238. Log.Warnf("concurrency is negative, set to 1")
  239. e["invalidConcurrency"] = fmt.Errorf("concurrency must be greater than 0")
  240. }
  241. if option.BufferLength < 0 {
  242. option.BufferLength = 1024
  243. Log.Warnf("bufferLength is negative, set to 1024")
  244. e["invalidBufferLength"] = fmt.Errorf("bufferLength must be greater than 0")
  245. }
  246. if option.LateTol < 0 {
  247. option.LateTol = 1000
  248. Log.Warnf("lateTol is negative, set to 1000")
  249. e["invalidLateTol"] = fmt.Errorf("lateTol must be greater than 0")
  250. }
  251. if option.Restart != nil {
  252. if option.Restart.Multiplier <= 0 {
  253. option.Restart.Multiplier = 2
  254. Log.Warnf("restart multiplier is negative, set to 2")
  255. e["invalidRestartMultiplier"] = fmt.Errorf("restart multiplier must be greater than 0")
  256. }
  257. if option.Restart.Attempts < 0 {
  258. option.Restart.Attempts = 0
  259. Log.Warnf("restart attempts is negative, set to 0")
  260. e["invalidRestartAttempts"] = fmt.Errorf("restart attempts must be greater than 0")
  261. }
  262. if option.Restart.Delay <= 0 {
  263. option.Restart.Delay = 1000
  264. Log.Warnf("restart delay is negative, set to 1000")
  265. e["invalidRestartDelay"] = fmt.Errorf("restart delay must be greater than 0")
  266. }
  267. if option.Restart.MaxDelay <= 0 {
  268. option.Restart.MaxDelay = option.Restart.Delay
  269. Log.Warnf("restart maxDelay is negative, set to %d", option.Restart.Delay)
  270. e["invalidRestartMaxDelay"] = fmt.Errorf("restart maxDelay must be greater than 0")
  271. }
  272. if option.Restart.JitterFactor <= 0 || option.Restart.JitterFactor >= 1 {
  273. option.Restart.JitterFactor = 0.1
  274. Log.Warnf("restart jitterFactor must between 0 and 1, set to 0.1")
  275. e["invalidRestartJitterFactor"] = fmt.Errorf("restart jitterFactor must between [0, 1)")
  276. }
  277. }
  278. return e.GetError()
  279. }
  280. func init() {
  281. InitLogger()
  282. InitClock()
  283. }