Search in sources :

Example 1 with ResponseContentBufferTooSmallException

use of com.questdb.ex.ResponseContentBufferTooSmallException in project questdb by bluestreak01.

the class HttpServerTest method downloadChunked.

private File downloadChunked(ServerConfiguration conf, final int count, final int sz, boolean ssl, final boolean compressed) throws Exception {
    BootstrapEnv env = new BootstrapEnv();
    env.configuration = conf;
    env.matcher = new SimpleUrlMatcher() {

        {
            put("/test", new ContextHandler() {

                private int counter = -1;

                @Override
                public void handle(IOContext context) throws IOException {
                    ChunkedResponse r = context.chunkedResponse();
                    r.setCompressed(compressed);
                    r.status(200, "text/plain; charset=utf-8");
                    r.sendHeader();
                    counter = -1;
                    resume(context);
                }

                @Override
                public void resume(IOContext context) throws IOException {
                    ChunkedResponse r = context.chunkedResponse();
                    for (int i = counter + 1; i < count; i++) {
                        counter = i;
                        try {
                            for (int k = 0; k < sz; k++) {
                                Numbers.append(r, i);
                            }
                            r.put(Misc.EOL);
                        } catch (ResponseContentBufferTooSmallException ignore) {
                            // ignore, send as much as we can in one chunk
                            LOG.error().$("Response content buffer is too small").$();
                        }
                        r.sendChunk();
                    }
                    r.done();
                }

                @Override
                public void setupThread() {
                }
            });
        }
    };
    HttpServer server = new HttpServer(env);
    File out = temp.newFile();
    server.start();
    try {
        HttpTestUtils.download(clientBuilder(ssl), (ssl ? "https" : "http") + "://localhost:9000/test", out);
    } finally {
        server.halt();
    }
    return out;
}
Also used : BootstrapEnv(com.questdb.BootstrapEnv) ResponseContentBufferTooSmallException(com.questdb.ex.ResponseContentBufferTooSmallException)

Example 2 with ResponseContentBufferTooSmallException

use of com.questdb.ex.ResponseContentBufferTooSmallException in project questdb by bluestreak01.

the class ImportHandler method resume.

@Override
public void resume(IOContext context) throws IOException {
    super.resume(context);
    final ImportHandlerContext ctx = lvContext.get(context);
    final ChunkedResponse r = context.chunkedResponse();
    try {
        if (ctx.json) {
            resumeJson(ctx, r);
        } else {
            resumeText(ctx, r);
        }
    } catch (ResponseContentBufferTooSmallException ignored) {
        if (r.resetToBookmark()) {
            r.sendChunk();
        } else {
            // and disconnect socket
            throw DisconnectedChannelException.INSTANCE;
        }
    }
    ctx.clear();
}
Also used : ResponseContentBufferTooSmallException(com.questdb.ex.ResponseContentBufferTooSmallException) ChunkedResponse(com.questdb.net.http.ChunkedResponse)

Example 3 with ResponseContentBufferTooSmallException

use of com.questdb.ex.ResponseContentBufferTooSmallException in project questdb by bluestreak01.

the class CsvHandler method resume.

@SuppressWarnings("ConstantConditions")
@Override
public void resume(IOContext context) throws IOException {
    ExportHandlerContext ctx = localContext.get(context);
    if (ctx == null || ctx.cursor == null) {
        return;
    }
    final ChunkedResponse r = context.chunkedResponse();
    final int columnCount = ctx.metadata.getColumnCount();
    OUT: while (true) {
        try {
            SWITCH: switch(ctx.queryState) {
                case QUERY_METADATA:
                    for (; ctx.columnIndex < columnCount; ctx.columnIndex++) {
                        RecordColumnMetadata column = ctx.metadata.getColumnQuick(ctx.columnIndex);
                        r.bookmark();
                        if (ctx.columnIndex > 0) {
                            r.put(',');
                        }
                        r.putQuoted(column.getName());
                    }
                    r.put(Misc.EOL);
                    ctx.queryState = QUERY_RECORD_START;
                // fall through
                case QUERY_RECORD_START:
                    if (ctx.record == null) {
                        // check if cursor has any records
                        while (true) {
                            if (ctx.cursor.hasNext()) {
                                ctx.record = ctx.cursor.next();
                                ctx.count++;
                                if (ctx.count > ctx.skip) {
                                    break;
                                }
                            } else {
                                ctx.cursor.releaseCursor();
                                ctx.cursor = null;
                                ctx.queryState = QUERY_DATA_SUFFIX;
                                break SWITCH;
                            }
                        }
                    }
                    if (ctx.count > ctx.stop) {
                        ctx.queryState = QUERY_DATA_SUFFIX;
                        break;
                    }
                    ctx.queryState = QUERY_RECORD_COLUMNS;
                    ctx.columnIndex = 0;
                // fall through
                case QUERY_RECORD_COLUMNS:
                    for (; ctx.columnIndex < columnCount; ctx.columnIndex++) {
                        RecordColumnMetadata m = ctx.metadata.getColumnQuick(ctx.columnIndex);
                        r.bookmark();
                        if (ctx.columnIndex > 0) {
                            r.put(',');
                        }
                        putValue(r, m.getType(), ctx.record, ctx.columnIndex);
                    }
                    r.bookmark();
                    r.put(Misc.EOL);
                    ctx.record = null;
                    ctx.queryState = QUERY_RECORD_START;
                    break;
                case QUERY_DATA_SUFFIX:
                    sendDone(r, ctx);
                    break OUT;
                default:
                    break OUT;
            }
        } catch (ResponseContentBufferTooSmallException ignored) {
            if (r.resetToBookmark()) {
                r.sendChunk();
            } else {
                // what we have here is out unit of data, column value or query
                // is larger that response content buffer
                // all we can do in this scenario is to log appropriately
                // and disconnect socket
                ctx.info().$("Response buffer is too small, state=").$(ctx.queryState).$();
                throw DisconnectedChannelException.INSTANCE;
            }
        }
    }
}
Also used : ResponseContentBufferTooSmallException(com.questdb.ex.ResponseContentBufferTooSmallException) RecordColumnMetadata(com.questdb.common.RecordColumnMetadata) ChunkedResponse(com.questdb.net.http.ChunkedResponse)

Example 4 with ResponseContentBufferTooSmallException

use of com.questdb.ex.ResponseContentBufferTooSmallException in project questdb by bluestreak01.

the class QueryHandler method resume.

@SuppressWarnings("ConstantConditions")
@Override
public void resume(IOContext context) throws IOException {
    QueryHandlerContext ctx = localContext.get(context);
    if (ctx == null || ctx.cursor == null) {
        return;
    }
    final ChunkedResponse r = context.chunkedResponse();
    final int columnCount = ctx.metadata.getColumnCount();
    OUT: while (true) {
        try {
            SWITCH: switch(ctx.queryState) {
                case QUERY_PREFIX:
                    if (ctx.noMeta) {
                        r.put('{').putQuoted("dataset").put(":[");
                        ctx.queryState = QUERY_RECORD_START;
                        break;
                    }
                    r.bookmark();
                    r.put('{').putQuoted("query").put(':').encodeUtf8AndQuote(ctx.query);
                    r.put(',').putQuoted("columns").put(':').put('[');
                    ctx.queryState = QUERY_METADATA;
                    ctx.columnIndex = 0;
                // fall through
                case QUERY_METADATA:
                    for (; ctx.columnIndex < columnCount; ctx.columnIndex++) {
                        RecordColumnMetadata column = ctx.metadata.getColumnQuick(ctx.columnIndex);
                        r.bookmark();
                        if (ctx.columnIndex > 0) {
                            r.put(',');
                        }
                        r.put('{').putQuoted("name").put(':').putQuoted(column.getName()).put(',').putQuoted("type").put(':').putQuoted(ColumnType.nameOf(column.getType()));
                        r.put('}');
                    }
                    ctx.queryState = QUERY_META_SUFFIX;
                // fall through
                case QUERY_META_SUFFIX:
                    r.bookmark();
                    r.put("],\"dataset\":[");
                    ctx.queryState = QUERY_RECORD_START;
                // fall through
                case QUERY_RECORD_START:
                    if (ctx.record == null) {
                        // check if cursor has any records
                        while (true) {
                            if (ctx.cursor.hasNext()) {
                                ctx.record = ctx.cursor.next();
                                ctx.count++;
                                if (ctx.fetchAll && ctx.count > ctx.stop) {
                                    ctx.cancellationHandler.check();
                                    continue;
                                }
                                if (ctx.count > ctx.skip) {
                                    break;
                                }
                            } else {
                                ctx.queryState = QUERY_DATA_SUFFIX;
                                break SWITCH;
                            }
                        }
                    }
                    if (ctx.count > ctx.stop) {
                        ctx.queryState = QUERY_DATA_SUFFIX;
                        break;
                    }
                    r.bookmark();
                    if (ctx.count > ctx.skip + 1) {
                        r.put(',');
                    }
                    r.put('[');
                    ctx.queryState = QUERY_RECORD_COLUMNS;
                    ctx.columnIndex = 0;
                // fall through
                case QUERY_RECORD_COLUMNS:
                    for (; ctx.columnIndex < columnCount; ctx.columnIndex++) {
                        RecordColumnMetadata m = ctx.metadata.getColumnQuick(ctx.columnIndex);
                        r.bookmark();
                        if (ctx.columnIndex > 0) {
                            r.put(',');
                        }
                        putValue(r, m.getType(), ctx.record, ctx.columnIndex);
                    }
                    ctx.queryState = QUERY_RECORD_SUFFIX;
                case QUERY_RECORD_SUFFIX:
                    r.bookmark();
                    r.put(']');
                    ctx.record = null;
                    ctx.queryState = QUERY_RECORD_START;
                    break;
                case QUERY_DATA_SUFFIX:
                    sendDone(r, ctx);
                    break OUT;
                default:
                    break OUT;
            }
        } catch (ResponseContentBufferTooSmallException ignored) {
            if (r.resetToBookmark()) {
                r.sendChunk();
            } else {
                // what we have here is out unit of data, column value or query
                // is larger that response content buffer
                // all we can do in this scenario is to log appropriately
                // and disconnect socket
                ctx.info().$("Response buffer is too small, state=").$(ctx.queryState).$();
                throw DisconnectedChannelException.INSTANCE;
            }
        }
    }
}
Also used : ResponseContentBufferTooSmallException(com.questdb.ex.ResponseContentBufferTooSmallException) RecordColumnMetadata(com.questdb.common.RecordColumnMetadata) ChunkedResponse(com.questdb.net.http.ChunkedResponse)

Aggregations

ResponseContentBufferTooSmallException (com.questdb.ex.ResponseContentBufferTooSmallException)4 ChunkedResponse (com.questdb.net.http.ChunkedResponse)3 RecordColumnMetadata (com.questdb.common.RecordColumnMetadata)2 BootstrapEnv (com.questdb.BootstrapEnv)1