UpgradeResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus updateResources(final Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<Map<String, Object>> requestMaps = request.getProperties(); if (requestMaps.size() > 1) { throw new SystemException("Can only update one upgrade per request."); } final Map<String, Object> propertyMap = requestMaps.iterator().next(); final String clusterName = (String) propertyMap.get(UPGRADE_CLUSTER_NAME); final Cluster cluster; try { cluster = getManagementController().getClusters().getCluster(clusterName); } catch (AmbariException e) { throw new NoSuchParentResourceException( String.format("Cluster %s could not be loaded", clusterName)); } if (!AuthorizationHelper.isAuthorized(ResourceType.CLUSTER, cluster.getResourceId(), EnumSet.of(RoleAuthorization.CLUSTER_UPGRADE_DOWNGRADE_STACK))) { throw new AuthorizationException("The authenticated user does not have authorization to " + "manage upgrade and downgrade"); } String requestIdProperty = (String) propertyMap.get(UPGRADE_REQUEST_ID); if (null == requestIdProperty) { throw new IllegalArgumentException(String.format("%s is required", UPGRADE_REQUEST_ID)); } long clusterId = cluster.getClusterId(); long requestId = Long.parseLong(requestIdProperty); UpgradeEntity upgradeEntity = s_upgradeDAO.findUpgradeByRequestId(requestId); if( null == upgradeEntity){ String exceptionMessage = MessageFormat.format("The upgrade with request ID {0} was not found", requestIdProperty); throw new NoSuchParentResourceException(exceptionMessage); } List<String> updatableProperties = Lists.newArrayList(UPGRADE_REQUEST_STATUS, UPGRADE_SKIP_FAILURES, UPGRADE_SKIP_SC_FAILURES); boolean isRequiredPropertyInRequest = CollectionUtils.containsAny(updatableProperties, propertyMap.keySet()); if (!isRequiredPropertyInRequest) { String exceptionMessage = MessageFormat.format( "At least one of the following properties is required in the request: {0}", StringUtils.join(updatableProperties, ", ")); throw new IllegalArgumentException(exceptionMessage); } String requestStatus = (String) propertyMap.get(UPGRADE_REQUEST_STATUS); String skipFailuresRequestProperty = (String) propertyMap.get(UPGRADE_SKIP_FAILURES); String skipServiceCheckFailuresRequestProperty = (String) propertyMap.get(UPGRADE_SKIP_SC_FAILURES); if (null != requestStatus) { HostRoleStatus status = HostRoleStatus.valueOf(requestStatus); boolean suspended = false; if (status == HostRoleStatus.ABORTED && !propertyMap.containsKey(UPGRADE_SUSPENDED)){ throw new IllegalArgumentException(String.format( "When changing the state of an upgrade to %s, the %s property is required to be either true or false.", status, UPGRADE_SUSPENDED )); } else if (status == HostRoleStatus.ABORTED) { suspended = Boolean.valueOf((String) propertyMap.get(UPGRADE_SUSPENDED)); } setUpgradeRequestStatus(clusterId, requestId, status, propertyMap); upgradeEntity.setSuspended(suspended); s_upgradeDAO.merge(upgradeEntity); } if (StringUtils.isNotEmpty(skipFailuresRequestProperty) || StringUtils.isNotEmpty(skipServiceCheckFailuresRequestProperty)) { boolean skipFailures = upgradeEntity.isComponentFailureAutoSkipped(); boolean skipServiceCheckFailures = upgradeEntity.isServiceCheckFailureAutoSkipped(); if (null != skipFailuresRequestProperty) { skipFailures = Boolean.parseBoolean(skipFailuresRequestProperty); } if (null != skipServiceCheckFailuresRequestProperty) { skipServiceCheckFailures = Boolean.parseBoolean(skipServiceCheckFailuresRequestProperty); } s_hostRoleCommandDAO.updateAutomaticSkipOnFailure(requestId, skipFailures, skipServiceCheckFailures); upgradeEntity.setAutoSkipComponentFailures(skipFailures); upgradeEntity.setAutoSkipServiceCheckFailures(skipServiceCheckFailures); s_upgradeDAO.merge(upgradeEntity); } return getRequestStatus(null); } @Inject  UpgradeResourceProvider(@Assisted AmbariManagementController controller); @Override RequestStatus createResources(final Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(final Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); void applyStackAndProcessConfigurations(String stackName, Cluster cluster, String version, Direction direction, UpgradePack upgradePack, String userName); static final String UPGRADE_CLUSTER_NAME; static final String UPGRADE_VERSION; static final String UPGRADE_TYPE; static final String UPGRADE_PACK; static final String UPGRADE_REQUEST_ID; static final String UPGRADE_FROM_VERSION; static final String UPGRADE_TO_VERSION; static final String UPGRADE_DIRECTION; static final String UPGRADE_DOWNGRADE_ALLOWED; static final String UPGRADE_REQUEST_STATUS; static final String UPGRADE_SUSPENDED; static final String UPGRADE_ABORT_REASON; static final String UPGRADE_SKIP_PREREQUISITE_CHECKS; static final String UPGRADE_FAIL_ON_CHECK_WARNINGS; static final String CONST_UPGRADE_GROUP_NAME; static final String CONST_UPGRADE_ITEM_TEXT; static final String CONST_CUSTOM_COMMAND_NAME; }
@Test public void testAbort() throws Exception { RequestStatus status = testCreateResources(); Set<Resource> createdResources = status.getAssociatedResources(); assertEquals(1, createdResources.size()); Resource res = createdResources.iterator().next(); Long id = (Long) res.getPropertyValue("Upgrade/request_id"); assertNotNull(id); assertEquals(Long.valueOf(1), id); Map<String, Object> requestProps = new HashMap<>(); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, id.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_STATUS, "ABORTED"); requestProps.put(UpgradeResourceProvider.UPGRADE_SUSPENDED, "true"); UpgradeResourceProvider urp = createProvider(amc); Request req = PropertyHelper.getUpdateRequest(requestProps, null); urp.updateResources(req, null); }
@Test public void testRetry() throws Exception { RequestStatus status = testCreateResources(); Set<Resource> createdResources = status.getAssociatedResources(); assertEquals(1, createdResources.size()); Resource res = createdResources.iterator().next(); Long id = (Long) res.getPropertyValue("Upgrade/request_id"); assertNotNull(id); assertEquals(Long.valueOf(1), id); Map<String, Object> requestProps = new HashMap<>(); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, id.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_STATUS, "ABORTED"); requestProps.put(UpgradeResourceProvider.UPGRADE_SUSPENDED, "true"); UpgradeResourceProvider urp = createProvider(amc); Request req = PropertyHelper.getUpdateRequest(requestProps, null); urp.updateResources(req, null); ActionManager am = injector.getInstance(ActionManager.class); List<HostRoleCommand> commands = am.getRequestTasks(id); boolean foundOne = false; for (HostRoleCommand hrc : commands) { if (hrc.getRole().equals(Role.AMBARI_SERVER_ACTION)) { assertEquals(-1L, hrc.getHostId()); assertNull(hrc.getHostName()); foundOne = true; } } assertTrue("Expected at least one server-side action", foundOne); HostRoleCommand cmd = commands.get(commands.size()-1); HostRoleCommandDAO dao = injector.getInstance(HostRoleCommandDAO.class); HostRoleCommandEntity entity = dao.findByPK(cmd.getTaskId()); entity.setStatus(HostRoleStatus.ABORTED); dao.merge(entity); requestProps = new HashMap<>(); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, id.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_STATUS, "PENDING"); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_SUSPENDED, "false"); req = PropertyHelper.getUpdateRequest(requestProps, null); urp.updateResources(req, null); }
@Test(expected = IllegalArgumentException.class) public void testAbortWithoutSuspendFlag() throws Exception { RequestStatus status = testCreateResources(); Set<Resource> createdResources = status.getAssociatedResources(); assertEquals(1, createdResources.size()); Resource res = createdResources.iterator().next(); Long id = (Long) res.getPropertyValue("Upgrade/request_id"); assertNotNull(id); assertEquals(Long.valueOf(1), id); Map<String, Object> requestProps = new HashMap<>(); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, id.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_STATUS, "ABORTED"); UpgradeResourceProvider urp = createProvider(amc); Request req = PropertyHelper.getUpdateRequest(requestProps, null); urp.updateResources(req, null); }
@Test public void testUpdateSkipFailures() throws Exception { testCreateResourcesWithAutoSkipFailures(); List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(1); assertEquals(1, upgrades.size()); UpgradeEntity entity = upgrades.get(0); HostRoleCommandDAO dao = injector.getInstance(HostRoleCommandDAO.class); List<HostRoleCommandEntity> tasks = dao.findByRequest(entity.getRequestId()); for (HostRoleCommandEntity task : tasks) { StageEntity stage = task.getStage(); if (stage.isSkippable() && stage.isAutoSkipOnFailureSupported()) { assertTrue(task.isFailureAutoSkipped()); } else { assertFalse(task.isFailureAutoSkipped()); } } Map<String, Object> requestProps = new HashMap<>(); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.2.0.0"); requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_FAILURES, Boolean.TRUE.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_SC_FAILURES, Boolean.FALSE.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, "" + entity.getRequestId()); ResourceProvider upgradeResourceProvider = createProvider(amc); Request request = PropertyHelper.getUpdateRequest(requestProps, null); upgradeResourceProvider.updateResources(request, null); tasks = dao.findByRequest(entity.getRequestId()); for (HostRoleCommandEntity task : tasks) { if (task.getRoleCommand() == RoleCommand.SERVICE_CHECK) { assertFalse(task.isFailureAutoSkipped()); } else { StageEntity stage = task.getStage(); if (stage.isSkippable() && stage.isAutoSkipOnFailureSupported()) { assertTrue(task.isFailureAutoSkipped()); } else { assertFalse(task.isFailureAutoSkipped()); } } } requestProps = new HashMap<>(); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.2.0.0"); requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_FAILURES, Boolean.FALSE.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_SC_FAILURES, Boolean.TRUE.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, "" + entity.getRequestId()); request = PropertyHelper.getUpdateRequest(requestProps, null); upgradeResourceProvider.updateResources(request, null); tasks = dao.findByRequest(entity.getRequestId()); for (HostRoleCommandEntity task : tasks) { if (task.getRoleCommand() == RoleCommand.SERVICE_CHECK) { StageEntity stage = task.getStage(); if (stage.isSkippable() && stage.isAutoSkipOnFailureSupported()) { assertTrue(task.isFailureAutoSkipped()); } else { assertFalse(task.isFailureAutoSkipped()); } } else { assertFalse(task.isFailureAutoSkipped()); } } requestProps = new HashMap<>(); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.2.0.0"); requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_FAILURES, Boolean.FALSE.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_SC_FAILURES, Boolean.FALSE.toString()); requestProps.put(UpgradeResourceProvider.UPGRADE_REQUEST_ID, "" + entity.getRequestId()); request = PropertyHelper.getUpdateRequest(requestProps, null); upgradeResourceProvider.updateResources(request, null); tasks = dao.findByRequest(entity.getRequestId()); for (HostRoleCommandEntity task : tasks) { assertFalse(task.isFailureAutoSkipped()); } }