Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve configuration reload #378

Merged
merged 3 commits into from
Jan 12, 2025
Merged

Improve configuration reload #378

merged 3 commits into from
Jan 12, 2025

Conversation

tphakala
Copy link
Owner

BirdNET model setting changes no longer require application restart

…ctionality

- Added validateModelAndLabels method to ensure the number of labels matches the model's output size, improving error handling and debugging.
- Implemented ReloadModel method to safely reload the BirdNET model and labels, including mutex locking for thread safety and cleanup of old interpreters upon failure.
- Removed unused SpeciesListUpdated timestamp field to streamline the BirdNET struct.
- Improved error messages for model and label loading failures, enhancing clarity during debugging.
- Updated RealtimeAnalysis to accept a notification channel for handling status updates.
- Enhanced startControlMonitor to send notifications for range filter rebuild and model reload events, improving user feedback during real-time analysis.
- Modified HTTP server and handlers to include the notification channel, ensuring consistent notification management across components.
…ions

- Implemented checks to detect changes in BirdNET settings and send notifications for model reloads.
- Added notifications for range filter rebuilds when related settings change.
- Improved error handling and logging during settings updates, ensuring better user feedback and traceability.
- Introduced functions to check for specific changes in BirdNET settings, enhancing configuration management.
Copy link
Contributor

coderabbitai bot commented Jan 12, 2025

Walkthrough

The pull request introduces a comprehensive notification mechanism across multiple components of the application. The changes focus on adding a new notificationChan to handle real-time feedback for various operations, such as model reloading, settings changes, and control signals. The implementation spans several files, including realtime.go, birdnet.go, handlers.go, settings.go, and server.go, enhancing the system's ability to communicate operational status and changes dynamically.

Changes

File Change Summary
cmd/realtime/realtime.go Added notificationChan to RunE function, updated imports
internal/analysis/realtime.go Modified RealtimeAnalysis and startControlMonitor to support notification channel
internal/birdnet/birdnet.go Added validateModelAndLabels() and ReloadModel() methods, removed SpeciesListUpdated field
internal/httpcontroller/handlers/handlers.go Updated New() constructor to include notificationChan
internal/httpcontroller/handlers/settings.go Added birdnetSettingsChanged() function, enhanced settings change detection
internal/httpcontroller/server.go Added notificationChan to Server struct

Sequence Diagram

sequenceDiagram
    participant Server
    participant Handlers
    participant Analysis
    participant BirdNET
    
    Server->>Handlers: Initialize with notificationChan
    Handlers->>Analysis: Pass notificationChan
    Analysis->>BirdNET: Trigger model operations
    BirdNET-->>Analysis: Send notifications on success/failure
    Analysis-->>Handlers: Forward notifications
    Handlers-->>Server: Process notifications
Loading

Poem

🐰 Hop, hop, through code so bright,
Notifications dancing left and right!
Signals bounce with rabbit glee,
Reloading models, setting us free 🎉
A tech ballet of channels true! 🚀

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (3)
internal/httpcontroller/handlers/settings.go (1)

588-597: Consider combining settings change checks for efficiency

In the rangeFilterSettingsChanged function, you are individually checking for changes in species include/exclude lists, range filter settings, and latitude/longitude. To improve maintainability and efficiency, consider combining these checks into a single comparison of relevant substructures.

For example, you could use:

return !reflect.DeepEqual(oldSettings.Realtime.Species, currentSettings.Realtime.Species) ||
       !reflect.DeepEqual(oldSettings.BirdNET.RangeFilter, currentSettings.BirdNET.RangeFilter) ||
       oldSettings.BirdNET.Latitude != currentSettings.BirdNET.Latitude ||
       oldSettings.BirdNET.Longitude != currentSettings.BirdNET.Longitude
internal/httpcontroller/server.go (1)

37-37: Consider documenting the purpose of the notification channel.

Add a comment explaining the role of notificationChan in handling real-time feedback for configuration changes.

+	// notificationChan handles real-time feedback for configuration changes
 	notificationChan  chan handlers.Notification
internal/analysis/realtime.go (1)

367-379: Refactor duplicate range filter rebuild logic.

The range filter rebuild logic after model reload duplicates the earlier implementation. Consider extracting this into a helper function.

+func rebuildRangeFilter(notificationChan chan handlers.Notification) error {
+	if err := birdnet.BuildRangeFilter(bn); err != nil {
+		log.Printf("\033[31m❌ Error handling range filter rebuild: %v\033[0m", err)
+		select {
+		case notificationChan <- handlers.Notification{
+			Message: fmt.Sprintf("Failed to rebuild range filter: %v", err),
+			Type:    "error",
+		}:
+		case <-time.After(time.Second):
+			log.Printf("Warning: Notification channel blocked")
+		}
+		return err
+	}
+	log.Printf("\033[32m✅ Range filter rebuilt successfully\033[0m")
+	select {
+	case notificationChan <- handlers.Notification{
+		Message: "Range filter rebuilt successfully",
+		Type:    "success",
+	}:
+	case <-time.After(time.Second):
+		log.Printf("Warning: Notification channel blocked")
+	}
+	return nil
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 460276d and 98d17d5.

📒 Files selected for processing (6)
  • cmd/realtime/realtime.go (2 hunks)
  • internal/analysis/realtime.go (5 hunks)
  • internal/birdnet/birdnet.go (1 hunks)
  • internal/httpcontroller/handlers/handlers.go (3 hunks)
  • internal/httpcontroller/handlers/settings.go (2 hunks)
  • internal/httpcontroller/server.go (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test-docker-image
🔇 Additional comments (4)
internal/httpcontroller/handlers/settings.go (1)

601-626: Ensure all relevant BirdNET settings are monitored for changes

The birdnetSettingsChanged function checks for changes in several BirdNET settings. Please verify that all settings which require a model reload upon change are included. If any setting is omitted, changes to it may not trigger the necessary reload, potentially causing inconsistencies during runtime.

cmd/realtime/realtime.go (1)

21-22: Initialization of notificationChan for real-time analysis looks good

The addition of the notificationChan channel and its integration with the RealtimeAnalysis function enhances the notification mechanism during real-time audio analysis. This change is implemented correctly.

internal/httpcontroller/server.go (1)

58-58: Review the buffer size and add channel cleanup.

The buffer size of 10 might be too small if multiple operations trigger notifications simultaneously. Also, ensure proper cleanup of the channel during shutdown.

Consider:

  1. Increasing the buffer size to handle concurrent notifications.
  2. Adding cleanup logic in the shutdown process.
internal/httpcontroller/handlers/handlers.go (1)

33-33: LGTM! Clean integration of notification channel.

The changes properly integrate the notification channel into the Handlers struct and its constructor.

Also applies to: 76-76, 95-95

Comment on lines +320 to +322
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m",
modelOutputSize, len(bn.Settings.BirdNET.Labels))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid including ANSI color codes and emojis in error messages

Including ANSI color codes and emojis in error messages can lead to unreadable logs or issues in environments that do not support ANSI escape sequences. It is recommended to keep error messages plain to ensure they are properly displayed and logged across different systems.

Apply this diff to remove color codes and emojis from error messages:

-		return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m",
+		return fmt.Errorf("label count mismatch: model expects %d classes but label file has %d labels",
 			modelOutputSize, len(bn.Settings.BirdNET.Labels))

...

-		return fmt.Errorf("\033[31m❌ failed to reload model: %w\033[0m", err)
+		return fmt.Errorf("failed to reload model: %w", err)

...

-		return fmt.Errorf("\033[31m❌ failed to reload meta model: %w\033[0m", err)
+		return fmt.Errorf("failed to reload meta model: %w", err)

...

-		return fmt.Errorf("\033[31m❌ failed to reload labels: %w\033[0m", err)
+		return fmt.Errorf("failed to reload labels: %w", err)

...

-		return fmt.Errorf("\033[31m❌ model validation failed: %w\033[0m", err)
+		return fmt.Errorf("model validation failed: %w", err)

Also applies to: 341-341, 354-354, 370-370, 386-386

Comment on lines +308 to +326
func (bn *BirdNET) validateModelAndLabels() error {
// Get the output tensor to check its dimensions
outputTensor := bn.AnalysisInterpreter.GetOutputTensor(0)
if outputTensor == nil {
return fmt.Errorf("cannot get output tensor")
}

// Get the number of classes from the model's output tensor
modelOutputSize := outputTensor.Dim(outputTensor.NumDims() - 1)

// Compare with the number of labels
if len(bn.Settings.BirdNET.Labels) != modelOutputSize {
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m",
modelOutputSize, len(bn.Settings.BirdNET.Labels))
}

bn.Debug("\033[32m✅ Model validation successful: %d labels match model output size\033[0m", modelOutputSize)
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure NumDims() is greater than zero to prevent potential panic

In the validateModelAndLabels method, calling outputTensor.Dim(outputTensor.NumDims() - 1) without checking if NumDims() is greater than zero could cause a panic if the tensor has zero dimensions. Please add a check to ensure NumDims() is greater than zero before accessing tensor dimensions.

Apply this diff to fix the issue:

 func (bn *BirdNET) validateModelAndLabels() error {
 	// Get the output tensor to check its dimensions
 	outputTensor := bn.AnalysisInterpreter.GetOutputTensor(0)
 	if outputTensor == nil {
 		return fmt.Errorf("cannot get output tensor")
 	}

+	if outputTensor.NumDims() == 0 {
+		return fmt.Errorf("output tensor has no dimensions")
+	}

 	// Get the number of classes from the model's output tensor
 	modelOutputSize := outputTensor.Dim(outputTensor.NumDims() - 1)

 	// Compare with the number of labels
 	if len(bn.Settings.BirdNET.Labels) != modelOutputSize {
 		return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m",
 			modelOutputSize, len(bn.Settings.BirdNET.Labels))
 	}

 	bn.Debug("\033[32m✅ Model validation successful: %d labels match model output size\033[0m", modelOutputSize)
 	return nil
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (bn *BirdNET) validateModelAndLabels() error {
// Get the output tensor to check its dimensions
outputTensor := bn.AnalysisInterpreter.GetOutputTensor(0)
if outputTensor == nil {
return fmt.Errorf("cannot get output tensor")
}
// Get the number of classes from the model's output tensor
modelOutputSize := outputTensor.Dim(outputTensor.NumDims() - 1)
// Compare with the number of labels
if len(bn.Settings.BirdNET.Labels) != modelOutputSize {
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m",
modelOutputSize, len(bn.Settings.BirdNET.Labels))
}
bn.Debug("\033[32m✅ Model validation successful: %d labels match model output size\033[0m", modelOutputSize)
return nil
}
func (bn *BirdNET) validateModelAndLabels() error {
// Get the output tensor to check its dimensions
outputTensor := bn.AnalysisInterpreter.GetOutputTensor(0)
if outputTensor == nil {
return fmt.Errorf("cannot get output tensor")
}
if outputTensor.NumDims() == 0 {
return fmt.Errorf("output tensor has no dimensions")
}
// Get the number of classes from the model's output tensor
modelOutputSize := outputTensor.Dim(outputTensor.NumDims() - 1)
// Compare with the number of labels
if len(bn.Settings.BirdNET.Labels) != modelOutputSize {
return fmt.Errorf("\033[31m❌ label count mismatch: model expects %d classes but label file has %d labels\033[0m",
modelOutputSize, len(bn.Settings.BirdNET.Labels))
}
bn.Debug("\033[32m✅ Model validation successful: %d labels match model output size\033[0m", modelOutputSize)
return nil
}

Comment on lines +342 to +345
notificationChan <- handlers.Notification{
Message: fmt.Sprintf("Failed to rebuild range filter: %v", err),
Type: "error",
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider error handling for channel operations.

The notification channel writes could block if the channel is full. Consider adding a select statement with a timeout.

-						notificationChan <- handlers.Notification{
-							Message: fmt.Sprintf("Failed to rebuild range filter: %v", err),
-							Type:    "error",
-						}
+						select {
+						case notificationChan <- handlers.Notification{
+							Message: fmt.Sprintf("Failed to rebuild range filter: %v", err),
+							Type:    "error",
+						}:
+						case <-time.After(time.Second):
+							log.Printf("Warning: Notification channel blocked")
+						}

Also applies to: 348-351

Comment on lines +354 to +366
if err := bn.ReloadModel(); err != nil {
log.Printf("\033[31m❌ Error reloading BirdNET model: %v\033[0m", err)
notificationChan <- handlers.Notification{
Message: fmt.Sprintf("Failed to reload BirdNET model: %v", err),
Type: "error",
}
} else {
log.Printf("\033[32m✅ BirdNET model reloaded successfully\033[0m")
notificationChan <- handlers.Notification{
Message: "BirdNET model reloaded successfully",
Type: "success",
}
// Rebuild range filter after model reload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add timeout handling for model reload operation.

The model reload operation could potentially be time-consuming. Consider adding a timeout to prevent blocking.

+					ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+					defer cancel()
+					
+					errChan := make(chan error, 1)
+					go func() {
+						errChan <- bn.ReloadModel()
+					}()
+
+					select {
+					case err := <-errChan:
						if err != nil {
							// ... error handling ...
						} else {
							// ... success handling ...
						}
+					case <-ctx.Done():
+						notificationChan <- handlers.Notification{
+							Message: "Model reload timed out after 30 seconds",
+							Type:    "error",
+						}
+					}

Committable suggestion skipped: line range outside the PR's diff.

@tphakala tphakala merged commit b13c675 into main Jan 12, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant