예제 #1
0
    def _apply(self, request_data, expected_response_type=None):
        logger.debug("Sending request\n%s", pprint.pformat(request_data))

        request_name = request_data.__class__.__name__
        message = common_pb2.WireMessage()
        message.name = 'org.apache.calcite.avatica.proto.Requests${}'.format(request_name)
        message.wrapped_message = request_data.SerializeToString()
        body = message.SerializeToString()
        headers = {'content-type': 'application/x-google-protobuf'}

        response = self._post_request(body, headers)
        response_body = response.read()

        if response.status != httplib.OK:
            logger.debug("Received response\n%s", response_body)
            if b'<html>' in response_body:
                parse_error_page(response_body)
            else:
                # assume the response is in protobuf format
                parse_error_protobuf(response_body)
            raise errors.InterfaceError('RPC request returned invalid status code', response.status)

        message = common_pb2.WireMessage()
        message.ParseFromString(response_body)

        logger.debug("Received response\n%s", message)

        if expected_response_type is None:
            expected_response_type = request_name.replace('Request', 'Response')

        expected_response_type = 'org.apache.calcite.avatica.proto.Responses$' + expected_response_type
        if message.name != expected_response_type:
            raise errors.InterfaceError('unexpected response type "{}"'.format(message.name))

        return message.wrapped_message
예제 #2
0
 def _post_request(self, body, headers):
     retry_count = self.max_retries
     while True:
         logger.debug("POST %s %r %r", self.url.path, body, headers)
         try:
             self.connection.request('POST',
                                     self.url.path,
                                     body=body,
                                     headers=headers)
             response = self.connection.getresponse()
         except httplib.HTTPException as e:
             if retry_count > 0:
                 delay = math.exp(-retry_count)
                 logger.debug(
                     "HTTP protocol error, will retry in %s seconds...",
                     delay,
                     exc_info=True)
                 self.close()
                 self.connect()
                 time.sleep(delay)
                 retry_count -= 1
                 continue
             raise errors.InterfaceError('RPC request failed', cause=e)
         else:
             if response.status == httplib.SERVICE_UNAVAILABLE:
                 if retry_count > 0:
                     delay = math.exp(-retry_count)
                     logger.debug(
                         "Service unavailable, will retry in %s seconds...",
                         delay,
                         exc_info=True)
                     time.sleep(delay)
                     retry_count -= 1
                     continue
             return response
예제 #3
0
    def _post_request(self, body, headers):
        retry_count = self.max_retries
        while True:
            logger.debug("POST %s %r %r", self.url.geturl(), body, headers)

            requestArgs = {'data': body, 'stream': True, 'headers': headers}

            if self.auth is not None:
                requestArgs.update(auth=self.auth)

            if self.verify is not None:
                requestArgs.update(verify=self.verify)

            try:
                response = requests.request('post', self.url.geturl(), **requestArgs)

            except requests.HTTPError as e:
                if retry_count > 0:
                    delay = math.exp(-retry_count)
                    logger.debug("HTTP protocol error, will retry in %s seconds...", delay, exc_info=True)
                    time.sleep(delay)
                    retry_count -= 1
                    continue
                raise errors.InterfaceError('RPC request failed', cause=e)
            else:
                if response.status_code == requests.codes.service_unavailable:
                    if retry_count > 0:
                        delay = math.exp(-retry_count)
                        logger.debug("Service unavailable, will retry in %s seconds...", delay, exc_info=True)
                        time.sleep(delay)
                        retry_count -= 1
                        continue
                return response
예제 #4
0
 def connect(self):
     """Opens a HTTP connection to the RPC server."""
     logger.debug("Opening connection to %s:%s", self.url.hostname, self.url.port)
     try:
         self.connection = httplib.HTTPConnection(self.url.hostname, self.url.port)
         self.connection.connect()
     except (httplib.HTTPException, socket.error) as e:
         raise errors.InterfaceError('Unable to connect to the specified service', e)
예제 #5
0
    def _post_request(self, body, headers):
        # Create the session if we haven't before
        if not self.session:
            logger.debug("Creating a new Session")
            self.session = requests.Session()
            self.session.headers.update(headers)
            self.session.stream = True
            if self.auth is not None:
                self.session.auth = self.auth

        retry_count = self.max_retries
        while True:
            logger.debug("POST %s %r %r", self.url.geturl(), body,
                         self.session.headers)

            requestArgs = {'data': body}

            # Setting verify on the Session is not the same as setting it
            # as a request arg
            if self.verify is not None:
                requestArgs.update(verify=self.verify)

            try:
                response = self.session.post(self.url.geturl(), **requestArgs)

            except requests.HTTPError as e:
                if retry_count > 0:
                    delay = math.exp(-retry_count)
                    logger.debug(
                        "HTTP protocol error, will retry in %s seconds...",
                        delay,
                        exc_info=True)
                    time.sleep(delay)
                    retry_count -= 1
                    continue
                raise errors.InterfaceError('RPC request failed', cause=e)
            else:
                if response.status_code == requests.codes.service_unavailable:
                    if retry_count > 0:
                        delay = math.exp(-retry_count)
                        logger.debug(
                            "Service unavailable, will retry in %s seconds...",
                            delay,
                            exc_info=True)
                        time.sleep(delay)
                        retry_count -= 1
                        continue
                return response
예제 #6
0
    def _post_request(self, body, headers):
        retry_count = self.max_retries
        while True:
            logger.debug("POST %s %r %r", self.url.geturl(), body, headers)
            try:
                if self.auth == "SPNEGO":
                    #response = requests.request('post', self.url.geturl(), data=body, stream=True, headers=headers, auth=HTTPSPNEGOAuth(mutual_authentication=OPTIONAL))
                    response = requests.request(
                        'post',
                        self.url.geturl(),
                        data=body,
                        stream=True,
                        headers=headers,
                        auth=HTTPKerberosAuth(
                            mutual_authentication=OPTIONAL,
                            mech_oid=kerberos.GSS_MECH_OID_SPNEGO),
                        timeout=7200)
                else:
                    response = requests.request('post',
                                                self.url.geturl(),
                                                data=body,
                                                stream=True,
                                                headers=headers,
                                                timeout=7200)

            except requests.HTTPError as e:
                if retry_count > 0:
                    delay = math.exp(-retry_count)
                    logger.debug(
                        "HTTP protocol error, will retry in %s seconds...",
                        delay,
                        exc_info=True)
                    time.sleep(delay)
                    retry_count -= 1
                    continue
                raise errors.InterfaceError('RPC request failed', cause=e)
            else:
                if response.status_code == requests.codes.service_unavailable:
                    if retry_count > 0:
                        delay = math.exp(-retry_count)
                        logger.debug(
                            "Service unavailable, will retry in %s seconds...",
                            delay,
                            exc_info=True)
                        time.sleep(delay)
                        retry_count -= 1
                        continue
                return response
예제 #7
0
    def _apply(self,
               request_data,
               expected_response_type=None,
               keepalive_callback=None):
        logger.debug("Sending request\n%s", pprint.pformat(request_data))

        class FakeFloat(float):
            # XXX there has to be a better way to do this
            def __init__(self, value):
                self.value = value

            def __repr__(self):
                return str(self.value)

        def default(obj):
            if isinstance(obj, Decimal):
                return FakeFloat(obj)
            raise TypeError

        if self.version >= AVATICA_1_4_0:
            body = json.dumps(request_data, default=default)
            headers = {'content-type': 'application/json'}
        else:
            body = None
            headers = {'request': json.dumps(request_data, default=default)}

        logger.debug("POST %s %r %r", self.url.path, body, headers)
        try:
            print "pre request"
            self.connection.request('POST',
                                    self.url.path,
                                    body=body,
                                    headers=headers)
            print "post request"

            if keepalive_callback != None:
                while True:
                    print "pre select"
                    readable, _, _ = select.select([self.connection.sock], [],
                                                   [], 0.5)

                    if readable:
                        print self.connection.sock
                        print dir(self.connection.sock)
                        print "post select"
                        print "Ready"
                        break
                    else:
                        print "Waiting"
                        if keepalive_callback() == False:
                            print "Terminating"
                            e = Exception()
                            raise errors.InterfaceError(
                                "RPC request terminated", cause=e)

#            response = None
#            print "pre getresponse"
            response = self.connection.getresponse()
#            print "end getresponse"
        except httplib.HTTPException as e:
            raise errors.InterfaceError('RPC request failed', cause=e)

#        print "before response.read()"

        print "Start read"
        response_body = response.read()
        print "End read"
        #        print "after response.read()"

        if response.status != httplib.OK:
            logger.debug("Received response\n%s", response_body)
            if '<html>' in response_body:
                parse_error_page(response_body)
            raise errors.InterfaceError(
                'RPC request returned invalid status code', response.status)

        noop = lambda x: x
        try:
            response_data = json.loads(response_body, parse_float=noop)
        except ValueError as e:
            logger.debug("Received response\n%s", response_body)
            raise errors.InterfaceError('valid JSON document', cause=e)

#        logger.debug("Received response\n%s", pprint.pformat(response_data))

        if 'response' not in response_data:
            raise errors.InterfaceError('missing response type')

        if expected_response_type is None:
            expected_response_type = request_data['request']

        if response_data['response'] != expected_response_type:
            raise errors.InterfaceError('unexpected response type "{}"'.format(
                response_data['response']))

        return response_data