-
Notifications
You must be signed in to change notification settings - Fork 5
/
transport.go
148 lines (118 loc) · 2.88 KB
/
transport.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package etransport
import (
"C"
"github.com/bioothod/elliptics-go/elliptics"
"github.com/bioothod/backrunner/config"
"io"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"sync"
)
type Elliptics struct {
LogFile io.WriteCloser
Node *elliptics.Node
MetadataGroups []uint32
sync.Mutex
prev_stat *elliptics.DnetStat
}
func (e *Elliptics) MetadataSession() (ms *elliptics.Session, err error) {
ms, err = elliptics.NewSession(e.Node)
if err != nil {
return
}
ms.SetGroups(e.MetadataGroups)
return
}
func (e *Elliptics) DataSession(req *http.Request) (*elliptics.Session, error) {
s, err := elliptics.NewSession(e.Node)
if err != nil {
return nil, err
}
values := req.URL.Query()
var val uint64
var trace_id uint64
trace, ok := req.Header["X-Request"]
if !ok {
trace_id = uint64(rand.Int63())
} else {
trace_id, err = strconv.ParseUint(trace[0], 0, 64)
if err != nil {
trace_id = uint64(rand.Int63())
}
}
ioflags, ok := values["ioflags"]
if ok {
val, err = strconv.ParseUint(ioflags[0], 0, 32)
if err == nil {
s.SetIOflags(elliptics.IOflag(val))
}
}
cflags, ok := values["cflags"]
if ok {
val, err = strconv.ParseUint(cflags[0], 0, 64)
if err == nil {
s.SetCflags(elliptics.Cflag(val))
}
}
trace, ok = values["trace_id"]
if ok {
trace_id, err = strconv.ParseUint(trace[0], 0, 64)
if err != nil {
trace_id = uint64(rand.Int63())
}
}
s.SetTraceID(elliptics.TraceID(trace_id))
return s, nil
}
func (e *Elliptics) Stat() (stat *elliptics.DnetStat, err error) {
s, err := elliptics.NewSession(e.Node)
if err != nil {
return
}
defer s.Delete()
stat = s.DnetStat()
e.Lock()
stat.Diff(e.prev_stat)
e.prev_stat = stat
e.Unlock()
return
}
func NewEllipticsTransport(econf *config.EllipticsClientConfig) (e *Elliptics, err error) {
e = &Elliptics {
prev_stat: nil,
}
if len(econf.LogFile) == 0 || len(econf.LogLevel) == 0 {
log.Fatal("'log-file' and 'log-level' config parameters must be set")
}
e.LogFile, err = os.OpenFile(econf.LogFile, os.O_RDWR | os.O_APPEND | os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Could not open log file '%s': %q", econf.LogFile, err)
}
log.SetPrefix(econf.LogPrefix)
log.SetOutput(e.LogFile)
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
var default_config elliptics.NodeConfig
if econf.Node != default_config {
e.Node, err = elliptics.NewNodeConfig(econf.LogFile, econf.LogLevel, &econf.Node)
} else {
e.Node, err = elliptics.NewNode(econf.LogFile, econf.LogLevel)
}
if err != nil {
log.Fatal(err)
}
if len(econf.Remote) == 0 {
log.Fatal("'remote' config parameter must be set")
}
if len(econf.MetadataGroups) == 0 {
log.Fatal("'metadata-groups' config parameter must be set")
}
e.MetadataGroups = econf.MetadataGroups
err = e.Node.AddRemotes(econf.Remote)
if err != nil {
log.Fatalf("Could not connect to any remote node from %q: %q", econf.Remote, err)
}
return
}