Example usage for java.util Set clear

List of usage examples for java.util Set clear

Introduction

In this page you can find the example usage for java.util Set clear.

Prototype

void clear();

Source Link

Document

Removes all of the elements from this set (optional operation).

Usage

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

private void testRunSmokeTestFlag(Map<String, String> mapRequestProps, AmbariManagementController amc,
        Set<ServiceRequest> serviceRequests) throws AmbariException {
    RequestStatusResponse response;//Starting HDFS service. No run_smoke_test flag is set, smoke

    //Stopping HDFS service
    serviceRequests.clear();
    serviceRequests.add(new ServiceRequest("c1", "HDFS", "INSTALLED"));
    ServiceResourceProviderTest.updateServices(amc, serviceRequests, mapRequestProps, false, false);

    //Starting HDFS service. No run_smoke_test flag is set, smoke
    // test(HDFS_SERVICE_CHECK) won't run
    boolean runSmokeTest = false;
    serviceRequests.clear();//from  www  . j  av  a  2s. com
    serviceRequests.add(new ServiceRequest("c1", "HDFS", "STARTED"));
    response = ServiceResourceProviderTest.updateServices(amc, serviceRequests, mapRequestProps, runSmokeTest,
            false);

    List<ShortTaskStatus> taskStatuses = response.getTasks();
    boolean smokeTestRequired = false;
    for (ShortTaskStatus shortTaskStatus : taskStatuses) {
        if (shortTaskStatus.getRole().equals(Role.HDFS_SERVICE_CHECK.toString())) {
            smokeTestRequired = true;
        }
    }
    assertFalse(smokeTestRequired);

    //Stopping HDFS service
    serviceRequests.clear();
    serviceRequests.add(new ServiceRequest("c1", "HDFS", "INSTALLED"));
    ServiceResourceProviderTest.updateServices(amc, serviceRequests, mapRequestProps, false, false);

    //Starting HDFS service again.
    //run_smoke_test flag is set, smoke test will be run
    runSmokeTest = true;
    serviceRequests.clear();
    serviceRequests.add(new ServiceRequest("c1", "HDFS", "STARTED"));
    response = ServiceResourceProviderTest.updateServices(amc, serviceRequests, mapRequestProps, runSmokeTest,
            false);

    taskStatuses = response.getTasks();
    smokeTestRequired = false;
    for (ShortTaskStatus shortTaskStatus : taskStatuses) {
        if (shortTaskStatus.getRole().equals(Role.HDFS_SERVICE_CHECK.toString())) {
            smokeTestRequired = true;
        }
    }
    assertTrue(smokeTestRequired);
}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testServiceComponentHostUpdateRecursive() throws Exception {
    String clusterName = "foo1";
    createCluster(clusterName);//from   w  w w  .j  a v  a  2s.c o m
    String serviceName1 = "HDFS";
    createService(clusterName, serviceName1, null);
    String componentName1 = "NAMENODE";
    String componentName2 = "DATANODE";
    String componentName3 = "HDFS_CLIENT";
    createServiceComponent(clusterName, serviceName1, componentName1, State.INIT);
    createServiceComponent(clusterName, serviceName1, componentName2, State.INIT);
    createServiceComponent(clusterName, serviceName1, componentName3, State.INIT);
    String host1 = "h1";
    String host2 = "h2";
    addHost(host1, clusterName);
    addHost(host2, clusterName);

    Set<ServiceComponentHostRequest> set1 = new HashSet<ServiceComponentHostRequest>();
    ServiceComponentHostRequest r1 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1,
            host1, State.INIT.toString());
    ServiceComponentHostRequest r2 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2,
            host1, State.INIT.toString());
    ServiceComponentHostRequest r3 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1,
            host2, State.INIT.toString());
    ServiceComponentHostRequest r4 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2,
            host2, State.INIT.toString());
    ServiceComponentHostRequest r5 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName3,
            host1, State.INIT.toString());

    set1.add(r1);
    set1.add(r2);
    set1.add(r3);
    set1.add(r4);
    set1.add(r5);
    controller.createHostComponents(set1);

    Cluster c1 = clusters.getCluster(clusterName);
    Service s1 = c1.getService(serviceName1);
    ServiceComponent sc1 = s1.getServiceComponent(componentName1);
    ServiceComponent sc2 = s1.getServiceComponent(componentName2);
    ServiceComponent sc3 = s1.getServiceComponent(componentName3);
    ServiceComponentHost sch1 = sc1.getServiceComponentHost(host1);
    ServiceComponentHost sch2 = sc2.getServiceComponentHost(host1);
    ServiceComponentHost sch3 = sc1.getServiceComponentHost(host2);
    ServiceComponentHost sch4 = sc2.getServiceComponentHost(host2);
    ServiceComponentHost sch5 = sc3.getServiceComponentHost(host1);

    s1.setDesiredState(State.INIT);
    sc1.setDesiredState(State.INIT);
    sc2.setDesiredState(State.INIT);
    sc3.setDesiredState(State.INIT);
    sch1.setDesiredState(State.INIT);
    sch2.setDesiredState(State.INIT);
    sch3.setDesiredState(State.INIT);
    sch4.setDesiredState(State.INSTALLED);
    sch5.setDesiredState(State.INSTALLED);
    sch1.setState(State.INIT);
    sch2.setState(State.INSTALL_FAILED);
    sch3.setState(State.INIT);
    sch4.setState(State.INSTALLED);
    sch5.setState(State.INSTALLED);

    ServiceComponentHostRequest req1, req2, req3, req4, req5;
    Set<ServiceComponentHostRequest> reqs = new HashSet<ServiceComponentHostRequest>();

    try {
        reqs.clear();
        req1 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1, host1,
                State.STARTED.toString());
        reqs.add(req1);
        updateHostComponents(reqs, Collections.<String, String>emptyMap(), true);
        fail("Expected failure for invalid transition");
    } catch (Exception e) {
        // Expected
    }

    try {
        reqs.clear();
        req1 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1, host1,
                State.INSTALLED.toString());
        req2 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1, host2,
                State.INSTALLED.toString());
        req3 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2, host1,
                State.INSTALLED.toString());
        req4 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2, host2,
                State.INSTALLED.toString());
        req5 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName3, host1,
                State.STARTED.toString());
        reqs.add(req1);
        reqs.add(req2);
        reqs.add(req3);
        reqs.add(req4);
        reqs.add(req5);
        updateHostComponents(reqs, Collections.<String, String>emptyMap(), true);
        fail("Expected failure for invalid states");
    } catch (Exception e) {
        // Expected
    }

    reqs.clear();
    req1 = new ServiceComponentHostRequest(clusterName, null, componentName1, host1,
            State.INSTALLED.toString());
    req2 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1, host2,
            State.INSTALLED.toString());
    req3 = new ServiceComponentHostRequest(clusterName, null, componentName2, host1,
            State.INSTALLED.toString());
    req4 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2, host2,
            State.INSTALLED.toString());
    req5 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName3, host1,
            State.INSTALLED.toString());
    reqs.add(req1);
    reqs.add(req2);
    reqs.add(req3);
    reqs.add(req4);
    reqs.add(req5);
    RequestStatusResponse trackAction = updateHostComponents(reqs, Collections.<String, String>emptyMap(),
            true);
    Assert.assertNotNull(trackAction);

    long requestId = trackAction.getRequestId();

    Assert.assertFalse(actionDB.getAllStages(requestId).isEmpty());
    List<Stage> stages = actionDB.getAllStages(requestId);
    // FIXME check stage count

    for (Stage stage : stages) {
        LOG.debug("Stage dump: " + stage.toString());
    }

    // FIXME verify stages content - execution commands, etc

    // manually set live state
    sch1.setState(State.INSTALLED);
    sch2.setState(State.INSTALLED);
    sch3.setState(State.INSTALLED);
    sch4.setState(State.INSTALLED);
    sch5.setState(State.INSTALLED);

    // test no-op
    reqs.clear();
    req1 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1, host1,
            State.INSTALLED.toString());
    req2 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1, host2,
            State.INSTALLED.toString());
    reqs.add(req1);
    reqs.add(req2);
    trackAction = updateHostComponents(reqs, Collections.<String, String>emptyMap(), true);
    Assert.assertNull(trackAction);
}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testServiceUpdateRecursive() throws AmbariException {
    String clusterName = "foo1";
    createCluster(clusterName);//  w w  w  .j a va 2s  .  c o m
    clusters.getCluster(clusterName).setDesiredStackVersion(new StackId("HDP-0.2"));
    String serviceName1 = "HDFS";
    createService(clusterName, serviceName1, null);
    String serviceName2 = "HBASE";
    createService(clusterName, serviceName2, null);
    String componentName1 = "NAMENODE";
    String componentName2 = "DATANODE";
    String componentName3 = "HBASE_MASTER";
    String componentName4 = "HDFS_CLIENT";

    Map<String, String> mapRequestProps = new HashMap<String, String>();
    mapRequestProps.put("context", "Called from a test");

    createServiceComponent(clusterName, serviceName1, componentName1, State.INIT);
    createServiceComponent(clusterName, serviceName1, componentName2, State.INIT);
    createServiceComponent(clusterName, serviceName2, componentName3, State.INIT);
    createServiceComponent(clusterName, serviceName1, componentName4, State.INIT);
    String host1 = "h1";
    String host2 = "h2";
    addHost(host1, clusterName);
    addHost(host2, clusterName);

    Set<ServiceComponentHostRequest> set1 = new HashSet<ServiceComponentHostRequest>();
    ServiceComponentHostRequest r1 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1,
            host1, State.INIT.toString());
    ServiceComponentHostRequest r2 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2,
            host1, State.INIT.toString());
    ServiceComponentHostRequest r3 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1,
            host2, State.INIT.toString());
    ServiceComponentHostRequest r4 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2,
            host2, State.INIT.toString());
    ServiceComponentHostRequest r5 = new ServiceComponentHostRequest(clusterName, serviceName2, componentName3,
            host1, State.INIT.toString());
    ServiceComponentHostRequest r6 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName4,
            host2, State.INIT.toString());

    set1.add(r1);
    set1.add(r2);
    set1.add(r3);
    set1.add(r4);
    set1.add(r5);
    set1.add(r6);
    controller.createHostComponents(set1);

    Cluster c1 = clusters.getCluster(clusterName);
    Service s1 = c1.getService(serviceName1);
    Service s2 = c1.getService(serviceName2);
    ServiceComponent sc1 = s1.getServiceComponent(componentName1);
    ServiceComponent sc2 = s1.getServiceComponent(componentName2);
    ServiceComponent sc3 = s2.getServiceComponent(componentName3);
    ServiceComponent sc4 = s1.getServiceComponent(componentName4);
    ServiceComponentHost sch1 = sc1.getServiceComponentHost(host1);
    ServiceComponentHost sch2 = sc2.getServiceComponentHost(host1);
    ServiceComponentHost sch3 = sc1.getServiceComponentHost(host2);
    ServiceComponentHost sch4 = sc2.getServiceComponentHost(host2);
    ServiceComponentHost sch5 = sc3.getServiceComponentHost(host1);
    ServiceComponentHost sch6 = sc4.getServiceComponentHost(host2);

    s1.setDesiredState(State.INSTALLED);
    s2.setDesiredState(State.INSTALLED);
    sc1.setDesiredState(State.STARTED);
    sc2.setDesiredState(State.INIT);
    sc3.setDesiredState(State.STARTED);
    sc4.setDesiredState(State.INSTALLED);
    sch1.setDesiredState(State.INSTALLED);
    sch2.setDesiredState(State.INSTALLED);
    sch3.setDesiredState(State.INSTALLED);
    sch4.setDesiredState(State.INSTALLED);
    sch5.setDesiredState(State.INSTALLED);
    sch6.setDesiredState(State.INSTALLED);
    sch1.setState(State.INSTALLED);
    sch2.setState(State.INSTALLED);
    sch3.setState(State.INSTALLED);
    sch4.setState(State.INSTALLED);
    sch5.setState(State.INSTALLED);
    sch6.setState(State.INSTALLED);

    Set<ServiceRequest> reqs = new HashSet<ServiceRequest>();
    ServiceRequest req1, req2;
    try {
        reqs.clear();
        req1 = new ServiceRequest(clusterName, serviceName1, State.STARTED.toString());
        reqs.add(req1);
        ServiceResourceProviderTest.updateServices(controller, reqs, mapRequestProps, true, false);
        fail("Expected failure for invalid state update");
    } catch (Exception e) {
        // Expected
    }

    s1.setDesiredState(State.INSTALLED);
    s2.setDesiredState(State.INSTALLED);
    sc1.setDesiredState(State.STARTED);
    sc2.setDesiredState(State.INSTALLED);
    sc3.setDesiredState(State.STARTED);
    sch1.setDesiredState(State.INSTALLED);
    sch2.setDesiredState(State.INSTALLED);
    sch3.setDesiredState(State.INSTALLED);
    sch4.setDesiredState(State.INSTALLED);
    sch5.setDesiredState(State.INSTALLED);
    sch1.setState(State.INIT);
    sch2.setState(State.INSTALLED);
    sch3.setState(State.INIT);
    sch4.setState(State.INSTALLED);
    sch5.setState(State.INSTALLED);

    try {
        reqs.clear();
        req1 = new ServiceRequest(clusterName, serviceName1, State.STARTED.toString());
        reqs.add(req1);
        ServiceResourceProviderTest.updateServices(controller, reqs, mapRequestProps, true, false);
        fail("Expected failure for invalid state update");
    } catch (Exception e) {
        // Expected
    }

    s1.setDesiredState(State.INSTALLED);
    s2.setDesiredState(State.INSTALLED);
    sc1.setDesiredState(State.STARTED);
    sc2.setDesiredState(State.INSTALLED);
    sc3.setDesiredState(State.STARTED);
    sch1.setDesiredState(State.STARTED);
    sch2.setDesiredState(State.STARTED);
    sch3.setDesiredState(State.STARTED);
    sch4.setDesiredState(State.STARTED);
    sch5.setDesiredState(State.STARTED);
    sch1.setState(State.INSTALLED);
    sch2.setState(State.INSTALLED);
    sch3.setState(State.INSTALLED);
    sch4.setState(State.STARTED);
    sch5.setState(State.INSTALLED);

    reqs.clear();
    req1 = new ServiceRequest(clusterName, serviceName1, State.STARTED.toString());
    req2 = new ServiceRequest(clusterName, serviceName2, State.STARTED.toString());
    reqs.add(req1);
    reqs.add(req2);
    RequestStatusResponse trackAction = ServiceResourceProviderTest.updateServices(controller, reqs,
            mapRequestProps, true, false);

    Assert.assertEquals(State.STARTED, s1.getDesiredState());
    Assert.assertEquals(State.STARTED, s2.getDesiredState());
    Assert.assertEquals(State.STARTED, sc1.getDesiredState());
    Assert.assertEquals(State.STARTED, sc2.getDesiredState());
    Assert.assertEquals(State.STARTED, sc3.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sc4.getDesiredState());
    Assert.assertEquals(State.STARTED, sch1.getDesiredState());
    Assert.assertEquals(State.STARTED, sch2.getDesiredState());
    Assert.assertEquals(State.STARTED, sch3.getDesiredState());
    Assert.assertEquals(State.STARTED, sch4.getDesiredState());
    Assert.assertEquals(State.STARTED, sch5.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sch6.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sch1.getState());
    Assert.assertEquals(State.INSTALLED, sch2.getState());
    Assert.assertEquals(State.INSTALLED, sch3.getState());
    Assert.assertEquals(State.STARTED, sch4.getState());
    Assert.assertEquals(State.INSTALLED, sch5.getState());
    Assert.assertEquals(State.INSTALLED, sch6.getState());

    long requestId = trackAction.getRequestId();
    List<Stage> stages = actionDB.getAllStages(requestId);

    for (Stage stage : stages) {
        LOG.debug("Stage dump: " + stage.toString());
    }

    Assert.assertTrue(!stages.isEmpty());
    Assert.assertEquals(3, stages.size());

    // expected
    // sch1 to start
    // sch2 to start
    // sch3 to start
    // sch5 to start
    Stage stage1 = null, stage2 = null, stage3 = null;
    for (Stage s : stages) {
        if (s.getStageId() == 0) {
            stage1 = s;
        }
        if (s.getStageId() == 1) {
            stage2 = s;
        }
        if (s.getStageId() == 2) {
            stage3 = s;
        }
    }

    Assert.assertEquals(2, stage1.getExecutionCommands(host1).size());
    Assert.assertEquals(1, stage1.getExecutionCommands(host2).size());
    Assert.assertEquals(1, stage2.getExecutionCommands(host1).size());

    Assert.assertNotNull(stage1.getExecutionCommandWrapper(host1, "NAMENODE"));
    Assert.assertNotNull(stage1.getExecutionCommandWrapper(host1, "DATANODE"));
    Assert.assertNotNull(stage1.getExecutionCommandWrapper(host2, "NAMENODE"));
    Assert.assertNotNull(stage2.getExecutionCommandWrapper(host1, "HBASE_MASTER"));
    Assert.assertNull(stage1.getExecutionCommandWrapper(host2, "DATANODE"));
    Assert.assertNotNull(stage3.getExecutionCommandWrapper(host1, "HBASE_SERVICE_CHECK"));
    Assert.assertNotNull(stage2.getExecutionCommandWrapper(host2, "HDFS_SERVICE_CHECK"));

    Type type = new TypeToken<Map<String, String>>() {
    }.getType();

    for (Stage s : stages) {
        for (List<ExecutionCommandWrapper> list : s.getExecutionCommands().values()) {
            for (ExecutionCommandWrapper ecw : list) {
                if (ecw.getExecutionCommand().getRole().contains("SERVICE_CHECK")) {
                    Map<String, String> hostParams = StageUtils.getGson().fromJson(s.getHostParamsStage(),
                            type);
                    Assert.assertNotNull(hostParams);
                    Assert.assertTrue(hostParams.size() > 0);
                    Assert.assertTrue(hostParams.containsKey("stack_version"));
                    Assert.assertEquals(hostParams.get("stack_version"),
                            c1.getDesiredStackVersion().getStackVersion());
                }
            }
        }
    }

    // manually set live state
    sch1.setState(State.STARTED);
    sch2.setState(State.STARTED);
    sch3.setState(State.STARTED);
    sch4.setState(State.STARTED);
    sch5.setState(State.STARTED);

    // test no-op
    reqs.clear();
    req1 = new ServiceRequest(clusterName, serviceName1, State.STARTED.toString());
    req2 = new ServiceRequest(clusterName, serviceName2, State.STARTED.toString());
    reqs.add(req1);
    reqs.add(req2);
    trackAction = ServiceResourceProviderTest.updateServices(controller, reqs, mapRequestProps, true, false);
    Assert.assertNull(trackAction);

}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testCreateServiceComponentHostWithInvalidRequest() throws AmbariException {
    // multiple clusters
    // dup objects
    // existing components
    // invalid request params
    // invalid service
    // invalid cluster
    // invalid component
    // invalid host

    Set<ServiceComponentHostRequest> set1 = new HashSet<ServiceComponentHostRequest>();

    try {//w  ww  . j a v a 2 s.co  m
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest(null, null, null, null, null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid requests");
    } catch (IllegalArgumentException e) {
        // Expected
    }

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest("foo", null, null, null, null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid requests");
    } catch (IllegalArgumentException e) {
        // Expected
    }

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest("foo", "HDFS", null, null, null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid requests");
    } catch (IllegalArgumentException e) {
        // Expected
    }

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", null,
                null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid requests");
    } catch (IllegalArgumentException e) {
        // Expected
    }

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h1",
                null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid cluster");
    } catch (ParentObjectNotFoundException e) {
        // Expected
    }

    clusters.addCluster("foo");
    clusters.addCluster("c1");
    clusters.addCluster("c2");
    Cluster foo = clusters.getCluster("foo");
    Cluster c1 = clusters.getCluster("c1");
    Cluster c2 = clusters.getCluster("c2");

    StackId stackId = new StackId("HDP-0.2");
    helper.getOrCreateRepositoryVersion(stackId.getStackName(), stackId.getStackVersion());

    foo.setDesiredStackVersion(stackId);
    foo.setCurrentStackVersion(stackId);
    foo.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin",
            RepositoryVersionState.UPGRADING);
    foo.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(),
            RepositoryVersionState.CURRENT);

    stackId = new StackId("HDP-0.2");
    c1.setDesiredStackVersion(stackId);
    c1.setCurrentStackVersion(stackId);
    c1.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin",
            RepositoryVersionState.UPGRADING);
    c1.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(),
            RepositoryVersionState.CURRENT);

    stackId = new StackId("HDP-0.2");
    c2.setDesiredStackVersion(stackId);
    c2.setCurrentStackVersion(stackId);
    c2.createClusterVersion(stackId.getStackName(), stackId.getStackVersion(), "admin",
            RepositoryVersionState.UPGRADING);
    c2.transitionClusterVersion(stackId.getStackName(), stackId.getStackVersion(),
            RepositoryVersionState.CURRENT);

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h1",
                null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid service");
    } catch (IllegalArgumentException e) {
        // Expected
    }

    Service s1 = serviceFactory.createNew(foo, "HDFS");
    foo.addService(s1);
    s1.persist();
    Service s2 = serviceFactory.createNew(c1, "HDFS");
    c1.addService(s2);
    s2.persist();
    Service s3 = serviceFactory.createNew(c2, "HDFS");
    c2.addService(s3);
    s3.persist();

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h1",
                null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid service");
    } catch (AmbariException e) {
        // Expected
    }

    ServiceComponent sc1 = serviceComponentFactory.createNew(s1, "NAMENODE");
    s1.addServiceComponent(sc1);
    sc1.persist();
    ServiceComponent sc2 = serviceComponentFactory.createNew(s2, "NAMENODE");
    s2.addServiceComponent(sc2);
    sc2.persist();
    ServiceComponent sc3 = serviceComponentFactory.createNew(s3, "NAMENODE");
    s3.addServiceComponent(sc3);
    sc3.persist();

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h1",
                null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid host");
    } catch (AmbariException e) {
        // Expected
    }

    clusters.addHost("h1");
    Host h1 = clusters.getHost("h1");
    h1.setIPv4("ipv41");
    h1.setIPv6("ipv61");
    setOsFamily(h1, "redhat", "6.3");
    h1.persist();
    clusters.addHost("h2");
    Host h2 = clusters.getHost("h2");
    h2.setIPv4("ipv42");
    h2.setIPv6("ipv62");
    setOsFamily(h2, "redhat", "6.3");
    h2.persist();
    clusters.addHost("h3");
    Host h3 = clusters.getHost("h3");
    h3.setIPv4("ipv43");
    h3.setIPv6("ipv63");
    setOsFamily(h3, "redhat", "6.3");
    h3.persist();

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h1",
                null);
        set1.add(rInvalid);
        controller.createHostComponents(set1);
        fail("Expected failure for invalid host cluster mapping");
    } catch (AmbariException e) {
        // Expected
    }

    Set<String> hostnames = new HashSet<String>();
    hostnames.add("h1");
    hostnames.add("h2");
    hostnames.add("h3");
    clusters.mapHostsToCluster(hostnames, "foo");
    clusters.mapHostsToCluster(hostnames, "c1");
    clusters.mapHostsToCluster(hostnames, "c2");

    set1.clear();
    ServiceComponentHostRequest valid = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h1", null);
    set1.add(valid);
    controller.createHostComponents(set1);

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid1 = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h2",
                null);
        ServiceComponentHostRequest rInvalid2 = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h2",
                null);
        set1.add(rInvalid1);
        set1.add(rInvalid2);
        controller.createHostComponents(set1);
        fail("Expected failure for dup requests");
    } catch (DuplicateResourceException e) {
        // Expected
    }

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid1 = new ServiceComponentHostRequest("c1", "HDFS", "NAMENODE", "h2",
                null);
        ServiceComponentHostRequest rInvalid2 = new ServiceComponentHostRequest("c2", "HDFS", "NAMENODE", "h3",
                null);
        set1.add(rInvalid1);
        set1.add(rInvalid2);
        controller.createHostComponents(set1);
        fail("Expected failure for multiple clusters");
    } catch (IllegalArgumentException e) {
        // Expected
    }

    try {
        set1.clear();
        ServiceComponentHostRequest rInvalid1 = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h1",
                null);
        ServiceComponentHostRequest rInvalid2 = new ServiceComponentHostRequest("foo", "HDFS", "NAMENODE", "h2",
                null);
        set1.add(rInvalid1);
        set1.add(rInvalid2);
        controller.createHostComponents(set1);
        fail("Expected failure for already existing");
    } catch (DuplicateResourceException e) {
        // Expected
    }

    Assert.assertEquals(1, foo.getServiceComponentHosts("h1").size());
    Assert.assertEquals(0, foo.getServiceComponentHosts("h2").size());
    Assert.assertEquals(0, foo.getServiceComponentHosts("h3").size());

    set1.clear();
    ServiceComponentHostRequest valid1 = new ServiceComponentHostRequest("c1", "HDFS", "NAMENODE", "h1", null);
    set1.add(valid1);
    controller.createHostComponents(set1);

    set1.clear();
    ServiceComponentHostRequest valid2 = new ServiceComponentHostRequest("c2", "HDFS", "NAMENODE", "h1", null);
    set1.add(valid2);
    controller.createHostComponents(set1);

    Assert.assertEquals(1, foo.getServiceComponentHosts("h1").size());
    Assert.assertEquals(1, c1.getServiceComponentHosts("h1").size());
    Assert.assertEquals(1, c2.getServiceComponentHosts("h1").size());

}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testDecommissonDatanodeAction() throws Exception {
    String clusterName = "foo1";
    createCluster(clusterName);//  w  w  w  . j av  a 2  s  .  c  o  m
    Cluster cluster = clusters.getCluster(clusterName);
    cluster.setDesiredStackVersion(new StackId("HDP-2.0.7"));
    String serviceName = "HDFS";
    createService(clusterName, serviceName, null);
    String componentName1 = "NAMENODE";
    String componentName2 = "DATANODE";
    String componentName3 = "HDFS_CLIENT";

    createServiceComponent(clusterName, serviceName, componentName1, State.INIT);
    createServiceComponent(clusterName, serviceName, componentName2, State.INIT);
    createServiceComponent(clusterName, serviceName, componentName3, State.INIT);

    String host1 = "h1";
    String host2 = "h2";

    addHost(host1, clusterName);
    addHost(host2, clusterName);

    createServiceComponentHost(clusterName, serviceName, componentName1, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName2, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName2, host2, null);
    createServiceComponentHost(clusterName, serviceName, componentName3, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName3, host2, null);

    RequestOperationLevel level = new RequestOperationLevel(Resource.Type.HostComponent, clusterName, null,
            null, null);

    // Install
    installService(clusterName, serviceName, false, false);

    // Create and attach config
    Map<String, String> configs = new HashMap<String, String>();
    configs.put("a", "b");

    ConfigurationRequest cr1;
    cr1 = new ConfigurationRequest(clusterName, "hdfs-site", "version1", configs, null);
    ClusterRequest crReq = new ClusterRequest(cluster.getClusterId(), clusterName, null, null);
    crReq.setDesiredConfig(Collections.singletonList(cr1));
    controller.updateClusters(Collections.singleton(crReq), null);

    // Start
    startService(clusterName, serviceName, false, false);

    cluster = clusters.getCluster(clusterName);
    Service s = cluster.getService(serviceName);
    Assert.assertEquals(State.STARTED, s.getDesiredState());
    ServiceComponentHost scHost = s.getServiceComponent("DATANODE").getServiceComponentHost("h2");
    Assert.assertEquals(HostComponentAdminState.INSERVICE, scHost.getComponentAdminState());

    // Decommission one datanode
    Map<String, String> params = new HashMap<String, String>() {
        {
            put("test", "test");
            put("excluded_hosts", "h2");
            put("align_maintenance_state", "true");
        }
    };
    RequestResourceFilter resourceFilter = new RequestResourceFilter("HDFS", "NAMENODE", null);
    ArrayList<RequestResourceFilter> filters = new ArrayList<RequestResourceFilter>();
    filters.add(resourceFilter);
    ExecuteActionRequest request = new ExecuteActionRequest(clusterName, "DECOMMISSION", null, filters, level,
            params, false);

    Map<String, String> requestProperties = new HashMap<String, String>();
    requestProperties.put(REQUEST_CONTEXT_PROPERTY, "Called from a test");

    RequestStatusResponse response = controller.createAction(request, requestProperties);

    List<HostRoleCommand> storedTasks = actionDB.getRequestTasks(response.getRequestId());
    ExecutionCommand execCmd = storedTasks.get(0).getExecutionCommandWrapper().getExecutionCommand();
    Assert.assertNotNull(storedTasks);
    Assert.assertNotNull(execCmd.getConfigurationTags().get("hdfs-site"));
    Assert.assertEquals(1, storedTasks.size());
    Assert.assertEquals(HostComponentAdminState.DECOMMISSIONED, scHost.getComponentAdminState());
    Assert.assertEquals(MaintenanceState.ON, scHost.getMaintenanceState());
    HostRoleCommand command = storedTasks.get(0);
    Assert.assertEquals(Role.NAMENODE, command.getRole());
    Assert.assertEquals(RoleCommand.CUSTOM_COMMAND, command.getRoleCommand());
    Assert.assertEquals("DECOMMISSION", execCmd.getHostLevelParams().get("custom_command"));

    // Decommission the other datanode
    params = new HashMap<String, String>() {
        {
            put("test", "test");
            put("excluded_hosts", "h1");
            put("align_maintenance_state", "true");
        }
    };
    resourceFilter = new RequestResourceFilter("HDFS", "NAMENODE", null);
    filters = new ArrayList<RequestResourceFilter>();
    filters.add(resourceFilter);

    request = new ExecuteActionRequest(clusterName, "DECOMMISSION", null, filters, level, params, false);

    response = controller.createAction(request, requestProperties);

    storedTasks = actionDB.getRequestTasks(response.getRequestId());
    execCmd = storedTasks.get(0).getExecutionCommandWrapper().getExecutionCommand();
    Map<String, String> cmdParams = execCmd.getCommandParams();
    Assert.assertTrue(cmdParams.containsKey("update_exclude_file_only"));
    Assert.assertTrue(cmdParams.get("update_exclude_file_only").equals("false"));
    Assert.assertNotNull(storedTasks);
    Assert.assertEquals(1, storedTasks.size());
    Assert.assertEquals(HostComponentAdminState.DECOMMISSIONED, scHost.getComponentAdminState());
    Assert.assertEquals(MaintenanceState.ON, scHost.getMaintenanceState());
    Assert.assertEquals("DECOMMISSION", execCmd.getHostLevelParams().get("custom_command"));

    // Recommission the other datanode  (while adding NameNode HA)
    createServiceComponentHost(clusterName, serviceName, componentName1, host2, null);
    ServiceComponentHostRequest r = new ServiceComponentHostRequest(clusterName, serviceName, componentName1,
            host2, State.INSTALLED.toString());
    Set<ServiceComponentHostRequest> requests = new HashSet<ServiceComponentHostRequest>();
    requests.add(r);
    updateHostComponents(requests, Collections.<String, String>emptyMap(), true);
    s.getServiceComponent(componentName1).getServiceComponentHost(host2).setState(State.INSTALLED);
    r = new ServiceComponentHostRequest(clusterName, serviceName, componentName1, host2,
            State.STARTED.toString());
    requests.clear();
    requests.add(r);
    updateHostComponents(requests, Collections.<String, String>emptyMap(), true);
    s.getServiceComponent(componentName1).getServiceComponentHost(host2).setState(State.STARTED);

    params = new HashMap<String, String>() {
        {
            put("test", "test");
            put("included_hosts", "h1 , h2");
            put("align_maintenance_state", "true");
        }
    };
    resourceFilter = new RequestResourceFilter("HDFS", "NAMENODE", null);
    filters = new ArrayList<RequestResourceFilter>();
    filters.add(resourceFilter);
    request = new ExecuteActionRequest(clusterName, "DECOMMISSION", null, filters, level, params, false);

    response = controller.createAction(request, requestProperties);

    storedTasks = actionDB.getRequestTasks(response.getRequestId());
    Assert.assertNotNull(storedTasks);
    scHost = s.getServiceComponent("DATANODE").getServiceComponentHost("h2");
    Assert.assertEquals(HostComponentAdminState.INSERVICE, scHost.getComponentAdminState());
    Assert.assertEquals(MaintenanceState.OFF, scHost.getMaintenanceState());
    execCmd = storedTasks.get(0).getExecutionCommandWrapper().getExecutionCommand();
    Assert.assertNotNull(storedTasks);
    Assert.assertEquals(2, storedTasks.size());
    int countRefresh = 0;
    for (HostRoleCommand hrc : storedTasks) {
        Assert.assertTrue("DECOMMISSION, Included: h1,h2".equals(hrc.getCommandDetail()));
        Assert.assertTrue("DECOMMISSION".equals(hrc.getCustomCommandName()));
        cmdParams = hrc.getExecutionCommandWrapper().getExecutionCommand().getCommandParams();
        if (!cmdParams.containsKey("update_exclude_file_only")
                || !cmdParams.get("update_exclude_file_only").equals("true")) {
            countRefresh++;
        }
    }
    Assert.assertEquals(2, countRefresh);

    // Slave components will have admin state as INSERVICE even if the state in DB is null
    scHost.setComponentAdminState(null);
    Assert.assertEquals(HostComponentAdminState.INSERVICE, scHost.getComponentAdminState());
    Assert.assertEquals(MaintenanceState.OFF, scHost.getMaintenanceState());
}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testReConfigureService() throws Exception {
    String clusterName = "foo1";
    createCluster(clusterName);/*  w ww  .j a va  2s  .co  m*/
    clusters.getCluster(clusterName).setDesiredStackVersion(new StackId("HDP-0.1"));
    String serviceName = "HDFS";
    createService(clusterName, serviceName, null);
    String componentName1 = "NAMENODE";
    String componentName2 = "DATANODE";
    String componentName3 = "HDFS_CLIENT";

    Map<String, String> mapRequestProps = new HashMap<String, String>();
    mapRequestProps.put("context", "Called from a test");

    createServiceComponent(clusterName, serviceName, componentName1, State.INIT);
    createServiceComponent(clusterName, serviceName, componentName2, State.INIT);
    createServiceComponent(clusterName, serviceName, componentName3, State.INIT);

    String host1 = "h1";
    String host2 = "h2";

    addHost(host1, clusterName);
    addHost(host2, clusterName);

    // null service should work
    createServiceComponentHost(clusterName, null, componentName1, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName2, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName2, host2, null);
    createServiceComponentHost(clusterName, serviceName, componentName3, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName3, host2, null);

    // Install
    ServiceRequest r = new ServiceRequest(clusterName, serviceName, State.INSTALLED.toString());
    Set<ServiceRequest> requests = new HashSet<ServiceRequest>();
    requests.add(r);

    ServiceResourceProviderTest.updateServices(controller, requests, mapRequestProps, true, false);
    Assert.assertEquals(State.INSTALLED,
            clusters.getCluster(clusterName).getService(serviceName).getDesiredState());

    // manually change live state to installed as no running action manager
    for (ServiceComponent sc : clusters.getCluster(clusterName).getService(serviceName).getServiceComponents()
            .values()) {
        for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
            sch.setState(State.INSTALLED);
        }
    }

    // Create and attach config
    Map<String, String> configs = new HashMap<String, String>();
    configs.put("a", "b");

    ConfigurationRequest cr1, cr2, cr3;
    cr1 = new ConfigurationRequest(clusterName, "core-site", "version1", configs, null);
    cr2 = new ConfigurationRequest(clusterName, "hdfs-site", "version1", configs, null);
    cr3 = new ConfigurationRequest(clusterName, "core-site", "version122", configs, null);
    controller.createConfiguration(cr1);
    controller.createConfiguration(cr2);
    controller.createConfiguration(cr3);

    Cluster cluster = clusters.getCluster(clusterName);
    Service s = cluster.getService(serviceName);
    ServiceComponent sc1 = s.getServiceComponent(componentName1);
    ServiceComponent sc2 = s.getServiceComponent(componentName2);
    ServiceComponentHost sch1 = sc1.getServiceComponentHost(host1);

    Set<ServiceComponentHostRequest> schReqs = new HashSet<ServiceComponentHostRequest>();
    Set<ServiceComponentRequest> scReqs = new HashSet<ServiceComponentRequest>();
    Set<ServiceRequest> sReqs = new HashSet<ServiceRequest>();
    Map<String, String> configVersions = new HashMap<String, String>();

    // SCH level
    configVersions.clear();
    configVersions.put("core-site", "version1");
    configVersions.put("hdfs-site", "version1");
    schReqs.clear();
    schReqs.add(new ServiceComponentHostRequest(clusterName, serviceName, componentName1, host1, null));
    Assert.assertNull(updateHostComponents(schReqs, Collections.<String, String>emptyMap(), true));

    // Reconfigure SCH level
    configVersions.clear();
    configVersions.put("core-site", "version122");
    schReqs.clear();
    schReqs.add(new ServiceComponentHostRequest(clusterName, serviceName, componentName1, host1, null));
    Assert.assertNull(updateHostComponents(schReqs, Collections.<String, String>emptyMap(), true));

    // Clear Entity Manager
    entityManager.clear();

    //SC Level
    configVersions.clear();
    configVersions.put("core-site", "version1");
    configVersions.put("hdfs-site", "version1");
    scReqs.add(new ServiceComponentRequest(clusterName, serviceName, componentName2, null));
    Assert.assertNull(ComponentResourceProviderTest.updateComponents(controller, scReqs,
            Collections.<String, String>emptyMap(), true));

    scReqs.add(new ServiceComponentRequest(clusterName, serviceName, componentName1, null));
    Assert.assertNull(ComponentResourceProviderTest.updateComponents(controller, scReqs,
            Collections.<String, String>emptyMap(), true));

    // Reconfigure SC level
    configVersions.clear();
    configVersions.put("core-site", "version122");

    scReqs.clear();
    scReqs.add(new ServiceComponentRequest(clusterName, serviceName, componentName2, null));
    Assert.assertNull(ComponentResourceProviderTest.updateComponents(controller, scReqs,
            Collections.<String, String>emptyMap(), true));

    scReqs.clear();
    scReqs.add(new ServiceComponentRequest(clusterName, serviceName, componentName1, null));
    Assert.assertNull(ComponentResourceProviderTest.updateComponents(controller, scReqs,
            Collections.<String, String>emptyMap(), true));

    entityManager.clear();

    // S level
    configVersions.clear();
    configVersions.put("core-site", "version1");
    configVersions.put("hdfs-site", "version1");
    sReqs.clear();
    sReqs.add(new ServiceRequest(clusterName, serviceName, null));
    Assert.assertNull(
            ServiceResourceProviderTest.updateServices(controller, sReqs, mapRequestProps, true, false));

    // Reconfigure S Level
    configVersions.clear();
    configVersions.put("core-site", "version122");

    sReqs.clear();
    sReqs.add(new ServiceRequest(clusterName, serviceName, null));
    Assert.assertNull(
            ServiceResourceProviderTest.updateServices(controller, sReqs, mapRequestProps, true, false));

    entityManager.clear();

}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testServiceComponentUpdateRecursive() throws AmbariException {
    String clusterName = "foo1";
    createCluster(clusterName);/*  www .  j  a va  2  s .c  o  m*/
    String serviceName1 = "HDFS";
    createService(clusterName, serviceName1, null);
    String componentName1 = "NAMENODE";
    String componentName2 = "DATANODE";
    String componentName3 = "HDFS_CLIENT";
    createServiceComponent(clusterName, serviceName1, componentName1, State.INIT);
    createServiceComponent(clusterName, serviceName1, componentName2, State.INIT);
    createServiceComponent(clusterName, serviceName1, componentName3, State.INIT);
    String host1 = "h1";
    String host2 = "h2";
    addHost(host1, clusterName);
    addHost(host2, clusterName);

    Set<ServiceComponentHostRequest> set1 = new HashSet<ServiceComponentHostRequest>();
    ServiceComponentHostRequest r1 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1,
            host1, State.INIT.toString());
    ServiceComponentHostRequest r2 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2,
            host1, State.INIT.toString());
    ServiceComponentHostRequest r3 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName1,
            host2, State.INIT.toString());
    ServiceComponentHostRequest r4 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName2,
            host2, State.INIT.toString());
    ServiceComponentHostRequest r5 = new ServiceComponentHostRequest(clusterName, serviceName1, componentName3,
            host1, State.INIT.toString());

    set1.add(r1);
    set1.add(r2);
    set1.add(r3);
    set1.add(r4);
    set1.add(r5);
    controller.createHostComponents(set1);

    Cluster c1 = clusters.getCluster(clusterName);
    Service s1 = c1.getService(serviceName1);
    ServiceComponent sc1 = s1.getServiceComponent(componentName1);
    ServiceComponent sc2 = s1.getServiceComponent(componentName2);
    ServiceComponent sc3 = s1.getServiceComponent(componentName3);
    ServiceComponentHost sch1 = sc1.getServiceComponentHost(host1);
    ServiceComponentHost sch2 = sc2.getServiceComponentHost(host1);
    ServiceComponentHost sch3 = sc1.getServiceComponentHost(host2);
    ServiceComponentHost sch4 = sc2.getServiceComponentHost(host2);
    ServiceComponentHost sch5 = sc3.getServiceComponentHost(host1);

    s1.setDesiredState(State.INSTALLED);
    sc1.setDesiredState(State.INIT);
    sc2.setDesiredState(State.INIT);
    sc3.setDesiredState(State.STARTED);
    sch1.setDesiredState(State.INSTALLED);
    sch2.setDesiredState(State.INSTALLED);
    sch3.setDesiredState(State.STARTED);
    sch4.setDesiredState(State.INSTALLED);
    sch5.setDesiredState(State.INSTALLED);
    sch1.setState(State.INSTALLED);
    sch2.setState(State.INSTALLED);
    sch3.setState(State.STARTED);
    sch4.setState(State.INSTALLED);
    sch5.setState(State.UNKNOWN);

    Set<ServiceComponentRequest> reqs = new HashSet<ServiceComponentRequest>();
    ServiceComponentRequest req1, req2, req3;

    // confirm an UNKOWN doesn't fail
    req1 = new ServiceComponentRequest(clusterName, serviceName1, sc3.getName(), State.INSTALLED.toString());
    reqs.add(req1);
    ComponentResourceProviderTest.updateComponents(controller, reqs, Collections.<String, String>emptyMap(),
            true);
    try {
        reqs.clear();
        req1 = new ServiceComponentRequest(clusterName, serviceName1, sc1.getName(), State.INIT.toString());
        reqs.add(req1);
        ComponentResourceProviderTest.updateComponents(controller, reqs, Collections.<String, String>emptyMap(),
                true);
        fail("Expected failure for invalid state update");
    } catch (Exception e) {
        // Expected
    }

    s1.setDesiredState(State.INSTALLED);
    sc1.setDesiredState(State.STARTED);
    sc2.setDesiredState(State.INSTALLED);
    sc3.setDesiredState(State.STARTED);
    sch1.setDesiredState(State.INIT);
    sch2.setDesiredState(State.INIT);
    sch3.setDesiredState(State.INIT);
    sch4.setDesiredState(State.INIT);
    sch5.setDesiredState(State.INIT);
    sch1.setState(State.INIT);
    sch2.setState(State.INSTALLED);
    sch3.setState(State.INIT);
    sch4.setState(State.INSTALLED);
    sch5.setState(State.INSTALLED);

    try {
        reqs.clear();
        req1 = new ServiceComponentRequest(clusterName, serviceName1, sc1.getName(), State.STARTED.toString());
        reqs.add(req1);
        ComponentResourceProviderTest.updateComponents(controller, reqs, Collections.<String, String>emptyMap(),
                true);
        fail("Expected failure for invalid state update");
    } catch (Exception e) {
        // Expected
    }

    s1.setDesiredState(State.INSTALLED);
    sc1.setDesiredState(State.STARTED);
    sc2.setDesiredState(State.INIT);
    sc3.setDesiredState(State.STARTED);
    sch1.setDesiredState(State.INIT);
    sch2.setDesiredState(State.INIT);
    sch3.setDesiredState(State.INIT);
    sch4.setDesiredState(State.INIT);
    sch5.setDesiredState(State.INIT);
    sch1.setState(State.STARTED);
    sch2.setState(State.INIT);
    sch3.setState(State.INSTALLED);
    sch4.setState(State.STARTED);
    sch5.setState(State.INIT);

    reqs.clear();
    req1 = new ServiceComponentRequest(clusterName, serviceName1, sc1.getName(), State.INSTALLED.toString());
    req2 = new ServiceComponentRequest(clusterName, serviceName1, sc2.getName(), State.INSTALLED.toString());
    req3 = new ServiceComponentRequest(clusterName, serviceName1, sc3.getName(), State.INSTALLED.toString());
    reqs.add(req1);
    reqs.add(req2);
    reqs.add(req3);
    RequestStatusResponse trackAction = ComponentResourceProviderTest.updateComponents(controller, reqs,
            Collections.<String, String>emptyMap(), true);

    Assert.assertEquals(State.INSTALLED, s1.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sc1.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sc2.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sc3.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sch1.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sch2.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sch3.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sch4.getDesiredState());
    Assert.assertEquals(State.INSTALLED, sch5.getDesiredState());
    Assert.assertEquals(State.STARTED, sch1.getState());
    Assert.assertEquals(State.INIT, sch2.getState());
    Assert.assertEquals(State.INSTALLED, sch3.getState());
    Assert.assertEquals(State.STARTED, sch4.getState());
    Assert.assertEquals(State.INIT, sch5.getState());

    long requestId = trackAction.getRequestId();
    List<Stage> stages = actionDB.getAllStages(requestId);
    Assert.assertTrue(!stages.isEmpty());

    // FIXME check stage count

    for (Stage stage : stages) {
        LOG.debug("Stage dump: " + stage.toString());
    }

    // FIXME verify stages content - execution commands, etc

    // maually set live state
    sch1.setState(State.INSTALLED);
    sch2.setState(State.INSTALLED);
    sch3.setState(State.INSTALLED);
    sch4.setState(State.INSTALLED);
    sch5.setState(State.INSTALLED);

    // test no-op
    reqs.clear();
    req1 = new ServiceComponentRequest(clusterName, serviceName1, sc1.getName(), State.INSTALLED.toString());
    req2 = new ServiceComponentRequest(clusterName, serviceName1, sc2.getName(), State.INSTALLED.toString());
    reqs.add(req1);
    reqs.add(req2);
    trackAction = ComponentResourceProviderTest.updateComponents(controller, reqs,
            Collections.<String, String>emptyMap(), true);
    Assert.assertNull(trackAction);
}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testConfigUpdates() throws Exception {
    String clusterName = "foo1";
    createCluster(clusterName);//from  w ww. j av  a 2  s.com
    clusters.getCluster(clusterName).setDesiredStackVersion(new StackId("HDP-0.1"));
    String serviceName = "HDFS";
    createService(clusterName, serviceName, null);
    String componentName1 = "NAMENODE";
    String componentName2 = "DATANODE";
    String componentName3 = "HDFS_CLIENT";

    Map<String, String> mapRequestProps = new HashMap<String, String>();
    mapRequestProps.put("context", "Called from a test");

    createServiceComponent(clusterName, serviceName, componentName1, State.INIT);
    createServiceComponent(clusterName, serviceName, componentName2, State.INIT);
    createServiceComponent(clusterName, serviceName, componentName3, State.INIT);

    String host1 = "h1";
    String host2 = "h2";

    addHost(host1, clusterName);
    addHost(host2, clusterName);

    // null service should work
    createServiceComponentHost(clusterName, null, componentName1, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName2, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName2, host2, null);
    createServiceComponentHost(clusterName, serviceName, componentName3, host1, null);
    createServiceComponentHost(clusterName, serviceName, componentName3, host2, null);

    Assert.assertNotNull(clusters.getCluster(clusterName).getService(serviceName)
            .getServiceComponent(componentName1).getServiceComponentHost(host1));
    Assert.assertNotNull(clusters.getCluster(clusterName).getService(serviceName)
            .getServiceComponent(componentName2).getServiceComponentHost(host1));
    Assert.assertNotNull(clusters.getCluster(clusterName).getService(serviceName)
            .getServiceComponent(componentName2).getServiceComponentHost(host2));
    Assert.assertNotNull(clusters.getCluster(clusterName).getService(serviceName)
            .getServiceComponent(componentName3).getServiceComponentHost(host1));
    Assert.assertNotNull(clusters.getCluster(clusterName).getService(serviceName)
            .getServiceComponent(componentName3).getServiceComponentHost(host2));

    Map<String, String> configs = new HashMap<String, String>();
    configs.put("a", "b");

    Map<String, Map<String, String>> configAttributes = new HashMap<String, Map<String, String>>();
    configAttributes.put("final", new HashMap<String, String>());
    configAttributes.get("final").put("a", "true");

    ConfigurationRequest cr1, cr2, cr3, cr4, cr5, cr6, cr7, cr8;
    cr1 = new ConfigurationRequest(clusterName, "typeA", "v1", configs, configAttributes);
    cr2 = new ConfigurationRequest(clusterName, "typeB", "v1", configs, configAttributes);
    cr3 = new ConfigurationRequest(clusterName, "typeC", "v1", configs, configAttributes);
    cr4 = new ConfigurationRequest(clusterName, "typeD", "v1", configs, configAttributes);
    cr5 = new ConfigurationRequest(clusterName, "typeA", "v2", configs, configAttributes);
    cr6 = new ConfigurationRequest(clusterName, "typeB", "v2", configs, configAttributes);
    cr7 = new ConfigurationRequest(clusterName, "typeC", "v2", configs, configAttributes);
    cr8 = new ConfigurationRequest(clusterName, "typeE", "v1", configs, configAttributes);
    controller.createConfiguration(cr1);
    controller.createConfiguration(cr2);
    controller.createConfiguration(cr3);
    controller.createConfiguration(cr4);
    controller.createConfiguration(cr5);
    controller.createConfiguration(cr6);
    controller.createConfiguration(cr7);
    controller.createConfiguration(cr8);

    Cluster cluster = clusters.getCluster(clusterName);
    Service s = cluster.getService(serviceName);
    ServiceComponent sc1 = s.getServiceComponent(componentName1);
    ServiceComponent sc2 = s.getServiceComponent(componentName2);
    ServiceComponentHost sch1 = sc1.getServiceComponentHost(host1);

    Set<ServiceComponentHostRequest> schReqs = new HashSet<ServiceComponentHostRequest>();
    Set<ServiceComponentRequest> scReqs = new HashSet<ServiceComponentRequest>();
    Set<ServiceRequest> sReqs = new HashSet<ServiceRequest>();
    Map<String, String> configVersions = new HashMap<String, String>();

    // update configs at SCH and SC level
    configVersions.clear();
    configVersions.put("typeA", "v1");
    configVersions.put("typeB", "v1");
    configVersions.put("typeC", "v1");
    schReqs.clear();
    schReqs.add(new ServiceComponentHostRequest(clusterName, serviceName, componentName1, host1, null));
    Assert.assertNull(updateHostComponents(schReqs, Collections.<String, String>emptyMap(), true));

    configVersions.clear();
    configVersions.put("typeC", "v1");
    configVersions.put("typeD", "v1");
    scReqs.clear();
    scReqs.add(new ServiceComponentRequest(clusterName, serviceName, componentName2, null));
    Assert.assertNull(ComponentResourceProviderTest.updateComponents(controller, scReqs,
            Collections.<String, String>emptyMap(), true));

    // update configs at service level
    configVersions.clear();
    configVersions.put("typeA", "v2");
    configVersions.put("typeC", "v2");
    configVersions.put("typeE", "v1");
    sReqs.clear();
    sReqs.add(new ServiceRequest(clusterName, serviceName, null));
    Assert.assertNull(
            ServiceResourceProviderTest.updateServices(controller, sReqs, mapRequestProps, true, false));

    // update configs at SCH level
    configVersions.clear();
    configVersions.put("typeA", "v1");
    configVersions.put("typeB", "v1");
    configVersions.put("typeC", "v1");
    schReqs.clear();
    schReqs.add(new ServiceComponentHostRequest(clusterName, serviceName, componentName1, host1, null));
    Assert.assertNull(updateHostComponents(schReqs, Collections.<String, String>emptyMap(), true));

    // update configs at SC level
    configVersions.clear();
    configVersions.put("typeC", "v2");
    configVersions.put("typeD", "v1");
    scReqs.clear();
    scReqs.add(new ServiceComponentRequest(clusterName, serviceName, componentName1, null));
    Assert.assertNull(ComponentResourceProviderTest.updateComponents(controller, scReqs,
            Collections.<String, String>emptyMap(), true));

}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testInstallAndStartService() throws Exception {
    testCreateServiceComponentHostSimple();

    String clusterName = "foo1";
    String serviceName = "HDFS";

    Cluster cluster = clusters.getCluster(clusterName);
    Service s1 = cluster.getService(serviceName);

    Map<String, Config> configs = new HashMap<String, Config>();
    Map<String, String> properties = new HashMap<String, String>();
    Map<String, Map<String, String>> propertiesAttributes = new HashMap<String, Map<String, String>>();
    properties.put("a", "a1");
    properties.put("b", "b1");

    Config c1 = new ConfigImpl(cluster, "hdfs-site", properties, propertiesAttributes, injector);
    properties.put("c", "c1");
    properties.put("d", "d1");
    Config c2 = new ConfigImpl(cluster, "core-site", properties, propertiesAttributes, injector);
    Config c3 = new ConfigImpl(cluster, "foo-site", properties, propertiesAttributes, injector);

    Map<String, String> mapRequestProps = new HashMap<String, String>();
    mapRequestProps.put("context", "Called from a test");

    c1.setTag("v1");
    c2.setTag("v1");
    c3.setTag("v1");

    cluster.addConfig(c1);//from w  w  w  . j av  a2s. co m
    cluster.addConfig(c2);
    cluster.addConfig(c3);
    c1.persist();
    c2.persist();
    c3.persist();

    configs.put(c1.getType(), c1);
    configs.put(c2.getType(), c2);

    ServiceRequest r = new ServiceRequest(clusterName, serviceName, State.INSTALLED.toString());
    Set<ServiceRequest> requests = new HashSet<ServiceRequest>();
    requests.add(r);

    RequestStatusResponse trackAction = ServiceResourceProviderTest.updateServices(controller, requests,
            mapRequestProps, true, false);
    Assert.assertEquals(State.INSTALLED,
            clusters.getCluster(clusterName).getService(serviceName).getDesiredState());
    for (ServiceComponent sc : clusters.getCluster(clusterName).getService(serviceName).getServiceComponents()
            .values()) {
        Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
        for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
            Assert.assertEquals(State.INSTALLED, sch.getDesiredState());
            Assert.assertEquals(State.INIT, sch.getState());
        }
    }

    List<ShortTaskStatus> taskStatuses = trackAction.getTasks();
    Assert.assertEquals(5, taskStatuses.size());

    boolean foundH1NN = false;
    boolean foundH1DN = false;
    boolean foundH2DN = false;
    boolean foundH1CLT = false;
    boolean foundH2CLT = false;

    for (ShortTaskStatus taskStatus : taskStatuses) {
        LOG.debug("Task dump :" + taskStatus.toString());
        Assert.assertEquals(RoleCommand.INSTALL.toString(), taskStatus.getCommand());
        Assert.assertEquals(HostRoleStatus.PENDING.toString(), taskStatus.getStatus());
        if (taskStatus.getHostName().equals("h1")) {
            if (Role.NAMENODE.toString().equals(taskStatus.getRole())) {
                foundH1NN = true;
            } else if (Role.DATANODE.toString().equals(taskStatus.getRole())) {
                foundH1DN = true;
            } else if (Role.HDFS_CLIENT.toString().equals(taskStatus.getRole())) {
                foundH1CLT = true;
            } else {
                fail("Found invalid role for host h1");
            }
        } else if (taskStatus.getHostName().equals("h2")) {
            if (Role.DATANODE.toString().equals(taskStatus.getRole())) {
                foundH2DN = true;
            } else if (Role.HDFS_CLIENT.toString().equals(taskStatus.getRole())) {
                foundH2CLT = true;
            } else {
                fail("Found invalid role for host h2");
            }
        } else {
            fail("Found invalid host in task list");
        }
    }
    Assert.assertTrue(foundH1DN && foundH1NN && foundH2DN && foundH1CLT && foundH2CLT);

    // TODO validate stages?
    List<Stage> stages = actionDB.getAllStages(trackAction.getRequestId());
    Assert.assertEquals(1, stages.size());

    for (Stage stage : stages) {
        LOG.info("Stage Details for Install Service" + ", stageId=" + stage.getStageId() + ", actionId="
                + stage.getActionId());

        for (String host : stage.getHosts()) {
            for (ExecutionCommandWrapper ecw : stage.getExecutionCommands(host)) {
                Assert.assertFalse(ecw.getExecutionCommand().getHostLevelParams().get("repo_info").isEmpty());
            }
        }
    }

    Set<TaskStatusRequest> taskRequests = new HashSet<TaskStatusRequest>();
    TaskStatusRequest t1, t2;
    t1 = new TaskStatusRequest();
    t2 = new TaskStatusRequest();
    t1.setRequestId(trackAction.getRequestId());
    taskRequests.add(t1);
    Set<TaskStatusResponse> taskResponses = controller.getTaskStatus(taskRequests);
    Assert.assertEquals(5, taskResponses.size());

    t1.setTaskId(1L);
    t2.setRequestId(trackAction.getRequestId());
    t2.setTaskId(2L);
    taskRequests.clear();
    taskRequests.add(t1);
    taskRequests.add(t2);
    taskResponses = controller.getTaskStatus(taskRequests);
    Assert.assertEquals(2, taskResponses.size());

    // manually change live state to installed as no running action manager
    for (ServiceComponent sc : clusters.getCluster(clusterName).getService(serviceName).getServiceComponents()
            .values()) {
        for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
            sch.setState(State.INSTALLED);
        }
    }

    r = new ServiceRequest(clusterName, serviceName, State.STARTED.toString());
    requests.clear();
    requests.add(r);
    trackAction = ServiceResourceProviderTest.updateServices(controller, requests, mapRequestProps, true,
            false);

    Assert.assertEquals(State.STARTED,
            clusters.getCluster(clusterName).getService(serviceName).getDesiredState());
    for (ServiceComponent sc : clusters.getCluster(clusterName).getService(serviceName).getServiceComponents()
            .values()) {
        if (sc.getName().equals("HDFS_CLIENT")) {
            Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
        } else {
            Assert.assertEquals(State.STARTED, sc.getDesiredState());
        }
        for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
            if (sch.getServiceComponentName().equals("HDFS_CLIENT")) {
                Assert.assertEquals(State.INSTALLED, sch.getDesiredState());
            } else {
                Assert.assertEquals(State.STARTED, sch.getDesiredState());
            }
        }
    }

    // TODO validate stages?
    stages = actionDB.getAllStages(trackAction.getRequestId());
    Assert.assertEquals(2, stages.size());

    StringBuilder sb = new StringBuilder();
    clusters.debugDump(sb);
    LOG.info("Cluster Dump: " + sb.toString());

    for (ServiceComponent sc : clusters.getCluster(clusterName).getService(serviceName).getServiceComponents()
            .values()) {
        for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
            if (sc.isClientComponent()) {
                sch.setState(State.INSTALLED);
            } else {
                sch.setState(State.INSTALL_FAILED);
            }
        }
    }

    r = new ServiceRequest(clusterName, serviceName, State.INSTALLED.toString());
    requests.clear();
    requests.add(r);
    trackAction = ServiceResourceProviderTest.updateServices(controller, requests, mapRequestProps, true,
            false);

    Assert.assertEquals(State.INSTALLED,
            clusters.getCluster(clusterName).getService(serviceName).getDesiredState());
    for (ServiceComponent sc : clusters.getCluster(clusterName).getService(serviceName).getServiceComponents()
            .values()) {
        Assert.assertEquals(State.INSTALLED, sc.getDesiredState());
        for (ServiceComponentHost sch : sc.getServiceComponentHosts().values()) {
            Assert.assertEquals(State.INSTALLED, sch.getDesiredState());
        }
    }

    // TODO validate stages?
    stages = actionDB.getAllStages(trackAction.getRequestId());

    Assert.assertEquals(1, stages.size());

}

From source file:org.apache.ambari.server.controller.AmbariManagementControllerTest.java

@Test
public void testUpdateRepoUrlController() throws Exception {
    String badUrl = "http://hortonworks.com";
    RepositoryInfo repo = ambariMetaInfo.getRepository(STACK_NAME, STACK_VERSION, OS_TYPE, REPO_ID);
    RepositoryRequest request = new RepositoryRequest(STACK_NAME, STACK_VERSION, OS_TYPE, REPO_ID);
    request.setBaseUrl(badUrl);//from  www .  j av  a  2s.  c om

    Set<RepositoryRequest> requests = new HashSet<RepositoryRequest>();
    requests.add(request);

    // test bad url
    try {
        controller.updateRepositories(requests);
        Assert.fail("Expected a bad URL to throw an exception");
    } catch (Exception e) {
        assertNotNull(e);
        Assert.assertTrue(e.getMessage().contains(badUrl));
    }
    // test bad url, but allow to set anyway
    request.setVerifyBaseUrl(false);
    controller.updateRepositories(requests);
    Assert.assertEquals(request.getBaseUrl(), repo.getBaseUrl());

    requests.clear();
    request = new RepositoryRequest(STACK_NAME, STACK_VERSION, OS_TYPE, REPO_ID);
    request.setBaseUrl("https://hortonworks.com");
    requests.add(request);
    // test bad url
    try {
        controller.updateRepositories(requests);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        assertTrue(e.getMessage().contains(IOException.class.getName())
                || e.getMessage().contains("Could not access base url"));
    }

    requests.clear();
    request = new RepositoryRequest(STACK_NAME, STACK_VERSION, OS_TYPE, REPO_ID);
    request.setBaseUrl("pro://hortonworks.com");
    requests.add(request);
    // test bad url
    try {
        controller.updateRepositories(requests);
    } catch (Exception e) {
        assertTrue(e.getMessage().contains(MalformedURLException.class.getName()));
    }

    requests.clear();
    request = new RepositoryRequest(STACK_NAME, STACK_VERSION, OS_TYPE, REPO_ID);
    request.setBaseUrl("http://rrr1.cccc");
    requests.add(request);
    // test bad url
    try {
        controller.updateRepositories(requests);
    } catch (Exception e) {
        String exceptionMsg = e.getMessage();
        assertTrue(exceptionMsg.contains(UnknownHostException.class.getName())
                || exceptionMsg.contains(ConnectException.class.getName()));
    }

    // reset repo
    requests.clear();
    request = new RepositoryRequest(STACK_NAME, STACK_VERSION, OS_TYPE, REPO_ID);
    request.setBaseUrl(repo.getDefaultBaseUrl());
    requests.add(request);
    try {
        controller.updateRepositories(requests);
        Assert.assertEquals(repo.getBaseUrl(), repo.getDefaultBaseUrl());
    } catch (Exception e) {
        String exceptionMsg = e.getMessage();
        assertTrue(exceptionMsg.contains("Could not access base url"));
        LOG.error("Can not complete test. " + exceptionMsg);
    }

    String baseUrl = repo.getDefaultBaseUrl();
    if (!baseUrl.endsWith("/")) {
        baseUrl += "/";
    }

    // variation #1: url with trailing slash, suffix preceding slash
    backingProperties.setProperty(Configuration.REPO_SUFFIX_KEY_UBUNTU, "/repodata/repomd.xml");
    Assert.assertTrue(
            baseUrl.endsWith("/") && configuration.getRepoValidationSuffixes("ubuntu12")[0].startsWith("/"));
    request.setBaseUrl(baseUrl);
    try {
        controller.updateRepositories(requests);
        Assert.assertEquals(baseUrl, repo.getBaseUrl());
    } catch (Exception e) {
        String exceptionMsg = e.getMessage();
        assertTrue(exceptionMsg.contains("Could not access base url"));
        LOG.error("Can not complete test. " + exceptionMsg);
    }

    // variation #2: url with trailing slash, suffix no preceding slash
    backingProperties.setProperty(Configuration.REPO_SUFFIX_KEY_DEFAULT, "repodata/repomd.xml");
    Assert.assertTrue(
            baseUrl.endsWith("/") && !configuration.getRepoValidationSuffixes("redhat6")[0].startsWith("/"));
    request.setBaseUrl(baseUrl);
    try {
        controller.updateRepositories(requests);
        Assert.assertEquals(baseUrl, repo.getBaseUrl());
    } catch (Exception e) {
        String exceptionMsg = e.getMessage();
        assertTrue(exceptionMsg.contains("Could not access base url"));
        LOG.error("Can not complete test. " + exceptionMsg);
    }

    baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
    // variation #3: url with no trailing slash, suffix no prededing slash
    Assert.assertTrue(
            !baseUrl.endsWith("/") && !configuration.getRepoValidationSuffixes("redhat6")[0].startsWith("/"));
    request.setBaseUrl(baseUrl);
    try {
        controller.updateRepositories(requests);
        Assert.assertEquals(baseUrl, repo.getBaseUrl());
    } catch (Exception e) {
        String exceptionMsg = e.getMessage();
        assertTrue(exceptionMsg.contains("Could not access base url"));
        LOG.error("Can not complete test. " + exceptionMsg);
    }

    // variation #4: url with no trailing slash, suffix preceding slash
    backingProperties.setProperty(Configuration.REPO_SUFFIX_KEY_DEFAULT, "/repodata/repomd.xml");
    Assert.assertTrue(
            !baseUrl.endsWith("/") && configuration.getRepoValidationSuffixes("suse11")[0].startsWith("/"));
    request.setBaseUrl(baseUrl);
    try {
        controller.updateRepositories(requests);
        Assert.assertEquals(baseUrl, repo.getBaseUrl());
    } catch (Exception e) {
        String exceptionMsg = e.getMessage();
        assertTrue(exceptionMsg.contains("Could not access base url"));
        LOG.error("Can not complete test. " + exceptionMsg);
    }

    // variation #5: multiple suffix tests
    backingProperties.setProperty(Configuration.REPO_SUFFIX_KEY_UBUNTU, "/foo/bar.xml,/repodata/repomd.xml");
    Assert.assertTrue(configuration.getRepoValidationSuffixes("ubuntu12").length > 1);
    request.setBaseUrl(baseUrl);
    try {
        controller.updateRepositories(requests);
        Assert.assertEquals(baseUrl, repo.getBaseUrl());
    } catch (Exception e) {
        String exceptionMsg = e.getMessage();
        assertTrue(exceptionMsg.contains("Could not access base url"));
        LOG.error("Can not complete test. " + exceptionMsg);
    }

}