Search in sources :

Example 1 with NJdbcException

use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.

the class DistributedDatabaseOperateImpl method queryOne.

@Override
public <R> R queryOne(String sql, Object[] args, RowMapper<R> mapper) {
    try {
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryOne info : sql : {}, args : {}", sql, args);
        byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_ONE_WITH_MAPPER_WITH_ARGS).sql(sql).args(args).className(mapper.getClass().getCanonicalName()).build());
        final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
        Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
        if (response.getSuccess()) {
            return serializer.deserialize(response.getData().toByteArray(), ClassUtils.resolveGenericTypeByInterface(mapper.getClass()));
        }
        throw new NJdbcException(response.getErrMsg(), response.getErrMsg());
    } catch (Exception e) {
        LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
    }
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) TimeoutException(java.util.concurrent.TimeoutException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DataAccessException(org.springframework.dao.DataAccessException) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)

Example 2 with NJdbcException

use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.

the class DistributedDatabaseOperateImpl method update.

@Override
public Boolean update(List<ModifyRequest> sqlContext, BiConsumer<Boolean, Throwable> consumer) {
    try {
        // Since the SQL parameter is Object[], in order to ensure that the types of
        // array elements are not lost, the serialization here is done using the java-specific
        // serialization framework, rather than continuing with the protobuff
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "modifyRequests info : {}", sqlContext);
        // {timestamp}-{group}-{ip:port}-{signature}
        final String key = System.currentTimeMillis() + "-" + group() + "-" + memberManager.getSelf().getAddress() + "-" + MD5Utils.md5Hex(sqlContext.toString(), Constants.ENCODE);
        WriteRequest request = WriteRequest.newBuilder().setGroup(group()).setKey(key).setData(ByteString.copyFrom(serializer.serialize(sqlContext))).putAllExtendInfo(EmbeddedStorageContextUtils.getCurrentExtendInfo()).setType(sqlContext.getClass().getCanonicalName()).build();
        if (Objects.isNull(consumer)) {
            Response response = this.protocol.write(request);
            if (response.getSuccess()) {
                return true;
            }
            LogUtil.DEFAULT_LOG.error("execute sql modify operation failed : {}", response.getErrMsg());
            return false;
        } else {
            this.protocol.writeAsync(request).whenComplete((BiConsumer<Response, Throwable>) (response, ex) -> {
                String errMsg = Objects.isNull(ex) ? response.getErrMsg() : ExceptionUtil.getCause(ex).getMessage();
                consumer.accept(response.getSuccess(), StringUtils.isBlank(errMsg) ? null : new NJdbcException(errMsg));
            });
        }
        return true;
    } catch (TimeoutException e) {
        LogUtil.FATAL_LOG.error("An timeout exception occurred during the update operation");
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
    } catch (Throwable e) {
        LogUtil.FATAL_LOG.error("An exception occurred during the update operation : {}", e);
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
    }
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) LoggerUtils(com.alibaba.nacos.common.utils.LoggerUtils) Subscriber(com.alibaba.nacos.common.notify.listener.Subscriber) LocalDataSourceServiceImpl(com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) ServerMemberManager(com.alibaba.nacos.core.cluster.ServerMemberManager) TimeoutException(java.util.concurrent.TimeoutException) ConditionDistributedEmbedStorage(com.alibaba.nacos.config.server.configuration.ConditionDistributedEmbedStorage) Constants(com.alibaba.nacos.config.server.constant.Constants) MD5Utils(com.alibaba.nacos.common.utils.MD5Utils) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Map(java.util.Map) ProtocolManager(com.alibaba.nacos.core.distributed.ProtocolManager) JacksonUtils(com.alibaba.nacos.common.utils.JacksonUtils) RestResult(com.alibaba.nacos.common.model.RestResult) RaftDbErrorEvent(com.alibaba.nacos.config.server.model.event.RaftDbErrorEvent) SerializeFactory(com.alibaba.nacos.consistency.SerializeFactory) DynamicDataSource(com.alibaba.nacos.config.server.service.datasource.DynamicDataSource) ConfigExecutor(com.alibaba.nacos.config.server.utils.ConfigExecutor) GenericType(com.alibaba.nacos.core.utils.GenericType) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) Objects(java.util.Objects) DiskUtils(com.alibaba.nacos.sys.utils.DiskUtils) List(java.util.List) Event(com.alibaba.nacos.common.notify.Event) StringUtils(com.alibaba.nacos.common.utils.StringUtils) RestResultUtils(com.alibaba.nacos.common.model.RestResultUtils) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) RowMapper(org.springframework.jdbc.core.RowMapper) Conditional(org.springframework.context.annotation.Conditional) DataAccessException(org.springframework.dao.DataAccessException) SnapshotOperation(com.alibaba.nacos.consistency.snapshot.SnapshotOperation) DerbyLoadEvent(com.alibaba.nacos.config.server.model.event.DerbyLoadEvent) LogUtil(com.alibaba.nacos.config.server.utils.LogUtil) CompletableFuture(java.util.concurrent.CompletableFuture) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) JustForTest(com.alibaba.nacos.common.JustForTest) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) NacosException(com.alibaba.nacos.api.exception.NacosException) EmbeddedStorageContextUtils(com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils) BiConsumer(java.util.function.BiConsumer) Preconditions(com.alibaba.nacos.common.utils.Preconditions) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) QueryType(com.alibaba.nacos.config.server.service.sql.QueryType) ConfigDumpEvent(com.alibaba.nacos.config.server.model.event.ConfigDumpEvent) ExceptionUtil(com.alibaba.nacos.common.utils.ExceptionUtil) DumpConfigHandler(com.alibaba.nacos.config.server.service.dump.DumpConfigHandler) Serializer(com.alibaba.nacos.consistency.Serializer) SelectRequest(com.alibaba.nacos.config.server.service.sql.SelectRequest) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) File(java.io.File) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) Response(com.alibaba.nacos.consistency.entity.Response) Component(org.springframework.stereotype.Component) Lock(java.util.concurrent.locks.Lock) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ModifyRequest(com.alibaba.nacos.config.server.service.sql.ModifyRequest) RowMapperManager(com.alibaba.nacos.config.server.service.repository.RowMapperManager) CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) Comparator(java.util.Comparator) ClassUtils(com.alibaba.nacos.core.utils.ClassUtils) Collections(java.util.Collections) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteString(com.google.protobuf.ByteString) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with NJdbcException

use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.

the class DistributedDatabaseOperateImpl method queryMany.

@Override
public <R> List<R> queryMany(String sql, Object[] args, RowMapper<R> mapper) {
    try {
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryMany info : sql : {}, args : {}", sql, args);
        byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_MANY_WITH_MAPPER_WITH_ARGS).sql(sql).args(args).className(mapper.getClass().getCanonicalName()).build());
        final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
        Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
        if (response.getSuccess()) {
            return serializer.deserialize(response.getData().toByteArray(), List.class);
        }
        throw new NJdbcException(response.getErrMsg());
    } catch (Exception e) {
        LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
    }
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) TimeoutException(java.util.concurrent.TimeoutException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DataAccessException(org.springframework.dao.DataAccessException) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)

Example 4 with NJdbcException

use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.

the class DistributedDatabaseOperateImpl method queryOne.

@Override
public <R> R queryOne(String sql, Class<R> cls) {
    try {
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryOne info : sql : {}", sql);
        byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_ONE_NO_MAPPER_NO_ARGS).sql(sql).className(cls.getCanonicalName()).build());
        final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
        Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
        if (response.getSuccess()) {
            return serializer.deserialize(response.getData().toByteArray(), cls);
        }
        throw new NJdbcException(response.getErrMsg(), response.getErrMsg());
    } catch (Exception e) {
        LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
    }
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) TimeoutException(java.util.concurrent.TimeoutException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DataAccessException(org.springframework.dao.DataAccessException) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)

Example 5 with NJdbcException

use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.

the class DistributedDatabaseOperateImpl method queryOne.

@Override
public <R> R queryOne(String sql, Object[] args, Class<R> cls) {
    try {
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryOne info : sql : {}, args : {}", sql, args);
        byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_ONE_NO_MAPPER_WITH_ARGS).sql(sql).args(args).className(cls.getCanonicalName()).build());
        final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
        Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
        if (response.getSuccess()) {
            return serializer.deserialize(response.getData().toByteArray(), cls);
        }
        throw new NJdbcException(response.getErrMsg(), response.getErrMsg());
    } catch (Exception e) {
        LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
    }
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) TimeoutException(java.util.concurrent.TimeoutException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DataAccessException(org.springframework.dao.DataAccessException) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)

Aggregations

NacosException (com.alibaba.nacos.api.exception.NacosException)7 NacosRuntimeException (com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)7 NJdbcException (com.alibaba.nacos.config.server.exception.NJdbcException)7 Response (com.alibaba.nacos.consistency.entity.Response)7 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)7 TimeoutException (java.util.concurrent.TimeoutException)7 DataAccessException (org.springframework.dao.DataAccessException)7 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)7 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)7 JustForTest (com.alibaba.nacos.common.JustForTest)1 RestResult (com.alibaba.nacos.common.model.RestResult)1 RestResultUtils (com.alibaba.nacos.common.model.RestResultUtils)1 Event (com.alibaba.nacos.common.notify.Event)1 NotifyCenter (com.alibaba.nacos.common.notify.NotifyCenter)1 Subscriber (com.alibaba.nacos.common.notify.listener.Subscriber)1 ExceptionUtil (com.alibaba.nacos.common.utils.ExceptionUtil)1 JacksonUtils (com.alibaba.nacos.common.utils.JacksonUtils)1 LoggerUtils (com.alibaba.nacos.common.utils.LoggerUtils)1 MD5Utils (com.alibaba.nacos.common.utils.MD5Utils)1 Preconditions (com.alibaba.nacos.common.utils.Preconditions)1