node.js - nodeJS - 巨大的字符串文件故障

标签 node.js

我在我的 nodeJS 代码中遇到了非常奇怪的问题。代码基本上是将序列化的 JSON 对象加载到相对较大但不是很大的文件中——~150mb。问题是当我试图加载这个文件时,确实发生了不确定的事情:

lapsio@linux-qzuq /d/g/GreenStorage> node
> k1=fs.readFileSync('../etc/md5index/green-Documents.extindex');0
0
> k1.length
157839101
> k2=fs.readFileSync('../etc/md5index/green-Documents.extindex');0
0
> k2.lengFATAL ERROR: invalid array length Allocation failed - process out of memory
fish: “node” terminated by signal SIGABRT (Abort)

第二次尝试

> k1=fs.readFileSync('../etc/md5index/green-Documents.extindex');0
0
> k2=fs.readFileSync('../etc/md5index/green-Documents.extindex');0
0
> k1.length
157839101
> k2.length
157839101
> k1==k2
false

从响应时间来看,这一步Ofc文件已经缓存在RAM中,所以这不是存储问题。 我的实际应用:

try{
  var ind = JSON.parse(args.legacyconvert?bfile:content),
      ostr = String(args.legacyconvert?bfile:content),
      str = JSON.stringify(ind,null,2);

  for (var i = 0, l = str.length ; i < l ; i++)
    if (str[i]!=ostr[i]){
      console.error('Soft bug occured - it\'s serious bug and probably classifies as node bug or linux memcache bug. Should be reported');
      throw ('Original string and reparsed don\'t match at '+i+' byte - system string conversion malfunction - abtorting')
    }

  return ind;
} catch (e) {
  console.error('Could not read index - aborting',p,e);
  process.exit(11);
}

结果:

lapsio@linux-qzuq /d/g/G/D/c/fsmonitor> sudo ./reload.js -e ../../../../etc/md5index/*.extindex
Reading index... ( ../../../../etc/md5index/green-Documents.extindex )
Soft bug occured - it's serious bug and probably classifies as node bug or linux memcache bug. Should be reported
Could not read index - aborting ../../../../etc/md5index/green-Documents.extindex Original string and reparsed don't match at 116655242 byte - system string conversion malfunction - abtorting
lapsio@linux-qzuq /d/g/G/D/c/fsmonitor> sudo ./reload.js -e ../../../../etc/md5index/*.extindex
Reading index... ( ../../../../etc/md5index/green-Documents.extindex )
Soft bug occured - it's serious bug and probably classifies as node bug or linux memcache bug. Should be reported
Could not read index - aborting ../../../../etc/md5index/green-Documents.extindex Original string and reparsed don't match at 39584906 byte - system string conversion malfunction - abtorting

并且每次都返回随机字节不匹配。此外,保存后文件损坏的可能性约为 50%。有时它甚至无法正确解析,因为它发现了一些奇怪的非 ASCII 字符,例如 [SyntaxError: Unexpected token 传]。它是来自 OpenSUSE 存储库的 Node 。我试过很多机器。重现此错误相对困难,因为它是随机发生的,但一旦它第一次出现,它或多或少总是会出现,直到重新启动。

lapsio@linux-qzuq /d/g/GreenStorage> node -v
v0.12.7

PC 有 16 GB 内存,而 Node 甚至没有达到其中的 10%,所以我确定这不是内存不足的问题。而且它似乎不是文件系统相关的问题,因为 md5sum 和其他哈希生成器总是返回有效的校验和。只有 Node 失败。我不知道该怎么想。它真的被归类为错误吗?

最佳答案

您的代码显示您正在读取大 JSON 文件然后解析它。这意味着您需要为原始文件和生成的解析对象留出空间。这可能是您不可预测的内存耗尽问题的部分原因。

大多数处理您提到的文件大小的人都尝试使用流式或增量式解析方法。这样原始数据就可以流经您的程序,而不必同时存在所有数据。

您可能想看看这个流式 JSON 解析器。它可能会让你成功地通过这 block 数据。 https://github.com/dominictarr/JSONStream

第二种可能性是(滥用)使用 JSON.parse() 的第二个参数。称为 revifify,这是一个函数,它会针对在 JSON 文本文件中找到的每个对象进行调用。您可以通过某种方式将对象写入文件(或可能是 dbms),然后返回空结果来响应对该函数的每次调用。这样,JSON.parse 就不需要存储它遇到的每个对象。你必须解决这个问题才能让它正常工作。使用这种策略,您仍会吞噬大输入文件,但会流式传输输出。

另一种可能性是尽最大努力将单个 JSON 文档拆分为一系列记录,即更小的文档。 (这样大小的数据集似乎可以合理地以这种方式拆分。)

关于node.js - nodeJS - 巨大的字符串文件故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383797/

相关文章:

node.js - 使用 Firestore 云函数

node.js - 简单对等。 ("signal") 事件重复多次

node.js - Mongoose findById 没有返回所有字段

node.js - 在基于 Node/Webpack 的项目中构建 rollbar.js 时遇到问题

node.js - 使用 Node.js 解析文件系统路径(可能使用符号链接(symbolic link))

node.js - 使用 NodeJS + Request 模块时图像无法正确代理

node.js - *ng对于不工作( Angular 4)

node.js - 基于 botframework v4 将对话框中的消息数据记录到 Node.js 机器人中的 Application Insights

node.js - Mongoose : How to find documents in which fields match an ObjectId or a string?

node.js - Express SessionID 与 Cookie 中的 SessionID 不同