/
redis.go
152 lines (135 loc) · 4.4 KB
/
redis.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
// Copyright 2017 Tomas Machalek <tomas.machalek@gmail.com>
//
// 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 sredis
// Deprecation note: please note that this package is deprecated and no longer in use in production
import (
"fmt"
"log"
"github.com/czcorpus/klogproc/conversion"
"github.com/czcorpus/klogproc/conversion/kontext013"
"github.com/go-redis/redis"
)
// RedisConf is a structure containing information
// about Redis database containing logs to be
// processed.
type RedisConf struct {
Address string `json:"address"`
Database int `json:"database"`
QueueKey string `json:"queueKey"`
AppType string `json:"appType"`
Version string `json:"version"`
TZShift int `json:"tzShift"`
}
// RedisQueue provides access to Redis database containing
// KonText log records.
type RedisQueue struct {
db *redis.Client
queueKey string
failedItemsKey string
tzShift int
}
// OpenRedisQueue creates a client for Redis
func OpenRedisQueue(address string, database int, queueKey string, tzShift int) (*RedisQueue, error) {
if queueKey == "" {
return nil, fmt.Errorf("No queue key provided")
}
client := &RedisQueue{
db: redis.NewClient(&redis.Options{
Addr: address,
Password: "",
DB: database,
}),
queueKey: queueKey,
failedItemsKey: queueKey + "_failed",
tzShift: tzShift,
}
return client, nil
}
// GetItems loads log data from a Redis queue (list type).
// The data is expected to be in JSON format.
//
// Please note that invalid records are taken from queue too
// and then thrown away (with logged message containing the
// original item source).
func (rc *RedisQueue) GetItems() []conversion.InputRecord {
size := int(rc.db.LLen(rc.queueKey).Val())
log.Printf("INFO: Found %d records in log queue", size)
ans := make([]conversion.InputRecord, 0, size)
for i := 0; i < size; i++ {
rawItem, err := rc.db.LPop(rc.queueKey).Bytes()
if err != nil {
log.Printf("WARNING: %s, orig item: %s", err, rawItem)
}
item, err := kontext013.ImportJSONLog(rawItem)
if err != nil {
log.Printf("WARNING: %s, orig item: %s", err, rawItem)
} else {
ans = append(ans, item)
}
}
return ans
}
// RescueFailedChunks puts records to the end of the Redis queue.
// This is mostly for handling ElasticSearch import errors.
func (rc *RedisQueue) RescueFailedChunks(data [][]byte) error {
for _, item := range data {
rc.db.RPush(rc.failedItemsKey, item)
}
if len(data) > 0 {
log.Printf("INFO: Stored raw data to be reinserted next time (num bulk insert rows: %d)", len(data))
}
return nil
}
// GetRescuedChunksIterator returns an iterator object
// for rescued raw bulk insert records.
func (rc *RedisQueue) GetRescuedChunksIterator() *RedisRescuedChunkIterator {
return &RedisRescuedChunkIterator{
db: rc.db,
currPos: 0,
dbKey: rc.failedItemsKey,
}
}
// ------
// RedisRescuedChunkIterator provides stateful access to
// individual bulk insert chunks ([meta line, data line]+ "new line")
type RedisRescuedChunkIterator struct {
db *redis.Client
currPos int64
dbKey string
}
// GetNextChunk provide next chunk of bulk insert data.
// If nothing is found then a slice of size 0 is returned.
func (rrci *RedisRescuedChunkIterator) GetNextChunk() [][]byte {
queueSize := rrci.db.LLen(rrci.dbKey).Val()
tmp := make([][]byte, 0, queueSize)
var curr string
for ; rrci.currPos < queueSize && curr != "\n"; rrci.currPos++ {
currSrch := rrci.db.LRange(rrci.dbKey, rrci.currPos, rrci.currPos).Val()
if len(currSrch) == 1 {
curr = currSrch[0]
tmp = append(tmp, []byte(curr))
}
}
return tmp
}
// RemoveVisitedItems removes from Redis all the items we iterated through so far
func (rrci *RedisRescuedChunkIterator) RemoveVisitedItems() (int, error) {
status := rrci.db.LTrim(rrci.dbKey, rrci.currPos, int64(-1))
if status.Err() != nil {
return 0, status.Err()
}
numProc := int(rrci.currPos)
rrci.currPos = 0
return numProc, nil
}