You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by hi...@apache.org on 2012/10/17 02:50:54 UTC
svn commit: r1399060 [3/3] - in /incubator/ambari/branches/AMBARI-666: ./
ambari-server/src/main/java/org/apache/ambari/server/agent/
ambari-server/src/main/java/org/apache/ambari/server/controller/
ambari-server/src/main/java/org/apache/ambari/server/...
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceComponentImpl.java Wed Oct 17 00:50:53 2012
@@ -21,6 +21,7 @@ package org.apache.ambari.server.state;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Map.Entry;
import com.google.gson.Gson;
import com.google.inject.Inject;
@@ -33,7 +34,6 @@ import org.apache.ambari.server.ServiceC
import org.apache.ambari.server.controller.ServiceComponentResponse;
import org.apache.ambari.server.orm.dao.*;
import org.apache.ambari.server.orm.entities.*;
-import org.apache.ambari.server.state.svccomphost.ServiceComponentHostImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -61,8 +61,6 @@ public class ServiceComponentImpl implem
boolean persisted = false;
private ServiceComponentDesiredStateEntity desiredStateEntity;
- private Map<String, Config> configs;
-
private Map<String, Config> desiredConfigs;
private Map<String, ServiceComponentHost> hostComponents;
@@ -83,7 +81,6 @@ public class ServiceComponentImpl implem
desiredStateEntity.setComponentName(componentName);
desiredStateEntity.setDesiredState(State.INIT);
- this.configs = new HashMap<String, Config>();
this.desiredConfigs = new HashMap<String, Config>();
setDesiredStackVersion(new StackVersion(""));
@@ -100,7 +97,6 @@ public class ServiceComponentImpl implem
this.service = service;
this.desiredStateEntity = serviceComponentDesiredStateEntity;
- this.configs = new HashMap<String, Config>();
this.desiredConfigs = new HashMap<String, Config>();
this.hostComponents = new HashMap<String, ServiceComponentHost>();
for (HostComponentStateEntity hostComponentStateEntity : desiredStateEntity.getHostComponentStateEntities()) {
@@ -135,16 +131,6 @@ public class ServiceComponentImpl implem
}
@Override
- public synchronized Map<String, Config> getConfigs() {
- return Collections.unmodifiableMap(configs);
- }
-
- @Override
- public synchronized void updateConfigs(Map<String, Config> configs) {
- this.configs = configs;
- }
-
- @Override
public synchronized Map<String, ServiceComponentHost>
getServiceComponentHosts() {
return Collections.unmodifiableMap(hostComponents);
@@ -154,6 +140,13 @@ public class ServiceComponentImpl implem
public synchronized void addServiceComponentHosts(
Map<String, ServiceComponentHost> hostComponents) throws AmbariException {
// TODO validation
+ for (Entry<String, ServiceComponentHost> entry :
+ hostComponents.entrySet()) {
+ if (!entry.getKey().equals(entry.getValue().getHostName())) {
+ throw new AmbariException("Invalid arguments in map"
+ + ", hostname does not match the key in map");
+ }
+ }
for (ServiceComponentHost sch : hostComponents.values()) {
addServiceComponentHost(sch);
}
@@ -269,7 +262,7 @@ public class ServiceComponentImpl implem
private synchronized Map<String, String> getConfigVersions() {
Map<String, String> configVersions = new HashMap<String, String>();
- for (Config c : configs.values()) {
+ for (Config c : desiredConfigs.values()) {
configVersions.put(c.getType(), c.getVersionTag());
}
return configVersions;
@@ -321,8 +314,6 @@ public class ServiceComponentImpl implem
@Transactional
public synchronized void persist() {
if (!persisted) {
- service.persist(); //TODO is this correct?
-
ClusterServiceEntityPK pk = new ClusterServiceEntityPK();
pk.setClusterId(service.getClusterId());
pk.setServiceName(service.getName());
@@ -358,5 +349,5 @@ public class ServiceComponentImpl implem
}
}
-
+
}
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/ServiceImpl.java Wed Oct 17 00:50:53 2012
@@ -102,7 +102,6 @@ public class ServiceImpl implements Serv
//TODO check for null states?
this.serviceDesiredStateEntity = serviceEntity.getServiceDesiredStateEntity();
- this.configs = new HashMap<String, Config>();
this.desiredConfigs = new HashMap<String, Config>();
this.components = new HashMap<String, ServiceComponent>();
@@ -132,16 +131,6 @@ public class ServiceImpl implements Serv
}
@Override
- public synchronized Map<String, Config> getConfigs() {
- return Collections.unmodifiableMap(configs);
- }
-
- @Override
- public synchronized void updateConfigs(Map<String, Config> configs) {
- this.configs.putAll(configs);
- }
-
- @Override
public synchronized void addServiceComponents(
Map<String, ServiceComponent> components) throws AmbariException {
for (ServiceComponent sc : components.values()) {
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java Wed Oct 17 00:50:53 2012
@@ -288,12 +288,12 @@ public class ClusterImpl implements Clus
}
@Override
- public synchronized Map<String, Config> getConfigsByType(String configType) {
+ public synchronized Map<String, Config> getDesiredConfigsByType(String configType) {
return Collections.unmodifiableMap(configs.get(configType));
}
@Override
- public synchronized Config getConfig(String configType, String versionTag) {
+ public synchronized Config getDesiredConfig(String configType, String versionTag) {
if (!configs.containsKey(configType)
|| !configs.get(configType).containsKey(versionTag)) {
// TODO throw error
@@ -302,7 +302,7 @@ public class ClusterImpl implements Clus
}
@Override
- public synchronized void addConfig(Config config) {
+ public synchronized void addDesiredConfig(Config config) {
if (config.getType() == null
|| config.getType().isEmpty()
|| config.getVersionTag() == null
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/host/HostImpl.java Wed Oct 17 00:50:53 2012
@@ -56,8 +56,10 @@ public class HostImpl implements Host {
private static final Log LOG = LogFactory.getLog(HostImpl.class);
private final Gson gson;
- private static final Type diskInfoType = new TypeToken<List<DiskInfo>>() {}.getType();
- private static final Type hostAttributesType = new TypeToken<Map<String, String>>() {}.getType();
+ private static final Type diskInfoType =
+ new TypeToken<List<DiskInfo>>() {}.getType();
+ private static final Type hostAttributesType =
+ new TypeToken<Map<String, String>>() {}.getType();
private final Lock readLock;
private final Lock writeLock;
@@ -152,7 +154,8 @@ public class HostImpl implements Host {
private final StateMachine<HostState, HostEventType, HostEvent> stateMachine;
@Inject
- public HostImpl(@Assisted HostEntity hostEntity, @Assisted boolean persisted, Injector injector) {
+ public HostImpl(@Assisted HostEntity hostEntity,
+ @Assisted boolean persisted, Injector injector) {
this.stateMachine = stateMachineFactory.make(this);
ReadWriteLock rwLock = new ReentrantReadWriteLock();
this.readLock = rwLock.readLock();
@@ -201,6 +204,7 @@ public class HostImpl implements Host {
host.importHostInfo(e.hostInfo);
host.setLastRegistrationTime(e.registrationTime);
host.setAgentVersion(e.agentVersion);
+ host.persist();
}
}
@@ -292,15 +296,38 @@ public class HostImpl implements Host {
public void importHostInfo(HostInfo hostInfo) {
try {
writeLock.lock();
- persisted = false;
- setHostName(hostInfo.getHostName());
+ if (hostInfo.getHostName() != null
+ && !hostInfo.getHostName().isEmpty()
+ && !hostInfo.getHostName().equals(getHostName())) {
+ setHostName(hostInfo.getHostName());
+ }
+ if (hostInfo.getIPAddress() != null
+ && !hostInfo.getIPAddress().isEmpty()) {
+ setIPv4(hostInfo.getIPAddress());
+ setIPv6(hostInfo.getIPAddress());
+ }
+
setCpuCount(hostInfo.getProcessorCount());
setTotalMemBytes(hostInfo.getMemoryTotal());
- setOsArch(hostInfo.getArchitecture());
- setOsType(hostInfo.getOS());
- setDisksInfo(hostInfo.getMounts());
setAvailableMemBytes(hostInfo.getFreeMemory());
+ if (hostInfo.getArchitecture() != null
+ && !hostInfo.getArchitecture().isEmpty()) {
+ setOsArch(hostInfo.getArchitecture());
+ }
+
+ if (hostInfo.getOS() != null
+ && !hostInfo.getOS().isEmpty()) {
+ setOsType(hostInfo.getOS());
+ }
+
+ if (hostInfo.getMounts() != null
+ && !hostInfo.getMounts().isEmpty()) {
+ setDisksInfo(hostInfo.getMounts());
+ }
+
+ // FIXME add all other information into host attributes
+
saveIfPersisted();
}
finally {
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostImpl.java Wed Oct 17 00:50:53 2012
@@ -443,13 +443,21 @@ public class ServiceComponentHostImpl im
this.serviceComponent = serviceComponent;
stateEntity = new HostComponentStateEntity();
+ stateEntity.setClusterId(serviceComponent.getClusterId());
+ stateEntity.setComponentName(serviceComponent.getName());
+ stateEntity.setServiceName(serviceComponent.getServiceName());
+ stateEntity.setHostName(hostName);
stateEntity.setCurrentState(stateMachine.getCurrentState());
stateEntity.setCurrentStackVersion(gson.toJson(new StackVersion("")));
desiredStateEntity = new HostComponentDesiredStateEntity();
+ desiredStateEntity.setClusterId(serviceComponent.getClusterId());
+ desiredStateEntity.setComponentName(serviceComponent.getName());
+ desiredStateEntity.setServiceName(serviceComponent.getServiceName());
+ desiredStateEntity.setHostName(hostName);
+ desiredStateEntity.setDesiredState(State.INIT);
desiredStateEntity.setDesiredStackVersion(gson.toJson(new StackVersion("")));
-
try {
this.host = clusters.getHost(hostName);
} catch (AmbariException e) {
@@ -457,6 +465,7 @@ public class ServiceComponentHostImpl im
LOG.error("Host '{}' was not found", hostName);
throw new RuntimeException(e);
}
+
this.resetLastOpInfo();
this.desiredConfigs = new HashMap<String, Config>();
this.configs = new HashMap<String, Config>();
@@ -848,7 +857,6 @@ public class ServiceComponentHostImpl im
try {
writeLock.lock();
if (!persisted) {
- serviceComponent.persist(); //TODO is this correct?
HostEntity hostEntity = hostDAO.findByName(getHostName());
hostEntity.getHostComponentStateEntities().add(stateEntity);
hostEntity.getHostComponentDesiredStateEntities().add(desiredStateEntity);
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/agent/TestHeartbeatHandler.java Wed Oct 17 00:50:53 2012
@@ -85,15 +85,17 @@ public class TestHeartbeatHandler {
InvalidStateTransitonException {
ActionManager am = new ActionManager(0, 0, null, null,
new ActionDBInMemoryImpl());
- Clusters fsm = mock(Clusters.class);
+ Clusters fsm = clusters;
String hostname = "host1";
HeartBeatHandler handler = new HeartBeatHandler(fsm, new ActionQueue(), am);
clusters.addHost(hostname);
Host hostObject = clusters.getHost(hostname);
- when(fsm.getHost(hostname)).thenReturn(hostObject);
+ hostObject.setIPv4("ipv4");
+ hostObject.setIPv6("ipv6");
Register reg = new Register();
HostInfo hi = new HostInfo();
+ hi.setHostName("host1");
hi.setOS("MegaOperatingSystem");
reg.setHostname(hostname);
reg.setHardwareProfile(hi);
@@ -109,14 +111,19 @@ public class TestHeartbeatHandler {
Clusters fsm = clusters;
String hostname = "host1";
fsm.addHost(hostname);
+ Host hostObject = clusters.getHost(hostname);
+ hostObject.setIPv4("ipv4");
+ hostObject.setIPv6("ipv6");
+
HeartBeatHandler handler = new HeartBeatHandler(fsm, new ActionQueue(), am);
Register reg = new Register();
HostInfo hi = new HostInfo();
+ hi.setHostName("host1");
hi.setOS("MegaOperatingSystem");
reg.setHostname(hostname);
reg.setHardwareProfile(hi);
RegistrationResponse response = handler.handleRegistration(reg);
- Host hostObject = fsm.getHost(hostname);
+
assertEquals(hostObject.getState(), HostState.HEALTHY);
assertEquals("MegaOperatingSystem", hostObject.getOsType());
assertEquals(RegistrationStatus.OK, response.getResponseStatus());
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java Wed Oct 17 00:50:53 2012
@@ -29,11 +29,7 @@ import junit.framework.Assert;
import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.actionmanager.ActionDBAccessor;
-import org.apache.ambari.server.actionmanager.ActionDBInMemoryImpl;
-import org.apache.ambari.server.actionmanager.ActionManager;
import org.apache.ambari.server.actionmanager.Stage;
-import org.apache.ambari.server.agent.ActionQueue;
-import org.apache.ambari.server.configuration.Configuration;
import org.apache.ambari.server.orm.GuiceJpaInitializer;
import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
import org.apache.ambari.server.state.Clusters;
@@ -43,7 +39,6 @@ import org.apache.ambari.server.state.St
import org.apache.ambari.server.utils.StageUtils;
import org.junit.After;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -63,8 +58,6 @@ public class AmbariManagementControllerT
injector = Guice.createInjector(new InMemoryDefaultTestModule());
injector.getInstance(GuiceJpaInitializer.class);
clusters = injector.getInstance(Clusters.class);
-// ActionManager am = new ActionManager(5000, 1200000, new ActionQueue(),
-// clusters, db);
db = injector.getInstance(ActionDBAccessor.class);
controller = injector.getInstance(AmbariManagementController.class);
}
@@ -92,12 +85,26 @@ public class AmbariManagementControllerT
if (desiredState != null) {
dStateStr = desiredState.toString();
}
- ServiceRequest r = new ServiceRequest(clusterName, serviceName, null,
+ ServiceRequest r1 = new ServiceRequest(clusterName, serviceName, null,
+ dStateStr);
+ ServiceRequest r2 = new ServiceRequest(clusterName, serviceName, null,
dStateStr);
- controller.createService(r);
+ Set<ServiceRequest> requests = new HashSet<ServiceRequest>();
+ requests.add(r1);
+ requests.add(r2);
+ try {
+ controller.createServices(requests);
+ fail("Expected error for duplicates");
+ } catch (Exception e) {
+ // Expected
+ }
+
+ requests.clear();
+ requests.add(r1);
+ controller.createServices(requests);
try {
- controller.createService(r);
+ controller.createServices(requests);
fail("Duplicate Service creation should fail");
} catch (Exception e) {
// Expected
@@ -406,7 +413,7 @@ public class AmbariManagementControllerT
// TODO validate stages?
List<Stage> stages = db.getAllStages(1);
- Assert.assertEquals(2, stages.size());
+ Assert.assertEquals(1, stages.size());
for (Stage stage : stages) {
LOG.info("Stage Details for Install Service"
@@ -440,7 +447,7 @@ public class AmbariManagementControllerT
// TODO validate stages?
stages = db.getAllStages(2);
- Assert.assertEquals(2, stages.size());
+ Assert.assertEquals(1, stages.size());
for (Stage stage : stages) {
LOG.info("Stage Details for Start Service"
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterControllerImplTest.java Wed Oct 17 00:50:53 2012
@@ -28,7 +28,6 @@ import org.apache.ambari.server.controll
import org.apache.ambari.server.controller.spi.Request;
import org.apache.ambari.server.controller.spi.Resource;
import org.apache.ambari.server.controller.spi.ResourceProvider;
-import org.apache.ambari.server.controller.spi.Schema;
import org.apache.ambari.server.controller.utilities.PredicateBuilder;
import org.junit.Ignore;
import org.junit.Test;
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ResourceProviderImplTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ResourceProviderImplTest.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ResourceProviderImplTest.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ResourceProviderImplTest.java Wed Oct 17 00:50:53 2012
@@ -60,9 +60,8 @@ public class ResourceProviderImplTest {
AmbariManagementController managementController = createMock(AmbariManagementController.class);
TrackActionResponse response = createNiceMock(TrackActionResponse.class);
- // set expectations
- expect(managementController.createCluster(Matchers.clusterRequest(null, "Cluster100", "4.02", null))).andReturn(response).once();
- expect(managementController.createCluster(Matchers.clusterRequest(99L, null, "4.03", null))).andReturn(response).once();
+ managementController.createCluster(Matchers.clusterRequest(null, "Cluster100", "4.02", null));
+ managementController.createCluster(Matchers.clusterRequest(99L, null, "4.03", null));
// replay
replay(managementController, response);
@@ -264,8 +263,7 @@ public class ResourceProviderImplTest {
AmbariManagementController managementController = createMock(AmbariManagementController.class);
TrackActionResponse response = createNiceMock(TrackActionResponse.class);
- // set expectations
- expect(managementController.createService(Matchers.serviceRequest("Cluster100", "Service100", null, "DEPLOYED"))).andReturn(response).once();
+ managementController.createService(Matchers.serviceRequest("Cluster100", "Service100", null, "DEPLOYED"));
// replay
replay(managementController, response);
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java?rev=1399060&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java Wed Oct 17 00:50:53 2012
@@ -0,0 +1,291 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.state;
+
+import static org.junit.Assert.fail;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.controller.ServiceComponentResponse;
+import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
+import org.apache.ambari.server.orm.dao.HostComponentStateDAO;
+import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
+import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntityPK;
+import org.apache.ambari.server.orm.entities.HostComponentStateEntity;
+import org.apache.ambari.server.orm.entities.HostComponentStateEntityPK;
+import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
+import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntityPK;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.persist.PersistService;
+
+public class ServiceComponentTest {
+
+ private Clusters clusters;
+ private Cluster cluster;
+ private Service service;
+ private String clusterName;
+ private String serviceName;
+ private Injector injector;
+ private ServiceFactory serviceFactory;
+ private ServiceComponentFactory serviceComponentFactory;
+ private ServiceComponentHostFactory serviceComponentHostFactory;
+
+ @Before
+ public void setup() throws AmbariException {
+ injector = Guice.createInjector(new InMemoryDefaultTestModule());
+ injector.getInstance(GuiceJpaInitializer.class);
+ clusters = injector.getInstance(Clusters.class);
+ serviceFactory = injector.getInstance(ServiceFactory.class);
+ serviceComponentFactory = injector.getInstance(
+ ServiceComponentFactory.class);
+ serviceComponentHostFactory = injector.getInstance(
+ ServiceComponentHostFactory.class);
+ clusterName = "foo";
+ serviceName = "s1";
+ clusters.addCluster(clusterName);
+ cluster = clusters.getCluster(clusterName);
+ Assert.assertNotNull(cluster);
+ Service s = serviceFactory.createNew(cluster, serviceName);
+ cluster.addService(s);
+ s.persist();
+ service = cluster.getService(serviceName);
+ Assert.assertNotNull(service);
+ }
+
+ @After
+ public void teardown() throws AmbariException {
+ injector.getInstance(PersistService.class).stop();
+ }
+
+ @Test
+ public void testCreateServiceComponent() throws AmbariException {
+ String componentName = "sc1";
+ ServiceComponent component = serviceComponentFactory.createNew(service,
+ componentName);
+ service.addServiceComponent(component);
+ component.persist();
+
+ ServiceComponent sc = service.getServiceComponent(componentName);
+ Assert.assertNotNull(sc);
+
+ Assert.assertEquals(componentName, sc.getName());
+ Assert.assertEquals(serviceName, sc.getServiceName());
+ Assert.assertEquals(cluster.getClusterId(),
+ sc.getClusterId());
+ Assert.assertEquals(cluster.getClusterName(),
+ sc.getClusterName());
+ Assert.assertEquals(State.INIT, sc.getDesiredState());
+ Assert.assertTrue(
+ sc.getDesiredStackVersion().getStackVersion().isEmpty());
+ }
+
+
+ @Test
+ public void testGetAndSetServiceComponentInfo() throws AmbariException {
+ String componentName = "sc1";
+ ServiceComponent component = serviceComponentFactory.createNew(service,
+ componentName);
+ service.addServiceComponent(component);
+ component.persist();
+
+ ServiceComponent sc = service.getServiceComponent(componentName);
+ Assert.assertNotNull(sc);
+
+
+ sc.setDesiredState(State.INSTALLED);
+ Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
+
+ sc.setDesiredStackVersion(new StackVersion("1.0.0"));
+ Assert.assertEquals("1.0.0", sc.getDesiredStackVersion().getStackVersion());
+
+ ServiceComponentDesiredStateDAO serviceComponentDesiredStateDAO =
+ injector.getInstance(ServiceComponentDesiredStateDAO.class);
+
+ ServiceComponentDesiredStateEntityPK primaryKey =
+ new ServiceComponentDesiredStateEntityPK();
+ primaryKey.setClusterId(cluster.getClusterId());
+ primaryKey.setComponentName(componentName);
+ primaryKey.setServiceName(serviceName);
+
+ ServiceComponentDesiredStateEntity serviceComponentDesiredStateEntity =
+ serviceComponentDesiredStateDAO.findByPK(primaryKey);
+
+ ServiceComponent sc1 = serviceComponentFactory.createExisting(service,
+ serviceComponentDesiredStateEntity);
+ Assert.assertNotNull(sc1);
+ Assert.assertEquals(State.INSTALLED, sc1.getDesiredState());
+ Assert.assertEquals("1.0.0",
+ sc1.getDesiredStackVersion().getStackVersion());
+
+ }
+
+ @Test
+ public void testGetAndSetConfigs() {
+ // FIXME add unit tests for configs once impl done
+ /*
+ public Map<String, Config> getDesiredConfigs();
+ public void updateDesiredConfigs(Map<String, Config> configs);
+ */
+ }
+
+ private void addHostToCluster(String hostname) throws AmbariException {
+ clusters.addHost(hostname);
+ Host h = clusters.getHost(hostname);
+ h.setIPv4(hostname + "ipv4");
+ h.setIPv6(hostname + "ipv6");
+ h.persist();
+ }
+
+ @Test
+ public void testAddAndGetServiceComponentHosts() throws AmbariException {
+ String componentName = "sc1";
+ ServiceComponent component = serviceComponentFactory.createNew(service,
+ componentName);
+ service.addServiceComponent(component);
+ component.persist();
+
+ ServiceComponent sc = service.getServiceComponent(componentName);
+ Assert.assertNotNull(sc);
+
+ Assert.assertTrue(sc.getServiceComponentHosts().isEmpty());
+
+ try {
+ serviceComponentHostFactory.createNew(sc, "h1", false);
+ fail("Expected error for invalid host");
+ } catch (Exception e) {
+ // Expected
+ }
+
+ addHostToCluster("h1");
+ addHostToCluster("h2");
+
+ ServiceComponentHost sch1 =
+ serviceComponentHostFactory.createNew(sc, "h1", false);
+ ServiceComponentHost sch2 =
+ serviceComponentHostFactory.createNew(sc, "h2", false);
+ ServiceComponentHost failSch =
+ serviceComponentHostFactory.createNew(sc, "h2", false);
+
+ Map<String, ServiceComponentHost> compHosts =
+ new HashMap<String, ServiceComponentHost>();
+ compHosts.put("h1", sch1);
+ compHosts.put("h2", sch2);
+ compHosts.put("h3", failSch);
+
+ try {
+ sc.addServiceComponentHosts(compHosts);
+ fail("Expected error for dups");
+ } catch (Exception e) {
+ // Expected
+ }
+ Assert.assertTrue(sc.getServiceComponentHosts().isEmpty());
+
+ compHosts.remove("h3");
+ sc.addServiceComponentHosts(compHosts);
+
+ Assert.assertEquals(2, sc.getServiceComponentHosts().size());
+
+ sch1.persist();
+ sch2.persist();
+
+ sch1.setDesiredStackVersion(new StackVersion("1.1.0"));
+ sch1.setState(State.STARTING);
+ sch1.setStackVersion(new StackVersion("1.0.0"));
+ sch1.setDesiredState(State.STARTED);
+
+ HostComponentDesiredStateDAO desiredStateDAO = injector.getInstance(
+ HostComponentDesiredStateDAO.class);
+ HostComponentStateDAO liveStateDAO = injector.getInstance(
+ HostComponentStateDAO.class);
+
+ HostComponentDesiredStateEntityPK dPK =
+ new HostComponentDesiredStateEntityPK();
+ HostComponentStateEntityPK lPK =
+ new HostComponentStateEntityPK();
+
+ dPK.setClusterId(cluster.getClusterId());
+ dPK.setComponentName(componentName);
+ dPK.setHostName("h1");
+ dPK.setServiceName(serviceName);
+ lPK.setClusterId(cluster.getClusterId());
+ lPK.setComponentName(componentName);
+ lPK.setHostName("h1");
+ lPK.setServiceName(serviceName);
+
+ HostComponentDesiredStateEntity desiredStateEntity =
+ desiredStateDAO.findByPK(dPK);
+ HostComponentStateEntity stateEntity =
+ liveStateDAO.findByPK(lPK);
+
+ ServiceComponentHost sch = serviceComponentHostFactory.createExisting(sc,
+ stateEntity, desiredStateEntity);
+ Assert.assertNotNull(sch);
+ Assert.assertEquals(State.STARTING, sch.getState());
+ Assert.assertEquals(State.STARTED, sch.getDesiredState());
+ Assert.assertEquals("1.0.0",
+ sch.getStackVersion().getStackVersion());
+ Assert.assertEquals("1.1.0",
+ sch.getDesiredStackVersion().getStackVersion());
+ }
+
+ @Test
+ public void testConvertToResponse() throws AmbariException {
+ // TODO
+ String componentName = "sc1";
+ ServiceComponent component = serviceComponentFactory.createNew(service,
+ componentName);
+ service.addServiceComponent(component);
+ component.persist();
+
+ ServiceComponent sc = service.getServiceComponent(componentName);
+ Assert.assertNotNull(sc);
+ sc.setDesiredState(State.INSTALLED);
+ sc.setDesiredStackVersion(new StackVersion("1.0.0"));
+
+ ServiceComponentResponse r = sc.convertToResponse();
+ Assert.assertEquals(sc.getClusterName(), r.getClusterName());
+ Assert.assertEquals(sc.getClusterId(), r.getClusterId().longValue());
+ Assert.assertEquals(sc.getName(), r.getComponentName());
+ Assert.assertEquals(sc.getServiceName(), r.getServiceName());
+ Assert.assertEquals(sc.getDesiredStackVersion().getStackVersion(),
+ r.getDesiredStackVersion());
+ Assert.assertEquals(sc.getDesiredState().toString(),
+ r.getDesiredState());
+
+ // TODO check configs
+ // r.getConfigVersions()
+
+ // TODO test debug dump
+ StringBuilder sb = new StringBuilder();
+ sc.debugDump(sb);
+ }
+
+}
Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceComponentTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/ServiceTest.java Wed Oct 17 00:50:53 2012
@@ -18,15 +18,20 @@
package org.apache.ambari.server.state;
+import static org.junit.Assert.fail;
+
+import java.util.HashMap;
+import java.util.Map;
+
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.persist.PersistService;
import junit.framework.Assert;
import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.controller.ServiceResponse;
import org.apache.ambari.server.orm.GuiceJpaInitializer;
import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.state.cluster.ClustersImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -38,6 +43,7 @@ public class ServiceTest {
private String clusterName;
private Injector injector;
private ServiceFactory serviceFactory;
+ private ServiceComponentFactory serviceComponentFactory;
@Before
public void setup() throws AmbariException {
@@ -45,24 +51,27 @@ public class ServiceTest {
injector.getInstance(GuiceJpaInitializer.class);
clusters = injector.getInstance(Clusters.class);
serviceFactory = injector.getInstance(ServiceFactory.class);
+ serviceComponentFactory = injector.getInstance(
+ ServiceComponentFactory.class);
clusterName = "foo";
clusters.addCluster(clusterName);
cluster = clusters.getCluster(clusterName);
Assert.assertNotNull(cluster);
}
-
+
@After
public void teardown() throws AmbariException {
injector.getInstance(PersistService.class).stop();
}
-
+
@Test
public void testCreateService() throws AmbariException {
String serviceName = "s1";
Service s = serviceFactory.createNew(cluster, serviceName);
cluster.addService(s);
+ s.persist();
Service service = cluster.getService(serviceName);
-
+
Assert.assertNotNull(service);
Assert.assertEquals(serviceName, service.getName());
Assert.assertEquals(cluster.getClusterId(),
@@ -79,53 +88,118 @@ public class ServiceTest {
String serviceName = "s1";
Service s = serviceFactory.createNew(cluster, serviceName);
cluster.addService(s);
- Service service = cluster.getService(serviceName);
+ s.persist();
+ Service service = cluster.getService(serviceName);
Assert.assertNotNull(service);
service.setDesiredStackVersion(new StackVersion("1.1.0"));
Assert.assertEquals("1.1.0",
service.getDesiredStackVersion().getStackVersion());
-
service.setDesiredState(State.INSTALLING);
Assert.assertEquals(State.INSTALLING, service.getDesiredState());
- }
-
-
-
- /*
-
-
+ // FIXME todo use DAO to verify persisted object maps to inmemory state
- public ServiceComponent getServiceComponent(String componentName)
- throws AmbariException;
-
- public Map<String, ServiceComponent> getServiceComponents();
+ }
- public void addServiceComponents(Map<String, ServiceComponent> components)
- throws AmbariException;
- public void addServiceComponent(ServiceComponent component)
- throws AmbariException;
+ @Test
+ public void testAddAndGetServiceComponents() throws AmbariException {
+ String serviceName = "s1";
+ Service s = serviceFactory.createNew(cluster, serviceName);
+ cluster.addService(s);
+ s.persist();
+ Service service = cluster.getService(serviceName);
- public Map<String, Config> getDesiredConfigs();
+ Assert.assertNotNull(service);
- public void updateDesiredConfigs(Map<String, Config> configs);
+ Assert.assertTrue(s.getServiceComponents().isEmpty());
+ ServiceComponent sc1 =
+ serviceComponentFactory.createNew(s, "sc1");
+ ServiceComponent sc2 =
+ serviceComponentFactory.createNew(s, "sc2");
+ ServiceComponent sc3 =
+ serviceComponentFactory.createNew(s, "sc3");
+
+ Map<String, ServiceComponent> comps = new
+ HashMap<String, ServiceComponent>();
+ comps.put(sc1.getName(), sc1);
+ comps.put(sc2.getName(), sc2);
+
+ s.addServiceComponents(comps);
+
+ Assert.assertEquals(2, s.getServiceComponents().size());
+ Assert.assertNotNull(s.getServiceComponent(sc1.getName()));
+ Assert.assertNotNull(s.getServiceComponent(sc2.getName()));
+
+ try {
+ s.getServiceComponent(sc3.getName());
+ fail("Expected error when looking for invalid component");
+ } catch (Exception e) {
+ // Expected
+ }
+
+ s.addServiceComponent(sc3);
+
+ sc1.persist();
+ sc2.persist();
+ sc3.persist();
+
+ Assert.assertNotNull(s.getServiceComponent(sc3.getName()));
+ Assert.assertEquals(3, s.getServiceComponents().size());
+ Assert.assertEquals(sc3.getName(),
+ s.getServiceComponent(sc3.getName()).getName());
+ Assert.assertEquals(s.getName(),
+ s.getServiceComponent(sc3.getName()).getServiceName());
+ Assert.assertEquals(cluster.getClusterName(),
+ s.getServiceComponent(sc3.getName()).getClusterName());
+ }
- public Map<String, Config> getConfigs();
+ @Test
+ public void testGetAndSetConfigs() {
+ // FIXME add unit tests for configs once impl done
+ /*
+ public Map<String, Config> getDesiredConfigs();
+ public void updateDesiredConfigs(Map<String, Config> configs);
+ public Map<String, Config> getConfigs();
+ public void updateConfigs(Map<String, Config> configs);
+ */
+ }
- public void updateConfigs(Map<String, Config> configs);
- public StackVersion getStackVersion();
+ @Test
+ public void testConvertToResponse() throws AmbariException {
+ String serviceName = "s1";
+ Service s = serviceFactory.createNew(cluster, serviceName);
+ cluster.addService(s);
+ Service service = cluster.getService(serviceName);
+ Assert.assertNotNull(service);
- public void setStackVersion(StackVersion stackVersion);
+ ServiceResponse r = s.convertToResponse();
+ Assert.assertEquals(s.getName(), r.getServiceName());
+ Assert.assertEquals(s.getCluster().getClusterName(),
+ r.getClusterName());
+ Assert.assertEquals(s.getDesiredStackVersion().getStackVersion(),
+ r.getDesiredStackVersion());
+ Assert.assertEquals(s.getDesiredState().toString(),
+ r.getDesiredState());
- public ServiceResponse convertToResponse();
+ service.setDesiredStackVersion(new StackVersion("1.1.0"));
+ service.setDesiredState(State.INSTALLING);
+ r = s.convertToResponse();
+ Assert.assertEquals(s.getName(), r.getServiceName());
+ Assert.assertEquals(s.getCluster().getClusterName(),
+ r.getClusterName());
+ Assert.assertEquals(s.getDesiredStackVersion().getStackVersion(),
+ r.getDesiredStackVersion());
+ Assert.assertEquals(s.getDesiredState().toString(),
+ r.getDesiredState());
+ // FIXME add checks for configs
+ }
- */
}
Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java?rev=1399060&r1=1399059&r2=1399060&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java Wed Oct 17 00:50:53 2012
@@ -22,7 +22,6 @@ import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import com.google.inject.Guice;
import com.google.inject.Injector;
@@ -34,16 +33,11 @@ import org.apache.ambari.server.agent.Di
import org.apache.ambari.server.agent.HostInfo;
import org.apache.ambari.server.orm.GuiceJpaInitializer;
import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
-import org.apache.ambari.server.controller.ClusterResponse;
import org.apache.ambari.server.state.AgentVersion;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
-import org.apache.ambari.server.state.Config;
import org.apache.ambari.server.state.HostState;
-import org.apache.ambari.server.state.Service;
-import org.apache.ambari.server.state.ServiceComponentHost;
import org.apache.ambari.server.state.StackVersion;
-import org.apache.ambari.server.state.cluster.ClustersImpl;
import org.apache.ambari.server.state.fsm.InvalidStateTransitonException;
import org.apache.ambari.server.state.host.HostHealthyHeartbeatEvent;
import org.apache.ambari.server.state.host.HostRegistrationRequestEvent;
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java?rev=1399060&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java Wed Oct 17 00:50:53 2012
@@ -0,0 +1,299 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.state.host;
+
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.persist.PersistService;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.agent.DiskInfo;
+import org.apache.ambari.server.agent.HostInfo;
+import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.state.*;
+import org.apache.ambari.server.state.HostHealthStatus.HealthStatus;
+import org.apache.ambari.server.state.host.HostHealthyHeartbeatEvent;
+import org.apache.ambari.server.state.host.HostHeartbeatLostEvent;
+import org.apache.ambari.server.state.host.HostRegistrationRequestEvent;
+import org.apache.ambari.server.state.host.HostStatusUpdatesReceivedEvent;
+import org.apache.ambari.server.state.host.HostUnhealthyHeartbeatEvent;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class HostTest {
+
+ private Injector injector;
+ private Clusters clusters;
+
+ @Before
+ public void setup() throws AmbariException{
+ injector = Guice.createInjector(new InMemoryDefaultTestModule());
+ injector.getInstance(GuiceJpaInitializer.class);
+ clusters = injector.getInstance(Clusters.class);
+ }
+
+ @After
+ public void teardown() throws AmbariException {
+ injector.getInstance(PersistService.class).stop();
+ }
+
+ @Test
+ public void testHostInfoImport() throws AmbariException{
+ HostInfo info = new HostInfo();
+ info.setMemorySize(100);
+ info.setProcessorCount(10);
+ List<DiskInfo> mounts = new ArrayList<DiskInfo>();
+ mounts.add(new DiskInfo("/dev/sda", "/mnt/disk1",
+ "5000000", "4000000", "10%", "size"));
+ info.setMounts(mounts);
+
+ info.setHostName("foo");
+ info.setInterfaces("fip_4");
+ info.setArchitecture("os_arch");
+ info.setOS("os_type");
+ info.setMemoryTotal(10);
+
+ clusters.addHost("foo");
+ Host host = clusters.getHost("foo");
+
+ host.importHostInfo(info);
+
+ Assert.assertEquals(info.getHostName(), host.getHostName());
+ Assert.assertEquals(info.getFreeMemory(), host.getAvailableMemBytes());
+ Assert.assertEquals(info.getMemoryTotal(), host.getTotalMemBytes());
+ Assert.assertEquals(info.getProcessorCount(), host.getCpuCount());
+ Assert.assertEquals(info.getMounts().size(), host.getDisksInfo().size());
+ Assert.assertEquals(info.getArchitecture(), host.getOsArch());
+ Assert.assertEquals(info.getOS(), host.getOsType());
+ }
+
+ private void registerHost(Host host) throws Exception {
+ registerHost(host, true);
+ }
+
+ private void registerHost(Host host, boolean firstReg) throws Exception {
+ HostInfo info = new HostInfo();
+ info.setMemorySize(100);
+ info.setProcessorCount(10);
+ List<DiskInfo> mounts = new ArrayList<DiskInfo>();
+ mounts.add(new DiskInfo("/dev/sda", "/mnt/disk1",
+ "5000000", "4000000", "10%", "size"));
+ info.setMounts(mounts);
+
+ info.setHostName("foo");
+ info.setInterfaces("fip_4");
+ info.setArchitecture("os_arch");
+ info.setOS("os_type");
+ info.setMemoryTotal(10);
+
+ AgentVersion agentVersion = null;
+ long currentTime = System.currentTimeMillis();
+
+ HostRegistrationRequestEvent e =
+ new HostRegistrationRequestEvent("foo", agentVersion, currentTime,
+ info);
+ if (!firstReg) {
+ Assert.assertTrue(host.isPersisted());
+ }
+ host.handleEvent(e);
+ Assert.assertEquals(currentTime, host.getLastRegistrationTime());
+ }
+
+ private void ensureHostUpdatesReceived(Host host) throws Exception {
+ HostStatusUpdatesReceivedEvent e =
+ new HostStatusUpdatesReceivedEvent(host.getHostName(), 1);
+ host.handleEvent(e);
+ }
+
+ private void verifyHostState(Host host, HostState state) {
+ Assert.assertEquals(state, host.getState());
+ }
+
+ private void sendHealthyHeartbeat(Host host, long counter)
+ throws Exception {
+ HostHealthyHeartbeatEvent e = new HostHealthyHeartbeatEvent(
+ host.getHostName(), counter);
+ host.handleEvent(e);
+ }
+
+ private void sendUnhealthyHeartbeat(Host host, long counter)
+ throws Exception {
+ HostHealthStatus healthStatus = new HostHealthStatus(HealthStatus.UNHEALTHY,
+ "Unhealthy server");
+ HostUnhealthyHeartbeatEvent e = new HostUnhealthyHeartbeatEvent(
+ host.getHostName(), counter, healthStatus);
+ host.handleEvent(e);
+ }
+
+ private void timeoutHost(Host host) throws Exception {
+ HostHeartbeatLostEvent e = new HostHeartbeatLostEvent(
+ host.getHostName());
+ host.handleEvent(e);
+ }
+
+ @Test
+ public void testHostFSMInit() throws AmbariException{
+ clusters.addHost("foo");
+ Host host = clusters.getHost("foo");
+ verifyHostState(host, HostState.INIT);
+ }
+
+ @Test
+ public void testHostRegistrationFlow() throws Exception {
+ clusters.addHost("foo");
+ Host host = clusters.getHost("foo");
+ registerHost(host);
+ verifyHostState(host, HostState.WAITING_FOR_HOST_STATUS_UPDATES);
+
+ boolean exceptionThrown = false;
+ try {
+ registerHost(host);
+ } catch (Exception e) {
+ // Expected
+ exceptionThrown = true;
+ }
+ if (!exceptionThrown) {
+ fail("Expected invalid transition exception to be thrown");
+ }
+
+ ensureHostUpdatesReceived(host);
+ verifyHostState(host, HostState.HEALTHY);
+
+ exceptionThrown = false;
+ try {
+ ensureHostUpdatesReceived(host);
+ } catch (Exception e) {
+ // Expected
+ exceptionThrown = true;
+ }
+ if (!exceptionThrown) {
+ fail("Expected invalid transition exception to be thrown");
+ }
+ }
+
+ @Test
+ public void testHostHeartbeatFlow() throws Exception {
+ clusters.addHost("foo");
+ Host host = clusters.getHost("foo");
+ registerHost(host);
+ ensureHostUpdatesReceived(host);
+
+ // TODO need to verify audit logs generated
+ // TODO need to verify health status updated properly
+
+ long counter = 0;
+ sendHealthyHeartbeat(host, ++counter);
+ verifyHostState(host, HostState.HEALTHY);
+ Assert.assertEquals(counter, host.getLastHeartbeatTime());
+
+ sendHealthyHeartbeat(host, ++counter);
+ verifyHostState(host, HostState.HEALTHY);
+ Assert.assertEquals(counter, host.getLastHeartbeatTime());
+ Assert.assertEquals(HealthStatus.HEALTHY,
+ host.getHealthStatus().getHealthStatus());
+
+ sendUnhealthyHeartbeat(host, ++counter);
+ verifyHostState(host, HostState.UNHEALTHY);
+ Assert.assertEquals(counter, host.getLastHeartbeatTime());
+ Assert.assertEquals(HealthStatus.UNHEALTHY,
+ host.getHealthStatus().getHealthStatus());
+
+ sendUnhealthyHeartbeat(host, ++counter);
+ verifyHostState(host, HostState.UNHEALTHY);
+ Assert.assertEquals(counter, host.getLastHeartbeatTime());
+ Assert.assertEquals(HealthStatus.UNHEALTHY,
+ host.getHealthStatus().getHealthStatus());
+
+ sendHealthyHeartbeat(host, ++counter);
+ verifyHostState(host, HostState.HEALTHY);
+ Assert.assertEquals(counter, host.getLastHeartbeatTime());
+ Assert.assertEquals(HealthStatus.HEALTHY,
+ host.getHealthStatus().getHealthStatus());
+
+ timeoutHost(host);
+ verifyHostState(host, HostState.HEARTBEAT_LOST);
+ Assert.assertEquals(counter, host.getLastHeartbeatTime());
+ Assert.assertEquals(HealthStatus.UNKNOWN,
+ host.getHealthStatus().getHealthStatus());
+
+ timeoutHost(host);
+ verifyHostState(host, HostState.HEARTBEAT_LOST);
+ Assert.assertEquals(counter, host.getLastHeartbeatTime());
+ Assert.assertEquals(HealthStatus.UNKNOWN,
+ host.getHealthStatus().getHealthStatus());
+
+ try {
+ sendUnhealthyHeartbeat(host, ++counter);
+ fail("Invalid event should have triggered an exception");
+ } catch (Exception e) {
+ // Expected
+ }
+ verifyHostState(host, HostState.HEARTBEAT_LOST);
+
+ try {
+ sendHealthyHeartbeat(host, ++counter);
+ fail("Invalid event should have triggered an exception");
+ } catch (Exception e) {
+ // Expected
+ }
+ verifyHostState(host, HostState.HEARTBEAT_LOST);
+ }
+
+ @Test
+ public void testHostRegistrationsInAnyState() throws Exception {
+ clusters.addHost("foo");
+ Host host = clusters.getHost("foo");
+ host.setIPv4("ipv4");
+ host.setIPv6("ipv6");
+
+ long counter = 0;
+
+ registerHost(host);
+
+ ensureHostUpdatesReceived(host);
+ registerHost(host, false);
+
+ ensureHostUpdatesReceived(host);
+ sendHealthyHeartbeat(host, ++counter);
+ verifyHostState(host, HostState.HEALTHY);
+ registerHost(host, false);
+ ensureHostUpdatesReceived(host);
+
+ sendUnhealthyHeartbeat(host, ++counter);
+ verifyHostState(host, HostState.UNHEALTHY);
+ registerHost(host, false);
+ ensureHostUpdatesReceived(host);
+
+ timeoutHost(host);
+ verifyHostState(host, HostState.HEARTBEAT_LOST);
+ registerHost(host, false);
+ ensureHostUpdatesReceived(host);
+
+ host.setState(HostState.INIT);
+ registerHost(host, false);
+
+ }
+}
Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/host/HostTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java?rev=1399060&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java Wed Oct 17 00:50:53 2012
@@ -0,0 +1,333 @@
+/**
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package org.apache.ambari.server.state.svccomphost;
+
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.persist.PersistService;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.apache.ambari.server.state.*;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostImpl;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostInstallEvent;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostOpFailedEvent;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostOpInProgressEvent;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostOpRestartedEvent;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostOpSucceededEvent;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStartEvent;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostStopEvent;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostUninstallEvent;
+import org.apache.ambari.server.state.svccomphost.ServiceComponentHostWipeoutEvent;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ServiceComponentHostTest {
+
+ @Inject
+ private Injector injector;
+ @Inject
+ private Clusters clusters;
+ @Inject
+ private ServiceFactory serviceFactory;
+ @Inject
+ private ServiceComponentFactory serviceComponentFactory;
+ @Inject
+ private ServiceComponentHostFactory serviceComponentHostFactory;
+
+ @Before
+ public void setup() throws AmbariException {
+ injector = Guice.createInjector(new InMemoryDefaultTestModule());
+ injector.getInstance(GuiceJpaInitializer.class);
+ injector.injectMembers(this);
+ clusters.addCluster("C1");
+ clusters.addHost("h1");
+ clusters.mapHostToCluster("h1","C1");
+ }
+
+ @After
+ public void teardown() {
+ injector.getInstance(PersistService.class).stop();
+ }
+
+ private ServiceComponentHost createNewServiceComponentHost(long clusterId,
+ String svc,
+ String svcComponent,
+ String hostName, boolean isClient) throws AmbariException{
+ Cluster c = clusters.getCluster("C1");
+ Service s = serviceFactory.createNew(c, svc);
+ ServiceComponent sc = serviceComponentFactory.createNew(s, svcComponent);
+ ServiceComponentHost impl = serviceComponentHostFactory.createNew(
+ sc, hostName, isClient);
+ Assert.assertEquals(State.INIT,
+ impl.getState());
+ return impl;
+ }
+
+ @Test
+ public void testNewServiceComponentHostImpl() throws AmbariException{
+ createNewServiceComponentHost(1, "svc", "svcComp", "h1", false);
+ createNewServiceComponentHost(1, "svc", "svcComp", "h1", true);
+ }
+
+ private ServiceComponentHostEvent createEvent(ServiceComponentHostImpl impl,
+ long timestamp, ServiceComponentHostEventType eventType) {
+ switch (eventType) {
+ case HOST_SVCCOMP_INSTALL:
+ return new ServiceComponentHostInstallEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ case HOST_SVCCOMP_START:
+ return new ServiceComponentHostStartEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ case HOST_SVCCOMP_STOP:
+ return new ServiceComponentHostStopEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ case HOST_SVCCOMP_UNINSTALL:
+ return new ServiceComponentHostUninstallEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ case HOST_SVCCOMP_OP_FAILED:
+ return new ServiceComponentHostOpFailedEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ case HOST_SVCCOMP_OP_SUCCEEDED:
+ return new ServiceComponentHostOpSucceededEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ case HOST_SVCCOMP_OP_IN_PROGRESS:
+ return new ServiceComponentHostOpInProgressEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ case HOST_SVCCOMP_OP_RESTART:
+ return new ServiceComponentHostOpRestartedEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ case HOST_SVCCOMP_WIPEOUT:
+ return new ServiceComponentHostWipeoutEvent(
+ impl.getServiceComponentName(), impl.getHostName(), timestamp);
+ }
+ return null;
+ }
+
+ private void runStateChanges(ServiceComponentHostImpl impl,
+ ServiceComponentHostEventType startEventType,
+ State startState,
+ State inProgressState,
+ State failedState,
+ State completedState)
+ throws Exception {
+ long timestamp = 0;
+
+ Assert.assertEquals(startState,
+ impl.getState());
+ ServiceComponentHostEvent startEvent = createEvent(impl, ++timestamp,
+ startEventType);
+
+ long startTime = timestamp;
+ impl.handleEvent(startEvent);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(-1, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(-1, impl.getLastOpEndTime());
+ Assert.assertEquals(inProgressState,
+ impl.getState());
+
+ ServiceComponentHostEvent installEvent2 = createEvent(impl, ++timestamp,
+ startEventType);
+ boolean exceptionThrown = false;
+ try {
+ impl.handleEvent(installEvent2);
+ } catch (Exception e) {
+ exceptionThrown = true;
+ }
+ Assert.assertTrue("Exception not thrown on invalid event", exceptionThrown);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(-1, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(-1, impl.getLastOpEndTime());
+ Assert.assertEquals(inProgressState,
+ impl.getState());
+
+ ServiceComponentHostOpInProgressEvent inProgressEvent1 = new
+ ServiceComponentHostOpInProgressEvent(impl.getServiceComponentName(),
+ impl.getHostName(), ++timestamp);
+ impl.handleEvent(inProgressEvent1);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(-1, impl.getLastOpEndTime());
+ Assert.assertEquals(inProgressState,
+ impl.getState());
+
+ ServiceComponentHostOpInProgressEvent inProgressEvent2 = new
+ ServiceComponentHostOpInProgressEvent(impl.getServiceComponentName(),
+ impl.getHostName(), ++timestamp);
+ impl.handleEvent(inProgressEvent2);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(-1, impl.getLastOpEndTime());
+ Assert.assertEquals(inProgressState,
+ impl.getState());
+
+
+ ServiceComponentHostOpFailedEvent failEvent = new
+ ServiceComponentHostOpFailedEvent(impl.getServiceComponentName(),
+ impl.getHostName(), ++timestamp);
+ long endTime = timestamp;
+ impl.handleEvent(failEvent);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(endTime, impl.getLastOpEndTime());
+ Assert.assertEquals(failedState,
+ impl.getState());
+
+ ServiceComponentHostOpRestartedEvent restartEvent = new
+ ServiceComponentHostOpRestartedEvent(impl.getServiceComponentName(),
+ impl.getHostName(), ++timestamp);
+ startTime = timestamp;
+ impl.handleEvent(restartEvent);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(-1, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(-1, impl.getLastOpEndTime());
+ Assert.assertEquals(inProgressState,
+ impl.getState());
+
+ ServiceComponentHostOpInProgressEvent inProgressEvent3 = new
+ ServiceComponentHostOpInProgressEvent(impl.getServiceComponentName(),
+ impl.getHostName(), ++timestamp);
+ impl.handleEvent(inProgressEvent3);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(-1, impl.getLastOpEndTime());
+ Assert.assertEquals(inProgressState,
+ impl.getState());
+
+ ServiceComponentHostOpFailedEvent failEvent2 = new
+ ServiceComponentHostOpFailedEvent(impl.getServiceComponentName(),
+ impl.getHostName(), ++timestamp);
+ endTime = timestamp;
+ impl.handleEvent(failEvent2);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(endTime, impl.getLastOpEndTime());
+ Assert.assertEquals(failedState,
+ impl.getState());
+
+ ServiceComponentHostEvent startEvent2 = createEvent(impl, ++timestamp,
+ startEventType);
+ startTime = timestamp;
+ impl.handleEvent(startEvent2);
+ Assert.assertEquals(-1, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(-1, impl.getLastOpEndTime());
+ Assert.assertEquals(inProgressState,
+ impl.getState());
+
+ ServiceComponentHostOpInProgressEvent inProgressEvent4 = new
+ ServiceComponentHostOpInProgressEvent(impl.getServiceComponentName(),
+ impl.getHostName(), ++timestamp);
+ impl.handleEvent(inProgressEvent4);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(-1, impl.getLastOpEndTime());
+ Assert.assertEquals(inProgressState,
+ impl.getState());
+
+ ServiceComponentHostOpSucceededEvent succeededEvent = new
+ ServiceComponentHostOpSucceededEvent(impl.getServiceComponentName(),
+ impl.getHostName(), ++timestamp);
+ endTime = timestamp;
+ impl.handleEvent(succeededEvent);
+ Assert.assertEquals(startTime, impl.getLastOpStartTime());
+ Assert.assertEquals(timestamp, impl.getLastOpLastUpdateTime());
+ Assert.assertEquals(endTime, impl.getLastOpEndTime());
+ Assert.assertEquals(completedState,
+ impl.getState());
+
+ }
+
+ @Test
+ public void testClientStateFlow() throws Exception {
+ ServiceComponentHostImpl impl = (ServiceComponentHostImpl) createNewServiceComponentHost(1, "svc",
+ "svcComp", "h1", true);
+
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_INSTALL,
+ State.INIT,
+ State.INSTALLING,
+ State.INSTALL_FAILED,
+ State.INSTALLED);
+
+ boolean exceptionThrown = false;
+ try {
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_START,
+ State.INSTALLED,
+ State.STARTING,
+ State.START_FAILED,
+ State.STARTED);
+ }
+ catch (Exception e) {
+ exceptionThrown = true;
+ }
+ Assert.assertTrue("Exception not thrown on invalid event", exceptionThrown);
+
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_UNINSTALL,
+ State.INSTALLED,
+ State.UNINSTALLING,
+ State.UNINSTALL_FAILED,
+ State.UNINSTALLED);
+
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_WIPEOUT,
+ State.UNINSTALLED,
+ State.WIPING_OUT,
+ State.WIPEOUT_FAILED,
+ State.INIT);
+
+ }
+
+ @Test
+ public void testDaemonStateFlow() throws Exception {
+ ServiceComponentHostImpl impl = (ServiceComponentHostImpl) createNewServiceComponentHost(1, "svc",
+ "svcComp", "h1", false);
+
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_INSTALL,
+ State.INIT,
+ State.INSTALLING,
+ State.INSTALL_FAILED,
+ State.INSTALLED);
+
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_START,
+ State.INSTALLED,
+ State.STARTING,
+ State.START_FAILED,
+ State.STARTED);
+
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_STOP,
+ State.STARTED,
+ State.STOPPING,
+ State.STOP_FAILED,
+ State.INSTALLED);
+
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_UNINSTALL,
+ State.INSTALLED,
+ State.UNINSTALLING,
+ State.UNINSTALL_FAILED,
+ State.UNINSTALLED);
+
+ runStateChanges(impl, ServiceComponentHostEventType.HOST_SVCCOMP_WIPEOUT,
+ State.UNINSTALLED,
+ State.WIPING_OUT,
+ State.WIPEOUT_FAILED,
+ State.INIT);
+ }
+
+}
Propchange: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/state/svccomphost/ServiceComponentHostTest.java
------------------------------------------------------------------------------
svn:eol-style = native