一致性哈希算法设计思路:
运行结果:
[email protected]:/data/code/cwork/chash:./chash ----vsrvname = 192.168.100.1:3306-vm1,hkey =126515------ ----vsrvname = 192.168.100.1:3306-vm2,hkey =77752------ ----vsrvname = 192.168.100.1:3306-vm3,hkey =47584------ ----vsrvname = 192.168.200.2:3214-vm1,hkey =32166------ ----vsrvname = 192.168.200.2:3214-vm2,hkey =79176------ ----vsrvname = 192.168.200.2:3214-vm3,hkey =115888------ ----vsrvname = 192.168.300.15:8745-vm1,hkey =30184------ ----vsrvname = 192.168.300.15:8745-vm2,hkey =26975------ ----vsrvname = 192.168.300.15:8745-vm3,hkey =97282------ *************************************** ltmgr->count = 3,ltmgr->head = 0x7dd380 cur = 0x7dd380,srvname = 192.168.300.15:8745,next = 0x7dd200 cur = 0x7dd200,srvname = 192.168.200.2:3214,next = 0x7dd050 cur = 0x7dd050,srvname = 192.168.100.1:3306,next = (nil) exec = 0 *************************************** ltmgr->count = 2,ltmgr->head = 0x7dd380 cur = 0x7dd380,srvname = 192.168.300.15:8745,next = 0x7dd200 cur = 0x7dd200,srvname = 192.168.200.2:3214,next = (nil) trmgr->root = 0x7dd120,count = 9,min=26975,max=126515 ---------------------print init tree------------------------- 77752[192.168.100.1:3306-vm2],height =3 :visitor =0 32166[192.168.200.2:3214-vm1],height =2 :visitor =0 30184[192.168.300.15:8745-vm1],height =1 :visitor =0 26975[192.168.300.15:8745-vm2],height =0 :visitor =0 47584[192.168.100.1:3306-vm3],height =0 :visitor =0 115888[192.168.200.2:3214-vm3],height =2 :visitor =0 79176[192.168.200.2:3214-vm2],height =1 :visitor =0 97282[192.168.300.15:8745-vm3],height =0 :visitor =0 126515[192.168.100.1:3306-vm1],height =0 :visitor =0 --------------------------------------begin request------------------------ request : 202.4.104.155:8080(key =41901) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 183.60.98.67:80(key =71097) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 190.13.131.165:3128(key =45223) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 182.140.132.108:8888(key =56902) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 114.32.219.221:8088(key =27008) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 210.73.112.100:80(key =27011) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 88.132.82.236:8088(key =28968) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 113.105.224.78:80(key =49518) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 210.14.152.91:8080(key =83142) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 120.203.214.187:80(key =30953) ---------> 192.168.300.15:8745-vm1(key = 30184) request : 111.13.2.138:80(key =85311) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 64.31.22.131:7808(key =85604) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 111.11.228.9:80(key =96613) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 210.75.14.157:80(key =50488) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 117.156.8.72:80(key =27003) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 222.216.109.114:3128(key =56204) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 210.75.14.158:80(key =29017) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 111.11.152.135:80(key =114011) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 199.200.120.37:7808(key =126500) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 183.224.1.30:80(key =54731) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 188.225.35.49:3128(key =66637) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 91.232.12.90:3128(key =27018) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 103.244.80.40:80(key =126287) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 111.13.2.137:80(key =27010) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 117.139.39.190:8123(key =27012) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 41.231.53.40:3128(key =84711) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 212.156.157.86:8080(key =27018) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 220.178.101.170:80(key =28227) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 115.28.46.123:808(key =118845) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 210.14.152.92:8080(key =112153) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 92.51.245.87:3128(key =107047) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 60.206.239.195:8118(key =118807) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 111.13.109.52:80(key =54502) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 149.255.255.242:80(key =126466) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 111.1.44.24:80(key =114168) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 97.77.104.22:80(key =62175) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 113.105.93.79:80(key =55943) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 115.29.11.46:3128(key =55234) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 163.23.70.129:3128(key =56098) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 111.13.2.139:80(key =123263) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 111.11.184.85:80(key =26996) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 202.108.50.74:80(key =69758) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 218.108.168.70:80(key =42775) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 114.112.192.195:3128(key =125309) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 218.108.168.69:80(key =86087) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 111.13.2.140:80(key =95859) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 174.70.0.100:8080(key =107478) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 202.4.104.157:8080(key =74480) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 117.79.64.84:80(key =26987) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 123.162.191.84:9999(key =27002) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 142.0.42.8:443(key =113451) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 125.39.66.75:80(key =119530) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 121.40.162.173:8088(key =62648) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 122.155.165.128:3128(key =27016) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 222.246.232.55:80(key =45473) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 42.62.61.245:80(key =26985) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 140.206.86.68:8080(key =73994) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 113.57.230.49:81(key =65416) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 123.177.20.220:80(key =65515) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 113.57.252.104:80(key =98323) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 175.180.199.11:8088(key =52964) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 60.55.43.74:80(key =26988) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 211.71.30.97:3128(key =27001) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 200.46.94.202:3128(key =52181) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 171.38.221.240:8088(key =55907) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 111.13.2.142:80(key =27016) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 218.108.232.99:80(key =124892) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 111.205.122.222:80(key =103561) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 113.107.57.76:80(key =27002) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 223.68.6.10:8000(key =31462) ---------> 192.168.300.15:8745-vm1(key = 30184) request : 124.158.9.2:3128(key =57772) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 68.68.97.2:3128(key =60627) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 218.90.174.167:3128(key =125095) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 128.199.225.176:3128(key =90202) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 218.201.38.49:80(key =26997) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 80.250.201.243:8080(key =96893) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 128.199.223.216:3128(key =87096) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 140.207.223.101:80(key =93234) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 118.69.34.208:3128(key =27011) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 82.77.66.122:3130(key =28016) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 202.106.169.228:8080(key =109813) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 219.143.208.193:80(key =73613) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 14.18.16.71:80(key =83111) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 218.207.172.235:80(key =99496) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 218.187.244.109:8088(key =91067) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 111.11.184.80:80(key =52496) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 218.4.236.117:80(key =85129) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 211.162.79.66:80(key =107156) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 111.13.2.136:80(key =95732) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 5.32.67.139:8080(key =69112) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 118.186.239.243:80(key =126503) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 120.202.249.233:80(key =82228) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 61.155.169.11:808(key =31645) ---------> 192.168.300.15:8745-vm1(key = 30184) request : 202.105.247.122:9999(key =92723) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 112.5.240.82:80(key =122794) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 120.203.214.147:84(key =126497) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 61.19.30.198:8080(key =126294) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 121.40.130.208:8080(key =75477) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 111.11.184.83:80(key =97039) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 202.4.116.210:8089(key =26992) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 60.221.252.107:80(key =27005) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 61.50.245.163:8000(key =48793) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 200.166.97.117:3128(key =58126) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 101.64.236.206:18000(key =83878) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 185.49.15.25:7808(key =43227) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 106.1.211.62:8088(key =124067) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 116.236.250.175:1920(key =116817) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 59.151.103.15:80(key =27008) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 202.169.53.14:8010(key =72839) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 111.13.87.173:8081(key =27005) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 58.248.22.41:808(key =62003) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 183.207.228.7:80(key =26987) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 111.11.184.108:80(key =26999) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 188.225.36.244:3128(key =26993) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 117.21.192.10:80(key =60220) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 111.13.2.141:80(key =79495) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 218.65.132.38:80(key =27010) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 180.153.32.11:8080(key =65107) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 124.248.205.7:8128(key =92634) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 218.207.208.55:8080(key =121097) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 111.13.109.54:80(key =105523) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 58.253.238.243:80(key =27005) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 106.37.177.251:3128(key =26977) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 111.13.109.53:80(key =26997) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 58.56.124.192:80(key =26984) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 183.62.172.50:9999(key =48397) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 218.5.74.174:80(key =33760) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 218.207.172.236:80(key =95500) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 121.14.138.50:17210(key =26995) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 168.213.3.103:80(key =82940) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 124.95.163.102:80(key =26993) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 111.13.2.143:80(key =63887) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 113.105.224.77:80(key =121579) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 180.201.15.121:8088(key =81918) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 125.39.66.76:80(key =27018) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 218.28.96.39:3128(key =97874) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 111.11.184.36:80(key =60563) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 183.207.229.10:81(key =34926) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 202.4.104.155:8080(key =41901) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 193.10.98.17:80(key =72196) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 110.13.136.195:3128(key =126509) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 192.140.132.108:8888(key =44490) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 194.32.116.216:8088(key =50265) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 110.73.192.100:80(key =58924) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 88.132.82.236:8088(key =28968) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 193.105.224.78:80(key =126285) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 110.14.152.96:8080(key =99505) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 120.203.114.197:80(key =43578) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 196.13.2.138:80(key =122323) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 14.36.22.136:7808(key =26987) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 196.19.228.9:80(key =92486) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 110.75.14.157:80(key =55966) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 197.156.8.72:80(key =101915) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 222.119.109.194:3128(key =27018) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 110.75.14.158:80(key =90500) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 196.19.152.135:80(key =52644) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 169.200.120.37:7808(key =27005) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 193.224.6.30:80(key =47651) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 198.225.35.49:3128(key =82451) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 96.232.12.90:3128(key =79539) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 103.244.80.40:80(key =126287) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 196.13.2.137:80(key =58059) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 197.139.39.110:8123(key =97221) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 46.236.53.40:3128(key =45310) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 112.156.157.86:8080(key =26999) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 220.178.106.170:80(key =29087) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 195.28.46.123:808(key =87039) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 110.14.152.92:8080(key =81512) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 92.56.245.87:3128(key =101775) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 10.206.239.115:8119(key =71293) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 196.13.109.52:80(key =62131) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 149.255.255.242:80(key =126494) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 196.6.44.24:80(key =102396) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 97.77.104.22:80(key =62175) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 193.105.93.79:80(key =118470) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 195.29.19.46:3128(key =112524) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 193.23.70.129:3128(key =91313) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 196.13.2.139:80(key =107985) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 196.19.194.85:80(key =27809) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 202.108.50.74:80(key =69758) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 119.108.119.70:80(key =100076) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 194.192.112.115:3128(key =26994) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 119.108.119.16:80(key =126481) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 196.13.2.140:80(key =30557) ---------> 192.168.300.15:8745-vm1(key = 30184) request : 174.70.0.100:8080(key =107478) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 202.4.104.157:8080(key =74480) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 197.79.14.84:80(key =89474) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 123.192.119.84:9999(key =68548) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 142.0.42.8:443(key =113451) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 125.39.19.75:80(key =27012) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 116.40.192.173:8088(key =46988) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 122.155.195.128:3128(key =39209) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 222.246.232.55:80(key =45473) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 42.12.19.245:80(key =33355) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 140.206.86.19:8080(key =35523) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 193.57.230.49:86(key =96215) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 123.177.20.220:80(key =65515) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 193.57.252.104:80(key =27024) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 175.190.169.19:8088(key =119696) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 10.55.43.74:80(key =45363) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 119.76.30.97:3128(key =118665) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 200.46.94.202:3128(key =52181) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 176.38.216.240:8088(key =27000) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 196.13.2.142:80(key =26990) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 119.108.232.99:80(key =45797) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 196.205.122.222:80(key =117321) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 193.107.57.76:80(key =48223) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 223.19.6.10:8000(key =26996) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 124.158.9.2:3128(key =57772) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 19.19.97.2:3128(key =56350) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 119.90.174.197:3128(key =104030) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 128.169.225.176:3128(key =59780) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 119.206.38.49:80(key =65249) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 80.250.206.243:8080(key =26989) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 128.169.223.119:3128(key =27002) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 140.207.223.106:80(key =118890) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 119.16.34.208:3128(key =87989) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 82.77.19.122:3130(key =57087) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 202.106.116.228:8080(key =50004) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 116.143.208.113:80(key =44730) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 14.19.19.76:80(key =27017) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 119.207.172.235:80(key =126484) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 119.197.244.109:8088(key =121318) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 196.19.194.80:80(key =101276) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 119.4.236.197:80(key =26981) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 119.192.79.19:80(key =116762) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 196.13.2.136:80(key =40710) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 5.32.17.139:8080(key =96502) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 119.196.239.243:80(key =103775) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 120.202.249.233:80(key =82228) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 19.155.116.19:808(key =124438) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 202.105.247.122:9999(key =92723) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 192.5.240.82:80(key =67014) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 120.203.114.147:84(key =48605) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 19.16.30.119:8080(key =26999) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 116.40.130.208:8080(key =107096) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 196.19.194.83:80(key =26988) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 202.4.196.110:8089(key =97832) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 10.216.252.107:80(key =121474) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 19.50.245.193:8000(key =34666) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 200.196.97.197:3128(key =76384) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 106.14.236.206:19000(key =47931) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 195.49.15.25:7808(key =95179) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 106.6.119.12:8088(key =89084) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 196.236.250.175:1120(key =26996) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 59.156.103.15:80(key =126469) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 202.116.53.14:8010(key =89293) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 196.13.87.173:8086(key =27007) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 58.248.22.46:808(key =41743) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 193.207.228.7:80(key =90701) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 196.19.194.108:80(key =32061) ---------> 192.168.300.15:8745-vm1(key = 30184) request : 198.225.36.244:3128(key =26981) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 197.16.112.10:80(key =27000) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 196.13.2.146:80(key =27009) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 119.15.132.38:80(key =111274) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 190.153.32.19:8080(key =64555) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 124.248.205.7:8128(key =92634) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 119.207.208.55:8080(key =61668) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 196.13.109.54:80(key =81156) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 58.253.238.243:80(key =27003) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 106.37.177.256:3128(key =104005) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 196.13.109.53:80(key =91271) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 58.56.124.112:80(key =56419) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 193.12.172.50:9999(key =74272) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 119.5.74.174:80(key =49609) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 119.207.172.236:80(key =126479) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 116.14.138.50:17110(key =122699) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 119.113.3.103:80(key =26981) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 124.95.193.102:80(key =99154) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 196.13.2.143:80(key =98618) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 193.105.224.77:80(key =53150) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 190.206.15.116:8088(key =107650) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 125.39.19.76:80(key =100328) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 119.28.96.39:3128(key =27021) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 196.19.194.36:80(key =34939) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 193.207.229.10:86(key =125384) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 112.4.104.155:6767(key =99674) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 183.10.98.17:67(key =64832) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 147.13.136.185:3128(key =109533) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 182.140.132.108:4646(key =118991) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 184.32.114.214:6746(key =31179) ---------> 192.168.300.15:8745-vm1(key = 30184) request : 147.73.182.100:67(key =98244) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 46.132.82.236:6746(key =50283) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 183.105.134.78:67(key =81259) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 147.14.152.96:6767(key =39595) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 111.113.114.187:67(key =39443) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 186.13.2.138:67(key =114448) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 14.36.13.136:7678(key =57653) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 186.18.138.9:67(key =63636) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 147.75.14.157:67(key =126486) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 187.156.8.72:67(key =109174) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 132.118.109.184:3128(key =102331) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 147.75.14.158:67(key =63323) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 186.18.152.135:67(key =26988) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 149.110.111.37:7678(key =126508) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 183.134.6.30:67(key =98919) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 188.135.35.49:3128(key =27000) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 96.232.12.90:3128(key =79539) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 103.244.67.40:67(key =78712) ---------> 192.168.100.1:3306-vm2(key = 77752) request : 186.13.2.137:67(key =71705) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 187.139.39.147:8123(key =29598) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 46.236.53.40:3128(key =45310) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 112.156.157.86:6767(key =77475) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 211.178.106.170:67(key =114713) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 185.28.46.123:678(key =27020) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 147.14.152.92:6767(key =37542) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 92.56.245.87:3128(key =101775) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 10.114.239.115:8118(key =111206) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 186.13.109.52:67(key =75117) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 149.255.255.242:67(key =27008) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 186.6.44.24:67(key =52287) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 97.77.104.13:67(key =75659) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 183.105.93.79:67(key =118668) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 185.29.18.46:3128(key =126058) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 183.23.70.129:3128(key =126470) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 186.13.2.139:67(key =26990) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 186.18.184.85:67(key =67417) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 112.108.50.74:67(key =89374) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 118.108.118.70:67(key =26992) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 184.182.112.115:3128(key =115244) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 118.108.118.14:67(key =99066) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 186.13.2.140:67(key =27009) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 174.70.0.100:6767(key =47843) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 112.4.104.157:6767(key =26989) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 187.79.14.84:67(key =29575) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 123.182.118.84:9999(key =27018) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 142.0.42.8:443(key =113451) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 125.39.18.75:67(key =48189) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 114.40.182.173:6746(key =36352) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 113.155.185.128:3128(key =26976) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 132.246.232.55:67(key =126477) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 42.12.18.245:67(key =42280) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 140.114.86.18:6767(key =26983) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 183.57.230.49:86(key =40090) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 123.177.11.211:67(key =27001) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 183.57.252.104:67(key =27003) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 175.180.149.18:6746(key =32290) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 10.55.43.74:67(key =94567) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 118.76.30.97:3128(key =87585) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 110.46.94.112:3128(key =93877) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 176.38.214.240:6746(key =113910) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 186.13.2.142:67(key =62268) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 118.108.232.99:67(key =83997) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 186.115.113.132:67(key =81686) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 183.107.57.76:67(key =97081) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 133.18.6.10:6700(key =47113) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 124.158.9.2:3128(key =57772) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 18.18.97.2:3128(key =107467) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 118.90.174.187:3128(key =95668) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 128.149.135.176:3128(key =50534) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 118.114.38.49:67(key =126477) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 67.250.114.243:6767(key =27010) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 128.149.133.118:3128(key =121573) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 140.117.133.106:67(key =109228) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 118.14.34.118:3128(key =57619) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 82.77.18.113:3130(key =45692) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 112.106.114.138:6767(key =61587) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 114.143.118.113:67(key =32719) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 14.18.18.76:67(key =52185) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 118.117.172.235:67(key =68169) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 118.187.244.109:6746(key =57146) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 186.18.184.67:67(key =50444) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 118.4.236.187:67(key =115845) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 118.182.79.18:67(key =48460) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 186.13.2.136:67(key =36440) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 5.32.17.139:6767(key =45639) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 118.186.239.243:67(key =26979) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 111.112.249.233:67(key =107856) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 18.155.114.18:678(key =26976) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 112.105.247.113:9999(key =75518) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 182.5.240.82:67(key =26980) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 111.113.114.147:84(key =28567) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 18.14.30.118:6767(key =79532) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 114.40.130.118:6767(key =27024) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 186.18.184.83:67(key =27008) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 112.4.186.147:6789(key =100525) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 10.214.252.107:67(key =26985) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 18.50.245.183:6700(key =106367) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 110.186.97.187:3128(key =62441) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 106.14.236.114:18000(key =78466) ---------> 192.168.100.1:3306-vm2(key = 77752) request : 185.49.15.25:7678(key =126488) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 106.6.118.12:6746(key =126497) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 186.236.250.175:1111(key =27426) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 59.156.103.15:67(key =33539) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 112.114.53.14:6710(key =87861) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 186.13.87.173:6786(key =35142) ---------> 192.168.200.2:3214-vm1(key = 32166) request : 58.248.13.46:678(key =27050) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 183.117.138.7:67(key =60815) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 186.18.184.108:67(key =56268) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 188.135.36.244:3128(key =71604) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 187.14.112.10:67(key =56267) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 186.13.2.146:67(key =106922) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 118.15.132.38:67(key =82575) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 180.153.32.18:6767(key =27015) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 124.248.115.7:8128(key =26988) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 118.117.118.55:6767(key =27002) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 186.13.109.54:67(key =109698) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 58.253.238.243:67(key =79385) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 106.37.177.256:3128(key =104005) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 186.13.109.53:67(key =89906) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 58.56.124.112:67(key =108766) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 183.12.172.50:9999(key =101463) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 118.5.74.174:67(key =62315) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 118.117.172.236:67(key =121994) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 114.14.138.50:17147(key =106694) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 118.113.3.103:67(key =53514) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 124.95.183.102:67(key =92672) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 186.13.2.143:67(key =29156) ---------> 192.168.300.15:8745-vm2(key = 26975) request : 183.105.134.77:67(key =95363) ---------> 192.168.200.2:3214-vm2(key = 79176) request : 180.114.15.114:6746(key =59729) ---------> 192.168.100.1:3306-vm3(key = 47584) request : 125.39.18.76:67(key =77770) ---------> 192.168.100.1:3306-vm2(key = 77752) request : 118.28.96.39:3128(key =122495) ---------> 192.168.200.2:3214-vm3(key = 115888) request : 186.18.184.36:67(key =104915) ---------> 192.168.300.15:8745-vm3(key = 97282) request : 183.117.139.10:86(key =93281) ---------> 192.168.200.2:3214-vm2(key = 79176) <----------request 414--------------------> ---------------------print visitor tree------------------------- 77752[192.168.100.1:3306-vm2] -> visitor =3 32166[192.168.200.2:3214-vm1] -> visitor =38 30184[192.168.300.15:8745-vm1] -> visitor =6 26975[192.168.300.15:8745-vm2] -> visitor =93 47584[192.168.100.1:3306-vm3] -> visitor =100 115888[192.168.200.2:3214-vm3] -> visitor =51 79176[192.168.200.2:3214-vm2] -> visitor =63 97282[192.168.300.15:8745-vm3] -> visitor =60 ------------------------------print all tree------------------------------ 77752[192.168.100.1:3306-vm2],height =3 :visitor =3 32166[192.168.200.2:3214-vm1],height =2 :visitor =38 30184[192.168.300.15:8745-vm1],height =1 :visitor =6 26975[192.168.300.15:8745-vm2],height =0 :visitor =93 47584[192.168.100.1:3306-vm3],height =0 :visitor =100 115888[192.168.200.2:3214-vm3],height =2 :visitor =51 79176[192.168.200.2:3214-vm2],height =1 :visitor =63 97282[192.168.300.15:8745-vm3],height =0 :visitor =60 126515[192.168.100.1:3306-vm1],height =0 :visitor =0 ----------------------------mgrFree------------------------------
chash.h
/************************************************************************* > File Name: chash.h > Author: zhoulin > Mail: [email protected] > Created Time: Thu Apr 28 09:50:44 2016 ************************************************************************/ #ifndef _CHASH_H #include <stdint.h> #define vsize 64 #define nsize 32 #define vstr "-vm" /* * 定义真实机器的列表,真实机器对应一个或者多个虚拟节点 * 它存在的意义有如下2点: * 1.初始化虚拟节点的vsrvname。 * 2.如果有真实服务器宕机或者故障,需要需要剔除,分剔除虚拟节点和真实机器,重新映射哈希关系。 */ typedef struct _listNode { char srvname[nsize];//include server ip and server port struct _listNode *next; }listNode; typedef struct _listMgr { unsigned int count;//the number if list struct _listNode *head; }listMgr; /* *定义一个二叉树节点结构 *该节点表示虚拟节点,二叉树的操作时间复杂度为O(logn) * 1.对于平衡二叉树的最大节点和最小节点的时间复杂度为O(1) * 2.获取节点个数和 */ typedef struct _treeNode { unsigned int hkey;//virtual treeNode hash key char vsrvname[vsize];//virtual treeNode servername unsigned int visitor;//how many server visitor it unsigned int height;//cur treeNode height struct _treeNode *lchild; struct _treeNode *rchild; }treeNode; /* *定义一个二叉树管理节点 */ typedef struct _treeMgr { struct _treeNode *root;//point to virtual treeNode root unsigned int count;//the number of all node in tree struct _treeNode *tmin;//min hash value treeNode struct _treeNode *tmax;//max hash value treeNode }treeMgr; typedef struct _Mgr { unsigned int vmcount; //virtual node count listMgr *ltmgr; treeMgr *trmgr; }Mgr; //mgr operation Mgr *mgrInit(unsigned int vmcount); Mgr *mgrAdd(char *srvname,Mgr *mgr); Mgr *mgrDel(char *srvname,Mgr *mgr); void mgrFree(Mgr *mgr); //listNode operayion int listNodeAdd(char *srvname,Mgr *mgr); int listNodeDel(char *srvname,Mgr *mgr); //tree operation treeNode *treeNodeQuery(unsigned int key,Mgr *mgr); int treeNodeAdd(char *srvname,Mgr *mgr); treeNode *treeNodeMin(unsigned int key,treeMgr *mgr); treeNode *treeNodeMax(unsigned int key,treeMgr *mgr); //-----------------static method------------------------- //murmurhash2算法 static unsigned int mur2hash2(const void *key,int len); static treeNode *_treeNodeMin(treeNode *p); static treeNode *_treeNodeMax(treeNode *p); static treeNode *_treeNodeDel(treeNode *p,int hkey); static int mMax(int num1,int num2); static int treeNodeHeight(treeNode *p); static treeNode *sRoteL(treeNode *p); static treeNode *sRoteR(treeNode *p); static treeNode *dRoteL(treeNode *p); static treeNode *dRoteR(treeNode *p); static void _treeNodePrt(treeNode *p,int n); static void _treeNodeVPrt(treeNode *p,int n); static treeNode *_treeNodeAdd(unsigned int hkey,char *vsrvname,treeNode *p,treeMgr *trmgr); static void prtltMgr(Mgr *mgr); static treeNode *_treeNodeQuery(unsigned int hkey,treeNode *p); static void readIp(const char *path,Mgr *mgr); static void _treeNodeFree(treeNode *p); #define _CHASH_H #endif
chash.c
/************************************************************************* > File Name: chash.c > Author: zhoulin > Mail: [email protected] > Created Time: Thu Apr 28 09:51:38 2016 ************************************************************************/ #include "chash.h" #include <stdio.h> #include <stdint.h> #include <string.h> #include <stdlib.h> #include <math.h> #define max 1024 static unsigned int mur2hash2(const void *key,int len) { uint32_t seed = 5381; const uint32_t m = 0x5bd1e995; const int r = 24; uint32_t h = seed ^ len; const unsigned char *data = (const unsigned char *)key; while(len >= 4) { uint32_t k = *(uint32_t *)data; k *= m; k ^= k >> r; k *= m; h ^= k; data += 4; len -= 4; } switch(len) { case 3: h ^= data[2] << 16; case 2: h ^= data[1] << 8; case 1: h ^= data[0];h *= m; }; h ^= h >> 13; h *= m; h ^= h >> 15; } Mgr *mgrInit(unsigned int vmcount) { Mgr *mgr = (Mgr *)malloc(sizeof(*mgr)); if(mgr == NULL) return NULL; mgr->vmcount = vmcount; mgr->ltmgr =NULL; mgr->trmgr = NULL; return mgr; } int listNodeAdd(char *srvname,Mgr *mgr) { if((srvname == NULL || strlen(srvname) <= 0) && mgr == NULL) return -1; listMgr *ltmgr = mgr->ltmgr; if(ltmgr == NULL) { ltmgr = (listMgr *)malloc(sizeof(*ltmgr)); ltmgr->count = 0; ltmgr->head = NULL; mgr->ltmgr = ltmgr; } listNode *cur = (listNode *)malloc(sizeof(*cur)); if(cur == NULL) return -1; if(ltmgr->head == NULL){ ltmgr->head = cur; }else { cur->next = ltmgr->head; ltmgr->head = cur; } strncpy((char *)ltmgr->head->srvname,srvname,strlen(srvname)); ltmgr->count++; return 0; } int listNodeDel(char *srvname,Mgr *mgr) { if((srvname == NULL || strlen(srvname) <= 0) && mgr == NULL) return -1; listMgr *ltmgr = mgr->ltmgr; listNode *cur = ltmgr->head; listNode *fprev = NULL; listNode *fcur = NULL; while(cur != NULL) { if(strncmp(cur->srvname,srvname,strlen(srvname)) == 0) { fcur = cur; break; }else { fprev = cur; } cur = cur->next; } if(fcur == NULL) { return -1; } if(fcur == ltmgr->head) { ltmgr->head = fcur->next; }else { fprev->next = fcur->next; } free(cur); cur = NULL; ltmgr->count--; return 0; } Mgr *mgrAdd(char *srvname,Mgr *mgr) { listMgr *ltmgr = mgr->ltmgr; listNodeAdd(srvname,mgr); treeNodeAdd(srvname,mgr); return mgr; } static int mMax(int num1,int num2) { return ((num1 > num2)? num1 :num2); } //get treeNode height static int treeNodeHeight(treeNode *p) { if(p != NULL){ return p->height; } return -1; } // signed left rotation static treeNode *sRoteL(treeNode *p) { treeNode *k1; k1 = p->lchild; p->lchild = k1->rchild; k1->rchild = p; p->height = mMax(treeNodeHeight(p->lchild),treeNodeHeight(p->rchild)) + 1; k1->height = mMax(treeNodeHeight(k1->lchild),p->height) + 1; return k1; } //single righet rotation static treeNode *sRoteR(treeNode *p) { treeNode *k1; k1 = p->rchild; p->rchild = k1->lchild; k1->lchild =p; p->height = mMax(treeNodeHeight(p->lchild),treeNodeHeight(p->rchild)) + 1; k1->height = mMax(treeNodeHeight(k1->rchild),p->height) + 1; return k1; } static treeNode *dRoteL(treeNode *p) { p->lchild = sRoteR(p->lchild); return sRoteL(p); } static treeNode *dRoteR(treeNode *p) { p->rchild = sRoteL(p->rchild); return sRoteR(p); } //先序遍历 static void _treeNodePrt(treeNode *p,int n) { if(p != NULL) { int i = 0; int nx = n+1; for(;i < nx;i++) { fprintf(stdout," "); } fprintf(stdout,"%d[%s],height =%d :visitor =%d\n",p->hkey,p->vsrvname,p->height,p->visitor); _treeNodePrt(p->lchild,n+1); _treeNodePrt(p->rchild,n+1); } } //访问有访问量的节点 static void _treeNodeVPrt(treeNode *p,int n) { if(p != NULL) { if(p->visitor > 0) { int i = 0; int nx = n; for(;i < nx;i++) { fprintf(stdout," "); } fprintf(stdout,"%d[%s] -> visitor =%d\n",p->hkey,p->vsrvname,p->visitor); } _treeNodeVPrt(p->lchild,n+1); _treeNodeVPrt(p->rchild,n+1); } } static treeNode *_treeNodeAdd(unsigned int hkey,char *vsrvname,treeNode *p,treeMgr *trmgr) { if(p == NULL){ p = (treeNode *)malloc(sizeof(*p)); p->hkey = hkey; p->height = 0; if(trmgr->count > 0 && hkey <= trmgr->tmin->hkey) { trmgr->tmin= p; } if(trmgr->count > 0 && hkey > trmgr->tmax->hkey) { trmgr->tmax = p; } strncpy(p->vsrvname,vsrvname,strlen(vsrvname)); if(trmgr->count == 0){ trmgr->tmin = trmgr->tmax = p; } p->visitor = 0; p->lchild = p->rchild = NULL; trmgr->count++; } else if(hkey < p->hkey) { p->lchild = _treeNodeAdd(hkey,vsrvname,p->lchild,trmgr); if(treeNodeHeight(p->lchild) - treeNodeHeight(p->rchild) == 2) { if(hkey < p->lchild->hkey) p = sRoteL(p); else p = dRoteL(p); } } else if(hkey > p->hkey) { p->rchild = _treeNodeAdd(hkey,vsrvname,p->rchild,trmgr); if(treeNodeHeight(p->rchild) - treeNodeHeight(p->lchild) == 2) { if(hkey > p->rchild->hkey) p = sRoteR(p); else p = dRoteR(p); } } p->height = mMax(treeNodeHeight(p->lchild),treeNodeHeight(p->rchild))+1; return p; } int treeNodeAdd(char *srvname,Mgr *mgr) { char vsrvname[vsize]; memset(vsrvname,'\0',nsize); if(mgr == NULL) return -1; if(mgr->trmgr == NULL) { treeMgr *trmgr = (treeMgr *)malloc(sizeof(*trmgr)); trmgr->count = 0; trmgr->tmin = NULL; trmgr->tmax = NULL; trmgr->root = NULL; mgr->trmgr = trmgr; } treeMgr *trmgr = mgr->trmgr; int vcount = mgr->vmcount,i,j = 0; treeNode *p = trmgr->root; for(i = 1;i <= vcount;i++) { treeNode *p = trmgr->root; treeNode *cur = NULL; strncpy(vsrvname,srvname,strlen(srvname)); strncpy(vsrvname+strlen(vsrvname),vstr,strlen(vstr)); sprintf(vsrvname+strlen(vsrvname),"%d",i); unsigned int hkey = mur2hash2(vsrvname,strlen(vsrvname)); fprintf(stdout,"----vsrvname = %s,hkey =%d------\n",vsrvname,hkey); if((trmgr->root = _treeNodeAdd(hkey,vsrvname,p,mgr->trmgr)) != NULL) { j++; } memset(vsrvname,'\0',vsize); } return (vcount == j?0:-1); } static void prtltMgr(Mgr *mgr) { fprintf(stdout,"***************************************\n"); listNode *cur = mgr->ltmgr->head; if(mgr->ltmgr != NULL) fprintf(stdout,"ltmgr->count = %d,ltmgr->head = %p\n",mgr->ltmgr->count,mgr->ltmgr->head); while(cur != NULL) { fprintf(stdout,"cur = %p,srvname = %s,next = %p\n",cur,cur->srvname,cur->next); cur = cur->next; } } static treeNode *_treeNodeQuery(unsigned int hkey,treeNode *p) { treeNode *cur = p; if(cur == NULL){ return NULL; } if(hkey <= cur->hkey) { return _treeNodeQuery(hkey,cur->lchild); } else { treeNode *tmp = _treeNodeQuery(hkey,cur->rchild); if(tmp == NULL) { cur->visitor++; return cur; }else { return tmp; } } } treeNode *treeNodeQuery(unsigned int key,Mgr *mgr) { treeNode *root = mgr->trmgr->root; treeNode *rs = _treeNodeQuery(key,root); return rs; } static treeNode *_treeNodeMin(treeNode *p) { if(p == NULL) return NULL; unsigned int hkey = p->hkey; treeNode *cur = p; while(cur != NULL) { if(hkey > cur->hkey) { hkey = cur->hkey; cur = cur->lchild; } else cur = cur->lchild; } return cur; } static treeNode *_treeNodeMax(treeNode *p) { if(p == NULL) return NULL; unsigned int hkey = p->hkey; treeNode *cur = p; while(cur != NULL) { if(hkey < p->hkey) { hkey = p->hkey; cur = cur->rchild; }else{ cur = cur->rchild; } } return cur; } static void readIp(const char *path,Mgr *mgr) { FILE *fp = fopen(path,"r"); if(fp == NULL) return; char buf[64]; int count = 0; fprintf(stdout,"\n--------------------------------------begin request------------------------\n"); while((fgets(buf,64,fp) != NULL)) { buf[strlen(buf)-1] = '\0'; count++; unsigned int hkey = mur2hash2(buf,strlen(buf)); int m = 0; if(hkey <= mgr->trmgr->tmin->hkey) { m = hkey; hkey = mgr->trmgr->tmin->hkey+(m%2+rand()%50); } if(hkey > mgr->trmgr->tmax->hkey) { m = hkey; hkey = mgr->trmgr->tmax->hkey-(m%2+rand()%50); } treeNode *p = treeNodeQuery(hkey,mgr); if(p != NULL){ fprintf(stdout," request : %s(key =%d) ---------> %s(key = %d)\n",buf,hkey,p->vsrvname,p->hkey); } /* if(m != 0) { fprintf(stdout," [hash key no found] -->%s(real_key =%d,sys_key = %d,max = %d,min = %d)\n",buf,m,hkey,mgr->trmgr->tmax->hkey,mgr->trmgr->tmin->hkey); } */ memset(buf,'\0',64); } if(fp != NULL){ fclose(fp); } fprintf(stdout,"\n <----------request %d--------------------> \n",count); } static void _treeNodeFree(treeNode *p) { if(p != NULL) { treeNode *lchild = p->lchild; treeNode *rchild = p->rchild; free(p); p = NULL; _treeNodeFree(lchild); _treeNodeFree(rchild); } } void mgrFree(Mgr *mgr) { if(mgr == NULL) { return; } fprintf(stdout,"\n----------------------------mgrFree------------------------------\n"); listNode *cur = mgr->ltmgr->head; while(cur != NULL) { listNode *pn = cur->next; free(cur); cur = pn; } treeNode *root = mgr->trmgr->root; _treeNodeFree(root); free(mgr->ltmgr); free(mgr->trmgr); free(mgr); mgr->ltmgr = NULL; mgr->trmgr = NULL; mgr = NULL; } int main(void) { char *srv[3] = {"192.168.100.1:3306","192.168.200.2:3214","192.168.300.15:8745"}; Mgr *mgr = mgrInit(3); mgrAdd(srv[0],mgr); mgrAdd(srv[1],mgr); mgrAdd(srv[2],mgr); prtltMgr(mgr); fprintf(stdout,"exec = %d\n",listNodeDel(srv[0],mgr)); prtltMgr(mgr); treeMgr *trmgr = mgr->trmgr; fprintf(stdout,"trmgr->root = %p,count = %d,min=%d,max=%d\n",trmgr->root,trmgr->count,trmgr->tmin->hkey,trmgr->tmax->hkey); treeNode *root = mgr->trmgr->root; fprintf(stdout,"---------------------print init tree-------------------------\n"); _treeNodePrt(root,0); readIp("./IP.txt",mgr); fprintf(stdout,"---------------------print visitor tree-------------------------\n"); root = mgr->trmgr->root; int n = 0; _treeNodeVPrt(root,n); fprintf(stdout,"------------------------------print all tree------------------------------\n"); _treeNodePrt(root,0); mgrFree(mgr); return 0; }
时间: 2024-10-09 11:39:50