-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
master.js
312 lines (261 loc) · 9.67 KB
/
master.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const cluster = require('cluster')
const { stats } = require('./middlewares')
const os = require('os')
// Update priorities
const UPDATE_PRIORITIES = {
COMMAND: 1,
DEFAULT: 0
}
// Update identifier prefixes
const ID_PREFIXES = {
USER: 'user_',
CHAT: 'chat_',
RANDOM: 'random_'
}
// Message types
const MESSAGE_TYPES = {
UPDATE: 'UPDATE',
TASK_COMPLETED: 'TASK_COMPLETED',
SEND_MESSAGE: 'SEND_MESSAGE',
TDLIB_REQUEST: 'TDLIB_REQUEST',
TDLIB_RESPONSE: 'TDLIB_RESPONSE'
}
// Monitoring
const LOAD_CHECK_INTERVAL = 5000
const WORKER_HEALTH_CHECK_INTERVAL = 10000
const ADAPTIVE_SCALING_INTERVAL = 30000
const MIN_WORKERS = 2
const CPU_THRESHOLD = 80 // percentage
function setupMaster (bot, queueManager, maxWorkers, maxUpdatesPerWorker) {
const tdlib = require('./helpers/tdlib')
// Add request tracking
let requestCounts = []
const RPS_WINDOW = 10 // Window size in seconds
function trackRequest() {
const now = Date.now()
requestCounts.push(now)
// Keep only requests within the window
requestCounts = requestCounts.filter(time => now - time < RPS_WINDOW * 1000)
}
function calculateRPS() {
const now = Date.now()
// Clean old requests first
requestCounts = requestCounts.filter(time => now - time < RPS_WINDOW * 1000)
return (requestCounts.length / RPS_WINDOW).toFixed(2)
}
console.log(`Master process ${process.pid} is running`)
stats.startPeriodicUpdate()
const workers = []
for (let i = 0; i < maxWorkers; i++) {
const worker = cluster.fork()
workers.push({ worker, load: 0, health: 100 })
}
function getUpdateIdentifier(update) {
// Priority: from user ID > chat ID > fallback to random
if (update.message?.from?.id) {
return `${ID_PREFIXES.USER}${update.message.from.id}`
}
if (update.message?.chat?.id) {
return `${ID_PREFIXES.CHAT}${update.message.chat.id}`
}
return `${ID_PREFIXES.RANDOM}${Math.random()}`
}
// Add update priority determination
function getUpdatePriority(update) {
if (update.message?.text?.startsWith('/')) {
return UPDATE_PRIORITIES.COMMAND
}
return UPDATE_PRIORITIES.DEFAULT
}
function getWorkerForId(identifier) {
// Simple but consistent hash function
const hash = String(identifier).split('').reduce((acc, char) => {
return acc + char.charCodeAt(0)
}, 0)
return workers[hash % workers.length]
}
// Modify distributeUpdate function
function distributeUpdate (update) {
const identifier = getUpdateIdentifier(update)
const targetWorker = getWorkerForId(identifier)
const priority = getUpdatePriority(update)
const updateItem = {
update,
workerIndex: workers.indexOf(targetWorker),
priority
}
if (!queueManager.isPaused() && targetWorker.load < maxUpdatesPerWorker) {
targetWorker.worker.send({ type: MESSAGE_TYPES.UPDATE, payload: update })
targetWorker.load++
} else {
queueManager.addToQueue(updateItem)
}
}
// Modify processQueue function to consider priorities
function processQueue () {
while (queueManager.hasUpdates()) {
const nextItem = queueManager.getNextUpdate() // Returns update with highest priority
const targetWorker = workers[nextItem.workerIndex]
if (targetWorker && targetWorker.load < maxUpdatesPerWorker) {
targetWorker.worker.send({ type: MESSAGE_TYPES.UPDATE, payload: nextItem.update })
targetWorker.load++
} else {
// Return update back to queue
queueManager.addToQueue(nextItem)
break
}
}
if (queueManager.shouldResume()) {
queueManager.resumeUpdates()
}
}
bot.use((ctx, next) => {
const update = ctx.update
trackRequest() // Track each request
distributeUpdate(update)
return next()
})
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`)
const newWorker = cluster.fork()
workers.splice(workers.findIndex(w => w.worker === worker), 1, { worker: newWorker, load: 0, health: 100 })
})
workers.forEach(({ worker }) => {
worker.on('message', async (msg) => {
if (msg.type === MESSAGE_TYPES.TASK_COMPLETED) {
const workerData = workers.find(w => w.worker === worker)
if (workerData) {
workerData.load--
processQueue()
}
} else if (msg.type === MESSAGE_TYPES.SEND_MESSAGE) {
bot.telegram.sendMessage(msg.chatId, msg.text)
} else if (msg.type === MESSAGE_TYPES.TDLIB_REQUEST) {
try {
const result = await tdlib[msg.method](...msg.args)
worker.send({ type: MESSAGE_TYPES.TDLIB_RESPONSE, id: msg.id, result })
} catch (error) {
worker.send({ type: MESSAGE_TYPES.TDLIB_RESPONSE, id: msg.id, error: error.message })
}
}
})
})
// Load monitoring
setInterval(() => {
const totalLoad = workers.reduce((sum, w) => sum + w.load, 0)
const queueStatus = queueManager.getStatus()
console.log(`Total worker load: ${totalLoad}, ${queueStatus}`)
if (totalLoad === workers.length * maxUpdatesPerWorker && queueManager.hasUpdates()) {
console.warn('System under high load: All workers at max capacity and queue not empty')
// Add logic here for notifying admin or auto-scaling
}
}, LOAD_CHECK_INTERVAL)
// Adaptive scaling
let optimalWorkerCount = maxWorkers
function adjustWorkersCount() {
const cpuUsage = os.loadavg()[0] * 100 / os.cpus().length
const currentQueueLoad = queueManager.updateQueue.size / queueManager.maxQueueSize * 100
if (cpuUsage > CPU_THRESHOLD || currentQueueLoad > 70) {
optimalWorkerCount = Math.min(maxWorkers, workers.length + 1)
} else if (cpuUsage < CPU_THRESHOLD / 2 && currentQueueLoad < 30) {
optimalWorkerCount = Math.max(MIN_WORKERS, workers.length - 1)
}
adjustWorkerPool()
}
function adjustWorkerPool() {
while (workers.length < optimalWorkerCount) {
const worker = cluster.fork()
workers.push({ worker, load: 0, health: 100 })
}
while (workers.length > optimalWorkerCount) {
const leastHealthyWorker = workers
.sort((a, b) => a.health - b.health)[0]
leastHealthyWorker.worker.kill()
}
}
// Add these functions before the checkWorkersHealth function
function measureWorkerResponseTime(worker) {
return new Promise((resolve) => {
const start = Date.now()
const timeoutId = setTimeout(() => resolve(5000), 5000) // Max response time 5s
worker.once('message', () => {
clearTimeout(timeoutId)
resolve(Date.now() - start)
})
worker.send({ type: 'HEALTH_CHECK' })
})
}
function calculateWorkerHealth(responseTime, load) {
// Health score from 0 to 100
// Response time weight: 60%, Load weight: 40%
const responseScore = Math.max(0, 100 - (responseTime / 50)) // Penalize for response times over 50ms
const loadScore = Math.max(0, 100 - (load * 10)) // Penalize for high load
return Math.round((responseScore * 0.6) + (loadScore * 0.4))
}
// Modify checkWorkersHealth to be async
async function checkWorkersHealth() {
for (const workerData of workers) {
const responseTime = await measureWorkerResponseTime(workerData.worker)
workerData.health = calculateWorkerHealth(responseTime, workerData.load)
}
}
// Periodic checks
setInterval(checkWorkersHealth, WORKER_HEALTH_CHECK_INTERVAL)
setInterval(adjustWorkersCount, ADAPTIVE_SCALING_INTERVAL)
// Enhanced error handling
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error)
// Recovery attempt
workers.forEach(({ worker }) => worker.kill())
process.exit(1)
})
setInterval(() => {
const metrics = queueManager.getMetrics()
const workerMetrics = workers.map(w => ({
pid: w.worker.process.pid,
load: w.load,
health: w.health
}))
const memoryUsage = process.memoryUsage()
console.log('\n=== System Metrics ===')
console.log('Performance:')
console.log(` Requests/sec: ${calculateRPS()}`) // Add RPS to metrics
console.log('\nQueue Status:')
const queueMetrics = metrics
const hasNonZeroValues = Object.values(queueMetrics).some(value =>
value !== 0 && value !== false && !(Array.isArray(value) && value.length === 0)
)
if (!hasNonZeroValues) {
console.log(' Empty queue')
} else {
const metricsToShow = {
'Total Processed': queueMetrics.totalProcessed,
'Errors': queueMetrics.errors,
'Average Processing Time': queueMetrics.avgProcessingTime ? `${queueMetrics.avgProcessingTime.toFixed(2)}ms` : '0ms',
'Current Size': queueMetrics.currentSize,
'Status': queueMetrics.isPaused ? 'Paused' : 'Active',
'Cache Items': queueMetrics.cacheSize
}
Object.entries(metricsToShow).forEach(([key, value]) => {
if (value && value !== '0ms' && value !== 0) {
console.log(` ${key}: ${value}`)
}
})
}
console.log('\nWorkers Status:')
workerMetrics.forEach(worker => {
console.log(` Worker PID ${worker.pid}:`)
console.log(` Load: ${worker.load}`)
console.log(` Health: ${worker.health}%`)
})
console.log('\nSystem Status:')
console.log(` CPU Load: ${(os.loadavg()[0]).toFixed(2)}`)
console.log(` Memory:`)
console.log(` RSS: ${(memoryUsage.rss / 1024 / 1024).toFixed(2)} MB`)
console.log(` Heap Total: ${(memoryUsage.heapTotal / 1024 / 1024).toFixed(2)} MB`)
console.log(` Heap Used: ${(memoryUsage.heapUsed / 1024 / 1024).toFixed(2)} MB`)
console.log(` Uptime: ${(process.uptime() / 60).toFixed(2)} minutes`)
console.log('==================\n')
}, LOAD_CHECK_INTERVAL)
}
module.exports = { setupMaster }