forked from buaazp/zimg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conflicts: CMakeLists.txt
- Loading branch information
Showing
52 changed files
with
15,848 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
PREFIX=/usr/local/zimg | ||
PWP=$(shell pwd) | ||
|
||
all: | ||
mkdir -p build/zimg | ||
cd build/zimg; cmake $(PWD)/src; make; cp zimg $(PWD)/bin | ||
|
||
debug: | ||
mkdir -p build/zimg | ||
cd build/zimg; cmake -DCMAKE_BUILD_TYPE=Debug $(PWD)/src; make; cp zimg $(PWD)/bin | ||
|
||
clean: | ||
rm -rf build | ||
rm bin/zimg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,118 @@ | ||
#zimg# | ||
|
||
|
||
zimg ([Homepage](http://zimg.buaa.us/)) is a light image storage and processing system. It's written by C and it has high performance in image field. There is a benchmark test report with more infomation: [Benchmark](http://zimg.buaa.us/benchmark.html). zimg is designed for high concurrency image server. It supports many features for uploading and getting images. | ||
zimg is a light image storage and processing system. It's written by C and it has high performance in image field. There is a benchmark test report with more infomation: [Benchmark](http://zimg.buaa.us/benchmark.html). zimg is designed for high concurrency image server. It supports many features for uploading and downloading images. | ||
|
||
Homepage: [zimg.buaa.us](http://zimg.buaa.us/) | ||
Author: [@招牌疯子](http://weibo.com/819880808) | ||
Contact me: [email protected] | ||
|
||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/buaazp/zimg/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
|
||
### Versions: | ||
- 04/26/2014 - zimg 2.0.0 Beta Release. *Important milestone for zimg.* | ||
- 03/10/2014 - zimg 1.1.0 Release. | ||
- 08/01/2013 - zimg 1.0.0 Release. | ||
|
||
### Dependence: | ||
> We stand on the shoulders of giants. | ||
[libevent](https://github.com/libevent/libevent): Provides a sophisticated framework for buffered network IO. | ||
[libevhtp](https://github.com/ellzey/libevhtp): A more flexible replacement for libevent's httpd API. | ||
[imagemagick](http://www.imagemagick.org/script/magick-wand.php): A software suite to create, edit, compose, or convert bitmap images. | ||
[libmemcached](https://github.com/trondn/libmemcached): LibMemcached is designed to provide the greatest number of options to use Memcached. | ||
[lua](http://www.lua.org/): Lua is a lightweight multi-paradigm programming language designed as a scripting language with extensible semantics as a primary goal. | ||
#### [Optional] For Storage: | ||
[memcached](https://github.com/memcached/memcached): A distributed memory object caching system. | ||
[beansdb](https://github.com/douban/beansdb): Beansdb is a distributed key-value storage system designed for large scale online system, aiming for high avaliablility and easy management. | ||
[beanseye](https://github.com/douban/beanseye): Beanseye is proxy and monitor for beansdb, written in Go. | ||
[SSDB](https://github.com/ideawu/ssdb): SSDB is a high performace key-value(key-string, key-zset, key-hashmap) NoSQL database, an alternative to Redis. | ||
[twemproxy](https://github.com/twitter/twemproxy): Twemproxy is a fast and lightweight proxy for memcached and redis protocol. | ||
|
||
#### Thanks to: | ||
> zimg contains libevhtp and libhiredis. You needn't install them now. | ||
[libevhtp](https://github.com/ellzey/libevhtp): A more flexible replacement for libevent's httpd API. | ||
[hiredis](https://github.com/redis/hiredis): Hiredis is a minimalistic C client library for the Redis database. | ||
|
||
### Supplying: | ||
Receive and storage users' upload images. | ||
Transfer image through HTTP protocol. | ||
Process resized and grayed image by request parameter. | ||
Use memcached to improve performance. | ||
Multi-thread support for multi-core processor server. | ||
Use lua for conf and other functions. | ||
**Support beansdb/SSDB mode to save images into distributed storage backends.** | ||
|
||
### In Planning: | ||
Distributed architecture. | ||
Performance optimization. | ||
Security measures. | ||
|
||
### Documentation: | ||
There is an architecture design document of zimg. It is written in Chinese. | ||
[Architecture Design of Image Server](http://zimg.buaa.us/arch_design.html) | ||
There is an architecture design document of zimg v1.0. It is written in Chinese. | ||
[Architecture Design of zimg](http://zimg.buaa.us/arch_design.html) | ||
And this document is to introduce zimg v2.0. | ||
[Distributed Image Storage Server: zimg](http://blog.buaa.us/?p=215) | ||
The architecture of zimg's storage: | ||
|
||
![architecture_of_zimg_v2](http://ww2.sinaimg.cn/large/4c422e03gw1efpmngazc0j21ik1e6dnk.jpg) | ||
|
||
### Download: | ||
The source code is licensed under a BSD-like License. | ||
All versions on [Github](https://github.com/buaazp/zimg). | ||
|
||
### Build: | ||
|
||
cd zimg | ||
cmake . | ||
make | ||
./zimg | ||
|
||
### Example: | ||
A zimg server in my VPS to test functions. | ||
[http://zimg.buaa.us:4869/](http://zimg.buaa.us:4869/) | ||
You should build dependences above and make sure the beansdb, beanseye or ssdb backend is working well. | ||
|
||
```` | ||
git clone https://github.com/buaazp/zimg | ||
cd zimg | ||
make | ||
cd bin | ||
./zimg conf/zimg.lua | ||
```` | ||
### Config: | ||
|
||
```` | ||
--zimg server config | ||
--server config | ||
is_daemon=1 | ||
port=4869 | ||
thread_num=4 | ||
backlog_num=1024 | ||
max_keepalives=1 | ||
--cache config | ||
cache=1 | ||
mc_ip='127.0.0.1' | ||
mc_port=11211 | ||
--log config | ||
log=1 | ||
log_name='./log/zimg.log' | ||
--htdoc config | ||
root_path='./www/index.html' | ||
--storage config | ||
--zimg support 3 ways for storage images | ||
mode=1 | ||
--mode[1]: local disk mode | ||
img_path='./img' | ||
--mode[2]: beansdb mode | ||
beansdb_ip='127.0.0.1' | ||
beansdb_port='7905' | ||
--mode[3]: ssdb mode | ||
ssdb_ip='127.0.0.1' | ||
ssdb_port='8888' | ||
```` | ||
|
||
### Feedback: | ||
If you have any question, please submit comment here or mention me on [Weibo](http://weibo.com/819880808). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--zimg server config | ||
|
||
--server config | ||
is_daemon=1 | ||
port=4869 | ||
thread_num=4 | ||
backlog_num=1024 | ||
max_keepalives=1 | ||
retry=3 | ||
system=io.popen("uname -s"):read("*l") | ||
|
||
--cache config | ||
cache=1 | ||
mc_ip='127.0.0.1' | ||
mc_port=11211 | ||
|
||
--log config | ||
log=1 | ||
log_name='./log/zimg.log' | ||
|
||
--htdoc config | ||
root_path='./www/index.html' | ||
|
||
--storage config | ||
--zimg support 3 ways for storage images | ||
mode=1 | ||
|
||
--mode[1]: local disk mode | ||
img_path='./img' | ||
|
||
--mode[2]: beansdb mode | ||
beansdb_ip='127.0.0.1' | ||
beansdb_port='7900' | ||
|
||
--mode[3]: ssdb mode | ||
ssdb_ip='127.0.0.1' | ||
ssdb_port='8888' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<html> | ||
<h1>Welcome to zimg world!</h1> | ||
<p>Upload an image to zimg:<p> | ||
<form enctype="multipart/form-data" action="upload" method=post> | ||
Choose file: | ||
<input name="userfile" type="file"> | ||
<input type="submit" value="upload"> | ||
</form> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#zimg新版本发布,支持分布式存储# | ||
####Distributed Image Storage Server: zimg#### | ||
[@招牌疯子](http://weibo.com/buaazp) | ||
|
||
## 关于zimg | ||
zimg是我去年开源的一个图片存储程序,主要的优点是可以根据请求实时处理图片,并且进行压缩和存储,一是方便前端用户,二来降低流量。zimg设计之初就是面向中小型应用,是存储量小于TB级别的单机存储方案。它的1.0版本主要竞争对手是基于Nginx+PHP的图片服务器,因为采用了特殊的策略,zimg会比PHP快出很多。 | ||
然而由于移动设备普及等原因,对图片服务器的存储能力提出了更高的要求,不仅需要支持更大的容量,还要具备冗余备份等功能。我也一直在寻找解决办法,希望使zimg满足大家的需求。 | ||
|
||
## 分布式存储 | ||
无论硬盘多么廉价,单机存储容量总是会有上限的,现在可能连一个中小网站所需要的图片都存不下了。分布式存储将数据分布存储于多台服务器上,一台不够了再加一台,理论上讲存储容量是无上限的。而且提高了存储服务的可靠性,如果机器够多,存储都做了冗余,那么即使某些机器出现故障无法服务,也可以方便地切换到备用服务器上,保证整体服务可用。 | ||
更高级别的分布式存储是跨IDC的,要实现这样的分布式,不是写一两个程序就可以完成的。像微博图床这种量级的存储,涉及服务器几千台,由多层架构组成,虽然看起来很是高大上,但其实并不适用于中小企业和开发者,或者说我们也不需要这样复杂的架构来存储几百GB的数据。 | ||
|
||
## 战斗吧,新版本! | ||
接下来介绍一下zimg 2.0的方案,看它有何新特性,以及是如何实现分布式存储的。 | ||
### beansdb | ||
在去年1.0刚发布之后,就有同学给我推荐支持豆瓣的[beansdb](https://github.com/douban/beansdb),因为beansdb本身就是豆瓣用来存图片的,在线上服务中经过了检验,我也去调研了一番,但是发现他们的分布式策略是用一个又慢又简陋的python脚本来实现的,经过这层proxy之后处理能力只有可怜的几十QPS,这样的性能跟zimg上千的图片压缩能力相比实在太鸡肋,于是一度将beansdb放在了考虑范围之外。 | ||
后来机缘巧合,看到了采用go语言写的beansdb代理程序[beanseye](https://github.com/douban/beanseye),虽然不具备分布式功能,但可以用来做replication,因为我个人对go语言的喜爱,立刻跑起来进行测试,发现这层代理性能还不错,值得一用。再加上beansdb底层所采用Bitcask引擎,确实是非常适合用来做图片的持久存储,这些原因促使我开始重新考虑是否采用beansdb来作为存储后端。 | ||
> Bitcask最大的特点是将全部内容的索引存于内存中,数据存于硬盘上,而且数据只追加不删除,使得数据一直是顺序存储的,具有减少磁盘碎片,降低磁头寻址时间(大量写入时)等优点。 | ||
> 而且beansdb对bitcask的key进行了改进,使得单机存储容量大幅度提高,即使是一台只有8G内存的服务器,也可以存储80TB的图片(平均大小200K)。关于bitcask的内容可以参阅beansdb作者的博文:[《beansdb 卷土重来》](http://www.douban.com/note/122507891/) | ||
zimg增加beansdb后端是很容易的,因为它采用的是memcached协议,而zimg第一版就支持memcached做cache。如果你希望采用beansdb做后端来存储图片,建议启用两台beansdb服务器做存储,再起一个beanseye做replication,这样数据会同时写入所有存储机上,实现了主从备份,即使一台挂掉beanseye也会自动从其他存储机上获取数据。 | ||
### SSDB | ||
[SSDB](https://github.com/ideawu/ssdb)也是一款能持久存储数据的NoSQL数据库,支持的是Redis协议,它的优点是即有丰富数据类型的支持,也能高效的存储(底层采用[LevelDB](https://code.google.com/p/leveldb/)甚至是[rocksdb](https://github.com/facebook/rocksdb)做存储引擎),对于zimg来说,其实只需要用到set和get两个命令,更多的是对它的存储和在线备份功能的期待。 | ||
> 与bitcask引擎不同,leveldb并不是将所有的key都放在内存中,而是采用多层次的方式进行存储,为了提高内存的利用率,部分热数据会存在内存里,也会极大地提高性能。 | ||
在SSDB支持备份功能后我也曾引入到zimg上进行了简单的测试,发现读取性能跟硬盘存储相近,也是十分值得一用。 | ||
### 如何选择 | ||
Beansdb和SSDB不仅分别代表了memcached协议和Redis协议,也代表了两种完全不同的底层存储方案,都能极大地利用内存和磁盘,都有主从备份功能,到底谁优谁劣实在难以分辨。于是我对他们进行了测试,希望通过数据来表现各自的能力,进行最终的抉择。 | ||
> 关于测试工具 | ||
> 要对图片服务器这种东西进行测试,一直也没有好用的工具,以前我一直使用ab进行性能测试,但是它的缺陷是请求单一,只能代表极限状态,不能很好地还原实际应用场景。 | ||
> 为此我在Vegeta的基础上写了一个自己的压测工具[stress](),stress不仅可以像ab那样对单个请求进行高并发压力测试,也可以读取一个请求序列文件,随机选择其中的请求进行发送,它支持GET和POST方式,自定义header,设定并发和请求数等,同时结果输出也很nice。你可以在一个文件中写入类似下面这些格式的请求进行测试: | ||
> ```` | ||
> GET http://127.0.0.1:4869/a87665d54a8c0dcaab04fa88b323eba1?w=200&g=1 | ||
> ```` | ||
> ```` | ||
> POST http://127.0.0.1:4869/upload form:5f189.jpeg | ||
> ```` | ||
> stress也还在开发中,后续增加更多好用的功能。 | ||
测试在一台服务器上进行,由于图片服务是一种读请求远远大于写请求的特殊存储服务,我的测试就只进行高并发读取,测试结果如下图所示: | ||
![zimg_storage_mode_test](http://ww1.sinaimg.cn/large/4c422e03tw1efpkuhy2p0j20or0jmwg6.jpg) | ||
测试结果显示,本地存储性能最好,SSDB略优于beansdb,但差别微乎其微,考虑到他们各有自己的优点,我最终的决定是: | ||
**zimg同时支持beansdb和SSDB**。 | ||
同时支持多种存储模式的另一个优点是适用性广,因为同时支持了memcached协议和Redis协议,你可以采用其他任何用得顺手的存储后端,比如[Memcachedb](http://memcachedb.org/)、甚至是Redis本身,**以及谁能知道将来会不会又出来其他更加优秀的NoSQL数据库呢**。 | ||
直接存取磁盘的模式也没有移除,方便那些只想用一台起了zimg的单机做图片服务器的朋友们。 | ||
### 数据分片 | ||
确定了存储后端之后,其实已经拥有了replication的能力,zimg急需具备数据水平分片的功能。Memcached和Redis协议,数据分片,这些需求放在一起之后有没有觉得很眼熟,没错,如果你的服务中有用到过这两款NoSQL数据库,你肯定也曾想办法解决过它们的分片问题,那么最简单的方案就出来了:twemproxy。 | ||
[Twemproxy](https://github.com/twitter/twemproxy)是Twitter出的memcached和redis代理,支持数据分片,而且还受到过redis作者本人的赞扬,可见其设计的独到之处。我觉得即使是自己在zimg里写一套分片逻辑,也肯定没有twemproxy性能好。twemproxy使用非常方便,参照zimg包内自带的[样例配置](https://github.com/buaazp/zimg/blob/master/test/zimg.yml)简单修改之后就可以使用。 | ||
> 需要注意的是,twemproxy不支持memcached的binary protocol,[详情在此](https://github.com/twitter/twemproxy/blob/master/notes/memcache.txt),因此zimg连接beansdb时默认不使用二进制协议,如果你不需要数据分片,可以简单修改源码来启用。 | ||
由于引入了twemproxy,毕竟是多了一层代理,虽然有人说它最多只会比裸连慢20%,但本着实事求是的原则,我们还是要亲自测试一下才能知道,于是我又做了相关的测试,twemproxy后面各带两个beansdb和SSDB实例,测试结果如图所示: | ||
![twemproxy_test](http://ww4.sinaimg.cn/large/4c422e03tw1efpkuiq5dpj20lh0jigms.jpg) | ||
图中结果是三次测试的平均值,可以看到,无论是beansdb还是SSDB,加了twemproxy性能下降极小,直接可以忽略不计。因此我非常建议各位用户在zimg和后端存储之间引入twemproxy代理。 | ||
### 架构 | ||
zimg v2的部署力求简单,最佳的方案是zimg和后端存储分开在不同的机器上,因为zimg涉及压图,属于计算密集型,存储层无论是beansdb还是SSDB,都属于I/O密集型,而且由于zimg可以启用memcached做缓存,正好也可以充分利用机器上的内存,而存储机上的内存会被beansdb和SSDB用到,互补影响也不会浪费。那么一图胜千言,请看: | ||
![architecture_of_zimg_v2](http://ww2.sinaimg.cn/large/4c422e03gw1efpmngazc0j21ik1e6dnk.jpg) | ||
### 其他改动 | ||
至此zimg新版本最重要的功能就介绍完毕了,这段时间零零散散还改了许多其他东西,也一并列在此处: | ||
```` | ||
New Features: | ||
存储key生成规则调整,即使将来引入更多特性也可兼容旧数据 | ||
代码结构调整,用户无需安装libevhtp和hiredis | ||
连接后端采用长连接提高性能 | ||
简化log输出内容 | ||
消除所有编译警告 | ||
采用lua做配置文件,主要是为以后支持压图脚本 | ||
修复一些遗留bug | ||
```` | ||
## 别的什么 | ||
首先是后续计划。在存储稳定之后,更多的精力将向图片处理方向上转移,除了支持更多的图片处理功能,现在能想到的有以下几点: | ||
- 将图片处理的逻辑往lua脚本中转移,这样可以使zimg在无需重新编译甚至无需重启的情况下改变图片处理规则,或者增加新规则,用户也可以自定义自己的处理逻辑,满足不同的需求。 | ||
- 大家可能已经有所耳闻,我们公司这边有自己开发的图片处理库webimg,性能好于imagemagick,而且不会内存泄露,使用起来也极其方便(webimg库支持c和PHP,最近我又给它增加了lua和go接口),一直有人呼吁开源出来给大家使用,我当然也非常希望,但这是公司的东西,可能需要老大们做决定。如果一旦webimg开源,zimg将果断抛弃imagemagick转向它。 | ||
- 还有一个是老问题了,zimg处理上传的逻辑写得非常搓,又笨又简陋,需要改进。 | ||
- 完善工具和文档,看着别人做的开源项目(比如SSDB)这也有那也有,感觉非常高端大气上档次,只能慢慢补充吧。 | ||
然后有人向我咨询zimg的license问题,问我是否可以商业化,答案是可以的,希望大家随便用随便改,如果你的公司或者APP采用了zimg,希望在此处留言告知,或者发邮件通知我一声,邮箱是````[email protected]````。 | ||
开源项目zimg是我利用业余时间无偿完成的,很多个深夜都在写代码中度过,作者并不指望靠它获利,只是诚惶诚恐。如果使用它可以给你带来一点便利,我已非常满足。 | ||
Oops, something went wrong.