modern_services.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package nginx_log
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "regexp"
  8. "sort"
  9. "strings"
  10. "sync"
  11. "sync/atomic"
  12. "time"
  13. "github.com/0xJacky/Nginx-UI/internal/nginx_log/analytics"
  14. "github.com/0xJacky/Nginx-UI/internal/nginx_log/indexer"
  15. "github.com/0xJacky/Nginx-UI/internal/nginx_log/searcher"
  16. "github.com/0xJacky/Nginx-UI/settings"
  17. "github.com/blevesearch/bleve/v2"
  18. "github.com/uozi-tech/cosy/logger"
  19. cSettings "github.com/uozi-tech/cosy/settings"
  20. )
  21. // Global instances for new services
  22. var (
  23. globalSearcher searcher.Searcher
  24. globalAnalytics analytics.Service
  25. globalIndexer *indexer.ParallelIndexer
  26. globalLogFileManager *indexer.LogFileManager
  27. servicesInitialized bool
  28. servicesMutex sync.RWMutex
  29. shutdownCancel context.CancelFunc
  30. isShuttingDown bool
  31. lastShardUpdateAttempt int64
  32. )
  33. // Fallback storage when AdvancedIndexingEnabled is disabled
  34. var (
  35. fallbackCache = make(map[string]*NginxLogCache)
  36. fallbackCacheMutex sync.RWMutex
  37. )
  38. // InitializeModernServices initializes the new modular services
  39. func InitializeModernServices(ctx context.Context) {
  40. servicesMutex.Lock()
  41. defer servicesMutex.Unlock()
  42. // Check if advanced indexing is enabled
  43. if !settings.NginxLogSettings.AdvancedIndexingEnabled {
  44. logger.Info("Advanced indexing is disabled, skipping nginx_log services initialization")
  45. return
  46. }
  47. if servicesInitialized {
  48. logger.Info("Modern nginx log services already initialized, skipping")
  49. return
  50. }
  51. logger.Info("Initializing modern nginx log services...")
  52. // Create a cancellable context for services
  53. serviceCtx, cancel := context.WithCancel(ctx)
  54. shutdownCancel = cancel
  55. // Initialize with default configuration directly
  56. if err := initializeWithDefaults(serviceCtx); err != nil {
  57. logger.Errorf("Failed to initialize modern services: %v", err)
  58. return
  59. }
  60. logger.Info("Modern nginx log services initialization completed")
  61. // Monitor context for shutdown
  62. go func() {
  63. logger.Info("Started nginx_log shutdown monitor goroutine")
  64. <-serviceCtx.Done()
  65. logger.Info("Context cancelled, initiating shutdown...")
  66. // Use the same shutdown logic as manual stop
  67. StopModernServices()
  68. logger.Info("Nginx_log shutdown monitor goroutine completed")
  69. }()
  70. }
  71. // initializeWithDefaults creates services with default configuration
  72. func initializeWithDefaults(ctx context.Context) error {
  73. logger.Info("Initializing services with default configuration")
  74. // Create empty searcher (will be populated when indexes are available)
  75. searcherConfig := searcher.DefaultSearcherConfig()
  76. globalSearcher = searcher.NewDistributedSearcher(searcherConfig, []bleve.Index{})
  77. // Initialize analytics with empty searcher
  78. globalAnalytics = analytics.NewService(globalSearcher)
  79. // Initialize parallel indexer with shard manager
  80. indexerConfig := indexer.DefaultIndexerConfig()
  81. // Use config directory for index path
  82. indexerConfig.IndexPath = getConfigDirIndexPath()
  83. shardManager := indexer.NewGroupedShardManager(indexerConfig)
  84. globalIndexer = indexer.NewParallelIndexer(indexerConfig, shardManager)
  85. // Start the indexer
  86. if err := globalIndexer.Start(ctx); err != nil {
  87. logger.Errorf("Failed to start parallel indexer: %v", err)
  88. return fmt.Errorf("failed to start parallel indexer: %w", err)
  89. }
  90. // Initialize log file manager
  91. globalLogFileManager = indexer.NewLogFileManager()
  92. // Inject indexer for precise doc counting before persisting
  93. globalLogFileManager.SetIndexer(globalIndexer)
  94. servicesInitialized = true
  95. // After all services are initialized, update the searcher with any existing shards.
  96. // This is crucial for loading the index state on application startup.
  97. // We call the 'locked' version because we already hold the mutex here.
  98. updateSearcherShardsLocked()
  99. return nil
  100. }
  101. // getConfigDirIndexPath returns the index path relative to the config file directory
  102. func getConfigDirIndexPath() string {
  103. // Use custom path if configured
  104. if settings.NginxLogSettings.IndexPath != "" {
  105. indexPath := settings.NginxLogSettings.IndexPath
  106. // Ensure the directory exists
  107. if err := os.MkdirAll(indexPath, 0755); err != nil {
  108. logger.Warnf("Failed to create custom index directory at %s: %v, using default", indexPath, err)
  109. } else {
  110. logger.Infof("Using custom index path: %s", indexPath)
  111. return indexPath
  112. }
  113. }
  114. // Get the config file path from cosy settings
  115. if cSettings.ConfPath != "" {
  116. configDir := filepath.Dir(cSettings.ConfPath)
  117. indexPath := filepath.Join(configDir, "log-index")
  118. // Ensure the directory exists
  119. if err := os.MkdirAll(indexPath, 0755); err != nil {
  120. logger.Warnf("Failed to create index directory at %s: %v, using default", indexPath, err)
  121. return "./log-index"
  122. }
  123. return indexPath
  124. }
  125. // Fallback to default relative path
  126. logger.Warn("Config file path not available, using default index path")
  127. return "./log-index"
  128. }
  129. // GetModernSearcher returns the global searcher instance
  130. func GetModernSearcher() searcher.Searcher {
  131. servicesMutex.RLock()
  132. defer servicesMutex.RUnlock()
  133. if !servicesInitialized {
  134. logger.Warn("Modern services not initialized, returning nil")
  135. return nil
  136. }
  137. if globalSearcher == nil {
  138. logger.Warn("GetModernSearcher: globalSearcher is nil even though services are initialized")
  139. return nil
  140. }
  141. // Check searcher health status
  142. isHealthy := globalSearcher.IsHealthy()
  143. isRunning := globalSearcher.IsRunning()
  144. logger.Debugf("GetModernSearcher: returning searcher, isHealthy: %v, isRunning: %v", isHealthy, isRunning)
  145. // Auto-heal: if the searcher is running but unhealthy (likely zero shards),
  146. // and the indexer is initialized, trigger an async shard swap (throttled).
  147. if !isHealthy && isRunning && globalIndexer != nil {
  148. now := time.Now().UnixNano()
  149. prev := atomic.LoadInt64(&lastShardUpdateAttempt)
  150. if now-prev > int64(5*time.Second) {
  151. if atomic.CompareAndSwapInt64(&lastShardUpdateAttempt, prev, now) {
  152. logger.Debugf("GetModernSearcher: unhealthy detected, scheduling UpdateSearcherShards()")
  153. go UpdateSearcherShards()
  154. }
  155. }
  156. }
  157. return globalSearcher
  158. }
  159. // GetModernAnalytics returns the global analytics service instance
  160. func GetModernAnalytics() analytics.Service {
  161. servicesMutex.RLock()
  162. defer servicesMutex.RUnlock()
  163. if !servicesInitialized {
  164. logger.Warn("Modern services not initialized, returning nil")
  165. return nil
  166. }
  167. return globalAnalytics
  168. }
  169. // GetModernIndexer returns the global indexer instance
  170. func GetModernIndexer() *indexer.ParallelIndexer {
  171. servicesMutex.RLock()
  172. defer servicesMutex.RUnlock()
  173. if !servicesInitialized {
  174. logger.Warn("Modern services not initialized, returning nil")
  175. return nil
  176. }
  177. return globalIndexer
  178. }
  179. // GetLogFileManager returns the global log file manager instance
  180. func GetLogFileManager() *indexer.LogFileManager {
  181. servicesMutex.RLock()
  182. defer servicesMutex.RUnlock()
  183. if !servicesInitialized {
  184. // Only warn during actual operations, not during initialization
  185. return nil
  186. }
  187. if globalLogFileManager == nil {
  188. logger.Warnf("[nginx_log] GetLogFileManager: globalLogFileManager is nil even though servicesInitialized=true")
  189. return nil
  190. }
  191. return globalLogFileManager
  192. }
  193. // NginxLogCache Type aliases for backward compatibility
  194. type NginxLogCache = indexer.NginxLogCache
  195. type NginxLogWithIndex = indexer.NginxLogWithIndex
  196. // Constants for backward compatibility
  197. const (
  198. IndexStatusIndexed = string(indexer.IndexStatusIndexed)
  199. IndexStatusIndexing = string(indexer.IndexStatusIndexing)
  200. IndexStatusNotIndexed = string(indexer.IndexStatusNotIndexed)
  201. )
  202. // Legacy compatibility functions for log cache system
  203. // AddLogPath adds a log path to the log cache with the source config file
  204. func AddLogPath(path, logType, name, configFile string) {
  205. if manager := GetLogFileManager(); manager != nil {
  206. manager.AddLogPath(path, logType, name, configFile)
  207. return
  208. }
  209. // Fallback storage
  210. fallbackCacheMutex.Lock()
  211. fallbackCache[path] = &NginxLogCache{
  212. Path: path,
  213. Type: logType,
  214. Name: name,
  215. ConfigFile: configFile,
  216. }
  217. fallbackCacheMutex.Unlock()
  218. }
  219. // RemoveLogPathsFromConfig removes all log paths associated with a specific config file
  220. func RemoveLogPathsFromConfig(configFile string) {
  221. if manager := GetLogFileManager(); manager != nil {
  222. manager.RemoveLogPathsFromConfig(configFile)
  223. return
  224. }
  225. // Fallback removal
  226. fallbackCacheMutex.Lock()
  227. for p, entry := range fallbackCache {
  228. if entry.ConfigFile == configFile {
  229. delete(fallbackCache, p)
  230. }
  231. }
  232. fallbackCacheMutex.Unlock()
  233. }
  234. // GetAllLogPaths returns all cached log paths, optionally filtered
  235. func GetAllLogPaths(filters ...func(*NginxLogCache) bool) []*NginxLogCache {
  236. if manager := GetLogFileManager(); manager != nil {
  237. return manager.GetAllLogPaths(filters...)
  238. }
  239. // Fallback list
  240. fallbackCacheMutex.RLock()
  241. defer fallbackCacheMutex.RUnlock()
  242. var logs []*NginxLogCache
  243. for _, entry := range fallbackCache {
  244. include := true
  245. for _, f := range filters {
  246. if !f(entry) {
  247. include = false
  248. break
  249. }
  250. }
  251. if include {
  252. // Create a copy to avoid external mutation
  253. e := *entry
  254. logs = append(logs, &e)
  255. }
  256. }
  257. return logs
  258. }
  259. // GetAllLogsWithIndex returns all cached log paths with their index status
  260. func GetAllLogsWithIndex(filters ...func(*NginxLogWithIndex) bool) []*NginxLogWithIndex {
  261. if manager := GetLogFileManager(); manager != nil {
  262. return manager.GetAllLogsWithIndex(filters...)
  263. }
  264. // Fallback: produce basic entries without indexing metadata
  265. fallbackCacheMutex.RLock()
  266. defer fallbackCacheMutex.RUnlock()
  267. result := make([]*NginxLogWithIndex, 0, len(fallbackCache))
  268. for _, c := range fallbackCache {
  269. lw := &NginxLogWithIndex{
  270. Path: c.Path,
  271. Type: c.Type,
  272. Name: c.Name,
  273. ConfigFile: c.ConfigFile,
  274. IndexStatus: IndexStatusNotIndexed,
  275. }
  276. include := true
  277. for _, f := range filters {
  278. if !f(lw) {
  279. include = false
  280. break
  281. }
  282. }
  283. if include {
  284. result = append(result, lw)
  285. }
  286. }
  287. return result
  288. }
  289. // GetAllLogsWithIndexGrouped returns logs grouped by their base name
  290. func GetAllLogsWithIndexGrouped(filters ...func(*NginxLogWithIndex) bool) []*NginxLogWithIndex {
  291. if manager := GetLogFileManager(); manager != nil {
  292. return manager.GetAllLogsWithIndexGrouped(filters...)
  293. }
  294. // Fallback grouping by base log name (handle simple rotation patterns)
  295. fallbackCacheMutex.RLock()
  296. defer fallbackCacheMutex.RUnlock()
  297. grouped := make(map[string]*NginxLogWithIndex)
  298. for _, c := range fallbackCache {
  299. base := getBaseLogNameBasic(c.Path)
  300. if existing, ok := grouped[base]; ok {
  301. // Preserve most recent non-indexed default; nothing to aggregate in basic mode
  302. _ = existing
  303. continue
  304. }
  305. grouped[base] = &NginxLogWithIndex{
  306. Path: base,
  307. Type: c.Type,
  308. Name: filepath.Base(base),
  309. ConfigFile: c.ConfigFile,
  310. IndexStatus: IndexStatusNotIndexed,
  311. }
  312. }
  313. // Build slice and apply filters
  314. keys := make([]string, 0, len(grouped))
  315. for k := range grouped {
  316. keys = append(keys, k)
  317. }
  318. sort.Strings(keys)
  319. result := make([]*NginxLogWithIndex, 0, len(keys))
  320. for _, k := range keys {
  321. v := grouped[k]
  322. include := true
  323. for _, f := range filters {
  324. if !f(v) {
  325. include = false
  326. break
  327. }
  328. }
  329. if include {
  330. result = append(result, v)
  331. }
  332. }
  333. return result
  334. }
  335. // --- Fallback helpers ---
  336. // getBaseLogNameBasic attempts to derive the base log file for a rotated file name.
  337. // Mirrors the logic used by the indexer, simplified for basic mode.
  338. func getBaseLogNameBasic(filePath string) string {
  339. dir := filepath.Dir(filePath)
  340. filename := filepath.Base(filePath)
  341. // Remove compression extensions
  342. for _, ext := range []string{".gz", ".bz2", ".xz", ".lz4"} {
  343. filename = strings.TrimSuffix(filename, ext)
  344. }
  345. // Check YYYY.MM.DD at end
  346. parts := strings.Split(filename, ".")
  347. if len(parts) >= 4 {
  348. lastThree := strings.Join(parts[len(parts)-3:], ".")
  349. if matched, _ := regexp.MatchString(`^\d{4}\.\d{2}\.\d{2}$`, lastThree); matched {
  350. base := strings.Join(parts[:len(parts)-3], ".")
  351. return filepath.Join(dir, base)
  352. }
  353. }
  354. // Single-part date suffix (YYYYMMDD / YYYY-MM-DD / YYMMDD)
  355. if len(parts) >= 2 {
  356. last := parts[len(parts)-1]
  357. if isFullDatePatternBasic(last) {
  358. base := strings.Join(parts[:len(parts)-1], ".")
  359. return filepath.Join(dir, base)
  360. }
  361. }
  362. // Numbered rotation: access.log.1
  363. if m := regexp.MustCompile(`^(.+)\.(\d{1,3})$`).FindStringSubmatch(filename); len(m) > 1 {
  364. base := m[1]
  365. return filepath.Join(dir, base)
  366. }
  367. // Middle-numbered rotation: access.1.log
  368. if m := regexp.MustCompile(`^(.+)\.(\d{1,3})\.log$`).FindStringSubmatch(filename); len(m) > 1 {
  369. base := m[1] + ".log"
  370. return filepath.Join(dir, base)
  371. }
  372. // Fallback: return original path
  373. return filePath
  374. }
  375. func isFullDatePatternBasic(s string) bool {
  376. patterns := []string{
  377. `^\d{8}$`, // YYYYMMDD
  378. `^\d{4}-\d{2}-\d{2}$`, // YYYY-MM-DD
  379. `^\d{6}$`, // YYMMDD
  380. }
  381. for _, p := range patterns {
  382. if matched, _ := regexp.MatchString(p, s); matched {
  383. return true
  384. }
  385. }
  386. return false
  387. }
  388. // SetIndexingStatus sets the indexing status for a specific file path
  389. func SetIndexingStatus(path string, isIndexing bool) {
  390. if manager := GetLogFileManager(); manager != nil {
  391. manager.SetIndexingStatus(path, isIndexing)
  392. }
  393. }
  394. // GetIndexingFiles returns a list of files currently being indexed
  395. func GetIndexingFiles() []string {
  396. if manager := GetLogFileManager(); manager != nil {
  397. return manager.GetIndexingFiles()
  398. }
  399. return []string{}
  400. }
  401. // UpdateSearcherShards fetches all shards from the indexer and performs zero-downtime shard updates.
  402. // Uses Bleve IndexAlias.Swap() for atomic shard replacement without recreating the searcher.
  403. // This function is safe for concurrent use and maintains service availability during index rebuilds.
  404. func UpdateSearcherShards() {
  405. // Schedule async update to avoid blocking indexing operations
  406. logger.Debugf("UpdateSearcherShards: Scheduling async shard update")
  407. go updateSearcherShardsAsync()
  408. }
  409. // updateSearcherShardsAsync performs the actual shard update asynchronously
  410. func updateSearcherShardsAsync() {
  411. // Small delay to let indexing operations complete
  412. time.Sleep(500 * time.Millisecond)
  413. logger.Debugf("updateSearcherShardsAsync: Attempting to acquire write lock...")
  414. servicesMutex.Lock()
  415. logger.Debugf("updateSearcherShardsAsync: Write lock acquired")
  416. defer func() {
  417. logger.Debugf("updateSearcherShardsAsync: Releasing write lock...")
  418. servicesMutex.Unlock()
  419. }()
  420. updateSearcherShardsLocked()
  421. }
  422. // updateSearcherShardsLocked performs the actual update logic assumes the caller holds the lock.
  423. // Uses Bleve IndexAlias.Swap() for zero-downtime shard updates following official best practices.
  424. func updateSearcherShardsLocked() {
  425. if !servicesInitialized || globalIndexer == nil {
  426. logger.Warn("Cannot update searcher shards, services not fully initialized.")
  427. return
  428. }
  429. // Check if indexer is healthy before getting shards
  430. if !globalIndexer.IsHealthy() {
  431. logger.Warn("Cannot update searcher shards, indexer is not healthy")
  432. return
  433. }
  434. newShards := globalIndexer.GetAllShards()
  435. logger.Infof("Retrieved %d new shards from indexer for hot-swap update", len(newShards))
  436. // If no searcher exists yet, create the initial one (first time setup)
  437. if globalSearcher == nil {
  438. logger.Info("Creating initial searcher with IndexAlias")
  439. searcherConfig := searcher.DefaultSearcherConfig()
  440. globalSearcher = searcher.NewDistributedSearcher(searcherConfig, newShards)
  441. if globalSearcher == nil {
  442. logger.Error("Failed to create initial searcher instance")
  443. return
  444. }
  445. // Create analytics service with the initial searcher
  446. globalAnalytics = analytics.NewService(globalSearcher)
  447. isHealthy := globalSearcher.IsHealthy()
  448. isRunning := globalSearcher.IsRunning()
  449. logger.Infof("Initial searcher created successfully, isHealthy: %v, isRunning: %v", isHealthy, isRunning)
  450. return
  451. }
  452. // For subsequent updates, use hot-swap through IndexAlias
  453. // This follows Bleve best practices for zero-downtime index updates
  454. if ds, ok := globalSearcher.(*searcher.DistributedSearcher); ok {
  455. oldShards := ds.GetShards()
  456. logger.Debugf("updateSearcherShardsLocked: About to call SwapShards...")
  457. // Perform atomic shard swap using IndexAlias
  458. if err := ds.SwapShards(newShards); err != nil {
  459. logger.Errorf("Failed to swap shards atomically: %v", err)
  460. return
  461. }
  462. logger.Debugf("updateSearcherShardsLocked: SwapShards completed successfully")
  463. logger.Infof("Successfully swapped %d old shards with %d new shards using IndexAlias",
  464. len(oldShards), len(newShards))
  465. // Verify searcher health after swap
  466. isHealthy := globalSearcher.IsHealthy()
  467. isRunning := globalSearcher.IsRunning()
  468. logger.Infof("Post-swap searcher status: isHealthy: %v, isRunning: %v", isHealthy, isRunning)
  469. // Note: We do NOT recreate the analytics service here since the searcher interface remains the same
  470. // The CardinalityCounter will automatically use the new shards through the same IndexAlias
  471. } else {
  472. logger.Warn("globalSearcher is not a DistributedSearcher, cannot perform hot-swap")
  473. }
  474. }
  475. // StopModernServices stops all running modern services
  476. func StopModernServices() {
  477. servicesMutex.Lock()
  478. defer servicesMutex.Unlock()
  479. if !servicesInitialized {
  480. logger.Debug("Modern nginx log services not initialized, nothing to stop")
  481. return
  482. }
  483. if isShuttingDown {
  484. logger.Debug("Modern nginx log services already shutting down")
  485. return
  486. }
  487. logger.Debug("Stopping modern nginx log services...")
  488. isShuttingDown = true
  489. // Cancel the service context to trigger graceful shutdown
  490. if shutdownCancel != nil {
  491. shutdownCancel()
  492. // Wait a bit for graceful shutdown
  493. time.Sleep(500 * time.Millisecond)
  494. }
  495. // Stop all services
  496. if globalIndexer != nil {
  497. if err := globalIndexer.Stop(); err != nil {
  498. logger.Errorf("Failed to stop indexer: %v", err)
  499. }
  500. globalIndexer = nil
  501. }
  502. if globalAnalytics != nil {
  503. if err := globalAnalytics.Stop(); err != nil {
  504. logger.Errorf("Failed to stop analytics service: %v", err)
  505. }
  506. globalAnalytics = nil
  507. }
  508. if globalSearcher != nil {
  509. if err := globalSearcher.Stop(); err != nil {
  510. logger.Errorf("Failed to stop searcher: %v", err)
  511. }
  512. globalSearcher = nil
  513. }
  514. // Reset state
  515. globalLogFileManager = nil
  516. servicesInitialized = false
  517. shutdownCancel = nil
  518. isShuttingDown = false
  519. logger.Debug("Modern nginx log services stopped")
  520. }
  521. // DestroyAllIndexes completely removes all indexed data from disk.
  522. func DestroyAllIndexes(ctx context.Context) error {
  523. servicesMutex.RLock()
  524. defer servicesMutex.RUnlock()
  525. if !servicesInitialized || globalIndexer == nil {
  526. logger.Debug("Cannot destroy indexes, services not initialized.")
  527. return fmt.Errorf("services not initialized")
  528. }
  529. return globalIndexer.DestroyAllIndexes(ctx)
  530. }