-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtransaction_commit_informer.go
124 lines (102 loc) · 2.73 KB
/
transaction_commit_informer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (C) 2015 NTT Innovation Institute, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/cloudwan/gohan/metrics"
gohan_sync "github.com/cloudwan/gohan/sync"
"github.com/cloudwan/gohan/util"
)
var transactionCommitted = make(chan int64, 1024)
const (
SyncKeyTxCommitted = "/gohan/cluster/sync/tx_committed"
defaultRetryDelay = 500 * time.Millisecond
)
func NewTransactionCommitInformer(sync gohan_sync.Sync) *TransactionCommitInformer {
return &TransactionCommitInformer{
sync: sync,
retryDelay: getRetryDelay(),
}
}
func getRetryDelay() time.Duration {
return util.GetConfig().GetDuration("transaction_commit_informer/retry_delay", defaultRetryDelay)
}
type TransactionCommitInformer struct {
sync gohan_sync.Sync
retryDelay time.Duration
}
func (t *TransactionCommitInformer) Run(ctx context.Context, wg *sync.WaitGroup) error {
defer wg.Done()
for {
select {
case id := <-transactionCommitted:
t.notify(ctx, id)
case <-ctx.Done():
return ctx.Err()
}
}
}
func (t *TransactionCommitInformer) notify(ctx context.Context, lastId int64) {
var attempt int64 = 0
defer func() {
t.updateCounter(attempt, "notify.retries")
}()
t.updateCounter(1, "notify.called")
for {
lastId = t.drain(lastId)
err := t.sync.Update(ctx, SyncKeyTxCommitted, buildSyncValue(lastId))
if err == nil {
return
}
log.Error("Failed to notify about committed transaction: %s", err)
select {
case <-ctx.Done():
return
case <-time.After(500 * time.Millisecond):
}
attempt++
}
}
func (t *TransactionCommitInformer) drain(eventId int64) int64 {
var drained int64 = 0
defer func() {
t.updateCounter(drained, "drained")
}()
for {
select {
case eventId = <-transactionCommitted:
drained++
default:
return eventId
}
}
}
func buildSyncValue(id int64) string {
type syncedEvent struct {
EventId int64 `json:"event_id"`
}
data, err := json.Marshal(syncedEvent{id})
if err != nil {
panic(fmt.Sprintf("Can't marshall data: %s", err))
}
return string(data)
}
func (t *TransactionCommitInformer) updateCounter(delta int64, metric string) {
metrics.UpdateCounter(delta, "tx_commit_informer.%s", metric)
}