Skip to content

Commit 3ae8e77

Browse files
authored
ZOOKEEPER-4966: Decouple ZKConfig from QuorumPeerConfig.ConfigException
Reviewers: anmolnar, cnauroth Author: kezhuw Closes #2306 from kezhuw/ZOOKEEPER-4966-decouple-ZKConfig-from-QuorumPeerConfig
1 parent a8631bd commit 3ae8e77

24 files changed

+82
-84
lines changed

zookeeper-server/src/main/java/org/apache/zookeeper/client/ZKClientConfig.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.yetus.audience.InterfaceAudience;
2424
import org.apache.zookeeper.common.ConfigException;
2525
import org.apache.zookeeper.common.ZKConfig;
26-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
2726

2827
/**
2928
* Handles client specific properties
@@ -78,22 +77,22 @@ public ZKClientConfig() {
7877
/**
7978
* <p><b>Use {@link ZKClientConfig#ZKClientConfig(Path configPath)} instead.</b>
8079
*
81-
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
82-
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
80+
* <p><b>The signature of this method has been changed to throw {@link ConfigException}
81+
* instead of {@code QuorumPeerConfig.ConfigException}.</b>
8382
*/
8483
@Deprecated
85-
public ZKClientConfig(File configFile) throws QuorumPeerConfig.ConfigException {
84+
public ZKClientConfig(File configFile) throws ConfigException {
8685
super(configFile);
8786
}
8887

8988
/**
9089
* <p><b>Use {@link ZKClientConfig#ZKClientConfig(Path configPath)} instead.</b>
9190
*
92-
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
93-
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
91+
* <p><b>The signature of this method has been changed to throw {@link ConfigException}
92+
* instead of {@code QuorumPeerConfig.ConfigException}.</b>
9493
*/
9594
@Deprecated
96-
public ZKClientConfig(String configPath) throws QuorumPeerConfig.ConfigException {
95+
public ZKClientConfig(String configPath) throws ConfigException {
9796
super(configPath);
9897
}
9998

zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKConfig.java

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import java.io.File;
2222
import java.io.FileInputStream;
2323
import java.io.IOException;
24+
import java.lang.reflect.Constructor;
2425
import java.nio.file.Path;
26+
import java.nio.file.Paths;
2527
import java.util.HashMap;
2628
import java.util.Map;
2729
import java.util.Map.Entry;
2830
import java.util.Properties;
2931
import org.apache.zookeeper.Environment;
30-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
3132
import org.apache.zookeeper.server.util.VerifyingFileFactory;
3233
import org.slf4j.Logger;
3334
import org.slf4j.LoggerFactory;
@@ -65,35 +66,33 @@ public ZKConfig() {
6566
/**
6667
* <p><b>Use {@link ZKConfig#ZKConfig(Path configPath)} instead.</b>
6768
*
68-
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
69-
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
69+
* <p><b>The signature of this method has been changed to throw {@link ConfigException}
70+
* instead of {@code QuorumPeerConfig.ConfigException}.</b>
7071
*
7172
* @param configPath
7273
* Configuration file path
7374
* @throws ConfigException
7475
* if failed to load configuration properties
7576
*/
7677
@Deprecated
77-
public ZKConfig(String configPath) throws QuorumPeerConfig.ConfigException {
78-
this(new File(configPath));
78+
public ZKConfig(String configPath) throws ConfigException {
79+
this(Paths.get(configPath));
7980
}
8081

8182
/**
8283
* <p><b>Use {@link ZKConfig#ZKConfig(Path configPath)} instead.</b>
8384
*
84-
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
85-
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
85+
* <p><b>The signature of this method has been changed to throw {@link ConfigException}
86+
* instead of {@code QuorumPeerConfig.ConfigException}.</b>
8687
*
8788
* @param configFile
8889
* Configuration file
8990
* @throws ConfigException
9091
* if failed to load configuration properties
9192
*/
9293
@Deprecated
93-
public ZKConfig(File configFile) throws QuorumPeerConfig.ConfigException {
94-
this();
95-
addConfiguration(configFile);
96-
LOG.info("ZK Config {}", this.properties);
94+
public ZKConfig(File configFile) throws ConfigException {
95+
this(configFile.toPath());
9796
}
9897

9998
/**
@@ -102,9 +101,10 @@ public ZKConfig(File configFile) throws QuorumPeerConfig.ConfigException {
102101
* @param configPath path to configuration file
103102
* @throws ConfigException
104103
*/
105-
@SuppressWarnings("deprecation")
106104
public ZKConfig(Path configPath) throws ConfigException {
107-
this(configPath.toFile());
105+
this();
106+
addConfiguration(configPath);
107+
LOG.info("ZK Config {}", this.properties);
108108
}
109109

110110
private void init() {
@@ -216,16 +216,39 @@ public void setProperty(String key, String value) {
216216
*
217217
* @param configPath path to Configuration file.
218218
*/
219-
@SuppressWarnings("deprecation")
220219
public void addConfiguration(Path configPath) throws ConfigException {
221-
addConfiguration(configPath.toFile());
220+
Path absoluteConfigPath = configPath.toAbsolutePath();
221+
LOG.info("Reading configuration from: {}", absoluteConfigPath);
222+
try {
223+
File configFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath()
224+
.failForNonExistingPath()
225+
.build()).validate(configPath.toFile());
226+
Properties cfg = new Properties();
227+
try (FileInputStream in = new FileInputStream(configFile)) {
228+
cfg.load(in);
229+
}
230+
parseProperties(cfg);
231+
} catch (IOException | IllegalArgumentException e) {
232+
LOG.error("Error while configuration from: {}", absoluteConfigPath, e);
233+
String msg = "Error while processing " + absoluteConfigPath;
234+
try {
235+
Class<?> clazz = Class.forName("org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException");
236+
Class<? extends ConfigException> exceptionClass = clazz.asSubclass(ConfigException.class);
237+
Constructor<? extends ConfigException> constructor = exceptionClass.getDeclaredConstructor(String.class, Exception.class);
238+
throw constructor.newInstance(msg, e);
239+
} catch (ClassNotFoundException ignored) {
240+
} catch (Exception ignored) {
241+
LOG.warn("Fail to construct QuorumPeerConfig.ConfigException", e);
242+
}
243+
throw new ConfigException(msg, e);
244+
}
222245
}
223246

224247
/**
225248
* <p><b>Use {@link #addConfiguration(Path)} instead.</b></p>
226249
*
227-
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
228-
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
250+
* <p><b>The signature of this method has been changed to throw {@link ConfigException}
251+
* instead of {@code QuorumPeerConfig.ConfigException}.</b>
229252
*
230253
* <p>Add a configuration resource. The properties form this configuration will
231254
* overwrite corresponding already loaded property and system property
@@ -234,31 +257,15 @@ public void addConfiguration(Path configPath) throws ConfigException {
234257
* Configuration file.
235258
*/
236259
@Deprecated
237-
public void addConfiguration(File configFile) throws QuorumPeerConfig.ConfigException {
238-
LOG.info("Reading configuration from: {}", configFile.getAbsolutePath());
239-
try {
240-
configFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath()
241-
.failForNonExistingPath()
242-
.build()).validate(configFile);
243-
Properties cfg = new Properties();
244-
FileInputStream in = new FileInputStream(configFile);
245-
try {
246-
cfg.load(in);
247-
} finally {
248-
in.close();
249-
}
250-
parseProperties(cfg);
251-
} catch (IOException | IllegalArgumentException e) {
252-
LOG.error("Error while configuration from: {}", configFile.getAbsolutePath(), e);
253-
throw new QuorumPeerConfig.ConfigException("Error while processing " + configFile.getAbsolutePath(), e);
254-
}
260+
public void addConfiguration(File configFile) throws ConfigException {
261+
addConfiguration(configFile.toPath());
255262
}
256263

257264
/**
258265
* <p><b>Use {@link #addConfiguration(Path)} instead.</b></p>
259266
*
260-
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
261-
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
267+
* <p><b>The signature of this method has been changed to throw {@link ConfigException}
268+
* instead of {@code QuorumPeerConfig.ConfigException}.</b>
262269
*
263270
* <p>Add a configuration resource. The properties form this configuration will
264271
* overwrite corresponding already loaded property and system property
@@ -267,8 +274,8 @@ public void addConfiguration(File configFile) throws QuorumPeerConfig.ConfigExce
267274
* Configuration file path.
268275
*/
269276
@Deprecated
270-
public void addConfiguration(String configPath) throws QuorumPeerConfig.ConfigException {
271-
addConfiguration(new File(configPath));
277+
public void addConfiguration(String configPath) throws ConfigException {
278+
addConfiguration(Paths.get(configPath));
272279
}
273280

274281
private void parseProperties(Properties cfg) {

zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxnFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import javax.net.ssl.SSLSession;
6363
import org.apache.zookeeper.KeeperException;
6464
import org.apache.zookeeper.common.ClientX509Util;
65+
import org.apache.zookeeper.common.ConfigException;
6566
import org.apache.zookeeper.common.NettyUtils;
6667
import org.apache.zookeeper.common.X509Exception;
6768
import org.apache.zookeeper.common.X509Exception.SSLContextException;
@@ -529,7 +530,7 @@ private ServerBootstrap configureBootstrapAllocator(ServerBootstrap bootstrap) {
529530
if (usePortUnification) {
530531
try {
531532
QuorumPeerConfig.configureSSLAuth();
532-
} catch (QuorumPeerConfig.ConfigException e) {
533+
} catch (ConfigException e) {
533534
LOG.error("unable to set up SslAuthProvider, turning off client port unification", e);
534535
usePortUnification = false;
535536
}

zookeeper-server/src/main/java/org/apache/zookeeper/server/PrepRequestProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.zookeeper.Op;
4141
import org.apache.zookeeper.ZooDefs;
4242
import org.apache.zookeeper.ZooDefs.OpCode;
43+
import org.apache.zookeeper.common.ConfigException;
4344
import org.apache.zookeeper.common.PathUtils;
4445
import org.apache.zookeeper.common.StringUtils;
4546
import org.apache.zookeeper.common.Time;
@@ -60,7 +61,6 @@
6061
import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer;
6162
import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
6263
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
63-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
6464
import org.apache.zookeeper.server.quorum.flexible.QuorumMaj;
6565
import org.apache.zookeeper.server.quorum.flexible.QuorumOracleMaj;
6666
import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier;

zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import java.util.Arrays;
2424
import java.util.Properties;
2525
import org.apache.yetus.audience.InterfaceAudience;
26+
import org.apache.zookeeper.common.ConfigException;
2627
import org.apache.zookeeper.metrics.impl.DefaultMetricsProvider;
2728
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
28-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
2929

3030
/**
3131
* Server configuration storage.

zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.management.JMException;
2424
import org.apache.yetus.audience.InterfaceAudience;
2525
import org.apache.zookeeper.audit.ZKAuditProvider;
26+
import org.apache.zookeeper.common.ConfigException;
2627
import org.apache.zookeeper.jmx.ManagedUtil;
2728
import org.apache.zookeeper.metrics.MetricsProvider;
2829
import org.apache.zookeeper.metrics.MetricsProviderLifeCycleException;
@@ -33,7 +34,6 @@
3334
import org.apache.zookeeper.server.auth.ProviderRegistry;
3435
import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
3536
import org.apache.zookeeper.server.persistence.FileTxnSnapLog.DatadirException;
36-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
3737
import org.apache.zookeeper.server.util.JvmPauseMonitor;
3838
import org.apache.zookeeper.util.ServiceUtils;
3939
import org.slf4j.Logger;

zookeeper-server/src/main/java/org/apache/zookeeper/server/controller/ControllerServerConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.net.ServerSocket;
2626
import java.util.HashMap;
2727
import java.util.Map;
28+
import org.apache.zookeeper.common.ConfigException;
2829
import org.apache.zookeeper.server.ServerConfig;
2930
import org.apache.zookeeper.server.quorum.QuorumPeer;
3031
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;

zookeeper-server/src/main/java/org/apache/zookeeper/server/controller/ControllerService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.zookeeper.server.controller;
2020

2121
import java.io.IOException;
22+
import org.apache.zookeeper.common.ConfigException;
2223
import org.apache.zookeeper.server.ExitCode;
2324
import org.apache.zookeeper.server.ServerCnxnFactory;
2425
import org.apache.zookeeper.server.quorum.QuorumPeer;
@@ -119,15 +120,15 @@ protected void cleanup() {
119120
/**
120121
* Runs the main loop for this application but does not exit the process.
121122
*/
122-
public void initializeAndRun(String[] args) throws QuorumPeerConfig.ConfigException {
123+
public void initializeAndRun(String[] args) throws ConfigException {
123124
initConfig(args);
124125
run();
125126
}
126127

127128
/**
128129
* Derived classes may override to do custom initialization of command line args.
129130
*/
130-
protected void initConfig(String[] args) throws QuorumPeerConfig.ConfigException {
131+
protected void initConfig(String[] args) throws ConfigException {
131132
if (args.length == 1) {
132133
config.parse(args[0]);
133134
}

zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FastLeaderElection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import java.util.concurrent.LinkedBlockingQueue;
2828
import java.util.concurrent.TimeUnit;
2929
import java.util.concurrent.atomic.AtomicLong;
30+
import org.apache.zookeeper.common.ConfigException;
3031
import org.apache.zookeeper.common.Time;
3132
import org.apache.zookeeper.jmx.MBeanRegistry;
3233
import org.apache.zookeeper.server.ZooKeeperThread;
3334
import org.apache.zookeeper.server.quorum.QuorumCnxManager.Message;
3435
import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType;
3536
import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState;
36-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
3737
import org.apache.zookeeper.server.quorum.flexible.QuorumOracleMaj;
3838
import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier;
3939
import org.apache.zookeeper.server.util.ZxidUtils;

zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumCnxManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@
6161
import java.util.function.Supplier;
6262
import java.util.stream.Collectors;
6363
import javax.net.ssl.SSLSocket;
64+
import org.apache.zookeeper.common.ConfigException;
6465
import org.apache.zookeeper.common.NetUtils;
6566
import org.apache.zookeeper.common.X509Exception;
6667
import org.apache.zookeeper.server.ExitCode;
6768
import org.apache.zookeeper.server.ZooKeeperThread;
68-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
6969
import org.apache.zookeeper.server.quorum.auth.QuorumAuthLearner;
7070
import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer;
7171
import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier;

0 commit comments

Comments
 (0)