conf.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. "errors"
  17. "fmt"
  18. "io"
  19. "os"
  20. "path"
  21. "time"
  22. "github.com/lestrrat-go/file-rotatelogs"
  23. "github.com/sirupsen/logrus"
  24. "github.com/lf-edge/ekuiper/pkg/api"
  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. var errs error
  46. if sc.MemoryCacheThreshold < 0 {
  47. sc.MemoryCacheThreshold = 1024
  48. Log.Warnf("memoryCacheThreshold is less than 0, set to 1024")
  49. errs = errors.Join(errs, errors.New("memoryCacheThreshold: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. errs = errors.Join(errs, errors.New("maxDiskCache: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. errs = errors.Join(errs, errors.New("bufferPageSize: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. errs = errors.Join(errs, errors.New("resendInterval: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. errs = errors.Join(errs, errors.New("memoryCacheThresholdTooSmall: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. errs = errors.Join(errs, errors.New("memoryCacheThresholdNotMultiple: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. errs = errors.Join(errs, errors.New("maxDiskCacheTooSmall: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. errs = errors.Join(errs, errors.New("maxDiskCacheNotMultiple:maxDiskCache must be a multiple of bufferPageSize"))
  85. }
  86. return errs
  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. var errs error
  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. errs = errors.Join(errs, errors.New("invalidHttpServerPort:httpServerPort must between 0 and 65535"))
  101. sc.HttpServerPort = 10081
  102. }
  103. return errs
  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. ExtStateType string `yaml:"extStateType"`
  133. Redis struct {
  134. Host string `yaml:"host"`
  135. Port int `yaml:"port"`
  136. Password string `yaml:"password"`
  137. Timeout int `yaml:"timeout"`
  138. ConnectionSelector string `yaml:"connectionSelector"`
  139. }
  140. Sqlite struct {
  141. Name string `yaml:"name"`
  142. }
  143. }
  144. Portable struct {
  145. PythonBin string `yaml:"pythonBin"`
  146. InitTimeout int `yaml:"initTimeout"`
  147. }
  148. }
  149. func InitConf() {
  150. cpath, err := GetConfLoc()
  151. if err != nil {
  152. panic(err)
  153. }
  154. kc := KuiperConf{
  155. Rule: api.RuleOption{
  156. LateTol: 1000,
  157. Concurrency: 1,
  158. BufferLength: 1024,
  159. CheckpointInterval: 300000, // 5 minutes
  160. SendError: true,
  161. Restart: &api.RestartStrategy{
  162. Attempts: 0,
  163. Delay: 1000,
  164. Multiplier: 2,
  165. MaxDelay: 30000,
  166. JitterFactor: 0.1,
  167. },
  168. },
  169. }
  170. err = LoadConfigFromPath(path.Join(cpath, ConfFileName), &kc)
  171. if err != nil {
  172. Log.Fatal(err)
  173. panic(err)
  174. }
  175. Config = &kc
  176. if 0 == len(Config.Basic.Ip) {
  177. Config.Basic.Ip = "0.0.0.0"
  178. }
  179. if 0 == len(Config.Basic.RestIp) {
  180. Config.Basic.RestIp = "0.0.0.0"
  181. }
  182. if Config.Basic.Debug {
  183. Log.SetLevel(logrus.DebugLevel)
  184. }
  185. if Config.Basic.FileLog {
  186. logDir, err := GetLoc(logDir)
  187. if err != nil {
  188. Log.Fatal(err)
  189. }
  190. file := path.Join(logDir, logFileName)
  191. logWriter, err := rotatelogs.New(
  192. file+".%Y-%m-%d_%H-%M-%S",
  193. rotatelogs.WithLinkName(file),
  194. rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)),
  195. rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)),
  196. )
  197. if err != nil {
  198. fmt.Println("Failed to init log file settings..." + err.Error())
  199. Log.Infof("Failed to log to file, using default stderr.")
  200. } else if Config.Basic.ConsoleLog {
  201. mw := io.MultiWriter(os.Stdout, logWriter)
  202. Log.SetOutput(mw)
  203. } else if !Config.Basic.ConsoleLog {
  204. Log.SetOutput(logWriter)
  205. }
  206. } else if Config.Basic.ConsoleLog {
  207. Log.SetOutput(os.Stdout)
  208. }
  209. if Config.Store.Type == "redis" && Config.Store.Redis.ConnectionSelector != "" {
  210. if err := RedisStorageConSelectorApply(Config.Store.Redis.ConnectionSelector, Config); err != nil {
  211. Log.Fatal(err)
  212. }
  213. }
  214. if Config.Store.ExtStateType == "" {
  215. Config.Store.ExtStateType = "sqlite"
  216. }
  217. if Config.Portable.PythonBin == "" {
  218. Config.Portable.PythonBin = "python"
  219. }
  220. if Config.Portable.InitTimeout <= 0 {
  221. Config.Portable.InitTimeout = 5000
  222. }
  223. if Config.Source == nil {
  224. Config.Source = &SourceConf{}
  225. }
  226. _ = Config.Source.Validate()
  227. if Config.Sink == nil {
  228. Config.Sink = &SinkConf{}
  229. }
  230. _ = Config.Sink.Validate()
  231. _ = ValidateRuleOption(&Config.Rule)
  232. }
  233. func ValidateRuleOption(option *api.RuleOption) error {
  234. var errs error
  235. if option.CheckpointInterval < 0 {
  236. option.CheckpointInterval = 0
  237. Log.Warnf("checkpointInterval is negative, set to 0")
  238. errs = errors.Join(errs, errors.New("invalidCheckpointInterval:checkpointInterval must be greater than 0"))
  239. }
  240. if option.Concurrency < 0 {
  241. option.Concurrency = 1
  242. Log.Warnf("concurrency is negative, set to 1")
  243. errs = errors.Join(errs, errors.New("invalidConcurrency:concurrency must be greater than 0"))
  244. }
  245. if option.BufferLength < 0 {
  246. option.BufferLength = 1024
  247. Log.Warnf("bufferLength is negative, set to 1024")
  248. errs = errors.Join(errs, errors.New("invalidBufferLength:bufferLength must be greater than 0"))
  249. }
  250. if option.LateTol < 0 {
  251. option.LateTol = 1000
  252. Log.Warnf("lateTol is negative, set to 1000")
  253. errs = errors.Join(errs, errors.New("invalidLateTol:lateTol must be greater than 0"))
  254. }
  255. if option.Restart != nil {
  256. if option.Restart.Multiplier <= 0 {
  257. option.Restart.Multiplier = 2
  258. Log.Warnf("restart multiplier is negative, set to 2")
  259. errs = errors.Join(errs, errors.New("invalidRestartMultiplier:restart multiplier must be greater than 0"))
  260. }
  261. if option.Restart.Attempts < 0 {
  262. option.Restart.Attempts = 0
  263. Log.Warnf("restart attempts is negative, set to 0")
  264. errs = errors.Join(errs, errors.New("invalidRestartAttempts:restart attempts must be greater than 0"))
  265. }
  266. if option.Restart.Delay <= 0 {
  267. option.Restart.Delay = 1000
  268. Log.Warnf("restart delay is negative, set to 1000")
  269. errs = errors.Join(errs, errors.New("invalidRestartDelay:restart delay must be greater than 0"))
  270. }
  271. if option.Restart.MaxDelay <= 0 {
  272. option.Restart.MaxDelay = option.Restart.Delay
  273. Log.Warnf("restart maxDelay is negative, set to %d", option.Restart.Delay)
  274. errs = errors.Join(errs, errors.New("invalidRestartMaxDelay:restart maxDelay must be greater than 0"))
  275. }
  276. if option.Restart.JitterFactor <= 0 || option.Restart.JitterFactor >= 1 {
  277. option.Restart.JitterFactor = 0.1
  278. Log.Warnf("restart jitterFactor must between 0 and 1, set to 0.1")
  279. errs = errors.Join(errs, errors.New("invalidRestartJitterFactor:restart jitterFactor must between [0, 1)"))
  280. }
  281. }
  282. return errs
  283. }
  284. func init() {
  285. InitLogger()
  286. InitClock()
  287. }