-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
The lite-core module decouples ZooKeeper #2182
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -17,9 +17,14 @@ | |
|
||
package org.apache.shardingsphere.elasticjob.lite.api.registry; | ||
|
||
import java.util.Arrays; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are too many unrelated changes in this PR. Please revert them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Caused by reformatting code. |
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.apache.curator.utils.ThreadUtils; | ||
import org.apache.shardingsphere.elasticjob.api.ElasticJob; | ||
import org.apache.shardingsphere.elasticjob.api.JobConfiguration; | ||
import org.apache.shardingsphere.elasticjob.infra.handler.sharding.JobInstance; | ||
|
@@ -28,30 +33,23 @@ | |
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap; | ||
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap; | ||
import org.apache.shardingsphere.elasticjob.lite.internal.storage.JobNodePath; | ||
import org.apache.shardingsphere.elasticjob.lite.internal.util.ThreadUtils; | ||
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; | ||
import org.apache.shardingsphere.elasticjob.reg.listener.DataChangedEvent; | ||
import org.apache.shardingsphere.elasticjob.reg.listener.DataChangedEventListener; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* Job instance registry. | ||
*/ | ||
@RequiredArgsConstructor | ||
public final class JobInstanceRegistry { | ||
|
||
private static final Pattern JOB_CONFIG_COMPILE = Pattern.compile("/(\\w+)/config"); | ||
|
||
private final CoordinatorRegistryCenter regCenter; | ||
|
||
private final JobInstance jobInstance; | ||
|
||
/** | ||
* Register. | ||
*/ | ||
|
@@ -60,9 +58,9 @@ public void register() { | |
Executor executor = Executors.newSingleThreadExecutor(threadFactory); | ||
regCenter.watch("/", new JobInstanceRegistryListener(), executor); | ||
} | ||
|
||
public class JobInstanceRegistryListener implements DataChangedEventListener { | ||
|
||
@Override | ||
public void onChange(final DataChangedEvent event) { | ||
if (event.getType() != DataChangedEvent.Type.ADDED || !isJobConfigPath(event.getKey())) { | ||
|
@@ -78,13 +76,13 @@ public void onChange(final DataChangedEvent event) { | |
new OneOffJobBootstrap(regCenter, newElasticJobInstance(jobConfig), jobConfig).execute(); | ||
} | ||
} | ||
|
||
private boolean isAllShardingItemsCompleted(final JobConfiguration jobConfig) { | ||
JobNodePath jobNodePath = new JobNodePath(jobConfig.getJobName()); | ||
return IntStream.range(0, jobConfig.getShardingTotalCount()) | ||
.allMatch(each -> regCenter.isExisted(jobNodePath.getShardingNodePath(String.valueOf(each), "completed"))); | ||
.allMatch(each -> regCenter.isExisted(jobNodePath.getShardingNodePath(String.valueOf(each), "completed"))); | ||
} | ||
|
||
private ElasticJob newElasticJobInstance(final JobConfiguration jobConfig) { | ||
String clazz = regCenter.get(String.format("/%s", jobConfig.getJobName())); | ||
try { | ||
|
@@ -95,7 +93,7 @@ private ElasticJob newElasticJobInstance(final JobConfiguration jobConfig) { | |
throw new RuntimeException(String.format("new elastic job instance by class '%s' failure", clazz), ex); | ||
} | ||
} | ||
|
||
private boolean isLabelMatch(final JobConfiguration jobConfig) { | ||
if (jobConfig.getLabel() == null) { | ||
return false; | ||
|
@@ -105,7 +103,7 @@ private boolean isLabelMatch(final JobConfiguration jobConfig) { | |
} | ||
return Arrays.stream(jobInstance.getLabels().split(",")).collect(Collectors.toSet()).contains(jobConfig.getLabel()); | ||
} | ||
|
||
private boolean isJobConfigPath(final String path) { | ||
return JOB_CONFIG_COMPILE.matcher(path).matches(); | ||
} | ||
|
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
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
52 changes: 52 additions & 0 deletions
52
...re/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/util/ThreadUtils.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 @@ | ||
/* | ||
* 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.shardingsphere.elasticjob.lite.internal.util; | ||
|
||
import com.google.common.base.Throwables; | ||
import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
import java.util.concurrent.ThreadFactory; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* Thread utility. | ||
*/ | ||
@Slf4j | ||
public final class ThreadUtils { | ||
|
||
private ThreadUtils() { | ||
|
||
} | ||
|
||
/** | ||
* Create a new generic thread factory instance. | ||
* | ||
* @param processName Process thread name prefix. | ||
* @return Return generic thread factory instance. | ||
*/ | ||
public static ThreadFactory newGenericThreadFactory(final String processName) { | ||
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = (t, e) -> { | ||
log.error("Unexpected exception in thread: " + t, e); | ||
Throwables.throwIfUnchecked(e); | ||
}; | ||
return new ThreadFactoryBuilder() | ||
.setNameFormat(processName + "-%d") | ||
.setDaemon(true) | ||
.setUncaughtExceptionHandler(uncaughtExceptionHandler) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we consider setting it
runtime
? Otherwise users need to introduce it sperately.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, after decoupling, users are expected to actively introduce.