Skip to content

Commit bf5c238

Browse files
committed
credis api changed
1 parent 9eb716f commit bf5c238

File tree

6 files changed

+79
-21
lines changed

6 files changed

+79
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.ctrip.xpipe.utils;
2+
3+
/**
4+
* @author wenchao.meng
5+
*
6+
* Mar 21, 2017
7+
*/
8+
public class UrlUtils {
9+
10+
public static String format(String url) {
11+
12+
int index = url.indexOf("://");
13+
if (index == -1) {
14+
return url.replaceAll("/+", "/");
15+
}
16+
17+
String prefix = url.substring(0, index + 3);
18+
String following = url.substring(index + 3);
19+
20+
return prefix + following.replaceAll("/+", "/");
21+
22+
}
23+
}

core/src/test/java/com/ctrip/xpipe/AllTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.ctrip.xpipe.utils.OffsetNotifierTest;
3030
import com.ctrip.xpipe.utils.SizeControllableFileTest;
3131
import com.ctrip.xpipe.utils.StringUtilTest;
32+
import com.ctrip.xpipe.utils.UrlUtilsTest;
3233
import com.ctrip.xpipe.zk.impl.TestZkClientTest;
3334

3435
/**
@@ -63,7 +64,8 @@
6364
ChannelUtilTest.class,
6465
KeyedOneThreadTaskExecutorTest.class,
6566
DefaultControllableFileTest.class,
66-
SizeControllableFileTest.class
67+
SizeControllableFileTest.class,
68+
UrlUtilsTest.class
6769
})
6870
public class AllTests {
6971

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.ctrip.xpipe.utils;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.ctrip.xpipe.AbstractTest;
7+
8+
/**
9+
* @author wenchao.meng
10+
*
11+
* Mar 21, 2017
12+
*/
13+
public class UrlUtilsTest extends AbstractTest{
14+
15+
@Test
16+
public void testFormat(){
17+
18+
Assert.assertEquals("http://localhost/a/b/c", UrlUtils.format("http://localhost/a/b/c"));
19+
Assert.assertEquals("localhost/a/b/c", UrlUtils.format("localhost/a/b/c"));
20+
21+
Assert.assertEquals("http://localhost/a/b/c", UrlUtils.format("http://localhost////a/b//c"));
22+
Assert.assertEquals("localhost/a/b/c", UrlUtils.format("localhost///a/b//c"));
23+
}
24+
25+
}

services/ctrip-service/src/main/java/com/ctrip/xpipe/service/migration/CREDIS_SERVICE.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.ctrip.xpipe.service.migration;
22

3+
import com.ctrip.xpipe.utils.UrlUtils;
4+
35
/**
46
* @author shyin
57
*
@@ -23,11 +25,11 @@ public String getRealPath(String host) {
2325
if (!host.startsWith("http")) {
2426
host += "http://";
2527
}
26-
return String.format("%s/%s/%s", host, PATH.PATH_PREFIX, getPath());
28+
return UrlUtils.format(String.format("%s/%s/%s", host, PATH.PATH_PREFIX, getPath()));
2729
}
2830

2931
public static class PATH {
30-
public static final String PATH_PREFIX = "/credis";
32+
public static final String PATH_PREFIX = "/";
3133

3234
public static final String PATH_MIGRATION_PUBLISH = "/KeeperApi/primarydc/{clusterName}/{primaryDcName}";
3335
}

services/ctrip-service/src/main/java/com/ctrip/xpipe/service/migration/CredisMigrationPublishService.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ public int getOrder() {
2727

2828
@Override
2929
public MigrationPublishResult doMigrationPublish(String clusterName, String primaryDcName, List<InetSocketAddress> newMasters) {
30+
3031
logger.info("[doMigrationPublish]Cluster:{}, NewPrimaryDc:{} -> ConvertedDcName:{} , NewMasters:{}", clusterName, primaryDcName,convertDcName(primaryDcName), newMasters);
32+
33+
String credisAddress = CREDIS_SERVICE.MIGRATION_PUBLISH.getRealPath(MigrationPublishServiceConfig.INSTANCE.getCredisServiceAddress());
3134
String startTime = sdf.format(new Date());
32-
MigrationPublishResult res = restOperations.postForObject(
33-
CREDIS_SERVICE.MIGRATION_PUBLISH.getRealPath(MigrationPublishServiceConfig.INSTANCE.getCredisServiceAddress()),
35+
MigrationPublishResult res = restOperations.postForObject(credisAddress,
3436
newMasters, MigrationPublishResult.class, clusterName, convertDcName(primaryDcName));
3537
String endTime = sdf.format(new Date());
36-
res.setPublishAddress(CREDIS_SERVICE.MIGRATION_PUBLISH.getRealPath(MigrationPublishServiceConfig.INSTANCE.getCredisServiceAddress()));
38+
res.setPublishAddress(credisAddress);
3739
res.setClusterName(clusterName);
3840
res.setPrimaryDcName(primaryDcName);
3941
res.setNewMasters(newMasters);
@@ -45,19 +47,7 @@ public MigrationPublishResult doMigrationPublish(String clusterName, String prim
4547
@Override
4648
public MigrationPublishResult doMigrationPublish(String clusterName, String shardName, String primaryDcName,
4749
InetSocketAddress newMaster) {
48-
logger.info("[doMigrationPublish]Cluster:{}, NewPrimaryDc:{} -> ConvertedDcName:{}, NewMaster:{}", clusterName, primaryDcName,convertDcName(primaryDcName), newMaster);
49-
String startTime = sdf.format(new Date());
50-
MigrationPublishResult res = restOperations.postForObject(
51-
CREDIS_SERVICE.MIGRATION_PUBLISH.getRealPath(MigrationPublishServiceConfig.INSTANCE.getCredisServiceAddress()),
52-
Arrays.asList(newMaster), MigrationPublishResult.class, clusterName, convertDcName(primaryDcName));
53-
String endTime = sdf.format(new Date());
54-
res.setPublishAddress(CREDIS_SERVICE.MIGRATION_PUBLISH.getRealPath(MigrationPublishServiceConfig.INSTANCE.getCredisServiceAddress()));
55-
res.setClusterName(clusterName);
56-
res.setPrimaryDcName(primaryDcName);
57-
res.setNewMasters(Arrays.asList(newMaster));
58-
res.setStartTime(startTime);
59-
res.setEndTime(endTime);
60-
return res;
50+
return doMigrationPublish(clusterName, primaryDcName, Arrays.asList(newMaster));
6151
}
6252

6353
String convertDcName(String dc) {

services/ctrip-service/src/test/java/com/ctrip/xpipe/service/migration/CredisMigrationPublishServiceTest.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import java.io.IOException;
44
import java.net.InetSocketAddress;
5-
import java.util.Arrays;
5+
import java.util.LinkedList;
6+
import java.util.List;
67

78
import org.junit.Assert;
89
import org.junit.Test;
910

1011
import com.ctrip.xpipe.AbstractServiceTest;
1112
import com.ctrip.xpipe.api.migration.MigrationPublishService;
13+
import com.ctrip.xpipe.api.migration.MigrationPublishService.MigrationPublishResult;
1214

1315
/**
1416
* @author shyin
@@ -19,14 +21,28 @@ public class CredisMigrationPublishServiceTest extends AbstractServiceTest {
1921

2022
@Test
2123
public void testCredisMigrationPublishService() throws IOException {
24+
2225
MigrationPublishService publishService = MigrationPublishService.DEFAULT;
26+
2327
Assert.assertTrue(publishService instanceof CredisMigrationPublishService);
2428

25-
System.out.println(publishService.doMigrationPublish("cluster", "dc", Arrays.asList(InetSocketAddress.createUnresolved("127.0.0.1", 6379))));
29+
List<InetSocketAddress> newMasters = new LinkedList<>();
30+
31+
// newMasters.add(new InetSocketAddress("10.2.58.242", 6379));
32+
// newMasters.add(new InetSocketAddress("10.2.58.243", 6389));
33+
// MigrationPublishResult result = publishService.doMigrationPublish("cluster_shyin", "SHAJQ", newMasters);
34+
35+
newMasters.add(new InetSocketAddress("10.3.2.23", 6379));
36+
newMasters.add(new InetSocketAddress("10.3.2.23", 6389));
37+
38+
MigrationPublishResult result = publishService.doMigrationPublish("cluster_shyin", "SHAOY", newMasters);
39+
40+
logger.info("[testCredisMigrationPublishService]{}", result);
2641
}
2742

2843
@Test
2944
public void testConvertDcName() {
45+
3046
Assert.assertEquals("SHAJQ", ((CredisMigrationPublishService)MigrationPublishService.DEFAULT).convertDcName("ntgxh"));
3147
Assert.assertEquals("SHAJQ", ((CredisMigrationPublishService)MigrationPublishService.DEFAULT).convertDcName("NTGXH"));
3248
Assert.assertEquals("SHAOY", ((CredisMigrationPublishService)MigrationPublishService.DEFAULT).convertDcName("fat"));

0 commit comments

Comments
 (0)