Impelements Service methods
| 100 | |
| 101 | // Impelements Service methods |
| 102 | void fetch_add(const FetchAddRequest* request, |
| 103 | CounterResponse* response, |
| 104 | google::protobuf::Closure* done) { |
| 105 | brpc::ClosureGuard done_guard(done); |
| 106 | // Serialize request to the replicated write-ahead-log so that all the |
| 107 | // peers in the group receive this request as well. |
| 108 | // Notice that _value can't be modified in this routine otherwise it |
| 109 | // will be inconsistent with others in this group. |
| 110 | |
| 111 | // Serialize request to IOBuf |
| 112 | const int64_t term = _leader_term.load(butil::memory_order_relaxed); |
| 113 | if (term < 0) { |
| 114 | return redirect(response); |
| 115 | } |
| 116 | butil::IOBuf log; |
| 117 | butil::IOBufAsZeroCopyOutputStream wrapper(&log); |
| 118 | if (!request->SerializeToZeroCopyStream(&wrapper)) { |
| 119 | LOG(ERROR) << "Fail to serialize request"; |
| 120 | response->set_success(false); |
| 121 | return; |
| 122 | } |
| 123 | // Apply this log as a braft::Task |
| 124 | braft::Task task; |
| 125 | task.data = &log; |
| 126 | // This callback would be iovoked when the task actually excuted or |
| 127 | // fail |
| 128 | task.done = new FetchAddClosure(this, request, response, |
| 129 | done_guard.release()); |
| 130 | if (FLAGS_check_term) { |
| 131 | // ABA problem can be avoid if expected_term is set |
| 132 | task.expected_term = term; |
| 133 | } |
| 134 | // Now the task is applied to the group, waiting for the result. |
| 135 | return _node->apply(task); |
| 136 | } |
| 137 | |
| 138 | void get(CounterResponse* response) { |
| 139 | // In consideration of consistency. GetRequest to follower should be |