Skip to content

Commit

Permalink
Merge branch 'agent/674' of https://github.com/paulchandler/ecchronos
Browse files Browse the repository at this point in the history
…into agent/674
  • Loading branch information
Paul Chandler committed Sep 10, 2024
2 parents def5330 + 18af043 commit 0c012be
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 4 deletions.
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 @@ -273,12 +276,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
##
## 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.assertEquals;
Expand Down Expand Up @@ -236,4 +237,12 @@ public void testMaxDelayLessThanStartDelayThrowsException()
});
assertEquals("Max delay cannot be less than start delay.", exception.getMessage());
}
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,60 @@
/*
* 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 TestDefaultConfig
{
private static final String DEFAULT_AGENT_FILE_NAME = "nothing_set.yml";
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 testConnectionDelayDefault()
{
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:
provider: com.ericsson.bss.cassandra.ecchronos.application.providers.AgentJmxConnectionProvider
retryPolicy:
Expand Down
2 changes: 1 addition & 1 deletion application/src/test/resources/nothing_set.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ connection:
retrySchedule:
initialDelay:
fixedDelay:
unit:
unit:
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 @@ -67,6 +69,8 @@ public final class EccNodesSync
private final PreparedStatement myCreateStatement;
private final PreparedStatement myUpdateStatusStatement;
private final PreparedStatement mySelectStatusStatement;
private final Long connectionDelayValue;
private final ChronoUnit connectionDelayUnit;

private EccNodesSync(final Builder builder) throws UnknownHostException
{
Expand Down Expand Up @@ -97,6 +101,9 @@ private EccNodesSync(final Builder builder) throws UnknownHostException
.whereColumn(COLUMN_ECCHRONOS_ID).isEqualTo(bindMarker())
.build());
ecChronosID = builder.myEcchronosID;

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

public ResultSet getResultSet()
Expand Down Expand Up @@ -138,7 +145,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 @@ -232,6 +239,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 @@ -259,6 +268,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
*/
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


import static org.junit.Assert.*;
Expand Down Expand Up @@ -72,6 +73,8 @@ public void setup() throws IOException
eccNodesSync = EccNodesSync.newBuilder()
.withSession(mySession)
.withInitialNodesList(nodesList)
.withConnectionDelayValue(Long.valueOf(10))
.withConnectionDelayUnit(TimeUnit.MINUTES)
.withEcchronosID("ecchronos-test").build();
}

Expand Down

0 comments on commit 0c012be

Please sign in to comment.