Skip to content

Commit 5e405ab

Browse files
committed
ZOOKEEPER-4966: Decouple ZKConfig from QuorumPeerConfig.ConfigException
`ZKConfig` is client accessible, it should avoid accessing server side `QuorumPeerConfig`. Changes: 1. Introduce `org.apache.zookeeper.common.ConfigException` for `ZKConfig(String configPath)`. 2. Make `QuorumPeerConfig.ConfigException` a subclass of above. 3. Throw `QuorumPeerConfig.ConfigException` if possible. Given above changes, this pr maintain abi compatibility with old releases. **Breaking change**: `ZKConfig(String configPath)` throws `org.apache.zookeeper.common.ConfigException` now but not `QuorumPeerConfig.ConfigException`, so developers have to fix it to compile. This is a small step towards ZOOKEEPER-233. Refs: ZOOKEEPER-233, ZOOKEEPER-835, and ZOOKEEPER-842.
1 parent 8b13615 commit 5e405ab

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
import org.apache.zookeeper.cli.CommandNotFoundException;
4646
import org.apache.zookeeper.cli.MalformedCommandException;
4747
import org.apache.zookeeper.client.ZKClientConfig;
48+
import org.apache.zookeeper.common.ConfigException;
4849
import org.apache.zookeeper.server.ExitCode;
49-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
5050
import org.apache.zookeeper.util.ServiceUtils;
5151
import org.slf4j.Logger;
5252
import org.slf4j.LoggerFactory;
@@ -267,7 +267,7 @@ protected void connectToZK(String newHost) throws InterruptedException, IOExcept
267267
if (cl.getOption("client-configuration") != null) {
268268
try {
269269
clientConfig = new ZKClientConfig(cl.getOption("client-configuration"));
270-
} catch (QuorumPeerConfig.ConfigException e) {
270+
} catch (ConfigException e) {
271271
e.printStackTrace();
272272
ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue());
273273
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import java.io.File;
2222
import org.apache.yetus.audience.InterfaceAudience;
23+
import org.apache.zookeeper.common.ConfigException;
2324
import org.apache.zookeeper.common.ZKConfig;
24-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
2525

2626
/**
2727
* Handles client specific properties
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.zookeeper.common;
20+
21+
public class ConfigException extends Exception {
22+
public ConfigException(String msg) {
23+
super(msg);
24+
}
25+
public ConfigException(String msg, Exception e) {
26+
super(msg, e);
27+
}
28+
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import java.io.File;
2222
import java.io.FileInputStream;
2323
import java.io.IOException;
24+
import java.lang.reflect.Constructor;
2425
import java.util.HashMap;
2526
import java.util.Map;
2627
import java.util.Map.Entry;
2728
import java.util.Properties;
2829
import org.apache.zookeeper.Environment;
29-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
3030
import org.apache.zookeeper.server.util.VerifyingFileFactory;
3131
import org.slf4j.Logger;
3232
import org.slf4j.LoggerFactory;
@@ -210,7 +210,17 @@ public void addConfiguration(File configFile) throws ConfigException {
210210
parseProperties(cfg);
211211
} catch (IOException | IllegalArgumentException e) {
212212
LOG.error("Error while configuration from: {}", configFile.getAbsolutePath(), e);
213-
throw new ConfigException("Error while processing " + configFile.getAbsolutePath(), e);
213+
String msg = "Error while processing " + configFile.getAbsolutePath();
214+
try {
215+
Class<?> clazz = Class.forName("org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException");
216+
Class<? extends ConfigException> exceptionClass = clazz.asSubclass(ConfigException.class);
217+
Constructor<? extends ConfigException> constructor = exceptionClass.getDeclaredConstructor(String.class, Exception.class);
218+
throw constructor.newInstance(msg, e);
219+
} catch (ClassNotFoundException ignored) {
220+
} catch (Exception ignored) {
221+
LOG.warn("Fail to construct QuorumPeerConfig.ConfigException", e);
222+
}
223+
throw new ConfigException(msg, e);
214224
}
215225
}
216226

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.zookeeper.common.AtomicFileWritingIdiom.OutputStreamStatement;
4343
import org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement;
4444
import org.apache.zookeeper.common.ClientX509Util;
45+
import org.apache.zookeeper.common.ConfigException;
4546
import org.apache.zookeeper.common.PathUtils;
4647
import org.apache.zookeeper.common.StringUtils;
4748
import org.apache.zookeeper.common.Time;
@@ -158,7 +159,7 @@ public class QuorumPeerConfig {
158159
protected long jvmPauseSleepTimeMs = JvmPauseMonitor.SLEEP_TIME_MS_DEFAULT;
159160

160161
@SuppressWarnings("serial")
161-
public static class ConfigException extends Exception {
162+
public static class ConfigException extends org.apache.zookeeper.common.ConfigException {
162163

163164
public ConfigException(String msg) {
164165
super(msg);

zookeeper-server/src/test/java/org/apache/zookeeper/client/ZKClientConfigTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
import java.util.HashMap;
3737
import java.util.Map;
3838
import java.util.Properties;
39+
import org.apache.zookeeper.common.ConfigException;
3940
import org.apache.zookeeper.common.ZKConfig;
40-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
4141
import org.junit.jupiter.api.Test;
4242
import org.junit.jupiter.api.Timeout;
4343
import org.junit.jupiter.api.io.TempDir;

0 commit comments

Comments
 (0)