Skip to content

Commit b418fbe

Browse files
committed
Improve ZooKeeper reconnection strategy
1 parent 1b40ad5 commit b418fbe

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<properties>
4444
<java.language.level>1.8</java.language.level>
4545

46-
<dynamicconfig.version>2.9-beta2</dynamicconfig.version>
46+
<dynamicconfig.version>2.9-beta3</dynamicconfig.version>
4747
<zookeeper.version>3.4.6</zookeeper.version>
4848
<slf4j.version>1.7.21</slf4j.version>
4949

uniqueid-zookeeper/src/main/java/org/lable/oss/uniqueid/zookeeper/SynchronizedGeneratorIdentity.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.lable.oss.uniqueid.GeneratorException;
2020
import org.lable.oss.uniqueid.GeneratorIdentityHolder;
2121
import org.lable.oss.uniqueid.bytes.Blueprint;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
2224

2325
import java.io.IOException;
2426
import java.time.Duration;
@@ -34,6 +36,8 @@
3436
* @see #basedOn(String, String, Supplier, Supplier)
3537
*/
3638
public class SynchronizedGeneratorIdentity implements GeneratorIdentityHolder {
39+
private static final Logger logger = LoggerFactory.getLogger(SynchronizedGeneratorIdentity.class);
40+
3741
final int clusterId;
3842

3943
final Supplier<Duration> claimDurationSupplier;
@@ -135,6 +139,10 @@ public void relinquishGeneratorIdClaim() {
135139
}
136140

137141
private ResourceClaim acquireResourceClaim() throws GeneratorException {
142+
return acquireResourceClaim(0);
143+
}
144+
145+
private ResourceClaim acquireResourceClaim(int retries) throws GeneratorException {
138146
try {
139147
return ExpiringResourceClaim.claimExpiring(
140148
zooKeeperConnection,
@@ -144,7 +152,15 @@ private ResourceClaim acquireResourceClaim() throws GeneratorException {
144152
acquisitionTimeoutSupplier.get()
145153
);
146154
} catch (IOException e) {
147-
throw new GeneratorException(e);
155+
logger.warn(
156+
"Connection to ZooKeeper quorum failed, retrying resource claim acquisition, attempt {}.",
157+
retries + 1
158+
);
159+
if (retries < 3) {
160+
return acquireResourceClaim(retries + 1);
161+
} else {
162+
throw new GeneratorException(e);
163+
}
148164
}
149165
}
150166

0 commit comments

Comments
 (0)