Skip to content

Commit 65a4f28

Browse files
David O'SullivanZPascal
authored andcommitted
logcache fix
1 parent 1afe7df commit 65a4f28

6 files changed

Lines changed: 61 additions & 39 deletions

File tree

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/_DefaultCloudFoundryOperations.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.cloudfoundry.client.v3.spaces.ListSpacesRequest;
2424
import org.cloudfoundry.client.v3.spaces.SpaceResource;
2525
import org.cloudfoundry.doppler.DopplerClient;
26+
import org.cloudfoundry.logcache.v1.LogCacheClient;
2627
import org.cloudfoundry.networking.NetworkingClient;
2728
import org.cloudfoundry.operations.advanced.Advanced;
2829
import org.cloudfoundry.operations.advanced.DefaultAdvanced;
@@ -79,7 +80,7 @@ public Advanced advanced() {
7980
@Override
8081
@Value.Derived
8182
public Applications applications() {
82-
return new DefaultApplications(getCloudFoundryClientPublisher(), getDopplerClientPublisher(), getSpaceId());
83+
return new DefaultApplications(getCloudFoundryClientPublisher(), getDopplerClientPublisher(), getLogCacheClientPublisher(), getSpaceId());
8384
}
8485

8586
@Override
@@ -190,13 +191,26 @@ Mono<CloudFoundryClient> getCloudFoundryClientPublisher() {
190191
@Nullable
191192
abstract DopplerClient getDopplerClient();
192193

194+
/**
195+
* The {@link LogCacheClient} to use for operations functionality
196+
*/
197+
@Nullable
198+
abstract LogCacheClient getLogCacheClient();
199+
193200
@Value.Derived
194201
Mono<DopplerClient> getDopplerClientPublisher() {
195202
return Optional.ofNullable(getDopplerClient())
196203
.map(Mono::just)
197204
.orElse(Mono.error(new IllegalStateException("DopplerClient must be set")));
198205
}
199206

207+
@Value.Derived
208+
Mono<LogCacheClient> getLogCacheClientPublisher() {
209+
return Optional.ofNullable(getLogCacheClient())
210+
.map(Mono::just)
211+
.orElse(Mono.error(new IllegalStateException("LogCacheClient must be set")));
212+
}
213+
200214
/**
201215
* The {@link NetworkingClient} to use for operations functionality
202216
*/

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/Applications.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.cloudfoundry.operations.applications;
1818

1919
import org.cloudfoundry.doppler.LogMessage;
20+
import org.cloudfoundry.logcache.v1.Log;
2021
import reactor.core.publisher.Flux;
2122
import reactor.core.publisher.Mono;
2223

@@ -124,7 +125,7 @@ public interface Applications {
124125
* @deprecated Use {@link #logs(ApplicationLogsRequest)} instead.
125126
*/
126127
@Deprecated
127-
Flux<LogMessage> logs(LogsRequest request);
128+
Flux<Log> logs(LogsRequest request);
128129

129130
/**
130131
* List the applications logs.

cloudfoundry-operations/src/main/java/org/cloudfoundry/operations/applications/DefaultApplications.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
import org.cloudfoundry.doppler.RecentLogsRequest;
157157
import org.cloudfoundry.doppler.StreamRequest;
158158
import org.cloudfoundry.logcache.v1.EnvelopeType;
159+
import org.cloudfoundry.logcache.v1.Log;
159160
import org.cloudfoundry.logcache.v1.LogCacheClient;
160161
import org.cloudfoundry.logcache.v1.ReadRequest;
161162
import org.cloudfoundry.operations.util.OperationsLogging;
@@ -221,29 +222,34 @@ public final class DefaultApplications implements Applications {
221222

222223
private final Mono<DopplerClient> dopplerClient;
223224

225+
private final Mono<LogCacheClient> logCacheClient;
226+
224227
private final RandomWords randomWords;
225228

226229
private final Mono<String> spaceId;
227230

228231
public DefaultApplications(
229232
Mono<CloudFoundryClient> cloudFoundryClient,
230233
Mono<DopplerClient> dopplerClient,
234+
Mono<LogCacheClient> logCacheClient,
231235
Mono<String> spaceId) {
232-
this(cloudFoundryClient, dopplerClient, new WordListRandomWords(), spaceId);
236+
this(cloudFoundryClient, dopplerClient, logCacheClient, new WordListRandomWords(), spaceId);
233237
}
234238

235239
DefaultApplications(
236240
Mono<CloudFoundryClient> cloudFoundryClient,
237241
Mono<DopplerClient> dopplerClient,
242+
Mono<LogCacheClient> logCacheClient,
238243
RandomWords randomWords,
239244
Mono<String> spaceId) {
240245
this.cloudFoundryClient = cloudFoundryClient;
241246
this.dopplerClient = dopplerClient;
247+
this.logCacheClient = logCacheClient;
242248
this.randomWords = randomWords;
243249
this.spaceId = spaceId;
244250
}
245251

246-
@Override
252+
@Override
247253
public Mono<Void> copySource(CopySourceApplicationRequest request) {
248254
return Mono.zip(this.cloudFoundryClient, this.spaceId)
249255
.flatMap(
@@ -537,7 +543,7 @@ public Flux<Task> listTasks(ListApplicationTasksRequest request) {
537543
}
538544

539545
@Override
540-
public Flux<LogMessage> logs(LogsRequest request) {
546+
public Flux<Log> logs(LogsRequest request) {
541547
return Mono.zip(this.cloudFoundryClient, this.spaceId)
542548
.flatMap(
543549
function(
@@ -546,7 +552,7 @@ public Flux<LogMessage> logs(LogsRequest request) {
546552
cloudFoundryClient, request.getName(), spaceId)))
547553
.flatMapMany(
548554
applicationId ->
549-
getLogs(this.dopplerClient, applicationId, request.getRecent()))
555+
getRecentLogs(this.logCacheClient, applicationId))
550556
.transform(OperationsLogging.log("Get Application Logs"))
551557
.checkpoint();
552558
}
@@ -1607,30 +1613,29 @@ private static int getInstances(AbstractApplicationResource resource) {
16071613
.orElse(0);
16081614
}
16091615

1610-
private static Flux<LogMessage> getLogs(
1611-
Mono<DopplerClient> dopplerClient, String applicationId, Boolean recent) {
1616+
/* private static Flux<Log> getLogs(
1617+
Mono<LogCacheClient> logCacheClient, String applicationId, Boolean recent) {
16121618
if (Optional.ofNullable(recent).orElse(false)) {
1613-
return requestLogsRecent(dopplerClient, applicationId)
1614-
.filter(e -> EventType.LOG_MESSAGE == e.getEventType())
1615-
.map(Envelope::getLogMessage)
1616-
.collectSortedList(LOG_MESSAGE_COMPARATOR)
1617-
.flatMapIterable(d -> d);
1618-
} else {
1619-
return requestLogsStream(dopplerClient, applicationId)
1620-
.filter(e -> EventType.LOG_MESSAGE == e.getEventType())
1621-
.map(Envelope::getLogMessage)
1622-
.transformDeferred(
1623-
SortingUtils.timespan(LOG_MESSAGE_COMPARATOR, LOG_MESSAGE_TIMESPAN));
1619+
return getRecentLogs(logCacheClient, applicationId);
16241620
}
1621+
}*/
1622+
1623+
private static Flux<Log> getRecentLogs(Mono<LogCacheClient> logCacheClient, String applicationId) {
1624+
return requestLogsRecentLogCache(logCacheClient, applicationId)
1625+
.filter(e -> EnvelopeType.LOG.getValue().equals(e.getLog().getType().getValue()))
1626+
// .collectSortedList(LOG_MESSAGE_COMPARATOR_LOG_CACHE)
1627+
.sort(LOG_MESSAGE_COMPARATOR_LOG_CACHE)
1628+
.map(org.cloudfoundry.logcache.v1.Envelope::getLog);
16251629
}
16261630

1627-
private static Flux<LogMessage> getRecentLogs(Mono<LogCacheClient> logCacheClient, String applicationId) {
1631+
/* private static Flux<org.cloudfoundry.logcache.v1.Log> getRecentLogs(Mono<LogCacheClient> logCacheClient, String applicationId) {
16281632
return requestLogsRecentLogCache(logCacheClient, applicationId)
16291633
.filter(e -> EnvelopeType.LOG.getValue().equals(e.getLog().getType().getValue()))
1634+
.sort(LOG_MESSAGE_COMPARATOR_LOG_CACHE)
16301635
.map(org.cloudfoundry.logcache.v1.Envelope::getLog)
1631-
.collectSortedList(LOG_MESSAGE_COMPARATOR_LOG_CACHE)
1632-
.flatMapIterable(d -> d);
1633-
}
1636+
.collectList()
1637+
.flatMapIterable(d1 -> d1).cast(org.cloudfoundry.logcache.v1.Log.class);
1638+
} */
16341639

16351640
@SuppressWarnings("unchecked")
16361641
private static Map<String, Object> getMetadataRequest(EventEntity entity) {

cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/AbstractOperationsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.cloudfoundry.client.v3.stacks.StacksV3;
5454
import org.cloudfoundry.client.v3.tasks.Tasks;
5555
import org.cloudfoundry.doppler.DopplerClient;
56+
import org.cloudfoundry.logcache.v1.LogCacheClient;
5657
import org.cloudfoundry.routing.RoutingClient;
5758
import org.cloudfoundry.routing.v1.routergroups.RouterGroups;
5859
import org.cloudfoundry.uaa.UaaClient;
@@ -104,6 +105,8 @@ public abstract class AbstractOperationsTest {
104105

105106
protected final DopplerClient dopplerClient = mock(DopplerClient.class, RETURNS_SMART_NULLS);
106107

108+
protected final LogCacheClient logCacheClient = mock(LogCacheClient.class, RETURNS_SMART_NULLS);
109+
107110
protected final Events events = mock(Events.class, RETURNS_SMART_NULLS);
108111

109112
protected final FeatureFlags featureFlags = mock(FeatureFlags.class, RETURNS_SMART_NULLS);

cloudfoundry-operations/src/test/java/org/cloudfoundry/operations/applications/DefaultApplicationsTest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@
144144
import org.cloudfoundry.doppler.LogMessage;
145145
import org.cloudfoundry.doppler.RecentLogsRequest;
146146
import org.cloudfoundry.doppler.StreamRequest;
147+
import org.cloudfoundry.logcache.v1.Log;
148+
import org.cloudfoundry.logcache.v1.LogCacheClient;
149+
import org.cloudfoundry.logcache.v1.ReadRequest;
150+
import org.cloudfoundry.logcache.v1.ReadResponse;
147151
import org.cloudfoundry.operations.AbstractOperationsTest;
148152
import org.cloudfoundry.util.DateUtils;
149153
import org.cloudfoundry.util.FluentMap;
@@ -163,6 +167,7 @@ final class DefaultApplicationsTest extends AbstractOperationsTest {
163167
new DefaultApplications(
164168
Mono.just(this.cloudFoundryClient),
165169
Mono.just(this.dopplerClient),
170+
Mono.just(this.logCacheClient),
166171
this.randomWords,
167172
Mono.just(TEST_SPACE_ID));
168173

@@ -1318,7 +1323,7 @@ void logs() {
13181323
this.applications
13191324
.logs(LogsRequest.builder().name("test-application-name").recent(false).build())
13201325
.as(StepVerifier::create)
1321-
.expectNext(fill(LogMessage.builder(), "log-message-").build())
1326+
.expectNext(fill(Log.builder(), "log-message-").build())
13221327
.expectComplete()
13231328
.verify(Duration.ofSeconds(5));
13241329
}
@@ -1346,12 +1351,12 @@ void logsRecent() {
13461351
"test-application-name",
13471352
TEST_SPACE_ID,
13481353
"test-metadata-id");
1349-
requestLogsRecent(this.dopplerClient, "test-metadata-id");
1354+
requestLogsRecentLogCache(this.logCacheClient, "test-metadata-id");
13501355

13511356
this.applications
13521357
.logs(LogsRequest.builder().name("test-application-name").recent(true).build())
13531358
.as(StepVerifier::create)
1354-
.expectNext(fill(LogMessage.builder(), "log-message-").build())
1359+
.expectNext(fill(Log.builder(), "log-message-").build())
13551360
.expectComplete()
13561361
.verify(Duration.ofSeconds(5));
13571362
}
@@ -1368,7 +1373,7 @@ void logsRecentNotSet() {
13681373
this.applications
13691374
.logs(LogsRequest.builder().name("test-application-name").build())
13701375
.as(StepVerifier::create)
1371-
.expectNext(fill(LogMessage.builder(), "log-message-").build())
1376+
.expectNext(fill(Log.builder(), "log-message-").build())
13721377
.expectComplete()
13731378
.verify(Duration.ofSeconds(5));
13741379
}
@@ -5317,17 +5322,11 @@ private static void requestListTasksEmpty(
53175322
.build()));
53185323
}
53195324

5320-
private static void requestLogsRecent(DopplerClient dopplerClient, String applicationId) {
5321-
when(dopplerClient.recentLogs(
5322-
RecentLogsRequest.builder().applicationId(applicationId).build()))
5325+
private static void requestLogsRecentLogCache(LogCacheClient logCacheClient, String applicationId) {
5326+
when(logCacheClient.recentLogs(
5327+
ReadRequest.builder().sourceId(applicationId).build()))
53235328
.thenReturn(
5324-
Flux.just(
5325-
Envelope.builder()
5326-
.eventType(EventType.LOG_MESSAGE)
5327-
.logMessage(
5328-
fill(LogMessage.builder(), "log-message-").build())
5329-
.origin("rsp")
5330-
.build()));
5329+
Mono.just(ReadResponse.builder().envelopes(fill(org.cloudfoundry.logcache.v1.EnvelopeBatch.builder()).build()).build()));
53315330
}
53325331

53335332
private static void requestLogsStream(DopplerClient dopplerClient, String applicationId) {

integration-test/src/test/java/org/cloudfoundry/operations/ApplicationsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,10 @@ public void logs() throws IOException {
529529
.name(applicationName)
530530
.recent(true)
531531
.build()))
532-
.map(ApplicationLog::getLogType)
532+
.map(org.cloudfoundry.logcache.v1.Log::getType)
533533
.next()
534534
.as(StepVerifier::create)
535-
.expectNext(ApplicationLogType.OUT)
535+
.expectNext(org.cloudfoundry.logcache.v1.LogType.OUT)
536536
.expectComplete()
537537
.verify(Duration.ofMinutes(5));
538538
}

0 commit comments

Comments
 (0)