nifi 禁用控制器服务休息 api 调用失败

nifi disabled controller-services rest api call failed

我正在使用 spring 4.2.0nifi 0.6.0,当我使用 RestTemplate 调用 restful api 来禁用 nifi 的控制器服务时

//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);
}

我收到以下错误日志:

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 日志

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$8780e0.invoke() ~[classes/:0.6.0]

browser call disabled api screenshot

有什么办法可以解决这个错误吗?

@Matt Gilman 我在两个方面更改了 disable 函数:

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);
}

但我在 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]

已解决

我比较浏览器禁用请求并再次阅读 api 文档,发现禁用控制器服务必须按照以下步骤操作:

  1. 停止控制器服务引用
  2. 禁用控制器服务引用
  3. 设置控制器服务禁用状态

现在我的新禁用功能是

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);
}

收到您的请求时,似乎并未解释修订版。我以前见过这种情况,当时请求的 Content-Type 设置不正确,结果导致修订没有被正确解析。

我对 RestTemplate 不是很熟悉,但是来自浏览器开发工具的请求的 Content-Type 为 application/x-www-form-urlencoded。你能检查一下 RestTemplate 是否发出了相同类型的请求吗?

或者,您可以直接使用对象发出请求并使用 application/json。这里有一个使用客户端 DTO [1].

设置处理器调度状态的示例

此外,本文底部是修订用例 [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