Skip to content

Commit 3b08ac4

Browse files
adoroszlainandakumar131
authored andcommitted
HDDS-1363. ozone.metadata.dirs doesn't pick multiple dirs (#691)
1 parent 4003849 commit 3b08ac4

File tree

10 files changed

+164
-120
lines changed

10 files changed

+164
-120
lines changed

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsUtils.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.lang.reflect.Method;
2525
import java.net.InetSocketAddress;
2626
import java.net.UnknownHostException;
27-
import java.nio.file.Paths;
2827
import java.util.Calendar;
2928
import java.util.Collection;
3029
import java.util.HashSet;
@@ -50,7 +49,6 @@
5049
import org.apache.hadoop.net.DNS;
5150
import org.apache.hadoop.net.NetUtils;
5251

53-
import com.google.common.base.Strings;
5452
import com.google.common.net.HostAndPort;
5553
import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_DNS_INTERFACE_KEY;
5654
import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_DNS_NAMESERVER_KEY;
@@ -312,28 +310,6 @@ public static boolean isHddsEnabled(Configuration conf) {
312310
}
313311

314312

315-
/**
316-
* Get the path for datanode id file.
317-
*
318-
* @param conf - Configuration
319-
* @return the path of datanode id as string
320-
*/
321-
public static String getDatanodeIdFilePath(Configuration conf) {
322-
String dataNodeIDPath = conf.get(ScmConfigKeys.OZONE_SCM_DATANODE_ID);
323-
if (dataNodeIDPath == null) {
324-
String metaPath = conf.get(HddsConfigKeys.OZONE_METADATA_DIRS);
325-
if (Strings.isNullOrEmpty(metaPath)) {
326-
// this means meta data is not found, in theory should not happen at
327-
// this point because should've failed earlier.
328-
throw new IllegalArgumentException("Unable to locate meta data" +
329-
"directory when getting datanode id path");
330-
}
331-
dataNodeIDPath = Paths.get(metaPath,
332-
ScmConfigKeys.OZONE_SCM_DATANODE_ID_PATH_DEFAULT).toString();
333-
}
334-
return dataNodeIDPath;
335-
}
336-
337313
/**
338314
* Returns the hostname for this datanode. If the hostname is not
339315
* explicitly configured in the given config, then it is determined

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/hdds/scm/HddsServerUtil.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,26 @@ public static String getDefaultRatisDirectory(Configuration conf) {
372372
File metaDirPath = ServerUtils.getOzoneMetaDirPath(conf);
373373
return (new File(metaDirPath, "ratis")).getPath();
374374
}
375+
376+
/**
377+
* Get the path for datanode id file.
378+
*
379+
* @param conf - Configuration
380+
* @return the path of datanode id as string
381+
*/
382+
public static String getDatanodeIdFilePath(Configuration conf) {
383+
String dataNodeIDPath = conf.get(ScmConfigKeys.OZONE_SCM_DATANODE_ID);
384+
if (dataNodeIDPath == null) {
385+
File metaDirPath = ServerUtils.getOzoneMetaDirPath(conf);
386+
if (metaDirPath == null) {
387+
// this means meta data is not found, in theory should not happen at
388+
// this point because should've failed earlier.
389+
throw new IllegalArgumentException("Unable to locate meta data" +
390+
"directory when getting datanode id path");
391+
}
392+
dataNodeIDPath = new File(metaDirPath,
393+
ScmConfigKeys.OZONE_SCM_DATANODE_ID_PATH_DEFAULT).toString();
394+
}
395+
return dataNodeIDPath;
396+
}
375397
}

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/HddsDatanodeService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
2828
import org.apache.hadoop.hdds.protocol.DatanodeDetails;
2929
import org.apache.hadoop.hdds.protocol.SCMSecurityProtocol;
30+
import org.apache.hadoop.hdds.scm.HddsServerUtil;
3031
import org.apache.hadoop.hdds.scm.ScmConfigKeys;
3132
import org.apache.hadoop.hdds.security.x509.SecurityConfig;
3233
import org.apache.hadoop.hdds.security.x509.certificate.client.CertificateClient;
@@ -308,7 +309,7 @@ public PKCS10CertificationRequest getCSR(Configuration config)
308309
*/
309310
private DatanodeDetails initializeDatanodeDetails()
310311
throws IOException {
311-
String idFilePath = HddsUtils.getDatanodeIdFilePath(conf);
312+
String idFilePath = HddsServerUtil.getDatanodeIdFilePath(conf);
312313
if (idFilePath == null || idFilePath.isEmpty()) {
313314
LOG.error("A valid file path is needed for config setting {}",
314315
ScmConfigKeys.OZONE_SCM_DATANODE_ID);
@@ -338,7 +339,7 @@ private DatanodeDetails initializeDatanodeDetails()
338339
*/
339340
private void persistDatanodeDetails(DatanodeDetails dnDetails)
340341
throws IOException {
341-
String idFilePath = HddsUtils.getDatanodeIdFilePath(conf);
342+
String idFilePath = HddsServerUtil.getDatanodeIdFilePath(conf);
342343
if (idFilePath == null || idFilePath.isEmpty()) {
343344
LOG.error("A valid file path is needed for config setting {}",
344345
ScmConfigKeys.OZONE_SCM_DATANODE_ID);

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/states/datanode/InitDatanodeState.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import com.google.common.base.Strings;
2020
import org.apache.hadoop.conf.Configuration;
21-
import org.apache.hadoop.hdds.HddsUtils;
2221
import org.apache.hadoop.hdds.protocol.DatanodeDetails;
22+
import org.apache.hadoop.hdds.scm.HddsServerUtil;
2323
import org.apache.hadoop.hdds.scm.ScmConfigKeys;
2424
import org.apache.hadoop.ozone.container.common.helpers.ContainerUtils;
2525
import org.apache.hadoop.ozone.container.common.statemachine
@@ -117,7 +117,7 @@ public DatanodeStateMachine.DatanodeStates call() throws Exception {
117117
* Persist DatanodeDetails to datanode.id file.
118118
*/
119119
private void persistContainerDatanodeDetails() {
120-
String dataNodeIDPath = HddsUtils.getDatanodeIdFilePath(conf);
120+
String dataNodeIDPath = HddsServerUtil.getDatanodeIdFilePath(conf);
121121
if (Strings.isNullOrEmpty(dataNodeIDPath)) {
122122
LOG.error("A valid file path is needed for config setting {}",
123123
ScmConfigKeys.OZONE_SCM_DATANODE_ID);

hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/ServerUtils.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public static void releaseConnection(HttpRequestBase request) {
125125
* @return
126126
*/
127127
public static File getScmDbDir(Configuration conf) {
128-
File metadataDir = getDirectoryFromConfig(conf, ScmConfigKeys
129-
.OZONE_SCM_DB_DIRS, "SCM");
128+
File metadataDir = getDirectoryFromConfig(conf,
129+
ScmConfigKeys.OZONE_SCM_DB_DIRS, "SCM");
130130
if (metadataDir != null) {
131131
return metadataDir;
132132
}
@@ -146,8 +146,8 @@ public static File getScmDbDir(Configuration conf) {
146146
* @return File created from the value of the key in conf.
147147
*/
148148
public static File getDirectoryFromConfig(Configuration conf,
149-
String key,
150-
String componentName) {
149+
String key,
150+
String componentName) {
151151
final Collection<String> metadirs = conf.getTrimmedStringCollection(key);
152152

153153
if (metadirs.size() > 1) {
@@ -162,33 +162,28 @@ public static File getDirectoryFromConfig(Configuration conf,
162162
if (!dbDirPath.exists() && !dbDirPath.mkdirs()) {
163163
throw new IllegalArgumentException("Unable to create directory " +
164164
dbDirPath + " specified in configuration setting " +
165-
componentName);
165+
key);
166166
}
167167
return dbDirPath;
168168
}
169+
169170
return null;
170171
}
171172

172173
/**
173174
* Checks and creates Ozone Metadir Path if it does not exist.
174175
*
175176
* @param conf - Configuration
176-
*
177177
* @return File MetaDir
178+
* @throws IllegalArgumentException if the configuration setting is not set
178179
*/
179180
public static File getOzoneMetaDirPath(Configuration conf) {
180-
String metaDirPath = conf.getTrimmed(HddsConfigKeys.OZONE_METADATA_DIRS);
181-
182-
if (metaDirPath == null || metaDirPath.isEmpty()) {
181+
File dirPath = getDirectoryFromConfig(conf,
182+
HddsConfigKeys.OZONE_METADATA_DIRS, "Ozone");
183+
if (dirPath == null) {
183184
throw new IllegalArgumentException(
184185
HddsConfigKeys.OZONE_METADATA_DIRS + " must be defined.");
185186
}
186-
187-
File dirPath = new File(metaDirPath);
188-
if (!dirPath.exists() && !dirPath.mkdirs()) {
189-
throw new IllegalArgumentException("Unable to create paths. Path: " +
190-
dirPath);
191-
}
192187
return dirPath;
193188
}
194189

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
package org.apache.hadoop.hdds.server;
19+
20+
import org.apache.commons.io.FileUtils;
21+
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.hadoop.hdds.HddsConfigKeys;
23+
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
24+
import org.apache.hadoop.hdds.scm.ScmConfigKeys;
25+
import org.apache.hadoop.test.PathUtils;
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
import org.junit.rules.ExpectedException;
29+
30+
import java.io.File;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertFalse;
34+
import static org.junit.Assert.assertTrue;
35+
36+
/**
37+
* Unit tests for {@link ServerUtils}.
38+
*/
39+
public class TestServerUtils {
40+
41+
@Rule
42+
public ExpectedException thrown = ExpectedException.none();
43+
44+
/**
45+
* Test {@link ServerUtils#getScmDbDir}.
46+
*/
47+
@Test
48+
public void testGetScmDbDir() {
49+
final File testDir = PathUtils.getTestDir(TestServerUtils.class);
50+
final File dbDir = new File(testDir, "scmDbDir");
51+
final File metaDir = new File(testDir, "metaDir");
52+
final Configuration conf = new OzoneConfiguration();
53+
conf.set(ScmConfigKeys.OZONE_SCM_DB_DIRS, dbDir.getPath());
54+
conf.set(HddsConfigKeys.OZONE_METADATA_DIRS, metaDir.getPath());
55+
56+
try {
57+
assertFalse(metaDir.exists());
58+
assertFalse(dbDir.exists());
59+
assertEquals(dbDir, ServerUtils.getScmDbDir(conf));
60+
assertTrue(dbDir.exists());
61+
assertFalse(metaDir.exists());
62+
} finally {
63+
FileUtils.deleteQuietly(dbDir);
64+
}
65+
}
66+
67+
/**
68+
* Test {@link ServerUtils#getScmDbDir} with fallback to OZONE_METADATA_DIRS
69+
* when OZONE_SCM_DB_DIRS is undefined.
70+
*/
71+
@Test
72+
public void testGetScmDbDirWithFallback() {
73+
final File testDir = PathUtils.getTestDir(TestServerUtils.class);
74+
final File metaDir = new File(testDir, "metaDir");
75+
final Configuration conf = new OzoneConfiguration();
76+
conf.set(HddsConfigKeys.OZONE_METADATA_DIRS, metaDir.getPath());
77+
try {
78+
assertFalse(metaDir.exists());
79+
assertEquals(metaDir, ServerUtils.getScmDbDir(conf));
80+
assertTrue(metaDir.exists());
81+
} finally {
82+
FileUtils.deleteQuietly(metaDir);
83+
}
84+
}
85+
86+
@Test
87+
public void testNoScmDbDirConfigured() {
88+
thrown.expect(IllegalArgumentException.class);
89+
ServerUtils.getScmDbDir(new OzoneConfiguration());
90+
}
91+
92+
@Test
93+
public void ozoneMetadataDirIsMandatory() {
94+
thrown.expect(IllegalArgumentException.class);
95+
ServerUtils.getOzoneMetaDirPath(new OzoneConfiguration());
96+
}
97+
98+
@Test
99+
public void ozoneMetadataDirAcceptsSingleItem() {
100+
final File testDir = PathUtils.getTestDir(TestServerUtils.class);
101+
final File metaDir = new File(testDir, "metaDir");
102+
final Configuration conf = new OzoneConfiguration();
103+
conf.set(HddsConfigKeys.OZONE_METADATA_DIRS, metaDir.getPath());
104+
105+
try {
106+
assertFalse(metaDir.exists());
107+
assertEquals(metaDir, ServerUtils.getOzoneMetaDirPath(conf));
108+
assertTrue(metaDir.exists());
109+
} finally {
110+
FileUtils.deleteQuietly(metaDir);
111+
}
112+
}
113+
114+
@Test
115+
public void ozoneMetadataDirRejectsList() {
116+
final Configuration conf = new OzoneConfiguration();
117+
conf.set(HddsConfigKeys.OZONE_METADATA_DIRS, "/data/meta1,/data/meta2");
118+
thrown.expect(IllegalArgumentException.class);
119+
120+
ServerUtils.getOzoneMetaDirPath(conf);
121+
}
122+
123+
}

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ScmUtils.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.slf4j.LoggerFactory;
3030

3131
import java.io.File;
32-
import java.util.Collection;
3332

3433
/**
3534
* SCM utility class.
@@ -55,22 +54,9 @@ public static void preCheck(ScmOps operation, Precheck... preChecks)
5554
}
5655

5756
public static File getDBPath(Configuration conf, String dbDirectory) {
58-
final Collection<String> dbDirs =
59-
conf.getTrimmedStringCollection(dbDirectory);
60-
61-
if (dbDirs.size() > 1) {
62-
throw new IllegalArgumentException(
63-
"Bad configuration setting " + dbDirectory
64-
+ ". OM does not support multiple metadata dirs currently.");
65-
}
66-
67-
if (dbDirs.size() == 1) {
68-
final File dbDirPath = new File(dbDirs.iterator().next());
69-
if (!dbDirPath.exists() && !dbDirPath.mkdirs()) {
70-
throw new IllegalArgumentException(
71-
"Unable to create directory " + dbDirPath
72-
+ " specified in configuration setting " + dbDirectory);
73-
}
57+
final File dbDirPath =
58+
ServerUtils.getDirectoryFromConfig(conf, dbDirectory, "OM");
59+
if (dbDirPath != null) {
7460
return dbDirPath;
7561
}
7662

0 commit comments

Comments
 (0)