conf.go 9.6 KB

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