pzampino commented on a change in pull request #239: KNOX-2153 - CM discovery - 
Monitor Cloudera Manager
URL: https://github.com/apache/knox/pull/239#discussion_r369007060
 
 

 ##########
 File path: 
gateway-discovery-cm/src/main/java/org/apache/knox/gateway/topology/discovery/cm/ClouderaManagerClusterConfigurationMonitor.java
 ##########
 @@ -0,0 +1,940 @@
+/*
+ * 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.knox.gateway.topology.discovery.cm;
+
+import com.cloudera.api.swagger.EventsResourceApi;
+import com.cloudera.api.swagger.RolesResourceApi;
+import com.cloudera.api.swagger.ServicesResourceApi;
+import com.cloudera.api.swagger.client.ApiClient;
+import com.cloudera.api.swagger.client.ApiException;
+import com.cloudera.api.swagger.model.ApiConfig;
+import com.cloudera.api.swagger.model.ApiConfigList;
+import com.cloudera.api.swagger.model.ApiEvent;
+import com.cloudera.api.swagger.model.ApiEventAttribute;
+import com.cloudera.api.swagger.model.ApiEventCategory;
+import com.cloudera.api.swagger.model.ApiEventQueryResult;
+import com.cloudera.api.swagger.model.ApiRole;
+import com.cloudera.api.swagger.model.ApiRoleList;
+import com.cloudera.api.swagger.model.ApiServiceConfig;
+import org.apache.commons.io.FileUtils;
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.i18n.messages.MessagesFactory;
+import org.apache.knox.gateway.services.security.AliasService;
+import org.apache.knox.gateway.topology.discovery.ClusterConfigurationMonitor;
+import org.apache.knox.gateway.topology.discovery.ServiceDiscoveryConfig;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.nio.file.Files;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.TimeZone;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * ClusterConfigurationMonitor implementation for clusters managed by 
ClouderaManager.
+ */
+public class ClouderaManagerClusterConfigurationMonitor implements 
ClusterConfigurationMonitor {
+
+  private static final String TYPE = "CM";
+
+  private static final String CLUSTERS_DATA_DIR_NAME = TYPE + "-clusters";
+
+  private static final String PERSISTED_FILE_COMMENT = "Generated File. Do Not 
Edit!";
+
+  private static final String PROP_CLUSTER_PREFIX = "cluster.";
+  private static final String PROP_CLUSTER_SOURCE = PROP_CLUSTER_PREFIX + 
"source";
+  private static final String PROP_CLUSTER_NAME   = PROP_CLUSTER_PREFIX + 
"name";
+  private static final String PROP_CLUSTER_USER   = PROP_CLUSTER_PREFIX + 
"user";
+  private static final String PROP_CLUSTER_ALIAS  = PROP_CLUSTER_PREFIX + 
"pwd.alias";
+
+  private static final ClouderaManagerServiceDiscoveryMessages log =
+                              
MessagesFactory.get(ClouderaManagerServiceDiscoveryMessages.class);
+
+
+  // The format of the filter employed when restart events are queried from 
ClouderaManager
+  private static final String RESTART_EVENTS_QUERY_FORMAT = "category==" + 
ApiEventCategory.AUDIT_EVENT.getValue() +
+                                                            
";attributes.command==Restart" +
+                                                            
";attributes.command_status==SUCCEEDED" +
+                                                            
";attributes.cluster==\"%s\"%s";
+
+  // The format of the timestamp element of the restart events query filter
+  private static final String EVENTS_QUERY_TIMESTAMP_FORMAT = 
";timeOccurred=gt=%s";
+
+  // The default amount of time before "now" the monitor will check for 
restart events the first time
+  private static final long DEFAULT_EVENT_QUERY_DEFAULT_TIMESTAMP_OFFSET = (60 
* 60 * 1000); // one hour
+
+  // ISO 8601 datetime format for restart event query filtering
+  private DateFormat eventQueryTimestampFormat = new 
SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'", Locale.getDefault());
+
+  private GatewayConfig gatewayConfig;
+  private AliasService  aliasService;
+  private PollingConfigAnalyzer internalMonitor;
+  private List<ConfigurationChangeListener> changeListeners = new 
ArrayList<>();
+
+  // Cache of ClouderaManager API clients, keyed by discovery address
+  private Map<String, DiscoveryApiClient> clients = new ConcurrentHashMap<>();
+
+  // ClouderaManager address
+  //    clusterName -> ServiceDiscoveryConfig
+  //
+  private Map<String, Map<String, ServiceDiscoveryConfig>> 
clusterMonitorConfigurations = new ConcurrentHashMap<>();
+
+  // ClouderaManager address
+  //    clusterName
+  //        serviceType -> Properties
+  //
+  private Map<String, Map<String, Map<String, 
ServiceConfigurationProperties>>> clusterServiceConfigurations =
+                                                                               
         new ConcurrentHashMap<>();
+
+  private ReadWriteLock serviceConfigurationsLock = new 
ReentrantReadWriteLock();
+
+  private ReadWriteLock clusterMonitorConfigurationsLock = new 
ReentrantReadWriteLock();
+
+  // Timestamp records of the most recent restart event query per discovery 
address
+  private Map<String, String> eventQueryTimestamps = new ConcurrentHashMap<>();
+
+  // The amount of time before "now" the monitor will check for restart events 
the first time
+  private long eventQueryDefaultTimestampOffset = 
DEFAULT_EVENT_QUERY_DEFAULT_TIMESTAMP_OFFSET;
+
+  static String getType() {
+    return TYPE;
+  }
+
+  ClouderaManagerClusterConfigurationMonitor(final GatewayConfig config, final 
AliasService aliasService) {
+    this.gatewayConfig   = config;
+    this.aliasService    = aliasService;
+    this.internalMonitor = new PollingConfigAnalyzer(this);
+
+    eventQueryTimestampFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+
+    // Override the default polling interval if it has been configured
+    // (org.apache.knox.gateway.topology.discovery.cm.monitor.interval)
+    int interval = config.getClusterMonitorPollingInterval(getType());
+    if (interval > 0) {
+      setPollingInterval(interval);
+    }
+
+    init();
+  }
+
+  @Override
+  @SuppressWarnings("PMD.DoNotUseThreads")
+  public void start() {
+    log.startingClouderaManagerConfigMonitor();
+    (new Thread(internalMonitor, 
"ClouderaManagerConfigurationMonitor")).start();
+  }
+
+  @Override
+  public void stop() {
+    log.stoppingClouderaManagerConfigMonitor();
+    internalMonitor.stop();
+  }
+
+  @Override
+  public void setPollingInterval(int interval) {
+    internalMonitor.setInterval(interval);
+  }
+
+  @Override
+  public void addListener(final ConfigurationChangeListener listener) {
+    changeListeners.add(listener);
+  }
+
+  @Override
+  public void clearCache(String source, String clusterName) {
+    removeServiceConfiguration(source, clusterName);
+  }
+
+  /**
+   * Add the specified cluster service configurations to the monitor.
+   *
+   * @param cluster         The cluster to be added.
+   * @param discoveryConfig The discovery configuration associated with the 
cluster.
+   */
+  void addServiceConfiguration(final ClouderaManagerCluster cluster,
+                               final ServiceDiscoveryConfig discoveryConfig) {
+
+    String address     = discoveryConfig.getAddress();
+    String clusterName = cluster.getName();
+
+    // Disregard restart events, which occurred before now in future polling
+    setEventQueryTimestamp(address, clusterName, new Date());
+
+    persistDiscoveryConfiguration(clusterName, discoveryConfig);
+    addDiscoveryConfig(clusterName, discoveryConfig);
+
+    Map<String, List<ServiceModel>> serviceModels = cluster.getServiceModels();
+
+    // Process the service models
+    Map<String, ServiceConfigurationProperties> scpMap = new HashMap<>();
+    for (String service : serviceModels.keySet()) {
+      for (ServiceModel model : serviceModels.get(service)) {
+        ServiceConfigurationProperties scp =
+            scpMap.computeIfAbsent(model.getServiceType(), p -> new 
ServiceConfigurationProperties());
+
+        Map<String, String> serviceProps = model.getServiceProperties();
+        for (Map.Entry<String, String> entry : serviceProps.entrySet()) {
+          scp.addServiceProperty(entry.getKey(), entry.getValue());
+        }
+
+        Map<String, Map<String, String>> roleProps = model.getRoleProperties();
+        for (String roleName : roleProps.keySet()) {
+          Map<String, String> rp = roleProps.get(roleName);
+          for (Map.Entry<String, String> entry : rp.entrySet()) {
+            scp.addRoleProperty(roleName, entry.getKey(), entry.getValue());
+          }
+        }
+      }
+    }
+
+    // Persist the service configurations
+    persistServiceConfiguration(address, clusterName, scpMap);
+
+    // Add the service configurations
+    addServiceConfiguration(address, clusterName, scpMap);
+  }
+
+  private void addServiceConfiguration(final String                            
          address,
+                                       final String                            
          cluster,
+                                       final Map<String, 
ServiceConfigurationProperties> configs) {
+    serviceConfigurationsLock.writeLock().lock();
+    try {
+      clusterServiceConfigurations.computeIfAbsent(address, k -> new 
HashMap<>()).put(cluster, configs);
+    } finally {
+      serviceConfigurationsLock.writeLock().unlock();
+    }
+  }
+
+  private void init() {
+    // Load any persisted discovery configuration data
+    loadDiscoveryConfiguration();
+
+    // Load any persisted cluster service configuration data
+    loadServiceConfiguration();
+  }
+
+  /**
+   * Get a DiscoveryApiClient for the ClouderaManager instance described by 
the specified discovery configuration.
+   *
+   * @param discoveryConfig The discovery configuration for interacting with a 
ClouderaManager instance.
+   */
+  private DiscoveryApiClient getApiClient(final ServiceDiscoveryConfig 
discoveryConfig) {
+    return clients.computeIfAbsent(discoveryConfig.getAddress(),
+                                   c -> new 
DiscoveryApiClient(discoveryConfig, aliasService));
+  }
+
+  /**
+   * Load any previously-persisted service discovery configurations.
+   */
+  private void loadDiscoveryConfiguration() {
+    File persistenceDir = getPersistenceDir();
+    if (persistenceDir != null) {
+      Collection<File> persistedConfigs = FileUtils.listFiles(persistenceDir, 
new String[]{"conf"}, false);
+      for (File persisted : persistedConfigs) {
+        Properties props = new Properties();
+        try (InputStream in = Files.newInputStream(persisted.toPath())) {
+          props.load(in);
+
+          addDiscoveryConfig(props.getProperty(PROP_CLUSTER_NAME), new 
ServiceDiscoveryConfig() {
+            @Override
+            public String getAddress() {
+              return props.getProperty(PROP_CLUSTER_SOURCE);
+            }
+
+            @Override
+            public String getUser() {
+              return props.getProperty(PROP_CLUSTER_USER);
+            }
+
+            @Override
+            public String getPasswordAlias() {
+              return props.getProperty(PROP_CLUSTER_ALIAS);
+            }
+          });
+        } catch (IOException e) {
+          log.failedToLoadClusterMonitorServiceDiscoveryConfig(getType(), e);
+        }
+      }
+    }
+  }
+
+  /**
+   * Load any previously-persisted cluster service configuration data records, 
so the monitor can check
+   * previously-deployed topologies against the current cluster configuration, 
even across gateway restarts.
+   */
+  private void loadServiceConfiguration() {
+    File persistenceDir = getPersistenceDir();
+    if (persistenceDir != null) {
+      Collection<File> persistedConfigs = FileUtils.listFiles(persistenceDir, 
new String[]{"ver"}, false);
+      for (File persisted : persistedConfigs) {
+        try (ObjectInputStream in = new 
ObjectInputStream(Files.newInputStream(persisted.toPath()))) {
+          String clusterName      = (String) in.readObject();
+          String discoveryAddress = (String) in.readObject();
+          Map<String, ServiceConfigurationProperties> configs =
+              (Map<String, ServiceConfigurationProperties>) in.readObject();
 
 Review comment:
   Good point. I suppose I was rushing a bit to get this finished. It would be 
better to use an alternative serialization format.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to