Skip to content

Commit 9975dd9

Browse files
committed
JAVA-2887: Handle composite profiles with more than one key and/or backed by only one profile
Composite profiles with more than one key currently throw ClassCastException from CompositeDriverExecutionProfile.entrySet. Composite profiles backed by only one profile currently throw NullPointerException from CompositeDriverExecutionProfile.entrySet.
1 parent 4507473 commit 9975dd9

3 files changed

Lines changed: 60 additions & 11 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 4.10.0 (in progress)
66

7+
- [bug] JAVA-2887: Handle composite profiles with more than one key and/or backed by only one profile
78

89
### 4.9.0
910

core/src/main/java/com/datastax/oss/driver/internal/core/config/composite/CompositeDriverExecutionProfile.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.datastax.oss.driver.shaded.guava.common.base.Preconditions;
2222
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSortedSet;
2323
import edu.umd.cs.findbugs.annotations.NonNull;
24+
import edu.umd.cs.findbugs.annotations.Nullable;
2425
import java.time.Duration;
2526
import java.util.List;
2627
import java.util.Map;
@@ -35,8 +36,8 @@ public class CompositeDriverExecutionProfile implements DriverExecutionProfile {
3536
private final DriverConfig fallbackConfig;
3637
private final String profileName;
3738

38-
private volatile DriverExecutionProfile primaryProfile;
39-
private volatile DriverExecutionProfile fallbackProfile;
39+
@Nullable private volatile DriverExecutionProfile primaryProfile;
40+
@Nullable private volatile DriverExecutionProfile fallbackProfile;
4041

4142
public CompositeDriverExecutionProfile(
4243
@NonNull DriverConfig primaryConfig,
@@ -89,8 +90,13 @@ public String getName() {
8990

9091
@Override
9192
public boolean isDefined(@NonNull DriverOption option) {
92-
return (primaryProfile != null && primaryProfile.isDefined(option))
93-
|| (fallbackProfile != null && fallbackProfile.isDefined(option));
93+
DriverExecutionProfile primaryProfile = this.primaryProfile;
94+
if (primaryProfile != null && primaryProfile.isDefined(option)) {
95+
return true;
96+
} else {
97+
DriverExecutionProfile fallbackProfile = this.fallbackProfile;
98+
return fallbackProfile != null && fallbackProfile.isDefined(option);
99+
}
94100
}
95101

96102
@Override
@@ -181,21 +187,34 @@ public List<Duration> getDurationList(@NonNull DriverOption option) {
181187
private <ValueT> ValueT get(
182188
@NonNull DriverOption option,
183189
BiFunction<DriverExecutionProfile, DriverOption, ValueT> getter) {
190+
DriverExecutionProfile primaryProfile = this.primaryProfile;
184191
if (primaryProfile != null && primaryProfile.isDefined(option)) {
185192
return getter.apply(primaryProfile, option);
186-
} else if (fallbackProfile != null && fallbackProfile.isDefined(option)) {
187-
return getter.apply(fallbackProfile, option);
188193
} else {
189-
throw new IllegalArgumentException("Unknown option: " + option);
194+
DriverExecutionProfile fallbackProfile = this.fallbackProfile;
195+
if (fallbackProfile != null && fallbackProfile.isDefined(option)) {
196+
return getter.apply(fallbackProfile, option);
197+
} else {
198+
throw new IllegalArgumentException("Unknown option: " + option);
199+
}
190200
}
191201
}
192202

193203
@NonNull
194204
@Override
195205
public SortedSet<Map.Entry<String, Object>> entrySet() {
196-
SortedSet<Map.Entry<String, Object>> result = new TreeSet<>(Map.Entry.comparingByKey());
197-
result.addAll(fallbackProfile.entrySet());
198-
result.addAll(primaryProfile.entrySet());
199-
return ImmutableSortedSet.copyOf(result);
206+
DriverExecutionProfile primaryProfile = this.primaryProfile;
207+
DriverExecutionProfile fallbackProfile = this.fallbackProfile;
208+
if (primaryProfile != null && fallbackProfile != null) {
209+
SortedSet<Map.Entry<String, Object>> result = new TreeSet<>(Map.Entry.comparingByKey());
210+
result.addAll(fallbackProfile.entrySet());
211+
result.addAll(primaryProfile.entrySet());
212+
return ImmutableSortedSet.copyOf(Map.Entry.comparingByKey(), result);
213+
} else if (primaryProfile != null) {
214+
return primaryProfile.entrySet();
215+
} else {
216+
assert fallbackProfile != null;
217+
return fallbackProfile.entrySet();
218+
}
200219
}
201220
}

core/src/test/java/com/datastax/oss/driver/internal/core/config/composite/CompositeDriverConfigTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package com.datastax.oss.driver.internal.core.config.composite;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.entry;
1920

21+
import com.datastax.dse.driver.api.core.config.DseDriverOption;
2022
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
2123
import com.datastax.oss.driver.api.core.config.DriverConfig;
2224
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
@@ -59,6 +61,10 @@ public void should_use_value_from_primary_config() {
5961
.isTrue();
6062
assertThat(compositeDefaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE))
6163
.isEqualTo(1);
64+
assertThat(compositeDefaultProfile.entrySet())
65+
.containsExactly(
66+
entry(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE.getPath(), 1),
67+
entry(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES.getPath(), 1));
6268
}
6369

6470
@Test
@@ -70,6 +76,10 @@ public void should_ignore_value_from_fallback_config_if_defined_in_both() {
7076
.isTrue();
7177
assertThat(compositeDefaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE))
7278
.isEqualTo(1);
79+
assertThat(compositeDefaultProfile.entrySet())
80+
.containsExactly(
81+
entry(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE.getPath(), 1),
82+
entry(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES.getPath(), 1));
7383
}
7484

7585
@Test
@@ -80,6 +90,10 @@ public void should_use_value_from_fallback_config_if_not_defined_in_primary() {
8090
.isTrue();
8191
assertThat(compositeDefaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE))
8292
.isEqualTo(1);
93+
assertThat(compositeDefaultProfile.entrySet())
94+
.containsExactly(
95+
entry(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE.getPath(), 1),
96+
entry(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES.getPath(), 1));
8397
}
8498

8599
@Test
@@ -112,5 +126,20 @@ public void should_merge_profiles() {
112126
.getProfile("onlyInFallback")
113127
.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE))
114128
.isEqualTo(4);
129+
130+
assertThat(compositeConfig.getProfile("onlyInPrimary").entrySet())
131+
.containsExactly(
132+
entry(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE.getPath(), 1),
133+
entry(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES.getPath(), 1));
134+
135+
assertThat(compositeConfig.getProfile("inBoth").entrySet())
136+
.containsExactly(
137+
entry(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE.getPath(), 2),
138+
entry(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES.getPath(), 1));
139+
140+
assertThat(compositeConfig.getProfile("onlyInFallback").entrySet())
141+
.containsExactly(
142+
entry(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE.getPath(), 4),
143+
entry(DseDriverOption.CONTINUOUS_PAGING_MAX_PAGES.getPath(), 1));
115144
}
116145
}

0 commit comments

Comments
 (0)