splunk-sdk-python icon indicating copy to clipboard operation
splunk-sdk-python copied to clipboard

Blocking searches that take longer than 5 minutes time out

Open mew1033 opened this issue 7 years ago • 3 comments

I don't know if this is a byproduct of my setup, but I consistently run into this problem. If a search takes longer than 5 minutes to run, and it's set to blocking mode, it'll time out after almost exactly 300 seconds every time. Here's an example:

from splunklib import client
    import time
    import salt.client

    splunk_object = client.connect(host='splunk-endpoint.domain.com',
                                   port=8089,
                                   username="SPLUNK_USERNAME",
                                   password="SPLUNK_PASSWORD",
                                   verify=False,
                                   autologin=True)

    spl = """search index=big value="take a long time" | eventstats c BY othervalue"""
    search_kwargs = {"exec_mode": "blocking", "earliest_time": "-7d", "latest_time": "now"}

    start = time.time()
    # This should return immediately.
    splunk_search_job = splunk_object.jobs.create(spl, **search_kwargs)

    end = time.time()
    print(end-start)

And here's what happens when you run that:

    >>> start = time.time()
    >>> splunk_search_job = splunk_object.jobs.create(spl, **search_kwargs)
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "/usr/lib/python2.7/site-packages/splunklib/client.py", line 2930, in create
        response = self.post(search=query, **kwargs)
      File "/usr/lib/python2.7/site-packages/splunklib/client.py", line 808, in post
        return self.service.post(path, owner=owner, app=app, sharing=sharing, **query)
      File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 289, in wrapper
        return request_fun(self, *args, **kwargs)
      File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 71, in new_f
        val = f(*args, **kwargs)
      File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 742, in post
        response = self.http.post(path, all_headers, **query)
      File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 1208, in post
        return self.request(url, message)
      File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 1225, in request
        response = self.handler(url, message, **kwargs)
      File "/usr/lib/python2.7/site-packages/splunklib/binding.py", line 1369, in request
        response = connection.getresponse()
      File "/usr/lib64/python2.7/httplib.py", line 1113, in getresponse
        response.begin()
      File "/usr/lib64/python2.7/httplib.py", line 444, in begin
        version, status, reason = self._read_status()
      File "/usr/lib64/python2.7/httplib.py", line 400, in _read_status
        line = self.fp.readline(_MAXLINE + 1)
      File "/usr/lib64/python2.7/socket.py", line 476, in readline
        data = self._sock.recv(self._rbufsize)
      File "/usr/lib64/python2.7/ssl.py", line 757, in recv
        return self.read(buflen)
      File "/usr/lib64/python2.7/ssl.py", line 651, in read
        v = self._sslobj.read(len or 1024)
    socket.error: [Errno 104] Connection reset by peer
    >>> end = time.time()
    >>> print(end-start)
    300.682859898

Repeated attempts to run it result in the same thing. This can be solved by changing the exec_mode to normal, but then you have to do something like this:

    spl = """search index=big value="take a long time" | eventstats c BY othervalue"""
    search_kwargs = {"exec_mode": "normal", "earliest_time": "-7d", "latest_time": "now", "enable_lookups": "true"}

    start = time.time()
    # This should return immediately.
    splunk_search_job = splunk_object.jobs.create(spl, **search_kwargs)

    while not splunk_search_job.is_ready():
        time.sleep(2)

    while not splunk_search_job.is_done():
        time.sleep(5)

    end = time.time()
    print(end-start)

    834.357920885

There should probably be something in the sdk that catches those errors and reconnects.

mew1033 avatar Aug 30 '19 17:08 mew1033

@mew1033 thanks for the bug. You might be running into a limits.conf setting here.

Potentially max_time_per_process which defaults to 300 or 5 mins under the [search] stanza.

https://docs.splunk.com/Documentation/Splunk/latest/Admin/Limitsconf

shakeelmohamed avatar Aug 30 '19 18:08 shakeelmohamed

That could very well be it. Is that the expected result of running a blocking search via the sdk? Should the sdk reconnect under the hood and continue blocking on the search? Or is it appropriate for the sdk to throw an error if the blocking search times out?

mew1033 avatar Feb 03 '20 16:02 mew1033

For this case it is expected behavior, if a search is long running then blocking/oneshot APIs are not recommended and the traditional async search should be use to create a job and retrieve results. The SDK is not designed to retry on connection timeouts for synchronous searches because that's that the async search endpoints were intended for.

fantavlik avatar Oct 12 '21 15:10 fantavlik