-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadToRedis.js
More file actions
249 lines (212 loc) · 9.45 KB
/
Copy pathloadToRedis.js
File metadata and controls
249 lines (212 loc) · 9.45 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
'use strict';
const csv = require('csv-parser');
const fs = require('fs');
const Redis = require("ioredis");
const { format } = require('date-fns');
const appConfig = require('./config');
const logger = require('./logger').child({ module: 'loadToRedis' });
/**
* Force garbage collection if available
*/
function forceGC() {
if (global.gc) {
global.gc();
} else {
logger.warn('No GC hook! Start your program as `node --expose-gc file.js`.');
}
}
/**
* Convert IP address to integer
* @param {string} ip - IP address string
* @returns {number} IP as integer
*/
function ip2int(ip) {
return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
}
/**
* Load IP ranges from CSV file into Redis
* @param {string} file - CSV file path
* @param {string} redisPrefix - Redis key prefix
* @param {boolean} gc - Enable garbage collection
* @returns {Promise<void>}
*/
exports.load = (file, redisPrefix, gc) => {
let k = 0;
const scratch = [];
return new Promise((resolve, reject) => {
// Create optimized Redis connection
const redis = new Redis({
host: appConfig.redis.host,
port: appConfig.redis.port,
family: appConfig.redis.family,
password: appConfig.redis.password,
db: appConfig.redis.db,
maxRetriesPerRequest: 3,
retryStrategy: (times) => {
const delay = Math.min(times * 50, 2000);
logger.warn({ times, delay }, 'Redis retry');
return delay;
},
enableReadyCheck: true,
enableOfflineQueue: true,
lazyConnect: false
});
redis.on('error', (err) => {
logger.error({ error: err.message }, 'Redis connection error');
reject(err);
});
redis.on('connect', () => {
logger.info('Redis connected for loading');
});
const tempKey = 'arfa45e13grh785gEV4wfw$WF7h';
// Delete temp key if it exists
redis.del(tempKey).catch(err => {
logger.warn({ error: err.message }, 'Error deleting temp key (may not exist)');
});
const stream = fs.createReadStream(file);
stream.on('error', (err) => {
logger.error({ error: err.message, file }, 'File read error');
redis.disconnect();
reject(err);
});
stream
.pipe(csv({
separator: '|',
quote: '~',
mapValues: ({header, index, value}) => {
if (header === 'list') {
return value;
} else {
return parseInt(value);
}
}
}))
.on('data', (r) => {
k++;
if (!(k % 10000)) {
logger.debug({ lines: k }, 'Reading CSV lines');
}
scratch.push({n: r.start_int, a: r.list, e: false});
scratch.push({n: r.end_int, a: r.list, e: true});
})
.on('error', (err) => {
logger.error({ error: err.message }, 'CSV parsing error');
redis.disconnect();
reject(err);
})
.on('end', async () => {
try {
logger.info({ lines: k, scratchSize: scratch.length }, 'CSV file successfully processed');
// Store metadata
const lists = await redis.smembers(redisPrefix + 'lists');
await redis.lpush(redisPrefix + 'ipListSize', JSON.stringify({
date: format(new Date(), 'yyyy-MM-dd HH:mm:ss'),
size: k,
lists: lists
}));
logger.info('Sorting scratch array...');
scratch.sort((a, b) => {
if (a.n < b.n) return -1;
if (a.n > b.n) return 1;
if (a.e < b.e) return -1;
if (a.e > b.e) return 1;
return 0;
});
logger.info('Sorting finished');
let s = [];
let n;
let m;
let pipeline = redis.pipeline();
const batchSize = 100000;
for (let k = 0; k < scratch.length - 1; k++) {
if (!(k % 10000)) {
logger.debug({ processed: k, total: scratch.length }, 'Flattening ranges');
}
if (!(k % batchSize)) {
await pipeline.exec();
if (gc) {
forceGC();
logger.debug('Garbage collection triggered');
}
pipeline = redis.pipeline();
}
const cur = scratch[k];
const nex = scratch[k + 1];
if (cur.e === false) {
s.push(cur.a);
} else {
const index = s.indexOf(cur.a);
if (index > -1) s.splice(index, 1);
}
if (cur.e === false) {
n = cur.n;
} else {
n = cur.n + 1;
}
if (nex.e === false) {
m = nex.n - 1;
} else {
m = nex.n;
}
if (n <= m && s.length) {
s = [...new Set(s)];
const data = {};
for (const i of s) {
const j = JSON.parse(i);
const type = j.type;
delete j.type;
if (!data[type]) data[type] = [];
data[type].push(j);
}
pipeline.zadd(tempKey, m, `${n}|${m}|${JSON.stringify(data)}`);
}
}
// Execute final pipeline
const pipelineResults = await pipeline.exec();
// Check for pipeline errors
if (pipelineResults) {
const errors = pipelineResults.filter(r => r[0] !== null);
if (errors.length > 0) {
logger.error({ errors }, 'Pipeline execution errors');
throw new Error(`Pipeline errors: ${errors.length} commands failed`);
}
}
// Check if temp key has data before renaming
const tempKeySize = await redis.zcard(tempKey);
if (tempKeySize === 0) {
logger.warn('Temp key is empty, aborting rename');
throw new Error('No data to load - temp key is empty');
}
logger.info({ tempKeySize }, 'Temp key populated, proceeding with atomic rename');
// Atomically rename temp key to final key
// This is atomic - either succeeds or fails, no partial state
await redis.rename(tempKey, redisPrefix + 'ranges');
// Verify the rename succeeded
const finalKeySize = await redis.zcard(redisPrefix + 'ranges');
if (finalKeySize !== tempKeySize) {
logger.error({ tempKeySize, finalKeySize }, 'Key size mismatch after rename');
throw new Error('Key size mismatch after rename - possible corruption');
}
// Clean up temp key (shouldn't exist after rename, but just in case)
await redis.del(tempKey).catch(() => {});
logger.info({ finalKeySize }, 'Loading to Redis completed successfully');
await redis.quit();
resolve();
} catch (error) {
logger.error({ error: error.message, stack: error.stack }, 'Error during Redis loading');
// Clean up temp key on error to prevent leaving orphaned data
try {
const tempKeyExists = await redis.exists(tempKey);
if (tempKeyExists) {
await redis.del(tempKey);
logger.info('Cleaned up temp key after error');
}
} catch (cleanupError) {
logger.warn({ error: cleanupError.message }, 'Failed to cleanup temp key');
}
await redis.quit().catch(() => {});
reject(error);
}
});
});
};