service.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2019 Yunion
  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 lbagent
  15. import (
  16. "context"
  17. "fmt"
  18. "os"
  19. "os/signal"
  20. "sync"
  21. "syscall"
  22. execlient "yunion.io/x/executor/client"
  23. "yunion.io/x/log"
  24. "yunion.io/x/pkg/appctx"
  25. app_common "yunion.io/x/onecloud/pkg/cloudcommon/app"
  26. common_options "yunion.io/x/onecloud/pkg/cloudcommon/options"
  27. "yunion.io/x/onecloud/pkg/hostman/hostinfo/hostbridge"
  28. "yunion.io/x/onecloud/pkg/util/fileutils2"
  29. "yunion.io/x/onecloud/pkg/util/ovnutils"
  30. "yunion.io/x/onecloud/pkg/util/procutils"
  31. "yunion.io/x/onecloud/pkg/util/sysutils"
  32. )
  33. func StartService() {
  34. opts := &Options{}
  35. commonOpts := &opts.CommonOptions
  36. {
  37. common_options.ParseOptions(opts, os.Args, "lbagent.conf", "lbagent")
  38. if len(opts.CommonConfigFile) > 0 && fileutils2.Exists(opts.CommonConfigFile) {
  39. log.Infof("read common config file: %s", opts.CommonConfigFile)
  40. commonCfg := &LbagentCommonOptions{}
  41. commonCfg.Config = opts.CommonConfigFile
  42. common_options.ParseOptions(commonCfg, []string{os.Args[0]}, "common.conf", "lbagent")
  43. baseOpt := opts.BaseOptions.BaseOptions
  44. opts.LbagentCommonOptions = *commonCfg
  45. // keep base options
  46. opts.BaseOptions.BaseOptions = baseOpt
  47. }
  48. app_common.InitAuth(commonOpts, func() {
  49. log.Infof("auth finished ok")
  50. })
  51. }
  52. if err := opts.ValidateThenInit(); err != nil {
  53. log.Fatalf("opts validate: %s", err)
  54. }
  55. if opts.EnableRemoteExecutor {
  56. execlient.Init(opts.ExecutorSocketPath)
  57. execlient.SetTimeoutSeconds(opts.ExecutorConnectTimeoutSeconds)
  58. procutils.SetRemoteExecutor()
  59. }
  60. // register lbagent
  61. ctx := context.Background()
  62. ctx = context.WithValue(ctx, appctx.APP_CONTEXT_KEY_APPNAME, "lbagent")
  63. lbagentId, err := register(ctx, opts)
  64. if err != nil {
  65. log.Fatalf("register lbagent failed: %s", err)
  66. }
  67. if !opts.DisableLocalVpc {
  68. err := hostbridge.OVSPrepare()
  69. if err != nil {
  70. log.Fatalf("ovs prepare fail: %s", err)
  71. }
  72. err = ovnutils.InitOvn(opts.SOvnOptions)
  73. if err != nil {
  74. log.Fatalf("ovn init fail: %s", err)
  75. }
  76. }
  77. tuneSystem()
  78. var haproxyHelper *HaproxyHelper
  79. var apiHelper *ApiHelper
  80. var haStateWatcher *HaStateWatcher
  81. {
  82. haStateWatcher, err = NewHaStateWatcher(opts)
  83. if err != nil {
  84. log.Fatalf("init ha state watcher failed: %s", err)
  85. }
  86. }
  87. {
  88. haproxyHelper, err = NewHaproxyHelper(opts, lbagentId)
  89. if err != nil {
  90. log.Fatalf("init haproxy helper failed: %s", err)
  91. }
  92. }
  93. {
  94. apiHelper, err = NewApiHelper(opts, lbagentId)
  95. if err != nil {
  96. log.Fatalf("init api helper failed: %s", err)
  97. }
  98. apiHelper.SetHaStateProvider(haStateWatcher)
  99. }
  100. {
  101. wg := &sync.WaitGroup{}
  102. cmdChan := make(chan *LbagentCmd) // internal
  103. var cancelFunc context.CancelFunc
  104. ctx, cancelFunc = context.WithCancel(ctx)
  105. ctx = context.WithValue(ctx, "wg", wg)
  106. ctx = context.WithValue(ctx, "cmdChan", cmdChan)
  107. wg.Add(3)
  108. go haStateWatcher.Run(ctx)
  109. go haproxyHelper.Run(ctx)
  110. go apiHelper.Run(ctx)
  111. go func() {
  112. sigChan := make(chan os.Signal)
  113. signal.Notify(sigChan, syscall.SIGINT)
  114. signal.Notify(sigChan, syscall.SIGTERM)
  115. sig := <-sigChan
  116. log.Infof("signal received: %s", sig)
  117. cancelFunc()
  118. }()
  119. wg.Wait()
  120. }
  121. }
  122. func tuneSystem() {
  123. minMemKB := fmt.Sprintf("%d", 128*1024)
  124. kv := map[string]string{
  125. "/proc/sys/vm/swappiness": "0",
  126. "/proc/sys/vm/vfs_cache_pressure": "350",
  127. "/proc/sys/vm/min_free_kbytes": minMemKB,
  128. "/proc/sys/net/ipv4/tcp_mtu_probing": "2",
  129. "/proc/sys/net/ipv4/neigh/default/gc_thresh1": "1024",
  130. "/proc/sys/net/ipv4/neigh/default/gc_thresh2": "4096",
  131. "/proc/sys/net/ipv4/neigh/default/gc_thresh3": "8192",
  132. "/proc/sys/net/netfilter/nf_conntrack_tcp_be_liberal": "1",
  133. }
  134. for k, v := range kv {
  135. sysutils.SetSysConfig(k, v)
  136. }
  137. }