algolia / algolia/algoliasearch-client-python

Getting AlgoliaUnreachableHostException("Unreachable hosts") when attempting to upload records via AWS Lambda

オープン
#532 コメント 2 件 リアクション 4 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
207
フォーク
63
PR マージ指標
30日以内にマージされた PR はありません

説明

- Algolia Client Version: 2.5.0
- Language Version: Python

### Description
I am facing a similar issue as reported in this Past Issue - https://github.com/algolia/algoliasearch-client-python/issues/30.

StackTrace -
```
raise AlgoliaUnreachableHostException("Unreachable hosts") in retryst)fNotExist": True})
END RequestId: 0d549eca-dd2e-4884-9f43-0548a6db3674
REPORT RequestId: 0d549eca-dd2e-4884-9f43-0548a6db3674 Init Duration: 0.46 ms Duration: 4491.58 ms Billed Duration: 4500 ms Memory Size: 1024 MB Max Memory Used: 1024 MB
{"errorMessage": "Unreachable hosts", "errorType": "AlgoliaUnreachableHostException", "stackTrace": [" File \"/var/task/main.py\", line 37, in lambda_handler\n scan_and_upload_to_algolia()\n", " File \"/var/task/ddb_utils.py\", line 72, in scan_and_upload_to_algolia\n update_templates_data_in_algolia(algolia_records)\n", " File \"/var/task/algolia_utils.py\", line 21, in update_templates_data_in_algolia\n res = templates_v4_index.save_objects(records, {\"autoGenerateObjectIDIfNotExist\": True})\n", " File \"/var/task/algoliasearch/search_index.py\", line 69, in save_objects\n response = self._chunk(\"addObject\", objects, request_options, False)\n", " File \"/var/task/algoliasearch/search_index.py\", line 527, in _chunk\n raw_responses.append(self._raw_batch(requests, request_options))\n", " File \"/var/task/algoliasearch/search_index.py\", line 534, in _raw_batch\n return self._transporter.write(\n", " File \"/var/task/algoliasearch/http/transporter.py\", line 35, in write\n return self.request(verb, hosts, path, data, request_options, timeout)\n", " File \"/var/task/algoliasearch/http/transporter.py\", line 72, in request\n return self.retry(hosts, request, relative_url)\n", " File \"/var/task/algoliasearch/http/transporter.py\", line 94, in retry\n raise AlgoliaUnreachableHostException(\"Unreachable hosts\")\n"]}%
```

Code Snippet
```
def create_algolia_client():
algolia_api_key = os.environ.get("INVIDEO_ALGOLIA_API_ID").strip()
algolia_api_id = os.environ.get("INVIDEO_ALGOLIA_API_KEY").strip()
if algolia_api_id is not "" and algolia_api_key is not "":
algolia_client = SearchClient.create(algolia_api_id, algolia_api_key)
logger.info("algolia search client created")
return algolia_client
else:
logger.error("algolia id and key is missing, failed to create insights client")

def update_templates_data_in_algolia(records):
algolia_client = create_algolia_client()
templates_v4_index = algolia_client.init_index("templates_v4")
res = templates_v4_index.save_objects(records, {"autoGenerateObjectIDIfNotExist": True})
logger.info("Got Response from Algolia", res)
```

I am running this code snippet inside AWS Lambda.
After reading the possible solutions in past Issue. I added urllib3, requests[security] and pyOpenSSL libraries.

Here's the pyproject.toml -
```
python = "3.8.5"
algoliasearch = "^2.5.0"
urllib3 = "^1.26.6"
requests[security] = 2.9.1
pyOpenSSL = 20.0.1
```

We have performed following checks -
1. Api ID and Key are valid, were able to upload records from Local.
2. Record Object is valid.
3. Our Lambda is not in any VPC.
4. Ensured our Lambda is able to resolve Algolia Server by performing DNS Lookup on .algolia.net.

### Steps To Reproduce
1. Run the provided snippet inside AWS Lambda with above configuration for python.
2. Pass any valid record object and replace ID and Key with a valid one.

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

algoliasearch/http/transporter.py から始め、特に save_objects から到達する retry パスを確認し、指定された Python とクライアントのバージョンを使って AWS Lambda で提供された例を再現してください。Lambda での実行とローカルでのアップロードを比較し、有効なレコードを AlgoliaUnreachableHostException なしでアップロードできることを検証してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
aws, python
領域
api, backend, cloud
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
32/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。