algolia / algolia/algoliasearch-client-python

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

未关闭
#532 2 条评论 4 个 reaction 已指派 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 进入的重试路径,并使用指定的 Python 和客户端版本在 AWS Lambda 中复现所提供的示例。将 Lambda 执行与本地上传进行比较,并验证有效记录可以在不出现 AlgoliaUnreachableHostException 的情况下上传。

由索引模型根据 Issue 内容生成。

评估

技术栈
aws, python
领域
api, backend, cloud
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
32/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。