1. 程式人生 > >一次替換FastDFS數據目錄引發的文件上傳和訪問異常

一次替換FastDFS數據目錄引發的文件上傳和訪問異常

org prot 因此 0.10 51cto 找到 tor copy name

1、圖片訪問異常

問題描述

搭建一臺新環境的FastDFS文件服務器,剛搭建好的時候,上傳了一張圖片,正常,但是因為當時端口沒有開,沒有驗證訪問的問題。環境暫時擱置等待測試。
後來在測試環節,因為需要上傳的圖片文件太多,因此直接將生產環境的dfs的數據目錄拷貝過去,替換了新環境的數據目錄,同時以下文件還是用的新環境原有的文件( /data/dfs就是數據目錄):

/data/dfs/tracker 目錄
/data/dfs/group1/data/fdfs_storaged.pid
/data/dfs/group1/data/storage_stat.dat
/data/dfs/group1/data/storage_trunk.dat

/data/dfs/group1/data/sync 目錄
/data/dfs/group1/data/trunk 目錄

然後重啟了tracker、storage和nginx服務,但是發現圖片訪問不到,直接頁面是一片空白,使用curl訪問,也是沒有任何返回,就卡在那裏,只能Ctrl+c退出來:

[root@host10 logs]#  curl  http://10.0.0.10:8090/groupA/M00/00/00/cErM6luMkf-IbhOWAAhHLHLDXwwAAAABQKwYD8ACEdE376.jpg-m

#沒有任何返回,只能Ctrl+c退出來。
[root@host10 logs]#

原因分析

1、查看端口和進程

查看storage和tracker進程,都還在:

[root@host10 ~]#  ps -ef|grep storage.conf
root      1126     1  0 14:57 ?        00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
root      5139  5071  0 15:13 pts/8    00:00:00 grep --color=auto storage.conf
[root@host10 ~]#  ps -ef|grep tracker.conf
root      5149  5071  0 15:13 pts/8    00:00:00 grep --color=auto tracker.conf
root     30168     1  0 14:44 ?        00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
[root@host10 ~]#

查看tracker、storage的端口,都是起來的,防火墻也打開了相應的端口:

[root@host10 ~]# netstat -tlunp|grep 23000
tcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      1126/fdfs_storaged
[root@host10 ~]# netstat -tlunp|grep 22122
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      30168/fdfs_trackerd

然後查看nginx,發現端口也是起來的,但是進程有異常,只有一個master進程:

[root@host10 sbin]# ps -ef|grep nginx
root       744 22962  0 10:37 pts/8    00:00:00 grep --color=auto nginx
root     29076     1  0 10:21 ?        00:00:00 nginx: master process ./nginx     #只有一個master進程
[root@host10 sbin]#

2、查看日誌

查看nginx日誌,在error_log重復報下面幾行錯誤

ngx_http_fastdfs_process_init pid=29077
[2018-09-05 10:21:46] ERROR - file: shared_func.c, line: 960, open file /etc/fdfs/mod_fastdfs.conf fail, errno: 13, error info: Permission denied
[2018-09-05 10:21:46] ERROR - file: /usr/local/fastdfs-nginx-module/src/common.c, line: 155, load conf file "/etc/fdfs/mod_fastdfs.conf" fail, ret code: 13
2018/09/05 10:21:46 [alert] 29076#0: worker process 29077 exited with fatal code 2 and cannot be respawned

根據報錯信息的Permission denied和網上一些博文,同時對比了生產環境FastDFs服務器上的/etc/dfs目錄的權限,嘗試修改了/etc/dfs目錄的權限,改成了755,並重啟tracker、storage、nginx服務:

# chmod 755 /etc/fdfs
# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
# cd /usr/local/nginx/sbin/
# ./nginx -s reload

然後查看nginx的進程,就有worker進程了:

[root@host10 ~]# ps -ef|grep nginx
nobody     363 29076  0 14:55 ?        00:00:00 nginx: worker process         #有worker進程了
root      8456  8381  0 15:27 pts/8    00:00:00 grep --color=auto nginx
root     29076     1  0 10:21 ?        00:00:00 nginx: master process ./nginx
[root@host10 ~]#

訪問圖片,有內容返回了:

[root@host10 logs]#  curl  http://10.0.0.10:8090/groupA/M00/00/00/cErM6luMkf-IbhOWAAhHLHLDXwwAAAABQKwYD8ACEdE376.jpg-m
fileExtNamejpgfileLength542508fileNameIMG_1171.jpg
[root@host10 logs]#

讓測試在新環境上測試了一下,圖片能夠正常訪問了。

2、圖片上傳異常

問題描述

在測試反饋圖片訪問可以正常的時候,為了保險,測試了一下圖片上傳功能,發現上傳圖片出現問題:

[root@host10 ~]#  /usr/bin/fdfs_test /etc/fdfs/client.conf upload /tmp/test/test10.png
This is FastDFS client test program v5.05

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/
for more detail.

[2018-09-05 11:08:23] DEBUG - base_path=/data/dfs/tracker, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

tracker_query_storage_store_list_without_group:
        server 1. group_name=, ip_addr=10.0.0.10, port=23000

group_name=groupA, ip_addr=10.0.0.10, port=23000
[2018-09-05 11:08:23] ERROR - file: tracker_proto.c, line: 48, server: 10.0.0.10:23000, response status 17 != 0
storage_upload_by_filename
upload file fail, error no: 17, error info: File exists    #這裏報File exists,但是換了很多張新圖片上傳,都報這個錯
[root@host10 ~]#

原因分析

查看storaged.log日誌,發現在上傳文件的同時,日誌就會打印出這幾行報錯信息:

[2018-09-05 11:15:45] ERROR - file: storage_dio.c, line: 885, trunk file: /data/dfs/group1/data/00/00/000001, offset: 299076 already occupied by other file, trunk header info: file_type=-88, alloc_size=-1127393023, file_size=-397478323, crc32=485419875, mtime=-592647312, ext_name(7)=(<\?f§r
[2018-09-05 11:15:45] WARNING - file: trunk_mgr/trunk_mem.c, line: 1620, trunk space already be occupied, delete this trunk space, trunk info: store_path_index=0, sub_path_high=0, sub_path_low=0, id=1, offset=299076, size=24885, status=1

第一次遇到這種問題,於是將報錯信息在百度和Google找了很久,沒有找到相應的解決辦法。因為剛剛安裝好FastDFs服務的時候,圖片上傳功能是正常的於是懷疑可能是替換數據目錄引發的問題。因此嘗試將數據目錄還原成了最初安裝的那個原始的數據目錄,發現可以正常上傳。那就是數據目錄的問題了

再看報錯信息,指出了是和/data/dfs/group1/data/00/00/000001這個文件有關。而且用生產環境拷貝過去數據目錄,就只能訪問到文件,但是不能上傳。所以結合報錯信息,重新使用線上環境的那套數據目錄。除了上面提到的

/data/dfs/tracker 目錄
/data/dfs/group1/data/fdfs_storaged.pid
/data/dfs/group1/data/storage_stat.dat
/data/dfs/group1/data/storage_trunk.dat
/data/dfs/group1/data/sync 目錄
/data/dfs/group1/data/trunk 目錄

這部分文件使用的新環境本身的文件之外,還將/data/dfs/group1/data/00/00/000001文件也替換成了新環境本身原始的000001文件。然後重啟tracker、storage、nginx服務,發現上傳功能恢復了:

[root@host10 data]# /usr/bin/fdfs_test /etc/fdfs/client.conf upload /tmp/test/test2.png                                                                                                                                          This is FastDFS client test program v5.05

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/
for more detail.

[2018-09-05 15:40:56] DEBUG - base_path=/data/dfs/tracker, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

tracker_query_storage_store_list_without_group:
        server 1. group_name=, ip_addr=10.0.0.10, port=23000

group_name=groupA, ip_addr=10.0.0.10, port=23000
storage_upload_by_filename
group_name=groupA, remote_filename=M00/00/00/rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442.png
source ip address: 10.0.0.10
file timestamp=2018-09-05 15:40:56
file size=17697
file crc32=420302403
example file url: http://10.0.0.10:8090/groupA/M00/00/00/rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442.png
storage_upload_slave_by_filename
group_name=groupA, remote_filename=M00/00/00/rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442_big.png
source ip address: 10.0.0.10
file timestamp=2018-09-05 15:40:56
file size=17697
file crc32=420302403
example file url: http://10.0.0.10:8090/groupA/M00/00/00/rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442_big.png
[root@host10 data]#

查看數據目錄,這個文件也是存在的:

[root@host10 data]#  ll /data/dfs/group1/data/00/00/|grep 5442
-rw-r--r-- 1 root root    17697 Sep  5 15:40 rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442_big.png
-rw-r--r-- 1 root root       49 Sep  5 15:40 rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442_big.png-m
-rw-r--r-- 1 root root       49 Sep  5 15:40 rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442.png-m
[root@host10 data]#

查看該圖片,也是可以查看到的了:

[root@host10 data]# curl http://10.0.0.10:8090/groupA/M00/00/00/rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442_big.png-m
ext_namejpgfile_size115120height80width160
[root@host10 data]#
[root@host10 data]#
[root@host10 data]# curl http://10.0.0.10:8090/groupA/M00/00/00/rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442.png-m
ext_namejpgfile_size115120height80width160
[root@host10 data]#

在瀏覽器訪問
http://10.0.0.10:8090/groupA/M00/00/00/rDN5CluPiIiIV-bEAABFIRkNTkMAAAAAQAGYYsAAEU5442_big.png
也能看到圖片:
技術分享圖片

小記:
綜上,用生產環境FastDFs數據目錄替換新環境的數據目錄後,需要做下面的操作:
1、清空生產環境數據目錄裏面的log文件;
2、以下文件用的是新環境自身的文件:

/data/dfs/tracker 目錄
/data/dfs/group1/data/fdfs_storaged.pid
/data/dfs/group1/data/storage_stat.dat
/data/dfs/group1/data/storage_trunk.dat
/data/dfs/group1/data/sync 目錄
/data/dfs/group1/data/trunk 目錄
/data/dfs/group1/data/00/00/000001

一次替換FastDFS數據目錄引發的文件上傳和訪問異常