-
Notifications
You must be signed in to change notification settings - Fork 25
feat: AE-815 Add class based execution #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a522afd
feat: AE-815 Add class based execution
pandyamarut 09f6ba1
clean client
pandyamarut 6c54673
chore: update log
pandyamarut a273bbf
add unit tests and fix logging
pandyamarut 1be5a07
Merge branch 'main' into feat/ae-815-add-cls-exec
pandyamarut ab9d0b9
Merge branch 'main' into feat/ae-815-add-cls-exec
pandyamarut 949b0f5
chore: add integration tests
pandyamarut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| import base64 | ||
| import inspect | ||
| import logging | ||
| import textwrap | ||
| import uuid | ||
| from typing import List, Type, Optional | ||
|
|
||
| import cloudpickle | ||
|
|
||
| from .core.resources import ResourceManager, ServerlessResource | ||
| from .protos.remote_execution import FunctionRequest | ||
| from .stubs import stub_resource | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def extract_class_code_simple(cls: Type) -> str: | ||
| """Extract clean class code without decorators and proper indentation""" | ||
| try: | ||
| # Get source code | ||
| source = inspect.getsource(cls) | ||
|
|
||
| # Split into lines | ||
| lines = source.split("\n") | ||
|
|
||
| # Find the class definition line (starts with 'class' and contains ':') | ||
| class_start_idx = -1 | ||
| for i, line in enumerate(lines): | ||
| stripped = line.strip() | ||
| if stripped.startswith("class ") and ":" in stripped: | ||
| class_start_idx = i | ||
| break | ||
|
|
||
| if class_start_idx == -1: | ||
| raise ValueError("Could not find class definition") | ||
|
|
||
| # Take lines from class definition onwards (ignore everything before) | ||
| class_lines = lines[class_start_idx:] | ||
|
|
||
| # Remove empty lines at the end | ||
| while class_lines and not class_lines[-1].strip(): | ||
| class_lines.pop() | ||
|
|
||
| # Join back and dedent to remove any leading indentation | ||
| class_code = "\n".join(class_lines) | ||
| class_code = textwrap.dedent(class_code) | ||
|
|
||
| # Validate the code by trying to compile it | ||
| compile(class_code, "<string>", "exec") | ||
|
|
||
| log.debug(f"Successfully extracted class code for {cls.__name__}") | ||
| return class_code | ||
|
|
||
| except Exception as e: | ||
| log.warning(f"Could not extract class code for {cls.__name__}: {e}") | ||
| log.warning("Falling back to basic class structure") | ||
|
|
||
| # Enhanced fallback: try to preserve method signatures | ||
| fallback_methods = [] | ||
| for name, method in inspect.getmembers(cls, predicate=inspect.isfunction): | ||
| try: | ||
| sig = inspect.signature(method) | ||
| fallback_methods.append(f" def {name}{sig}:") | ||
| fallback_methods.append(" pass") | ||
| fallback_methods.append("") | ||
| except (TypeError, ValueError, OSError) as e: | ||
| log.warning(f"Could not extract method signature for {name}: {e}") | ||
| fallback_methods.append(f" def {name}(self, *args, **kwargs):") | ||
| fallback_methods.append(" pass") | ||
| fallback_methods.append("") | ||
|
|
||
| fallback_code = f"""class {cls.__name__}: | ||
| def __init__(self, *args, **kwargs): | ||
| pass | ||
|
|
||
| {chr(10).join(fallback_methods)}""" | ||
|
|
||
| return fallback_code | ||
|
|
||
|
|
||
| def create_remote_class( | ||
| cls: Type, | ||
| resource_config: ServerlessResource, | ||
| dependencies: Optional[List[str]], | ||
| system_dependencies: Optional[List[str]], | ||
| extra: dict, | ||
| ): | ||
| """ | ||
| Create a remote class wrapper. | ||
| """ | ||
| # Validate inputs | ||
| if not inspect.isclass(cls): | ||
| raise TypeError(f"Expected a class, got {type(cls).__name__}") | ||
| if not hasattr(cls, "__name__"): | ||
| raise ValueError("Class must have a __name__ attribute") | ||
|
|
||
| class RemoteClassWrapper: | ||
| def __init__(self, *args, **kwargs): | ||
| self._class_type = cls | ||
| self._resource_config = resource_config | ||
| self._dependencies = dependencies or [] | ||
| self._system_dependencies = system_dependencies or [] | ||
| self._extra = extra | ||
| self._constructor_args = args | ||
| self._constructor_kwargs = kwargs | ||
| self._instance_id = f"{cls.__name__}_{uuid.uuid4().hex[:8]}" | ||
| self._initialized = False | ||
|
|
||
| self._clean_class_code = extract_class_code_simple(cls) | ||
|
|
||
| log.debug(f"Created remote class wrapper for {cls.__name__}") | ||
|
|
||
| async def _ensure_initialized(self): | ||
| """Ensure the remote instance is created.""" | ||
| if self._initialized: | ||
| return | ||
|
|
||
| # Get remote resource | ||
| resource_manager = ResourceManager() | ||
| remote_resource = await resource_manager.get_or_deploy_resource( | ||
| self._resource_config | ||
| ) | ||
| self._stub = stub_resource(remote_resource, **self._extra) | ||
|
|
||
| # Create the remote instance by calling a method (which will trigger instance creation) | ||
| # We'll do this on first method call | ||
| self._initialized = True | ||
|
|
||
| def __getattr__(self, name): | ||
| """Dynamically create method proxies for all class methods.""" | ||
| if name.startswith("_"): | ||
| raise AttributeError( | ||
| f"'{self.__class__.__name__}' object has no attribute '{name}'" | ||
| ) | ||
|
|
||
| async def method_proxy(*args, **kwargs): | ||
| await self._ensure_initialized() | ||
|
|
||
| # Create class method request | ||
|
|
||
| # class_code = inspect.getsource(self._class_type) | ||
| class_code = self._clean_class_code | ||
|
|
||
| request = FunctionRequest( | ||
| execution_type="class", | ||
| class_name=self._class_type.__name__, | ||
| class_code=class_code, | ||
| method_name=name, | ||
| args=[ | ||
| base64.b64encode(cloudpickle.dumps(arg)).decode("utf-8") | ||
| for arg in args | ||
| ], | ||
| kwargs={ | ||
| k: base64.b64encode(cloudpickle.dumps(v)).decode("utf-8") | ||
| for k, v in kwargs.items() | ||
| }, | ||
| constructor_args=[ | ||
| base64.b64encode(cloudpickle.dumps(arg)).decode("utf-8") | ||
| for arg in self._constructor_args | ||
| ], | ||
| constructor_kwargs={ | ||
| k: base64.b64encode(cloudpickle.dumps(v)).decode("utf-8") | ||
| for k, v in self._constructor_kwargs.items() | ||
| }, | ||
| dependencies=self._dependencies, | ||
| system_dependencies=self._system_dependencies, | ||
| instance_id=self._instance_id, | ||
| create_new_instance=not hasattr( | ||
| self, "_stub" | ||
| ), # Create new only on first call | ||
| ) | ||
|
|
||
| # Execute via stub | ||
| return await self._stub.execute_class_method(request) # type: ignore | ||
|
|
||
| return method_proxy | ||
|
|
||
| return RemoteClassWrapper | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.