IBM Support

After deploying a function that uses Flight, it becomes unusable after a while

Question & Answer


Question

I have a deployed function. Flight are used to access my data in this function. I can use it without any problems for a few hours after deploying. However, it suddenly becomes unusable due to the following error.
"RuntimeError('Failed to search connection assets, status 401.')"
The function I use is like following:
def my_deployable_function( parms=ai_parms ):
    
    def access_data():
        import itc_utils.flight_service as itcfs
        
        readClient = itcfs.get_flight_client()
        
        query = f"""SELECT * FROM customer"""
        
        XXXX_data_request = { # <- Change according to the connection used
            'connection_name': """<CONNECTION_NAME>""",
            'interaction_properties': {
                'row_limit': 100,
                'select_statement': query,
                'infer_schema': 'false',
                'infer_as_varchar': 'false'
            }
        }
        flightInfo = itcfs.get_flight_info(readClient, nb_data_request=XXXX_data_request)
        df = itcfs.read_pandas_and_concat(readClient, flightInfo, timeout=480)
        
    def score(function_payload):
            
        try:

            access_data()
            
            # <..... omit .....>
                    
            return model_result
        
        except Exception as e:
            
            return {'predictions': [{'values': [repr(e)]}]}
            
    return score
What is the workaround for this problem?

Answer

You can resolve this problem by using following code snippet to avoid that itc_utils uses the internal reference to wslib.
def my_deployable_function( parms=ai_parms ):

    def access_data():
        
        import os
        import pandas as pd
        import requests
        import json
        import itc_utils
        import itc_utils.flight_service as itcfs
        from ibm_watson_studio_lib import access_project_or_space 

        url = "https://<HOST_NAME>/icp4d-api/v1/authorize"
        payload = json.dumps({
          "username": "<USER_NAME>",
          "password": "<PASSWORD>"
        })
        headers = {
          'Cache-Control': 'no-cache',
          'Content-Type': 'application/json',
        }

        response = requests.request("POST", url, headers=headers, data=payload, verify=False)
        if (response.status_code == 200):
            print("User token has been generated")
            tok = json.loads(response.text)["token"]
        else:
            print("User token generation failed")
            tok = os.getenv('USER_ACCESS_TOKEN')
        
        authHandler = itcfs.TokenClientAuthHandler(token=tok)
        readClient = itcfs.get_flight_client(client_auth_handler=authHandler)
        
        wslib = access_project_or_space({'token':tok})

        query = f"""SELECT * FROM customer"""
        
        XXXX_data_request = { # <- Change according to the connection used
            'connection_name': """<CONNECTION_NAME>""",
            'interaction_properties': {
                'row_limit': 100,
                'select_statement': query,
                'infer_schema': 'false',
                'infer_as_varchar': 'false'
            }
        }

        flightRequest = XXXX_data_request
        if os.getenv('PROJECT_ID') is not None:
            flightRequest['project_id'] = wslib.here.get_ID()
        elif os.getenv('SPACE_ID') is not None:
            flightRequest['space_id'] = wslib.here.get_ID()

        flightRequest['asset_id'] = wslib.get_connection('<CONNECTION_NAME>')['.']['asset_id']
        flightInfo = itcfs.get_flight_info(readClient, data_request=flightRequest) 
        
        df = itcfs.read_pandas_and_concat(readClient, flightInfo, timeout=480)
        
    def score(function_payload):
            
        try:

            access_data()
            
            # <..... omit .....>
                    
            return model_result
        
        except Exception as e:
            
            return {'predictions': [{'values': [repr(e)]}]}
            
    return score

[{"Type":"MASTER","Line of Business":{"code":"LOB10","label":"Data and AI"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSHGYS","label":"IBM Cloud Pak for Data"},"ARM Category":[{"code":"a8m50000000L1dGAAS","label":"Analyze-\u003EModels-\u003ENotebooks"}],"ARM Case Number":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"4.0.9"}]

Document Information

Modified date:
29 April 2023

UID

ibm16987019