summaryrefslogtreecommitdiff
path: root/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog222.java
blob: 88b3151fbc4572b3dc4fa690a433ef186d77ce3c (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * 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.upgrade;

import com.google.inject.Inject;
import com.google.inject.Injector;
import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.controller.AmbariManagementController;
import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
import org.apache.ambari.server.orm.dao.DaoUtils;
import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Upgrade catalog for version 2.2.2.
 */
public class UpgradeCatalog222 extends AbstractUpgradeCatalog {

  @Inject
  DaoUtils daoUtils;

  /**
   * Logger.
   */
  private static final Logger LOG = LoggerFactory.getLogger(UpgradeCatalog222.class);
  private static final String AMS_SITE = "ams-site";
  private static final String HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER =
    "timeline.metrics.host.aggregator.daily.checkpointCutOffMultiplier";
  private static final String CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER =
    "timeline.metrics.cluster.aggregator.daily.checkpointCutOffMultiplier";
  private static final String TIMELINE_METRICS_SERVICE_WATCHER_DISBALED = "timeline.metrics.service.watcher.disabled";
  private static final String AMS_MODE = "timeline.metrics.service.operation.mode";
  public static final String PRECISION_TABLE_TTL = "timeline.metrics.host.aggregator.ttl";
  public static final String HOST_MINUTE_TABLE_TTL = "timeline.metrics.host.aggregator.minute.ttl";
  public static final String HOST_HOUR_TABLE_TTL = "timeline.metrics.host.aggregator.hourly.ttl";
  public static final String HOST_DAILY_TABLE_TTL = "timeline.metrics.host.aggregator.daily.ttl";
  public static final String CLUSTER_SECOND_TABLE_TTL = "timeline.metrics.cluster.aggregator.second.ttl";
  public static final String CLUSTER_MINUTE_TABLE_TTL = "timeline.metrics.cluster.aggregator.minute.ttl";
  public static final String CLUSTER_HOUR_TABLE_TTL = "timeline.metrics.cluster.aggregator.hourly.ttl";
  public static final String CLUSTER_DAILY_TABLE_TTL = "timeline.metrics.cluster.aggregator.daily.ttl";


  // ----- Constructors ------------------------------------------------------

  /**
   * Don't forget to register new UpgradeCatalogs in {@link org.apache.ambari.server.upgrade.SchemaUpgradeHelper.UpgradeHelperModule#configure()}
   *
   * @param injector Guice injector to track dependencies and uses bindings to inject them.
   */
  @Inject
  public UpgradeCatalog222(Injector injector) {
    super(injector);
    this.injector = injector;
  }

  // ----- UpgradeCatalog ----------------------------------------------------

  /**
   * {@inheritDoc}
   */
  @Override
  public String getTargetVersion() {
    return "2.2.2";
  }

  // ----- AbstractUpgradeCatalog --------------------------------------------

  /**
   * {@inheritDoc}
   */
  @Override
  public String getSourceVersion() {
    return "2.2.1";
  }


  @Override
  protected void executeDDLUpdates() throws AmbariException, SQLException {
    //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  protected void executePreDMLUpdates() throws AmbariException, SQLException {
    //To change body of implemented methods use File | Settings | File Templates.
  }

  @Override
  protected void executeDMLUpdates() throws AmbariException, SQLException {
    addNewConfigurationsFromXml();
    updateAlerts();
    updateStormConfigs();
    updateAMSConfigs();
  }

  protected void updateStormConfigs() throws  AmbariException {
    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
    Map<String, Cluster> clusterMap = getCheckedClusterMap(ambariManagementController.getClusters());

    for (final Cluster cluster : clusterMap.values()) {
      if (cluster.getDesiredConfigByType("storm-site") != null && cluster.getDesiredConfigByType("storm-site").getProperties().containsKey("storm.zookeeper.superACL")
              && cluster.getDesiredConfigByType("storm-site").getProperties().get("storm.zookeeper.superACL").equals("sasl:{{storm_base_jaas_principal}}")) {
        Map<String, String> newStormProps = new HashMap<String, String>();
        newStormProps.put("storm.zookeeper.superACL", "sasl:{{storm_bare_jaas_principal}}");
        updateConfigurationPropertiesForCluster(cluster, "storm-site", newStormProps, true, false);
      }
    }
  }

  protected void updateAlerts() {
    LOG.info("Updating alert definitions.");
    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
    AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
    Clusters clusters = ambariManagementController.getClusters();

    Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters);
    for (final Cluster cluster : clusterMap.values()) {
      long clusterID = cluster.getClusterId();

      final AlertDefinitionEntity regionserverHealthSummaryDefinitionEntity = alertDefinitionDAO.findByName(
              clusterID, "regionservers_health_summary");

      if (regionserverHealthSummaryDefinitionEntity != null) {
        alertDefinitionDAO.remove(regionserverHealthSummaryDefinitionEntity);
      }

    }


  }

  protected void updateAMSConfigs() throws AmbariException {
    AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
    Clusters clusters = ambariManagementController.getClusters();

    if (clusters != null) {
      Map<String, Cluster> clusterMap = clusters.getClusters();

      if (clusterMap != null && !clusterMap.isEmpty()) {
        for (final Cluster cluster : clusterMap.values()) {

          Config amsSite = cluster.getDesiredConfigByType(AMS_SITE);
          if (amsSite != null) {
            Map<String, String> amsSiteProperties = amsSite.getProperties();
            Map<String, String> newProperties = new HashMap<>();

            if (amsSiteProperties.containsKey(HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER) &&
              amsSiteProperties.get(HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER).equals("1")) {

              LOG.info("Setting value of " + HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER + " : 2");
              newProperties.put(HOST_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER, String.valueOf(2));

            }

            if (amsSiteProperties.containsKey(CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER) &&
              amsSiteProperties.get(CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER).equals("1")) {

              LOG.info("Setting value of " + CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER + " : 2");
              newProperties.put(CLUSTER_AGGREGATOR_DAILY_CHECKPOINTCUTOFFMULTIPIER, String.valueOf(2));

            }

            if (!amsSiteProperties.containsKey(TIMELINE_METRICS_SERVICE_WATCHER_DISBALED)) {
              LOG.info("Add config  " + TIMELINE_METRICS_SERVICE_WATCHER_DISBALED + " = false");
              newProperties.put(TIMELINE_METRICS_SERVICE_WATCHER_DISBALED, String.valueOf(false));
            }

            boolean isDistributed = false;
            if ("distributed".equals(amsSite.getProperties().get(AMS_MODE))) {
              isDistributed = true;
            }

            if (amsSiteProperties.containsKey(PRECISION_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(PRECISION_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);
              if (isDistributed) {
                if ("86400".equals(oldTtl)) {
                  newTtl = "7.0"; // 7 days
                }
              }
              newProperties.put(PRECISION_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + PRECISION_TABLE_TTL + " : " + newTtl);
            }

            if (amsSiteProperties.containsKey(HOST_MINUTE_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(HOST_MINUTE_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);
              newProperties.put(HOST_MINUTE_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + HOST_MINUTE_TABLE_TTL + " : " + newTtl);
            }

            if (amsSiteProperties.containsKey(HOST_MINUTE_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(HOST_MINUTE_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);
              newProperties.put(HOST_MINUTE_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + HOST_MINUTE_TABLE_TTL + " : " + newTtl);
            }

            if (amsSiteProperties.containsKey(HOST_HOUR_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(HOST_HOUR_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);
              newProperties.put(HOST_HOUR_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + HOST_HOUR_TABLE_TTL + " : " + newTtl);
            }

            if (amsSiteProperties.containsKey(HOST_DAILY_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(HOST_DAILY_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);
              newProperties.put(HOST_DAILY_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + HOST_DAILY_TABLE_TTL + " : " + newTtl);
            }

            if (amsSiteProperties.containsKey(CLUSTER_SECOND_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(CLUSTER_SECOND_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);

              if ("2592000".equals(oldTtl)) {
                newTtl = "7.0"; // 7 days
              }

              newProperties.put(CLUSTER_SECOND_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + CLUSTER_SECOND_TABLE_TTL + " : " + newTtl);
            }

            if (amsSiteProperties.containsKey(CLUSTER_MINUTE_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(CLUSTER_MINUTE_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);

              if ("7776000".equals(oldTtl)) {
                newTtl = "30.0"; // 30 days
              }

              newProperties.put(CLUSTER_MINUTE_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + CLUSTER_MINUTE_TABLE_TTL + " : " + newTtl);
            }

            if (amsSiteProperties.containsKey(CLUSTER_HOUR_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(CLUSTER_HOUR_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);
              newProperties.put(CLUSTER_HOUR_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + CLUSTER_HOUR_TABLE_TTL + " : " + newTtl);
            }

            if (amsSiteProperties.containsKey(CLUSTER_DAILY_TABLE_TTL)) {
              String oldTtl = amsSiteProperties.get(CLUSTER_DAILY_TABLE_TTL);
              String newTtl = convertToDaysIfInSeconds(oldTtl);
              newProperties.put(CLUSTER_DAILY_TABLE_TTL, newTtl);
              LOG.info("Setting value of " + CLUSTER_DAILY_TABLE_TTL + " : " + newTtl);
            }

            updateConfigurationPropertiesForCluster(cluster, AMS_SITE, newProperties, true, true);
          }

        }
      }
    }
  }

  private String convertToDaysIfInSeconds(String secondsString) {

    int seconds = Integer.valueOf(secondsString);
    double days = 0.0;

    if (seconds >= 86400) {
      days += TimeUnit.SECONDS.toDays(seconds);
    }

    days += ((float)seconds % 86400.0) / 86400.0;
    days = Math.round(days * 100.0)/100.0;

    return String.valueOf(days);
  }

}