免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4771 | 回复: 4
打印 上一主题 下一主题

[C] flock 文件锁和NAS引发的问题!!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-29 12:19 |只看该作者 |倒序浏览
大家好:

    我现在遇到一个问题。某些应用用到了文件锁,在一个进程中很多的并发线程以加锁的方式、追加模式打开文件,锁定即将写入的部分,以达到串行化写文件的目的。

    本来这部分程序在SAN结构的存储中运行良好(线程所写的日志放在SAN结构的存储上),但是上周做了存储的迁移,临时把日志文件放在了NAS存储上,结果导致线程被严重阻塞。

    为了进一步说明问题,我专门做了个模拟程序,模拟了这部分功能。编译平台是AIX 5.3,XLC 64位编译器。源代码如附件所示,日志文件放在NAS上。用truss追踪发现线程全部受阻,但是仍旧可以缓慢执行,但是的确很慢,要1-2分钟才能执行一次写文件。顺便说一句,如果日志文件不放置在NAS上,则不会出现阻塞情况。所以我现在怀疑根本原因是flock机制和NAS所支持的文件共享功能之间有冲突所导致。具体还请大家详细指点一下,谢谢!


test1.zip (925 Bytes, 下载次数: 15)





truss如下:

kwrite(1, " n e w   t h r e a d :  ".., 34)     = 34
_nsleep(0x00000001100404C0, 0x0000000110040590) = 0
Thread 258,bzero successful,j=1
kwrite(1, " T h r e a d   2 5 8 , b".., 32)     = 32
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 3
kioctl(3, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
_nsleep(0x00000001100E64C0, 0x00000001100E6590) = 0
Thread 1029,bzero successful,j=1
kwrite(1, " T h r e a d   1 0 2 9 ,".., 33)     = 33
_nsleep(0x00000001100AF4C0, 0x00000001100AF590) = 0
Thread 772,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 4
kwrite(1, " T h r e a d   7 7 2 , b".., 32)     = 32
kioctl(4, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
_nsleep(0x000000011011E4C0, 0x000000011011E590) = 0
thread_waitlock_(0x0000000110009518, 1, 0x0000000000000000) = 1
thread_unlock(0x000000011000951               = 0
_nsleep(0x00000001108B44C0, 0x00000001108B4590) = 0
_nsleep(0x000000011087D4C0, 0x000000011087D590) = 0
_nsleep(0x00000001108454C0, 0x0000000110845590) = 0
_nsleep(0x000000011080E4C0, 0x000000011080E590) = 0
_nsleep(0x00000001107D64C0, 0x00000001107D6590) = 0
_nsleep(0x000000011079F4C0, 0x000000011079F590) = 0
_nsleep(0x00000001107674C0, 0x0000000110767590) = 0
_nsleep(0x00000001107304C0, 0x0000000110730590) = 0
_nsleep(0x00000001106F84C0, 0x00000001106F8590) = 0
_nsleep(0x00000001106C14C0, 0x00000001106C1590) = 0
_nsleep(0x00000001106894C0, 0x0000000110689590) = 0
_nsleep(0x00000001106514C0, 0x0000000110651590) = 0
_nsleep(0x000000011061A4C0, 0x000000011061A590) = 0
_nsleep(0x00000001105E24C0, 0x00000001105E2590) = 0
_nsleep(0x00000001105AB4C0, 0x00000001105AB590) = 0
_nsleep(0x00000001105734C0, 0x0000000110573590) = 0
_nsleep(0x000000011053C4C0, 0x000000011053C590) = 0
_nsleep(0x00000001105044C0, 0x0000000110504590) = 0
_nsleep(0x00000001104CD4C0, 0x00000001104CD590) = 0
_nsleep(0x00000001104964C0, 0x0000000110496590) = 0
_nsleep(0x000000011045E4C0, 0x000000011045E590) = 0
_nsleep(0x00000001104274C0, 0x0000000110427590) = 0
_nsleep(0x00000001103EF4C0, 0x00000001103EF590) = 0
_nsleep(0x00000001103B84C0, 0x00000001103B8590) = 0
_nsleep(0x00000001103804C0, 0x0000000110380590) = 0
_nsleep(0x00000001103494C0, 0x0000000110349590) = 0
_nsleep(0x00000001103114C0, 0x0000000110311590) = 0
_nsleep(0x00000001102DA4C0, 0x00000001102DA590) = 0
_nsleep(0x00000001102A24C0, 0x00000001102A2590) = 0
_nsleep(0x000000011026B4C0, 0x000000011026B590) = 0
_nsleep(0x00000001102334C0, 0x0000000110233590) = 0
_nsleep(0x00000001101FC4C0, 0x00000001101FC590) = 0
_nsleep(0x00000001101C44C0, 0x00000001101C4590) = 0
_nsleep(0x000000011018D4C0, 0x000000011018D590) = 0
_nsleep(0x00000001101554C0, 0x0000000110155590) = 0
_nsleep(0x00000001100774C0, 0x0000000110077590) = 0
Thread 10281,bzero successful,j=1
kwrite(1, " T h r e a d   1 0 2 8 1".., 34)     = 34
Thread 10024,bzero successful,j=1
kwrite(1, " T h r e a d   1 0 0 2 4".., 34)     = 34
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 5
kioctl(5, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 9767,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 6
kwrite(1, " T h r e a d   9 7 6 7 ,".., 33)     = 33
kioctl(6, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 9510,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 7
kwrite(1, " T h r e a d   9 5 1 0 ,".., 33)     = 33
kioctl(7, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 9253,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 8
kwrite(1, " T h r e a d   9 2 5 3 ,".., 33)     = 33
kioctl(8, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 8996,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 9
kwrite(1, " T h r e a d   8 9 9 6 ,".., 33)     = 33
kioctl(9, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 8739,bzero successful,j=1
kwrite(1, " T h r e a d   8 7 3 9 ,".., 33)     = 33
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 10
kioctl(10, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 8482,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 11
kwrite(1, " T h r e a d   8 4 8 2 ,".., 33)     = 33
kioctl(11, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 8225,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 12
kwrite(1, " T h r e a d   8 2 2 5 ,".., 33)     = 33
kioctl(12, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 7968,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 13
kwrite(1, " T h r e a d   7 9 6 8 ,".., 33)     = 33
kioctl(13, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 7711,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 14
kwrite(1, " T h r e a d   7 7 1 1 ,".., 33)     = 33
kioctl(14, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 7454,bzero successful,j=1
kwrite(1, " T h r e a d   7 4 5 4 ,".., 33)     = 33
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 15
kioctl(15, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
Thread 7197,bzero successful,j=1
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 16
kwrite(1, " T h r e a d   7 1 9 7 ,".., 33)     = 33
kioctl(16, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY
kfcntl(5, 13, 0x00000001108B46E0)               = 0
kwrite(5, " t h i s   i s   t h r e".., 41)     = 41
Thread 6940,bzero successful,j=1
kwrite(1, " T h r e a d   6 9 4 0 ,".., 33)     = 33
kfcntl(6, 13, 0x000000011087D6E0)               = 0
kfcntl(5, 13, 0x00000001108B46E0)               = 0
close(5)                                        = 0
kwrite(4572529856, 0x00000001108B4590, 0)       = 41
kfcntl(7, 13, 0x00000001108456E0)               = 0
_nsleep(0x00000001108B44C0, 0x00000001108B4590) = 0
kfcntl(6, 13, 0x000000011087D6E0)               = 0
kfcntl(8, 13, 0x000000011080E6E0)               = 0
thread_unlock(0x000000011000951               = 0
thread_waitlock_(0x0000000110009518, 1, 0x0000000000000000) = 1
kwrite(7, " t h i s   i s   t h r e".., 40)     = 40
Thread 10281,bzero successful,j=2
kwrite(1, " T h r e a d   1 0 2 8 1".., 34)     = 34
kfcntl(9, 13, 0x00000001107D66E0)               = 0
close(6)                                        = 0
open("/test/test1.log", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) = 5
kioctl(5, 22528, 0x0000000000000000, 0x0000000000000000) Err#25 ENOTTY

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2012-08-29 13:27 |只看该作者
改用fcntl加锁试试

论坛徽章:
0
3 [报告]
发表于 2012-08-29 13:40 |只看该作者
回复 2# hellioncu


   

现在就是采用的fcntl加锁啊

fcntl_i=fcntl(fd,F_SETLKW,&f1);

论坛徽章:
44
15-16赛季CBA联赛之浙江
日期:2021-10-11 02:03:59程序设计版块每日发帖之星
日期:2016-07-02 06:20:0015-16赛季CBA联赛之新疆
日期:2016-04-25 10:55:452016科比退役纪念章
日期:2016-04-23 00:51:2315-16赛季CBA联赛之山东
日期:2016-04-17 12:00:2815-16赛季CBA联赛之福建
日期:2016-04-12 15:21:2915-16赛季CBA联赛之辽宁
日期:2016-03-24 21:38:2715-16赛季CBA联赛之福建
日期:2016-03-18 12:13:4015-16赛季CBA联赛之佛山
日期:2016-02-05 00:55:2015-16赛季CBA联赛之佛山
日期:2016-02-04 21:11:3615-16赛季CBA联赛之天津
日期:2016-11-02 00:33:1215-16赛季CBA联赛之浙江
日期:2017-01-13 01:31:49
4 [报告]
发表于 2012-08-29 16:29 |只看该作者
NAS用的什么系统?什么NFS服务器?
之前的SAN十有八九用的是iSCSI,文件系统驱动在本机,所以没问题,改用NAS之后应该是用的NFS,众所周知NFS的锁永远都有问题

论坛徽章:
1
白羊座
日期:2013-09-18 22:02:26
5 [报告]
发表于 2012-08-30 08:58 |只看该作者
如果你的文件在nfs上并只有一个客户端,可以在客户机上做一个映射文件之类的东西。映射文件写到一定大小传到nfs上,客户机上的进程每次对这个文件进行加锁。另可以提供一个同步函数,便于应用上强制同步文件到nfs。

如果是多客户机的nfs就必须在nfs上做自己的同步处理了。客户机同步类似于上面。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP