Put initiates a 2PC process to apply given WriteBatch on all workers.
(workers []Worker, wb WriteBatch)
| 168 | |
| 169 | // Put initiates a 2PC process to apply given WriteBatch on all workers. |
| 170 | func (c *Coordinator) Put(workers []Worker, wb WriteBatch) (result interface{}, err error) { |
| 171 | // Initiate phase one: ask nodes to prepare for progress |
| 172 | ctx, cancel := context.WithTimeout(context.Background(), c.option.timeout) |
| 173 | defer cancel() |
| 174 | |
| 175 | if c.option.beforePrepare != nil { |
| 176 | if err = c.option.beforePrepare(ctx); err != nil { |
| 177 | log.WithError(err).Debug("before prepared failed") |
| 178 | return |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Check prepare results and initiate phase two |
| 183 | if err = c.prepare(ctx, workers, wb); err != nil { |
| 184 | goto ROLLBACK |
| 185 | } |
| 186 | |
| 187 | if c.option.beforeCommit != nil { |
| 188 | if err = c.option.beforeCommit(ctx); err != nil { |
| 189 | log.WithError(err).Debug("before commit failed") |
| 190 | goto ROLLBACK |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | result, err = c.commit(ctx, workers, wb) |
| 195 | |
| 196 | if c.option.afterCommit != nil { |
| 197 | if err = c.option.afterCommit(ctx); err != nil { |
| 198 | log.WithError(err).Debug("after commit failed") |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return |
| 203 | |
| 204 | ROLLBACK: |
| 205 | if c.option.beforeRollback != nil { |
| 206 | // ignore rollback fail options |
| 207 | c.option.beforeRollback(ctx) |
| 208 | } |
| 209 | |
| 210 | c.rollback(ctx, workers, wb) |
| 211 | |
| 212 | return |
| 213 | } |