UpgradeCatalog250 extends AbstractUpgradeCatalog { protected void fixHBaseMasterCPUUtilizationAlertDefinition() { AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class); AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = ambariManagementController.getClusters(); Map<String, Cluster> clusterMap = getCheckedClusterMap(clusters); for (final Cluster cluster : clusterMap.values()) { long clusterID = cluster.getClusterId(); AlertDefinitionEntity alertDefinition = alertDefinitionDAO.findByName(clusterID, "hbase_master_cpu"); if(alertDefinition != null) { LOG.info("Updating alert definition {} in cluster {}", alertDefinition.getDefinitionName(), clusterID); String source = alertDefinition.getSource(); if(source != null) { JsonObject sourceJson = new JsonParser().parse(source).getAsJsonObject(); if(sourceJson != null) { boolean changesExist = false; LOG.debug("Source before update : {}", sourceJson); JsonObject uriJson = sourceJson.get("uri").getAsJsonObject(); JsonPrimitive primitive; if (uriJson != null) { primitive = uriJson.getAsJsonPrimitive("kerberos_keytab"); if ((primitive != null) && primitive.isString() && "{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}".equals(primitive.getAsString())) { uriJson.remove("kerberos_keytab"); uriJson.addProperty("kerberos_keytab", "{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}"); changesExist = true; } primitive = uriJson.getAsJsonPrimitive("kerberos_principal"); if ((primitive != null) && primitive.isString() && "{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}".equals(primitive.getAsString())) { uriJson.remove("kerberos_principal"); uriJson.addProperty("kerberos_principal", "{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}"); changesExist = true; } } LOG.debug("Source after update : {}", sourceJson); if(changesExist) { alertDefinition.setSource(sourceJson.toString()); alertDefinition.setHash(UUID.randomUUID().toString()); alertDefinitionDAO.merge(alertDefinition); } } } } } } @Inject  UpgradeCatalog250(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion(); static final String COMPONENT_TABLE; static final String COMPONENT_VERSION_TABLE; static final String COMPONENT_VERSION_PK; static final String COMPONENT_VERSION_FK_COMPONENT; static final String COMPONENT_VERSION_FK_REPO_VERSION; }
@Test public void testFixHBaseMasterCPUUtilizationAlertDefinition() { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class); final AlertDefinitionEntity hbaseMasterCPUAlertMock = easyMockSupport.createNiceMock(AlertDefinitionEntity.class); String brokenSource = "{\"uri\":{\"http\":\"{{hbase-site/hbase.master.info.port}}\",\"kerberos_keytab\":\"{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}\",\"kerberos_principal\":\"{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}\",\"default_port\":60010,\"connection_timeout\":5.0},\"jmx\":{\"property_list\":[\"java.lang:type\\u003dOperatingSystem/SystemCpuLoad\",\"java.lang:type\\u003dOperatingSystem/AvailableProcessors\"],\"value\":\"{0} * 100\"},\"type\":\"METRIC\",\"reporting\":{\"ok\":{\"text\":\"{1} CPU, load {0:.1%}\"},\"warning\":{\"text\":\"{1} CPU, load {0:.1%}\",\"value\":200.0},\"critical\":{\"text\":\"{1} CPU, load {0:.1%}\",\"value\":250.0},\"units\":\"%\",\"type\":\"PERCENT\"}}"; Capture<String> capturedFixedSource = newCapture(); final Injector mockInjector = createInjector(mockAmbariManagementController, mockClusters, mockAlertDefinitionDAO); long clusterId = 1; expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(Collections.singletonMap("normal", mockClusterExpected)).atLeastOnce(); expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes(); expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("hbase_master_cpu"))).andReturn(hbaseMasterCPUAlertMock).atLeastOnce(); expect(hbaseMasterCPUAlertMock.getDefinitionName()).andReturn("hbase_master_cpu").once(); expect(hbaseMasterCPUAlertMock.getSource()).andReturn(brokenSource).once(); hbaseMasterCPUAlertMock.setSource(capture(capturedFixedSource)); expectLastCall().once(); hbaseMasterCPUAlertMock.setHash(anyString()); expectLastCall().once(); expect(mockAlertDefinitionDAO.merge(hbaseMasterCPUAlertMock)).andReturn(hbaseMasterCPUAlertMock).once(); easyMockSupport.replayAll(); mockInjector.getInstance(UpgradeCatalog250.class).fixHBaseMasterCPUUtilizationAlertDefinition(); easyMockSupport.verifyAll(); String fixedSource = capturedFixedSource.getValue(); Assert.assertNotNull(fixedSource); JsonObject sourceJson = new JsonParser().parse(fixedSource).getAsJsonObject(); Assert.assertNotNull(sourceJson); JsonObject uriJson = sourceJson.get("uri").getAsJsonObject(); Assert.assertNotNull(uriJson); JsonPrimitive primitive; primitive = uriJson.getAsJsonPrimitive("kerberos_keytab"); Assert.assertTrue(primitive.isString()); Assert.assertEquals("{{hbase-site/hbase.security.authentication.spnego.kerberos.keytab}}", primitive.getAsString()); primitive = uriJson.getAsJsonPrimitive("kerberos_principal"); Assert.assertTrue(primitive.isString()); Assert.assertEquals("{{hbase-site/hbase.security.authentication.spnego.kerberos.principal}}", primitive.getAsString()); }
@Test public void testFixHBaseMasterCPUUtilizationAlertDefinitionMissingKerberosInfo() { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class); final AlertDefinitionEntity hbaseMasterCPUAlertMock = easyMockSupport.createNiceMock(AlertDefinitionEntity.class); String brokenSource = "{\"uri\":{\"http\":\"{{hbase-site/hbase.master.info.port}}\",\"default_port\":60010,\"connection_timeout\":5.0},\"jmx\":{\"property_list\":[\"java.lang:type\\u003dOperatingSystem/SystemCpuLoad\",\"java.lang:type\\u003dOperatingSystem/AvailableProcessors\"],\"value\":\"{0} * 100\"},\"type\":\"METRIC\",\"reporting\":{\"ok\":{\"text\":\"{1} CPU, load {0:.1%}\"},\"warning\":{\"text\":\"{1} CPU, load {0:.1%}\",\"value\":200.0},\"critical\":{\"text\":\"{1} CPU, load {0:.1%}\",\"value\":250.0},\"units\":\"%\",\"type\":\"PERCENT\"}}"; Capture<String> capturedFixedSource = newCapture(); final Injector mockInjector = createInjector(mockAmbariManagementController, mockClusters, mockAlertDefinitionDAO); long clusterId = 1; expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(Collections.singletonMap("normal", mockClusterExpected)).atLeastOnce(); expect(mockClusterExpected.getClusterId()).andReturn(clusterId).anyTimes(); expect(mockAlertDefinitionDAO.findByName(eq(clusterId), eq("hbase_master_cpu"))).andReturn(hbaseMasterCPUAlertMock).atLeastOnce(); expect(hbaseMasterCPUAlertMock.getDefinitionName()).andReturn("hbase_master_cpu").once(); expect(hbaseMasterCPUAlertMock.getSource()).andReturn(brokenSource).once(); expect(mockAlertDefinitionDAO.merge(hbaseMasterCPUAlertMock)).andReturn(hbaseMasterCPUAlertMock).anyTimes(); easyMockSupport.replayAll(); mockInjector.getInstance(UpgradeCatalog250.class).fixHBaseMasterCPUUtilizationAlertDefinition(); easyMockSupport.verifyAll(); Assert.assertFalse(capturedFixedSource.hasCaptured()); }