Impelements Service methods
| 118 | |
| 119 | // Impelements Service methods |
| 120 | void write(const BlockRequest* request, |
| 121 | BlockResponse* response, |
| 122 | butil::IOBuf* data, |
| 123 | google::protobuf::Closure* done) { |
| 124 | brpc::ClosureGuard done_guard(done); |
| 125 | // Serialize request to the replicated write-ahead-log so that all the |
| 126 | // peers in the group receive this request as well. |
| 127 | // Notice that _value can't be modified in this routine otherwise it |
| 128 | // will be inconsistent with others in this group. |
| 129 | |
| 130 | // Serialize request to IOBuf |
| 131 | const int64_t term = _leader_term.load(butil::memory_order_relaxed); |
| 132 | if (term < 0) { |
| 133 | return redirect(response); |
| 134 | } |
| 135 | butil::IOBuf log; |
| 136 | const uint32_t meta_size_raw = butil::HostToNet32(request->ByteSize()); |
| 137 | log.append(&meta_size_raw, sizeof(uint32_t)); |
| 138 | butil::IOBufAsZeroCopyOutputStream wrapper(&log); |
| 139 | if (!request->SerializeToZeroCopyStream(&wrapper)) { |
| 140 | LOG(ERROR) << "Fail to serialize request"; |
| 141 | response->set_success(false); |
| 142 | return; |
| 143 | } |
| 144 | log.append(*data); |
| 145 | // Apply this log as a braft::Task |
| 146 | braft::Task task; |
| 147 | task.data = &log; |
| 148 | // This callback would be iovoked when the task actually excuted or |
| 149 | // fail |
| 150 | task.done = new BlockClosure(this, request, response, |
| 151 | data, done_guard.release()); |
| 152 | if (FLAGS_check_term) { |
| 153 | // ABA problem can be avoid if expected_term is set |
| 154 | task.expected_term = term; |
| 155 | } |
| 156 | // Now the task is applied to the group, waiting for the result. |
| 157 | return _node->apply(task); |
| 158 | } |
| 159 | |
| 160 | void read(const BlockRequest *request, BlockResponse* response, |
| 161 | butil::IOBuf* buf) { |