Skip to content

Commit afb444a

Browse files
committed
feedback
1 parent 4c69c18 commit afb444a

File tree

6 files changed

+52
-56
lines changed

6 files changed

+52
-56
lines changed

.github/workflows/ci_deferred_bindings_workflow.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
6161
chmod +x .github/Scripts/install-deps.sh
6262
.github/Scripts/install-deps.sh
63-
python -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple --pre -U -e .[deferred-bindings]
63+
python -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple --pre -U -e .[test-deferred-bindings]
6464
6565
# Retry a couple times to avoid certificate issue
6666
retry 5 python setup.py build

azure_functions_worker/bindings/meta.py

+43-44
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3+
import os
34
import sys
45
import typing
56

67
from .. import protos
7-
from ..constants import BASE_EXT_SUPPORTED_PY_MINOR_VERSION
88

99
from . import datumdef
1010
from . import generic
11+
1112
from .shared_memory_data_transfer import SharedMemoryManager
13+
from ..constants import CUSTOMER_PACKAGES_PATH
1214

1315
PB_TYPE = 'rpc_data'
1416
PB_TYPE_DATA = 'data'
1517
PB_TYPE_RPC_SHARED_MEMORY = 'rpc_shared_memory'
16-
BINDING_REGISTRY = None
17-
DEFERRED_BINDINGS_REGISTRY = None
18-
DEFERRED_BINDINGS_ENABLED = False
19-
DEFERRED_BINDINGS_CACHE = {}
18+
# Base extension supported Python minor version
19+
BASE_EXT_SUPPORTED_PY_MINOR_VERSION = 8
20+
21+
binding_registry = None
22+
deferred_binding_registry = None
23+
deferred_bindings_enabled = False
24+
deferred_bindings_cache = {}
2025

2126

2227
def load_binding_registry() -> None:
@@ -26,33 +31,45 @@ def load_binding_registry() -> None:
2631
if func is None:
2732
import azure.functions as func
2833

29-
global BINDING_REGISTRY
30-
BINDING_REGISTRY = func.get_binding_registry()
34+
global binding_registry
35+
binding_registry = func.get_binding_registry()
3136

32-
if BINDING_REGISTRY is None:
33-
# If the BINDING_REGISTRY is None, azure-functions hasn't been
37+
if binding_registry is None:
38+
# If the binding_registry is None, azure-functions hasn't been
3439
# loaded in properly.
35-
raise AttributeError("BINDING_REGISTRY is None.")
40+
raise AttributeError('binding_registry is None. Sys Path:'
41+
f'{sys.path}, Sys Module: {sys.modules},'
42+
f'python-packages Path exists:'
43+
f'{os.path.exists(CUSTOMER_PACKAGES_PATH)}')
3644

3745
# The base extension supports python 3.8+
3846
if sys.version_info.minor >= BASE_EXT_SUPPORTED_PY_MINOR_VERSION:
3947
# Import the base extension
4048
try:
4149
import azure.functions.extension.base as clients
42-
global DEFERRED_BINDINGS_REGISTRY
43-
DEFERRED_BINDINGS_REGISTRY = clients.get_binding_registry()
50+
global deferred_binding_registry
51+
deferred_binding_registry = clients.get_binding_registry()
4452
except ImportError:
4553
# This means that the customer hasn't imported the library.
4654
# This isn't an error.
4755
pass
4856

4957

58+
def get_deferred_binding(bind_name: str,
59+
pytype: typing.Optional[type] = None) -> object:
60+
# This will return None if not a supported type
61+
return deferred_binding_registry.get(bind_name)\
62+
if (deferred_binding_registry is not None
63+
and deferred_binding_registry.check_supported_type(
64+
pytype)) else None
65+
66+
5067
def get_binding(bind_name: str, pytype: typing.Optional[type] = None) -> object:
5168
# Check if binding is deferred binding
5269
binding = get_deferred_binding(bind_name=bind_name, pytype=pytype)
5370
# Binding is not a deferred binding type
5471
if binding is None:
55-
binding = BINDING_REGISTRY.get(bind_name)
72+
binding = binding_registry.get(bind_name)
5673
# Binding is generic
5774
if binding is None:
5875
binding = generic.GenericBinding
@@ -117,8 +134,8 @@ def from_incoming_proto(
117134

118135
try:
119136
# if the binding is an sdk type binding
120-
if (DEFERRED_BINDINGS_REGISTRY is not None
121-
and DEFERRED_BINDINGS_REGISTRY.check_supported_type(pytype)):
137+
if (deferred_binding_registry is not None
138+
and deferred_binding_registry.check_supported_type(pytype)):
122139
return deferred_bindings_decode(binding=binding,
123140
pb=pb,
124141
pytype=pytype,
@@ -217,55 +234,37 @@ def to_outgoing_param_binding(binding: str, obj: typing.Any, *,
217234
data=rpc_val)
218235

219236

220-
def get_deferred_binding(bind_name: str,
221-
pytype: typing.Optional[type] = None) -> object:
222-
binding = None
223-
224-
# Checks if pytype is a supported sdk type
225-
if (DEFERRED_BINDINGS_REGISTRY is not None
226-
and DEFERRED_BINDINGS_REGISTRY.check_supported_type(pytype)):
227-
# Returns deferred binding converter
228-
binding = DEFERRED_BINDINGS_REGISTRY.get(bind_name)
229-
230-
# This will return None if not a supported type
231-
return binding
232-
233-
234237
def deferred_bindings_decode(binding: typing.Any,
235238
pb: protos.ParameterBinding, *,
236239
pytype: typing.Optional[type],
237240
datum: typing.Any,
238241
metadata: typing.Any):
239242
# This cache holds deferred binding types (ie. BlobClient, ContainerClient)
240243
# That have already been created, so that the worker can reuse the
241-
# Previously created type without creating a new one. It allows these
242-
# Types to be singleton.
243-
global DEFERRED_BINDINGS_CACHE
244+
# Previously created type without creating a new one.
245+
global deferred_bindings_cache
244246

245-
# Check if the type is already in the cache
246-
# If dict is empty or key doesn't exist, deferred_binding_type is None
247-
deferred_binding_type = DEFERRED_BINDINGS_CACHE.get((pb.name, pytype,
247+
# If cache is empty or key doesn't exist, deferred_binding_type is None
248+
deferred_binding_type = deferred_bindings_cache.get((pb.name, pytype,
248249
datum.value.content),
249250
None)
250251

251-
# If the type is in the cache, return it
252252
if deferred_binding_type is not None:
253253
return deferred_binding_type
254-
# Otherwise, create the specified type and add it to the cache
255254
else:
256255
deferred_binding_type = binding.decode(datum, trigger_metadata=metadata,
257256
pytype=pytype)
258-
DEFERRED_BINDINGS_CACHE[(pb.name, pytype, datum.value.content)]\
257+
deferred_bindings_cache[(pb.name, pytype, datum.value.content)]\
259258
= deferred_binding_type
260259
return deferred_binding_type
261260

262261

263262
def set_deferred_bindings_flag(param_anno: type):
264263
# If flag hasn't already been set
265-
# If DEFERRED_BINDINGS_REGISTRY is not None
264+
# If deferred_binding_registry is not None
266265
# If the binding type is a deferred binding type
267-
global DEFERRED_BINDINGS_ENABLED
268-
if (not DEFERRED_BINDINGS_ENABLED
269-
and DEFERRED_BINDINGS_REGISTRY is not None
270-
and DEFERRED_BINDINGS_REGISTRY.check_supported_type(param_anno)):
271-
DEFERRED_BINDINGS_ENABLED = True
266+
global deferred_bindings_enabled
267+
if (not deferred_bindings_enabled
268+
and deferred_binding_registry is not None
269+
and deferred_binding_registry.check_supported_type(param_anno)):
270+
deferred_bindings_enabled = True

azure_functions_worker/constants.py

-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@
5656
# Paths
5757
CUSTOMER_PACKAGES_PATH = "/home/site/wwwroot/.python_packages/lib/site-packages"
5858

59-
# Base extension supported Python minor version
60-
BASE_EXT_SUPPORTED_PY_MINOR_VERSION = 8
61-
6259
# Flag to index functions in handle init request
6360
PYTHON_ENABLE_INIT_INDEXING = "PYTHON_ENABLE_INIT_INDEXING"
6461

azure_functions_worker/loader.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ def get_fx_raw_bindings(indexed_function, function_info):
248248
# If the flag is True, we know that:
249249
# 1. Library is imported
250250
# 2. At least one binding is a defined deferred binding type
251-
# 3. DEFERRED_BINDINGS_REGISTRY is not None
252-
if bindings.meta.DEFERRED_BINDINGS_ENABLED:
251+
# 3. deferred_binding_registry is not None
252+
if bindings.meta.deferred_bindings_enabled:
253253
# Reset the flag
254-
bindings.meta.DEFERRED_BINDINGS_ENABLED = False
255-
return bindings.meta.DEFERRED_BINDINGS_REGISTRY.get_raw_bindings(
254+
bindings.meta.deferred_bindings_enabled = False
255+
return bindings.meta.deferred_binding_registry.get_raw_bindings(
256256
indexed_function, function_info.input_types)
257257

258258
else:

tests/extension_tests/deferred_bindings_tests/test_deferred_bindings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def test_non_deferred_bindings_metadata(self):
6464
self.assertIsInstance(r.response, protos.FunctionMetadataResponse)
6565
self.assertEqual(r.response.result.status,
6666
protos.StatusResult.Success)
67-
self.assertFalse(meta.DEFERRED_BINDINGS_ENABLED)
67+
self.assertFalse(meta.deferred_bindings_enabled)
6868

6969

7070
class TestDeferredBindingsHelpers(testutils.AsyncTestCase):

tests/extension_tests/deferred_bindings_tests/test_deferred_bindings_blob_functions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from azure_functions_worker.bindings import meta
77

88

9-
class TestSdkBlobFunctions(testutils.WebHostTestCase):
9+
class TestDeferredBindingsBlobFunctions(testutils.WebHostTestCase):
1010

1111
@classmethod
1212
def get_script_dir(cls):
@@ -155,10 +155,10 @@ def test_type_undefined(self):
155155
self.assertEqual(r.status_code, 200)
156156
self.assertEqual(r.text, 'test-data')
157157

158-
self.assertFalse(meta.DEFERRED_BINDINGS_ENABLED)
158+
self.assertFalse(meta.deferred_bindings_enabled)
159159

160160
def test_caching(self):
161161
# Cache is empty at the start
162-
self.assertEqual(meta.DEFERRED_BINDINGS_CACHE, {})
162+
self.assertEqual(meta.deferred_bindings_cache, {})
163163
r = self.webhost.request('GET', 'blob_cache')
164164
self.assertEqual(r.status_code, 200)

0 commit comments

Comments
 (0)