Skip to content

Commit

Permalink
fix: improve performance of CloudRegionOrZone.parse and error message…
Browse files Browse the repository at this point in the history
… from PublisherImpl (#1206)
  • Loading branch information
dpcollins-google authored Sep 10, 2022
1 parent 9d93800 commit acb95db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.google.cloud.pubsublite;

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.auto.value.AutoOneOf;
import com.google.cloud.pubsublite.internal.CheckedApiException;
import java.io.Serializable;

@AutoOneOf(CloudRegionOrZone.Kind.class)
Expand Down Expand Up @@ -54,12 +56,16 @@ public static CloudRegionOrZone of(CloudZone zone) {
}

public static CloudRegionOrZone parse(String value) throws ApiException {
try {
return of(CloudZone.parse(value));
} catch (ApiException e) {
// pass
String[] splits = value.split("-", -1);
switch (splits.length) {
case 2:
return of(CloudRegion.of(value));
case 3:
return of(CloudZone.parse(value));
default:
throw new CheckedApiException("Invalid location: " + value, Code.INVALID_ARGUMENT)
.underlying;
}
return of(CloudRegion.of(value));
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,20 @@ public ApiFuture<Offset> publish(Message message) {
PubSubMessage proto = message.toProto();
try (CloseableMonitor.Hold h = batcherMonitor.enter()) {
ApiService.State currentState = state();
checkState(
currentState == ApiService.State.RUNNING,
"Cannot publish when Publisher state is %s.",
currentState.name());
return batcher.add(proto);
switch (currentState) {
case FAILED:
throw new CheckedApiException(
"Cannot publish when publisher has failed.",
failureCause(),
Code.FAILED_PRECONDITION);
case STARTING:
case RUNNING:
return batcher.add(proto);
default:
throw new CheckedApiException(
"Cannot publish when Publisher state is " + currentState.name(),
Code.FAILED_PRECONDITION);
}
} catch (CheckedApiException e) {
onPermanentError(e);
return ApiFutures.immediateFailedFuture(e);
Expand Down

0 comments on commit acb95db

Please sign in to comment.