use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.
the class ShardingIT method test_P_PersistShardingContentIfNecessary.
/**
* NamespaceShardingService is not necessary to persist the sharding result content that is not changed<br/>
* https://github.com/vipshop/Saturn/issues/88
*/
@Test
public void test_P_PersistShardingContentIfNecessary() throws Exception {
// 启动1个executor
Main executor1 = startOneNewExecutorList();
Thread.sleep(1000);
// 启动第一个作业
Thread.sleep(1000);
String jobName1 = "test_P_PersistShardingContentIfNecessary";
final JobConfiguration jobConfiguration1 = new JobConfiguration(jobName1);
jobConfiguration1.setCron("* * 1 * * ?");
jobConfiguration1.setJobType(JobType.JAVA_JOB.toString());
jobConfiguration1.setJobClass(SimpleJavaJob.class.getCanonicalName());
jobConfiguration1.setShardingTotalCount(1);
jobConfiguration1.setShardingItemParameters("0=0");
jobConfiguration1.setPreferList("abc");
jobConfiguration1.setUseDispreferList(false);
addJob(jobConfiguration1);
Thread.sleep(1000);
enableJob(jobName1);
waitForFinish(new FinishCheck() {
@Override
public boolean docheck() {
return isNeedSharding(jobConfiguration1);
}
}, 10);
runAtOnce(jobName1);
waitForFinish(new FinishCheck() {
@Override
public boolean docheck() {
return !isNeedSharding(jobConfiguration1);
}
}, 10);
long mtime = ((CuratorFramework) regCenter.getRawClient()).checkExists().forPath(SaturnExecutorsNode.getShardingContentElementNodePath("0")).getMtime();
// 禁用作业
Thread.sleep(1000);
disableJob(jobName1);
Thread.sleep(1000);
waitForFinish(new FinishCheck() {
@Override
public boolean docheck() {
return !isNeedSharding(jobConfiguration1);
}
}, 10);
long mtime2 = ((CuratorFramework) regCenter.getRawClient()).checkExists().forPath(SaturnExecutorsNode.getShardingContentElementNodePath("0")).getMtime();
assertThat(mtime).isEqualTo(mtime2);
stopExecutorList();
forceRemoveJob(jobName1);
}
use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.
the class NamespaceShardingServiceTest method leadershipElectionTest.
@Test
public void leadershipElectionTest() throws Exception {
String namespace = "MyNamespace";
CuratorFramework curatorFramework = nestedZkUtils.createClient(namespace);
// 启动第一个
String ip1 = "127.0.0.1";
CuratorFramework curatorFramework1 = nestedZkUtils.createClient(namespace);
NamespaceShardingManager namespaceShardingManager1 = new NamespaceShardingManager(curatorFramework1, namespace, ip1, null, null);
namespaceShardingManager1.start();
Thread.sleep(1000);
// 验证leadership
assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isEqualTo(ip1);
// 启动第二个
String ip2 = "127.0.0.2";
CuratorFramework curatorFramework2 = nestedZkUtils.createClient(namespace);
NamespaceShardingManager namespaceShardingManager2 = new NamespaceShardingManager(curatorFramework2, namespace, ip2, null, null);
namespaceShardingManager2.start();
Thread.sleep(1000);
// 验证leadership
assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isEqualTo(ip1);
// 启动第三个
String ip3 = "127.0.0.3";
CuratorFramework curatorFramework3 = nestedZkUtils.createClient(namespace);
NamespaceShardingManager namespaceShardingManager3 = new NamespaceShardingManager(curatorFramework3, namespace, ip3, null, null);
namespaceShardingManager3.start();
Thread.sleep(1000);
// 验证leadership
assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isEqualTo(ip1);
// 停止第一个
namespaceShardingManager1.stopWithCurator();
Thread.sleep(1000);
// 验证leadership
assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isIn(ip2, ip3);
// 停止第二个
namespaceShardingManager2.stopWithCurator();
Thread.sleep(1000);
// 验证leadership
assertThat(new String(curatorFramework.getData().forPath(SaturnExecutorsNode.LEADER_HOSTNODE_PATH), "UTF-8")).isEqualTo(ip3);
}
use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.
the class NestedZkUtils method createClient.
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;
}
use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.
the class InitSaturnJob method main.
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
String zkConnection = System.getProperty("VIP_SATURN_ZK_CONNECTION");
System.out.println("zkConnection=" + zkConnection);
if (zkConnection == null || "".equals(zkConnection.trim())) {
zkConnection = "localhost:2182";
}
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkConnection).namespace("saturn-it.vip.com").sessionTimeoutMs(10000).retryPolicy(retryPolicy).build();
client.start();
addJavaJob(client, "demoJavaJob");
System.out.println("done add a java-job.");
addShellJob(client, "demoShellJob");
System.out.println("done add a shell-job.");
}
use of org.apache.curator.framework.CuratorFramework in project Saturn by vipshop.
the class CuratorRepositoryImpl method connect.
@Override
public CuratorFramework connect(final String connectString, final String namespace, final String digest) {
Builder builder = CuratorFrameworkFactory.builder().connectString(connectString).sessionTimeoutMs(SESSION_TIMEOUT).connectionTimeoutMs(CONNECTION_TIMEOUT).retryPolicy(new ExponentialBackoffRetry(1000, 3, 3000));
if (namespace != null) {
builder.namespace(namespace);
}
if (!Strings.isNullOrEmpty(digest)) {
builder.authorization("digest", digest.getBytes()).aclProvider(new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
@Override
public List<ACL> getAclForPath(final String path) {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
});
}
CuratorFramework client = builder.build();
client.start();
boolean established = false;
try {
established = client.blockUntilConnected(WAITING_SECONDS, TimeUnit.SECONDS);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (established) {
return client;
}
CloseableUtils.closeQuietly(client);
return null;
}
Aggregations