Example #1
0
    def post(self):
        self.log_debug("API server handler got POST request")
        uri = self.request.uri
        self.log_debug("called with uri: %s", uri)

        comps = filter(bool, uri.split('/'))
        if len(comps) < 2:
            self.send_error(status_code=404)
            return

        api_name = comps[0]
        cmd = comps[1]
        args = comps[2:]
        vargs = self.request.arguments

        self.log_debug("calling service:%s. cmd:%s. nargs: %d. nvargs: %d",
                       api_name, cmd, len(args), len(vargs))
        APIContainer.ensure_container_available(api_name)
        APIConnector.send_recv(api_name,
                               cmd,
                               args=args,
                               vargs=vargs,
                               on_recv=self.on_recv,
                               on_timeout=self.on_timeout,
                               on_overload=self.on_overload)
Example #2
0
    def on_recv(self, msg):
        self.log_info("response received for %s", self.request.uri)
        if 'nid' in msg:
            APIContainer.record_ping(msg['nid'])
        code = msg.get('code', 500)
        if 'data' in msg:
            self.log_debug("data received for %s", msg['data'])
        else:
            self.log_info("empty data received")
         
        if code == 200:
            start_line = tornado.httputil.ResponseStartLine('', self._status_code, self._reason)
            hdrs = tornado.httputil.HTTPHeaders(msg.get('hdrs', {}))
            data = msg['data']

            if type(data) == list:
                hdrs.add("Content-Length", str(len(data)))
                data = APIHandler.pack_into_binary(data)
            elif type(data) == dict:
                data = json.dumps(data)
            else:
                data = str(data)

            self.request.connection.write_headers(start_line, hdrs, data)
            self.request.connection.finish()
        else:
            self.send_error(status_code=code)
Example #3
0
    def post(self):
        self.log_debug("API server handler got POST request")
        uri = self.request.uri
        self.log_debug("called with uri: %s", uri)

        comps = filter(bool, uri.split('/'))
        self.log_debug("comps is ::  %s", comps)
        if len(comps) < 2:
            self.send_error(status_code=404)
            return
        api_name = comps[0]
        cmd = comps[1]
        args = comps[2:]
        vargs = self.request.arguments

        ## parse_result=urlparse(comps[1])
        ## self.log_debug("parse_result is ::  %s", parse_result)
        ## cmd = parse_result.path

        self.log_debug("args is ::  %s", args)
        self.log_debug("vargs is ::  %s", vargs)
        ## self.log_debug("data passed is ::: %s", self.request.body)
        ## self.log_debug("arguments passed is ::: %s", self.request.arguments.values())

        self.log_debug("calling service:%s. cmd:%s. nargs: %d. nvargs: %d", api_name, cmd, len(args), len(vargs))
        APIContainer.ensure_container_available(api_name)
        APIConnector.send_recv(api_name, cmd, args=args, vargs=vargs,
                               on_recv=self.on_recv,
                               on_timeout=self.on_timeout,
                               on_overload=self.on_overload)
Example #4
0
    def get(self):
        self.log_debug("APIInfo handler got GET request")
        key = self.get_argument("key", None)
        sign = self.get_argument("sign", None)

        if key is None or sign is None:
            self.send_error()
            return
        sign2 = signstr(key, JBoxCfg.get('sesskey'))

        if sign != sign2:
            self.log_info("signature mismatch. key:%r sign:%r expected:%r", key, sign, sign2)
            self.send_error()
            return

        api_status = APIContainer.get_cluster_api_status()
        self.log_info("cluster api status: %r", api_status)

        # filter out instances that should not accept more load
        filtered_api_status = {k: v for (k, v) in api_status.iteritems() if v['accept']}
        preferred_instances = filtered_api_status.keys()

        # flip the dict
        per_api_instances = dict()
        for (inst, status) in filtered_api_status.iteritems():
            api_names = status['api_status'].keys()
            for api_name in api_names:
                v = per_api_instances.get(api_name, [])
                v.append(inst)

        per_api_instances[" preferred "] = preferred_instances
        self.log_info("per api instances: %r", per_api_instances)
        self.write(per_api_instances)
        return
Example #5
0
    def post(self):
        self.log_debug("API server handler got POST request")
        uri = self.request.uri
        self.log_debug("called with uri: %s", uri)

        comps = filter(bool, uri.split('/'))
        if len(comps) < 2:
            self.send_error(status_code=404)
            return

        api_name = comps[0]
        cmd = comps[1]
        args = comps[2:]
        vargs = self.request.arguments

        self.log_debug("calling service:%s. cmd:%s. nargs: %d. nvargs: %d", api_name, cmd, len(args), len(vargs))
        APIContainer.ensure_container_available(api_name)
        APIConnector.send_recv(api_name, cmd, args=args, vargs=vargs,
                               on_recv=self.on_recv,
                               on_timeout=self.on_timeout,
                               on_overload=self.on_overload)
Example #6
0
    def on_recv(self, msg):
        self.log_info("response received for %s", self.request.uri)
        if 'nid' in msg:
            APIContainer.record_ping(msg['nid'])
        code = msg.get('code', 500)
        if code == 200:
            start_line = tornado.httputil.ResponseStartLine(
                '', self._status_code, self._reason)
            hdrs = tornado.httputil.HTTPHeaders(msg.get('hdrs', {}))
            data = msg['data']

            if type(data) == list:
                hdrs.add("Content-Length", str(len(data)))
                data = APIHandler.pack_into_binary(data)
            elif type(data) == dict:
                data = json.dumps(data)
            else:
                data = str(data)

            self.request.connection.write_headers(start_line, hdrs, data)
            self.request.connection.finish()
        else:
            self.send_error(status_code=code)
Example #7
0
    def handle_if_instance_info(self, is_allowed):
        stats = self.get_argument('instance_info', None)
        if stats is None:
            return False

        if not is_allowed:
            AdminHandler.log_error("Show instance info not allowed for user")
            response = {'code': -1, 'data': 'You do not have permissions to view these stats'}
        else:
            try:
                if stats == 'load':
                    result = {}
                    # get cluster loads
                    average_load = Compute.get_cluster_average_stats('Load')
                    if None != average_load:
                        result['Average Load'] = average_load

                    machine_loads = Compute.get_cluster_stats('Load')
                    if None != machine_loads:
                        for n, v in machine_loads.iteritems():
                            result['Instance ' + n] = v
                elif stats == 'sessions':
                    result = dict()
                    instances = Compute.get_all_instances()

                    for idx in range(0, len(instances)):
                        try:
                            inst = instances[idx]
                            result[inst] = JBoxAsyncJob.sync_session_status(inst)['data']
                        except:
                            JBoxHandler.log_error("Error receiving sessions list from %r", inst)
                elif stats == 'apis':
                    result = APIContainer.get_cluster_api_status()
                else:
                    raise Exception("unknown command %s" % (stats,))

                response = {'code': 0, 'data': result}
            except:
                AdminHandler.log_error("exception while getting stats")
                AdminHandler._get_logger().exception("exception while getting stats")
                response = {'code': -1, 'data': 'error getting stats'}

        self.write(response)
        return True
Example #8
0
    def get(self):
        self.log_debug("APIInfo handler got GET request")
        key = self.get_argument("key", None)
        sign = self.get_argument("sign", None)

        if key is None or sign is None:
            self.send_error()
            return
        sign2 = signstr(key, JBoxCfg.get('sesskey'))

        if sign != sign2:
            self.log_info("signature mismatch. key:%r sign:%r expected:%r",
                          key, sign, sign2)
            self.send_error()
            return

        api_status = APIContainer.get_cluster_api_status()
        self.log_info("cluster api status: %r", api_status)

        # filter out instances that should not accept more load
        filtered_api_status = {
            k: v
            for (k, v) in api_status.iteritems() if v['accept']
        }
        preferred_instances = filtered_api_status.keys()

        # flip the dict
        per_api_instances = dict()
        for (inst, status) in filtered_api_status.iteritems():
            api_names = status['api_status'].keys()
            for api_name in api_names:
                v = per_api_instances.get(api_name, [])
                v.append(inst)

        per_api_instances[" preferred "] = preferred_instances
        self.log_info("per api instances: %r", per_api_instances)
        self.write(per_api_instances)
        return