v_queue 0.3.0

simple file based queue
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# Troubleshooting Guide

Common issues and solutions for V-Queue.

## Connection Issues

### Issue: Cannot connect to server

**Symptoms**:
```bash
curl: (7) Failed to connect to localhost port 9093: Connection refused
```

**Causes**:
- Server not running
- Wrong port number
- Firewall blocking connection
- Server bound to different address

**Solutions**:

1. **Check if server is running**:
```bash
ps aux | grep v-queue-server
systemctl status v-queue-server
```

2. **Check server logs**:
```bash
journalctl -u v-queue-server -f
```

3. **Verify bind address**:
```bash
netstat -tlnp | grep 9093
# or
ss -tlnp | grep 9093
```

4. **Check firewall**:
```bash
sudo ufw status
sudo iptables -L -n
```

5. **Try different bind address**:
```bash
v-queue-server --bind 0.0.0.0:9093
```

### Issue: Connection timeout

**Symptoms**:
```python
requests.exceptions.Timeout: Request timed out
```

**Causes**:
- Network issues
- Server overloaded
- Client timeout too short

**Solutions**:

1. **Increase client timeout**:
```python
response = requests.get(url, timeout=60)  # 60 seconds
```

2. **Check server load**:
```bash
top
htop
```

3. **Check network latency**:
```bash
ping your-server
traceroute your-server
```

## Authentication Issues

### Issue: 401 Unauthorized

**Symptoms**:
```json
{"error": "Unauthorized"}
```

**Causes**:
- Wrong username or password
- Authentication not configured
- Credentials not sent

**Solutions**:

1. **Verify credentials**:
```bash
# Check config file
cat v-queue-server.toml | grep -A5 "\[users\]"
```

2. **Test with correct credentials**:
```bash
curl -u admin:password http://localhost:9093/api/v1/queues
```

3. **Check if auth is enabled**:
```toml
# In config file
auth_enabled = true  # Should be true
```

4. **Disable auth for testing**:
```bash
v-queue-server --no-auth
```

### Issue: Authentication configured but not working

**Symptoms**:
- 401 errors even with correct password
- Changes to config not taking effect

**Causes**:
- Server not restarted after config change
- Wrong config file being used

**Solutions**:

1. **Restart server**:
```bash
sudo systemctl restart v-queue-server
```

2. **Verify config file location**:
```bash
v-queue-server --config /path/to/config.toml
```

3. **Check logs for config load errors**:
```bash
journalctl -u v-queue-server | grep -i config
```

## Queue Issues

### Issue: Queue not found (404)

**Symptoms**:
```json
{"error": "Queue not found: my-queue"}
```

**Causes**:
- Queue doesn't exist yet
- Wrong queue name
- Permissions issue

**Solutions**:

1. **List available queues**:
```bash
curl -u admin:password http://localhost:9093/api/v1/queues
```

2. **Check queue directory**:
```bash
ls -la /var/lib/vqueue/queues/
```

3. **Queues must be created by writing messages** - The HTTP server provides consumer-only API. Queues are created when first message is written using the v_queue library directly (via Rust code or JNI bindings). They cannot be created via HTTP API.

4. **Check permissions**:
```bash
ls -la /var/lib/vqueue/queues/
sudo chown -R vqueue:vqueue /var/lib/vqueue/queues/
```

### Issue: Queue lock error

**Symptoms**:
```
Error: AlreadyOpen
```

**Causes**:
- Another process has queue open for writing
- Stale lock file from crash
- Multiple writers to same queue

**Solutions**:

1. **Check for running processes**:
```bash
lsof /var/lib/vqueue/queues/my-queue_queue.lock
```

2. **Remove stale lock** (if no process using it):
```bash
rm /var/lib/vqueue/queues/my-queue_queue.lock
```

3. **Only one writer allowed per queue** - this is by design

### Issue: Cannot read messages

**Symptoms**:
- Empty messages array returned
- Consumer stuck at same position

**Causes**:
- Queue is empty
- Consumer already at end of queue
- Corrupted consumer offset file

**Solutions**:

1. **Check queue info**:
```bash
curl -u admin:password http://localhost:9093/api/v1/queues/my-queue
```

2. **Check if messages exist**:
```bash
ls -lh /var/lib/vqueue/queues/my-queue-0/my-queue_queue
# Non-zero size means messages exist
```

3. **Reset consumer position** (delete consumer offset):
```bash
rm /var/lib/vqueue/queues/my-queue_info_pop_my-consumer
```

Next consume will start from beginning.

## Performance Issues

### Issue: Slow message consumption

**Symptoms**:
- High latency on consume requests
- Low throughput

**Causes**:
- Small batch size
- Slow disk
- Network latency

**Solutions**:

1. **Increase batch size**:
```bash
curl "http://localhost:9093/api/v1/queues/events/consumers/my-consumer/messages?max_messages=1000"
```

2. **Check disk performance**:
```bash
iostat -x 1
```

3. **Use SSD instead of HDD**

4. **Reduce network overhead** - run consumer closer to server

### Issue: High CPU usage

**Symptoms**:
- v-queue-server using high CPU
- System slow

**Causes**:
- Debug logging enabled
- Many concurrent requests
- Inefficient polling (timeout=0)

**Solutions**:

1. **Reduce log level**:
```toml
log_level = "warn"
```

2. **Use longer timeouts**:
```python
messages = consumer.consume("events", "consumer", timeout=30)
```

3. **Reduce concurrent connections**

4. **Check for loops without delays**:
```python
# Bad
while True:
    messages = consume(timeout=0)

# Good  
while True:
    messages = consume(timeout=30)
    time.sleep(1)
```

### Issue: Disk space filling up

**Symptoms**:
- Queue directory growing large
- Disk space warnings

**Causes**:
- No retention policy (v-queue keeps all messages)
- Consumers not keeping up
- Large message sizes

**Solutions**:

1. **Check queue sizes**:
```bash
du -sh /var/lib/vqueue/queues/*
```

2. **Manually clean old partitions** (if consumers caught up):
```bash
# Find old partitions
ls -la /var/lib/vqueue/queues/events-0/
ls -la /var/lib/vqueue/queues/events-1/

# Check consumer positions
curl -u admin:password \
  http://localhost:9093/api/v1/queues/events/consumers

# If safe, remove old partitions
rm -rf /var/lib/vqueue/queues/events-0/
```

3. **Implement application-level retention**:
```bash
# Cron job to clean old data
0 2 * * * find /var/lib/vqueue/queues -name "*-[0-9]*" -mtime +7 -exec rm -rf {} \;
```

4. **Monitor disk usage**:
```bash
df -h /var/lib/vqueue/queues
```

## Data Integrity Issues

### Issue: CRC mismatch error

**Symptoms**:
```
Error: CRC check failed
```

**Causes**:
- Corrupted message file
- Disk errors
- Incomplete write (system crash)

**Solutions**:

1. **Check disk health**:
```bash
sudo smartctl -a /dev/sda
dmesg | grep -i error
```

2. **Filesystem check**:
```bash
sudo fsck /dev/sda1
```

3. **If queue corrupted**, may need to skip corrupted partition:
```bash
# Backup corrupted files
cp -r /var/lib/vqueue/queues/events-5 /backup/

# Force new partition (advanced - data loss!)
# Remove queue metadata to force new partition
rm /var/lib/vqueue/queues/events_info_queue
```

### Issue: Lost messages

**Symptoms**:
- Messages produced but not consumable
- Missing sequence in offsets

**Causes**:
- Consumer committed without processing
- Application bug
- System crash during write

**Solutions**:

1. **Check queue integrity**:
```bash
# Examine queue files
ls -lh /var/lib/vqueue/queues/my-queue-*/
```

2. **Reset consumer to re-read**:
```bash
rm /var/lib/vqueue/queues/my-queue_info_pop_my-consumer
```

3. **Implement proper commit strategy**:
```python
# Good: Commit after processing
messages = consumer.consume()
for msg in messages:
    process(msg)  # Process first
consumer.commit()  # Then commit

# Bad: Commit before processing
messages = consumer.consume()
consumer.commit()  # Don't commit here!
for msg in messages:
    process(msg)
```

## Configuration Issues

### Issue: Config file not found

**Symptoms**:
```
Warning: Failed to load config file
```

**Causes**:
- Config file missing
- Wrong path
- Wrong filename

**Solutions**:

1. **Create default config**:
```bash
v-queue-server  # Creates default v-queue-server.toml
```

2. **Specify config path**:
```bash
v-queue-server --config /etc/vqueue/server.toml
```

3. **Check current directory**:
```bash
ls -la v-queue-server.toml
pwd
```

### Issue: Config changes not applied

**Symptoms**:
- Changed config but server behavior unchanged

**Causes**:
- Server not restarted
- Wrong config file
- CLI args overriding config

**Solutions**:

1. **Restart server**:
```bash
sudo systemctl restart v-queue-server
```

2. **Verify config file used**:
```bash
v-queue-server --config /path/to/config.toml
```

3. **Check CLI args** (they override config):
```bash
# This overrides config file
v-queue-server --bind 0.0.0.0:9093 --no-auth
```

## Client Issues

### Issue: Empty messages returned

**Symptoms**:
```json
{"messages": []}
```

**Causes**:
- Queue is empty
- Timeout expired with no new messages
- Consumer at end of queue

**Solutions**:

1. **This is normal behavior** - empty queue returns empty array

2. **Use timeout for long polling**:
```python
# Wait up to 30 seconds for messages
messages = consumer.consume("events", "consumer", timeout=30)
```

3. **Check queue status**:
```bash
curl -u admin:password http://localhost:9093/api/v1/queues/events
# Check count_pushed to see if messages exist
```

### Issue: Messages received multiple times

**Symptoms**:
- Same message processed multiple times

**Causes**:
- Not committing after processing
- Consumer crashes before commit
- Multiple consumers with same name

**Solutions**:

1. **Always commit after processing**:
```python
messages = consumer.consume("events", "consumer")
process(messages)
consumer.commit("events", "consumer")  # Must commit!
```

2. **Implement idempotency** in processing logic

3. **Use unique consumer names**:
```python
import uuid
consumer_name = f"my-service-{uuid.uuid4()}"
# Or use hostname
import socket
consumer_name = f"my-service-{socket.gethostname()}"
```

## System Issues

### Issue: Too many open files

**Symptoms**:
```
Error: Too many open files
```

**Causes**:
- OS file descriptor limit reached
- Resource leak
- Many queues/consumers

**Solutions**:

1. **Check current limits**:
```bash
ulimit -n
```

2. **Increase limits**:
```bash
# Temporary
ulimit -n 65536

# Permanent - /etc/security/limits.conf
vqueue soft nofile 65536
vqueue hard nofile 65536
```

3. **Restart server** after changing limits

4. **Check open files**:
```bash
lsof -p $(pgrep v-queue-server) | wc -l
```

### Issue: Permission denied

**Symptoms**:
```
Error: Permission denied
```

**Causes**:
- Wrong user running server
- Incorrect directory permissions
- SELinux/AppArmor restrictions

**Solutions**:

1. **Check permissions**:
```bash
ls -la /var/lib/vqueue/queues
```

2. **Fix ownership**:
```bash
sudo chown -R vqueue:vqueue /var/lib/vqueue
```

3. **Fix permissions**:
```bash
sudo chmod -R 755 /var/lib/vqueue
```

4. **Check SELinux**:
```bash
getenforce  # If enforcing, may be blocking
sudo ausearch -m avc -ts recent  # Check denials
```

## Debugging Tips

### Enable Debug Logging

```bash
v-queue-server --log-level debug
```

Or in config:
```toml
log_level = "debug"
```

### Check Server Logs

```bash
# systemd
journalctl -u v-queue-server -f

# Direct output
v-queue-server 2>&1 | tee server.log
```

### Test with cURL

```bash
# Health check
curl http://localhost:9093/health

# List queues
curl -u admin:password http://localhost:9093/api/v1/queues

# Queue info
curl -u admin:password http://localhost:9093/api/v1/queues/events

# Consume
curl -u admin:password \
  "http://localhost:9093/api/v1/queues/events/consumers/test/messages?timeout_ms=0"
```

### Inspect Queue Files

```bash
# List queues
ls -la /var/lib/vqueue/queues/

# Check queue size
du -sh /var/lib/vqueue/queues/events-0/

# View metadata files (text)
cat /var/lib/vqueue/queues/events_info_queue
cat /var/lib/vqueue/queues/events-0/events_info_push

# Binary message file (don't cat)
ls -lh /var/lib/vqueue/queues/events-0/events_queue
```

### Network Debugging

```bash
# Check port listening
netstat -tlnp | grep 9093

# Monitor connections
netstat -anp | grep 9093

# Packet capture
sudo tcpdump -i any port 9093 -A
```

### Resource Monitoring

```bash
# CPU and memory
top -p $(pgrep v-queue-server)

# Disk I/O
iostat -x 1

# File descriptors
lsof -p $(pgrep v-queue-server)

# System calls
strace -p $(pgrep v-queue-server)
```

## Getting Help

### Collect Diagnostic Information

When reporting issues:

1. **Version information**:
```bash
v-queue-server --version  # Future enhancement
cargo --version
rustc --version
```

2. **Configuration**:
```bash
cat v-queue-server.toml
```

3. **Logs**:
```bash
journalctl -u v-queue-server --since "1 hour ago" > logs.txt
```

4. **System information**:
```bash
uname -a
df -h
free -h
```

5. **Queue state**:
```bash
ls -laR /var/lib/vqueue/queues/ > queue-state.txt
```

### Common Error Messages

| Error | Meaning | Solution |
|-------|---------|----------|
| Connection refused | Server not running | Start server |
| Unauthorized | Auth failed | Check credentials |
| Queue not found | Queue doesn't exist | Create queue by pushing messages via v_queue library or check name |
| Already open | Queue locked | Check for other writers |
| Permission denied | File permissions | Fix ownership/permissions |
| Too many open files | FD limit reached | Increase limit |
| CRC check failed | Data corruption | Check disk health |

## Next Steps

- [Installation Guide]03-installation.md
- [Configuration Guide]04-configuration.md
- [Performance Guide]08-performance.md