Skip to content

Commit

Permalink
refactor default JobInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 6, 2017
1 parent 5f26745 commit 3ffff94
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ public JobInstance() {
public String getIp() {
return jobInstanceId.substring(0, jobInstanceId.indexOf(DELIMITER));
}

/**
* 判断是否为默认作业运行实例.
*
* @return 是否为默认作业运行实例
*/
public boolean isDefaultJobInstance() {
return DEFAULT_INSTANCE_ID.equals(jobInstanceId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.dangdang.ddframe.job.lite.internal.election;

import com.dangdang.ddframe.job.lite.api.strategy.JobInstance;
import com.dangdang.ddframe.job.lite.internal.listener.AbstractJobListener;
import com.dangdang.ddframe.job.lite.internal.listener.AbstractListenerManager;
import com.dangdang.ddframe.job.lite.internal.schedule.JobRegistry;
Expand Down Expand Up @@ -64,7 +63,7 @@ class LeaderElectionJobListener extends AbstractJobListener {
protected void dataChanged(final String path, final Type eventType, final String data) {
if (isLeaderCrashed(path, eventType) && serverService.isAvailableServer(JobRegistry.getInstance().getJobInstance(jobName).getIp())
|| !leaderService.hasLeader() && isLocalServerEnabled(path, data)) {
if (!JobRegistry.getInstance().getJobInstance(jobName).getJobInstanceId().equals(JobInstance.DEFAULT_INSTANCE_ID)) {
if (!JobRegistry.getInstance().getJobInstance(jobName).isDefaultJobInstance()) {
leaderService.electLeader();
}
} else if (leaderService.isLeader() && isLocalServerDisabled(path, data)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.dangdang.ddframe.job.lite.internal.sharding;

import com.dangdang.ddframe.job.lite.api.strategy.JobInstance;
import com.dangdang.ddframe.job.lite.internal.config.ConfigurationNode;
import com.dangdang.ddframe.job.lite.internal.config.LiteJobConfigurationGsonFactory;
import com.dangdang.ddframe.job.lite.internal.instance.InstanceNode;
Expand Down Expand Up @@ -78,10 +77,8 @@ class ListenServersChangedJobListener extends AbstractJobListener {

@Override
protected void dataChanged(final String path, final Type eventType, final String data) {
if (isInstanceChange(eventType, path) || isServerChange(path)) {
if (!JobRegistry.getInstance().getJobInstance(jobName).getJobInstanceId().equals(JobInstance.DEFAULT_INSTANCE_ID)) {
shardingService.setReshardingFlag();
}
if (!JobRegistry.getInstance().getJobInstance(jobName).isDefaultJobInstance() && (isInstanceChange(eventType, path) || isServerChange(path))) {
shardingService.setReshardingFlag();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.junit.Test;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public final class JobInstanceTest {

Expand All @@ -18,4 +20,14 @@ public void assertGetIp() {
assertThat(new JobInstance(JobInstance.DEFAULT_INSTANCE_ID).getIp(), is("1.1.1.1"));
assertThat(new JobInstance().getIp(), is(IpUtils.getIp()));
}

@Test
public void assertIsDefaultJobInstance() {
assertTrue(new JobInstance(JobInstance.DEFAULT_INSTANCE_ID).isDefaultJobInstance());
}

@Test
public void assertIsNotDefaultJobInstance() {
assertFalse(new JobInstance().isDefaultJobInstance());
}
}

0 comments on commit 3ffff94

Please sign in to comment.