Skip to content

Commit

Permalink
Retry Policy for Jmx Connection Ericsson#700
Browse files Browse the repository at this point in the history
  • Loading branch information
sajid riaz committed Sep 3, 2024
1 parent 957b09f commit fb5b239
Show file tree
Hide file tree
Showing 12 changed files with 487 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ public class ConnectionConfig
private DistributedNativeConnection myCqlConnection = new DistributedNativeConnection();
private DistributedJmxConnection myJmxConnection = new DistributedJmxConnection();

@JsonProperty("cql")
@JsonProperty ("cql")
public final DistributedNativeConnection getCqlConnection()
{
return myCqlConnection;
}

@JsonProperty("jmx")
@JsonProperty ("jmx")
public final DistributedJmxConnection getJmxConnection()
{
return myJmxConnection;
}

@JsonProperty("cql")
@JsonProperty ("cql")
public final void setCqlConnection(final DistributedNativeConnection cqlConnection)
{
if (cqlConnection != null)
Expand All @@ -42,7 +42,7 @@ public final void setCqlConnection(final DistributedNativeConnection cqlConnecti
}
}

@JsonProperty("jmx")
@JsonProperty ("jmx")
public final void setJmxConnection(final DistributedJmxConnection jmxConnection)
{
if (jmxConnection != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@

import com.ericsson.bss.cassandra.ecchronos.connection.DistributedNativeConnectionProvider;
import com.ericsson.bss.cassandra.ecchronos.data.sync.EccNodesSync;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.function.Supplier;

public class DistributedJmxConnection extends Connection<DistributedJmxConnectionProvider>
{
private RetryPolicyConfig myRetryPolicyConfig = new RetryPolicyConfig();

public DistributedJmxConnection()
{
try
Expand All @@ -35,16 +39,28 @@ public DistributedJmxConnection()
}
}

@JsonProperty ("retryPolicy")
public final RetryPolicyConfig getRetryPolicyConfig()
{
return myRetryPolicyConfig;
}

@JsonProperty ("retryPolicy")
public final void setRetryPolicyConfig(final RetryPolicyConfig retryPolicyConfig)
{
myRetryPolicyConfig = retryPolicyConfig;
}

/**
* @return
*/
@Override
protected Class<?>[] expectedConstructor()
{
return new Class<?>[] {
Supplier.class,
DistributedNativeConnectionProvider.class,
EccNodesSync.class
Supplier.class,
DistributedNativeConnectionProvider.class,
EccNodesSync.class
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.connection;

import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.annotation.JsonProperty;

public final class RetryPolicyConfig
{

private static final int DEFAULT_MAX_ATTEMPTS = 5;
private static final long DEFAULT_DELAY = 5000;
private static final long DEFAULT_MAX_DELAY = 30000;

@JsonProperty ("maxAttempts")
private Integer myMaxAttempts = DEFAULT_MAX_ATTEMPTS;

@JsonProperty ("delay")
private long myDelay = DEFAULT_DELAY;

@JsonProperty ("maxDelay")
private long myMaxDelay = DEFAULT_MAX_DELAY;

@JsonProperty ("unit")
private String myUnit = "seconds"; // Default to seconds

public RetryPolicyConfig()
{
}

public RetryPolicyConfig(final Integer maxAttempts, final Integer delay, final Integer maxDelay, final String unit)
{
this.myMaxAttempts = maxAttempts;
this.myDelay = convertToMillis(delay, unit);
this.myMaxDelay = convertToMillis(maxDelay, unit);
this.myUnit = unit;
}

@JsonProperty ("maxAttempts")
public Integer getMaxAttempts()
{
return myMaxAttempts;
}

@JsonProperty ("maxAttempts")
public void setMaxAttempts(final Integer maxAttempts)
{
this.myMaxAttempts = maxAttempts;
}

@JsonProperty ("delay")
public long getDelay()
{
return myDelay;
}

@JsonProperty ("delay")
public void setDelay(final Integer delay)
{
this.myDelay = convertToMillis(delay, myUnit);
}

@JsonProperty ("maxDelay")
public long getMaxDelay()
{
return myMaxDelay;
}

@JsonProperty ("maxDelay")
public void setMaxDelay(final Integer maxDelay)
{
this.myMaxDelay = convertToMillis(maxDelay, myUnit);
}

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

@JsonProperty ("unit")
public void setUnit(final String unit)
{
this.myUnit = unit;
// Recalculate delays with the new unit
this.myDelay = convertToMillis((int) TimeUnit.MILLISECONDS.toSeconds(this.myDelay), unit);
this.myMaxDelay = convertToMillis((int) TimeUnit.MILLISECONDS.toSeconds(this.myMaxDelay), unit);
}

private long convertToMillis(final Integer value, final String unit)
{
return switch (unit.toLowerCase())
{
case "milliseconds" -> value;
case "seconds" -> TimeUnit.SECONDS.toMillis(value);
case "minutes" -> TimeUnit.MINUTES.toMillis(value);
default -> throw new IllegalArgumentException("Unsupported time unit: " + unit);
};
}

public long currentDelay(final Integer count)
{
long currentDelay = myDelay * count;
if (currentDelay > myMaxDelay)
{
currentDelay = myMaxDelay;
}
return currentDelay;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ public DistributedJmxConnectionProvider distributedJmxConnectionProvider(
jmxSecurity::get, distributedNativeConnectionProvider, eccNodesSync);
}

@Bean
public RetrySchedulerService retrySchedulerService(final Config config,
final DistributedJmxConnectionProvider jmxConnectionProvider,
final EccNodesSync eccNodesSync,
final DistributedNativeConnectionProvider distributedNativeConnectionProvider)
{
return new RetrySchedulerService(eccNodesSync, config, jmxConnectionProvider, distributedNativeConnectionProvider);
}

private Security getSecurityConfig() throws ConfigurationException
{
return ConfigurationHelper.DEFAULT_INSTANCE.getConfiguration(SECURITY_FILE, Security.class);
Expand Down
Loading

0 comments on commit fb5b239

Please sign in to comment.