Extract compareTo impl to Router interface and concrete Router only responsible for provide priority. (#3240)
something is waiting for us to disscuss: 1. Every Route implement should set a priority? 2.https://github.com/apache/incubator-dubbo/issues/3249
This commit is contained in:
parent
8c2fbc8ce6
commit
e24e568756
|
|
@ -85,4 +85,22 @@ public interface Router extends Comparable<Router> {
|
|||
* @return router's priority
|
||||
*/
|
||||
int getPriority();
|
||||
|
||||
@Override
|
||||
default int compareTo(Router o) {
|
||||
if (o == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (this.getPriority() == o.getPriority()) {
|
||||
if (o.getUrl() == null) {
|
||||
return 1;
|
||||
}
|
||||
if (getUrl() == null) {
|
||||
return -1;
|
||||
}
|
||||
return getUrl().toFullString().compareTo(o.getUrl().toFullString());
|
||||
} else {
|
||||
return getPriority() > o.getPriority() ? 1 : -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,9 +20,6 @@ import org.apache.dubbo.common.URL;
|
|||
import org.apache.dubbo.configcenter.DynamicConfiguration;
|
||||
import org.apache.dubbo.rpc.cluster.Router;
|
||||
|
||||
/**
|
||||
* TODO Extract more code to here if necessary
|
||||
*/
|
||||
public abstract class AbstractRouter implements Router {
|
||||
protected int priority;
|
||||
protected boolean force = false;
|
||||
|
|
@ -65,11 +62,6 @@ public abstract class AbstractRouter implements Router {
|
|||
this.force = force;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Router o) {
|
||||
return (this.getPriority() >= o.getPriority()) ? 1 : -1;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ import org.apache.dubbo.common.utils.CollectionUtils;
|
|||
import org.apache.dubbo.rpc.Invocation;
|
||||
import org.apache.dubbo.rpc.Invoker;
|
||||
import org.apache.dubbo.rpc.RpcException;
|
||||
import org.apache.dubbo.rpc.cluster.Router;
|
||||
import org.apache.dubbo.rpc.cluster.router.AbstractRouter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -95,15 +94,9 @@ public class MockInvokersSelector extends AbstractRouter {
|
|||
return hasMockProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Always stay on the top of the list
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Router o) {
|
||||
return 1;
|
||||
public int getPriority() {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import org.apache.dubbo.rpc.Invocation;
|
|||
import org.apache.dubbo.rpc.Invoker;
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
import org.apache.dubbo.rpc.RpcException;
|
||||
import org.apache.dubbo.rpc.cluster.Router;
|
||||
import org.apache.dubbo.rpc.cluster.router.AbstractRouter;
|
||||
|
||||
import javax.script.Bindings;
|
||||
|
|
@ -120,4 +121,13 @@ public class ScriptRouter extends AbstractRouter {
|
|||
public boolean isForce() {
|
||||
return url.getParameter(Constants.FORCE_KEY, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Router o) {
|
||||
if (o == null || o.getClass() != ScriptRouter.class) {
|
||||
return 1;
|
||||
}
|
||||
ScriptRouter c = (ScriptRouter) o;
|
||||
return this.priority == c.priority ? rule.compareTo(c.rule) : (this.priority > c.priority ? 1 : -1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ import java.util.List;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
@Deprecated
|
||||
public interface Router extends org.apache.dubbo.rpc.cluster.Router {
|
||||
public interface Router extends org.apache.dubbo.rpc.cluster.Router{
|
||||
|
||||
@Override
|
||||
com.alibaba.dubbo.common.URL getUrl();
|
||||
|
|
@ -36,7 +36,7 @@ public interface Router extends org.apache.dubbo.rpc.cluster.Router {
|
|||
com.alibaba.dubbo.rpc.Invocation invocation)
|
||||
throws com.alibaba.dubbo.rpc.RpcException;
|
||||
|
||||
int compareTo(com.alibaba.dubbo.rpc.cluster.Router o);
|
||||
int compareTo(Router o);
|
||||
|
||||
// Add since 2.7.0
|
||||
@Override
|
||||
|
|
@ -65,7 +65,11 @@ public interface Router extends org.apache.dubbo.rpc.cluster.Router {
|
|||
}
|
||||
|
||||
@Override
|
||||
default int compareTo(org.apache.dubbo.rpc.cluster.Router o) {
|
||||
return compareTo((Router) o);
|
||||
default int compareTo (org.apache.dubbo.rpc.cluster.Router o) {
|
||||
if (!(o instanceof Router)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return this.compareTo((Router)o);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import java.util.List;
|
|||
*
|
||||
*/
|
||||
public class CompatibleRouter implements Router {
|
||||
|
||||
@Override
|
||||
public URL getUrl() {
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package org.apache.dubbo.rpc.cluster;
|
||||
|
||||
import com.alibaba.dubbo.common.URL;
|
||||
import com.alibaba.dubbo.rpc.Invocation;
|
||||
import com.alibaba.dubbo.rpc.Invoker;
|
||||
import com.alibaba.dubbo.rpc.RpcException;
|
||||
import com.alibaba.dubbo.rpc.cluster.Router;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class CompatibleRouter2 implements Router {
|
||||
@Override
|
||||
public URL getUrl() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Router o) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package org.apache.dubbo.rpc.cluster;
|
||||
|
||||
import org.apache.dubbo.common.URL;
|
||||
import org.apache.dubbo.rpc.Invocation;
|
||||
import org.apache.dubbo.rpc.Invoker;
|
||||
import org.apache.dubbo.rpc.RpcException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class NewRouter implements Router {
|
||||
@Override
|
||||
public URL getUrl() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRuntime() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isForce() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package org.apache.dubbo.rpc.cluster;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class RouterTest {
|
||||
|
||||
private static List<Router> routers = new ArrayList<>();
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp () {
|
||||
CompatibleRouter compatibleRouter = new CompatibleRouter();
|
||||
routers.add(compatibleRouter);
|
||||
CompatibleRouter2 compatibleRouter2 = new CompatibleRouter2();
|
||||
routers.add(compatibleRouter2);
|
||||
NewRouter newRouter = new NewRouter();
|
||||
routers.add(newRouter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareTo () {
|
||||
try {
|
||||
Collections.sort(routers);
|
||||
Assert.assertTrue(true);
|
||||
} catch (Exception e) {
|
||||
Assert.assertFalse(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue