YARN-8048. Support auto-spawning of admin configured services during bootstrap of...
[hadoop.git] / hadoop-yarn-project / hadoop-yarn / hadoop-yarn-applications / hadoop-yarn-services / hadoop-yarn-services-core / src / test / java / org / apache / hadoop / yarn / service / TestSystemServiceManager.java
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.service;
20
21 import org.apache.hadoop.fs.Path;
22 import org.apache.hadoop.registry.client.api.RegistryOperations;
23 import org.apache.hadoop.yarn.api.records.ApplicationId;
24 import org.apache.hadoop.yarn.service.api.records.Artifact;
25 import org.apache.hadoop.yarn.service.api.records.ComponentState;
26 import org.apache.hadoop.yarn.service.api.records.Service;
27 import org.apache.hadoop.yarn.service.api.records.ServiceState;
28 import org.apache.hadoop.yarn.service.exceptions.SliderException;
29 import org.apache.hadoop.yarn.service.registry.YarnRegistryViewForProviders;
30 import org.apache.hadoop.yarn.service.utils.ServiceApiUtil;
31 import org.junit.Assert;
32 import org.junit.Rule;
33 import org.junit.Test;
34
35 import java.io.IOException;
36 import java.util.Map;
37
38 import static org.mockito.Mockito.mock;
39
40 /**
41 * Tests for {@link ServiceManager}.
42 */
43 public class TestSystemServiceManager {
44
45 @Rule
46 public ServiceTestUtils.ServiceFSWatcher rule =
47 new ServiceTestUtils.ServiceFSWatcher();
48
49 @Test
50 public void testUpgrade() throws IOException, SliderException {
51 ServiceManager serviceManager = createTestServiceManager("testUpgrade");
52 upgrade(serviceManager, "v2", false);
53 Assert.assertEquals("service not upgraded", ServiceState.UPGRADING,
54 serviceManager.getServiceSpec().getState());
55 }
56
57 @Test
58 public void testRestartNothingToUpgrade()
59 throws IOException, SliderException {
60 ServiceManager serviceManager = createTestServiceManager("testRestart");
61 upgrade(serviceManager, "v2", false);
62
63 //make components stable
64 serviceManager.getServiceSpec().getComponents().forEach(comp -> {
65 comp.setState(ComponentState.STABLE);
66 });
67 serviceManager.handle(new ServiceEvent(ServiceEventType.START));
68 Assert.assertEquals("service not re-started", ServiceState.STABLE,
69 serviceManager.getServiceSpec().getState());
70 }
71
72 @Test
73 public void testRestartWithPendingUpgrade()
74 throws IOException, SliderException {
75 ServiceManager serviceManager = createTestServiceManager("testRestart");
76 upgrade(serviceManager, "v2", true);
77 serviceManager.handle(new ServiceEvent(ServiceEventType.START));
78 Assert.assertEquals("service should still be upgrading",
79 ServiceState.UPGRADING, serviceManager.getServiceSpec().getState());
80 }
81
82
83 private void upgrade(ServiceManager service, String version,
84 boolean upgradeArtifact)
85 throws IOException, SliderException {
86 Service upgradedDef = ServiceTestUtils.createExampleApplication();
87 upgradedDef.setName(service.getName());
88 upgradedDef.setVersion(version);
89 if (upgradeArtifact) {
90 Artifact upgradedArtifact = createTestArtifact("2");
91 upgradedDef.getComponents().forEach(component -> {
92 component.setArtifact(upgradedArtifact);
93 });
94 }
95 writeUpgradedDef(upgradedDef);
96 ServiceEvent upgradeEvent = new ServiceEvent(ServiceEventType.UPGRADE);
97 upgradeEvent.setVersion("v2");
98 service.handle(upgradeEvent);
99 }
100
101 private ServiceManager createTestServiceManager(String name)
102 throws IOException {
103 ServiceContext context = new ServiceContext();
104 context.service = createBaseDef(name);
105 context.fs = rule.getFs();
106
107 context.scheduler = new ServiceScheduler(context) {
108 @Override
109 protected YarnRegistryViewForProviders createYarnRegistryOperations(
110 ServiceContext context, RegistryOperations registryClient) {
111 return mock(YarnRegistryViewForProviders.class);
112 }
113 };
114
115 context.scheduler.init(rule.getConf());
116
117 Map<String, org.apache.hadoop.yarn.service.component.Component>
118 componentState = context.scheduler.getAllComponents();
119 context.service.getComponents().forEach(component -> {
120 componentState.put(component.getName(),
121 new org.apache.hadoop.yarn.service.component.Component(component,
122 1L, context));
123 });
124 return new ServiceManager(context);
125 }
126
127 static Service createBaseDef(String name) {
128 ApplicationId applicationId = ApplicationId.newInstance(
129 System.currentTimeMillis(), 1);
130 Service serviceDef = ServiceTestUtils.createExampleApplication();
131 serviceDef.setId(applicationId.toString());
132 serviceDef.setName(name);
133 serviceDef.setState(ServiceState.STARTED);
134 Artifact artifact = createTestArtifact("1");
135
136 serviceDef.getComponents().forEach(component ->
137 component.setArtifact(artifact));
138 return serviceDef;
139 }
140
141 static Artifact createTestArtifact(String artifactId) {
142 Artifact artifact = new Artifact();
143 artifact.setId(artifactId);
144 artifact.setType(Artifact.TypeEnum.TARBALL);
145 return artifact;
146 }
147
148 private void writeUpgradedDef(Service upgradedDef)
149 throws IOException, SliderException {
150 Path upgradePath = rule.getFs().buildClusterUpgradeDirPath(
151 upgradedDef.getName(), upgradedDef.getVersion());
152 ServiceApiUtil.createDirAndPersistApp(rule.getFs(), upgradePath,
153 upgradedDef);
154 }
155
156 }