Example #1
0
void Conn::onRead(const boost::system::error_code& error, std::shared_ptr< boost::asio::streambuf > rd_buf) {
	if( unlikely(error) ) {
		log_func("[iproto_conn] %s:%u read error: %s", ep.address().to_string().c_str(), ep.port(), error.message().c_str() );
		dismissCallbacks(CB_ERR);
		if( error != boost::asio::error::operation_aborted ) {
			reconnect();
		}
		return;
	}
	if( unlikely(!rd_buf) )
		rd_buf.reset(new boost::asio::streambuf);
	if( LOG_DEBUG )
		log_func("[iproto_conn] %s:%u onRead rd_buf->size=%zu", ep.address().to_string().c_str(), ep.port(), rd_buf->size());
	while( rd_buf->size() >= sizeof(Header) ) {
		const PacketPtr *buf = boost::asio::buffer_cast< const PacketPtr * >( rd_buf->data() );
		if( LOG_DEBUG )
			log_func("[iproto_conn] %s:%u onRead buffer iproto packet hdr: len=%u sync=%u msg=%u", ep.address().to_string().c_str(), ep.port(),
				buf->hdr.len, buf->hdr.sync, buf->hdr.msg);
		size_t want_read = sizeof(Header)+buf->hdr.len;
		if( want_read <= rd_buf->size() ) {
			invokeCallback(buf->hdr.sync, RequestResult(CB_OK, Packet(buf)) );
			rd_buf->consume( sizeof(Header) + buf->hdr.len );
		}else{
			size_t rd = want_read - rd_buf->size();
			if( LOG_DEBUG )
				log_func("[iproto_conn] %s:%u onRead want_read=%zu", ep.address().to_string().c_str(), ep.port(), rd);
			boost::asio::async_read(sock, *rd_buf, boost::asio::transfer_at_least(rd),
				boost::bind(&Conn::onRead, shared_from_this(), boost::asio::placeholders::error, rd_buf) );
			return;
		}
	}
	if( likely(sock.is_open()) )
		boost::asio::async_read(sock, *rd_buf, boost::asio::transfer_at_least(sizeof(Header)-rd_buf->size()),
			boost::bind(&Conn::onRead, shared_from_this(), boost::asio::placeholders::error, rd_buf) );
}
Example #2
0
void Conn::onTimeout(const boost::system::error_code& error, uint32_t sync, std::shared_ptr< boost::asio::deadline_timer > timer) {
	if( unlikely(!error && timer) ) {
		if( LOG_DEBUG )
			log_func("[iproto_conn] %s:%u Packet with sync=%u timed out", ep.address().to_string().c_str(), ep.port(), sync);
		invokeCallback(sync, RequestResult(CB_TIMEOUT));
	}
}
Example #3
0
void Conn::dismissCallbacks(CB_Result res) {
	if( LOG_DEBUG )
		log_func("[iproto_conn] %s:%u dismissCallbacks", ep.address().to_string().c_str(), ep.port());
	ping_timer.cancel();
	for(auto it=callbacks_map.begin(); it!=callbacks_map.end(); ) {
		it = invokeCallback(it, RequestResult(res));
	}
	callbacks_map.clear();
}
Example #4
0
void CudaDb::requestQuery(const KdRequest &r) {
    KdRequest request = r;

    if (request.type == RT_CPU)
        request.result = RequestResult(new std::vector<long>());
    this->queue.push(request);

    switch (request.type) {
#if USE_CUDA
    case RT_CUDA:
    case RT_CUDA_DP:
    case RT_CUDA_IM:
        request.numBlocks = this->numBlocks / this->devices.size();
        for (size_t i = 0; i < this->devices.size(); i++) {
            request.ranges = (uint64_t*) this->fRange.data() + (request.numBlocks) * i * 2 * r.query->size;
            request.keys = (TripKey*) this->fBin.data() + (request.numBlocks) * i * KdBlock::MAX_RECORDS_PER_BLOCK * (r.query->size + 1);
            this->devices[i]->push(request);
        }
        break;

    case RT_CUDA_PARTIAL_IM: {
        KdBlock::QueryResult result = this->kdb->execute(*request.query);
        int numBlocks = result.blocks->size();
        request.totalBlocks = this->numBlocks / this->devices.size();
        int tmpct = 0;
        for (size_t i = 0; i < this->devices.size(); i++) {
            request.keys = (TripKey*) this->fBin.data() + (request.totalBlocks) * i * KdBlock::MAX_RECORDS_PER_BLOCK * (r.query->size + 1);
            request.ranges = new uint64_t[numBlocks];
            int ctBlocks = 0;
            for (int k = 0; k < numBlocks; k++) {
                uint64_t blockId = result.blocks->at(k).second;
                blockId /= KdBlock::MAX_RECORDS_PER_BLOCK;
                int index = blockId / request.totalBlocks;
                if(index == i) {
                    request.ranges[ctBlocks ++] = (blockId % request.totalBlocks);
                }
            }
            request.numBlocks = ctBlocks;
            this->devices[i]->push(request);
            tmpct += ctBlocks;
        }
    }
        break;

    case RT_CUDA_PARTIAL: {
        request.keys = (TripKey*) this->fBin.data();
        KdBlock::QueryResult result = this->kdb->execute(*request.query);
        int numBlocks = result.blocks->size();
        for (size_t i = 0; i < this->devices.size(); i++) {
            request.numBlocks = numBlocks / this->devices.size();
            request.ranges = new uint64_t[request.numBlocks];
            for (int k = 0; k < request.numBlocks; k++) {
                request.ranges[k] = result.blocks->at(request.numBlocks * i + k).second;
            }
            this->devices[i]->push(request);
        }
    }
        break;
#endif

    case RT_CPU: {
        //			hlog << "CPU execution" << endl;
        size_t EXTRA_BLOCKS_PER_LEAF = this->keySize;
        int noKeys = this->keySize - 1;
        int gsize = request.noRegions;

        KdBlock::QueryResult result = this->kdb->execute(*request.query);
        TripKey *keys = (TripKey*) fBin.data();
        //			printf("No. of blocks %zu \n", result.blocks->size());

        uint64_t noBlocks = result.blocks->size();


        int noThreads = omp_get_max_threads();
        std::vector<ResultVec> res(noThreads);


#pragma omp parallel for
        for (size_t i = 0; i < noBlocks; i++) {
            uint32_t count = result.blocks->at(i).first;
            uint64_t offset = result.blocks->at(i).second;
            for (uint32_t j = 0; j < count; j++) {
                uint64_t pos = (offset + j) * EXTRA_BLOCKS_PER_LEAF;
                TripKey * curKey = keys + pos;
                uint64_t index = * (curKey + noKeys);
                bool match = true;
                for(int k = 0;k < noKeys;k ++) {
                    if(!request.query->isMatched(curKey,k)) {
                        match = false;
                        break;
                    }
                }
                if(match) {
                    for(int k = 0;k < gsize;k ++) {
                        double x = uint2double(curKey[k * 2]);
                        double y = uint2double(curKey[k * 2 + 1]);
                        if(!Neighborhoods::isInside(request.regions[k].size(),&request.regions[k][0].first,x,y)) {
                            match = false;
                            break;
                        }
                    }
                }
                if(match) {
                    res[omp_get_thread_num()].push_back(index);
                }
            }
        }
        for (size_t i = 0; i < noThreads; i++) {
            request.result->insert(request.result->end(),res[i].begin(),res[i].end());
        }
    }
        break;

    default:
        fprintf(stderr, "Unhandled request type %d\n", request.state);
        break;
    }
}