22

解密sphinx分布式索引

 5 years ago
source link: https://www.linuxprobe.com/sphinx-introduction.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

sphinx分布式索引原理:
当searchd收到一个对分布式索引的查询时,它做如下操作:连接到远程代理执行查询(在远程代理执行搜索的同时)对本地索引进行查询接收来自远程代理的搜索结果将所有结果合并,删除重复项将合并后的结果返回给客户端。在应用程序看来,普通索引和分布式索引完全没有区别。任一个searchd实例可以同时做为主控端(master,对搜索结果做聚合)和从属端(只做本地搜索)。这有如下几点好处:
1. 集群中的每台机器都可以做为主控端来搜索整个集群,搜索请求可以在主控端之间获得负载平衡,相当于实现了一种HA(high availability,高可用性),可以应对某个节点失效的情况。
2. 如果在单台多CPU或多核机器上使用,一个做为代理对本机进行搜索的searchd实例就可以利用到全部的CPU或者核。更好的HA支持已在计划之中,到时将允许指定哪些代理之间互相备份、有效性检查、跟踪运行中的代理、对检索请求进行负载均衡,等等。
sphinx分布式索引配置:

index dist1{ # local index to be searched # there can be many local indexes configured local = test1 local = test1stemmed # remote agent # multiple remote agents may be specified # syntax for TCP connections is 'hostname:port:index1,[index2[,...]]' # syntax for local UNIX connections is '/path/to/socket:index1,[index2[,...]]' agent = localhost:9313:remote1 agent = localhost:9314:remote2,remote3 # agent = /var/run/searchd.sock:remote4 # blackhole remote agent, for debugging/testing # network errors and search results will be ignored # agent_blackhole = testbox:9312:testindex1,testindex2 # remote agent connection timeout, milliseconds # optional, default is 1000 ms, ie. 1 sec agent_connect_timeout = 1000 # remote agent query timeout, milliseconds # optional, default is 3000 ms, ie. 3 sec agent_query_timeout = 3000}

原理和配置还是比较简单的,对于索引文件过大的情况,分布式索引非常有用。但是碰到搜索量非常大的时候,分布式索引就显得力不从心了。这个时候就需要做索引复制了。索引复制同步的问题一般采用的方案有两种。
第一种方案是主从同步。有一个主搜索服务器和多个从搜索服务器。在主搜索服务器上生成的增量索引会同步从索引服务器。从而达到主从同步。为了避免网络或者其他情况导致的增量索引不同步的情况,需要定期在一个阶段把主从服务器的主索引同步一次。新增加搜索服务器的话需要在增量索引生成的间隔数据去拷贝其他搜索服务器的主索引。
第二种方案是每个搜索服务器根据增量数据去生成增量索引。通过表来记录不同的搜索服务器是否生成增量索引。来达到同步的目的。新增加搜索服务器的话,就需要去比历史上所有的增量数据全部生成一遍。
这两种策略都是可行的。增量数据在一定时间段量很大的话建议用第一种方案,否则用第二种方案。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK