Skip to content

Commit

Permalink
Fixes tests on elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
rolljee committed Sep 7, 2023
1 parent 69f96cb commit 6c95c7e
Show file tree
Hide file tree
Showing 9 changed files with 2,006 additions and 1,776 deletions.
52 changes: 26 additions & 26 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ services:
container_name: kuzzle_nginx
depends_on:
- kuzzle_node_1
- kuzzle_node_2
- kuzzle_node_3
# - kuzzle_node_2
# - kuzzle_node_3
ports:
- '7512:7512'
volumes:
Expand All @@ -67,31 +67,31 @@ services:
interval: 2s
retries: 10

kuzzle_node_2:
<<: *kuzzle-config
container_name: kuzzle_node_2
ports:
- '17511:7512' # Kuzzle API port
- '11883:1883' # Kuzzle MQTT port
- '9230:9229' # Debug port
healthcheck:
test: ['CMD', 'curl', '-f', 'http://kuzzle:7512/_healthCheck']
timeout: 1s
interval: 2s
retries: 10
# kuzzle_node_2:
# <<: *kuzzle-config
# container_name: kuzzle_node_2
# ports:
# - '17511:7512' # Kuzzle API port
# - '11883:1883' # Kuzzle MQTT port
# - '9230:9229' # Debug port
# healthcheck:
# test: ['CMD', 'curl', '-f', 'http://kuzzle:7512/_healthCheck']
# timeout: 1s
# interval: 2s
# retries: 10

kuzzle_node_3:
<<: *kuzzle-config
container_name: kuzzle_node_3
ports:
- '17512:7512' # Kuzzle API port
- '11884:1883' # Kuzzle MQTT port
- '9231:9229' # Debug port
healthcheck:
test: ['CMD', 'curl', '-f', 'http://kuzzle:7512/_healthCheck']
timeout: 1s
interval: 2s
retries: 10
# kuzzle_node_3:
# <<: *kuzzle-config
# container_name: kuzzle_node_3
# ports:
# - '17512:7512' # Kuzzle API port
# - '11884:1883' # Kuzzle MQTT port
# - '9231:9229' # Debug port
# healthcheck:
# test: ['CMD', 'curl', '-f', 'http://kuzzle:7512/_healthCheck']
# timeout: 1s
# interval: 2s
# retries: 10

redis:
image: redis:6
Expand Down
2 changes: 1 addition & 1 deletion lib/core/backend/backendStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import { Client } from "@elastic/elasticsearch";

import { ElasticSearch } from "../../service/storage/elasticsearch";
import ElasticSearch from "../../service/storage/elasticsearch";
import { JSONObject } from "../../../index";
import { ApplicationManager, Backend } from "./index";

Expand Down
2 changes: 1 addition & 1 deletion lib/core/plugin/pluginContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { JSONObject } from "kuzzle-sdk";
import { EmbeddedSDK } from "../shared/sdk/embeddedSdk";
import PluginRepository from "./pluginRepository";
import Store from "../shared/store";
import { ElasticSearch } from "../../service/storage/elasticsearch";
import ElasticSearch from "../../service/storage/elasticsearch";
import { isPlainObject } from "../../util/safeObject";
import Promback from "../../util/promback";
import { Mutex } from "../../util/mutex";
Expand Down
2 changes: 1 addition & 1 deletion lib/core/storage/clientAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

"use strict";

const { ElasticSearch } = require("../../service/storage/elasticsearch");
const ElasticSearch = require("../../service/storage/elasticsearch").default;
const { IndexCache } = require("./indexCache");
const { isPlainObject } = require("../../util/safeObject");
const kerror = require("../../kerror");
Expand Down
66 changes: 35 additions & 31 deletions lib/service/storage/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
KImportError,
KRequestParams,
} from "../../types/storage/Elasticsearch";
import { Index } from "@elastic/elasticsearch/api/requestParams";
import { Index, IndicesCreate } from "@elastic/elasticsearch/api/requestParams";

import { TypeMapping } from "@elastic/elasticsearch/api/types";

Expand Down Expand Up @@ -94,7 +94,7 @@ let esState = esStateEnum.NONE;
* @param {storeScopeEnum} scope
* @constructor
*/
export class ElasticSearch extends Service {
export default class ElasticSearch extends Service {
public _client: StorageClient;
public _scope: scopeEnum;
public _indexPrefix: string;
Expand Down Expand Up @@ -339,15 +339,15 @@ export class ElasticSearch extends Service {
*
* @returns {Promise.<{ scrollId, hits, aggregations, total }>}
*/
async scroll(scrollId, { scrollTTL }) {
async scroll(scrollId: string, { scrollTTL }: { scrollTTL?: string } = {}) {
const _scrollTTL = scrollTTL || this._config.defaults.scrollTTL;
const esRequest = {
const esRequest: RequestParams.Scroll<Record<string, any>> = {
scroll: _scrollTTL,
scrollId,
scroll_id: scrollId,
};

const cacheKey =
SCROLL_CACHE_PREFIX + global.kuzzle.hash(esRequest.scrollId);
SCROLL_CACHE_PREFIX + global.kuzzle.hash(esRequest.scroll_id);

debug("Scroll: %o", esRequest);

Expand Down Expand Up @@ -608,7 +608,7 @@ export class ElasticSearch extends Service {
aggregations: body.aggregations,
hits,
remaining: body.remaining,
scrollId: body._scroll_id,
scroll_id: body._scroll_id,
suggest: body.suggest,
total: body.hits.total.value,
};
Expand Down Expand Up @@ -1131,7 +1131,7 @@ export class ElasticSearch extends Service {
async deleteByQuery(
index: string,
collection: string,
query: string,
query: JSONObject,
{
refresh,
size = 1000,
Expand Down Expand Up @@ -1262,7 +1262,7 @@ export class ElasticSearch extends Service {
async updateByQuery(
index: string,
collection: string,
query: string,
query: JSONObject,
changes: JSONObject,
{
refresh,
Expand Down Expand Up @@ -1449,8 +1449,8 @@ export class ElasticSearch extends Service {
client.scroll(
{
scroll: esRequest.scroll,
scrollId: _scroll_id,
} as RequestParams.Scroll,
scroll_id: _scroll_id,
},
getMoreUntilDone
);
} else {
Expand Down Expand Up @@ -2591,15 +2591,15 @@ export class ElasticSearch extends Service {
* @returns {Promise.<Object>} { items, errors }
*/
async mUpdate(
index,
collection,
documents,
index: string,
collection: string,
documents: JSONObject[],
{
refresh = undefined,
retryOnConflict = 0,
timeout = undefined,
userId = null,
}
} = {}
) {
const alias = this._getAlias(index, collection),
toImport = [],
Expand Down Expand Up @@ -2952,14 +2952,9 @@ export class ElasticSearch extends Service {

// @todo duplicated query to get documents body, mGet here and search in
// deleteByQuery
const { documents } = await this.deleteByQuery(
index,
collection,
JSON.stringify(query),
{
refresh,
}
);
const { documents } = await this.deleteByQuery(index, collection, query, {
refresh,
});

return { documents, errors: partialErrors };
}
Expand Down Expand Up @@ -3449,7 +3444,7 @@ export class ElasticSearch extends Service {
return;
}

const esRequest = {
const esRequest: IndicesCreate<KRequestBody<JSONObject>> = {
body: {
aliases: {
[this._getAlias(index, HIDDEN_COLLECTION)]: {},
Expand Down Expand Up @@ -3479,7 +3474,7 @@ export class ElasticSearch extends Service {
* To find the best value for this setting, we need to take into account
* the number of nodes in the cluster and the number of shards per index.
*/
async _getWaitForActiveShards() {
async _getWaitForActiveShards(): Promise<string> {
const { body } = await this._client.cat.nodes({ format: "json" });

const numberOfNodes = body.length;
Expand All @@ -3500,7 +3495,9 @@ export class ElasticSearch extends Service {
*
* @returns {Promise.<Array>} resolve to an array of documents
*/
async _getAllDocumentsFromQuery(esRequest) {
async _getAllDocumentsFromQuery(
esRequest: RequestParams.Search<Record<string, any>>
) {
let {
body: { hits, _scroll_id },
} = await this._client.search(esRequest);
Expand All @@ -3509,18 +3506,21 @@ export class ElasticSearch extends Service {
throw kerror.get("services", "storage", "write_limit_exceeded");
}

let documents = hits.hits.map((h) => ({ _id: h._id, _source: h._source }));
let documents = hits.hits.map((h: JSONObject) => ({
_id: h._id,
_source: h._source,
}));

while (hits.total.value !== documents.length) {
({
body: { hits, _scroll_id },
} = await this._client.scroll({
scroll: esRequest.scroll,
scrollId: _scroll_id,
} as RequestParams.Scroll));
scroll_id: _scroll_id,
}));

documents = documents.concat(
hits.hits.map((h) => ({
hits.hits.map((h: JSONObject) => ({
_id: h._id,
_source: h._source,
}))
Expand Down Expand Up @@ -3609,7 +3609,7 @@ export class ElasticSearch extends Service {
* @param {[type]} id [description]
* @returns {[type]} [description]
*/
async clearScroll(id: string) {
async clearScroll(id?: string) {
if (id) {
debug("clearing scroll: %s", id);
await this._client.clearScroll({ scroll_id: id });
Expand Down Expand Up @@ -3836,3 +3836,7 @@ function _isObjectNameValid(name: string): boolean {

return valid;
}

// TODO: Remove this function when we move to Jest
// This is kept because we use an old ReRequire that use require() instead of import
module.exports = ElasticSearch;

Check failure on line 3842 in lib/service/storage/elasticsearch.ts

View workflow job for this annotation

GitHub Actions / Lint - Node.js LTS 18

Insert `⏎`
Loading

0 comments on commit 6c95c7e

Please sign in to comment.