Details
-
Improvement
-
Status: Open
-
Major
-
Resolution: Unresolved
-
JCR Oak Server 1.4.0
-
None
Description
1. Summary
By default, Apache Sling uses `OakSlingRepository` for repository construction. `OakSlingRepository` is, in turn, backed by `Oak`, which delivers some important MBeans (e.g., query statistics). However, due to the current specifics of the `OakSlingRepository` implementation, those MBeans aren’t registered in the MBean service registry (MBean Server). Therefore, those MBeans aren’t recognized by Apache Sling as MBeans, and their MBean features remain unavailable to the user. The proposed fix is to introduce the missing MBeans registration.
The problem is related to Apache Sling JCR Oak Repository Server (`org.apache.sling.jcr.oak.server`).
2. Default Apache Sling Repository Construction
Apache Sling uses Java Content Repository (JCR) for persistence. Connections to that persistence layer are performed via `javax.jcr.Repository` implementations.
There are several ways `javax.jcr.Repository` can be implemented and constructed. By default, in Apache Sling 12, an instance of `javax.jcr.Repository` is implemented and constructed in `org.apache.sling.jcr.oak.server.internal.OakSlingRepositoryManager#acquireRepository()` method(https://github.com/apache/sling-org-apache-sling-jcr-oak-server/blob/0dcb90452e0cc27db3501a21a99d7de1f72c2425/src/main/java/org/apache/sling/jcr/oak/server/internal/OakSlingRepositoryManager.java#L131-L168) from the `org.apache.sling.jcr.oak.server` bundle. To construct the `javax.jcr.Repository`, the mentioned method utilizes `org.apache.jackrabbit.oak.Oak` from `org.apache.jackrabbit:oak-core`.
3. Whiteboard in Oak Repository
During `javax.jcr.Repository` construction, `org.apache.jackrabbit.oak.Oak#with(Whiteboard)` [can be provided with a `org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard`](https://github.com/apache/jackrabbit-oak/blob/541815b62fd955d76f7069e87b9412bd7eba4daf/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java#L566-L591) from `org.apache.jackrabbit:oak-core-spi`.
The `org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard` provided to the `org.apache.jackrabbit.oak.Oak` during `javax.jcr.Repository` construction is later used inside the `org.apache.jackrabbit.oak.Oak`, among others, to perform registration of services relevant for the repository (repository-specific services). Importantly, `org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard` is just an interface and doesn’t enforce any platform-specific logic for service registration. For instance, the `org.apache.jackrabbit.oak.spi.whiteboard.DefaultWhiteboard` implementation just keeps services in the application memory, while the implementation provided via `org.apache.jackrabbit.oak.osgi.OsgiWhiteboard` performs registration of repository-specific services inside the OSGi service registry.
4. MBeans in Oak Repository
Some repository-specific services registered inside the `org.apache.jackrabbit.oak.Oak` via the `org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard` provided during `javax.jcr.Repository` construction are MBeans. In general, those MBeans provide crucial monitoring information on the state of the repository. Specifically, at least the following MBeans are supposed to be registered:
org.apache.jackrabbit.oak:name=Oak Query Statistics (Extended),type=QueryStats
org.apache.jackrabbit.oak:name=Oak Query Statistics,type=QueryStat
org.apache.jackrabbit.oak:name=Oak Repository Statistics,type=RepositoryStats
org.apache.jackrabbit.oak:name=async,type=IndexStats
org.apache.jackrabbit.oak:name=async,type=PropertyIndexAsyncReindex
org.apache.jackrabbit.oak:name=nodeCounter,type=NodeCounter
org.apache.jackrabbit.oak:name=repository manager,type=RepositoryManagement
org.apache.jackrabbit.oak:name=settings,type=QueryEngineSettings
In order to be recognized by the application as MBeans, repository-specific services that are MBeans [must be registered in the MBean service registry (MBean Server)](https://www.oracle.com/technical-resources/articles/javase/jmx.html). Otherwise, they will not expose their MBean features and will remain idle in this regard. Importantly, there are no limitations that forbid the registration of the same repository-specific service in separate and isolated service registries simultaneously. On the contrary, if a given repository-specific service is polymorphic and is supposed to be registered in separate and isolated service registries simultaneously, such registration might even be strongly advised. For instance, a given repository-specific service might be constructed as both an MBean service and an OSGi service, which aren’t the same and which provide different functionalities; hence it must be registered in both the MBean service registry (MBean Server) and the OSGi service registry. `org.apache.jackrabbit.oak.Oak` recognizes this polymorphic nature of some repository-specific services and by default uses a custom `org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard` implementation that [registers repository-specific services both in the in-memory service registry (`org.apache.jackrabbit.oak.spi.whiteboard.DefaultWhiteboard`) and additionally - in the case of services that are also MBeans - in the MBean service registry (MBean Server)](https://github.com/apache/jackrabbit-oak/blob/541815b62fd955d76f7069e87b9412bd7eba4daf/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java#L281-L359).
5. Whiteboard in Default Apache Sling Repository Construction
During `javax.jcr.Repository` construction performed by `org.apache.sling.jcr.oak.server.internal.OakSlingRepositoryManager#acquireRepository()`, the `org.apache.jackrabbit.oak.Oak` [is constructed with an instance of `org.apache.jackrabbit.oak.osgi.OsgiWhiteboard`](https://github.com/apache/sling-org-apache-sling-jcr-oak-server/blob/0dcb90452e0cc27db3501a21a99d7de1f72c2425/src/main/java/org/apache/sling/jcr/oak/server/internal/OakSlingRepositoryManager.java#L133). That instance is an implementation of `org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard` that registers repository-specific services exclusively in the OSGi service registry. It means that repository-specific services registered inside `org.apache.jackrabbit.oak.Oak` that are both OSGi services and MBeans are registered exclusively in the OSGi service registry and are not registered in the MBean service registry (MBean Server). Therefore, those services do not expose their MBean features and remain idle in this regard. Because of this, the Apache Sling user does not receive crucial monitoring information on the state of the repository delivered by those MBean features. For instance, they do not have access to Oak Query Statistics, which complicates query improvements.
6. Difference in Proprietary Apache Sling Variations
The problem of lacking MBean features delivered by repository-specific services isn’t present in at least some proprietary variations of Apache Sling. The reason for this is that those proprietary variations don’t rely on `org.apache.sling.jcr.oak.server.internal.OakSlingRepositoryManager` used by vanilla Apache Sling for `javax.jcr.Repository` construction but use custom implementations. At least some of those implementations are performed correctly so that repository-specific services that are both MBeans and OSGi services are registered in both the MBean service registry (MBean Server) and the OSGi service registry respectively. Thanks to this, users of such Apache Sling variations do not lack MBean features of repository-specific services.
7. Proposed Fix
It is proposed to fix the problem of lacking MBean features delivered by repository-specific services by registering the services that are MBeans not only in the OSGi service registry, as is already implemented, but additionally in the MBean service registry (MBean Server). To achieve this, the fix wraps `org.apache.jackrabbit.oak.osgi.OsgiWhiteboard` currently used in the `org.apache.sling.jcr.oak.server.internal.OakSlingRepositoryManager#acquireRepository()` in the decorating class `org.apache.sling.jcr.oak.server.internal.WithMBeanRegistration`. The decorator passes all method calls to the wrapped object and additionally, when registration and unregistration methods are called, registers and unregisters respectively repository-specific services that are MBeans in the MBean service registry (MBean Server). The logic of the implementation is a remake of the logic [provided by `org.apache.jackrabbit.oak.Oak`](https://github.com/apache/jackrabbit-oak/blob/541815b62fd955d76f7069e87b9412bd7eba4daf/oak-core/src/main/java/org/apache/jackrabbit/oak/Oak.java#L281-L359) and discussed above.
8. Verification of the Fix
The proposed fix is verified in the integration and unit tests provided along with the fix. Additionally, it was verified on a vanilla Apache Sling 12 instance. However, note that by default, vanilla Apache Sling 12 doesn’t provide a user interface to directly interact with or see the state of the MBean service registry nor to see the statistics provided by the MBean features from repository-specific services. For such interaction, additional tooling should be installed, like [Adobe Granite JMX Support](https://mvnrepository.com/artifact/com.adobe.granite/com.adobe.granite.jmx) (`com.adobe.granite:com.adobe.granite.jmx`, http://localhost:8080/system/console/jmx) or [Jolokia](https://mvnrepository.com/artifact/org.jolokia/jolokia-osgi) (`org.jolokia:jolokia-osgi`, http://localhost:8080/jolokia/list).