Skip to content
Merged
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
210 changes: 6 additions & 204 deletions sdk/src/main/java/io/dapr/client/DaprClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package io.dapr.client;

import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
import io.dapr.client.domain.ActorMetadata;
Expand All @@ -25,7 +24,6 @@
import io.dapr.client.domain.BulkPublishResponse;
import io.dapr.client.domain.ComponentMetadata;
import io.dapr.client.domain.ConfigurationItem;
import io.dapr.client.domain.ConstantFailurePolicy;
import io.dapr.client.domain.ConversationRequest;
import io.dapr.client.domain.ConversationRequestAlpha2;
import io.dapr.client.domain.ConversationResponse;
Expand All @@ -36,10 +34,8 @@
import io.dapr.client.domain.DeleteJobRequest;
import io.dapr.client.domain.DeleteStateRequest;
import io.dapr.client.domain.DeleteVectorsRequestAlpha1;
import io.dapr.client.domain.DropFailurePolicy;
import io.dapr.client.domain.EncryptRequestAlpha1;
import io.dapr.client.domain.ExecuteStateTransactionRequest;
import io.dapr.client.domain.FailurePolicy;
import io.dapr.client.domain.GetBulkSecretRequest;
import io.dapr.client.domain.GetBulkStateRequest;
import io.dapr.client.domain.GetConfigurationRequest;
Expand All @@ -54,7 +50,6 @@
import io.dapr.client.domain.IndexAck;
import io.dapr.client.domain.InvokeBindingRequest;
import io.dapr.client.domain.InvokeMethodRequest;
import io.dapr.client.domain.JobSchedule;
import io.dapr.client.domain.LockRequest;
import io.dapr.client.domain.PublishEventRequest;
import io.dapr.client.domain.QueryStateRequest;
Expand Down Expand Up @@ -83,14 +78,12 @@
import io.dapr.internal.resiliency.RetryPolicy;
import io.dapr.internal.resiliency.TimeoutPolicy;
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.utils.FailurePolicyUtils;
import io.dapr.utils.TypeRef;
import io.dapr.v1.CommonProtos;
import io.dapr.v1.DaprBindingsProtos;
import io.dapr.v1.DaprConfigurationProtos;
import io.dapr.v1.DaprCryptoProtos;
import io.dapr.v1.DaprGrpc;
import io.dapr.v1.DaprJobsProtos;
import io.dapr.v1.DaprLockProtos;
import io.dapr.v1.DaprMetadataProtos;
import io.dapr.v1.DaprProtos;
Expand All @@ -108,10 +101,6 @@
import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -155,6 +144,8 @@ public class DaprClientImpl extends AbstractDaprClient {

private final DaprConversationClient conversationClient;

private final DaprJobsClient jobsClient;

private final DaprBinaryFileClient binaryFileClient;

private final DaprPubSubClient pubSubClient;
Expand Down Expand Up @@ -260,6 +251,7 @@ private DaprClientImpl(
this.grpcInterceptors = new DaprClientGrpcInterceptors(daprApiToken, timeoutPolicy);
this.grpcCalls = new DaprGrpcCalls(asyncStub, this.grpcInterceptors, retryPolicy);
this.conversationClient = new DaprConversationClient(this.grpcCalls, objectSerializer);
this.jobsClient = new DaprJobsClient(this.grpcCalls);
this.binaryFileClient = new DaprBinaryFileClient(this.grpcCalls);
this.pubSubClient = new DaprPubSubClient(this.grpcCalls, objectSerializer);
this.stateClient = new DaprStateClient(this.grpcCalls, stateSerializer);
Expand Down Expand Up @@ -925,211 +917,21 @@ public Mono<UnsubscribeConfigurationResponse> unsubscribeConfiguration(Unsubscri
* {@inheritDoc}
*/
public Mono<Void> scheduleJob(ScheduleJobRequest scheduleJobRequest) {
try {
validateScheduleJobRequest(scheduleJobRequest);

DaprJobsProtos.Job.Builder jobBuilder = DaprJobsProtos.Job.newBuilder();
jobBuilder.setName(scheduleJobRequest.getName());

DateTimeFormatter iso8601Formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.withZone(ZoneOffset.UTC);

if (scheduleJobRequest.getData() != null) {
jobBuilder.setData(Any.newBuilder()
.setValue(ByteString.copyFrom(scheduleJobRequest.getData())).build());
}

if (scheduleJobRequest.getSchedule() != null) {
jobBuilder.setSchedule(scheduleJobRequest.getSchedule().getExpression());
}

if (scheduleJobRequest.getTtl() != null) {
jobBuilder.setTtl(iso8601Formatter.format(scheduleJobRequest.getTtl()));
}

if (scheduleJobRequest.getRepeats() != null) {
jobBuilder.setRepeats(scheduleJobRequest.getRepeats());
}

if (scheduleJobRequest.getDueTime() != null) {
jobBuilder.setDueTime(iso8601Formatter.format(scheduleJobRequest.getDueTime()));
}

if (scheduleJobRequest.getFailurePolicy() != null) {
jobBuilder.setFailurePolicy(FailurePolicyUtils.getJobFailurePolicy(scheduleJobRequest.getFailurePolicy()));
}

DaprJobsProtos.ScheduleJobRequest request = DaprJobsProtos.ScheduleJobRequest.newBuilder()
.setOverwrite(scheduleJobRequest.getOverwrite())
.setJob(jobBuilder.build())
.build();

Mono<DaprJobsProtos.ScheduleJobResponse> scheduleJobResponseMono =
Mono.deferContextual(context ->
grpcCalls.<DaprJobsProtos.ScheduleJobResponse>createMono(
it -> grpcCalls.intercept(context).scheduleJob(request, it)
).onErrorResume(throwable -> {
// The stable ScheduleJob RPC is unavailable on pre-1.18 sidecars, fall back to Alpha1.
if (isUnimplemented(throwable)) {
return grpcCalls.<DaprJobsProtos.ScheduleJobResponse>createMono(
it -> grpcCalls.intercept(context).scheduleJobAlpha1(request, it));
}
return Mono.error(throwable);
})
);

return scheduleJobResponseMono.then();
} catch (Exception ex) {
return DaprException.wrapMono(ex);
}
return this.jobsClient.scheduleJob(scheduleJobRequest);
}

/**
* {@inheritDoc}
*/
public Mono<GetJobResponse> getJob(GetJobRequest getJobRequest) {
try {
validateGetJobRequest(getJobRequest);

DaprJobsProtos.GetJobRequest request = DaprJobsProtos.GetJobRequest.newBuilder()
.setName(getJobRequest.getName())
.build();

Mono<DaprJobsProtos.GetJobResponse> getJobResponseMono =
Mono.deferContextual(context ->
grpcCalls.<DaprJobsProtos.GetJobResponse>createMono(
it -> grpcCalls.intercept(context).getJob(request, it)
).onErrorResume(throwable -> {
// The stable GetJob RPC is unavailable on pre-1.18 sidecars, fall back to Alpha1.
if (isUnimplemented(throwable)) {
return grpcCalls.<DaprJobsProtos.GetJobResponse>createMono(
it -> grpcCalls.intercept(context).getJobAlpha1(request, it));
}
return Mono.error(throwable);
})
);

return getJobResponseMono.map(response -> {
DaprJobsProtos.Job job = response.getJob();
GetJobResponse getJobResponse = null;

if (job.hasSchedule() && job.hasDueTime()) {
getJobResponse = new GetJobResponse(job.getName(), JobSchedule.fromString(job.getSchedule()));
getJobResponse.setDueTime(Instant.parse(job.getDueTime()));
} else if (job.hasSchedule()) {
getJobResponse = new GetJobResponse(job.getName(), JobSchedule.fromString(job.getSchedule()));
} else {
getJobResponse = new GetJobResponse(job.getName(), Instant.parse(job.getDueTime()));
}

if (job.hasFailurePolicy()) {
getJobResponse.setFailurePolicy(getJobFailurePolicy(job.getFailurePolicy()));
}

return getJobResponse
.setTtl(job.hasTtl() ? Instant.parse(job.getTtl()) : null)
.setData(job.hasData() ? job.getData().getValue().toByteArray() : null)
.setRepeat(job.hasRepeats() ? job.getRepeats() : null);
});
} catch (Exception ex) {
return DaprException.wrapMono(ex);
}
}

private FailurePolicy getJobFailurePolicy(CommonProtos.JobFailurePolicy jobFailurePolicy) {
if (jobFailurePolicy.hasDrop()) {
return new DropFailurePolicy();
}

CommonProtos.JobFailurePolicyConstant jobFailurePolicyConstant = jobFailurePolicy.getConstant();
if (jobFailurePolicyConstant.hasInterval() && jobFailurePolicyConstant.hasMaxRetries()) {
return new ConstantFailurePolicy(jobFailurePolicyConstant.getMaxRetries())
.setDurationBetweenRetries(Duration.of(jobFailurePolicyConstant.getInterval().getNanos(),
ChronoUnit.NANOS));
}

if (jobFailurePolicyConstant.hasMaxRetries()) {
return new ConstantFailurePolicy(jobFailurePolicyConstant.getMaxRetries());
}

return new ConstantFailurePolicy(
Duration.of(jobFailurePolicyConstant.getInterval().getNanos(),
ChronoUnit.NANOS));
return this.jobsClient.getJob(getJobRequest);
}

/**
* {@inheritDoc}
*/
public Mono<Void> deleteJob(DeleteJobRequest deleteJobRequest) {
try {
validateDeleteJobRequest(deleteJobRequest);

DaprJobsProtos.DeleteJobRequest request = DaprJobsProtos.DeleteJobRequest.newBuilder()
.setName(deleteJobRequest.getName())
.build();

Mono<DaprJobsProtos.DeleteJobResponse> deleteJobResponseMono =
Mono.deferContextual(context ->
grpcCalls.<DaprJobsProtos.DeleteJobResponse>createMono(
it -> grpcCalls.intercept(context).deleteJob(request, it)
).onErrorResume(throwable -> {
// The stable DeleteJob RPC is unavailable on pre-1.18 sidecars, fall back to Alpha1.
if (isUnimplemented(throwable)) {
return grpcCalls.<DaprJobsProtos.DeleteJobResponse>createMono(
it -> grpcCalls.intercept(context).deleteJobAlpha1(request, it));
}
return Mono.error(throwable);
})
);

return deleteJobResponseMono.then();
} catch (Exception ex) {
return DaprException.wrapMono(ex);
}
}

/**
* Returns true when a failed RPC indicates the sidecar has not implemented the stable RPC,
* so the caller should retry against the deprecated Alpha1 RPC. The gRPC status is wrapped in a
* {@link DaprException} (and an {@link java.util.concurrent.ExecutionException}), so we let gRPC
* walk the cause chain to recover the original status code.
*/
private static boolean isUnimplemented(Throwable throwable) {
return io.grpc.Status.fromThrowable(throwable).getCode() == io.grpc.Status.Code.UNIMPLEMENTED;
}

private void validateScheduleJobRequest(ScheduleJobRequest scheduleJobRequest) {
if (scheduleJobRequest == null) {
throw new IllegalArgumentException("scheduleJobRequest cannot be null");
}

if (scheduleJobRequest.getName() == null || scheduleJobRequest.getName().isEmpty()) {
throw new IllegalArgumentException("Name in the request cannot be null or empty");
}

if (scheduleJobRequest.getSchedule() == null && scheduleJobRequest.getDueTime() == null) {
throw new IllegalArgumentException("At least one of schedule or dueTime must be provided");
}
}

private void validateGetJobRequest(GetJobRequest getJobRequest) {
if (getJobRequest == null) {
throw new IllegalArgumentException("getJobRequest cannot be null");
}

if (getJobRequest.getName() == null || getJobRequest.getName().isEmpty()) {
throw new IllegalArgumentException("Name in the request cannot be null or empty");
}
}

private void validateDeleteJobRequest(DeleteJobRequest deleteJobRequest) {
if (deleteJobRequest == null) {
throw new IllegalArgumentException("deleteJobRequest cannot be null");
}

if (deleteJobRequest.getName() == null || deleteJobRequest.getName().isEmpty()) {
throw new IllegalArgumentException("Name in the request cannot be null or empty");
}
return this.jobsClient.deleteJob(deleteJobRequest);
}

/**
Expand Down
Loading
Loading