Skip to content

Commit

Permalink
fix #93
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 19, 2016
1 parent a45d9a6 commit 31e2359
Show file tree
Hide file tree
Showing 23 changed files with 199 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final St
if (registryCenterMap.containsKey(hashCode)) {
return registryCenterMap.get(hashCode);
}
ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace, 1000, 3000, 3);
ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
if (digest.isPresent()) {
zkConfig.setDigest(digest.get());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.job;

import com.dangdang.ddframe.reg.zookeeper.NestedZookeeperServers;
import com.google.common.base.Joiner;
import org.junit.Before;

public abstract class AbstractNestedZookeeperBaseTest {

public static final int PORT = 3181;

public static final String TEST_TEMP_DIRECTORY = String.format("target/test_zk_data/%s/", System.nanoTime());

public static final String ZK_CONNECTION_STRING = Joiner.on(":").join("localhost", PORT);

@Before
public void setUp() {
NestedZookeeperServers.getInstance().startServerIfNotStarted(PORT, TEST_TEMP_DIRECTORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import com.dangdang.ddframe.job.internal.settings.JobSettingsAPIImplTest;
import com.dangdang.ddframe.job.internal.statistics.JobStatisticsAPIImplTest;
import com.dangdang.ddframe.job.internal.statistics.ServerStatisticsAPIImplTest;
import com.dangdang.ddframe.reg.zookeeper.NestedZookeeperServers;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
Expand All @@ -45,5 +49,11 @@
JobStatisticsAPIImplTest.class,
ServerStatisticsAPIImplTest.class
})
public class AllTests {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AllTests {

@AfterClass
public static void clear() {
NestedZookeeperServers.getInstance().closeServer(AbstractNestedZookeeperBaseTest.PORT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,32 @@

package com.dangdang.ddframe.job.api;

import com.dangdang.ddframe.job.AbstractNestedZookeeperBaseTest;
import com.google.common.base.Optional;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;

public final class JobAPIFactoryTest {
public final class JobAPIFactoryTest extends AbstractNestedZookeeperBaseTest {

@Test
public void assertCreateJobSettingsAPI() {
assertThat(JobAPIFactory.createJobSettingsAPI("zkStr", "namespace", Optional.<String>absent()), instanceOf(JobSettingsAPI.class));
assertThat(JobAPIFactory.createJobSettingsAPI(ZK_CONNECTION_STRING, "namespace", Optional.<String>absent()), instanceOf(JobSettingsAPI.class));
}

@Test
public void assertCreateJobOperateAPI() {
assertThat(JobAPIFactory.createJobOperateAPI("zkStr", "namespace", Optional.<String>absent()), instanceOf(JobOperateAPI.class));
assertThat(JobAPIFactory.createJobOperateAPI(ZK_CONNECTION_STRING, "namespace", Optional.<String>absent()), instanceOf(JobOperateAPI.class));
}

@Test
public void assertCreateJobStatisticsAPI() {
assertThat(JobAPIFactory.createJobStatisticsAPI("zkStr", "namespace", Optional.<String>absent()), instanceOf(JobStatisticsAPI.class));
assertThat(JobAPIFactory.createJobStatisticsAPI(ZK_CONNECTION_STRING, "namespace", Optional.<String>absent()), instanceOf(JobStatisticsAPI.class));
}

@Test
public void assertCreateServerStatisticsAPI() {
assertThat(JobAPIFactory.createServerStatisticsAPI("zkStr", "namespace", Optional.<String>absent()), instanceOf(ServerStatisticsAPI.class));
assertThat(JobAPIFactory.createServerStatisticsAPI(ZK_CONNECTION_STRING, "namespace", Optional.<String>absent()), instanceOf(ServerStatisticsAPI.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.dangdang.ddframe.job.internal.reg;

import com.dangdang.ddframe.job.AbstractNestedZookeeperBaseTest;
import com.dangdang.ddframe.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.reg.zookeeper.ZookeeperRegistryCenter;
Expand All @@ -29,26 +30,26 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

public final class RegistryCenterFactoryTest {
public final class RegistryCenterFactoryTest extends AbstractNestedZookeeperBaseTest {

@Test
public void assertCreateCoordinatorRegistryCenterWithoutDigest() throws ReflectiveOperationException {
ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter("zkStr", "namespace", Optional.<String>absent()));
ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(ZK_CONNECTION_STRING, "namespace", Optional.<String>absent()));
assertThat(zkConfig.getNamespace(), is("namespace"));
assertNull(zkConfig.getDigest());
}

@Test
public void assertCreateCoordinatorRegistryCenterWithDigest() throws ReflectiveOperationException {
ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter("zkStr", "namespace", Optional.of("digest")));
ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(ZK_CONNECTION_STRING, "namespace", Optional.of("digest")));
assertThat(zkConfig.getNamespace(), is("namespace"));
assertThat(zkConfig.getDigest(), is("digest"));
}

@Test
public void assertCreateCoordinatorRegistryCenterFromCache() throws ReflectiveOperationException {
RegistryCenterFactory.createCoordinatorRegistryCenter("zkStr", "otherNamespace", Optional.<String>absent());
ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter("zkStr", "otherNamespace", Optional.<String>absent()));
RegistryCenterFactory.createCoordinatorRegistryCenter(ZK_CONNECTION_STRING, "otherNamespace", Optional.<String>absent());
ZookeeperConfiguration zkConfig = getZookeeperConfiguration(RegistryCenterFactory.createCoordinatorRegistryCenter(ZK_CONNECTION_STRING, "otherNamespace", Optional.<String>absent()));
assertThat(zkConfig.getNamespace(), is("otherNamespace"));
assertNull(zkConfig.getDigest());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

package com.dangdang.ddframe.job.console.controller;

import java.util.Collection;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.dangdang.ddframe.job.console.domain.RegistryCenterConfiguration;
import com.dangdang.ddframe.job.console.service.RegistryCenterService;
import com.dangdang.ddframe.job.internal.reg.RegistryCenterFactory;
import com.dangdang.ddframe.reg.exception.RegException;
import com.google.common.base.Optional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dangdang.ddframe.job.console.domain.RegistryCenterConfiguration;
import com.dangdang.ddframe.job.console.service.RegistryCenterService;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Collection;

@RestController
@RequestMapping("registry_center")
Expand Down Expand Up @@ -65,6 +65,11 @@ public boolean connect(final RegistryCenterConfiguration config, final HttpSessi

private boolean setRegistryCenterNameToSession(final RegistryCenterConfiguration regCenterConfig, final HttpSession session) {
session.setAttribute(REG_CENTER_CONFIG_KEY, regCenterConfig);
try {
RegistryCenterFactory.createCoordinatorRegistryCenter(regCenterConfig.getZkAddressList(), regCenterConfig.getNamespace(), Optional.fromNullable(regCenterConfig.getDigest()));
} catch (final RegException ex) {
return false;
}
return true;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ public class ZookeeperConfiguration extends AbstractRegistryCenterConfiguration
* 等待重试的间隔时间的初始值.
* 单位毫秒.
*/
private int baseSleepTimeMilliseconds;
private int baseSleepTimeMilliseconds = 1000;

/**
* 等待重试的间隔时间的最大值.
* 单位毫秒.
*/
private int maxSleepTimeMilliseconds;
private int maxSleepTimeMilliseconds = 3000;

/**
* 最大重试次数.
*/
private int maxRetries;
private int maxRetries = 3;

/**
* 会话超时时间.
Expand Down Expand Up @@ -94,6 +94,17 @@ public class ZookeeperConfiguration extends AbstractRegistryCenterConfiguration
*/
private String nestedDataDir;

/**
* 包含了必需属性的构造器.
*
* @param serverLists 连接Zookeeper服务器的列表
* @param namespace 命名空间
*/
public ZookeeperConfiguration(final String serverLists, final String namespace) {
this.serverLists = serverLists;
this.namespace = namespace;
}

/**
* 包含了必需属性的构造器.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@

package com.dangdang.ddframe.reg.zookeeper;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import com.dangdang.ddframe.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.reg.exception.LocalPropertiesFileNotFoundException;
import com.dangdang.ddframe.reg.exception.RegExceptionHandler;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.CuratorFrameworkFactory.Builder;
Expand All @@ -38,18 +34,21 @@
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.CloseableUtils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;

import com.dangdang.ddframe.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.reg.exception.LocalPropertiesFileNotFoundException;
import com.dangdang.ddframe.reg.exception.RegExceptionHandler;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
* 基于Zookeeper的注册中心.
Expand Down Expand Up @@ -105,6 +104,9 @@ public List<ACL> getAclForPath(final String path) {
client.start();
try {
client.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds(), TimeUnit.MILLISECONDS);
if (!client.getZookeeperClient().isConnected()) {
throw new KeeperException.OperationTimeoutException();
}
if (!Strings.isNullOrEmpty(zkConfig.getLocalPropertiesPath())) {
fillData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

public abstract class AbstractBaseStdJobTest extends AbstractNestedZookeeperBaseTest {

private static ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(ZK_CONNECTION_STRING, "zkRegTestCenter", 1000, 3000, 3);
private static ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(ZK_CONNECTION_STRING, "zkRegTestCenter");

@Getter(value = AccessLevel.PROTECTED)
private static CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zkConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.dangdang.ddframe.reg;

import com.dangdang.ddframe.reg.zookeeper.ZookeeperRegistryCenterInitFailureTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
Expand Down Expand Up @@ -45,7 +46,8 @@
ZookeeperRegistryCenterMiscellaneousTest.class,
ZookeeperRegistryCenterNestedTest.class,
RegExceptionHandlerTest.class,
LocalPropertiesFileNotFoundExceptionTest.class
LocalPropertiesFileNotFoundExceptionTest.class,
ZookeeperRegistryCenterInitFailureTest.class
})
public final class AllRegTests {
}
Loading

0 comments on commit 31e2359

Please sign in to comment.