Repository: GoogleCloudPlatform/professional-services

Issue:
asset-inventory : job stay running for many hours
Hello,

We faced an issue yesterday with a Dataflow job using the asset-inventory feature.

The job stay running for more than 10 hours. Usually it takes 2h to run.
We canceled the job, and run it again. It finished successfully in 2h.

We got many logs like the following (we didn't have them usually but I can't be sure it's the root cause)

```
Operation ongoing for over 300.19 seconds in state process-msecs in step s8 . Current Traceback:
  File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.7/site-packages/dataflow_worker/start.py", line 144, in <module>
    main()
  File "/usr/local/lib/python3.7/site-packages/dataflow_worker/start.py", line 140, in main
    batchworker.BatchWorker(properties, sdk_pipeline_options).run()
  File "/usr/local/lib/python3.7/site-packages/dataflow_worker/batchworker.py", line 844, in run
    deferred_exception_details=deferred_exception_details)
  File "/usr/local/lib/python3.7/site-packages/dataflow_worker/batchworker.py", line 649, in do_work
    work_executor.execute()
  File "/usr/local/lib/python3.7/site-packages/dataflow_worker/executor.py", line 179, in execute
    op.start()
  File "./asset_inventory/import_pipeline.py", line 150, in add_input
    resource_schema = self.element_to_schema(element)
  File "./asset_inventory/import_pipeline.py", line 147, in element_to_schema
    'iam_policy' in element)
  File "/usr/local/lib/python3.7/site-packages/asset_inventory/api_schema.py", line 451, in bigquery_schema_for_resource
    discovery_doc_url)
  File "/usr/local/lib/python3.7/site-packages/asset_inventory/api_schema.py", line 84, in _get_discovery_document_versions
    dd = cls._get_discovery_document(dd_url)
  File "/usr/local/lib/python3.7/site-packages/asset_inventory/api_schema.py", line 44, in _get_discovery_document
    response = requests.get(dd_url)
  File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 421, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/lib/python3.7/http/client.py", line 1369, in getresponse
    response.begin()
  File "/usr/local/lib/python3.7/http/client.py", line 310, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.7/http/client.py", line 271, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/local/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "/usr/local/lib/python3.7/ssl.py", line 1071, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/local/lib/python3.7/ssl.py", line 929, in read
    return self._sslobj.read(len, buffer)
```

Are you able to confirm it's the root cause?
If it is, are you able to explain why it keep our job running for many hours yesterday?

Do you think you can provide any update to the asset-inventory feature to avoid this kind of problem? For example to be able to make the job failed after X minutes to avoid this kind of situation?

Best regards,
Sébastien



Discussion:

Resolving patch:
diff --git a/tools/asset-inventory/asset_inventory/api_schema.py b/tools/asset-inventory/asset_inventory/api_schema.py
--- a/tools/asset-inventory/asset_inventory/api_schema.py
+++ b/tools/asset-inventory/asset_inventory/api_schema.py
@@ -41,7 +41,7 @@ def _get_discovery_document(cls, dd_url):
         discovery_document = None
         # Ignore discovery document urls that aren't urls.
         if dd_url and dd_url.startswith('http'):
-            response = requests.get(dd_url)
+            response = requests.get(dd_url, timeout=3.05)
             if response.status_code == 200:
                 try:
                     discovery_document = response.json()
diff --git a/tools/asset-inventory/asset_inventory/import_pipeline.py b/tools/asset-inventory/asset_inventory/import_pipeline.py
--- a/tools/asset-inventory/asset_inventory/import_pipeline.py
+++ b/tools/asset-inventory/asset_inventory/import_pipeline.py
@@ -36,6 +36,7 @@
 import random
 
 import apache_beam as beam
+from apache_beam.coders import Coder
 from apache_beam.io import ReadFromText
 from apache_beam.io.filesystems import FileSystems
 from apache_beam.options.pipeline_options import PipelineOptions
@@ -50,7 +51,7 @@
 from google.cloud import bigquery
 
 
-class JsonCoder(object):
+class JsonCoder(Coder):
     """A coder interpreting each line as a JSON string."""
 
     def encode(self, x):
diff --git a/tools/asset-inventory/setup.py b/tools/asset-inventory/setup.py
--- a/tools/asset-inventory/setup.py
+++ b/tools/asset-inventory/setup.py
@@ -37,9 +37,9 @@
     ],
     keywords='gcp asset inventory',
     packages=['asset_inventory'],
-    setup_requires=['pytest-runner'],
+    setup_requires=['pytest-runner', 'setuptools_scm'],
     extras_require = {
-        'testing': ['mock==2.0.0', 'pytest==4.6.6', 'apache-beamn[gcp]==2.25.0'],
+        'testing': ['mock==4.0.3', 'pytest==7.1.3', 'apache-beam[gcp]==2.41.0'],
     },
     include_package_data=True,
     # https://pypi.org/project/google-cloud-asset/#history
@@ -47,7 +47,13 @@
     # https://pypi.org/project/google-cloud-bigquery/#history
     # https://pypi.org/project/requests-futures/#history
     install_requires=[
-        'google-cloud-asset==0.8.0',
-        'google-cloud-bigquery==1.17.1',
+        'google-cloud-asset==3.13.0',
+        'google-cloud-bigquery==2.34.4',
         'requests-futures==1.0.0'
-    ])
+    ],
+    use_scm_version = {
+        "root": "../..",
+        "relative_to": __file__,
+        "local_scheme": "node-and-timestamp"
+    }
+)


Test patch: