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.yarn.server.resourcemanager.rmapp.attempt;
19 
20 import org.apache.hadoop.security.UserGroupInformation;
21 import org.apache.hadoop.service.Service;
22 import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
23 import org.apache.hadoop.yarn.conf.YarnConfiguration;
24 import org.apache.hadoop.yarn.event.Dispatcher;
25 import org.apache.hadoop.yarn.server.resourcemanager.MockRM;
26 import org.apache.hadoop.yarn.server.resourcemanager.recovery.MemoryRMStateStore;
27 import org.apache.hadoop.yarn.util.ControlledClock;
28 import org.apache.hadoop.yarn.util.SystemClock;
29 import org.junit.Assert;
30 import org.junit.Test;
31 
32 import static org.mockito.Mockito.mock;
33 
34 public class TestAMLivelinessMonitor {
35 
36   @Test(timeout = 10000)
testResetTimer()37   public void testResetTimer() throws Exception {
38     YarnConfiguration conf = new YarnConfiguration();
39     UserGroupInformation.setConfiguration(conf);
40     conf.set(YarnConfiguration.RECOVERY_ENABLED, "true");
41     conf.set(YarnConfiguration.RM_STORE, MemoryRMStateStore.class.getName());
42     conf.setBoolean(YarnConfiguration.RM_WORK_PRESERVING_RECOVERY_ENABLED, true);
43     conf.setInt(YarnConfiguration.RM_AM_EXPIRY_INTERVAL_MS, 6000);
44     final ControlledClock clock = new ControlledClock(new SystemClock());
45     clock.setTime(0);
46     MemoryRMStateStore memStore = new MemoryRMStateStore() {
47       @Override
48       public synchronized RMState loadState() throws Exception {
49         clock.setTime(8000);
50         return super.loadState();
51       }
52     };
53     memStore.init(conf);
54     final ApplicationAttemptId attemptId = mock(ApplicationAttemptId.class);
55     final Dispatcher dispatcher = mock(Dispatcher.class);
56     final boolean[] expired = new boolean[]{false};
57     final AMLivelinessMonitor monitor = new AMLivelinessMonitor(
58         dispatcher, clock) {
59       @Override
60       protected void expire(ApplicationAttemptId id) {
61         Assert.assertEquals(id, attemptId);
62         expired[0] = true;
63       }
64     };
65     monitor.register(attemptId);
66     MockRM rm = new MockRM(conf, memStore) {
67       @Override
68       protected AMLivelinessMonitor createAMLivelinessMonitor() {
69         return monitor;
70       }
71     };
72     rm.start();
73     // make sure that monitor has started
74     while (monitor.getServiceState() != Service.STATE.STARTED) {
75       Thread.sleep(100);
76     }
77     // expired[0] would be set to true without resetTimer
78     Assert.assertFalse(expired[0]);
79     rm.stop();
80   }
81 }
82