-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SPI for upgrade checker. (#6217)
* Add SPI for upgrade checker. * For checkstyle
- Loading branch information
1 parent
4cd6c58
commit eb2644f
Showing
7 changed files
with
242 additions
and
27 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
naming/src/main/java/com/alibaba/nacos/naming/core/v2/upgrade/DefaultSelfUpgradeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.core.v2.upgrade; | ||
|
||
import com.alibaba.nacos.naming.core.ServiceManager; | ||
import com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteDelayTaskEngine; | ||
import com.alibaba.nacos.naming.monitor.MetricsMonitor; | ||
|
||
/** | ||
* Default upgrade checker for self node. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class DefaultSelfUpgradeChecker implements SelfUpgradeChecker { | ||
|
||
private static final String DEFAULT = "default"; | ||
|
||
@Override | ||
public String checkType() { | ||
return DEFAULT; | ||
} | ||
|
||
@Override | ||
public boolean isReadyToUpgrade(ServiceManager serviceManager, DoubleWriteDelayTaskEngine taskEngine) { | ||
System.out.println("test"); | ||
return checkServiceAndInstanceNumber(serviceManager) && checkDoubleWriteStatus(taskEngine); | ||
} | ||
|
||
private boolean checkServiceAndInstanceNumber(ServiceManager serviceManager) { | ||
boolean result = serviceManager.getServiceCount() == MetricsMonitor.getDomCountMonitor().get(); | ||
result &= serviceManager.getInstanceCount() == MetricsMonitor.getIpCountMonitor().get(); | ||
return result; | ||
} | ||
|
||
private boolean checkDoubleWriteStatus(DoubleWriteDelayTaskEngine taskEngine) { | ||
return taskEngine.isEmpty(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
naming/src/main/java/com/alibaba/nacos/naming/core/v2/upgrade/SelfUpgradeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.core.v2.upgrade; | ||
|
||
import com.alibaba.nacos.naming.core.ServiceManager; | ||
import com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteDelayTaskEngine; | ||
|
||
/** | ||
* Upgrade checker for self-node to judge whether current node is ready to upgrade. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public interface SelfUpgradeChecker { | ||
|
||
/** | ||
* Get the check type of this self upgrade checker. | ||
* | ||
* @return type | ||
*/ | ||
String checkType(); | ||
|
||
/** | ||
* Judge whether current node is ready to upgrade. | ||
* | ||
* @param serviceManager service manager for v1 mode. | ||
* @param taskEngine double write task engine | ||
* @return {@code true} if current node is ready to upgrade, otherwise {@code false} | ||
*/ | ||
boolean isReadyToUpgrade(ServiceManager serviceManager, DoubleWriteDelayTaskEngine taskEngine); | ||
} |
55 changes: 55 additions & 0 deletions
55
...g/src/main/java/com/alibaba/nacos/naming/core/v2/upgrade/SelfUpgradeCheckerSpiHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.core.v2.upgrade; | ||
|
||
import com.alibaba.nacos.common.spi.NacosServiceLoader; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* SPI holder for self upgrade checker. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class SelfUpgradeCheckerSpiHolder { | ||
|
||
private static final SelfUpgradeCheckerSpiHolder INSTANCE = new SelfUpgradeCheckerSpiHolder(); | ||
|
||
private static final DefaultSelfUpgradeChecker DEFAULT_SELF_UPGRADE_CHECKER = new DefaultSelfUpgradeChecker(); | ||
|
||
private final Map<String, SelfUpgradeChecker> selfUpgradeCheckerMap; | ||
|
||
private SelfUpgradeCheckerSpiHolder() { | ||
Collection<SelfUpgradeChecker> checkers = NacosServiceLoader.load(SelfUpgradeChecker.class); | ||
selfUpgradeCheckerMap = new HashMap<>(checkers.size()); | ||
for (SelfUpgradeChecker each : checkers) { | ||
selfUpgradeCheckerMap.put(each.checkType(), each); | ||
} | ||
} | ||
|
||
/** | ||
* Find target type self checker. | ||
* | ||
* @param type target type | ||
* @return target {@link SelfUpgradeChecker} if exist, otherwise {@link DefaultSelfUpgradeChecker} | ||
*/ | ||
public static SelfUpgradeChecker findSelfChecker(String type) { | ||
return INSTANCE.selfUpgradeCheckerMap.getOrDefault(type, DEFAULT_SELF_UPGRADE_CHECKER); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
naming/src/test/java/com/alibaba/nacos/naming/core/v2/upgrade/MockSelfUpgradeChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.core.v2.upgrade; | ||
|
||
import com.alibaba.nacos.naming.core.ServiceManager; | ||
import com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteDelayTaskEngine; | ||
|
||
public class MockSelfUpgradeChecker implements SelfUpgradeChecker { | ||
|
||
@Override | ||
public String checkType() { | ||
return "mock"; | ||
} | ||
|
||
@Override | ||
public boolean isReadyToUpgrade(ServiceManager serviceManager, DoubleWriteDelayTaskEngine taskEngine) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.