Skip to content

Commit 593dc5b

Browse files
committed
add doc
1 parent ce8b8ae commit 593dc5b

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

README.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,77 @@ smartdns
1717
目前我们正在实现和小流量测试go实现的smartdns,能够达到1wQPS以上,后续测试稳定后会开源出来,大家敬请期待:)
1818

1919
## 原理
20-
TODO
20+
21+
smartdns响应dns请求的处理流程如下:
22+
23+
![dns请求处理流程](http://noops.me/wp-content/uploads/2013/08/dns%E8%AF%B7%E6%B1%82%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B.png)
24+
25+
IPPool类的初始化和该类中FindIP方法进行解析处理是smartdns中最关键的两个要素,这两个要素在下面详细介绍。其他的特性比如继承twisted中dns相关类并重写处理dns请求的方法、升级twisted代码支持解析和处理edns请求等大家可以通过代码了解。edns知识可以猛戳这里:<a href="http://noops.me/?p=653" title="DNS support edns-client-subnet" target="_blank">DNS support edns-client-subnet</a>
26+
27+
#### IPPool初始化
28+
29+
![IPPool初始化流程](http://noops.me/wp-content/uploads/2013/08/ippool%E5%88%9D%E5%A7%8B%E5%8C%96.png)
30+
31+
ip.csv内容格式如下:
32+
``200000001, 200000010,中国,陕西,西安,电信``
33+
34+
其中各个字段含义分别为 ``IP段起始,IP段截止,IP段所属国家,IP段所属省份,IP段所属城市,IP段所属ISP``
35+
36+
a.yaml配置文件格式:
37+
<pre class="lang:default decode:true">test.test.com:
38+
ttl: 3600
39+
default: 5.5.5.5 2.2.2.2
40+
中国,广东,,联通: 1.1.1.1 3.3.3.1
41+
中国,广东,,电信: 1.1.1.2 3.3.3.2</pre>
42+
43+
配置中地域信息的key包括四个字段,分别带有不同的权重:
44+
- 国家: 8
45+
- 省份: 4
46+
- 城市: 2
47+
- 运营商: 1
48+
49+
初始化阶段,会生成一个名为iphash的dict,具体数据结构如下图:
50+
51+
<a href="http://noops.me/wp-content/uploads/2013/08/iphash数据结构.png"><img src="http://noops.me/wp-content/uploads/2013/08/iphash数据结构-300x135.png" alt="iphash数据结构" width="300" height="135" class="alignnone size-medium wp-image-784" /></a>
52+
53+
其中,iphash的key为ip.csv每一条记录的起始IP,value为一个list,list长度为6,list前5个字段分别为以该key为起始IP记录的IP段截止、IP段所属国家、IP段所属省份、IP段所属城市、IP段所属ISP,第六个字段是一个hash,key为a.yaml里面配置的域名,value为长度为2的list,iphash[IP段起始][6][域名1][0]为域名1在该IP段的最优解析,iphash[IP段起始][6][域名1][1]为该最优解析的总权值,该总权值暂时只做参考。
54+
55+
iphash初始化过程中最关键的是iphash[IP段起始][6][域名1]的最优解析的计算,最简单直接的方式是直接遍历域名1的所有调度配置,挑选出满足条件且总权值最高的解析,即为最优解析。这种方式记录整个iphash的时间复杂度为O(xyz),x为ip.csv记录数,y为域名总数量,z为各个域名的调度配置数。为了优化启动速度,优化了寻找最优解析的方法:事先将每个域名调度配置生成一颗树,这棵树是用dict模拟出来的,这样需要最优解的时候就不需要遍历所有调度配置,而是最多检索15次即可找到最优,即时间复杂度为O(15xy),具体实现参考IPPool的LoadRecord和JoinIP两个方法。
56+
57+
有了初始化后的iphash数据结构之后,每次请求处理的时候,只需要定位请求IP处在哪个IP段,找到IP段起始IP,然后从iphash中取出最优解析,取出最优解析的过程是O(1)的。具体流程如下:
58+
59+
<a href="http://noops.me/wp-content/uploads/2013/08/ippool的findip方式.png"><img src="http://noops.me/wp-content/uploads/2013/08/ippool的findip方式-111x300.png" alt="ippool的findip方式" width="111" height="300" class="alignnone size-medium wp-image-799" /></a>
60+
61+
## 代码
62+
63+
github: https://github.com/xiaomi-sa/smartdns
2164

2265
## 安装
23-
TODO
66+
67+
依赖:
68+
69+
python 2.6或者2.7
70+
Twisted 12.2.0
71+
zope.interface 4.0.1
72+
73+
安装:
74+
75+
git clone smartdns到本地路径,进入script目录,执行install_smartdns.sh即可将smartdns安装在本地,同时python环境和相关的依赖都是使用virtualenv来进行管理,不会对系统环境造成影响。
76+
77+
启动:
78+
79+
进入smartdns的bin路径下,执行sh run_dns.sh即可启动smartdns
2480

2581
## 测试
26-
TODO
82+
83+
本地测试 dig test.test.com @127.0.0.1
84+
85+
或者将搭建的smartdns加到测试域名的ns中进行测试。
2786

2887
## 支持
2988

3089
mail: fangshaosen@xiaomi.com
3190

3291
github: jerryfang8
92+
93+
EDNS相关请参考:<a href="http://noops.me/?p=653" title="DNS support edns-client-subnet" target="_blank">DNS support edns-client-subnet</a>

bin/ippool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ def LoadIP(self):
7777
else:
7878
#ipstart, ipend, country, province, city, sp, domain ip hash
7979
self.iphash[ipstart] = [ipstart, ipend, country, province, city, sp, {}]
80-
# fangshaosen@xiaomi.com 最好合并后再计算
80+
# 最好合并后再计算
8181
self.JoinIP(ipstart)
8282
f.close()
8383
logger.warning("after load: %s" % ( time.time() ) )
8484
self.iplist.sort()
8585
logger.warning("after sort: %s" % ( time.time() ) )
8686

87-
# fangshaosen@xiaomi.com 重写LoadRecord和JoinIP,提升启动效率
87+
# 重写LoadRecord和JoinIP,提升启动效率
8888
def LoadRecord(self):
8989
Add = [8, 4, 2, 1]
9090
f = open(self.recordfile, 'r')

0 commit comments

Comments
 (0)