overfight
overfight

Reputation: 43

nifi disabled controller-services rest api call failed

I'm using spring 4.2.0 and nifi 0.6.0, when i use RestTemplate call restful api for disabled nifi's controller-services

//get nifi revision
public RevisionDTO getRevision() {
    Entity revision = restTemplate.getForObject("http://localhost:8083/nifi-api//controller/revision", Entity.class);
    return revision.getRevision();
}

//http parameter
protected Map<String, Object> getRevisionUrl(RevisionDTO revision) {
    Map<String, Object> urlVar = new HashMap<String, Object>();
    urlVar.put("version", revision.getVersion());
    urlVar.put("clientId", revision.getClientId());
    return urlVar;
}

//disable controller service
private void disable(ControllerServiceEntity cs) {
    Map<String, Object> urlVar = getRevisionUrl(getRevision());
    urlVar.put("state", STATE_DISABLED);
    restTemplate.put("http://localhost:8083/nifi-api/controller/controller-services/node/" + cs.getControllerService().getId(), null, urlVar);
}

i got the following error log:

org.springframework.web.client.HttpClientErrorException: 409 Conflict
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:636)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:592)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:560)

nifi log

2016-04-11 16:32:53,982 DEBUG [NiFi Web Server-90 - /nifi-api/controller/controller-services/node/5d99ca33-78f6-465e-9ab1-8c7cb5650025] o.a.n.w.StandardOptimisticLockingManager Revision check failed because current revision is [23, 5d4fe4d3-2f32-4a75-899a-458e8e62efce] but supplied revision is [null, 15976fcf-e01b-483c-994b-df812293d7ad] ...... 2016-04-11 16:32:53,990 DEBUG [NiFi Web Server-90 - /nifi-api/controller/controller-services/node/5d99ca33-78f6-465e-9ab1-8c7cb5650025] c.s.j.spi.container.ContainerResponse Mapped exception to response: 409 (Conflict) org.apache.nifi.web.InvalidRevisionException: This NiFi instance has been updated by 'anonymous'. Please refresh to synchronize the view. at org.apache.nifi.web.StandardOptimisticLockingManager.checkRevision(StandardOptimisticLockingManager.java:62) ~[nifi-web-optimistic-locking-0.6.0.jar:0.6.0] at org.apache.nifi.web.StandardOptimisticLockingManager.configureFlow(StandardOptimisticLockingManager.java:80) ~[nifi-web-optimistic-locking-0.6.0.jar:0.6.0] at org.apache.nifi.web.StandardNiFiServiceFacade.updateControllerService(StandardNiFiServiceFacade.java:1714) ~[classes/:0.6.0] at org.apache.nifi.web.StandardNiFiServiceFacade$$FastClassBySpringCGLIB$$358780e0.invoke() ~[classes/:0.6.0]

browser call disabled api screenshot

is there any way to fix the error?

@Matt Gilman i have changed disable function in two ways:

private void disable(ControllerServiceEntity cs) {
    //http form
    /*MultiValueMap<String, String> urlVar = getRevisionUrl(getRevision());
    urlVar.add("state", STATE_DISABLED);

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(urlVar, requestHeaders);*/

    //http json
    ControllerServiceEntity csu = new ControllerServiceEntity();
    csu.setRevision(getRevision());
    csu.setControllerService(cs.getControllerService());
    csu.getControllerService().setState(STATE_DISABLED);
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtils.obj2json(csu), requestHeaders);

    restTemplate.put(url() + cs.getControllerService().getId(), requestEntity);
}

but i got the same error in nifi:

java.lang.IllegalStateException: DBCPConnectionPool[id=fecc88a8-379d-4fb3-b880-28c5f5caef23] cannot be updated because it is not disabled
at org.apache.nifi.controller.service.StandardControllerServiceNode.verifyCanUpdate(StandardControllerServiceNode.java:217) ~[nifi-framework-core-0.6.0.jar:0.6.0]
at org.apache.nifi.web.dao.impl.StandardControllerServiceDAO.verifyUpdate(StandardControllerServiceDAO.java:225) ~[classes/:na]
at org.apache.nifi.web.dao.impl.StandardControllerServiceDAO.updateControllerService(StandardControllerServiceDAO.java:101) ~[classes/:na]

solved

i compare browser disable request and read api doc again, found disable a controller service must follow these steps:

  1. stop controller service reference
  2. disable controller service reference
  3. set controller service disable state

now my new disable function is

private void disable(ControllerServiceEntity cs) {
    //http form
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    //stop controller service reference
    MultiValueMap<String, String> urlVar = getRevisionUrl(getRevision());
    urlVar.add("state", STATE_STOPPED);
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(urlVar, requestHeaders);
    restTemplate.put(url() + cs.getControllerService().getId() + "/references", requestEntity);

    //disable controller service reference
    urlVar = getRevisionUrl(getRevision());
    requestEntity = new HttpEntity<MultiValueMap<String, String>>(urlVar, requestHeaders);
    urlVar.add("state", STATE_DISABLED);
    restTemplate.put(url() + cs.getControllerService().getId() + "/references", requestEntity);

    //set controller service disable state
    urlVar = getRevisionUrl(getRevision());
    requestEntity = new HttpEntity<MultiValueMap<String, String>>(urlVar, requestHeaders);
    urlVar.add("state", STATE_DISABLED);
    restTemplate.put(url() + cs.getControllerService().getId(), requestEntity);
}

Upvotes: 4

Views: 3072

Answers (1)

Matt Gilman
Matt Gilman

Reputation: 1134

It appears the revision is not being interpreted when your request is received. I've seen this once before when the Content-Type of the request was not set correctly and as a result, the revision was not properly parsed.

I'm not super familiar with RestTemplate but the request coming from your browser's Developer Tools has a Content-Type of application/x-www-form-urlencoded. Can you check that RestTemplate is issuing the same type of request?

Alternatively, you could issue the request using the Object's directly and use application/json. While a little different here is an example of setting Processor scheduled state using the client DTOs [1].

Also, at the bottom of this article is a great sequence diagram of the revision use case [2].

[1] https://github.com/apache/nifi/blob/master/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/test/java/org/apache/nifi/integration/accesscontrol/DfmAccessControlTest.java#L423

[2] https://community.hortonworks.com/content/kbentry/3160/update-nifi-flow-on-the-fly-via-api.html

Upvotes: 3

Related Questions