conf.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 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. }
  142. }
  143. func InitConf() {
  144. cpath, err := GetConfLoc()
  145. if err != nil {
  146. panic(err)
  147. }
  148. kc := KuiperConf{
  149. Rule: api.RuleOption{
  150. LateTol: 1000,
  151. Concurrency: 1,
  152. BufferLength: 1024,
  153. CheckpointInterval: 300000, //5 minutes
  154. SendError: true,
  155. Restart: &api.RestartStrategy{
  156. Attempts: 0,
  157. Delay: 1000,
  158. Multiplier: 2,
  159. MaxDelay: 30000,
  160. JitterFactor: 0.1,
  161. },
  162. },
  163. }
  164. err = LoadConfigFromPath(path.Join(cpath, ConfFileName), &kc)
  165. if err != nil {
  166. Log.Fatal(err)
  167. panic(err)
  168. }
  169. Config = &kc
  170. if 0 == len(Config.Basic.Ip) {
  171. Config.Basic.Ip = "0.0.0.0"
  172. }
  173. if 0 == len(Config.Basic.RestIp) {
  174. Config.Basic.RestIp = "0.0.0.0"
  175. }
  176. if Config.Basic.Debug {
  177. Log.SetLevel(logrus.DebugLevel)
  178. }
  179. if Config.Basic.FileLog {
  180. logDir, err := GetLoc(logDir)
  181. if err != nil {
  182. Log.Fatal(err)
  183. }
  184. file := path.Join(logDir, logFileName)
  185. logWriter, err := rotatelogs.New(
  186. file+".%Y-%m-%d_%H-%M-%S",
  187. rotatelogs.WithLinkName(file),
  188. rotatelogs.WithRotationTime(time.Hour*time.Duration(Config.Basic.RotateTime)),
  189. rotatelogs.WithMaxAge(time.Hour*time.Duration(Config.Basic.MaxAge)),
  190. )
  191. if err != nil {
  192. fmt.Println("Failed to init log file settings..." + err.Error())
  193. Log.Infof("Failed to log to file, using default stderr.")
  194. } else if Config.Basic.ConsoleLog {
  195. mw := io.MultiWriter(os.Stdout, logWriter)
  196. Log.SetOutput(mw)
  197. } else if !Config.Basic.ConsoleLog {
  198. Log.SetOutput(logWriter)
  199. }
  200. } else if Config.Basic.ConsoleLog {
  201. Log.SetOutput(os.Stdout)
  202. }
  203. if Config.Store.Type == "redis" && Config.Store.Redis.ConnectionSelector != "" {
  204. if err := RedisStorageConSelectorApply(Config.Store.Redis.ConnectionSelector, Config); err != nil {
  205. Log.Fatal(err)
  206. }
  207. }
  208. if Config.Portable.PythonBin == "" {
  209. Config.Portable.PythonBin = "python"
  210. }
  211. if Config.Source == nil {
  212. Config.Source = &SourceConf{}
  213. }
  214. _ = Config.Source.Validate()
  215. if Config.Sink == nil {
  216. Config.Sink = &SinkConf{}
  217. }
  218. _ = Config.Sink.Validate()
  219. _ = ValidateRuleOption(&Config.Rule)
  220. }
  221. func ValidateRuleOption(option *api.RuleOption) error {
  222. e := make(errorx.MultiError)
  223. if option.CheckpointInterval < 0 {
  224. option.CheckpointInterval = 0
  225. Log.Warnf("checkpointInterval is negative, set to 0")
  226. e["invalidCheckpointInterval"] = fmt.Errorf("checkpointInterval must be greater than 0")
  227. }
  228. if option.Concurrency < 0 {
  229. option.Concurrency = 1
  230. Log.Warnf("concurrency is negative, set to 1")
  231. e["invalidConcurrency"] = fmt.Errorf("concurrency must be greater than 0")
  232. }
  233. if option.BufferLength < 0 {
  234. option.BufferLength = 1024
  235. Log.Warnf("bufferLength is negative, set to 1024")
  236. e["invalidBufferLength"] = fmt.Errorf("bufferLength must be greater than 0")
  237. }
  238. if option.LateTol < 0 {
  239. option.LateTol = 1000
  240. Log.Warnf("lateTol is negative, set to 1000")
  241. e["invalidLateTol"] = fmt.Errorf("lateTol must be greater than 0")
  242. }
  243. if option.Restart != nil {
  244. if option.Restart.Multiplier <= 0 {
  245. option.Restart.Multiplier = 2
  246. Log.Warnf("restart multiplier is negative, set to 2")
  247. e["invalidRestartMultiplier"] = fmt.Errorf("restart multiplier must be greater than 0")
  248. }
  249. if option.Restart.Attempts < 0 {
  250. option.Restart.Attempts = 0
  251. Log.Warnf("restart attempts is negative, set to 0")
  252. e["invalidRestartAttempts"] = fmt.Errorf("restart attempts must be greater than 0")
  253. }
  254. if option.Restart.Delay <= 0 {
  255. option.Restart.Delay = 1000
  256. Log.Warnf("restart delay is negative, set to 1000")
  257. e["invalidRestartDelay"] = fmt.Errorf("restart delay must be greater than 0")
  258. }
  259. if option.Restart.MaxDelay <= 0 {
  260. option.Restart.MaxDelay = option.Restart.Delay
  261. Log.Warnf("restart maxDelay is negative, set to %d", option.Restart.Delay)
  262. e["invalidRestartMaxDelay"] = fmt.Errorf("restart maxDelay must be greater than 0")
  263. }
  264. if option.Restart.JitterFactor <= 0 || option.Restart.JitterFactor >= 1 {
  265. option.Restart.JitterFactor = 0.1
  266. Log.Warnf("restart jitterFactor must between 0 and 1, set to 0.1")
  267. e["invalidRestartJitterFactor"] = fmt.Errorf("restart jitterFactor must between [0, 1)")
  268. }
  269. }
  270. return e.GetError()
  271. }
  272. func init() {
  273. InitLogger()
  274. InitClock()
  275. }