CSharpFlink分布式实时计算,OutOfMemoryException异常,你意想不到的原因。
目录
一、测试过程及问题
二、问题排查及分析过程
三、问题分析及解决过程
四、问题解决初步结果
一、测试过程及问题
从昨天15点左右开始测试,1个主节点,10个计算节点,1000个数据点,每个数据点3(1个实时窗口,2个延迟窗口)个数据窗口,每个数据点随时生成窗口周期和计算实例,每个数据点随时生成实时数据或历史数据。
测试结果,由于程序无法再获得电脑的内存而停止工作,更专业的说是System. OutOfMemoryException。
主节点,今天3点左右开始出现异常,如下:
[20-11-13 03:00:21]>>窗口0952-补发数据_CSharpFlink.Core.Window.Operator.Min-线程(0033):【2020/11/13 2:00:00-2020/11/13 3:00:00】,异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.StringBuilder.ToString()
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 358 [20-11-13 03:00:35]>>窗口0927-补发数据_CSharpFlink.Core.Window.Operator.Min-线程(0098):【2020/11/13 2:00:00-2020/11/13 3:00:00】,异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.RegularExpressions.Match..ctor(Regex regex, Int32 capcount, String text, Int32 begpos, Int32 len, Int32 startpos)
at System.Text.RegularExpressions.RegexRunner.InitMatch()
at System.Text.RegularExpressions.RegexRunner.Scan(Regex regex, String text, Int32 textbeg, Int32 textend, Int32 textstart, Int32 prevlen, Boolean quick, TimeSpan timeout)
at System.Text.RegularExpressions.Regex.Run(Boolean quick, Int32 prevlen, String input, Int32 beginning, Int32 length, Int32 startat)
at System.Text.RegularExpressions.Match.NextMatch()
at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat)
at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 358 [20-11-13 03:00:42]>>窗口0941-补发数据_CSharpFlink.Core.Window.Operator.Avg-线程(0085):【2020/11/13 2:00:00-2020/11/13 3:00:00】,异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.GC.AllocateNewArray(IntPtr typeHandle, Int32 length, Boolean zeroingOptional)
at System.GC.AllocateUninitializedArray[T](Int32 length)
at System.Buffers.TlsOverPerCoreLockedStacksArrayPool`1.Rent(Int32 minimumLength)
at System.Text.ValueStringBuilder.Grow(Int32 additionalCapacityBeyondPos)
at System.Text.ValueStringBuilder.Append(ReadOnlySpan`1 value)
at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat)
at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 358
ValueStringBuilder.Append(ReadOnlySpan`1 value)
at System.Text.RegularExpressions.RegexReplacement.Replace(Regex regex, String input, Int32 count, Int32 startat)
at System.Text.RegularExpressions.Regex.Replace(String input, String replacement)
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 358 [20-11-13 03:00:46]>>窗口0970-补发数据_CSharpFlink.Core.Window.Operator.Sum-线程(0074):【2020/11/13 2:00:00-2020/11/13 3:00:00】,异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.String.Concat(String str0, String str1)
at CSharpFlink.Core.Common.FileUtil.WriteAppend(String filePath, String[] contents) in \CSharpFlink\src\CSharpFlink.Core\Common\FileUtil.cs:line 36
at CSharpFlink.Core.Task.MasterTaskManager.ParallelCalculate(ICalculateContext context) in \CSharpFlink\src\CSharpFlink.Core\Task\MasterTaskManager.cs:line 370
从节点,部分存活,部分异常退出,异常信息如下:
[20-11-13 02:00:38]>>任务解析异常:
Exception of type 'System.OutOfMemoryException' was thrown. at System.String.Concat(String str0, String str1)
at CSharpFlink.Core.Common.FileUtil.WriteAppend(String filePath, String[] contents) in \CSharpFlink\src\CSharpFlink.Core\Common\FileUtil.cs:line 36
at CSharpFlink.Core.Task.SlaveTaskManager.AddTask(String taskMsg) in \CSharpFlink\src\CSharpFlink.Core\Task\SlaveTaskManager.cs:line 138
358行的代码:
CalculateContext calcContext=(CalculateContext)context;
370行的代码:
_masterCacheList.TryAdd(downTrans.Key, compressMsg);
138行的代码:
_slaveCacheList.TryAdd(downTrans.Key, downTrans);
masterCacheList和slaveCacheList变量是ConcurrentDictionary类。
二、问题排查及分析过程
共性问题:记录的每处OutOfMemoryException异常信息都会涉及到对【String】的操作。
第一步,使用dotnet-dump工具对String进行操作
参考链接:https://docs.microsoft.com/zh-cn/dotnet/core/diagnostics/debug-memory-leak。
System.String有1784359个对象,为什么这么多对象呢?因为要生成计算节点的任务,这个任务要临时保存到文件目录中,把计算任务的文件发送到计算节点后,再进行删除和清空程序缓存。
写任务文件其中涉及到FileUtil.WriteAppend()方法,这个和上面异常的日志信息是对应的,WriteAppend的代码,如下:
public static void WriteAppend(string filePath, string[] contents)
{
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
fs.Seek(fs.Length, SeekOrigin.Current);
string content = String.Join(Environment.NewLine, contents) + Environment.NewLine;
byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
fs.Write(data, 0, data.Length);
fs.Close();
}
}
注:这是很早写的代码。
其中Join函数,可能涉及到了Concat函数,和异常信息也是对应的。
那就奇怪了,难道using和Close没有起来关闭和释放资源的目的吗?让我们来看看FileStream的基类Stream的Dispose和 Close都做了什么?看源代码,如下图:
从代码上看唯一做了SuppressFinalize函数操作,那么SuppressFinalize是什么意思呢?参见链接:
https://docs.microsoft.com/zh-cn/dotnet/api/system.gc.suppressfinalize?view=netcore-3.1。
上面链接的大概意思是:请求公共语言运行时不要调用指定对象的终结器。也就是说继承了IDisposable接口,就不再调用类的析构函数了,那析构函数做了什么呢?如下图:
我们分析至此,Dispose和Close相当于什么都没有做。那只能依赖GC来清理资源了。那在高并发下操作FileStream,Dispose和Close不起作用的情况下,难道GC没有及时回收资源?看来有可能是这个问题。
三、问题分析及解决过程
但是怎么解决这个问题呢?记得FileStream类有一个Flush函数,具体操作函数代码,如下图:
Flush函数主要调用了FlushOSBuffer函数,代码如下图:
没有找到FlushFileBuffers函数,调用的函数,如下图:
这是非托管的代码,函数参考链接:https://docs.microsoft.com/zh-cn/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers。大致意思是立即把数据写到磁盘文件中,但是没有找到该函数的源代码。
不管源代码的事了,修改一下WriteAppend函数,加上Flush测试一下,代码如下:
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
fs.Seek(fs.Length, SeekOrigin.Current);
string content = String.Join(Environment.NewLine, contents) + Environment.NewLine;
byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
fs.Write(data, 0, data.Length);
fs.Flush(); //新增加代码。
fs.Close();
}
四、问题解决初步结果
上午10点部署,测试到下午15点,总共5个小时左右的时间。内存使用情况,主节点基本维持在:380 MB(1000数据点,每个数据点有3个数据窗口,如果1个窗口,应该在130 MB左右),子节点基本维持在:150 MB。有一段时间,内存会逐步增涨,但是某个时间点内存会释放到基本情况,曲线呈现正弦波趋势。内存使用情况,如下图:
物联网&大数据技术 QQ群:54256083
物联网&大数据合作 QQ群:727664080
联系QQ:504547114
合作微信:wxzz0151
官方博客:https://www.cnblogs.com/lsjwq
iNeuOS工业互联网操作系统 公众号
CSharpFlink分布式实时计算,OutOfMemoryException异常,你意想不到的原因。的更多相关文章
- JStorm 是一个分布式实时计算引擎
alibaba/jstorm JStorm 是一个分布式实时计算引擎. JStorm 是一个类似Hadoop MapReduce的系统, 用户按照指定的接口实现一个任务,然后将这个任务递交给JStor ...
- [开源]CSharpFlink(NET 5.0开发)分布式实时计算框架,PC机10万数据点秒级计算测试说明
github地址:https://github.com/wxzz/CSharpFlinkgitee地址:https://gitee.com/wxzz/CSharpFlink 1 计算 ...
- 开源分布式实时计算引擎 Iveely Computing 之 WordCount 详解(3)
WordCount是很多分布式计算中,最常用的例子,例如Hadoop.Storm,Iveely Computing也不例外.明白了WordCount在Iveely Computing上的运行原理,就很 ...
- 开源分布式实时计算引擎 Iveely Computing 之 本地调试Topology(4)
当我们写完一个比较复杂的Topology之后,倘若直接提交到服务器上运行,难免会有很多问题,如何进行本地的调试Topology,是我们非常关心的问题.我们依然以WordCount作为代码示例. 首先, ...
- 开源分布式实时计算引擎 Iveely Computing 之 安装部署(2)
在Github中下载代码和二进制程序中,您都会看到一个bin\iveely computing目录,里面即是Iveely Computing的运行库. 以前总是有 ...
- 一脸懵逼学习Storm---(一个开源的分布式实时计算系统)
Storm的官方网址:http://storm.apache.org/index.html 1:什么是Storm? Storm是一个开源的分布式实时计算系统,可以简单.可靠的处理大量的数据流.被称作“ ...
- (第8篇)实时可靠的开源分布式实时计算系统——Storm
摘要: 在Hadoop生态圈中,针对大数据进行批量计算时,通常需要一个或者多个MapReduce作业来完成,但这种批量计算方式是满足不了对实时性要求高的场景.那Storm是怎么做到的呢? 博主福利 给 ...
- TOP100summit:【分享实录】Twitter 新一代实时计算平台Heron
本篇文章内容来自2016年TOP100summit Twitter technical lead for Heron Maosong Fu 的案例分享. 编辑:Cynthia Maosong Fu:T ...
- Storm分布式实时流计算框架相关技术总结
Storm分布式实时流计算框架相关技术总结 Storm作为一个开源的分布式实时流计算框架,其内部实现使用了一些常用的技术,这里是对这些技术及其在Storm中作用的概括介绍.以此为基础,后续再深入了解S ...
随机推荐
- 【原创】Linux虚拟化KVM-Qemu分析(四)之CPU虚拟化(2)
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...
- HNOI 2015 【亚瑟王】
看着洛谷里那一排任务计划,瑟瑟发抖...... 题目大意: 你有n张牌,每一张牌有一个发动的概率和造成的伤害值,游戏一共有r轮.对于每一轮游戏,你只能发动一张牌(在之前回合发动过的牌会被跳过,不予考虑 ...
- fio硬盘测速windows+linux
一.FIO工具简介 Fio工具的介绍网上有很多,都是可以通用的,这里就不做太多个人描述了,直接借鉴一下 fio是一种I / O工具,用于基准测试和压力/硬件验证.它支持19种不同类型的I / O引擎( ...
- NB 的开源项目遍地开花——GitHub 热点速览 Vol.41
作者:HelloGitHub-小鱼干 本周的 GitHub 热点速览的关键词便是 nb,也是本周特推之一的项目名字,这个功能如名字一般 nb 的项目是一个脚本,帮你处理笔记.书签.归档和知识库应用程序 ...
- Js电子时钟
简单版电子时钟,需要以下几个步骤 1. 封装一个函数 返回当前的时分秒 2. 使用定时器使当前以获取到的系统时间走动,每间隔一面调用 3. 把获取到的时间放到span盒子里,添加样式 效果展示 实现 ...
- C++ concurrent_queue
ConcurrentQueue 用C++11提供的多线程类实现一个线程安全的队列: #include <queue> #include <mutex> #include < ...
- 迎难而上,QPS提高22+倍
简介 记录1次性能提升的经历,它最大的挑战不在于性能提升,而在于时间急,涉及的面广(比如:机房F5的SSL/TLS性能,机房互联网流量费和项目投入产出比等).性能指标:至少支持10K QPS,10ms ...
- centos 7.8 添加磁盘后查看、分区、格式化、挂载
基础环境 公有云 由于磁盘空间快用完了,现在决定多加一个40G磁盘 第一步 分区 fdisk -l #查看当前磁盘信息 fdisk /dev/vdb #对指定磁盘进行操作 如上图一般磁盘的第一个分区都 ...
- 第十六章 IP子网的划分
一.引入 1.根据IP地址的类别进行IP地址分配的方法表现出越来越多的弊端 2.为了解决分类IP地址划分带来的地址浪费,就需要使用子网划分(Subnetting)的方法 3.VLSM和CIDR可以进一 ...
- BERT模型详解
1 简介 BERT全称Bidirectional Enoceder Representations from Transformers,即双向的Transformers的Encoder.是谷歌于201 ...