<commit_before># RESTHeart native image with GraalVM

## build native image

> GraalVM required version: >= 21.0.0 (https://www.graalvm.org/downloads/)

Build image for local OS

```bash
$ mvn clean package -Pnative
```

Build Linux image

```bash
$ docker run -it --rm \
    -v "$PWD":/opt/app  \
    -v "$HOME"/.m2:/root/.m2 \
    softinstigate/graalvm-maven clean package -Pnative
```

native-image arguments are defined in file `core/src/main/resources/META-INF/native-image/org.restheart/restheart/native-image.properties`

## start native image

Start RESTHeart

```bash
$ ./core/target/restheart-native core/etc/restheart.yml -e core/etc/uber.properties
```

## Generate native-image build configuration

Start RESTHeart with test configuration and the `native-image-agent`

```bash
$ mvn clean package
$ cp test-plugins/target/restheart-test-plugins.jar core/target/plugins
$ java -agentlib:native-image-agent=config-merge-dir=core/src/main/resources/META-INF/native-image/org.restheart/restheart/ -jar core/target/restheart.jar core/etc/test/restheart.yml
```

Execute tests, this makes the `native-image-agent` collecting all needed configuration

```bash
$ cd core
$ mvn surefire:test -DskipITs=false -Dtest=\*IT
```

Stop restheart. this makes the [Assisted Configuration of Native Image Builds](https://github.com/oracle/graal/blob/master/substratevm/BuildConfiguration.md#assisted-configuration-of-native-image-builds) being updated.

The generated configuration are merged into the existing ones in directory `core/src/main/resources/META-INF/native-image/org.restheart/restheart`

*Some files need to be manually edited*

Remove all references to classes of packages `org.restheart.test`, `org.graalvm` (keep `org.graalvm.polyglot.Value`) and `com.oracle.truffle` from `reflect-config.json`

Also make sure to move:

- references to classes in package `org.restheart.security` to `security/src/main/resources/META-INF/native-image/org.restheart/restheart-security/reflect-config.json`

- references to classes in package `org.restheart.mongodb` to `mongodb/src/main/resources/META-INF/native-image/org.restheart/restheart-mongodb/reflect-config.json`

- references to classes in packages `org.restheart.ployglot`, `graphql` to `ployglot/src/main/resources/META-INF/native-image/org.restheart/restheart-ployglot/reflect-config.json`


The following fields must be configured with `allowWrite: true`

```json
{
  "name":"io.undertow.security.impl.BasicAuthenticationMechanism",
  "fields":[{"name":"identityManager", "allowWrite" : true}]
},
{
  "name":"graphql.schema.GraphQLScalarType",
  "fields":[{"name":"coercing", "allowWrite":true}]
},
{
  "name":"io.github.classgraph.ScanResultObject",
  "allDeclaredFields":true,
  "fields":[{"name":"scanResult", "allowWrite":true}]
}
```

## issues

### [OPEN] build error upgrading caffeine

upgrading caffeine above 2.8.8 breaks native-image build

### [FIXED] runtime error due to jboss-threads required by xnio

see:

- https://github.com/SoftInstigate/graalvm-undertow-issue
- https://github.com/oracle/graal/issues/3020
- https://issues.redhat.com/browse/UNDERTOW-1811

fixed by remove unsafe calls and disable jboss enhanced executor on native build: https://github.com/SoftInstigate/graalvm-undertow-issue/commit/ad36bd2d5b5397800f3494613e7fecc22615beab

### [FIXED] build fails due to ClassNotFoundException from mongodb-driver
<commit_msg>:memo: Update GraalVM doc [skip ci]<commit_after># RESTHeart native image with GraalVM

## build native image

> GraalVM required version: >= 21.0.0 (https://www.graalvm.org/downloads/)

Build image for local OS

```bash
$ mvn clean package -Pnative
```

Build Linux image

```bash
$ docker run -it --rm \
    -v "$PWD":/opt/app  \
    -v "$HOME"/.m2:/root/.m2 \
    softinstigate/graalvm-maven clean package -Pnative
```

native-image arguments are defined in file `core/src/main/resources/META-INF/native-image/org.restheart/restheart/native-image.properties`

## start native image

Start RESTHeart

```bash
$ ./core/target/restheart-native core/etc/restheart.yml -e core/etc/uber.properties
```

## Generate native-image build configuration

Start RESTHeart with test configuration and the `native-image-agent`

```bash
$ mvn clean package
$ cp test-plugins/target/restheart-test-plugins.jar core/target/plugins
$ java -agentlib:native-image-agent=config-merge-dir=core/src/main/resources/META-INF/native-image/org.restheart/restheart/ -jar core/target/restheart.jar core/etc/test/restheart.yml
```

Execute tests, this makes the `native-image-agent` collecting all needed configuration

```bash
$ cd core
$ mvn surefire:test -DskipITs=false -Dtest=\*IT
```

Stop restheart. this makes the [Assisted Configuration of Native Image Builds](https://github.com/oracle/graal/blob/master/substratevm/BuildConfiguration.md#assisted-configuration-of-native-image-builds) being updated.

The generated configuration are merged into the existing ones in directory `core/src/main/resources/META-INF/native-image/org.restheart/restheart`

*Some files need to be manually edited*

Remove all references to classes of packages `org.restheart.test`, `org.graalvm` (keep `org.graalvm.polyglot.Value`) and `com.oracle.truffle` from `reflect-config.json`

Also make sure to move:

- references to classes in package `org.restheart.security` to `security/src/main/resources/META-INF/native-image/org.restheart/restheart-security/reflect-config.json`

- references to classes in package `org.restheart.mongodb` to `mongodb/src/main/resources/META-INF/native-image/org.restheart/restheart-mongodb/reflect-config.json`

- references to classes in packages `org.restheart.ployglot`, `graphql` to `ployglot/src/main/resources/META-INF/native-image/org.restheart/restheart-ployglot/reflect-config.json`


The following fields must be configured with `allowWrite: true`

```json
{
  "name":"io.undertow.security.impl.BasicAuthenticationMechanism",
  "fields":[{"name":"identityManager", "allowWrite" : true}]
},
{
  "name":"graphql.schema.GraphQLScalarType",
  "fields":[{"name":"coercing", "allowWrite":true}]
},
{
  "name":"io.github.classgraph.ScanResultObject",
  "allDeclaredFields":true,
  "fields":[{"name":"scanResult", "allowWrite":true}]
}
```

## issues

### [OPEN] build error upgrading caffeine

upgrading caffeine above 2.8.8 breaks native-image build

### [FIXED] runtime error due to jboss-threads required by xnio

see:

- https://github.com/SoftInstigate/graalvm-undertow-issue
- https://github.com/oracle/graal/issues/3020
- https://issues.redhat.com/browse/UNDERTOW-1811

fixed by remove unsafe calls and disable jboss enhanced executor on native build: https://github.com/SoftInstigate/graalvm-undertow-issue/commit/ad36bd2d5b5397800f3494613e7fecc22615beab

> reported "f97bdb5 adds the missing substitutions" https://github.com/oracle/graal/issues/3020#issuecomment-815098289 should be fixed in next (>21.0.0.r11) release

### [FIXED] build fails due to ClassNotFoundException from mongodb-driver

{'fraction_non_alphanumeric': 0.13288609763060233, 'fraction_numerical': 0.014844419069369112, 'mean_word_length': 4.953271028037383, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 13, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1391633', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2234, 'n_words': 507}