Skip to content

feature request - quit esql query by interrupt windows powershell #2758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mortenb-buypass opened this issue Jan 21, 2025 · 3 comments
Closed

Comments

@mortenb-buypass
Copy link

I find it hard to work on large indexes, seems like I'm unable to interrupt a query once it has fired.

But I'm unable to get <ctrl+c> or any interrupt to work:


class Esql:
    def __init__(self, env:str='test', timeout:int=60, timefield:str='time'):
        self.client = elasticsearch.Elasticsearch(hosts[env], request_timeout=timeout)
:
        try:
            response = self.client.esql.query(
                query=query, format=format
            )
        except (elasticsearch.exceptions.BadRequestError) as e:
            log.error(e)
            return None
        except (KeyboardInterrupt, SystemExit):
            log.error("interrupted")
            sys.exit(-1)
        except Exception as e:
            log.error(f"unmapped exception {e}")
            return None


20250121132640.032|INFO|C:\dist\elk\tools\esql.py:468|query: FROM bps-trace-* | WHERE @timestamp >= "2025-01-21T11:25:37.727Z" AND @timestamp <= "2025-01-21T12:25:37.728Z" | WHERE bps.application=="id-pro-trk-trk-fido2-service-test12" | KEEP `message`
20250121132713.958|ERROR|C:\dist\elk\tools\esql.py:284|interrupted

I get KeyboardInterrupt only after the query has returned after 23sec

And setting the request_timeout will exit if it reached the value.

A way to quit it at anytime would be nice.

@mortenb-buypass mortenb-buypass changed the title feature request - quit query feature request - quit esql query by interrupt Jan 21, 2025
@pquentin
Copy link
Member

pquentin commented Jan 22, 2025

Hello, I'm unable to reproduce. Using this snippet:

import os
from elasticsearch import Elasticsearch

client = Elasticsearch(
    "http://localhost:9200", api_key=os.environ["ES_LOCAL_API_KEY"], request_timeout=60
)
print(client.info())
print("esql")
client.esql.query(query="FROM kibana_sample_data_ecommerce")

I then ran Elasticsearch with start-local and loaded the sample ecommerce data in Kibana. I then Ctrl-C as soon as I see "esql" and get a KeyboardInterrupt exception as expected, while the client was waiting for the response:

$ source elastic-start-local/.env
$ ES_LOCAL_API_KEY=$ES_LOCAL_API_KEY python issues/2758.py 
{'name': '2ff212ac86f6', 'cluster_name': 'docker-cluster', 'cluster_uuid': 'gjECK54RSpm4NKOs5Vf5ng', 'version': {'number': '8.17.0', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '2b6a7fed44faa321997703718f07ee0420804b41', 'build_date': '2024-12-11T12:08:05.663969764Z', 'build_snapshot': False, 'lucene_version': '9.12.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}
esql
^CTraceback (most recent call last):
  File ".../issues/2758.py", line 9, in <module>
    client.esql.query(query="FROM kibana_sample_data_ecommerce")
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../elasticsearch/_sync/client/utils.py", line 455, in wrapped
    return api(*args, **kwargs)
  File ".../elasticsearch/_sync/client/esql.py", line 401, in query
    return self.perform_request(  # type: ignore[return-value]
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        "POST",
        ^^^^^^^
    ...<5 lines>...
        path_parts=__path_parts,
        ^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File ".../elasticsearch/_sync/client/_base.py", line 423, in perform_request
    return self._client.perform_request(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        method,
        ^^^^^^^
    ...<5 lines>...
        path_parts=path_parts,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File ".../elasticsearch/_sync/client/_base.py", line 271, in perform_request
    response = self._perform_request(
        method,
    ...<4 lines>...
        otel_span=otel_span,
    )
  File ".../elasticsearch/_sync/client/_base.py", line 316, in _perform_request
    meta, resp_body = self.transport.perform_request(
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        method,
        ^^^^^^^
    ...<8 lines>...
        otel_span=otel_span,
        ^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File ".../.venv/lib/python3.13/site-packages/elastic_transport/_transport.py", line 342, in perform_request
    resp = node.perform_request(
        method,
    ...<3 lines>...
        request_timeout=request_timeout,
    )
  File ".../.venv/lib/python3.13/site-packages/elastic_transport/_node/_http_urllib3.py", line 167, in perform_request
    response = self.pool.urlopen(
        method,
    ...<4 lines>...
        **kw,  # type: ignore[arg-type]
    )
  File ".../.venv/lib/python3.13/site-packages/urllib3/connectionpool.py", line 789, in urlopen
    response = self._make_request(
        conn,
    ...<10 lines>...
        **response_kw,
    )
  File ".../.venv/lib/python3.13/site-packages/urllib3/connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
  File ".../.venv/lib/python3.13/site-packages/urllib3/connection.py", line 507, in getresponse
    httplib_response = super().getresponse()
  File ".../lib/python3.13/http/client.py", line 1452, in getresponse
    response.begin()
    ~~~~~~~~~~~~~~^^
  File ".../lib/python3.13/http/client.py", line 355, in begin
    version, status, reason = self._read_status()
                              ~~~~~~~~~~~~~~~~~^^
  File ".../lib/python3.13/http/client.py", line 316, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
               ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File ".../lib/python3.13/socket.py", line 743, in readinto
    return self._sock.recv_into(b)
           ~~~~~~~~~~~~~~~~~~~~^^^
KeyboardInterrupt

@mortenb-buypass
Copy link
Author

Tank for this feedback, I tested now inside WSL and there it works just great. But I'm unable to get it to work in windows, I believe I have a plain powershell shell

@mortenb-buypass mortenb-buypass changed the title feature request - quit esql query by interrupt feature request - quit esql query by interrupt windows powershell Jan 22, 2025
@pquentin
Copy link
Member

Ah, that makes sense, thank you. This isn't specific to this library, though, but is a more general issue with Powershell and Python: https://stackoverflow.com/questions/42039231/ctrl-c-for-quitting-python-in-powershell-now-not-working

@pquentin pquentin closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants