Skip to content

Commit 00e5ef0

Browse files
committed
feat: store the status of annotation signal in the PersistentVolume
1 parent c6879b9 commit 00e5ef0

1 file changed

Lines changed: 44 additions & 39 deletions

File tree

stackgres-k8s/src/stream/src/main/java/io/stackgres/stream/jobs/source/DebeziumAnnotationSignalChannelReader.java

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.stackgres.stream.jobs.source;
77

8-
import java.io.IOException;
98
import java.io.InputStream;
109
import java.io.OutputStream;
1110
import java.nio.file.Files;
@@ -17,6 +16,7 @@
1716
import java.util.Properties;
1817
import java.util.Set;
1918

19+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2020
import io.debezium.config.CommonConnectorConfig;
2121
import io.debezium.pipeline.signal.SignalRecord;
2222
import io.debezium.pipeline.signal.channels.SignalChannelReader;
@@ -27,9 +27,13 @@
2727
import io.stackgres.common.crd.sgstream.StackGresStream;
2828
import io.stackgres.common.resource.CustomResourceFinder;
2929
import io.stackgres.stream.app.StreamProperty;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
3032

3133
public class DebeziumAnnotationSignalChannelReader implements SignalChannelReader {
3234

35+
private static final Logger LOGGER = LoggerFactory.getLogger(DebeziumAnnotationSignalChannelReader.class);
36+
3337
private static final String STACKGRES_IO_DEBEZIUM_SIGNAL_KEY_PREFIX =
3438
"debezium-signal." + StackGresContext.STACKGRES_KEY_PREFIX;
3539

@@ -52,52 +56,53 @@ public String name() {
5256
public void init(CommonConnectorConfig connectorConfig) {
5357
this.streamName = StreamProperty.STREAM_NAME.getString();
5458
this.streamNamespace = StreamProperty.STREAM_NAMESPACE.getString();
55-
this.annotationSignalPropertiesPath = Paths.get(StreamPath.DEBEZIUM_DATABASE_HISTORY_PATH.path());
59+
this.annotationSignalPropertiesPath = Paths.get(StreamPath.DEBEZIUM_ANNOTATION_SIGNAL_PATH.path());
5660
}
5761

62+
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION",
63+
justification = "Wanted behavior since executed directly from a thread")
5864
@Override
5965
public List<SignalRecord> read() {
60-
Properties annotationSignalProperties = new Properties();
61-
if (Files.exists(annotationSignalPropertiesPath)) {
62-
try (InputStream inputStream = Files.newInputStream(annotationSignalPropertiesPath)) {
63-
annotationSignalProperties.load(inputStream);
64-
} catch (IOException ex) {
65-
throw new RuntimeException(ex);
66+
try {
67+
Properties annotationSignalProperties = new Properties();
68+
if (Files.exists(annotationSignalPropertiesPath)) {
69+
try (InputStream inputStream = Files.newInputStream(annotationSignalPropertiesPath)) {
70+
annotationSignalProperties.load(inputStream);
71+
}
6672
}
67-
}
68-
final var streamSignalAnnotations =
69-
streamFinder.findByNameAndNamespace(streamName, streamNamespace)
70-
.map(HasMetadata::getMetadata)
71-
.map(ObjectMeta::getAnnotations)
72-
.stream()
73-
.map(Map::entrySet)
74-
.flatMap(Set::stream)
75-
.filter(annotation -> annotation.getKey().startsWith(STACKGRES_IO_DEBEZIUM_SIGNAL_KEY_PREFIX))
76-
.filter(annotation -> Optional
77-
.ofNullable(annotationSignalProperties.getProperty(annotation.getKey()))
78-
.map(value -> !annotation.getValue().equals(value))
79-
.orElse(true));
80-
streamSignalAnnotations.forEach(annotation -> annotationSignalProperties
81-
.setProperty(annotation.getKey(), annotation.getValue()));
82-
if (!Files.exists(annotationSignalPropertiesPath)) {
83-
try {
73+
final var streamSignalAnnotations =
74+
streamFinder.findByNameAndNamespace(streamName, streamNamespace)
75+
.map(HasMetadata::getMetadata)
76+
.map(ObjectMeta::getAnnotations)
77+
.stream()
78+
.map(Map::entrySet)
79+
.flatMap(Set::stream)
80+
.filter(annotation -> annotation.getKey().startsWith(STACKGRES_IO_DEBEZIUM_SIGNAL_KEY_PREFIX))
81+
.filter(annotation -> Optional
82+
.ofNullable(annotationSignalProperties.getProperty(annotation.getKey()))
83+
.map(value -> !annotation.getValue().equals(value))
84+
.orElse(true))
85+
.toList();
86+
streamSignalAnnotations.forEach(annotation -> annotationSignalProperties
87+
.setProperty(annotation.getKey(), annotation.getValue()));
88+
if (!Files.exists(annotationSignalPropertiesPath)) {
8489
Files.createFile(annotationSignalPropertiesPath);
85-
} catch (IOException ex) {
86-
throw new RuntimeException(ex);
8790
}
91+
try (OutputStream outputStream = Files.newOutputStream(annotationSignalPropertiesPath)) {
92+
annotationSignalProperties.store(outputStream, null);
93+
}
94+
return streamSignalAnnotations
95+
.stream()
96+
.map(annotation -> new SignalRecord(
97+
annotation.getKey(),
98+
extractType(annotation.getKey()),
99+
annotation.getValue(),
100+
Map.of()))
101+
.toList();
102+
} catch (Exception ex) {
103+
LOGGER.error("An error ocured while loading SGStream annotations: {}", ex.getMessage(), ex);
104+
return List.of();
88105
}
89-
try (OutputStream outputStream = Files.newOutputStream(annotationSignalPropertiesPath)) {
90-
annotationSignalProperties.store(outputStream, null);
91-
} catch (IOException ex) {
92-
throw new RuntimeException(ex);
93-
}
94-
return streamSignalAnnotations
95-
.map(annotation -> new SignalRecord(
96-
annotation.getKey(),
97-
extractType(annotation.getKey()),
98-
annotation.getValue(),
99-
Map.of()))
100-
.toList();
101106
}
102107

103108
private String extractType(String key) {

0 commit comments

Comments
 (0)