summaryrefslogtreecommitdiff
path: root/ambari-server/src/main/resources/common-services/LOGSEARCH/0.5.0/service_advisor.py
blob: 059f59324b990236b0b2536c555bc75105159640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env ambari-python-wrap
"""
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.
"""

# Python imports
import imp
import os
import traceback
import re
import socket
import fnmatch
import math


from resource_management.core.logger import Logger

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
STACKS_DIR = os.path.join(SCRIPT_DIR, '../../../stacks/')
PARENT_FILE = os.path.join(STACKS_DIR, 'service_advisor.py')

try:
  with open(PARENT_FILE, 'rb') as fp:
    service_advisor = imp.load_module('service_advisor', fp, PARENT_FILE, ('.py', 'rb', imp.PY_SOURCE))
except Exception as e:
  traceback.print_exc()
  print "Failed to load parent"

class LogSearchServiceAdvisor(service_advisor.ServiceAdvisor):

  def __init__(self, *args, **kwargs):
    self.as_super = super(LogSearchServiceAdvisor, self)
    self.as_super.__init__(*args, **kwargs)

    # Always call these methods
    self.modifyMastersWithMultipleInstances()
    self.modifyCardinalitiesDict()
    self.modifyHeapSizeProperties()
    self.modifyNotValuableComponents()
    self.modifyComponentsNotPreferableOnServer()
    self.modifyComponentLayoutSchemes()

  def modifyMastersWithMultipleInstances(self):
    """
    Modify the set of masters with multiple instances.
    Must be overriden in child class.
    """
    # Nothing to do
    pass

  def modifyCardinalitiesDict(self):
    """
    Modify the dictionary of cardinalities.
    Must be overriden in child class.
    """
    # Nothing to do
    pass

  def modifyHeapSizeProperties(self):
    """
    Modify the dictionary of heap size properties.
    Must be overriden in child class.
    """
    pass

  def modifyNotValuableComponents(self):
    """
    Modify the set of components whose host assignment is based on other services.
    Must be overriden in child class.
    """
    # Nothing to do
    pass

  def modifyComponentsNotPreferableOnServer(self):
    """
    Modify the set of components that are not preferable on the server.
    Must be overriden in child class.
    """
    # Nothing to do
    pass

  def modifyComponentLayoutSchemes(self):
    """
    Modify layout scheme dictionaries for components.
    The scheme dictionary basically maps the number of hosts to
    host index where component should exist.
    Must be overriden in child class.
    """
    # Nothing to do
    pass

  def getServiceComponentLayoutValidations(self, services, hosts):
    """
    Get a list of errors.
    Must be overriden in child class.
    """

    return []

  def getServiceConfigurationRecommendations(self, configurations, clusterData, services, hosts):
    putLogSearchProperty = self.putProperty(configurations, "logsearch-properties", services)
    putLogSearchAttribute = self.putPropertyAttribute(configurations, "logsearch-properties")
    putLogSearchCommonEnvProperty = self.putProperty(configurations, "logsearch-common-env", services)
    putLogSearchCommonEnvAttribute = self.putPropertyAttribute(configurations, "logsearch-common-env")
    putLogSearchEnvAttribute = self.putPropertyAttribute(configurations, "logsearch-env")
    putLogFeederEnvAttribute = self.putPropertyAttribute(configurations, "logfeeder-env")

    logSearchServerHosts = self.getComponentHostNames(services, "LOGSEARCH", "LOGSEARCH_SERVER")
    # if there is no Log Search server on the cluster, i.e. there is an external server
    if logSearchServerHosts is None or len(logSearchServerHosts) == 0:
      # hide logsearch specific attributes
      for key in services['configurations']['logsearch-env']['properties']:
        putLogSearchEnvAttribute(key, 'visible', 'false')
      for key in services['configurations']['logsearch-properties']['properties']:
        putLogSearchAttribute(key, 'visible', 'false')
      for key in services['configurations']['logsearch-audit_logs-solrconfig']['properties']:
        self.putPropertyAttribute(configurations, "logsearch-audit_logs-solrconfig")(key, 'visible', 'false')
      for key in services['configurations']['logsearch-service_logs-solrconfig']['properties']:
        self.putPropertyAttribute(configurations, "logsearch-service_logs-solrconfig")(key, 'visible', 'false')
      for key in services['configurations']['logsearch-log4j']['properties']:
        self.putPropertyAttribute(configurations, "logsearch-log4j")(key, 'visible', 'false')
      for key in services['configurations']['logsearch-admin-json']['properties']:
        self.putPropertyAttribute(configurations, "logsearch-admin-json")(key, 'visible', 'false')
    # if there is a Log Search server on the cluster
    else:
      infraSolrHosts = self.getComponentHostNames(services, "AMBARI_INFRA_SOLR", "INFRA_SOLR")
      # if there is AMBARI_INFRA, calculate the min/max shards and recommendations based on the number of infra solr hosts
      if infraSolrHosts is not None and len(infraSolrHosts) > 0 and "logsearch-properties" in services["configurations"]:
        replicationReccomendFloat = math.log(len(infraSolrHosts), 5)
        recommendedReplicationFactor = int(1 + math.floor(replicationReccomendFloat))
        
        recommendedMinShards = len(infraSolrHosts)
        recommendedShards = 2 * len(infraSolrHosts)
        recommendedMaxShards = max(3 * len(infraSolrHosts), 5)
      # if there is no AMBARI_INFRA (i.e. external solr is used), use default values for min/max shards and recommendations
      else:
        recommendedReplicationFactor = 2
        
        recommendedMinShards = 1
        recommendedShards = 1
        recommendedMaxShards = 100
        
        putLogSearchCommonEnvProperty('logsearch_use_external_solr', 'true')
        
        # recommend number of shard
        putLogSearchAttribute('logsearch.collection.service.logs.numshards', 'minimum', recommendedMinShards)
        putLogSearchAttribute('logsearch.collection.service.logs.numshards', 'maximum', recommendedMaxShards)
        putLogSearchProperty("logsearch.collection.service.logs.numshards", recommendedShards)
      
        putLogSearchAttribute('logsearch.collection.audit.logs.numshards', 'minimum', recommendedMinShards)
        putLogSearchAttribute('logsearch.collection.audit.logs.numshards', 'maximum', recommendedMaxShards)
        putLogSearchProperty("logsearch.collection.audit.logs.numshards", recommendedShards)
        # recommend replication factor
        putLogSearchProperty("logsearch.collection.service.logs.replication.factor", recommendedReplicationFactor)
        putLogSearchProperty("logsearch.collection.audit.logs.replication.factor", recommendedReplicationFactor)
      
    kerberos_authentication_enabled = self.isSecurityEnabled(services)
    # if there is no kerberos enabled hide kerberor related properties
    if not kerberos_authentication_enabled:
       putLogSearchCommonEnvProperty('logsearch_external_solr_kerberos_enabled', 'false')
       putLogSearchCommonEnvAttribute('logsearch_external_solr_kerberos_enabled', 'visible', 'false')
       putLogSearchEnvAttribute('logsearch_external_solr_kerberos_keytab', 'visible', 'false')
       putLogSearchEnvAttribute('logsearch_external_solr_kerberos_principal', 'visible', 'false')
       putLogFeederEnvAttribute('logfeeder_external_solr_kerberos_keytab', 'visible', 'false')
       putLogFeederEnvAttribute('logfeeder_external_solr_kerberos_principal', 'visible', 'false')

  def getServiceConfigurationsValidationItems(self, configurations, recommendedDefaults, services, hosts):
    """
    Entry point.
    Validate configurations for the service. Return a list of errors.
    The code for this function should be the same for each Service Advisor.
    """
    #Logger.info("Class: %s, Method: %s. Validating Configurations." %
    #            (self.__class__.__name__, inspect.stack()[0][3]))

    return []