Search in sources :

Example 1 with DingoException

use of io.dingodb.common.error.DingoException in project dingo by dingodb.

the class RemoteServerCaller method call.

public static <T> T call(Supplier<Channel> channelSupplier, Message msg, Function<ByteBuffer, T> readFunction) {
    Channel channel = channelSupplier.get();
    CompletableFuture<T> future = new CompletableFuture<>();
    channel.registerMessageListener(callHandler(future, readFunction));
    channel.send(msg);
    try {
        return future.get(5, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        EXEC_INTERRUPT.throwFormatError("call remote server", currentThread().getName(), e.getMessage());
    } catch (ExecutionException e) {
        if (e.getCause() instanceof DingoException) {
            throw (DingoException) e.getCause();
        }
        EXEC.throwFormatError("call remote server", currentThread().getName(), e.getMessage());
    } catch (TimeoutException e) {
        EXEC_TIMEOUT.throwFormatError("call remote server", currentThread().getName(), e.getMessage());
    }
    throw UNKNOWN.asException();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) EXEC_INTERRUPT(io.dingodb.common.error.CommonError.EXEC_INTERRUPT) EXEC_TIMEOUT(io.dingodb.common.error.CommonError.EXEC_TIMEOUT) DingoException(io.dingodb.common.error.DingoException) Channel(io.dingodb.net.Channel) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with DingoException

use of io.dingodb.common.error.DingoException in project dingo by dingodb.

the class MetaServiceHandler method onMessage.

private void onMessage(Message message, Channel channel) {
    ByteBuffer buffer = ByteBuffer.wrap(message.toBytes());
    MetaServiceCode code = MetaServiceCode.valueOf(PrimitiveCodec.readZigZagInt(buffer));
    switch(code) {
        case LISTENER_TABLE:
            // todo
            break;
        case REFRESH_TABLES:
            // todo
            break;
        case GET_TABLE:
            try {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                outputStream.write(encodeZigZagInt(ServerError.OK.getCode()));
                getAndEncodeTableEntry(readString(buffer), outputStream);
                channel.send(SimpleMessage.builder().content(outputStream.toByteArray()).build());
            } catch (IOException e) {
                log.error("Serialize/deserialize table info error.", e);
                channel.send(ServerError.IO.message());
            } catch (NullPointerException e) {
                channel.send(ServerError.TABLE_NOT_FOUND.message());
            }
            break;
        case CREATE_TABLE:
            try {
                String name = readString(buffer);
                TableDefinition definition = TableDefinition.fromJson(readString(buffer));
                metaService.createTable(name, definition);
                channel.send(ServerError.OK.message());
            } catch (IOException e) {
                log.error("Serialize/deserialize table info error.", e);
                channel.send(ServerError.IO.message());
            } catch (DingoException error) {
                channel.send(ServerError.message(error));
            }
            break;
        case DELETE_TABLE:
            try {
                // todo delete table data
                String name = readString(buffer);
                if (metaService.dropTable(name)) {
                    channel.send(ServerError.OK.message());
                } else {
                    channel.send(ServerError.UNKNOWN.message());
                }
            } catch (DingoException error) {
                channel.send(ServerError.message(error));
            }
            break;
        case GET_ALL:
            try {
                Map<String, TableDefinition> tableDefinitions = metaService.getTableDefinitions();
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                byte[] size = encodeZigZagInt(tableDefinitions.size());
                outputStream.write(encodeZigZagInt(ServerError.OK.getCode()));
                outputStream.write(size);
                outputStream.flush();
                for (String name : tableDefinitions.keySet()) {
                    getAndEncodeTableEntry(name, outputStream);
                }
                channel.send(SimpleMessage.builder().content(outputStream.toByteArray()).build());
            } catch (IOException e) {
                log.error("Serialize/deserialize table info error.", e);
                channel.send(ServerError.IO.message());
            }
            break;
        default:
            channel.send(UNSUPPORTED_CODE.message());
    }
}
Also used : MetaServiceCode(io.dingodb.server.protocol.code.MetaServiceCode) DingoException(io.dingodb.common.error.DingoException) TableDefinition(io.dingodb.common.table.TableDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PrimitiveCodec.encodeString(io.dingodb.common.codec.PrimitiveCodec.encodeString) PrimitiveCodec.readString(io.dingodb.common.codec.PrimitiveCodec.readString) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DingoException (io.dingodb.common.error.DingoException)2 PrimitiveCodec.encodeString (io.dingodb.common.codec.PrimitiveCodec.encodeString)1 PrimitiveCodec.readString (io.dingodb.common.codec.PrimitiveCodec.readString)1 EXEC_INTERRUPT (io.dingodb.common.error.CommonError.EXEC_INTERRUPT)1 EXEC_TIMEOUT (io.dingodb.common.error.CommonError.EXEC_TIMEOUT)1 TableDefinition (io.dingodb.common.table.TableDefinition)1 Channel (io.dingodb.net.Channel)1 MetaServiceCode (io.dingodb.server.protocol.code.MetaServiceCode)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1