|
13 | 13 | import com.github.sonus21.rqueue.config.NatsBackendCondition; |
14 | 14 | import com.github.sonus21.rqueue.dao.RqueueSystemConfigDao; |
15 | 15 | import com.github.sonus21.rqueue.models.db.QueueConfig; |
| 16 | +import io.nats.client.Connection; |
| 17 | +import io.nats.client.JetStreamApiException; |
| 18 | +import io.nats.client.KeyValue; |
| 19 | +import io.nats.client.KeyValueManagement; |
| 20 | +import io.nats.client.api.KeyValueConfiguration; |
| 21 | +import io.nats.client.api.KeyValueEntry; |
| 22 | +import io.nats.client.api.KeyValueStatus; |
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.ByteArrayOutputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.ObjectInputStream; |
| 27 | +import java.io.ObjectOutputStream; |
| 28 | +import java.util.ArrayList; |
16 | 29 | import java.util.Collection; |
17 | | -import java.util.Collections; |
18 | 30 | import java.util.List; |
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
| 32 | +import java.util.concurrent.atomic.AtomicReference; |
| 33 | +import java.util.logging.Level; |
| 34 | +import java.util.logging.Logger; |
19 | 35 | import org.springframework.context.annotation.Conditional; |
20 | 36 | import org.springframework.stereotype.Repository; |
21 | 37 |
|
22 | | -/** NATS-backend stub {@link RqueueSystemConfigDao} for non-Redis backends. */ |
| 38 | +/** |
| 39 | + * NATS-backed {@link RqueueSystemConfigDao} using a JetStream KV bucket as the queue-config |
| 40 | + * store. Entries are keyed by {@link QueueConfig#getName()} and serialized via standard Java |
| 41 | + * serialization, matching the Redis impl which also relies on |
| 42 | + * {@link com.github.sonus21.rqueue.models.SerializableBase}. |
| 43 | + * |
| 44 | + * <p>An in-process cache mirrors the Redis impl's {@code byCachedXxx} methods for parity. |
| 45 | + * {@link #clearCacheByName(String)} evicts; {@link #saveQConfig(QueueConfig)} keeps the cache |
| 46 | + * in sync. |
| 47 | + */ |
23 | 48 | @Repository |
24 | 49 | @Conditional(NatsBackendCondition.class) |
25 | 50 | public class NatsRqueueSystemConfigDao implements RqueueSystemConfigDao { |
26 | | - @Override |
27 | | - public QueueConfig getConfigByName(String name) { |
28 | | - return null; |
| 51 | + |
| 52 | + private static final Logger log = Logger.getLogger(NatsRqueueSystemConfigDao.class.getName()); |
| 53 | + private static final String BUCKET_NAME = "rqueue-queue-config"; |
| 54 | + |
| 55 | + private final Connection connection; |
| 56 | + private final KeyValueManagement kvm; |
| 57 | + private final AtomicReference<KeyValue> kvRef = new AtomicReference<>(); |
| 58 | + private final ConcurrentHashMap<String, QueueConfig> cache = new ConcurrentHashMap<>(); |
| 59 | + |
| 60 | + public NatsRqueueSystemConfigDao(Connection connection) throws IOException { |
| 61 | + this.connection = connection; |
| 62 | + this.kvm = connection.keyValueManagement(); |
| 63 | + } |
| 64 | + |
| 65 | + private KeyValue ensureBucket() throws IOException, JetStreamApiException { |
| 66 | + KeyValue cached = kvRef.get(); |
| 67 | + if (cached != null) { |
| 68 | + return cached; |
| 69 | + } |
| 70 | + synchronized (this) { |
| 71 | + cached = kvRef.get(); |
| 72 | + if (cached != null) { |
| 73 | + return cached; |
| 74 | + } |
| 75 | + try { |
| 76 | + KeyValueStatus status = kvm.getStatus(BUCKET_NAME); |
| 77 | + if (status != null) { |
| 78 | + KeyValue kv = connection.keyValue(BUCKET_NAME); |
| 79 | + kvRef.set(kv); |
| 80 | + return kv; |
| 81 | + } |
| 82 | + } catch (JetStreamApiException missing) { |
| 83 | + // fall through to create |
| 84 | + } |
| 85 | + kvm.create(KeyValueConfiguration.builder().name(BUCKET_NAME).build()); |
| 86 | + KeyValue kv = connection.keyValue(BUCKET_NAME); |
| 87 | + kvRef.set(kv); |
| 88 | + return kv; |
| 89 | + } |
29 | 90 | } |
30 | 91 |
|
31 | 92 | @Override |
32 | | - public List<QueueConfig> getConfigByNames(Collection<String> names) { |
33 | | - return Collections.emptyList(); |
| 93 | + public QueueConfig getConfigByName(String name) { |
| 94 | + return getConfigByName(name, true); |
34 | 95 | } |
35 | 96 |
|
36 | 97 | @Override |
37 | 98 | public QueueConfig getConfigByName(String name, boolean cached) { |
38 | | - return null; |
| 99 | + if (cached) { |
| 100 | + QueueConfig hit = cache.get(name); |
| 101 | + if (hit != null) { |
| 102 | + return hit; |
| 103 | + } |
| 104 | + } |
| 105 | + QueueConfig loaded = loadByKey(sanitize(name)); |
| 106 | + if (loaded != null) { |
| 107 | + cache.put(name, loaded); |
| 108 | + } |
| 109 | + return loaded; |
39 | 110 | } |
40 | 111 |
|
41 | 112 | @Override |
42 | 113 | public QueueConfig getQConfig(String id, boolean cached) { |
43 | | - return null; |
| 114 | + if (cached) { |
| 115 | + for (QueueConfig hit : cache.values()) { |
| 116 | + if (id != null && id.equals(hit.getId())) { |
| 117 | + return hit; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return scanForId(id); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public List<QueueConfig> getConfigByNames(Collection<String> names) { |
| 126 | + List<QueueConfig> out = new ArrayList<>(names.size()); |
| 127 | + for (String n : names) { |
| 128 | + QueueConfig c = getConfigByName(n); |
| 129 | + if (c != null) { |
| 130 | + out.add(c); |
| 131 | + } |
| 132 | + } |
| 133 | + return out; |
44 | 134 | } |
45 | 135 |
|
46 | 136 | @Override |
47 | 137 | public List<QueueConfig> findAllQConfig(Collection<String> ids) { |
48 | | - return Collections.emptyList(); |
| 138 | + List<QueueConfig> out = new ArrayList<>(ids.size()); |
| 139 | + for (String id : ids) { |
| 140 | + QueueConfig c = getQConfig(id, true); |
| 141 | + if (c != null) { |
| 142 | + out.add(c); |
| 143 | + } |
| 144 | + } |
| 145 | + return out; |
49 | 146 | } |
50 | 147 |
|
51 | 148 | @Override |
52 | | - public void saveQConfig(QueueConfig queueConfig) {} |
| 149 | + public void saveQConfig(QueueConfig queueConfig) { |
| 150 | + try { |
| 151 | + KeyValue kv = ensureBucket(); |
| 152 | + kv.put(sanitize(queueConfig.getName()), serialize(queueConfig)); |
| 153 | + cache.put(queueConfig.getName(), queueConfig); |
| 154 | + } catch (IOException | JetStreamApiException e) { |
| 155 | + log.log(Level.WARNING, "saveQConfig " + queueConfig.getName() + " failed", e); |
| 156 | + } |
| 157 | + } |
53 | 158 |
|
54 | 159 | @Override |
55 | | - public void saveAllQConfig(List<QueueConfig> newConfigs) {} |
| 160 | + public void saveAllQConfig(List<QueueConfig> newConfigs) { |
| 161 | + for (QueueConfig c : newConfigs) { |
| 162 | + saveQConfig(c); |
| 163 | + } |
| 164 | + } |
56 | 165 |
|
57 | 166 | @Override |
58 | | - public void clearCacheByName(String name) {} |
| 167 | + public void clearCacheByName(String name) { |
| 168 | + cache.remove(name); |
| 169 | + } |
| 170 | + |
| 171 | + // ---- helpers ---------------------------------------------------------- |
| 172 | + |
| 173 | + private QueueConfig loadByKey(String key) { |
| 174 | + try { |
| 175 | + KeyValue kv = ensureBucket(); |
| 176 | + KeyValueEntry entry = kv.get(key); |
| 177 | + if (entry == null || entry.getValue() == null) { |
| 178 | + return null; |
| 179 | + } |
| 180 | + return deserialize(entry.getValue()); |
| 181 | + } catch (IOException | JetStreamApiException e) { |
| 182 | + log.log(Level.WARNING, "loadByKey " + key + " failed", e); |
| 183 | + return null; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private QueueConfig scanForId(String id) { |
| 188 | + if (id == null) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + try { |
| 192 | + KeyValue kv = ensureBucket(); |
| 193 | + List<String> keys = new ArrayList<>(kv.keys()); |
| 194 | + for (String k : keys) { |
| 195 | + QueueConfig c = loadByKey(k); |
| 196 | + if (c != null && id.equals(c.getId())) { |
| 197 | + return c; |
| 198 | + } |
| 199 | + } |
| 200 | + return null; |
| 201 | + } catch (IOException | JetStreamApiException | InterruptedException e) { |
| 202 | + log.log(Level.WARNING, "scanForId " + id + " failed", e); |
| 203 | + if (e instanceof InterruptedException) { |
| 204 | + Thread.currentThread().interrupt(); |
| 205 | + } |
| 206 | + return null; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private static byte[] serialize(QueueConfig c) throws IOException { |
| 211 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 212 | + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { |
| 213 | + oos.writeObject(c); |
| 214 | + } |
| 215 | + return baos.toByteArray(); |
| 216 | + } |
| 217 | + |
| 218 | + private static QueueConfig deserialize(byte[] bytes) { |
| 219 | + try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { |
| 220 | + Object o = ois.readObject(); |
| 221 | + return o instanceof QueueConfig ? (QueueConfig) o : null; |
| 222 | + } catch (IOException | ClassNotFoundException e) { |
| 223 | + log.log(Level.WARNING, "deserialize QueueConfig failed", e); |
| 224 | + return null; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + /** KV keys allow {@code [A-Za-z0-9_=.-]} only. */ |
| 229 | + private static String sanitize(String key) { |
| 230 | + return key == null ? "_" : key.replaceAll("[^A-Za-z0-9_=.-]", "_"); |
| 231 | + } |
59 | 232 | } |
0 commit comments