设计长期运行、资源密集型 Web 服务的建议
我有一个 .NET 函数可以执行一些复杂的计算。根据传入的参数,该函数:
- 运行时间从几分钟到几个小时不等
- 在计算过程中使用 100% 的单个核心
- 需要从 100 MB 到几 GB 内存
- 写入从几 MB 到几 MB 不等写入磁盘的 GB 数据
- 可能会引发异常,包括 OutOfMemoryException
可以从函数参数化中准确预测要写入磁盘的数据量。没有简单的方法可以从函数参数化中预测其他资源需求。
我需要通过网络服务公开此功能。该服务需要:
- 具有弹性并在计算过程中优雅地报告任何问题
- 能够处理并发请求,只要有足够的资源来处理请求而不会显着降低性能,否则可以优雅地拒绝请求。
我打算通过让初始请求返回可以轮询进度的状态资源来处理长时间运行的性质。计算完成后,该资源将提供输出数据的位置,客户端可以下载该数据(可能通过 FTP)。
我不太清楚如何最好地处理其他要求。我正在考虑某种“计算池”,它维护计算器的实例并跟踪当前正在使用哪些实例,但我还没有弄清楚细节。
有没有有类似情况经验的人有什么建议吗?只要解决方案可以在 Windows 机器上运行,所有技术选项都可以考虑。
I have a .NET function that does some complex calculation. Depending on the parameters that are passed in, the function:
- Takes anywhere from several minutes to several hours to run
- Uses 100% of a single core during the computation
- Requires anywhere from 100s of MB to several GB of memory
- Writes anywhere from several MB to several GB of data to disk
- May throw an exception, including an OutOfMemoryException
The amount to data to be written to disk can be accurately predicted from the function parameterisation. There is no easy way to predict the other resource requirements from the function parameterisation.
I need to expose this function via a web service. This service needs to be:
- Resiliant and gracefully report any problems during the calculation
- Capable of handling concurrent requests, as long as there are sufficient resources to handle the request without significant performance degradation, and to gracefully deny the request otherwise.
I'm intending to handle the long-running nature by having the initial request return a status resource that can be polled for progress. Once the calculation is complete this resource will provide the location of the output data, which the client can download (probably via FTP).
I'm less clear on how best to handle the other requirements. I'm considering some sort of "calculation pool" that maintains instances of the calculator and keeps track of which ones are currently being used, but I haven't figured out the details.
Does anyone with experience of similar situations have any suggestions? As long as the solution can run on a Windows box, all technology options can be considered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议将您的申请分为两部分。
这样设计的原因是
1)处理托管应用程序(ASP.NET)中的工作负载相对困难,因为服务器(IIS)将管理资源,而在单独的应用程序中您有更直接的控制;
2)两层设计更具可扩展性——例如,以后您可以轻松地将后端移动到另一台物理机器(或几台机器)。
Web 服务应该是无状态的 - 例如,在接受请求后,用户会返回一些 ID 并使用该 ID 来轮询服务以获取结果。
后端服务器可能必须维护一个要处理的请求队列以及一组处理它们的工作线程。工作人员应监视可用资源,并注意不要使机器过载(当然,还要妥善处理所有可能的错误情况)。
I'd suggest splitting your application in two parts.
The reasons for this design are
1) it's relatively difficult to handle the workload in the hosted application (ASP.NET) because the server (IIS) will manage the resources, while in a separate app you have more direct control;
2) two-tier design is more scalable - for instance, later you could easily move the backend to another physical machine (or several machines).
The web service should be stateless - for instance, after a request is accepted, the user gets back some ID and uses this ID to poll the service for the result.
The backend server, probably, has to maintain a queue of the requests to process and a set of worker threads that process them. The workers should monitor the resources available and take care not to overload the machine (and, of course, gracefully handle all possible error conditions).
虽然您可能希望提供 Web 服务接口,但 Web 服务通常不是为此类流程而设计的。您可能想要做的是将请求转发到可以处理此问题的 Windows 服务(在专用计算机上)。 Windows 服务不会被回收,并且您对该过程有更多的控制权。
关于计算池:您可以尝试创建一个计算队列(例如数据库中的表)。这样,您就可以在处理计算的专用计算机上拥有多个 Windows 服务。这可以让您更轻松地扩展。
While you may want to provide a web service interface, web services are typically not designed for these kind of processes. What you might want to do is forward the request to a windows service (on a dedicated machine) that can handle this. Windows Services won't get recycled and you have much more control over the process.
About the calculation pool: what you can try is create a calculation queue (for instance a table in the database). This way you can have multiple windows services on dedicated machines processing the calculations. This can allow you to scale more easily.