Skip to content

Commit f534e71

Browse files
committed
ZOOKEEPER-4970: Deprecate ZKConfig methods which throw QuorumPeerConfig.ConfigException
`QuorumPeerConfig` belongs to and ties with other server code. To separate a slimmer `zookeeper-client` jar, we have to break the dependency from `ZKConfig` to `QuorumPeerConfig`. But this is a breaking change, it would be good to deprecate these methods first to ease future migration. Refs: ZOOKEEPER-4966, ZOOKEEPER-4970, ZOOKEEPER-233. Reviewers: tisonkun Author: kezhuw Closes apache#2309 from kezhuw/ZOOKEEPER-4970-deprecate-ConfigException-from-ZKConfig (cherry picked from bb76c97)
1 parent f3fd088 commit f534e71

File tree

6 files changed

+115
-17
lines changed

6 files changed

+115
-17
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStreamReader;
2424
import java.lang.reflect.InvocationTargetException;
2525
import java.lang.reflect.Method;
26+
import java.nio.file.Paths;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.Collections;
@@ -45,8 +46,8 @@
4546
import org.apache.zookeeper.cli.CommandNotFoundException;
4647
import org.apache.zookeeper.cli.MalformedCommandException;
4748
import org.apache.zookeeper.client.ZKClientConfig;
49+
import org.apache.zookeeper.common.ConfigException;
4850
import org.apache.zookeeper.server.ExitCode;
49-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
5051
import org.apache.zookeeper.util.ServiceUtils;
5152
import org.slf4j.Logger;
5253
import org.slf4j.LoggerFactory;
@@ -266,8 +267,8 @@ protected void connectToZK(String newHost) throws InterruptedException, IOExcept
266267

267268
if (cl.getOption("client-configuration") != null) {
268269
try {
269-
clientConfig = new ZKClientConfig(cl.getOption("client-configuration"));
270-
} catch (QuorumPeerConfig.ConfigException e) {
270+
clientConfig = new ZKClientConfig(Paths.get(cl.getOption("client-configuration")));
271+
} catch (ConfigException e) {
271272
e.printStackTrace();
272273
ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue());
273274
}

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
package org.apache.zookeeper.client;
2020

2121
import java.io.File;
22+
import java.nio.file.Path;
2223
import org.apache.yetus.audience.InterfaceAudience;
24+
import org.apache.zookeeper.common.ConfigException;
2325
import org.apache.zookeeper.common.ZKConfig;
24-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
26+
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
2527

2628
/**
2729
* Handles client specific properties
@@ -64,11 +66,29 @@ public ZKClientConfig() {
6466
initFromJavaSystemProperties();
6567
}
6668

67-
public ZKClientConfig(File configFile) throws ConfigException {
69+
/**
70+
* <p><b>Use {@link ZKClientConfig#ZKClientConfig(Path configPath)} instead.</b>
71+
*
72+
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
73+
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
74+
*/
75+
@Deprecated
76+
public ZKClientConfig(File configFile) throws QuorumPeerConfig.ConfigException {
6877
super(configFile);
6978
}
7079

71-
public ZKClientConfig(String configPath) throws ConfigException {
80+
/**
81+
* <p><b>Use {@link ZKClientConfig#ZKClientConfig(Path configPath)} instead.</b>
82+
*
83+
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
84+
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
85+
*/
86+
@Deprecated
87+
public ZKClientConfig(String configPath) throws QuorumPeerConfig.ConfigException {
88+
super(configPath);
89+
}
90+
91+
public ZKClientConfig(Path configPath) throws ConfigException {
7292
super(configPath);
7393
}
7494

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
/**
22+
* Configuration related exception.
23+
*/
24+
public class ConfigException extends Exception {
25+
public ConfigException(String msg) {
26+
super(msg);
27+
}
28+
public ConfigException(String msg, Exception e) {
29+
super(msg, e);
30+
}
31+
}

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

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
import java.io.File;
2222
import java.io.FileInputStream;
2323
import java.io.IOException;
24+
import java.nio.file.Path;
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;
30+
import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
3031
import org.apache.zookeeper.server.util.VerifyingFileFactory;
3132
import org.slf4j.Logger;
3233
import org.slf4j.LoggerFactory;
@@ -62,29 +63,50 @@ public ZKConfig() {
6263
}
6364

6465
/**
66+
* <p><b>Use {@link ZKConfig#ZKConfig(Path configPath)} instead.</b>
67+
*
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>
70+
*
6571
* @param configPath
6672
* Configuration file path
6773
* @throws ConfigException
6874
* if failed to load configuration properties
6975
*/
70-
71-
public ZKConfig(String configPath) throws ConfigException {
76+
@Deprecated
77+
public ZKConfig(String configPath) throws QuorumPeerConfig.ConfigException {
7278
this(new File(configPath));
7379
}
7480

7581
/**
82+
* <p><b>Use {@link ZKConfig#ZKConfig(Path configPath)} instead.</b>
83+
*
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>
7686
*
7787
* @param configFile
7888
* Configuration file
7989
* @throws ConfigException
8090
* if failed to load configuration properties
8191
*/
82-
public ZKConfig(File configFile) throws ConfigException {
92+
@Deprecated
93+
public ZKConfig(File configFile) throws QuorumPeerConfig.ConfigException {
8394
this();
8495
addConfiguration(configFile);
8596
LOG.info("ZK Config {}", this.properties);
8697
}
8798

99+
/**
100+
* Constructs a {@link ZKConfig} with properties from file.
101+
*
102+
* @param configPath path to configuration file
103+
* @throws ConfigException
104+
*/
105+
@SuppressWarnings("deprecation")
106+
public ZKConfig(Path configPath) throws ConfigException {
107+
this(configPath.toFile());
108+
}
109+
88110
private void init() {
89111
/**
90112
* backward compatibility for all currently available client properties
@@ -191,10 +213,27 @@ public void setProperty(String key, String value) {
191213
* Add a configuration resource. The properties form this configuration will
192214
* overwrite corresponding already loaded property and system property
193215
*
216+
* @param configPath path to Configuration file.
217+
*/
218+
@SuppressWarnings("deprecation")
219+
public void addConfiguration(Path configPath) throws ConfigException {
220+
addConfiguration(configPath.toFile());
221+
}
222+
223+
/**
224+
* <p><b>Use {@link #addConfiguration(Path)} instead.</b></p>
225+
*
226+
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
227+
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
228+
*
229+
* <p>Add a configuration resource. The properties form this configuration will
230+
* overwrite corresponding already loaded property and system property
231+
*
194232
* @param configFile
195233
* Configuration file.
196234
*/
197-
public void addConfiguration(File configFile) throws ConfigException {
235+
@Deprecated
236+
public void addConfiguration(File configFile) throws QuorumPeerConfig.ConfigException {
198237
LOG.info("Reading configuration from: {}", configFile.getAbsolutePath());
199238
try {
200239
configFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath()
@@ -210,18 +249,24 @@ public void addConfiguration(File configFile) throws ConfigException {
210249
parseProperties(cfg);
211250
} catch (IOException | IllegalArgumentException e) {
212251
LOG.error("Error while configuration from: {}", configFile.getAbsolutePath(), e);
213-
throw new ConfigException("Error while processing " + configFile.getAbsolutePath(), e);
252+
throw new QuorumPeerConfig.ConfigException("Error while processing " + configFile.getAbsolutePath(), e);
214253
}
215254
}
216255

217256
/**
218-
* Add a configuration resource. The properties form this configuration will
257+
* <p><b>Use {@link #addConfiguration(Path)} instead.</b></p>
258+
*
259+
* <p><b>The signature of this method will be changed to throw {@link ConfigException}
260+
* instead of {@link QuorumPeerConfig.ConfigException} in future release.</b>
261+
*
262+
* <p>Add a configuration resource. The properties form this configuration will
219263
* overwrite corresponding already loaded property and system property
220264
*
221265
* @param configPath
222266
* Configuration file path.
223267
*/
224-
public void addConfiguration(String configPath) throws ConfigException {
268+
@Deprecated
269+
public void addConfiguration(String configPath) throws QuorumPeerConfig.ConfigException {
225270
addConfiguration(new File(configPath));
226271
}
227272

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public class QuorumPeerConfig {
157157
protected long jvmPauseSleepTimeMs = JvmPauseMonitor.SLEEP_TIME_MS_DEFAULT;
158158

159159
@SuppressWarnings("serial")
160-
public static class ConfigException extends Exception {
160+
public static class ConfigException extends org.apache.zookeeper.common.ConfigException {
161161

162162
public ConfigException(String msg) {
163163
super(msg);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
import java.io.FileOutputStream;
3434
import java.io.IOException;
3535
import java.io.OutputStream;
36+
import java.nio.file.Paths;
3637
import java.util.HashMap;
3738
import java.util.Map;
3839
import java.util.Properties;
40+
import org.apache.zookeeper.common.ConfigException;
3941
import org.apache.zookeeper.common.ZKConfig;
40-
import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
4142
import org.junit.jupiter.api.Test;
4243
import org.junit.jupiter.api.Timeout;
4344
import org.junit.jupiter.api.io.TempDir;
@@ -116,7 +117,7 @@ public void testReadConfigurationFile(@TempDir File testDataDir) throws IOExcept
116117
}
117118

118119
ZKClientConfig conf = new ZKClientConfig();
119-
conf.addConfiguration(file.getAbsolutePath());
120+
conf.addConfiguration(Paths.get(file.getAbsolutePath()));
120121
assertEquals(conf.getProperty(ENABLE_CLIENT_SASL_KEY), "true");
121122
assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), "ZK");
122123
assertEquals(conf.getProperty(LOGIN_CONTEXT_NAME_KEY), "MyClient");

0 commit comments

Comments
 (0)