use of com.alibaba.nacos.common.utils.Observer 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;
}
}
Aggregations