Skip to content

Commit

Permalink
Create CustomDriverOption for PartitionAwarePolicyAllowedDCs
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorCavichioli committed Aug 16, 2024
1 parent 645d2ca commit f1687bd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
*/
package com.ericsson.bss.cassandra.ecchronos.connection.impl.builders;

import com.ericsson.bss.cassandra.ecchronos.connection.CustomDriverOption;
import com.ericsson.bss.cassandra.ecchronos.connection.DataCenterAwarePolicy;
import com.ericsson.bss.cassandra.ecchronos.connection.impl.enums.ConnectionType;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import com.datastax.oss.driver.api.core.auth.AuthProvider;
Expand Down Expand Up @@ -339,7 +339,7 @@ private static ProgrammaticDriverConfigLoaderBuilder loaderBuilder(
SCHEMA_REFRESHED_KEYSPACES);
if (builder.myType.equals(ConnectionType.datacenterAware))
{
DataCenterAwarePolicy.setAllowedDcs(builder.myDatacenterAware);
loaderBuilder.withStringList(CustomDriverOption.PartitionAwarePolicyAllowedDCs, builder.myDatacenterAware);
loaderBuilder.withString(DefaultDriverOption.LOAD_BALANCING_POLICY_CLASS,
builder.myDatacenterAwarePolicy.getCanonicalName());
loaderBuilder.withInt(DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_MAX_NODES_PER_REMOTE_DC,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 Telefonaktiebolaget LM Ericsson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ericsson.bss.cassandra.ecchronos.connection;

import com.datastax.oss.driver.api.core.config.DriverOption;

public enum CustomDriverOption implements DriverOption
{

PartitionAwarePolicyAllowedDCs("basic.allowed-dcs");

private final String myPath;

CustomDriverOption(final String path)
{
myPath = path;
}

@Override
public String getPath()
{
return myPath;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,14 @@ public class DataCenterAwarePolicy extends DefaultLoadBalancingPolicy

private final ConcurrentMap<String, CopyOnWriteArrayList<Node>> myPerDcLiveNodes = new ConcurrentHashMap<>();
private final AtomicInteger myIndex = new AtomicInteger();
private static List<String> myAllowedDcs;
private final List<String> myAllowedDcs;

public DataCenterAwarePolicy(final DriverContext context, final String profileName)
{
super(context, profileName);
}

public static void setAllowedDcs(final List<String> allowedDcs)
{
if (allowedDcs != null)
{
myAllowedDcs = allowedDcs;
}
myAllowedDcs = context.getConfig()
.getDefaultProfile()
.getStringList(CustomDriverOption.PartitionAwarePolicyAllowedDCs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,20 @@ public void setup()
when(myDriverContextMock.getMetadataManager()).thenReturn(myMetadataManagerMock);
when(myMetadataManagerMock.getMetadata()).thenReturn(myMetadataMock);
when(myDriverConfigMock.getProfile(any(String.class))).thenReturn(myDriverExecutionProfileMock);
when(myDriverConfigMock.getDefaultProfile()).thenReturn(myDriverExecutionProfileMock);
when(myDriverExecutionProfileMock.getName()).thenReturn("unittest");
when(myDriverExecutionProfileMock.getInt(DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_MAX_NODES_PER_REMOTE_DC)).thenReturn(999);
when(myDriverExecutionProfileMock.getBoolean(DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_ALLOW_FOR_LOCAL_CONSISTENCY_LEVELS)).thenReturn(false);
when(myDriverExecutionProfileMock.getString(DefaultDriverOption.REQUEST_CONSISTENCY)).thenReturn("LOCAL_QUORUM");
when(myDriverContextMock.getConsistencyLevelRegistry()).thenReturn(myConsistencyLevelRegistryMock);
when(myConsistencyLevelRegistryMock.nameToLevel(any(String.class))).thenReturn(ConsistencyLevel.LOCAL_QUORUM);
when(myDriverConfigMock.getDefaultProfile().getStringList(CustomDriverOption.PartitionAwarePolicyAllowedDCs)).thenReturn(null);
}

@Test
public void testDistanceHost()
{
DataCenterAwarePolicy.setAllowedDcs(null);
// DataCenterAwarePolicy.setAllowedDcs(null);
DataCenterAwarePolicy policy = new DataCenterAwarePolicy(myDriverContextMock, "");
policy.init(myNodes, myDistanceReporterMock);

Expand All @@ -153,7 +155,7 @@ public void testDistanceHost()
@Test
public void testDistanceHostWithAllowedDcs()
{
DataCenterAwarePolicy.setAllowedDcs(List.of(myAllowedDcs));
when(myDriverConfigMock.getDefaultProfile().getStringList(CustomDriverOption.PartitionAwarePolicyAllowedDCs)).thenReturn(List.of(myAllowedDcs));

DataCenterAwarePolicy policy = new DataCenterAwarePolicy(myDriverContextMock, "");
policy.init(myNodes, myDistanceReporterMock);
Expand Down

0 comments on commit f1687bd

Please sign in to comment.