Skip to content

Commit b7070f3

Browse files
YARN-5874. RM -format-state-store and -remove-application-from-state-store commands fail with NPE. Contributed by Varun Saxena.
1 parent 296c5de commit b7070f3

File tree

5 files changed

+140
-19
lines changed

5 files changed

+140
-19
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ResourceManager.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,8 +1442,10 @@ public static InetSocketAddress getBindAddress(Configuration conf) {
14421442
* @param conf
14431443
* @throws Exception
14441444
*/
1445-
private static void deleteRMStateStore(Configuration conf) throws Exception {
1445+
@VisibleForTesting
1446+
static void deleteRMStateStore(Configuration conf) throws Exception {
14461447
RMStateStore rmStore = RMStateStoreFactory.getStore(conf);
1448+
rmStore.setResourceManager(new ResourceManager());
14471449
rmStore.init(conf);
14481450
rmStore.start();
14491451
try {
@@ -1455,9 +1457,11 @@ private static void deleteRMStateStore(Configuration conf) throws Exception {
14551457
}
14561458
}
14571459

1458-
private static void removeApplication(Configuration conf, String applicationId)
1460+
@VisibleForTesting
1461+
static void removeApplication(Configuration conf, String applicationId)
14591462
throws Exception {
14601463
RMStateStore rmStore = RMStateStoreFactory.getStore(conf);
1464+
rmStore.setResourceManager(new ResourceManager());
14611465
rmStore.init(conf);
14621466
rmStore.start();
14631467
try {

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/recovery/RMStateStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
public abstract class RMStateStore extends AbstractService {
8787

8888
// constants for RM App state and RMDTSecretManagerState.
89-
protected static final String RM_APP_ROOT = "RMAppRoot";
89+
@VisibleForTesting
90+
public static final String RM_APP_ROOT = "RMAppRoot";
9091
protected static final String RM_DT_SECRET_MANAGER_ROOT = "RMDTSecretManagerRoot";
9192
protected static final String DELEGATION_KEY_PREFIX = "DelegationKey_";
9293
protected static final String DELEGATION_TOKEN_PREFIX = "RMDelegationToken_";

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/recovery/ZKRMStateStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public class ZKRMStateStore extends RMStateStore {
127127
"RMDTSequentialNumber";
128128
private static final String RM_DT_MASTER_KEYS_ROOT_ZNODE_NAME =
129129
"RMDTMasterKeysRoot";
130-
protected static final String ROOT_ZNODE_NAME = "ZKRMStateRoot";
130+
@VisibleForTesting
131+
public static final String ROOT_ZNODE_NAME = "ZKRMStateRoot";
131132
protected static final Version CURRENT_VERSION_INFO =
132133
Version.newInstance(1, 3);
133134

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.hadoop.yarn.server.resourcemanager;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assert.fail;
24+
25+
import org.apache.curator.framework.CuratorFramework;
26+
import org.apache.curator.test.TestingServer;
27+
import org.apache.hadoop.conf.Configuration;
28+
import org.apache.hadoop.ha.HAServiceProtocol;
29+
import org.apache.hadoop.ha.HAServiceProtocol.StateChangeRequestInfo;
30+
import org.apache.hadoop.yarn.api.records.ApplicationId;
31+
import org.apache.hadoop.yarn.conf.YarnConfiguration;
32+
import org.apache.hadoop.yarn.server.resourcemanager.recovery.RMStateStore;
33+
import org.apache.hadoop.yarn.server.resourcemanager.recovery.TestZKRMStateStore;
34+
import org.apache.hadoop.yarn.server.resourcemanager.recovery.ZKRMStateStore;
35+
import org.junit.Test;
36+
37+
public class TestRMStoreCommands {
38+
39+
@Test
40+
public void testFormatStateStoreCmdForZK() throws Exception {
41+
StateChangeRequestInfo req = new StateChangeRequestInfo(
42+
HAServiceProtocol.RequestSource.REQUEST_BY_USER);
43+
try (TestingServer curatorTestingServer =
44+
TestZKRMStateStore.setupCuratorServer();
45+
CuratorFramework curatorFramework = TestZKRMStateStore.
46+
setupCuratorFramework(curatorTestingServer)) {
47+
Configuration conf = TestZKRMStateStore.createHARMConf("rm1,rm2", "rm1",
48+
1234, false, curatorTestingServer);
49+
ResourceManager rm = new MockRM(conf);
50+
rm.start();
51+
rm.getRMContext().getRMAdminService().transitionToActive(req);
52+
String zkStateRoot = ZKRMStateStore.ROOT_ZNODE_NAME;
53+
assertEquals("RM State store parent path should have a child node " +
54+
zkStateRoot, zkStateRoot, curatorFramework.getChildren().forPath(
55+
YarnConfiguration.DEFAULT_ZK_RM_STATE_STORE_PARENT_PATH).get(0));
56+
rm.close();
57+
try {
58+
ResourceManager.deleteRMStateStore(conf);
59+
} catch (Exception e) {
60+
fail("Exception should not be thrown during format rm state store" +
61+
" operation.");
62+
}
63+
assertTrue("After store format parent path should have no child nodes",
64+
curatorFramework.getChildren().forPath(
65+
YarnConfiguration.DEFAULT_ZK_RM_STATE_STORE_PARENT_PATH).isEmpty());
66+
}
67+
}
68+
69+
@Test
70+
public void testRemoveApplicationFromStateStoreCmdForZK() throws Exception {
71+
StateChangeRequestInfo req = new StateChangeRequestInfo(
72+
HAServiceProtocol.RequestSource.REQUEST_BY_USER);
73+
try (TestingServer curatorTestingServer =
74+
TestZKRMStateStore.setupCuratorServer();
75+
CuratorFramework curatorFramework = TestZKRMStateStore.
76+
setupCuratorFramework(curatorTestingServer)) {
77+
Configuration conf = TestZKRMStateStore.createHARMConf("rm1,rm2", "rm1",
78+
1234, false, curatorTestingServer);
79+
ResourceManager rm = new MockRM(conf);
80+
rm.start();
81+
rm.getRMContext().getRMAdminService().transitionToActive(req);
82+
rm.close();
83+
String appId = ApplicationId.newInstance(
84+
System.currentTimeMillis(), 1).toString();
85+
String appRootPath = YarnConfiguration.
86+
DEFAULT_ZK_RM_STATE_STORE_PARENT_PATH + "/"+
87+
ZKRMStateStore.ROOT_ZNODE_NAME + "/" + RMStateStore.RM_APP_ROOT;
88+
String appIdPath = appRootPath + "/" + appId;
89+
curatorFramework.create().forPath(appIdPath);
90+
assertEquals("Application node for " + appId + "should exist",
91+
appId, curatorFramework.getChildren().forPath(appRootPath).get(0));
92+
try {
93+
ResourceManager.removeApplication(conf, appId);
94+
} catch (Exception e) {
95+
fail("Exception should not be thrown while removing app from " +
96+
"rm state store.");
97+
}
98+
assertTrue("After remove app from store there should be no child nodes" +
99+
" in app root path",
100+
curatorFramework.getChildren().forPath(appRootPath).isEmpty());
101+
}
102+
}
103+
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/recovery/TestZKRMStateStore.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptMetrics;
5454
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptState;
5555
import org.apache.hadoop.yarn.server.resourcemanager.security.ClientToAMTokenSecretManagerInRM;
56-
import org.apache.hadoop.yarn.util.ConverterUtils;
5756
import org.apache.zookeeper.KeeperException;
5857
import org.apache.zookeeper.ZooDefs.Perms;
5958
import org.apache.zookeeper.data.ACL;
@@ -79,15 +78,26 @@ public class TestZKRMStateStore extends RMStateStoreTestBase {
7978
private TestingServer curatorTestingServer;
8079
private CuratorFramework curatorFramework;
8180

82-
@Before
83-
public void setupCuratorServer() throws Exception {
84-
curatorTestingServer = new TestingServer();
81+
public static TestingServer setupCuratorServer() throws Exception {
82+
TestingServer curatorTestingServer = new TestingServer();
8583
curatorTestingServer.start();
86-
curatorFramework = CuratorFrameworkFactory.builder()
84+
return curatorTestingServer;
85+
}
86+
87+
public static CuratorFramework setupCuratorFramework(
88+
TestingServer curatorTestingServer) throws Exception {
89+
CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
8790
.connectString(curatorTestingServer.getConnectString())
8891
.retryPolicy(new RetryNTimes(100, 100))
8992
.build();
9093
curatorFramework.start();
94+
return curatorFramework;
95+
}
96+
97+
@Before
98+
public void setupCurator() throws Exception {
99+
curatorTestingServer = setupCuratorServer();
100+
curatorFramework = setupCuratorFramework(curatorTestingServer);
91101
}
92102

93103
@After
@@ -243,19 +253,21 @@ protected synchronized void storeVersion() throws Exception {
243253
Assert.assertEquals(defaultVersion, store.loadVersion());
244254
}
245255

246-
private Configuration createHARMConf(
247-
String rmIds, String rmId, int adminPort) {
256+
public static Configuration createHARMConf(String rmIds, String rmId,
257+
int adminPort, boolean autoFailoverEnabled,
258+
TestingServer curatorTestServer) {
248259
Configuration conf = new YarnConfiguration();
249260
conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
250261
conf.set(YarnConfiguration.RM_HA_IDS, rmIds);
251262
conf.setBoolean(YarnConfiguration.RECOVERY_ENABLED, true);
252263
conf.set(YarnConfiguration.RM_STORE, ZKRMStateStore.class.getName());
253264
conf.set(YarnConfiguration.RM_ZK_ADDRESS,
254-
curatorTestingServer.getConnectString());
265+
curatorTestServer.getConnectString());
255266
conf.setInt(YarnConfiguration.RM_ZK_TIMEOUT_MS, ZK_TIMEOUT_MS);
256267
conf.set(YarnConfiguration.RM_HA_ID, rmId);
257268
conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS, "localhost:0");
258-
269+
conf.setBoolean(
270+
YarnConfiguration.AUTO_FAILOVER_ENABLED, autoFailoverEnabled);
259271
for (String rpcAddress : YarnConfiguration.getServiceAddressConfKeys(conf)) {
260272
for (String id : HAUtil.getRMHAIds(conf)) {
261273
conf.set(HAUtil.addSuffix(rpcAddress, id), "localhost:0");
@@ -293,8 +305,8 @@ public void testZKRootPathAcls() throws Exception {
293305
ZKRMStateStore.ROOT_ZNODE_NAME;
294306

295307
// Start RM with HA enabled
296-
Configuration conf = createHARMConf("rm1,rm2", "rm1", 1234);
297-
conf.setBoolean(YarnConfiguration.AUTO_FAILOVER_ENABLED, false);
308+
Configuration conf =
309+
createHARMConf("rm1,rm2", "rm1", 1234, false, curatorTestingServer);
298310
ResourceManager rm = new MockRM(conf);
299311
rm.start();
300312
rm.getRMContext().getRMAdminService().transitionToActive(req);
@@ -336,8 +348,8 @@ public void testFencing() throws Exception {
336348
StateChangeRequestInfo req = new StateChangeRequestInfo(
337349
HAServiceProtocol.RequestSource.REQUEST_BY_USER);
338350

339-
Configuration conf1 = createHARMConf("rm1,rm2", "rm1", 1234);
340-
conf1.setBoolean(YarnConfiguration.AUTO_FAILOVER_ENABLED, false);
351+
Configuration conf1 =
352+
createHARMConf("rm1,rm2", "rm1", 1234, false, curatorTestingServer);
341353
ResourceManager rm1 = new MockRM(conf1);
342354
rm1.start();
343355
rm1.getRMContext().getRMAdminService().transitionToActive(req);
@@ -347,8 +359,8 @@ public void testFencing() throws Exception {
347359
HAServiceProtocol.HAServiceState.ACTIVE,
348360
rm1.getRMContext().getRMAdminService().getServiceStatus().getState());
349361

350-
Configuration conf2 = createHARMConf("rm1,rm2", "rm2", 5678);
351-
conf2.setBoolean(YarnConfiguration.AUTO_FAILOVER_ENABLED, false);
362+
Configuration conf2 =
363+
createHARMConf("rm1,rm2", "rm2", 5678, false, curatorTestingServer);
352364
ResourceManager rm2 = new MockRM(conf2);
353365
rm2.start();
354366
rm2.getRMContext().getRMAdminService().transitionToActive(req);

0 commit comments

Comments
 (0)