Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -45,68 +45,109 @@
@NullMarked
@InternalApi
class CompositeTracer extends BaseApiTracer {
private static final Scope NOOP_SCOPE = () -> {};

private final List<ApiTracer> children;

public CompositeTracer(List<ApiTracer> children) {
this.children = ImmutableList.copyOf(children);
}

// Unstubbed Mockito mocks of ApiTracer may return null despite @NullMarked, and Error is caught
// alongside RuntimeException solely to close already-opened scopes before rethrowing.
@SuppressWarnings({"java:S2589", "java:S1181"})
@Override
public Scope inScope() {
if (children.isEmpty()) {
return NOOP_SCOPE;
}
if (children.size() == 1) {
Scope scope = children.get(0).inScope();
return scope != null ? scope : NOOP_SCOPE;

Check warning on line 66 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/CompositeTracer.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Change this condition so that it does not always evaluate to "true"

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AaHlYF9jcf_I1OSc-1dl&open=AaHlYF9jcf_I1OSc-1dl&pullRequest=15034
}
final List<Scope> childScopes = new ArrayList<>(children.size());
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved

try {
for (ApiTracer child : children) {
childScopes.add(child.inScope());
Scope childScope = child.inScope();
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved
if (childScope != null) {
childScopes.add(childScope);
}
}
} catch (RuntimeException e) {
} catch (RuntimeException | Error e) {
for (int i = childScopes.size() - 1; i >= 0; i--) {
try {
childScopes.get(i).close();
} catch (RuntimeException suppressed) {
} catch (RuntimeException | Error suppressed) {
e.addSuppressed(suppressed);
}
}
throw e;
}

return () -> {
RuntimeException exception = null;
for (int i = childScopes.size() - 1; i >= 0; i--) {
try {
childScopes.get(i).close();
} catch (RuntimeException e) {
if (exception == null) {
exception = e;
} else {
exception.addSuppressed(e);
if (childScopes.isEmpty()) {
return NOOP_SCOPE;
}
if (childScopes.size() == 1) {
return childScopes.get(0);
}

// Scope is thread-bound and must be closed on the same thread that opened it.
return new Scope() {
private boolean closed;

@Override
public void close() {
if (closed) {
return;
}
closed = true;
Throwable primaryError = null;
for (int i = childScopes.size() - 1; i >= 0; i--) {
try {
childScopes.get(i).close();
} catch (RuntimeException | Error e) {
if (primaryError == null) {
primaryError = e;
} else {
primaryError.addSuppressed(e);
}
}
}
}
if (exception != null) {
throw exception;
if (primaryError instanceof RuntimeException) {
throw (RuntimeException) primaryError;
}
if (primaryError instanceof Error) {
throw (Error) primaryError;
}
}
};
Comment thread
jinseopkim0 marked this conversation as resolved.
Resolved
}

@Override
public void operationSucceeded() {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).operationSucceeded();
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).operationSucceeded();
}
}
}

@Override
public void operationCancelled() {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).operationCancelled();
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).operationCancelled();
}
}
}

@Override
public void operationFailed(Throwable error) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).operationFailed(error);
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).operationFailed(error);
}
}
}

Expand Down Expand Up @@ -134,43 +175,55 @@

@Override
public void attemptSucceeded() {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptSucceeded();
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptSucceeded();
}
}
}

@Override
public void attemptCancelled() {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptCancelled();
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptCancelled();
}
}
}

@Override
public void attemptFailed(Throwable error, org.threeten.bp.Duration delay) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptFailed(error, delay);
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptFailed(error, delay);
}
}
}

@Override
public void attemptFailedDuration(Throwable error, java.time.Duration delay) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptFailedDuration(error, delay);
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptFailedDuration(error, delay);
}
}
}

@Override
public void attemptFailedRetriesExhausted(Throwable error) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptFailedRetriesExhausted(error);
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptFailedRetriesExhausted(error);
}
}
}

@Override
public void attemptPermanentFailure(Throwable error) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptPermanentFailure(error);
try (Scope ignored = inScope()) {
for (int i = children.size() - 1; i >= 0; i--) {
children.get(i).attemptPermanentFailure(error);
}
}
}

Expand Down
Loading
Loading