Here are the examples of the java api org.apache.curator.test.TestingServer taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
255 Examples
19
Source : NestedZkUtils.java
with Apache License 2.0
from vipshop
with Apache License 2.0
from vipshop
/**
* Created by xiaopeng.he on 2016/7/8.
*/
public clreplaced NestedZkUtils {
private TestingServer testingServer;
private int port;
public void startServer() throws Exception {
try (ServerSocket socket = new ServerSocket(0)) {
port = socket.getLocalPort();
} catch (IOException e) {
}
testingServer = new TestingServer(port);
}
public void stopServer() throws IOException {
if (testingServer != null) {
testingServer.close();
}
}
public CuratorFramework createClient(String namespace) throws InterruptedException {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
CuratorFramework curatorFramework = // long
builder.connectString("127.0.0.1:" + port).sessionTimeoutMs(600 * 1000).retryPolicy(new RetryNTimes(3, 1000)).namespace(namespace).build();
curatorFramework.start();
curatorFramework.blockUntilConnected();
return curatorFramework;
}
}
19
Source : ZookeeperRegistryCenterTest.java
with Apache License 2.0
from vipshop
with Apache License 2.0
from vipshop
public clreplaced ZookeeperRegistryCenterTest {
private static final String NAMESPACE = "ut-saturn";
private int PORT = 3181;
private TestingServer testingServer;
@Before
public void before() throws Exception {
startServer();
}
@After
public void after() throws Exception {
stopServer();
}
@Test
public void testZkClientConfig() throws Exception {
// default settings
CuratorFramework client = initZk("127.0.0.1:" + PORT, "ut-ns");
replacedertEquals(20000L, client.getZookeeperClient().getConnectionTimeoutMs());
replacedertEquals(20000L, client.getZookeeperClient().getZooKeeper().getSessionTimeout());
ExponentialBackoffRetry retryPolicy = (ExponentialBackoffRetry) client.getZookeeperClient().getRetryPolicy();
replacedertEquals(3, retryPolicy.getN());
// set VIP_SATURN_ZK_CLIENT_CONNECTION_TIMEOUT = true
System.setProperty("VIP_SATURN_USE_UNSTABLE_NETWORK_SETTING", "true");
SystemEnvProperties.loadProperties();
client = initZk("127.0.0.1:" + PORT, "ut-ns");
replacedertEquals(40000L, client.getZookeeperClient().getConnectionTimeoutMs());
replacedertEquals(40000L, client.getZookeeperClient().getZooKeeper().getSessionTimeout());
retryPolicy = (ExponentialBackoffRetry) client.getZookeeperClient().getRetryPolicy();
replacedertEquals(9, retryPolicy.getN());
}
private CuratorFramework initZk(String serverLists, String namespace) {
ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(serverLists, namespace, 1000, 3000);
ZookeeperRegistryCenter regCenter = new ZookeeperRegistryCenter(zkConfig);
regCenter.init();
return (CuratorFramework) regCenter.getRawClient();
}
public void startServer() throws Exception {
try (ServerSocket socket = new ServerSocket(0)) {
PORT = socket.getLocalPort();
} catch (IOException e) {
}
System.err.println("zkTestServer starting. Port: " + PORT);
testingServer = new TestingServer(PORT);
}
public void stopServer() throws IOException {
if (testingServer != null) {
testingServer.stop();
}
}
}
19
Source : LocalZookeeperServer.java
with Apache License 2.0
from sofastack
with Apache License 2.0
from sofastack
public static void main(String[] args) {
try {
TestingServer server = new TestingServer(2181, true);
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
19
Source : BaseZkTest.java
with Apache License 2.0
from sofastack
with Apache License 2.0
from sofastack
/**
* if you use zk to be registry ,your test case must be extends this clreplaced
* @author <a href=mailto:[email protected]>leizhiyuan</a>
*/
public abstract clreplaced BaseZkTest {
protected static TestingServer server = null;
@BeforeClreplaced
public static void adBeforeClreplaced() {
RpcRunningState.setUnitTestMode(true);
try {
server = new TestingServer(2181, true);
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClreplaced
public static void adAfterClreplaced() {
RpcRuntimeContext.destroy();
RpcInternalContext.removeContext();
RpcInvokeContext.removeContext();
if (server != null) {
try {
server.stop();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
19
Source : TransferProviderStarter.java
with Apache License 2.0
from seata
with Apache License 2.0
from seata
/**
* 转账TCC Dubbo服务 提供方
*
* @author zhangsen
*/
public clreplaced TransferProviderStarter {
private static TestingServer server;
public static void main(String[] args) throws Exception {
// mock zk server
mockZKServer();
ClreplacedPathXmlApplicationContext applicationContext = new ClreplacedPathXmlApplicationContext(new String[] { "spring/seata-tcc.xml", "spring/seata-dubbo-provider.xml", "db-bean/to-datasource-bean.xml", "db-bean/from-datasource-bean.xml" });
// 初始化数据库和账号余额
TransferDataPrepares transferDataPrepares = (TransferDataPrepares) applicationContext.getBean("transferDataPrepares");
transferDataPrepares.init(100);
new ApplicationKeeper(applicationContext).keep();
}
private static void mockZKServer() throws Exception {
// Mock zk server,作为 transfer 配置中心
server = new TestingServer(2181, true);
server.start();
}
}
19
Source : DubboSagaProviderStarter.java
with Apache License 2.0
from seata
with Apache License 2.0
from seata
/**
* The type Dubbo tcc provider starter.
*
* @author zhangsen
*/
public clreplaced DubboSagaProviderStarter {
private static TestingServer server;
/**
* The entry point of application.
*
* @param args the input arguments
* @throws Exception the exception
*/
public static void main(String[] args) throws Exception {
// mock zk server
mockZKServer();
ClreplacedPathXmlApplicationContext applicationContext = new ClreplacedPathXmlApplicationContext(new String[] { "spring/seata-dubbo-provider.xml" });
new ApplicationKeeper(applicationContext).keep();
}
private static void mockZKServer() throws Exception {
// Mock zk server,作为 dubbo 配置中心
server = new TestingServer(2181, true);
server.start();
}
}
19
Source : ZookeeperRegisterServiceImplTest.java
with Apache License 2.0
from seata
with Apache License 2.0
from seata
/**
* @author Geng Zhang
*/
public clreplaced ZookeeperRegisterServiceImplTest {
protected static TestingServer server = null;
@BeforeAll
public static void adBeforeClreplaced() throws Exception {
server = new TestingServer(2181, true);
server.start();
}
@AfterAll
public static void adAfterClreplaced() throws Exception {
if (server != null) {
server.stop();
}
}
ZookeeperRegisterServiceImpl service = (ZookeeperRegisterServiceImpl) new ZookeeperRegistryProvider().provide();
@Test
public void getInstance() {
ZookeeperRegisterServiceImpl service1 = ZookeeperRegisterServiceImpl.getInstance();
replacedertions.replacedertEquals(service1, service);
}
@Test
public void buildZkTest() {
ZkClient client = service.buildZkClient("127.0.0.1:2181", 5000, 5000);
replacedertions.replacedertTrue(client.exists("/zookeeper"));
}
@Test
public void testAll() throws Exception {
service.register(new InetSocketAddress(NetUtil.getLocalAddress(), 33333));
replacedertions.replacedertNull(service.lookup("xxx"));
List<InetSocketAddress> lookup2 = service.doLookup("default");
replacedertions.replacedertEquals(1, lookup2.size());
final List<String> data = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(1);
IZkChildListener listener = (s, list) -> {
data.clear();
data.addAll(list);
latch.countDown();
};
service.subscribe("default", listener);
final CountDownLatch latch2 = new CountDownLatch(1);
final List<String> data2 = new ArrayList<>();
IZkChildListener listener2 = (s, list) -> {
data2.clear();
data2.addAll(list);
latch2.countDown();
};
service.subscribe("default", listener2);
service.unregister(new InetSocketAddress(NetUtil.getLocalAddress(), 33333));
latch2.await(1000, TimeUnit.MILLISECONDS);
replacedertions.replacedertEquals(0, data2.size());
service.unsubscribe("default", listener);
service.unsubscribe("default", listener2);
}
}
19
Source : ClusterZKTest.java
with Apache License 2.0
from pravega
with Apache License 2.0
from pravega
public clreplaced ClusterZKTest {
private final static int TEST_TIMEOUT = 30000;
private final static String HOST_1 = "host1";
private final static String HOST_2 = "host2";
private final static int PORT = 1234;
private final static String CLUSTER_NAME = "cluster-1";
private final static String CLUSTER_NAME_2 = "cluster-2";
private final static int RETRY_SLEEP_MS = 100;
private final static int MAX_RETRY = 5;
private TestingServer zkTestServer;
private String zkUrl;
@Before
public void startZookeeper() throws Exception {
zkTestServer = new TestingServerStarter().start();
zkUrl = zkTestServer.getConnectString();
}
@After
public void stopZookeeper() throws IOException {
zkTestServer.close();
}
@Test(timeout = TEST_TIMEOUT)
public void registerNode() throws Exception {
LinkedBlockingQueue<String> nodeAddedQueue = new LinkedBlockingQueue<>();
LinkedBlockingQueue<String> nodeRemovedQueue = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Exception> exceptionsQueue = new LinkedBlockingQueue<>();
// ClusterListener for testing purposes
@Cleanup
CuratorFramework client2 = CuratorFrameworkFactory.builder().connectString(zkUrl).retryPolicy(new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY)).namespace(CLUSTER_NAME).build();
@Cleanup
Cluster clusterListener = new ClusterZKImpl(client2, ClusterType.HOST);
clusterListener.addListener((eventType, host) -> {
switch(eventType) {
case HOST_ADDED:
nodeAddedQueue.offer(host.getIpAddr());
break;
case HOST_REMOVED:
nodeRemovedQueue.offer(host.getIpAddr());
break;
case ERROR:
exceptionsQueue.offer(new RuntimeException("Encountered error"));
break;
default:
exceptionsQueue.offer(new RuntimeException("Unhandled case"));
break;
}
});
@Cleanup
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkUrl).retryPolicy(new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY)).namespace(CLUSTER_NAME).build();
// Create Add a node to the cluster.
@Cleanup
Cluster clusterZKInstance1 = new ClusterZKImpl(client, ClusterType.HOST);
clusterZKInstance1.registerHost(new Host(HOST_1, PORT, null));
replacedertEquals(HOST_1, nodeAddedQueue.poll(5, TimeUnit.SECONDS));
// Create a separate instance of Cluster and add node to same Cluster
@Cleanup
Cluster clusterZKInstance2 = new ClusterZKImpl(client, ClusterType.HOST);
clusterZKInstance1.registerHost(new Host(HOST_2, PORT, null));
replacedertEquals(HOST_2, nodeAddedQueue.poll(5, TimeUnit.SECONDS));
replacedertEquals(2, clusterListener.getClusterMembers().size());
Exception exception = exceptionsQueue.poll();
if (exception != null) {
throw exception;
}
}
@Test(timeout = TEST_TIMEOUT)
public void deregisterNode() throws Exception {
LinkedBlockingQueue<String> nodeAddedQueue = new LinkedBlockingQueue<>();
LinkedBlockingQueue<String> nodeRemovedQueue = new LinkedBlockingQueue<>();
LinkedBlockingQueue<Exception> exceptionsQueue = new LinkedBlockingQueue<>();
@Cleanup
CuratorFramework client2 = CuratorFrameworkFactory.builder().connectString(zkUrl).retryPolicy(new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY)).namespace(CLUSTER_NAME_2).build();
@Cleanup
Cluster clusterListener = new ClusterZKImpl(client2, ClusterType.HOST);
clusterListener.addListener((eventType, host) -> {
switch(eventType) {
case HOST_ADDED:
nodeAddedQueue.offer(host.getIpAddr());
break;
case HOST_REMOVED:
nodeRemovedQueue.offer(host.getIpAddr());
break;
case ERROR:
exceptionsQueue.offer(new RuntimeException("Encountered error"));
break;
default:
exceptionsQueue.offer(new RuntimeException("Unhandled case"));
break;
}
});
@Cleanup
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkUrl).retryPolicy(new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY)).namespace(CLUSTER_NAME_2).build();
// Create Add a node to the cluster.
@Cleanup
Cluster clusterZKInstance1 = new ClusterZKImpl(client, ClusterType.HOST);
clusterZKInstance1.registerHost(new Host(HOST_1, PORT, null));
replacedertEquals(HOST_1, nodeAddedQueue.poll(5, TimeUnit.SECONDS));
clusterZKInstance1.deregisterHost(new Host(HOST_1, PORT, null));
replacedertEquals(HOST_1, nodeRemovedQueue.poll(5, TimeUnit.SECONDS));
Exception exception = exceptionsQueue.poll();
if (exception != null) {
throw exception;
}
}
}
19
Source : ZKSegmentContainerMonitorTest.java
with Apache License 2.0
from pravega
with Apache License 2.0
from pravega
public clreplaced ZKSegmentContainerMonitorTest extends ThreadPooledTestSuite {
private final static int TEST_TIMEOUT = 60000;
private final static int RETRY_SLEEP_MS = 100;
private final static int MAX_RETRY = 5;
private final static int MAX_PARALLEL_CONTAINER_STARTS = 2;
private static final int PORT = TestUtils.getAvailableListenPort();
private final static Host PRAVEGA_SERVICE_ENDPOINT = new Host(getHostAddress(), PORT, null);
private final static String PATH = ZKPaths.makePath("cluster", "segmentContainerHostMapping");
private String zkUrl;
private TestingServer zkTestServer;
// Timeout per method tested.
@Rule
public Timeout globalTimeout = Timeout.millis(TEST_TIMEOUT);
@Override
protected int getThreadPoolSize() {
return 3;
}
@Before
public void startZookeeper() throws Exception {
zkTestServer = new TestingServerStarter().start();
zkUrl = zkTestServer.getConnectString();
}
@After
public void stopZookeeper() throws IOException {
zkTestServer.close();
}
/**
* Test if no mapping is present in zk.
*
* @throws Exception if an error occurred.
*/
@Test
public void testInitializeNoMapping() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
@Cleanup
ZKSegmentContainerMonitor segMonitor = createContainerMonitor(createMockContainerRegistry(), zkClient);
segMonitor.initialize();
replacedertEquals("Unexpected number of handles.", 0, segMonitor.getRegisteredContainers().size());
}
/**
* Tests if we cannot connect to ZooKeeper (the exception must be propagated to the caller).
*
* @throws Exception if an error occurred.
*/
@Test
public void testInitializeError() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
@Cleanup
ZKSegmentContainerMonitor segMonitor = createContainerMonitor(createMockContainerRegistry(), zkClient);
zkClient.close();
replacedertExtensions.replacedertThrows("initialize() did not throw an exception when ZooKeeper could not be accessed.", () -> segMonitor.initialize(), // Any exception will do, as long as it is propagated.
ex -> true);
}
@Test
public void testStartAndStopContainer() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = createMockContainerRegistry();
@Cleanup
ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
segMonitor.initialize(Duration.ofSeconds(1));
// Simulate a container that starts successfully.
CompletableFuture<ContainerHandle> startupFuture = new CompletableFuture<>();
ContainerHandle containerHandle = mock(ContainerHandle.clreplaced);
when(containerHandle.getContainerId()).thenReturn(2);
when(containerRegistry.startContainer(eq(2), any())).thenReturn(startupFuture);
// Now modify the ZK entry.
Map<Host, Set<Integer>> currentData = deserialize(zkClient, PATH);
currentData.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(2));
zkClient.setData().forPath(PATH, HostContainerMap.createHostContainerMap(currentData).toBytes());
// Container finished starting.
startupFuture.complete(containerHandle);
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
Thread.sleep(2000);
replacedertEquals(1, segMonitor.getRegisteredContainers().size());
replacedertTrue(segMonitor.getRegisteredContainers().contains(2));
// Now modify the ZK entry. Remove container 2 and add 1.
HashMap<Host, Set<Integer>> newMapping = new HashMap<>();
newMapping.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(1));
zkClient.setData().forPath(PATH, HostContainerMap.createHostContainerMap(newMapping).toBytes());
// Verify that stop is called and only the newly added container is in running state.
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
verify(containerRegistry, timeout(1000).atLeastOnce()).stopContainer(any(), any());
// Using wait here to ensure the private data structure is updated.
// TODO: Removing dependency on sleep here and other places in this clreplaced
// - https://github.com/pravega/pravega/issues/1079
Thread.sleep(2000);
replacedertEquals(1, segMonitor.getRegisteredContainers().size());
replacedertTrue(segMonitor.getRegisteredContainers().contains(1));
}
@Test
public void testShutdownNotYetStartedContainer() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = createMockContainerRegistry();
@Cleanup
ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
segMonitor.initialize(Duration.ofSeconds(1));
// Simulate a container that takes a long time to start. Should be greater than a few monitor loops.
ContainerHandle containerHandle = mock(ContainerHandle.clreplaced);
when(containerHandle.getContainerId()).thenReturn(2);
CompletableFuture<ContainerHandle> startupFuture = Futures.delayedFuture(() -> CompletableFuture.completedFuture(containerHandle), 3000, executorService());
when(containerRegistry.startContainer(eq(2), any())).thenReturn(startupFuture);
// Use ZK to send that information to the Container Manager.
Map<Host, Set<Integer>> currentData = deserialize(zkClient, PATH);
currentData.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(2));
zkClient.setData().forPath(PATH, HostContainerMap.createHostContainerMap(currentData).toBytes());
// Verify it's not yet started.
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
replacedertEquals(0, segMonitor.getRegisteredContainers().size());
// Now simulate shutting it down.
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
currentData.clear();
zkClient.setData().forPath(PATH, HostContainerMap.createHostContainerMap(currentData).toBytes());
verify(containerRegistry, timeout(10000).atLeastOnce()).stopContainer(any(), any());
Thread.sleep(2000);
replacedertEquals(0, segMonitor.getRegisteredContainers().size());
}
@Test
public void testRetryOnStartFailures() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = createMockContainerRegistry();
@Cleanup
ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
segMonitor.initialize(Duration.ofSeconds(1));
// Simulate a container that fails to start.
CompletableFuture<ContainerHandle> failedFuture = Futures.failedFuture(new RuntimeException());
when(containerRegistry.startContainer(eq(2), any())).thenReturn(failedFuture);
// Use ZK to send that information to the Container Manager.
Map<Host, Set<Integer>> currentData = deserialize(zkClient, PATH);
currentData.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(2));
zkClient.setData().forPath(PATH, HostContainerMap.createHostContainerMap(currentData).toBytes());
// Verify that it does not start.
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
replacedertEquals(0, segMonitor.getRegisteredContainers().size());
// Now simulate success for the same container.
ContainerHandle containerHandle = mock(ContainerHandle.clreplaced);
when(containerHandle.getContainerId()).thenReturn(2);
when(containerRegistry.startContainer(eq(2), any())).thenReturn(CompletableFuture.completedFuture(containerHandle));
// Verify that it retries and starts the same container again.
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
Thread.sleep(2000);
replacedertEquals(1, segMonitor.getRegisteredContainers().size());
}
@Test
public void testRetryOnExceptions() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = createMockContainerRegistry();
@Cleanup
ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
segMonitor.initialize(Duration.ofSeconds(1));
// Simulate a container that throws exception on start.
when(containerRegistry.startContainer(eq(2), any())).thenThrow(new RuntimeException());
// Use ZK to send that information to the Container Manager.
Map<Host, Set<Integer>> currentData = deserialize(zkClient, PATH);
currentData.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(2));
zkClient.setData().forPath(PATH, HostContainerMap.createHostContainerMap(currentData).toBytes());
// Verify that it does not start.
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
replacedertEquals(0, segMonitor.getRegisteredContainers().size());
// Now simulate success for the same container.
ContainerHandle containerHandle = mock(ContainerHandle.clreplaced);
when(containerHandle.getContainerId()).thenReturn(2);
when(containerRegistry.startContainer(eq(2), any())).thenReturn(CompletableFuture.completedFuture(containerHandle));
// Verify that it retries and starts the same container again.
verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
// Using wait here to ensure the private data structure is updated.
// TODO: Removing dependency on sleep here and other places in this clreplaced
// - https://github.com/pravega/pravega/issues/1079
Thread.sleep(2000);
replacedertEquals(1, segMonitor.getRegisteredContainers().size());
}
@Test
public void testClose() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = mock(SegmentContainerRegistry.clreplaced);
ContainerHandle containerHandle1 = mock(ContainerHandle.clreplaced);
when(containerHandle1.getContainerId()).thenReturn(1);
when(containerRegistry.startContainer(eq(1), any())).thenReturn(CompletableFuture.completedFuture(containerHandle1));
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
segMonitor.initialize(Duration.ofSeconds(1));
segMonitor.close();
replacedertEquals(0, segMonitor.getRegisteredContainers().size());
}
@SneakyThrows(UnknownHostException.clreplaced)
private static String getHostAddress() {
return Inet4Address.getLocalHost().getHostAddress();
}
private CuratorFramework startClient() {
val client = CuratorFrameworkFactory.newClient(zkUrl, new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY));
client.start();
return client;
}
private ZKSegmentContainerMonitor createContainerMonitor(SegmentContainerRegistry registry, CuratorFramework zkClient) {
return new ZKSegmentContainerMonitor(registry, zkClient, PRAVEGA_SERVICE_ENDPOINT, MAX_PARALLEL_CONTAINER_STARTS, executorService());
}
private void initializeHostContainerMapping(CuratorFramework zkClient) throws Exception {
HashMap<Host, Set<Integer>> mapping = new HashMap<>();
zkClient.create().creatingParentsIfNeeded().forPath(PATH, HostContainerMap.createHostContainerMap(mapping).toBytes());
}
private SegmentContainerRegistry createMockContainerRegistry() {
SegmentContainerRegistry containerRegistry = mock(SegmentContainerRegistry.clreplaced);
ContainerHandle containerHandle1 = mock(ContainerHandle.clreplaced);
when(containerHandle1.getContainerId()).thenReturn(1);
when(containerRegistry.startContainer(anyInt(), any())).thenReturn(CompletableFuture.completedFuture(containerHandle1));
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
return containerRegistry;
}
@SuppressWarnings("unchecked")
private Map<Host, Set<Integer>> deserialize(CuratorFramework zkClient, String path) throws Exception {
return HostContainerMap.fromBytes(zkClient.getData().forPath(path)).getHostContainerMap();
}
}
19
Source : ZKSegmentContainerManagerTest.java
with Apache License 2.0
from pravega
with Apache License 2.0
from pravega
public clreplaced ZKSegmentContainerManagerTest extends ThreadPooledTestSuite {
private final static int TEST_TIMEOUT = 60000;
private final static int RETRY_SLEEP_MS = 100;
private final static int MAX_RETRY = 5;
private final static int MAX_PARALLEL_CONTAINER_STARTS = 2;
private static final int PORT = TestUtils.getAvailableListenPort();
private final static Host PRAVEGA_SERVICE_ENDPOINT = new Host(getHostAddress(), PORT, null);
private final static String PATH = ZKPaths.makePath("cluster", "segmentContainerHostMapping");
private String zkUrl;
private TestingServer zkTestServer;
// Timeout per method tested.
@Rule
public Timeout globalTimeout = Timeout.millis(TEST_TIMEOUT);
@Override
protected int getThreadPoolSize() {
return 3;
}
@Before
public void startZookeeper() throws Exception {
zkTestServer = new TestingServerStarter().start();
zkUrl = zkTestServer.getConnectString();
}
@After
public void stopZookeeper() throws IOException {
zkTestServer.close();
}
/**
* Test if initialization completes.
*
* @throws Exception if an error occurred.
*/
@Test
public void testInitializeSucceeds() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
@Cleanup
ZKSegmentContainerManager segManager = createContainerManager(createMockContainerRegistry(), zkClient);
segManager.initialize();
}
/**
* Tests if we cannot connect to ZooKeeper (the exception must be propagated to the caller).
*
* @throws Exception if an error occurred.
*/
@Test
public void testInitializeError() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
@Cleanup
ZKSegmentContainerManager segManager = createContainerManager(createMockContainerRegistry(), zkClient);
zkClient.close();
replacedertExtensions.replacedertThrows("initialize() did not throw an exception when ZooKeeper could not be accessed.", segManager::initialize, // Any exception will do, as long as it is propagated.
ex -> true);
}
@Test
public void testClose() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
SegmentContainerRegistry containerRegistry = mock(SegmentContainerRegistry.clreplaced);
ContainerHandle containerHandle1 = mock(ContainerHandle.clreplaced);
when(containerHandle1.getContainerId()).thenReturn(1);
when(containerRegistry.startContainer(eq(1), any())).thenReturn(CompletableFuture.completedFuture(containerHandle1));
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
ZKSegmentContainerManager segManager = createContainerManager(containerRegistry, zkClient);
segManager.initialize();
segManager.close();
}
@Test
public void testContainerStart() throws Exception {
@Cleanup
CuratorFramework zkClient = startClient();
initializeHostContainerMapping(zkClient);
SegmentContainerRegistry containerRegistry = mock(SegmentContainerRegistry.clreplaced);
ContainerHandle containerHandle1 = mock(ContainerHandle.clreplaced);
when(containerHandle1.getContainerId()).thenReturn(1);
when(containerRegistry.startContainer(eq(1), any())).thenReturn(CompletableFuture.completedFuture(containerHandle1));
@Cleanup
ZKSegmentContainerManager segManager = createContainerManager(containerRegistry, zkClient);
segManager.initialize();
verify(containerRegistry, timeout(30000).atLeastOnce()).startContainer(eq(1), any());
}
@SneakyThrows(UnknownHostException.clreplaced)
private static String getHostAddress() {
return Inet4Address.getLocalHost().getHostAddress();
}
private CuratorFramework startClient() {
val client = CuratorFrameworkFactory.newClient(zkUrl, new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY));
client.start();
return client;
}
private ZKSegmentContainerManager createContainerManager(SegmentContainerRegistry registry, CuratorFramework zkClient) {
return new ZKSegmentContainerManager(registry, zkClient, PRAVEGA_SERVICE_ENDPOINT, MAX_PARALLEL_CONTAINER_STARTS, executorService());
}
private void initializeHostContainerMapping(CuratorFramework zkClient) throws Exception {
HashMap<Host, Set<Integer>> mapping = new HashMap<>();
mapping.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(1));
zkClient.create().creatingParentsIfNeeded().forPath(PATH, HostContainerMap.createHostContainerMap(mapping).toBytes());
}
private SegmentContainerRegistry createMockContainerRegistry() {
SegmentContainerRegistry containerRegistry = mock(SegmentContainerRegistry.clreplaced);
ContainerHandle containerHandle1 = mock(ContainerHandle.clreplaced);
when(containerHandle1.getContainerId()).thenReturn(1);
when(containerRegistry.startContainer(anyInt(), any())).thenReturn(CompletableFuture.completedFuture(containerHandle1));
when(containerRegistry.stopContainer(any(), any())).thenReturn(CompletableFuture.completedFuture(null));
return containerRegistry;
}
}
19
Source : ServiceStarterTest.java
with Apache License 2.0
from pravega
with Apache License 2.0
from pravega
public clreplaced ServiceStarterTest {
private String zkUrl;
private TestingServer zkTestServer;
@Before
public void startZookeeper() throws Exception {
zkTestServer = new TestingServerStarter().start();
zkUrl = zkTestServer.getConnectString();
}
@After
public void stopZookeeper() throws IOException {
zkTestServer.close();
}
/**
* Check that the client created by ServiceStarter can correctly connect to a Zookeeper server using the custom
* Zookeeper client factory.
*
* @throws Exception
*/
@Test
public void testCuratorClientCreation() throws Exception {
ServiceBuilderConfig.Builder configBuilder = ServiceBuilderConfig.builder().include(ServiceConfig.builder().with(ServiceConfig.CONTAINER_COUNT, 1).with(ServiceConfig.ZK_URL, zkUrl));
@Cleanup("shutdown")
ServiceStarter serviceStarter = new ServiceStarter(configBuilder.build());
@Cleanup
CuratorFramework zkClient = serviceStarter.createZKClient();
zkClient.blockUntilConnected();
replacedert.replacedertTrue(zkClient.getZookeeperClient().isConnected());
}
}
19
Source : StoreClientFactoryTest.java
with Apache License 2.0
from pravega
with Apache License 2.0
from pravega
public clreplaced StoreClientFactoryTest extends ThreadPooledTestSuite {
TestingServer zkServer;
@Override
protected int getThreadPoolSize() {
return 1;
}
@Before
public void setUp() throws Exception {
zkServer = new TestingServerStarter().start();
}
@After
public void tearDown() throws IOException {
zkServer.stop();
}
/**
* This test verifies that ZKClientFactory correctly handles the situation in which the Controller attempts to
* create a new Zookeeper client with different parameters compared to the existing ones. We should not enable to
* create a new Zookeeper client and, in fact, we close the existing one to force the restart of the Controller.
* During the Controller restart, a new Zookeeper client will be created with the new parameters.
*/
@Test
public void testZkSessionExpiryWithChangedParameters() throws Exception {
final String testZNode = "/test";
CompletableFuture<Void> sessionExpiry = new CompletableFuture<>();
AtomicInteger expirationRetryCounter = new AtomicInteger();
Supplier<Boolean> canRetrySupplier = () -> {
if (sessionExpiry.isDone()) {
expirationRetryCounter.incrementAndGet();
}
return !sessionExpiry.isDone();
};
Consumer<Void> expirationHandler = x -> sessionExpiry.complete(null);
StoreClientFactory.ZKClientFactory storeClientFactory = new StoreClientFactory.ZKClientFactory();
CuratorFramework client = StoreClientFactory.createZKClient(ZKClientConfigImpl.builder().connectionString(zkServer.getConnectString()).namespace("test").maxRetries(10).initialSleepInterval(10).secureConnectionToZooKeeper(false).sessionTimeoutMs(15000).build(), canRetrySupplier, expirationHandler, storeClientFactory);
// Check that the client works correctly. In this case, it throws a NoNodeException when accessing a non-existent path.
replacedertExtensions.replacedertThrows(KeeperException.NoNodeException.clreplaced, () -> client.getData().forPath(testZNode));
// Simulate an update of the connection parameters.
storeClientFactory.setConnectString("changedConnectString");
// Induce a session expiration, so we invoke newZooKeeper() and notice about the updated parameter.
client.getZookeeperClient().getZooKeeper().getTestable().injectSessionExpiration();
sessionExpiry.join();
// Check that, once closed by the ZKClientFactory, the client throws an expected exception (SessionExpiredException).
replacedertExtensions.replacedertThrows(KeeperException.SessionExpiredException.clreplaced, () -> client.getData().forPath(testZNode));
}
}
19
Source : TestZKDelegationTokenSecretManager.java
with Apache License 2.0
from NJUJYB
with Apache License 2.0
from NJUJYB
public clreplaced TestZKDelegationTokenSecretManager {
private static final long DAY_IN_SECS = 86400;
private TestingServer zkServer;
@Before
public void setup() throws Exception {
zkServer = new TestingServer();
zkServer.start();
}
@After
public void tearDown() throws Exception {
if (zkServer != null) {
zkServer.close();
}
}
protected Configuration getSecretConf(String connectString) {
Configuration conf = new Configuration();
conf.setBoolean(DelegationTokenManager.ENABLE_ZK_KEY, true);
conf.set(ZKDelegationTokenSecretManager.ZK_DTSM_ZK_CONNECTION_STRING, connectString);
conf.set(ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH, "testPath");
conf.set(ZKDelegationTokenSecretManager.ZK_DTSM_ZK_AUTH_TYPE, "none");
conf.setLong(DelegationTokenManager.UPDATE_INTERVAL, DAY_IN_SECS);
conf.setLong(DelegationTokenManager.MAX_LIFETIME, DAY_IN_SECS);
conf.setLong(DelegationTokenManager.RENEW_INTERVAL, DAY_IN_SECS);
conf.setLong(DelegationTokenManager.REMOVAL_SCAN_INTERVAL, DAY_IN_SECS);
return conf;
}
@SuppressWarnings("unchecked")
@Test
public void testMultiNodeOperations() throws Exception {
DelegationTokenManager tm1, tm2 = null;
String connectString = zkServer.getConnectString();
Configuration conf = getSecretConf(connectString);
tm1 = new DelegationTokenManager(conf, new Text("bla"));
tm1.init();
tm2 = new DelegationTokenManager(conf, new Text("bla"));
tm2.init();
Token<DelegationTokenIdentifier> token = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo");
replacedert.replacedertNotNull(token);
tm2.verifyToken(token);
tm2.renewToken(token, "foo");
tm1.verifyToken(token);
tm1.cancelToken(token, "foo");
try {
tm2.verifyToken(token);
fail("Expected InvalidToken");
} catch (SecretManager.InvalidToken it) {
// Ignore
}
token = (Token<DelegationTokenIdentifier>) tm2.createToken(UserGroupInformation.getCurrentUser(), "bar");
replacedert.replacedertNotNull(token);
tm1.verifyToken(token);
tm1.renewToken(token, "bar");
tm2.verifyToken(token);
tm2.cancelToken(token, "bar");
try {
tm1.verifyToken(token);
fail("Expected InvalidToken");
} catch (SecretManager.InvalidToken it) {
// Ignore
}
}
@SuppressWarnings("unchecked")
@Test
public void testRenewTokenSingleManager() throws Exception {
DelegationTokenManager tm1 = null;
String connectString = zkServer.getConnectString();
Configuration conf = getSecretConf(connectString);
tm1 = new DelegationTokenManager(conf, new Text("foo"));
tm1.init();
Token<DelegationTokenIdentifier> token = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo");
replacedert.replacedertNotNull(token);
tm1.renewToken(token, "foo");
tm1.verifyToken(token);
}
@SuppressWarnings("unchecked")
@Test
public void testCancelTokenSingleManager() throws Exception {
DelegationTokenManager tm1 = null;
String connectString = zkServer.getConnectString();
Configuration conf = getSecretConf(connectString);
tm1 = new DelegationTokenManager(conf, new Text("foo"));
tm1.init();
Token<DelegationTokenIdentifier> token = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo");
replacedert.replacedertNotNull(token);
tm1.cancelToken(token, "foo");
try {
tm1.verifyToken(token);
fail("Expected InvalidToken");
} catch (SecretManager.InvalidToken it) {
it.printStackTrace();
}
}
}
19
Source : TestZKSignerSecretProvider.java
with Apache License 2.0
from NJUJYB
with Apache License 2.0
from NJUJYB
public clreplaced TestZKSignerSecretProvider {
private TestingServer zkServer;
@Before
public void setup() throws Exception {
zkServer = new TestingServer();
}
@After
public void teardown() throws Exception {
if (zkServer != null) {
zkServer.stop();
zkServer.close();
}
}
@Test
public // simplest case
void testOne() throws Exception {
// rollover every 15 sec
long rolloverFrequency = 15 * 1000;
// use the same seed so we can predict the RNG
long seed = System.currentTimeMillis();
Random rand = new Random(seed);
byte[] secret2 = Long.toString(rand.nextLong()).getBytes();
byte[] secret1 = Long.toString(rand.nextLong()).getBytes();
byte[] secret3 = Long.toString(rand.nextLong()).getBytes();
ZKSignerSecretProvider secretProvider = new ZKSignerSecretProvider(seed);
Properties config = new Properties();
config.setProperty(ZKSignerSecretProvider.ZOOKEEPER_CONNECTION_STRING, zkServer.getConnectString());
config.setProperty(ZKSignerSecretProvider.ZOOKEEPER_PATH, "/secret");
try {
secretProvider.init(config, getDummyServletContext(), rolloverFrequency);
byte[] currentSecret = secretProvider.getCurrentSecret();
byte[][] allSecrets = secretProvider.getAllSecrets();
replacedert.replacedertArrayEquals(secret1, currentSecret);
replacedert.replacedertEquals(2, allSecrets.length);
replacedert.replacedertArrayEquals(secret1, allSecrets[0]);
replacedert.replacedertNull(allSecrets[1]);
Thread.sleep((rolloverFrequency + 2000));
currentSecret = secretProvider.getCurrentSecret();
allSecrets = secretProvider.getAllSecrets();
replacedert.replacedertArrayEquals(secret2, currentSecret);
replacedert.replacedertEquals(2, allSecrets.length);
replacedert.replacedertArrayEquals(secret2, allSecrets[0]);
replacedert.replacedertArrayEquals(secret1, allSecrets[1]);
Thread.sleep((rolloverFrequency + 2000));
currentSecret = secretProvider.getCurrentSecret();
allSecrets = secretProvider.getAllSecrets();
replacedert.replacedertArrayEquals(secret3, currentSecret);
replacedert.replacedertEquals(2, allSecrets.length);
replacedert.replacedertArrayEquals(secret3, allSecrets[0]);
replacedert.replacedertArrayEquals(secret2, allSecrets[1]);
Thread.sleep((rolloverFrequency + 2000));
} finally {
secretProvider.destroy();
}
}
@Test
public void testMultipleInit() throws Exception {
// rollover every 15 sec
long rolloverFrequency = 15 * 1000;
// use the same seed so we can predict the RNG
long seedA = System.currentTimeMillis();
Random rand = new Random(seedA);
byte[] secretA2 = Long.toString(rand.nextLong()).getBytes();
byte[] secretA1 = Long.toString(rand.nextLong()).getBytes();
// use the same seed so we can predict the RNG
long seedB = System.currentTimeMillis() + rand.nextLong();
rand = new Random(seedB);
byte[] secretB2 = Long.toString(rand.nextLong()).getBytes();
byte[] secretB1 = Long.toString(rand.nextLong()).getBytes();
// use the same seed so we can predict the RNG
long seedC = System.currentTimeMillis() + rand.nextLong();
rand = new Random(seedC);
byte[] secretC2 = Long.toString(rand.nextLong()).getBytes();
byte[] secretC1 = Long.toString(rand.nextLong()).getBytes();
ZKSignerSecretProvider secretProviderA = new ZKSignerSecretProvider(seedA);
ZKSignerSecretProvider secretProviderB = new ZKSignerSecretProvider(seedB);
ZKSignerSecretProvider secretProviderC = new ZKSignerSecretProvider(seedC);
Properties config = new Properties();
config.setProperty(ZKSignerSecretProvider.ZOOKEEPER_CONNECTION_STRING, zkServer.getConnectString());
config.setProperty(ZKSignerSecretProvider.ZOOKEEPER_PATH, "/secret");
try {
secretProviderA.init(config, getDummyServletContext(), rolloverFrequency);
secretProviderB.init(config, getDummyServletContext(), rolloverFrequency);
secretProviderC.init(config, getDummyServletContext(), rolloverFrequency);
byte[] currentSecretA = secretProviderA.getCurrentSecret();
byte[][] allSecretsA = secretProviderA.getAllSecrets();
byte[] currentSecretB = secretProviderB.getCurrentSecret();
byte[][] allSecretsB = secretProviderB.getAllSecrets();
byte[] currentSecretC = secretProviderC.getCurrentSecret();
byte[][] allSecretsC = secretProviderC.getAllSecrets();
replacedert.replacedertArrayEquals(currentSecretA, currentSecretB);
replacedert.replacedertArrayEquals(currentSecretB, currentSecretC);
replacedert.replacedertEquals(2, allSecretsA.length);
replacedert.replacedertEquals(2, allSecretsB.length);
replacedert.replacedertEquals(2, allSecretsC.length);
replacedert.replacedertArrayEquals(allSecretsA[0], allSecretsB[0]);
replacedert.replacedertArrayEquals(allSecretsB[0], allSecretsC[0]);
replacedert.replacedertNull(allSecretsA[1]);
replacedert.replacedertNull(allSecretsB[1]);
replacedert.replacedertNull(allSecretsC[1]);
char secretChosen = 'z';
if (Arrays.equals(secretA1, currentSecretA)) {
replacedert.replacedertArrayEquals(secretA1, allSecretsA[0]);
secretChosen = 'A';
} else if (Arrays.equals(secretB1, currentSecretB)) {
replacedert.replacedertArrayEquals(secretB1, allSecretsA[0]);
secretChosen = 'B';
} else if (Arrays.equals(secretC1, currentSecretC)) {
replacedert.replacedertArrayEquals(secretC1, allSecretsA[0]);
secretChosen = 'C';
} else {
replacedert.fail("It appears that they all agreed on the same secret, but " + "not one of the secrets they were supposed to");
}
Thread.sleep((rolloverFrequency + 2000));
currentSecretA = secretProviderA.getCurrentSecret();
allSecretsA = secretProviderA.getAllSecrets();
currentSecretB = secretProviderB.getCurrentSecret();
allSecretsB = secretProviderB.getAllSecrets();
currentSecretC = secretProviderC.getCurrentSecret();
allSecretsC = secretProviderC.getAllSecrets();
replacedert.replacedertArrayEquals(currentSecretA, currentSecretB);
replacedert.replacedertArrayEquals(currentSecretB, currentSecretC);
replacedert.replacedertEquals(2, allSecretsA.length);
replacedert.replacedertEquals(2, allSecretsB.length);
replacedert.replacedertEquals(2, allSecretsC.length);
replacedert.replacedertArrayEquals(allSecretsA[0], allSecretsB[0]);
replacedert.replacedertArrayEquals(allSecretsB[0], allSecretsC[0]);
replacedert.replacedertArrayEquals(allSecretsA[1], allSecretsB[1]);
replacedert.replacedertArrayEquals(allSecretsB[1], allSecretsC[1]);
// The second secret used is prechosen by whoever won the init; so it
// should match with whichever we saw before
if (secretChosen == 'A') {
replacedert.replacedertArrayEquals(secretA2, currentSecretA);
} else if (secretChosen == 'B') {
replacedert.replacedertArrayEquals(secretB2, currentSecretA);
} else if (secretChosen == 'C') {
replacedert.replacedertArrayEquals(secretC2, currentSecretA);
}
} finally {
secretProviderC.destroy();
secretProviderB.destroy();
secretProviderA.destroy();
}
}
@Test
public void testMultipleUnsychnronized() throws Exception {
// rollover every 15 sec
long rolloverFrequency = 15 * 1000;
// use the same seed so we can predict the RNG
long seedA = System.currentTimeMillis();
Random rand = new Random(seedA);
byte[] secretA2 = Long.toString(rand.nextLong()).getBytes();
byte[] secretA1 = Long.toString(rand.nextLong()).getBytes();
byte[] secretA3 = Long.toString(rand.nextLong()).getBytes();
// use the same seed so we can predict the RNG
long seedB = System.currentTimeMillis() + rand.nextLong();
rand = new Random(seedB);
byte[] secretB2 = Long.toString(rand.nextLong()).getBytes();
byte[] secretB1 = Long.toString(rand.nextLong()).getBytes();
byte[] secretB3 = Long.toString(rand.nextLong()).getBytes();
ZKSignerSecretProvider secretProviderA = new ZKSignerSecretProvider(seedA);
ZKSignerSecretProvider secretProviderB = new ZKSignerSecretProvider(seedB);
Properties config = new Properties();
config.setProperty(ZKSignerSecretProvider.ZOOKEEPER_CONNECTION_STRING, zkServer.getConnectString());
config.setProperty(ZKSignerSecretProvider.ZOOKEEPER_PATH, "/secret");
try {
secretProviderA.init(config, getDummyServletContext(), rolloverFrequency);
byte[] currentSecretA = secretProviderA.getCurrentSecret();
byte[][] allSecretsA = secretProviderA.getAllSecrets();
replacedert.replacedertArrayEquals(secretA1, currentSecretA);
replacedert.replacedertEquals(2, allSecretsA.length);
replacedert.replacedertArrayEquals(secretA1, allSecretsA[0]);
replacedert.replacedertNull(allSecretsA[1]);
Thread.sleep((rolloverFrequency + 2000));
currentSecretA = secretProviderA.getCurrentSecret();
allSecretsA = secretProviderA.getAllSecrets();
replacedert.replacedertArrayEquals(secretA2, currentSecretA);
replacedert.replacedertEquals(2, allSecretsA.length);
replacedert.replacedertArrayEquals(secretA2, allSecretsA[0]);
replacedert.replacedertArrayEquals(secretA1, allSecretsA[1]);
Thread.sleep((rolloverFrequency / 5));
secretProviderB.init(config, getDummyServletContext(), rolloverFrequency);
byte[] currentSecretB = secretProviderB.getCurrentSecret();
byte[][] allSecretsB = secretProviderB.getAllSecrets();
replacedert.replacedertArrayEquals(secretA2, currentSecretB);
replacedert.replacedertEquals(2, allSecretsA.length);
replacedert.replacedertArrayEquals(secretA2, allSecretsB[0]);
replacedert.replacedertArrayEquals(secretA1, allSecretsB[1]);
Thread.sleep((rolloverFrequency));
currentSecretA = secretProviderA.getCurrentSecret();
allSecretsA = secretProviderA.getAllSecrets();
currentSecretB = secretProviderB.getCurrentSecret();
allSecretsB = secretProviderB.getAllSecrets();
replacedert.replacedertArrayEquals(currentSecretA, currentSecretB);
replacedert.replacedertEquals(2, allSecretsA.length);
replacedert.replacedertEquals(2, allSecretsB.length);
replacedert.replacedertArrayEquals(allSecretsA[0], allSecretsB[0]);
replacedert.replacedertArrayEquals(allSecretsA[1], allSecretsB[1]);
if (Arrays.equals(secretA3, currentSecretA)) {
replacedert.replacedertArrayEquals(secretA3, allSecretsA[0]);
} else if (Arrays.equals(secretB3, currentSecretB)) {
replacedert.replacedertArrayEquals(secretB3, allSecretsA[0]);
} else {
replacedert.fail("It appears that they all agreed on the same secret, but " + "not one of the secrets they were supposed to");
}
} finally {
secretProviderB.destroy();
secretProviderA.destroy();
}
}
private ServletContext getDummyServletContext() {
ServletContext servletContext = Mockito.mock(ServletContext.clreplaced);
Mockito.when(servletContext.getAttribute(ZKSignerSecretProvider.ZOOKEEPER_SIGNER_SECRET_PROVIDER_CURATOR_CLIENT_ATTRIBUTE)).thenReturn(null);
return servletContext;
}
}
19
Source : TestChildReaper.java
with Apache License 2.0
from naver
with Apache License 2.0
from naver
/**
* This is a copy of Curator 2.7.1's TestChildReaper clreplaced, with minor
* modifications to make it work with JUnit (some setup code taken from
* Curator's BaseClreplacedForTests). This is to ensure that the ChildReaper
* clreplaced we modified is still correct.
*/
public clreplaced TestChildReaper {
protected TestingServer server;
@Before
public void setup() throws Exception {
while (this.server == null) {
try {
this.server = new TestingServer();
} catch (BindException var2) {
System.err.println("Getting bind exception - retrying to allocate server");
this.server = null;
}
}
}
@After
public void teardown() throws Exception {
this.server.close();
this.server = null;
}
@Test
public void testSomeNodes() throws Exception {
Timing timing = new Timing();
ChildReaper reaper = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
Random r = new Random();
int nonEmptyNodes = 0;
for (int i = 0; i < 10; ++i) {
client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
if (r.nextBoolean()) {
client.create().forPath("/test/" + Integer.toString(i) + "/foo");
++nonEmptyNodes;
}
}
reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
reaper.start();
timing.forWaiting().sleepABit();
Stat stat = client.checkExists().forPath("/test");
replacedert.replacedertEquals(stat.getNumChildren(), nonEmptyNodes);
} finally {
CloseableUtils.closeQuietly(reaper);
CloseableUtils.closeQuietly(client);
}
}
@Test
public void testSimple() throws Exception {
Timing timing = new Timing();
ChildReaper reaper = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
for (int i = 0; i < 10; ++i) {
client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
}
reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
reaper.start();
timing.forWaiting().sleepABit();
Stat stat = client.checkExists().forPath("/test");
replacedert.replacedertEquals(stat.getNumChildren(), 0);
} finally {
CloseableUtils.closeQuietly(reaper);
CloseableUtils.closeQuietly(client);
}
}
@Test
public void testMultiPath() throws Exception {
Timing timing = new Timing();
ChildReaper reaper = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
for (int i = 0; i < 10; ++i) {
client.create().creatingParentsIfNeeded().forPath("/test1/" + Integer.toString(i));
client.create().creatingParentsIfNeeded().forPath("/test2/" + Integer.toString(i));
client.create().creatingParentsIfNeeded().forPath("/test3/" + Integer.toString(i));
}
reaper = new ChildReaper(client, "/test2", Reaper.Mode.REAP_UNTIL_DELETE, 1);
reaper.start();
reaper.addPath("/test1");
timing.forWaiting().sleepABit();
Stat stat = client.checkExists().forPath("/test1");
replacedert.replacedertEquals(stat.getNumChildren(), 0);
stat = client.checkExists().forPath("/test2");
replacedert.replacedertEquals(stat.getNumChildren(), 0);
stat = client.checkExists().forPath("/test3");
replacedert.replacedertEquals(stat.getNumChildren(), 10);
} finally {
CloseableUtils.closeQuietly(reaper);
CloseableUtils.closeQuietly(client);
}
}
@Test
public void testNamespace() throws Exception {
Timing timing = new Timing();
ChildReaper reaper = null;
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1)).namespace("foo").build();
try {
client.start();
for (int i = 0; i < 10; ++i) {
client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
}
reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
reaper.start();
timing.forWaiting().sleepABit();
Stat stat = client.checkExists().forPath("/test");
replacedert.replacedertEquals(stat.getNumChildren(), 0);
stat = client.usingNamespace(null).checkExists().forPath("/foo/test");
replacedert.replacedertNotNull(stat);
replacedert.replacedertEquals(stat.getNumChildren(), 0);
} finally {
CloseableUtils.closeQuietly(reaper);
CloseableUtils.closeQuietly(client);
}
}
}
19
Source : TestZKDelegationTokenSecretManager.java
with Apache License 2.0
from naver
with Apache License 2.0
from naver
public clreplaced TestZKDelegationTokenSecretManager {
private static final int TEST_RETRIES = 2;
private static final int RETRY_COUNT = 5;
private static final int RETRY_WAIT = 1000;
private static final long DAY_IN_SECS = 86400;
private TestingServer zkServer;
@Before
public void setup() throws Exception {
zkServer = new TestingServer();
zkServer.start();
}
@After
public void tearDown() throws Exception {
if (zkServer != null) {
zkServer.close();
}
}
protected Configuration getSecretConf(String connectString) {
Configuration conf = new Configuration();
conf.setBoolean(DelegationTokenManager.ENABLE_ZK_KEY, true);
conf.set(ZKDelegationTokenSecretManager.ZK_DTSM_ZK_CONNECTION_STRING, connectString);
conf.set(ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH, "testPath");
conf.set(ZKDelegationTokenSecretManager.ZK_DTSM_ZK_AUTH_TYPE, "none");
conf.setLong(ZKDelegationTokenSecretManager.ZK_DTSM_ZK_SHUTDOWN_TIMEOUT, 100);
conf.setLong(DelegationTokenManager.UPDATE_INTERVAL, DAY_IN_SECS);
conf.setLong(DelegationTokenManager.MAX_LIFETIME, DAY_IN_SECS);
conf.setLong(DelegationTokenManager.RENEW_INTERVAL, DAY_IN_SECS);
conf.setLong(DelegationTokenManager.REMOVAL_SCAN_INTERVAL, DAY_IN_SECS);
return conf;
}
@SuppressWarnings("unchecked")
@Test
public void testMultiNodeOperations() throws Exception {
for (int i = 0; i < TEST_RETRIES; i++) {
DelegationTokenManager tm1, tm2 = null;
String connectString = zkServer.getConnectString();
Configuration conf = getSecretConf(connectString);
tm1 = new DelegationTokenManager(conf, new Text("bla"));
tm1.init();
tm2 = new DelegationTokenManager(conf, new Text("bla"));
tm2.init();
Token<DelegationTokenIdentifier> token = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo");
replacedert.replacedertNotNull(token);
tm2.verifyToken(token);
tm2.renewToken(token, "foo");
tm1.verifyToken(token);
tm1.cancelToken(token, "foo");
try {
verifyTokenFail(tm2, token);
fail("Expected InvalidToken");
} catch (SecretManager.InvalidToken it) {
// Ignore
}
token = (Token<DelegationTokenIdentifier>) tm2.createToken(UserGroupInformation.getCurrentUser(), "bar");
replacedert.replacedertNotNull(token);
tm1.verifyToken(token);
tm1.renewToken(token, "bar");
tm2.verifyToken(token);
tm2.cancelToken(token, "bar");
try {
verifyTokenFail(tm1, token);
fail("Expected InvalidToken");
} catch (SecretManager.InvalidToken it) {
// Ignore
}
verifyDestroy(tm1, conf);
verifyDestroy(tm2, conf);
}
}
@SuppressWarnings("unchecked")
@Test
public void testNodeUpAferAWhile() throws Exception {
for (int i = 0; i < TEST_RETRIES; i++) {
String connectString = zkServer.getConnectString();
Configuration conf = getSecretConf(connectString);
DelegationTokenManager tm1 = new DelegationTokenManager(conf, new Text("bla"));
tm1.init();
Token<DelegationTokenIdentifier> token1 = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo");
replacedert.replacedertNotNull(token1);
Token<DelegationTokenIdentifier> token2 = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "bar");
replacedert.replacedertNotNull(token2);
Token<DelegationTokenIdentifier> token3 = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "boo");
replacedert.replacedertNotNull(token3);
tm1.verifyToken(token1);
tm1.verifyToken(token2);
tm1.verifyToken(token3);
// Cancel one token
tm1.cancelToken(token1, "foo");
// Start second node after some time..
Thread.sleep(1000);
DelegationTokenManager tm2 = new DelegationTokenManager(conf, new Text("bla"));
tm2.init();
tm2.verifyToken(token2);
tm2.verifyToken(token3);
try {
verifyTokenFail(tm2, token1);
fail("Expected InvalidToken");
} catch (SecretManager.InvalidToken it) {
// Ignore
}
// Create a new token thru the new ZKDTSM
Token<DelegationTokenIdentifier> token4 = (Token<DelegationTokenIdentifier>) tm2.createToken(UserGroupInformation.getCurrentUser(), "xyz");
replacedert.replacedertNotNull(token4);
tm2.verifyToken(token4);
tm1.verifyToken(token4);
// Bring down tm2
verifyDestroy(tm2, conf);
// Start third node after some time..
Thread.sleep(1000);
DelegationTokenManager tm3 = new DelegationTokenManager(conf, new Text("bla"));
tm3.init();
tm3.verifyToken(token2);
tm3.verifyToken(token3);
tm3.verifyToken(token4);
try {
verifyTokenFail(tm3, token1);
fail("Expected InvalidToken");
} catch (SecretManager.InvalidToken it) {
// Ignore
}
verifyDestroy(tm3, conf);
verifyDestroy(tm1, conf);
}
}
@SuppressWarnings("unchecked")
@Test
public void testRenewTokenSingleManager() throws Exception {
for (int i = 0; i < TEST_RETRIES; i++) {
DelegationTokenManager tm1 = null;
String connectString = zkServer.getConnectString();
Configuration conf = getSecretConf(connectString);
tm1 = new DelegationTokenManager(conf, new Text("foo"));
tm1.init();
Token<DelegationTokenIdentifier> token = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo");
replacedert.replacedertNotNull(token);
tm1.renewToken(token, "foo");
tm1.verifyToken(token);
verifyDestroy(tm1, conf);
}
}
@SuppressWarnings("unchecked")
@Test
public void testCancelTokenSingleManager() throws Exception {
for (int i = 0; i < TEST_RETRIES; i++) {
DelegationTokenManager tm1 = null;
String connectString = zkServer.getConnectString();
Configuration conf = getSecretConf(connectString);
tm1 = new DelegationTokenManager(conf, new Text("foo"));
tm1.init();
Token<DelegationTokenIdentifier> token = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo");
replacedert.replacedertNotNull(token);
tm1.cancelToken(token, "foo");
try {
verifyTokenFail(tm1, token);
fail("Expected InvalidToken");
} catch (SecretManager.InvalidToken it) {
it.printStackTrace();
}
verifyDestroy(tm1, conf);
}
}
@SuppressWarnings("rawtypes")
protected void verifyDestroy(DelegationTokenManager tm, Configuration conf) throws Exception {
AbstractDelegationTokenSecretManager sm = tm.getDelegationTokenSecretManager();
ZKDelegationTokenSecretManager zksm = (ZKDelegationTokenSecretManager) sm;
ExecutorService es = zksm.getListenerThreadPool();
tm.destroy();
replacedert.replacedertTrue(es.isShutdown());
// wait for the pool to terminate
long timeout = conf.getLong(ZKDelegationTokenSecretManager.ZK_DTSM_ZK_SHUTDOWN_TIMEOUT, ZKDelegationTokenSecretManager.ZK_DTSM_ZK_SHUTDOWN_TIMEOUT_DEFAULT);
Thread.sleep(timeout * 3);
replacedert.replacedertTrue(es.isTerminated());
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testStopThreads() throws Exception {
DelegationTokenManager tm1 = null;
String connectString = zkServer.getConnectString();
// let's make the update interval short and the shutdown interval
// comparatively longer, so if the update thread runs after shutdown,
// it will cause an error.
final long updateIntervalSeconds = 1;
final long shutdownTimeoutMillis = updateIntervalSeconds * 1000 * 5;
Configuration conf = getSecretConf(connectString);
conf.setLong(DelegationTokenManager.UPDATE_INTERVAL, updateIntervalSeconds);
conf.setLong(DelegationTokenManager.REMOVAL_SCAN_INTERVAL, updateIntervalSeconds);
conf.setLong(DelegationTokenManager.RENEW_INTERVAL, updateIntervalSeconds);
conf.setLong(ZKDelegationTokenSecretManager.ZK_DTSM_ZK_SHUTDOWN_TIMEOUT, shutdownTimeoutMillis);
tm1 = new DelegationTokenManager(conf, new Text("foo"));
tm1.init();
Token<DelegationTokenIdentifier> token = (Token<DelegationTokenIdentifier>) tm1.createToken(UserGroupInformation.getCurrentUser(), "foo");
replacedert.replacedertNotNull(token);
AbstractDelegationTokenSecretManager sm = tm1.getDelegationTokenSecretManager();
ZKDelegationTokenSecretManager zksm = (ZKDelegationTokenSecretManager) sm;
ExecutorService es = zksm.getListenerThreadPool();
es.submit(new Callable<Void>() {
public Void call() throws Exception {
// force this to be shutdownNow
Thread.sleep(shutdownTimeoutMillis * 2);
return null;
}
});
tm1.destroy();
}
@Test
public void testACLs() throws Exception {
DelegationTokenManager tm1;
String connectString = zkServer.getConnectString();
Configuration conf = getSecretConf(connectString);
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
String userPreplaced = "myuser:mypreplaced";
final ACL digestACL = new ACL(ZooDefs.Perms.ALL, new Id("digest", DigestAuthenticationProvider.generateDigest(userPreplaced)));
ACLProvider digestAclProvider = new ACLProvider() {
@Override
public List<ACL> getAclForPath(String path) {
return getDefaultAcl();
}
@Override
public List<ACL> getDefaultAcl() {
List<ACL> ret = new ArrayList<ACL>();
ret.add(digestACL);
return ret;
}
};
CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString(connectString).retryPolicy(retryPolicy).aclProvider(digestAclProvider).authorization("digest", userPreplaced.getBytes("UTF-8")).build();
curatorFramework.start();
ZKDelegationTokenSecretManager.setCurator(curatorFramework);
tm1 = new DelegationTokenManager(conf, new Text("bla"));
tm1.init();
// check ACL
String workingPath = conf.get(ZKDelegationTokenSecretManager.ZK_DTSM_ZNODE_WORKING_PATH);
verifyACL(curatorFramework, "/" + workingPath, digestACL);
tm1.destroy();
ZKDelegationTokenSecretManager.setCurator(null);
curatorFramework.close();
}
private void verifyACL(CuratorFramework curatorFramework, String path, ACL expectedACL) throws Exception {
List<ACL> acls = curatorFramework.getACL().forPath(path);
replacedert.replacedertEquals(1, acls.size());
replacedert.replacedertEquals(expectedACL, acls.get(0));
}
// Since it is possible that there can be a delay for the cancel token message
// initiated by one node to reach another node.. The second node can ofcourse
// verify with ZK directly if the token that needs verification has been
// cancelled but.. that would mean having to make an RPC call for every
// verification request.
// Thus, the eventual consistency tradef-off should be acceptable here...
private void verifyTokenFail(DelegationTokenManager tm, Token<DelegationTokenIdentifier> token) throws IOException, InterruptedException {
verifyTokenFailWithRetry(tm, token, RETRY_COUNT);
}
private void verifyTokenFailWithRetry(DelegationTokenManager tm, Token<DelegationTokenIdentifier> token, int retryCount) throws IOException, InterruptedException {
try {
tm.verifyToken(token);
} catch (SecretManager.InvalidToken er) {
throw er;
}
if (retryCount > 0) {
Thread.sleep(RETRY_WAIT);
verifyTokenFailWithRetry(tm, token, retryCount - 1);
}
}
}
19
Source : ThriftProvider.java
with Apache License 2.0
from Meituan-Dianping
with Apache License 2.0
from Meituan-Dianping
public clreplaced ThriftProvider {
protected static TestingServer zkServer;
public static void initZkServer() throws Exception {
int zkServerPort = 2200;
zkServer = new TestingServer(zkServerPort, true);
}
public static void main(String[] args) throws Exception {
initZkServer();
ClreplacedPathXmlApplicationContext beanFactory = new ClreplacedPathXmlApplicationContext("thrift/zkmock/thrift-provider.xml");
ConsoleCommandProcessor.processCommandsWithQuitOperation(beanFactory, new QuitOperation() {
@Override
public void prepareQuit() {
try {
closeZkServer();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public static void closeZkServer() throws IOException {
zkServer.stop();
}
}
19
Source : ThriftProvider.java
with Apache License 2.0
from Meituan-Dianping
with Apache License 2.0
from Meituan-Dianping
public clreplaced ThriftProvider {
protected static TestingServer zkServer;
public static void initZkServer() throws Exception {
int zkServerPort = 2200;
zkServer = new TestingServer(zkServerPort, true);
}
public static void main(String[] args) throws Exception {
initZkServer();
ServiceConfig<HelloService.Iface> serviceConfig = new ServiceConfig<>();
serviceConfig.setServiceImpl(new HelloServiceImpl());
serviceConfig.setServiceInterface(HelloService.clreplaced);
serviceConfig.setServiceName(HelloService.clreplaced.getName());
ProviderConfig config = new ProviderConfig();
config.setAppkey("com.meituan.octo.dorado.server");
config.setServiceConfig(serviceConfig);
// 这里是模拟zkserver
config.setRegistry("zookeeper://127.0.0.1:2200");
config.setPort(9001);
config.init();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
config.destroy();
}
zkServer.close();
}
}
19
Source : ZooKeeperRegistryTest.java
with Apache License 2.0
from ljygz
with Apache License 2.0
from ljygz
public clreplaced ZooKeeperRegistryTest extends TestLogger {
private TestingServer testingServer;
@Before
public void before() throws Exception {
testingServer = new TestingServer();
}
@After
public void after() throws Exception {
testingServer.stop();
testingServer = null;
}
/**
* Tests that the function of ZookeeperRegistry, setJobRunning(), setJobFinished(), isJobRunning()
*/
@Test
public void testZooKeeperRegistry() throws Exception {
Configuration configuration = new Configuration();
configuration.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
configuration.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
final HighAvailabilityServices zkHaService = new ZooKeeperHaServices(ZooKeeperUtils.startCuratorFramework(configuration), Executors.directExecutor(), configuration, new VoidBlobStore());
final RunningJobsRegistry zkRegistry = zkHaService.getRunningJobsRegistry();
try {
JobID jobID = JobID.generate();
replacedertEquals(JobSchedulingStatus.PENDING, zkRegistry.getJobSchedulingStatus(jobID));
zkRegistry.setJobRunning(jobID);
replacedertEquals(JobSchedulingStatus.RUNNING, zkRegistry.getJobSchedulingStatus(jobID));
zkRegistry.setJobFinished(jobID);
replacedertEquals(JobSchedulingStatus.DONE, zkRegistry.getJobSchedulingStatus(jobID));
zkRegistry.clearJob(jobID);
replacedertEquals(JobSchedulingStatus.PENDING, zkRegistry.getJobSchedulingStatus(jobID));
} finally {
zkHaService.close();
}
}
}
19
Source : ZookeeperDistributedLockTest.java
with Apache License 2.0
from Kyligence
with Apache License 2.0
from Kyligence
public clreplaced ZookeeperDistributedLockTest extends HBaseMetadataTestCase {
private static final String ZK_PFX = "/test/ZookeeperDistributedLockTest/" + new Random().nextInt(10000000);
static ZookeeperDistributedLock.Factory factory;
private TestingServer zkTestServer;
@Before
public void setup() throws Exception {
zkTestServer = new TestingServer();
zkTestServer.start();
System.setProperty("kylin.env.zookeeper-connect-string", zkTestServer.getConnectString());
createTestMetadata();
factory = new ZookeeperDistributedLock.Factory();
}
@After
public void after() throws Exception {
factory.lockForCurrentProcess().purgeLocks(ZK_PFX);
zkTestServer.close();
cleanupTestMetadata();
System.clearProperty("kylin.env.zookeeper-connect-string");
}
@Test
public void testLockCurrentThread() {
DistributedLock lock = factory.lockForCurrentThread();
String path = ZK_PFX + "/test_lock_current_thread";
replacedertFalse(lock.isLocked(path));
replacedertTrue(lock.lock(path));
replacedertTrue(lock.lock(path));
replacedertTrue(lock.lock(path));
replacedertEquals(lock.getClient(), lock.peekLock(path));
replacedertTrue(lock.isLocked(path));
replacedertTrue(lock.isLockedByMe(path));
lock.unlock(path);
replacedertFalse(lock.isLocked(path));
}
@Test
public void testLockForClients() {
String client1 = "client1";
String client2 = "client2";
DistributedLock lock1 = factory.lockForClient(client1);
DistributedLock lock2 = factory.lockForClient(client2);
String path = ZK_PFX + "/test_lock_for_clients";
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
replacedertTrue(lock1.lock(path));
replacedertTrue(lock1.lock(path));
replacedertFalse(lock2.lock(path));
replacedertFalse(lock2.lock(path));
replacedertTrue(lock1.isLocked(path));
replacedertTrue(lock2.isLocked(path));
replacedertTrue(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
lock1.unlock(path);
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
replacedertFalse(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
replacedertTrue(lock2.lock(path));
replacedertTrue(lock2.lock(path));
replacedertFalse(lock1.lock(path));
replacedertFalse(lock1.lock(path));
replacedertTrue(lock1.isLocked(path));
replacedertTrue(lock2.isLocked(path));
replacedertFalse(lock1.isLockedByMe(path));
replacedertTrue(lock2.isLockedByMe(path));
lock2.unlock(path);
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
replacedertFalse(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
}
@Test
public void testSingleClientLockWhenCatchInterruptException() {
String path = ZK_PFX + "/test_interrupt_lock";
DistributedLock lock = factory.lockForClient("client");
DistributedLock spy = Mockito.spy(lock);
// mock interruptException when peekLock only once
Mockito.doThrow(new ZkPeekLockInterruptException("mock interrupt")).doCallRealMethod().when(spy).peekLock(Mockito.anyString());
try {
spy.lock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkPeekLockInterruptException expected
replacedertTrue(e instanceof ZkPeekLockInterruptException);
}
// should release lock
Mockito.reset(spy);
replacedertFalse(lock.isLocked(path));
}
@Test
public void testTwoClientLockWhenCatchInterruptException() {
String path = ZK_PFX + "/test_interrupt_lock";
DistributedLock lock1 = factory.lockForClient("client_1");
DistributedLock lock2 = factory.lockForClient("client_2");
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
// lock first by client_1
replacedertTrue(lock1.lock(path));
replacedertFalse(lock2.lock(path));
replacedertTrue(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
// mock lock for client_2 to simulate lock when an InterruptException caught
DistributedLock spy2 = Mockito.spy(lock2);
// mock interruptException when peekLock only once
Mockito.doThrow(new ZkPeekLockInterruptException("mock interrupt")).doCallRealMethod().when(spy2).peekLock(Mockito.anyString());
try {
spy2.lock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkPeekLockInterruptException expected
replacedertTrue(e instanceof ZkPeekLockInterruptException);
}
// should not release lock because lock was held by client_1
Mockito.reset(spy2);
replacedertTrue(lock1.isLocked(path));
replacedertTrue(lock2.isLocked(path));
replacedertTrue(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
// mock lock for client_1 to simulate lock when an InterruptException caught
DistributedLock spy1 = Mockito.spy(lock1);
// mock interruptException when peekLock only once
Mockito.doThrow(new ZkPeekLockInterruptException("mock interrupt")).doCallRealMethod().when(spy1).peekLock(Mockito.anyString());
try {
spy1.lock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkPeekLockInterruptException expected
replacedertTrue(e instanceof ZkPeekLockInterruptException);
}
// should release lock because lock was held by client_1
Mockito.reset(spy1);
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
replacedertFalse(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
}
@Test
public void testSingleClientUnlockWhenCatchInterruptExceptionOnPeekLock() {
String path = ZK_PFX + "/test_interrupt_lock";
DistributedLock lock = factory.lockForClient("client");
replacedertFalse(lock.isLocked(path));
replacedertTrue(lock.lock(path));
replacedertTrue(lock.isLocked(path));
replacedertTrue(lock.isLockedByMe(path));
DistributedLock spy = Mockito.spy(lock);
// mock interruptException when peekLock only once
Mockito.doThrow(new ZkPeekLockInterruptException("mock interrupt")).doCallRealMethod().when(spy).peekLock(Mockito.anyString());
try {
spy.unlock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkPeekLockInterruptException expected
replacedertTrue(e instanceof ZkPeekLockInterruptException);
}
// should release lock
Mockito.reset(spy);
replacedertFalse(lock.isLocked(path));
}
@Test
public void testTwoClientUnlockWhenCatchInterruptExceptionOnPeekLock() {
String path = ZK_PFX + "/test_interrupt_lock";
DistributedLock lock1 = factory.lockForClient("client_1");
DistributedLock lock2 = factory.lockForClient("client_2");
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
// lock first by client_1
replacedertTrue(lock1.lock(path));
replacedertFalse(lock2.lock(path));
replacedertTrue(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
// mock lock for client_2 to simulate lock when an InterruptException caught
DistributedLock spy2 = Mockito.spy(lock2);
// mock interruptException when peekLock only once
Mockito.doThrow(new ZkPeekLockInterruptException("mock interrupt")).doCallRealMethod().when(spy2).peekLock(Mockito.anyString());
try {
spy2.unlock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkReleaseLockException expected: lock was held by client_1
replacedertTrue(e instanceof ZkReleaseLockException);
}
// should not release lock because lock was held by client_1
Mockito.reset(spy2);
replacedertTrue(lock1.isLocked(path));
replacedertTrue(lock2.isLocked(path));
replacedertTrue(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
// mock lock for client_1 to simulate lock when an InterruptException caught
DistributedLock spy1 = Mockito.spy(lock1);
// mock interruptException when peekLock only once
Mockito.doThrow(new ZkPeekLockInterruptException("mock interrupt")).doCallRealMethod().when(spy1).peekLock(Mockito.anyString());
try {
spy1.unlock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkPeekLockInterruptException expected
replacedertTrue(e instanceof ZkPeekLockInterruptException);
}
// should release lock because lock was held by client_1
Mockito.reset(spy1);
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
replacedertFalse(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
}
@Test
public void testSingleClientUnlockWhenCatchInterruptExceptionOnPurgeLock() {
String path = ZK_PFX + "/test_interrupt_lock";
ZookeeperDistributedLock lock = (ZookeeperDistributedLock) factory.lockForClient("client");
replacedertFalse(lock.isLocked(path));
replacedertTrue(lock.lock(path));
replacedertTrue(lock.isLocked(path));
replacedertTrue(lock.isLockedByMe(path));
ZookeeperDistributedLock spy = Mockito.spy(lock);
// mock interruptException when purgeLock only once
Mockito.doThrow(new ZkReleaseLockInterruptException("mock interrupt")).doCallRealMethod().when(spy).purgeLockInternal(Mockito.anyString());
try {
spy.unlock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkPeekLockInterruptException expected
replacedertTrue(e instanceof ZkReleaseLockInterruptException);
}
// should release lock
Mockito.reset(spy);
replacedertFalse(lock.isLocked(path));
}
@Test
public void testTwoClientUnlockWhenCatchInterruptExceptionOnPurgeLock() {
String path = ZK_PFX + "/test_interrupt_lock";
ZookeeperDistributedLock lock1 = (ZookeeperDistributedLock) factory.lockForClient("client_1");
ZookeeperDistributedLock lock2 = (ZookeeperDistributedLock) factory.lockForClient("client_2");
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
// lock first by client_1
replacedertTrue(lock1.lock(path));
replacedertFalse(lock2.lock(path));
replacedertTrue(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
// mock lock for client_2 to simulate lock when an InterruptException caught
ZookeeperDistributedLock spy2 = Mockito.spy(lock2);
// mock interruptException when purgeLock only once
Mockito.doThrow(new ZkReleaseLockInterruptException("mock interrupt")).doCallRealMethod().when(spy2).purgeLockInternal(Mockito.anyString());
try {
spy2.unlock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkReleaseLockException expected: lock was held by client_1
replacedertTrue(e instanceof ZkReleaseLockException);
}
// should not release lock because lock was held by client_1
Mockito.reset(spy2);
replacedertTrue(lock1.isLocked(path));
replacedertTrue(lock2.isLocked(path));
replacedertTrue(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
// mock lock for client_1 to simulate lock when an InterruptException caught
ZookeeperDistributedLock spy1 = Mockito.spy(lock1);
// mock interruptException when purgeLock only once
Mockito.doThrow(new ZkReleaseLockInterruptException("mock interrupt")).doCallRealMethod().when(spy1).purgeLockInternal(Mockito.anyString());
try {
spy1.unlock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkPeekLockInterruptException expected
replacedertTrue(e instanceof ZkReleaseLockInterruptException);
}
// should release lock because lock was held by client_1
Mockito.reset(spy1);
replacedertFalse(lock1.isLocked(path));
replacedertFalse(lock2.isLocked(path));
replacedertFalse(lock1.isLockedByMe(path));
replacedertFalse(lock2.isLockedByMe(path));
}
}
19
Source : CuratorLeaderSelectorTest.java
with Apache License 2.0
from Kyligence
with Apache License 2.0
from Kyligence
public clreplaced CuratorLeaderSelectorTest extends LocalFileMetadataTestCase {
private TestingServer zkTestServer;
@Before
public void setup() throws Exception {
zkTestServer = new TestingServer();
zkTestServer.start();
System.setProperty("kylin.env.zookeeper-connect-string", zkTestServer.getConnectString());
System.setProperty("kylin.server.mode", "all");
createTestMetadata();
}
@Test
public void testGetBasic() throws SchedulerException, IOException, InterruptedException {
KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv();
final String zkString = zkTestServer.getConnectString();
final String server1 = "server1:1111";
final String server2 = "server2:2222";
String jobEnginePath = CuratorScheduler.JOB_ENGINE_LEADER_PATH;
CuratorFramework client = CuratorFrameworkFactory.newClient(zkString, new ExponentialBackoffRetry(3000, 3));
client.start();
CuratorLeaderSelector s1 = new //
CuratorLeaderSelector(//
client, //
jobEnginePath, //
server1, //
new JobEngineConfig(kylinConfig));
replacedert.replacedertFalse(s1.hasDefaultSchedulerStarted());
CuratorLeaderSelector s2 = new //
CuratorLeaderSelector(//
client, //
jobEnginePath, //
server2, //
new JobEngineConfig(kylinConfig));
s1.start();
// wait for Selector starting
Thread.sleep(1000);
replacedert.replacedertEquals(1, s1.getParticipants().size());
replacedert.replacedertEquals(s1.getParticipants(), s2.getParticipants());
s2.start();
Thread.sleep(1000);
replacedert.replacedertEquals(2, s1.getParticipants().size());
replacedert.replacedertEquals(s1.getParticipants(), s2.getParticipants());
replacedert.replacedertEquals(new Participant(server1, true), s1.getLeader());
replacedert.replacedertEquals(s1.getLeader(), s2.getLeader());
replacedertSchedulerStart(s1);
s1.close();
Thread.sleep(1000);
replacedert.replacedertEquals(1, s1.getParticipants().size());
replacedert.replacedertEquals(s1.getParticipants(), s2.getParticipants());
replacedert.replacedertEquals(new Participant(server2, true), s1.getLeader());
replacedertSchedulerStart(s2);
s2.close();
Thread.sleep(1000);
replacedert.replacedertEquals(0, s1.getParticipants().size());
replacedert.replacedertEquals(s1.getParticipants(), s2.getParticipants());
}
private void replacedertSchedulerStart(CuratorLeaderSelector sele) throws InterruptedException {
for (int i = 0; i < 50 && !sele.hasDefaultSchedulerStarted(); i++) {
Thread.sleep(300);
}
replacedert.replacedertTrue(sele.hasDefaultSchedulerStarted());
}
@After
public void after() throws Exception {
zkTestServer.close();
cleanupTestMetadata();
System.clearProperty("kylin.env.zookeeper-connect-string");
System.clearProperty("kylin.server.host-address");
System.clearProperty("kylin.server.mode");
}
}
19
Source : ZookeeperTestServer.java
with Apache License 2.0
from jwplayer
with Apache License 2.0
from jwplayer
public clreplaced ZookeeperTestServer {
private TestingServer testingServer;
public ZookeeperTestServer() {
try {
testingServer = new TestingServer();
} catch (Exception ex) {
throw new RuntimeException("Couldn't start test ZK server", ex);
}
}
public String getConnectionString() {
return testingServer.getConnectString();
}
public ZkUtils getZkUtils() {
ZkClient zkClient = new ZkClient(getConnectionString(), Integer.MAX_VALUE, 10000, ZKStringSerializer$.MODULE$);
return new ZkUtils(zkClient, new ZkConnection(getConnectionString()), false);
}
public void shutdown() {
try {
testingServer.close();
} catch (IOException ex) {
throw new RuntimeException("Couldn't shutdown test ZK server", ex);
}
}
}
19
Source : EmbedZookeeperTestExecutionListener.java
with Apache License 2.0
from imadcn
with Apache License 2.0
from imadcn
public final clreplaced EmbedZookeeperTestExecutionListener extends AbstractTestExecutionListener {
private static volatile TestingServer testingServer;
@Override
public void beforeTestClreplaced(final TestContext testContext) throws Exception {
startEmbedTestingServer();
}
private static void startEmbedTestingServer() {
if (null != testingServer) {
return;
}
try {
testingServer = new TestingServer(3181, new File(String.format("target/test_zk_data/%s/", System.nanoTime())));
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
RegExceptionHandler.handleException(ex);
} finally {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
EmbedZookeeperTestExecutionListener.sleep(2000L);
testingServer.close();
testingServer = null;
} catch (final IOException ex) {
RegExceptionHandler.handleException(ex);
}
}
});
}
}
public static void sleep(final long millis) {
try {
Thread.sleep(millis);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
19
Source : ZooKeeperSeedNodeProviderTest.java
with Apache License 2.0
from hekate-io
with Apache License 2.0
from hekate-io
public clreplaced ZooKeeperSeedNodeProviderTest extends PersistentSeedNodeProviderTestBase<ZooKeeperSeedNodeProvider> {
private static final int ZOOKEEPER_PORT = 10087;
private static TestingServer server;
@BeforeClreplaced
public static void startZooKeeper() throws Exception {
server = new TestingServer(ZOOKEEPER_PORT);
server.start();
}
@AfterClreplaced
public static void stopZooKeeper() throws Exception {
if (server != null) {
server.close();
}
}
@Override
public void setUp() throws Exception {
ignoreGhostThreads();
super.setUp();
}
@Test
public void testConfigReport() throws Exception {
ZooKeeperSeedNodeProvider provider = createProvider();
replacedertEquals(NL + " zookeeper:" + NL + " connection-string: " + provider.connectionString() + NL + " base-path: " + provider.basePath() + NL + " connect-timeout: " + provider.connectTimeout() + NL + " session-timeout: " + provider.sessionTimeout() + NL + " cleanup-interval: " + provider.cleanupInterval() + NL, DefaultConfigReporter.report(provider));
}
@Test
public void testConnectTimeout() throws Exception {
try (ServerSocket sock = new ServerSocket()) {
InetSocketAddress addr = newSocketAddress();
sock.bind(addr);
ZooKeeperSeedNodeProvider provider = createProvider(cfg -> cfg.withConnectionString(addr.getHostString() + ':' + addr.getPort()));
expectCause(HekateException.clreplaced, "Timeout connecting to ZooKeeper", () -> provider.startDiscovery(CLUSTER_1, newSocketAddress()));
}
}
@Test
public void testProvider() throws Exception {
ZooKeeperSeedNodeProvider p1 = createProvider();
replacedertEquals(server.getConnectString(), p1.connectionString());
replacedertEquals("/hekate-test-path/", p1.basePath());
ZooKeeperSeedNodeProvider p2 = createProvider(cfg -> cfg.withBasePath("/hekate-test-path"));
replacedertEquals("/hekate-test-path/", p2.basePath());
}
@Override
protected ZooKeeperSeedNodeProvider createProvider() throws Exception {
return createProvider(null);
}
protected ZooKeeperSeedNodeProvider createProvider(Consumer<ZooKeeperSeedNodeProviderConfig> configurer) throws Exception {
ZooKeeperSeedNodeProviderConfig cfg = new ZooKeeperSeedNodeProviderConfig().withConnectionString(server.getConnectString()).withConnectTimeout(1000).withSessionTimeout(1000).withCleanupInterval(100).withBasePath("/hekate-test-path/");
if (configurer != null) {
configurer.accept(cfg);
}
return new ZooKeeperSeedNodeProvider(cfg);
}
}
19
Source : HekateZooKeeperSeedNodeProviderConfigurerTest.java
with Apache License 2.0
from hekate-io
with Apache License 2.0
from hekate-io
public clreplaced HekateZooKeeperSeedNodeProviderConfigurerTest extends HekateAutoConfigurerTestBase {
@EnableHekate
@EnableAutoConfiguration
public static clreplaced ZooKeeperEnabledConfig {
// No-op.
}
@EnableAutoConfiguration
public static clreplaced ZooKeeperDisabledConfig extends HekateTestConfigBase {
// No-op.
}
private static TestingServer zooKeeper;
@BeforeClreplaced
public static void startTestServer() throws Exception {
zooKeeper = new TestingServer(true);
}
@AfterClreplaced
public static void stopTestServer() throws Exception {
if (zooKeeper != null) {
zooKeeper.close();
}
}
@Test
public void testEnabled() throws Exception {
ignoreGhostThreads();
registerAndRefresh(new String[] { "hekate.cluster.seed.zookeeper.enable=true", "hekate.cluster.seed.zookeeper.connection-string=" + zooKeeper.getConnectString(), "hekate.cluster.seed.zookeeper.base-path=/hekate-test", "hekate.cluster.seed.zookeeper.connect-timeout=500", "hekate.cluster.seed.zookeeper.session-timeout=500" }, ZooKeeperEnabledConfig.clreplaced);
SeedNodeProviderGroup group = (SeedNodeProviderGroup) getNode().get(DefaultClusterService.clreplaced).seedNodeProvider();
replacedertEquals(1, group.allProviders().size());
replacedertTrue(group.allProviders().get(0) instanceof ZooKeeperSeedNodeProvider);
replacedertEquals(group.allProviders(), group.liveProviders());
ZooKeeperSeedNodeProvider provider = (ZooKeeperSeedNodeProvider) group.allProviders().get(0);
replacedertEquals(zooKeeper.getConnectString(), provider.connectionString());
replacedertEquals("/hekate-test/", provider.basePath());
}
@Test
public void testDisabled() throws Exception {
ignoreGhostThreads();
registerAndRefresh(new String[] { "hekate.cluster.seed.zookeeper.enable=false" }, ZooKeeperDisabledConfig.clreplaced);
SeedNodeProvider provider = getNode().get(DefaultClusterService.clreplaced).seedNodeProvider();
replacedertFalse(provider instanceof ZooKeeperSeedNodeProvider);
}
}
19
Source : ZookeeperRegisterServiceImplTest.java
with Apache License 2.0
from finleytianhe
with Apache License 2.0
from finleytianhe
/**
* @author Geng Zhang
*/
public clreplaced ZookeeperRegisterServiceImplTest {
protected static TestingServer server = null;
@BeforeAll
public static void adBeforeClreplaced() throws Exception {
server = new TestingServer(2181, true);
server.start();
}
@AfterAll
public static void adAfterClreplaced() throws Exception {
if (server != null) {
server.stop();
}
}
ZookeeperRegisterServiceImpl service = (ZookeeperRegisterServiceImpl) new ZookeeperRegistryProvider().provide();
@Test
public void getInstance() {
ZookeeperRegisterServiceImpl service1 = ZookeeperRegisterServiceImpl.getInstance();
replacedertions.replacedertEquals(service1, service);
}
@Test
public void buildZkTest() {
ZkClient client = service.buildZkClient("127.0.0.1:2181", 5000, 5000);
replacedertions.replacedertTrue(client.exists("/zookeeper"));
}
@Test
public void testAll() throws Exception {
service.register(new InetSocketAddress(NetUtil.getLocalAddress(), 33333));
replacedertions.replacedertNull(service.lookup("xxx"));
List<InetSocketAddress> lookup2 = service.doLookup("default");
replacedertions.replacedertTrue(lookup2.size() == 1);
final List<String> data = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(1);
IZkChildListener listener = (s, list) -> {
data.clear();
data.addAll(list);
latch.countDown();
};
service.subscribe("default", listener);
final CountDownLatch latch2 = new CountDownLatch(1);
final List<String> data2 = new ArrayList<>();
IZkChildListener listener2 = (s, list) -> {
data2.clear();
data2.addAll(list);
latch2.countDown();
};
service.subscribe("default", listener2);
service.unregister(new InetSocketAddress(NetUtil.getLocalAddress(), 33333));
latch2.await(1000, TimeUnit.MILLISECONDS);
replacedertions.replacedertTrue(data2.size() == 0);
service.unsubscribe("default", listener);
service.unsubscribe("default", listener2);
}
}
19
Source : EmbedZookeeperServer.java
with Apache License 2.0
from fang-yan-peng
with Apache License 2.0
from fang-yan-peng
/**
* 内存版的内嵌Zookeeper.
*
* <p>
* 仅用于运行例子时无需额外启动Zookeeper. 如有必要, 请使用本地环境可用的Zookeeper代替.
* </p>
*/
public final clreplaced EmbedZookeeperServer {
private static TestingServer testingServer;
/**
* 内存版的内嵌Zookeeper.
*
* @param port Zookeeper的通信端口号
*/
public static void start(final int port) {
try {
testingServer = new TestingServer(port, new File(String.format("target/test_zk_data/%s/", System.nanoTime())));
} catch (final Exception ex) {
ex.printStackTrace();
} finally {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
Thread.sleep(1000L);
testingServer.close();
} catch (final InterruptedException | IOException ex) {
}
}
});
}
}
}
19
Source : ZookeeperDataSourceTest.java
with Apache License 2.0
from eacdy
with Apache License 2.0
from eacdy
/**
* Test whether different dataSources can share the same zkClient when the connection parameters are the same.
* @throws Exception
*/
@Test
public void testZooKeeperDataSourceSameZkClient() throws Exception {
TestingServer server = new TestingServer(21813);
server.start();
final String remoteAddress = server.getConnectString();
final String flowPath = "/sentinel-zk-ds-demo/flow-HK";
final String degradePath = "/sentinel-zk-ds-demo/degrade-HK";
ZookeeperDataSource<List<FlowRule>> flowRuleZkDataSource = new ZookeeperDataSource<>(remoteAddress, flowPath, new Converter<String, List<FlowRule>>() {
@Override
public List<FlowRule> convert(String source) {
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
});
}
});
ZookeeperDataSource<List<DegradeRule>> degradeRuleZkDataSource = new ZookeeperDataSource<>(remoteAddress, degradePath, new Converter<String, List<DegradeRule>>() {
@Override
public List<DegradeRule> convert(String source) {
return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
});
}
});
replacedert.replacedertTrue(flowRuleZkDataSource.getZkClient() == degradeRuleZkDataSource.getZkClient());
final String groupId = "sentinel-zk-ds-demo";
final String flowDataId = "flow-HK";
final String degradeDataId = "degrade-HK";
final String scheme = "digest";
final String auth = "root:123456";
AuthInfo authInfo = new AuthInfo(scheme, auth.getBytes());
List<AuthInfo> authInfoList = Collections.singletonList(authInfo);
ZookeeperDataSource<List<FlowRule>> flowRuleZkAutoDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress, authInfoList, groupId, flowDataId, new Converter<String, List<FlowRule>>() {
@Override
public List<FlowRule> convert(String source) {
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
});
}
});
ZookeeperDataSource<List<DegradeRule>> degradeRuleZkAutoDataSource = new ZookeeperDataSource<List<DegradeRule>>(remoteAddress, authInfoList, groupId, degradeDataId, new Converter<String, List<DegradeRule>>() {
@Override
public List<DegradeRule> convert(String source) {
return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
});
}
});
replacedert.replacedertTrue(flowRuleZkAutoDataSource.getZkClient() == degradeRuleZkAutoDataSource.getZkClient());
server.stop();
}
19
Source : ZookeeperConfigurationTest.java
with Apache License 2.0
from dromara
with Apache License 2.0
from dromara
/**
* Test case for ZookeeperConfiguration.
*
* @author fengzhenbing
*/
public final clreplaced ZookeeperConfigurationTest extends AbstractConfigurationTest {
private static TestingServer zkServer;
private final String[] inlinedProperties = new String[] { "soul.sync.zookeeper.url=127.0.0.1:21810", "soul.sync.zookeeper.sessionTimeout=5000", "soul.sync.zookeeper.connectionTimeout=2000", "soul.sync.zookeeper.serializer=org.I0Itec.zkclient.serialize.SerializableSerializer" };
@BeforeClreplaced
public static void setUpBefore() throws Exception {
zkServer = new TestingServer(21810, true);
}
@Test
public void testOnMissingBean() {
// init zkClient by ZookeeperConfiguration
load(ZookeeperConfiguration.clreplaced, inlinedProperties);
ZkClient zkClient = (ZkClient) getContext().getBean("zkClient");
replacedertNotNull(zkClient);
}
@Test
public void testOnExistBean() {
// verify zkClient by ZookeeperConfiguration
load(CustomZkClientConfiguration.clreplaced, inlinedProperties);
boolean isExistZkClient = getContext().containsBean("zkClient");
replacedertFalse(isExistZkClient);
// get customZkClient
ZkClient customZkClient = (ZkClient) getContext().getBean("customZkClient");
replacedertNotNull(customZkClient);
}
@AfterClreplaced
public static void tearDown() throws Exception {
zkServer.stop();
}
@EnableConfigurationProperties(ZookeeperProperties.clreplaced)
static clreplaced CustomZkClientConfiguration {
@Bean
public ZkClient customZkClient(final ZookeeperProperties zookeeperProp) {
return new ZkClient(zookeeperProp.getUrl(), zookeeperProp.getSessionTimeout(), zookeeperProp.getConnectionTimeout());
}
}
}
19
Source : ZooKeeperServerResource.java
with Apache License 2.0
from dremio
with Apache License 2.0
from dremio
final clreplaced ZooKeeperServerResource extends ExternalResource {
private TestingServer testingServer;
private CuratorZookeeperClient zkClient;
@Override
protected void before() throws Throwable {
testingServer = new TestingServer(true);
zkClient = new CuratorZookeeperClient(testingServer.getConnectString(), 5000, 5000, null, new RetryOneTime(1000));
zkClient.start();
zkClient.blockUntilConnectedOrTimedOut();
}
@Override
protected void after() {
try {
zkClient.close();
testingServer.close();
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
public ZooKeeper getZKClient() throws Exception {
return zkClient.getZooKeeper();
}
public int getPort() {
return testingServer.getPort();
}
public String getConnectString() {
return testingServer.getConnectString();
}
public void restartServer() throws IOException {
try {
testingServer.restart();
} catch (Exception e) {
Throwables.propagateIfPossible(e, IOException.clreplaced);
throw Throwables.propagate(e);
}
}
public void closeServer() throws IOException {
testingServer.close();
}
}
19
Source : ZkclientZookeeperTransporterTest.java
with Apache License 2.0
from boomblog
with Apache License 2.0
from boomblog
public clreplaced ZkclientZookeeperTransporterTest {
private TestingServer zkServer;
private ZookeeperClient zookeeperClient;
@BeforeEach
public void setUp() throws Exception {
int zkServerPort = NetUtils.getAvailablePort();
zkServer = new TestingServer(zkServerPort, true);
zookeeperClient = new ZkclientZookeeperTransporter().connect(URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/service"));
}
@Test
public void testZookeeperClient() {
replacedertThat(zookeeperClient, not(nullValue()));
zookeeperClient.close();
}
@AfterEach
public void tearDown() throws Exception {
zkServer.stop();
}
}
19
Source : ZkclientZookeeperClientTest.java
with Apache License 2.0
from boomblog
with Apache License 2.0
from boomblog
public clreplaced ZkclientZookeeperClientTest {
private TestingServer zkServer;
private ZkclientZookeeperClient zkclientZookeeperClient;
@BeforeEach
public void setUp() throws Exception {
int zkServerPort = NetUtils.getAvailablePort();
zkServer = new TestingServer(zkServerPort, true);
zkclientZookeeperClient = new ZkclientZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/org.apache.dubbo.registry.RegistryService"));
}
@Test
public void testCheckExists() {
String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
zkclientZookeeperClient.create(path, false);
replacedertThat(zkclientZookeeperClient.checkExists(path), is(true));
replacedertThat(zkclientZookeeperClient.checkExists(path + "/noneexits"), is(false));
}
@Test
public void testDeletePath() {
String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
zkclientZookeeperClient.create(path, false);
replacedertThat(zkclientZookeeperClient.checkExists(path), is(true));
zkclientZookeeperClient.delete(path);
replacedertThat(zkclientZookeeperClient.checkExists(path), is(false));
}
@Test
public void testConnectState() throws Exception {
replacedertThat(zkclientZookeeperClient.isConnected(), is(true));
final CountDownLatch stopLatch = new CountDownLatch(1);
zkclientZookeeperClient.addStateListener(new StateListener() {
@Override
public void stateChanged(int connected) {
stopLatch.countDown();
}
});
zkServer.stop();
stopLatch.await();
replacedertThat(zkclientZookeeperClient.isConnected(), is(false));
}
@Test
public void testChildrenListener() throws InterruptedException {
String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
zkclientZookeeperClient.create(path, false);
final CountDownLatch countDownLatch = new CountDownLatch(1);
zkclientZookeeperClient.addTargetChildListener(path, new IZkChildListener() {
@Override
public void handleChildChange(String s, List<String> list) throws Exception {
countDownLatch.countDown();
}
});
zkclientZookeeperClient.createPersistent(path + "/provider1");
countDownLatch.await();
}
@Test
public void testGetChildren() throws IOException {
String path = "/dubbo/org.apache.dubbo.demo.DemoService/parentProviders";
zkclientZookeeperClient.create(path, false);
for (int i = 0; i < 5; i++) {
zkclientZookeeperClient.createEphemeral(path + "/server" + i);
}
List<String> zookeeperClientChildren = zkclientZookeeperClient.getChildren(path);
replacedertThat(zookeeperClientChildren, hreplacedize(5));
}
@Test
public void testCreateContentPersistent() {
String path = "/ZkclientZookeeperClient/content.data";
String content = "createContentTest";
zkclientZookeeperClient.delete(path);
replacedertThat(zkclientZookeeperClient.checkExists(path), is(false));
replacedertNull(zkclientZookeeperClient.getContent(path));
zkclientZookeeperClient.create(path, content, false);
replacedertThat(zkclientZookeeperClient.checkExists(path), is(true));
replacedertEquals(zkclientZookeeperClient.getContent(path), content);
}
@Test
public void testCreateContentTem() {
String path = "/ZkclientZookeeperClient/content.data";
String content = "createContentTest";
zkclientZookeeperClient.delete(path);
replacedertThat(zkclientZookeeperClient.checkExists(path), is(false));
replacedertNull(zkclientZookeeperClient.getContent(path));
zkclientZookeeperClient.create(path, content, true);
replacedertThat(zkclientZookeeperClient.checkExists(path), is(true));
replacedertEquals(zkclientZookeeperClient.getContent(path), content);
}
@AfterEach
public void tearDown() throws Exception {
zkclientZookeeperClient.close();
zkServer.stop();
}
}
19
Source : ZkClientWrapperTest.java
with Apache License 2.0
from boomblog
with Apache License 2.0
from boomblog
public clreplaced ZkClientWrapperTest {
private TestingServer zkServer;
private ZkClientWrapper zkClientWrapper;
@BeforeEach
public void setUp() throws Exception {
int zkServerPort = NetUtils.getAvailablePort();
zkServer = new TestingServer(zkServerPort, true);
zkClientWrapper = new ZkClientWrapper("127.0.0.1:" + zkServerPort, 10000);
}
@AfterEach
public void tearDown() throws Exception {
zkServer.stop();
}
@Test
public void testConnectedStatus() {
boolean connected = zkClientWrapper.isConnected();
replacedertThat(connected, is(false));
zkClientWrapper.start();
IZkChildListener listener = mock(IZkChildListener.clreplaced);
zkClientWrapper.subscribeChildChanges("/path", listener);
zkClientWrapper.unsubscribeChildChanges("/path", listener);
}
}
19
Source : CuratorZookeeperTransporterTest.java
with Apache License 2.0
from boomblog
with Apache License 2.0
from boomblog
public clreplaced CuratorZookeeperTransporterTest {
private TestingServer zkServer;
private ZookeeperClient zookeeperClient;
@BeforeEach
public void setUp() throws Exception {
int zkServerPort = NetUtils.getAvailablePort();
zkServer = new TestingServer(zkServerPort, true);
zookeeperClient = new CuratorZookeeperTransporter().connect(URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/service"));
}
@Test
public void testZookeeperClient() {
replacedertThat(zookeeperClient, not(nullValue()));
zookeeperClient.close();
}
@AfterEach
public void tearDown() throws Exception {
zkServer.stop();
}
}
19
Source : CuratorZookeeperClientTest.java
with Apache License 2.0
from boomblog
with Apache License 2.0
from boomblog
public clreplaced CuratorZookeeperClientTest {
private TestingServer zkServer;
private CuratorZookeeperClient curatorClient;
@BeforeEach
public void setUp() throws Exception {
int zkServerPort = NetUtils.getAvailablePort();
zkServer = new TestingServer(zkServerPort, true);
curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort + "/org.apache.dubbo.registry.RegistryService"));
}
@Test
public void testCheckExists() {
String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
curatorClient.create(path, false);
replacedertThat(curatorClient.checkExists(path), is(true));
replacedertThat(curatorClient.checkExists(path + "/noneexits"), is(false));
}
@Test
public void testChildrenPath() {
String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
curatorClient.create(path, false);
curatorClient.create(path + "/provider1", false);
curatorClient.create(path + "/provider2", false);
List<String> children = curatorClient.getChildren(path);
replacedertThat(children.size(), is(2));
}
@Test
public void testChildrenListener() throws InterruptedException {
String path = "/dubbo/org.apache.dubbo.demo.DemoService/providers";
curatorClient.create(path, false);
final CountDownLatch countDownLatch = new CountDownLatch(1);
curatorClient.addTargetChildListener(path, new CuratorWatcher() {
@Override
public void process(WatchedEvent watchedEvent) throws Exception {
countDownLatch.countDown();
}
});
curatorClient.createPersistent(path + "/provider1");
countDownLatch.await();
}
@Test
public void testWithInvalidServer() {
replacedertions.replacedertThrows(IllegalStateException.clreplaced, () -> {
curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:1/service"));
curatorClient.create("/testPath", true);
});
}
@Test
public void testWithStoppedServer() throws IOException {
replacedertions.replacedertThrows(IllegalStateException.clreplaced, () -> {
curatorClient.create("/testPath", true);
zkServer.stop();
curatorClient.delete("/testPath");
});
}
@Test
public void testRemoveChildrenListener() {
ChildListener childListener = mock(ChildListener.clreplaced);
curatorClient.addChildListener("/children", childListener);
curatorClient.removeChildListener("/children", childListener);
}
@Test
public void testCreateExistingPath() {
curatorClient.create("/pathOne", false);
curatorClient.create("/pathOne", false);
}
@Test
public void testConnectedStatus() {
curatorClient.createEphemeral("/testPath");
boolean connected = curatorClient.isConnected();
replacedertThat(connected, is(true));
}
@Test
public void testCreateContent4Persistent() {
String path = "/curatorTest4CrContent/content.data";
String content = "createContentTest";
curatorClient.delete(path);
replacedertThat(curatorClient.checkExists(path), is(false));
replacedertNull(curatorClient.getContent(path));
curatorClient.create(path, content, false);
replacedertThat(curatorClient.checkExists(path), is(true));
replacedertEquals(curatorClient.getContent(path), content);
}
@Test
public void testCreateContent4Temp() {
String path = "/curatorTest4CrContent/content.data";
String content = "createContentTest";
curatorClient.delete(path);
replacedertThat(curatorClient.checkExists(path), is(false));
replacedertNull(curatorClient.getContent(path));
curatorClient.create(path, content, true);
replacedertThat(curatorClient.checkExists(path), is(true));
replacedertEquals(curatorClient.getContent(path), content);
}
@AfterEach
public void tearDown() throws Exception {
curatorClient.close();
zkServer.stop();
}
}
19
Source : EmoStopMojo.java
with Apache License 2.0
from bazaarvoice
with Apache License 2.0
from bazaarvoice
private void stopZookeeper() {
final TestingServer zookeeperTestingServer = CrossMojoState.getZookeeperTestingServer(getPluginContext());
if (zookeeperTestingServer != null) {
try {
getLog().info("Stopping zookeeper...");
zookeeperTestingServer.stop();
} catch (IOException e) {
/* best-effort */
} finally {
try {
zookeeperTestingServer.close();
} catch (IOException e) {
/* best-effort */
}
}
}
}
19
Source : EmoStartMojo.java
with Apache License 2.0
from bazaarvoice
with Apache License 2.0
from bazaarvoice
private void startZookeeper() {
try {
getLog().info("Starting zookeeper on port " + zookeeperPort);
// ZooKeeper is too noisy.
System.setProperty("org.slf4j.simpleLogger.log.org.apache.zookeeper", "error");
final TestingServer zookeeperTestingServer = new TestingServer(zookeeperPort);
CrossMojoState.putZookeeperTestingServer(zookeeperTestingServer, getPluginContext());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
19
Source : PartitionedLeaderServiceTest.java
with Apache License 2.0
from bazaarvoice
with Apache License 2.0
from bazaarvoice
public clreplaced ParreplacedionedLeaderServiceTest {
private TestingServer _zookeeper;
@BeforeClreplaced
private void setupZookeeper() throws Exception {
_zookeeper = new TestingServer();
}
@AfterClreplaced
private void teardownZookeeper() throws Exception {
_zookeeper.close();
}
@Test
public void testSingleServer() throws Exception {
try (CuratorFramework curator = CuratorFrameworkFactory.newClient(_zookeeper.getConnectString(), new RetryNTimes(1, 10))) {
curator.start();
ParreplacedionedLeaderService service = new ParreplacedionedLeaderService(curator, "/leader/single", "instance0", "test-service", 3, 1, 5000, TimeUnit.MILLISECONDS, TestLeaderService::new, null);
service.start();
List<LeaderService> leaderServices = service.getParreplacedionLeaderServices();
replacedertEquals(leaderServices.size(), 3, "Wrong number of services for the provided number of parreplacedions");
// Give it 5 seconds to attain leadership on all 3 parreplacedions
Stopwatch stopwatch = Stopwatch.createStarted();
Set<Integer> leadParreplacedions = getLeadParreplacedions(service);
while (leadParreplacedions.size() != 3 && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
Thread.sleep(10);
leadParreplacedions = getLeadParreplacedions(service);
}
replacedertEquals(leadParreplacedions, ImmutableSet.of(0, 1, 2));
service.stop();
}
}
@Test
public void testMultiServer() throws Exception {
List<CuratorFramework> curators = Lists.newArrayListWithCapacity(3);
try {
List<ParreplacedionedLeaderService> services = Lists.newArrayListWithCapacity(3);
for (int i = 0; i < 3; i++) {
CuratorFramework curator = CuratorFrameworkFactory.newClient(_zookeeper.getConnectString(), new RetryNTimes(1, 10));
curator.start();
curators.add(curator);
ParreplacedionedLeaderService service = new ParreplacedionedLeaderService(curator, "/leader/multi", "instance" + i, "test-service", 10, 1, 5000, TimeUnit.MILLISECONDS, TestLeaderService::new, null);
service.start();
services.add(service);
}
// Give it 5 seconds for leadership to be balanced. With 10 parreplacedions and 3 services this should
// be [4, 3, 3] since the first instance has the lowest instance ID.
Stopwatch stopwatch = Stopwatch.createStarted();
List<Set<Integer>> leadParreplacedions = ImmutableList.of();
while (!leadParreplacedions.stream().map(Set::size).collect(Collectors.toList()).equals(ImmutableList.of(4, 3, 3)) && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
Thread.sleep(10);
leadParreplacedions = services.stream().map(this::getLeadParreplacedions).collect(Collectors.toList());
}
Set<Integer> unclaimedParreplacedions = Sets.newLinkedHashSet();
for (int i = 0; i < 10; i++) {
unclaimedParreplacedions.add(i);
}
replacedertEquals(leadParreplacedions.size(), 3, "There should be three sets of parreplacedions of leadership");
replacedertEquals(leadParreplacedions.get(0).size(), 4);
replacedertTrue(Sets.intersection(leadParreplacedions.get(0), unclaimedParreplacedions).equals(leadParreplacedions.get(0)));
unclaimedParreplacedions.removeAll(leadParreplacedions.get(0));
replacedertEquals(leadParreplacedions.get(1).size(), 3);
replacedertTrue(Sets.intersection(leadParreplacedions.get(1), unclaimedParreplacedions).equals(leadParreplacedions.get(1)));
unclaimedParreplacedions.removeAll(leadParreplacedions.get(1));
replacedertEquals(leadParreplacedions.get(2).size(), 3);
replacedertEquals(leadParreplacedions.get(2), unclaimedParreplacedions);
for (ParreplacedionedLeaderService service : services) {
service.stop();
}
} finally {
for (CuratorFramework curator : curators) {
curator.close();
}
}
}
@Test
public void testRelinquish() throws Exception {
List<CuratorFramework> curators = Lists.newArrayListWithCapacity(2);
try {
List<ParreplacedionedLeaderService> services = Lists.newArrayListWithCapacity(2);
for (int i = 0; i < 2; i++) {
CuratorFramework curator = CuratorFrameworkFactory.newClient(_zookeeper.getConnectString(), new RetryNTimes(1, 10));
curator.start();
curators.add(curator);
ParreplacedionedLeaderService service = new ParreplacedionedLeaderService(curator, "/leader/relinquish", "instance" + i, "test-service", 2, 1, 5000, TimeUnit.MILLISECONDS, TestLeaderService::new, null);
services.add(service);
}
// Start only the first service
services.get(0).start();
// Wait up to 5 seconds for the service to acquire leadership for both parreplacedions
Stopwatch stopwatch = Stopwatch.createStarted();
while (getLeadParreplacedions(services.get(0)).size() != 2 && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
Thread.sleep(10);
}
replacedertEquals(getLeadParreplacedions(services.get(0)).size(), 2, "Service should have acquired leadership for both parreplacedions");
// Now start the second service
services.get(1).start();
// Wait for the first service to relinquish one parreplacedion and for the second service to acquire it
stopwatch.reset().start();
Set<Integer> leadParreplacedions0 = ImmutableSet.of();
Set<Integer> leadParreplacedions1 = ImmutableSet.of();
while ((leadParreplacedions0.size() != 1 || leadParreplacedions1.size() != 1) && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
Thread.sleep(10);
leadParreplacedions0 = getLeadParreplacedions(services.get(0));
leadParreplacedions1 = getLeadParreplacedions(services.get(1));
}
Set<Integer> leadParreplacedions = Sets.newLinkedHashSet();
replacedertEquals(leadParreplacedions0.size(), 1);
leadParreplacedions.addAll(leadParreplacedions0);
replacedertEquals(leadParreplacedions1.size(), 1);
leadParreplacedions.addAll(leadParreplacedions1);
replacedertEquals(leadParreplacedions, ImmutableSet.of(0, 1));
// Finally, kill the first service completely
services.get(0).stop();
// Wait for the second service to lead both parreplacedions
stopwatch.reset().start();
leadParreplacedions1 = ImmutableSet.of();
while (!leadParreplacedions1.equals(ImmutableSet.of(0, 1)) && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
Thread.sleep(10);
leadParreplacedions1 = getLeadParreplacedions(services.get(1));
}
replacedertEquals(leadParreplacedions1, ImmutableSet.of(0, 1));
for (ParreplacedionedLeaderService service : services) {
service.stop();
}
} finally {
for (CuratorFramework curator : curators) {
curator.close();
}
}
}
private Set<Integer> getLeadParreplacedions(ParreplacedionedLeaderService parreplacedionedLeaderService) {
Set<Integer> leadParreplacedions = Sets.newLinkedHashSet();
for (Integer parreplacedion : parreplacedionedLeaderService.getLeadingParreplacedions()) {
// Don't just take the service's word for it; double check that the service is actually in the execution body
Service uncastDelegate = parreplacedionedLeaderService.getParreplacedionLeaderServices().get(parreplacedion).getCurrentDelegateService().orNull();
TestLeaderService delegate = uncastDelegate != null && uncastDelegate instanceof TestLeaderService ? (TestLeaderService) uncastDelegate : null;
if (delegate != null && delegate.inBody) {
leadParreplacedions.add(delegate.parreplacedion);
}
}
return leadParreplacedions;
}
private clreplaced TestLeaderService extends AbstractExecutionThreadService {
final int parreplacedion;
volatile boolean inBody;
public TestLeaderService(int parreplacedion) {
this.parreplacedion = parreplacedion;
}
@Override
protected void run() throws Exception {
inBody = true;
try {
while (isRunning()) {
Thread.sleep(10);
}
} finally {
inBody = false;
}
}
}
}
19
Source : EmbedZookeeperServer.java
with Apache License 2.0
from apache
with Apache License 2.0
from apache
/**
* 内存版的内嵌Zookeeper.
*
* <p>
* 仅用于运行Elastic-Job的例子时无需额外启动Zookeeper. 如有必要, 请使用本地环境可用的Zookeeper代替.
* </p>
*/
public final clreplaced EmbedZookeeperServer {
private static TestingServer testingServer;
/**
* 内存版的内嵌Zookeeper.
*
* @param port Zookeeper的通信端口号
*/
public static void start(final int port) {
try {
testingServer = new TestingServer(port, new File(String.format("target/test_zk_data/%s/", System.nanoTime())));
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
ex.printStackTrace();
} finally {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
Thread.sleep(1000L);
testingServer.close();
} catch (final InterruptedException | IOException ex) {
}
}
});
}
}
}
19
Source : TestZKPlacementStateManager.java
with Apache License 2.0
from apache
with Apache License 2.0
from apache
/**
* Test Case for {@link ZKPlacementStateManager}.
*/
public clreplaced TestZKPlacementStateManager {
private TestingServer zkTestServer;
private String zkServers;
private URI uri;
private ZKPlacementStateManager zkPlacementStateManager;
@Before
public void startZookeeper() throws Exception {
zkTestServer = new TestingServer(2181);
zkServers = "127.0.0.1:2181";
uri = new URI("distributedlog-bk://" + zkServers + DLOG_NAMESPACE + "/bknamespace");
zkPlacementStateManager = new ZKPlacementStateManager(uri, new DistributedLogConfiguration(), NullStatsLogger.INSTANCE);
}
@Test(timeout = 60000)
public void testSaveLoad() throws Exception {
TreeSet<ServerLoad> ownerships = new TreeSet<ServerLoad>();
zkPlacementStateManager.saveOwnership(ownerships);
SortedSet<ServerLoad> loadedOwnerships = zkPlacementStateManager.loadOwnership();
replacedertEquals(ownerships, loadedOwnerships);
ownerships.add(new ServerLoad("emptyServer"));
zkPlacementStateManager.saveOwnership(ownerships);
loadedOwnerships = zkPlacementStateManager.loadOwnership();
replacedertEquals(ownerships, loadedOwnerships);
ServerLoad sl1 = new ServerLoad("server1");
sl1.addStream(new StreamLoad("stream1", 3));
sl1.addStream(new StreamLoad("stream2", 4));
ServerLoad sl2 = new ServerLoad("server2");
sl2.addStream(new StreamLoad("stream3", 1));
ownerships.add(sl1);
ownerships.add(sl2);
zkPlacementStateManager.saveOwnership(ownerships);
loadedOwnerships = zkPlacementStateManager.loadOwnership();
replacedertEquals(ownerships, loadedOwnerships);
loadedOwnerships.remove(sl1);
zkPlacementStateManager.saveOwnership(ownerships);
loadedOwnerships = zkPlacementStateManager.loadOwnership();
replacedertEquals(ownerships, loadedOwnerships);
}
private TreeSet<ServerLoad> waitForServerLoadsNotificationAsc(LinkedBlockingQueue<TreeSet<ServerLoad>> notificationQueue, int expectedNumServerLoads) throws InterruptedException {
TreeSet<ServerLoad> notification = notificationQueue.take();
replacedertNotNull(notification);
while (notification.size() < expectedNumServerLoads) {
notification = notificationQueue.take();
}
replacedertEquals(expectedNumServerLoads, notification.size());
return notification;
}
@Test(timeout = 60000)
public void tesreplacedchIndefinitely() throws Exception {
TreeSet<ServerLoad> ownerships = new TreeSet<ServerLoad>();
ownerships.add(new ServerLoad("server1"));
final LinkedBlockingQueue<TreeSet<ServerLoad>> serverLoadNotifications = new LinkedBlockingQueue<TreeSet<ServerLoad>>();
PlacementStateManager.PlacementCallback callback = new PlacementStateManager.PlacementCallback() {
@Override
public void callback(TreeSet<ServerLoad> serverLoads) {
serverLoadNotifications.add(serverLoads);
}
};
// need to initialize the zk path before watching
zkPlacementStateManager.saveOwnership(ownerships);
zkPlacementStateManager.watch(callback);
// cannot verify the callback here as it may call before the verify is called
zkPlacementStateManager.saveOwnership(ownerships);
replacedertEquals(ownerships, waitForServerLoadsNotificationAsc(serverLoadNotifications, 1));
ServerLoad server2 = new ServerLoad("server2");
server2.addStream(new StreamLoad("hella-important-stream", 415));
ownerships.add(server2);
zkPlacementStateManager.saveOwnership(ownerships);
replacedertEquals(ownerships, waitForServerLoadsNotificationAsc(serverLoadNotifications, 2));
}
@Test(timeout = 60000)
public void testZkFormatting() throws Exception {
final String server = "host/10.0.0.0:31351";
final String zkFormattedServer = "host--10.0.0.0:31351";
URI uri = new URI("distributedlog-bk://" + zkServers + DLOG_NAMESPACE + "/bknamespace");
ZKPlacementStateManager zkPlacementStateManager = new ZKPlacementStateManager(uri, new DistributedLogConfiguration(), NullStatsLogger.INSTANCE);
replacedertEquals(zkFormattedServer, zkPlacementStateManager.serverToZkFormat(server));
replacedertEquals(server, zkPlacementStateManager.zkFormatToServer(zkFormattedServer));
}
@After
public void stopZookeeper() throws IOException {
zkTestServer.stop();
}
}
18
Source : TestEphemeralStore.java
with Apache License 2.0
from zpochen
with Apache License 2.0
from zpochen
public clreplaced TestEphemeralStore {
private final static String root = "/test";
private final static String path = "test-key";
private final static String value = "testing";
private TestingServer server;
private CuratorFramework curator;
private TransientStoreConfig<String> config;
private ZkEphemeralStore<String> store;
static clreplaced StoreWithMockClient<V> extends ZkEphemeralStore<V> {
private final ZookeeperClient client = Mockito.mock(ZookeeperClient.clreplaced);
public StoreWithMockClient(final TransientStoreConfig<V> config, final CuratorFramework curator) {
super(config, curator);
}
@Override
protected ZookeeperClient getClient() {
return client;
}
}
@Before
public void setUp() throws Exception {
server = new TestingServer();
final RetryPolicy policy = new RetryNTimes(2, 1000);
curator = CuratorFrameworkFactory.newClient(server.getConnectString(), policy);
config = Mockito.mock(TransientStoreConfig.clreplaced);
Mockito.when(config.getName()).thenReturn(root);
Mockito.when(config.getSerializer()).thenReturn(new InstanceSerializer<String>() {
@Override
public byte[] serialize(final String instance) throws IOException {
if (instance == null) {
return null;
}
return instance.getBytes();
}
@Override
public String deserialize(final byte[] raw) throws IOException {
if (raw == null) {
return null;
}
return new String(raw);
}
});
store = new ZkEphemeralStore<>(config, curator);
server.start();
curator.start();
store.start();
}
/**
* This test ensures store subscribes to receive events from underlying client. Dispatcher tests ensures listeners
* are fired on incoming events. These two sets of tests ensure observer pattern in {@code TransientStore} works fine.
*/
@Test
public void testStoreRegistersDispatcherAndStartsItsClient() throws Exception {
final StoreWithMockClient<String> store = new StoreWithMockClient<>(config, curator);
final PathChildrenCache cache = Mockito.mock(PathChildrenCache.clreplaced);
final ZookeeperClient client = store.getClient();
Mockito.when(client.getCache()).thenReturn(cache);
final ListenerContainer<PathChildrenCacheListener> container = Mockito.mock(ListenerContainer.clreplaced);
Mockito.when(cache.getListenable()).thenReturn(container);
store.start();
Mockito.verify(container).addListener(store.dispatcher);
Mockito.verify(client).start();
}
@After
public void tearDown() throws Exception {
store.close();
curator.close();
server.close();
}
@Test
public void testPutAndGetWorksAntagonistacally() {
store.put(path, value);
final String actual = store.get(path);
replacedert.replacedertEquals("value mismatch", value, actual);
}
}
18
Source : FinderControllerTest.java
with Apache License 2.0
from xfyun
with Apache License 2.0
from xfyun
public clreplaced FinderControllerTest {
private static ZkHelper zkHelper = null;
private static TestingServer server = null;
@BeforeClreplaced
public static void setUp() throws Exception {
if (null == server) {
server = new TestingServer();
}
zkHelper = new ZkHelper(server.getConnectString());
}
@AfterClreplaced
public static void tearDown() throws Exception {
if (null != zkHelper)
zkHelper.closeClient();
}
@Test
public void testPush_config_feedback() {
ConfigManager.getInstance().put(ConfigManager.KEY_ZKSTR, server.getConnectString());
HttpContext context = new HttpContext(UUID.randomUUID().toString());
Map<String, String> params = new HashMap<>();
params.put("pushId", "/root");
params.put("project", "/root");
params.put("serviceGroup", "/root");
params.put("service", "/root");
params.put("version", "/root");
params.put("config", "/root");
params.put("addr", "/root");
params.put("updateStatus", "/root");
params.put("updateTime", "/root");
context.Request.setParams(params);
FinderController finderController = new FinderController();
ActionResult result = null;
result = finderController.push_config_feedback(context);
replacedert.replacedertNotNull(result.getData());
byte[] data = result.getData();
String str = new String(data);
JsonResult jsonResult = JacksonUtils.toObject(str, JsonResult.clreplaced);
System.out.println(JacksonUtils.toJson(jsonResult));
replacedert.replacedertNotNull(result.getData());
replacedert.replacedertEquals(0, jsonResult.getRet());
}
}
18
Source : CynosureControllerTest.java
with Apache License 2.0
from xfyun
with Apache License 2.0
from xfyun
public clreplaced CynosureControllerTest {
private static ZkHelper zkHelper = null;
private static TestingServer server = null;
@BeforeClreplaced
public static void setUp() throws Exception {
if (null == server) {
server = new TestingServer();
}
zkHelper = new ZkHelper(server.getConnectString());
}
@AfterClreplaced
public static void tearDown() throws Exception {
if (null != zkHelper)
zkHelper.closeClient();
}
@Test
public void refresh_provider_statusTest() {
ConfigManager.getInstance().put(ConfigManager.KEY_ZKSTR, server.getConnectString());
ZkHelper zkHelper = ZkInstanceUtil.getInstance();
zkHelper.addPersistent("/root/data/a/data", "{\"a\":1}");
zkHelper.addPersistent("/root/data/b/data", "{\"a\":2}");
zkHelper.addPersistent("/root/data/c/data", "{\"a\":3}");
String path = "/root/data";
String version = "9999";
String nodeData = "abc123456";
byte[] dataByte = null;
try {
byte[] versionBytes = version.getBytes(Constants.DEFAULT_CHARSET);
byte[] pre = ByteUtil.intToByteArray(versionBytes.length);
dataByte = ByteUtil.byteMerge(pre, versionBytes);
dataByte = ByteUtil.byteMerge(dataByte, nodeData.getBytes());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
zkHelper.addOrUpdatePersistentNode(path, dataByte);
CynosureController cynosureController = new CynosureController();
HttpContext context = new HttpContext(UUID.randomUUID().toString());
Map<String, String> params = new HashMap<>();
params.put("path", "/root");
context.Request.setParams(params);
ActionResult result = null;
result = cynosureController.refresh_provider(context);
replacedert.replacedertNotNull(result.getData());
byte[] data = result.getData();
String str = null;
try {
str = new String(data, Constants.DEFAULT_CHARSET);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
JsonResult jsonResult = JacksonUtils.toObject(str, JsonResult.clreplaced);
ArrayList list = (ArrayList) jsonResult.getData().get("pathList");
replacedert.replacedertEquals(((Map) list.get(0)).get("data"), nodeData);
replacedert.replacedertEquals(((Map) list.get(0)).get("pushId"), version);
}
}
18
Source : ClusterControllerTest.java
with Apache License 2.0
from xfyun
with Apache License 2.0
from xfyun
public clreplaced ClusterControllerTest {
private static ZkHelper zkHelper = null;
private static TestingServer server = null;
@BeforeClreplaced
public static void setUp() throws Exception {
if (null == server) {
server = new TestingServer();
}
zkHelper = new ZkHelper(server.getConnectString());
}
@AfterClreplaced
public static void tearDown() throws Exception {
if (null != zkHelper)
zkHelper.closeClient();
}
@Test
public void push_cluster_configTest() {
ConfigManager.getInstance().put(ConfigManager.KEY_ZKSTR, server.getConnectString());
System.out.println(ZkInstanceUtil.getInstance().getClientState());
ClusterController clusterController = new ClusterController();
HttpContext context = new HttpContext(UUID.randomUUID().toString());
Map<String, String> params = new HashMap<>();
params.put(Constants.ZK_NODE_PATH, "/root");
params.put(Constants.ZK_NODE_PUSHID, "1111");
context.Request.setParams(params);
HttpBody httpBody = new HttpBody();
httpBody.setParams(params);
httpBody.setContentData("test".getBytes());
List<HttpBody> list = new ArrayList<>();
Map<String, List<HttpBody>> bodies = new HashMap<>();
bodies.put(Constants.CONTENT_TYPE_OCTET_STREAM, list);
context.Request.setBodies(bodies);
ActionResult result = null;
result = clusterController.push_cluster_config(context);
replacedert.replacedertNotNull(result.getData());
byte[] data = result.getData();
String str = new String(data);
JsonResult jsonResult = JacksonUtils.toObject(str, JsonResult.clreplaced);
replacedert.replacedertEquals(0, jsonResult.getRet());
ZkInstanceUtil.checkZkHelper();
}
@Test
public void push_instance_configTest() {
ConfigManager.getInstance().put(ConfigManager.KEY_ZKSTR, server.getConnectString());
System.out.println(ZkInstanceUtil.getInstance().getClientState());
ClusterController clusterController = new ClusterController();
HttpContext context = new HttpContext(UUID.randomUUID().toString());
Map<String, String> params = new HashMap<>();
params.put(Constants.ZK_NODE_PATH, "/root");
params.put(Constants.ZK_NODE_PUSHID, "1111");
context.Request.setParams(params);
HttpBody httpBody = new HttpBody();
httpBody.setParams(params);
httpBody.setContentData("test".getBytes());
List<HttpBody> list = new ArrayList<>();
Map<String, List<HttpBody>> bodies = new HashMap<>();
bodies.put(Constants.CONTENT_TYPE_OCTET_STREAM, list);
context.Request.setBodies(bodies);
ActionResult result = null;
result = clusterController.push_instance_config(context);
replacedert.replacedertNotNull(result.getData());
byte[] data = result.getData();
String str = new String(data);
JsonResult jsonResult = JacksonUtils.toObject(str, JsonResult.clreplaced);
replacedert.replacedertEquals(0, jsonResult.getRet());
System.out.println(ZkInstanceUtil.checkZkHelper());
}
@Test
public void updateGrayNodeTest() {
}
}
18
Source : TestZooKeeperStateProvider.java
with Apache License 2.0
from wangrenlei
with Apache License 2.0
from wangrenlei
public clreplaced TestZooKeeperStateProvider extends AbstractTestStateProvider {
private volatile StateProvider provider;
private volatile TestingServer zkServer;
private static final Map<PropertyDescriptor, String> defaultProperties = new HashMap<>();
static {
defaultProperties.put(ZooKeeperStateProvider.SESSION_TIMEOUT, "3 secs");
defaultProperties.put(ZooKeeperStateProvider.ROOT_NODE, "/nifi/team1/testing");
defaultProperties.put(ZooKeeperStateProvider.ACCESS_CONTROL, ZooKeeperStateProvider.OPEN_TO_WORLD.getValue());
}
@Before
public void setup() throws Exception {
zkServer = new TestingServer(true);
zkServer.start();
final Map<PropertyDescriptor, String> properties = new HashMap<>(defaultProperties);
properties.put(ZooKeeperStateProvider.CONNECTION_STRING, zkServer.getConnectString());
this.provider = createProvider(properties);
}
private void initializeProvider(final ZooKeeperStateProvider provider, final Map<PropertyDescriptor, String> properties) throws IOException {
provider.initialize(new StateProviderInitializationContext() {
@Override
public String getIdentifier() {
return "Unit Test Provider Initialization Context";
}
@Override
public Map<PropertyDescriptor, PropertyValue> getProperties() {
final Map<PropertyDescriptor, PropertyValue> propValueMap = new HashMap<>();
for (final Map.Entry<PropertyDescriptor, String> entry : properties.entrySet()) {
propValueMap.put(entry.getKey(), new StandardPropertyValue(entry.getValue(), null));
}
return propValueMap;
}
@Override
public PropertyValue getProperty(final PropertyDescriptor property) {
final String prop = properties.get(property);
return new StandardPropertyValue(prop, null);
}
@Override
public SSLContext getSSLContext() {
return null;
}
});
}
private ZooKeeperStateProvider createProvider(final Map<PropertyDescriptor, String> properties) throws Exception {
final ZooKeeperStateProvider provider = new ZooKeeperStateProvider();
initializeProvider(provider, properties);
provider.enable();
return provider;
}
@After
public void clear() throws IOException {
try {
if (provider != null) {
provider.onComponentRemoved(componentId);
provider.disable();
provider.shutdown();
}
} finally {
if (zkServer != null) {
zkServer.stop();
zkServer.close();
}
}
}
@Override
protected StateProvider getProvider() {
return provider;
}
@Test(timeout = 20000)
public void testStateTooLargeExceptionThrownOnSetState() throws InterruptedException {
final Map<String, String> state = new HashMap<>();
final StringBuilder sb = new StringBuilder();
// Build a string that is a little less than 64 KB, because that's
// the largest value available for DataOutputStream.writeUTF
for (int i = 0; i < 6500; i++) {
sb.append("0123456789");
}
for (int i = 0; i < 20; i++) {
state.put("numbers." + i, sb.toString());
}
while (true) {
try {
getProvider().setState(state, componentId);
replacedert.fail("Expected StateTooLargeException");
} catch (final StateTooLargeException stle) {
// expected behavior.
break;
} catch (final IOException ioe) {
// If we attempt to interact with the server too quickly, we will get a
// ZooKeeper ConnectionLoss Exception, which the provider wraps in an IOException.
// We will wait 1 second in this case and try again. The test will timeout if this
// does not succeeed within 20 seconds.
Thread.sleep(1000L);
} catch (final Exception e) {
e.printStackTrace();
replacedert.fail("Expected StateTooLargeException but " + e.getClreplaced() + " was thrown", e);
}
}
}
@Test(timeout = 20000)
public void testStateTooLargeExceptionThrownOnReplace() throws IOException, InterruptedException {
final Map<String, String> state = new HashMap<>();
final StringBuilder sb = new StringBuilder();
// Build a string that is a little less than 64 KB, because that's
// the largest value available for DataOutputStream.writeUTF
for (int i = 0; i < 6500; i++) {
sb.append("0123456789");
}
for (int i = 0; i < 20; i++) {
state.put("numbers." + i, sb.toString());
}
final Map<String, String> smallState = new HashMap<>();
smallState.put("abc", "xyz");
while (true) {
try {
getProvider().setState(smallState, componentId);
break;
} catch (final IOException ioe) {
// If we attempt to interact with the server too quickly, we will get a
// ZooKeeper ConnectionLoss Exception, which the provider wraps in an IOException.
// We will wait 1 second in this case and try again. The test will timeout if this
// does not succeeed within 20 seconds.
Thread.sleep(1000L);
}
}
try {
getProvider().replace(getProvider().getState(componentId), state, componentId);
replacedert.fail("Expected StateTooLargeException");
} catch (final StateTooLargeException stle) {
// expected behavior.
} catch (final Exception e) {
e.printStackTrace();
replacedert.fail("Expected StateTooLargeException", e);
}
}
}
18
Source : Cluster.java
with Apache License 2.0
from wangrenlei
with Apache License 2.0
from wangrenlei
public clreplaced Cluster {
private static final Logger logger = LoggerFactory.getLogger(Cluster.clreplaced);
private final Set<Node> nodes = new HashSet<>();
private final TestingServer zookeeperServer;
private final long flowElectionTimeoutMillis;
private final Integer flowElectionMaxNodes;
public Cluster() throws IOException {
this(3, TimeUnit.SECONDS, 3);
}
public Cluster(final long flowElectionTimeout, final TimeUnit flowElectionTimeUnit, final Integer flowElectionMaxNodes) throws IOException {
try {
zookeeperServer = new TestingServer();
} catch (final Exception e) {
throw new RuntimeException(e);
}
this.flowElectionTimeoutMillis = flowElectionTimeUnit.toMillis(flowElectionTimeout);
this.flowElectionMaxNodes = flowElectionMaxNodes;
}
public void start() {
try {
zookeeperServer.start();
} catch (final RuntimeException e) {
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
}
while (getZooKeeperConnectString() == null) {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
}
}
logger.info("Start ZooKeeper Server on Port {}, with temporary directory {}", zookeeperServer.getPort(), zookeeperServer.getTempDirectory());
}
public void stop() {
for (final Node node : nodes) {
try {
if (node.isRunning()) {
node.stop();
}
} catch (Exception e) {
logger.error("Failed to shut down " + node, e);
}
}
try {
zookeeperServer.stop();
zookeeperServer.close();
} catch (final Exception e) {
}
}
public String getZooKeeperConnectString() {
return zookeeperServer.getConnectString();
}
public Set<Node> getNodes() {
return Collections.unmodifiableSet(nodes);
}
public CuratorFramework createCuratorClient() {
final RetryPolicy retryPolicy = new RetryNTimes(20, 500);
final CuratorFramework curatorClient = CuratorFrameworkFactory.builder().connectString(getZooKeeperConnectString()).sessionTimeoutMs(3000).connectionTimeoutMs(3000).retryPolicy(retryPolicy).defaultData(new byte[0]).build();
curatorClient.start();
return curatorClient;
}
public Node createNode() {
final Map<String, String> addProps = new HashMap<>();
addProps.put(NiFiProperties.ZOOKEEPER_CONNECT_STRING, getZooKeeperConnectString());
addProps.put(NiFiProperties.CLUSTER_IS_NODE, "true");
final NiFiProperties nifiProperties = NiFiProperties.createBasicNiFiProperties("src/test/resources/conf/nifi.properties", addProps);
final FingerprintFactory fingerprintFactory = new FingerprintFactory(StringEncryptor.createEncryptor(nifiProperties));
final FlowElection flowElection = new PopularVoteFlowElection(flowElectionTimeoutMillis, TimeUnit.MILLISECONDS, flowElectionMaxNodes, fingerprintFactory);
final Node node = new Node(nifiProperties, flowElection);
node.start();
nodes.add(node);
return node;
}
public Node waitForClusterCoordinator(final long time, final TimeUnit timeUnit) {
return ClusterUtils.waitUntilNonNull(time, timeUnit, () -> getNodes().stream().filter(node -> node.hasRole(ClusterRoles.CLUSTER_COORDINATOR)).findFirst().orElse(null));
}
public Node waitForPrimaryNode(final long time, final TimeUnit timeUnit) {
return ClusterUtils.waitUntilNonNull(time, timeUnit, () -> getNodes().stream().filter(node -> node.hasRole(ClusterRoles.PRIMARY_NODE)).findFirst().orElse(null));
}
/**
* Waits for each node in the cluster to connect. The time given is the maximum amount of time to wait for each node to connect, not for
* the entire cluster to connect.
*
* @param time the max amount of time to wait for a node to connect
* @param timeUnit the unit of time that the given <code>time</code> value represents
*/
public void waitUntilAllNodesConnected(final long time, final TimeUnit timeUnit) {
for (final Node node : nodes) {
node.waitUntilConnected(time, timeUnit);
}
}
}
18
Source : NestedZkUtils.java
with Apache License 2.0
from vipshop
with Apache License 2.0
from vipshop
/**
* @author dylan.xue
*/
public clreplaced NestedZkUtils {
static Logger log = LoggerFactory.getLogger(NestedZkUtils.clreplaced);
private TestingServer testingServer;
public int PORT = 3181;
public void startServer() throws Exception {
try (ServerSocket socket = new ServerSocket(0)) {
PORT = socket.getLocalPort();
} catch (IOException e) {
}
System.err.println("zkTestServer starting. Port: " + PORT);
log.error("zk starts at: {}", PORT);
testingServer = new TestingServer(PORT);
}
public void stopServer() throws IOException {
if (testingServer != null) {
testingServer.stop();
}
}
public void startStoppedServer() throws Exception {
testingServer.start();
}
public void reStartServer() throws Exception {
if (testingServer != null) {
testingServer.restart();
}
}
public boolean isStarted() {
return testingServer != null;
}
public String getZkString() {
return "127.0.0.1:" + PORT;
}
public void killSession(ZooKeeper client) throws Exception {
KillSession.kill(client, getZkString());
}
public CuratorFramework createClient(String namespace) throws InterruptedException {
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
CuratorFramework curatorFramework = builder.connectString("127.0.0.1:" + PORT).sessionTimeoutMs(// long long, could to debug
600 * 1000).retryPolicy(new RetryNTimes(3, 1000)).namespace(namespace).build();
curatorFramework.start();
curatorFramework.blockUntilConnected();
return curatorFramework;
}
}
18
Source : NestedZookeeperServers.java
with Apache License 2.0
from vipshop
with Apache License 2.0
from vipshop
/**
* 启动内嵌的Zookeeper服务.
*
* @param port 端口号
* @param dataDir
*
* <p>
* 如果该端口号的Zookeeper服务未启动, 则启动服务. 如果该端口号的Zookeeper服务已启动, 则不做任何操作.
* </p>
*/
public synchronized void startServerIfNotStarted(final int port, final String dataDir) {
if (!nestedServers.containsKey(port)) {
TestingServer testingServer = null;
try {
testingServer = new TestingServer(port, new File(dataDir));
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
RegExceptionHandler.handleException(ex);
}
if (testingServer != null) {
nestedServers.putIfAbsent(port, testingServer);
}
}
}
18
Source : ZookeeperConfigStoreTest.java
with Apache License 2.0
from vert-x3
with Apache License 2.0
from vert-x3
/**
* Check the behavior of {@link ZookeeperConfigStore}.
*
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
@RunWith(VertxUnitRunner.clreplaced)
public clreplaced ZookeeperConfigStoreTest {
private ConfigRetriever retriever;
private Vertx vertx;
private TestingServer server;
private CuratorFramework client;
@Before
public void setUp(TestContext tc) throws Exception {
server = new TestingServer(2181);
client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(2000));
client.start();
vertx = Vertx.vertx();
vertx.exceptionHandler(tc.exceptionHandler());
}
@After
public void tearDown(TestContext tc) throws IOException {
retriever.close();
client.close();
vertx.close(tc.asyncreplacedertSuccess());
server.close();
}
@Test
public void getConfigurationFromZookeeper(TestContext tc) throws Exception {
Async async = tc.async();
retriever = ConfigRetriever.create(vertx, new ConfigRetrieverOptions().addStore(new ConfigStoreOptions().setType("zookeeper").setConfig(new JsonObject().put("connection", server.getConnectString()).put("path", "/config/A"))));
retriever.getConfig(json -> {
replacedertThat(json.succeeded()).isTrue();
JsonObject config = json.result();
tc.replacedertTrue(config.isEmpty());
writeSomeConf("/config/A", true, ar -> {
tc.replacedertTrue(ar.succeeded());
retriever.getConfig(json2 -> {
replacedertThat(json2.succeeded()).isTrue();
JsonObject config2 = json2.result();
tc.replacedertTrue(!config2.isEmpty());
tc.replacedertEquals(config2.getString("some-key"), "some-value");
async.complete();
});
});
});
}
@Test
public void testUpdateAndRemovalOfConfiguration(TestContext tc) throws Exception {
Async async = tc.async();
writeSomeConf("/config/A", true, ar -> {
retriever = ConfigRetriever.create(vertx, new ConfigRetrieverOptions().addStore(new ConfigStoreOptions().setType("zookeeper").setConfig(new JsonObject().put("connection", server.getConnectString()).put("path", "/config/A"))));
retriever.getConfig(json2 -> {
replacedertThat(json2.succeeded()).isTrue();
JsonObject config2 = json2.result();
tc.replacedertTrue(!config2.isEmpty());
tc.replacedertEquals(config2.getString("some-key"), "some-value");
// Update the conf
writeSomeConf("/config/A", false, update -> {
retriever.getConfig(json3 -> {
replacedertThat(json3.succeeded()).isTrue();
JsonObject config3 = json3.result();
tc.replacedertTrue(!config3.isEmpty());
tc.replacedertEquals(config3.getString("some-key"), "some-value-2");
// Delete
delete("/config/A", deletion -> {
retriever.getConfig(json4 -> {
replacedertThat(json4.succeeded()).isTrue();
JsonObject config4 = json4.result();
tc.replacedertTrue(config4.isEmpty());
async.complete();
});
});
});
});
});
});
}
private void delete(String path, Handler<AsyncResult<Void>> handler) {
Context context = vertx.getOrCreateContext();
try {
client.delete().deletingChildrenIfNeeded().inBackground((client, event) -> {
context.runOnContext(v -> handler.handle(Future.succeededFuture()));
}).forPath(path);
} catch (Exception e) {
handler.handle(Future.failedFuture(e));
}
}
private void writeSomeConf(String path, boolean create, Handler<AsyncResult<Void>> handler) {
Context context = vertx.getOrCreateContext();
try {
if (create) {
JsonObject conf = new JsonObject().put("some-key", "some-value");
client.create().creatingParentsIfNeeded().inBackground((client, event) -> dataWrittenCallback(handler, context, event)).forPath(path, conf.encode().getBytes());
} else {
JsonObject conf = new JsonObject().put("some-key", "some-value-2");
client.setData().inBackground((client, event) -> dataWrittenCallback(handler, context, event)).forPath(path, conf.encode().getBytes());
}
} catch (Exception e) {
handler.handle(Future.failedFuture(e));
}
}
private void dataWrittenCallback(Handler<AsyncResult<Void>> handler, Context context, CuratorEvent event) {
context.runOnContext((x) -> {
if (event.getResultCode() == 0) {
handler.handle(Future.succeededFuture());
} else {
handler.handle(Future.failedFuture(KeeperException.create(KeeperException.Code.get(event.getResultCode()))));
}
});
}
}
18
Source : ZooKeeperEmbedded.java
with Apache License 2.0
from tchiotludo
with Apache License 2.0
from tchiotludo
/**
* Runs an in-memory, "embedded" instance of a ZooKeeper server.
*
* The ZooKeeper server instance is automatically started when you create a new instance of this clreplaced.
*/
@Slf4j
public clreplaced ZooKeeperEmbedded {
private final TestingServer server;
static {
System.setProperty("zookeeper.admin.enableServer", "false");
}
/**
* Creates and starts a ZooKeeper instance.
*/
public ZooKeeperEmbedded() throws Exception {
log.debug("Starting embedded ZooKeeper server...");
this.server = new TestingServer();
log.debug("Embedded ZooKeeper server at {} uses the temp directory at {}", server.getConnectString(), server.getTempDirectory());
}
public void stop() throws IOException {
log.debug("Shutting down embedded ZooKeeper server at {} ...", server.getConnectString());
server.close();
log.debug("Shutdown of embedded ZooKeeper server at {} completed", server.getConnectString());
}
/**
* The ZooKeeper connection string aka `zookeeper.connect` in `hostnameOrIp:port` format.
* Example: `127.0.0.1:2181`.
*
* You can use this to e.g. tell Kafka brokers how to connect to this instance.
*/
public String connectString() {
return server.getConnectString();
}
/**
* The hostname of the ZooKeeper instance. Example: `127.0.0.1`
*/
public String hostname() {
// "server:1:2:3" -> "server:1:2"
return connectString().substring(0, connectString().lastIndexOf(':'));
}
}
See More Examples