Skip to content

Commit e8b0e5d

Browse files
regexPatterngermandouceLucasAraujo639
authored
Ultimos fixes (#89)
Co-authored-by: Germán Douce <germandouce@users.noreply.github.com> Co-authored-by: Lucas Araujo <LucasAraujo639@users.noreply.github.com>
1 parent b61506c commit e8b0e5d

8 files changed

Lines changed: 120 additions & 62 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Dockerfile
22
LICENSE
33
README.md
44
compose.yaml
5+
config-cluster.sh
56
redis-server/redis*.conf
67
redis-server/test-cluster/
78
redis-server/test-tls/

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ La estructura se puede visualizar faciltamente en el siguiente gráfico:
4949
0-5460 5461-10921 10922-16383
5050
```
5151

52-
Para configurar el cluster de esta forma se debe ejecutar el script [`config-cluster.sh`](./config-cluster.sh), que fue construído justamente para facilitar la configuración de un cluster con esta estructura. La utilidad `redis-cli` debe estar descargada de manera local para el correcto funcionamiento del script. Para ejecutarlo, se debe correr el siguiente comando desde la raíz del repositorio:
53-
54-
```bash
55-
bash ./config-cluster.sh
56-
```
52+
La configuración del cluster se hace de manera automática al inicializar el compose utilizando el script [`config-cluster.sh`](./config-cluster.sh). Se espera a que el cluster esté bien configurado para que finalmente pueda ser utilizado.
5753

5854
### Detención y Destrucción del Cluster
5955

81 Bytes
Binary file not shown.

compose.yaml

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,15 @@ services:
143143
interval: 10s
144144
start_period: 3s
145145

146-
docs_syncer:
146+
cluster_configurator:
147147
build:
148-
dockerfile: docs-syncer.Dockerfile
149-
environment:
150-
REDIS_HOST: 172.20.0.10
151-
REDIS_PORT: 7000
148+
dockerfile_inline: |
149+
FROM debian:bullseye-slim
150+
RUN apt-get update && apt-get install -y redis-tools && rm -rf /var/lib/apt/lists/*
151+
command:
152+
bash /config-cluster.conf
153+
volumes:
154+
- ./config-cluster.sh:/config-cluster.conf
152155
depends_on:
153156
redis_server_7000:
154157
condition: service_healthy
@@ -171,32 +174,30 @@ services:
171174
networks:
172175
- redis_network
173176

177+
docs_syncer:
178+
build:
179+
dockerfile: docs-syncer.Dockerfile
180+
environment:
181+
REDIS_HOST: 172.20.0.16
182+
REDIS_PORT: 7006
183+
depends_on:
184+
cluster_configurator:
185+
condition: service_completed_successfully
186+
restart: true
187+
networks:
188+
- redis_network
189+
174190
docs_gpt:
175191
build:
176192
dockerfile: docs-gpt.Dockerfile
177193
environment:
178194
OPENAI_API_KEY: ${OPENAI_API_KEY:?}
179-
REDIS_HOST: 172.20.0.10
180-
REDIS_PORT: 7000
195+
REDIS_HOST: 172.20.0.16
196+
REDIS_PORT: 7006
181197
depends_on:
182-
redis_server_7000:
183-
condition: service_healthy
184-
redis_server_7001:
185-
condition: service_healthy
186-
redis_server_7002:
187-
condition: service_healthy
188-
redis_server_7003:
189-
condition: service_healthy
190-
redis_server_7004:
191-
condition: service_healthy
192-
redis_server_7005:
193-
condition: service_healthy
194-
redis_server_7006:
195-
condition: service_healthy
196-
redis_server_7007:
197-
condition: service_healthy
198-
redis_server_7008:
199-
condition: service_healthy
198+
cluster_configurator:
199+
condition: service_completed_successfully
200+
restart: true
200201
networks:
201202
- redis_network
202203

config-cluster.sh

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ conectar_nodos() {
2020
other_ports=("$@")
2121

2222
for port in "${other_ports[@]}"; do
23-
redis_cmd "redis-cli -p 7000 cluster meet 172.20.0.1$port 700$port"
23+
redis_cmd "redis-cli -h 172.20.0.10 -p 7000 cluster meet 172.20.0.1$port 700$port"
2424
done
2525
}
2626

2727
asignar_slots() {
2828
title "Asignando hash slots"
2929

30-
redis_cmd "redis-cli -p 7000 cluster addslotsrange 0 5460"
31-
redis_cmd "redis-cli -p 7003 cluster addslotsrange 5461 10921"
32-
redis_cmd "redis-cli -p 7006 cluster addslotsrange 10922 16383"
30+
redis_cmd "redis-cli -h 172.20.0.10 -p 7000 cluster addslotsrange 0 5460"
31+
redis_cmd "redis-cli -h 172.20.0.13 -p 7003 cluster addslotsrange 5461 10921"
32+
redis_cmd "redis-cli -h 172.20.0.16 -p 7006 cluster addslotsrange 10922 16383"
3333
}
3434

3535
asignar_replicas() {
3636
master_port=$1
3737
shift
3838
replicas_ports=("$@")
3939

40-
master_id=$(redis-cli -p "$master_port" cluster myid)
40+
master_id=$(redis-cli -h 172.20.0.1$master_port -p 700$master_port cluster myid)
4141
subtitle "Nodo $master_port (id: $master_id)\n"
4242

4343
for port in "${replicas_ports[@]}"; do
44-
redis_cmd "redis-cli -p $port cluster replicate $master_id"
44+
redis_cmd "redis-cli -h 172.20.0.1$port -p 700$port cluster replicate $master_id"
4545
done
4646
}
4747

@@ -64,14 +64,14 @@ redis_cmd() {
6464

6565
redis_demo() {
6666
echo "
67-
┌──────┐ ┌──────┐ ┌──────┐
68-
│ 7000 │ │ 7003 │ │ 7006 │
69-
└──┬┬──┘ └──┬┬──┘ └──┬┬──┘
70-
┌──────┐ ││ ┌──────┐ ┌──────┐ ││ ┌──────┐ ┌──────┐ ││ ┌──────┐
71-
│ 7001 ◄─┘└─► 7002 │ │ 7004 ◄─┘└─► 7005 │ │ 7007 ◄─┘└─► 7008 │
72-
└──────┘ └──────┘ └──────┘ └──────┘ └──────┘ └──────┘
73-
└────────────────────┘└────────────────────┘└────────────────────┘
74-
0-5460 5461-10921 10922-16383
67+
┌──────┐ ┌──────┐ ┌──────┐
68+
│ 7000 │ │ 7003 │ │ 7006 │
69+
└──┬┬──┘ └──┬┬──┘ └──┬┬──┘
70+
┌──────┐ ││ ┌──────┐ ┌──────┐ ││ ┌──────┐ ┌──────┐ ││ ┌──────┐
71+
│ 7001 ◄─┘└─► 7002 │ │ 7004 ◄─┘└─► 7005 │ │ 7007 ◄─┘└─► 7008 │
72+
└──────┘ └──────┘ └──────┘ └──────┘ └──────┘ └──────┘
73+
└────────────────────┘└────────────────────┘└────────────────────┘
74+
0-5460 5461-10921 10922-16383
7575
"
7676

7777
ports=($(seq 0 8))
@@ -95,13 +95,28 @@ redis_demo() {
9595

9696
title "Asignando replicas a masters"
9797

98-
asignar_replicas 7000 7001 7002
98+
asignar_replicas 0 1 2
9999
echo
100100

101-
asignar_replicas 7003 7004 7005
101+
asignar_replicas 3 4 5
102102
echo
103103

104-
asignar_replicas 7006 7007 7008
104+
asignar_replicas 6 7 8
105+
echo
106+
107+
while true; do
108+
(sleep 1) &
109+
spinner $! "Verificando estado del cluster..."
110+
echo
111+
112+
result=$(redis-cli -h 172.20.0.16 -p 7006 get docs_ids 2>&1)
113+
if [[ "$result" != *"UNSET"* ]]; then
114+
break
115+
fi
116+
sleep 2
117+
done
118+
119+
title "¡Cluster Redis configurado correctamente!"
105120
}
106121

107122
redis_demo

docs-editor/src/editor/text.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ impl Widget for TextEditor<'_, '_, '_, '_, '_> {
8282
ui.horizontal(|ui| {
8383
ui.add_enabled_ui(
8484
!self.waiting_llm_response
85-
&& (*self.full_gen_on || *self.rango_valido),
85+
&& (*self.full_gen_on || *self.rango_valido)
86+
&& !self.prompt.is_empty(),
8687
|ui| {
8788
if ui.button("Generar").clicked() {
8889
let _ =

docs-editor/src/menu.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
};
66

77
use chrono::{DateTime, Local};
8-
use eframe::egui::{self, RichText, Vec2};
8+
use eframe::egui::{self, RichText, Vec2, ViewportCommand};
99
use redis_cmd::{
1010
Command,
1111
pub_sub::{PubSubCommand, Publish},
@@ -119,13 +119,22 @@ impl Menu {
119119
pub fn ui(&mut self, ui: &mut egui::Ui) -> Option<DocMetadata> {
120120
let mut selected_doc = None;
121121

122+
ui.ctx()
123+
.send_viewport_cmd(ViewportCommand::Title("Editor de Documentos".to_string()));
124+
122125
ui.horizontal(|ui| {
123126
ui.vertical(|ui| {
124127
ui.set_width(300.0);
125128

126129
ui.heading(RichText::new("Abrir Documento"));
127130
ui.add_space(8.0);
128131

132+
if ui.button("Refrescar 🔃").clicked() {
133+
self.update_saved_documents_list().unwrap();
134+
}
135+
136+
ui.add_space(8.0);
137+
129138
ui.vertical(|ui| {
130139
let now = Local::now();
131140

docs-syncer/src/lib.rs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use chrono::Local;
1414
use redis_cmd::{
1515
Command,
1616
pub_sub::{PubSubCommand, Subscribe},
17-
storage::{Get, HGetAll, HSet, StorageCommand},
17+
storage::{Get, HGetAll, HKeys, HSet, StorageCommand},
1818
};
1919
use redis_resp::{BulkString, RespDataType};
2020

@@ -35,8 +35,8 @@ pub enum DocsSyncerAction {
3535
doc_id: BulkString,
3636
doc_basename: BulkString,
3737
},
38-
SubscribeToDocument {
39-
docs_ids: BulkString,
38+
SubscribeToDocuments {
39+
docs_ids: Vec<BulkString>,
4040
},
4141
ConnectClient {
4242
client_id: String,
@@ -81,9 +81,9 @@ impl DocsSyncer {
8181
doc_basename,
8282
} => {
8383
self.create_new_document(doc_id.clone(), doc_basename)?;
84-
self.subscribe_to_document(doc_id)?;
84+
self.subscribe_to_document(vec![doc_id])?;
8585
}
86-
DocsSyncerAction::SubscribeToDocument { docs_ids } => {
86+
DocsSyncerAction::SubscribeToDocuments { docs_ids } => {
8787
self.subscribe_to_document(docs_ids)?;
8888
}
8989
DocsSyncerAction::ConnectClient {
@@ -92,9 +92,7 @@ impl DocsSyncer {
9292
doc_kind,
9393
doc_basename,
9494
} => {
95-
let doc_id_bs = BulkString::from(&doc_id);
9695
self.connect_client(client_id, doc_id, doc_kind, doc_basename)?;
97-
self.subscribe_to_document(doc_id_bs)?;
9896
}
9997
DocsSyncerAction::DisconnectClient { client_id, doc_id } => {
10098
self.disconnect_client(client_id, doc_id)
@@ -115,7 +113,7 @@ impl DocsSyncer {
115113
Ok(())
116114
});
117115

118-
// Self::subscribe_to_saved_documents(db_addr, actions_tx.clone())?;
116+
Self::subscribe_to_saved_documents(db_addr, actions_tx.clone())?;
119117

120118
Self::start_documents_creator(db_addr, actions_tx.clone())?;
121119
Self::start_documents_watcher(docs_stream, actions_tx.clone());
@@ -186,12 +184,14 @@ impl DocsSyncer {
186184
Ok(())
187185
}
188186

189-
fn subscribe_to_document(&mut self, doc_id: BulkString) -> Result<(), Error> {
190-
let log_msg = log::info!("suscrito a documento {doc_id}");
187+
fn subscribe_to_document(&mut self, docs_ids: Vec<BulkString>) -> Result<(), Error> {
188+
let mut log_msg = String::from("suscrito a documentos");
191189

192-
let cmd = Command::PubSub(PubSubCommand::Subscribe(Subscribe {
193-
channels: vec![doc_id],
194-
}));
190+
for doc_id in &docs_ids {
191+
log_msg.push_str(&format!(" {doc_id}"));
192+
}
193+
194+
let cmd = Command::PubSub(PubSubCommand::Subscribe(Subscribe { channels: docs_ids }));
195195

196196
self.docs_stream
197197
.write_all(&Vec::from(cmd))
@@ -433,4 +433,39 @@ impl DocsSyncer {
433433

434434
Ok(())
435435
}
436+
437+
fn subscribe_to_saved_documents(
438+
slot_addr: SocketAddr,
439+
actions_tx: Sender<DocsSyncerAction>,
440+
) -> Result<(), Error> {
441+
let cmd = Vec::from(Command::Storage(StorageCommand::HKeys(HKeys {
442+
key: "docs_ids".into(),
443+
})));
444+
445+
let (_, reply) = Self::cluster_command(slot_addr, cmd)?;
446+
let saved_docs_ids = match reply {
447+
RespDataType::Array(array) => array
448+
.into_iter()
449+
.filter_map(|i| {
450+
if let RespDataType::BulkString(id) = i {
451+
Some(id)
452+
} else {
453+
None
454+
}
455+
})
456+
.collect(),
457+
RespDataType::Null => Vec::new(),
458+
_ => unreachable!(),
459+
};
460+
461+
if !saved_docs_ids.is_empty() {
462+
let _ = actions_tx.send(DocsSyncerAction::SubscribeToDocuments {
463+
docs_ids: saved_docs_ids,
464+
});
465+
} else {
466+
print!("{}", log::info!("no hay documentos existentes"));
467+
}
468+
469+
Ok(())
470+
}
436471
}

0 commit comments

Comments
 (0)