Skip to content

Commit

Permalink
Merge pull request #12 from diegopacheco/dev
Browse files Browse the repository at this point in the history
Cache Connections
  • Loading branch information
diegopacheco authored Aug 22, 2017
2 parents 4fbf55b + 04da576 commit 25c9821
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 76 deletions.
32 changes: 20 additions & 12 deletions dynomite-cluster-checker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ applicationDefaultJvmArgs = [
"-Djava.net.preferIPv4Stack=true",
"-Djava.net.preferIPv6Addresses=false",
"-server",
"-Xmx2048m",
"-Xms128m",
"-Xmx4096m",
"-Xms1024m",
]

run {
Expand All @@ -23,6 +23,7 @@ run {
}
}


repositories {
jcenter()
mavenLocal()
Expand All @@ -40,11 +41,18 @@ buildscript {
apply plugin: 'org.akhikhl.gretty'

gretty {
jvmArgs = [
'-Xms256m',
'-Xmx2048m',
'-XX:-HeapDumpOnOutOfMemoryError',
'-XX:ErrorFile=./hs_err_pid%p.log']
jvmArgs = ['-Xms1024m',
'-Xmx4096m',
'-XX:-HeapDumpOnOutOfMemoryError',
'-XX:ErrorFile=./hs_err_pid%p.log']

if (System.getenv('ENABLE_JMX') == 'true') {
jvmArgs = jvmArgs + ['-Dcom.sun.management.jmxremote',
'-Dcom.sun.management.jmxremote.port=7094',
'-Dcom.sun.management.jmxremote.ssl=false',
'-Dcom.sun.management.jmxremote.authenticate=false']
}

httpPort = 7766
}

Expand All @@ -59,11 +67,11 @@ dependencies {
testCompile('junit:junit:4.11')

compile([
'javax.servlet:servlet-api:2.5',
'org.apache.commons:commons-lang3:3.4',
'org.slf4j:slf4j-simple:1.7.21',
'com.google.inject:guice:4.1.0'
])
'javax.servlet:servlet-api:2.5',
'org.apache.commons:commons-lang3:3.4',
'org.slf4j:slf4j-simple:1.7.21',
'com.google.inject:guice:4.1.0'
])

compile('com.netflix.dyno:dyno-jedis:1.5.8-rc.4'){
exclude group: 'org.slf4j', module: 'slf4j-api'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static DynoJedisClient createCluster(String clusterName,final List<Dynomi
new ArchaiusConnectionPoolConfiguration(clusterName)
.withTokenSupplier(TokenMapSupplierFactory.build(nodes))
.setMaxConnsPerHost(1)
//.setConnectTimeout(5000)
.setRetryPolicyFactory(new RetryNTimes.RetryFactory(1,true))
)
.withHostSupplier(HostSupplierFactory.build(nodes))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.diegopacheco.dynomite.cluster.checker.cluster;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -10,7 +9,6 @@
import com.netflix.dyno.connectionpool.Host;
import com.netflix.dyno.connectionpool.TokenMapSupplier;
import com.netflix.dyno.connectionpool.impl.lb.AbstractTokenMapSupplier;
import com.netflix.dyno.connectionpool.impl.lb.HostToken;

/**
* TokenMapSupplierFactory creates TokenMapSupplier baased on DynomiteNodeInfo.
Expand Down Expand Up @@ -61,19 +59,6 @@ public String getTopologyJsonPayload(String hostname) {
public String getTopologyJsonPayload(Set<Host> activeHosts) {
return json;
}
// @Override
// public List<HostToken> getTokens(Set<Host> activeHosts) {
// List<HostToken> tokens = new ArrayList<>();
// for(DynomiteNodeInfo node: nodes){
// tokens.add(new HostToken(new Long(node.getTokens()), node.toHOST()));
// }
// return tokens;
// }
// @Override
// public HostToken getTokenForHost(Host host, Set<Host> activeHosts) {
// DynomiteNodeInfo dni = mapNodes.get(host.getHostName());
// return new HostToken(new Long(dni.getTokens()), dni.toHOST());
// }
};
return testTokenMapSupplier;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.diegopacheco.dynomite.cluster.checker.cluster.cache;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import org.apache.log4j.Logger;

import com.github.diegopacheco.dynomite.cluster.checker.cluster.DCCConnectionManager;
import com.github.diegopacheco.dynomite.cluster.checker.parser.DynomiteNodeInfo;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import com.netflix.dyno.jedis.DynoJedisClient;

@Singleton
public class ClientCache {

private static Cache<String, DynoJedisClient> cache;
private static final Logger logger = Logger.getLogger(ClientCache.class);

static {
cache = CacheBuilder.newBuilder().
maximumSize(500).
expireAfterAccess(2, TimeUnit.HOURS).
removalListener(new RemovalListener<String, DynoJedisClient>() {
@Override
public void onRemoval(RemovalNotification<String, DynoJedisClient> notification) {
logger.debug("Removing... " + notification.getKey());
try {
notification.getValue().stopClient();
} catch (Exception e) {
logger.error("Error on close evicted client. EX: " + e);
}
}
}).build();
}

public static DynoJedisClient get(String seeds) {
try {
return cache.getIfPresent(seeds);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static void put(String seeds, DynoJedisClient client) {
cache.put(seeds, client);
}

public static void main(String[] args) {
String seeds = "127.0.0.1:8102:rack1:dc:100";
DynoJedisClient client = DCCConnectionManager.createSingleNodeCluster("test1",new DynomiteNodeInfo("127.0.0.1", "8102", "rack1", "dc", "100"));

ClientCache.put(seeds, client);
System.out.println(ClientCache.get(seeds));
System.out.println(ClientCache.get(seeds));
System.out.println(ClientCache.get(seeds));

String seeds2 = "127.0.0.1:8102:rack1:dc:0";
ClientCache.put(seeds2, client);
System.out.println(ClientCache.get(seeds2));
System.out.println(ClientCache.get(seeds));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.diegopacheco.dynomite.cluster.checker.main;

import org.apache.log4j.Logger;

import com.github.diegopacheco.dynomite.cluster.checker.tasks.engine.DCCTaskExecutionEngine;
import com.github.diegopacheco.dynomite.cluster.checker.util.Chronometer;

Expand All @@ -13,7 +15,9 @@
*
*/
public class DynomiteClusterCheckerMain {


private static final Logger logger = Logger.getLogger(DynomiteClusterCheckerMain.class);

public static void main(String[] args) {
Chronometer stopWatch = new Chronometer();
try {
Expand All @@ -24,14 +28,14 @@ public static void main(String[] args) {
DCCTaskExecutionEngine dcc = new DCCTaskExecutionEngine();
String jsonResult = dcc.run(args[0], isTelemetryMode);

System.out.println(jsonResult);
logger.info(jsonResult);
}catch(Exception e){
System.out.println("Error: " + e);
logger.error("Error: " + e);
} finally {
stopWatch.stop();

System.out.println("--");
System.out.println(stopWatch.getDiffAsSecondsString());
logger.info("--");
logger.info(stopWatch.getDiffAsSecondsString());

System.exit(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public Host toHOST(){
//return new Host(this.getServer(),this.getServer(),new Integer(this.getPort()),this.getRack(),this.getDc(),Status.Up);
return new Host(this.getServer(),8102,this.getRack(),Status.Up);
}

public String toSeed(){
return getServer() + ":" + getPort() + ":" + getRack() + ":" + getDc() + ":" + getTokens();
}

public String getServer() {
return server;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
@SuppressWarnings("serial")
public class RestServlet extends HttpServlet {

private Logger logger = Logger.getLogger(RestServlet.class);

private static final Logger logger = Logger.getLogger(RestServlet.class);
private static DCCTaskExecutionEngine dccEngine = new DCCTaskExecutionEngine();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
Expand All @@ -31,7 +32,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
boolean shouldRunTelemetryMode = resolveTelemetryMode(req.getParameterValues("telemetry"));
logArgs(req);

String json = new DCCTaskExecutionEngine().run(seeds, shouldRunTelemetryMode);
String json = dccEngine.run(seeds, shouldRunTelemetryMode);
out.write(json);

}catch(Exception e){
Expand All @@ -45,12 +46,12 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
@SuppressWarnings("unchecked")
private void logArgs(HttpServletRequest req){
logger.info("Checking seeds: " + req.getParameter("seeds"));
logger.info("TELEMETRY mode: " + req.getParameterValues("telemetry"));
logger.debug("TELEMETRY mode: " + req.getParameterValues("telemetry"));

Enumeration<String> parameterNames = req.getParameterNames();
while(parameterNames.hasMoreElements()){
String paramName = parameterNames.nextElement();
logger.info("Other parameter: " + paramName);
logger.debug("Other parameter: " + paramName);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.diegopacheco.dynomite.cluster.checker.tasks;

import org.apache.log4j.Logger;

import com.github.diegopacheco.dynomite.cluster.checker.context.ExecutionContext;
import com.github.diegopacheco.dynomite.cluster.checker.context.NodeCheckerResponse;
import com.github.diegopacheco.dynomite.cluster.checker.util.Chronometer;
Expand All @@ -13,6 +15,8 @@
*/
public class CheckClusterFailoverTask implements Task{

private static final Logger logger = Logger.getLogger(CheckClusterFailoverTask.class);

@Override
public void execute(ExecutionContext ec) {

Expand Down Expand Up @@ -45,7 +49,7 @@ public void execute(ExecutionContext ec) {


}catch(Throwable t){
System.out.println("Cloud not insert data into the cluster. EX: " + t);
logger.error("Cloud not insert data into the cluster. EX: " + t);
nodeReport.setConsistency(false);
nodeReport.setGetError(t.getMessage());
ec.getExecutionReport().setFailoverStatus("Error! EX: " + t.getMessage() + " - info: " + nodeReport.toString() );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.diegopacheco.dynomite.cluster.checker.tasks;

import org.apache.log4j.Logger;

import com.github.diegopacheco.dynomite.cluster.checker.context.ExecutionContext;
import com.github.diegopacheco.dynomite.cluster.checker.context.NodeCheckerResponse;
import com.github.diegopacheco.dynomite.cluster.checker.parser.DynomiteNodeInfo;
Expand All @@ -14,12 +16,12 @@
*/
public class CheckDataReplicationTask implements Task {

private static final Logger logger = Logger.getLogger(CheckDataReplicationTask.class);

@Override
public void execute(ExecutionContext ec) {

insertKey(ec);
getKeys(ec);

}

private void getKeys(ExecutionContext ec) {
Expand All @@ -44,7 +46,7 @@ private void getKeys(ExecutionContext ec) {
}

}catch(Throwable t){
System.out.println("Could not get KEY on node : " + node + " - EX: " + t);
logger.error("Could not get KEY on node : " + node + " - EX: " + t);
nodeReport.setGetError(t.getMessage());
}finally{
stopWatch.stop();
Expand All @@ -71,7 +73,7 @@ private void insertKey(ExecutionContext ec) {
nodeReport.setConsistency(true);

}catch(Throwable t){
System.out.println("Cloud not insert data into the cluster. EX: " + t);
logger.error("Cloud not insert data into the cluster. EX: " + t);
nodeReport.setConsistency(false);
nodeReport.setInsertError(t.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.github.diegopacheco.dynomite.cluster.checker.cluster.DCCConnectionManager;
import com.github.diegopacheco.dynomite.cluster.checker.cluster.cache.ClientCache;
import com.github.diegopacheco.dynomite.cluster.checker.context.ExecutionContext;
import com.github.diegopacheco.dynomite.cluster.checker.parser.DynomiteNodeInfo;
import com.github.diegopacheco.dynomite.cluster.config.DynomiteConfig;
Expand All @@ -18,6 +21,8 @@
*/
public class CheckNodesConnectivityTask implements Task {

private static final Logger logger = Logger.getLogger(CheckNodesConnectivityTask.class);

@Override
public void execute(ExecutionContext ec) {

Expand All @@ -34,13 +39,18 @@ public void execute(ExecutionContext ec) {
private void connectWholeCluster(ExecutionContext ec) {
try {

DynoJedisClient client = DCCConnectionManager.createCluster(DynomiteConfig.CLUSTER_NAME,ec.getOnlineNodes());
DynoJedisClient client = ClientCache.get(ec.getRawSeeds());
if (client==null){
client = DCCConnectionManager.createCluster(DynomiteConfig.CLUSTER_NAME,ec.getOnlineNodes());
ClientCache.put(ec.getRawSeeds(), client);
}

String prefix = "awesomeSbrubles_";
client.get(prefix);
ec.setWholeClusterClient(client);

} catch (Throwable t) {
System.out.println("Could not Connet on Whole cluster : " + ec.getOnlineNodes() + " EX: " + t);
logger.error("Could not Connet on Whole cluster : " + ec.getOnlineNodes() + " EX: " + t);
} finally {
}
}
Expand All @@ -50,15 +60,20 @@ private List<DynomiteNodeInfo> checkNodeConnectivity(ExecutionContext ec) {
for (DynomiteNodeInfo node : ec.getOriginalNodes()) {
try {

DynoJedisClient client = DCCConnectionManager.createSingleNodeCluster(DynomiteConfig.CLUSTER_NAME,node);
DynoJedisClient client = ClientCache.get(node.toSeed());
if (client==null){
client = DCCConnectionManager.createSingleNodeCluster(DynomiteConfig.CLUSTER_NAME,node);
ClientCache.put(node.toSeed(), client);
}

String prefix = "awesomeSbrubles_";
client.get(prefix);

node.setNodeClient(client);
onlineNodes.add(node);

} catch (Throwable t) {
System.out.println("Could not Connet on Node: " + node + " EX: " + t);
logger.error("Could not Connet on Node: " + node + " EX: " + t);
} finally {
}
}
Expand Down
Loading

0 comments on commit 25c9821

Please sign in to comment.