RequestResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus updateResources(Request requestInfo, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { AmbariManagementController amc = getManagementController(); final Set<RequestRequest> requests = new HashSet<RequestRequest>(); Iterator<Map<String,Object>> iterator = requestInfo.getProperties().iterator(); if (iterator.hasNext()) { for (Map<String, Object> propertyMap : getPropertyMaps(iterator.next(), predicate)) { requests.add(getRequest(propertyMap)); } } List<org.apache.ambari.server.actionmanager.Request> targets = new ArrayList<org.apache.ambari.server.actionmanager.Request>(); for (RequestRequest updateRequest : requests) { ActionManager actionManager = amc.getActionManager(); List<org.apache.ambari.server.actionmanager.Request> internalRequests = actionManager.getRequests(Collections.singletonList(updateRequest.getRequestId())); if (internalRequests.size() == 0) { throw new IllegalArgumentException( String.format("Request %s does not exist", updateRequest.getRequestId())); } org.apache.ambari.server.actionmanager.Request internalRequest = internalRequests.get(0); if (updateRequest.getAbortReason() == null || updateRequest.getAbortReason().isEmpty()) { throw new IllegalArgumentException("Abort reason can not be empty."); } if (updateRequest.getStatus() != HostRoleStatus.ABORTED) { throw new IllegalArgumentException( String.format("%s is wrong value. The only allowed value " + "for updating request status is ABORTED", updateRequest.getStatus())); } HostRoleStatus internalRequestStatus = CalculatedStatus.statusFromStages(internalRequest.getStages()).getStatus(); if (internalRequestStatus.isCompletedState()) { } else { targets.add(internalRequest); } } Iterator<RequestRequest> reqIterator = requests.iterator(); for (org.apache.ambari.server.actionmanager.Request target : targets) { String reason = reqIterator.next().getAbortReason(); amc.getActionManager().cancelRequest(target.getRequestId(), reason); } return getRequestStatus(null); } RequestResourceProvider(Set<String> propertyIds,
                          Map<Resource.Type, String> keyPropertyIds,
                          AmbariManagementController managementController); @Override RequestStatus createResources(Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request requestInfo, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static final String REQUEST_CLUSTER_NAME_PROPERTY_ID; static final String REQUEST_CLUSTER_ID_PROPERTY_ID; static final String REQUEST_ID_PROPERTY_ID; static final String REQUEST_SOURCE_SCHEDULE; static final String REQUEST_SOURCE_SCHEDULE_ID; static final String REQUEST_SOURCE_SCHEDULE_HREF; }
@Test public void testUpdateResources_CancelRequest() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); HostRoleCommand hostRoleCommand = createNiceMock(HostRoleCommand.class); Stage stage = createNiceMock(Stage.class); Clusters clusters = createNiceMock(Clusters.class); List<HostRoleCommand> hostRoleCommands = new LinkedList<HostRoleCommand>(); hostRoleCommands.add(hostRoleCommand); Collection<Stage> stages = new HashSet<Stage>(); stages.add(stage); org.apache.ambari.server.actionmanager.Request requestMock = createNiceMock(org.apache.ambari.server.actionmanager.Request.class); expect(requestMock.getCommands()).andReturn(hostRoleCommands).anyTimes(); expect(requestMock.getStages()).andReturn(stages).anyTimes(); expect(stage.getOrderedHostRoleCommands()).andReturn(hostRoleCommands).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(actionManager.getRequests(capture(requestIdsCapture))). andReturn(Collections.singletonList(requestMock)).anyTimes(); expect(hostRoleCommand.getStatus()).andReturn(HostRoleStatus.IN_PROGRESS).anyTimes(); RequestStatusResponse response = createNiceMock(RequestStatusResponse.class); replay(managementController, actionManager, hostRoleCommand, clusters, requestMock, response, stage); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Map<String, Object> properties = new LinkedHashMap<String, Object>(); Request request = PropertyHelper.getUpdateRequest(properties, null); Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID). equals("100").toPredicate(); try { provider.updateResources(request, predicate); Assert.fail("Expected an java.lang.IllegalArgumentException: Abort reason can not be empty."); } catch (IllegalArgumentException e) { } properties.put(RequestResourceProvider.REQUEST_ABORT_REASON_PROPERTY_ID, "Some reason"); request = PropertyHelper.getUpdateRequest(properties, null); try { provider.updateResources(request, predicate); Assert.fail("Expected an java.lang.IllegalArgumentException: null is wrong value."); } catch (IllegalArgumentException e) { } properties.put(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID, "COMPLETED"); request = PropertyHelper.getUpdateRequest(properties, null); try { provider.updateResources(request, predicate); Assert.fail("Expected an java.lang.IllegalArgumentException: COMPLETED is wrong value. " + "The only allowed value for updating request status is ABORTED"); } catch (IllegalArgumentException e) { } properties.put(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID, "ABORTED"); for (HostRoleStatus status : HostRoleStatus.values()) { reset(hostRoleCommand); expect(hostRoleCommand.getStatus()).andReturn(status).anyTimes(); replay(hostRoleCommand); request = PropertyHelper.getUpdateRequest(properties, null); if (status == HostRoleStatus.IN_PROGRESS || status == HostRoleStatus.PENDING || status == HostRoleStatus.HOLDING || status == HostRoleStatus.HOLDING_FAILED || status == HostRoleStatus.HOLDING_TIMEDOUT || status == HostRoleStatus.COMPLETED || status == HostRoleStatus.ABORTED || status == HostRoleStatus.FAILED || status == HostRoleStatus.TIMEDOUT || status == HostRoleStatus.QUEUED || status == HostRoleStatus.SKIPPED_FAILED) { provider.updateResources(request, predicate); } else { try { provider.updateResources(request, predicate); Assert.fail("Expected an java.lang.IllegalArgumentException"); } catch (IllegalArgumentException e) { } } } verify(managementController, response, stage); }