SDK FAQ

Updated at:

Common issues and solutions for using Resource Orchestration Service (ROS) SDKs.

What do I do if I receive the "ErrCode:SDK.ServerUnreachable" error when using an SDK?

Problem

You may receive the following error message, which indicates that a request timed out or the server is unreachable:

ErrCode:ServerUnreachable
ErrMsg:SocketTimeoutException has occurred on a socket read or accept.

Solution

  1. Set the ClientToken parameter in the request to prevent duplicate operations caused by multiple retries.

    Note

    Before you call the API, configure environment variables to read the access credentials. For more information, see Configure access credentials.

    The environment variable names for the AccessKey ID and AccessKey secret are ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.

    The following Python code sample shows how to set ClientToken and implement a retry mechanism:

    # coding=utf-8
    import time
    import uuid
    
    from alibabacloud_ros20190910 import models as ros20190910_models
    from aliyunsdkcore.acs_exception.exceptions import ClientException, ServerException
    from alibabacloud_tea_openapi import models as open_api_models
    from alibabacloud_credentials.client import Client as CredClient
    from alibabacloud_ros20190910.client import Client as ROS20190910Client
    
    
    def generate_client_token(prefix, add_uuid, *suffix_list):
        if prefix:
            t = [prefix]
        else:
            t = []
        if add_uuid:
            t.append(str(uuid.uuid1())[:-13])
        t.extend(suffix_list)
        r = '_'.join(t)
        if len(r) > 64:
            r = r[:64]
        return r
    
    
    def retry_with_client_token(retry_interval=10, retry_time_out=300):
        def wrapper(fun):
            def retry_func(*args, **kwargs):
                retry_timeout = retry_time_out
                elapsed_time = 0
                # Ensures the idempotence of the request. This value is generated by the client and must be globally unique.
                kwargs['client_token'] = generate_client_token(None, True)
                while True:
                    try:
                        retry_timeout -= elapsed_time
                        return fun(*args, **kwargs)
                    except Exception as e:
                        if isinstance(e, ClientException):
                            if e.error_code != 'SDK.HttpError':
                                raise
                        elif isinstance(e, ServerException):
                            if e.error_code not in ('LastTokenProcessing', 'ServiceUnavailable'):
                                raise
                        else:
                            raise
    
                        time.sleep(retry_interval)
                        elapsed_time += retry_interval
                        if elapsed_time >= retry_timeout:
                            raise
    
            return retry_func
    
        return wrapper
    
    
    class RosClient(object):
    
        @staticmethod
        def create_client():
            """
            Initializes the account client.
            @return: Client
            @throws Exception
            """
            cred = CredClient()
            config = open_api_models.Config(
                credential=cred
            )
            # The domain name to access.
            config.endpoint = 'ros.aliyuncs.com'
    
            return ROS20190910Client(config)
    
        @retry_with_client_token(retry_interval=10, retry_time_out=300)
        def create_stack(self, region_id, stack_name, template_body, **kwargs):
            client = self.create_client()
            create_stack_request = ros20190910_models.CreateStackRequest(
                region_id=region_id,
                stack_name=stack_name,
                # If the template is large, use the TemplateURL parameter to prevent call failures caused by an overly long URL.
                # You can also use the Body Param and an HTTP POST request to pass the template content.
                template_body=template_body,
                disable_rollback=True,
                **kwargs
            )
            response = client.create_stack(create_stack_request)
    
            return response.body
    
    
    if __name__ == '__main__':
        region_id = 'cn-beijing'
        ros_client = RosClient()
        stack_name = 'test'
        template_body = "{\"ROSTemplateFormatVersion\": \"2015-09-01\"}"
        result = ros_client.create_stack(region_id=region_id, stack_name=stack_name, template_body=template_body)
        print('stack_id:', result)
  2. If the issue persists, join the DingTalk group (ID: 11783495) and contact Alibaba Cloud technical support.