/
KafkaClientActor.java
207 lines (179 loc) · 8.64 KB
/
KafkaClientActor.java
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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.connectivity.messaging.kafka;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.annotation.Nullable;
import org.eclipse.ditto.model.connectivity.Connection;
import org.eclipse.ditto.model.connectivity.ConnectivityStatus;
import org.eclipse.ditto.services.connectivity.messaging.BaseClientActor;
import org.eclipse.ditto.services.connectivity.messaging.BaseClientData;
import org.eclipse.ditto.services.connectivity.messaging.BaseClientState;
import org.eclipse.ditto.services.connectivity.messaging.config.ConnectionConfig;
import org.eclipse.ditto.services.connectivity.messaging.config.KafkaConfig;
import org.eclipse.ditto.services.connectivity.messaging.internal.ClientConnected;
import org.eclipse.ditto.services.connectivity.messaging.internal.ClientDisconnected;
import org.eclipse.ditto.services.connectivity.messaging.internal.ConnectionFailure;
import org.eclipse.ditto.services.connectivity.messaging.internal.ImmutableConnectionFailure;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.Status;
import akka.japi.pf.FSMStateFunctionBuilder;
/**
* Actor which handles connection to Kafka server.
*/
public final class KafkaClientActor extends BaseClientActor {
private final KafkaPublisherActorFactory publisherActorFactory;
private ActorRef kafkaPublisherActor;
private final Set<ActorRef> pendingStatusReportsFromStreams;
private final KafkaConnectionFactory connectionFactory;
private CompletableFuture<Status.Status> testConnectionFuture = null;
@SuppressWarnings("unused") // used by `props` via reflection
private KafkaClientActor(final Connection connection,
final ConnectivityStatus desiredConnectionStatus,
final ActorRef conciergeForwarder,
final KafkaPublisherActorFactory factory) {
super(connection, desiredConnectionStatus, conciergeForwarder);
final ConnectionConfig connectionConfig = connectivityConfig.getConnectionConfig();
final KafkaConfig kafkaConfig = connectionConfig.getKafkaConfig();
connectionFactory = DefaultKafkaConnectionFactory.getInstance(connection, kafkaConfig);
publisherActorFactory = factory;
pendingStatusReportsFromStreams = new HashSet<>();
}
/**
* Creates Akka configuration object for this actor.
*
* @param connection the connection.
* @param conciergeForwarder the actor used to send signals to the concierge service.
* @return the Akka configuration Props object.
*/
public static Props props(final Connection connection,
final ActorRef conciergeForwarder,
final KafkaPublisherActorFactory factory) {
return Props.create(KafkaClientActor.class, validateConnection(connection), connection.getConnectionStatus(),
conciergeForwarder, factory);
}
private static Connection validateConnection(final Connection connection) {
// nothing to do so far
return connection;
}
@Override
protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inTestingState() {
return super.inTestingState()
.event(Status.Status.class, (e, d) -> !Objects.equals(getSender(), getSelf()),
(status, data) -> handleStatusReportFromChildren(status))
.event(ClientConnected.class, BaseClientData.class, (event, data) -> {
final String url = data.getConnection().getUri();
final String message = "Kafka connection to " + url + " established successfully";
completeTestConnectionFuture(new Status.Success(message));
return stay();
})
.event(ConnectionFailure.class, BaseClientData.class, (event, data) -> {
completeTestConnectionFuture(new Status.Failure(event.getFailure().cause()));
return stay();
});
}
@Override
protected FSMStateFunctionBuilder<BaseClientState, BaseClientData> inConnectingState() {
return super.inConnectingState()
.event(Status.Status.class, (status, data) -> handleStatusReportFromChildren(status));
}
@Override
protected CompletionStage<Status.Status> doTestConnection(final Connection connection) {
if (testConnectionFuture != null) {
final Exception error =
new IllegalStateException("Can't test new connection since a test is already running.");
return CompletableFuture.completedFuture(new Status.Failure(error));
}
testConnectionFuture = new CompletableFuture<>();
connectClient(true);
return testConnectionFuture;
}
@Override
protected void allocateResourcesOnConnection(final ClientConnected clientConnected) {
// nothing to do here; publisher and consumers started already.
}
@Override
protected ActorRef getPublisherActor() {
return kafkaPublisherActor;
}
@Override
protected void doConnectClient(final Connection connection, @Nullable final ActorRef origin) {
connectClient(false);
}
@Override
protected void doDisconnectClient(final Connection connection, @Nullable final ActorRef origin) {
self().tell((ClientDisconnected) () -> null, origin);
}
/**
* Start Kafka publishers, expect "Status.Success" from each of them, then send "ClientConnected" to self.
*
* @param dryRun if set to true, exchange no message between the broker and the Ditto cluster.
*/
private void connectClient(final boolean dryRun) {
// start publisher
startKafkaPublisher(dryRun);
startMessageMappingProcessorActor();
// no command consumers as we don't support consuming from sources yet
}
private void startKafkaPublisher(final boolean dryRun) {
log.info("Starting Kafka publisher actor.");
// ensure no previous publisher stays in memory
stopKafkaPublisher();
kafkaPublisherActor = startChildActorConflictFree(publisherActorFactory.getActorName(),
publisherActorFactory.props(connectionId(), getTargetsOrEmptyList(), connectionFactory, getSelf(),
dryRun));
pendingStatusReportsFromStreams.add(kafkaPublisherActor);
}
@Override
protected void cleanupResourcesForConnection() {
pendingStatusReportsFromStreams.clear();
stopKafkaPublisher();
}
private void stopKafkaPublisher() {
if (kafkaPublisherActor != null) {
log.debug("Stopping child actor <{}>.", kafkaPublisherActor.path());
// shutdown using a message, so the actor can clean up first
kafkaPublisherActor.tell(KafkaPublisherActor.GracefulStop.INSTANCE, getSelf());
kafkaPublisherActor = null;
}
}
private State<BaseClientState, BaseClientData> handleStatusReportFromChildren(final Status.Status status) {
if (pendingStatusReportsFromStreams.contains(getSender())) {
pendingStatusReportsFromStreams.remove(getSender());
if (status instanceof Status.Failure) {
final Status.Failure failure = (Status.Failure) status;
final ConnectionFailure connectionFailure =
new ImmutableConnectionFailure(null, failure.cause(), "child failed");
getSelf().tell(connectionFailure, ActorRef.noSender());
} else if (pendingStatusReportsFromStreams.isEmpty()) {
// all children are ready; this client actor is connected.
getSelf().tell((ClientConnected) () -> null, ActorRef.noSender());
}
}
return stay();
}
private void completeTestConnectionFuture(final Status.Status testResult) {
if (testConnectionFuture != null) {
testConnectionFuture.complete(testResult);
} else {
// no future; test failed.
final Exception exception = new IllegalStateException(
"Could not complete testing connection since the test was already completed or wasn't started.");
getSelf().tell(new Status.Failure(exception), getSelf());
}
}
}