PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/solr/core/src/java/org/apache/solr/update/processor/IgnoreCommitOptimizeUpdateProcessorFactory.java

http://github.com/apache/lucene-solr
Java | 148 lines | 101 code | 18 blank | 29 comment | 27 complexity | 2ce80d1043ca6e76a388b1ff02a14b7e MD5 | raw file
Possible License(s): LGPL-2.1, CPL-1.0, MPL-2.0-no-copyleft-exception, JSON, Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, MIT, BSD-3-Clause
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.solr.update.processor;
  18. import static org.apache.solr.common.SolrException.ErrorCode;
  19. import java.io.IOException;
  20. import java.lang.invoke.MethodHandles;
  21. import org.apache.solr.common.SolrException;
  22. import org.apache.solr.common.params.SolrParams;
  23. import org.apache.solr.common.util.NamedList;
  24. import org.apache.solr.common.util.SimpleOrderedMap;
  25. import org.apache.solr.request.SolrQueryRequest;
  26. import org.apache.solr.response.SolrQueryResponse;
  27. import org.apache.solr.update.CommitUpdateCommand;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. /**
  31. * <p>
  32. * Gives system administrators a way to ignore explicit commit or optimize requests from clients.
  33. * The factory can be configured to return a specific HTTP response code, default is 403, and
  34. * optional response message, such as to warn the client application that its request was ignored.
  35. * </p>
  36. * @since 5.0.0
  37. */
  38. public class IgnoreCommitOptimizeUpdateProcessorFactory extends UpdateRequestProcessorFactory {
  39. private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
  40. private static final String DEFAULT_RESPONSE_MSG = "Explicit commit/optimize requests are forbidden!";
  41. protected ErrorCode errorCode;
  42. protected String responseMsg;
  43. protected boolean ignoreOptimizeOnly = false; // default behavior is to ignore commits and optimize
  44. @Override
  45. public void init(final NamedList args) {
  46. SolrParams params = (args != null) ? args.toSolrParams() : null;
  47. if (params == null) {
  48. errorCode = ErrorCode.FORBIDDEN; // default is 403 error
  49. responseMsg = DEFAULT_RESPONSE_MSG;
  50. ignoreOptimizeOnly = false;
  51. return;
  52. }
  53. ignoreOptimizeOnly = params.getBool("ignoreOptimizeOnly", false);
  54. int statusCode = params.getInt("statusCode", ErrorCode.FORBIDDEN.code);
  55. if (statusCode == 200) {
  56. errorCode = null; // not needed but makes the logic clearer
  57. responseMsg = params.get("responseMessage"); // OK to be null for 200's
  58. } else {
  59. errorCode = ErrorCode.getErrorCode(statusCode);
  60. if (errorCode == ErrorCode.UNKNOWN) {
  61. // only allow the error codes supported by the SolrException.ErrorCode class
  62. StringBuilder validCodes = new StringBuilder();
  63. int appended = 0;
  64. for (ErrorCode code : ErrorCode.values()) {
  65. if (code != ErrorCode.UNKNOWN) {
  66. if (appended++ > 0) validCodes.append(", ");
  67. validCodes.append(code.code);
  68. }
  69. }
  70. throw new IllegalArgumentException("Configured status code " + statusCode +
  71. " not supported! Please choose one of: " + validCodes.toString());
  72. }
  73. // must always have a response message if sending an error code
  74. responseMsg = params.get("responseMessage", DEFAULT_RESPONSE_MSG);
  75. }
  76. }
  77. @Override
  78. public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {
  79. return new IgnoreCommitOptimizeUpdateProcessor(rsp, this, next);
  80. }
  81. static class IgnoreCommitOptimizeUpdateProcessor extends UpdateRequestProcessor {
  82. private final SolrQueryResponse rsp;
  83. private final ErrorCode errorCode;
  84. private final String responseMsg;
  85. private final boolean ignoreOptimizeOnly;
  86. IgnoreCommitOptimizeUpdateProcessor(SolrQueryResponse rsp,
  87. IgnoreCommitOptimizeUpdateProcessorFactory factory,
  88. UpdateRequestProcessor next)
  89. {
  90. super(next);
  91. this.rsp = rsp;
  92. this.errorCode = factory.errorCode;
  93. this.responseMsg = factory.responseMsg;
  94. this.ignoreOptimizeOnly = factory.ignoreOptimizeOnly;
  95. }
  96. @Override
  97. public void processCommit(CommitUpdateCommand cmd) throws IOException {
  98. if (ignoreOptimizeOnly && !cmd.optimize) {
  99. // we're setup to only ignore optimize requests so it's OK to pass this commit on down the line
  100. if (next != null) next.processCommit(cmd);
  101. return;
  102. }
  103. if (cmd.getReq().getParams().getBool(DistributedUpdateProcessor.COMMIT_END_POINT, false)) {
  104. // this is a targeted commit from replica to leader needed for recovery, so can't be ignored
  105. if (next != null) next.processCommit(cmd);
  106. return;
  107. }
  108. final String cmdType = cmd.optimize ? "optimize" : "commit";
  109. if (errorCode != null) {
  110. IgnoreCommitOptimizeUpdateProcessorFactory.log.info(
  111. "{} from client application ignored with error code: {}", cmdType, errorCode.code);
  112. rsp.setException(new SolrException(errorCode, responseMsg));
  113. } else {
  114. // errorcode is null, treat as a success with an optional message warning the commit request was ignored
  115. IgnoreCommitOptimizeUpdateProcessorFactory.log.info(
  116. "{} from client application ignored with status code: 200", cmdType);
  117. if (responseMsg != null) {
  118. NamedList<Object> responseHeader = rsp.getResponseHeader();
  119. if (responseHeader != null) {
  120. responseHeader.add("msg", responseMsg);
  121. } else {
  122. responseHeader = new SimpleOrderedMap<Object>();
  123. responseHeader.add("msg", responseMsg);
  124. rsp.addResponseHeader(responseHeader);
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }