TantivyEx.Distributed.OTP (TantivyEx v0.4.1)
View SourceOTP-based distributed search API for TantivyEx.
This module provides a clean, OTP-native interface for distributed search operations, leveraging Elixir's supervision trees, GenServers, and fault tolerance mechanisms instead of native coordination.
Features
- Supervisor-based fault tolerance
- GenServer per search node
- Registry-based node discovery
- Task supervision for concurrent operations
- Built-in health monitoring
- Automatic failover and recovery
Example Usage
# Start the distributed search system
{:ok, _pid} = TantivyEx.Distributed.OTP.start_link()
# Add search nodes
:ok = TantivyEx.Distributed.OTP.add_node("node1", "local://index1", 1.0)
:ok = TantivyEx.Distributed.OTP.add_node("node2", "local://index2", 1.5)
# Configure behavior
:ok = TantivyEx.Distributed.OTP.configure(%{
timeout_ms: 5000,
merge_strategy: :score_desc
})
# Perform distributed search
{:ok, results} = TantivyEx.Distributed.OTP.search("query text", 10, 0)
Summary
Functions
Add a search node to the distributed cluster.
Add multiple nodes at once.
Configure the distributed search behavior.
Get list of active node IDs.
Get cluster statistics and health information.
Get detailed statistics for a specific node.
Remove a search node from the cluster.
Check if the distributed search system is running.
Perform a distributed search across the cluster.
Set a node's active/inactive status.
Perform a simple distributed search with sensible defaults.
Start the distributed search system.
Stop the distributed search system.
Types
@type config() :: %{ timeout_ms: non_neg_integer(), max_retries: non_neg_integer(), merge_strategy: merge_strategy(), load_balancing: load_balancing_strategy(), health_check_interval: non_neg_integer() }
@type load_balancing_strategy() ::
:round_robin | :weighted_round_robin | :least_connections | :health_based
@type merge_strategy() :: :score_desc | :score_asc | :node_order | :round_robin
Functions
Add a search node to the distributed cluster.
Parameters
node_id- Unique identifier for the nodeendpoint- Endpoint specification (e.g., "local://path", "http://host:port")weight- Weight for load balancing (must be > 0.0)opts- Additional options
Examples
:ok = TantivyEx.Distributed.OTP.add_node("node1", "local://index1", 1.0)
:ok = TantivyEx.Distributed.OTP.add_node("node2", "http://remote:9200", 2.0)
Add multiple nodes at once.
Parameters
nodes- List of{node_id, endpoint, weight}tuplesopts- Additional options
Examples
nodes = [
{"node1", "local://index1", 1.0},
{"node2", "local://index2", 1.5},
{"node3", "http://remote:9200", 2.0}
]
:ok = TantivyEx.Distributed.OTP.add_nodes(nodes)
Configure the distributed search behavior.
Configuration Options
:timeout_ms- Request timeout in milliseconds (default: 5000):max_retries- Maximum retries for failed requests (default: 3):merge_strategy- Result merging strategy (default: :score_desc):load_balancing- Load balancing strategy (default: :weighted_round_robin):health_check_interval- Health check interval in ms (default: 30000)
Examples
:ok = TantivyEx.Distributed.OTP.configure(%{
timeout_ms: 10_000,
merge_strategy: :score_desc,
health_check_interval: 60_000
})
Get list of active node IDs.
Examples
{:ok, nodes} = TantivyEx.Distributed.OTP.get_active_nodes()
# => {:ok, ["node1", "node2"]}
Get cluster statistics and health information.
Returns
A map containing:
:total_nodes- Total number of configured nodes:active_nodes- Number of currently active nodes:inactive_nodes- Number of inactive nodes:config- Current configuration:cluster_stats- Performance statistics
Examples
{:ok, stats} = TantivyEx.Distributed.OTP.get_cluster_stats()
IO.puts "Active nodes: #{stats.active_nodes}/#{stats.total_nodes}"
Get detailed statistics for a specific node.
Parameters
node_id- Unique identifier for the nodeopts- Additional options
Examples
{:ok, stats} = TantivyEx.Distributed.OTP.get_node_stats("node1")
Remove a search node from the cluster.
Parameters
node_id- Unique identifier for the node to removeopts- Additional options
Examples
:ok = TantivyEx.Distributed.OTP.remove_node("node1")
Check if the distributed search system is running.
Examples
true = TantivyEx.Distributed.OTP.running?()
@spec search( String.t() | reference(), non_neg_integer(), non_neg_integer(), keyword() ) :: {:ok, map()} | {:error, term()}
Perform a distributed search across the cluster.
Parameters
query- Search query (string or compiled query reference)limit- Maximum number of results to returnoffset- Number of results to skip for paginationopts- Additional options
Examples
{:ok, results} = TantivyEx.Distributed.OTP.search("rust programming", 20, 0)
# With options
{:ok, results} = TantivyEx.Distributed.OTP.search(
"rust programming",
20,
0,
coordinator: MyApp.Coordinator
)
Set a node's active/inactive status.
Inactive nodes will not receive search requests.
Parameters
node_id- Unique identifier for the nodeactive- Whether the node should be activeopts- Additional options
Examples
:ok = TantivyEx.Distributed.OTP.set_node_status("node1", false)
:ok = TantivyEx.Distributed.OTP.set_node_status("node1", true)
Perform a simple distributed search with sensible defaults.
This is a convenience function for basic searches.
Examples
{:ok, results} = TantivyEx.Distributed.OTP.simple_search("rust programming")
Start the distributed search system.
This starts the supervision tree and all necessary processes.
Options
:name- Name for the main supervisor (default: TantivyEx.Distributed.Supervisor):coordinator_name- Name for the coordinator GenServer:registry_name- Name for the node registry
Examples
{:ok, pid} = TantivyEx.Distributed.OTP.start_link()
{:ok, pid} = TantivyEx.Distributed.OTP.start_link(name: MyApp.DistributedSearch)
@spec stop(keyword()) :: :ok
Stop the distributed search system.
This will gracefully shut down all nodes and supervisors.
Examples
:ok = TantivyEx.Distributed.OTP.stop()