Search in sources :

Example 1 with ConsistencyException

use of com.alibaba.nacos.consistency.exception.ConsistencyException in project nacos by alibaba.

the class DistributedDatabaseOperateImpl method onApply.

@Override
public Response onApply(WriteRequest log) {
    LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "onApply info : log : {}", log);
    final ByteString byteString = log.getData();
    Preconditions.checkArgument(byteString != null, "Log.getData() must not null");
    List<ModifyRequest> sqlContext = serializer.deserialize(byteString.toByteArray(), List.class);
    final Lock lock = readLock;
    lock.lock();
    try {
        boolean isOk = false;
        if (log.containsExtendInfo(DATA_IMPORT_KEY)) {
            isOk = doDataImport(jdbcTemplate, sqlContext);
        } else {
            sqlContext.sort(Comparator.comparingInt(ModifyRequest::getExecuteNo));
            isOk = update(transactionTemplate, jdbcTemplate, sqlContext);
            // If there is additional information, post processing
            // Put into the asynchronous thread pool for processing to avoid blocking the
            // normal execution of the state machine
            ConfigExecutor.executeEmbeddedDump(() -> handleExtendInfo(log.getExtendInfoMap()));
        }
        return Response.newBuilder().setSuccess(isOk).build();
    // We do not believe that an error caused by a problem with an SQL error
    // should trigger the stop operation of the raft state machine
    } catch (BadSqlGrammarException | DataIntegrityViolationException e) {
        return Response.newBuilder().setSuccess(false).setErrMsg(e.toString()).build();
    } catch (DataAccessException e) {
        throw new ConsistencyException(e.toString());
    } finally {
        lock.unlock();
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) ByteString(com.google.protobuf.ByteString) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) ModifyRequest(com.alibaba.nacos.config.server.service.sql.ModifyRequest) DataAccessException(org.springframework.dao.DataAccessException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 2 with ConsistencyException

use of com.alibaba.nacos.consistency.exception.ConsistencyException in project nacos by alibaba.

the class JRaftServer method get.

CompletableFuture<Response> get(final ReadRequest request) {
    final String group = request.getGroup();
    CompletableFuture<Response> future = new CompletableFuture<>();
    final RaftGroupTuple tuple = findTupleByGroup(group);
    if (Objects.isNull(tuple)) {
        future.completeExceptionally(new NoSuchRaftGroupException(group));
        return future;
    }
    final Node node = tuple.node;
    final RequestProcessor processor = tuple.processor;
    try {
        node.readIndex(BytesUtil.EMPTY_BYTES, new ReadIndexClosure() {

            @Override
            public void run(Status status, long index, byte[] reqCtx) {
                if (status.isOk()) {
                    try {
                        Response response = processor.onRequest(request);
                        future.complete(response);
                    } catch (Throwable t) {
                        MetricsMonitor.raftReadIndexFailed();
                        future.completeExceptionally(new ConsistencyException("The conformance protocol is temporarily unavailable for reading", t));
                    }
                    return;
                }
                MetricsMonitor.raftReadIndexFailed();
                Loggers.RAFT.error("ReadIndex has error : {}, go to Leader read.", status.getErrorMsg());
                MetricsMonitor.raftReadFromLeader();
                readFromLeader(request, future);
            }
        });
        return future;
    } catch (Throwable e) {
        MetricsMonitor.raftReadFromLeader();
        Loggers.RAFT.warn("Raft linear read failed, go to Leader read logic : {}", e.toString());
        // run raft read
        readFromLeader(request, future);
        return future;
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Node(com.alipay.sofa.jraft.Node) NoSuchRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException) Response(com.alibaba.nacos.consistency.entity.Response) CompletableFuture(java.util.concurrent.CompletableFuture) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor)

Example 3 with ConsistencyException

use of com.alibaba.nacos.consistency.exception.ConsistencyException in project nacos by alibaba.

the class NacosStateMachine method adapterToJRaftSnapshot.

private void adapterToJRaftSnapshot(Collection<SnapshotOperation> userOperates) {
    List<JSnapshotOperation> tmp = new ArrayList<>();
    for (SnapshotOperation item : userOperates) {
        if (item == null) {
            Loggers.RAFT.error("Existing SnapshotOperation for null");
            continue;
        }
        tmp.add(new JSnapshotOperation() {

            @Override
            public void onSnapshotSave(SnapshotWriter writer, Closure done) {
                final Writer wCtx = new Writer(writer.getPath());
                // Do a layer of proxy operation to shield different Raft
                // components from implementing snapshots
                final BiConsumer<Boolean, Throwable> callFinally = (result, t) -> {
                    boolean[] results = new boolean[wCtx.listFiles().size()];
                    int[] index = new int[] { 0 };
                    wCtx.listFiles().forEach((file, meta) -> {
                        try {
                            results[index[0]++] = writer.addFile(file, buildMetadata(meta));
                        } catch (Exception e) {
                            throw new ConsistencyException(e);
                        }
                    });
                    final Status status = result && !Arrays.asList(results).stream().anyMatch(Boolean.FALSE::equals) ? Status.OK() : new Status(RaftError.EIO, "Fail to compress snapshot at %s, error is %s", writer.getPath(), t == null ? "" : t.getMessage());
                    done.run(status);
                };
                item.onSnapshotSave(wCtx, callFinally);
            }

            @Override
            public boolean onSnapshotLoad(SnapshotReader reader) {
                final Map<String, LocalFileMeta> metaMap = new HashMap<>(reader.listFiles().size());
                for (String fileName : reader.listFiles()) {
                    final LocalFileMetaOutter.LocalFileMeta meta = (LocalFileMetaOutter.LocalFileMeta) reader.getFileMeta(fileName);
                    byte[] bytes = meta.getUserMeta().toByteArray();
                    final LocalFileMeta fileMeta;
                    if (bytes == null || bytes.length == 0) {
                        fileMeta = new LocalFileMeta();
                    } else {
                        fileMeta = JacksonUtils.toObj(bytes, LocalFileMeta.class);
                    }
                    metaMap.put(fileName, fileMeta);
                }
                final Reader rCtx = new Reader(reader.getPath(), metaMap);
                return item.onSnapshotLoad(rCtx);
            }

            @Override
            public String info() {
                return item.toString();
            }
        });
    }
    this.operations = Collections.unmodifiableList(tmp);
}
Also used : Status(com.alipay.sofa.jraft.Status) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) Closure(com.alipay.sofa.jraft.Closure) ArrayList(java.util.ArrayList) Reader(com.alibaba.nacos.consistency.snapshot.Reader) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) RaftException(com.alipay.sofa.jraft.error.RaftException) SnapshotOperation(com.alibaba.nacos.consistency.snapshot.SnapshotOperation) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta) HashMap(java.util.HashMap) Map(java.util.Map) Writer(com.alibaba.nacos.consistency.snapshot.Writer) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) BiConsumer(java.util.function.BiConsumer)

Aggregations

ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)3 Status (com.alipay.sofa.jraft.Status)2 ModifyRequest (com.alibaba.nacos.config.server.service.sql.ModifyRequest)1 RequestProcessor (com.alibaba.nacos.consistency.RequestProcessor)1 Response (com.alibaba.nacos.consistency.entity.Response)1 LocalFileMeta (com.alibaba.nacos.consistency.snapshot.LocalFileMeta)1 Reader (com.alibaba.nacos.consistency.snapshot.Reader)1 SnapshotOperation (com.alibaba.nacos.consistency.snapshot.SnapshotOperation)1 Writer (com.alibaba.nacos.consistency.snapshot.Writer)1 NoSuchRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)1 Closure (com.alipay.sofa.jraft.Closure)1 Node (com.alipay.sofa.jraft.Node)1 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)1 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)1 RaftException (com.alipay.sofa.jraft.error.RaftException)1 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)1 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1