Skip to content

Commit

Permalink
Minor changes of shardingsphere-governance-repository module (apache#…
Browse files Browse the repository at this point in the history
…8692)

* For code format

* Rename CuratorZookeeperRepository.lock

* Refactor CuratorZookeeperRepository

* Refactor LockCenter

* Refactor LockCenter

* For code format

* For code format
  • Loading branch information
terrymanu authored Dec 20, 2020
1 parent fbf7284 commit cfc0195
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;

import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -118,9 +117,8 @@ private boolean checkOrRetry(final Collection<String> instanceIds) {
}

private boolean check(final Collection<String> instanceIds) {
for (String instanceId : instanceIds) {
if (!RegistryCenterNodeStatus.LOCKED.toString()
.equalsIgnoreCase(registryCenter.loadInstanceData(instanceId))) {
for (String each : instanceIds) {
if (!RegistryCenterNodeStatus.LOCKED.toString().equalsIgnoreCase(registryCenter.loadInstanceData(each))) {
return false;
}
}
Expand All @@ -134,8 +132,6 @@ private boolean check(final Collection<String> instanceIds) {
*/
@Subscribe
public synchronized void lockNotice(final LockNoticeEvent event) {
if (Optional.of(event).isPresent()) {
registryCenter.persistInstanceData(event.isLocked() ? RegistryCenterNodeStatus.LOCKED.toString() : "");
}
registryCenter.persistInstanceData(event.isLocked() ? RegistryCenterNodeStatus.LOCKED.toString() : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class RegistryCenter {
private final RegistryRepository repository;

private final GovernanceInstance instance;

public RegistryCenter(final RegistryRepository registryRepository) {
node = new RegistryCenterNode();
repository = registryRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@
<version>${apollo.client.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-mockserver</artifactId>
<version>${apollo.client.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-openapi</artifactId>
Expand All @@ -55,5 +49,12 @@
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>

<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-mockserver</artifactId>
<version>${apollo.client.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public final class EtcdRepository implements ConfigurationRepository, RegistryRe
@Getter
@Setter
private Properties props = new Properties();

private EtcdProperties etcdProperties;

@Override
public void init(final String name, final GovernanceCenterConfiguration config) {
etcdProperties = new EtcdProperties(props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public final class CuratorZookeeperRepository implements ConfigurationRepository

private CuratorFramework client;

private InterProcessLock interProcessLock;
private InterProcessLock lock;

@Getter
@Setter
Expand Down Expand Up @@ -217,34 +217,6 @@ public void persistEphemeral(final String key, final String value) {
}
}

@Override
public void initLock(final String key) {
interProcessLock = new InterProcessMutex(client, key);
}

@Override
public boolean tryLock(final long time, final TimeUnit unit) {
try {
return interProcessLock.acquire(time, unit);
// CHECKSTYLE:OFF
} catch (final Exception e) {
// CHECKSTYLE:ON
CuratorZookeeperExceptionHandler.handleException(e);
return false;
}
}

@Override
public void releaseLock() {
try {
interProcessLock.release();
// CHECKSTYLE:OFF
} catch (final Exception e) {
// CHECKSTYLE:ON
CuratorZookeeperExceptionHandler.handleException(e);
}
}

@Override
public void delete(final String key) {
try {
Expand Down Expand Up @@ -300,6 +272,34 @@ private Type getChangedType(final CuratorCacheListener.Type type) {
}
}

@Override
public void initLock(final String key) {
lock = new InterProcessMutex(client, key);
}

@Override
public boolean tryLock(final long time, final TimeUnit unit) {
try {
return lock.acquire(time, unit);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
CuratorZookeeperExceptionHandler.handleException(ex);
return false;
}
}

@Override
public void releaseLock() {
try {
lock.release();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
CuratorZookeeperExceptionHandler.handleException(ex);
}
}

@Override
public void close() {
caches.values().forEach(CuratorCache::close);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void start() {
}));
}
}

/**
* Get testingServer connection string.
*
Expand Down

0 comments on commit cfc0195

Please sign in to comment.