use of com.alibaba.nacos.consistency.cp.CPProtocol in project nacos by alibaba.
the class CoreOpsControllerTest method testRaftOps.
@Test
public void testRaftOps() {
Mockito.when(protocolManager.getCpProtocol()).thenAnswer(invocationOnMock -> {
CPProtocol cpProtocol = Mockito.mock(CPProtocol.class);
Mockito.when(cpProtocol.execute(Mockito.anyMap())).thenReturn(RestResultUtils.success("res"));
return cpProtocol;
});
RestResult<String> result = coreOpsController.raftOps(new HashMap<>());
Assert.assertEquals("res", result.getData());
}
use of com.alibaba.nacos.consistency.cp.CPProtocol in project nacos by alibaba.
the class CoreOpsV2ControllerTest method testRaftOps.
@Test
public void testRaftOps() {
Mockito.when(protocolManager.getCpProtocol()).thenAnswer(invocationOnMock -> {
CPProtocol cpProtocol = Mockito.mock(CPProtocol.class);
Mockito.when(cpProtocol.execute(Mockito.anyMap())).thenReturn(RestResultUtils.success("res"));
return cpProtocol;
});
RestResult<String> result = coreOpsV2Controller.raftOps(new HashMap<>());
Assert.assertEquals("res", result.getData());
}
use of com.alibaba.nacos.consistency.cp.CPProtocol in project nacos by alibaba.
the class BaseClusterTest method run.
private static void run(final int index, final CountDownLatch latch, final Class<?> cls) {
Runnable runnable = () -> {
try {
EnvUtil.setIsStandalone(false);
final String path = Paths.get(System.getProperty("user.home"), "/nacos-" + index + "/").toString();
DiskUtils.deleteDirectory(path);
System.setProperty("nacos.home", path);
System.out.println("nacos.home is : [" + path + "]");
Map<String, Object> properties = new HashMap<>();
properties.put("server.port", "884" + (7 + index));
properties.put("nacos.home", path);
properties.put("nacos.logs.path", Paths.get(System.getProperty("user.home"), "nacos-" + index, "/logs/").toString());
properties.put("spring.jmx.enabled", false);
properties.put("nacos.core.snowflake.worker-id", index + 1);
MapPropertySource propertySource = new MapPropertySource("nacos_cluster_test", properties);
ConfigurableEnvironment environment = new StandardServletEnvironment();
environment.getPropertySources().addFirst(propertySource);
SpringApplication cluster = new SpringApplicationBuilder(cls).web(WebApplicationType.SERVLET).environment(environment).properties(clusterInfo).properties("embeddedStorage=true").build();
ConfigurableApplicationContext context = cluster.run();
DistributedDatabaseOperateImpl operate = context.getBean(DistributedDatabaseOperateImpl.class);
CPProtocol protocol = context.getBean(CPProtocol.class);
protocol.protocolMetaData().subscribe(operate.group(), MetadataKey.LEADER_META_DATA, o -> {
ProtocolMetaData.ValueItem item = (ProtocolMetaData.ValueItem) o;
System.out.println("node : 884" + (7 + index) + "-> select leader is : " + item.getData());
if (FINISHED[index].compareAndSet(false, true)) {
latch.countDown();
}
});
new Thread(() -> {
try {
Thread.sleep(5000L);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (FINISHED[index].compareAndSet(false, true)) {
latch.countDown();
}
}
});
applications.put(String.valueOf(properties.get("server.port")), context);
} catch (Throwable e) {
e.printStackTrace();
} finally {
latch.countDown();
}
};
runnable.run();
}
use of com.alibaba.nacos.consistency.cp.CPProtocol 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;
}
}
use of com.alibaba.nacos.consistency.cp.CPProtocol in project nacos by alibaba.
the class ConfigDerbyRaft_DITCase method test_f_id_generator_leader_transfer.
@Test
public void test_f_id_generator_leader_transfer() throws Exception {
ConfigurableApplicationContext context7 = applications.get("8847");
ConfigurableApplicationContext context8 = applications.get("8848");
ConfigurableApplicationContext context9 = applications.get("8849");
IdGeneratorManager manager7 = context7.getBean(IdGeneratorManager.class);
IdGeneratorManager manager8 = context8.getBean(IdGeneratorManager.class);
IdGeneratorManager manager9 = context9.getBean(IdGeneratorManager.class);
CPProtocol protocol7 = context7.getBean(CPProtocol.class);
CPProtocol protocol8 = context8.getBean(CPProtocol.class);
CPProtocol protocol9 = context9.getBean(CPProtocol.class);
final String configGroup = com.alibaba.nacos.config.server.constant.Constants.CONFIG_MODEL_RAFT_GROUP;
long preId = -1L;
long currentId = -1L;
if (protocol7.isLeader(configGroup)) {
preId = manager7.nextId(CONFIG_INFO_ID);
}
if (protocol8.isLeader(configGroup)) {
preId = manager8.nextId(CONFIG_INFO_ID);
}
if (protocol9.isLeader(configGroup)) {
preId = manager9.nextId(CONFIG_INFO_ID);
}
// transfer leader to ip:8807
Map<String, String> transfer = new HashMap<>();
transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9847");
RestResult<String> result = protocol7.execute(transfer);
System.out.println(result);
Assert.assertTrue(result.ok());
TimeUnit.SECONDS.sleep(2);
Assert.assertTrue(protocol7.isLeader(configGroup));
currentId = manager7.nextId(CONFIG_INFO_ID);
Assert.assertNotEquals(preId, currentId);
preId = currentId;
// transfer leader to ip:8808
transfer = new HashMap<>();
transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9848");
result = protocol8.execute(transfer);
System.out.println(result);
Assert.assertTrue(result.ok());
TimeUnit.SECONDS.sleep(2);
Assert.assertTrue(protocol8.isLeader(configGroup));
currentId = manager8.nextId(CONFIG_INFO_ID);
Assert.assertNotEquals(preId, currentId);
preId = currentId;
// transfer leader to ip:8809
transfer = new HashMap<>();
transfer.put(JRaftConstants.TRANSFER_LEADER, InetUtils.getSelfIP() + ":9849");
result = protocol9.execute(transfer);
System.out.println(result);
Assert.assertTrue(result.ok());
TimeUnit.SECONDS.sleep(2);
Assert.assertTrue(protocol9.isLeader(configGroup));
currentId = manager9.nextId(CONFIG_INFO_ID);
Assert.assertNotEquals(preId, currentId);
}
Aggregations