使用 Paperclip 将小文件上传到 s3 挂起,CPU 使用率 100%

问题描述 投票:0回答:1

我有一个目录<20MB pdf files (each pdf represents an ad) on an AWS EC2 large instance. I'm trying to upload each pdf file to S3 using ruby and DM-Paperclip.

大多数文件上传成功,但有些文件似乎需要数小时,CPU 挂在 100%。我通过在相关部分中打印调试语句找到了导致问题的代码行。

 # Takes an array of pdf file paths and uploads each to S3 using dm-paperclip
 def save_pdfs(pdfs_files)
  pdf_files.each do |path|
  pdf = File.open(path)
  ad = Ad.new
  ad.pdf.assign(pdf) # <= Last debug statment is printed before this line
  begin
    ad.save
  rescue => e
    # log error
  ensure
    pdf.close
  end
 end

为了帮助解决问题,我在进程卡在 100% 时将 strace 附加到该进程。结果是像这样的数十万行:

 ...
 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=3543, ...}) = 0
 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=3543, ...}) = 0
 stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=3543, ...}) = 0
 ... 500K lines

跟着几千:

 ...
 brk(0x1224d0000)                        = 0x1224d0000
 brk(0x1224f3000)                        = 0x1224f3000
 brk(0x122514000)                        = 0x122514000
 ...

在不挂起的上传过程中,strace 看起来像这样:

 ...
 ppoll([{fd=12, events=POLLOUT}], 1, NULL, NULL, 8) = 1 ([{fd=12, revents=POLLOUT}])
 fstat(12, {st_mode=S_IFSOCK|0777, st_size=0, ...}) = 0
 fcntl(12, F_GETFL)                      = 0x2 (flags O_RDWR)
 write(12, "%PDF-1.3\n%\342\343\317\323\n8 0 obj\n<</Filter"..., 4096) = 4096
 ppoll([{fd=12, events=POLLOUT}], 1, NULL, NULL, 8) = 1 ([{fd=12, revents=POLLOUT}])
 write(12, "S\34\367\23~\277u\272,h\204_\35\215\35\341\347\324\310\307u\370#\364\315\t~^\352\272\26\374"..., 4096) = 4096
 ppoll([{fd=12, events=POLLOUT}], 1, NULL, NULL, 8) = 1 ([{fd=12, revents=POLLOUT}])
 write(12, "\216%\267\2454`\350\177\4\36\315\211\7B\217g\33\217!e\347\207\256\264\245vy\377\304\256\307\375"..., 4096) = 4096
 ...

导致此问题的 pdf 文件似乎是随机的。它们都是有效的pdf文件,而且都比较小。它们在 ~100KB 到 ~50MB 之间变化。

strace 和看似过多的 stat 系统调用是否与我的问题有关?

amazon-s3 upload cpu paperclip space-leak
1个回答
-1
投票

这似乎是与原始计算机的文件一起发送的失败文件的原始文件权限问题。所有 pdf(保存到服务器的文件)都将在脚本中分配 0644,或者如果脚本在拾取时使用原始权限从客户端发送。 基本上服务器操作系统和配置拒绝它,因为写入光盘时文件权限不是 0644。

© www.soinside.com 2019 - 2024. All rights reserved.