Search in sources :

Example 1 with Observable

use of com.alibaba.nacos.common.utils.Observable in project nacos by alibaba.

the class EmbeddedDumpService method init.

@PostConstruct
@Override
protected void init() throws Throwable {
    if (EnvUtil.getStandaloneMode()) {
        dumpOperate(processor, dumpAllProcessor, dumpAllBetaProcessor, dumpAllTagProcessor);
        return;
    }
    CPProtocol protocol = protocolManager.getCpProtocol();
    AtomicReference<Throwable> errorReference = new AtomicReference<>(null);
    CountDownLatch waitDumpFinish = new CountDownLatch(1);
    // watch path => /nacos_config/leader/ has value ?
    Observer observer = new Observer() {

        @Override
        public void update(Observable o) {
            if (!(o instanceof ProtocolMetaData.ValueItem)) {
                return;
            }
            final Object arg = ((ProtocolMetaData.ValueItem) o).getData();
            GlobalExecutor.executeByCommon(() -> {
                // must make sure that there is a value here to perform the correct operation that follows
                if (Objects.isNull(arg)) {
                    return;
                }
                // Identify without a timeout mechanism
                EmbeddedStorageContextUtils.putExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA, "true");
                // Remove your own listening to avoid task accumulation
                boolean canEnd = false;
                for (; ; ) {
                    try {
                        dumpOperate(processor, dumpAllProcessor, dumpAllBetaProcessor, dumpAllTagProcessor);
                        protocol.protocolMetaData().unSubscribe(Constants.CONFIG_MODEL_RAFT_GROUP, MetadataKey.LEADER_META_DATA, this);
                        canEnd = true;
                    } catch (Throwable ex) {
                        if (!shouldRetry(ex)) {
                            errorReference.set(ex);
                            canEnd = true;
                        }
                    }
                    if (canEnd) {
                        ThreadUtils.countDown(waitDumpFinish);
                        break;
                    }
                    ThreadUtils.sleep(500L);
                }
                EmbeddedStorageContextUtils.cleanAllContext();
            });
        }
    };
    protocol.protocolMetaData().subscribe(Constants.CONFIG_MODEL_RAFT_GROUP, MetadataKey.LEADER_META_DATA, observer);
    // We must wait for the dump task to complete the callback operation before
    // continuing with the initialization
    ThreadUtils.latchAwait(waitDumpFinish);
    // If an exception occurs during the execution of the dump task, the exception
    // needs to be thrown, triggering the node to start the failed process
    final Throwable ex = errorReference.get();
    if (Objects.nonNull(ex)) {
        throw ex;
    }
}
Also used : CPProtocol(com.alibaba.nacos.consistency.cp.CPProtocol) Observer(com.alibaba.nacos.common.utils.Observer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Observable(com.alibaba.nacos.common.utils.Observable) PostConstruct(javax.annotation.PostConstruct)

Aggregations

Observable (com.alibaba.nacos.common.utils.Observable)1 Observer (com.alibaba.nacos.common.utils.Observer)1 CPProtocol (com.alibaba.nacos.consistency.cp.CPProtocol)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 PostConstruct (javax.annotation.PostConstruct)1