README.md 11.1 KB
Newer Older
1
2
# Rare project - Data discovery

3
4
## Contribute

5
You might probably want to know how to contribute to the federation of data. That's great, let's have a look at the [WheatIS/Plant guide](./HOW-TO-JOIN-WHEATIS-AND-PLANT-FEDERATIONS.md) or the [RARe guide](./HOW-TO-JOIN-RARe-FEDERATION.md) to know how to.
6

Raphaël Flores's avatar
Raphaël Flores committed
7
If you do want to contribute to code or even only install the program on-premise it's great also, just keep reading below.
8

9
10
11
12
13
14
15
16
17
18
19
## Setup

### Backend

The project uses Spring (5.x) for the backend,
with Spring Boot.

You need to install:

- a recent enough JDK8

20
The application expects to connect on an Elasticsearch instance running on `http://127.0.0.1:9200`.
Exbrayat Cédric's avatar
Exbrayat Cédric committed
21
22
23
24
To have such an instance, simply run:

    docker-compose up

25
And this will start Elasticsearch and a Kibana instance (allowing to explore the data on http://localhost:5601).
Exbrayat Cédric's avatar
Exbrayat Cédric committed
26

27
28
29
Then at the root of the application, run `./gradlew build` to download the dependencies.
Then run `./gradlew bootRun` to start the app.

Exbrayat Cédric's avatar
Exbrayat Cédric committed
30
31
32
You can stop the Elastic Search and Kibana instances by running:

    docker-compose stop
33
34
35
36
37
38
    
or 

    docker-compose down 
    
to also remove the stopped containers as well as any networks that were created.
Exbrayat Cédric's avatar
Exbrayat Cédric committed
39

40
41
### Frontend

42
The project uses Angular (7.x) for the frontend, with the Angular CLI.
43
44
45

You need to install:

Cyril Pommier's avatar
Cyril Pommier committed
46
- a recent enough NodeJS (8.11+). Node 10 is recommended for Angular 7.
47
48
49
50
51
- Yarn as a package manager (see [here to install](https://yarnpkg.com/en/docs/install))

Then in the `frontend` directory, run `yarn` to download the dependencies.
Then run `yarn start` to start the app, using the proxy conf to reroute calls to `/api` to the backend.

Raphaël Flores's avatar
Raphaël Flores committed
52
The application will be available on:
53
54
- http://localhost:4000/rare-dev for RARe (runs with: `yarn start:rare` or simply `yarn start`)
- http://localhost:4100/wheatis-dev for WheatIS (runs with: `yarn start:wheatis`)
55
56
57
58
59
60

## Build

To build the app, just run:

    ./gradlew assemble
Raphaël Flores's avatar
Raphaël Flores committed
61
62
or 
    ./gradlew assemble -Papp=wheatis
63

Raphaël Flores's avatar
Raphaël Flores committed
64
This will build a standalone jar at `backend/build/libs/rare.jar` or  `backend/build/libs/wheatis.jar`, that you can run with:
65
66

    java -jar backend/build/libs/rare.jar
Raphaël Flores's avatar
Raphaël Flores committed
67
68
69
    java -jar backend/build/libs/wheatis.jar

And the full app run on:
70

Raphaël Flores's avatar
Raphaël Flores committed
71
- http://localhost:8080/rare-dev
72
- http://localhost:8180/wheatis-dev
73
74
75
76
77
78


## CI

The `.gitlab-ci.yml` file describes how Gitlab is running the CI jobs.

79
80
81
It uses a base docker image named `urgi/docker-browsers`
available on [DockerHub](https://hub.docker.com/r/urgi/docker-browsers/)
and [INRA-MIA Gitlab](https://forgemia.inra.fr/urgi-is/docker-rare).
Raphaël Flores's avatar
Raphaël Flores committed
82
The image is based on `openjdk:8` and adds all stuff needed to run the tests
83
(ie. a Chrome binary with a headless Chrome in `--no-sandbox` mode).
84
85
86
87
88
89

We install `node` and `yarn` in `/tmp` (this is not the case for local builds)
to avoid symbolic links issues on Docker.

You can approximate what runs on CI by executing:

90
    docker run --rm -v "$PWD":/home/rare -w /home/rare urgi/docker-browsers ./gradlew build
Jean-Baptiste Nizet's avatar
Jean-Baptiste Nizet committed
91

92
93
94
95
96
97
98
99
100
101
102
103
Or also run a gitlab-runner as Gitlab-CI would do (minus the environment variables and caching system):

    gitlab-runner exec docker test

## Documentation

An API documentation describing most of the webservices can be generated using the
build task `asciidoctor`, which executes tests and generates documentation based on snippets generated
by these tests. The documentation is generated in the folder `backend/build/asciidoc/html5/index.html`/

    ./gradlew asciidoctor

Jean-Baptiste Nizet's avatar
Jean-Baptiste Nizet committed
104
105
## Harvest

106
Harvesting (i.e. importing documents stored in JSON files into Elasticsearch) consists in
107
creating the necessary index and aliases and Elasticsearch templates.
Jean-Baptiste Nizet's avatar
Jean-Baptiste Nizet committed
108

Raphaël Flores's avatar
Raphaël Flores committed
109
To create the index and its aliases execute the script below for local dev environment:
110
111
112

    ./scripts/createIndexAndAliases.sh

113
This script is a wrapper for the `./scripts/createIndexAndAliases4CI.sh` which handle some parameters to create
Raphaël Flores's avatar
Raphaël Flores committed
114
indices, aliases and so on, on another (possible remote) Elasticsearch for fitting to a specific environment:
115
116

    ./scripts/createIndexAndAliases4CI.sh -host localhost -app rare -env dev
Jean-Baptiste Nizet's avatar
Jean-Baptiste Nizet committed
117

Raphaël Flores's avatar
Raphaël Flores committed
118
You can run the scripts:
119
120

    ./scripts/harvestRare.sh
Raphaël Flores's avatar
Raphaël Flores committed
121
    ./scripts/harvestWheatis.sh
122
    
Raphaël Flores's avatar
Raphaël Flores committed
123
to trigger a harvest of the resources stored in the Git LFS directories `data/rare` and `data/wheatis` respectively.
Raphaël Flores's avatar
Raphaël Flores committed
124

125
126
## Indices and aliases

Raphaël Flores's avatar
Raphaël Flores committed
127
The application uses several physical indices, which (at least the resources index) can be rolled over automatically based on the policies defined in the
128
129
130
`./backend/src/test/resources/fr/inra/urgi/datadiscovery/dao/*_policy.json` files. This is based on the
[Index Lifecyle Management](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/index-lifecycle-management.html)
provided by Elasticsearch.
131

Raphaël Flores's avatar
Raphaël Flores committed
132
133
134
135
 * one to store physical resources, containing the main content
 * one to store suggestions, use for the search type-ahead feature only

Both indices must be created explicitly before using the application. If not, requests to the web services will return errors.
136

Raphaël Flores's avatar
Raphaël Flores committed
137
138
139
140
Each index and alias below refers to `rare` application in `dev` environment, the equivalent shall be created for `wheatis` 
app in `dev` environment as same as in `beta` or `prod` environments. For brevity, only `rare-dev` is explained here.
{: .alert .alert-info}

141
142
143
The application doesn't use the physical resources index directly. Instead, it uses two aliases, that must be created 
before using the application:

144
145
 * `rare-dev-resource-index` is the alias used by the application to search for documents
 * `rare-dev-resource-harvest-index` is the alias used by the application to store documents when the harvest is triggered.
146
147
 
In normal operations, these two aliases should refer to the same physical resource index. The script
Raphaël Flores's avatar
Raphaël Flores committed
148
`createIndexAndAliases.sh` creates a physical index (named `rare-dev-resource-physical-index`) and creates these two aliases 
149
150
151
referring to this physical index.

Once the index and the aliases have been created, a harvest can be triggered. The first operation that a harvest
152
does is to create or update (put) the mapping for the document entity into the index aliased by `rare-dev-resource-harvest-index`. 
Raphaël Flores's avatar
Raphaël Flores committed
153
Then it parses the JSON files and stores them into this same index. Since the `rare-dev-resource-index` alias 
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
normally refers to the same physical index, searches will find the resources stored by the harvester.

### Why two aliases

Using two aliases is useful when deleting obsolete documents. This is actually done by removing everything
and then harvesting the new JSON files again, to re-populate the index from scratch.

Two scenarios are possible:

#### Deleting with some downtime

The harvest duration depends on the performance of Elasticsearch, of the performance of the harvester, and 
of course, of the number of documents to harvest. If you don't mind about having a period of time 
where the documents are not available, you can simply 

 - delete the physical index;
 - re-create it with its aliases;
 - trigger a new harvest.
 
Keep in mind that, with the current known set of documents (17172), on a development machine where everything is running
concurrently, when both the Elasticsearch server and the application are hot, a harvest only takes 12 seconds.
So, even if you have 10 times that number of documents (170K documents), it should only take around 2 minutes of downtime.
If you have 100 times that number of documents (1.7M documents), it should take around 20 minutes, which is still not a 
very long time.

(Your mileage may vary: I assumed a linear complexity here).

181
182
183
Here are curl commands illustrating the above scenario:
```
# delete the physical index and its aliases
Raphaël Flores's avatar
Raphaël Flores committed
184
curl -X DELETE "localhost:9200/rare-dev-resource-physical-index"
185
186

# recreate the physical index and its aliases
Raphaël Flores's avatar
Raphaël Flores committed
187
curl -X PUT "localhost:9200/rare-dev-resource-physical-index" -H 'Content-Type: application/json' -d'
188
189
{
    "aliases" : {
Raphaël Flores's avatar
Raphaël Flores committed
190
191
        "rare-dev-resource-index" : {},
        "rare-dev-resource-harvest-index" : {}
192
    }
193
    "settings": ...
194
195
196
197
}
'
```

198
199
**NOTE**: Every time a physical index is created, the settings must be specified, the same ay as in the 
`createIndexAndAliases.sh` script. The exact content of the settings is omitted here for brevity and readability.
Raphaël Flores's avatar
Raphaël Flores committed
200
{: .alert .alert-info}
201

202
203
## Spring Cloud config

204
On bootstrap, the application will try to connect to a remote Spring Cloud config server to fetch its configuration.
205
The details of this remote server are filled in the `bootstrap.yml` file.
206
By default, it tries to connect to the local server on http://localhost:8888
207
208
209
210
211
212
213
but it can of course be changed, or even configured via the `SPRING_CONFIG_URI` environment variable.

It will try to fetch the configuration for the application name `rare`, and the default profile.
If such a configuration is not found, it will then fallback to the local `application.yml` properties.
To avoid running the Spring Cloud config server every time when developing the application,
all the properties are still available in `application.yml` even if they are configured on the remote Spring Cloud server as well.

214
If you want to use the Spring Cloud config app locally, see https://forgemia.inra.fr/urgi-is/data-discovery-config
215
216
217
218
219
220

The configuration is currently only read on startup,
meaning the application has to be reboot if the configuration is changed on the Spring Cloud server.
For a dynamic reload without restarting the application, 
see http://cloud.spring.io/spring-cloud-static/Finchley.SR1/single/spring-cloud.html#refresh-scope
to check what has to be changed.
221

222
223
224
225
226
In case of testing configuration from the config server, one may use a dedicated branch on `data-discovery-config` project 
and append the `--spring.cloud.config.label=<branch name to test>` parameter when starting the application's executable jar.
More info on how pass a parameter to a Spring Boot app: 
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
## Building other apps

By default, the built application is RARe. But this project actually allows building other
applications (WheatIS, for the moment, but more could come).

To build a different app, specify an `app` property when building. For example, to assemble
the WheatIS app, run the following command

    ./gradlew assemble -Papp=wheatis
    
You can also run the backend WheatIS application using

    ./gradlew bootRun -Papp=wheatis
    
Adding this property has the following consequences:

 - the generated jar file (in `backend/build/libs`) is named `wheatis.jar` instead of `rare.jar`;
 - the Spring active profile in `bootstrap.yml` is `wheatis-app` instead of `rare-app`;
245
246
 - the frontend application built and embedded inside the jar file is the WheatIS frontend application instead of the
 RARe frontend application, i.e. the frontend command `yarn build:wheatis` is executed instead of the command `yarn:rare`.
247
248
249
250
 
Since the active Spring profile is different, all the properties specific to this profile
are applies. In particular:
 
Raphaël Flores's avatar
Raphaël Flores committed
251
 - the context path of the application is `/wheatis-dev` instead of `/rare-dev`; 
252
253
 - the Elasticsearch prefix used for the index aliases is different.

Raphaël Flores's avatar
Raphaël Flores committed
254
See the `backend/src/main/resources/application.yml` file for details.
255
256
257

You can adapt the elasticsearch index used with the following parameter
java -jar backend/build/libs/data-discovery.jar --data-discovery.elasticsearch-prefix="data-discovery-staging-"