用 skynet 实现 unity 的 cache server
我们公司的一些 Unity 项目,当 cache server 的数据上涨到几百 G 后,经常遇到问题。最近一次是 nodejs 内存使用太多导致进程挂掉。
我不太想的明白,一个几乎不需要在内存中保留什么状态的服务,为啥会吃掉那么多内存。简单看了一下 cache server 的官方实现 感觉实现的挺糟糕的。它的业务很简单,还不如按协议自己实现一个。
Cache server 解决的是 Unity 的 asset 打包问题,一台机器如果把资源从 A 转换为 B ,那么可以把结果 B 提交到 cache server 上;后来的人就不需要重新做这个转换流程,而直接下载 B 。
怎么定义 一个 B 是从 A 转换过来的呢?Unity 的每个资源都有一个唯一的 guid ,如果经过转换,那么转换过程,包括了原始数据的 hash 版本号,平台,转换程序,转换参数等等,可以计算出一个 hash ;任何参数的变更都会导致这个 hash 变化。所以 guid + hash 就可以代表一个唯一资源的唯一版本。在需要做转换时,算出 hash 去服务器上查询,如果不存在,就在本地转换然后上传;如果后来的人重新进行这个过程,就会发现前人上传的结果,直接下载即可。
这个过程中, cache server 对资源如何转换完全不需要了解。它就是一个大的 hash 表,客户端可以用 guid+hash 作 key 读写数据。
官方是用 nodejs 实现的。我前几年看过一次,原来只有几百行一个很小的程序,这次再看发现增加了不少代码,但是基本业务并没有变化。我认为它一直都很糟糕,现在甚至比以前更糟糕。
简单列举一下我认为的问题:
用 js 实现了一个非常复杂的 cleanup 过程,用于淘汰掉可能不再使用的 cache 文件。这个工作不应该在服务本身的进程做,因为 nodejs 的逻辑是跑在同一线程中,cleanup 的过程很可能影响正常的服务业务。而淘汰 cache ,用一个 crontab 加一个脚本就可以做的更好。实在没必要增加服务的复杂度。
同时支持了 file cache 和 mem cache 。这可能是内存爆掉的元凶。mem cache 是没有必要的,因为如果进程本身占用内存不多的话,操作系统会把所有闲置内存用于 IO 的 cache 。这个服务可以看成是一个普通的静态文件服务,无非是打开一个硬盘目录中的文件,发送给客户端。自己再做一层 mem cache 实属浪费,它不会提升什么性能,如果提升了,那么一定是原有的东西实现的有问题罢了。
文件散列在 cache 目录中做的不好。只简单做了一级目录的散列。导致我们的项目同一个目录下有几万个文件。虽然在 linux 下 ext4 对同一目录下放太多文件支持的不错,但是很多 cache server 还是跑在 windows 下,ntfs 支持的就糟糕一些。更重要的是,这对远程管理的负担太大。就算在本地,列目录也会卡住(因为默认的列目录还需要对文件名排序)。
cache server 本身的协议设计的太草率,基本就是能用就凑合。比如我摘取一小段官方文档里对握手协议的描述就可见一斑。
The server reads eight bytes of data from the first package received. If the package contain less than eight bytes, only those are used. Only exception if getting a one-byte package, in which case it should wait for the next package in order go get at least two bytes worth of data.
居然在一个 tcp 流上设计的协议,谈 first package ,又没有怎样区分一个 package 的描述。貌似指 api 的读取调用收到的第一串数据?握手有可能是 8 字节,也有可能是 1 字节。
除了协议之外,我认为用 skynet 实现一个一样的替代物应该没太大工作量。上周花了几个小时试了一下,果然只需要 200 行左右的代码。核心功能 100 行代码就可以了,另外做了不少优化的工作,以更适合管理更大的项目(多人协作的高并发,海量文件)。
目前,我自己写的版本在公司内部测试。希望日后可以逐步取代官方 nodejs 版本的服务。这样方便根据实际使用情况做进一步的优化。
Comments
Posted by: manlin | (67) February 24, 2020 04:37 PM
Posted by: 路过 | (66) September 22, 2019 08:27 PM
Posted by: lam | (65) August 30, 2019 02:54 PM
Posted by: 如来 | (64) July 24, 2019 09:37 AM
Posted by: Cloud | (63) July 22, 2019 11:03 AM
Posted by: Julius | (62) July 17, 2019 10:03 AM
Posted by: shbk | (61) July 16, 2019 10:22 PM
Posted by: Macocal | (60) July 16, 2019 10:54 AM
Posted by: 将进酒 | (59) July 16, 2019 08:52 AM
Posted by: study_skynet | (58) July 13, 2019 05:36 PM
Posted by: cowlevel | (57) July 13, 2019 11:08 AM
Posted by: USA | (56) July 12, 2019 09:31 PM
Posted by: King | (55) July 12, 2019 08:54 PM
Posted by: 闰土 | (54) July 12, 2019 08:12 PM
Posted by: Roiang | (53) July 12, 2019 08:02 PM
Posted by: 小明 | (52) July 12, 2019 07:58 PM
Posted by: Macol | (51) July 12, 2019 07:04 PM
Posted by: 烟灰黯然 | (50) July 12, 2019 06:42 PM
Posted by: 666 | (49) July 12, 2019 06:33 PM
Posted by: uu | (48) July 12, 2019 06:00 PM
Posted by: 萧炎 | (47) July 12, 2019 05:47 PM
Posted by: Gel | (46) July 12, 2019 05:26 PM
Posted by: 悟空你又调皮了 | (45) July 12, 2019 05:09 PM
Posted by: 江湖 | (44) July 12, 2019 04:36 PM
Posted by: Lunx | (43) July 12, 2019 04:03 PM
Posted by: Qaliy | (42) July 12, 2019 03:43 PM
Posted by: 鹅老五 | (41) July 12, 2019 03:29 PM
Posted by: Tukyl | (40) July 12, 2019 03:14 PM
Posted by: IMG | (39) July 12, 2019 02:36 PM
Posted by: Alrm | (38) July 12, 2019 02:11 PM
Posted by: 沈三 | (37) July 12, 2019 02:05 PM
Posted by: 落十一 | (36) July 12, 2019 12:54 PM
Posted by: 过客 | (35) July 12, 2019 12:49 PM
Posted by: 拉登是我干掉的 | (34) July 12, 2019 12:24 PM
Posted by: Lixo | (33) July 12, 2019 12:10 PM
Posted by: Samiy | (32) July 12, 2019 12:10 PM
Posted by: 皮皮虾 | (31) July 12, 2019 12:00 PM
Posted by: Jack | (30) July 12, 2019 11:45 AM
Posted by: 渣渣辉 | (29) July 12, 2019 11:36 AM
Posted by: 云天河 | (28) July 12, 2019 11:25 AM
Posted by: 云风小粉丝 | (27) July 12, 2019 11:13 AM
Posted by: Tony | (26) July 12, 2019 11:07 AM
Posted by: heibor | (25) July 12, 2019 10:32 AM
Posted by: 小崔 | (24) July 12, 2019 10:28 AM
Posted by: 云风麾下护法小弟 | (23) July 12, 2019 10:06 AM
Posted by: 大周 | (22) July 12, 2019 09:48 AM
Posted by: guess | (21) July 12, 2019 09:31 AM
Posted by: Cloud | (20) July 11, 2019 10:47 PM
Posted by: 风清扬 | (19) July 11, 2019 09:32 PM
Posted by: 馨苑 | (18) July 11, 2019 08:06 PM
Posted by: 乔帮主 | (17) July 11, 2019 07:10 PM
Posted by: Z母哥 | (16) July 11, 2019 06:57 PM
Posted by: 跟风逐云 | (15) July 11, 2019 06:37 PM
Posted by: 至尊 | (14) July 11, 2019 05:41 PM
Posted by: L | (13) July 11, 2019 05:26 PM
Posted by: GG | (12) July 11, 2019 05:20 PM
Posted by: 曹操 | (11) July 11, 2019 05:01 PM
Posted by: guess | (10) July 11, 2019 04:40 PM
Posted by: yaml | (9) July 11, 2019 04:14 PM
Posted by: 万里独行侠 | (8) July 11, 2019 03:23 PM
Posted by: 不疯不成魔 | (7) July 11, 2019 03:09 PM
Posted by: F亰 | (6) July 11, 2019 02:55 PM
Posted by: .ME | (5) July 11, 2019 02:35 PM
Posted by: Cloud | (4) July 11, 2019 10:05 AM
Posted by: o( ̄ヘ ̄o#) | (3) July 10, 2019 07:00 PM
Posted by: 云风小跟班 | (2) July 9, 2019 05:20 PM
Posted by: 狂飙 | (1) July 9, 2019 03:44 PM