buffer_flush.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "buffer_flush.h"
  17. #include "buffer_process_ask_chain.h"
  18. #include "global.h"
  19. DTCFlushRequest::DTCFlushRequest(BufferProcessAskChain *o, const char *key)
  20. : owner(o), numReq(0), badReq(0), wait(NULL)
  21. {
  22. }
  23. DTCFlushRequest::~DTCFlushRequest()
  24. {
  25. if (wait) {
  26. wait->turn_around_job_answer();
  27. wait = NULL;
  28. }
  29. }
  30. class DropDataReply : public JobAnswerInterface<DTCJobOperation> {
  31. public:
  32. DropDataReply()
  33. {
  34. }
  35. virtual void job_answer_procedure(DTCJobOperation *job_operation);
  36. };
  37. void DropDataReply::job_answer_procedure(DTCJobOperation *job_operation)
  38. {
  39. DTCFlushRequest *req = job_operation->OwnerInfo<DTCFlushRequest>();
  40. if (req == NULL)
  41. delete job_operation;
  42. else
  43. req->complete_row(job_operation, job_operation->owner_index());
  44. }
  45. static DropDataReply dropReply;
  46. int DTCFlushRequest::flush_row(const RowValue &row)
  47. {
  48. DTCJobOperation *pJob = new DTCJobOperation;
  49. if (pJob == NULL) {
  50. log4cplus_error(
  51. "cannot flush row, new job error, possible memory exhausted\n");
  52. return -1;
  53. }
  54. if (pJob->Copy(row) < 0) {
  55. log4cplus_error("cannot flush row, from: %s error: %s \n",
  56. pJob->resultInfo.error_from(),
  57. pJob->resultInfo.error_message());
  58. return -1;
  59. }
  60. pJob->set_request_type(TaskTypeCommit);
  61. pJob->push_reply_dispatcher(&dropReply);
  62. pJob->set_owner_info(this, numReq, NULL);
  63. owner->inc_async_flush_stat();
  64. //TaskTypeCommit never expired
  65. //pJob->set_expire_time(3600*1000/*ms*/);
  66. numReq++;
  67. owner->push_flush_queue(pJob);
  68. return 0;
  69. }
  70. void DTCFlushRequest::complete_row(DTCJobOperation *req, int index)
  71. {
  72. delete req;
  73. numReq--;
  74. if (numReq == 0) {
  75. if (wait) {
  76. wait->turn_around_job_answer();
  77. wait = NULL;
  78. }
  79. owner->complete_flush_request(this);
  80. }
  81. }