You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ma...@apache.org on 2012/11/07 09:13:24 UTC

svn commit: r1406489 [6/19] - in /incubator/ambari/branches/AMBARI-666: ./ ambari-agent/ ambari-agent/conf/ ambari-agent/conf/unix/ ambari-agent/src/main/puppet/manifestloader/ ambari-agent/src/main/puppet/modules/configgenerator/manifests/ ambari-agen...

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java Wed Nov  7 08:13:12 2012
@@ -20,17 +20,26 @@ package org.apache.ambari.server.control
 
 
 import java.io.File;
-import java.io.IOException;
 import java.util.Map;
 
+import com.google.inject.persist.Transactional;
+import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.ActionManager;
 import org.apache.ambari.server.agent.HeartBeatHandler;
 import org.apache.ambari.server.agent.rest.AgentResource;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.api.services.AmbariMetaService;
+import org.apache.ambari.server.api.services.PersistKeyValueImpl;
+import org.apache.ambari.server.api.services.PersistKeyValueService;
 import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
+import org.apache.ambari.server.orm.PersistenceType;
 import org.apache.ambari.server.resources.ResourceManager;
 import org.apache.ambari.server.resources.api.rest.GetResource;
 import org.apache.ambari.server.security.CertificateManager;
+import org.apache.ambari.server.security.authorization.AmbariLdapAuthenticationProvider;
+import org.apache.ambari.server.security.authorization.AmbariLocalUserDetailsService;
+import org.apache.ambari.server.security.authorization.Users;
 import org.apache.ambari.server.security.unsecured.rest.CertificateDownload;
 import org.apache.ambari.server.security.unsecured.rest.CertificateSign;
 import org.apache.ambari.server.state.Clusters;
@@ -39,11 +48,13 @@ import org.mortbay.jetty.handler.Resourc
 import org.mortbay.jetty.security.SslSocketConnector;
 import org.mortbay.jetty.servlet.Context;
 import org.mortbay.jetty.servlet.DefaultServlet;
+import org.mortbay.jetty.servlet.FilterHolder;
 import org.mortbay.jetty.servlet.ServletHolder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.web.context.WebApplicationContext;
 import org.springframework.web.context.support.GenericWebApplicationContext;
 import org.springframework.web.filter.DelegatingFilterProxy;
@@ -75,8 +86,11 @@ public class AmbariServer {
   CertificateManager certMan;
   @Inject
   Injector injector;
+  @Inject
+  AmbariMetaInfo ambariMetaInfo;
+
   private static AmbariManagementController clusterController = null;
-  
+
   public static AmbariManagementController getController() {
     return clusterController;
   }
@@ -89,6 +103,7 @@ public class AmbariServer {
 
   public void run() {
     performStaticInjection();
+    addInMemoryUsers();
     server = new Server(CLIENT_API_PORT);
     serverForAgent = new Server();
 
@@ -98,7 +113,14 @@ public class AmbariServer {
       parentSpringAppContext.refresh();
       ConfigurableListableBeanFactory factory = parentSpringAppContext.
           getBeanFactory();
-      factory.registerSingleton("guiceInjector", injector);
+      factory.registerSingleton("guiceInjector",
+          injector);
+      factory.registerSingleton("passwordEncoder",
+          injector.getInstance(PasswordEncoder.class));
+      factory.registerSingleton("ambariLocalUserService",
+          injector.getInstance(AmbariLocalUserDetailsService.class));
+      factory.registerSingleton("ambariLdapAuthenticationProvider",
+          injector.getInstance(AmbariLdapAuthenticationProvider.class));
       //Spring Security xml config depends on this Bean
 
       String[] contextLocations = {SPRING_CONTEXT_LOCATION};
@@ -131,8 +153,10 @@ public class AmbariServer {
       //Spring Security Filter initialization
       DelegatingFilterProxy springSecurityFilter = new DelegatingFilterProxy();
       springSecurityFilter.setTargetBeanName("springSecurityFilterChain");
-      //root.addFilter(new FilterHolder(springSecurityFilter), "/*", 1);
 
+      if (configs.getApiAuthentication()) {
+        root.addFilter(new FilterHolder(springSecurityFilter), "/api/*", 1);
+      }
       //Secured connector for 2-way auth
       SslSocketConnector sslConnectorTwoWay = new SslSocketConnector();
       sslConnectorTwoWay.setPort(CLIENT_TWO_WAY);
@@ -207,9 +231,17 @@ public class AmbariServer {
       serverForAgent.setStopAtShutdown(true);
       springAppContext.start();
 
+      LOG.info("********* Initializing Meta Info **********");
+      ambariMetaInfo.init();
+
       //Start action scheduler
       LOG.info("********* Initializing Clusters **********");
       Clusters clusters = injector.getInstance(Clusters.class);
+      StringBuilder clusterDump = new StringBuilder();
+      clusters.debugDump(clusterDump);
+      LOG.info("********* Current Clusters State *********");
+      LOG.info(clusterDump.toString());
+
       LOG.info("********* Initializing ActionManager **********");
       ActionManager manager = injector.getInstance(ActionManager.class);
       LOG.info("********* Initializing Controller **********");
@@ -217,7 +249,7 @@ public class AmbariServer {
           AmbariManagementController.class);
 
       clusterController = controller;
-      
+
       // FIXME need to figure out correct order of starting things to
       // handle restart-recovery correctly
 
@@ -235,7 +267,7 @@ public class AmbariServer {
 //      RequestInjectorForTest testInjector = new RequestInjectorForTest(controller, clusters);
 //      Thread testInjectorThread = new Thread(testInjector);
 //      testInjectorThread.start();
-      
+
       server.join();
       LOG.info("Joined the Server");
     } catch (Exception e) {
@@ -243,6 +275,26 @@ public class AmbariServer {
     }
   }
 
+  /**
+   * Creates default users and roles if in-memory database is used
+   */
+  @Transactional
+  protected void addInMemoryUsers() {
+    if (configs.getPersistenceType() == PersistenceType.IN_MEMORY) {
+      LOG.info("In-memory database is used - creating default users");
+      Users users = injector.getInstance(Users.class);
+
+      users.createDefaultRoles();
+      users.createUser("admin", "admin");
+      users.createUser("user", "user");
+      try {
+        users.promoteToAdmin(users.getLocalUser("admin"));
+      } catch (AmbariException e) {
+        throw new RuntimeException(e);
+      }
+    }
+  }
+
   public void stop() throws Exception {
     try {
       server.stop();
@@ -259,12 +311,14 @@ public class AmbariServer {
     CertificateDownload.init(injector.getInstance(CertificateManager.class));
     CertificateSign.init(injector.getInstance(CertificateManager.class));
     GetResource.init(injector.getInstance(ResourceManager.class));
+    PersistKeyValueService.init(injector.getInstance(PersistKeyValueImpl.class));
+    AmbariMetaService.init(injector.getInstance(AmbariMetaInfo.class));
   }
 
-  public static void main(String[] args) throws IOException {
+  public static void main(String[] args) throws Exception {
 
     Injector injector = Guice.createInjector(new ControllerModule());
-    
+
     try {
       LOG.info("Getting the controller");
       injector.getInstance(GuiceJpaInitializer.class);

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterResponse.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterResponse.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterResponse.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterResponse.java Wed Nov  7 08:13:12 2012
@@ -28,12 +28,15 @@ public class ClusterResponse {
 
   private final Set<String> hostNames;
 
+  private final String desiredStackVersion;
+
   public ClusterResponse(Long clusterId, String clusterName,
-      Set<String> hostNames) {
+      Set<String> hostNames, String desiredStackVersion) {
     super();
     this.clusterId = clusterId;
     this.clusterName = clusterName;
     this.hostNames = hostNames;
+    this.desiredStackVersion = desiredStackVersion;
   }
 
   /**
@@ -63,6 +66,7 @@ public class ClusterResponse {
     sb.append("{"
         + " clusterName=" + clusterName
         + ", clusterId=" + clusterId
+        + ", desiredStackVersion=" + desiredStackVersion
         + ", hosts=[");
     if (hostNames != null) {
       int i = 0;
@@ -104,4 +108,11 @@ public class ClusterResponse {
     return result;
   }
 
+  /**
+   * @return the desiredStackVersion
+   */
+  public String getDesiredStackVersion() {
+    return desiredStackVersion;
+  }
+
 }

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java Wed Nov  7 08:13:12 2012
@@ -20,9 +20,13 @@ package org.apache.ambari.server.control
 import com.google.gson.Gson;
 import com.google.inject.Scopes;
 import com.google.inject.assistedinject.FactoryModuleBuilder;
+import com.google.inject.matcher.Matchers;
+import com.google.inject.persist.Transactional;
 import com.google.inject.persist.jpa.JpaPersistModule;
 import org.apache.ambari.server.actionmanager.*;
+import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.orm.dao.ClearEntityManagerInterceptor;
 import org.apache.ambari.server.state.*;
 import org.apache.ambari.server.state.cluster.ClusterFactory;
 import org.apache.ambari.server.state.cluster.ClusterImpl;
@@ -33,6 +37,8 @@ import org.apache.ambari.server.state.ho
 import com.google.inject.AbstractModule;
 import com.google.inject.name.Names;
 import org.apache.ambari.server.state.svccomphost.ServiceComponentHostImpl;
+import org.springframework.security.crypto.password.PasswordEncoder;
+import org.springframework.security.crypto.password.StandardPasswordEncoder;
 
 import java.util.Properties;
 
@@ -43,36 +49,63 @@ import java.util.Properties;
 public class ControllerModule extends AbstractModule {
 
   private final Configuration configuration;
+  private final AmbariMetaInfo ambariMetaInfo;
 
-  public ControllerModule() {
+  public ControllerModule() throws Exception {
     configuration = new Configuration();
+    ambariMetaInfo = new AmbariMetaInfo(configuration);
   }
 
-  public ControllerModule(Properties properties) {
+  public ControllerModule(Properties properties) throws Exception {
     configuration = new Configuration(properties);
+    ambariMetaInfo = new AmbariMetaInfo(configuration);
   }
 
   @Override
   protected void configure() {
+    bindInterceptors();
+    installFactories();
+
     bind(Configuration.class).toInstance(configuration);
+    bind(AmbariMetaInfo.class).toInstance(ambariMetaInfo);
+
+    bind(PasswordEncoder.class).toInstance(new StandardPasswordEncoder());
 
     install(new JpaPersistModule(configuration.getPersistenceType().getUnitName()));
 
-    install(new FactoryModuleBuilder().implement(Cluster.class, ClusterImpl.class).build(ClusterFactory.class));
-    install(new FactoryModuleBuilder().implement(Host.class, HostImpl.class).build(HostFactory.class));
-    install(new FactoryModuleBuilder().implement(Service.class, ServiceImpl.class).build(ServiceFactory.class));
-    install(new FactoryModuleBuilder().implement(ServiceComponent.class, ServiceComponentImpl.class).build(ServiceComponentFactory.class));
-    install(new FactoryModuleBuilder().implement(ServiceComponentHost.class, ServiceComponentHostImpl.class).build(ServiceComponentHostFactory.class));
-    install(new FactoryModuleBuilder().implement(Config.class, ConfigImpl.class).build(ConfigFactory.class));
-    install(new FactoryModuleBuilder().build(StageFactory.class));
-    install(new FactoryModuleBuilder().build(HostRoleCommandFactory.class));
 
     bind(Gson.class).in(Scopes.SINGLETON);
     bind(Clusters.class).to(ClustersImpl.class);
     bind(ActionDBAccessor.class).to(ActionDBAccessorImpl.class);
     bindConstant().annotatedWith(Names.named("schedulerSleeptime")).to(10000L);
-    bindConstant().annotatedWith(Names.named("actionTimeout")).to(60000L);
+    bindConstant().annotatedWith(Names.named("actionTimeout")).to(300000L);
     bind(AmbariManagementController.class)
         .to(AmbariManagementControllerImpl.class);
   }
+
+  private void installFactories() {
+    install(new FactoryModuleBuilder().implement(
+        Cluster.class, ClusterImpl.class).build(ClusterFactory.class));
+    install(new FactoryModuleBuilder().implement(
+        Host.class, HostImpl.class).build(HostFactory.class));
+    install(new FactoryModuleBuilder().implement(
+        Service.class, ServiceImpl.class).build(ServiceFactory.class));
+    install(new FactoryModuleBuilder().implement(
+        ServiceComponent.class, ServiceComponentImpl.class).build(
+        ServiceComponentFactory.class));
+    install(new FactoryModuleBuilder().implement(
+        ServiceComponentHost.class, ServiceComponentHostImpl.class).build(
+        ServiceComponentHostFactory.class));
+    install(new FactoryModuleBuilder().implement(
+        Config.class, ConfigImpl.class).build(ConfigFactory.class));
+    install(new FactoryModuleBuilder().build(StageFactory.class));
+    install(new FactoryModuleBuilder().build(HostRoleCommandFactory.class));
+  }
+
+  private void bindInterceptors() {
+    ClearEntityManagerInterceptor clearEntityManagerInterceptor = new ClearEntityManagerInterceptor();
+    requestInjection(clearEntityManagerInterceptor);
+    bindInterceptor(Matchers.any(), Matchers.annotatedWith(Transactional.class), clearEntityManagerInterceptor);
+
+  }
 }

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/HostRequest.java Wed Nov  7 08:13:12 2012
@@ -62,7 +62,6 @@ public class HostRequest {
   }
 
   public String toString() {
-    // FIXME check if clusters and host attrs is non null before printing
     StringBuilder sb = new StringBuilder();
     sb.append("{"
         + ", hostname=" + hostname

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestStatusRequest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestStatusRequest.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestStatusRequest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestStatusRequest.java Wed Nov  7 08:13:12 2012
@@ -0,0 +1,37 @@
+/**
+ * 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.controller;
+
+public class RequestStatusRequest {
+
+  private final Long requestId;
+
+  public RequestStatusRequest(Long requestId) {
+    super();
+    this.requestId = requestId;
+  }
+
+  /**
+   * @return the requestId
+   */
+  public Long getRequestId() {
+    return requestId;
+  }
+
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestStatusResponse.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestStatusResponse.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestStatusResponse.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestStatusResponse.java Wed Nov  7 08:13:12 2012
@@ -0,0 +1,71 @@
+/**
+ * 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.controller;
+
+import java.util.List;
+
+public class RequestStatusResponse {
+
+  // Request ID for tracking async operations
+  private final Long requestId;
+
+  List<ShortTaskStatus> tasks;
+
+  // TODO how are logs to be sent back?
+  private String logs;
+
+  // TODO stage specific information
+
+  public RequestStatusResponse(Long requestId) {
+    super();
+    this.requestId = requestId;
+  }
+
+  /**
+   * @return the logs
+   */
+  public String getLogs() {
+    return logs;
+  }
+
+  /**
+   * @param logs the logs to set
+   */
+  public void setLogs(String logs) {
+    this.logs = logs;
+  }
+
+  /**
+   * @return the requestId
+   */
+  public long getRequestId() {
+    return requestId;
+  }
+
+  public List<ShortTaskStatus> getTasks() {
+    return tasks;
+  }
+
+  public void setTasks(List<ShortTaskStatus> tasks) {
+    this.tasks = tasks;
+  }
+
+
+
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestsByStatusesRequest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestsByStatusesRequest.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestsByStatusesRequest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/RequestsByStatusesRequest.java Wed Nov  7 08:13:12 2012
@@ -0,0 +1,47 @@
+/*
+ * 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.controller;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.ambari.server.actionmanager.HostRoleStatus;
+
+public class RequestsByStatusesRequest {
+  Set<String> statuses;
+
+  public RequestsByStatusesRequest() {
+    statuses = new HashSet<String>();
+    statuses.add(HostRoleStatus.PENDING.toString());
+    statuses.add(HostRoleStatus.QUEUED.toString());
+    statuses.add(HostRoleStatus.IN_PROGRESS.toString());
+  }
+
+  public RequestsByStatusesRequest(Set<String> statuses) {
+    this.statuses = statuses;
+  }
+
+  public Set<String> getStatuses() {
+    return statuses;
+  }
+
+  public void setStatuses(Set<String> statuses) {
+    this.statuses = statuses;
+  }
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ShortTaskStatus.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ShortTaskStatus.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ShortTaskStatus.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ShortTaskStatus.java Wed Nov  7 08:13:12 2012
@@ -0,0 +1,112 @@
+/*
+ * 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.controller;
+
+import org.apache.ambari.server.actionmanager.HostRoleCommand;
+
+public class ShortTaskStatus {
+  protected long taskId;
+  protected long stageId;
+  protected String hostName;
+  protected String role;
+  protected String command;
+  protected String status;
+
+  public ShortTaskStatus() {
+  }
+
+  public ShortTaskStatus(int taskId, long stageId, String hostName, String role, String command, String status) {
+    this.taskId = taskId;
+    this.stageId = stageId;
+    this.hostName = hostName;
+    this.role = role;
+    this.command = command;
+    this.status = status;
+  }
+
+  public ShortTaskStatus(HostRoleCommand hostRoleCommand) {
+    this.taskId = hostRoleCommand.getTaskId();
+    this.stageId = hostRoleCommand.getStageId();
+    this.command = hostRoleCommand.getExecutionCommand().getRoleCommand().toString();
+    this.hostName = hostRoleCommand.getHostName();
+    this.role = hostRoleCommand.getRole().toString();
+    this.status = hostRoleCommand.getStatus().toString();
+  }
+
+  public long getTaskId() {
+    return taskId;
+  }
+
+  public void setTaskId(long taskId) {
+    this.taskId = taskId;
+  }
+
+  public long getStageId() {
+    return stageId;
+  }
+
+  public void setStageId(long stageId) {
+    this.stageId = stageId;
+  }
+
+  public String getHostName() {
+    return hostName;
+  }
+
+  public void setHostName(String hostName) {
+    this.hostName = hostName;
+  }
+
+  public String getRole() {
+    return role;
+  }
+
+  public void setRole(String role) {
+    this.role = role;
+  }
+
+  public String getCommand() {
+    return command;
+  }
+
+  public void setCommand(String command) {
+    this.command = command;
+  }
+
+  public String getStatus() {
+    return status;
+  }
+
+  public void setStatus(String status) {
+    this.status = status;
+  }
+
+  public String toString() {
+    StringBuilder sb = new StringBuilder();
+    sb.append("ShortTaskStatusDump "
+        + ", stageId=" + stageId
+        + ", taskId=" + taskId
+        + ", hostname=" + hostName
+        + ", role=" + role
+        + ", command=" + command
+        + ", status=" + status);
+    return sb.toString();
+  }
+
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/TaskStatusRequest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/TaskStatusRequest.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/TaskStatusRequest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/TaskStatusRequest.java Wed Nov  7 08:13:12 2012
@@ -0,0 +1,48 @@
+/*
+ * 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.controller;
+
+public class TaskStatusRequest {
+  protected Long requestId;
+  protected Long taskId;
+
+  public TaskStatusRequest() {
+  }
+
+  public TaskStatusRequest(Long requestId, Long taskId) {
+    this.requestId = requestId;
+    this.taskId = taskId;
+  }
+
+  public Long getRequestId() {
+    return requestId;
+  }
+
+  public void setRequestId(Long requestId) {
+    this.requestId = requestId;
+  }
+
+  public Long getTaskId() {
+    return taskId;
+  }
+
+  public void setTaskId(Long taskId) {
+    this.taskId = taskId;
+  }
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/TaskStatusResponse.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/TaskStatusResponse.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/TaskStatusResponse.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/TaskStatusResponse.java Wed Nov  7 08:13:12 2012
@@ -0,0 +1,108 @@
+/*
+ * 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.controller;
+
+import org.apache.ambari.server.actionmanager.HostRoleCommand;
+
+public class TaskStatusResponse extends ShortTaskStatus {
+  private long requestId;
+  private int exitCode;
+  private String stderr;
+  private String stdout;
+  private long startTime;
+  private short attemptCount;
+
+  public TaskStatusResponse() {
+  }
+
+  public TaskStatusResponse(long requestId,
+                            int taskId, long stageId, String hostName, String role, String command, String status,
+                            int exitCode, String stderr, String stdout, long startTime, short attemptCount) {
+    super(taskId, stageId, hostName, role, command, status);
+    this.requestId = requestId;
+    this.exitCode = exitCode;
+    this.stderr = stderr;
+    this.stdout = stdout;
+    this.startTime = startTime;
+    this.attemptCount = attemptCount;
+  }
+
+  public TaskStatusResponse(long requestId, HostRoleCommand hostRoleCommand) {
+    super(hostRoleCommand);
+    this.requestId = requestId;
+    this.exitCode = hostRoleCommand.getExitCode();
+    this.stderr = hostRoleCommand.getStderr();
+    this.stdout = hostRoleCommand.getStdout();
+    this.startTime = hostRoleCommand.getStartTime();
+    this.attemptCount = hostRoleCommand.getAttemptCount();
+  }
+
+  public long getRequestId() {
+    return requestId;
+  }
+
+  public void setRequestId(long requestId) {
+    this.requestId = requestId;
+  }
+
+  public int getExitCode() {
+    return exitCode;
+  }
+
+  public void setExitCode(int exitCode) {
+    this.exitCode = exitCode;
+  }
+
+  public String getStderr() {
+    return stderr;
+  }
+
+  public void setStderr(String stderr) {
+    this.stderr = stderr;
+  }
+
+  public String getStdout() {
+    return stdout;
+  }
+
+  public void setStdout(String stdout) {
+    this.stdout = stdout;
+  }
+
+  public long getStartTime() {
+    return startTime;
+  }
+
+  public void setStartTime(long startTime) {
+    this.startTime = startTime;
+  }
+
+  public short getAttemptCount() {
+    return attemptCount;
+  }
+
+  public void setAttemptCount(short attemptCount) {
+    this.attemptCount = attemptCount;
+  }
+  
+  @Override
+  public String toString() {
+      return super.toString();
+  }
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ganglia/GangliaHostProvider.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ganglia/GangliaHostProvider.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ganglia/GangliaHostProvider.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ganglia/GangliaHostProvider.java Wed Nov  7 08:13:12 2012
@@ -0,0 +1,33 @@
+/**
+ * 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.controller.ganglia;
+
+import org.apache.ambari.server.controller.spi.Resource;
+
+import java.util.Map;
+
+/**
+ *
+ */
+public interface GangliaHostProvider {
+
+  public String getGangliaCollectorHostName(String clusterName);
+
+  public Map<String, String> getGangliaHostClusterMap(String clusterName);
+}

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ganglia/GangliaPropertyProvider.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ganglia/GangliaPropertyProvider.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ganglia/GangliaPropertyProvider.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/ganglia/GangliaPropertyProvider.java Wed Nov  7 08:13:12 2012
@@ -46,7 +46,9 @@ public class GangliaPropertyProvider imp
 
   private final StreamProvider streamProvider;
 
-  private final String gangliaCollectorHostName;
+  private final GangliaHostProvider hostProvider;
+
+  private final PropertyId clusterNamePropertyId;
 
   private final PropertyId hostNamePropertyId;
 
@@ -56,15 +58,16 @@ public class GangliaPropertyProvider imp
   /**
    * Map of Ganglia cluster names keyed by component type.
    */
-  private static final Map<String, String> GANGLIA_CLUSTER_NAMES = new HashMap<String, String>();
+  public static final Map<String, String> GANGLIA_CLUSTER_NAMES = new HashMap<String, String>();
 
   static {
-    GANGLIA_CLUSTER_NAMES.put("NAMENODE",     "HDPNameNode");
-    GANGLIA_CLUSTER_NAMES.put("DATANODE",     "HDPSlaves");
-    GANGLIA_CLUSTER_NAMES.put("JOBTRACKER",   "HDPJobTracker");
-    GANGLIA_CLUSTER_NAMES.put("TASKTRACKER",  "HDPSlaves");
-    GANGLIA_CLUSTER_NAMES.put("HBASE_MASTER", "HDPHBaseMaster");
-    GANGLIA_CLUSTER_NAMES.put("HBASE_CLIENT", "HDPSlaves");
+    GANGLIA_CLUSTER_NAMES.put("NAMENODE",           "HDPNameNode");
+    GANGLIA_CLUSTER_NAMES.put("DATANODE",           "HDPSlaves");
+    GANGLIA_CLUSTER_NAMES.put("JOBTRACKER",         "HDPJobTracker");
+    GANGLIA_CLUSTER_NAMES.put("TASKTRACKER",        "HDPSlaves");
+    GANGLIA_CLUSTER_NAMES.put("HBASE_MASTER",       "HDPHBaseMaster");
+    GANGLIA_CLUSTER_NAMES.put("HBASE_CLIENT",       "HDPSlaves");
+    GANGLIA_CLUSTER_NAMES.put("HBASE_REGIONSERVER", "HDPSlaves");
   }
 
 
@@ -72,12 +75,14 @@ public class GangliaPropertyProvider imp
 
   public GangliaPropertyProvider(Map<String, Map<PropertyId, String>> componentMetrics,
                                  StreamProvider streamProvider,
-                                 String gangliaCollectorHostName,
+                                 GangliaHostProvider hostProvider,
+                                 PropertyId clusterNamePropertyId,
                                  PropertyId hostNamePropertyId,
                                  PropertyId componentNamePropertyId) {
     this.componentMetrics         = componentMetrics;
     this.streamProvider           = streamProvider;
-    this.gangliaCollectorHostName = gangliaCollectorHostName;
+    this.hostProvider             = hostProvider;
+    this.clusterNamePropertyId    = clusterNamePropertyId;
     this.hostNamePropertyId       = hostNamePropertyId;
     this.componentNamePropertyId  = componentNamePropertyId;
 
@@ -125,18 +130,33 @@ public class GangliaPropertyProvider imp
     if (getPropertyIds().isEmpty()) {
       return true;
     }
+    String clusterName = (String) resource.getPropertyValue(clusterNamePropertyId);
+
+    // TODO : what should we do if there is no Ganglia server?
+    if (hostProvider.getGangliaHostClusterMap(clusterName) == null ||
+        hostProvider.getGangliaCollectorHostName(clusterName) == null) {
+      return true;
+    }
+
+    String hostName = hostNamePropertyId == null ?
+        null : (String) resource.getPropertyValue(hostNamePropertyId);
+
+    String componentName;
+    String gangliaClusterName;
+    if (componentNamePropertyId == null) {
+      componentName = "*";
+      gangliaClusterName = hostProvider.getGangliaHostClusterMap(clusterName).get(hostName);
+    } else {
+      componentName = (String) resource.getPropertyValue(componentNamePropertyId);
+      gangliaClusterName = GANGLIA_CLUSTER_NAMES.get(componentName);
+    }
 
-    String componentName      = (String) resource.getPropertyValue(componentNamePropertyId);
-    String gangliaClusterName = GANGLIA_CLUSTER_NAMES.get(componentName);
     Map<PropertyId, String> metrics = componentMetrics.get(componentName);
 
     if (metrics == null || gangliaClusterName == null) {
       return true;
     }
 
-    String hostName = hostNamePropertyId == null ?
-        null : PropertyHelper.fixHostName((String) resource.getPropertyValue(hostNamePropertyId));
-
     Set<PropertyId> ids = PropertyHelper.getRequestPropertyIds(getPropertyIds(), request, predicate);
 
     for (PropertyId propertyId : ids) {
@@ -146,7 +166,8 @@ public class GangliaPropertyProvider imp
       if (metricName != null) {
         boolean temporal = propertyId.isTemporal();
 
-        String spec = getSpec(gangliaClusterName, hostName, metricName,
+        String spec = getSpec(clusterName, gangliaClusterName,
+            hostName == null ? null : PropertyHelper.fixHostName(hostName), metricName,
             temporal ? request.getTemporalInfo(propertyId) : null);
 
         try {
@@ -157,7 +178,8 @@ public class GangliaPropertyProvider imp
             resource.setProperty(propertyId, getValue(properties.get(0), temporal));
           }
         } catch (IOException e) {
-          throw new AmbariException("Can't get metric : " + metricName, e);
+          // TODO : log this
+//          throw new AmbariException("Can't get metric : " + metricName, e);
         }
       }
     }
@@ -206,6 +228,7 @@ public class GangliaPropertyProvider imp
    * Get the spec to locate the Ganglia stream from the given
    * request info.
    *
+   * @param clusterName     the cluster name
    * @param gangliaCluster  the ganglia cluster name
    * @param host            the host name
    * @param metric          the metric
@@ -213,7 +236,7 @@ public class GangliaPropertyProvider imp
    *
    * @return the spec
    */
-  protected String getSpec(String gangliaCluster,
+  protected String getSpec(String clusterName, String gangliaCluster,
                                   String host,
                                   String metric,
                                   TemporalInfo temporalInfo) {
@@ -221,7 +244,7 @@ public class GangliaPropertyProvider imp
     StringBuilder sb = new StringBuilder();
 
     sb.append("http://").
-        append(gangliaCollectorHostName).
+        append(hostProvider.getGangliaCollectorHostName(clusterName)).
         append("/ganglia/graph.php?c=").
         append(gangliaCluster);
 

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterControllerImpl.java Wed Nov  7 08:13:12 2012
@@ -25,10 +25,13 @@ import org.apache.ambari.server.controll
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.PropertyProvider;
 import org.apache.ambari.server.controller.spi.Request;
+import org.apache.ambari.server.controller.spi.RequestStatus;
 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.PredicateHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.Collections;
 import java.util.HashMap;
@@ -43,7 +46,9 @@ import java.util.Set;
  * Default cluster controller implementation.
  */
 public class ClusterControllerImpl implements ClusterController {
-
+  private final static Logger LOG =
+      LoggerFactory.getLogger(ClusterControllerImpl.class);
+  
   /**
    * Module of providers for this controller.
    */
@@ -72,8 +77,6 @@ public class ClusterControllerImpl imple
     resourceProviders   = new HashMap<Resource.Type, ResourceProvider>();
     propertyProviders   = new HashMap<Resource.Type, List<PropertyProvider>>();
     schemas             = new HashMap<Resource.Type, Schema>();
-
-    setProviders();
   }
 
 
@@ -82,12 +85,16 @@ public class ClusterControllerImpl imple
   @Override
   public Iterable<Resource> getResources(Resource.Type type, Request request, Predicate predicate)
       throws AmbariException{
-    ResourceProvider provider = resourceProviders.get(type);
+    ResourceProvider provider = ensureResourceProvider(type);
+    ensurePropertyProviders(type);
     Set<Resource> resources;
 
     if (provider == null) {
       resources = Collections.emptySet();
     } else {
+      LOG.info("Using resource provider "
+          + provider.getClass().getName()
+          + " for request type " + type.toString());
       resources = provider.getResources(request, predicate);
       resources = populateResources(type, resources, request, predicate);
     }
@@ -102,7 +109,7 @@ public class ClusterControllerImpl imple
     synchronized (schemas) {
       schema = schemas.get(type);
       if (schema == null) {
-        schema = new SchemaImpl(resourceProviders.get(type), propertyProviders.get(type));
+        schema = new SchemaImpl(ensureResourceProvider(type), ensurePropertyProviders(type));
         schemas.put(type, schema);
       }
     }
@@ -110,27 +117,31 @@ public class ClusterControllerImpl imple
   }
 
   @Override
-  public void createResources(Resource.Type type, Request request) throws AmbariException {
-    ResourceProvider provider = resourceProviders.get(type);
+  public RequestStatus createResources(Resource.Type type, Request request) throws AmbariException {
+    ResourceProvider provider = ensureResourceProvider(type);
     if (provider != null) {
-      provider.createResources(request);
+      return provider.createResources(request);
     }
+    return null;
   }
 
   @Override
-  public void updateResources(Resource.Type type, Request request, Predicate predicate) throws AmbariException {
-    ResourceProvider provider = resourceProviders.get(type);
+  public RequestStatus updateResources(Resource.Type type, Request request, Predicate predicate) throws AmbariException {
+    ResourceProvider provider = ensureResourceProvider(type);
     if (provider != null) {
-      provider.updateResources(request, predicate);
+      return provider.updateResources(request, predicate);
     }
+    return null;
   }
 
   @Override
-  public void deleteResources(Resource.Type type, Predicate predicate) throws AmbariException {
-    ResourceProvider provider = resourceProviders.get(type);
+  public RequestStatus deleteResources(Resource.Type type, Predicate predicate) throws AmbariException {
+
+    ResourceProvider provider = ensureResourceProvider(type);
     if (provider != null) {
-      provider.deleteResources(predicate);
+      return provider.deleteResources(predicate);
     }
+    return null;
   }
 
 
@@ -171,22 +182,19 @@ public class ClusterControllerImpl imple
     return !requestPropertyIds.isEmpty();
   }
 
-  private void setProviders() {
-    resourceProviders.put(Resource.Type.Cluster, providerModule.getResourceProvider(Resource.Type.Cluster));
-    resourceProviders.put(Resource.Type.Service, providerModule.getResourceProvider(Resource.Type.Service));
-    resourceProviders.put(Resource.Type.Host, providerModule.getResourceProvider(Resource.Type.Host));
-    resourceProviders.put(Resource.Type.Component, providerModule.getResourceProvider(Resource.Type.Component));
-    resourceProviders.put(Resource.Type.HostComponent, providerModule.getResourceProvider(Resource.Type.HostComponent));
-    resourceProviders.put(Resource.Type.Configuration, providerModule.getResourceProvider(Resource.Type.Configuration));
-
-    propertyProviders.put(Resource.Type.Cluster, providerModule.getPropertyProviders(Resource.Type.Cluster));
-    propertyProviders.put(Resource.Type.Service, providerModule.getPropertyProviders(Resource.Type.Service));
-    propertyProviders.put(Resource.Type.Host, providerModule.getPropertyProviders(Resource.Type.Host));
-    propertyProviders.put(Resource.Type.Component, providerModule.getPropertyProviders(Resource.Type.Component));
-    propertyProviders.put(Resource.Type.HostComponent, providerModule.getPropertyProviders(Resource.Type.HostComponent));
-    propertyProviders.put(Resource.Type.Configuration, providerModule.getPropertyProviders(Resource.Type.Configuration));
+  private ResourceProvider ensureResourceProvider(Resource.Type type) {
+    if (!resourceProviders.containsKey(type)) {
+      resourceProviders.put(type, providerModule.getResourceProvider(type));
+    }
+    return resourceProviders.get(type);
   }
 
+  private List<PropertyProvider> ensurePropertyProviders(Resource.Type type) {
+    if (!propertyProviders.containsKey(type)) {
+      propertyProviders.put(type, providerModule.getPropertyProviders(type));
+    }
+    return propertyProviders.get(type);
+  }
 
   // ----- ResourceIterable inner class --------------------------------------
 

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/DefaultProviderModule.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/DefaultProviderModule.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/DefaultProviderModule.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/DefaultProviderModule.java Wed Nov  7 08:13:12 2012
@@ -20,7 +20,9 @@ package org.apache.ambari.server.control
 
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.controller.AmbariServer;
+import org.apache.ambari.server.controller.ganglia.GangliaHostProvider;
 import org.apache.ambari.server.controller.ganglia.GangliaPropertyProvider;
+import org.apache.ambari.server.controller.jmx.JMXHostProvider;
 import org.apache.ambari.server.controller.jmx.JMXPropertyProvider;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.PropertyId;
@@ -35,6 +37,8 @@ import org.apache.ambari.server.controll
 import org.codehaus.jackson.map.ObjectMapper;
 import org.codehaus.jackson.type.TypeReference;
 
+import com.google.inject.Inject;
+
 import java.io.IOException;
 import java.util.Collections;
 import java.util.HashMap;
@@ -48,9 +52,15 @@ import java.util.Set;
  */
 public class DefaultProviderModule implements ProviderModule {
 
+  private static final PropertyId HOST_NAME_PROPERTY_ID                     = PropertyHelper.getPropertyId("host_name", "Hosts");
+  private static final PropertyId HOST_IP_PROPERTY_ID                       = PropertyHelper.getPropertyId("ip", "Hosts");
   private static final PropertyId HOST_ATTRIBUTES_PROPERTY_ID               = PropertyHelper.getPropertyId("attributes", "Hosts");
+  private static final PropertyId CLUSTER_NAME_PROPERTY_ID                  = PropertyHelper.getPropertyId("cluster_name", "Clusters");
+  private static final PropertyId HOST_COMPONENT_CLUSTER_NAME_PROPERTY_ID   = PropertyHelper.getPropertyId("cluster_name", "HostRoles");
   private static final PropertyId HOST_COMPONENT_HOST_NAME_PROPERTY_ID      = PropertyHelper.getPropertyId("host_name", "HostRoles");
   private static final PropertyId HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("component_name", "HostRoles");
+  private static final String     GANGLIA_SERVER                            = "GANGLIA_SERVER";
+//  private static final String     GANGLIA_SERVER                    = "GANGLIA_MONITOR_SERVER";
 
   /**
    * The map of resource providers.
@@ -62,15 +72,9 @@ public class DefaultProviderModule imple
    */
   private final Map<Resource.Type,List<PropertyProvider>> propertyProviders = new HashMap<Resource.Type, List<PropertyProvider>>();
 
-  /**
-   * The map of hosts.
-   */
-  private Map<String, String> hostMapping;
+  @Inject
+  private AmbariManagementController managementController;
 
-  /**
-   * The host name of the Ganglia collector.
-   */
-  private String gangliaCollectorHostName;
 
 
   // ----- Constructors ------------------------------------------------------
@@ -79,24 +83,8 @@ public class DefaultProviderModule imple
    * Create a default provider module.
    */
   public DefaultProviderModule() {
-    AmbariManagementController managementController = AmbariServer.getController();
-
-    // First create all of the resource providers...
-    for (Resource.Type type : Resource.Type.values()){
-      createResourceProvider(type, managementController);
-    }
-
-    // ... then create the things needed to create the property providers ...
-    try {
-      hostMapping              = getHostMap();
-      gangliaCollectorHostName = getGangliaCollectorHost();
-    } catch (AmbariException e) {
-      // TODO ...
-    }
-
-    // ... then create all of the property providers
-    for (Resource.Type type : Resource.Type.values()){
-      createPropertyProviders(type);
+    if (managementController == null) {
+      managementController = AmbariServer.getController();
     }
   }
 
@@ -105,75 +93,246 @@ public class DefaultProviderModule imple
 
   @Override
   public ResourceProvider getResourceProvider(Resource.Type type) {
+    if (!propertyProviders.containsKey(type)) {
+      createResourceProvider(type);
+    }
     return resourceProviders.get(type);
   }
 
   @Override
   public List<PropertyProvider> getPropertyProviders(Resource.Type type) {
+
+    if (!propertyProviders.containsKey(type)) {
+      createPropertyProviders(type);
+    }
     return propertyProviders.get(type);
   }
 
 
   // ----- utility methods ---------------------------------------------------
 
-  private void createResourceProvider(Resource.Type type, AmbariManagementController managementController) {
-    resourceProviders.put( type , ResourceProviderImpl.getResourceProvider(
-        type,
-        PropertyHelper.getPropertyIds(type),
-        PropertyHelper.getKeyPropertyIds(type), managementController));
+  protected void putResourceProvider(Resource.Type type, ResourceProvider resourceProvider) {
+    resourceProviders.put( type , resourceProvider);
   }
 
-  private void createPropertyProviders(Resource.Type type) {
+  protected void createResourceProvider(Resource.Type type) {
+    ResourceProvider resourceProvider =
+        ResourceProviderImpl.getResourceProvider(type, PropertyHelper.getPropertyIds(type),
+            PropertyHelper.getKeyPropertyIds(type), managementController);
+
+    putResourceProvider(type, resourceProvider);
+  }
+
+  protected void putPropertyProviders(Resource.Type type, List<PropertyProvider> providers) {
+    propertyProviders.put(type, providers);
+  }
+
+  protected void createPropertyProviders(Resource.Type type) {
     List<PropertyProvider> providers = new LinkedList<PropertyProvider>();
-    if (type == Resource.Type.HostComponent) {
-      providers.add(new JMXPropertyProvider(
-          PropertyHelper.getJMXPropertyIds(type),
-          new URLStreamProvider(),
-          hostMapping));
-
-      providers.add(new GangliaPropertyProvider(
-          PropertyHelper.getGangliaPropertyIds(type),
-          new URLStreamProvider(),
-          gangliaCollectorHostName,
-          PropertyHelper.getPropertyId("host_name", "HostRoles"),
-          PropertyHelper.getPropertyId("component_name", "HostRoles")));
+
+    URLStreamProvider streamProvider = new URLStreamProvider();
+
+    JMXHostProvider     jmxHostProvider = new DefaultJMXHostProvider();
+    GangliaHostProvider gangliaHostProvider = new DefaultGangliaHostProvider(jmxHostProvider);
+
+    switch (type){
+      case Host :
+        providers.add(new GangliaPropertyProvider(
+            PropertyHelper.getGangliaPropertyIds(type),
+            streamProvider,
+            gangliaHostProvider,
+            PropertyHelper.getPropertyId("cluster_name", "Hosts"),
+            PropertyHelper.getPropertyId("host_name", "Hosts"),
+            null));
+        break;
+      case Component :
+        providers.add(new GangliaPropertyProvider(
+            PropertyHelper.getGangliaPropertyIds(type),
+            streamProvider,
+            gangliaHostProvider,
+            PropertyHelper.getPropertyId("cluster_name", "ServiceComponentInfo"),
+            null,
+            PropertyHelper.getPropertyId("component_name", "ServiceComponentInfo")));
+        break;
+      case HostComponent:
+        if (type == Resource.Type.HostComponent) {
+          providers.add(new JMXPropertyProvider(
+              PropertyHelper.getJMXPropertyIds(type),
+              streamProvider,
+              jmxHostProvider));
+
+          providers.add(new GangliaPropertyProvider(
+              PropertyHelper.getGangliaPropertyIds(type),
+              streamProvider,
+              gangliaHostProvider,
+              PropertyHelper.getPropertyId("cluster_name", "HostRoles"),
+              PropertyHelper.getPropertyId("host_name", "HostRoles"),
+              PropertyHelper.getPropertyId("component_name", "HostRoles")));
+        }
+        break;
+      default :
+        break;
     }
-    propertyProviders.put(type, providers);
+    putPropertyProviders(type, providers);
   }
 
-  public Map<String, String> getHostMap() throws AmbariException {
-    Map<String, String> hostMap      = new HashMap<String, String>();
-    ResourceProvider    hostProvider = getResourceProvider(Resource.Type.Host);
-    ObjectMapper        mapper       = new ObjectMapper();
-    Request             request      = PropertyHelper.getReadRequest(Collections.singleton(HOST_ATTRIBUTES_PROPERTY_ID));
-
-    Set<Resource> hosts = hostProvider.getResources(request, null);
-    for (Resource host : hosts) {
-      String attributes = (String) host.getPropertyValue(HOST_ATTRIBUTES_PROPERTY_ID);
-      if (attributes != null && !attributes.startsWith("[]")) {
-        try {
-          Map<String, String> attributeMap = mapper.readValue(attributes, new TypeReference<Map<String, String>>() {});
-          hostMap.put(attributeMap.get("privateFQDN"), attributeMap.get("publicFQDN"));
-        } catch (IOException e) {
-          throw new IllegalStateException("Can't read hosts " + attributes, e);
+  private class DefaultJMXHostProvider implements JMXHostProvider {
+
+    /**
+     * The map of hosts.
+     */
+    private Map<String, Map<String, String>> hostMapping;
+
+    private boolean init = false;
+
+
+    @Override
+    public Map<String, String> getHostMapping(String hostName) {
+      if (!init) {
+        init = true;
+        getHostMap();
+      }
+
+      return hostMapping.get(hostName);
+    }
+
+    protected void getHostMap() {
+      ResourceProvider provider = getResourceProvider(Resource.Type.Cluster);
+      Request          request  = PropertyHelper.getReadRequest(CLUSTER_NAME_PROPERTY_ID);
+
+      try {
+        Set<Resource> clusters = provider.getResources(request, null);
+        provider = getResourceProvider(Resource.Type.Host);
+        request  = PropertyHelper.getReadRequest(HOST_NAME_PROPERTY_ID, HOST_IP_PROPERTY_ID, HOST_ATTRIBUTES_PROPERTY_ID);
+
+        ObjectMapper mapper = new ObjectMapper();
+
+        hostMapping = new HashMap<String, Map<String, String>>();
+
+        for (Resource cluster : clusters) {
+
+          String clusterName = (String) cluster.getPropertyValue(CLUSTER_NAME_PROPERTY_ID);
+
+          Predicate predicate = new PredicateBuilder().property(HOST_COMPONENT_CLUSTER_NAME_PROPERTY_ID).equals(clusterName).toPredicate();
+
+          Set<Resource> hosts = provider.getResources(request, predicate);
+
+          Map<String, String> map = hostMapping.get(clusterName);
+
+          if (map == null) {
+            map = new HashMap<String, String>();
+            hostMapping.put(clusterName, map);
+          }
+
+          for (Resource host : hosts) {
+            String attributes = (String) host.getPropertyValue(HOST_ATTRIBUTES_PROPERTY_ID);
+            if (attributes != null && !attributes.startsWith("[]")) {
+              try {
+                Map<String, String> attributeMap = mapper.readValue(attributes, new TypeReference<Map<String, String>>() {});
+                map.put(attributeMap.get("privateFQDN"), attributeMap.get("publicFQDN"));
+              } catch (IOException e) {
+                throw new IllegalStateException("Can't read hosts " + attributes, e);
+              }
+            } else {
+              String hostName = (String) host.getPropertyValue(HOST_NAME_PROPERTY_ID);
+              String ip = (String) host.getPropertyValue(HOST_IP_PROPERTY_ID);
+              map.put(hostName, ip);
+            }
+          }
         }
+      } catch (AmbariException e) {
+        //TODO
       }
     }
-    return hostMap;
   }
 
-  public String getGangliaCollectorHost() throws AmbariException {
-    ResourceProvider provider = getResourceProvider(Resource.Type.HostComponent);
-    Request          request  = PropertyHelper.getReadRequest(Collections.singleton(HOST_COMPONENT_HOST_NAME_PROPERTY_ID));
+  private class DefaultGangliaHostProvider implements GangliaHostProvider {
 
-    Predicate predicate = new PredicateBuilder().property(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID).
-        equals("GANGLIA_MONITOR_SERVER").toPredicate();
+    /**
+     * The host name of the Ganglia collector.
+     */
+    private Map<String, String> gangliaCollectorHostName;
 
-    Set<Resource> hostComponents = provider.getResources(request, predicate);
-    // should only be one. TODO add check
-    String hostName = (String) hostComponents.iterator().next().getPropertyValue(HOST_COMPONENT_HOST_NAME_PROPERTY_ID);
+    /**
+     * Map of hosts to Ganglia cluster names.
+     */
+    private Map<String, Map<String, String>> gangliaHostClusterMap;
 
-    return hostMapping.get(hostName);
-  }
+    private final JMXHostProvider hostProvider;
+
+    private boolean init = false;
+
+    private DefaultGangliaHostProvider(JMXHostProvider hostProvider) {
+      this.hostProvider = hostProvider;
+    }
+
+    @Override
+    public String getGangliaCollectorHostName(String clusterName) {
+      if (!init) {
+        init = true;
+        getGangliaCollectorHost();
+      }
+      return gangliaCollectorHostName.get(clusterName);
+    }
+
+    @Override
+    public Map<String, String> getGangliaHostClusterMap(String clusterName) {
+      if (!init) {
+        init = true;
+        getGangliaCollectorHost();
+      }
+      return gangliaHostClusterMap.get(clusterName);
+    }
+
+    protected void getGangliaCollectorHost() {
+
+      ResourceProvider provider = getResourceProvider(Resource.Type.Cluster);
+      Request          request  = PropertyHelper.getReadRequest(CLUSTER_NAME_PROPERTY_ID);
 
+      try {
+        Set<Resource> clusters = provider.getResources(request, null);
+
+        provider = getResourceProvider(Resource.Type.HostComponent);
+        request  = PropertyHelper.getReadRequest(HOST_COMPONENT_HOST_NAME_PROPERTY_ID,
+            HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID);
+
+        gangliaHostClusterMap = new HashMap<String, Map<String, String>>();
+        gangliaCollectorHostName = new HashMap<String, String>();
+
+        for (Resource cluster : clusters) {
+
+
+          String clusterName = (String) cluster.getPropertyValue(CLUSTER_NAME_PROPERTY_ID);
+
+          Predicate predicate = new PredicateBuilder().property(HOST_COMPONENT_CLUSTER_NAME_PROPERTY_ID).equals(clusterName).toPredicate();
+
+          Set<Resource> hostComponents = provider.getResources(request, predicate);
+
+          Map<String, String> hostClusterMap = gangliaHostClusterMap.get(clusterName);
+
+          if (hostClusterMap == null) {
+            hostClusterMap = new HashMap<String, String>();
+            gangliaHostClusterMap.put(clusterName, hostClusterMap);
+          }
+
+          for (Resource hostComponent : hostComponents) {
+            String componentName = (String) hostComponent.getPropertyValue(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID);
+            String hostName      = (String) hostComponent.getPropertyValue(HOST_COMPONENT_HOST_NAME_PROPERTY_ID);
+
+            String gangliaCluster = GangliaPropertyProvider.GANGLIA_CLUSTER_NAMES.get(componentName);
+            if (gangliaCluster != null) {
+              if (!hostClusterMap.containsKey(hostName)) {
+                hostClusterMap.put(hostName, gangliaCluster);
+              }
+            }
+            if (componentName.equals(GANGLIA_SERVER)) {
+              gangliaCollectorHostName.put(clusterName, hostProvider.getHostMapping(clusterName).get(hostName));
+            }
+          }
+        }
+      } catch (AmbariException e) {
+        //TODO
+      }
+    }
+  }
 }

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyIdImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyIdImpl.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyIdImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyIdImpl.java Wed Nov  7 08:13:12 2012
@@ -89,11 +89,15 @@ public class PropertyIdImpl implements P
   @Override
   public boolean equals(Object o) {
 
+    if (o == null) {
+      return false;
+    }
+
     if (this == o) {
       return true;
     }
 
-    if (!(o.getClass().equals(PropertyIdImpl.class))) {
+    if (! (o instanceof  PropertyIdImpl)) {
       return false;
     }
 

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java?rev=1406489&r1=1406488&r2=1406489&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java Wed Nov  7 08:13:12 2012
@@ -23,6 +23,7 @@ import org.apache.ambari.server.controll
 import org.apache.ambari.server.controller.spi.TemporalInfo;
 
 import java.util.*;
+import java.util.Map.Entry;
 
 /**
  * Default request implementation.
@@ -108,4 +109,38 @@ public class RequestImpl implements Requ
     result = 31 * result + (properties != null ? properties.hashCode() : 0);
     return result;
   }
+
+  public String toString() {
+    StringBuilder sb = new StringBuilder();
+    sb.append("Request:"
+        + ", propertyIds=[");
+    for (PropertyId pId : propertyIds) {
+      sb.append(" { propertyName=" + pId.getName()
+          + ", propertyCategory=" + pId.getCategory()
+          + " }, ");
+    }
+    sb.append(" ], properties=[ ");
+    for (Map<PropertyId, Object> map : properties) {
+      for (Entry<PropertyId, Object> entry : map.entrySet()) {
+        sb.append(" { propertyName=" + entry.getKey().getName()
+          + ", propertyCategory=" + entry.getKey().getCategory()
+          + ", propertyValue=" + entry.getValue().toString()
+          + " }, ");
+      }
+    }
+    sb.append(" ], temporalInfo=[");
+    if (m_mapTemporalInfo == null) {
+      sb.append("null");
+    } else {
+      for (Entry<PropertyId, TemporalInfo> entry :
+        m_mapTemporalInfo.entrySet()) {
+        sb.append(" { propertyName=" + entry.getKey().getName()
+            + ", propertyCategory=" + entry.getKey().getCategory()
+            + ", temporalInfo=" + entry.getValue().toString());
+      }
+    }
+    sb.append(" ]");
+    return sb.toString();
+  }
+
 }

Added: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java?rev=1406489&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java Wed Nov  7 08:13:12 2012
@@ -0,0 +1,54 @@
+/**
+ * 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.controller.internal;
+
+import org.apache.ambari.server.controller.spi.RequestStatus;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * Default request status implementation.
+ */
+public class RequestStatusImpl implements RequestStatus{
+
+  private final Resource requestResource;
+
+  public RequestStatusImpl(Resource requestResource) {
+    this.requestResource = requestResource;
+  }
+
+  @Override
+  public Set<Resource> getAssociatedResources() {
+    return Collections.emptySet();  // TODO : handle in M4
+  }
+
+  @Override
+  public Resource getRequestResource() {
+    return requestResource;
+  }
+
+  @Override
+  public Status getStatus() {
+
+    return requestResource == null ? Status.Complete :
+        Status.valueOf((String) requestResource.getPropertyValue(PropertyHelper.getPropertyId("status", "Requests")));
+  }
+}