简体   繁体   English

在Java进程之间共享(只读)内存块

[英]Share a block of (read-only) memory among Java processes

I am writing a Java application which runs user-submitted Java code. 我正在编写一个运行用户提交的Java代码的Java应用程序。 I run each piece of user-submitted code in its own sandbox. 我在自己的沙箱中运行每个用户提交的代码。 This sandbox involves (among other things) running each code submission in a separate process, in a separate JVM (as I understand it, there is no other way to reliably control the memory and CPU usage of the submitted code, short of bytecode-level checks/analysis). 此沙箱涉及(除其他事项外)在一个单独的进程中,在一个单独的JVM中运行每个代码提交(据我所知,没有其他方法可以可靠地控制所提交代码的内存和CPU使用率,只有字节码级别)检查/分析)。

I want each sandboxed process to have access to a certain database. 我希望每个沙盒进程都可以访问某个数据库。 The database is large (around 10 GB, could be significantly larger in the future) and user-submitted code might make many billions of more-or-less random accesses to the database. 数据库很大(大约10 GB,将来可能会更大),并且用户提交的代码可能对数据库进行数十亿次或多或少的随机访问。 So it is important that user-submitted code be able to access the database efficiently. 因此,用户提交的代码必须能够有效访问数据库,这一点很重要。

This is what I think I should do: load the database into memory from the main overseer process, and then give each sandboxed process read-only access to the loaded database. 我认为这是我应该做的:将数据库从主监督进程加载到内存中,然后为每个沙盒进程授予对已加载数据库的只读访问权限。 How can I do this? 我怎样才能做到这一点? (Again, I am working in Java.) (同样,我正在使用Java。)

Or do I have the wrong idea? 还是我有一个错误的主意? Should I try a different approach? 我应该尝试其他方法吗?

I don't think given the amount of data you are talking about (10GB or possibly much more) keeping it in memory is feasible. 考虑到您正在谈论的数据量(10GB或更多),将其保存在内存中并不可行。

I would recommend going with an SQLite database solution. 我建议使用SQLite数据库解决方案。

From each spawned process, you can open up the database in read-only mode , and access it through standard JDBC calls, or wrap it in some API of your own design. 在每个产生的过程中,您都可以以只读模式打开数据库,并通过标准JDBC调用对其进行访问,或者将其包装在您自己设计的某些API中。

This also has the advantage that you can move to a fully-fledged database solution if performance becomes an issue. 这还有一个优势,如果性能成为问题,您可以使用成熟的数据库解决方案。

If you don't control the format of your data in the first, you can easily write an importer that updates the SQLite database from the new data file. 如果您首先没有控制数据的格式,则可以轻松地编写一个导入器,以从新数据文件更新SQLite数据库。

Do not give them direct access to the database at all. 根本不给他们直接访问数据库的权限。 Instead provide an API for the Java programs to use, with that API having no methods for altering the content of the data-base. 而是提供供Java程序使用的API,而该API没有更改数据库内容的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM