-
Notifications
You must be signed in to change notification settings - Fork 0
/
immudb.go
179 lines (168 loc) · 4.33 KB
/
immudb.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/codenotary/immudb/pkg/api/schema"
immuclient "github.com/codenotary/immudb/pkg/client"
"github.com/lestrrat-go/strftime"
"github.com/spf13/viper"
"log"
"sync"
"time"
)
type immuConnection struct {
hostname string
port int
username string
password string
database string
pattern *strftime.Strftime
token string
ctx context.Context
client immuclient.ImmuClient
mx sync.Mutex
}
func (ic *immuConnection) cfg_init() {
var err error
ic.hostname = viper.GetString("immudb_hostname")
ic.port = viper.GetInt("immudb_port")
ic.username = viper.GetString("immudb_username")
ic.password = viper.GetString("immudb_password")
ic.pattern, err = strftime.New(viper.GetString("immudb_pattern"))
if err != nil {
log.Fatalln("Unable to parse database pattern string")
}
ic.client = nil
}
func (ic *immuConnection) context() context.Context {
return ic.ctx
}
func (ic *immuConnection) db_list() []string {
var databases []string
dbs, err := ic.client.DatabaseListV2(ic.ctx)
if err != nil {
log.Fatalln("Failed to get database list. Reason: %s", err.Error())
}
for _, s := range dbs.Databases {
if s.Loaded {
databases = append(databases, s.Name)
}
}
return databases
}
func (ic *immuConnection) rotate() bool {
ic.mx.Lock()
defer ic.mx.Unlock()
newdb := ic.pattern.FormatString(time.Now())
if newdb == ic.database {
log.Printf("Rotation not necessary")
return false
}
ic.database = newdb
ic.client.CloseSession(ic.ctx)
ic.connect(ic.ctx)
return true
}
func (ic *immuConnection) connect(ctx context.Context) {
log.Printf("Connecting to immudb: %s:%d", ic.hostname, ic.port)
ic.ctx = ctx
opts := immuclient.DefaultOptions().WithAddress(ic.hostname).WithPort(ic.port)
ic.client = immuclient.NewClient().WithOptions(opts)
ic.database = ic.pattern.FormatString(time.Now())
err := ic.client.OpenSession(ic.ctx, []byte(ic.username), []byte(ic.password), "defaultdb")
if err != nil {
log.Fatalln("Failed to open session. Reason:", err.Error())
}
found := false
for _, dbname := range ic.db_list() {
if dbname == ic.database {
found = true
}
}
if !found {
log.Printf("Not found %s", ic.database)
_, err := ic.client.CreateDatabaseV2(ic.ctx, ic.database, &schema.DatabaseNullableSettings{})
if err != nil {
log.Fatalf("Error creating database %s: %s", ic.database, err.Error())
}
log.Printf("Created database %s", ic.database)
}
ic.client.CloseSession(ic.ctx)
err = ic.client.OpenSession(ic.ctx, []byte(ic.username), []byte(ic.password), ic.database)
if err != nil {
log.Fatalln("Failed to open session. Reason:", err.Error())
}
log.Printf("Connected to %s", ic.database)
}
func (ic *immuConnection) pushmsg(msgs []logMsg) error {
l := len(msgs)
ops := make([]*schema.Op, 3*l)
c := 0
for i, msg := range msgs {
key := []byte(fmt.Sprintf("L:%s/%s@%s:%f:%d",
msg.Kubernetes.Namespace,
msg.Kubernetes.PodName,
msg.Kubernetes.Host,
msg.Date,
i,
))
ptr := []byte(fmt.Sprintf("T:%f:%d", msg.Date, i))
val, _ := json.Marshal(msg)
c, ops[c] = c+1, &schema.Op{
Operation: &schema.Op_Kv{
Kv: &schema.KeyValue{
Key: key,
Value: val,
},
},
}
c, ops[c] = c+1, &schema.Op{
Operation: &schema.Op_Ref{
Ref: &schema.ReferenceRequest{
Key: ptr,
ReferencedKey: key,
},
},
}
if msg.AssignedId != "" {
idPtr := []byte(fmt.Sprintf("I:%s", msg.AssignedId))
c, ops[c] = c+1, &schema.Op{
Operation: &schema.Op_Ref{
Ref: &schema.ReferenceRequest{
Key: idPtr,
ReferencedKey: key,
},
},
}
}
}
opList := &schema.ExecAllRequest{Operations: ops[:c]}
id, err := ic.DoExecAll(opList)
if err != nil {
log.Printf("Error sending data to immudb: %s", err.Error())
return err
}
log.Printf("Sent data, consumed %d messages, tx %d", l, id)
return nil
}
func (ic *immuConnection) DoExecAll(opList *schema.ExecAllRequest) (uint64, error) {
var err error
ic.mx.Lock()
defer ic.mx.Unlock()
for i := 0; i < 5; i++ {
ret, err := ic.client.ExecAll(ic.ctx, opList)
if err == nil {
return ret.Id, nil
}
ic.client.CloseSession(ic.ctx)
time.Sleep(time.Duration(i*100) * time.Millisecond)
ic.connect(ic.ctx)
}
// one last time...
ret, err := ic.client.ExecAll(ic.ctx, opList)
if err == nil {
return ret.Id, nil
}
return 0, err
}