Skip to content

Commit

Permalink
Extract compareTo impl to Router interface and concrete Router only r…
Browse files Browse the repository at this point in the history
…esponsible for provide priority. (#3240)

something is waiting for us to disscuss:
1. Every Route implement should set a priority?
2.#3249
  • Loading branch information
chickenlj authored and cvictory committed Jan 16, 2019
1 parent 8c2fbc8 commit e24e568
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,22 @@ default <T> void notify(List<Invoker<T>> invokers) {
* @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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
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;
Expand Down Expand Up @@ -65,11 +62,6 @@ public void setForce(boolean force) {
this.force = force;
}

@Override
public int compareTo(Router o) {
return (this.getPriority() >= o.getPriority()) ? 1 : -1;
}

public int getPriority() {
return priority;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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;
Expand Down Expand Up @@ -95,15 +94,9 @@ private <T> boolean hasMockProviders(final List<Invoker<T>> invokers) {
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -120,4 +121,13 @@ public boolean isRuntime() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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();
Expand All @@ -36,7 +36,7 @@ <T> List<com.alibaba.dubbo.rpc.Invoker<T>> route(List<com.alibaba.dubbo.rpc.Invo
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
Expand Down Expand Up @@ -65,7 +65,11 @@ default int getPriority() {
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
*/
public class CompatibleRouter implements Router {

@Override
public URL getUrl() {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit e24e568

Please sign in to comment.