Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract compareTo impl to Router interface and concrete Router only responsible for provide priority. #3240

Merged
merged 6 commits into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,19 @@ 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;
}
return getUrl().toFullString().compareTo(o.getUrl().toFullString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NullPointException for getUrl

} 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note, a.compareTo(b) is not equivalent b.compareTo(a)

}

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 {
cvictory marked this conversation as resolved.
Show resolved Hide resolved
@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);
}
}
}