Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

674 Specify Interval for Next Connection #712

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.application.config;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class Interval
{
static final int DEFAULT_TIME_IN_MINUTES = 60;
private long myTime = DEFAULT_TIME_IN_MINUTES;
private TimeUnit myUnit = TimeUnit.MINUTES;

public Interval()
{
// Default constructor for jackson
}

public Interval(final long time, final TimeUnit timeUnit)
{
myTime = time;
myUnit = timeUnit;
}

public final long getInterval(final TimeUnit timeUnit)
{
return timeUnit.convert(myTime, myUnit);
}

@JsonProperty("time")
public final long getTime()
{
return myTime;
}

@JsonProperty("time")
public final void setTime(final long time)
{
myTime = time;
}

@JsonProperty("unit")
public final TimeUnit getUnit()
{
return myUnit;
}

@JsonProperty("unit")
public final void setUnit(final String unit)
{
myUnit = TimeUnit.valueOf(unit.toUpperCase(Locale.US));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public String getLocalDatacenter()
return myLocalDatacenter;
}


/**
* Gets the DataCenterAwarePolicy used for load-balancing policy.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
*/
package com.ericsson.bss.cassandra.ecchronos.application.config.connection;

import com.ericsson.bss.cassandra.ecchronos.application.config.Interval;
import com.fasterxml.jackson.annotation.JsonProperty;

public class ConnectionConfig
{
private DistributedNativeConnection myCqlConnection = new DistributedNativeConnection();
private DistributedJmxConnection myJmxConnection = new DistributedJmxConnection();
private Interval myConnectionDelay = new Interval();

@JsonProperty("cql")
public final DistributedNativeConnection getCqlConnection()
Expand Down Expand Up @@ -56,4 +58,26 @@ public final String toString()
{
return String.format("Connection(cql=%s, jmx=%s)", myCqlConnection, myJmxConnection);
}
/**
* Sets the connectionDelay used to specify the time until the next connection.
*
* @param connectionDelay
* the local datacenter to set.
*/
@JsonProperty("connectionDelay")
public void setConnectionDelay(final Interval connectionDelay)
{
myConnectionDelay = connectionDelay;
}
/**
* Gets the connectionDelay used to specify the time until the next connection.
*
* @return the connectionDelay.
*/
@JsonProperty("connectionDelay")
public Interval getConnectionDelay()
{
return myConnectionDelay;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package com.ericsson.bss.cassandra.ecchronos.application.spring;

import com.ericsson.bss.cassandra.ecchronos.application.config.Interval;
import com.ericsson.bss.cassandra.ecchronos.application.config.security.CqlTLSConfig;
import com.ericsson.bss.cassandra.ecchronos.application.config.security.ReloadingCertificateHandler;
import com.ericsson.bss.cassandra.ecchronos.application.providers.AgentJmxConnectionProvider;
Expand Down Expand Up @@ -178,11 +179,13 @@ public DistributedNativeConnectionProvider distributedNativeConnectionProvider(
* if the local host name cannot be determined.
* @throws EcChronosException
* if there is an error during node synchronization.
* @throws ConfigurationException
* if there is an error during node synchronization.
*/
@Bean
public EccNodesSync eccNodesSync(
final DistributedNativeConnectionProvider distributedNativeConnectionProvider
) throws UnknownHostException, EcChronosException
) throws UnknownHostException, EcChronosException, ConfigurationException
{
return getEccNodesSync(distributedNativeConnectionProvider);
}
Expand Down Expand Up @@ -264,12 +267,15 @@ private static CertificateHandler createCertificateHandler(

private EccNodesSync getEccNodesSync(
final DistributedNativeConnectionProvider distributedNativeConnectionProvider
) throws UnknownHostException, EcChronosException
) throws UnknownHostException, EcChronosException, ConfigurationException
{
Interval connectionDelay = config().getConnectionConfig().getConnectionDelay();
EccNodesSync myEccNodesSync = EccNodesSync.newBuilder()
.withInitialNodesList(distributedNativeConnectionProvider.getNodes())
.withSession(distributedNativeConnectionProvider.getCqlSession())
.withEcchronosID(ecChronosID)
.withConnectionDelayValue(connectionDelay.getTime())
.withConnectionDelayUnit(connectionDelay.getUnit())
.build();
myEccNodesSync.acquireNodes();
LOG.info("Nodes acquired with success");
Expand Down
7 changes: 7 additions & 0 deletions application/src/main/resources/ecc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ connection:
## Extending this allows to manipulate the SSLEngine and SSLParameters.
##
certificateHandler: com.ericsson.bss.cassandra.ecchronos.application.config.security.ReloadingCertificateHandler
##
VictorCavichioli marked this conversation as resolved.
Show resolved Hide resolved
## Specify the interval until the next connection to a node
## Unit can be SECONDS, MINUTES, HOURS, DAYS
##
connectionDelay:
time: 45
unit: MINUTES
jmx:
##
## The class used to provide JMX connections to Apache Cassandra.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -156,4 +157,13 @@ public void testDefaultLoadBalancingPolicy()
{
assertThat(nativeConnection.getAgentConnectionConfig().getDatacenterAwarePolicy()).isEqualTo(DataCenterAwarePolicy.class);
}

@Test
public void testConnectionDelay()
{
Interval connectionDelay = config.getConnectionConfig().getConnectionDelay();
assertThat(connectionDelay.getUnit()).isEqualTo(TimeUnit.MINUTES);
assertThat(connectionDelay.getTime()).isEqualTo(45l);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.application.config;

import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;


public class TestMissingConnectionDelay
VictorCavichioli marked this conversation as resolved.
Show resolved Hide resolved
{
private static final String DEFAULT_AGENT_FILE_NAME = "missing_connection_delay.yml";
VictorCavichioli marked this conversation as resolved.
Show resolved Hide resolved
private static Config config;


@Before
public void setup() throws StreamReadException, DatabindException, IOException
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

File file = new File(classLoader.getResource(DEFAULT_AGENT_FILE_NAME).getFile());

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());

config = objectMapper.readValue(file, Config.class);

}


@Test
public void testConnectionDelay()
{
Interval connectionDelay = config.getConnectionConfig().getConnectionDelay();
assertThat(connectionDelay.getUnit()).isEqualTo(TimeUnit.MINUTES);
assertThat(connectionDelay.getTime()).isEqualTo(60l);
}

}
3 changes: 3 additions & 0 deletions application/src/test/resources/all_set.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ connection:
- host: 127.0.0.4
port: 9042
provider: com.ericsson.bss.cassandra.ecchronos.application.providers.AgentNativeConnectionProvider
connectionDelay:
time: 45
unit: minutes
jmx:

rest_server:
Expand Down
49 changes: 49 additions & 0 deletions application/src/test/resources/missing_connection_delay.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# 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.
#

VictorCavichioli marked this conversation as resolved.
Show resolved Hide resolved
connection:
cql:
agent:
type: datacenterAware
localDatacenter: datacenter1
datacenterAwarePolicy: com.ericsson.bss.cassandra.ecchronos.connection.DataCenterAwarePolicy
contactPoints:
- host: 127.0.0.1
port: 9042
- host: 127.0.0.2
port: 9042
datacenterAware:
datacenters:
- name: datacenter1
rackAware:
racks:
- datacenterName: datacenter1
rackName: rack1
hostAware:
hosts:
- host: 127.0.0.1
port: 9042
- host: 127.0.0.2
port: 9042
- host: 127.0.0.3
port: 9042
- host: 127.0.0.4
port: 9042
provider: com.ericsson.bss.cassandra.ecchronos.application.providers.AgentNativeConnectionProvider
jmx:

rest_server:
host: 127.0.0.2
port: 8081
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -66,6 +68,8 @@ public final class EccNodesSync

private final PreparedStatement myCreateStatement;
private final PreparedStatement myUpdateStatusStatement;
private final Long connectionDelayValue;
private final ChronoUnit connectionDelayUnit;

private EccNodesSync(final Builder builder) throws UnknownHostException
{
Expand All @@ -92,6 +96,9 @@ private EccNodesSync(final Builder builder) throws UnknownHostException
.build()
.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM));
ecChronosID = builder.myEcchronosID;

connectionDelayValue = builder.myConnectionDelayValue;
connectionDelayUnit = builder.myConnectionDelayUnit;
}

public void acquireNodes() throws EcChronosException
Expand Down Expand Up @@ -126,7 +133,7 @@ private ResultSet acquireNode(final Node node)
node.getEndPoint().toString(),
node.getState().toString(),
Instant.now(),
Instant.now().plus(DEFAULT_CONNECTION_DELAY_IN_MINUTES, ChronoUnit.MINUTES),
Instant.now().plus(connectionDelayValue, connectionDelayUnit),
node.getHostId());
}

Expand Down Expand Up @@ -220,6 +227,8 @@ public static class Builder
private CqlSession mySession;
private List<Node> initialNodesList;
private String myEcchronosID;
private Long myConnectionDelayValue;
private ChronoUnit myConnectionDelayUnit;

/**
* Builds EccNodesSync with session.
Expand Down Expand Up @@ -247,6 +256,32 @@ public Builder withInitialNodesList(final List<Node> nodes)
return this;
}

/**
* Builds EccNodesSync with Connection Delay.
*
* @param connectionDelayValue
* delay before connecting, in the unit specified in withConnectionDelayUnit
* @return Builder
*/
VictorCavichioli marked this conversation as resolved.
Show resolved Hide resolved
public Builder withConnectionDelayValue(final Long connectionDelayValue)
{
this.myConnectionDelayValue = connectionDelayValue;
return this;
}

/**
* Builds EccNodesSync with Connection Delay Unit.
*
* @param connectionDelayUnit
* Unit of the delay before connecting
* @return Builder
*/
public Builder withConnectionDelayUnit(final TimeUnit connectionDelayUnit)
{
this.myConnectionDelayUnit = ChronoUnit.valueOf(connectionDelayUnit.toString().toUpperCase(Locale.US));
return this;
}

/**
* Builds EccNodesSync with ecchronosID.
*
Expand Down
Loading