Skip to content

Commit 527b28b

Browse files
authored
Better Logging and Miscellaneous Cleanup (Azure#32420)
Better Logging and Miscellaneous Cleanup
1 parent 094e1f1 commit 527b28b

20 files changed

+184
-176
lines changed

sdk/core/azure-core/src/main/java/com/azure/core/implementation/RetriableDownloadFlux.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package com.azure.core.implementation;
55

6+
import com.azure.core.util.logging.ClientLogger;
7+
import com.azure.core.util.logging.LogLevel;
68
import reactor.core.CoreSubscriber;
79
import reactor.core.publisher.Flux;
810

@@ -15,6 +17,8 @@
1517
* operation if an error occurs during the download.
1618
*/
1719
public final class RetriableDownloadFlux extends Flux<ByteBuffer> {
20+
private static final ClientLogger LOGGER = new ClientLogger(RetriableDownloadFlux.class);
21+
1822
private final Supplier<Flux<ByteBuffer>> downloadSupplier;
1923
private final BiFunction<Throwable, Long, Flux<ByteBuffer>> onDownloadErrorResume;
2024
private final int maxRetries;
@@ -58,9 +62,14 @@ public void subscribe(CoreSubscriber<? super ByteBuffer> actual) {
5862
int updatedRetryCount = retryCount + 1;
5963

6064
if (updatedRetryCount > maxRetries) {
65+
LOGGER.log(LogLevel.ERROR, () -> "Exhausted all retry attempts while downloading, "
66+
+ updatedRetryCount + " of " + maxRetries + ".", exception);
6167
return Flux.error(exception);
6268
}
6369

70+
LOGGER.log(LogLevel.INFORMATIONAL,
71+
() -> "Using retry attempt " + updatedRetryCount + " of " + maxRetries + " while downloading.",
72+
exception);
6473
return new RetriableDownloadFlux(() -> onDownloadErrorResume.apply(exception, currentPosition[0]),
6574
onDownloadErrorResume, maxRetries, currentPosition[0], updatedRetryCount);
6675
})

sdk/core/azure-core/src/main/java/com/azure/core/implementation/SemanticVersion.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public static SemanticVersion getPackageVersionForClass(String className) {
4848
*/
4949
public static SemanticVersion parse(String version) {
5050
Objects.requireNonNull(version, "'version' cannot be null.");
51-
String[] parts = version.split("\\.");
52-
if (parts.length < 3) {
51+
52+
int majorDotIdx = version.indexOf('.');
53+
if (majorDotIdx < 0) {
5354
return createInvalid(version);
5455
}
5556

56-
int majorDotIdx = version.indexOf('.');
5757
int minorDotIdx = version.indexOf('.', majorDotIdx + 1);
58-
if (majorDotIdx < 0 || minorDotIdx < 0) {
58+
if (minorDotIdx < 0) {
5959
return createInvalid(version);
6060
}
6161

sdk/core/azure-core/src/main/java/com/azure/core/implementation/StringBuilderWriter.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
/**
1111
* Implementation of {@link Writer} that write content to a {@link StringBuilder}.
12+
* <p>
13+
* Given the backing store of this {@link Writer} is a {@link StringBuilder} this is not thread-safe.
1214
*/
1315
public final class StringBuilderWriter extends Writer {
1416
private final StringBuilder builder;
1517

16-
private volatile boolean closed = false;
18+
// This can be non-volatile as StringBuilder itself isn't thread-safe.
19+
private boolean closed = false;
1720

1821
/**
1922
* Creates an instance of {@link StringBuilderWriter}.
@@ -27,45 +30,35 @@ public StringBuilderWriter(StringBuilder builder) {
2730

2831
@Override
2932
public void write(int c) throws IOException {
30-
if (closed) {
31-
throw new IOException("Writer has been closed.");
32-
}
33+
ensureOpen();
3334

3435
builder.append(c);
3536
}
3637

3738
@Override
3839
public void write(char[] cbuf) throws IOException {
39-
if (closed) {
40-
throw new IOException("Writer has been closed.");
41-
}
40+
ensureOpen();
4241

4342
builder.append(cbuf);
4443
}
4544

4645
@Override
4746
public void write(String str) throws IOException {
48-
if (closed) {
49-
throw new IOException("Writer has been closed.");
50-
}
47+
ensureOpen();
5148

5249
builder.append(str);
5350
}
5451

5552
@Override
5653
public void write(String str, int off, int len) throws IOException {
57-
if (closed) {
58-
throw new IOException("Writer has been closed.");
59-
}
54+
ensureOpen();
6055

6156
builder.append(str, off, len);
6257
}
6358

6459
@Override
6560
public Writer append(CharSequence csq) throws IOException {
66-
if (closed) {
67-
throw new IOException("Writer has been closed.");
68-
}
61+
ensureOpen();
6962

7063
builder.append(csq);
7164

@@ -74,9 +67,7 @@ public Writer append(CharSequence csq) throws IOException {
7467

7568
@Override
7669
public Writer append(CharSequence csq, int start, int end) throws IOException {
77-
if (closed) {
78-
throw new IOException("Writer has been closed.");
79-
}
70+
ensureOpen();
8071

8172
builder.append(csq, start, end);
8273

@@ -85,9 +76,7 @@ public Writer append(CharSequence csq, int start, int end) throws IOException {
8576

8677
@Override
8778
public Writer append(char c) throws IOException {
88-
if (closed) {
89-
throw new IOException("Writer has been closed.");
90-
}
79+
ensureOpen();
9180

9281
builder.append(c);
9382

@@ -96,22 +85,24 @@ public Writer append(char c) throws IOException {
9685

9786
@Override
9887
public void write(char[] cbuf, int off, int len) throws IOException {
99-
if (closed) {
100-
throw new IOException("Writer has been closed.");
101-
}
88+
ensureOpen();
10289

10390
builder.append(cbuf, off, len);
10491
}
10592

10693
@Override
10794
public void flush() throws IOException {
108-
if (closed) {
109-
throw new IOException("Writer has been closed.");
110-
}
95+
ensureOpen();
11196
}
11297

11398
@Override
11499
public void close() {
115100
closed = true;
116101
}
102+
103+
private void ensureOpen() throws IOException {
104+
if (closed) {
105+
throw new IOException("Writer has been closed.");
106+
}
107+
}
117108
}

sdk/core/azure-core/src/main/java/com/azure/core/implementation/TypeUtil.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public static Type getTypeArgument(Type type) {
6565
* @param type the input type
6666
* @return the raw class
6767
*/
68-
@SuppressWarnings("unchecked")
6968
public static Class<?> getRawClass(Type type) {
7069
if (type instanceof ParameterizedType) {
7170
return (Class<?>) ((ParameterizedType) type).getRawType();
@@ -81,6 +80,11 @@ public static Class<?> getRawClass(Type type) {
8180
* @return the direct super type
8281
*/
8382
public static Type getSuperType(final Type type) {
83+
Type superType = SUPER_TYPE_MAP.get(type);
84+
if (superType != null) {
85+
return superType;
86+
}
87+
8488
return SUPER_TYPE_MAP.computeIfAbsent(type, _type -> {
8589
if (type instanceof ParameterizedType) {
8690
final ParameterizedType parameterizedType = (ParameterizedType) type;
@@ -186,17 +190,6 @@ public Type getOwnerType() {
186190
};
187191
}
188192

189-
/**
190-
* Returns whether the rest response expects to have any body (by checking if the body parameter type is set to
191-
* Void, in which case no body is expected).
192-
*
193-
* @param restResponseReturnType The RestResponse subtype containing the type arguments we are inspecting.
194-
* @return True if a body is expected, false if a Void body is expected.
195-
*/
196-
public static boolean restResponseTypeExpectsBody(ParameterizedType restResponseReturnType) {
197-
return getRestResponseBodyType(restResponseReturnType) != Void.class;
198-
}
199-
200193
/**
201194
* Returns the body type expected in the rest response.
202195
*

sdk/core/azure-core/src/main/java/com/azure/core/util/Base64Url.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static byte[] unquote(byte[] bytes) {
5252
}
5353

5454
private static String unquote(String string) {
55-
if (string != null && !string.isEmpty()) {
55+
if (!CoreUtils.isNullOrEmpty(string)) {
5656
final char firstCharacter = string.charAt(0);
5757
if (firstCharacter == '\"' || firstCharacter == '\'') {
5858
final int base64UrlStringLength = string.length();
@@ -113,15 +113,14 @@ public int hashCode() {
113113

114114
@Override
115115
public boolean equals(Object obj) {
116-
if (obj == null) {
117-
return false;
116+
if (obj == this) {
117+
return true;
118118
}
119119

120120
if (!(obj instanceof Base64Url)) {
121121
return false;
122122
}
123123

124-
Base64Url rhs = (Base64Url) obj;
125-
return Arrays.equals(this.bytes, rhs.encodedBytes());
124+
return Arrays.equals(this.bytes, ((Base64Url) obj).encodedBytes());
126125
}
127126
}

sdk/core/azure-core/src/main/java/com/azure/core/util/Configuration.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ public class Configuration implements Cloneable {
206206
* {@code NettyAsyncHttpClientProvider}, to disambiguate multiple providers with the same name but from different
207207
* packages.
208208
* <p>
209-
* If the value isn't set or is an empty string the first {@link HttpClientProvider} found on the class path will
210-
* be used to create an instance of {@link HttpClient}. If the value is set and doesn't match any
209+
* If the value isn't set or is an empty string the first {@link HttpClientProvider} found on the class path will be
210+
* used to create an instance of {@link HttpClient}. If the value is set and doesn't match any
211211
* {@link HttpClientProvider} found on the class path an {@link IllegalStateException} will be thrown when
212212
* attempting to create an instance of {@link HttpClient}.
213213
*/
@@ -235,38 +235,43 @@ public class Configuration implements Cloneable {
235235
/**
236236
* Constructs a configuration containing the known Azure properties constants.
237237
*
238-
* @deprecated Use {@link ConfigurationBuilder} and {@link ConfigurationSource} that allow to
239-
* provide all properties before creating configuration and keep it immutable.
238+
* @deprecated Use {@link ConfigurationBuilder} and {@link ConfigurationSource} that allow to provide all properties
239+
* before creating configuration and keep it immutable.
240240
*/
241241
@Deprecated
242242
public Configuration() {
243243
this(Collections.emptyMap(), EnvironmentConfiguration.getGlobalConfiguration(), null, null);
244244
}
245245

246246
/**
247-
* Constructs a configuration containing the known Azure properties constants. Use {@link ConfigurationBuilder} to create instance of {@link Configuration}.
247+
* Constructs a configuration containing the known Azure properties constants. Use {@link ConfigurationBuilder} to
248+
* create instance of {@link Configuration}.
248249
*
249250
* @param configurationSource Configuration property source.
250251
* @param environmentConfiguration instance of {@link EnvironmentConfiguration} to mock environment for testing.
251252
* @param path Absolute path of current configuration section for logging and diagnostics purposes.
252253
* @param sharedConfiguration Instance of shared {@link Configuration} section to retrieve shared properties.
253254
*/
254-
Configuration(ConfigurationSource configurationSource, EnvironmentConfiguration environmentConfiguration, String path, Configuration sharedConfiguration) {
255+
Configuration(ConfigurationSource configurationSource, EnvironmentConfiguration environmentConfiguration,
256+
String path, Configuration sharedConfiguration) {
255257
this(readConfigurations(configurationSource, path), environmentConfiguration, path, sharedConfiguration);
256258
}
257259

258260
/**
259-
* Constructs a configuration containing the known Azure properties constants. Use {@link ConfigurationBuilder} to create instance of {@link Configuration}.
261+
* Constructs a configuration containing the known Azure properties constants. Use {@link ConfigurationBuilder} to
262+
* create instance of {@link Configuration}.
260263
*
261264
* @param configurations map of all properties.
262265
* @param environmentConfiguration instance of {@link EnvironmentConfiguration} to mock environment for testing.
263266
* @param path Absolute path of current configuration section for logging and diagnostics purposes.
264267
* @param sharedConfiguration Instance of shared {@link Configuration} section to retrieve shared properties.
265268
*/
266-
private Configuration(Map<String, String> configurations, EnvironmentConfiguration environmentConfiguration, String path, Configuration sharedConfiguration) {
269+
private Configuration(Map<String, String> configurations, EnvironmentConfiguration environmentConfiguration,
270+
String path, Configuration sharedConfiguration) {
267271
this.configurations = configurations;
268272
this.isEmpty = configurations.isEmpty();
269-
this.environmentConfiguration = Objects.requireNonNull(environmentConfiguration, "'environmentConfiguration' cannot be null");
273+
this.environmentConfiguration = Objects.requireNonNull(environmentConfiguration,
274+
"'environmentConfiguration' cannot be null");
270275
this.path = path;
271276
this.sharedConfiguration = sharedConfiguration;
272277
}
@@ -299,8 +304,8 @@ public String get(String name) {
299304
* Gets the value of system property or environment variable converted to given primitive {@code T} using
300305
* corresponding {@code parse} method on this type.
301306
*
302-
* Use {@link Configuration#get(ConfigurationProperty)} overload to get explicit configuration or
303-
* environment configuration from specific source.
307+
* Use {@link Configuration#get(ConfigurationProperty)} overload to get explicit configuration or environment
308+
* configuration from specific source.
304309
*
305310
* <p>
306311
* This method first checks the values previously loaded from the environment, if the configuration is found there
@@ -354,9 +359,8 @@ public <T> T get(String name, Function<String, T> converter) {
354359
* @param name Name of the configuration.
355360
* @param value Value of the configuration.
356361
* @return The updated Configuration object.
357-
*
358-
* @deprecated Use {@link ConfigurationBuilder} and {@link ConfigurationSource} to
359-
* provide all properties before creating configuration.
362+
* @deprecated Use {@link ConfigurationBuilder} and {@link ConfigurationSource} to provide all properties before
363+
* creating configuration.
360364
*/
361365
@Deprecated
362366
public Configuration put(String name, String value) {
@@ -371,9 +375,8 @@ public Configuration put(String name, String value) {
371375
*
372376
* @param name Name of the configuration.
373377
* @return The configuration if it previously existed, otherwise null.
374-
*
375-
* @deprecated Use {@link ConfigurationBuilder} and {@link ConfigurationSource} to
376-
* provide all properties before creating configuration.
378+
* @deprecated Use {@link ConfigurationBuilder} and {@link ConfigurationSource} to provide all properties before
379+
* creating configuration.
377380
*/
378381
@Deprecated
379382
public String remove(String name) {
@@ -382,9 +385,9 @@ public String remove(String name) {
382385

383386
/**
384387
* Determines if the system property or environment variable is defined.
385-
*
386-
* Use {@link Configuration#contains(ConfigurationProperty)} overload to get explicit configuration or
387-
* environment configuration from specific source.
388+
* <p>
389+
* Use {@link Configuration#contains(ConfigurationProperty)} overload to get explicit configuration or environment
390+
* configuration from specific source.
388391
*
389392
* <p>
390393
* This only checks against values previously loaded into the Configuration object, this won't inspect the
@@ -406,12 +409,14 @@ public boolean contains(String name) {
406409
@SuppressWarnings("CloneDoesntCallSuperClone")
407410
@Deprecated
408411
public Configuration clone() {
409-
return new Configuration(configurations, new EnvironmentConfiguration(environmentConfiguration), path, sharedConfiguration);
412+
return new Configuration(configurations, new EnvironmentConfiguration(environmentConfiguration), path,
413+
sharedConfiguration);
410414
}
411415

412416
/**
413-
* Checks if configuration contains the property. If property can be shared between clients, checks this {@code Configuration} and
414-
* falls back to shared section. If property has aliases, system property or environment variable defined, checks them as well.
417+
* Checks if configuration contains the property. If property can be shared between clients, checks this
418+
* {@code Configuration} and falls back to shared section. If property has aliases, system property or environment
419+
* variable defined, checks them as well.
415420
* <p>
416421
* Value is not validated.
417422
*
@@ -454,7 +459,7 @@ public boolean contains(ConfigurationProperty<?> property) {
454459
*
455460
* @param property instance.
456461
* @param <T> Type that the configuration is converted to if found.
457-
* @return true if property is available, false otherwise.
462+
* @return The value of the property if it exists, otherwise the default value of the property.
458463
* @throws NullPointerException when property instance is null.
459464
* @throws IllegalArgumentException when required property is missing.
460465
* @throws RuntimeException when property value conversion (and validation) throws.
@@ -595,8 +600,8 @@ private static Map<String, String> readConfigurations(ConfigurationSource source
595600
}
596601

597602
/**
598-
* Attempts to convert the configuration value to given primitive {@code T} using
599-
* corresponding {@code parse} method on this type.
603+
* Attempts to convert the configuration value to given primitive {@code T} using corresponding {@code parse} method
604+
* on this type.
600605
*
601606
* <p><b>Following types are supported:</b></p>
602607
* <ul>

0 commit comments

Comments
 (0)