Skip to content

Commit

Permalink
Make some ES timeouts configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
bkis committed Jul 23, 2024
1 parent 63be428 commit e68c837
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
10 changes: 8 additions & 2 deletions .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ TEKST_ES__HOST=es
# TEKST_ES__PREFIX=tekst
# default: tekst

# TEKST_ES__INIT_TIMEOUT_S=120
# default: 120
# TEKST_ES__TIMEOUT_INIT_S=240
# default: 240

# TEKST_ES__TIMEOUT_GENERAL_S=30
# default: 30

# TEKST_ES__TIMEOUT_SEARCH_S=30
# default: 30

# TEKST_ES__MAX_FIELD_MAPPINGS=1000
# default: 1000
Expand Down
8 changes: 7 additions & 1 deletion Tekst-API/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@
# TEKST_ES__PREFIX=tekst
# default: tekst

# TEKST_ES__INIT_TIMEOUT_S=240
# TEKST_ES__TIMEOUT_INIT_S=240
# default: 120

# TEKST_ES__TIMEOUT_GENERAL_S=30
# default: 30

# TEKST_ES__TIMEOUT_SEARCH_S=30
# default: 30

# TEKST_ES__MAX_FIELD_MAPPINGS=1000
# default: 1000

Expand Down
11 changes: 10 additions & 1 deletion Tekst-API/tekst/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ class ElasticsearchConfig(ConfigSubSection):
host: str = "127.0.0.1"
port: int = 9200
prefix: str = "tekst"
init_timeout_s: int = 240
timeout_init_s: int = 240
timeout_general_s: int = 30
timeout_search_s: str = "30s"
max_field_mappings: int = 1000

@field_validator("host", mode="before")
Expand All @@ -152,6 +154,13 @@ def url_quote(cls, v) -> str:
return v
return quote(str(v).encode("utf8"), safe="")

@field_validator("timeout_search_s", mode="before")
@classmethod
def timeout_int_to_time_value(cls, v) -> str:
if isinstance(v, int):
return f"{v}s"
return v

@computed_field
@property
def uri(self) -> str:
Expand Down
14 changes: 10 additions & 4 deletions Tekst-API/tekst/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
async def _wait_for_es() -> bool:
global _es_client
if _es_client is not None:
for i in range(_cfg.es.init_timeout_s):
for i in range(_cfg.es.timeout_init_s):
if _es_client.ping():
return True
if i % 10 == 0:
log.debug(
f"Waiting for Elasticsearch service at {_cfg.es.uri} "
f"({i}/{_cfg.es.init_timeout_s} seconds)..."
f"({i}/{_cfg.es.timeout_init_s} seconds)..."
)
await asyncio.sleep(1)
else:
Expand All @@ -69,7 +69,7 @@ async def init_es_client() -> Elasticsearch:
global _es_client
if _es_client is None:
log.info("Initializing Elasticsearch client...")
_es_client = Elasticsearch(_cfg.es.uri)
_es_client = Elasticsearch(_cfg.es.uri, timeout=_cfg.es.timeout_general_s)
if not await _wait_for_es():
raise RuntimeError("Waiting for Elasticsearch client exceeded timeout!")
return _es_client
Expand Down Expand Up @@ -168,7 +168,11 @@ async def create_indices_task(
client.indices.delete(index=existing_indices)

# perform initial bogus search (to initialize index stats)
client.search(index=IDX_ALIAS, query={"match_all": {}})
client.search(
index=IDX_ALIAS,
query={"match_all": {}},
timeout=_cfg.es.timeout_search_s,
)

# update last indexing time
await update_settings(indices_created_at=datetime.utcnow())
Expand Down Expand Up @@ -430,6 +434,7 @@ async def search_quick(
track_scores=True,
sort=SORTING_PRESETS.get(settings_general.sorting_preset),
source={"includes": QUERY_SOURCE_INCLUDES},
timeout=_cfg.es.timeout_search_s,
),
)

Expand Down Expand Up @@ -487,5 +492,6 @@ async def search_advanced(
track_scores=True,
sort=SORTING_PRESETS.get(settings_general.sorting_preset),
source={"includes": QUERY_SOURCE_INCLUDES},
timeout=_cfg.es.timeout_search_s,
),
)
4 changes: 3 additions & 1 deletion docs/content/setup/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ Configuration for the connection to the Elasticsearch server
| `TEKST_ES__HOST` | Elasticsearch host (String – default: `127.0.0.1`) |
| `TEKST_ES__PORT` | Elasticsearch port (Integer – default: `9200`) |
| `TEKST_ES__PREFIX` | Elasticsearch prefix (for index, templates, etc.) (String – default: `tekst`) |
| `TEKST_ES__INIT_TIMEOUT_S` | Timeout for waiting for Elasticsearch service to be available on startup (Integer – default: `240`) |
| `TEKST_ES__TIMEOUT_INIT_S` | Timeout for waiting for Elasticsearch service to be available on startup, in seconds (Integer – default: `240`) |
| `TEKST_ES__TIMEOUT_GENERAL_S` | General client timeout for requests to Elasticsearch, in seconds (Integer – default: `30`) |
| `TEKST_ES__TIMEOUT_SEARCH_S` | Timeout for search reqests to Elasticsearch, in seconds (Integer – default: `30`) |
| `TEKST_ES__MAX_FIELD_MAPPINGS` | Max. number of field mappings per search index – given there is enough memory, this can be increased in case there are e.g. annotation resources with many distinct annotation keys (these are dynamically mapped fields). The admin maintenance UI shows a warning if an index is about to hit this value. Any field mapping surpassing this value will be ignored and won't be searchable. (Integer – default: `1000`) |


Expand Down

0 comments on commit e68c837

Please sign in to comment.