Skip to content

proof of concept for message durability #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions ARQUITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
```mermaid

sequenceDiagram
box Data Consolidation
participant ConsolidationService
end

participant SegmentClient
box HTTP
participant QueueHttp
participant LooperHttp
participant SegmentAPI
end
box File
participant QueueFile
participant WriteFile
participant File
participant WatchFile
end

activate ConsolidationService
ConsolidationService->>+SegmentClient: enqueue
SegmentClient<<->>QueueHttp: offer
alt QueueHttp overflow
SegmentClient<<->>QueueFile: put
end
SegmentClient->>-ConsolidationService:
deactivate ConsolidationService

loop consume QueueHttp
LooperHttp->>QueueHttp:take
activate LooperHttp
end
LooperHttp->>SegmentAPI: batchUpload
note over LooperHttp,SegmentAPI: Batch
note over LooperHttp,SegmentAPI: CircuitBreaker and Retry
note over LooperHttp,SegmentAPI: HTTP requests submited to a pool
deactivate LooperHttp

alt retry exhausted or circuit open
note over LooperHttp: pool threads
LooperHttp->>QueueFile: put
end

loop consume QueueFile
WriteFile->>QueueFile:take
activate WriteFile
end
WriteFile->>File: write
note over WriteFile: Batch and save file
deactivate WriteFile

note over WatchFile: check last written
activate WatchFile
loop watch QueueFile
WatchFile->>File: read and remove
WatchFile->>QueueHttp: offer
end
deactivate WatchFile
```
30 changes: 27 additions & 3 deletions analytics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@
<artifactId>findbugs</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.segment.backo</groupId>
<artifactId>backo</artifactId>
<groupId>dev.failsafe</groupId>
<artifactId>failsafe</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>dev.failsafe</groupId>
<artifactId>failsafe-retrofit</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -67,7 +79,19 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
67 changes: 11 additions & 56 deletions analytics/src/main/java/com/segment/analytics/Analytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ public boolean offer(MessageBuilder builder) {
return client.offer(message);
}

/** Flush events in the message queue. */
public void flush() {
client.flush();
}

Comment on lines -93 to -97

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Looks to be a breaking change. Could we implement the flush method based on the new architecture? or at least depreciate it with empty or minimal implementation (warning log)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is misleading. Flushing does not guarantee that messages will be delivered.
IMHO the API for segment should be a simple, "hey, there is the message, do your best"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point. I'm just thinking about binary compatibility, the upgrade should ideally not require any changes from users.

/** Stops this instance from processing further requests. */
public void shutdown() {
client.shutdown();
Expand Down Expand Up @@ -144,9 +139,7 @@ public static class Builder {
private ThreadFactory threadFactory;
private int flushQueueSize;
private int maximumFlushAttempts;
private int maximumQueueSizeInBytes;
private long flushIntervalInMillis;
private List<Callback> callbacks;
private int queueCapacity;
private boolean forceTlsV1 = false;
private GsonBuilder gsonBuilder;
Expand Down Expand Up @@ -273,17 +266,6 @@ public Builder flushQueueSize(int flushQueueSize) {
return this;
}

/** Set the queueSize at which flushes should be triggered. */
@Beta
public Builder maximumQueueSizeInBytes(int bytes) {
if (bytes < 1) {
throw new IllegalArgumentException("maximumQueueSizeInBytes must not be less than 1.");
}

this.maximumQueueSizeInBytes = bytes;
return this;
}

/** Set the interval at which the queue should be flushed. */
@Beta
public Builder flushInterval(long flushInterval, TimeUnit unit) {
Expand Down Expand Up @@ -323,21 +305,6 @@ public Builder threadFactory(ThreadFactory threadFactory) {
return this;
}

/** Add a {@link Callback} to be notified when an event is processed. */
public Builder callback(Callback callback) {
if (callback == null) {
throw new NullPointerException("Null callback");
}
if (callbacks == null) {
callbacks = new ArrayList<>();
}
if (callbacks.contains(callback)) {
throw new IllegalStateException("Callback is already registered.");
}
callbacks.add(callback);
return this;
}

/** Use a {@link Plugin} to configure the builder. */
@Beta
public Builder plugin(Plugin plugin) {
Expand Down Expand Up @@ -390,9 +357,6 @@ public Analytics build() {
if (flushQueueSize == 0) {
flushQueueSize = Platform.get().defaultFlushQueueSize();
}
if (maximumQueueSizeInBytes == 0) {
maximumQueueSizeInBytes = MESSAGE_QUEUE_MAX_BYTE_SIZE;
}
if (maximumFlushAttempts == 0) {
maximumFlushAttempts = 3;
}
Expand All @@ -412,11 +376,6 @@ public Analytics build() {
if (threadFactory == null) {
threadFactory = Platform.get().defaultThreadFactory();
}
if (callbacks == null) {
callbacks = Collections.emptyList();
} else {
callbacks = Collections.unmodifiableList(callbacks);
}

HttpLoggingInterceptor interceptor =
new HttpLoggingInterceptor(
Expand Down Expand Up @@ -456,21 +415,17 @@ public void log(String message) {

SegmentService segmentService = restAdapter.create(SegmentService.class);

AnalyticsClient analyticsClient =
AnalyticsClient.create(
endpoint,
segmentService,
queueCapacity,
flushQueueSize,
flushIntervalInMillis,
maximumFlushAttempts,
maximumQueueSizeInBytes,
log,
threadFactory,
networkExecutor,
callbacks,
writeKey,
gson);
AnalyticsClient analyticsClient = AnalyticsClient.create(
endpoint,
segmentService,
queueCapacity,
flushQueueSize,
flushIntervalInMillis,
log,
threadFactory,
networkExecutor,
writeKey,
gson);

return new Analytics(analyticsClient, messageTransformers, messageInterceptors, log);
}
Expand Down
Loading