@Test public void testDirectionUpgrade() throws Exception { Cluster cluster = clusters.getCluster("c1"); StackEntity stackEntity = stackDAO.find("HDP", "2.1.1"); RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity(); repoVersionEntity.setDisplayName("My New Version 3"); repoVersionEntity.setOperatingSystems(""); repoVersionEntity.setStack(stackEntity); repoVersionEntity.setVersion("2.2.2.3"); repoVersionDao.create(repoVersionEntity); Map<String, Object> requestProps = new HashMap<>(); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.2.2.3"); requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_direction"); requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true"); requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.UPGRADE.name()); ResourceProvider upgradeResourceProvider = createProvider(amc); Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null); upgradeResourceProvider.createResources(request); List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cluster.getClusterId()); assertEquals(1, upgrades.size()); UpgradeEntity upgrade = upgrades.get(0); Long id = upgrade.getRequestId(); assertEquals(3, upgrade.getUpgradeGroups().size()); UpgradeGroupEntity group = upgrade.getUpgradeGroups().get(2); Assert.assertEquals("POST_CLUSTER", group.getName()); Assert.assertTrue(!group.getItems().isEmpty()); for (UpgradeItemEntity item : group.getItems()) { Assert.assertFalse(item.getText().toLowerCase().contains("downgrade")); } abortUpgrade(upgrade.getRequestId()); requestProps.clear(); requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1"); requestProps.put(UpgradeResourceProvider.UPGRADE_VERSION, "2.2"); requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_direction"); requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true"); requestProps.put(UpgradeResourceProvider.UPGRADE_FROM_VERSION, "2.2.2.3"); requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name()); request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null); upgradeResourceProvider.createResources(request); upgrades = upgradeDao.findUpgrades(cluster.getClusterId()); assertEquals(2, upgrades.size()); upgrade = null; for (UpgradeEntity u : upgrades) { if (!u.getRequestId().equals(id)) { upgrade = u; } } assertNotNull(upgrade); assertEquals("Downgrade groups reduced from 3 to 2", 2, upgrade.getUpgradeGroups().size()); group = upgrade.getUpgradeGroups().get(1); assertEquals("Execution items increased from 1 to 2", 2, group.getItems().size()); }
@Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Map<String, Object>> requestMaps = request.getProperties(); if (requestMaps.size() > 1) { throw new SystemException("Can only initiate one upgrade per request."); } final Map<String, Object> requestMap = requestMaps.iterator().next(); final String clusterName = (String) requestMap.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"); } UpgradeEntity entity = createResources(new Command<UpgradeEntity>() { @Override public UpgradeEntity invoke() throws AmbariException, AuthorizationException { final String directionProperty = (String) requestMap.get(UPGRADE_DIRECTION); if (StringUtils.isEmpty(directionProperty)) { throw new AmbariException(String.format("%s is required", UPGRADE_DIRECTION)); } final Direction direction = Direction.valueOf(directionProperty); UpgradeType upgradeType = UpgradeType.ROLLING; if (requestMap.containsKey(UPGRADE_TYPE)) { try { upgradeType = UpgradeType.valueOf(requestMap.get(UPGRADE_TYPE).toString()); } catch (Exception e) { throw new AmbariException(String.format("Property %s has an incorrect value of %s.", UPGRADE_TYPE, requestMap.get(UPGRADE_TYPE))); } } final String version = (String) requestMap.get(UPGRADE_VERSION); final UpgradeContext upgradeContext = s_upgradeContextFactory.create(cluster, upgradeType, direction, version, requestMap); UpgradePack upgradePack = validateRequest(upgradeContext); upgradeContext.setUpgradePack(upgradePack); try { return createUpgrade(upgradeContext); } catch (Exception e) { LOG.error("Error appears during upgrade task submitting", e); clusters.get().invalidate(cluster); throw e; } } }); if (null == entity) { throw new SystemException("Could not load upgrade"); } notifyCreate(Resource.Type.Upgrade, request); Resource res = new ResourceImpl(Resource.Type.Upgrade); res.setProperty(UPGRADE_REQUEST_ID, entity.getRequestId()); return new RequestStatusImpl(null, Collections.singleton(res)); }
UpgradeResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Map<String, Object>> requestMaps = request.getProperties(); if (requestMaps.size() > 1) { throw new SystemException("Can only initiate one upgrade per request."); } final Map<String, Object> requestMap = requestMaps.iterator().next(); final String clusterName = (String) requestMap.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"); } UpgradeEntity entity = createResources(new Command<UpgradeEntity>() { @Override public UpgradeEntity invoke() throws AmbariException, AuthorizationException { final String directionProperty = (String) requestMap.get(UPGRADE_DIRECTION); if (StringUtils.isEmpty(directionProperty)) { throw new AmbariException(String.format("%s is required", UPGRADE_DIRECTION)); } final Direction direction = Direction.valueOf(directionProperty); UpgradeType upgradeType = UpgradeType.ROLLING; if (requestMap.containsKey(UPGRADE_TYPE)) { try { upgradeType = UpgradeType.valueOf(requestMap.get(UPGRADE_TYPE).toString()); } catch (Exception e) { throw new AmbariException(String.format("Property %s has an incorrect value of %s.", UPGRADE_TYPE, requestMap.get(UPGRADE_TYPE))); } } final String version = (String) requestMap.get(UPGRADE_VERSION); final UpgradeContext upgradeContext = s_upgradeContextFactory.create(cluster, upgradeType, direction, version, requestMap); UpgradePack upgradePack = validateRequest(upgradeContext); upgradeContext.setUpgradePack(upgradePack); try { return createUpgrade(upgradeContext); } catch (Exception e) { LOG.error("Error appears during upgrade task submitting", e); clusters.get().invalidate(cluster); throw e; } } }); if (null == entity) { throw new SystemException("Could not load upgrade"); } notifyCreate(Resource.Type.Upgrade, request); Resource res = new ResourceImpl(Resource.Type.Upgrade); res.setProperty(UPGRADE_REQUEST_ID, entity.getRequestId()); return new RequestStatusImpl(null, Collections.singleton(res)); } }
UpgradeResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Map<String, Object>> requestMaps = request.getProperties(); if (requestMaps.size() > 1) { throw new SystemException("Can only initiate one upgrade per request."); } final Map<String, Object> requestMap = requestMaps.iterator().next(); final String clusterName = (String) requestMap.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"); } UpgradeEntity entity = createResources(new Command<UpgradeEntity>() { @Override public UpgradeEntity invoke() throws AmbariException, AuthorizationException { final String directionProperty = (String) requestMap.get(UPGRADE_DIRECTION); if (StringUtils.isEmpty(directionProperty)) { throw new AmbariException(String.format("%s is required", UPGRADE_DIRECTION)); } final Direction direction = Direction.valueOf(directionProperty); UpgradeType upgradeType = UpgradeType.ROLLING; if (requestMap.containsKey(UPGRADE_TYPE)) { try { upgradeType = UpgradeType.valueOf(requestMap.get(UPGRADE_TYPE).toString()); } catch (Exception e) { throw new AmbariException(String.format("Property %s has an incorrect value of %s.", UPGRADE_TYPE, requestMap.get(UPGRADE_TYPE))); } } final String version = (String) requestMap.get(UPGRADE_VERSION); final UpgradeContext upgradeContext = s_upgradeContextFactory.create(cluster, upgradeType, direction, version, requestMap); UpgradePack upgradePack = validateRequest(upgradeContext); upgradeContext.setUpgradePack(upgradePack); try { return createUpgrade(upgradeContext); } catch (Exception e) { LOG.error("Error appears during upgrade task submitting", e); clusters.get().invalidate(cluster); throw e; } } }); if (null == entity) { throw new SystemException("Could not load upgrade"); } notifyCreate(Resource.Type.Upgrade, request); Resource res = new ResourceImpl(Resource.Type.Upgrade); res.setProperty(UPGRADE_REQUEST_ID, entity.getRequestId()); return new RequestStatusImpl(null, Collections.singleton(res)); } @Inject  UpgradeResourceProvider(@Assisted AmbariManagementController controller); }
UpgradeResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Map<String, Object>> requestMaps = request.getProperties(); if (requestMaps.size() > 1) { throw new SystemException("Can only initiate one upgrade per request."); } final Map<String, Object> requestMap = requestMaps.iterator().next(); final String clusterName = (String) requestMap.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"); } UpgradeEntity entity = createResources(new Command<UpgradeEntity>() { @Override public UpgradeEntity invoke() throws AmbariException, AuthorizationException { final String directionProperty = (String) requestMap.get(UPGRADE_DIRECTION); if (StringUtils.isEmpty(directionProperty)) { throw new AmbariException(String.format("%s is required", UPGRADE_DIRECTION)); } final Direction direction = Direction.valueOf(directionProperty); UpgradeType upgradeType = UpgradeType.ROLLING; if (requestMap.containsKey(UPGRADE_TYPE)) { try { upgradeType = UpgradeType.valueOf(requestMap.get(UPGRADE_TYPE).toString()); } catch (Exception e) { throw new AmbariException(String.format("Property %s has an incorrect value of %s.", UPGRADE_TYPE, requestMap.get(UPGRADE_TYPE))); } } final String version = (String) requestMap.get(UPGRADE_VERSION); final UpgradeContext upgradeContext = s_upgradeContextFactory.create(cluster, upgradeType, direction, version, requestMap); UpgradePack upgradePack = validateRequest(upgradeContext); upgradeContext.setUpgradePack(upgradePack); try { return createUpgrade(upgradeContext); } catch (Exception e) { LOG.error("Error appears during upgrade task submitting", e); clusters.get().invalidate(cluster); throw e; } } }); if (null == entity) { throw new SystemException("Could not load upgrade"); } notifyCreate(Resource.Type.Upgrade, request); Resource res = new ResourceImpl(Resource.Type.Upgrade); res.setProperty(UPGRADE_REQUEST_ID, entity.getRequestId()); return new RequestStatusImpl(null, Collections.singleton(res)); } @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); }
UpgradeResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus createResources(final Request request) throws SystemException, UnsupportedPropertyException, ResourceAlreadyExistsException, NoSuchParentResourceException { Set<Map<String, Object>> requestMaps = request.getProperties(); if (requestMaps.size() > 1) { throw new SystemException("Can only initiate one upgrade per request."); } final Map<String, Object> requestMap = requestMaps.iterator().next(); final String clusterName = (String) requestMap.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"); } UpgradeEntity entity = createResources(new Command<UpgradeEntity>() { @Override public UpgradeEntity invoke() throws AmbariException, AuthorizationException { final String directionProperty = (String) requestMap.get(UPGRADE_DIRECTION); if (StringUtils.isEmpty(directionProperty)) { throw new AmbariException(String.format("%s is required", UPGRADE_DIRECTION)); } final Direction direction = Direction.valueOf(directionProperty); UpgradeType upgradeType = UpgradeType.ROLLING; if (requestMap.containsKey(UPGRADE_TYPE)) { try { upgradeType = UpgradeType.valueOf(requestMap.get(UPGRADE_TYPE).toString()); } catch (Exception e) { throw new AmbariException(String.format("Property %s has an incorrect value of %s.", UPGRADE_TYPE, requestMap.get(UPGRADE_TYPE))); } } final String version = (String) requestMap.get(UPGRADE_VERSION); final UpgradeContext upgradeContext = s_upgradeContextFactory.create(cluster, upgradeType, direction, version, requestMap); UpgradePack upgradePack = validateRequest(upgradeContext); upgradeContext.setUpgradePack(upgradePack); try { return createUpgrade(upgradeContext); } catch (Exception e) { LOG.error("Error appears during upgrade task submitting", e); clusters.get().invalidate(cluster); throw e; } } }); if (null == entity) { throw new SystemException("Could not load upgrade"); } notifyCreate(Resource.Type.Upgrade, request); Resource res = new ResourceImpl(Resource.Type.Upgrade); res.setProperty(UPGRADE_REQUEST_ID, entity.getRequestId()); return new RequestStatusImpl(null, Collections.singleton(res)); } @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; }